summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/webkit/WebCore/generated/JSSVGPointList.cpp
Commit message (Expand)AuthorAgeFilesLines
* Update src/3rdparty/webkit from trunk.Jocelyn Turcotte2010-04-061-23/+85
* Updated WebKit from /home/jturcott/dev/webkit/ to qtwebkit-4.6-snapshot-20091...Jocelyn Turcotte2009-11-031-1/+1
* Updated WebKit from /home/joce/dev/qtwebkit/ to qtwebkit-4.6-snapshot-3009200...Jocelyn Turcotte2009-10-011-1/+1
* Updated WebKit from /home/joce/dev/qtwebkit3/ to qtwebkit-4.6-snapshot-240920...Jocelyn Turcotte2009-09-241-1/+1
* Updated WebKit from /home/joce/dev/qtwebkit/ to qtwebkit-4.6-snapshot-1809200...Jocelyn Turcotte2009-09-181-8/+18
* Updated WebKit from /home/shausman/src/webkit/trunk to qtwebkit-4.6-snapshot-...Simon Hausmann2009-07-291-6/+6
* Updated WebKit from /home/shausman/src/webkit/trunk to qtwebkit-4.6-snapshot-...Simon Hausmann2009-07-131-2/+2
* Updated WebKit from /home/shausman/src/webkit/trunk to qtwebkit-4.6-snapshot-...Simon Hausmann2009-06-241-5/+1
* Updated WebKit from /home/shausman/src/webkit/trunk to qtwebkit-4.6-snapshot-...Simon Hausmann2009-06-151-24/+31
* Long live Qt!Lars Knoll2009-03-231-0/+188
w-r--r--Doc/c-api/number.rst17
-rw-r--r--Doc/c-api/sys.rst54
-rw-r--r--Doc/c-api/typeobj.rst3
-rw-r--r--Doc/c-api/unicode.rst35
-rw-r--r--Doc/data/refcounts.dat5
-rw-r--r--Doc/distutils/apiref.rst4
-rw-r--r--Doc/distutils/builtdist.rst2
-rw-r--r--Doc/extending/embedding.rst20
-rw-r--r--Doc/extending/extending.rst12
-rw-r--r--Doc/glossary.rst20
-rw-r--r--Doc/howto/clinic.rst8
-rw-r--r--Doc/howto/logging-cookbook.rst11
-rw-r--r--Doc/howto/regex.rst2
-rw-r--r--Doc/howto/sockets.rst2
-rw-r--r--Doc/howto/unicode.rst14
-rw-r--r--Doc/includes/run-func.c2
-rw-r--r--Doc/install/index.rst2
-rw-r--r--Doc/library/argparse.rst37
-rw-r--r--Doc/library/asynchat.rst34
-rw-r--r--Doc/library/asyncio-eventloops.rst6
-rw-r--r--Doc/library/cgi.rst7
-rw-r--r--Doc/library/cmd.rst2
-rw-r--r--Doc/library/code.rst5
-rw-r--r--Doc/library/codecs.rst41
-rw-r--r--Doc/library/collections.abc.rst2
-rw-r--r--Doc/library/collections.rst5
-rw-r--r--Doc/library/compileall.rst54
-rw-r--r--Doc/library/concurrent.futures.rst25
-rw-r--r--Doc/library/configparser.rst122
-rw-r--r--Doc/library/constants.rst1
-rw-r--r--Doc/library/contextlib.rst10
-rw-r--r--Doc/library/copyreg.rst2
-rw-r--r--Doc/library/csv.rst1
-rw-r--r--Doc/library/datetime.rst11
-rw-r--r--Doc/library/dbm.rst9
-rw-r--r--Doc/library/decimal.rst15
-rw-r--r--Doc/library/difflib.rst16
-rw-r--r--Doc/library/dis.rst26
-rw-r--r--Doc/library/doctest.rst12
-rw-r--r--Doc/library/email.mime.rst6
-rw-r--r--Doc/library/enum.rst22
-rw-r--r--Doc/library/formatter.rst2
-rw-r--r--Doc/library/functions.rst30
-rw-r--r--Doc/library/gc.rst6
-rw-r--r--Doc/library/gettext.rst6
-rw-r--r--Doc/library/glob.rst24
-rw-r--r--Doc/library/heapq.rst14
-rw-r--r--Doc/library/html.parser.rst47
-rw-r--r--Doc/library/http.client.rst212
-rw-r--r--Doc/library/http.rst113
-rw-r--r--Doc/library/http.server.rst2
-rw-r--r--Doc/library/imaplib.rst13
-rw-r--r--Doc/library/imghdr.rst10
-rw-r--r--Doc/library/importlib.rst77
-rw-r--r--Doc/library/inspect.rst46
-rw-r--r--Doc/library/io.rst50
-rw-r--r--Doc/library/ipaddress.rst36
-rw-r--r--Doc/library/itertools.rst38
-rw-r--r--Doc/library/json.rst103
-rw-r--r--Doc/library/locale.rst8
-rw-r--r--Doc/library/logging.handlers.rst15
-rw-r--r--Doc/library/logging.rst13
-rw-r--r--Doc/library/lzma.rst37
-rw-r--r--Doc/library/math.rst16
-rw-r--r--Doc/library/multiprocessing.rst3
-rw-r--r--Doc/library/operator.rst18
-rw-r--r--Doc/library/os.rst46
-rw-r--r--Doc/library/pathlib.rst112
-rw-r--r--Doc/library/pickle.rst2
-rw-r--r--Doc/library/pkgutil.rst4
-rw-r--r--Doc/library/plistlib.rst2
-rw-r--r--Doc/library/random.rst3
-rw-r--r--Doc/library/re.rst73
-rw-r--r--Doc/library/readline.rst29
-rw-r--r--Doc/library/resource.rst2
-rw-r--r--Doc/library/select.rst2
-rw-r--r--Doc/library/selectors.rst11
-rw-r--r--Doc/library/shutil.rst52
-rw-r--r--Doc/library/signal.rst13
-rw-r--r--Doc/library/site.rst20
-rw-r--r--Doc/library/smtpd.rst60
-rw-r--r--Doc/library/smtplib.rst39
-rw-r--r--Doc/library/sndhdr.rst15
-rw-r--r--Doc/library/socket.rst32
-rw-r--r--Doc/library/socketserver.rst4
-rw-r--r--Doc/library/ssl.rst273
-rw-r--r--Doc/library/stat.rst28
-rw-r--r--Doc/library/stdtypes.rst193
-rw-r--r--Doc/library/subprocess.rst1
-rw-r--r--Doc/library/sys.rst8
-rw-r--r--Doc/library/tarfile.rst45
-rw-r--r--Doc/library/tempfile.rst7
-rw-r--r--Doc/library/threading.rst2
-rw-r--r--Doc/library/time.rst4
-rw-r--r--Doc/library/timeit.rst36
-rw-r--r--Doc/library/tkinter.ttk.rst2
-rw-r--r--Doc/library/token.rst1
-rw-r--r--Doc/library/tracemalloc.rst2
-rw-r--r--Doc/library/turtle.rst6
-rw-r--r--Doc/library/types.rst4
-rw-r--r--Doc/library/unicodedata.rst8
-rw-r--r--Doc/library/unittest.mock.rst118
-rw-r--r--Doc/library/unittest.rst91
-rw-r--r--Doc/library/urllib.parse.rst5
-rw-r--r--Doc/library/urllib.request.rst16
-rw-r--r--Doc/library/weakref.rst10
-rw-r--r--Doc/library/webbrowser.rst2
-rw-r--r--Doc/library/wsgiref.rst9
-rw-r--r--Doc/library/xml.dom.rst2
-rw-r--r--Doc/library/xmlrpc.client.rst27
-rw-r--r--Doc/library/zipfile.rst10
-rw-r--r--Doc/make.bat17
-rw-r--r--Doc/reference/datamodel.rst60
-rw-r--r--Doc/reference/expressions.rst17
-rw-r--r--Doc/reference/import.rst20
-rw-r--r--Doc/reference/lexical_analysis.rst8
-rw-r--r--Doc/reference/simple_stmts.rst2
-rw-r--r--Doc/tools/extensions/pyspecific.py2
-rw-r--r--Doc/tools/susp-ignored.csv16
-rw-r--r--Doc/tools/templates/indexsidebar.html3
-rw-r--r--Doc/tutorial/appendix.rst4
-rw-r--r--Doc/tutorial/datastructures.rst5
-rw-r--r--Doc/tutorial/interpreter.rst12
-rw-r--r--Doc/tutorial/stdlib.rst2
-rw-r--r--Doc/tutorial/stdlib2.rst4
-rw-r--r--Doc/using/venv-create.inc4
-rw-r--r--Doc/using/win_installer.pngbin0 -> 133120 bytes-rw-r--r--Doc/using/windows.rst507
-rw-r--r--Doc/whatsnew/2.1.rst2
-rw-r--r--Doc/whatsnew/3.3.rst6
-rw-r--r--Doc/whatsnew/3.5.rst551
-rw-r--r--Doc/whatsnew/index.rst1
-rw-r--r--Grammar/Grammar6
-rw-r--r--Include/Python-ast.h6
-rw-r--r--Include/Python.h1
-rw-r--r--Include/abstract.h12
-rw-r--r--Include/bytes_methods.h4
-rw-r--r--Include/bytesobject.h1
-rw-r--r--Include/code.h7
-rw-r--r--Include/codecs.h3
-rw-r--r--Include/complexobject.h22
-rw-r--r--Include/dictobject.h8
-rw-r--r--Include/fileutils.h13
-rw-r--r--Include/genobject.h8
-rw-r--r--Include/memoryobject.h4
-rw-r--r--Include/methodobject.h1
-rw-r--r--Include/object.h17
-rw-r--r--Include/objimpl.h4
-rw-r--r--Include/opcode.h246
-rw-r--r--Include/osdefs.h5
-rw-r--r--Include/patchlevel.h8
-rw-r--r--Include/pyatomic.h80
-rw-r--r--Include/pydebug.h2
-rw-r--r--Include/pyerrors.h7
-rw-r--r--Include/pylifecycle.h124
-rw-r--r--Include/pymacro.h15
-rw-r--r--Include/pymem.h13
-rw-r--r--Include/pyport.h19
-rw-r--r--Include/pythonrun.h110
-rw-r--r--Include/pytime.h28
-rw-r--r--Include/setobject.h96
-rw-r--r--Include/token.h13
-rw-r--r--Include/typeslots.h2
-rw-r--r--Include/unicodeobject.h2
-rw-r--r--Lib/_collections_abc.py8
-rw-r--r--Lib/_dummy_thread.py8
-rw-r--r--Lib/_pydecimal.py6381
-rw-r--r--Lib/_pyio.py126
-rw-r--r--Lib/_strptime.py4
-rw-r--r--Lib/abc.py2
-rw-r--r--Lib/argparse.py45
-rw-r--r--Lib/asynchat.py3
-rw-r--r--Lib/asyncio/events.py4
-rw-r--r--Lib/asyncore.py28
-rwxr-xr-xLib/cgi.py6
-rw-r--r--Lib/code.py45
-rw-r--r--Lib/codecs.py18
-rw-r--r--Lib/collections/__init__.py40
-rw-r--r--Lib/compileall.py130
-rw-r--r--Lib/concurrent/futures/_base.py21
-rw-r--r--Lib/concurrent/futures/process.py80
-rw-r--r--Lib/concurrent/futures/thread.py10
-rw-r--r--Lib/configparser.py182
-rw-r--r--Lib/contextlib.py40
-rw-r--r--Lib/copy.py8
-rw-r--r--Lib/ctypes/__init__.py2
-rw-r--r--Lib/ctypes/_endian.py2
-rw-r--r--Lib/ctypes/test/test_byteswap.py20
-rw-r--r--Lib/ctypes/test/test_pointers.py5
-rw-r--r--Lib/ctypes/test/test_prototypes.py5
-rw-r--r--Lib/ctypes/util.py6
-rw-r--r--Lib/datetime.py361
-rw-r--r--Lib/dbm/dumb.py27
-rw-r--r--Lib/decimal.py6419
-rw-r--r--Lib/difflib.py39
-rw-r--r--Lib/dis.py8
-rw-r--r--Lib/distutils/__init__.py2
-rw-r--r--Lib/distutils/command/build.py9
-rw-r--r--Lib/distutils/command/build_ext.py73
-rw-r--r--Lib/distutils/command/install.py2
-rw-r--r--Lib/distutils/command/upload.py40
-rw-r--r--Lib/distutils/command/wininst-14.0-amd64.exebin0 -> 84480 bytes-rw-r--r--Lib/distutils/command/wininst-14.0.exebin0 -> 75264 bytes-rw-r--r--Lib/distutils/dist.py69
-rw-r--r--Lib/distutils/extension.py8
-rw-r--r--Lib/distutils/msvc9compiler.py3
-rw-r--r--Lib/distutils/msvccompiler.py3
-rw-r--r--Lib/distutils/spawn.py3
-rw-r--r--Lib/distutils/sysconfig.py27
-rw-r--r--Lib/distutils/tests/test_build_ext.py56
-rw-r--r--Lib/distutils/tests/test_install.py2
-rw-r--r--Lib/distutils/version.py6
-rw-r--r--Lib/doctest.py17
-rw-r--r--Lib/email/__init__.py2
-rw-r--r--Lib/email/charset.py3
-rw-r--r--Lib/email/header.py3
-rw-r--r--Lib/email/headerregistry.py6
-rw-r--r--Lib/email/message.py16
-rw-r--r--Lib/email/mime/text.py3
-rw-r--r--Lib/encodings/cp65001.py9
-rw-r--r--Lib/enum.py19
-rw-r--r--Lib/formatter.py2
-rw-r--r--Lib/fractions.py50
-rw-r--r--Lib/ftplib.py117
-rw-r--r--Lib/functools.py57
-rw-r--r--Lib/genericpath.py13
-rw-r--r--Lib/gettext.py13
-rw-r--r--Lib/glob.py56
-rw-r--r--Lib/heapq.py349
-rw-r--r--Lib/html/entities.py3
-rw-r--r--Lib/html/parser.py114
-rw-r--r--Lib/http/__init__.py135
-rw-r--r--Lib/http/client.py331
-rw-r--r--Lib/http/cookiejar.py4
-rw-r--r--Lib/http/cookies.py56
-rw-r--r--Lib/http/server.py153
-rw-r--r--Lib/idlelib/NEWS.txt15
-rw-r--r--Lib/idlelib/WidgetRedirector.py8
-rw-r--r--Lib/idlelib/idle_test/test_searchengine.py2
-rw-r--r--Lib/idlelib/idlever.py2
-rw-r--r--Lib/imaplib.py8
-rw-r--r--Lib/imghdr.py12
-rw-r--r--Lib/imp.py17
-rw-r--r--Lib/importlib/__init__.py8
-rw-r--r--Lib/importlib/_bootstrap.py408
-rw-r--r--Lib/importlib/abc.py8
-rw-r--r--Lib/importlib/util.py99
-rw-r--r--Lib/inspect.py318
-rw-r--r--Lib/ipaddress.py545
-rw-r--r--Lib/json/__init__.py7
-rw-r--r--Lib/json/decoder.py86
-rw-r--r--Lib/json/encoder.py9
-rw-r--r--Lib/json/tool.py39
-rw-r--r--Lib/locale.py15
-rw-r--r--Lib/logging/__init__.py27
-rw-r--r--Lib/logging/config.py10
-rw-r--r--Lib/logging/handlers.py14
-rw-r--r--Lib/macpath.py32
-rw-r--r--Lib/mailbox.py6
-rw-r--r--Lib/multiprocessing/connection.py33
-rw-r--r--Lib/multiprocessing/dummy/__init__.py2
-rw-r--r--Lib/multiprocessing/dummy/connection.py5
-rw-r--r--Lib/multiprocessing/forkserver.py20
-rw-r--r--Lib/multiprocessing/heap.py11
-rw-r--r--Lib/multiprocessing/managers.py35
-rw-r--r--Lib/multiprocessing/pool.py27
-rw-r--r--Lib/multiprocessing/popen_fork.py3
-rw-r--r--Lib/multiprocessing/queues.py27
-rw-r--r--Lib/multiprocessing/sharedctypes.py26
-rw-r--r--Lib/multiprocessing/synchronize.py32
-rw-r--r--Lib/multiprocessing/util.py14
-rw-r--r--Lib/ntpath.py260
-rw-r--r--Lib/opcode.py3
-rw-r--r--Lib/operator.py11
-rw-r--r--Lib/os.py4
-rw-r--r--Lib/pathlib.py118
-rwxr-xr-xLib/pdb.py2
-rw-r--r--Lib/pkgutil.py2
-rw-r--r--Lib/posixpath.py30
-rw-r--r--Lib/pprint.py87
-rwxr-xr-xLib/pydoc.py12
-rw-r--r--Lib/pydoc_data/topics.py14
-rw-r--r--Lib/queue.py5
-rw-r--r--Lib/random.py2
-rw-r--r--Lib/re.py41
-rw-r--r--Lib/reprlib.py12
-rw-r--r--Lib/runpy.py2
-rw-r--r--Lib/sched.py6
-rw-r--r--Lib/selectors.py67
-rw-r--r--Lib/shutil.py102
-rw-r--r--Lib/signal.py85
-rw-r--r--Lib/site.py15
-rwxr-xr-xLib/smtpd.py247
-rwxr-xr-xLib/smtplib.py113
-rw-r--r--Lib/sndhdr.py7
-rw-r--r--Lib/socket.py203
-rw-r--r--Lib/socketserver.py97
-rw-r--r--Lib/sre_compile.py178
-rw-r--r--Lib/sre_constants.py222
-rw-r--r--Lib/sre_parse.py430
-rw-r--r--Lib/ssl.py310
-rw-r--r--Lib/stat.py23
-rw-r--r--Lib/statistics.py4
-rw-r--r--Lib/subprocess.py135
-rw-r--r--Lib/sysconfig.py19
-rwxr-xr-xLib/tarfile.py46
-rw-r--r--Lib/telnetlib.py5
-rw-r--r--Lib/tempfile.py26
-rw-r--r--Lib/test/_test_multiprocessing.py4
-rw-r--r--Lib/test/datetimetester.py117
-rw-r--r--Lib/test/eintrdata/eintr_tester.py260
-rw-r--r--Lib/test/fork_wait.py7
-rw-r--r--Lib/test/imghdrdata/python.exrbin0 -> 2635 bytes-rw-r--r--Lib/test/imghdrdata/python.webpbin0 -> 432 bytes-rw-r--r--Lib/test/inspect_fodder2.py13
-rw-r--r--Lib/test/list_tests.py10
-rw-r--r--Lib/test/lock_tests.py8
-rw-r--r--Lib/test/mock_socket.py15
-rw-r--r--Lib/test/pickletester.py97
-rwxr-xr-xLib/test/pystone.py10
-rwxr-xr-xLib/test/regrtest.py4
-rw-r--r--Lib/test/script_helper.py4
-rw-r--r--Lib/test/ssl_servers.py8
-rw-r--r--Lib/test/string_tests.py3
-rw-r--r--Lib/test/support/__init__.py3
-rw-r--r--Lib/test/test_argparse.py138
-rw-r--r--Lib/test/test_array.py9
-rw-r--r--Lib/test/test_asdl_parser.py122
-rw-r--r--Lib/test/test_asynchat.py13
-rw-r--r--Lib/test/test_asyncore.py27
-rw-r--r--Lib/test/test_augassign.py15
-rw-r--r--Lib/test/test_buffer.py15
-rw-r--r--Lib/test/test_builtin.py6
-rw-r--r--Lib/test/test_bytes.py64
-rw-r--r--Lib/test/test_capi.py58
-rw-r--r--Lib/test/test_cgi.py19
-rw-r--r--Lib/test/test_cmd_line.py2
-rw-r--r--Lib/test/test_code_module.py35
-rw-r--r--Lib/test/test_codeccallbacks.py128
-rw-r--r--Lib/test/test_codecs.py95
-rw-r--r--Lib/test/test_collections.py70
-rw-r--r--Lib/test/test_compileall.py98
-rw-r--r--Lib/test/test_concurrent_futures.py55
-rw-r--r--Lib/test/test_configparser.py277
-rw-r--r--Lib/test/test_contextlib.py58
-rw-r--r--Lib/test/test_cprofile.py10
-rw-r--r--Lib/test/test_csv.py16
-rw-r--r--Lib/test/test_datetime.py10
-rw-r--r--Lib/test/test_dbm_dumb.py8
-rw-r--r--Lib/test/test_decimal.py157
-rw-r--r--Lib/test/test_defaultdict.py5
-rw-r--r--Lib/test/test_deque.py5
-rw-r--r--Lib/test/test_descr.py97
-rw-r--r--Lib/test/test_dis.py180
-rw-r--r--Lib/test/test_doctest.py58
-rw-r--r--Lib/test/test_docxmlrpc.py9
-rw-r--r--Lib/test/test_eintr.py20
-rw-r--r--Lib/test/test_email/test_email.py4
-rw-r--r--Lib/test/test_email/test_message.py10
-rw-r--r--Lib/test/test_enum.py66
-rw-r--r--Lib/test/test_epoll.py2
-rw-r--r--Lib/test/test_faulthandler.py26
-rw-r--r--Lib/test/test_fileio.py35
-rw-r--r--Lib/test/test_fork1.py5
-rw-r--r--Lib/test/test_format.py395
-rw-r--r--Lib/test/test_fractions.py3
-rw-r--r--Lib/test/test_ftplib.py29
-rw-r--r--Lib/test/test_functools.py18
-rw-r--r--Lib/test/test_generators.py39
-rw-r--r--Lib/test/test_genericpath.py34
-rw-r--r--Lib/test/test_getargs2.py10
-rw-r--r--Lib/test/test_gettext.py70
-rw-r--r--Lib/test/test_glob.py132
-rw-r--r--Lib/test/test_grammar.py14
-rw-r--r--Lib/test/test_heapq.py23
-rw-r--r--Lib/test/test_htmlparser.py76
-rw-r--r--Lib/test/test_http_cookies.py12
-rw-r--r--Lib/test/test_httplib.py350
-rw-r--r--Lib/test/test_imaplib.py145
-rw-r--r--Lib/test/test_imghdr.py4
-rw-r--r--Lib/test/test_import/__init__.py (renamed from Lib/test/test_import.py)42
-rw-r--r--Lib/test/test_import/__main__.py3
-rw-r--r--Lib/test/test_import/data/circular_imports/basic.py2
-rw-r--r--Lib/test/test_import/data/circular_imports/basic2.py1
-rw-r--r--Lib/test/test_import/data/circular_imports/indirect.py1
-rw-r--r--Lib/test/test_import/data/circular_imports/rebinding.py3
-rw-r--r--Lib/test/test_import/data/circular_imports/rebinding2.py3
-rw-r--r--Lib/test/test_import/data/circular_imports/subpackage.py2
-rw-r--r--Lib/test/test_import/data/circular_imports/subpkg/subpackage2.py2
-rw-r--r--Lib/test/test_import/data/circular_imports/subpkg/util.py2
-rw-r--r--Lib/test/test_import/data/circular_imports/util.py2
-rw-r--r--Lib/test/test_importlib/builtin/test_finder.py33
-rw-r--r--Lib/test/test_importlib/builtin/test_loader.py39
-rw-r--r--Lib/test/test_importlib/builtin/util.py7
-rw-r--r--Lib/test/test_importlib/extension/test_case_sensitivity.py16
-rw-r--r--Lib/test/test_importlib/extension/test_finder.py15
-rw-r--r--Lib/test/test_importlib/extension/test_loader.py36
-rw-r--r--Lib/test/test_importlib/extension/test_path_hook.py13
-rw-r--r--Lib/test/test_importlib/extension/util.py19
-rw-r--r--Lib/test/test_importlib/frozen/test_finder.py12
-rw-r--r--Lib/test/test_importlib/frozen/test_loader.py17
-rw-r--r--Lib/test/test_importlib/import_/test___loader__.py15
-rw-r--r--Lib/test/test_importlib/import_/test___package__.py19
-rw-r--r--Lib/test/test_importlib/import_/test_api.py17
-rw-r--r--Lib/test/test_importlib/import_/test_caching.py9
-rw-r--r--Lib/test/test_importlib/import_/test_fromlist.py13
-rw-r--r--Lib/test/test_importlib/import_/test_meta_path.py21
-rw-r--r--Lib/test/test_importlib/import_/test_packages.py7
-rw-r--r--Lib/test/test_importlib/import_/test_path.py77
-rw-r--r--Lib/test/test_importlib/import_/test_relative_imports.py7
-rw-r--r--Lib/test/test_importlib/import_/util.py20
-rw-r--r--Lib/test/test_importlib/source/test_case_sensitivity.py19
-rw-r--r--Lib/test/test_importlib/source/test_file_loader.py111
-rw-r--r--Lib/test/test_importlib/source/test_finder.py28
-rw-r--r--Lib/test/test_importlib/source/test_path_hook.py8
-rw-r--r--Lib/test/test_importlib/source/test_source_encoding.py33
-rw-r--r--Lib/test/test_importlib/source/util.py96
-rw-r--r--Lib/test/test_importlib/test_abc.py292
-rw-r--r--Lib/test/test_importlib/test_api.py99
-rw-r--r--Lib/test/test_importlib/test_lazy.py132
-rw-r--r--Lib/test/test_importlib/test_locks.py178
-rw-r--r--Lib/test/test_importlib/test_spec.py256
-rw-r--r--Lib/test/test_importlib/test_util.py219
-rw-r--r--Lib/test/test_importlib/test_windows.py100
-rw-r--r--Lib/test/test_importlib/util.py185
-rw-r--r--Lib/test/test_inspect.py197
-rw-r--r--Lib/test/test_io.py121
-rw-r--r--Lib/test/test_ipaddress.py165
-rw-r--r--Lib/test/test_itertools.py2
-rw-r--r--Lib/test/test_json/__init__.py4
-rw-r--r--Lib/test/test_json/test_decode.py8
-rw-r--r--Lib/test/test_json/test_encode_basestring_ascii.py3
-rw-r--r--Lib/test/test_json/test_fail.py59
-rw-r--r--Lib/test/test_json/test_scanstring.py2
-rw-r--r--Lib/test/test_json/test_tool.py41
-rw-r--r--Lib/test/test_kqueue.py2
-rw-r--r--Lib/test/test_locale.py54
-rw-r--r--Lib/test/test_logging.py205
-rw-r--r--Lib/test/test_long.py2
-rw-r--r--Lib/test/test_lzma.py91
-rw-r--r--Lib/test/test_macpath.py2
-rw-r--r--Lib/test/test_marshal.py2
-rw-r--r--Lib/test/test_math.py23
-rw-r--r--Lib/test/test_memoryio.py51
-rw-r--r--Lib/test/test_memoryview.py4
-rw-r--r--Lib/test/test_module.py24
-rw-r--r--Lib/test/test_operator.py11
-rw-r--r--Lib/test/test_os.py233
-rw-r--r--Lib/test/test_pathlib.py216
-rw-r--r--Lib/test/test_pkgutil.py3
-rw-r--r--Lib/test/test_platform.py2
-rw-r--r--Lib/test/test_poplib.py17
-rw-r--r--Lib/test/test_posix.py7
-rw-r--r--Lib/test/test_posixpath.py12
-rw-r--r--Lib/test/test_pprint.py97
-rw-r--r--Lib/test/test_pty.py23
-rw-r--r--Lib/test/test_pydoc.py40
-rw-r--r--Lib/test/test_re.py259
-rw-r--r--Lib/test/test_readline.py42
-rw-r--r--Lib/test/test_reprlib.py48
-rwxr-xr-xLib/test/test_script_helper.py20
-rw-r--r--Lib/test/test_selectors.py25
-rw-r--r--Lib/test/test_set.py2
-rw-r--r--Lib/test/test_shutil.py26
-rw-r--r--Lib/test/test_signal.py252
-rw-r--r--Lib/test/test_site.py10
-rw-r--r--Lib/test/test_smtpd.py397
-rw-r--r--Lib/test/test_smtplib.py70
-rw-r--r--Lib/test/test_sndhdr.py14
-rw-r--r--Lib/test/test_socket.py426
-rw-r--r--Lib/test/test_socketserver.py32
-rw-r--r--Lib/test/test_ssl.py477
-rw-r--r--Lib/test/test_stat.py29
-rw-r--r--Lib/test/test_subprocess.py20
-rw-r--r--Lib/test/test_support.py2
-rw-r--r--Lib/test/test_sys.py78
-rw-r--r--Lib/test/test_sys_setprofile.py1
-rw-r--r--Lib/test/test_tarfile.py94
-rw-r--r--Lib/test/test_tcl.py16
-rw-r--r--Lib/test/test_telnetlib.py2
-rw-r--r--Lib/test/test_threaded_import.py10
-rw-r--r--Lib/test/test_timeit.py16
-rw-r--r--Lib/test/test_timeout.py8
-rw-r--r--Lib/test/test_tokenize.py5
-rw-r--r--Lib/test/test_trace.py6
-rw-r--r--Lib/test/test_traceback.py37
-rw-r--r--Lib/test/test_tracemalloc.py2
-rw-r--r--Lib/test/test_tuple.py13
-rw-r--r--Lib/test/test_types.py2
-rw-r--r--Lib/test/test_unicode.py47
-rw-r--r--Lib/test/test_unicodedata.py7
-rw-r--r--Lib/test/test_urllib.py8
-rw-r--r--Lib/test/test_urllib2.py15
-rw-r--r--Lib/test/test_urllibnet.py3
-rw-r--r--Lib/test/test_urlparse.py52
-rw-r--r--Lib/test/test_uuid.py43
-rw-r--r--Lib/test/test_wait3.py3
-rw-r--r--Lib/test/test_wait4.py5
-rw-r--r--Lib/test/test_warnings.py51
-rw-r--r--Lib/test/test_weakref.py20
-rw-r--r--Lib/test/test_wsgiref.py6
-rw-r--r--Lib/test/test_xmlrpc.py22
-rw-r--r--Lib/test/test_zipfile.py175
-rw-r--r--Lib/test/test_zipimport_support.py2
-rw-r--r--Lib/threading.py15
-rwxr-xr-xLib/timeit.py30
-rw-r--r--Lib/tkinter/__init__.py98
-rw-r--r--Lib/tkinter/test/test_tkinter/test_misc.py5
-rw-r--r--Lib/tkinter/test/test_tkinter/test_widgets.py7
-rw-r--r--Lib/token.py11
-rw-r--r--Lib/tokenize.py6
-rwxr-xr-xLib/trace.py24
-rw-r--r--Lib/traceback.py2
-rw-r--r--Lib/unittest/__init__.py9
-rw-r--r--Lib/unittest/loader.py252
-rw-r--r--Lib/unittest/mock.py66
-rw-r--r--Lib/unittest/test/test_case.py10
-rw-r--r--Lib/unittest/test/test_discovery.py316
-rw-r--r--Lib/unittest/test/test_loader.py423
-rw-r--r--Lib/unittest/test/test_setups.py7
-rw-r--r--Lib/unittest/test/testmock/testmock.py36
-rw-r--r--Lib/unittest/test/testmock/testpatch.py24
-rw-r--r--Lib/unittest/util.py2
-rw-r--r--Lib/urllib/parse.py69
-rw-r--r--Lib/urllib/request.py15
-rw-r--r--Lib/urllib/robotparser.py2
-rw-r--r--Lib/uuid.py98
-rw-r--r--Lib/warnings.py4
-rw-r--r--Lib/weakref.py4
-rw-r--r--Lib/wsgiref/headers.py6
-rw-r--r--Lib/xml/dom/minidom.py8
-rw-r--r--Lib/xml/etree/ElementPath.py20
-rw-r--r--Lib/xml/etree/ElementTree.py8
-rw-r--r--Lib/xmlrpc/client.py30
-rw-r--r--Lib/zipfile.py538
-rw-r--r--Mac/BuildScript/README.txt59
-rw-r--r--Mac/BuildScript/resources/ReadMe.rtf113
-rw-r--r--Mac/BuildScript/resources/Welcome.rtf8
-rw-r--r--Mac/PythonLauncher/Info.plist.in4
-rw-r--r--Mac/PythonLauncher/Makefile.in29
-rwxr-xr-xMac/Tools/bundlebuilder.py934
-rw-r--r--Makefile.pre.in68
-rw-r--r--Misc/ACKS31
-rw-r--r--Misc/HISTORY1113
-rw-r--r--Misc/NEWS5610
-rw-r--r--Misc/RPM/python-3.5.spec (renamed from Misc/RPM/python-3.4.spec)4
-rw-r--r--Misc/coverity_model.c2
-rw-r--r--Modules/README2
-rw-r--r--Modules/Setup.config.in2
-rw-r--r--Modules/Setup.dist1
-rw-r--r--Modules/_bz2module.c4
-rw-r--r--Modules/_codecsmodule.c18
-rw-r--r--Modules/_collectionsmodule.c72
-rw-r--r--Modules/_csv.c6
-rw-r--r--Modules/_ctypes/callproc.c2
-rw-r--r--Modules/_ctypes/libffi_msvc/ffi.c37
-rw-r--r--Modules/_cursesmodule.c2
-rw-r--r--Modules/_datetimemodule.c38
-rw-r--r--Modules/_decimal/_decimal.c4
-rw-r--r--Modules/_decimal/docstrings.h860
-rw-r--r--Modules/_decimal/tests/deccheck.py7
-rw-r--r--Modules/_elementtree.c2
-rw-r--r--Modules/_hashopenssl.c12
-rw-r--r--Modules/_heapqmodule.c355
-rw-r--r--Modules/_io/_iomodule.c28
-rw-r--r--Modules/_io/bufferedio.c66
-rw-r--r--Modules/_io/bytesio.c234
-rw-r--r--Modules/_io/fileio.c183
-rw-r--r--Modules/_io/textio.c42
-rw-r--r--Modules/_json.c154
-rw-r--r--Modules/_lsprof.c11
-rw-r--r--Modules/_lzmamodule.c214
-rw-r--r--Modules/_operator.c6
-rw-r--r--Modules/_pickle.c208
-rw-r--r--Modules/_sqlite/cursor.c2
-rw-r--r--Modules/_sre.c134
-rw-r--r--Modules/_ssl.c806
-rw-r--r--Modules/_stat.c36
-rw-r--r--Modules/_struct.c14
-rw-r--r--Modules/_testbuffer.c53
-rw-r--r--Modules/_testcapimodule.c279
-rw-r--r--Modules/_threadmodule.c19
-rw-r--r--Modules/_tkinter.c164
-rw-r--r--Modules/_tracemalloc.c86
-rw-r--r--Modules/_winapi.c137
-rw-r--r--Modules/arraymodule.c702
-rw-r--r--Modules/atexitmodule.c2
-rw-r--r--Modules/cjkcodecs/cjkcodecs.h2
-rw-r--r--Modules/cjkcodecs/clinic/multibytecodec.c.h301
-rw-r--r--Modules/cjkcodecs/multibytecodec.c337
-rw-r--r--Modules/clinic/_lzmamodule.c.h37
-rw-r--r--Modules/clinic/arraymodule.c.h505
-rw-r--r--Modules/clinic/binascii.c.h4
-rw-r--r--Modules/clinic/cmathmodule.c.h851
-rw-r--r--Modules/clinic/fcntlmodule.c.h188
-rw-r--r--Modules/clinic/grpmodule.c.h87
-rw-r--r--Modules/clinic/pwdmodule.c.h73
-rw-r--r--Modules/clinic/spwdmodule.c.h70
-rw-r--r--Modules/cmathmodule.c493
-rw-r--r--Modules/faulthandler.c73
-rw-r--r--Modules/fcntlmodule.c438
-rw-r--r--Modules/gcmodule.c82
-rw-r--r--Modules/getpath.c16
-rw-r--r--Modules/grpmodule.c79
-rw-r--r--Modules/main.c24
-rw-r--r--Modules/mathmodule.c53
-rw-r--r--Modules/md5module.c176
-rw-r--r--Modules/posixmodule.c11752
-rw-r--r--Modules/pwdmodule.c72
-rw-r--r--Modules/pyexpat.c628
-rw-r--r--Modules/readline.c41
-rw-r--r--Modules/sha1module.c176
-rw-r--r--Modules/sha256module.c234
-rw-r--r--Modules/sha512module.c268
-rw-r--r--Modules/signalmodule.c222
-rw-r--r--Modules/socketmodule.c384
-rw-r--r--Modules/spwdmodule.c49
-rw-r--r--Modules/sre.h9
-rw-r--r--Modules/sre_constants.h3
-rw-r--r--Modules/sre_lib.h28
-rw-r--r--Modules/timemodule.c235
-rw-r--r--Modules/tkappinit.c19
-rw-r--r--Modules/unicodedata_db.h6998
-rw-r--r--Modules/unicodename_db.h40478
-rw-r--r--Modules/winreparse.h53
-rw-r--r--Objects/README1
-rw-r--r--Objects/abstract.c103
-rw-r--r--Objects/bytearrayobject.c1542
-rw-r--r--Objects/bytes_methods.c27
-rw-r--r--Objects/bytesobject.c1703
-rw-r--r--Objects/classobject.c43
-rw-r--r--Objects/complexobject.c34
-rw-r--r--Objects/dictobject.c56
-rw-r--r--Objects/exceptions.c2
-rw-r--r--Objects/floatobject.c4
-rw-r--r--Objects/genobject.c96
-rw-r--r--Objects/listobject.c4
-rw-r--r--Objects/longobject.c85
-rw-r--r--Objects/memoryobject.c20
-rw-r--r--Objects/methodobject.c6
-rw-r--r--Objects/moduleobject.c53
-rw-r--r--Objects/object.c28
-rw-r--r--Objects/obmalloc.c140
-rw-r--r--Objects/setobject.c358
-rw-r--r--Objects/tupleobject.c4
-rw-r--r--Objects/typeobject.c302
-rw-r--r--Objects/typeslots.inc2
-rw-r--r--Objects/unicodeobject.c593
-rw-r--r--Objects/unicodetype_db.h2712
-rw-r--r--PC/VS9.0/_bz2.vcproj581
-rw-r--r--PC/VS9.0/_ctypes.vcproj705
-rw-r--r--PC/VS9.0/_ctypes_test.vcproj521
-rw-r--r--PC/VS9.0/_decimal.vcproj743
-rw-r--r--PC/VS9.0/_elementtree.vcproj613
-rw-r--r--PC/VS9.0/_hashlib.vcproj537
-rw-r--r--PC/VS9.0/_lzma.vcproj537
-rw-r--r--PC/VS9.0/_msi.vcproj529
-rw-r--r--PC/VS9.0/_multiprocessing.vcproj541
-rw-r--r--PC/VS9.0/_socket.vcproj537
-rw-r--r--PC/VS9.0/_sqlite3.vcproj609
-rw-r--r--PC/VS9.0/_ssl.vcproj537
-rw-r--r--PC/VS9.0/_testbuffer.vcproj521
-rw-r--r--PC/VS9.0/_testcapi.vcproj521
-rw-r--r--PC/VS9.0/_testimportmultiple.vcproj521
-rw-r--r--PC/VS9.0/_tkinter.vcproj541
-rw-r--r--PC/VS9.0/bdist_wininst.vcproj270
-rw-r--r--PC/VS9.0/debug.vsprops15
-rw-r--r--PC/VS9.0/kill_python.c178
-rw-r--r--PC/VS9.0/kill_python.vcproj279
-rw-r--r--PC/VS9.0/make_buildinfo.c195
-rw-r--r--PC/VS9.0/make_buildinfo.vcproj101
-rw-r--r--PC/VS9.0/make_versioninfo.vcproj324
-rw-r--r--PC/VS9.0/pcbuild.sln690
-rw-r--r--PC/VS9.0/pginstrument.vsprops34
-rw-r--r--PC/VS9.0/pgupdate.vsprops14
-rw-r--r--PC/VS9.0/pyd.vsprops28
-rw-r--r--PC/VS9.0/pyd_d.vsprops36
-rw-r--r--PC/VS9.0/pyexpat.vcproj553
-rw-r--r--PC/VS9.0/pyproject.vsprops91
-rw-r--r--PC/VS9.0/python.vcproj637
-rw-r--r--PC/VS9.0/python3dll.vcproj246
-rw-r--r--PC/VS9.0/pythoncore.vcproj1877
-rw-r--r--PC/VS9.0/pythonw.vcproj618
-rw-r--r--PC/VS9.0/release.vsprops15
-rw-r--r--PC/VS9.0/select.vcproj537
-rw-r--r--PC/VS9.0/sqlite3.vcproj537
-rw-r--r--PC/VS9.0/sqlite3.vsprops14
-rw-r--r--PC/VS9.0/ssl.vcproj189
-rw-r--r--PC/VS9.0/unicodedata.vcproj533
-rw-r--r--PC/VS9.0/winsound.vcproj523
-rw-r--r--PC/VS9.0/x64.vsprops22
-rw-r--r--PC/VS9.0/xxlimited.vcproj417
-rw-r--r--PC/bdist_wininst/archive.h9
-rw-r--r--PC/bdist_wininst/bdist_wininst.vcxproj103
-rw-r--r--PC/bdist_wininst/bdist_wininst.vcxproj.filters (renamed from PCbuild/bdist_wininst.vcxproj.filters)0
-rw-r--r--PC/bdist_wininst/build.bat22
-rw-r--r--PC/bdist_wininst/extract.c9
-rw-r--r--PC/bdist_wininst/install.c38
-rw-r--r--PC/bdist_wininst/install.rc162
-rw-r--r--PC/bdist_wininst/resource.h24
-rw-r--r--PC/bdist_wininst/wininst-7.1.sln21
-rw-r--r--PC/bdist_wininst/wininst-7.1.vcproj214
-rw-r--r--PC/bdist_wininst/wininst-8.sln19
-rw-r--r--PC/bdist_wininst/wininst-8.vcproj320
-rw-r--r--PC/bdist_wininst/wininst.dsp123
-rw-r--r--PC/bdist_wininst/wininst.dsw29
-rw-r--r--PC/config.c4
-rw-r--r--PC/dl_nt.c8
-rw-r--r--PC/example_nt/example.vcproj4
-rw-r--r--PC/launcher.c120
-rw-r--r--PC/pyconfig.h44
-rw-r--r--PC/pylauncher.rc2
-rw-r--r--PC/python.manifest12
-rw-r--r--PC/python3.def1400
-rw-r--r--PC/python3.mak14
-rw-r--r--PC/python34gen.py26
-rw-r--r--PC/python34stub.def700
-rw-r--r--PC/python_exe.rc50
-rw-r--r--PC/python_nt.rc42
-rw-r--r--PC/python_ver_rc.h35
-rw-r--r--PC/readme.txt5
-rw-r--r--PCbuild/_bz2.vcxproj184
-rw-r--r--PCbuild/_ctypes.vcxproj211
-rw-r--r--PCbuild/_ctypes_test.vcxproj131
-rw-r--r--PCbuild/_decimal.vcxproj212
-rw-r--r--PCbuild/_elementtree.vcxproj183
-rw-r--r--PCbuild/_freeze_importlib.vcxproj174
-rw-r--r--PCbuild/_freeze_importlib.vcxproj.filters4
-rw-r--r--PCbuild/_hashlib.vcxproj222
-rw-r--r--PCbuild/_lzma.vcxproj189
-rw-r--r--PCbuild/_msi.vcxproj164
-rw-r--r--PCbuild/_multiprocessing.vcxproj162
-rw-r--r--PCbuild/_overlapped.vcxproj170
-rw-r--r--PCbuild/_socket.vcxproj162
-rw-r--r--PCbuild/_sqlite3.vcxproj183
-rw-r--r--PCbuild/_ssl.vcxproj222
-rw-r--r--PCbuild/_testbuffer.vcxproj156
-rw-r--r--PCbuild/_testcapi.vcxproj156
-rw-r--r--PCbuild/_testembed.vcxproj132
-rw-r--r--PCbuild/_testembed.vcxproj.filters2
-rw-r--r--PCbuild/_testimportmultiple.vcxproj156
-rw-r--r--PCbuild/_tkinter.vcxproj191
-rw-r--r--PCbuild/bdist_wininst.vcxproj158
-rw-r--r--PCbuild/build.bat42
-rw-r--r--PCbuild/build_pgo.bat37
-rw-r--r--PCbuild/build_ssl.py264
-rw-r--r--PCbuild/build_tkinter.py78
-rw-r--r--PCbuild/clean.bat5
-rw-r--r--PCbuild/debug.props27
-rw-r--r--PCbuild/env.bat19
-rw-r--r--PCbuild/get_externals.bat103
-rw-r--r--PCbuild/idle.bat6
-rw-r--r--PCbuild/installer.bmpbin58806 -> 0 bytes-rw-r--r--PCbuild/kill_python.c178
-rw-r--r--PCbuild/kill_python.vcxproj120
-rw-r--r--PCbuild/kill_python.vcxproj.filters13
-rw-r--r--PCbuild/libeay.vcxproj890
-rw-r--r--PCbuild/make_buildinfo.c194
-rw-r--r--PCbuild/make_buildinfo.vcxproj52
-rw-r--r--PCbuild/make_buildinfo.vcxproj.filters14
-rw-r--r--PCbuild/make_versioninfo.vcxproj200
-rw-r--r--PCbuild/make_versioninfo.vcxproj.filters13
-rw-r--r--PCbuild/openssl.props75
-rw-r--r--PCbuild/pcbuild.proj84
-rw-r--r--PCbuild/pcbuild.sln301
-rw-r--r--PCbuild/pginstrument.props38
-rw-r--r--PCbuild/pgupdate.props17
-rw-r--r--PCbuild/prepare_ssl.bat (renamed from PCbuild/build_ssl.bat)8
-rw-r--r--PCbuild/prepare_ssl.py240
-rw-r--r--PCbuild/pyd.props25
-rw-r--r--PCbuild/pyd_d.props31
-rw-r--r--PCbuild/pyexpat.vcxproj173
-rw-r--r--PCbuild/pylauncher.vcxproj250
-rw-r--r--PCbuild/pyproject.props206
-rw-r--r--PCbuild/python.props116
-rw-r--r--PCbuild/python.vcxproj306
-rw-r--r--PCbuild/python.vcxproj.filters4
-rw-r--r--PCbuild/python3dll.vcxproj206
-rw-r--r--PCbuild/pythoncore.vcxproj436
-rw-r--r--PCbuild/pythoncore.vcxproj.filters6
-rw-r--r--PCbuild/pythonw.vcxproj278
-rw-r--r--PCbuild/pywlauncher.vcxproj190
-rw-r--r--PCbuild/readme.txt261
-rw-r--r--PCbuild/release.props19
-rw-r--r--PCbuild/rt.bat16
-rw-r--r--PCbuild/select.vcxproj171
-rw-r--r--PCbuild/sqlite3.props16
-rw-r--r--PCbuild/sqlite3.vcxproj181
-rw-r--r--PCbuild/ssl.vcxproj221
-rw-r--r--PCbuild/ssleay.vcxproj119
-rw-r--r--PCbuild/tcl.vcxproj90
-rw-r--r--PCbuild/tcltk.props45
-rw-r--r--PCbuild/tix.vcxproj94
-rw-r--r--PCbuild/tk.vcxproj95
-rw-r--r--PCbuild/unicodedata.vcxproj155
-rw-r--r--PCbuild/vs9to10.py56
-rw-r--r--PCbuild/vs9to8.py34
-rw-r--r--PCbuild/winsound.vcxproj155
-rw-r--r--PCbuild/x64.props20
-rw-r--r--PCbuild/xxlimited.vcxproj152
-rw-r--r--Parser/Python.asdl2
-rw-r--r--Parser/asdl.py587
-rwxr-xr-xParser/asdl_c.py54
-rw-r--r--Parser/node.c4
-rw-r--r--Parser/pgenmain.c7
-rw-r--r--Parser/printgrammar.c5
-rw-r--r--Parser/spark.py849
-rw-r--r--Parser/tokenizer.c8
-rw-r--r--Programs/README1
-rw-r--r--Programs/_freeze_importlib.c (renamed from Modules/_freeze_importlib.c)2
-rw-r--r--Programs/_testembed.c (renamed from Modules/_testembed.c)4
-rw-r--r--Programs/python.c (renamed from Modules/python.c)2
-rw-r--r--Python/Python-ast.c24
-rw-r--r--Python/README1
-rw-r--r--Python/_warnings.c13
-rw-r--r--Python/asdl.c16
-rw-r--r--Python/ast.c8
-rw-r--r--Python/bltinmodule.c1567
-rw-r--r--Python/ceval.c76
-rw-r--r--Python/clinic/import.c.h323
-rw-r--r--Python/codecs.c199
-rw-r--r--Python/compile.c14
-rw-r--r--Python/dynload_win.c21
-rw-r--r--Python/errors.c25
-rw-r--r--Python/fileutils.c120
-rw-r--r--Python/formatter_unicode.c7
-rw-r--r--Python/frozenmain.c2
-rw-r--r--Python/getargs.c32
-rw-r--r--Python/graminit.c435
-rw-r--r--Python/import.c349
-rw-r--r--Python/importlib.h7902
-rw-r--r--Python/marshal.c205
-rw-r--r--Python/opcode_targets.h4
-rw-r--r--Python/pylifecycle.c1502
-rw-r--r--Python/pythonrun.c1471
-rw-r--r--Python/pytime.c304
-rw-r--r--Python/sysmodule.c13
-rw-r--r--Python/thread.c2
-rw-r--r--Python/thread_foobar.h63
-rw-r--r--Python/thread_pthread.h10
-rw-r--r--Python/traceback.c11
-rw-r--r--README18
-rw-r--r--Tools/buildbot/README.tcltk-AMD6436
-rw-r--r--Tools/buildbot/build-amd64.bat6
-rw-r--r--Tools/buildbot/build.bat21
-rw-r--r--Tools/buildbot/buildmsi.bat22
-rw-r--r--Tools/buildbot/clean-amd64.bat10
-rw-r--r--Tools/buildbot/clean.bat37
-rw-r--r--Tools/buildbot/external-amd64.bat30
-rw-r--r--Tools/buildbot/external-common.bat54
-rw-r--r--Tools/buildbot/external.bat31
-rw-r--r--Tools/buildbot/test-amd64.bat8
-rw-r--r--Tools/buildbot/test.bat8
-rwxr-xr-xTools/clinic/clinic.py35
-rwxr-xr-xTools/i18n/makelocalealias.py62
-rw-r--r--Tools/msi/README.txt511
-rw-r--r--Tools/msi/build.bat46
-rw-r--r--Tools/msi/buildrelease.bat163
-rw-r--r--Tools/msi/bundle/Default.thm123
-rw-r--r--Tools/msi/bundle/Default.wxl103
-rw-r--r--Tools/msi/bundle/SideBar.pngbin0 -> 19170 bytes-rw-r--r--Tools/msi/bundle/bootstrap/LICENSE.txt25
-rw-r--r--Tools/msi/bundle/bootstrap/PythonBootstrapperApplication.cpp2652
-rw-r--r--Tools/msi/bundle/bootstrap/pch.cpp1
-rw-r--r--Tools/msi/bundle/bootstrap/pch.h59
-rw-r--r--Tools/msi/bundle/bootstrap/pythonba.cpp76
-rw-r--r--Tools/msi/bundle/bootstrap/pythonba.def18
-rw-r--r--Tools/msi/bundle/bootstrap/pythonba.sln22
-rw-r--r--Tools/msi/bundle/bootstrap/pythonba.vcxproj69
-rw-r--r--Tools/msi/bundle/bootstrap/resource.h25
-rw-r--r--Tools/msi/bundle/bundle.icobin0 -> 19790 bytes-rw-r--r--Tools/msi/bundle/bundle.targets99
-rw-r--r--Tools/msi/bundle/bundle.wxs80
-rw-r--r--Tools/msi/bundle/full.wixproj21
-rw-r--r--Tools/msi/bundle/packagegroups/core.wxs56
-rw-r--r--Tools/msi/bundle/packagegroups/crt.wxs25
-rw-r--r--Tools/msi/bundle/packagegroups/dev.wxs40
-rw-r--r--Tools/msi/bundle/packagegroups/doc.wxs24
-rw-r--r--Tools/msi/bundle/packagegroups/exe.wxs56
-rw-r--r--Tools/msi/bundle/packagegroups/launcher.wxs23
-rw-r--r--Tools/msi/bundle/packagegroups/lib.wxs56
-rw-r--r--Tools/msi/bundle/packagegroups/postinstall.wxs62
-rw-r--r--Tools/msi/bundle/packagegroups/tcltk.wxs62
-rw-r--r--Tools/msi/bundle/packagegroups/test.wxs56
-rw-r--r--Tools/msi/bundle/packagegroups/tools.wxs24
-rw-r--r--Tools/msi/bundle/postinstall_en-US.wxl_template4
-rw-r--r--Tools/msi/bundle/releaselocal.wixproj21
-rw-r--r--Tools/msi/bundle/releaseweb.wixproj21
-rw-r--r--Tools/msi/bundle/snapshot.wixproj21
-rw-r--r--Tools/msi/common.wxs101
-rw-r--r--Tools/msi/common_en-US.wxl_template17
-rw-r--r--Tools/msi/core/core.wixproj19
-rw-r--r--Tools/msi/core/core.wxs13
-rw-r--r--Tools/msi/core/core_d.wixproj19
-rw-r--r--Tools/msi/core/core_d.wxs13
-rw-r--r--Tools/msi/core/core_en-US.wxl5
-rw-r--r--Tools/msi/core/core_files.wxs31
-rw-r--r--Tools/msi/core/core_pdb.wixproj19
-rw-r--r--Tools/msi/core/core_pdb.wxs13
-rw-r--r--Tools/msi/crt/crt.wixproj20
-rw-r--r--Tools/msi/crt/crt.wxs26
-rw-r--r--Tools/msi/crt/crt_en-US.wxl5
-rw-r--r--Tools/msi/crt/crt_files.12.0.wxs20
-rw-r--r--Tools/msi/crt/crt_files.14.0.wxs40
-rw-r--r--Tools/msi/crt/crtlicense.txt47
-rw-r--r--Tools/msi/csv_to_wxs.py127
-rw-r--r--Tools/msi/dev/dev.wixproj49
-rw-r--r--Tools/msi/dev/dev.wxs18
-rw-r--r--Tools/msi/dev/dev_d.wixproj19
-rw-r--r--Tools/msi/dev/dev_d.wxs13
-rw-r--r--Tools/msi/dev/dev_en-US.wxl5
-rw-r--r--Tools/msi/dev/dev_files.wxs42
-rw-r--r--Tools/msi/doc/doc.wixproj30
-rw-r--r--Tools/msi/doc/doc.wxs28
-rw-r--r--Tools/msi/doc/doc_en-US.wxl_template7
-rw-r--r--Tools/msi/doc/doc_files.wxs12
-rw-r--r--Tools/msi/doc/doc_no_files.wxs17
-rw-r--r--Tools/msi/exe/crtlicense.txt (renamed from Tools/msi/crtlicense.txt)0
-rw-r--r--Tools/msi/exe/exe.wixproj43
-rw-r--r--Tools/msi/exe/exe.wxs28
-rw-r--r--Tools/msi/exe/exe_d.wixproj20
-rw-r--r--Tools/msi/exe/exe_d.wxs13
-rw-r--r--Tools/msi/exe/exe_en-US.wxl_template9
-rw-r--r--Tools/msi/exe/exe_files.wxs68
-rw-r--r--Tools/msi/exe/exe_pdb.wixproj20
-rw-r--r--Tools/msi/exe/exe_pdb.wxs13
-rw-r--r--Tools/msi/get_wix.py49
-rw-r--r--Tools/msi/launcher/launcher.wixproj20
-rw-r--r--Tools/msi/launcher/launcher.wxs31
-rw-r--r--Tools/msi/launcher/launcher_en-US.wxl8
-rw-r--r--Tools/msi/launcher/launcher_files.wxs24
-rw-r--r--Tools/msi/launcher/launcher_reg.wxs32
-rw-r--r--Tools/msi/lib/lib.wixproj34
-rw-r--r--Tools/msi/lib/lib.wxs16
-rw-r--r--Tools/msi/lib/lib_d.wixproj19
-rw-r--r--Tools/msi/lib/lib_d.wxs13
-rw-r--r--Tools/msi/lib/lib_en-US.wxl5
-rw-r--r--Tools/msi/lib/lib_files.wxs72
-rw-r--r--Tools/msi/lib/lib_pdb.wixproj19
-rw-r--r--Tools/msi/lib/lib_pdb.wxs13
-rw-r--r--Tools/msi/msi.props160
-rw-r--r--Tools/msi/msi.py1454
-rw-r--r--Tools/msi/msi.targets62
-rw-r--r--Tools/msi/msilib.py679
-rw-r--r--Tools/msi/msisupport.c93
-rw-r--r--Tools/msi/msisupport.mak9
-rw-r--r--Tools/msi/path/path.wixproj19
-rw-r--r--Tools/msi/path/path.wxs35
-rw-r--r--Tools/msi/path/path_en-US.wxl6
-rw-r--r--Tools/msi/pip/pip.wixproj19
-rw-r--r--Tools/msi/pip/pip.wxs41
-rw-r--r--Tools/msi/pip/pip_en-US.wxl6
-rw-r--r--Tools/msi/schema.py1007
-rw-r--r--Tools/msi/sequence.py126
-rw-r--r--Tools/msi/tcltk/tcltk.wixproj49
-rw-r--r--Tools/msi/tcltk/tcltk.wxs40
-rw-r--r--Tools/msi/tcltk/tcltk_d.wixproj28
-rw-r--r--Tools/msi/tcltk/tcltk_d.wxs14
-rw-r--r--Tools/msi/tcltk/tcltk_en-US.wxl_template8
-rw-r--r--Tools/msi/tcltk/tcltk_files.wxs35
-rw-r--r--Tools/msi/tcltk/tcltk_pdb.wixproj19
-rw-r--r--Tools/msi/tcltk/tcltk_pdb.wxs13
-rw-r--r--Tools/msi/test/test.wixproj29
-rw-r--r--Tools/msi/test/test.wxs15
-rw-r--r--Tools/msi/test/test_d.wixproj19
-rw-r--r--Tools/msi/test/test_d.wxs13
-rw-r--r--Tools/msi/test/test_en-US.wxl7
-rw-r--r--Tools/msi/test/test_files.wxs65
-rw-r--r--Tools/msi/test/test_pdb.wixproj19
-rw-r--r--Tools/msi/test/test_pdb.wxs13
-rw-r--r--Tools/msi/testrelease.bat100
-rw-r--r--Tools/msi/tools/tools.wixproj43
-rw-r--r--Tools/msi/tools/tools.wxs14
-rw-r--r--Tools/msi/tools/tools_en-US.wxl5
-rw-r--r--Tools/msi/tools/tools_files.wxs16
-rw-r--r--Tools/msi/uisample.py1400
-rw-r--r--Tools/msi/wix.props12
-rw-r--r--Tools/parser/unparse.py2
-rwxr-xr-xTools/scripts/diff.py34
-rw-r--r--Tools/scripts/generate_opcode_h.py54
-rw-r--r--Tools/scripts/run_tests.py8
-rw-r--r--Tools/scripts/win_add2path.py3
-rw-r--r--Tools/ssl/sslspeed.vcxproj70
-rw-r--r--Tools/unicode/makeunicodedata.py2
-rwxr-xr-xconfigure324
-rw-r--r--configure.ac129
-rw-r--r--pyconfig.h.in12
-rw-r--r--setup.py48
998 files changed, 94017 insertions, 93422 deletions
diff --git a/.gitignore b/.gitignore
index ba9ee85..fb3dd5b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,11 +9,8 @@
*~
.gdb_history
Doc/build/
-Doc/tools/docutils/
-Doc/tools/jinja/
-Doc/tools/jinja2/
-Doc/tools/pygments/
-Doc/tools/sphinx/
+Doc/venv/
+Lib/distutils/command/*.pdb
Lib/lib2to3/*.pickle
Lib/test/data/*
Lib/_sysconfigdata.py
@@ -27,18 +24,30 @@ Modules/Setup.config
Modules/Setup.local
Modules/config.c
Modules/ld_so_aix
-Modules/_freeze_importlib
-Modules/_testembed
-PCbuild/*.bsc
-PCbuild/*.dll
-PCbuild/*.exe
-PCbuild/*.exp
-PCbuild/*.lib
-PCbuild/*.ncb
-PCbuild/*.o
-PCbuild/*.pdb
-PCbuild/Win32-temp-*
+Programs/_freeze_importlib
+Programs/_testembed
+PC/python_nt*.h
+PC/pythonnt_rc*.h
+PC/*/*.exe
+PC/*/*.exp
+PC/*/*.lib
+PC/*/*.bsc
+PC/*/*.dll
+PC/*/*.pdb
+PC/*/*.user
+PC/*/*.ncb
+PC/*/*.suo
+PC/*/Win32-temp-*
+PC/*/x64-temp-*
+PC/*/amd64
+PCbuild/*.user
+PCbuild/*.suo
+PCbuild/*.*sdf
+PCbuild/*-pgi
+PCbuild/*-pgo
PCbuild/amd64/
+PCbuild/obj
+PCBuild/win32
.purify
Parser/pgen
__pycache__
@@ -61,6 +70,7 @@ pyconfig.h
python
python-config
python-config.py
+python.bat
python.exe
python-gdb.py
python.exe-gdb.py
@@ -72,3 +82,6 @@ TAGS
coverage/
externals/
htmlcov/
+Tools/msi/obj
+Tools/ssl/amd64
+Tools/ssl/win32
diff --git a/.hgignore b/.hgignore
index 9e5a583..7a52606 100644
--- a/.hgignore
+++ b/.hgignore
@@ -9,6 +9,7 @@ TAGS$
autom4te.cache$
^build/
^Doc/build/
+^Doc/venv/
buildno$
config.cache
config.log
@@ -18,6 +19,7 @@ db_home
platform$
pyconfig.h$
python$
+python.bat$
python.exe$
python-config$
python-config.py$
@@ -48,13 +50,12 @@ libpython*.so*
*.pyd
*.cover
*~
+Lib/distutils/command/*.pdb
Lib/lib2to3/*.pickle
Lib/test/data/*
Misc/*.wpu
PC/python_nt*.h
PC/pythonnt_rc*.h
-PC/*.obj
-PC/*.exe
PC/*/*.exe
PC/*/*.exp
PC/*/*.lib
@@ -67,29 +68,20 @@ PC/*/*.suo
PC/*/Win32-temp-*
PC/*/x64-temp-*
PC/*/amd64
-PCbuild/*.exe
-PCbuild/*.dll
-PCbuild/*.pdb
-PCbuild/*.lib
-PCbuild/*.exp
-PCbuild/*.o
-PCbuild/*.ncb
-PCbuild/*.bsc
PCbuild/*.user
PCbuild/*.suo
PCbuild/*.*sdf
-PCbuild/Win32-temp-*
-PCbuild/x64-temp-*
PCbuild/*-pgi
PCbuild/*-pgo
PCbuild/amd64
-PCbuild/ipch
+PCbuild/obj
+PCbuild/win32
Tools/unicode/build/
Tools/unicode/MAPPINGS/
BuildLog.htm
__pycache__
-Modules/_freeze_importlib
-Modules/_testembed
+Programs/_freeze_importlib
+Programs/_testembed
.coverage
coverage/
externals/
@@ -98,3 +90,6 @@ htmlcov/
*.gcno
*.gcov
coverage.info
+Tools/msi/obj
+Tools/ssl/amd64
+Tools/ssl/win32
diff --git a/.hgtags b/.hgtags
index b1e8eee..a2caf77 100644
--- a/.hgtags
+++ b/.hgtags
@@ -1,145 +1,146 @@
-64cc5439e10a6fdf984effaf0141e94fa4cc1004 v0.9.8
-78a7ed6953025e7ecdde9585099b01a6ae40b76a v0.9.9
-b15b8cc9b8d10e0352a0b8b7e8d51fa309db6df3 v1.0.1
-0326b5d61445ee3a8d3de28119f9652cb72d2e3f v1.0.2
-832615ec07646e310c85316b8ba6bc9b17ad3547 v1.1
-9895475d18c7b5f32adaf78f71886ae041e4d10c v1.1.1
-16eb4c51ee97169046340998e850a63c65225b0a v1.2b1
-b45c688756d04fb84d4a0d518fc3d7e3cb25fa8d v1.2b2
-9e82daf7605bad7976a9abc997cb5e0abe434078 v1.2b3
-065e31cf5862e27521cf5725b003aed211f091b2 v1.2b4
-e72257e655454d569468da8b1189e0ec336f3536 v1.2
-e63d83f8275853aaaa3d1972cb86564505e65583 v1.3b1
-7d743c865a9aa6bde8b603e32e0542031bba3c33 v1.3
-4fc85c82cc222554ae6b9c0b87776ed5f2b70c6e v1.4b1
-129f1299d4e97e884bbbbdd00baf101d178973e6 v1.4b2
-44a82ac654a4175569deed8e8a94b0cc8edee08d v1.4b3
-db49494c93dc73de06d5721c74eab533a947a92c v1.4
-062aed8a4ce2c91c81b80e29f02faff1cf5a761b v1.5a1
-c9498ac988372575cf7028b86395b900c9b0a840 v1.5a2
-dc5c968ec992aab3d40a7189df0c443d1c7a1a68 v1.5a3
-746654a0af680c7d9b814b210a026eb91bec9533 v1.5a4
-8ff58b5730f06be08fbbdc2bf592226f7a736201 v1.5b1
-eb78658d819fb0af09a8e6f9bedcb670805ed5f6 v1.5b2
-84461011a1a0ab402e352f06748f29fb5b5559e5 v1.5
-44aba4d26b01fbae0403efe654f9fd0347606732 v1.5.1
-fed63ccbe6dc3ac663bfe97a2f7006b1b28568f9 v1.5.2a1
-21d71f2e27248a0f4e393d0fc321ecf9b89321d2 v1.5.2a2
-f08c7a2a56f80741f5f192fd0ebe0b0967a203cf v1.5.2b1
-8fe7ec4b4fc1518fcac89e6bf674fbbce16150a9 v1.5.2b2
-39fb0dcc83dc375c1565ba65dbce0ed59b1359c9 v1.5.2c1
-61c91c7f101bab3149adfcd5646ae40e048de712 v1.5.2
-605eb9326ffe1fd1e43f40e2338d6652ab449fdf v1.6a1
-011bee8fd9f7f4da457ec71596484fb0882c0614 v1.6a2
-35c4fc1414a59888614b9be784a25f233ba67984 v2.0b1
-55bba197d4870cdae62aeca00e20240a756b84f8 v2.0b2
-e276329cce036a5f9e9d3451256dca5984e543dc v2.0c1
-2fa4e35083e02342ca014bf5bfba46aecb816c31 v2.0
-b60831eeab5a06dd3c5e8297a99e39297aa8794b v2.1a1
-b382f1f07ec6b2c95551658b30c6139eeb32077a v2.1a2
-d0c830db5e68edd4aaa3401216e610c9ff145826 v2.1b1
-b59a536ae1ef3774fd85c17f623e8926b7b6c095 v2.1b2
-d611276e9ad53b5d32d1e8065e1d811c32f7d96f v2.1c1
-ff065e674af6c9ab895bd9eff7d9e9039a376c7d v2.1c2
-020e95d8180d7943fe54701e1db0a7d7d87e2b1e v2.1
-08796a137f1ada2462f7a3177306df5f67a767e1 v2.2a3
-d054c29647f90bccb8345bd779bca1eecf2dd7f2 v2.3c1
-fce5c9e9abc722394cb2e909b7e2a39080d4448e v2.3c2
-92ca658fd420095b6284c9ce6e9082a80285ec9c v2.4a1
-055fc6955f3c6522bfeb7ed4c671c97d5baaaac2 v2.4a2
-186b72550e53533ef6175f6411f932c1298193d7 v2.4a3
-53cff04283855adf88ed0c0fd3698827ca843637 v2.4b1
-7e387a9dcc79954a77695adef8b593da35be1214 v2.4b2
-ff80d8bbef6e13426c8a85d7f9d837b8f8f89834 v2.4c1
-f31e18d313c7a4fc66914b2d27e130a0f72c0b69 v2.4
-cd3f783cd08a16781e236c0b9cb5717d1d995fa9 v3.0a1
-65e82140e281bf26f2e22eda05a7f9956c420f8b v3.0a2
-df15827f34881b9af0936350813ced5c123c8230 v3.0a3
-15f773f7300e372c56a21d59fe49ca26955a6477 v3.0a4
-e35935475153377d6727d64e6c52f72c3b84015b v3.0a5
-a335c4d643b1cfe14197a9ef195c9b2804f608fc v3.0b1
-16ec4bb14a68ea428acf09ebf0c92981da2646f3 v3.0b2
-509e30a7968e01be329ec121540b3e755fc4e566 v3.0b3
-507ede9c7f7f475dfafbd4a52c22d767d10a2bc0 v3.0rc1
-8fae465a39627b590385462e6095eb63af45240a v3.0rc2
-e83a60c69d53f5551a306e77a6d38e9b11485496 v3.0rc3
-bc1ce368986e45b1faf96f93995df46bcd75e7b8 v3.1a1
-ee430e5075db2adf8124e6b94916a89ca41d3171 v3.1a2
-b63020797f9678adaf4d2c3e26574a9eef2ef028 v3.1b1
-4353fd0843cb31b356adc50f93d220e2e7255ef2 v3.1rc1
-0b87e438e1b53e3f812cad963a7fdb65d198ba2f v3.1rc2
-a69a031ac1402dede8b1ef80096436bca6d371f3 v3.1
-35efb1054ec6ceca72017a587263cb6a9257340b v3.1.1rc1
-8b9c0f573ab29c41c6c5fdcca82a1fe0ff5355af v3.1.1
-149b8b87514d10416b598884db5f74651f625b38 v3.1.2rc1
-960efa327c5d9c18df995437b0ac550cb89c9f85 v3.1.2
-d18e9d71f369d8211f6ac87252c6d3211f9bd09f v3.1.3rc1
-a4f75773c0060cee38b0bb651a7aba6f56b0e996 v3.1.3
-32fcb9e94985cb19ce37ba9543f091c0dbe9d7dd v3.1.4rc1
-c918ec9f3a76d6afedfbb5d455004de880443a3d v3.1.4
-ee26aca3219cf4bb0b93352e83edcc9cb28c7802 v3.1.5rc1
-75db2bc69fc9a3e4801e94e3e19801cb096208d8 v3.1.5rc2
-7395330e495ec3316862ca1f6ce0aaf7bdf6785b v3.1.5
-b37b7834757492d009b99cf0ca4d42d2153d7fac v3.2a1
-56d4373cecb73c8b45126ba7b045b3c7b3f94b0b v3.2a2
-da012d9a2c23d144e399d2e01a55b8a83ad94573 v3.2a3
-d92a5b850f5e56808bedc01723906ed64c5e6e2e v3.2a4
-b635cea94195780c8716e236479af319bcc26253 v3.2b1
-e3af5f3a7904c0d5343ec9633ea66e7acfd23a66 v3.2b2
-865d5b24bf28ca41b536befc326407c03e74a4d5 v3.2rc1
-acf3e24dd0d0dfd1e20c907d696d3da965a8f56f v3.2rc2
-18c1f52896501c7ee13b038454a39acb45a87979 v3.2rc3
-a222a015e28d8ae9af3899258dc6c15c3d40add0 v3.2
-8ffac2337a3323323d02153ac919fd1483176652 v3.2.1b1
-cfa9364997c7f2e67b9cbb45c3a5fa3bba4e4999 v3.2.1rc1
-5df549718fb4841ff521fe051f6b54f290fad5d8 v3.2.1rc2
-ac1f7e5c05104d557d5acd922e95625ba5d1fe10 v3.2.1
-c860feaa348d663e598986894ee4680480577e15 v3.2.2rc1
-137e45f15c0bd262c9ad4c032d97425bc0589456 v3.2.2
-7085403daf439adb3f9e70ef13f6bedb1c447376 v3.2.3rc1
-428f05cb7277e1d42bb9dd8d1af6b6270ebc6112 v3.2.3rc2
-3d0686d90f55a78f96d9403da2c52dc2411419d0 v3.2.3
-b2cb7bc1edb8493c0a78f9331eae3e8fba6a881d v3.2.4rc1
-1e10bdeabe3de02f038a63c001911561ac1d13a7 v3.2.4
-cef745775b6583446572cffad704100983db2bea v3.2.5
-51382a5598ec96119cb84594572901c9c964dc3c v3.2.6rc1
-0bd5f4f14de965ca8e44c6e3965fee106176cfc4 v3.2.6
-f1a9a6505731714f0e157453ff850e3b71615c45 v3.3.0a1
-2f69db52d6de306cdaef0a0cc00cc823fb350b01 v3.3.0a2
-0b53b70a40a00013505eb35e3660057b62be77be v3.3.0a3
-7c51388a3aa7ce76a8541bbbdfc05d2d259a162c v3.3.0a4
-e15c554cd43eb23bc0a528a4e8741da9bbec9607 v3.3.0b1
-4972a8f1b2aa3d7cdd64dc96aa7fa112fe1ea343 v3.3.0b2
-8bb5c7bc46ba43804480f3e328e1fa956672c885 v3.3.0rc1
-88a0792e8ba3e4916b24c7e7a522c277d326d66e v3.3.0rc2
-c191d21cefafb3832c45570e84854e309aa62eaa v3.3.0rc3
-bd8afb90ebf28ba4edc901d4a235f75e7bbc79fd v3.3.0
-92c2cfb924055ce68c4f78f836dcfe688437ceb8 v3.3.1rc1
-d9893d13c6289aa03d33559ec67f97dcbf5c9e3c v3.3.1
-d047928ae3f6314a13b6137051315453d0ae89b6 v3.3.2
-fd53c500f8b80f54f3ecedec9da2e8c7e52a6888 v3.3.3rc1
-d32442c0e60dfbd71234e807d3d1dedd227495a9 v3.3.3rc2
-c3896275c0f61b2510a6c7e6c458a750359a91b8 v3.3.3
-fa92f5f940c6c0d839d7f0611e4b717606504a3c v3.3.4rc1
-7ff62415e4263c432c8acf6e424224209211eadb v3.3.4
-9ec811df548ed154a9bf9815383a916d6df31b98 v3.3.5rc1
-ca5635efe090f78806188ac2758f9948596aa8b2 v3.3.5rc2
-62cf4e77f78564714e7ea3d4bf1479ca1fbd0758 v3.3.5
-51317c9786f54267975abf2e9c502e6aaaa4a249 v3.3.6rc1
-971fec30da1fc5bf2b9fb28e09812a5127014211 v3.3.6
-46535f65e7f3bcdcf176f36d34bc1fed719ffd2b v3.4.0a1
-9265a2168e2cb2a84785d8717792acc661e6b692 v3.4.0a2
-dd9cdf90a5073510877e9dd5112f8e6cf20d5e89 v3.4.0a3
-e245b0d7209bb6d0e19316e1e2af1aa9c2139104 v3.4.0a4
-3405dc9a6afaa0a06dd1f6f182ec5c998dce6f5f v3.4.0b1
-ba32913eb13ec545a46dd0ce18035b6c416f0d78 v3.4.0b2
-a97ce3ecc96af79bd2e1ac66ce48d9138e0ca749 v3.4.0b3
-5e088cea8660677969113741c1313d570d977e02 v3.4.0rc1
-a300712ed38c9a242b736c44e806caea25a6dc05 v3.4.0rc2
-8a81cdab3e9d521daaef989fade94b16455fc3b8 v3.4.0rc3
-04f714765c13824c3bc2835d7b008908862e083a v3.4.0
-c67a19e11a7191baf30f313bf55e2e0b6c6f574e v3.4.1rc1
-c0e311e010fcb5bae8d87ca22051cd0845ea0ca0 v3.4.1
-8711a09513848cfc48c689d983495ee64f4668ca v3.4.2rc1
-ab2c023a9432f16652e89c404bbc84aa91bf55af v3.4.2
-69dd528ca6255a66c37cc5cf680e8357d108b036 v3.4.3rc1
+64cc5439e10a6fdf984effaf0141e94fa4cc1004 v0.9.8
+78a7ed6953025e7ecdde9585099b01a6ae40b76a v0.9.9
+b15b8cc9b8d10e0352a0b8b7e8d51fa309db6df3 v1.0.1
+0326b5d61445ee3a8d3de28119f9652cb72d2e3f v1.0.2
+832615ec07646e310c85316b8ba6bc9b17ad3547 v1.1
+9895475d18c7b5f32adaf78f71886ae041e4d10c v1.1.1
+16eb4c51ee97169046340998e850a63c65225b0a v1.2b1
+b45c688756d04fb84d4a0d518fc3d7e3cb25fa8d v1.2b2
+9e82daf7605bad7976a9abc997cb5e0abe434078 v1.2b3
+065e31cf5862e27521cf5725b003aed211f091b2 v1.2b4
+e72257e655454d569468da8b1189e0ec336f3536 v1.2
+e63d83f8275853aaaa3d1972cb86564505e65583 v1.3b1
+7d743c865a9aa6bde8b603e32e0542031bba3c33 v1.3
+4fc85c82cc222554ae6b9c0b87776ed5f2b70c6e v1.4b1
+129f1299d4e97e884bbbbdd00baf101d178973e6 v1.4b2
+44a82ac654a4175569deed8e8a94b0cc8edee08d v1.4b3
+db49494c93dc73de06d5721c74eab533a947a92c v1.4
+062aed8a4ce2c91c81b80e29f02faff1cf5a761b v1.5a1
+c9498ac988372575cf7028b86395b900c9b0a840 v1.5a2
+dc5c968ec992aab3d40a7189df0c443d1c7a1a68 v1.5a3
+746654a0af680c7d9b814b210a026eb91bec9533 v1.5a4
+8ff58b5730f06be08fbbdc2bf592226f7a736201 v1.5b1
+eb78658d819fb0af09a8e6f9bedcb670805ed5f6 v1.5b2
+84461011a1a0ab402e352f06748f29fb5b5559e5 v1.5
+44aba4d26b01fbae0403efe654f9fd0347606732 v1.5.1
+fed63ccbe6dc3ac663bfe97a2f7006b1b28568f9 v1.5.2a1
+21d71f2e27248a0f4e393d0fc321ecf9b89321d2 v1.5.2a2
+f08c7a2a56f80741f5f192fd0ebe0b0967a203cf v1.5.2b1
+8fe7ec4b4fc1518fcac89e6bf674fbbce16150a9 v1.5.2b2
+39fb0dcc83dc375c1565ba65dbce0ed59b1359c9 v1.5.2c1
+61c91c7f101bab3149adfcd5646ae40e048de712 v1.5.2
+605eb9326ffe1fd1e43f40e2338d6652ab449fdf v1.6a1
+011bee8fd9f7f4da457ec71596484fb0882c0614 v1.6a2
+35c4fc1414a59888614b9be784a25f233ba67984 v2.0b1
+55bba197d4870cdae62aeca00e20240a756b84f8 v2.0b2
+e276329cce036a5f9e9d3451256dca5984e543dc v2.0c1
+2fa4e35083e02342ca014bf5bfba46aecb816c31 v2.0
+b60831eeab5a06dd3c5e8297a99e39297aa8794b v2.1a1
+b382f1f07ec6b2c95551658b30c6139eeb32077a v2.1a2
+d0c830db5e68edd4aaa3401216e610c9ff145826 v2.1b1
+b59a536ae1ef3774fd85c17f623e8926b7b6c095 v2.1b2
+d611276e9ad53b5d32d1e8065e1d811c32f7d96f v2.1c1
+ff065e674af6c9ab895bd9eff7d9e9039a376c7d v2.1c2
+020e95d8180d7943fe54701e1db0a7d7d87e2b1e v2.1
+08796a137f1ada2462f7a3177306df5f67a767e1 v2.2a3
+d054c29647f90bccb8345bd779bca1eecf2dd7f2 v2.3c1
+fce5c9e9abc722394cb2e909b7e2a39080d4448e v2.3c2
+92ca658fd420095b6284c9ce6e9082a80285ec9c v2.4a1
+055fc6955f3c6522bfeb7ed4c671c97d5baaaac2 v2.4a2
+186b72550e53533ef6175f6411f932c1298193d7 v2.4a3
+53cff04283855adf88ed0c0fd3698827ca843637 v2.4b1
+7e387a9dcc79954a77695adef8b593da35be1214 v2.4b2
+ff80d8bbef6e13426c8a85d7f9d837b8f8f89834 v2.4c1
+f31e18d313c7a4fc66914b2d27e130a0f72c0b69 v2.4
+cd3f783cd08a16781e236c0b9cb5717d1d995fa9 v3.0a1
+65e82140e281bf26f2e22eda05a7f9956c420f8b v3.0a2
+df15827f34881b9af0936350813ced5c123c8230 v3.0a3
+15f773f7300e372c56a21d59fe49ca26955a6477 v3.0a4
+e35935475153377d6727d64e6c52f72c3b84015b v3.0a5
+a335c4d643b1cfe14197a9ef195c9b2804f608fc v3.0b1
+16ec4bb14a68ea428acf09ebf0c92981da2646f3 v3.0b2
+509e30a7968e01be329ec121540b3e755fc4e566 v3.0b3
+507ede9c7f7f475dfafbd4a52c22d767d10a2bc0 v3.0rc1
+8fae465a39627b590385462e6095eb63af45240a v3.0rc2
+e83a60c69d53f5551a306e77a6d38e9b11485496 v3.0rc3
+bc1ce368986e45b1faf96f93995df46bcd75e7b8 v3.1a1
+ee430e5075db2adf8124e6b94916a89ca41d3171 v3.1a2
+b63020797f9678adaf4d2c3e26574a9eef2ef028 v3.1b1
+4353fd0843cb31b356adc50f93d220e2e7255ef2 v3.1rc1
+0b87e438e1b53e3f812cad963a7fdb65d198ba2f v3.1rc2
+a69a031ac1402dede8b1ef80096436bca6d371f3 v3.1
+35efb1054ec6ceca72017a587263cb6a9257340b v3.1.1rc1
+8b9c0f573ab29c41c6c5fdcca82a1fe0ff5355af v3.1.1
+149b8b87514d10416b598884db5f74651f625b38 v3.1.2rc1
+960efa327c5d9c18df995437b0ac550cb89c9f85 v3.1.2
+d18e9d71f369d8211f6ac87252c6d3211f9bd09f v3.1.3rc1
+a4f75773c0060cee38b0bb651a7aba6f56b0e996 v3.1.3
+32fcb9e94985cb19ce37ba9543f091c0dbe9d7dd v3.1.4rc1
+c918ec9f3a76d6afedfbb5d455004de880443a3d v3.1.4
+ee26aca3219cf4bb0b93352e83edcc9cb28c7802 v3.1.5rc1
+75db2bc69fc9a3e4801e94e3e19801cb096208d8 v3.1.5rc2
+7395330e495ec3316862ca1f6ce0aaf7bdf6785b v3.1.5
+b37b7834757492d009b99cf0ca4d42d2153d7fac v3.2a1
+56d4373cecb73c8b45126ba7b045b3c7b3f94b0b v3.2a2
+da012d9a2c23d144e399d2e01a55b8a83ad94573 v3.2a3
+d92a5b850f5e56808bedc01723906ed64c5e6e2e v3.2a4
+b635cea94195780c8716e236479af319bcc26253 v3.2b1
+e3af5f3a7904c0d5343ec9633ea66e7acfd23a66 v3.2b2
+865d5b24bf28ca41b536befc326407c03e74a4d5 v3.2rc1
+acf3e24dd0d0dfd1e20c907d696d3da965a8f56f v3.2rc2
+18c1f52896501c7ee13b038454a39acb45a87979 v3.2rc3
+a222a015e28d8ae9af3899258dc6c15c3d40add0 v3.2
+8ffac2337a3323323d02153ac919fd1483176652 v3.2.1b1
+cfa9364997c7f2e67b9cbb45c3a5fa3bba4e4999 v3.2.1rc1
+5df549718fb4841ff521fe051f6b54f290fad5d8 v3.2.1rc2
+ac1f7e5c05104d557d5acd922e95625ba5d1fe10 v3.2.1
+c860feaa348d663e598986894ee4680480577e15 v3.2.2rc1
+137e45f15c0bd262c9ad4c032d97425bc0589456 v3.2.2
+7085403daf439adb3f9e70ef13f6bedb1c447376 v3.2.3rc1
+428f05cb7277e1d42bb9dd8d1af6b6270ebc6112 v3.2.3rc2
+3d0686d90f55a78f96d9403da2c52dc2411419d0 v3.2.3
+b2cb7bc1edb8493c0a78f9331eae3e8fba6a881d v3.2.4rc1
+1e10bdeabe3de02f038a63c001911561ac1d13a7 v3.2.4
+cef745775b6583446572cffad704100983db2bea v3.2.5
+51382a5598ec96119cb84594572901c9c964dc3c v3.2.6rc1
+0bd5f4f14de965ca8e44c6e3965fee106176cfc4 v3.2.6
+f1a9a6505731714f0e157453ff850e3b71615c45 v3.3.0a1
+2f69db52d6de306cdaef0a0cc00cc823fb350b01 v3.3.0a2
+0b53b70a40a00013505eb35e3660057b62be77be v3.3.0a3
+7c51388a3aa7ce76a8541bbbdfc05d2d259a162c v3.3.0a4
+e15c554cd43eb23bc0a528a4e8741da9bbec9607 v3.3.0b1
+4972a8f1b2aa3d7cdd64dc96aa7fa112fe1ea343 v3.3.0b2
+8bb5c7bc46ba43804480f3e328e1fa956672c885 v3.3.0rc1
+88a0792e8ba3e4916b24c7e7a522c277d326d66e v3.3.0rc2
+c191d21cefafb3832c45570e84854e309aa62eaa v3.3.0rc3
+bd8afb90ebf28ba4edc901d4a235f75e7bbc79fd v3.3.0
+92c2cfb924055ce68c4f78f836dcfe688437ceb8 v3.3.1rc1
+d9893d13c6289aa03d33559ec67f97dcbf5c9e3c v3.3.1
+d047928ae3f6314a13b6137051315453d0ae89b6 v3.3.2
+fd53c500f8b80f54f3ecedec9da2e8c7e52a6888 v3.3.3rc1
+d32442c0e60dfbd71234e807d3d1dedd227495a9 v3.3.3rc2
+c3896275c0f61b2510a6c7e6c458a750359a91b8 v3.3.3
+fa92f5f940c6c0d839d7f0611e4b717606504a3c v3.3.4rc1
+7ff62415e4263c432c8acf6e424224209211eadb v3.3.4
+9ec811df548ed154a9bf9815383a916d6df31b98 v3.3.5rc1
+ca5635efe090f78806188ac2758f9948596aa8b2 v3.3.5rc2
+62cf4e77f78564714e7ea3d4bf1479ca1fbd0758 v3.3.5
+51317c9786f54267975abf2e9c502e6aaaa4a249 v3.3.6rc1
+971fec30da1fc5bf2b9fb28e09812a5127014211 v3.3.6
+46535f65e7f3bcdcf176f36d34bc1fed719ffd2b v3.4.0a1
+9265a2168e2cb2a84785d8717792acc661e6b692 v3.4.0a2
+dd9cdf90a5073510877e9dd5112f8e6cf20d5e89 v3.4.0a3
+e245b0d7209bb6d0e19316e1e2af1aa9c2139104 v3.4.0a4
+3405dc9a6afaa0a06dd1f6f182ec5c998dce6f5f v3.4.0b1
+ba32913eb13ec545a46dd0ce18035b6c416f0d78 v3.4.0b2
+a97ce3ecc96af79bd2e1ac66ce48d9138e0ca749 v3.4.0b3
+5e088cea8660677969113741c1313d570d977e02 v3.4.0rc1
+a300712ed38c9a242b736c44e806caea25a6dc05 v3.4.0rc2
+8a81cdab3e9d521daaef989fade94b16455fc3b8 v3.4.0rc3
+04f714765c13824c3bc2835d7b008908862e083a v3.4.0
+c67a19e11a7191baf30f313bf55e2e0b6c6f574e v3.4.1rc1
+c0e311e010fcb5bae8d87ca22051cd0845ea0ca0 v3.4.1
+8711a09513848cfc48c689d983495ee64f4668ca v3.4.2rc1
+ab2c023a9432f16652e89c404bbc84aa91bf55af v3.4.2
+69dd528ca6255a66c37cc5cf680e8357d108b036 v3.4.3rc1
+5d4b6a57d5fd7564bf73f3db0e46fe5eeb00bcd8 v3.5.0a1
diff --git a/.hgtouch b/.hgtouch
index 7e3a5e7..b9be0f1 100644
--- a/.hgtouch
+++ b/.hgtouch
@@ -2,7 +2,9 @@
# Define dependencies of generated files that are checked into hg.
# The syntax of this file uses make rule dependencies, without actions
-Python/importlib.h: Lib/importlib/_bootstrap.py Modules/_freeze_importlib.c
+Python/importlib.h: Lib/importlib/_bootstrap.py Programs/_freeze_importlib.c
+
+Include/opcode.h: Lib/opcode.py Tools/scripts/generate_opcode_h.py
Include/Python-ast.h: Parser/Python.asdl Parser/asdl.py Parser/asdl_c.py
Python/Python-ast.c: Include/Python-ast.h
diff --git a/Doc/Makefile b/Doc/Makefile
index ea30231..200a9f7 100644
--- a/Doc/Makefile
+++ b/Doc/Makefile
@@ -15,11 +15,12 @@ ALLSPHINXOPTS = -b $(BUILDER) -d build/doctrees -D latex_paper_size=$(PAPER) \
.PHONY: help build html htmlhelp latex text changes linkcheck \
suspicious coverage doctest pydoc-topics htmlview clean dist check serve \
- autobuild-dev autobuild-stable
+ autobuild-dev autobuild-stable venv
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " clean to remove build files"
+ @echo " venv to create a venv with necessary tools"
@echo " html to make standalone HTML files"
@echo " htmlview to open the index page built by the html target in your browser"
@echo " htmlhelp to make HTML files and a HTML help project"
@@ -102,7 +103,11 @@ htmlview: html
$(PYTHON) -c "import webbrowser; webbrowser.open('build/html/index.html')"
clean:
- -rm -rf build/*
+ -rm -rf build/* venv/*
+
+venv:
+ $(PYTHON) -m venv venv
+ ./venv/bin/python3 -m pip install -U Sphinx
dist:
rm -rf dist
@@ -172,4 +177,3 @@ autobuild-stable:
exit 1;; \
esac
@make autobuild-dev
-
diff --git a/Doc/README.txt b/Doc/README.txt
index 58bda62..580d102 100644
--- a/Doc/README.txt
+++ b/Doc/README.txt
@@ -3,7 +3,7 @@ Python Documentation README
This directory contains the reStructuredText (reST) sources to the Python
documentation. You don't need to build them yourself, prebuilt versions are
-available at <https://docs.python.org/3.4/download.html>.
+available at <https://docs.python.org/dev/download.html>.
Documentation on authoring Python documentation, including information about
both style and markup, is available in the "Documenting Python" chapter of the
diff --git a/Doc/c-api/codec.rst b/Doc/c-api/codec.rst
index 83252af..dfe3d43 100644
--- a/Doc/c-api/codec.rst
+++ b/Doc/c-api/codec.rst
@@ -116,3 +116,8 @@ Registry API for Unicode encoding error handlers
Replace the unicode encode error with backslash escapes (``\x``, ``\u`` and
``\U``).
+.. c:function:: PyObject* PyCodec_NameReplaceErrors(PyObject *exc)
+
+ Replace the unicode encode error with ``\N{...}`` escapes.
+
+ .. versionadded:: 3.5
diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst
index 66b7752..814317b 100644
--- a/Doc/c-api/exceptions.rst
+++ b/Doc/c-api/exceptions.rst
@@ -9,13 +9,19 @@ Exception Handling
The functions described in this chapter will let you handle and raise Python
exceptions. It is important to understand some of the basics of Python
-exception handling. It works somewhat like the Unix :c:data:`errno` variable:
+exception handling. It works somewhat like the POSIX :c:data:`errno` variable:
there is a global indicator (per thread) of the last error that occurred. Most
-functions don't clear this on success, but will set it to indicate the cause of
-the error on failure. Most functions also return an error indicator, usually
-*NULL* if they are supposed to return a pointer, or ``-1`` if they return an
-integer (exception: the :c:func:`PyArg_\*` functions return ``1`` for success and
-``0`` for failure).
+C API functions don't clear this on success, but will set it to indicate the
+cause of the error on failure. Most C API functions also return an error
+indicator, usually *NULL* if they are supposed to return a pointer, or ``-1``
+if they return an integer (exception: the :c:func:`PyArg_\*` functions
+return ``1`` for success and ``0`` for failure).
+
+Concretely, the error indicator consists of three object pointers: the
+exception's type, the exception's value, and the traceback object. Any
+of those pointers can be NULL if non-set (although some combinations are
+forbidden, for example you can't have a non-NULL traceback if the exception
+type is NULL).
When a function must fail because some function it called failed, it generally
doesn't set the error indicator; the function it called already set it. It is
@@ -27,12 +33,21 @@ the caller that an error has been set. If the error is not handled or carefully
propagated, additional calls into the Python/C API may not behave as intended
and may fail in mysterious ways.
-The error indicator consists of three Python objects corresponding to the result
-of ``sys.exc_info()``. API functions exist to interact with the error indicator
-in various ways. There is a separate error indicator for each thread.
+.. note::
+ The error indicator is **not** the result of :func:`sys.exc_info()`.
+ The former corresponds to an exception that is not yet caught (and is
+ therefore still propagating), while the latter returns an exception after
+ it is caught (and has therefore stopped propagating).
-.. XXX Order of these should be more thoughtful.
- Either alphabetical or some kind of structure.
+
+Printing and clearing
+=====================
+
+
+.. c:function:: void PyErr_Clear()
+
+ Clear the error indicator. If the error indicator is not set, there is no
+ effect.
.. c:function:: void PyErr_PrintEx(int set_sys_last_vars)
@@ -51,127 +66,24 @@ in various ways. There is a separate error indicator for each thread.
Alias for ``PyErr_PrintEx(1)``.
-.. c:function:: PyObject* PyErr_Occurred()
-
- Test whether the error indicator is set. If set, return the exception *type*
- (the first argument to the last call to one of the :c:func:`PyErr_Set\*`
- functions or to :c:func:`PyErr_Restore`). If not set, return *NULL*. You do not
- own a reference to the return value, so you do not need to :c:func:`Py_DECREF`
- it.
-
- .. note::
-
- Do not compare the return value to a specific exception; use
- :c:func:`PyErr_ExceptionMatches` instead, shown below. (The comparison could
- easily fail since the exception may be an instance instead of a class, in the
- case of a class exception, or it may be a subclass of the expected exception.)
-
-
-.. c:function:: int PyErr_ExceptionMatches(PyObject *exc)
-
- Equivalent to ``PyErr_GivenExceptionMatches(PyErr_Occurred(), exc)``. This
- should only be called when an exception is actually set; a memory access
- violation will occur if no exception has been raised.
-
-
-.. c:function:: int PyErr_GivenExceptionMatches(PyObject *given, PyObject *exc)
-
- Return true if the *given* exception matches the exception in *exc*. If
- *exc* is a class object, this also returns true when *given* is an instance
- of a subclass. If *exc* is a tuple, all exceptions in the tuple (and
- recursively in subtuples) are searched for a match.
-
-
-.. c:function:: void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb)
-
- Under certain circumstances, the values returned by :c:func:`PyErr_Fetch` below
- can be "unnormalized", meaning that ``*exc`` is a class object but ``*val`` is
- not an instance of the same class. This function can be used to instantiate
- the class in that case. If the values are already normalized, nothing happens.
- The delayed normalization is implemented to improve performance.
-
- .. note::
-
- This function *does not* implicitly set the ``__traceback__``
- attribute on the exception value. If setting the traceback
- appropriately is desired, the following additional snippet is needed::
-
- if (tb != NULL) {
- PyException_SetTraceback(val, tb);
- }
-
-
-.. c:function:: void PyErr_Clear()
-
- Clear the error indicator. If the error indicator is not set, there is no
- effect.
-
-
-.. c:function:: void PyErr_Fetch(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
-
- Retrieve the error indicator into three variables whose addresses are passed.
- If the error indicator is not set, set all three variables to *NULL*. If it is
- set, it will be cleared and you own a reference to each object retrieved. The
- value and traceback object may be *NULL* even when the type object is not.
-
- .. note::
-
- This function is normally only used by code that needs to handle exceptions or
- by code that needs to save and restore the error indicator temporarily.
-
-
-.. c:function:: void PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
-
- Set the error indicator from the three objects. If the error indicator is
- already set, it is cleared first. If the objects are *NULL*, the error
- indicator is cleared. Do not pass a *NULL* type and non-*NULL* value or
- traceback. The exception type should be a class. Do not pass an invalid
- exception type or value. (Violating these rules will cause subtle problems
- later.) This call takes away a reference to each object: you must own a
- reference to each object before the call and after the call you no longer own
- these references. (If you don't understand this, don't use this function. I
- warned you.)
-
- .. note::
-
- This function is normally only used by code that needs to save and restore the
- error indicator temporarily; use :c:func:`PyErr_Fetch` to save the current
- exception state.
-
-
-.. c:function:: void PyErr_GetExcInfo(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
-
- Retrieve the exception info, as known from ``sys.exc_info()``. This refers
- to an exception that was already caught, not to an exception that was
- freshly raised. Returns new references for the three objects, any of which
- may be *NULL*. Does not modify the exception info state.
-
- .. note::
-
- This function is not normally used by code that wants to handle exceptions.
- Rather, it can be used when code needs to save and restore the exception
- state temporarily. Use :c:func:`PyErr_SetExcInfo` to restore or clear the
- exception state.
-
- .. versionadded:: 3.3
-
+.. c:function:: void PyErr_WriteUnraisable(PyObject *obj)
-.. c:function:: void PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback)
+ This utility function prints a warning message to ``sys.stderr`` when an
+ exception has been set but it is impossible for the interpreter to actually
+ raise the exception. It is used, for example, when an exception occurs in an
+ :meth:`__del__` method.
- Set the exception info, as known from ``sys.exc_info()``. This refers
- to an exception that was already caught, not to an exception that was
- freshly raised. This function steals the references of the arguments.
- To clear the exception state, pass *NULL* for all three arguments.
- For general rules about the three arguments, see :c:func:`PyErr_Restore`.
+ The function is called with a single argument *obj* that identifies the context
+ in which the unraisable exception occurred. The repr of *obj* will be printed in
+ the warning message.
- .. note::
- This function is not normally used by code that wants to handle exceptions.
- Rather, it can be used when code needs to save and restore the exception
- state temporarily. Use :c:func:`PyErr_GetExcInfo` to read the exception
- state.
+Raising exceptions
+==================
- .. versionadded:: 3.3
+These functions help you set the current thread's error indicator.
+For convenience, some of these functions will always return a
+NULL pointer for use in a ``return`` statement.
.. c:function:: void PyErr_SetString(PyObject *type, const char *message)
@@ -197,6 +109,14 @@ in various ways. There is a separate error indicator for each thread.
string.
+.. c:function:: PyObject* PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
+
+ Same as :c:func:`PyErr_Format`, but taking a :c:type:`va_list` argument rather
+ than a variable number of arguments.
+
+ .. versionadded:: 3.5
+
+
.. c:function:: void PyErr_SetNone(PyObject *type)
This is a shorthand for ``PyErr_SetObject(type, Py_None)``.
@@ -346,6 +266,22 @@ in various ways. There is a separate error indicator for each thread.
use.
+Issuing warnings
+================
+
+Use these functions to issue warnings from C code. They mirror similar
+functions exported by the Python :mod:`warnings` module. They normally
+print a warning message to *sys.stderr*; however, it is
+also possible that the user has specified that warnings are to be turned into
+errors, and in that case they will raise an exception. It is also possible that
+the functions raise an exception because of a problem with the warning machinery.
+The return value is ``0`` if no exception is raised, or ``-1`` if an exception
+is raised. (It is not possible to determine whether a warning message is
+actually printed, nor what the reason is for the exception; this is
+intentional.) If an exception is raised, the caller should do its normal
+exception handling (for example, :c:func:`Py_DECREF` owned references and return
+an error value).
+
.. c:function:: int PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level)
Issue a warning message. The *category* argument is a warning category (see
@@ -355,18 +291,6 @@ in various ways. There is a separate error indicator for each thread.
is the function calling :c:func:`PyErr_WarnEx`, 2 is the function above that,
and so forth.
- This function normally prints a warning message to *sys.stderr*; however, it is
- also possible that the user has specified that warnings are to be turned into
- errors, and in that case this will raise an exception. It is also possible that
- the function raises an exception because of a problem with the warning machinery
- (the implementation imports the :mod:`warnings` module to do the heavy lifting).
- The return value is ``0`` if no exception is raised, or ``-1`` if an exception
- is raised. (It is not possible to determine whether a warning message is
- actually printed, nor what the reason is for the exception; this is
- intentional.) If an exception is raised, the caller should do its normal
- exception handling (for example, :c:func:`Py_DECREF` owned references and return
- an error value).
-
Warning categories must be subclasses of :c:data:`Warning`; the default warning
category is :c:data:`RuntimeWarning`. The standard Python warning categories are
available as global variables whose names are ``PyExc_`` followed by the Python
@@ -410,6 +334,139 @@ in various ways. There is a separate error indicator for each thread.
.. versionadded:: 3.2
+Querying the error indicator
+============================
+
+.. c:function:: PyObject* PyErr_Occurred()
+
+ Test whether the error indicator is set. If set, return the exception *type*
+ (the first argument to the last call to one of the :c:func:`PyErr_Set\*`
+ functions or to :c:func:`PyErr_Restore`). If not set, return *NULL*. You do not
+ own a reference to the return value, so you do not need to :c:func:`Py_DECREF`
+ it.
+
+ .. note::
+
+ Do not compare the return value to a specific exception; use
+ :c:func:`PyErr_ExceptionMatches` instead, shown below. (The comparison could
+ easily fail since the exception may be an instance instead of a class, in the
+ case of a class exception, or it may be a subclass of the expected exception.)
+
+
+.. c:function:: int PyErr_ExceptionMatches(PyObject *exc)
+
+ Equivalent to ``PyErr_GivenExceptionMatches(PyErr_Occurred(), exc)``. This
+ should only be called when an exception is actually set; a memory access
+ violation will occur if no exception has been raised.
+
+
+.. c:function:: int PyErr_GivenExceptionMatches(PyObject *given, PyObject *exc)
+
+ Return true if the *given* exception matches the exception type in *exc*. If
+ *exc* is a class object, this also returns true when *given* is an instance
+ of a subclass. If *exc* is a tuple, all exception types in the tuple (and
+ recursively in subtuples) are searched for a match.
+
+
+.. c:function:: void PyErr_Fetch(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
+
+ Retrieve the error indicator into three variables whose addresses are passed.
+ If the error indicator is not set, set all three variables to *NULL*. If it is
+ set, it will be cleared and you own a reference to each object retrieved. The
+ value and traceback object may be *NULL* even when the type object is not.
+
+ .. note::
+
+ This function is normally only used by code that needs to catch exceptions or
+ by code that needs to save and restore the error indicator temporarily, e.g.::
+
+ {
+ PyObject **type, **value, **traceback;
+ PyErr_Fetch(&type, &value, &traceback);
+
+ /* ... code that might produce other errors ... */
+
+ PyErr_Restore(type, value, traceback);
+ }
+
+
+.. c:function:: void PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
+
+ Set the error indicator from the three objects. If the error indicator is
+ already set, it is cleared first. If the objects are *NULL*, the error
+ indicator is cleared. Do not pass a *NULL* type and non-*NULL* value or
+ traceback. The exception type should be a class. Do not pass an invalid
+ exception type or value. (Violating these rules will cause subtle problems
+ later.) This call takes away a reference to each object: you must own a
+ reference to each object before the call and after the call you no longer own
+ these references. (If you don't understand this, don't use this function. I
+ warned you.)
+
+ .. note::
+
+ This function is normally only used by code that needs to save and restore the
+ error indicator temporarily. Use :c:func:`PyErr_Fetch` to save the current
+ error indicator.
+
+
+.. c:function:: void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb)
+
+ Under certain circumstances, the values returned by :c:func:`PyErr_Fetch` below
+ can be "unnormalized", meaning that ``*exc`` is a class object but ``*val`` is
+ not an instance of the same class. This function can be used to instantiate
+ the class in that case. If the values are already normalized, nothing happens.
+ The delayed normalization is implemented to improve performance.
+
+ .. note::
+
+ This function *does not* implicitly set the ``__traceback__``
+ attribute on the exception value. If setting the traceback
+ appropriately is desired, the following additional snippet is needed::
+
+ if (tb != NULL) {
+ PyException_SetTraceback(val, tb);
+ }
+
+
+.. c:function:: void PyErr_GetExcInfo(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
+
+ Retrieve the exception info, as known from ``sys.exc_info()``. This refers
+ to an exception that was *already caught*, not to an exception that was
+ freshly raised. Returns new references for the three objects, any of which
+ may be *NULL*. Does not modify the exception info state.
+
+ .. note::
+
+ This function is not normally used by code that wants to handle exceptions.
+ Rather, it can be used when code needs to save and restore the exception
+ state temporarily. Use :c:func:`PyErr_SetExcInfo` to restore or clear the
+ exception state.
+
+ .. versionadded:: 3.3
+
+
+.. c:function:: void PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback)
+
+ Set the exception info, as known from ``sys.exc_info()``. This refers
+ to an exception that was *already caught*, not to an exception that was
+ freshly raised. This function steals the references of the arguments.
+ To clear the exception state, pass *NULL* for all three arguments.
+ For general rules about the three arguments, see :c:func:`PyErr_Restore`.
+
+ .. note::
+
+ This function is not normally used by code that wants to handle exceptions.
+ Rather, it can be used when code needs to save and restore the exception
+ state temporarily. Use :c:func:`PyErr_GetExcInfo` to read the exception
+ state.
+
+ .. versionadded:: 3.3
+
+
+Signal Handling
+===============
+
+
.. c:function:: int PyErr_CheckSignals()
.. index::
@@ -443,13 +500,21 @@ in various ways. There is a separate error indicator for each thread.
.. c:function:: int PySignal_SetWakeupFd(int fd)
- This utility function specifies a file descriptor to which a ``'\0'`` byte will
- be written whenever a signal is received. It returns the previous such file
- descriptor. The value ``-1`` disables the feature; this is the initial state.
+ This utility function specifies a file descriptor to which the signal number
+ is written as a single byte whenever a signal is received. *fd* must be
+ non-blocking. It returns the previous such file descriptor.
+
+ The value ``-1`` disables the feature; this is the initial state.
This is equivalent to :func:`signal.set_wakeup_fd` in Python, but without any
error checking. *fd* should be a valid file descriptor. The function should
only be called from the main thread.
+ .. versionchanged:: 3.5
+ On Windows, the function now also supports socket handles.
+
+
+Exception Classes
+=================
.. c:function:: PyObject* PyErr_NewException(char *name, PyObject *base, PyObject *dict)
@@ -475,18 +540,6 @@ in various ways. There is a separate error indicator for each thread.
.. versionadded:: 3.2
-.. c:function:: void PyErr_WriteUnraisable(PyObject *obj)
-
- This utility function prints a warning message to ``sys.stderr`` when an
- exception has been set but it is impossible for the interpreter to actually
- raise the exception. It is used, for example, when an exception occurs in an
- :meth:`__del__` method.
-
- The function is called with a single argument *obj* that identifies the context
- in which the unraisable exception occurred. The repr of *obj* will be printed in
- the warning message.
-
-
Exception Objects
=================
diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst
index 4d358ca..fac9b47 100644
--- a/Doc/c-api/init.rst
+++ b/Doc/c-api/init.rst
@@ -134,6 +134,9 @@ Process-wide parameters
change for the duration of the program's execution. No code in the Python
interpreter will change the contents of this storage.
+ Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
+ :c:type:`wchar_*` string.
+
.. c:function:: wchar* Py_GetProgramName()
@@ -245,6 +248,9 @@ Process-wide parameters
:data:`sys.exec_prefix` to be empty. It is up to the caller to modify these
if required after calling :c:func:`Py_Initialize`.
+ Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
+ :c:type:`wchar_*` string.
+
The path argument is copied internally, so the caller may free it after the
call completes.
@@ -344,6 +350,9 @@ Process-wide parameters
:data:`sys.path`, which is the same as prepending the current working
directory (``"."``).
+ Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
+ :c:type:`wchar_*` string.
+
.. note::
It is recommended that applications embedding the Python interpreter
for purposes other than executing a single script pass 0 as *updatepath*,
@@ -368,6 +377,9 @@ Process-wide parameters
to 1 unless the :program:`python` interpreter was started with the
:option:`-I`.
+ Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
+ :c:type:`wchar_*` string.
+
.. versionchanged:: 3.4 The *updatepath* value depends on :option:`-I`.
@@ -382,6 +394,9 @@ Process-wide parameters
execution. No code in the Python interpreter will change the contents of
this storage.
+ Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
+ :c:type:`wchar_*` string.
+
.. c:function:: w_char* Py_GetPythonHome()
diff --git a/Doc/c-api/memory.rst b/Doc/c-api/memory.rst
index a82e1c2..5d78f38 100644
--- a/Doc/c-api/memory.rst
+++ b/Doc/c-api/memory.rst
@@ -92,8 +92,8 @@ functions are thread-safe, the :term:`GIL <global interpreter lock>` does not
need to be held.
The default raw memory block allocator uses the following functions:
-:c:func:`malloc`, :c:func:`realloc` and :c:func:`free`; call ``malloc(1)`` when
-requesting zero bytes.
+:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`; call
+``malloc(1)`` (or ``calloc(1, 1)``) when requesting zero bytes.
.. versionadded:: 3.4
@@ -106,6 +106,17 @@ requesting zero bytes.
been initialized in any way.
+.. c:function:: void* PyMem_RawCalloc(size_t nelem, size_t elsize)
+
+ Allocates *nelem* elements each whose size in bytes is *elsize* and returns
+ a pointer of type :c:type:`void\*` to the allocated memory, or *NULL* if the
+ request fails. The memory is initialized to zeros. Requesting zero elements
+ or elements of size zero bytes returns a distinct non-*NULL* pointer if
+ possible, as if ``PyMem_RawCalloc(1, 1)`` had been called instead.
+
+ .. versionadded:: 3.5
+
+
.. c:function:: void* PyMem_RawRealloc(void *p, size_t n)
Resizes the memory block pointed to by *p* to *n* bytes. The contents will
@@ -136,8 +147,8 @@ 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:`realloc` and :c:func:`free`; call ``malloc(1)`` when
-requesting zero bytes.
+:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`; call
+``malloc(1)`` (or ``calloc(1, 1)``) when requesting zero bytes.
.. warning::
@@ -152,6 +163,17 @@ requesting zero bytes.
been called instead. The memory will not have been initialized in any way.
+.. c:function:: void* PyMem_Calloc(size_t nelem, size_t elsize)
+
+ Allocates *nelem* elements each whose size in bytes is *elsize* and returns
+ a pointer of type :c:type:`void\*` to the allocated memory, or *NULL* if the
+ request fails. The memory is initialized to zeros. Requesting zero elements
+ or elements of size zero bytes returns a distinct non-*NULL* pointer if
+ possible, as if ``PyMem_Calloc(1, 1)`` had been called instead.
+
+ .. versionadded:: 3.5
+
+
.. c:function:: void* PyMem_Realloc(void *p, size_t n)
Resizes the memory block pointed to by *p* to *n* bytes. The contents will be
@@ -210,7 +232,7 @@ Customize Memory Allocators
.. versionadded:: 3.4
-.. c:type:: PyMemAllocator
+.. c:type:: PyMemAllocatorEx
Structure used to describe a memory block allocator. The structure has
four fields:
@@ -222,11 +244,19 @@ Customize Memory Allocators
+----------------------------------------------------------+---------------------------------------+
| ``void* malloc(void *ctx, size_t size)`` | allocate a memory block |
+----------------------------------------------------------+---------------------------------------+
+ | ``void* calloc(void *ctx, size_t nelem, size_t elsize)`` | allocate a memory block initialized |
+ | | with zeros |
+ +----------------------------------------------------------+---------------------------------------+
| ``void* realloc(void *ctx, void *ptr, size_t new_size)`` | allocate or resize a memory block |
+----------------------------------------------------------+---------------------------------------+
| ``void free(void *ctx, void *ptr)`` | free a memory block |
+----------------------------------------------------------+---------------------------------------+
+ .. versionchanged:: 3.5
+ The :c:type:`PyMemAllocator` structure was renamed to
+ :c:type:`PyMemAllocatorEx` and a new ``calloc`` field was added.
+
+
.. c:type:: PyMemAllocatorDomain
Enum used to identify an allocator domain. Domains:
@@ -239,12 +269,12 @@ Customize Memory Allocators
:c:func:`PyObject_Realloc` and :c:func:`PyObject_Free`
-.. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
+.. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
Get the memory block allocator of the specified domain.
-.. c:function:: void PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
+.. c:function:: void PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
Set the memory block allocator of the specified domain.
diff --git a/Doc/c-api/number.rst b/Doc/c-api/number.rst
index 21951c3..9bcb649 100644
--- a/Doc/c-api/number.rst
+++ b/Doc/c-api/number.rst
@@ -30,6 +30,14 @@ Number Protocol
the equivalent of the Python expression ``o1 * o2``.
+.. c:function:: PyObject* PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2)
+
+ Returns the result of matrix multiplication on *o1* and *o2*, or *NULL* on
+ failure. This is the equivalent of the Python expression ``o1 @ o2``.
+
+ .. versionadded:: 3.5
+
+
.. c:function:: PyObject* PyNumber_FloorDivide(PyObject *o1, PyObject *o2)
Return the floor of *o1* divided by *o2*, or *NULL* on failure. This is
@@ -146,6 +154,15 @@ Number Protocol
the Python statement ``o1 *= o2``.
+.. c:function:: PyObject* PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2)
+
+ Returns the result of matrix multiplication on *o1* and *o2*, or *NULL* on
+ failure. The operation is done *in-place* when *o1* supports it. This is
+ the equivalent of the Python statement ``o1 @= o2``.
+
+ .. versionadded:: 3.5
+
+
.. c:function:: PyObject* PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2)
Returns the mathematical floor of dividing *o1* by *o2*, or *NULL* on failure.
diff --git a/Doc/c-api/sys.rst b/Doc/c-api/sys.rst
index 9760dca..a6a939c 100644
--- a/Doc/c-api/sys.rst
+++ b/Doc/c-api/sys.rst
@@ -47,6 +47,60 @@ Operating System Utilities
not call those functions directly! :c:type:`PyOS_sighandler_t` is a typedef
alias for :c:type:`void (\*)(int)`.
+.. c:function:: wchar_t* Py_DecodeLocale(const char* arg, size_t *size)
+
+ Decode a byte string from the locale encoding with the :ref:`surrogateescape
+ error handler <surrogateescape>`: undecodable bytes are decoded as
+ characters in range U+DC80..U+DCFF. If a byte sequence can be decoded as a
+ surrogate character, escape the bytes using the surrogateescape error
+ handler instead of decoding them.
+
+ Return a pointer to a newly allocated wide character string, use
+ :c:func:`PyMem_RawFree` to free the memory. If size is not ``NULL``, write
+ the number of wide characters excluding the null character into ``*size``
+
+ Return ``NULL`` on decoding error or memory allocation error. If *size* is
+ not ``NULL``, ``*size`` is set to ``(size_t)-1`` on memory error or set to
+ ``(size_t)-2`` on decoding error.
+
+ Decoding errors should never happen, unless there is a bug in the C
+ library.
+
+ Use the :c:func:`Py_EncodeLocale` function to encode the character string
+ back to a byte string.
+
+ .. seealso::
+
+ The :c:func:`PyUnicode_DecodeFSDefaultAndSize` and
+ :c:func:`PyUnicode_DecodeLocaleAndSize` functions.
+
+ .. versionadded:: 3.5
+
+
+.. c:function:: char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
+
+ Encode a wide character string to the locale encoding with the
+ :ref:`surrogateescape error handler <surrogateescape>`: surrogate characters
+ in the range U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
+
+ Return a pointer to a newly allocated byte string, use :c:func:`PyMem_Free`
+ to free the memory. Return ``NULL`` on encoding error or memory allocation
+ error
+
+ If error_pos is not ``NULL``, ``*error_pos`` is set to the index of the
+ invalid character on encoding error, or set to ``(size_t)-1`` otherwise.
+
+ Use the :c:func:`Py_DecodeLocale` function to decode the bytes string back
+ to a wide character string.
+
+ .. seealso::
+
+ The :c:func:`PyUnicode_EncodeFSDefault` and
+ :c:func:`PyUnicode_EncodeLocale` functions.
+
+ .. versionadded:: 3.5
+
+
.. _systemfunctions:
System Functions
diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst
index b43622a..eb63705 100644
--- a/Doc/c-api/typeobj.rst
+++ b/Doc/c-api/typeobj.rst
@@ -1118,6 +1118,9 @@ Number Object Structures
binaryfunc nb_inplace_true_divide;
unaryfunc nb_index;
+
+ binaryfunc nb_matrix_multiply;
+ binaryfunc nb_inplace_matrix_multiply;
} PyNumberMethods;
.. note::
diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst
index 03c284a..00063d0 100644
--- a/Doc/c-api/unicode.rst
+++ b/Doc/c-api/unicode.rst
@@ -759,11 +759,13 @@ system.
*errors* is ``NULL``. *str* must end with a null character but
cannot contain embedded null characters.
+ Use :c:func:`PyUnicode_DecodeFSDefaultAndSize` to decode a string from
+ :c:data:`Py_FileSystemDefaultEncoding` (the locale encoding read at
+ Python startup).
+
.. seealso::
- Use :c:func:`PyUnicode_DecodeFSDefaultAndSize` to decode a string from
- :c:data:`Py_FileSystemDefaultEncoding` (the locale encoding read at
- Python startup).
+ The :c:func:`Py_DecodeLocale` function.
.. versionadded:: 3.3
@@ -784,11 +786,13 @@ system.
*errors* is ``NULL``. Return a :class:`bytes` object. *str* cannot
contain embedded null characters.
+ Use :c:func:`PyUnicode_EncodeFSDefault` to encode a string to
+ :c:data:`Py_FileSystemDefaultEncoding` (the locale encoding read at
+ Python startup).
+
.. seealso::
- Use :c:func:`PyUnicode_EncodeFSDefault` to encode a string to
- :c:data:`Py_FileSystemDefaultEncoding` (the locale encoding read at
- Python startup).
+ The :c:func:`Py_EncodeLocale` function.
.. versionadded:: 3.3
@@ -833,12 +837,14 @@ used, passing :c:func:`PyUnicode_FSDecoder` as the conversion function:
If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
locale encoding.
+ :c:data:`Py_FileSystemDefaultEncoding` is initialized at startup from the
+ locale encoding and cannot be modified later. If you need to decode a string
+ from the current locale encoding, use
+ :c:func:`PyUnicode_DecodeLocaleAndSize`.
+
.. seealso::
- :c:data:`Py_FileSystemDefaultEncoding` is initialized at startup from the
- locale encoding and cannot be modified later. If you need to decode a
- string from the current locale encoding, use
- :c:func:`PyUnicode_DecodeLocaleAndSize`.
+ The :c:func:`Py_DecodeLocale` function.
.. versionchanged:: 3.2
Use ``"strict"`` error handler on Windows.
@@ -868,12 +874,13 @@ used, passing :c:func:`PyUnicode_FSDecoder` as the conversion function:
If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
locale encoding.
+ :c:data:`Py_FileSystemDefaultEncoding` is initialized at startup from the
+ locale encoding and cannot be modified later. If you need to encode a string
+ to the current locale encoding, use :c:func:`PyUnicode_EncodeLocale`.
+
.. seealso::
- :c:data:`Py_FileSystemDefaultEncoding` is initialized at startup from the
- locale encoding and cannot be modified later. If you need to encode a
- string to the current locale encoding, use
- :c:func:`PyUnicode_EncodeLocale`.
+ The :c:func:`Py_EncodeLocale` function.
.. versionadded:: 3.2
diff --git a/Doc/data/refcounts.dat b/Doc/data/refcounts.dat
index 6025617..d1c24e5 100644
--- a/Doc/data/refcounts.dat
+++ b/Doc/data/refcounts.dat
@@ -349,6 +349,11 @@ PyErr_Format:PyObject*:exception:+1:
PyErr_Format:const char*:format::
PyErr_Format::...::
+PyErr_FormatV:PyObject*::null:
+PyErr_FormatV:PyObject*:exception:+1:
+PyErr_FormatV:const char*:format::
+PyErr_FormatV:va_list:vargs::
+
PyErr_WarnEx:int:::
PyErr_WarnEx:PyObject*:category:0:
PyErr_WarnEx:const char*:message::
diff --git a/Doc/distutils/apiref.rst b/Doc/distutils/apiref.rst
index 82bed24..53d7527 100644
--- a/Doc/distutils/apiref.rst
+++ b/Doc/distutils/apiref.rst
@@ -1099,13 +1099,13 @@ other utility module.
during the build of Python), not the OS version of the current system.
For universal binary builds on Mac OS X the architecture value reflects
- the univeral binary status instead of the architecture of the current
+ the universal binary status instead of the architecture of the current
processor. For 32-bit universal binaries the architecture is ``fat``,
for 64-bit universal binaries the architecture is ``fat64``, and
for 4-way universal binaries the architecture is ``universal``. Starting
from Python 2.7 and Python 3.2 the architecture ``fat3`` is used for
a 3-way universal build (ppc, i386, x86_64) and ``intel`` is used for
- a univeral build with the i386 and x86_64 architectures
+ a universal build with the i386 and x86_64 architectures
Examples of returned values on Mac OS X:
diff --git a/Doc/distutils/builtdist.rst b/Doc/distutils/builtdist.rst
index ac96c40..c5827b6 100644
--- a/Doc/distutils/builtdist.rst
+++ b/Doc/distutils/builtdist.rst
@@ -355,7 +355,7 @@ support this option, so the command::
would create a 64bit installation executable on your 32bit version of Windows.
To cross-compile, you must download the Python source code and cross-compile
-Python itself for the platform you are targetting - it is not possible from a
+Python itself for the platform you are targeting - it is not possible from a
binary installation of Python (as the .lib etc file for other platforms are
not included.) In practice, this means the user of a 32 bit operating
system will need to use Visual Studio 2008 to open the
diff --git a/Doc/extending/embedding.rst b/Doc/extending/embedding.rst
index 6cb686a..acd60ae 100644
--- a/Doc/extending/embedding.rst
+++ b/Doc/extending/embedding.rst
@@ -58,12 +58,18 @@ perform some operation on a file. ::
int
main(int argc, char *argv[])
{
- Py_SetProgramName(argv[0]); /* optional but recommended */
- Py_Initialize();
- PyRun_SimpleString("from time import time,ctime\n"
- "print('Today is', ctime(time()))\n");
- Py_Finalize();
- return 0;
+ wchar_t *program = Py_DecodeLocale(argv[0], NULL);
+ if (program == NULL) {
+ fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
+ exit(1);
+ }
+ Py_SetProgramName(program); /* optional but recommended */
+ Py_Initialize();
+ PyRun_SimpleString("from time import time,ctime\n"
+ "print('Today is', ctime(time()))\n");
+ Py_Finalize();
+ PyMem_RawFree(program);
+ return 0;
}
The :c:func:`Py_SetProgramName` function should be called before
@@ -160,7 +166,7 @@ for data conversion between Python and C, and for error reporting. The
interesting part with respect to embedding Python starts with ::
Py_Initialize();
- pName = PyUnicode_FromString(argv[1]);
+ pName = PyUnicode_DecodeFSDefault(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
diff --git a/Doc/extending/extending.rst b/Doc/extending/extending.rst
index c10efa9..17cfdeb 100644
--- a/Doc/extending/extending.rst
+++ b/Doc/extending/extending.rst
@@ -375,11 +375,17 @@ optionally followed by an import of the module::
int
main(int argc, char *argv[])
{
+ wchar_t *program = Py_DecodeLocale(argv[0], NULL);
+ if (program == NULL) {
+ fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
+ exit(1);
+ }
+
/* Add a built-in module, before Py_Initialize */
PyImport_AppendInittab("spam", PyInit_spam);
/* Pass argv[0] to the Python interpreter */
- Py_SetProgramName(argv[0]);
+ Py_SetProgramName(program);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
@@ -391,6 +397,10 @@ optionally followed by an import of the module::
...
+ PyMem_RawFree(program);
+ return 0;
+ }
+
.. note::
Removing entries from ``sys.modules`` or importing compiled modules into
diff --git a/Doc/glossary.rst b/Doc/glossary.rst
index 1de86ef..578fd11 100644
--- a/Doc/glossary.rst
+++ b/Doc/glossary.rst
@@ -402,6 +402,19 @@ Glossary
than compiled ones, though their programs generally also run more
slowly. See also :term:`interactive`.
+ interpreter shutdown
+ When asked to shut down, the Python interpreter enters a special phase
+ where it gradually releases all allocated resources, such as modules
+ and various critical internal structures. It also makes several calls
+ to the :term:`garbage collector <garbage collection>`. This can trigger
+ the execution of code in user-defined destructors or weakref callbacks.
+ Code executed during the shutdown phase can encounter various
+ exceptions as the resources it relies on may not function anymore
+ (common examples are library modules or the warnings machinery).
+
+ The main reason for interpreter shutdown is that the ``__main__`` module
+ or the script being run has finished executing.
+
iterable
An object capable of returning its members one at a time. Examples of
iterables include all sequence types (such as :class:`list`, :class:`str`,
@@ -444,12 +457,13 @@ Glossary
A number of tools in Python accept key functions to control how elements
are ordered or grouped. They include :func:`min`, :func:`max`,
- :func:`sorted`, :meth:`list.sort`, :func:`heapq.nsmallest`,
- :func:`heapq.nlargest`, and :func:`itertools.groupby`.
+ :func:`sorted`, :meth:`list.sort`, :func:`heapq.merge`,
+ :func:`heapq.nsmallest`, :func:`heapq.nlargest`, and
+ :func:`itertools.groupby`.
There are several ways to create a key function. For example. the
:meth:`str.lower` method can serve as a key function for case insensitive
- sorts. Alternatively, an ad-hoc key function can be built from a
+ sorts. Alternatively, a key function can be built from a
:keyword:`lambda` expression such as ``lambda r: (r[0], r[2])``. Also,
the :mod:`operator` module provides three key function constructors:
:func:`~operator.attrgetter`, :func:`~operator.itemgetter`, and
diff --git a/Doc/howto/clinic.rst b/Doc/howto/clinic.rst
index 750ddbe..ca8e1cb 100644
--- a/Doc/howto/clinic.rst
+++ b/Doc/howto/clinic.rst
@@ -886,7 +886,7 @@ Argument Clinic generates code that does it for you (in the parsing function).
Advanced converters
-------------------
-Remeber those format units you skipped for your first
+Remember those format units you skipped for your first
time because they were advanced? Here's how to handle those too.
The trick is, all those format units take arguments--either
@@ -1020,12 +1020,12 @@ any of the default arguments you can omit the parentheses.
the ``"as"`` should come before the return converter.)
There's one additional complication when using return converters: how do you
-indicate an error has occured? Normally, a function returns a valid (non-``NULL``)
+indicate an error has occurred? Normally, a function returns a valid (non-``NULL``)
pointer for success, and ``NULL`` for failure. But if you use an integer return converter,
all integers are valid. How can Argument Clinic detect an error? Its solution: each return
converter implicitly looks for a special value that indicates an error. If you return
that value, and an error has been set (``PyErr_Occurred()`` returns a true
-value), then the generated code will propogate the error. Otherwise it will
+value), then the generated code will propagate the error. Otherwise it will
encode the value you return like normal.
Currently Argument Clinic supports only a few return converters::
@@ -1573,7 +1573,7 @@ The fourth new directive is ``set``::
``line_prefix`` is a string that will be prepended to every line of Clinic's output;
``line_suffix`` is a string that will be appended to every line of Clinic's output.
-Both of these suport two format strings:
+Both of these support two format strings:
``{block comment start}``
Turns into the string ``/*``, the start-comment text sequence for C files.
diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst
index 114ec09..e31b6c2 100644
--- a/Doc/howto/logging-cookbook.rst
+++ b/Doc/howto/logging-cookbook.rst
@@ -325,6 +325,15 @@ which, when run, will produce::
MainThread: Look out!
+.. versionchanged:: 3.5
+ Prior to Python 3.5, the :class:`QueueListener` always passed every message
+ received from the queue to every handler it was initialized with. (This was
+ because it was assumed that level filtering was all done on the other side,
+ where the queue is filled.) From 3.5 onwards, this behaviour can be changed
+ by passing a keyword argument ``respect_handler_level=True`` to the
+ listener's constructor. When this is done, the listener compares the level
+ of each message with the handler's level, and only passes a message to a
+ handler if it's appropriate to do so.
.. _network-logging:
@@ -1680,7 +1689,7 @@ as in the following complete example::
def main():
logging.basicConfig(level=logging.INFO, format='%(message)s')
- logging.info(_('message 1', set_value=set([1, 2, 3]), snowman='\u2603'))
+ logging.info(_('message 1', set_value={1, 2, 3}, snowman='\u2603'))
if __name__ == '__main__':
main()
diff --git a/Doc/howto/regex.rst b/Doc/howto/regex.rst
index fbe763b..9ae04d7 100644
--- a/Doc/howto/regex.rst
+++ b/Doc/howto/regex.rst
@@ -852,7 +852,7 @@ keep track of the group numbers. There are two features which help with this
problem. Both of them use a common syntax for regular expression extensions, so
we'll look at that first.
-Perl 5 is well-known for its powerful additions to standard regular expressions.
+Perl 5 is well known for its powerful additions to standard regular expressions.
For these new features the Perl developers couldn't choose new single-keystroke metacharacters
or new special sequences beginning with ``\`` without making Perl's regular
expressions confusingly different from standard REs. If they chose ``&`` as a
diff --git a/Doc/howto/sockets.rst b/Doc/howto/sockets.rst
index d5aff90..04394d4 100644
--- a/Doc/howto/sockets.rst
+++ b/Doc/howto/sockets.rst
@@ -234,7 +234,7 @@ messages to be sent back to back (without some kind of reply), and you pass
following message. You'll need to put that aside and hold onto it, until it's
needed.
-Prefixing the message with it's length (say, as 5 numeric characters) gets more
+Prefixing the message with its length (say, as 5 numeric characters) gets more
complex, because (believe it or not), you may not get all 5 characters in one
``recv``. In playing around, you'll get away with it; but in high network loads,
your code will very quickly break unless you use two ``recv`` loops - the first
diff --git a/Doc/howto/unicode.rst b/Doc/howto/unicode.rst
index b49ac39..ee31a9c 100644
--- a/Doc/howto/unicode.rst
+++ b/Doc/howto/unicode.rst
@@ -280,8 +280,9 @@ and optionally an *errors* argument.
The *errors* argument specifies the response when the input string can't be
converted according to the encoding's rules. Legal values for this argument are
``'strict'`` (raise a :exc:`UnicodeDecodeError` exception), ``'replace'`` (use
-``U+FFFD``, ``REPLACEMENT CHARACTER``), or ``'ignore'`` (just leave the
-character out of the Unicode result).
+``U+FFFD``, ``REPLACEMENT CHARACTER``), ``'ignore'`` (just leave the
+character out of the Unicode result), or ``'backslashreplace'`` (inserts a
+``\xNN`` escape sequence).
The following examples show the differences::
>>> b'\x80abc'.decode("utf-8", "strict") #doctest: +NORMALIZE_WHITESPACE
@@ -291,6 +292,8 @@ The following examples show the differences::
invalid start byte
>>> b'\x80abc'.decode("utf-8", "replace")
'\ufffdabc'
+ >>> b'\x80abc'.decode("utf-8", "backslashreplace")
+ '\\x80abc'
>>> b'\x80abc'.decode("utf-8", "ignore")
'abc'
@@ -325,8 +328,9 @@ The *errors* parameter is the same as the parameter of the
:meth:`~bytes.decode` method but supports a few more possible handlers. As well as
``'strict'``, ``'ignore'``, and ``'replace'`` (which in this case
inserts a question mark instead of the unencodable character), there is
-also ``'xmlcharrefreplace'`` (inserts an XML character reference) and
-``backslashreplace`` (inserts a ``\uNNNN`` escape sequence).
+also ``'xmlcharrefreplace'`` (inserts an XML character reference),
+``backslashreplace`` (inserts a ``\uNNNN`` escape sequence) and
+``namereplace`` (inserts a ``\N{...}`` escape sequence).
The following example shows the different results::
@@ -346,6 +350,8 @@ The following example shows the different results::
b'&#40960;abcd&#1972;'
>>> u.encode('ascii', 'backslashreplace')
b'\\ua000abcd\\u07b4'
+ >>> u.encode('ascii', 'namereplace')
+ b'\\N{YI SYLLABLE IT}abcd\\u07b4'
The low-level routines for registering and accessing the available
encodings are found in the :mod:`codecs` module. Implementing new
diff --git a/Doc/includes/run-func.c b/Doc/includes/run-func.c
index 1c9860d..986d670 100644
--- a/Doc/includes/run-func.c
+++ b/Doc/includes/run-func.c
@@ -13,7 +13,7 @@ main(int argc, char *argv[])
}
Py_Initialize();
- pName = PyUnicode_FromString(argv[1]);
+ pName = PyUnicode_DecodeFSDefault(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
diff --git a/Doc/install/index.rst b/Doc/install/index.rst
index 8f3ad72..876f350 100644
--- a/Doc/install/index.rst
+++ b/Doc/install/index.rst
@@ -361,7 +361,7 @@ And here are the values used on Windows:
Type of file Installation directory
=============== ===========================================================
modules :file:`{userbase}\\Python{XY}\\site-packages`
-scripts :file:`{userbase}\\Scripts`
+scripts :file:`{userbase}\\Python{XY}\\Scripts`
data :file:`{userbase}`
C headers :file:`{userbase}\\Python{XY}\\Include\\{distname}`
=============== ===========================================================
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index 73f9480..1f75cd9 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -135,7 +135,7 @@ ArgumentParser objects
formatter_class=argparse.HelpFormatter, \
prefix_chars='-', fromfile_prefix_chars=None, \
argument_default=None, conflict_handler='error', \
- add_help=True)
+ add_help=True, allow_abbrev=True)
Create a new :class:`ArgumentParser` object. All parameters should be passed
as keyword arguments. Each parameter has its own more detailed description
@@ -169,6 +169,12 @@ ArgumentParser objects
* add_help_ - Add a -h/--help option to the parser (default: ``True``)
+ * allow_abbrev_ - Allows long options to be abbreviated if the
+ abbreviation is unambiguous. (default: ``True``)
+
+ .. versionchanged:: 3.5
+ *allow_abbrev* parameter was added.
+
The following sections describe how each of these are used.
@@ -518,6 +524,26 @@ calls, we supply ``argument_default=SUPPRESS``::
>>> parser.parse_args([])
Namespace()
+.. _allow_abbrev:
+
+allow_abbrev
+^^^^^^^^^^^^
+
+Normally, when you pass an argument list to the
+:meth:`~ArgumentParser.parse_args` method of a :class:`ArgumentParser`,
+it :ref:`recognizes abbreviations <prefix-matching>` of long options.
+
+This feature can be disabled by setting ``allow_abbrev`` to ``False``::
+
+ >>> parser = argparse.ArgumentParser(prog='PROG', allow_abbrev=False)
+ >>> parser.add_argument('--foobar', action='store_true')
+ >>> parser.add_argument('--foonley', action='store_false')
+ >>> parser.parse_args([--foon])
+ usage: PROG [-h] [--foobar] [--foonley]
+ PROG: error: unrecognized arguments: --foon
+
+.. versionadded:: 3.5
+
conflict_handler
^^^^^^^^^^^^^^^^
@@ -1410,9 +1436,9 @@ argument::
Argument abbreviations (prefix matching)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-The :meth:`~ArgumentParser.parse_args` method allows long options to be
-abbreviated to a prefix, if the abbreviation is unambiguous (the prefix matches
-a unique option)::
+The :meth:`~ArgumentParser.parse_args` method :ref:`by default <allow_abbrev>`
+allows long options to be abbreviated to a prefix, if the abbreviation is
+unambiguous (the prefix matches a unique option)::
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-bacon')
@@ -1426,6 +1452,7 @@ a unique option)::
PROG: error: ambiguous option: -ba could match -badger, -bacon
An error is produced for arguments that could produce more than one options.
+This feature can be disabled by setting :ref:`allow_abbrev` to ``False``.
Beyond ``sys.argv``
@@ -1910,7 +1937,7 @@ Customizing file parsing
Arguments that are read from a file (see the *fromfile_prefix_chars*
keyword argument to the :class:`ArgumentParser` constructor) are read one
- argument per line. :meth:`convert_arg_line_to_args` can be overriden for
+ argument per line. :meth:`convert_arg_line_to_args` can be overridden for
fancier reading.
This method takes a single argument *arg_line* which is a string read from
diff --git a/Doc/library/asynchat.rst b/Doc/library/asynchat.rst
index c6fa061..794da8c 100644
--- a/Doc/library/asynchat.rst
+++ b/Doc/library/asynchat.rst
@@ -147,40 +147,6 @@ connection requests.
by the channel after :meth:`found_terminator` is called.
-asynchat - Auxiliary Classes
-------------------------------------------
-
-.. class:: fifo(list=None)
-
- A :class:`fifo` holding data which has been pushed by the application but
- not yet popped for writing to the channel. A :class:`fifo` is a list used
- to hold data and/or producers until they are required. If the *list*
- argument is provided then it should contain producers or data items to be
- written to the channel.
-
-
- .. method:: is_empty()
-
- Returns ``True`` if and only if the fifo is empty.
-
-
- .. method:: first()
-
- Returns the least-recently :meth:`push`\ ed item from the fifo.
-
-
- .. method:: push(data)
-
- Adds the given data (which may be a string or a producer object) to the
- producer fifo.
-
-
- .. method:: pop()
-
- If the fifo is not empty, returns ``True, first()``, deleting the popped
- item. Returns ``False, None`` for an empty fifo.
-
-
.. _asynchat-example:
asynchat Example
diff --git a/Doc/library/asyncio-eventloops.rst b/Doc/library/asyncio-eventloops.rst
index afb8b9f..ae3bf90 100644
--- a/Doc/library/asyncio-eventloops.rst
+++ b/Doc/library/asyncio-eventloops.rst
@@ -100,8 +100,6 @@ Common limits of Windows event loops:
:class:`ProactorEventLoop` specific limits:
-- SSL is not supported: :meth:`~BaseEventLoop.create_connection` and
- :meth:`~BaseEventLoop.create_server` cannot be used with SSL for example
- :meth:`~BaseEventLoop.create_datagram_endpoint` (UDP) is not supported
- :meth:`~BaseEventLoop.add_reader` and :meth:`~BaseEventLoop.add_writer` are
not supported
@@ -112,6 +110,10 @@ The best resolution is 0.5 msec. The resolution depends on the hardware
<http://fr.wikipedia.org/wiki/High_Precision_Event_Timer>`_) and on the Windows
configuration. See :ref:`asyncio delayed calls <asyncio-delayed-calls>`.
+.. versionchanged:: 3.5
+
+ :class:`ProactorEventLoop` now supports SSL.
+
Mac OS X
^^^^^^^^
diff --git a/Doc/library/cgi.rst b/Doc/library/cgi.rst
index fa13145..0a9e884 100644
--- a/Doc/library/cgi.rst
+++ b/Doc/library/cgi.rst
@@ -157,6 +157,9 @@ return bytes)::
if not line: break
linecount = linecount + 1
+:class:`FieldStorage` objects also support being used in a :keyword:`with`
+statement, which will automatically close them when done.
+
If an error is encountered when obtaining the contents of an uploaded file
(for example, when the user interrupts the form submission by clicking on
a Back or Cancel button) the :attr:`~FieldStorage.done` attribute of the
@@ -182,6 +185,10 @@ A form submitted via POST that also has a query string will contain both
The :attr:`~FieldStorage.file` attribute is automatically closed upon the
garbage collection of the creating :class:`FieldStorage` instance.
+.. versionchanged:: 3.5
+ Added support for the context management protocol to the
+ :class:`FieldStorage` class.
+
Higher Level Interface
----------------------
diff --git a/Doc/library/cmd.rst b/Doc/library/cmd.rst
index 80d2a5d..1ab2d74 100644
--- a/Doc/library/cmd.rst
+++ b/Doc/library/cmd.rst
@@ -259,7 +259,7 @@ immediate playback::
'Move turtle to an absolute position with changing orientation. GOTO 100 200'
goto(*parse(arg))
def do_home(self, arg):
- 'Return turtle to the home postion: HOME'
+ 'Return turtle to the home position: HOME'
home()
def do_circle(self, arg):
'Draw circle with given radius an options extent and steps: CIRCLE 50'
diff --git a/Doc/library/code.rst b/Doc/library/code.rst
index 5b5d7cc..275201c 100644
--- a/Doc/library/code.rst
+++ b/Doc/library/code.rst
@@ -4,6 +4,7 @@
.. module:: code
:synopsis: Facilities to implement read-eval-print loops.
+**Source code:** :source:`Lib/code.py`
The ``code`` module provides facilities to implement read-eval-print loops in
Python. Two classes and convenience functions are included which can be used to
@@ -113,6 +114,9 @@ Interactive Interpreter Objects
because it is within the interpreter object implementation. The output is
written by the :meth:`write` method.
+ .. versionchanged:: 3.5 The full chained traceback is displayed instead
+ of just the primary traceback.
+
.. method:: InteractiveInterpreter.write(data)
@@ -165,4 +169,3 @@ interpreter objects as well as the following additions.
newline. When the user enters the EOF key sequence, :exc:`EOFError` is raised.
The base implementation reads from ``sys.stdin``; a subclass may replace this
with a different implementation.
-
diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst
index dd44cb2..048f0e9 100644
--- a/Doc/library/codecs.rst
+++ b/Doc/library/codecs.rst
@@ -7,6 +7,7 @@
.. sectionauthor:: Marc-André Lemburg <mal@lemburg.com>
.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
+**Source code:** :source:`Lib/codecs.py`
.. index::
single: Unicode
@@ -29,10 +30,9 @@ module features are restricted to use specifically with
The module defines the following functions for encoding and decoding with
any codec:
-.. function:: encode(obj, [encoding[, errors]])
+.. function:: encode(obj, encoding='utf-8', errors='strict')
- Encodes *obj* using the codec registered for *encoding*. The default
- encoding is ``utf-8``.
+ Encodes *obj* using the codec registered for *encoding*.
*Errors* may be given to set the desired error handling scheme. The
default error handler is ``'strict'`` meaning that encoding errors raise
@@ -40,10 +40,9 @@ any codec:
:exc:`UnicodeEncodeError`). Refer to :ref:`codec-base-classes` for more
information on codec error handling.
-.. function:: decode(obj, [encoding[, errors]])
+.. function:: decode(obj, encoding='utf-8', errors='strict')
- Decodes *obj* using the codec registered for *encoding*. The default
- encoding is ``utf-8``.
+ Decodes *obj* using the codec registered for *encoding*.
*Errors* may be given to set the desired error handling scheme. The
default error handler is ``'strict'`` meaning that decoding errors raise
@@ -104,7 +103,6 @@ The full details for each codec can also be looked up directly:
To simplify access to the various codec components, the module provides
these additional functions which use :func:`lookup` for the codec lookup:
-
.. function:: getencoder(encoding)
Look up the codec for the given encoding and return its encoder function.
@@ -274,6 +272,7 @@ implement the file protocols. Codec authors also need to define how the
codec will handle encoding and decoding errors.
+.. _surrogateescape:
.. _error-handlers:
Error Handlers
@@ -315,10 +314,14 @@ The following error handlers are only applicable to
| | reference (only for encoding). Implemented |
| | in :func:`xmlcharrefreplace_errors`. |
+-------------------------+-----------------------------------------------+
-| ``'backslashreplace'`` | Replace with backslashed escape sequences |
-| | (only for encoding). Implemented in |
+| ``'backslashreplace'`` | Replace with backslashed escape sequences. |
+| | Implemented in |
| | :func:`backslashreplace_errors`. |
+-------------------------+-----------------------------------------------+
+| ``'namereplace'`` | Replace with ``\N{...}`` escape sequences |
+| | (only for encoding). Implemented in |
+| | :func:`namereplace_errors`. |
++-------------------------+-----------------------------------------------+
| ``'surrogateescape'`` | On decoding, replace byte with individual |
| | surrogate code ranging from ``U+DC80`` to |
| | ``U+DCFF``. This code will then be turned |
@@ -344,6 +347,13 @@ In addition, the following error handler is specific to the given codecs:
.. versionchanged:: 3.4
The ``'surrogatepass'`` error handlers now works with utf-16\* and utf-32\* codecs.
+.. versionadded:: 3.5
+ The ``'namereplace'`` error handler.
+
+.. versionchanged:: 3.5
+ The ``'backslashreplace'`` error handlers now works with decoding and
+ translating.
+
The set of allowed values can be extended by registering a new named error
handler:
@@ -411,9 +421,17 @@ functions:
.. function:: backslashreplace_errors(exception)
- Implements the ``'backslashreplace'`` error handling (for encoding with
+ Implements the ``'backslashreplace'`` error handling (for
+ :term:`text encodings <text encoding>` only): malformed data is
+ replaced by a backslashed escape sequence.
+
+.. function:: namereplace_errors(exception)
+
+ Implements the ``'namereplace'`` error handling (for encoding with
:term:`text encodings <text encoding>` only): the
- unencodable character is replaced by a backslashed escape sequence.
+ unencodable character is replaced by a ``\N{...}`` escape sequence.
+
+ .. versionadded:: 3.5
.. _codec-objects:
@@ -1444,4 +1462,3 @@ This module implements a variant of the UTF-8 codec: On encoding a UTF-8 encoded
BOM will be prepended to the UTF-8 encoded bytes. For the stateful encoder this
is only done once (on the first write to the byte stream). For decoding an
optional UTF-8 encoded BOM at the start of the data will be skipped.
-
diff --git a/Doc/library/collections.abc.rst b/Doc/library/collections.abc.rst
index 599e9fa..99c4311 100644
--- a/Doc/library/collections.abc.rst
+++ b/Doc/library/collections.abc.rst
@@ -179,7 +179,7 @@ Notes on using :class:`Set` and :class:`MutableSet` as a mixin:
(3)
The :class:`Set` mixin provides a :meth:`_hash` method to compute a hash value
for the set; however, :meth:`__hash__` is not defined because not all sets
- are hashable or immutable. To add set hashabilty using mixins,
+ are hashable or immutable. To add set hashability using mixins,
inherit from both :meth:`Set` and :meth:`Hashable`, then define
``__hash__ = Set._hash``.
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index 0acde12..c21f456 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -982,12 +982,15 @@ 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 <view>` of :class:`OrderedDict` now
+ support reverse iteration using :func:`reversed`.
:class:`OrderedDict` Examples and Recipes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Since an ordered dictionary remembers its insertion order, it can be used
-in conjuction with sorting to make a sorted dictionary::
+in conjunction with sorting to make a sorted dictionary::
>>> # regular unsorted dictionary
>>> d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}
diff --git a/Doc/library/compileall.rst b/Doc/library/compileall.rst
index 9712de2..430362e 100644
--- a/Doc/library/compileall.rst
+++ b/Doc/library/compileall.rst
@@ -42,7 +42,8 @@ compile Python sources.
.. cmdoption:: -q
- Do not print the list of files compiled, print only error messages.
+ Do not print the list of files compiled. If passed once, error messages will
+ still be printed. If passed twice (``-qq``), all output is suppressed.
.. cmdoption:: -d destdir
@@ -70,9 +71,29 @@ compile Python sources.
is to write files to their :pep:`3147` locations and names, which allows
byte-code files from multiple versions of Python to coexist.
+.. cmdoption:: -r
+
+ Control the maximum recursion level for subdirectories.
+ If this is given, then ``-l`` option will not be taken into account.
+ :program:`python -m compileall <directory> -r 0` is equivalent to
+ :program:`python -m compileall <directory> -l`.
+
+.. cmdoption:: -j N
+
+ Use *N* workers to compile the files within the given directory.
+ If ``0`` is used, then the result of :func:`os.cpu_count()`
+ will be used.
+
.. versionchanged:: 3.2
Added the ``-i``, ``-b`` and ``-h`` options.
+.. versionchanged:: 3.5
+ Added the ``-j`` and ``-r`` options.
+
+.. versionchanged:: 3.5
+ ``-q`` option was changed to a multilevel value.
+
+
There is no command-line option to control the optimization level used by the
:func:`compile` function, because the Python interpreter itself already
provides the option: :program:`python -O -m compileall`.
@@ -80,7 +101,7 @@ provides the option: :program:`python -O -m compileall`.
Public functions
----------------
-.. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1)
+.. 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.
@@ -101,8 +122,9 @@ Public functions
file considered for compilation, and if it returns a true value, the file
is skipped.
- If *quiet* is true, nothing is printed to the standard output unless errors
- occur.
+ If *quiet* is ``False`` or ``0`` (the default), the filenames and other
+ information are printed to standard out. Set to ``1``, only errors are
+ printed. Set to ``2``, all output is suppressed.
If *legacy* is true, byte-code files are written to their legacy locations
and names, which may overwrite byte-code files created by another version of
@@ -113,11 +135,22 @@ Public functions
*optimize* specifies the optimization level for the compiler. It is passed to
the built-in :func:`compile` function.
+ The argument *workers* specifies how many workers are used to
+ compile files in parallel. The default is to not use multiple workers.
+ If the platform can't use multiple workers and *workers* argument is given,
+ then a :exc:`NotImplementedError` will be raised.
+ If *workers* is lower than ``0``, a :exc:`ValueError` will be raised.
+
.. versionchanged:: 3.2
Added the *legacy* and *optimize* parameter.
+ .. versionchanged:: 3.5
+ Added the *workers* parameter.
+
+ .. versionchanged:: 3.5
+ *quiet* parameter was changed to a multilevel value.
-.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1)
+.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1)
Compile the file with path *fullname*.
@@ -131,8 +164,9 @@ Public functions
file being compiled, and if it returns a true value, the file is not
compiled and ``True`` is returned.
- If *quiet* is true, nothing is printed to the standard output unless errors
- occur.
+ If *quiet* is ``False`` or ``0`` (the default), the filenames and other
+ information are printed to standard out. Set to ``1``, only errors are
+ printed. Set to ``2``, all output is suppressed.
If *legacy* is true, byte-code files are written to their legacy locations
and names, which may overwrite byte-code files created by another version of
@@ -145,8 +179,10 @@ Public functions
.. versionadded:: 3.2
+ .. versionchanged:: 3.5
+ *quiet* parameter was changed to a multilevel value.
-.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, legacy=False, optimize=-1)
+.. 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
@@ -157,6 +193,8 @@ Public functions
.. versionchanged:: 3.2
Added the *legacy* and *optimize* parameter.
+ .. versionchanged:: 3.5
+ *quiet* parameter was changed to a multilevel value.
To force a recompile of all the :file:`.py` files in the :file:`Lib/`
subdirectory and all its subdirectories::
diff --git a/Doc/library/concurrent.futures.rst b/Doc/library/concurrent.futures.rst
index bd56696..c177340 100644
--- a/Doc/library/concurrent.futures.rst
+++ b/Doc/library/concurrent.futures.rst
@@ -38,7 +38,7 @@ Executor Objects
future = executor.submit(pow, 323, 1235)
print(future.result())
- .. method:: map(func, *iterables, timeout=None)
+ .. method:: map(func, *iterables, timeout=None, chunksize=1)
Equivalent to :func:`map(func, *iterables) <map>` except *func* is executed
asynchronously and several calls to *func* may be made concurrently. The
@@ -48,7 +48,16 @@ Executor Objects
*timeout* can be an int or a float. If *timeout* is not specified or
``None``, there is no limit to the wait time. If a call raises an
exception, then that exception will be raised when its value is
- retrieved from the iterator.
+ retrieved from the iterator. When using :class:`ProcessPoolExecutor`, this
+ method chops *iterables* into a number of chunks which it submits to the
+ pool as separate tasks. The (approximate) size of these chunks can be
+ specified by setting *chunksize* to a positive integer. For very long
+ iterables, using a large value for *chunksize* can significantly improve
+ performance compared to the default size of 1. With :class:`ThreadPoolExecutor`,
+ *chunksize* has no effect.
+
+ .. versionchanged:: 3.5
+ Added the *chunksize* argument.
.. method:: shutdown(wait=True)
@@ -115,11 +124,19 @@ And::
executor.submit(wait_on_future)
-.. class:: ThreadPoolExecutor(max_workers)
+.. class:: ThreadPoolExecutor(max_workers=None)
An :class:`Executor` subclass that uses a pool of at most *max_workers*
threads to execute calls asynchronously.
+ .. versionchanged:: 3.5
+ If *max_workers* is ``None`` or
+ not given, it will default to the number of processors on the machine,
+ multiplied by ``5``, assuming that :class:`ThreadPoolExecutor` is often
+ used to overlap I/O instead of CPU work and the number of workers
+ should be higher than the number of workers
+ for :class:`ProcessPoolExecutor`.
+
.. _threadpoolexecutor-example:
@@ -175,6 +192,8 @@ to a :class:`ProcessPoolExecutor` will result in deadlock.
An :class:`Executor` subclass that executes calls asynchronously using a pool
of at most *max_workers* processes. If *max_workers* is ``None`` or not
given, it will default to the number of processors on the machine.
+ If *max_workers* is lower or equal to ``0``, then a :exc:`ValueError`
+ will be raised.
.. versionchanged:: 3.3
When one of the worker processes terminates abruptly, a
diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst
index 024d27c..c9187a3 100644
--- a/Doc/library/configparser.rst
+++ b/Doc/library/configparser.rst
@@ -11,6 +11,8 @@
.. sectionauthor:: Christopher G. Petrilli <petrilli@amber.org>
.. sectionauthor:: Łukasz Langa <lukasz@langa.pl>
+**Source code:** :source:`Lib/configparser.py`
+
.. index::
pair: .ini; file
pair: configuration; file
@@ -142,12 +144,13 @@ datatypes, you should convert on your own:
>>> float(topsecret['CompressionLevel'])
9.0
-Extracting Boolean values is not that simple, though. Passing the value
-to ``bool()`` would do no good since ``bool('False')`` is still
-``True``. This is why config parsers also provide :meth:`getboolean`.
-This method is case-insensitive and recognizes Boolean values from
-``'yes'``/``'no'``, ``'on'``/``'off'`` and ``'1'``/``'0'`` [1]_.
-For example:
+Since this task is so common, config parsers provide a range of handy getter
+methods to handle integers, floats and booleans. The last one is the most
+interesting because simply passing the value to ``bool()`` would do no good
+since ``bool('False')`` is still ``True``. This is why config parsers also
+provide :meth:`getboolean`. This method is case-insensitive and recognizes
+Boolean values from ``'yes'``/``'no'``, ``'on'``/``'off'``,
+``'true'``/``'false'`` and ``'1'``/``'0'`` [1]_. For example:
.. doctest::
@@ -159,10 +162,8 @@ For example:
True
Apart from :meth:`getboolean`, config parsers also provide equivalent
-:meth:`getint` and :meth:`getfloat` methods, but these are far less
-useful since conversion using :func:`int` and :func:`float` is
-sufficient for these types.
-
+:meth:`getint` and :meth:`getfloat` methods. You can register your own
+converters and customize the provided ones. [1]_
Fallback Values
---------------
@@ -317,11 +318,11 @@ from ``get()`` calls.
.. class:: ExtendedInterpolation()
An alternative handler for interpolation which implements a more advanced
- syntax, used for instance in ``zc.buildout``. Extended interpolation is
+ syntax, used for instance in ``zc.buildout``. Extended interpolation is
using ``${section:option}`` to denote a value from a foreign section.
- Interpolation can span multiple levels. For convenience, if the ``section:``
- part is omitted, interpolation defaults to the current section (and possibly
- the default values from the special section).
+ Interpolation can span multiple levels. For convenience, if the
+ ``section:`` part is omitted, interpolation defaults to the current section
+ (and possibly the default values from the special section).
For example, the configuration specified above with basic interpolation,
would look like this with extended interpolation:
@@ -386,7 +387,7 @@ However, there are a few differences that should be taken into account:
* All sections include ``DEFAULTSECT`` values as well which means that
``.clear()`` on a section may not leave the section visibly empty. This is
because default values cannot be deleted from the section (because technically
- they are not there). If they are overriden in the section, deleting causes
+ they are not there). If they are overridden in the section, deleting causes
the default value to be visible again. Trying to delete a default value
causes a ``KeyError``.
@@ -399,13 +400,13 @@ However, there are a few differences that should be taken into account:
* ``parser.popitem()`` never returns it.
* ``parser.get(section, option, **kwargs)`` - the second argument is **not**
- a fallback value. Note however that the section-level ``get()`` methods are
+ a fallback value. Note however that the section-level ``get()`` methods are
compatible both with the mapping protocol and the classic configparser API.
* ``parser.items()`` is compatible with the mapping protocol (returns a list of
*section_name*, *section_proxy* pairs including the DEFAULTSECT). However,
this method can also be invoked with arguments: ``parser.items(section, raw,
- vars)``. The latter call returns a list of *option*, *value* pairs for
+ vars)``. The latter call returns a list of *option*, *value* pairs for
a specified ``section``, with all interpolations expanded (unless
``raw=True`` is provided).
@@ -539,9 +540,9 @@ the :meth:`__init__` options:
* *delimiters*, default value: ``('=', ':')``
- Delimiters are substrings that delimit keys from values within a section. The
- first occurrence of a delimiting substring on a line is considered a delimiter.
- This means values (but not keys) can contain the delimiters.
+ Delimiters are substrings that delimit keys from values within a section.
+ The first occurrence of a delimiting substring on a line is considered
+ a delimiter. This means values (but not keys) can contain the delimiters.
See also the *space_around_delimiters* argument to
:meth:`ConfigParser.write`.
@@ -553,7 +554,7 @@ the :meth:`__init__` options:
Comment prefixes are strings that indicate the start of a valid comment within
a config file. *comment_prefixes* are used only on otherwise empty lines
(optionally indented) whereas *inline_comment_prefixes* can be used after
- every valid value (e.g. section names, options and empty lines as well). By
+ every valid value (e.g. section names, options and empty lines as well). By
default inline comments are disabled and ``'#'`` and ``';'`` are used as
prefixes for whole line comments.
@@ -563,10 +564,10 @@ the :meth:`__init__` options:
Please note that config parsers don't support escaping of comment prefixes so
using *inline_comment_prefixes* may prevent users from specifying option
- values with characters used as comment prefixes. When in doubt, avoid setting
- *inline_comment_prefixes*. In any circumstances, the only way of storing
- comment prefix characters at the beginning of a line in multiline values is to
- interpolate the prefix, for example::
+ values with characters used as comment prefixes. When in doubt, avoid
+ setting *inline_comment_prefixes*. In any circumstances, the only way of
+ storing comment prefix characters at the beginning of a line in multiline
+ values is to interpolate the prefix, for example::
>>> from configparser import ConfigParser, ExtendedInterpolation
>>> parser = ConfigParser(interpolation=ExtendedInterpolation())
@@ -611,7 +612,7 @@ the :meth:`__init__` options:
When set to ``True``, the parser will not allow for any section or option
duplicates while reading from a single source (using :meth:`read_file`,
- :meth:`read_string` or :meth:`read_dict`). It is recommended to use strict
+ :meth:`read_string` or :meth:`read_dict`). It is recommended to use strict
parsers in new applications.
.. versionchanged:: 3.2
@@ -646,12 +647,12 @@ the :meth:`__init__` options:
The convention of allowing a special section of default values for other
sections or interpolation purposes is a powerful concept of this library,
- letting users create complex declarative configurations. This section is
+ letting users create complex declarative configurations. This section is
normally called ``"DEFAULT"`` but this can be customized to point to any
- other valid section name. Some typical values include: ``"general"`` or
- ``"common"``. The name provided is used for recognizing default sections when
- reading from any source and is used when writing configuration back to
- a file. Its current value can be retrieved using the
+ other valid section name. Some typical values include: ``"general"`` or
+ ``"common"``. The name provided is used for recognizing default sections
+ when reading from any source and is used when writing configuration back to
+ a file. Its current value can be retrieved using the
``parser_instance.default_section`` attribute and may be modified at runtime
(i.e. to convert files from one format to another).
@@ -660,14 +661,30 @@ the :meth:`__init__` options:
Interpolation behaviour may be customized by providing a custom handler
through the *interpolation* argument. ``None`` can be used to turn off
interpolation completely, ``ExtendedInterpolation()`` provides a more
- advanced variant inspired by ``zc.buildout``. More on the subject in the
+ advanced variant inspired by ``zc.buildout``. More on the subject in the
`dedicated documentation section <#interpolation-of-values>`_.
:class:`RawConfigParser` has a default value of ``None``.
+* *converters*, default value: not set
+
+ Config parsers provide option value getters that perform type conversion. By
+ default :meth:`getint`, :meth:`getfloat`, and :meth:`getboolean` are
+ implemented. Should other getters be desirable, users may define them in
+ a subclass or pass a dictionary where each key is a name of the converter and
+ each value is a callable implementing said conversion. For instance, passing
+ ``{'decimal': decimal.Decimal}`` would add :meth:`getdecimal` on both the
+ parser object and all section proxies. In other words, it will be possible
+ to write both ``parser_instance.getdecimal('section', 'key', fallback=0)``
+ and ``parser_instance['section'].getdecimal('key', 0)``.
+
+ If the converter needs to access the state of the parser, it can be
+ implemented as a method on a config parser subclass. If the name of this
+ method starts with ``get``, it will be available on all section proxies, in
+ the dict-compatible form (see the ``getdecimal()`` example above).
More advanced customization may be achieved by overriding default values of
-these parser attributes. The defaults are defined on the classes, so they
-may be overriden by subclasses or by attribute assignment.
+these parser attributes. The defaults are defined on the classes, so they may
+be overridden by subclasses or by attribute assignment.
.. attribute:: BOOLEAN_STATES
@@ -725,10 +742,11 @@ may be overriden by subclasses or by attribute assignment.
.. attribute:: SECTCRE
- A compiled regular expression used to parse section headers. The default
- matches ``[section]`` to the name ``"section"``. Whitespace is considered part
- of the section name, thus ``[ larch ]`` will be read as a section of name
- ``" larch "``. Override this attribute if that's unsuitable. For example:
+ A compiled regular expression used to parse section headers. The default
+ matches ``[section]`` to the name ``"section"``. Whitespace is considered
+ part of the section name, thus ``[ larch ]`` will be read as a section of
+ name ``" larch "``. Override this attribute if that's unsuitable. For
+ example:
.. doctest::
@@ -859,7 +877,7 @@ interpolation if an option used is not defined elsewhere. ::
ConfigParser Objects
--------------------
-.. class:: ConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT, interpolation=BasicInterpolation())
+.. class:: ConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT, interpolation=BasicInterpolation(), converters={})
The main configuration parser. When *defaults* is given, it is initialized
into the dictionary of intrinsic defaults. When *dict_type* is given, it
@@ -869,8 +887,8 @@ ConfigParser Objects
When *delimiters* is given, it is used as the set of substrings that
divide keys from values. When *comment_prefixes* is given, it will be used
as the set of substrings that prefix comments in otherwise empty lines.
- Comments can be indented. When *inline_comment_prefixes* is given, it will be
- used as the set of substrings that prefix comments in non-empty lines.
+ Comments can be indented. When *inline_comment_prefixes* is given, it will
+ be used as the set of substrings that prefix comments in non-empty lines.
When *strict* is ``True`` (the default), the parser won't allow for
any section or option duplicates while reading from a single source (file,
@@ -884,13 +902,13 @@ ConfigParser Objects
When *default_section* is given, it specifies the name for the special
section holding default values for other sections and interpolation purposes
- (normally named ``"DEFAULT"``). This value can be retrieved and changed on
+ (normally named ``"DEFAULT"``). This value can be retrieved and changed on
runtime using the ``default_section`` instance attribute.
Interpolation behaviour may be customized by providing a custom handler
through the *interpolation* argument. ``None`` can be used to turn off
interpolation completely, ``ExtendedInterpolation()`` provides a more
- advanced variant inspired by ``zc.buildout``. More on the subject in the
+ advanced variant inspired by ``zc.buildout``. More on the subject in the
`dedicated documentation section <#interpolation-of-values>`_.
All option names used in interpolation will be passed through the
@@ -899,6 +917,12 @@ ConfigParser Objects
converts option names to lower case), the values ``foo %(bar)s`` and ``foo
%(BAR)s`` are equivalent.
+ When *converters* is given, it should be a dictionary where each key
+ represents the name of a type converter and each value is a callable
+ implementing the conversion from string to the desired datatype. Every
+ converter gets its own corresponding :meth:`get*()` method on the parser
+ object and section proxies.
+
.. versionchanged:: 3.1
The default *dict_type* is :class:`collections.OrderedDict`.
@@ -907,6 +931,9 @@ ConfigParser Objects
*empty_lines_in_values*, *default_section* and *interpolation* were
added.
+ .. versionchanged:: 3.5
+ The *converters* argument was added.
+
.. method:: defaults()
@@ -944,7 +971,7 @@ ConfigParser Objects
.. method:: has_option(section, option)
If the given *section* exists, and contains the given *option*, return
- :const:`True`; otherwise return :const:`False`. If the specified
+ :const:`True`; otherwise return :const:`False`. If the specified
*section* is :const:`None` or an empty string, DEFAULT is assumed.
@@ -1069,7 +1096,7 @@ ConfigParser Objects
:meth:`get` method.
.. versionchanged:: 3.2
- Items present in *vars* no longer appear in the result. The previous
+ Items present in *vars* no longer appear in the result. The previous
behaviour mixed actual parser options with variables provided for
interpolation.
@@ -1170,7 +1197,7 @@ RawConfigParser Objects
.. note::
Consider using :class:`ConfigParser` instead which checks types of
- the values to be stored internally. If you don't want interpolation, you
+ the values to be stored internally. If you don't want interpolation, you
can use ``ConfigParser(interpolation=None)``.
@@ -1181,7 +1208,7 @@ RawConfigParser Objects
*default section* name is passed, :exc:`ValueError` is raised.
Type of *section* is not checked which lets users create non-string named
- sections. This behaviour is unsupported and may cause internal errors.
+ sections. This behaviour is unsupported and may cause internal errors.
.. method:: set(section, option, value)
@@ -1282,3 +1309,4 @@ Exceptions
.. [1] Config parsers allow for heavy customization. If you are interested in
changing the behaviour outlined by the footnote reference, consult the
`Customizing Parser Behaviour`_ section.
+
diff --git a/Doc/library/constants.rst b/Doc/library/constants.rst
index 42b5af2..d5a0f09 100644
--- a/Doc/library/constants.rst
+++ b/Doc/library/constants.rst
@@ -45,7 +45,6 @@ A small number of constants live in the built-in namespace. They are:
for more details.
-
.. data:: Ellipsis
The same as ``...``. Special value used mostly in conjunction with extended
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst
index 6f36864..550b347 100644
--- a/Doc/library/contextlib.rst
+++ b/Doc/library/contextlib.rst
@@ -172,6 +172,16 @@ Functions and classes provided:
.. versionadded:: 3.4
+.. function:: redirect_stderr(new_target)
+
+ Similar to :func:`~contextlib.redirect_stdout` but redirecting
+ :data:`sys.stderr` to another file or file-like object.
+
+ This context manager is :ref:`reentrant <reentrant-cms>`.
+
+ .. versionadded:: 3.5
+
+
.. class:: ContextDecorator()
A base class that enables a context manager to also be used as a decorator.
diff --git a/Doc/library/copyreg.rst b/Doc/library/copyreg.rst
index 50d5879..18306c7 100644
--- a/Doc/library/copyreg.rst
+++ b/Doc/library/copyreg.rst
@@ -9,7 +9,7 @@
module: pickle
module: copy
-The :mod:`copyreg` module offers a way to define fuctions used while pickling
+The :mod:`copyreg` module offers a way to define functions used while pickling
specific objects. The :mod:`pickle` and :mod:`copy` modules use those functions
when pickling/copying those objects. The module provides configuration
information about object constructors which are not classes.
diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst
index 9f7b58a..e7516b6 100644
--- a/Doc/library/csv.rst
+++ b/Doc/library/csv.rst
@@ -5,6 +5,7 @@
:synopsis: Write and read tabular data to and from delimited files.
.. sectionauthor:: Skip Montanaro <skip@pobox.com>
+**Source code:** :source:`Lib/csv.py`
.. index::
single: csv
diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst
index 7dd8613..7a9f93c 100644
--- a/Doc/library/datetime.rst
+++ b/Doc/library/datetime.rst
@@ -7,6 +7,8 @@
.. sectionauthor:: Tim Peters <tim@zope.com>
.. sectionauthor:: A.M. Kuchling <amk@amk.ca>
+**Source code:** :source:`Lib/datetime.py`
+
.. XXX what order should the types be discussed in?
The :mod:`datetime` module supplies classes for manipulating dates and times in
@@ -1376,10 +1378,13 @@ Supported operations:
* efficient pickling
-* in Boolean contexts, a :class:`.time` object is considered to be true if and
- only if, after converting it to minutes and subtracting :meth:`utcoffset` (or
- ``0`` if that's ``None``), the result is non-zero.
+In boolean contexts, a :class:`.time` object is always considered to be true.
+.. versionchanged:: 3.5
+ Before Python 3.5, a :class:`.time` object was considered to be false if it
+ represented midnight in UTC. This behavior was considered obscure and
+ error-prone and has been removed in Python 3.5. See :issue:`13936` for full
+ details.
Instance methods:
diff --git a/Doc/library/dbm.rst b/Doc/library/dbm.rst
index e6a82d6..3f3c43d 100644
--- a/Doc/library/dbm.rst
+++ b/Doc/library/dbm.rst
@@ -325,13 +325,18 @@ The module defines the following:
dumbdbm database is created, files with :file:`.dat` and :file:`.dir` extensions
are created.
- The optional *flag* argument is currently ignored; the database is always opened
- for update, and will be created if it does not exist.
+ The optional *flag* argument supports only the semantics of ``'c'``
+ and ``'n'`` values. Other values will default to database being always
+ opened for update, and will be created if it does not exist.
The optional *mode* argument is the Unix mode of the file, used only when the
database has to be created. It defaults to octal ``0o666`` (and will be modified
by the prevailing umask).
+ .. versionchanged:: 3.5
+ :func:`.open` always creates a new database when the flag has the value
+ ``'n'``.
+
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 059ae7c..7052985 100644
--- a/Doc/library/decimal.rst
+++ b/Doc/library/decimal.rst
@@ -12,6 +12,8 @@
.. moduleauthor:: Stefan Krah <skrah at bytereef.org>
.. sectionauthor:: Raymond D. Hettinger <python at rcn.com>
+**Source code:** :source:`Lib/decimal.py`
+
.. import modules for testing inline doctests with the Sphinx doctest builder
.. testsetup:: *
@@ -742,7 +744,7 @@ Decimal objects
* ``"NaN"``, indicating that the operand is a quiet NaN (Not a Number).
* ``"sNaN"``, indicating that the operand is a signaling NaN.
- .. method:: quantize(exp, rounding=None, context=None, watchexp=True)
+ .. method:: quantize(exp, rounding=None, context=None)
Return a value equal to the first operand after rounding and having the
exponent of the second operand.
@@ -765,14 +767,8 @@ Decimal objects
``context`` argument; if neither argument is given the rounding mode of
the current thread's context is used.
- If *watchexp* is set (default), then an error is returned whenever the
- resulting exponent is greater than :attr:`Emax` or less than
- :attr:`Etiny`.
-
- .. deprecated:: 3.3
- *watchexp* is an implementation detail from the pure Python version
- and is not present in the C version. It will be removed in version
- 3.4, where it defaults to ``True``.
+ An error is returned whenever the resulting exponent is greater than
+ :attr:`Emax` or less than :attr:`Etiny`.
.. method:: radix()
@@ -2092,4 +2088,3 @@ Alternatively, inputs can be rounded upon creation using the
>>> Context(prec=5, rounding=ROUND_DOWN).create_decimal('1.2345678')
Decimal('1.2345')
-
diff --git a/Doc/library/difflib.rst b/Doc/library/difflib.rst
index 5f72ea6..329bde0 100644
--- a/Doc/library/difflib.rst
+++ b/Doc/library/difflib.rst
@@ -7,6 +7,8 @@
.. sectionauthor:: Tim Peters <tim_one@users.sourceforge.net>
.. Markup by Fred L. Drake, Jr. <fdrake@acm.org>
+**Source code:** :source:`Lib/difflib.py`
+
.. testsetup::
import sys
@@ -25,7 +27,9 @@ diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
little fancier than, an algorithm published in the late 1980's by Ratcliff and
Obershelp under the hyperbolic name "gestalt pattern matching." The idea is to
find the longest contiguous matching subsequence that contains no "junk"
- elements (the Ratcliff and Obershelp algorithm doesn't address junk). The same
+ elements; these "junk" elements are ones that are uninteresting in some
+ sense, such as blank lines or whitespace. (Handling junk is an
+ extension to the Ratcliff and Obershelp algorithm.) The same
idea is then applied recursively to the pieces of the sequences to the left and
to the right of the matching subsequence. This does not yield minimal edit
sequences, but does tend to yield matches that "look right" to people.
@@ -208,7 +212,7 @@ diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
Compare *a* and *b* (lists of strings); return a :class:`Differ`\ -style
delta (a :term:`generator` generating the delta lines).
- Optional keyword parameters *linejunk* and *charjunk* are for filter functions
+ Optional keyword parameters *linejunk* and *charjunk* are filtering functions
(or ``None``):
*linejunk*: A function that accepts a single string argument, and returns
@@ -222,7 +226,7 @@ diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
*charjunk*: A function that accepts a character (a string of length 1), and
returns if the character is junk, or false if not. The default is module-level
function :func:`IS_CHARACTER_JUNK`, which filters out whitespace characters (a
- blank or tab; note: bad idea to include newline in this!).
+ blank or tab; it's a bad idea to include newline in this!).
:file:`Tools/scripts/ndiff.py` is a command-line front-end to this function.
@@ -622,6 +626,12 @@ The :class:`Differ` class has this constructor:
length 1), and returns true if the character is junk. The default is ``None``,
meaning that no character is considered junk.
+ These junk-filtering functions speed up matching to find
+ differences and do not cause any differing lines or characters to
+ be ignored. Read the description of the
+ :meth:`~SequenceMatcher.find_longest_match` method's *isjunk*
+ parameter for an explanation.
+
:class:`Differ` objects are used (deltas generated) via a single method:
diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst
index 1b7cfe1..3b419e6 100644
--- a/Doc/library/dis.rst
+++ b/Doc/library/dis.rst
@@ -48,8 +48,8 @@ compiled code.
.. class:: Bytecode(x, *, first_line=None, current_offset=None)
- Analyse the bytecode corresponding to a function, method, string of
- source code, or a code object (as returned by :func:`compile`).
+ Analyse the bytecode corresponding to a function, generator, method,
+ string of source code, or a code object (as returned by :func:`compile`).
This is a convenience wrapper around many of the functions listed below,
most notably :func:`get_instructions`, as iterating over a
@@ -112,7 +112,7 @@ object isn't useful:
.. function:: code_info(x)
Return a formatted multi-line string with detailed code object information
- for the supplied function, method, source code string or code object.
+ for the supplied function, generator, method, source code string or code object.
Note that the exact contents of code info strings are highly implementation
dependent and they may change arbitrarily across Python VMs or Python
@@ -139,11 +139,11 @@ object isn't useful:
.. function:: dis(x=None, *, file=None)
Disassemble the *x* object. *x* can denote either a module, a class, a
- method, a function, a code object, a string of source code or a byte sequence
- of raw bytecode. For a module, it disassembles all functions. For a class,
- it disassembles all methods. For a code object or sequence of raw bytecode,
- it prints one line per bytecode instruction. Strings are first compiled to
- code objects with the :func:`compile` built-in function before being
+ method, a function, a generator, a code object, a string of source code or
+ a byte sequence of raw bytecode. For a module, it disassembles all functions.
+ For a class, it disassembles all methods. For a code object or sequence of
+ raw bytecode, it prints one line per bytecode instruction. Strings are first
+ compiled to code objects with the :func:`compile` built-in function before being
disassembled. If no object is provided, this function disassembles the last
traceback.
@@ -364,6 +364,11 @@ result back on the stack.
Implements ``TOS = TOS1 * TOS``.
+.. opcode:: BINARY_MATRIX_MULTIPLY
+
+ Implements ``TOS = TOS1 @ TOS``.
+
+
.. opcode:: BINARY_FLOOR_DIVIDE
Implements ``TOS = TOS1 // TOS``.
@@ -436,6 +441,11 @@ the original TOS1.
Implements in-place ``TOS = TOS1 * TOS``.
+.. opcode:: INPLACE_MATRIX_MULTIPLY
+
+ Implements in-place ``TOS = TOS1 @ TOS``.
+
+
.. opcode:: INPLACE_FLOOR_DIVIDE
Implements in-place ``TOS = TOS1 // TOS``.
diff --git a/Doc/library/doctest.rst b/Doc/library/doctest.rst
index 2e372de..9aa9ea6 100644
--- a/Doc/library/doctest.rst
+++ b/Doc/library/doctest.rst
@@ -1058,15 +1058,9 @@ from text files and modules with doctests:
This function uses the same search technique as :func:`testmod`.
- .. note::
- Unlike :func:`testmod` and :class:`DocTestFinder`, this function raises
- a :exc:`ValueError` if *module* contains no docstrings. You can prevent
- this error by passing a :class:`DocTestFinder` instance as the
- *test_finder* argument with its *exclude_empty* keyword argument set
- to ``False``::
-
- >>> finder = doctest.DocTestFinder(exclude_empty=False)
- >>> suite = doctest.DocTestSuite(test_finder=finder)
+ .. versionchanged:: 3.5
+ :func:`DocTestSuite` returns an empty :class:`unittest.TestSuite` if *module*
+ contains no docstrings instead of raising :exc:`ValueError`.
Under the covers, :func:`DocTestSuite` creates a :class:`unittest.TestSuite` out
diff --git a/Doc/library/email.mime.rst b/Doc/library/email.mime.rst
index 1d70225..67d0a67 100644
--- a/Doc/library/email.mime.rst
+++ b/Doc/library/email.mime.rst
@@ -195,7 +195,8 @@ Here are the classes:
set of the text and is passed as an argument to the
:class:`~email.mime.nonmultipart.MIMENonMultipart` constructor; it defaults
to ``us-ascii`` if the string contains only ``ascii`` code points, and
- ``utf-8`` otherwise.
+ ``utf-8`` otherwise. The *_charset* parameter accepts either a string or a
+ :class:`~email.charset.Charset` instance.
Unless the *_charset* argument is explicitly set to ``None``, the
MIMEText object created will have both a :mailheader:`Content-Type` header
@@ -206,3 +207,6 @@ Here are the classes:
``Content-Transfer-Encoding`` header, after which a ``set_payload`` call
will automatically encode the new payload (and add a new
:mailheader:`Content-Transfer-Encoding` header).
+
+ .. versionchanged:: 3.5
+ *_charset* also accepts :class:`~email.charset.Charset` instances.
diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst
index cf09559..d3b838c 100644
--- a/Doc/library/enum.rst
+++ b/Doc/library/enum.rst
@@ -314,11 +314,11 @@ Then::
>>> str(Mood.funky)
'my custom str! 1'
-The rules for what is allowed are as follows: _sunder_ names (starting and
-ending with a single underscore) are reserved by enum and cannot be used;
-all other attributes defined within an enumeration will become members of this
-enumeration, with the exception of *__dunder__* names and descriptors (methods
-are also descriptors).
+The rules for what is allowed are as follows: names that start and end with a
+with a single underscore are reserved by enum and cannot be used; all other
+attributes defined within an enumeration will become members of this
+enumeration, with the exception of special methods (:meth:`__str__`,
+:meth:`__add__`, etc.) and descriptors (methods are also descriptors).
Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__` then
whatever value(s) were given to the enum member will be passed into those
@@ -400,7 +400,8 @@ The second argument is the *source* of enumeration member names. It can be a
whitespace-separated string of names, a sequence of names, a sequence of
2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to
values. The last two options enable assigning arbitrary values to
-enumerations; the others auto-assign increasing integers starting with 1. A
+enumerations; the others auto-assign increasing integers starting with 1 (use
+the ``start`` parameter to specify a different starting value). A
new class derived from :class:`Enum` is returned. In other words, the above
assignment to :class:`Animal` is equivalent to::
@@ -438,12 +439,12 @@ SomeData in the global scope::
The complete signature is::
- Enum(value='NewEnumName', names=<...>, *, module='...', qualname='...', type=<mixed-in class>)
+ Enum(value='NewEnumName', names=<...>, *, module='...', qualname='...', type=<mixed-in class>, start=1)
:value: What the new Enum class will record as its name.
:names: The Enum members. This can be a whitespace or comma separated string
- (values will start at 1)::
+ (values will start at 1 unless otherwise specified)::
'red green blue' | 'red,green,blue' | 'red, green, blue'
@@ -465,6 +466,11 @@ The complete signature is::
:type: type to mix in to new Enum class.
+:start: number to start counting at if only names are passed in
+
+.. versionchanged:: 3.5
+ The *start* parameter was added.
+
Derived Enumerations
--------------------
diff --git a/Doc/library/formatter.rst b/Doc/library/formatter.rst
index 1847a80..a515f74 100644
--- a/Doc/library/formatter.rst
+++ b/Doc/library/formatter.rst
@@ -5,7 +5,7 @@
:synopsis: Generic output formatter and device interface.
:deprecated:
-.. deprecated:: 3.4
+.. deprecated-removed:: 3.4 3.6
Due to lack of usage, the formatter module has been deprecated and is slated
for removal in Python 3.6.
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index d9e5cfb..eb28513 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -156,11 +156,12 @@ are always available. They are listed here in alphabetical order.
.. function:: chr(i)
- Return the string representing a character whose Unicode code point is the integer
- *i*. For example, ``chr(97)`` returns the string ``'a'``. This is the
- inverse of :func:`ord`. The valid range for the argument is from 0 through
- 1,114,111 (0x10FFFF in base 16). :exc:`ValueError` will be raised if *i* is
- outside that range.
+ Return the string representing a character whose Unicode code point is the
+ integer *i*. For example, ``chr(97)`` returns the string ``'a'``, while
+ ``chr(931)`` returns the string ``'Σ'``. This is the inverse of :func:`ord`.
+
+ The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in
+ base 16). :exc:`ValueError` will be raised if *i* is outside that range.
.. function:: classmethod(function)
@@ -972,9 +973,11 @@ are always available. They are listed here in alphabetical order.
Characters not supported by the encoding are replaced with the
appropriate XML character reference ``&#nnn;``.
- * ``'backslashreplace'`` (also only supported when writing)
- replaces unsupported characters with Python's backslashed escape
- sequences.
+ * ``'backslashreplace'`` replaces malformed data by Python's backslashed
+ escape sequences.
+
+ * ``'namereplace'`` (also only supported when writing)
+ replaces unsupported characters with ``\N{...}`` escape sequences.
.. index::
single: universal newlines; open() built-in function
@@ -999,8 +1002,8 @@ are always available. They are listed here in alphabetical order.
If *closefd* is ``False`` and a file descriptor rather than a filename was
given, the underlying file descriptor will be kept open when the file is
- closed. If a filename is given *closefd* has no effect and must be ``True``
- (the default).
+ closed. If a filename is given *closefd* must be ``True`` (the default)
+ otherwise an error will be raised.
A custom opener can be used by passing a callable as *opener*. The underlying
file descriptor for the file object is then obtained by calling *opener* with
@@ -1063,13 +1066,12 @@ are always available. They are listed here in alphabetical order.
The ``'U'`` mode.
-.. XXX works for bytes too, but should it?
.. function:: ord(c)
Given a string representing one Unicode character, return an integer
- representing the Unicode code
- point of that character. For example, ``ord('a')`` returns the integer ``97``
- and ``ord('\u2020')`` returns ``8224``. This is the inverse of :func:`chr`.
+ representing the Unicode code point of that character. For example,
+ ``ord('a')`` returns the integer ``97`` and ``ord('Σ')`` returns ``931``.
+ This is the inverse of :func:`chr`.
.. function:: pow(x, y[, z])
diff --git a/Doc/library/gc.rst b/Doc/library/gc.rst
index 8135542..d11c2e1 100644
--- a/Doc/library/gc.rst
+++ b/Doc/library/gc.rst
@@ -186,7 +186,7 @@ values but should not rebind them):
added to this list rather than freed.
.. versionchanged:: 3.2
- If this list is non-empty at interpreter shutdown, a
+ If this list is non-empty at :term:`interpreter shutdown`, a
:exc:`ResourceWarning` is emitted, which is silent by default. If
:const:`DEBUG_UNCOLLECTABLE` is set, in addition all uncollectable objects
are printed.
@@ -252,8 +252,8 @@ The following constants are provided for use with :func:`set_debug`:
to the ``garbage`` list.
.. versionchanged:: 3.2
- Also print the contents of the :data:`garbage` list at interpreter
- shutdown, if it isn't empty.
+ Also print the contents of the :data:`garbage` list at
+ :term:`interpreter shutdown`, if it isn't empty.
.. data:: DEBUG_SAVEALL
diff --git a/Doc/library/gettext.rst b/Doc/library/gettext.rst
index ff23b59..514cc5a 100644
--- a/Doc/library/gettext.rst
+++ b/Doc/library/gettext.rst
@@ -344,9 +344,9 @@ will assume message ids as Unicode strings, not byte strings.
The entire set of key/value pairs are placed into a dictionary and set as the
"protected" :attr:`_info` instance variable.
-If the :file:`.mo` file's magic number is invalid, or if other problems occur
-while reading the file, instantiating a :class:`GNUTranslations` class can raise
-:exc:`OSError`.
+If the :file:`.mo` file's magic number is invalid, the major version number is
+unexpected, or if other problems occur while reading the file, instantiating a
+:class:`GNUTranslations` class can raise :exc:`OSError`.
The following methods are overridden from the base class implementation:
diff --git a/Doc/library/glob.rst b/Doc/library/glob.rst
index abcbf38..50f38a4 100644
--- a/Doc/library/glob.rst
+++ b/Doc/library/glob.rst
@@ -29,7 +29,7 @@ For example, ``'[?]'`` matches the character ``'?'``.
The :mod:`pathlib` module offers high-level path objects.
-.. function:: glob(pathname)
+.. function:: glob(pathname, *, recursive=False)
Return a possibly-empty list of path names that match *pathname*, which must be
a string containing a path specification. *pathname* can be either absolute
@@ -37,8 +37,19 @@ For example, ``'[?]'`` matches the character ``'?'``.
:file:`../../Tools/\*/\*.gif`), and can contain shell-style wildcards. Broken
symlinks are included in the results (as in the shell).
+ If *recursive* is true, the pattern "``**``" will match any files and zero or
+ more directories and subdirectories. If the pattern is followed by a
+ ``os.sep``, only directories and subdirectories match.
-.. function:: iglob(pathname)
+ .. note::
+ Using the "``**``" pattern in large directory trees may consume
+ an inordinate amount of time.
+
+ .. versionchanged:: 3.5
+ Support for recursive globs using "``**``".
+
+
+.. function:: iglob(pathname, recursive=False)
Return an :term:`iterator` which yields the same values as :func:`glob`
without actually storing them all simultaneously.
@@ -55,8 +66,9 @@ For example, ``'[?]'`` matches the character ``'?'``.
.. versionadded:: 3.4
-For example, consider a directory containing only the following files:
-:file:`1.gif`, :file:`2.txt`, and :file:`card.gif`. :func:`glob` will produce
+For example, consider a directory containing the following files:
+:file:`1.gif`, :file:`2.txt`, :file:`card.gif` and a subdirectory :file:`sub`
+which contains only the file :file:`3.txt`. :func:`glob` will produce
the following results. Notice how any leading components of the path are
preserved. ::
@@ -67,6 +79,10 @@ preserved. ::
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
+ >>> glob.glob('**/*.txt', recursive=True)
+ ['2.txt', 'sub/3.txt']
+ >>> glob.glob('./**/', recursive=True)
+ ['./', './sub/']
If the directory contains files starting with ``.`` they won't be matched by
default. For example, consider a directory containing :file:`card.gif` and
diff --git a/Doc/library/heapq.rst b/Doc/library/heapq.rst
index 43088ad..9682b59 100644
--- a/Doc/library/heapq.rst
+++ b/Doc/library/heapq.rst
@@ -81,7 +81,7 @@ The following functions are provided:
The module also offers three general purpose functions based on heaps.
-.. function:: merge(*iterables)
+.. function:: merge(*iterables, key=None, reverse=False)
Merge multiple sorted inputs into a single sorted output (for example, merge
timestamped entries from multiple log files). Returns an :term:`iterator`
@@ -91,6 +91,18 @@ The module also offers three general purpose functions based on heaps.
not pull the data into memory all at once, and assumes that each of the input
streams is already sorted (smallest to largest).
+ Has two optional arguments which must be specified as keyword arguments.
+
+ *key* specifies a :term:`key function` of one argument that is used to
+ extract a comparison key from each input element. The default value is
+ ``None`` (compare the elements directly).
+
+ *reverse* is a boolean value. If set to ``True``, then the input elements
+ are merged as if each comparison were reversed.
+
+ .. versionchanged:: 3.5
+ Added the optional *key* and *reverse* parameters.
+
.. function:: nlargest(n, iterable, key=None)
diff --git a/Doc/library/html.parser.rst b/Doc/library/html.parser.rst
index 44b7d6e..b84c60b 100644
--- a/Doc/library/html.parser.rst
+++ b/Doc/library/html.parser.rst
@@ -16,21 +16,13 @@
This module defines a class :class:`HTMLParser` which serves as the basis for
parsing text files formatted in HTML (HyperText Mark-up Language) and XHTML.
-.. class:: HTMLParser(strict=False, *, convert_charrefs=False)
+.. class:: HTMLParser(*, convert_charrefs=True)
- Create a parser instance.
+ Create a parser instance able to parse invalid markup.
- If *convert_charrefs* is ``True`` (default: ``False``), all character
+ If *convert_charrefs* is ``True`` (the default), all character
references (except the ones in ``script``/``style`` elements) are
automatically converted to the corresponding Unicode characters.
- The use of ``convert_charrefs=True`` is encouraged and will become
- the default in Python 3.5.
-
- If *strict* is ``False`` (the default), the parser will accept and parse
- invalid markup. If *strict* is ``True`` the parser will raise an
- :exc:`~html.parser.HTMLParseError` exception instead [#]_ when it's not
- able to parse the markup. The use of ``strict=True`` is discouraged and
- the *strict* argument is deprecated.
An :class:`.HTMLParser` instance is fed HTML data and calls handler methods
when start tags, end tags, text, comments, and other markup elements are
@@ -40,31 +32,11 @@ parsing text files formatted in HTML (HyperText Mark-up Language) and XHTML.
This parser does not check that end tags match start tags or call the end-tag
handler for elements which are closed implicitly by closing an outer element.
- .. versionchanged:: 3.2
- *strict* argument added.
-
- .. deprecated-removed:: 3.3 3.5
- The *strict* argument and the strict mode have been deprecated.
- The parser is now able to accept and parse invalid markup too.
-
.. versionchanged:: 3.4
*convert_charrefs* keyword argument added.
-An exception is defined as well:
-
-
-.. exception:: HTMLParseError
-
- Exception raised by the :class:`HTMLParser` class when it encounters an error
- while parsing and *strict* is ``True``. This exception provides three
- attributes: :attr:`msg` is a brief message explaining the error,
- :attr:`lineno` is the number of the line on which the broken construct was
- detected, and :attr:`offset` is the number of characters into the line at
- which the construct starts.
-
- .. deprecated-removed:: 3.3 3.5
- This exception has been deprecated because it's never raised by the parser
- (when the default non-strict mode is used).
+ .. versionchanged:: 3.5
+ The default value for argument *convert_charrefs* is now ``True``.
Example HTML Parser Application
@@ -246,8 +218,7 @@ implementations do nothing (except for :meth:`~HTMLParser.handle_startendtag`):
The *data* parameter will be the entire contents of the declaration inside
the ``<![...]>`` markup. It is sometimes useful to be overridden by a
- derived class. The base class implementation raises an :exc:`HTMLParseError`
- when *strict* is ``True``.
+ derived class. The base class implementation does nothing.
.. _htmlparser-examples:
@@ -358,9 +329,3 @@ Parsing invalid HTML (e.g. unquoted attributes) also works::
Data : tag soup
End tag : p
End tag : a
-
-.. rubric:: Footnotes
-
-.. [#] For backward compatibility reasons *strict* mode does not raise
- exceptions for all non-compliant HTML. That is, some invalid HTML
- is tolerated even in *strict* mode.
diff --git a/Doc/library/http.client.rst b/Doc/library/http.client.rst
index a3f2e35..b495f41 100644
--- a/Doc/library/http.client.rst
+++ b/Doc/library/http.client.rst
@@ -180,221 +180,15 @@ The constants defined in this module are:
The default port for the HTTPS protocol (always ``443``).
-and also the following constants for integer status codes:
-
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| Constant | Value | Definition |
-+==========================================+=========+=======================================================================+
-| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.1.1 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.1.2 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
-| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.2.1 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.2.2 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.2.3 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.2.4 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.2.5 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.2.6 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.2.7 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
-| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
-| | | :rfc:`3229`, Section 10.4.1 |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.3.1 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.3.2 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.3.3 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.3.4 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.3.5 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.3.6 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.3.8 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.1 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.2 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.3 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.4 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.5 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.6 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.7 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.8 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.9 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.10 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.11 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.12 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.13 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.14 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.15 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.16 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.17 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.4.18 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
-| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
-| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
-| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
-| | | :rfc:`2817`, Section 6 |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`PRECONDITION_REQUIRED` | ``428`` | Additional HTTP Status Codes, |
-| | | :rfc:`6585`, Section 3 |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`TOO_MANY_REQUESTS` | ``429`` | Additional HTTP Status Codes, |
-| | | :rfc:`6585`, Section 4 |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`REQUEST_HEADER_FIELDS_TOO_LARGE` | ``431`` | Additional HTTP Status Codes, |
-| | | :rfc:`6585`, Section 5 |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.5.1 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.5.2 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
-| | | 10.5.3 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.5.4 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
-| | | 10.5.5 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
-| | | 10.5.6 |
-| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
-| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
-| | | :rfc:`2774`, Section 7 |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-| :const:`NETWORK_AUTHENTICATION_REQUIRED` | ``511`` | Additional HTTP Status Codes, |
-| | | :rfc:`6585`, Section 6 |
-+------------------------------------------+---------+-----------------------------------------------------------------------+
-
-.. versionchanged:: 3.3
- Added codes ``428``, ``429``, ``431`` and ``511`` from :rfc:`6585`.
-
-
.. data:: responses
This dictionary maps the HTTP 1.1 status codes to the W3C names.
Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``.
+See :ref:`http-status-codes` for a list of HTTP status codes that are
+available in this module as constants.
+
.. _httpconnection-objects:
diff --git a/Doc/library/http.rst b/Doc/library/http.rst
index a387a37..b6f2c58 100644
--- a/Doc/library/http.rst
+++ b/Doc/library/http.rst
@@ -1,7 +1,16 @@
:mod:`http` --- HTTP modules
============================
-``http`` is a package that collects several modules for working with the
+.. module:: http
+ :synopsis: HTTP status codes and messages
+
+.. index::
+ pair: HTTP; protocol
+ single: HTTP; http (standard module)
+
+**Source code:** :source:`Lib/http/__init__.py`
+
+:mod:`http` is a package that collects several modules for working with the
HyperText Transfer Protocol:
* :mod:`http.client` is a low-level HTTP protocol client; for high-level URL
@@ -9,3 +18,105 @@ HyperText Transfer Protocol:
* :mod:`http.server` contains basic HTTP server classes based on :mod:`socketserver`
* :mod:`http.cookies` has utilities for implementing state management with cookies
* :mod:`http.cookiejar` provides persistence of cookies
+
+:mod:`http` is also a module that defines a number of HTTP status codes and
+associated messages through the :class:`http.HTTPStatus` enum:
+
+.. class:: HTTPStatus
+
+ .. versionadded:: 3.5
+
+ A subclass of :class:`enum.IntEnum` that defines a set of HTTP status codes,
+ reason phrases and long descriptions written in English.
+
+ Usage::
+
+ >>> from http import HTTPStatus
+ >>> HTTPStatus.OK
+ <HTTPStatus.OK: 200>
+ >>> HTTPStatus.OK == 200
+ True
+ >>> http.HTTPStatus.OK.value
+ 200
+ >>> HTTPStatus.OK.phrase
+ 'OK'
+ >>> HTTPStatus.OK.description
+ 'Request fulfilled, document follows'
+ >>> list(HTTPStatus)
+ [<HTTPStatus.CONTINUE: 100>, <HTTPStatus.SWITCHING_PROTOCOLS: 101>, ...]
+
+.. _http-status-codes:
+
+HTTP status codes
+-----------------
+
+Supported,
+`IANA-registered <http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml>`_
+status codes available in :class:`http.HTTPStatus` are:
+
+======= =================================== ==================================================================
+Code Enum Name Details
+======= =================================== ==================================================================
+``100`` ``CONTINUE`` HTTP/1.1 :rfc:`7231`, Section 6.2.1
+``101`` ``SWITCHING_PROTOCOLS`` HTTP/1.1 :rfc:`7231`, Section 6.2.2
+``102`` ``PROCESSING`` WebDAV :rfc:`2518`, Section 10.1
+``200`` ``OK`` HTTP/1.1 :rfc:`7231`, Section 6.3.1
+``201`` ``CREATED`` HTTP/1.1 :rfc:`7231`, Section 6.3.2
+``202`` ``ACCEPTED`` HTTP/1.1 :rfc:`7231`, Section 6.3.3
+``203`` ``NON_AUTHORITATIVE_INFORMATION`` HTTP/1.1 :rfc:`7231`, Section 6.3.4
+``204`` ``NO_CONTENT`` HTTP/1.1 :rfc:`7231`, Section 6.3.5
+``205`` ``RESET_CONTENT`` HTTP/1.1 :rfc:`7231`, Section 6.3.6
+``206`` ``PARTIAL_CONTENT`` HTTP/1.1 :rfc:`7233`, Section 4.1
+``207`` ``MULTI_STATUS`` WebDAV :rfc:`4918`, Section 11.1
+``208`` ``ALREADY_REPORTED`` WebDAV Binding Extensions :rfc:`5842`, Section 7.1 (Experimental)
+``226`` ``IM_USED`` Delta Encoding in HTTP :rfc:`3229`, Section 10.4.1
+``300`` ``MULTIPLE_CHOICES`` HTTP/1.1 :rfc:`7231`, Section 6.4.1
+``301`` ``MOVED_PERMANENTLY`` HTTP/1.1 :rfc:`7231`, Section 6.4.2
+``302`` ``FOUND`` HTTP/1.1 :rfc:`7231`, Section 6.4.3
+``303`` ``SEE_OTHER`` HTTP/1.1 :rfc:`7231`, Section 6.4.4
+``304`` ``NOT_MODIFIED`` HTTP/1.1 :rfc:`7232`, Section 4.1
+``305`` ``USE_PROXY`` HTTP/1.1 :rfc:`7231`, Section 6.4.5
+``307`` ``TEMPORARY_REDIRECT`` HTTP/1.1 :rfc:`7231`, Section 6.4.7
+``308`` ``PERMANENT_REDIRECT`` Permanent Redirect :rfc:`7238`, Section 3 (Experimental)
+``400`` ``BAD_REQUEST`` HTTP/1.1 :rfc:`7231`, Section 6.5.1
+``401`` ``UNAUTHORIZED`` HTTP/1.1 Authentication :rfc:`7235`, Section 3.1
+``402`` ``PAYMENT_REQUIRED`` HTTP/1.1 :rfc:`7231`, Section 6.5.2
+``403`` ``FORBIDDEN`` HTTP/1.1 :rfc:`7231`, Section 6.5.3
+``404`` ``NOT_FOUND`` HTTP/1.1 :rfc:`7231`, Section 6.5.4
+``405`` ``METHOD_NOT_ALLOWED`` HTTP/1.1 :rfc:`7231`, Section 6.5.5
+``406`` ``NOT_ACCEPTABLE`` HTTP/1.1 :rfc:`7231`, Section 6.5.6
+``407`` ``PROXY_AUTHENTICATION_REQUIRED`` HTTP/1.1 Authentication :rfc:`7235`, Section 3.2
+``408`` ``REQUEST_TIMEOUT`` HTTP/1.1 :rfc:`7231`, Section 6.5.7
+``409`` ``CONFLICT`` HTTP/1.1 :rfc:`7231`, Section 6.5.8
+``410`` ``GONE`` HTTP/1.1 :rfc:`7231`, Section 6.5.9
+``411`` ``LENGTH_REQUIRED`` HTTP/1.1 :rfc:`7231`, Section 6.5.10
+``412`` ``PRECONDITION_FAILED`` HTTP/1.1 :rfc:`7232`, Section 4.2
+``413`` ``REQUEST_ENTITY_TOO_LARGE`` HTTP/1.1 :rfc:`7231`, Section 6.5.11
+``414`` ``REQUEST_URI_TOO_LONG`` HTTP/1.1 :rfc:`7231`, Section 6.5.12
+``415`` ``UNSUPPORTED_MEDIA_TYPE`` HTTP/1.1 :rfc:`7231`, Section 6.5.13
+``416`` ``REQUEST_RANGE_NOT_SATISFIABLE`` HTTP/1.1 Range Requests :rfc:`7233`, Section 4.4
+``417`` ``EXPECTATION_FAILED`` HTTP/1.1 :rfc:`7231`, Section 6.5.14
+``422`` ``UNPROCESSABLE_ENTITY`` WebDAV :rfc:`4918`, Section 11.2
+``423`` ``LOCKED`` WebDAV :rfc:`4918`, Section 11.3
+``424`` ``FAILED_DEPENDENCY`` WebDAV :rfc:`4918`, Section 11.4
+``426`` ``UPGRADE_REQUIRED`` HTTP/1.1 :rfc:`7231`, Section 6.5.15
+``428`` ``PRECONDITION_REQUIRED`` Additional HTTP Status Codes :rfc:`6585`
+``429`` ``TOO_MANY_REQUESTS`` Additional HTTP Status Codes :rfc:`6585`
+``431`` ``REQUEST_HEADER_FIELDS_TOO_LARGE`` Additional HTTP Status Codes :rfc:`6585`
+``500`` ``INTERNAL_SERVER_ERROR`` HTTP/1.1 :rfc:`7231`, Section 6.6.1
+``501`` ``NOT_IMPLEMENTED`` HTTP/1.1 :rfc:`7231`, Section 6.6.2
+``502`` ``BAD_GATEWAY`` HTTP/1.1 :rfc:`7231`, Section 6.6.3
+``503`` ``SERVICE_UNAVAILABLE`` HTTP/1.1 :rfc:`7231`, Section 6.6.4
+``504`` ``GATEWAY_TIMEOUT`` HTTP/1.1 :rfc:`7231`, Section 6.6.5
+``505`` ``HTTP_VERSION_NOT_SUPPORTED`` HTTP/1.1 :rfc:`7231`, Section 6.6.6
+``506`` ``VARIANT_ALSO_NEGOTIATES`` Transparent Content Negotiation in HTTP :rfc:`2295`, Section 8.1 (Experimental)
+``507`` ``INSUFFICIENT_STORAGE`` WebDAV :rfc:`4918`, Section 11.5
+``508`` ``LOOP_DETECTED`` WebDAV Binding Extensions :rfc:`5842`, Section 7.2 (Experimental)
+``510`` ``NOT_EXTENDED`` An HTTP Extension Framework :rfc:`2774`, Section 7 (Experimental)
+``511`` ``NETWORK_AUTHENTICATION_REQUIRED`` Additional HTTP Status Codes :rfc:`6585`, Section 6
+======= =================================== ==================================================================
+
+In order to preserve backwards compatibility, enum values are also present
+in the :mod:`http.client` module in the form of constants. The enum name is
+equal to the constant name (i.e. ``http.HTTPStatus.OK`` is also available as
+``http.client.OK``).
diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst
index 0d8e7fe..ec54643 100644
--- a/Doc/library/http.server.rst
+++ b/Doc/library/http.server.rst
@@ -220,7 +220,7 @@ of which this module provides three different variants:
.. method:: send_response_only(code, message=None)
- Sends the reponse header only, used for the purposes when ``100
+ Sends the response header only, used for the purposes when ``100
Continue`` response is sent by the server to the client. The headers not
buffered and sent directly the output stream.If the *message* is not
specified, the HTTP message corresponding the response *code* is sent.
diff --git a/Doc/library/imaplib.rst b/Doc/library/imaplib.rst
index fa736fe..f263bab 100644
--- a/Doc/library/imaplib.rst
+++ b/Doc/library/imaplib.rst
@@ -37,6 +37,19 @@ base class:
initialized. If *host* is not specified, ``''`` (the local host) is used. If
*port* is omitted, the standard IMAP4 port (143) is used.
+ The :class:`IMAP4` class supports the :keyword:`with` statement. When used
+ like this, the IMAP4 ``LOGOUT`` command is issued automatically when the
+ :keyword:`with` statement exits. E.g.::
+
+ >>> from imaplib import IMAP4
+ >>> with IMAP4("domain.org") as M:
+ ... M.noop()
+ ...
+ ('OK', [b'Nothing Accomplished. d25if65hy903weo.87'])
+
+ .. versionchanged:: 3.5
+ Support for the :keyword:`with` statement was added.
+
Three exceptions are defined as attributes of the :class:`IMAP4` class:
diff --git a/Doc/library/imghdr.rst b/Doc/library/imghdr.rst
index 9e89523..c60df24 100644
--- a/Doc/library/imghdr.rst
+++ b/Doc/library/imghdr.rst
@@ -48,6 +48,16 @@ from :func:`what`:
+------------+-----------------------------------+
| ``'png'`` | Portable Network Graphics |
+------------+-----------------------------------+
+| ``'webp'`` | WebP files |
++------------+-----------------------------------+
+| ``'exr'`` | OpenEXR Files |
++------------+-----------------------------------+
+
+.. versionadded:: 3.5
+ The *exr* format was added.
+
+.. versionchanged:: 3.5
+ The *webp* type was added.
You can extend the list of file types :mod:`imghdr` can recognize by appending
to this variable:
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
index 91328af..9c6b280 100644
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -347,13 +347,16 @@ ABC hierarchy::
.. method:: create_module(spec)
- An optional method that returns the module object to use when
- importing a module. create_module() may also return ``None``,
- indicating that the default module creation should take place
- instead.
+ A method that returns the module object to use when
+ importing a module. This method may return ``None``,
+ indicating that default module creation semantics should take place.
.. versionadded:: 3.4
+ .. versionchanged:: 3.5
+ Starting in Python 3.6, this method will not be optional when
+ :meth:`exec_module` is defined.
+
.. method:: exec_module(module)
An abstract method that executes the module in its own namespace
@@ -417,7 +420,7 @@ ABC hierarchy::
.. deprecated:: 3.4
The recommended API for loading a module is :meth:`exec_module`
- (and optionally :meth:`create_module`). Loaders should implement
+ (and :meth:`create_module`). Loaders should implement
it instead of load_module(). The import machinery takes care of
all the other responsibilities of load_module() when exec_module()
is implemented.
@@ -499,7 +502,7 @@ ABC hierarchy::
.. versionchanged:: 3.4
Raises :exc:`ImportError` instead of :exc:`NotImplementedError`.
- .. method:: source_to_code(data, path='<string>')
+ .. staticmethod:: source_to_code(data, path='<string>')
Create a code object from Python source.
@@ -508,8 +511,14 @@ ABC hierarchy::
the "path" to where the source code originated from, which can be an
abstract concept (e.g. location in a zip file).
+ With the subsequent code object one can execute it in a module by
+ running ``exec(code, module.__dict__)``.
+
.. versionadded:: 3.4
+ .. versionchanged:: 3.5
+ Made the method static.
+
.. method:: exec_module(module)
Implementation of :meth:`Loader.exec_module`.
@@ -788,6 +797,11 @@ find and load modules.
.. versionadded:: 3.4
+ .. versionchanged:: 3.5
+ If the current working directory -- represented by an empty string --
+ is no longer valid then ``None`` is returned but no value is cached
+ in :data:`sys.path_importer_cache`.
+
.. classmethod:: find_module(fullname, path=None)
A legacy wrapper around :meth:`find_spec`.
@@ -1123,6 +1137,21 @@ an :term:`importer`.
.. versionadded:: 3.4
+.. function:: module_from_spec(spec)
+
+ Create a new module based on **spec** and ``spec.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.
+
+ 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
+ the module as possible.
+
+ .. versionadded:: 3.5
+
.. decorator:: module_for_loader
A :term:`decorator` for :meth:`importlib.abc.Loader.load_module`
@@ -1201,3 +1230,39 @@ an :term:`importer`.
module will be file-based.
.. versionadded:: 3.4
+
+.. class:: LazyLoader(loader)
+
+ A class which postpones the execution of the loader of a module until the
+ module has an attribute accessed.
+
+ 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``). 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
+ potentially minimizing the cost of loading a module if it is never used.
+ For projects where startup time is not essential then use of this class is
+ **heavily** discouraged due to error messages created during loading being
+ postponed and thus occurring out of context.
+
+ .. versionadded:: 3.5
+
+ .. classmethod:: factory(loader)
+
+ A static method which returns a callable that creates a lazy loader. This
+ is meant to be used in situations where the loader is passed by class
+ instead of by instance.
+ ::
+
+ suffixes = importlib.machinery.SOURCE_SUFFIXES
+ loader = importlib.machinery.SourceFileLoader
+ lazy_loader = importlib.util.LazyLoader.factory(loader)
+ finder = importlib.machinery.FileFinder(path, [(lazy_loader, suffixes)])
diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst
index c540b11..3d2132f 100644
--- a/Doc/library/inspect.rst
+++ b/Doc/library/inspect.rst
@@ -159,6 +159,16 @@ attributes:
| | | arguments and local |
| | | variables |
+-----------+-----------------+---------------------------+
+| generator | __name__ | name |
++-----------+-----------------+---------------------------+
+| | __qualname__ | qualified name |
++-----------+-----------------+---------------------------+
+| | gi_frame | frame |
++-----------+-----------------+---------------------------+
+| | gi_running | is the generator running? |
++-----------+-----------------+---------------------------+
+| | gi_code | code |
++-----------+-----------------+---------------------------+
| builtin | __doc__ | documentation string |
+-----------+-----------------+---------------------------+
| | __name__ | original name of this |
@@ -169,6 +179,12 @@ attributes:
| | | ``None`` |
+-----------+-----------------+---------------------------+
+.. versionchanged:: 3.5
+
+ Add ``__qualname__`` attribute to generators. The ``__name__`` attribute of
+ generators is now set from the function name, instead of the code name, and
+ it can now be modified.
+
.. function:: getmembers(object[, predicate])
@@ -462,6 +478,9 @@ function.
Signature objects are *immutable*. Use :meth:`Signature.replace` to make a
modified copy.
+ .. versionchanged:: 3.5
+ Signature objects are picklable and hashable.
+
.. attribute:: Signature.empty
A special class-level marker to specify absence of a return annotation.
@@ -506,12 +525,29 @@ function.
>>> str(new_sig)
"(a, b) -> 'new return anno'"
+ .. classmethod:: Signature.from_callable(obj)
+
+ Return a :class:`Signature` (or its subclass) object for a given callable
+ ``obj``. This method simplifies subclassing of :class:`Signature`:
+
+ ::
+
+ class MySignature(Signature):
+ pass
+ sig = MySignature.from_callable(min)
+ assert isinstance(sig, MySignature)
+
+ .. versionadded:: 3.5
+
.. class:: Parameter(name, kind, \*, default=Parameter.empty, annotation=Parameter.empty)
Parameter objects are *immutable*. Instead of modifying a Parameter object,
you can use :meth:`Parameter.replace` to create a modified copy.
+ .. versionchanged:: 3.5
+ Parameter objects are picklable and hashable.
+
.. attribute:: Parameter.empty
A special class-level marker to specify absence of default values and
@@ -849,11 +885,17 @@ Classes and functions
The interpreter stack
---------------------
-When the following functions return "frame records," each record is a tuple of
-six items: the frame object, the filename, the line number of the current line,
+When the following functions return "frame records," each record is a
+:term:`named tuple`
+``FrameInfo(frame, filename, lineno, function, code_context, index)``.
+The tuple contains the frame object, the filename, the line number of the
+current line,
the function name, a list of lines of context from the source code, and the
index of the current line within that list.
+.. versionchanged:: 3.5
+ Return a named tuple instead of a tuple.
+
.. note::
Keeping references to frame objects, as found in the first element of the frame
diff --git a/Doc/library/io.rst b/Doc/library/io.rst
index 3adf6e9..634bf58 100644
--- a/Doc/library/io.rst
+++ b/Doc/library/io.rst
@@ -391,8 +391,8 @@ I/O Base Classes
.. method:: readinto(b)
Read up to ``len(b)`` bytes into :class:`bytearray` *b* and return the
- number of bytes read. If the object is in non-blocking mode and no
- bytes are available, ``None`` is returned.
+ number of bytes read. If the object is in non-blocking mode and no bytes
+ are available, ``None`` is returned.
.. method:: write(b)
@@ -465,9 +465,10 @@ I/O Base Classes
.. method:: read1(size=-1)
- Read and return up to *size* bytes, with at most one call to the underlying
- raw stream's :meth:`~RawIOBase.read` method. This can be useful if you
- are implementing your own buffering on top of a :class:`BufferedIOBase`
+ Read and return up to *size* bytes, with at most one call to the
+ underlying raw stream's :meth:`~RawIOBase.read` (or
+ :meth:`~RawIOBase.readinto`) method. This can be useful if you are
+ implementing your own buffering on top of a :class:`BufferedIOBase`
object.
.. method:: readinto(b)
@@ -478,8 +479,19 @@ I/O Base Classes
Like :meth:`read`, multiple reads may be issued to the underlying raw
stream, unless the latter is interactive.
- A :exc:`BlockingIOError` is raised if the underlying raw stream is in
- non blocking-mode, and has no data available at the moment.
+ A :exc:`BlockingIOError` is raised if the underlying raw stream is in non
+ blocking-mode, and has no data available at the moment.
+
+ .. method:: readinto1(b)
+
+ Read up to ``len(b)`` bytes into bytearray *b*, ,using at most one call to
+ the underlying raw stream's :meth:`~RawIOBase.read` (or
+ :meth:`~RawIOBase.readinto`) method. Return the number of bytes read.
+
+ A :exc:`BlockingIOError` is raised if the underlying raw stream is in non
+ blocking-mode, and has no data available at the moment.
+
+ .. versionadded:: 3.5
.. method:: write(b)
@@ -507,9 +519,12 @@ Raw File I/O
The *name* can be one of two things:
* a character string or :class:`bytes` object representing the path to the
- file which will be opened;
+ file which will be opened. In this case closefd must be True (the default)
+ otherwise an error will be raised.
* an integer representing the number of an existing OS-level file descriptor
- to which the resulting :class:`FileIO` object will give access.
+ to which the resulting :class:`FileIO` object will give access. When the
+ FileIO object is closed this fd will be closed as well, unless *closefd*
+ is set to ``False``.
The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading
(default), writing, exclusive creation or appending. The file will be
@@ -598,6 +613,11 @@ than raw I/O does.
In :class:`BytesIO`, this is the same as :meth:`read`.
+ .. method:: readinto1()
+
+ In :class:`BytesIO`, this is the same as :meth:`readinto`.
+
+ .. versionadded:: 3.5
.. class:: BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)
@@ -807,11 +827,13 @@ Text I/O
exception if there is an encoding error (the default of ``None`` has the same
effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding
errors can lead to data loss.) ``'replace'`` causes a replacement marker
- (such as ``'?'``) to be inserted where there is malformed data. When
- writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character
- reference) or ``'backslashreplace'`` (replace with backslashed escape
- sequences) can be used. Any other error handling name that has been
- registered with :func:`codecs.register_error` is also valid.
+ (such as ``'?'``) to be inserted where there is malformed data.
+ ``'backslashreplace'`` causes malformed data to be replaced by a
+ backslashed escape sequence. When writing, ``'xmlcharrefreplace'``
+ (replace with the appropriate XML character reference) or ``'namereplace'``
+ (replace with ``\N{...}`` escape sequences) can be used. Any other error
+ handling name that has been registered with
+ :func:`codecs.register_error` is also valid.
.. index::
single: universal newlines; io.TextIOWrapper class
diff --git a/Doc/library/ipaddress.rst b/Doc/library/ipaddress.rst
index ca87980..7b59440 100644
--- a/Doc/library/ipaddress.rst
+++ b/Doc/library/ipaddress.rst
@@ -103,7 +103,7 @@ write code that handles both IP versions correctly.
1. A string in decimal-dot notation, consisting of four decimal integers in
the inclusive range 0-255, separated by dots (e.g. ``192.168.0.1``). Each
integer represents an octet (byte) in the address. Leading zeroes are
- tolerated only for values less then 8 (as there is no ambiguity
+ tolerated only for values less than 8 (as there is no ambiguity
between the decimal and octal interpretations of such strings).
2. An integer that fits into 32 bits.
3. An integer packed into a :class:`bytes` object of length 4 (most
@@ -146,6 +146,20 @@ write code that handles both IP versions correctly.
the appropriate length (most significant octet first). This is 4 bytes
for IPv4 and 16 bytes for IPv6.
+ .. attribute:: reverse_pointer
+
+ The name of the reverse DNS PTR record for the IP address, e.g.::
+
+ >>> ipaddress.ip_address("127.0.0.1").reverse_pointer
+ '1.0.0.127.in-addr.arpa'
+ >>> ipaddress.ip_address("2001:db8::1").reverse_pointer
+ '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa'
+
+ This is the name that could be used for performing a PTR lookup, not the
+ resolved hostname itself.
+
+ .. versionadded:: 3.5
+
.. attribute:: is_multicast
``True`` if the address is reserved for multicast use. See
@@ -226,6 +240,7 @@ write code that handles both IP versions correctly.
:class:`IPv4Address` class:
.. attribute:: packed
+ .. attribute:: reverse_pointer
.. attribute:: version
.. attribute:: max_prefixlen
.. attribute:: is_multicast
@@ -377,6 +392,12 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
3. An integer packed into a :class:`bytes` object of length 4, big-endian.
The interpretation is similar to an integer *address*.
+ 4. A two-tuple of an address description and a netmask, where the address
+ description is either a string, a 32-bits integer, a 4-bytes packed
+ integer, or an existing IPv4Address object; and the netmask is either
+ an integer representing the prefix length (e.g. ``24``) or a string
+ representing the prefix mask (e.g. ``255.255.255.0``).
+
An :exc:`AddressValueError` is raised if *address* is not a valid IPv4
address. A :exc:`NetmaskValueError` is raised if the mask is not valid for
an IPv4 address.
@@ -389,6 +410,10 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
objects will raise :exc:`TypeError` if the argument's IP version is
incompatible to ``self``
+ .. versionchanged:: 3.5
+
+ Added the two-tuple form for the *address* constructor parameter.
+
.. attribute:: version
.. attribute:: max_prefixlen
@@ -553,6 +578,11 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
3. An integer packed into a :class:`bytes` object of length 16, bit-endian.
The interpretation is similar to an integer *address*.
+ 4. A two-tuple of an address description and a netmask, where the address
+ description is either a string, a 128-bits integer, a 16-bytes packed
+ integer, or an existing IPv4Address object; and the netmask is an
+ integer representing the prefix length.
+
An :exc:`AddressValueError` is raised if *address* is not a valid IPv6
address. A :exc:`NetmaskValueError` is raised if the mask is not valid for
an IPv6 address.
@@ -561,6 +591,10 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
to determine the appropriate network address.
+ .. versionchanged:: 3.5
+
+ Added the two-tuple form for the *address* constructor parameter.
+
.. attribute:: version
.. attribute:: max_prefixlen
.. attribute:: is_multicast
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index f489535..8c7592d 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -87,10 +87,15 @@ loops that truncate the stream.
.. function:: accumulate(iterable[, func])
- Make an iterator that returns accumulated sums. Elements may be any addable
- type including :class:`~decimal.Decimal` or :class:`~fractions.Fraction`.
- If the optional *func* argument is supplied, it should be a function of two
- arguments and it will be used instead of addition.
+ Make an iterator that returns accumulated sums, or accumulated
+ results of other binary functions (specified via the optional
+ *func* argument). If *func* is supplied, it should be a function
+ of two arguments. Elements of the input *iterable* may be any type
+ that can be accepted as arguments to *func*. (For example, with
+ the default operation of addition, elements may be any addable
+ type including :class:`~decimal.Decimal` or
+ :class:`~fractions.Fraction`.) If the input iterable is empty, the
+ output iterable will also be empty.
Equivalent to::
@@ -99,7 +104,10 @@ loops that truncate the stream.
# accumulate([1,2,3,4,5]) --> 1 3 6 10 15
# accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
it = iter(iterable)
- total = next(it)
+ try:
+ total = next(it)
+ except StopIteration:
+ return
yield total
for element in it:
total = func(total, element)
@@ -400,7 +408,10 @@ loops that truncate the stream.
def _grouper(self, tgtkey):
while self.currkey == tgtkey:
yield self.currvalue
- self.currvalue = next(self.it) # Exit on StopIteration
+ try:
+ self.currvalue = next(self.it)
+ except StopIteration:
+ return
self.currkey = self.keyfunc(self.currvalue)
@@ -424,7 +435,10 @@ loops that truncate the stream.
# islice('ABCDEFG', 0, None, 2) --> A C E G
s = slice(*args)
it = iter(range(s.start or 0, s.stop or sys.maxsize, s.step or 1))
- nexti = next(it)
+ try:
+ nexti = next(it)
+ except StopIteration:
+ return
for i, element in enumerate(iterable):
if i == nexti:
yield element
@@ -582,7 +596,10 @@ loops that truncate the stream.
def gen(mydeque):
while True:
if not mydeque: # when the local deque is empty
- newval = next(it) # fetch a new value and
+ try:
+ newval = next(it) # fetch a new value and
+ except StopIteration:
+ return
for d in deques: # load it to all the deques
d.append(newval)
yield mydeque.popleft()
@@ -657,6 +674,11 @@ which incur interpreter overhead.
"Return function(0), function(1), ..."
return map(function, count(start))
+ def tail(n, iterable):
+ "Return an iterator over the last n items"
+ # tail(3, 'ABCDEFG') --> E F G
+ return iter(collections.deque(iterable, maxlen=n))
+
def consume(iterator, n):
"Advance the iterator n-steps ahead. If n is none, consume entirely."
# Use functions that consume iterators at C speed.
diff --git a/Doc/library/json.rst b/Doc/library/json.rst
index 6f5f8b1..49bb090 100644
--- a/Doc/library/json.rst
+++ b/Doc/library/json.rst
@@ -106,6 +106,8 @@ Using json.tool from the shell to validate and pretty-print::
$ echo '{1.2:3.4}' | python -m json.tool
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
+See :ref:`json-commandline` for detailed documentation.
+
.. highlight:: python3
.. note::
@@ -248,7 +250,7 @@ Basic Usage
will be passed to the constructor of the class.
If the data being deserialized is not a valid JSON document, a
- :exc:`ValueError` will be raised.
+ :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)
@@ -259,7 +261,7 @@ Basic Usage
*encoding* which is ignored and deprecated.
If the data being deserialized is not a valid JSON document, a
- :exc:`ValueError` will be raised.
+ :exc:`JSONDecodeError` will be raised.
Encoders and Decoders
---------------------
@@ -332,13 +334,16 @@ Encoders and Decoders
``'\n'``, ``'\r'`` and ``'\0'``.
If the data being deserialized is not a valid JSON document, a
- :exc:`ValueError` will be raised.
+ :exc:`JSONDecodeError` will be raised.
.. method:: decode(s)
Return the Python representation of *s* (a :class:`str` instance
containing a JSON document)
+ :exc:`JSONDecodeError` will be raised if the given JSON document is not
+ valid.
+
.. method:: raw_decode(s)
Decode a JSON document from *s* (a :class:`str` beginning with a
@@ -467,6 +472,36 @@ Encoders and Decoders
mysocket.write(chunk)
+Exceptions
+----------
+
+.. exception:: JSONDecodeError(msg, doc, pos, end=None)
+
+ Subclass of :exc:`ValueError` with the following additional attributes:
+
+ .. attribute:: msg
+
+ The unformatted error message.
+
+ .. attribute:: doc
+
+ The JSON document being parsed.
+
+ .. attribute:: pos
+
+ The start index of *doc* where parsing failed.
+
+ .. attribute:: lineno
+
+ The line corresponding to *pos*.
+
+ .. attribute:: colno
+
+ The column corresponding to *pos*.
+
+ .. versionadded:: 3.5
+
+
Standard Compliance and Interoperability
----------------------------------------
@@ -588,6 +623,68 @@ when serializing Python :class:`int` values of extremely large magnitude, or
when serializing instances of "exotic" numerical types such as
:class:`decimal.Decimal`.
+.. highlight:: bash
+.. module:: json.tool
+
+.. _json-commandline:
+
+Command Line Interface
+----------------------
+
+The :mod:`json.tool` module provides a simple command line interface to validate
+and pretty-print JSON objects.
+
+If the optional ``infile`` and ``outfile`` arguments are not
+specified, :attr:`sys.stdin` and :attr:`sys.stdout` will be used respectively::
+
+ $ echo '{"json": "obj"}' | python -m json.tool
+ {
+ "json": "obj"
+ }
+ $ echo '{1.2:3.4}' | python -m json.tool
+ Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
+
+.. versionchanged:: 3.5
+ The output is now in the same order as the input. Use the
+ :option:`--sort-keys` option to sort the output of dictionaries
+ alphabetically by key.
+
+Command line options
+^^^^^^^^^^^^^^^^^^^^
+
+.. cmdoption:: infile
+
+ The JSON file to be validated or pretty-printed::
+
+ $ python -m json.tool mp_films.json
+ [
+ {
+ "title": "And Now for Something Completely Different",
+ "year": 1971
+ },
+ {
+ "title": "Monty Python and the Holy Grail",
+ "year": 1975
+ }
+ ]
+
+ If *infile* is not specified, read from :attr:`sys.stdin`.
+
+.. cmdoption:: outfile
+
+ Write the output of the *infile* to the given *outfile*. Otherwise, write it
+ to :attr:`sys.stdout`.
+
+.. cmdoption:: --sort-keys
+
+ Sort the output of dictionaries alphabetically by key.
+
+ .. versionadded:: 3.5
+
+.. cmdoption:: -h, --help
+
+ Show the help message.
+
.. rubric:: Footnotes
diff --git a/Doc/library/locale.rst b/Doc/library/locale.rst
index 9600193..bc7f5f9 100644
--- a/Doc/library/locale.rst
+++ b/Doc/library/locale.rst
@@ -387,6 +387,14 @@ The :mod:`locale` module defines the following exception and functions:
``str(float)``, but takes the decimal point into account.
+.. function:: delocalize(string)
+
+ Converts a string into a normalized number string, following the
+ :const:`LC_NUMERIC` settings.
+
+ .. versionadded:: 3.5
+
+
.. function:: atof(string)
Converts a string to a floating point number, following the :const:`LC_NUMERIC`
diff --git a/Doc/library/logging.handlers.rst b/Doc/library/logging.handlers.rst
index 63aba1e..67403a9 100644
--- a/Doc/library/logging.handlers.rst
+++ b/Doc/library/logging.handlers.rst
@@ -436,7 +436,7 @@ sends logging output to a network socket. The base class uses a TCP socket.
.. method:: createSocket()
Tries to create a socket; on failure, uses an exponential back-off
- algorithm. On intial failure, the handler will drop the message it was
+ algorithm. On initial failure, the handler will drop the message it was
trying to send. When subsequent messages are handled by the same
instance, it will not try connecting until some time has passed. The
default parameters are such that the initial delay is one second, and if
@@ -853,7 +853,7 @@ supports sending logging messages to a Web server, using either ``GET`` or
credentials, you should also specify secure=True so that your userid and
password are not passed in cleartext across the wire.
- .. versionchanged:: 3.4.3
+ .. versionchanged:: 3.5
The *context* parameter was added.
.. method:: mapLogRecord(record)
@@ -953,13 +953,20 @@ applications where threads servicing clients need to respond as quickly as
possible, while any potentially slow operations (such as sending an email via
:class:`SMTPHandler`) are done on a separate thread.
-.. class:: QueueListener(queue, *handlers)
+.. class:: QueueListener(queue, *handlers, respect_handler_level=False)
Returns a new instance of the :class:`QueueListener` class. The instance is
initialized with the queue to send messages to and a list of handlers which
will handle entries placed on the queue. The queue can be any queue-
like object; it's passed as-is to the :meth:`dequeue` method, which needs
- to know how to get messages from it.
+ to know how to get messages from it. If ``respect_handler_level`` is ``True``,
+ a handler's level is respected (compared with the level for the message) when
+ deciding whether to pass messages to that handler; otherwise, the behaviour
+ is as in previous Python versions - to always pass each message to each
+ handler.
+
+ .. versionchanged:: 3.5
+ The ``respect_handler_levels`` argument was added.
.. method:: dequeue(block)
diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst
index 96861a7..7915c27 100644
--- a/Doc/library/logging.rst
+++ b/Doc/library/logging.rst
@@ -159,11 +159,13 @@ is the module's name in the Python package namespace.
*msg* using the string formatting operator. (Note that this means that you can
use keywords in the format string, together with a single dictionary argument.)
- There are three keyword arguments in *kwargs* which are inspected: *exc_info*
- which, if it does not evaluate as false, causes exception information to be
+ There are three keyword arguments in *kwargs* which are inspected:
+ *exc_info*, *stack_info*, and *extra*.
+
+ If *exc_info* does not evaluate as false, it causes exception information to be
added to the logging message. If an exception tuple (in the format returned by
- :func:`sys.exc_info`) is provided, it is used; otherwise, :func:`sys.exc_info`
- is called to get the exception information.
+ :func:`sys.exc_info`) or an exception instance is provided, it is used;
+ otherwise, :func:`sys.exc_info` is called to get the exception information.
The second optional keyword argument is *stack_info*, which defaults to
``False``. If true, stack information is added to the logging
@@ -220,6 +222,9 @@ is the module's name in the Python package namespace.
.. versionadded:: 3.2
The *stack_info* parameter was added.
+ .. versionchanged:: 3.5
+ The *exc_info* parameter can now accept exception instances.
+
.. method:: Logger.info(msg, *args, **kwargs)
diff --git a/Doc/library/lzma.rst b/Doc/library/lzma.rst
index b71051d..99f07dc 100644
--- a/Doc/library/lzma.rst
+++ b/Doc/library/lzma.rst
@@ -221,13 +221,32 @@ Compressing and decompressing data in memory
decompress a multi-stream input with :class:`LZMADecompressor`, you must
create a new decompressor for each stream.
- .. method:: decompress(data)
+ .. method:: decompress(data, max_length=-1)
- Decompress *data* (a :class:`bytes` object), returning a :class:`bytes`
- object containing the decompressed data for at least part of the input.
- Some of *data* may be buffered internally, for use in later calls to
- :meth:`decompress`. The returned data should be concatenated with the
- output of any previous calls to :meth:`decompress`.
+ Decompress *data* (a :term:`bytes-like object`), returning
+ uncompressed data as bytes. Some of *data* may be buffered
+ internally, for use in later calls to :meth:`decompress`. The
+ returned data should be concatenated with the output of any
+ previous calls to :meth:`decompress`.
+
+ If *max_length* is nonnegative, returns at most *max_length*
+ bytes of decompressed data. If this limit is reached and further
+ output can be produced, the :attr:`~.needs_input` attribute will
+ be set to ``False``. In this case, the next call to
+ :meth:`~.decompress` may provide *data* as ``b''`` to obtain
+ more of the output.
+
+ If all of the input data was decompressed and returned (either
+ because this was less than *max_length* bytes, or because
+ *max_length* was negative), the :attr:`~.needs_input` attribute
+ will be set to ``True``.
+
+ Attempting to decompress data after the end of stream is reached
+ raises an `EOFError`. Any data found after the end of the
+ stream is ignored and saved in the :attr:`~.unused_data` attribute.
+
+ .. versionchanged:: 3.5
+ Added the *max_length* parameter.
.. attribute:: check
@@ -245,6 +264,12 @@ Compressing and decompressing data in memory
Before the end of the stream is reached, this will be ``b""``.
+ .. attribute:: needs_input
+
+ ``False`` if the :meth:`.decompress` method can provide more
+ decompressed data before requiring new uncompressed input.
+
+ .. versionadded:: 3.5
.. function:: compress(data, format=FORMAT_XZ, check=-1, preset=None, filters=None)
diff --git a/Doc/library/math.rst b/Doc/library/math.rst
index 3c41672..eda0056 100644
--- a/Doc/library/math.rst
+++ b/Doc/library/math.rst
@@ -383,6 +383,22 @@ Constants
The mathematical constant e = 2.718281..., to available precision.
+.. data:: inf
+
+ A floating-point positive infinity. (For negative infinity, use
+ ``-math.inf``.) Equivalent to the output of ``float('inf')``.
+
+ .. versionadded:: 3.5
+
+
+.. data:: nan
+
+ A floating-point "not a number" (NaN) value. Equivalent to the output of
+ ``float('nan')``.
+
+ .. versionadded:: 3.5
+
+
.. impl-detail::
The :mod:`math` module consists mostly of thin wrappers around the platform C
diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst
index ecac5b0..4b829af 100644
--- a/Doc/library/multiprocessing.rst
+++ b/Doc/library/multiprocessing.rst
@@ -1344,6 +1344,9 @@ processes.
Note that accessing the ctypes object through the wrapper can be a lot slower
than accessing the raw ctypes object.
+ .. versionchanged:: 3.5
+ Synchronized objects support the :term:`context manager` protocol.
+
The table below compares the syntax for creating shared ctypes objects from
shared memory with the normal ctypes syntax. (In the table ``MyStruct`` is some
diff --git a/Doc/library/operator.rst b/Doc/library/operator.rst
index f9e2a3d..c01e63b 100644
--- a/Doc/library/operator.rst
+++ b/Doc/library/operator.rst
@@ -138,6 +138,14 @@ The mathematical and bitwise operations are the most numerous:
Return ``a * b``, for *a* and *b* numbers.
+.. function:: matmul(a, b)
+ __matmul__(a, b)
+
+ Return ``a @ b``.
+
+ .. versionadded:: 3.5
+
+
.. function:: neg(obj)
__neg__(obj)
@@ -391,6 +399,8 @@ Python syntax and the functions in the :mod:`operator` module.
+-----------------------+-------------------------+---------------------------------------+
| Multiplication | ``a * b`` | ``mul(a, b)`` |
+-----------------------+-------------------------+---------------------------------------+
+| Matrix Multiplication | ``a @ b`` | ``matmul(a, b)`` |
++-----------------------+-------------------------+---------------------------------------+
| Negation (Arithmetic) | ``- a`` | ``neg(a)`` |
+-----------------------+-------------------------+---------------------------------------+
| Negation (Logical) | ``not a`` | ``not_(a)`` |
@@ -499,6 +509,14 @@ will perform the update, so no subsequent assignment is necessary:
``a = imul(a, b)`` is equivalent to ``a *= b``.
+.. function:: imatmul(a, b)
+ __imatmul__(a, b)
+
+ ``a = imatmul(a, b)`` is equivalent to ``a @= b``.
+
+ .. versionadded:: 3.5
+
+
.. function:: ior(a, b)
__ior__(a, b)
diff --git a/Doc/library/os.rst b/Doc/library/os.rst
index f217a36..c4b8bac 100644
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -78,9 +78,10 @@ uses the file system encoding to perform this conversion (see
.. versionchanged:: 3.1
On some systems, conversion using the file system encoding may fail. In this
- case, Python uses the ``surrogateescape`` encoding error handler, which means
- that undecodable bytes are replaced by a Unicode character U+DCxx on
- decoding, and these are again translated to the original byte on encoding.
+ case, Python uses the :ref:`surrogateescape encoding error handler
+ <surrogateescape>`, which means that undecodable bytes are replaced by a
+ Unicode character U+DCxx on decoding, and these are again translated to the
+ original byte on encoding.
The file system encoding must guarantee to successfully decode all bytes
@@ -807,6 +808,17 @@ as internal buffering of data.
Availability: Unix.
+.. function:: get_blocking(fd)
+
+ Get the blocking mode of the file descriptor: ``False`` if the
+ :data:`O_NONBLOCK` flag is set, ``True`` if the flag is cleared.
+
+ See also :func:`set_blocking` and :meth:`socket.socket.setblocking`.
+
+ Availability: Unix.
+
+ .. versionadded:: 3.5
+
.. function:: isatty(fd)
Return ``True`` if the file descriptor *fd* is open and connected to a
@@ -1107,6 +1119,18 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
.. versionadded:: 3.3
+.. function:: set_blocking(fd, blocking)
+
+ Set the blocking mode of the specified file descriptor. Set the
+ :data:`O_NONBLOCK` flag if blocking is ``False``, clear the flag otherwise.
+
+ See also :func:`get_blocking` and :meth:`socket.socket.setblocking`.
+
+ Availability: Unix.
+
+ .. versionadded:: 3.5
+
+
.. data:: SF_NODISKIO
SF_MNOWAIT
SF_SYNC
@@ -2044,6 +2068,15 @@ features:
File type.
+ On Windows systems, the following attribute is also available:
+
+ .. attribute:: st_file_attributes
+
+ Windows file attributes: ``dwFileAttributes`` member of the
+ ``BY_HANDLE_FILE_INFORMATION`` structure returned by
+ :c:func:`GetFileInformationByHandle`. See the ``FILE_ATTRIBUTE_*``
+ constants in the :mod:`stat` module.
+
The standard module :mod:`stat` defines functions and constants that are
useful for extracting information from a :c:type:`stat` structure. (On
Windows, some items are filled with dummy values.)
@@ -2061,6 +2094,9 @@ features:
Added the :attr:`st_atime_ns`, :attr:`st_mtime_ns`, and
:attr:`st_ctime_ns` members.
+ .. versionadded:: 3.5
+ Added the :attr:`st_file_attributes` member on Windows.
+
.. function:: stat_float_times([newvalue])
@@ -2988,6 +3024,10 @@ written in Python, such as a mail server's external command delivery program.
doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
the path is properly encoded for Win32.
+ To reduce interpreter startup overhead, the Win32 :c:func:`ShellExecute`
+ function is not resolved until this function is first called. If the function
+ cannot be resolved, :exc:`NotImplementedError` will be raised.
+
Availability: Windows.
diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst
index ec1dc4f..0226ce4 100644
--- a/Doc/library/pathlib.rst
+++ b/Doc/library/pathlib.rst
@@ -628,6 +628,17 @@ call fails (for example because the path doesn't exist):
PosixPath('/home/antoine/pathlib')
+.. classmethod:: Path.home()
+
+ Return a new path object representing the user's home directory (as
+ returned by :func:`os.path.expanduser` with ``~`` construct)::
+
+ >>> Path.home()
+ PosixPath('/home/antoine')
+
+ .. versionadded:: 3.5
+
+
.. method:: Path.stat()
Return information about this path (similarly to :func:`os.stat`).
@@ -670,6 +681,18 @@ call fails (for example because the path doesn't exist):
symlink *points to* an existing file or directory.
+.. method:: Path.expanduser()
+
+ Return a new path with expanded ``~`` and ``~user`` constructs,
+ as returned by :meth:`os.path.expanduser`::
+
+ >>> p = PosixPath('~/films/Monty Python')
+ >>> p.expanduser()
+ PosixPath('/home/eric/films/Monty Python')
+
+ .. versionadded:: 3.5
+
+
.. method:: Path.glob(pattern)
Glob the given *pattern* in the directory represented by this path,
@@ -791,7 +814,7 @@ call fails (for example because the path doesn't exist):
the symbolic link's information rather than its target's.
-.. method:: Path.mkdir(mode=0o777, parents=False)
+.. method:: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
Create a new directory at this given path. If *mode* is given, it is
combined with the process' ``umask`` value to determine the file mode
@@ -805,6 +828,16 @@ call fails (for example because the path doesn't exist):
If *parents* is false (the default), a missing parent raises
:exc:`FileNotFoundError`.
+ If *exist_ok* is false (the default), an :exc:`FileExistsError` is
+ raised if the target directory already exists.
+
+ If *exist_ok* is true, :exc:`FileExistsError` exceptions will be
+ ignored (same behavior as the POSIX ``mkdir -p`` command), but only if the
+ last path component is not an existing non-directory file.
+
+ .. versionchanged:: 3.5
+ The *exist_ok* parameter was added.
+
.. method:: Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)
@@ -824,6 +857,34 @@ call fails (for example because the path doesn't exist):
if the file's uid isn't found in the system database.
+.. method:: Path.read_bytes()
+
+ Return the binary contents of the pointed-to file as a bytes object::
+
+ >>> p = Path('my_binary_file')
+ >>> p.write_bytes(b'Binary file contents')
+ 20
+ >>> p.read_bytes()
+ b'Binary file contents'
+
+ .. versionadded:: 3.5
+
+
+.. method:: Path.read_text(encoding=None, errors=None)
+
+ Return the decoded contents of the pointed-to file as a string::
+
+ >>> p = Path('my_text_file')
+ >>> p.write_text('Text file contents')
+ 18
+ >>> p.read_text()
+ 'Text file contents'
+
+ The optional parameters have the same meaning as in :func:`open`.
+
+ .. versionadded:: 3.5
+
+
.. method:: Path.rename(target)
Rename this file or directory to the given *target*. *target* can be
@@ -884,6 +945,25 @@ call fails (for example because the path doesn't exist):
Remove this directory. The directory must be empty.
+.. method:: Path.samefile(other_path)
+
+ Return whether this path points to the same file as *other_path*, which
+ can be either a Path object, or a string. The semantics are similar
+ to :func:`os.path.samefile` and :func:`os.path.samestat`.
+
+ An :exc:`OSError` can be raised if either file cannot be accessed for some
+ reason.
+
+ >>> p = Path('spam')
+ >>> q = Path('eggs')
+ >>> p.samefile(q)
+ False
+ >>> p.samefile('spam')
+ True
+
+ .. versionadded:: 3.5
+
+
.. method:: Path.symlink_to(target, target_is_directory=False)
Make this path a symbolic link to *target*. Under Windows,
@@ -917,3 +997,33 @@ call fails (for example because the path doesn't exist):
Remove this file or symbolic link. If the path points to a directory,
use :func:`Path.rmdir` instead.
+
+
+.. method:: Path.write_bytes(data)
+
+ Open the file pointed to in bytes mode, write *data* to it, and close the
+ file::
+
+ >>> p = Path('my_binary_file')
+ >>> p.write_bytes(b'Binary file contents')
+ 20
+ >>> p.read_bytes()
+ b'Binary file contents'
+
+ An existing file of the same name is overwritten.
+
+ .. versionadded:: 3.5
+
+
+.. method:: Path.write_text(data, encoding=None, errors=None)
+
+ Open the file pointed to in text mode, write *data* to it, and close the
+ file::
+
+ >>> p = Path('my_text_file')
+ >>> p.write_text('Text file contents')
+ 18
+ >>> p.read_text()
+ 'Text file contents'
+
+ .. versionadded:: 3.5
diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst
index 47356f9..4ce4d34 100644
--- a/Doc/library/pickle.rst
+++ b/Doc/library/pickle.rst
@@ -859,7 +859,7 @@ For the simplest code, use the :func:`dump` and :func:`load` functions. ::
data = {
'a': [1, 2.0, 3, 4+6j],
'b': ("character string", b"byte string"),
- 'c': set([None, True, False])
+ 'c': {None, True, False}
}
with open('data.pickle', 'wb') as f:
diff --git a/Doc/library/pkgutil.rst b/Doc/library/pkgutil.rst
index 13ea7b9..5d3295d 100644
--- a/Doc/library/pkgutil.rst
+++ b/Doc/library/pkgutil.rst
@@ -58,7 +58,7 @@ support.
.. deprecated:: 3.3
This emulation is no longer needed, as the standard import mechanism
- is now fully PEP 302 compliant and available in :mod:`importlib`
+ is now fully PEP 302 compliant and available in :mod:`importlib`.
.. class:: ImpLoader(fullname, file, filename, etc)
@@ -67,7 +67,7 @@ support.
.. deprecated:: 3.3
This emulation is no longer needed, as the standard import mechanism
- is now fully PEP 302 compliant and available in :mod:`importlib`
+ is now fully PEP 302 compliant and available in :mod:`importlib`.
.. function:: find_loader(fullname)
diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst
index 2bef120..4165591 100644
--- a/Doc/library/plistlib.rst
+++ b/Doc/library/plistlib.rst
@@ -129,7 +129,7 @@ The following functions are deprecated:
and binary) file object. Returns the unpacked root object (which usually
is a dictionary).
- This function calls :func:`load` to do the actual work, the the documentation
+ This function calls :func:`load` to do the actual work, see the documentation
of :func:`that function <load>` for an explanation of the keyword arguments.
.. note::
diff --git a/Doc/library/random.rst b/Doc/library/random.rst
index 11dd367..f8b7727 100644
--- a/Doc/library/random.rst
+++ b/Doc/library/random.rst
@@ -46,8 +46,7 @@ from sources provided by the operating system.
.. warning::
The pseudo-random generators of this module should not be used for
- security purposes. Use :func:`os.urandom` or :class:`SystemRandom` if
- you require a cryptographically secure pseudo-random number generator.
+ security purposes.
Bookkeeping functions:
diff --git a/Doc/library/re.rst b/Doc/library/re.rst
index dfc25ea..8e20496 100644
--- a/Doc/library/re.rst
+++ b/Doc/library/re.rst
@@ -521,7 +521,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.
+ for Unicode (str) patterns. This flag makes sense only with bytes patterns.
+
+ .. deprecated-removed:: 3.5 3.6
+ Deprecated the use of :const:`re.LOCALE` with string patterns or
+ :const:`re.ASCII`.
.. data:: M
@@ -622,17 +626,37 @@ form.
That way, separator components are always found at the same relative
indices within the result list.
- Note that *split* will never split a string on an empty pattern match.
- For example:
+ .. note::
+
+ :func:`split` doesn't currently split a string on an empty pattern match.
+ For example:
+
+ >>> re.split('x*', 'axbc')
+ ['a', 'bc']
- >>> re.split('x*', 'foo')
- ['foo']
- >>> re.split("(?m)^$", "foo\n\nbar\n")
- ['foo\n\nbar\n']
+ Even though ``'x*'`` also matches 0 'x' before 'a', between 'b' and 'c',
+ and after 'c', currently these matches are ignored. The correct behavior
+ (i.e. splitting on empty matches too and returning ``['', 'a', 'b', 'c',
+ '']``) will be implemented in future versions of Python, but since this
+ is a backward incompatible change, a :exc:`FutureWarning` will be raised
+ in the meanwhile.
+
+ Patterns that can only match empty strings currently never split the
+ string. Since this doesn't match the expected behavior, a
+ :exc:`ValueError` will be raised starting from Python 3.5::
+
+ >>> re.split("^$", "foo\n\nbar\n", flags=re.M)
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ ...
+ ValueError: split() requires a non-empty pattern match.
.. versionchanged:: 3.1
Added the optional flags argument.
+ .. versionchanged:: 3.5
+ Splitting on a pattern that could match an empty string now raises
+ a warning. Patterns that can only match empty strings are now rejected.
.. function:: findall(pattern, string, flags=0)
@@ -702,6 +726,9 @@ form.
.. versionchanged:: 3.1
Added the optional flags argument.
+ .. versionchanged:: 3.5
+ Unmatched groups are replaced with an empty string.
+
.. function:: subn(pattern, repl, string, count=0, flags=0)
@@ -711,6 +738,9 @@ form.
.. versionchanged:: 3.1
Added the optional flags argument.
+ .. versionchanged:: 3.5
+ Unmatched groups are replaced with an empty string.
+
.. function:: escape(string)
@@ -727,13 +757,36 @@ form.
Clear the regular expression cache.
-.. exception:: error
+.. exception:: error(msg, pattern=None, pos=None)
Exception raised when a string passed to one of the functions here is not a
valid regular expression (for example, it might contain unmatched parentheses)
or when some other error occurs during compilation or matching. It is never an
- error if a string contains no match for a pattern.
+ error if a string contains no match for a pattern. The error instance has
+ the following additional attributes:
+
+ .. attribute:: msg
+
+ The unformatted error message.
+
+ .. attribute:: pattern
+
+ The regular expression pattern.
+
+ .. attribute:: pos
+
+ The index of *pattern* where compilation failed.
+
+ .. attribute:: lineno
+
+ The line corresponding to *pos*.
+
+ .. attribute:: colno
+
+ The column corresponding to *pos*.
+ .. versionchanged:: 3.5
+ Added additional attributes.
.. _re-objects:
@@ -886,6 +939,8 @@ Match objects support the following methods and attributes:
(``\g<1>``, ``\g<name>``) are replaced by the contents of the
corresponding group.
+ .. versionchanged:: 3.5
+ Unmatched groups are replaced with an empty string.
.. method:: match.group([group1, ...])
diff --git a/Doc/library/readline.rst b/Doc/library/readline.rst
index 692310b..3864f0d 100644
--- a/Doc/library/readline.rst
+++ b/Doc/library/readline.rst
@@ -59,6 +59,14 @@ The :mod:`readline` module defines the following functions:
Save a readline history file. The default filename is :file:`~/.history`.
+.. function:: append_history_file(nelements[, filename])
+
+ Append the last *nelements* of history to a file. The default filename is
+ :file:`~/.history`. The file must already exist.
+
+ .. versionadded:: 3.5
+
+
.. function:: clear_history()
Clear the current history. (Note: this function is not available if the
@@ -209,6 +217,26 @@ from the user's :envvar:`PYTHONSTARTUP` file. ::
This code is actually automatically run when Python is run in
:ref:`interactive mode <tut-interactive>` (see :ref:`rlcompleter-config`).
+The following example achieves the same goal but supports concurrent interactive
+sessions, by only appending the new history. ::
+
+ import atexit
+ import os
+ import realine
+ histfile = os.path.join(os.path.expanduser("~"), ".python_history")
+
+ try:
+ readline.read_history_file(histfile)
+ h_len = readline.get_history_length()
+ except FileNotFoundError:
+ open(histfile, 'wb').close()
+ h_len = 0
+
+ def save(prev_h_len, histfile):
+ new_h_len = readline.get_history_length()
+ readline.append_history_file(new_h_len - prev_h_len, histfile)
+ atexit.register(save, h_len, histfile)
+
The following example extends the :class:`code.InteractiveConsole` class to
support history save/restore. ::
@@ -234,4 +262,3 @@ support history save/restore. ::
def save_history(self, histfile):
readline.write_history_file(histfile)
-
diff --git a/Doc/library/resource.rst b/Doc/library/resource.rst
index f8112cc..7c0e4ca 100644
--- a/Doc/library/resource.rst
+++ b/Doc/library/resource.rst
@@ -45,7 +45,7 @@ this module for those platforms.
.. data:: RLIM_INFINITY
- Constant used to represent the the limit for an unlimited resource.
+ Constant used to represent the limit for an unlimited resource.
.. function:: getrlimit(resource)
diff --git a/Doc/library/select.rst b/Doc/library/select.rst
index 734e426..5334af8 100644
--- a/Doc/library/select.rst
+++ b/Doc/library/select.rst
@@ -207,7 +207,7 @@ object.
.. warning::
Registering a file descriptor that's already registered is not an
- error, but the result is undefined. The appropiate action is to
+ error, but the result is undefined. The appropriate action is to
unregister or modify it first. This is an important difference
compared with :c:func:`poll`.
diff --git a/Doc/library/selectors.rst b/Doc/library/selectors.rst
index 98377c8..8bd9e1c 100644
--- a/Doc/library/selectors.rst
+++ b/Doc/library/selectors.rst
@@ -45,6 +45,7 @@ Classes hierarchy::
+-- SelectSelector
+-- PollSelector
+-- EpollSelector
+ +-- DevpollSelector
+-- KqueueSelector
@@ -207,6 +208,16 @@ below:
This returns the file descriptor used by the underlying
:func:`select.epoll` object.
+.. class:: DevpollSelector()
+
+ :func:`select.devpoll`-based selector.
+
+ .. method:: fileno()
+
+ This returns the file descriptor used by the underlying
+ :func:`select.devpoll` object.
+
+ .. versionadded:: 3.5
.. class:: KqueueSelector()
diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst
index d96fd71..82974ad 100644
--- a/Doc/library/shutil.rst
+++ b/Doc/library/shutil.rst
@@ -191,7 +191,8 @@ Directory and files operations
match one of the glob-style *patterns* provided. See the example below.
-.. function:: copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)
+.. function:: copytree(src, dst, symlinks=False, ignore=None, \
+ copy_function=copy2, ignore_dangling_symlinks=False)
Recursively copy an entire directory tree rooted at *src*, returning the
destination directory. The destination
@@ -282,7 +283,7 @@ Directory and files operations
.. versionadded:: 3.3
-.. function:: move(src, dst)
+.. function:: move(src, dst, copy_function=copy2)
Recursively move a file or directory (*src*) to another location (*dst*)
and return the destination.
@@ -295,15 +296,26 @@ Directory and files operations
:func:`os.rename` semantics.
If the destination is on the current filesystem, then :func:`os.rename` is
- used. Otherwise, *src* is copied (using :func:`shutil.copy2`) to *dst* and
- then removed. In case of symlinks, a new symlink pointing to the target of
- *src* will be created in or as *dst* and *src* will be removed.
+ used. Otherwise, *src* is copied to *dst* using *copy_function* and then
+ removed. In case of symlinks, a new symlink pointing to the target of *src*
+ will be created in or as *dst* and *src* will be removed.
+
+ If *copy_function* is given, it must be a callable that takes two arguments
+ *src* and *dst*, and will be used to copy *src* to *dest* if
+ :func:`os.rename` cannot be used. If the source is a directory,
+ :func:`copytree` is called, passing it the :func:`copy_function`. The
+ default *copy_function* is :func:`copy2`. Using :func:`copy` as the
+ *copy_function* allows the move to succeed when it is not possible to also
+ copy the metadata, at the expense of not copying any of the metadata.
.. versionchanged:: 3.3
Added explicit symlink handling for foreign filesystems, thus adapting
it to the behavior of GNU's :program:`mv`.
Now returns *dst*.
+ .. versionchanged:: 3.5
+ Added the *copy_function* keyword argument.
+
.. function:: disk_usage(path)
Return disk usage statistics about the given path as a :term:`named tuple`
@@ -341,7 +353,7 @@ Directory and files operations
On Windows, the current directory is always prepended to the *path* whether
or not you use the default or provide your own, which is the behavior the
- command shell uses when finding executables. Additionaly, when finding the
+ command shell uses when finding executables. Additionally, when finding the
*cmd* in the *path*, the ``PATHEXT`` environment variable is checked. For
example, if you call ``shutil.which("python")``, :func:`which` will search
``PATHEXT`` to know that it should look for ``python.exe`` within the *path*
@@ -421,6 +433,26 @@ Another example that uses the *ignore* argument to add a logging call::
copytree(source, destination, ignore=_logpath)
+.. _shutil-rmtree-example:
+
+rmtree example
+~~~~~~~~~~~~~~
+
+This example shows how to remove a directory tree on Windows where some
+of the files have their read-only bit set. It uses the onerror callback
+to clear the readonly bit and reattempt the remove. Any subsequent failure
+will propagate. ::
+
+ import os, stat
+ import shutil
+
+ def remove_readonly(func, path, _):
+ "Clear the readonly bit and reattempt the removal"
+ os.chmod(path, stat.S_IWRITE)
+ func(path)
+
+ shutil.rmtree(directory, onerror=remove_readonly)
+
.. _archiving-operations:
Archiving operations
@@ -437,7 +469,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
*base_name* is the name of the file to create, including the path, minus
any format-specific extension. *format* is the archive format: one of
- "zip", "tar", "bztar" (if the :mod:`bz2` module is available) or "gztar".
+ "zip", "tar", "bztar" (if the :mod:`bz2` module is available), "xztar"
+ (if the :mod:`lzma` module is available) or "gztar".
*root_dir* is a directory that will be the root directory of the
archive; for example, we typically chdir into *root_dir* before creating the
@@ -460,6 +493,9 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
The *verbose* argument is unused and deprecated.
+ .. versionchanged:: 3.5
+ Added support for the *xztar* format.
+
.. function:: get_archive_formats()
@@ -470,6 +506,7 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
- *gztar*: gzip'ed tar-file
- *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.)
+ - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available.)
- *tar*: uncompressed tar file
- *zip*: ZIP file
@@ -545,6 +582,7 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
- *gztar*: gzip'ed tar-file
- *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.)
+ - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available.)
- *tar*: uncompressed tar file
- *zip*: ZIP file
diff --git a/Doc/library/signal.rst b/Doc/library/signal.rst
index 84e2836..ed616b2 100644
--- a/Doc/library/signal.rst
+++ b/Doc/library/signal.rst
@@ -65,6 +65,16 @@ Besides, only the main thread is allowed to set a new signal handler.
Module contents
---------------
+.. versionchanged:: 3.5
+ signal (SIG*), handler (:const:`SIG_DFL`, :const:`SIG_IGN`) and sigmask
+ (:const:`SIG_BLOCK`, :const:`SIG_UNBLOCK`, :const:`SIG_SETMASK`)
+ related constants listed below were turned into
+ :class:`enums <enum.IntEnum>`.
+ :func:`getsignal`, :func:`pthread_sigmask`, :func:`sigpending` and
+ :func:`sigwait` functions return human-readable
+ :class:`enums <enum.IntEnum>`.
+
+
The variables defined in the :mod:`signal` module are:
@@ -308,6 +318,9 @@ The :mod:`signal` module defines the following functions:
attempting to call it from other threads will cause a :exc:`ValueError`
exception to be raised.
+ .. versionchanged:: 3.5
+ On Windows, the function now also supports socket handles.
+
.. function:: siginterrupt(signalnum, flag)
diff --git a/Doc/library/site.rst b/Doc/library/site.rst
index e57b8cc..43daf79 100644
--- a/Doc/library/site.rst
+++ b/Doc/library/site.rst
@@ -26,24 +26,23 @@ additions, call the :func:`site.main` function.
:option:`-S`.
.. index::
- pair: site-python; directory
pair: site-packages; directory
It starts by constructing up to four directories from a head and a tail part.
For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads
are skipped. For the tail part, it uses the empty string and then
:file:`lib/site-packages` (on Windows) or
-:file:`lib/python{X.Y}/site-packages` and then :file:`lib/site-python` (on
-Unix and Macintosh). For each of the distinct head-tail combinations, it sees
-if it refers to an existing directory, and if so, adds it to ``sys.path`` and
-also inspects the newly added path for configuration files.
+:file:`lib/python{X.Y}/site-packages` (on Unix and Macintosh). For each
+of the distinct head-tail combinations, it sees if it refers to an existing
+directory, and if so, adds it to ``sys.path`` and also inspects the newly
+added path for configuration files.
-.. deprecated:: 3.4
- Support for the "site-python" directory will be removed in 3.5.
+.. versionchanged:: 3.5
+ Support for the "site-python" directory has been removed.
If a file named "pyvenv.cfg" exists one directory above sys.executable,
sys.prefix and sys.exec_prefix are set to that directory and
-it is also checked for site-packages and site-python (sys.base_prefix and
+it is also checked for site-packages (sys.base_prefix and
sys.base_exec_prefix will always be the "real" prefixes of the Python
installation). If "pyvenv.cfg" (a bootstrap configuration file) contains
the key "include-system-site-packages" set to anything other than "false"
@@ -184,7 +183,7 @@ Module contents
unless the Python interpreter was started with the :option:`-S` flag.
.. versionchanged:: 3.3
- This function used to be called unconditionnally.
+ This function used to be called unconditionally.
.. function:: addsitedir(sitedir, known_paths=None)
@@ -195,8 +194,7 @@ Module contents
.. function:: getsitepackages()
- Return a list containing all global site-packages directories (and possibly
- site-python).
+ Return a list containing all global site-packages directories.
.. versionadded:: 3.2
diff --git a/Doc/library/smtpd.rst b/Doc/library/smtpd.rst
index 3ebed06..3e0c6fb 100644
--- a/Doc/library/smtpd.rst
+++ b/Doc/library/smtpd.rst
@@ -20,7 +20,8 @@ specific mail-sending strategies.
Additionally the SMTPChannel may be extended to implement very specific
interaction behaviour with SMTP clients.
-The code supports :RFC:`5321`, plus the :rfc:`1870` SIZE extension.
+The code supports :RFC:`5321`, plus the :rfc:`1870` SIZE and :rfc:`6531`
+SMTPUTF8 extensions.
SMTPServer Objects
@@ -28,7 +29,7 @@ SMTPServer Objects
.. class:: SMTPServer(localaddr, remoteaddr, data_size_limit=33554432,\
- map=None)
+ map=None, enable_SMTPUTF8=False, decode_data=True)
Create a new :class:`SMTPServer` object, which binds to local address
*localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. It
@@ -39,18 +40,47 @@ SMTPServer Objects
accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no
limit.
+ *enable_SMTPUTF8* determins whether the ``SMTPUTF8`` extension (as defined
+ in :RFC:`6531`) should be enabled. The default is ``False``. If
+ *enable_SMTPUTF* is set to ``True``, the :meth:`process_smtputf8_message`
+ method must be defined. A :exc:`ValueError` is raised if both
+ *enable_SMTPUTF8* and *decode_data* are 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`.
+
.. method:: process_message(peer, mailfrom, rcpttos, data)
- Raise :exc:`NotImplementedError` exception. Override this in subclasses to
+ Raise a :exc:`NotImplementedError` exception. Override this in subclasses to
do something useful with this message. Whatever was passed in the
constructor as *remoteaddr* will be available as the :attr:`_remoteaddr`
attribute. *peer* is the remote host's address, *mailfrom* is the envelope
originator, *rcpttos* are the envelope recipients and *data* is a string
- containing the contents of the e-mail (which should be in :rfc:`2822`
+ containing the contents of the e-mail (which should be in :rfc:`5321`
format).
+ If the *decode_data* constructor keyword is set to ``True``, the *data*
+ argument will be a unicode string. If it is set to ``False``, it
+ will be a bytes object.
+
+ Return ``None`` to request a normal ``250 Ok`` response; otherwise
+ return the desired response string in :RFC:`5321` format.
+
+ .. method:: process_smtputf8_message(peer, mailfrom, rcpttos, data)
+
+ Raise a :exc:`NotImplementedError` exception. Override this in
+ subclasses to do something useful with messages when *enable_SMTPUTF8*
+ has been set to ``True`` and the SMTP client requested ``SMTPUTF8``,
+ since this method is called rather than :meth:`process_message` when the
+ client actively requests ``SMTPUTF8``. The *data* argument will always
+ be a bytes object, and any non-``None`` return value should conform to
+ :rfc:`6531`; otherwise, the API is the same as for
+ :meth:`process_message`.
+
.. attribute:: channel_class
Override this in subclasses to use a custom :class:`SMTPChannel` for
@@ -59,6 +89,13 @@ SMTPServer Objects
.. versionchanged:: 3.4
The *map* argument was added.
+ .. versionchanged:: 3.5
+ *localaddr* and *remoteaddr* may now contain IPv6 addresses.
+
+ .. versionadded:: 3.5
+ the *decode_data* and *enable_SMTPUTF8* constructor arguments, and the
+ :meth:`process_smtputf8_message` method.
+
DebuggingServer Objects
-----------------------
@@ -97,7 +134,7 @@ SMTPChannel Objects
-------------------
.. class:: SMTPChannel(server, conn, addr, data_size_limit=33554432,\
- map=None))
+ map=None, enable_SMTPUTF8=False, decode_data=True)
Create a new :class:`SMTPChannel` object which manages the communication
between the server and a single SMTP client.
@@ -108,11 +145,24 @@ SMTPChannel Objects
accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no
limit.
+ *enable_SMTPUTF8* determins 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.
+
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`.
+
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 :class:`SMTPChannel` has the following instance variables:
.. attribute:: smtp_server
diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst
index 8e1bfb5..74de77b 100644
--- a/Doc/library/smtplib.rst
+++ b/Doc/library/smtplib.rst
@@ -240,8 +240,7 @@ An :class:`SMTP` instance has the following methods:
the server is stored as the :attr:`ehlo_resp` attribute, :attr:`does_esmtp`
is set to true or false depending on whether the server supports ESMTP, and
:attr:`esmtp_features` will be a dictionary containing the names of the
- SMTP service extensions this server supports, and their
- parameters (if any).
+ SMTP service extensions this server supports, and their parameters (if any).
Unless you wish to use :meth:`has_extn` before sending mail, it should not be
necessary to call this method explicitly. It will be implicitly called by
@@ -291,6 +290,42 @@ An :class:`SMTP` instance has the following methods:
:exc:`SMTPException`
No suitable authentication method was found.
+ Each of the authentication methods supported by :mod:`smtplib` are tried in
+ turn if they are advertised as supported by the server (see :meth:`auth`
+ for a list of supported authentication methods).
+
+
+.. method:: SMTP.auth(mechanism, authobject)
+
+ Issue an ``SMTP`` ``AUTH`` command for the specified authentication
+ *mechanism*, and handle the challenge response via *authobject*.
+
+ *mechanism* specifies which authentication mechanism is to
+ be used as argument to the ``AUTH`` command; the valid values are
+ those listed in the ``auth`` element of :attr:`esmtp_features`.
+
+ *authobject* must be a callable object taking a single argument:
+
+ data = authobject(challenge)
+
+ It will be called to process the server's challenge response; the
+ *challenge* argument it is passed will be a ``bytes``. It should return
+ ``bytes`` *data* that will be base64 encoded and sent to the server.
+
+ The ``SMTP`` class provides ``authobjects`` for the ``CRAM-MD5``, ``PLAIN``,
+ and ``LOGIN`` mechanisms; they are named ``SMTP.auth_cram_md5``,
+ ``SMTP.auth_plain``, and ``SMTP.auth_login`` respectively. They all require
+ that the ``user`` and ``password`` properties of the ``SMTP`` instance are
+ set to appropriate values.
+
+ User code does not normally need to call ``auth`` directly, but can instead
+ call the :meth:`login` method, which will try each of the above mechanisms in
+ turn, in the order listed. ``auth`` is exposed to facilitate the
+ implementation of authentication methods not (or not yet) supported directly
+ by :mod:`smtplib`.
+
+ .. versionadded:: 3.5
+
.. method:: SMTP.starttls(keyfile=None, certfile=None, context=None)
diff --git a/Doc/library/sndhdr.rst b/Doc/library/sndhdr.rst
index f36df68..f8b5d8b 100644
--- a/Doc/library/sndhdr.rst
+++ b/Doc/library/sndhdr.rst
@@ -16,8 +16,9 @@
The :mod:`sndhdr` provides utility functions which attempt to determine the type
of sound data which is in a file. When these functions are able to determine
-what type of sound data is stored in a file, they return a tuple ``(type,
-sampling_rate, channels, frames, bits_per_sample)``. The value for *type*
+what type of sound data is stored in a file, they return a
+:func:`~collections.namedtuple`, containing five attributes: (``filetype``,
+``framerate``, ``nchannels``, ``nframes``, ``sampwidth``). 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'``. The *sampling_rate* will be either the actual
@@ -31,13 +32,19 @@ be the sample size in bits or ``'A'`` for A-LAW or ``'U'`` for u-LAW.
.. function:: what(filename)
Determines the type of sound data stored in the file *filename* using
- :func:`whathdr`. If it succeeds, returns a tuple as described above, otherwise
+ :func:`whathdr`. If it succeeds, returns a namedtuple as described above, otherwise
``None`` is returned.
+ .. versionchanged:: 3.5
+ Result changed from a tuple to a namedtuple.
+
.. function:: whathdr(filename)
Determines the type of sound data stored in a file based on the file header.
- The name of the file is given by *filename*. This function returns a tuple as
+ The name of the file is given by *filename*. This function returns a namedtuple as
described above on success, or ``None``.
+ .. versionchanged:: 3.5
+ Result changed from a tuple to a namedtuple.
+
diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst
index b703e9e..e330f0a 100644
--- a/Doc/library/socket.rst
+++ b/Doc/library/socket.rst
@@ -352,7 +352,6 @@ The following functions all create :ref:`socket objects <socket-objects>`.
type, and protocol number. Address family, socket type, and protocol number are
as for the :func:`.socket` function above. The default family is :const:`AF_UNIX`
if defined on the platform; otherwise, the default is :const:`AF_INET`.
- Availability: Unix.
The newly created sockets are :ref:`non-inheritable <fd_inheritance>`.
@@ -363,6 +362,9 @@ The following functions all create :ref:`socket objects <socket-objects>`.
.. versionchanged:: 3.4
The returned sockets are now non-inheritable.
+ .. versionchanged:: 3.5
+ Windows support added.
+
.. function:: create_connection(address[, timeout[, source_address]])
@@ -908,12 +910,15 @@ 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.
-.. method:: socket.listen(backlog)
+.. method:: socket.listen([backlog])
- Listen for connections made to the socket. The *backlog* argument specifies the
- maximum number of queued connections and should be at least 0; the maximum value
- is system-dependent (usually 5), the minimum value is forced to 0.
+ Enable a server to accept connections. If *backlog* is specified, it must
+ be at least 0 (if it is lower, it is set to 0); it specifies the number of
+ unaccepted connections that the system will allow before refusing new
+ connections. If not specified, a default reasonable value is chosen.
+ .. versionchanged:: 3.5
+ The *backlog* parameter is now optional.
.. method:: socket.makefile(mode='r', buffering=None, *, encoding=None, \
errors=None, newline=None)
@@ -1148,6 +1153,21 @@ to sockets.
.. versionadded:: 3.3
+.. method:: socket.sendfile(file, offset=0, count=None)
+
+ Send a file until EOF is reached by using high-performance
+ :mod:`os.sendfile` and return the total number of bytes which were sent.
+ *file* must be a regular file object opened in binary mode. If
+ :mod:`os.sendfile` is not available (e.g. Windows) or *file* is not a
+ regular file :meth:`send` will be used instead. *offset* tells from where to
+ start reading the file. If specified, *count* is the total number of bytes
+ to transmit as opposed to sending the file until EOF is reached. File
+ position is updated on return or also in case of error in which case
+ :meth:`file.tell() <io.IOBase.tell>` can be used to figure out the number of
+ bytes which were sent. The socket must be of :const:`SOCK_STREAM` type. Non-
+ blocking sockets are not supported.
+
+ .. versionadded:: 3.5
.. method:: socket.set_inheritable(inheritable)
@@ -1447,7 +1467,7 @@ After binding (:const:`CAN_RAW`) or connecting (:const:`CAN_BCM`) the socket, yo
can use the :meth:`socket.send`, and the :meth:`socket.recv` operations (and
their counterparts) on the socket object as usual.
-This example might require special priviledge::
+This example might require special privileges::
import socket
import struct
diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst
index 1ec4438..9db36d5 100644
--- a/Doc/library/socketserver.rst
+++ b/Doc/library/socketserver.rst
@@ -113,7 +113,7 @@ the request handler class :meth:`handle` method.
Another approach to handling multiple simultaneous requests in an environment
that supports neither threads nor :func:`~os.fork` (or where these are too
expensive or inappropriate for the service) is to maintain an explicit table of
-partially finished requests and to use :func:`~select.select` to decide which
+partially finished requests and to use :mod:`selectors` to decide which
request to work on next (or whether to handle a new incoming request). This is
particularly important for stream services where each client can potentially be
connected for a long time (if threads or subprocesses cannot be used). See
@@ -136,7 +136,7 @@ Server Objects
.. method:: BaseServer.fileno()
Return an integer file descriptor for the socket on which the server is
- listening. This function is most commonly passed to :func:`select.select`, to
+ listening. This function is most commonly passed to :mod:`selectors`, to
allow monitoring multiple servers in the same process.
diff --git a/Doc/library/ssl.rst b/Doc/library/ssl.rst
index 015e0db..254fc1f 100644
--- a/Doc/library/ssl.rst
+++ b/Doc/library/ssl.rst
@@ -327,6 +327,8 @@ Random generation
See http://egd.sourceforge.net/ or http://prngd.sourceforge.net/ for sources
of entropy-gathering daemons.
+ Availability: not available with LibreSSL.
+
.. function:: RAND_add(bytes, entropy)
Mixes the given *bytes* into the SSL pseudo-random number generator. The
@@ -342,10 +344,9 @@ Certificate handling
Verify that *cert* (in decoded format as returned by
:meth:`SSLSocket.getpeercert`) matches the given *hostname*. The rules
applied are those for checking the identity of HTTPS servers as outlined
- in :rfc:`2818` and :rfc:`6125`, except that IP addresses are not currently
- supported. In addition to HTTPS, this function should be suitable for
- checking the identity of servers in various SSL-based protocols such as
- FTPS, IMAPS, POPS and others.
+ in :rfc:`2818` and :rfc:`6125`. In addition to HTTPS, this function
+ should be suitable for checking the identity of servers in various
+ SSL-based protocols such as FTPS, IMAPS, POPS and others.
:exc:`CertificateError` is raised on failure. On success, the function
returns nothing::
@@ -367,22 +368,38 @@ Certificate handling
IDN A-labels such as ``www*.xn--pthon-kva.org`` are still supported,
but ``x*.python.org`` no longer matches ``xn--tda.python.org``.
-.. function:: cert_time_to_seconds(timestring)
+ .. versionchanged:: 3.5
+ Matching of IP addresses, when present in the subjectAltName field
+ of the certificate, is now supported.
+
+.. function:: cert_time_to_seconds(cert_time)
+
+ Return the time in seconds since the Epoch, given the ``cert_time``
+ string representing the "notBefore" or "notAfter" date from a
+ certificate in ``"%b %d %H:%M:%S %Y %Z"`` strptime format (C
+ locale).
+
+ Here's an example:
+
+ .. doctest:: newcontext
- Returns a floating-point value containing a normal seconds-after-the-epoch
- time value, given the time-string representing the "notBefore" or "notAfter"
- date from a certificate.
+ >>> import ssl
+ >>> timestamp = ssl.cert_time_to_seconds("Jan 5 09:34:43 2018 GMT")
+ >>> timestamp
+ 1515144883
+ >>> from datetime import datetime
+ >>> print(datetime.utcfromtimestamp(timestamp))
+ 2018-01-05 09:34:43
- Here's an example::
+ "notBefore" or "notAfter" dates must use GMT (:rfc:`5280`).
- >>> import ssl
- >>> ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT")
- 1178694000.0
- >>> import time
- >>> time.ctime(ssl.cert_time_to_seconds("May 9 00:00:00 2007 GMT"))
- 'Wed May 9 00:00:00 2007'
+ .. versionchanged:: 3.5
+ Interpret the input time as a time in UTC as specified by 'GMT'
+ timezone in the input string. Local timezone was used
+ previously. Return an integer (no fractions of a second in the
+ input format)
-.. function:: get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None)
+.. function:: get_server_certificate(addr, ssl_version=PROTOCOL_SSLv23, ca_certs=None)
Given the address ``addr`` of an SSL-protected server, as a (*hostname*,
*port-number*) pair, fetches the server's certificate, and returns it as a
@@ -396,6 +413,10 @@ Certificate handling
.. versionchanged:: 3.3
This function is now IPv6-compatible.
+ .. versionchanged:: 3.5
+ The default *ssl_version* is changed from :data:`PROTOCOL_SSLv3` to
+ :data:`PROTOCOL_SSLv23` for maximum compatibility with modern servers.
+
.. function:: DER_cert_to_PEM_cert(DER_cert_bytes)
Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded
@@ -655,6 +676,13 @@ Constants
.. versionadded:: 3.3
+.. data:: HAS_ALPN
+
+ Whether the OpenSSL library has built-in support for the *Application-Layer
+ Protocol Negotiation* TLS extension as described in :rfc:`7301`.
+
+ .. versionadded:: 3.5
+
.. data:: HAS_ECDH
Whether the OpenSSL library has built-in support for Elliptic Curve-based
@@ -772,6 +800,8 @@ SSL Sockets
(but passing a non-zero ``flags`` argument is not allowed)
- :meth:`~socket.socket.send()`, :meth:`~socket.socket.sendall()` (with
the same limitation)
+ - :meth:`~socket.socket.sendfile()` (but :mod:`os.sendfile` will be used
+ for plain-text sockets only, else :meth:`~socket.socket.send()` will be used)
- :meth:`~socket.socket.shutdown()`
However, since the SSL (and TLS) protocol has its own framing atop
@@ -782,6 +812,10 @@ SSL Sockets
Usually, :class:`SSLSocket` are not created directly, but using the
:func:`wrap_socket` function or the :meth:`SSLContext.wrap_socket` method.
+ .. versionchanged:: 3.5
+ The :meth:`sendfile` method was added.
+
+
SSL sockets also have the following additional methods and attributes:
.. method:: SSLSocket.read(len=0, buffer=None)
@@ -901,6 +935,17 @@ SSL sockets also have the following additional methods and attributes:
version of the SSL protocol that defines its use, and the number of secret
bits being used. If no connection has been established, returns ``None``.
+.. method:: SSLSocket.shared_ciphers()
+
+ Return the list of ciphers shared by the client during the handshake. Each
+ entry of the returned list is a three-value tuple containing the name of the
+ cipher, the version of the SSL protocol that defines its use, and the number
+ of secret bits the cipher uses. :meth:`~SSLSocket.shared_ciphers` returns
+ ``None`` if no connection has been established or the socket is a client
+ socket.
+
+ .. versionadded:: 3.5
+
.. method:: SSLSocket.compression()
Return the compression algorithm being used as a string, or ``None``
@@ -924,12 +969,22 @@ SSL sockets also have the following additional methods and attributes:
.. versionadded:: 3.3
+.. method:: SSLSocket.selected_alpn_protocol()
+
+ Return the protocol that was selected during the TLS handshake. If
+ :meth:`SSLContext.set_alpn_protocols` was not called, if the other party does
+ not support ALPN, if this socket does not support any of the client's
+ proposed protocols, or if the handshake has not happened yet, ``None`` is
+ returned.
+
+ .. versionadded:: 3.5
+
.. method:: SSLSocket.selected_npn_protocol()
- Returns the protocol that was selected during the TLS/SSL handshake. If
- :meth:`SSLContext.set_npn_protocols` was not called, or if the other party
- does not support NPN, or if the handshake has not yet happened, this will
- return ``None``.
+ Return the higher-level protocol that was selected during the TLS/SSL
+ handshake. If :meth:`SSLContext.set_npn_protocols` was not called, or
+ if the other party does not support NPN, or if the handshake has not yet
+ happened, this will return ``None``.
.. versionadded:: 3.3
@@ -941,6 +996,16 @@ SSL sockets also have the following additional methods and attributes:
returned socket should always be used for further communication with the
other side of the connection, rather than the original socket.
+.. method:: SSLSocket.version()
+
+ Return the actual SSL protocol version negotiated by the connection
+ as a string, or ``None`` is no secure connection is established.
+ As of this writing, possible return values include ``"SSLv2"``,
+ ``"SSLv3"``, ``"TLSv1"``, ``"TLSv1.1"`` and ``"TLSv1.2"``.
+ Recent OpenSSL versions may define more return values.
+
+ .. versionadded:: 3.5
+
.. method:: SSLSocket.pending()
Returns the number of already decrypted bytes available for read, pending on
@@ -1115,6 +1180,20 @@ to speed up repeated connections from the same clients.
when connected, the :meth:`SSLSocket.cipher` method of SSL sockets will
give the currently selected cipher.
+.. method:: SSLContext.set_alpn_protocols(protocols)
+
+ Specify which protocols the socket should advertise during the SSL/TLS
+ handshake. It should be a list of ASCII strings, like ``['http/1.1',
+ 'spdy/2']``, ordered by preference. The selection of a protocol will happen
+ during the handshake, and will play out according to :rfc:`7301`. After a
+ successful handshake, the :meth:`SSLSocket.selected_alpn_protocol` method will
+ return the agreed-upon protocol.
+
+ This method will raise :exc:`NotImplementedError` if :data:`HAS_ALPN` is
+ False.
+
+ .. versionadded:: 3.5
+
.. method:: SSLContext.set_npn_protocols(protocols)
Specify which protocols the socket should advertise during the SSL/TLS
@@ -1155,7 +1234,7 @@ to speed up repeated connections from the same clients.
Due to the early negotiation phase of the TLS connection, only limited
methods and attributes are usable like
- :meth:`SSLSocket.selected_npn_protocol` and :attr:`SSLSocket.context`.
+ :meth:`SSLSocket.selected_alpn_protocol` and :attr:`SSLSocket.context`.
:meth:`SSLSocket.getpeercert`, :meth:`SSLSocket.getpeercert`,
:meth:`SSLSocket.cipher` and :meth:`SSLSocket.compress` methods require that
the TLS connection has progressed beyond the TLS Client Hello and therefore
@@ -1231,10 +1310,20 @@ 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.
- .. versionchanged:: 3.4.3
+ .. versionchanged:: 3.5
Always allow a server_hostname to be passed, even if OpenSSL does not
have SNI.
+.. method:: SSLContext.wrap_bio(incoming, outgoing, server_side=False, \
+ server_hostname=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`.
+
.. method:: SSLContext.session_stats()
Get statistics about the SSL sessions created or managed by this context.
@@ -1619,7 +1708,7 @@ are finished with the client (or the client is finished with you)::
And go back to listening for new client connections (of course, a real server
would probably handle each client connection in a separate thread, or put
-the sockets in non-blocking mode and use an event loop).
+the sockets in :ref:`non-blocking mode <ssl-nonblocking>` and use an event loop).
.. _ssl-nonblocking:
@@ -1641,6 +1730,12 @@ thus several things you need to be aware of:
socket first, and attempts to *read* from the SSL socket may require
a prior *write* to the underlying socket.
+ .. versionchanged:: 3.5
+
+ In earlier Python versions, the :meth:`!SSLSocket.send` method
+ returned zero instead of raising :exc:`SSLWantWriteError` or
+ :exc:`SSLWantReadError`.
+
- Calling :func:`~select.select` tells you that the OS-level socket can be
read from (or written to), but it does not imply that there is sufficient
data at the upper SSL layer. For example, only part of an SSL frame might
@@ -1673,13 +1768,143 @@ thus several things you need to be aware of:
.. seealso::
- The :mod:`asyncio` module supports non-blocking SSL sockets and provides a
+ The :mod:`asyncio` module supports :ref:`non-blocking SSL sockets
+ <ssl-nonblocking>` and provides a
higher level API. It polls for events using the :mod:`selectors` module and
handles :exc:`SSLWantWriteError`, :exc:`SSLWantReadError` and
:exc:`BlockingIOError` exceptions. It runs the SSL handshake asynchronously
as well.
+Memory BIO Support
+------------------
+
+.. versionadded:: 3.5
+
+Ever since the SSL module was introduced in Python 2.6, the :class:`SSLSocket`
+class has provided two related but distinct areas of functionality:
+
+- SSL protocol handling
+- Network IO
+
+The network IO API is identical to that provided by :class:`socket.socket`,
+from which :class:`SSLSocket` also inherits. This allows an SSL socket to be
+used as a drop-in replacement for a regular socket, making it very easy to add
+SSL support to an existing application.
+
+Combining SSL protocol handling and network IO usually works well, but there
+are some cases where it doesn't. An example is async IO frameworks that want to
+use a different IO multiplexing model than the "select/poll on a file
+descriptor" (readiness based) model that is assumed by :class:`socket.socket`
+and by the internal OpenSSL socket IO routines. This is mostly relevant for
+platforms like Windows where this model is not efficient. For this purpose, a
+reduced scope variant of :class:`SSLSocket` called :class:`SSLObject` is
+provided.
+
+.. class:: SSLObject
+
+ A reduced-scope variant of :class:`SSLSocket` representing an SSL protocol
+ instance that does not contain any network IO methods. This class is
+ typically used by framework authors that want to implement asynchronous IO
+ for SSL through memory buffers.
+
+ This class implements an interface on top of a low-level SSL object as
+ implemented by OpenSSL. This object captures the state of an SSL connection
+ but does not provide any network IO itself. IO needs to be performed through
+ separate "BIO" objects which are OpenSSL's IO abstraction layer.
+
+ An :class:`SSLObject` instance can be created using the
+ :meth:`~SSLContext.wrap_bio` method. This method will create the
+ :class:`SSLObject` instance and bind it to a pair of BIOs. The *incoming*
+ BIO is used to pass data from Python to the SSL protocol instance, while the
+ *outgoing* BIO is used to pass data the other way around.
+
+ The following methods are available:
+
+ - :attr:`~SSLSocket.context`
+ - :attr:`~SSLSocket.server_side`
+ - :attr:`~SSLSocket.server_hostname`
+ - :meth:`~SSLSocket.read`
+ - :meth:`~SSLSocket.write`
+ - :meth:`~SSLSocket.getpeercert`
+ - :meth:`~SSLSocket.selected_npn_protocol`
+ - :meth:`~SSLSocket.cipher`
+ - :meth:`~SSLSocket.shared_ciphers`
+ - :meth:`~SSLSocket.compression`
+ - :meth:`~SSLSocket.pending`
+ - :meth:`~SSLSocket.do_handshake`
+ - :meth:`~SSLSocket.unwrap`
+ - :meth:`~SSLSocket.get_channel_binding`
+
+ When compared to :class:`SSLSocket`, this object lacks the following
+ features:
+
+ - Any form of network IO incluging methods such as ``recv()`` and
+ ``send()``.
+
+ - There is no *do_handshake_on_connect* machinery. You must always manually
+ call :meth:`~SSLSocket.do_handshake` to start the handshake.
+
+ - There is no handling of *suppress_ragged_eofs*. All end-of-file conditions
+ that are in violation of the protocol are reported via the
+ :exc:`SSLEOFError` exception.
+
+ - The method :meth:`~SSLSocket.unwrap` call does not return anything,
+ unlike for an SSL socket where it returns the underlying socket.
+
+ - The *server_name_callback* callback passed to
+ :meth:`SSLContext.set_servername_callback` will get an :class:`SSLObject`
+ instance instead of a :class:`SSLSocket` instance as its first parameter.
+
+ Some notes related to the use of :class:`SSLObject`:
+
+ - All IO on an :class:`SSLObject` is :ref:`non-blocking <ssl-nonblocking>`.
+ This means that for example :meth:`~SSLSocket.read` will raise an
+ :exc:`SSLWantReadError` if it needs more data than the incoming BIO has
+ available.
+
+ - There is no module-level ``wrap_bio()`` call like there is for
+ :meth:`~SSLContext.wrap_socket`. An :class:`SSLObject` is always created
+ via an :class:`SSLContext`.
+
+An SSLObject communicates with the outside world using memory buffers. The
+class :class:`MemoryBIO` provides a memory buffer that can be used for this
+purpose. It wraps an OpenSSL memory BIO (Basic IO) object:
+
+.. class:: MemoryBIO
+
+ A memory buffer that can be used to pass data between Python and an SSL
+ protocol instance.
+
+ .. attribute:: MemoryBIO.pending
+
+ Return the number of bytes currently in the memory buffer.
+
+ .. attribute:: MemoryBIO.eof
+
+ A boolean indicating whether the memory BIO is current at the end-of-file
+ position.
+
+ .. method:: MemoryBIO.read(n=-1)
+
+ Read up to *n* bytes from the memory buffer. If *n* is not specified or
+ negative, all bytes are returned.
+
+ .. method:: MemoryBIO.write(buf)
+
+ Write the bytes from *buf* to the memory BIO. The *buf* argument must be an
+ object supporting the buffer protocol.
+
+ The return value is the number of bytes written, which is always equal to
+ the length of *buf*.
+
+ .. method:: MemoryBIO.write_eof()
+
+ Write an EOF marker to the memory BIO. After this method has been called, it
+ is illegal to call :meth:`~MemoryBIO.write`. The attribute :attr:`eof` will
+ become true after all data currently in the buffer has been read.
+
+
.. _ssl-security:
Security considerations
diff --git a/Doc/library/stat.rst b/Doc/library/stat.rst
index 24769f6..845b2ef 100644
--- a/Doc/library/stat.rst
+++ b/Doc/library/stat.rst
@@ -126,7 +126,7 @@ Example::
if __name__ == '__main__':
walktree(sys.argv[1], visitfile)
-An additional utility function is provided to covert a file's mode in a human
+An additional utility function is provided to convert a file's mode in a human
readable string:
.. function:: filemode(mode)
@@ -399,3 +399,29 @@ The following flags can be used in the *flags* argument of :func:`os.chflags`:
The file is a snapshot file.
See the \*BSD or Mac OS systems man page :manpage:`chflags(2)` for more information.
+
+On Windows, the following file attribute constants are available for use when
+testing bits in the ``st_file_attributes`` member returned by :func:`os.stat`.
+See the `Windows API documentation
+<http://msdn.microsoft.com/en-us/library/windows/desktop/gg258117.aspx>`_
+for more detail on the meaning of these constants.
+
+.. data:: FILE_ATTRIBUTE_ARCHIVE
+ FILE_ATTRIBUTE_COMPRESSED
+ FILE_ATTRIBUTE_DEVICE
+ FILE_ATTRIBUTE_DIRECTORY
+ FILE_ATTRIBUTE_ENCRYPTED
+ FILE_ATTRIBUTE_HIDDEN
+ FILE_ATTRIBUTE_INTEGRITY_STREAM
+ FILE_ATTRIBUTE_NORMAL
+ FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
+ FILE_ATTRIBUTE_NO_SCRUB_DATA
+ FILE_ATTRIBUTE_OFFLINE
+ FILE_ATTRIBUTE_READONLY
+ FILE_ATTRIBUTE_REPARSE_POINT
+ FILE_ATTRIBUTE_SPARSE_FILE
+ FILE_ATTRIBUTE_SYSTEM
+ FILE_ATTRIBUTE_TEMPORARY
+ FILE_ATTRIBUTE_VIRTUAL
+
+ .. versionadded:: 3.5
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index e0f8cfa..55f5351 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -354,7 +354,7 @@ Notes:
The numeric literals accepted include the digits ``0`` to ``9`` or any
Unicode equivalent (code points with the ``Nd`` property).
- See http://www.unicode.org/Public/6.3.0/ucd/extracted/DerivedNumericType.txt
+ See http://www.unicode.org/Public/7.0.0/ucd/extracted/DerivedNumericType.txt
for a complete list of code points with the ``Nd`` property.
@@ -3057,6 +3057,197 @@ place, and instead produce new objects.
always produces a new object, even if no changes were made.
+.. _bytes-formatting:
+
+``printf``-style Bytes Formatting
+----------------------------------
+
+.. index::
+ single: formatting, bytes (%)
+ single: formatting, bytearray (%)
+ single: interpolation, bytes (%)
+ single: interpolation, bytearray (%)
+ single: bytes; formatting
+ single: bytearray; formatting
+ single: bytes; interpolation
+ single: bytearray; interpolation
+ single: printf-style formatting
+ single: sprintf-style formatting
+ single: % formatting
+ single: % interpolation
+
+.. note::
+
+ 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). If the value being printed may be a tuple or
+ dictionary, wrap it in a tuple.
+
+Bytes objects (``bytes``/``bytearray``) have one unique built-in operation:
+the ``%`` operator (modulo).
+This is also known as the bytes *formatting* or *interpolation* operator.
+Given ``format % values`` (where *format* is a bytes object), ``%`` conversion
+specifications in *format* are replaced with zero or more elements of *values*.
+The effect is similar to using the :c:func:`sprintf` in the C language.
+
+If *format* requires a single argument, *values* may be a single non-tuple
+object. [5]_ Otherwise, *values* must be a tuple with exactly the number of
+items specified by the format bytes object, or a single mapping object (for
+example, a dictionary).
+
+A conversion specifier contains two or more characters and has the following
+components, which must occur in this order:
+
+#. The ``'%'`` character, which marks the start of the specifier.
+
+#. Mapping key (optional), consisting of a parenthesised sequence of characters
+ (for example, ``(somename)``).
+
+#. Conversion flags (optional), which affect the result of some conversion
+ types.
+
+#. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the
+ actual width is read from the next element of the tuple in *values*, and the
+ object to convert comes after the minimum field width and optional precision.
+
+#. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If
+ specified as ``'*'`` (an asterisk), the actual precision is read from the next
+ element of the tuple in *values*, and the value to convert comes after the
+ precision.
+
+#. Length modifier (optional).
+
+#. Conversion type.
+
+When the right argument is a dictionary (or other mapping type), then the
+formats in the bytes object *must* include a parenthesised mapping key into that
+dictionary inserted immediately after the ``'%'`` character. The mapping key
+selects the value to be formatted from the mapping. For example:
+
+ >>> print(b'%(language)s has %(number)03d quote types.' %
+ ... {b'language': b"Python", b"number": 2})
+ b'Python has 002 quote types.'
+
+In this case no ``*`` specifiers may occur in a format (since they require a
+sequential parameter list).
+
+The conversion flag characters are:
+
++---------+---------------------------------------------------------------------+
+| Flag | Meaning |
++=========+=====================================================================+
+| ``'#'`` | The value conversion will use the "alternate form" (where defined |
+| | below). |
++---------+---------------------------------------------------------------------+
+| ``'0'`` | The conversion will be zero padded for numeric values. |
++---------+---------------------------------------------------------------------+
+| ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` |
+| | conversion if both are given). |
++---------+---------------------------------------------------------------------+
+| ``' '`` | (a space) A blank should be left before a positive number (or empty |
+| | string) produced by a signed conversion. |
++---------+---------------------------------------------------------------------+
+| ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion |
+| | (overrides a "space" flag). |
++---------+---------------------------------------------------------------------+
+
+A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it
+is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``.
+
+The conversion types are:
+
++------------+-----------------------------------------------------+-------+
+| Conversion | Meaning | Notes |
++============+=====================================================+=======+
+| ``'d'`` | Signed integer decimal. | |
++------------+-----------------------------------------------------+-------+
+| ``'i'`` | Signed integer decimal. | |
++------------+-----------------------------------------------------+-------+
+| ``'o'`` | Signed octal value. | \(1) |
++------------+-----------------------------------------------------+-------+
+| ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(7) |
++------------+-----------------------------------------------------+-------+
+| ``'x'`` | Signed hexadecimal (lowercase). | \(2) |
++------------+-----------------------------------------------------+-------+
+| ``'X'`` | Signed hexadecimal (uppercase). | \(2) |
++------------+-----------------------------------------------------+-------+
+| ``'e'`` | Floating point exponential format (lowercase). | \(3) |
++------------+-----------------------------------------------------+-------+
+| ``'E'`` | Floating point exponential format (uppercase). | \(3) |
++------------+-----------------------------------------------------+-------+
+| ``'f'`` | Floating point decimal format. | \(3) |
++------------+-----------------------------------------------------+-------+
+| ``'F'`` | Floating point decimal format. | \(3) |
++------------+-----------------------------------------------------+-------+
+| ``'g'`` | Floating point format. Uses lowercase exponential | \(4) |
+| | format if exponent is less than -4 or not less than | |
+| | precision, decimal format otherwise. | |
++------------+-----------------------------------------------------+-------+
+| ``'G'`` | Floating point format. Uses uppercase exponential | \(4) |
+| | format if exponent is less than -4 or not less than | |
+| | precision, decimal format otherwise. | |
++------------+-----------------------------------------------------+-------+
+| ``'c'`` | Single byte (accepts integer or single | |
+| | byte objects). | |
++------------+-----------------------------------------------------+-------+
+| ``'b'`` | Bytes (any object that follows the | \(5) |
+| | :ref:`buffer protocol <bufferobjects>` or has | |
+| | :meth:`__bytes__`). | |
++------------+-----------------------------------------------------+-------+
+| ``'s'`` | ``'s'`` is an alias for ``'b'`` and should only | \(6) |
+| | be used for Python2/3 code bases. | |
++------------+-----------------------------------------------------+-------+
+| ``'a'`` | Bytes (converts any Python object using | \(5) |
+| | ``repr(obj).encode('ascii','backslashreplace)``). | |
++------------+-----------------------------------------------------+-------+
+| ``'%'`` | No argument is converted, results in a ``'%'`` | |
+| | character in the result. | |
++------------+-----------------------------------------------------+-------+
+
+Notes:
+
+(1)
+ The alternate form causes a leading zero (``'0'``) to be inserted between
+ left-hand padding and the formatting of the number if the leading character
+ of the result is not already a zero.
+
+(2)
+ The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
+ the ``'x'`` or ``'X'`` format was used) to be inserted between left-hand padding
+ and the formatting of the number if the leading character of the result is not
+ already a zero.
+
+(3)
+ The alternate form causes the result to always contain a decimal point, even if
+ no digits follow it.
+
+ The precision determines the number of digits after the decimal point and
+ defaults to 6.
+
+(4)
+ The alternate form causes the result to always contain a decimal point, and
+ trailing zeroes are not removed as they would otherwise be.
+
+ The precision determines the number of significant digits before and after the
+ decimal point and defaults to 6.
+
+(5)
+ If precision is ``N``, the output is truncated to ``N`` characters.
+
+(6)
+ ``b'%s'`` is deprecated, but will not be removed during the 3.x series.
+
+(7)
+ See :pep:`237`.
+
+.. note::
+
+ The bytearray version of this method does *not* operate in place - it
+ always produces a new object, even if no changes were made.
+
+.. seealso:: :pep:`461`.
+.. versionadded:: 3.5
+
.. _typememoryview:
Memory Views
diff --git a/Doc/library/subprocess.rst b/Doc/library/subprocess.rst
index 36cbf3c..49a82bd 100644
--- a/Doc/library/subprocess.rst
+++ b/Doc/library/subprocess.rst
@@ -635,6 +635,7 @@ Instances of the :class:`Popen` class have the following methods:
must be bytes or, if *universal_newlines* was ``True``, a string.
:meth:`communicate` returns a tuple ``(stdout_data, stderr_data)``.
+ The data will be bytes or, if *universal_newlines* was ``True``, strings.
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
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst
index 3024086..fa125a0 100644
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -718,6 +718,14 @@ always available.
value of :func:`intern` around to benefit from it.
+.. function:: is_finalizing()
+
+ Return :const:`True` if the Python interpreter is
+ :term:`shutting down <interpreter shutdown>`, :const:`False` otherwise.
+
+ .. versionadded:: 3.5
+
+
.. data:: last_type
last_value
last_traceback
diff --git a/Doc/library/tarfile.rst b/Doc/library/tarfile.rst
index 4536eca..4fd94fd 100644
--- a/Doc/library/tarfile.rst
+++ b/Doc/library/tarfile.rst
@@ -62,6 +62,23 @@ Some facts and figures:
+------------------+---------------------------------------------+
| ``'r:xz'`` | Open for reading with lzma compression. |
+------------------+---------------------------------------------+
+ | ``'x'`` or | Create a tarfile exclusively without |
+ | ``'x:'`` | compression. |
+ | | Raise an :exc:`FileExistsError` exception |
+ | | if it is already exists. |
+ +------------------+---------------------------------------------+
+ | ``'x:gz'`` | Create a tarfile with gzip compression. |
+ | | Raise an :exc:`FileExistsError` exception |
+ | | if it is already exists. |
+ +------------------+---------------------------------------------+
+ | ``'x:bz2'`` | Create a tarfile with bzip2 compression. |
+ | | Raise an :exc:`FileExistsError` exception |
+ | | if it is already exists. |
+ +------------------+---------------------------------------------+
+ | ``'x:xz'`` | Create a tarfile with lzma compression. |
+ | | Raise an :exc:`FileExistsError` exception |
+ | | if it is already exists. |
+ +------------------+---------------------------------------------+
| ``'a' or 'a:'`` | Open for appending with no compression. The |
| | file is created if it does not exist. |
+------------------+---------------------------------------------+
@@ -82,9 +99,9 @@ Some facts and figures:
If *fileobj* is specified, it is used as an alternative to a :term:`file object`
opened in binary mode for *name*. It is supposed to be at position 0.
- For modes ``'w:gz'``, ``'r:gz'``, ``'w:bz2'``, ``'r:bz2'``, :func:`tarfile.open`
- accepts the keyword argument *compresslevel* to specify the compression level of
- the file.
+ For modes ``'w:gz'``, ``'r:gz'``, ``'w:bz2'``, ``'r:bz2'``, ``'x:gz'``,
+ ``'x:bz2'``, :func:`tarfile.open` accepts the keyword argument
+ *compresslevel* to specify the compression level of the file.
For special purposes, there is a second format for *mode*:
``'filemode|[compression]'``. :func:`tarfile.open` will return a :class:`TarFile`
@@ -127,6 +144,8 @@ Some facts and figures:
| | writing. |
+-------------+--------------------------------------------+
+ .. versionchanged:: 3.5
+ The ``'x'`` (exclusive creation) mode was added.
.. class:: TarFile
@@ -252,8 +271,8 @@ be finalized; only the internally used file object will be closed. See the
In this case, the file object's :attr:`name` attribute is used if it exists.
*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.
+ data to an existing file, ``'w'`` to create a new file overwriting an existing
+ one or ``'x'`` to create a new file only if it's not exists.
If *fileobj* is given, it is used for reading or writing data. If it can be
determined, *mode* is overridden by *fileobj*'s mode. *fileobj* will be used
@@ -292,12 +311,14 @@ be finalized; only the internally used file object will be closed. See the
to be handled. The default settings will work for most users.
See section :ref:`tar-unicode` for in-depth information.
- .. versionchanged:: 3.2
- Use ``'surrogateescape'`` as the default for the *errors* argument.
-
The *pax_headers* argument is an optional dictionary of strings which
will be added as a pax global header if *format* is :const:`PAX_FORMAT`.
+ .. versionchanged:: 3.2
+ Use ``'surrogateescape'`` as the default for the *errors* argument.
+
+ .. versionchanged:: 3.5
+ The ``'x'`` (exclusive creation) mode was added.
.. classmethod:: TarFile.open(...)
@@ -328,11 +349,15 @@ be finalized; only the internally used file object will be closed. See the
returned by :meth:`getmembers`.
-.. method:: TarFile.list(verbose=True)
+.. method:: TarFile.list(verbose=True, *, members=None)
Print a table of contents to ``sys.stdout``. If *verbose* is :const:`False`,
only the names of the members are printed. If it is :const:`True`, output
- similar to that of :program:`ls -l` is produced.
+ similar to that of :program:`ls -l` is produced. If optional *members* is
+ given, it must be a subset of the list returned by :meth:`getmembers`.
+
+ .. versionchanged:: 3.5
+ Added the *members* parameter.
.. method:: TarFile.next()
diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst
index 44d025d..2c68377 100644
--- a/Doc/library/tempfile.rst
+++ b/Doc/library/tempfile.rst
@@ -54,6 +54,13 @@ The module defines the following user-callable items:
underlying true file object. This file-like object can be used in a
:keyword:`with` statement, just like a normal file.
+ The :py:data:`os.O_TMPFILE` flag is used if it is available and works
+ (Linux-specific, require Linux kernel 3.11 or later).
+
+ .. versionchanged:: 3.5
+
+ The :py:data:`os.O_TMPFILE` flag is now used if available.
+
.. function:: NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None, delete=True)
diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst
index f59ffe5..e7a3544 100644
--- a/Doc/library/threading.rst
+++ b/Doc/library/threading.rst
@@ -630,7 +630,7 @@ item to the buffer only needs to wake up one consumer thread.
cv.wait()
Therefore, the same rules apply as with :meth:`wait`: The lock must be
- held when called and is re-aquired on return. The predicate is evaluated
+ held when called and is re-acquired on return. The predicate is evaluated
with the lock held.
.. versionadded:: 3.2
diff --git a/Doc/library/time.rst b/Doc/library/time.rst
index 8d8b7d4..6bfd521 100644
--- a/Doc/library/time.rst
+++ b/Doc/library/time.rst
@@ -314,9 +314,9 @@ The module defines the following functions and data items:
processes running for more than 49 days. On more recent versions of Windows
and on other operating systems, :func:`monotonic` is system-wide.
- Availability: Windows, Mac OS X, Linux, FreeBSD, OpenBSD, Solaris.
-
.. versionadded:: 3.3
+ .. versionchanged:: 3.5
+ The function is now always available.
.. function:: perf_counter()
diff --git a/Doc/library/timeit.rst b/Doc/library/timeit.rst
index 503a705..17588bd 100644
--- a/Doc/library/timeit.rst
+++ b/Doc/library/timeit.rst
@@ -59,18 +59,26 @@ Python Interface
The module defines three convenience functions and a public class:
-.. function:: timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000)
+.. function:: timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000, globals=None)
Create a :class:`Timer` instance with the given statement, *setup* code and
*timer* function and run its :meth:`.timeit` method with *number* executions.
+ The optional *globals* argument specifies a namespace in which to execute the
+ code.
+ .. versionchanged:: 3.5
+ The optional *globals* parameter was added.
-.. function:: repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000)
+
+.. function:: repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000, globals=None)
Create a :class:`Timer` instance with the given statement, *setup* code and
*timer* function and run its :meth:`.repeat` method with the given *repeat*
- count and *number* executions.
+ count and *number* executions. The optional *globals* argument specifies a
+ namespace in which to execute the code.
+ .. versionchanged:: 3.5
+ The optional *globals* parameter was added.
.. function:: default_timer()
@@ -80,7 +88,7 @@ The module defines three convenience functions and a public class:
:func:`time.perf_counter` is now the default timer.
-.. class:: Timer(stmt='pass', setup='pass', timer=<timer function>)
+.. class:: Timer(stmt='pass', setup='pass', timer=<timer function>, globals=None)
Class for timing execution speed of small code snippets.
@@ -88,7 +96,9 @@ The module defines three convenience functions and a public class:
for setup, and a timer function. Both statements default to ``'pass'``;
the timer function is platform-dependent (see the module doc string).
*stmt* and *setup* may also contain multiple statements separated by ``;``
- or newlines, as long as they don't contain multi-line string literals.
+ or newlines, as long as they don't contain multi-line string literals. The
+ statement will by default be executed within timeit's namespace; this behavior
+ 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`
@@ -99,6 +109,8 @@ The module defines three convenience functions and a public class:
will then be executed by :meth:`.timeit`. Note that the timing overhead is a
little larger in this case because of the extra function calls.
+ .. versionchanged:: 3.5
+ The optional *globals* parameter was added.
.. method:: Timer.timeit(number=1000000)
@@ -318,3 +330,17 @@ To give the :mod:`timeit` module access to functions you define, you can pass a
if __name__ == '__main__':
import timeit
print(timeit.timeit("test()", setup="from __main__ import test"))
+
+Another option is to pass :func:`globals` to the *globals* parameter, which will cause the code
+to be executed within your current global namespace. This can be more convenient
+than individually specifying imports::
+
+ def f(x):
+ return x**2
+ def g(x):
+ return x**4
+ def h(x):
+ return x**8
+
+ import timeit
+ print(timeit.timeit('[func(42) for func in (f,g,h)]', globals=globals()))
diff --git a/Doc/library/tkinter.ttk.rst b/Doc/library/tkinter.ttk.rst
index 6f8bf1c..b0eefcb 100644
--- a/Doc/library/tkinter.ttk.rst
+++ b/Doc/library/tkinter.ttk.rst
@@ -1167,7 +1167,7 @@ Ttk Styling
Each widget in :mod:`ttk` is assigned a style, which specifies the set of
elements making up the widget and how they are arranged, along with dynamic
and default settings for element options. By default the style name is the
-same as the widget's class name, but it may be overriden by the widget's style
+same as the widget's class name, but it may be overridden by the widget's style
option. If you don't know the class name of a widget, use the method
:meth:`Misc.winfo_class` (somewidget.winfo_class()).
diff --git a/Doc/library/token.rst b/Doc/library/token.rst
index 4cd7098..88fb38b 100644
--- a/Doc/library/token.rst
+++ b/Doc/library/token.rst
@@ -93,6 +93,7 @@ The token constants are:
DOUBLESLASH
DOUBLESLASHEQUAL
AT
+ ATEQUAL
RARROW
ELLIPSIS
OP
diff --git a/Doc/library/tracemalloc.rst b/Doc/library/tracemalloc.rst
index 552e84b..a04a432 100644
--- a/Doc/library/tracemalloc.rst
+++ b/Doc/library/tracemalloc.rst
@@ -350,7 +350,7 @@ Functions
the *nframe* parameter of the :func:`start` function to store more frames.
The :mod:`tracemalloc` module must be tracing memory allocations to take a
- snapshot, see the the :func:`start` function.
+ snapshot, see the :func:`start` function.
See also the :func:`get_object_traceback` function.
diff --git a/Doc/library/turtle.rst b/Doc/library/turtle.rst
index 5899f3d..157fe93 100644
--- a/Doc/library/turtle.rst
+++ b/Doc/library/turtle.rst
@@ -1809,7 +1809,7 @@ Input methods
Pop up a dialog window for input of a number. title is the title of the
dialog window, prompt is a text mostly describing what numerical information
- to input. default: default value, minval: minimum value for imput,
+ to input. default: default value, minval: minimum value for input,
maxval: maximum value for input
The number input must be in the range minval .. maxval if these are
given. If not, a hint is issued and the dialog remains open for
@@ -1879,7 +1879,7 @@ Settings and special methods
>>> cv = screen.getcanvas()
>>> cv
- <turtle.ScrolledCanvas object at ...>
+ <turtle.ScrolledCanvas object ...>
.. function:: getshapes()
@@ -2402,7 +2402,7 @@ Changes since Python 3.0
Accordingly the latter has got an alias: :meth:`Screen.onkeyrelease`.
- The method :meth:`Screen.mainloop` has been added. So when working only
- with Screen and Turtle objects one must not additonally import
+ with Screen and Turtle objects one must not additionally import
:func:`mainloop` anymore.
- Two input methods has been added :meth:`Screen.textinput` and
diff --git a/Doc/library/types.rst b/Doc/library/types.rst
index abdb939..34fffe6 100644
--- a/Doc/library/types.rst
+++ b/Doc/library/types.rst
@@ -115,6 +115,10 @@ Standard names are defined for the following types:
The type of :term:`modules <module>`. Constructor takes the name of the
module to be created and optionally its :term:`docstring`.
+ .. note::
+ Use :func:`importlib.util.module_from_spec` to create a new module if you
+ wish to set the various import-controlled attributes.
+
.. attribute:: __doc__
The :term:`docstring` of the module. Defaults to ``None``.
diff --git a/Doc/library/unicodedata.rst b/Doc/library/unicodedata.rst
index 3b3d3a0..24ddef8 100644
--- a/Doc/library/unicodedata.rst
+++ b/Doc/library/unicodedata.rst
@@ -15,8 +15,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 6.3.0
-<http://www.unicode.org/Public/6.3.0/ucd>`_.
+this database is compiled from the `UCD version 7.0.0
+<http://www.unicode.org/Public/7.0.0/ucd>`_.
The module uses the same names and symbols as defined by Unicode
Standard Annex #44, `"Unicode Character Database"
@@ -166,6 +166,6 @@ Examples:
.. rubric:: Footnotes
-.. [#] http://www.unicode.org/Public/6.3.0/ucd/NameAliases.txt
+.. [#] http://www.unicode.org/Public/7.0.0/ucd/NameAliases.txt
-.. [#] http://www.unicode.org/Public/6.3.0/ucd/NamedSequences.txt
+.. [#] http://www.unicode.org/Public/7.0.0/ucd/NamedSequences.txt
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index 845e0c4..c19adf8 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -181,71 +181,77 @@ The Mock Class
--------------
-`Mock` is a flexible mock object intended to replace the use of stubs and
+:class:`Mock` is a flexible mock object intended to replace the use of stubs and
test doubles throughout your code. Mocks are callable and create attributes as
new mocks when you access them [#]_. Accessing the same attribute will always
return the same mock. Mocks record how you use them, allowing you to make
assertions about what your code has done to them.
-:class:`MagicMock` is a subclass of `Mock` with all the magic methods
+:class:`MagicMock` is a subclass of :class:`Mock` with all the magic methods
pre-created and ready to use. There are also non-callable variants, useful
when you are mocking out objects that aren't callable:
:class:`NonCallableMock` and :class:`NonCallableMagicMock`
The :func:`patch` decorators makes it easy to temporarily replace classes
-in a particular module with a `Mock` object. By default `patch` will create
-a `MagicMock` for you. You can specify an alternative class of `Mock` using
-the `new_callable` argument to `patch`.
+in a particular module with a :class:`Mock` object. By default :func:`patch` will create
+a :class:`MagicMock` for you. You can specify an alternative class of :class:`Mock` using
+the *new_callable* argument to :func:`patch`.
-.. class:: Mock(spec=None, side_effect=None, return_value=DEFAULT, wraps=None, name=None, spec_set=None, **kwargs)
+.. class:: Mock(spec=None, side_effect=None, return_value=DEFAULT, wraps=None, name=None, spec_set=None, unsafe=False, **kwargs)
- Create a new `Mock` object. `Mock` takes several optional arguments
+ Create a new :class:`Mock` object. :class:`Mock` takes several optional arguments
that specify the behaviour of the Mock object:
- * `spec`: This can be either a list of strings or an existing object (a
+ * *spec*: This can be either a list of strings or an existing object (a
class or instance) that acts as the specification for the mock object. If
you pass in an object then a list of strings is formed by calling dir on
the object (excluding unsupported magic attributes and methods).
- Accessing any attribute not in this list will raise an `AttributeError`.
+ Accessing any attribute not in this list will raise an :exc:`AttributeError`.
- If `spec` is an object (rather than a list of strings) then
+ If *spec* is an object (rather than a list of strings) then
:attr:`~instance.__class__` returns the class of the spec object. This
- allows mocks to pass `isinstance` tests.
+ allows mocks to pass :func:`isinstance` tests.
- * `spec_set`: A stricter variant of `spec`. If used, attempting to *set*
+ * *spec_set*: A stricter variant of *spec*. If used, attempting to *set*
or get an attribute on the mock that isn't on the object passed as
- `spec_set` will raise an `AttributeError`.
+ *spec_set* will raise an :exc:`AttributeError`.
- * `side_effect`: A function to be called whenever the Mock is called. See
+ * *side_effect*: A function to be called whenever the Mock is called. See
the :attr:`~Mock.side_effect` attribute. Useful for raising exceptions or
dynamically changing return values. The function is called with the same
arguments as the mock, and unless it returns :data:`DEFAULT`, the return
value of this function is used as the return value.
- Alternatively `side_effect` can be an exception class or instance. In
+ Alternatively *side_effect* can be an exception class or instance. In
this case the exception will be raised when the mock is called.
- If `side_effect` is an iterable then each call to the mock will return
+ If *side_effect* is an iterable then each call to the mock will return
the next value from the iterable.
- A `side_effect` can be cleared by setting it to `None`.
+ A *side_effect* can be cleared by setting it to ``None``.
- * `return_value`: The value returned when the mock is called. By default
+ * *return_value*: The value returned when the mock is called. By default
this is a new Mock (created on first access). See the
:attr:`return_value` attribute.
- * `wraps`: Item for the mock object to wrap. If `wraps` is not None then
+ * *unsafe*: By default if any attribute starts with *assert* or
+ *assret* will raise an :exc:`AttributeError`. Passing ``unsafe=True``
+ will allow access to these attributes.
+
+ .. versionadded:: 3.5
+
+ * *wraps*: Item for the mock object to wrap. If *wraps* is not None then
calling the Mock will pass the call through to the wrapped object
(returning the real result). Attribute access on the mock will return a
Mock object that wraps the corresponding attribute of the wrapped
object (so attempting to access an attribute that doesn't exist will
- raise an `AttributeError`).
+ raise an :exc:`AttributeError`).
- If the mock has an explicit `return_value` set then calls are not passed
- to the wrapped object and the `return_value` is returned instead.
+ If the mock has an explicit *return_value* set then calls are not passed
+ to the wrapped object and the *return_value* is returned instead.
- * `name`: If the mock has a name then it will be used in the repr of the
+ * *name*: If the mock has a name then it will be used in the repr of the
mock. This can be useful for debugging. The name is propagated to child
mocks.
@@ -315,6 +321,20 @@ the `new_callable` argument to `patch`.
>>> calls = [call(4), call(2), call(3)]
>>> mock.assert_has_calls(calls, any_order=True)
+ .. method:: assert_not_called(*args, **kwargs)
+
+ Assert the mock was never called.
+
+ >>> m = Mock()
+ >>> m.hello.assert_not_called()
+ >>> obj = m.hello()
+ >>> m.hello.assert_not_called()
+ Traceback (most recent call last):
+ ...
+ AssertionError: Expected 'hello' to not have been called. Called 1 times.
+
+ .. versionadded:: 3.5
+
.. method:: reset_mock()
@@ -1032,25 +1052,31 @@ patch
default because it can be dangerous. With it switched on you can write
passing tests against APIs that don't actually exist!
- Patch can be used as a `TestCase` class decorator. It works by
+ .. note::
+
+ .. versionchanged:: 3.5
+ If you are patching builtins in a module then you don't
+ need to pass ``create=True``, it will be added by default.
+
+ Patch can be used as a :class:`TestCase` class decorator. It works by
decorating each test method in the class. This reduces the boilerplate
- code when your test methods share a common patchings set. `patch` finds
- tests by looking for method names that start with `patch.TEST_PREFIX`.
- By default this is `test`, which matches the way `unittest` finds tests.
- You can specify an alternative prefix by setting `patch.TEST_PREFIX`.
+ code when your test methods share a common patchings set. :func:`patch` finds
+ tests by looking for method names that start with ``patch.TEST_PREFIX``.
+ By default this is ``'test'``, which matches the way :mod:`unittest` finds tests.
+ You can specify an alternative prefix by setting ``patch.TEST_PREFIX``.
Patch can be used as a context manager, with the with statement. Here the
patching applies to the indented block after the with statement. If you
use "as" then the patched object will be bound to the name after the
- "as"; very useful if `patch` is creating a mock object for you.
+ "as"; very useful if :func:`patch` is creating a mock object for you.
- `patch` takes arbitrary keyword arguments. These will be passed to
- the `Mock` (or `new_callable`) on construction.
+ :func:`patch` takes arbitrary keyword arguments. These will be passed to
+ the :class:`Mock` (or *new_callable*) on construction.
- `patch.dict(...)`, `patch.multiple(...)` and `patch.object(...)` are
+ ``patch.dict(...)``, ``patch.multiple(...)`` and ``patch.object(...)`` are
available for alternate use-cases.
-`patch` as function decorator, creating the mock for you and passing it into
+:func:`patch` as function decorator, creating the mock for you and passing it into
the decorated function:
>>> @patch('__main__.SomeClass')
@@ -1392,15 +1418,31 @@ method of a :class:`TestCase`:
... assert package.module.Class is self.MockClass
...
- As an added bonus you no longer need to keep a reference to the `patcher`
+ As an added bonus you no longer need to keep a reference to the ``patcher``
object.
It is also possible to stop all patches which have been started by using
-`patch.stopall`.
+:func:`patch.stopall`.
.. function:: patch.stopall
- Stop all active patches. Only stops patches started with `start`.
+ Stop all active patches. Only stops patches started with ``start``.
+
+
+.. _patch-builtins:
+
+patch builtins
+~~~~~~~~~~~~~~
+You can patch any builtins within a module. The following example patches
+builtin :func:`ord`:
+
+ >>> @patch('__main__.ord')
+ ... def test(mock_ord):
+ ... mock_ord.return_value = 101
+ ... print(ord('c'))
+ ...
+ >>> test()
+ 101
TEST_PREFIX
@@ -2013,7 +2055,7 @@ Mocking context managers with a :class:`MagicMock` is common enough and fiddly
enough that a helper function is useful.
>>> m = mock_open()
- >>> with patch('__main__.open', m, create=True):
+ >>> with patch('__main__.open', m):
... with open('foo', 'w') as h:
... h.write('some stuff')
...
@@ -2028,7 +2070,7 @@ enough that a helper function is useful.
And for reading files:
- >>> with patch('__main__.open', mock_open(read_data='bibble'), create=True) as m:
+ >>> with patch('__main__.open', mock_open(read_data='bibble')) as m:
... with open('foo') as h:
... result = h.read()
...
diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst
index c65a99d..92609ec 100644
--- a/Doc/library/unittest.rst
+++ b/Doc/library/unittest.rst
@@ -1552,6 +1552,20 @@ Loading and running tests
:data:`unittest.defaultTestLoader`. Using a subclass or instance, however,
allows customization of some configurable properties.
+ :class:`TestLoader` objects have the following attributes:
+
+
+ .. attribute:: errors
+
+ A list of the non-fatal errors encountered while loading tests. Not reset
+ by the loader at any point. Fatal errors are signalled by the relevant
+ a method raising an exception to the caller. Non-fatal errors are also
+ indicated by a synthetic test that will raise the original error when
+ run.
+
+ .. versionadded:: 3.5
+
+
:class:`TestLoader` objects have the following methods:
@@ -1561,7 +1575,7 @@ Loading and running tests
:class:`testCaseClass`.
- .. method:: loadTestsFromModule(module)
+ .. method:: loadTestsFromModule(module, pattern=None)
Return a suite of all tests cases contained in the given module. This
method searches *module* for classes derived from :class:`TestCase` and
@@ -1578,11 +1592,18 @@ Loading and running tests
If a module provides a ``load_tests`` function it will be called to
load the tests. This allows modules to customize test loading.
- This is the `load_tests protocol`_.
+ This is the `load_tests protocol`_. The *pattern* argument is passed as
+ the third argument to ``load_tests``.
.. versionchanged:: 3.2
Support for ``load_tests`` added.
+ .. versionchanged:: 3.5
+ The undocumented and unofficial *use_load_tests* default argument is
+ deprecated and ignored, although it is still accepted for backward
+ compatibility. The method also now accepts a keyword-only argument
+ *pattern* which is passed to ``load_tests`` as the third argument.
+
.. method:: loadTestsFromName(name, module=None)
@@ -1608,6 +1629,12 @@ Loading and running tests
The method optionally resolves *name* relative to the given *module*.
+ .. versionchanged:: 3.5
+ If an :exc:`ImportError` or :exc:`AttributeError` occurs while traversing
+ *name* then a synthetic test that raises that error when run will be
+ returned. These errors are included in the errors accumulated by
+ self.errors.
+
.. method:: loadTestsFromNames(names, module=None)
@@ -1634,18 +1661,22 @@ Loading and running tests
the start directory is not the top level directory then the top level
directory must be specified separately.
- If importing a module fails, for example due to a syntax error, then this
- will be recorded as a single error and discovery will continue. If the
- import failure is due to :exc:`SkipTest` being raised, it will be recorded
- as a skip instead of an error.
+ If importing a module fails, for example due to a syntax error, then
+ this will be recorded as a single error and discovery will continue. If
+ the import failure is due to :exc:`SkipTest` being raised, it will be
+ recorded as a skip instead of an error.
- If a test package name (directory with :file:`__init__.py`) matches the
- pattern then the package will be checked for a ``load_tests``
- function. If this exists then it will be called with *loader*, *tests*,
- *pattern*.
+ If a package (a directory containing a file named :file:`__init__.py`) is
+ found, the package will be checked for a ``load_tests`` function. If this
+ exists then it will be called
+ ``package.load_tests(loader, tests, pattern)``. Test discovery takes care
+ to ensure that a package is only checked for tests once during an
+ invocation, even if the load_tests function itself calls
+ ``loader.discover``.
- If ``load_tests`` exists then discovery does *not* recurse into the package,
- ``load_tests`` is responsible for loading all tests in the package.
+ If ``load_tests`` exists then discovery does *not* recurse into the
+ package, ``load_tests`` is responsible for loading all tests in the
+ package.
The pattern is deliberately not stored as a loader attribute so that
packages can continue discovery themselves. *top_level_dir* is stored so
@@ -1664,6 +1695,11 @@ Loading and running tests
the same even if the underlying file system's ordering is not
dependent on file name.
+ .. versionchanged:: 3.5
+ Found packages are now checked for ``load_tests`` regardless of
+ whether their path matches *pattern*, because it is impossible for
+ a package name to match the default pattern.
+
The following attributes of a :class:`TestLoader` can be configured either by
subclassing or assignment on an instance:
@@ -2032,7 +2068,10 @@ test runs or test discovery by implementing a function called ``load_tests``.
If a test module defines ``load_tests`` it will be called by
:meth:`TestLoader.loadTestsFromModule` with the following arguments::
- load_tests(loader, standard_tests, None)
+ load_tests(loader, standard_tests, pattern)
+
+where *pattern* is passed straight through from ``loadTestsFromModule``. It
+defaults to ``None``.
It should return a :class:`TestSuite`.
@@ -2054,21 +2093,12 @@ A typical ``load_tests`` function that loads tests from a specific set of
suite.addTests(tests)
return suite
-If discovery is started, either from the command line or by calling
-:meth:`TestLoader.discover`, with a pattern that matches a package
-name then the package :file:`__init__.py` will be checked for ``load_tests``.
-
-.. note::
-
- The default pattern is ``'test*.py'``. This matches all Python files
- that start with ``'test'`` but *won't* match any test directories.
-
- A pattern like ``'test*'`` will match test packages as well as
- modules.
-
-If the package :file:`__init__.py` defines ``load_tests`` then it will be
-called and discovery not continued into the package. ``load_tests``
-is called with the following arguments::
+If discovery is started in a directory containing a package, either from the
+command line or by calling :meth:`TestLoader.discover`, then the package
+:file:`__init__.py` will be checked for ``load_tests``. If that function does
+not exist, discovery will recurse into the package as though it were just
+another directory. Otherwise, discovery of the package's tests will be left up
+to ``load_tests`` which is called with the following arguments::
load_tests(loader, standard_tests, pattern)
@@ -2087,6 +2117,11 @@ continue (and potentially modify) test discovery. A 'do nothing'
standard_tests.addTests(package_tests)
return standard_tests
+.. versionchanged:: 3.5
+ Discovery no longer checks package names for matching *pattern* due to the
+ impossibility of package names matching the default pattern.
+
+
Class and Module Fixtures
-------------------------
diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst
index 154a521..3ecdda1 100644
--- a/Doc/library/urllib.parse.rst
+++ b/Doc/library/urllib.parse.rst
@@ -268,6 +268,11 @@ or on combining URL components into a URL string.
:func:`urlunsplit`, removing possible *scheme* and *netloc* parts.
+ .. versionchanged:: 3.5
+
+ Behaviour updated to match the semantics defined in :rfc:`3986`.
+
+
.. function:: urldefrag(url)
If *url* contains a fragment identifier, return a modified version of *url*
diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst
index 249396e..2400526 100644
--- a/Doc/library/urllib.request.rst
+++ b/Doc/library/urllib.request.rst
@@ -63,7 +63,7 @@ The :mod:`urllib.request` module defines the following functions:
:class:`http.client.HTTPResponse` object which has the following
:ref:`httpresponse-objects` methods.
- For ftp, file, and data urls and requests explicity handled by legacy
+ For ftp, file, and data urls and requests explicitly handled by legacy
:class:`URLopener` and :class:`FancyURLopener` classes, this function
returns a :class:`urllib.response.addinfourl` object which can work as
:term:`context manager` and has methods such as
@@ -110,6 +110,7 @@ The :mod:`urllib.request` module defines the following functions:
.. versionchanged:: 3.4.3
*context* was added.
+
.. function:: install_opener(opener)
Install an :class:`OpenerDirector` instance as the default global opener.
@@ -300,6 +301,17 @@ The following classes are provided:
presented with a wrong Authentication scheme.
+.. class:: HTTPBasicPriorAuthHandler(password_mgr=None)
+
+ A variant of :class:`HTTPBasicAuthHandler` which automatically sends
+ authorization credentials with the first request, rather than waiting to
+ first receive a HTTP 401 "Unauthorised" error response. This allows
+ authentication to sites that don't provide a 401 response when receiving
+ a request without an Authorization header. Aside from this difference,
+ this behaves exactly as :class:`HTTPBasicAuthHandler`.
+
+ .. versionadded:: 3.5
+
.. class:: ProxyBasicAuthHandler(password_mgr=None)
Handle authentication with the proxy. *password_mgr*, if given, should be
@@ -1066,7 +1078,7 @@ The following W3C document, http://www.w3.org/International/O-charset\ , lists
the various ways in which a (X)HTML or a XML document could have specified its
encoding information.
-As the python.org website uses *utf-8* encoding as specified in it's meta tag, we
+As the python.org website uses *utf-8* encoding as specified in its meta tag, we
will use the same for decoding the bytes object. ::
>>> with urllib.request.urlopen('http://www.python.org/') as f:
diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst
index 9ca60a9..2e16077 100644
--- a/Doc/library/weakref.rst
+++ b/Doc/library/weakref.rst
@@ -258,7 +258,7 @@ These method have the same issues as the and :meth:`keyrefs` method of
are called in reverse order of creation.
A finalizer will never invoke its callback during the later part of
- the interpreter shutdown when module globals are liable to have
+ the :term:`interpreter shutdown` when module globals are liable to have
been replaced by :const:`None`.
.. method:: __call__()
@@ -527,8 +527,8 @@ follows::
Starting with Python 3.4, :meth:`__del__` methods no longer prevent
reference cycles from being garbage collected, and module globals are
-no longer forced to :const:`None` during interpreter shutdown. So this
-code should work without any issues on CPython.
+no longer forced to :const:`None` during :term:`interpreter shutdown`.
+So this code should work without any issues on CPython.
However, handling of :meth:`__del__` methods is notoriously implementation
specific, since it depends on internal details of the interpreter's garbage
@@ -566,8 +566,8 @@ third party, such as running code when a module is unloaded::
.. note::
- If you create a finalizer object in a daemonic thread just as the
- the program exits then there is the possibility that the finalizer
+ If you create a finalizer object in a daemonic thread just as the program
+ exits then there is the possibility that the finalizer
does not get called at exit. However, in a daemonic thread
:func:`atexit.register`, ``try: ... finally: ...`` and ``with: ...``
do not guarantee that cleanup occurs either.
diff --git a/Doc/library/webbrowser.rst b/Doc/library/webbrowser.rst
index ef63769..aa5e4ad 100644
--- a/Doc/library/webbrowser.rst
+++ b/Doc/library/webbrowser.rst
@@ -20,7 +20,7 @@ available. If text-mode browsers are used, the calling process will block until
the user exits the browser.
If the environment variable :envvar:`BROWSER` exists, it is interpreted as the
-:data:`os.pathsep`-separated list of browsers to try ahead of the the platform
+:data:`os.pathsep`-separated list of browsers to try ahead of the platform
defaults. When the value of a list part contains the string ``%s``, then it is
interpreted as a literal browser command line to be used with the argument URL
substituted for ``%s``; if the part does not contain ``%s``, it is simply
diff --git a/Doc/library/wsgiref.rst b/Doc/library/wsgiref.rst
index 1cef2e9..2238140 100644
--- a/Doc/library/wsgiref.rst
+++ b/Doc/library/wsgiref.rst
@@ -184,10 +184,11 @@ This module provides a single class, :class:`Headers`, for convenient
manipulation of WSGI response headers using a mapping-like interface.
-.. class:: Headers(headers)
+.. class:: Headers([headers])
Create a mapping-like object wrapping *headers*, which must be a list of header
- name/value tuples as described in :pep:`3333`.
+ name/value tuples as described in :pep:`3333`. The default value of *headers* is
+ an empty list.
:class:`Headers` objects support typical mapping operations including
:meth:`__getitem__`, :meth:`get`, :meth:`__setitem__`, :meth:`setdefault`,
@@ -251,6 +252,10 @@ manipulation of WSGI response headers using a mapping-like interface.
Content-Disposition: attachment; filename="bud.gif"
+ .. versionchanged:: 3.5
+ *headers* parameter is optional.
+
+
:mod:`wsgiref.simple_server` -- a simple WSGI HTTP server
---------------------------------------------------------
diff --git a/Doc/library/xml.dom.rst b/Doc/library/xml.dom.rst
index 19512ed..4914738 100644
--- a/Doc/library/xml.dom.rst
+++ b/Doc/library/xml.dom.rst
@@ -412,7 +412,7 @@ objects:
.. method:: NodeList.item(i)
Return the *i*'th item from the sequence, if there is one, or ``None``. The
- index *i* is not allowed to be less then zero or greater than or equal to the
+ index *i* is not allowed to be less than zero or greater than or equal to the
length of the sequence.
diff --git a/Doc/library/xmlrpc.client.rst b/Doc/library/xmlrpc.client.rst
index cc5e83a..e199931 100644
--- a/Doc/library/xmlrpc.client.rst
+++ b/Doc/library/xmlrpc.client.rst
@@ -27,7 +27,7 @@ between conformable Python objects and XML on the wire.
constructed data. If you need to parse untrusted or unauthenticated data see
:ref:`xml-vulnerabilities`.
-.. versionchanged:: 3.4.3
+.. versionchanged:: 3.5
For https URIs, :mod:`xmlrpc.client` now performs all the necessary
certificate and hostname checks by default
@@ -129,7 +129,7 @@ between conformable Python objects and XML on the wire.
:class:`Server` is retained as an alias for :class:`ServerProxy` for backwards
compatibility. New code should use :class:`ServerProxy`.
- .. versionchanged:: 3.4.3
+ .. versionchanged:: 3.5
Added the *context* argument.
@@ -200,6 +200,11 @@ grouped under the reserved :attr:`system` attribute:
no such string is available, an empty string is returned. The documentation
string may contain HTML markup.
+.. versionchanged:: 3.5
+
+ Instances of :class:`ServerProxy` support the :term:`context manager` protocol
+ for closing the underlying transport.
+
A working example follows. The server code::
@@ -217,9 +222,9 @@ The client code for the preceding server::
import xmlrpc.client
- proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
- print("3 is even: %s" % str(proxy.is_even(3)))
- print("100 is even: %s" % str(proxy.is_even(100)))
+ with xmlrpc.client.ServerProxy("http://localhost:8000/") as proxy:
+ print("3 is even: %s" % str(proxy.is_even(3)))
+ print("100 is even: %s" % str(proxy.is_even(100)))
.. _datetime-objects:
@@ -527,14 +532,14 @@ Example of Client Usage
from xmlrpc.client import ServerProxy, Error
# server = ServerProxy("http://localhost:8000") # local server
- server = ServerProxy("http://betty.userland.com")
+ with ServerProxy("http://betty.userland.com") as proxy:
- print(server)
+ print(proxy)
- try:
- print(server.examples.getStateName(41))
- except Error as v:
- print("ERROR", v)
+ try:
+ print(proxy.examples.getStateName(41))
+ except Error as v:
+ print("ERROR", v)
To access an XML-RPC server through a proxy, you need to define a custom
transport. The following example shows how:
diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst
index 1d23a7c..465d786 100644
--- a/Doc/library/zipfile.rst
+++ b/Doc/library/zipfile.rst
@@ -219,14 +219,8 @@ ZipFile Objects
.. note::
- If the ZipFile was created by passing in a file-like object as the first
- argument to the constructor, then the object returned by :meth:`.open` shares the
- ZipFile's file pointer. Under these circumstances, the object returned by
- :meth:`.open` should not be used after any additional operations are performed
- on the ZipFile object. If the ZipFile was created by passing in a string (the
- filename) as the first argument to the constructor, then :meth:`.open` will
- create a new file object that will be held by the ZipExtFile, allowing it to
- operate independently of the ZipFile.
+ Objects returned by :meth:`.open` can operate independently of the
+ ZipFile.
.. note::
diff --git a/Doc/make.bat b/Doc/make.bat
index 1070166..6aae34a 100644
--- a/Doc/make.bat
+++ b/Doc/make.bat
@@ -8,9 +8,17 @@ set this=%~n0
if "%SPHINXBUILD%" EQU "" set SPHINXBUILD=sphinx-build
if "%PYTHON%" EQU "" set PYTHON=py
-if DEFINED ProgramFiles(x86) set _PRGMFLS=%ProgramFiles(x86)%
-if NOT DEFINED ProgramFiles(x86) set _PRGMFLS=%ProgramFiles%
-if "%HTMLHELP%" EQU "" set HTMLHELP=%_PRGMFLS%\HTML Help Workshop\hhc.exe
+if "%1" NEQ "htmlhelp" goto :skiphhcsearch
+if exist "%HTMLHELP%" goto :skiphhcsearch
+
+rem Search for HHC in likely places
+set HTMLHELP=
+where hhc /q && set HTMLHELP=hhc && goto :skiphhcsearch
+where /R ..\externals hhc > "%TEMP%\hhc.loc" 2> nul && set /P HTMLHELP= < "%TEMP%\hhc.loc" & del "%TEMP%\hhc.loc"
+if not exist "%HTMLHELP%" where /R "%ProgramFiles(x86)%" hhc > "%TEMP%\hhc.loc" 2> nul && set /P HTMLHELP= < "%TEMP%\hhc.loc" & del "%TEMP%\hhc.loc"
+if not exist "%HTMLHELP%" where /R "%ProgramFiles%" hhc > "%TEMP%\hhc.loc" 2> nul && set /P HTMLHELP= < "%TEMP%\hhc.loc" & del "%TEMP%\hhc.loc"
+if not exist "%HTMLHELP%" echo Cannot find HHC on PATH or in externals & exit /B 1
+:skiphhcsearch
if "%DISTVERSION%" EQU "" for /f "usebackq" %%v in (`%PYTHON% tools/extensions/patchlevel.py`) do set DISTVERSION=%%v
@@ -36,7 +44,8 @@ if errorlevel 9009 (
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
- goto end
+ popd
+ exit /B 1
)
rem Targets that do require sphinx-build and have their own label
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index e3646f8..5e909a9 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1986,6 +1986,7 @@ left undefined.
.. method:: object.__add__(self, other)
object.__sub__(self, other)
object.__mul__(self, other)
+ object.__matmul__(self, other)
object.__truediv__(self, other)
object.__floordiv__(self, other)
object.__mod__(self, other)
@@ -2002,15 +2003,16 @@ left undefined.
builtin: pow
builtin: pow
- These methods are called to implement the binary arithmetic operations (``+``,
- ``-``, ``*``, ``/``, ``//``, ``%``, :func:`divmod`, :func:`pow`, ``**``, ``<<``,
- ``>>``, ``&``, ``^``, ``|``). For instance, to evaluate the expression
- ``x + y``, where *x* is an instance of a class that has an :meth:`__add__`
- method, ``x.__add__(y)`` is called. The :meth:`__divmod__` method should be the
- equivalent to using :meth:`__floordiv__` and :meth:`__mod__`; it should not be
- related to :meth:`__truediv__`. Note that :meth:`__pow__` should be defined
- to accept an optional third argument if the ternary version of the built-in
- :func:`pow` function is to be supported.
+ These methods are called to implement the binary arithmetic operations
+ (``+``, ``-``, ``*``, ``@``, ``/``, ``//``, ``%``, :func:`divmod`,
+ :func:`pow`, ``**``, ``<<``, ``>>``, ``&``, ``^``, ``|``). For instance, to
+ evaluate the expression ``x + y``, where *x* is an instance of a class that
+ has an :meth:`__add__` method, ``x.__add__(y)`` is called. The
+ :meth:`__divmod__` method should be the equivalent to using
+ :meth:`__floordiv__` and :meth:`__mod__`; it should not be related to
+ :meth:`__truediv__`. Note that :meth:`__pow__` should be defined to accept
+ an optional third argument if the ternary version of the built-in :func:`pow`
+ function is to be supported.
If one of those methods does not support the operation with the supplied
arguments, it should return ``NotImplemented``.
@@ -2019,6 +2021,7 @@ left undefined.
.. method:: object.__radd__(self, other)
object.__rsub__(self, other)
object.__rmul__(self, other)
+ object.__rmatmul__(self, other)
object.__rtruediv__(self, other)
object.__rfloordiv__(self, other)
object.__rmod__(self, other)
@@ -2034,14 +2037,14 @@ left undefined.
builtin: divmod
builtin: pow
- These methods are called to implement the binary arithmetic operations (``+``,
- ``-``, ``*``, ``/``, ``//``, ``%``, :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 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*.
+ These methods are called to implement the binary arithmetic operations
+ (``+``, ``-``, ``*``, ``@``, ``/``, ``//``, ``%``, :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
+ 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*.
.. index:: builtin: pow
@@ -2059,6 +2062,7 @@ left undefined.
.. method:: object.__iadd__(self, other)
object.__isub__(self, other)
object.__imul__(self, other)
+ object.__imatmul__(self, other)
object.__itruediv__(self, other)
object.__ifloordiv__(self, other)
object.__imod__(self, other)
@@ -2070,17 +2074,17 @@ left undefined.
object.__ior__(self, other)
These methods are called to implement the augmented arithmetic assignments
- (``+=``, ``-=``, ``*=``, ``/=``, ``//=``, ``%=``, ``**=``, ``<<=``, ``>>=``,
- ``&=``, ``^=``, ``|=``). These methods should attempt to do the operation
- in-place (modifying *self*) and return the result (which could be, but does
- not have to be, *self*). If a specific method is not defined, the augmented
- assignment falls back to the normal methods. For instance, if *x* is an
- instance of a class with an :meth:`__iadd__` method, ``x += y`` is equivalent
- to ``x = x.__iadd__(y)`` . Otherwise, ``x.__add__(y)`` and ``y.__radd__(x)``
- are considered, as with the evaluation of ``x + y``. In certain situations,
- augmented assignment can result in unexpected errors (see
- :ref:`faq-augmented-assignment-tuple-error`), but this behavior is in
- fact part of the data model.
+ (``+=``, ``-=``, ``*=``, ``@=``, ``/=``, ``//=``, ``%=``, ``**=``, ``<<=``,
+ ``>>=``, ``&=``, ``^=``, ``|=``). These methods should attempt to do the
+ operation in-place (modifying *self*) and return the result (which could be,
+ but does not have to be, *self*). If a specific method is not defined, the
+ augmented assignment falls back to the normal methods. For instance, if *x*
+ is an instance of a class with an :meth:`__iadd__` method, ``x += y`` is
+ equivalent to ``x = x.__iadd__(y)`` . Otherwise, ``x.__add__(y)`` and
+ ``y.__radd__(x)`` are considered, as with the evaluation of ``x + y``. In
+ certain situations, augmented assignment can result in unexpected errors (see
+ :ref:`faq-augmented-assignment-tuple-error`), but this behavior is in fact
+ part of the data model.
.. method:: object.__neg__(self)
diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst
index d0682ca..58d38ed 100644
--- a/Doc/reference/expressions.rst
+++ b/Doc/reference/expressions.rst
@@ -891,8 +891,9 @@ from the power operator, there are only two levels, one for multiplicative
operators and one for additive operators:
.. productionlist::
- m_expr: `u_expr` | `m_expr` "*" `u_expr` | `m_expr` "//" `u_expr` | `m_expr` "/" `u_expr`
- : | `m_expr` "%" `u_expr`
+ m_expr: `u_expr` | `m_expr` "*" `u_expr` | `m_expr` "@" `m_expr` |
+ : `m_expr` "//" `u_expr`| `m_expr` "/" `u_expr` |
+ : `m_expr` "%" `u_expr`
a_expr: `m_expr` | `a_expr` "+" `m_expr` | `a_expr` "-" `m_expr`
.. index:: single: multiplication
@@ -903,6 +904,13 @@ the other must be a sequence. In the former case, the numbers are converted to a
common type and then multiplied together. In the latter case, sequence
repetition is performed; a negative repetition factor yields an empty sequence.
+.. index:: single: matrix multiplication
+
+The ``@`` (at) operator is intended to be used for matrix multiplication. No
+builtin Python types implement this operator.
+
+.. versionadded:: 3.5
+
.. index::
exception: ZeroDivisionError
single: division
@@ -1346,8 +1354,9 @@ precedence and have a left-to-right chaining feature as described in the
+-----------------------------------------------+-------------------------------------+
| ``+``, ``-`` | Addition and subtraction |
+-----------------------------------------------+-------------------------------------+
-| ``*``, ``/``, ``//``, ``%`` | Multiplication, division, remainder |
-| | [#]_ |
+| ``*``, ``@``, ``/``, ``//``, ``%`` | Multiplication, matrix |
+| | multiplication division, |
+| | remainder [#]_ |
+-----------------------------------------------+-------------------------------------+
| ``+x``, ``-x``, ``~x`` | Positive, negative, bitwise NOT |
+-----------------------------------------------+-------------------------------------+
diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst
index e9b7e53..2d60e62 100644
--- a/Doc/reference/import.rst
+++ b/Doc/reference/import.rst
@@ -339,6 +339,7 @@ of what happens during the loading portion of import::
module = None
if spec.loader is not None and hasattr(spec.loader, 'create_module'):
+ # It is assumed 'exec_module' will also be defined on the loader.
module = spec.loader.create_module(spec)
if module is None:
module = ModuleType(spec.name)
@@ -427,7 +428,7 @@ Module loaders may opt in to creating the module object during loading
by implementing a :meth:`~importlib.abc.Loader.create_module` method.
It takes one argument, the module spec, and returns the new module object
to use during loading. ``create_module()`` does not need to set any attributes
-on the module object. If the loader does not define ``create_module()``, the
+on the module object. If the method returns ``None``, the
import machinery will create the new module itself.
.. versionadded:: 3.4
@@ -459,7 +460,13 @@ import machinery will create the new module itself.
* If loading fails, the loader must remove any modules it has inserted
into :data:`sys.modules`, but it must remove **only** the failing
- module, and only if the loader itself has loaded it explicitly.
+ module(s), and only if the loader itself has loaded the module(s)
+ explicitly.
+
+.. versionchanged:: 3.5
+ A :exc:`DeprecationWarning` is raised when ``exec_module()`` is defined but
+ ``create_module()`` is not. Starting in Python 3.6 it will be an error to not
+ define ``create_module()`` on a loader attached to a ModuleSpec.
Module spec
-----------
@@ -753,6 +760,15 @@ hook` callables on :data:`sys.path_hooks`, then the following protocol is used
to ask the finder for a module spec, which is then used when loading the
module.
+The current working directory -- denoted by an empty string -- is handled
+slightly differently from other entries on :data:`sys.path`. First, if the
+current working directory is found to not exist, no value is stored in
+:data:`sys.path_importer_cache`. Second, the value for the current working
+directory is looked up fresh for each module lookup. Third, the path used for
+:data:`sys.path_importer_cache` and returned by
+:meth:`importlib.machinery.PathFinder.find_spec` will be the actual current
+working directory and not the empty string.
+
Path entry finder protocol
--------------------------
diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst
index 2bf66b1..c673791 100644
--- a/Doc/reference/lexical_analysis.rst
+++ b/Doc/reference/lexical_analysis.rst
@@ -311,7 +311,7 @@ The Unicode category codes mentioned above stand for:
* *Nd* - decimal numbers
* *Pc* - connector punctuations
* *Other_ID_Start* - explicit list of characters in `PropList.txt
- <http://www.unicode.org/Public/6.3.0/ucd/PropList.txt>`_ to support backwards
+ <http://www.unicode.org/Public/7.0.0/ucd/PropList.txt>`_ to support backwards
compatibility
* *Other_ID_Continue* - likewise
@@ -691,7 +691,7 @@ Operators
The following tokens are operators::
- + - * ** / // %
+ + - * ** / // % @
<< >> & | ^ ~
< > <= >= == !=
@@ -707,7 +707,7 @@ The following tokens serve as delimiters in the grammar::
( ) [ ] { }
, : . ; @ = ->
- += -= *= /= //= %=
+ += -= *= /= //= %= @=
&= |= ^= >>= <<= **=
The period can also occur in floating-point and imaginary literals. A sequence
@@ -728,4 +728,4 @@ occurrence outside string literals and comments is an unconditional error::
.. rubric:: Footnotes
-.. [#] http://www.unicode.org/Public/6.3.0/ucd/NameAliases.txt
+.. [#] http://www.unicode.org/Public/7.0.0/ucd/NameAliases.txt
diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst
index 8946b4f..5f60540 100644
--- a/Doc/reference/simple_stmts.rst
+++ b/Doc/reference/simple_stmts.rst
@@ -281,7 +281,7 @@ operation and an assignment statement:
.. productionlist::
augmented_assignment_stmt: `augtarget` `augop` (`expression_list` | `yield_expression`)
augtarget: `identifier` | `attributeref` | `subscription` | `slicing`
- augop: "+=" | "-=" | "*=" | "/=" | "//=" | "%=" | "**="
+ augop: "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**="
: | ">>=" | "<<=" | "&=" | "^=" | "|="
(See section :ref:`primaries` for the syntax definitions of the last three
diff --git a/Doc/tools/extensions/pyspecific.py b/Doc/tools/extensions/pyspecific.py
index 17b3c82..b4ec6ee 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.4/%s'
+SOURCE_URI = 'https://hg.python.org/cpython/file/default/%s'
# monkey-patch reST parser to disable alphabetic and roman enumerated lists
from docutils.parsers.rst.states import Body
diff --git a/Doc/tools/susp-ignored.csv b/Doc/tools/susp-ignored.csv
index 48653af8..9856afd 100644
--- a/Doc/tools/susp-ignored.csv
+++ b/Doc/tools/susp-ignored.csv
@@ -122,6 +122,8 @@ library/ipaddress,,:db8,>>> ipaddress.IPv6Address('2001:db8::1000')
library/ipaddress,,::,>>> ipaddress.IPv6Address('2001:db8::1000')
library/ipaddress,,:db8,IPv6Address('2001:db8::1000')
library/ipaddress,,::,IPv6Address('2001:db8::1000')
+library/ipaddress,,:db8,">>> ipaddress.ip_address(""2001:db8::1"").reverse_pointer"
+library/ipaddress,,::,">>> ipaddress.ip_address(""2001:db8::1"").reverse_pointer"
library/ipaddress,,::,"""::abc:7:def"""
library/ipaddress,,:def,"""::abc:7:def"""
library/ipaddress,,::,::FFFF/96
@@ -256,6 +258,11 @@ whatsnew/2.4,,:System,
whatsnew/2.5,,:memory,:memory:
whatsnew/2.5,,:step,[start:stop:step]
whatsnew/2.5,,:stop,[start:stop:step]
+whatsnew/2.7,,::,"ParseResult(scheme='http', netloc='[1080::8:800:200C:417A]',"
+whatsnew/2.7,,::,>>> urlparse.urlparse('http://[1080::8:800:200C:417A]/foo')
+whatsnew/2.7,,:Sunday,'2009:4:Sunday'
+whatsnew/2.7,,:Cookie,"export PYTHONWARNINGS=all,error:::Cookie:0"
+whatsnew/2.7,,::,"export PYTHONWARNINGS=all,error:::Cookie:0"
whatsnew/3.2,,:affe,"netloc='[dead:beef:cafe:5417:affe:8FA3:deaf:feed]',"
whatsnew/3.2,,:affe,>>> urllib.parse.urlparse('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/')
whatsnew/3.2,,:beef,"netloc='[dead:beef:cafe:5417:affe:8FA3:deaf:feed]',"
@@ -271,14 +278,5 @@ 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,,:platform,:platform:
whatsnew/changelog,,:gz,": TarFile opened with external fileobj and ""w:gz"" mode didn't"
-whatsnew/changelog,,:PythonCmd,"With Tk < 8.5 _tkinter.c:PythonCmd() raised UnicodeDecodeError, caused"
-whatsnew/changelog,,::,": Fix FTP tests for IPv6, bind to ""::1"" instead of ""localhost""."
whatsnew/changelog,,::,": Use ""127.0.0.1"" or ""::1"" instead of ""localhost"" as much as"
-whatsnew/changelog,,:password,user:password
-whatsnew/2.7,780,:Sunday,'2009:4:Sunday'
-whatsnew/2.7,907,::,"export PYTHONWARNINGS=all,error:::Cookie:0"
-whatsnew/2.7,907,:Cookie,"export PYTHONWARNINGS=all,error:::Cookie:0"
-whatsnew/2.7,1657,::,>>> urlparse.urlparse('http://[1080::8:800:200C:417A]/foo')
-whatsnew/2.7,1657,::,"ParseResult(scheme='http', netloc='[1080::8:800:200C:417A]',"
diff --git a/Doc/tools/templates/indexsidebar.html b/Doc/tools/templates/indexsidebar.html
index abdf070..78e9c4f 100644
--- a/Doc/tools/templates/indexsidebar.html
+++ b/Doc/tools/templates/indexsidebar.html
@@ -3,8 +3,7 @@
<h3>Docs for other versions</h3>
<ul>
<li><a href="https://docs.python.org/2.7/">Python 2.7 (stable)</a></li>
- <li><a href="https://docs.python.org/3.3/">Python 3.3 (stable)</a></li>
- <li><a href="https://docs.python.org/3.5/">Python 3.5 (in development)</a></li>
+ <li><a href="https://docs.python.org/3.4/">Python 3.4 (stable)</a></li>
<li><a href="https://www.python.org/doc/versions/">Old versions</a></li>
</ul>
diff --git a/Doc/tutorial/appendix.rst b/Doc/tutorial/appendix.rst
index 8670efc..67262a1 100644
--- a/Doc/tutorial/appendix.rst
+++ b/Doc/tutorial/appendix.rst
@@ -40,7 +40,7 @@ Executable Python Scripts
On BSD'ish Unix systems, Python scripts can be made directly executable, like
shell scripts, by putting the line ::
- #!/usr/bin/env python3.4
+ #!/usr/bin/env python3.5
(assuming that the interpreter is on the user's :envvar:`PATH`) at the beginning
of the script and giving the file an executable mode. The ``#!`` must be the
@@ -107,7 +107,7 @@ of your user site-packages directory. Start Python and run this code::
>>> import site
>>> site.getusersitepackages()
- '/home/user/.local/lib/python3.4/site-packages'
+ '/home/user/.local/lib/python3.5/site-packages'
Now you can create a file named :file:`usercustomize.py` in that directory and
put anything you want in it. It will affect every invocation of Python, unless
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
index 1ea299f..a2031ed 100644
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -73,10 +73,11 @@ objects:
Return the number of times *x* appears in the list.
-.. method:: list.sort()
+.. method:: list.sort(key=None, reverse=False)
:noindex:
- Sort the items of the list in place.
+ Sort the items of the list in place (the arguments can be used for sort
+ customization, see :func:`sorted` for their explanation).
.. method:: list.reverse()
diff --git a/Doc/tutorial/interpreter.rst b/Doc/tutorial/interpreter.rst
index 8051634..d5789a6 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.4`
+The Python interpreter is usually installed as :file:`/usr/local/bin/python3.5`
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.4
+ python3.5
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:\\Python34`, though you can change this when you're running the
+:file:`C:\\Python35`, 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:\python34
+ set path=%path%;C:\python35
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
@@ -96,8 +96,8 @@ with the *secondary prompt*, by default three dots (``...``). The interpreter
prints a welcome message stating its version number and a copyright notice
before printing the first prompt::
- $ python3.4
- Python 3.4 (default, Mar 16 2014, 09:25:04)
+ $ python3.5
+ Python 3.5 (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/stdlib.rst b/Doc/tutorial/stdlib.rst
index cd73bc2..954ef44 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:\\Python34'
+ 'C:\\Python35'
>>> 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 c0197ea..f7d2a0a 100644
--- a/Doc/tutorial/stdlib2.rst
+++ b/Doc/tutorial/stdlib2.rst
@@ -18,7 +18,7 @@ abbreviated displays of large or deeply nested containers::
>>> import reprlib
>>> reprlib.repr(set('supercalifragilisticexpialidocious'))
- "set(['a', 'c', 'd', 'e', 'f', 'g', ...])"
+ "{'a', 'c', 'd', 'e', 'f', 'g', ...}"
The :mod:`pprint` module offers more sophisticated control over printing both
built-in and user defined objects in a way that is readable by the interpreter.
@@ -277,7 +277,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:/python34/lib/weakref.py", line 46, in __getitem__
+ File "C:/python35/lib/weakref.py", line 46, in __getitem__
o = self.data[key]()
KeyError: 'primary'
diff --git a/Doc/using/venv-create.inc b/Doc/using/venv-create.inc
index 45bdd5a..02bcbee 100644
--- a/Doc/using/venv-create.inc
+++ b/Doc/using/venv-create.inc
@@ -21,11 +21,11 @@ subdirectory (on Windows, this is ``Lib\site-packages``).
On Windows, you may have to invoke the ``pyvenv`` script as follows, if you
don't have the relevant PATH and PATHEXT settings::
- c:\Temp>c:\Python34\python c:\Python34\Tools\Scripts\pyvenv.py myenv
+ c:\Temp>c:\Python35\python c:\Python35\Tools\Scripts\pyvenv.py myenv
or equivalently::
- c:\Temp>c:\Python34\python -m venv myenv
+ c:\Temp>c:\Python35\python -m venv myenv
The command, if run with ``-h``, will show the available options::
diff --git a/Doc/using/win_installer.png b/Doc/using/win_installer.png
new file mode 100644
index 0000000..4696bb2
--- /dev/null
+++ b/Doc/using/win_installer.png
Binary files differ
diff --git a/Doc/using/windows.rst b/Doc/using/windows.rst
index c05f72a..af66196 100644
--- a/Doc/using/windows.rst
+++ b/Doc/using/windows.rst
@@ -7,6 +7,7 @@
*************************
.. sectionauthor:: Robert Lehmann <lehmannro@gmail.com>
+.. sectionauthor:: Steve Dower <steve.dower@microsoft.com>
This document aims to give an overview of Windows-specific behaviour you should
know about when using Python on Microsoft Windows.
@@ -19,10 +20,172 @@ know about when using Python on Microsoft Windows.
Installing Python
=================
-Unlike most Unix systems and services, Windows does not require Python natively
-and thus does not pre-install a version of Python. However, the CPython team
+Unlike most Unix systems and services, Windows does not include a system
+supported installation of Python. To make Python available, the CPython team
has compiled Windows installers (MSI packages) with every `release
-<https://www.python.org/download/releases/>`_ for many years.
+<https://www.python.org/download/releases/>`_ for many years. These installers
+are primarily intended to add a system-wide installation of Python, with the
+core interpreter and library being shared by all application. Non-shared
+layouts of the Python interpreter may also be created with the same installer,
+however, the released installer is not intended for embedding in other
+installers.
+
+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
+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
+during installation.
+
+After starting the installer, one of three options may be selected:
+
+.. image:: win_installer.png
+
+If you select "Install for All Users":
+
+* You may be required to provide administrative credentials or approval
+* Python will be installed into your Program Files directory
+* The :ref:`launcher` will be installed into your Windows directory
+* The standard library, test suite, launcher and pip will be installed
+* After installation, the standard library will be pre-compiled to bytecode
+* If selected, the install directory will be added to :envvar:`PATH`
+
+If you select "Install Just for Me":
+
+* You will *not* need to be an administrator
+* Python will be installed into your user directory
+* The :ref:`launcher` will *also* be installed into your user directory
+* The standard library, test suite, launcher and pip will be installed
+* If selected, the install directory will be added to :envvar:`PATH`
+
+Selecting "Customize installation" will allow you to select the features to
+install, the installation location and other options or post-install actions.
+To install debugging symbols or binaries, you will need to use this option.
+
+.. _install-quiet-option:
+
+Installing Without UI
+---------------------
+
+All of the options available in the installer UI can also be specified from the
+command line, allowing scripted installers to replicate an installation on many
+machines without user interaction. These options may also be set without
+suppressing the UI in order to change some of the defaults.
+
+To completely hide the installer UI and install Python silently, pass the
+``/quiet`` (``/q``) option. To skip past the user interaction but still display
+progress and errors, pass the ``/passive`` (``/p``) option. The ``/uninstall``
+option may be passed to immediately begin removing Python - no prompt will be
+displayed.
+
+All other options are passed as ``name=value``, where the value is usually
+``0`` to disable a feature, ``1`` to enable a feature, or a path. The full list
+of available options is shown below.
+
++---------------------------+--------------------------------------+--------------------------+
+| Name | Description | Default |
++===========================+======================================+==========================+
+| InstallAllUsers | Perform a system-wide installation. | 1 |
++---------------------------+--------------------------------------+--------------------------+
+| TargetDir | The installation directory | Selected based on |
+| | | InstallAllUsers |
++---------------------------+--------------------------------------+--------------------------+
+| DefaultAllUsersTargetDir | The default installation directory | :file:`%ProgramFiles%\\\ |
+| | for all-user installs | Python X.Y` or :file:`\ |
+| | | %ProgramFiles(x86)%\\\ |
+| | | Python X.Y` |
++---------------------------+--------------------------------------+--------------------------+
+| DefaultJustForMeTargetDir | The default install directory for | :file:`%LocalAppData%\\\ |
+| | just-for-me installs | Programs\\PythonXY` or |
+| | | :file:`%LocalAppData%\\\ |
+| | | Programs\\PythonXY-32` |
++---------------------------+--------------------------------------+--------------------------+
+| DefaultCustomTargetDir | The default custom install directory | (empty) |
+| | displayed in the UI | |
++---------------------------+--------------------------------------+--------------------------+
+| AssociateFiles | Create file associations if the | 1 |
+| | launcher is also installed. | |
++---------------------------+--------------------------------------+--------------------------+
+| CompileAll | Compile all ``.py`` files to | 0 |
+| | ``.pyc`` and ``.pyo``. | |
++---------------------------+--------------------------------------+--------------------------+
+| PrependPath | Add install and Scripts directories | 0 |
+| | tho :envvar:`PATH` and ``.PY`` to | |
+| | :envvar:`PATHEXT` | |
++---------------------------+--------------------------------------+--------------------------+
+| Include_doc | Install Python manual | 1 |
++---------------------------+--------------------------------------+--------------------------+
+| Include_debug | Install debug binaries | 0 |
++---------------------------+--------------------------------------+--------------------------+
+| Include_dev | Install developer headers and | 1 |
+| | libraries | |
++---------------------------+--------------------------------------+--------------------------+
+| Include_exe | Install :file:`python.exe` and | 1 |
+| | related files | |
++---------------------------+--------------------------------------+--------------------------+
+| Include_launcher | Install :ref:`launcher`. | 1 |
++---------------------------+--------------------------------------+--------------------------+
+| Include_lib | Install standard library and | 1 |
+| | extension modules | |
++---------------------------+--------------------------------------+--------------------------+
+| Include_pip | Install bundled pip and setuptools | 1 |
++---------------------------+--------------------------------------+--------------------------+
+| Include_symbols | Install debugging symbols (`*`.pdb) | 0 |
++---------------------------+--------------------------------------+--------------------------+
+| Include_tcltk | Install Tcl/Tk support and IDLE | 1 |
++---------------------------+--------------------------------------+--------------------------+
+| Include_test | Install standard library test suite | 1 |
++---------------------------+--------------------------------------+--------------------------+
+| Include_tools | Install utility scripts | 1 |
++---------------------------+--------------------------------------+--------------------------+
+| SimpleInstall | Disable most install UI | 0 |
++---------------------------+--------------------------------------+--------------------------+
+
+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
+
+To allow users to easily install a personal copy of Python without the test
+suite, you could provide a shortcut with the following command::
+
+ python-3.5.0.exe /passive InstallAllUsers=0 Include_launcher=0 Include_test=0 SimpleInstall=1
+
+(Note that omitting the launcher also omits file associations, and is only
+recommended for per-user installs when there is also a system-wide installation
+that included the launcher.)
+
+.. _install-layout-option:
+
+Installing Without Downloading
+------------------------------
+
+As some features of Python are not included in the initial installer download,
+selecting those features may require an internet connection. To avoid this
+need, all possible components may be downloaded on-demand to create a complete
+*layout* that will no longer require an internet connection regardless of the
+selected features. Note that this download may be bigger than required, but
+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
+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]
+
+You may also specify the ``/quiet`` option to hide the progress display.
+
+
+Other Platforms
+---------------
With ongoing development of Python, some platforms that used to be supported
earlier are no longer supported (due to the lack of users or developers).
@@ -66,19 +229,31 @@ key features:
`ActivePython <http://www.activestate.com/activepython/>`_
Installer with multi-platform compatibility, documentation, PyWin32
-`Enthought Python Distribution <https://www.enthought.com/products/epd/>`_
- Popular modules (such as PyWin32) with their respective documentation, tool
- suite for building extensible Python applications
+`Anaconda <http://www.continuum.io/downloads/>`_
+ Popular scientific modules (such as numpy, scipy and pandas) and the
+ ``conda`` package manager.
+
+`Canopy <https://www.enthought.com/products/canopy/>`_
+ A "comprehensive Python analysis environment" with editors and other
+ development tools.
-Notice that these packages are likely to install *older* versions of Python.
+`WinPython <https://winpython.github.io/>`_
+ Windows-specific distribution with prebuilt scientific packages and
+ tools for building packages.
+
+Note that these packages may not include the latest versions of Python or
+other libraries, and are not maintained or supported by the core Python team.
Configuring Python
==================
-In order to run Python flawlessly, you might have to change certain environment
-settings in Windows.
+To run Python conveniently from a command prompt, you might consider changing
+some default environment variables in Windows. While the installer provides an
+option to configure the PATH and PATHEXT variables for you, this is only
+reliable for a single, system-wide installation. If you regularly use multiple
+versions of Python, consider using the :ref:`launcher`.
.. _setting-envvars:
@@ -86,163 +261,86 @@ settings in Windows.
Excursus: Setting environment variables
---------------------------------------
-Windows has a built-in dialog for changing environment variables (following
-guide applies to XP classical view): Right-click the icon for your machine
-(usually located on your Desktop and called "My Computer") and choose
-:menuselection:`Properties` there. Then, open the :guilabel:`Advanced` tab
-and click the :guilabel:`Environment Variables` button.
+Windows allows environment variables to be configured permanently at both the
+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::
-In short, your path is:
+ C:\>set PATH=C:\Program Files\Python 3.5;%PATH%
+ C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
+ C:\>python
- :menuselection:`My Computer
- --> Properties
- --> Advanced
- --> Environment Variables`
+These changes will apply to any further commands executed in that console, and
+will be inherited by any applications started from the console.
+Including the variable name within percent signs will expand to the existing
+value, allowing you to add your new value at either the start or the end.
+Modifying :envvar:`PATH` by adding the directory containing
+:program:`python.exe` to the start is a common way to ensure the correct version
+of Python is launched.
+
+To permanently modify the default environment variables, click Start and search
+for 'edit environment variables', or open System properties, :guilabel:`Advanced
+system settings` and click the :guilabel:`Environment Variables` button.
In this dialog, you can add or modify User and System variables. To change
System variables, you need non-restricted access to your machine
(i.e. Administrator rights).
-Another way of adding variables to your environment is using the :command:`set`
-command::
-
- set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
-
-To make this setting permanent, you could add the corresponding command line to
-your :file:`autoexec.bat`. :program:`msconfig` is a graphical interface to this
-file.
+.. note::
-Viewing environment variables can also be done more straight-forward: The
-command prompt will expand strings wrapped into percent signs automatically::
+ Windows will concatenate User variables *after* System variables, which may
+ cause unexpected results when modifying :envvar:`PATH`.
- echo %PATH%
-
-Consult :command:`set /?` for details on this behaviour.
+ The :envvar:`PYTHONPATH` variable is used by all versions of Python 2 and
+ Python 3, so you should not permanently configure this variable unless it
+ only includes code that is compatible all of your installed Python
+ versions.
.. seealso::
- http://support.microsoft.com/kb/100843
+ http://support.microsoft.com/kb/100843
Environment variables in Windows NT
- http://support.microsoft.com/kb/310519
+ http://technet.microsoft.com/en-us/library/cc754250.aspx
+ The SET command, for temporarily modifying environment variables
+
+ http://technet.microsoft.com/en-us/library/cc755104.aspx
+ The SETX command, for permanently modifying environment variables
+
+ http://support.microsoft.com/kb/310519
How To Manage Environment Variables in Windows XP
- http://www.chem.gla.ac.uk/~louis/software/faq/q1.html
+ http://www.chem.gla.ac.uk/~louis/software/faq/q1.html
Setting Environment variables, Louis J. Farrugia
-
.. _windows-path-mod:
Finding the Python executable
-----------------------------
-.. versionchanged:: 3.3
+.. versionchanged:: 3.5
Besides using the automatically created start menu entry for the Python
-interpreter, you might want to start Python in the command prompt. As of
-Python 3.3, the installer has an option to set that up for you.
-
-At the "Customize Python 3.3" screen, an option called
-"Add python.exe to search path" can be enabled to have the installer place
-your installation into the :envvar:`%PATH%`. This allows you to type
-:command:`python` to run the interpreter. Thus, you can also execute your
+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.
+
+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
+: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` or . Thus, you can also execute your
scripts with command line options, see :ref:`using-on-cmdline` documentation.
If you don't enable this option at install time, you can always re-run the
-installer to choose it.
-
-The alternative is manually modifying the :envvar:`%PATH%` using the
-directions in :ref:`setting-envvars`. You need to set your :envvar:`%PATH%`
-environment variable to include the directory of your Python distribution,
-delimited by a semicolon from other entries. An example variable could look
-like this (assuming the first two entries are Windows' default)::
-
- C:\WINDOWS\system32;C:\WINDOWS;C:\Python33
-
-
-Finding modules
----------------
-
-Python usually stores its library (and thereby your site-packages folder) in the
-installation directory. So, if you had installed Python to
-:file:`C:\\Python\\`, the default library would reside in
-: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:
-
-* An empty entry is added at the start, which corresponds to the current
- directory.
-
-* If the environment variable :envvar:`PYTHONPATH` exists, as described in
- :ref:`using-on-envvars`, its entries are added next. Note that on Windows,
- paths in this variable must be separated by semicolons, to distinguish them
- from the colon used in drive identifiers (``C:\`` etc.).
-
-* Additional "application paths" can be added in the registry as subkeys of
- :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the
- ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives. Subkeys which have
- semicolon-delimited path strings as their default value will cause each path
- to be added to :data:`sys.path`. (Note that all known installers only use
- HKLM, so HKCU is typically empty.)
-
-* 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.
-
-* 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
- relative entries is used (e.g. ``.\Lib;.\plat-win``, etc).
-
-The end result of all this is:
-
-* When running :file:`python.exe`, or any other .exe in the main Python
- directory (either an installed version, or directly from the PCbuild
- directory), the core path is deduced, and the core paths in the registry are
- ignored. Other "application paths" in the registry are always read.
-
-* When Python is hosted in another .exe (different directory, embedded via COM,
- etc), the "Python Home" will not be deduced, so the core path from the
- registry is used. Other "application paths" in the registry are always read.
-
-* If Python can't find its home and there is no registry (eg, frozen .exe, some
- very strange installation setup) you get a path with some default, but
- relative, paths.
-
-
-Executing scripts
------------------
-
-As of Python 3.3, Python includes a launcher which facilitates running Python
-scripts. See :ref:`launcher` for more information.
-
-Executing scripts without the Python launcher
----------------------------------------------
-
-Without the Python launcher installed, Python scripts (files with the extension
-``.py``) will be executed by :program:`python.exe` by default. This executable
-opens a terminal, which stays open even if the program uses a GUI. If you do
-not want this to happen, use the extension ``.pyw`` which will cause the script
-to be executed by :program:`pythonw.exe` by default (both executables are
-located in the top-level of your Python installation directory). This
-suppresses the terminal window on startup.
-
-You can also make all ``.py`` scripts execute with :program:`pythonw.exe`,
-setting this through the usual facilities, for example (might require
-administrative rights):
-
-#. Launch a command prompt.
-#. Associate the correct file group with ``.py`` scripts::
-
- assoc .py=Python.File
-
-#. Redirect all Python files to the new executable::
-
- ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
+installer, select Modify, and enable it. Alternatively, you can manually
+modify the :envvar:`PATH` using the directions in :ref:`setting-envvars`. You
+need to set your :envvar:`PATH` environment variable to include the directory
+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
.. _launcher:
@@ -251,21 +349,26 @@ Python Launcher for Windows
.. versionadded:: 3.3
-The Python launcher for Windows is a utility which aids in the location and
-execution of different Python versions. It allows scripts (or the
+The Python launcher for Windows is a utility which aids in locating and
+executing of different Python versions. It allows scripts (or the
command-line) to indicate a preference for a specific Python version, and
will locate and execute that version.
+Unlike the :envvar:`PATH` variable, the launcher will correctly select the most
+appropriate version of Python. It will prefer per-user installations over
+system-wide ones, and orders by language version rather than using the most
+recently installed version.
+
Getting started
---------------
From the command-line
^^^^^^^^^^^^^^^^^^^^^
-You should ensure the launcher is on your PATH - depending on how it was
-installed it may already be there, but check just in case it is not.
-
-From a command-prompt, execute the following command:
+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
+launcher is available, execute the following command in Command Prompt:
::
@@ -291,6 +394,16 @@ If you have a Python 3.x installed, try the command:
You should find the latest version of Python 3.x starts.
+If you see the following error, you do not have the launcher installed:
+
+::
+
+ 'py' is not recognized as an internal or external command,
+ operable program or batch file.
+
+Per-user installations of Python do not add the launcher to :envvar:`PATH`
+unless the option was selected on installation.
+
From a script
^^^^^^^^^^^^^
@@ -383,17 +496,16 @@ Customization
Customization via INI files
^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Two .ini files will be searched by the launcher - ``py.ini`` in the
- current user's "application data" directory (i.e. the directory returned
- by calling the Windows function SHGetFolderPath with CSIDL_LOCAL_APPDATA)
- and ``py.ini`` in the same directory as the launcher. The same .ini
- files are used for both the 'console' version of the launcher (i.e.
- py.exe) and for the 'windows' version (i.e. pyw.exe)
+Two .ini files will be searched by the launcher - ``py.ini`` in the current
+user's "application data" directory (i.e. the directory returned by calling the
+Windows function SHGetFolderPath with CSIDL_LOCAL_APPDATA) and ``py.ini`` in the
+same directory as the launcher. The same .ini files are used for both the
+'console' version of the launcher (i.e. py.exe) and for the 'windows' version
+(i.e. pyw.exe)
- Customization specified in the "application directory" will have
- precedence over the one next to the executable, so a user, who may not
- have write access to the .ini file next to the launcher, can override
- commands in that global .ini file)
+Customization specified in the "application directory" will have precedence over
+the one next to the executable, so a user, who may not have write access to the
+.ini file next to the launcher, can override commands in that global .ini file)
Customizing default Python versions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -488,6 +600,78 @@ particular version was chosen and the exact command-line used to execute the
target Python.
+
+Finding modules
+===============
+
+Python usually stores its library (and thereby your site-packages folder) in the
+installation directory. So, if you had installed Python to
+:file:`C:\\Python\\`, the default library would reside in
+: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:
+
+* An empty entry is added at the start, which corresponds to the current
+ directory.
+
+* If the environment variable :envvar:`PYTHONPATH` exists, as described in
+ :ref:`using-on-envvars`, its entries are added next. Note that on Windows,
+ paths in this variable must be separated by semicolons, to distinguish them
+ from the colon used in drive identifiers (``C:\`` etc.).
+
+* Additional "application paths" can be added in the registry as subkeys of
+ :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the
+ ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives. Subkeys which have
+ semicolon-delimited path strings as their default value will cause each path
+ to be added to :data:`sys.path`. (Note that all known installers only use
+ HKLM, so HKCU is typically empty.)
+
+* 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.
+
+* 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
+ relative entries is used (e.g. ``.\Lib;.\plat-win``, etc).
+
+The end result of all this is:
+
+* When running :file:`python.exe`, or any other .exe in the main Python
+ directory (either an installed version, or directly from the PCbuild
+ directory), the core path is deduced, and the core paths in the registry are
+ ignored. Other "application paths" in the registry are always read.
+
+* When Python is hosted in another .exe (different directory, embedded via COM,
+ etc), the "Python Home" will not be deduced, so the core path from the
+ registry is used. Other "application paths" in the registry are always read.
+
+* If Python can't find its home and there is no registry (eg, frozen .exe, some
+ very strange installation setup) you get a path with some default, but
+ relative, paths.
+
+For those who want to bundle Python into their application or distribution, the
+following advice will prevent conflicts with other installations:
+
+* If you are loading :file:`python3.dll` or :file:`python35.dll` in your own
+ executable, explicitly call :c:func:`Py_SetPath` or (at least)
+ :c:func:`Py_SetProgramName` before :c:func:`Py_Initialize`.
+
+* Clear and/or overwrite :envvar:`PYTHONPATH` and set :envvar:`PYTHONHOME`
+ before launching :file:`python.exe` from your application.
+
+* 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 bundled library.
+ (Note that it will not be detected inside a ZIP file.)
+
+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.
+Otherwise, your users may experience problems using your application.
+
Additional modules
==================
@@ -498,7 +682,6 @@ and external, and snippets exist to use these features.
The Windows-specific standard modules are documented in
:ref:`mswin-specific-services`.
-
PyWin32
-------
@@ -557,20 +740,8 @@ latest release's source or just grab a fresh `checkout
<https://docs.python.org/devguide/setup.html#getting-the-source-code>`_.
The source tree contains a build solution and project files for Microsoft
-Visual C++, which is the compiler used to build the official Python releases.
-View the :file:`readme.txt` in their respective directories:
-
-+--------------------+--------------+-----------------------+
-| Directory | MSVC version | Visual Studio version |
-+====================+==============+=======================+
-| :file:`PC/VS9.0/` | 9.0 | 2008 |
-+--------------------+--------------+-----------------------+
-| :file:`PCbuild/` | 10.0 | 2010 |
-+--------------------+--------------+-----------------------+
-
-Note that any build directories within the :file:`PC` directory are not
-necessarily fully supported. The :file:`PCbuild` directory contains the files
-for the compiler used to build the official release.
+Visual Studio 2015, which is the compiler used to build the official Python
+releases. These files are in the :file:`PCbuild` directory.
Check :file:`PCbuild/readme.txt` for general information on the build process.
diff --git a/Doc/whatsnew/2.1.rst b/Doc/whatsnew/2.1.rst
index 5058bf1..ff15662 100644
--- a/Doc/whatsnew/2.1.rst
+++ b/Doc/whatsnew/2.1.rst
@@ -219,7 +219,7 @@ comparison. I won't cover the C API here, but will refer you to PEP 207, or to
.. seealso::
- :pep:`207` - Rich Comparisions
+ :pep:`207` - Rich Comparisons
Written by Guido van Rossum, heavily based on earlier work by David Ascher, and
implemented by Guido van Rossum.
diff --git a/Doc/whatsnew/3.3.rst b/Doc/whatsnew/3.3.rst
index 1fdb365..1d4ce72 100644
--- a/Doc/whatsnew/3.3.rst
+++ b/Doc/whatsnew/3.3.rst
@@ -1579,7 +1579,7 @@ os
avoid race conditions in multi-threaded programs.
* The :mod:`os` module has a new :func:`~os.sendfile` function which provides
- an efficent "zero-copy" way for copying data from one file (or socket)
+ an efficient "zero-copy" way for copying data from one file (or socket)
descriptor to another. The phrase "zero-copy" refers to the fact that all of
the copying of data between the two descriptors is done entirely by the
kernel, with no copying of data into userspace buffers. :func:`~os.sendfile`
@@ -1908,7 +1908,7 @@ socketserver
:meth:`~socketserver.BaseServer.service_actions` that is called by the
:meth:`~socketserver.BaseServer.serve_forever` method in the service loop.
:class:`~socketserver.ForkingMixIn` now uses this to clean up zombie
-child proceses. (Contributed by Justin Warkentin in :issue:`11109`.)
+child processes. (Contributed by Justin Warkentin in :issue:`11109`.)
sqlite3
@@ -2360,7 +2360,7 @@ Porting Python code
bytecode file, make sure to call :func:`importlib.invalidate_caches` to clear
out the cache for the finders to notice the new file.
-* :exc:`ImportError` now uses the full name of the module that was attemped to
+* :exc:`ImportError` now uses the full name of the module that was attempted to
be imported. Doctests that check ImportErrors' message will need to be
updated to use the full name of the module instead of just the tail of the
name.
diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
new file mode 100644
index 0000000..7c4e530
--- /dev/null
+++ b/Doc/whatsnew/3.5.rst
@@ -0,0 +1,551 @@
+****************************
+ What's New In Python 3.5
+****************************
+
+:Release: |release|
+:Date: |today|
+
+.. 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.5, compared to 3.4.
+
+For full details, see the :source:`Misc/NEWS` file.
+
+.. note:: Prerelease users should be aware that this document is currently in
+ draft form. It will be updated substantially as Python 3.5 moves towards
+ release, so it's worth checking back even after reading earlier versions.
+
+
+.. seealso::
+
+ :pep:`478` - Python 3.5 Release Schedule
+
+
+Summary -- Release highlights
+=============================
+
+.. This section singles out the most important changes in Python 3.3.
+ Brevity is key.
+
+New syntax features:
+
+* None yet.
+
+New library modules:
+
+* None yet.
+
+New built-in features:
+
+* None yet.
+
+Implementation improvements:
+
+* When the ``LC_TYPE`` locale is the POSIX locale (``C`` locale),
+ :py:data:`sys.stdin` and :py:data:`sys.stdout` are now using the
+ ``surrogateescape`` error handler, instead of the ``strict`` error handler
+ (:issue:`19977`).
+
+Significantly Improved Library Modules:
+
+* None yet.
+
+Security improvements:
+
+* None yet.
+
+Please read on for a comprehensive list of user-facing changes.
+
+
+.. PEP-sized items next.
+
+.. _pep-4XX:
+
+.. PEP 4XX: Virtual Environments
+.. =============================
+
+
+.. (Implemented by Foo Bar.)
+
+.. .. seealso::
+
+ :pep:`4XX` - Python Virtual Environments
+ PEP written by Carl Meyer
+
+
+PEP 475: Retry system calls failing with EINTR
+----------------------------------------------
+
+:pep:`475` adds support for automatic retry of system calls failing with EINTR:
+this means that user code doesn't have to deal with EINTR or InterruptedError
+manually, and should make it more robust against asynchronous signal reception.
+
+.. seealso::
+
+ :pep:`475` -- Retry system calls failing with EINTR
+
+
+Other Language Changes
+======================
+
+Some smaller changes made to the core Python language are:
+
+* Added the ``'namereplace'`` error handlers. The ``'backslashreplace'``
+ error handlers now works with decoding and translating.
+ (Contributed by Serhiy Storchaka in :issue:`19676` and :issue:`22286`.)
+
+
+
+New Modules
+===========
+
+.. module name
+.. -----------
+
+* None yet.
+
+
+Improved Modules
+================
+
+argparse
+--------
+
+* :class:`~argparse.ArgumentParser` now allows to disable
+ :ref:`abbreviated usage <prefix-matching>` of long options by setting
+ :ref:`allow_abbrev` to ``False``.
+ (Contributed by Jonathan Paugh, Steven Bethard, paul j3 and Daniel Eriksson.)
+
+cgi
+---
+
+* :class:`~cgi.FieldStorage` now supports the context management protocol.
+ (Contributed by Berker Peksag in :issue:`20289`.)
+
+code
+----
+
+* The :func:`code.InteractiveInterpreter.showtraceback` method now prints
+ the full chained traceback, just like the interactive interpreter.
+ (Contributed by Claudiu Popa in :issue:`17442`.)
+
+compileall
+----------
+
+* :func:`compileall.compile_dir` and :mod:`compileall`'s command-line interface
+ can now do parallel bytecode compilation.
+ (Contributed by Claudiu Popa in :issue:`16104`.)
+
+contextlib
+----------
+
+* The new :func:`contextlib.redirect_stderr` context manager(similar to
+ :func:`contextlib.redirect_stdout`) makes it easier for utility scripts to
+ handle inflexible APIs that write their output to :data:`sys.stderr` and
+ don't provide any options to redirect it.
+ (Contributed by Berker Peksag in :issue:`22389`.)
+
+distutils
+---------
+
+* The ``build`` and ``build_ext`` commands now accept a ``-j``
+ option to enable parallel building of extension modules.
+ (Contributed by Antoine Pitrou in :issue:`5309`.)
+
+doctest
+-------
+
+* :func:`doctest.DocTestSuite` returns an empty :class:`unittest.TestSuite` if
+ *module* contains no docstrings instead of raising :exc:`ValueError`.
+ (Contributed by Glenn Jones in :issue:`15916`.)
+
+glob
+----
+
+* :func:`~glob.iglob` and :func:`~glob.glob` now support recursive search in
+ subdirectories using the "``**``" pattern.
+ (Contributed by Serhiy Storchaka in :issue:`13968`.)
+
+imaplib
+-------
+
+* :class:`IMAP4` now supports the context management protocol. When used in a
+ :keyword:`with` statement, the IMAP4 ``LOGOUT`` command will be called
+ automatically at the end of the block. (Contributed by Tarek Ziadé and
+ Serhiy Storchaka in :issue:`4972`.)
+
+imghdr
+------
+
+* :func:`~imghdr.what` now recognizes the `OpenEXR <http://www.openexr.com>`_
+ format. (Contributed by Martin Vignali and Claudiu Popa in :issue:`20295`.)
+
+importlib
+---------
+
+* :class:`importlib.util.LazyLoader` allows for the lazy loading of modules in
+ applications where startup time is paramount.
+ (Contributed by Brett Cannon in :issue:`17621`.)
+
+* :func:`importlib.abc.InspectLoader.source_to_code` is now a
+ static method to make it easier to work with source code in a string.
+ With a module object that you want to initialize you can then use
+ ``exec(code, module.__dict__)`` to execute the code in the module.
+
+* :func:`importlib.util.module_from_spec` is now the preferred way to create a
+ new module. Compared to :class:`types.ModuleType`, this new function will set
+ the various import-controlled attributes based on the passed-in spec object.
+
+inspect
+-------
+
+* :class:`inspect.Signature` and :class:`inspect.Parameter` are now
+ picklable and hashable. (Contributed by Yury Selivanov in :issue:`20726`
+ and :issue:`20334`.)
+
+* New class method :meth:`inspect.Signature.from_callable`, which makes
+ subclassing of :class:`~inspect.Signature` easier. (Contributed
+ by Yury Selivanov and Eric Snow in :issue:`17373`.)
+
+ipaddress
+---------
+
+* :class:`ipaddress.IPv4Network` and :class:`ipaddress.IPv6Network` now
+ accept an ``(address, netmask)`` tuple argument, so as to easily construct
+ network objects from existing addresses. (Contributed by Peter Moody
+ and Antoine Pitrou in :issue:`16531`.)
+
+json
+----
+
+* The output of :mod:`json.tool` command line interface is now in the same
+ order as the input. Use the :option:`--sort-keys` option to sort the output
+ of dictionaries alphabetically by key. (Contributed by Berker Peksag in
+ :issue:`21650`.)
+
+* JSON decoder now raises :exc:`json.JSONDecodeError` instead of
+ :exc:`ValueError`. (Contributed by Serhiy Storchaka in :issue:`19361`.)
+
+os
+--
+
+* :class:`os.stat_result` now has a :attr:`~os.stat_result.st_file_attributes`
+ attribute on Windows. (Contributed by Ben Hoyt in :issue:`21719`.)
+
+re
+--
+
+* Number of capturing groups in regular expression is no longer limited by 100.
+ (Contributed by Serhiy Storchaka in :issue:`22437`.)
+
+* Now unmatched groups are replaced with empty strings in :func:`re.sub`
+ and :func:`re.subn`. (Contributed by Serhiy Storchaka in :issue:`1519638`.)
+
+math
+----
+
+* :data:`math.inf` and :data:`math.nan` constants added. (Contributed by Mark
+ Dickinson in :issue:`23185`.)
+
+shutil
+------
+
+* :func:`~shutil.move` now accepts a *copy_function* argument, allowing,
+ for example, :func:`~shutil.copy` to be used instead of the default
+ :func:`~shutil.copy2` if there is a need to ignore metadata. (Contributed by
+ Claudiu Popa in :issue:`19840`.)
+
+signal
+------
+
+* Different constants of :mod:`signal` module are now enumeration values using
+ the :mod:`enum` module. This allows meaningful names to be printed during
+ debugging, instead of integer “magic numbers”. (Contributed by Giampaolo
+ Rodola' in :issue:`21076`.)
+
+smtpd
+-----
+
+* Both :class:`~smtpd.SMTPServer` and :class:`smtpd.SMTPChannel` now accept a
+ *decode_data* keyword to determine if the DATA portion of the SMTP
+ transaction is decoded using the ``utf-8`` codec or is instead provided to
+ :meth:`~smtpd.SMTPServer.process_message` as a byte string. The default
+ is ``True`` for backward compatibility reasons, but will change to ``False``
+ in Python 3.6. (Contributed by Maciej Szulik in :issue:`19662`.)
+
+* It is now possible to provide, directly or via name resolution, IPv6
+ addresses in the :class:`~smtpd.SMTPServer` constructor, and have it
+ successfully connect. (Contributed by Milan Oberkirch in :issue:`14758`.)
+
+* :mod:`~smtpd.SMTPServer` now supports :rfc:`6531` via the *enable_SMTPUTF8*
+ constructor argument and a user-provided
+ :meth:`~smtpd.SMTPServer.process_smtputf8_message` method.
+
+smtplib
+-------
+
+* A new :meth:`~smtplib.SMTP.auth` method provides a convenient way to
+ implement custom authentication mechanisms.
+ (Contributed by Milan Oberkirch in :issue:`15014`.)
+
+sndhdr
+------
+
+* :func:`~sndhdr.what` and :func:`~sndhdr.whathdr` now return
+ :func:`~collections.namedtuple`.
+ (Contributed by Claudiu Popa in :issue:`18615`.)
+
+socket
+------
+
+* New :meth:`socket.socket.sendfile` method allows to send a file over a socket
+ by using high-performance :func:`os.sendfile` function on UNIX resulting in
+ uploads being from 2x to 3x faster than when using plain
+ :meth:`socket.socket.send`.
+ (Contributed by Giampaolo Rodola' in :issue:`17552`.)
+
+sysconfig
+---------
+
+* The user scripts directory on Windows is now versioned.
+ (Contributed by Paul Moore in :issue:`23437`.)
+
+
+tarfile
+-------
+
+* The :func:`tarfile.open` function now supports ``'x'`` (exclusive creation)
+ mode. (Contributed by Berker Peksag in :issue:`21717`.)
+
+time
+----
+
+* The :func:`time.monotonic` function is now always available. (Contributed by
+ Victor Stinner in :issue:`22043`.)
+
+urllib
+------
+
+* A new :class:`urllib.request.HTTPBasicPriorAuthHandler` allows HTTP Basic
+ Authentication credentials to be sent unconditionally with the first HTTP
+ request, rather than waiting for a HTTP 401 Unauthorized response from the
+ server.
+ (Contributed by Matej Cepl in :issue:`19494`.)
+
+wsgiref
+-------
+
+* *headers* parameter of :class:`wsgiref.headers.Headers` is now optional.
+ (Contributed by Pablo Torres Navarrete and SilentGhost in :issue:`5800`.)
+
+xmlrpc
+------
+
+* :class:`xmlrpc.client.ServerProxy` is now a :term:`context manager`.
+ (Contributed by Claudiu Popa in :issue:`20627`.)
+
+
+Optimizations
+=============
+
+The following performance enhancements have been added:
+
+* Construction of ``bytes(int)`` (filled by zero bytes) is faster and use less
+ memory for large objects. ``calloc()`` is used instead of ``malloc()`` to
+ allocate memory for these objects.
+
+* Some operations on :class:`~ipaddress.IPv4Network` and
+ :class:`~ipaddress.IPv6Network` have been massively sped up, such as
+ :meth:`~ipaddress.IPv4Network.subnets`, :meth:`~ipaddress.IPv4Network.supernet`,
+ :func:`~ipaddress.summarize_address_range`, :func:`~ipaddress.collapse_addresses`.
+ The speed up can range from 3x to 15x.
+ (:issue:`21486`, :issue:`21487`, :issue:`20826`)
+
+* Many operations on :class:`io.BytesIO` are now 50% to 100% faster.
+ (Contributed by Serhiy Storchaka in :issue:`15381` and David Wilson in
+ :issue:`22003`.)
+
+* :func:`marshal.dumps` is now faster (65%-85% with versions 3--4, 20-25% with
+ versions 0--2 on typical data, and up to 5x in best cases).
+ (Contributed by Serhiy Storchaka in :issue:`20416` and :issue:`23344`.)
+
+
+Build and C API Changes
+=======================
+
+Changes to Python's build process and to the C API include:
+
+* New ``calloc`` functions:
+
+ * :c:func:`PyMem_RawCalloc`
+ * :c:func:`PyMem_Calloc`
+ * :c:func:`PyObject_Calloc`
+ * :c:func:`_PyObject_GC_Calloc`
+
+
+Deprecated
+==========
+
+Unsupported Operating Systems
+-----------------------------
+
+* None yet.
+
+
+Deprecated Python modules, functions and methods
+------------------------------------------------
+
+* The :mod:`formatter` module has now graduated to full deprecation and is still
+ slated for removal in Python 3.6.
+
+* :mod:`smtpd` has in the past always decoded the DATA portion of email
+ messages using the ``utf-8`` codec. This can now be controlled by the new
+ *decode_data* keyword to :class:`~smtpd.SMTPServer`. The default value is
+ ``True``, but this default is deprecated. Specify the *decode_data* keyword
+ with an appropriate value to avoid the deprecation warning.
+
+
+Deprecated functions and types of the C API
+-------------------------------------------
+
+* None yet.
+
+
+Deprecated features
+-------------------
+
+* None yet.
+
+
+Removed
+=======
+
+API and Feature Removals
+------------------------
+
+The following obsolete and previously deprecated APIs and features have been
+removed:
+
+* The ``__version__`` attribute has been dropped from the email package. The
+ email code hasn't been shipped separately from the stdlib for a long time,
+ and the ``__version__`` string was not updated in the last few releases.
+
+* The internal ``Netrc`` class in the :mod:`ftplib` module was deprecated in
+ 3.4, and has now been removed.
+ (Contributed by Matt Chaput in :issue:`6623`.)
+
+Porting to Python 3.5
+=====================
+
+This section lists previously described changes and other bugfixes
+that may require changes to your code.
+
+Changes in the Python API
+-------------------------
+
+* Before Python 3.5, a :class:`datetime.time` object was considered to be false
+ if it represented midnight in UTC. This behavior was considered obscure and
+ error-prone and has been removed in Python 3.5. See :issue:`13936` for full
+ details.
+
+* :meth:`ssl.SSLSocket.send()` now raises either :exc:`ssl.SSLWantReadError`
+ or :exc:`ssl.SSLWantWriteError` on a non-blocking socket if the operation
+ would block. Previously, it would return 0. See :issue:`20951`.
+
+* The ``__name__`` attribute of generator is now set from the function name,
+ instead of being set from the code name. Use ``gen.gi_code.co_name`` to
+ retrieve the code name. Generators also have a new ``__qualname__``
+ attribute, the qualified name, which is now used for the representation
+ of a generator (``repr(gen)``). See :issue:`21205`.
+
+* The deprecated "strict" mode and argument of :class:`~html.parser.HTMLParser`,
+ :meth:`HTMLParser.error`, and the :exc:`HTMLParserError` exception have been
+ removed. (Contributed by Ezio Melotti in :issue:`15114`.)
+ The *convert_charrefs* argument of :class:`~html.parser.HTMLParser` is
+ now ``True`` by default. (Contributed by Berker Peksag in :issue:`21047`.)
+
+* Although it is not formally part of the API, it is worth noting for porting
+ purposes (ie: fixing tests) that error messages that were previously of the
+ form "'sometype' does not support the buffer protocol" are now of the form "a
+ bytes-like object is required, not 'sometype'". (Contributed by Ezio Melotti
+ in :issue:`16518`.)
+
+* If the current directory is set to a directory that no longer exists then
+ :exc:`FileNotFoundError` will no longer be raised and instead
+ :meth:`~importlib.machinery.FileFinder.find_spec` will return ``None``
+ **without** caching ``None`` in :data:`sys.path_importer_cache` which is
+ different than the typical case (:issue:`22834`).
+
+* HTTP status code and messages from :mod:`http.client` and :mod:`http.server`
+ were refactored into a common :class:`~http.HTTPStatus` enum. The values in
+ :mod:`http.client` and :mod:`http.server` remain available for backwards
+ compatibility. (Contributed by Demian Brecht in :issue:`21793`.)
+
+* When an import loader defines :meth:`~importlib.machinery.Loader.exec_module`
+ it is now expected to also define
+ :meth:`~importlib.machinery.Loader.create_module` (raises a
+ :exc:`DeprecationWarning` now, will be an error in Python 3.6). If the loader
+ inherits from :class:`importlib.abc.Loader` then there is nothing to do, else
+ simply define :meth:`~importlib.machinery.Loader.create_module` to return
+ ``None`` (:issue:`23014`).
+
+* :func:`re.split` always ignored empty pattern matches, so the ``'x*'``
+ pattern worked the same as ``'x+'``, and the ``'\b'`` pattern never worked.
+ Now :func:`re.split` raises a warning if the pattern could match
+ an empty string. For compatibility use patterns that never match an empty
+ string (e.g. ``'x+'`` instead of ``'x*'``). Patterns that could only match
+ an empty string (such as ``'\b'``) now raise an error.
+
+Changes in the C API
+--------------------
+
+* The undocumented :c:member:`~PyMemoryViewObject.format` member of the
+ (non-public) :c:type:`PyMemoryViewObject` structure has been removed.
+
+ All extensions relying on the relevant parts in ``memoryobject.h``
+ must be rebuilt.
+
+* The :c:type:`PyMemAllocator` structure was renamed to
+ :c:type:`PyMemAllocatorEx` and a new ``calloc`` field was added.
+
+* Removed non-documented macro :c:macro:`PyObject_REPR` which leaked references.
+ Use format character ``%R`` in :c:func:`PyUnicode_FromFormat`-like functions
+ to format the :func:`repr` of the object.
diff --git a/Doc/whatsnew/index.rst b/Doc/whatsnew/index.rst
index 29902e4..edb5502 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.5.rst
3.4.rst
3.3.rst
3.2.rst
diff --git a/Grammar/Grammar b/Grammar/Grammar
index d7aaffd..6c012c0 100644
--- a/Grammar/Grammar
+++ b/Grammar/Grammar
@@ -40,7 +40,7 @@ small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
('=' (yield_expr|testlist_star_expr))*)
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
-augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
+augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
'<<=' | '>>=' | '**=' | '//=')
# For normal assignments, additional restrictions enforced by the interpreter
del_stmt: 'del' exprlist
@@ -89,7 +89,7 @@ and_test: not_test ('and' not_test)*
not_test: 'not' not_test | comparison
comparison: expr (comp_op expr)*
# <> isn't actually a valid comparison operator in Python. It's here for the
-# sake of a __future__ import described in PEP 401
+# sake of a __future__ import described in PEP 401 (which really works :-)
comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
star_expr: '*' expr
expr: xor_expr ('|' xor_expr)*
@@ -97,7 +97,7 @@ xor_expr: and_expr ('^' and_expr)*
and_expr: shift_expr ('&' shift_expr)*
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
arith_expr: term (('+'|'-') term)*
-term: factor (('*'|'/'|'%'|'//') factor)*
+term: factor (('*'|'@'|'/'|'%'|'//') factor)*
factor: ('+'|'-'|'~') factor | power
power: atom trailer* ['**' factor]
atom: ('(' [yield_expr|testlist_comp] ')' |
diff --git a/Include/Python-ast.h b/Include/Python-ast.h
index 67d677b..37e9a60 100644
--- a/Include/Python-ast.h
+++ b/Include/Python-ast.h
@@ -15,9 +15,9 @@ typedef struct _slice *slice_ty;
typedef enum _boolop { And=1, Or=2 } boolop_ty;
-typedef enum _operator { Add=1, Sub=2, Mult=3, Div=4, Mod=5, Pow=6, LShift=7,
- RShift=8, BitOr=9, BitXor=10, BitAnd=11, FloorDiv=12 }
- operator_ty;
+typedef enum _operator { Add=1, Sub=2, Mult=3, MatMult=4, Div=5, Mod=6, Pow=7,
+ LShift=8, RShift=9, BitOr=10, BitXor=11, BitAnd=12,
+ FloorDiv=13 } operator_ty;
typedef enum _unaryop { Invert=1, Not=2, UAdd=3, USub=4 } unaryop_ty;
diff --git a/Include/Python.h b/Include/Python.h
index 2dd8290..46d2ece 100644
--- a/Include/Python.h
+++ b/Include/Python.h
@@ -112,6 +112,7 @@
#include "pyarena.h"
#include "modsupport.h"
#include "pythonrun.h"
+#include "pylifecycle.h"
#include "ceval.h"
#include "sysmodule.h"
#include "intrcheck.h"
diff --git a/Include/abstract.h b/Include/abstract.h
index 6e850b8..db70f21 100644
--- a/Include/abstract.h
+++ b/Include/abstract.h
@@ -658,6 +658,12 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
o1*o2.
*/
+ PyAPI_FUNC(PyObject *) PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2);
+
+ /*
+ This is the equivalent of the Python expression: o1 @ o2.
+ */
+
PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
/*
@@ -832,6 +838,12 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
o1 *= o2.
*/
+ PyAPI_FUNC(PyObject *) PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2);
+
+ /*
+ This is the equivalent of the Python expression: o1 @= o2.
+ */
+
PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
PyObject *o2);
diff --git a/Include/bytes_methods.h b/Include/bytes_methods.h
index 1498b8f..11d5f42 100644
--- a/Include/bytes_methods.h
+++ b/Include/bytes_methods.h
@@ -21,8 +21,8 @@ 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);
-/* This one gets the raw argument list. */
-extern PyObject* _Py_bytes_maketrans(PyObject *args);
+/* The maketrans() static method. */
+extern PyObject* _Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to);
/* Shared __doc__ strings. */
extern const char _Py_isspace__doc__[];
diff --git a/Include/bytesobject.h b/Include/bytesobject.h
index 0ee8d36..e379bac 100644
--- a/Include/bytesobject.h
+++ b/Include/bytesobject.h
@@ -62,6 +62,7 @@ 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 *);
#endif
PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
const char *, Py_ssize_t,
diff --git a/Include/code.h b/Include/code.h
index 7c7e5bf..ff2b97e 100644
--- a/Include/code.h
+++ b/Include/code.h
@@ -21,7 +21,12 @@ typedef struct {
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 doesn't count for hash or comparisons */
+ /* 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
+ 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) */
diff --git a/Include/codecs.h b/Include/codecs.h
index b3088e4..4669135 100644
--- a/Include/codecs.h
+++ b/Include/codecs.h
@@ -225,6 +225,9 @@ 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);
+/* replace the unicode encode error with backslash escapes (\N, \x, \u and \U) */
+PyAPI_FUNC(PyObject *) PyCodec_NameReplaceErrors(PyObject *exc);
+
PyAPI_DATA(const char *) Py_hexdigits;
#ifdef __cplusplus
diff --git a/Include/complexobject.h b/Include/complexobject.h
index 1934f3b..cb8c52c 100644
--- a/Include/complexobject.h
+++ b/Include/complexobject.h
@@ -14,21 +14,13 @@ typedef struct {
/* Operations on complex numbers from complexmodule.c */
-#define c_sum _Py_c_sum
-#define c_diff _Py_c_diff
-#define c_neg _Py_c_neg
-#define c_prod _Py_c_prod
-#define c_quot _Py_c_quot
-#define c_pow _Py_c_pow
-#define c_abs _Py_c_abs
-
-PyAPI_FUNC(Py_complex) c_sum(Py_complex, Py_complex);
-PyAPI_FUNC(Py_complex) c_diff(Py_complex, Py_complex);
-PyAPI_FUNC(Py_complex) c_neg(Py_complex);
-PyAPI_FUNC(Py_complex) c_prod(Py_complex, Py_complex);
-PyAPI_FUNC(Py_complex) c_quot(Py_complex, Py_complex);
-PyAPI_FUNC(Py_complex) c_pow(Py_complex, Py_complex);
-PyAPI_FUNC(double) c_abs(Py_complex);
+PyAPI_FUNC(Py_complex) _Py_c_sum(Py_complex, Py_complex);
+PyAPI_FUNC(Py_complex) _Py_c_diff(Py_complex, Py_complex);
+PyAPI_FUNC(Py_complex) _Py_c_neg(Py_complex);
+PyAPI_FUNC(Py_complex) _Py_c_prod(Py_complex, Py_complex);
+PyAPI_FUNC(Py_complex) _Py_c_quot(Py_complex, Py_complex);
+PyAPI_FUNC(Py_complex) _Py_c_pow(Py_complex, Py_complex);
+PyAPI_FUNC(double) _Py_c_abs(Py_complex);
#endif
/* Complex object interface */
diff --git a/Include/dictobject.h b/Include/dictobject.h
index ef122bd..09dff59 100644
--- a/Include/dictobject.h
+++ b/Include/dictobject.h
@@ -50,6 +50,10 @@ PyAPI_DATA(PyTypeObject) PyDictValues_Type;
PyAPI_FUNC(PyObject *) PyDict_New(void);
PyAPI_FUNC(PyObject *) PyDict_GetItem(PyObject *mp, PyObject *key);
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
+ Py_hash_t hash);
+#endif
PyAPI_FUNC(PyObject *) PyDict_GetItemWithError(PyObject *mp, PyObject *key);
PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
struct _Py_Identifier *key);
@@ -58,6 +62,10 @@ PyAPI_FUNC(PyObject *) PyDict_SetDefault(
PyObject *mp, PyObject *key, PyObject *defaultobj);
#endif
PyAPI_FUNC(int) PyDict_SetItem(PyObject *mp, PyObject *key, PyObject *item);
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,
+ PyObject *item, Py_hash_t hash);
+#endif
PyAPI_FUNC(int) PyDict_DelItem(PyObject *mp, PyObject *key);
PyAPI_FUNC(void) PyDict_Clear(PyObject *mp);
PyAPI_FUNC(int) PyDict_Next(
diff --git a/Include/fileutils.h b/Include/fileutils.h
index e9bad80..c5eebc5 100644
--- a/Include/fileutils.h
+++ b/Include/fileutils.h
@@ -7,11 +7,11 @@ extern "C" {
PyAPI_FUNC(PyObject *) _Py_device_encoding(int);
-PyAPI_FUNC(wchar_t *) _Py_char2wchar(
+PyAPI_FUNC(wchar_t *) Py_DecodeLocale(
const char *arg,
size_t *size);
-PyAPI_FUNC(char*) _Py_wchar2char(
+PyAPI_FUNC(char*) Py_EncodeLocale(
const wchar_t *text,
size_t *error_pos);
@@ -70,7 +70,14 @@ PyAPI_FUNC(int) _Py_set_inheritable(int fd, int inheritable,
int *atomic_flag_works);
PyAPI_FUNC(int) _Py_dup(int fd);
-#endif
+
+#ifndef MS_WINDOWS
+PyAPI_FUNC(int) _Py_get_blocking(int fd);
+
+PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
+#endif /* !MS_WINDOWS */
+
+#endif /* Py_LIMITED_API */
#ifdef __cplusplus
}
diff --git a/Include/genobject.h b/Include/genobject.h
index 65f1ecf..23571e6 100644
--- a/Include/genobject.h
+++ b/Include/genobject.h
@@ -25,6 +25,12 @@ typedef struct {
/* List of weak reference. */
PyObject *gi_weakreflist;
+
+ /* Name of the generator. */
+ PyObject *gi_name;
+
+ /* Qualified name of the generator. */
+ PyObject *gi_qualname;
} PyGenObject;
PyAPI_DATA(PyTypeObject) PyGen_Type;
@@ -33,6 +39,8 @@ PyAPI_DATA(PyTypeObject) PyGen_Type;
#define PyGen_CheckExact(op) (Py_TYPE(op) == &PyGen_Type)
PyAPI_FUNC(PyObject *) PyGen_New(struct _frame *);
+PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(struct _frame *,
+ PyObject *name, PyObject *qualname);
PyAPI_FUNC(int) PyGen_NeedsFinalizing(PyGenObject *);
PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **);
PyObject *_PyGen_Send(PyGenObject *, PyObject *);
diff --git a/Include/memoryobject.h b/Include/memoryobject.h
index 382ca92..ab5ee09 100644
--- a/Include/memoryobject.h
+++ b/Include/memoryobject.h
@@ -45,9 +45,6 @@ typedef struct {
} _PyManagedBufferObject;
-/* deprecated, removed in 3.5 */
-#define _Py_MEMORYVIEW_MAX_FORMAT 3 /* must be >= 3 */
-
/* memoryview state flags */
#define _Py_MEMORYVIEW_RELEASED 0x001 /* access to master buffer blocked */
#define _Py_MEMORYVIEW_C 0x002 /* C-contiguous layout */
@@ -62,7 +59,6 @@ typedef struct {
int flags; /* state flags */
Py_ssize_t exports; /* number of buffer re-exports */
Py_buffer view; /* private copy of the exporter's view */
- char format[_Py_MEMORYVIEW_MAX_FORMAT]; /* deprecated, removed in 3.5 */
PyObject *weakreflist;
Py_ssize_t ob_array[1]; /* shape, strides, suboffsets */
} PyMemoryViewObject;
diff --git a/Include/methodobject.h b/Include/methodobject.h
index 3cc2ea9..0236228 100644
--- a/Include/methodobject.h
+++ b/Include/methodobject.h
@@ -77,6 +77,7 @@ typedef struct {
PyMethodDef *m_ml; /* Description of the C function to call */
PyObject *m_self; /* Passed as 'self' arg to the C func, can be NULL */
PyObject *m_module; /* The __module__ attribute, can be anything */
+ PyObject *m_weakreflist; /* List of weak references */
} PyCFunctionObject;
#endif
diff --git a/Include/object.h b/Include/object.h
index 9306288..e29608a 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -65,6 +65,7 @@ whose size is determined when the object is allocated.
#error Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG
#endif
+
#ifdef Py_TRACE_REFS
/* Define pointers to support a doubly-linked list of all live heap objects. */
#define _PyObject_HEAD_EXTRA \
@@ -275,6 +276,9 @@ typedef struct {
binaryfunc nb_inplace_true_divide;
unaryfunc nb_index;
+
+ binaryfunc nb_matrix_multiply;
+ binaryfunc nb_inplace_matrix_multiply;
} PyNumberMethods;
typedef struct {
@@ -572,13 +576,6 @@ PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
-#ifndef Py_LIMITED_API
-/* Helper for passing objects to printf and the like.
- Leaks refcounts. Don't use it!
-*/
-#define PyObject_REPR(obj) PyUnicode_AsUTF8(PyObject_Repr(obj))
-#endif
-
/* Flag bits for printing: */
#define Py_PRINT_RAW 1 /* No string quotes etc. */
@@ -714,11 +711,17 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
_Py_NegativeRefcount(__FILE__, __LINE__, \
(PyObject *)(OP)); \
}
+/* Py_REF_DEBUG also controls the display of refcounts and memory block
+ * allocations at the interactive prompt and at interpreter shutdown
+ */
+PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void);
+#define _PY_DEBUG_PRINT_TOTAL_REFS() _PyDebug_PrintTotalRefs()
#else
#define _Py_INC_REFTOTAL
#define _Py_DEC_REFTOTAL
#define _Py_REF_DEBUG_COMMA
#define _Py_CHECK_REFCNT(OP) /* a semicolon */;
+#define _PY_DEBUG_PRINT_TOTAL_REFS()
#endif /* Py_REF_DEBUG */
#ifdef COUNT_ALLOCS
diff --git a/Include/objimpl.h b/Include/objimpl.h
index 3f21b70..65b6d91 100644
--- a/Include/objimpl.h
+++ b/Include/objimpl.h
@@ -95,6 +95,7 @@ PyObject_{New, NewVar, Del}.
the raw memory.
*/
PyAPI_FUNC(void *) PyObject_Malloc(size_t size);
+PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);
PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
PyAPI_FUNC(void) PyObject_Free(void *ptr);
@@ -321,7 +322,8 @@ extern PyGC_Head *_PyGC_generation0;
(!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
#endif /* Py_LIMITED_API */
-PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t);
+PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
+PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
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/opcode.h b/Include/opcode.h
index 0936f2d..0638b54 100644
--- a/Include/opcode.h
+++ b/Include/opcode.h
@@ -1,3 +1,4 @@
+/* Auto-generated by Tools/scripts/generate_opcode_h.py */
#ifndef Py_OPCODE_H
#define Py_OPCODE_H
#ifdef __cplusplus
@@ -5,141 +6,111 @@ extern "C" {
#endif
-/* Instruction opcodes for compiled code */
-
-#define POP_TOP 1
-#define ROT_TWO 2
-#define ROT_THREE 3
-#define DUP_TOP 4
-#define DUP_TOP_TWO 5
-#define NOP 9
-
-#define UNARY_POSITIVE 10
-#define UNARY_NEGATIVE 11
-#define UNARY_NOT 12
-
-#define UNARY_INVERT 15
-
-#define BINARY_POWER 19
-
-#define BINARY_MULTIPLY 20
-
-#define BINARY_MODULO 22
-#define BINARY_ADD 23
-#define BINARY_SUBTRACT 24
-#define BINARY_SUBSCR 25
-#define BINARY_FLOOR_DIVIDE 26
-#define BINARY_TRUE_DIVIDE 27
-#define INPLACE_FLOOR_DIVIDE 28
-#define INPLACE_TRUE_DIVIDE 29
-
-#define STORE_MAP 54
-#define INPLACE_ADD 55
-#define INPLACE_SUBTRACT 56
-#define INPLACE_MULTIPLY 57
-
-#define INPLACE_MODULO 59
-#define STORE_SUBSCR 60
-#define DELETE_SUBSCR 61
-
-#define BINARY_LSHIFT 62
-#define BINARY_RSHIFT 63
-#define BINARY_AND 64
-#define BINARY_XOR 65
-#define BINARY_OR 66
-#define INPLACE_POWER 67
-#define GET_ITER 68
-#define PRINT_EXPR 70
-#define LOAD_BUILD_CLASS 71
-#define YIELD_FROM 72
-
-#define INPLACE_LSHIFT 75
-#define INPLACE_RSHIFT 76
-#define INPLACE_AND 77
-#define INPLACE_XOR 78
-#define INPLACE_OR 79
-#define BREAK_LOOP 80
-#define WITH_CLEANUP 81
-
-#define RETURN_VALUE 83
-#define IMPORT_STAR 84
-
-#define YIELD_VALUE 86
-#define POP_BLOCK 87
-#define END_FINALLY 88
-#define POP_EXCEPT 89
-
-#define HAVE_ARGUMENT 90 /* Opcodes from here have an argument: */
-
-#define STORE_NAME 90 /* Index in name list */
-#define DELETE_NAME 91 /* "" */
-#define UNPACK_SEQUENCE 92 /* Number of sequence items */
-#define FOR_ITER 93
-#define UNPACK_EX 94 /* Num items before variable part +
- (Num items after variable part << 8) */
-
-#define STORE_ATTR 95 /* Index in name list */
-#define DELETE_ATTR 96 /* "" */
-#define STORE_GLOBAL 97 /* "" */
-#define DELETE_GLOBAL 98 /* "" */
-
-#define LOAD_CONST 100 /* Index in const list */
-#define LOAD_NAME 101 /* Index in name list */
-#define BUILD_TUPLE 102 /* Number of tuple items */
-#define BUILD_LIST 103 /* Number of list items */
-#define BUILD_SET 104 /* Number of set items */
-#define BUILD_MAP 105 /* Always zero for now */
-#define LOAD_ATTR 106 /* Index in name list */
-#define COMPARE_OP 107 /* Comparison operator */
-#define IMPORT_NAME 108 /* Index in name list */
-#define IMPORT_FROM 109 /* Index in name list */
-
-#define JUMP_FORWARD 110 /* Number of bytes to skip */
-#define JUMP_IF_FALSE_OR_POP 111 /* Target byte offset from beginning of code */
-#define JUMP_IF_TRUE_OR_POP 112 /* "" */
-#define JUMP_ABSOLUTE 113 /* "" */
-#define POP_JUMP_IF_FALSE 114 /* "" */
-#define POP_JUMP_IF_TRUE 115 /* "" */
-
-#define LOAD_GLOBAL 116 /* Index in name list */
-
-#define CONTINUE_LOOP 119 /* Start of loop (absolute) */
-#define SETUP_LOOP 120 /* Target address (relative) */
-#define SETUP_EXCEPT 121 /* "" */
-#define SETUP_FINALLY 122 /* "" */
-
-#define LOAD_FAST 124 /* Local variable number */
-#define STORE_FAST 125 /* Local variable number */
-#define DELETE_FAST 126 /* Local variable number */
-
-#define RAISE_VARARGS 130 /* Number of raise arguments (1, 2 or 3) */
-/* CALL_FUNCTION_XXX opcodes defined below depend on this definition */
-#define CALL_FUNCTION 131 /* #args + (#kwargs<<8) */
-#define MAKE_FUNCTION 132 /* #defaults + #kwdefaults<<8 + #annotations<<16 */
-#define BUILD_SLICE 133 /* Number of items */
-
-#define MAKE_CLOSURE 134 /* same as MAKE_FUNCTION */
-#define LOAD_CLOSURE 135 /* Load free variable from closure */
-#define LOAD_DEREF 136 /* Load and dereference from closure cell */
-#define STORE_DEREF 137 /* Store into cell */
-#define DELETE_DEREF 138 /* Delete closure cell */
-
-/* The next 3 opcodes must be contiguous and satisfy
- (CALL_FUNCTION_VAR - CALL_FUNCTION) & 3 == 1 */
-#define CALL_FUNCTION_VAR 140 /* #args + (#kwargs<<8) */
-#define CALL_FUNCTION_KW 141 /* #args + (#kwargs<<8) */
-#define CALL_FUNCTION_VAR_KW 142 /* #args + (#kwargs<<8) */
-
-#define SETUP_WITH 143
-
-/* Support for opargs more than 16 bits long */
-#define EXTENDED_ARG 144
-
-#define LIST_APPEND 145
-#define SET_ADD 146
-#define MAP_ADD 147
-
-#define LOAD_CLASSDEREF 148
+ /* Instruction opcodes for compiled code */
+#define POP_TOP 1
+#define ROT_TWO 2
+#define ROT_THREE 3
+#define DUP_TOP 4
+#define DUP_TOP_TWO 5
+#define NOP 9
+#define UNARY_POSITIVE 10
+#define UNARY_NEGATIVE 11
+#define UNARY_NOT 12
+#define UNARY_INVERT 15
+#define BINARY_MATRIX_MULTIPLY 16
+#define INPLACE_MATRIX_MULTIPLY 17
+#define BINARY_POWER 19
+#define BINARY_MULTIPLY 20
+#define BINARY_MODULO 22
+#define BINARY_ADD 23
+#define BINARY_SUBTRACT 24
+#define BINARY_SUBSCR 25
+#define BINARY_FLOOR_DIVIDE 26
+#define BINARY_TRUE_DIVIDE 27
+#define INPLACE_FLOOR_DIVIDE 28
+#define INPLACE_TRUE_DIVIDE 29
+#define STORE_MAP 54
+#define INPLACE_ADD 55
+#define INPLACE_SUBTRACT 56
+#define INPLACE_MULTIPLY 57
+#define INPLACE_MODULO 59
+#define STORE_SUBSCR 60
+#define DELETE_SUBSCR 61
+#define BINARY_LSHIFT 62
+#define BINARY_RSHIFT 63
+#define BINARY_AND 64
+#define BINARY_XOR 65
+#define BINARY_OR 66
+#define INPLACE_POWER 67
+#define GET_ITER 68
+#define PRINT_EXPR 70
+#define LOAD_BUILD_CLASS 71
+#define YIELD_FROM 72
+#define INPLACE_LSHIFT 75
+#define INPLACE_RSHIFT 76
+#define INPLACE_AND 77
+#define INPLACE_XOR 78
+#define INPLACE_OR 79
+#define BREAK_LOOP 80
+#define WITH_CLEANUP 81
+#define RETURN_VALUE 83
+#define IMPORT_STAR 84
+#define YIELD_VALUE 86
+#define POP_BLOCK 87
+#define END_FINALLY 88
+#define POP_EXCEPT 89
+#define HAVE_ARGUMENT 90
+#define STORE_NAME 90
+#define DELETE_NAME 91
+#define UNPACK_SEQUENCE 92
+#define FOR_ITER 93
+#define UNPACK_EX 94
+#define STORE_ATTR 95
+#define DELETE_ATTR 96
+#define STORE_GLOBAL 97
+#define DELETE_GLOBAL 98
+#define LOAD_CONST 100
+#define LOAD_NAME 101
+#define BUILD_TUPLE 102
+#define BUILD_LIST 103
+#define BUILD_SET 104
+#define BUILD_MAP 105
+#define LOAD_ATTR 106
+#define COMPARE_OP 107
+#define IMPORT_NAME 108
+#define IMPORT_FROM 109
+#define JUMP_FORWARD 110
+#define JUMP_IF_FALSE_OR_POP 111
+#define JUMP_IF_TRUE_OR_POP 112
+#define JUMP_ABSOLUTE 113
+#define POP_JUMP_IF_FALSE 114
+#define POP_JUMP_IF_TRUE 115
+#define LOAD_GLOBAL 116
+#define CONTINUE_LOOP 119
+#define SETUP_LOOP 120
+#define SETUP_EXCEPT 121
+#define SETUP_FINALLY 122
+#define LOAD_FAST 124
+#define STORE_FAST 125
+#define DELETE_FAST 126
+#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 SETUP_WITH 143
+#define EXTENDED_ARG 144
+#define LIST_APPEND 145
+#define SET_ADD 146
+#define MAP_ADD 147
+#define LOAD_CLASSDEREF 148
/* 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
@@ -148,8 +119,9 @@ extern "C" {
#define EXCEPT_HANDLER 257
-enum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE, PyCmp_GT=Py_GT, PyCmp_GE=Py_GE,
- PyCmp_IN, PyCmp_NOT_IN, PyCmp_IS, PyCmp_IS_NOT, PyCmp_EXC_MATCH, PyCmp_BAD};
+enum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE,
+ PyCmp_GT=Py_GT, PyCmp_GE=Py_GE, PyCmp_IN, PyCmp_NOT_IN,
+ PyCmp_IS, PyCmp_IS_NOT, PyCmp_EXC_MATCH, PyCmp_BAD};
#define HAS_ARG(op) ((op) >= HAVE_ARGUMENT)
diff --git a/Include/osdefs.h b/Include/osdefs.h
index 0c2e34b..bd84c1c 100644
--- a/Include/osdefs.h
+++ b/Include/osdefs.h
@@ -7,15 +7,12 @@ extern "C" {
/* Operating system dependencies */
-/* Mod by chrish: QNX has WATCOM, but isn't DOS */
-#if !defined(__QNX__)
-#if defined(MS_WINDOWS) || defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__DJGPP__)
+#ifdef MS_WINDOWS
#define SEP L'\\'
#define ALTSEP L'/'
#define MAXPATHLEN 256
#define DELIM L';'
#endif
-#endif
/* Filename separator */
#ifndef SEP
diff --git a/Include/patchlevel.h b/Include/patchlevel.h
index e68274b..ea0c042 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 4
-#define PY_MICRO_VERSION 3
-#define PY_RELEASE_LEVEL PY_RELEASE_LEVEL_GAMMA
+#define PY_MINOR_VERSION 5
+#define PY_MICRO_VERSION 0
+#define PY_RELEASE_LEVEL PY_RELEASE_LEVEL_ALPHA
#define PY_RELEASE_SERIAL 1
/* Version as a string */
-#define PY_VERSION "3.4.3rc1+"
+#define PY_VERSION "3.5.0a1+"
/*--end constants--*/
/* Version as a single 4-byte hex number, e.g. 0x010502B2 == 1.5.2b2.
diff --git a/Include/pyatomic.h b/Include/pyatomic.h
index d4e19e0..80bd825 100644
--- a/Include/pyatomic.h
+++ b/Include/pyatomic.h
@@ -1,12 +1,15 @@
#ifndef Py_LIMITED_API
#ifndef Py_ATOMIC_H
#define Py_ATOMIC_H
-/* XXX: When compilers start offering a stdatomic.h with lock-free
- atomic_int and atomic_address types, include that here and rewrite
- the atomic operations in terms of it. */
#include "dynamic_annotations.h"
+#include "pyconfig.h"
+
+#if defined(HAVE_STD_ATOMIC)
+#include <stdatomic.h>
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -20,6 +23,76 @@ extern "C" {
* Beware, the implementations here are deep magic.
*/
+#if defined(HAVE_STD_ATOMIC)
+
+typedef enum _Py_memory_order {
+ _Py_memory_order_relaxed = memory_order_relaxed,
+ _Py_memory_order_acquire = memory_order_acquire,
+ _Py_memory_order_release = memory_order_release,
+ _Py_memory_order_acq_rel = memory_order_acq_rel,
+ _Py_memory_order_seq_cst = memory_order_seq_cst
+} _Py_memory_order;
+
+typedef struct _Py_atomic_address {
+ _Atomic void *_value;
+} _Py_atomic_address;
+
+typedef struct _Py_atomic_int {
+ atomic_int _value;
+} _Py_atomic_int;
+
+#define _Py_atomic_signal_fence(/*memory_order*/ ORDER) \
+ atomic_signal_fence(ORDER)
+
+#define _Py_atomic_thread_fence(/*memory_order*/ ORDER) \
+ atomic_thread_fence(ORDER)
+
+#define _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL, ORDER) \
+ atomic_store_explicit(&(ATOMIC_VAL)->_value, NEW_VAL, ORDER)
+
+#define _Py_atomic_load_explicit(ATOMIC_VAL, ORDER) \
+ atomic_load_explicit(&(ATOMIC_VAL)->_value, ORDER)
+
+/* Use builtin atomic operations in GCC >= 4.7 */
+#elif defined(HAVE_BUILTIN_ATOMIC)
+
+typedef enum _Py_memory_order {
+ _Py_memory_order_relaxed = __ATOMIC_RELAXED,
+ _Py_memory_order_acquire = __ATOMIC_ACQUIRE,
+ _Py_memory_order_release = __ATOMIC_RELEASE,
+ _Py_memory_order_acq_rel = __ATOMIC_ACQ_REL,
+ _Py_memory_order_seq_cst = __ATOMIC_SEQ_CST
+} _Py_memory_order;
+
+typedef struct _Py_atomic_address {
+ void *_value;
+} _Py_atomic_address;
+
+typedef struct _Py_atomic_int {
+ int _value;
+} _Py_atomic_int;
+
+#define _Py_atomic_signal_fence(/*memory_order*/ ORDER) \
+ __atomic_signal_fence(ORDER)
+
+#define _Py_atomic_thread_fence(/*memory_order*/ ORDER) \
+ __atomic_thread_fence(ORDER)
+
+#define _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL, ORDER) \
+ (assert((ORDER) == __ATOMIC_RELAXED \
+ || (ORDER) == __ATOMIC_SEQ_CST \
+ || (ORDER) == __ATOMIC_RELEASE), \
+ __atomic_store_n(&(ATOMIC_VAL)->_value, NEW_VAL, ORDER))
+
+#define _Py_atomic_load_explicit(ATOMIC_VAL, ORDER) \
+ (assert((ORDER) == __ATOMIC_RELAXED \
+ || (ORDER) == __ATOMIC_SEQ_CST \
+ || (ORDER) == __ATOMIC_ACQUIRE \
+ || (ORDER) == __ATOMIC_CONSUME), \
+ __atomic_load_n(&(ATOMIC_VAL)->_value, ORDER))
+
+#else
+
typedef enum _Py_memory_order {
_Py_memory_order_relaxed,
_Py_memory_order_acquire,
@@ -162,6 +235,7 @@ _Py_ANNOTATE_MEMORY_ORDER(const volatile void *address, _Py_memory_order order)
((ATOMIC_VAL)->_value)
#endif /* !gcc x86 */
+#endif
/* Standardized shortcuts. */
#define _Py_atomic_store(ATOMIC_VAL, NEW_VAL) \
diff --git a/Include/pydebug.h b/Include/pydebug.h
index 8fe9818..19bec2b 100644
--- a/Include/pydebug.h
+++ b/Include/pydebug.h
@@ -5,6 +5,8 @@
extern "C" {
#endif
+/* These global variable are defined in pylifecycle.c */
+/* XXX (ncoghlan): move these declarations to pylifecycle.h? */
PyAPI_DATA(int) Py_DebugFlag;
PyAPI_DATA(int) Py_VerboseFlag;
PyAPI_DATA(int) Py_QuietFlag;
diff --git a/Include/pyerrors.h b/Include/pyerrors.h
index 02f65d6..8a7e4f4 100644
--- a/Include/pyerrors.h
+++ b/Include/pyerrors.h
@@ -99,6 +99,7 @@ PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject *, PyObject *, PyObject *);
#define _Py_NO_RETURN
#endif
+/* Defined in Python/pylifecycle.c */
PyAPI_FUNC(void) Py_FatalError(const char *message) _Py_NO_RETURN;
#if defined(Py_DEBUG) || defined(Py_LIMITED_API)
@@ -244,6 +245,12 @@ PyAPI_FUNC(PyObject *) PyErr_Format(
const char *format, /* ASCII-encoded string */
...
);
+#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
+PyAPI_FUNC(PyObject *) PyErr_FormatV(
+ PyObject *exception,
+ const char *format,
+ va_list vargs);
+#endif
#ifdef MS_WINDOWS
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
diff --git a/Include/pylifecycle.h b/Include/pylifecycle.h
new file mode 100644
index 0000000..ccdebe2
--- /dev/null
+++ b/Include/pylifecycle.h
@@ -0,0 +1,124 @@
+
+/* Interfaces to configure, query, create & destroy the Python runtime */
+
+#ifndef Py_PYLIFECYCLE_H
+#define Py_PYLIFECYCLE_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+PyAPI_FUNC(void) Py_SetProgramName(wchar_t *);
+PyAPI_FUNC(wchar_t *) Py_GetProgramName(void);
+
+PyAPI_FUNC(void) Py_SetPythonHome(wchar_t *);
+PyAPI_FUNC(wchar_t *) Py_GetPythonHome(void);
+
+#ifndef Py_LIMITED_API
+/* Only used by applications that embed the interpreter and need to
+ * override the standard encoding determination mechanism
+ */
+PyAPI_FUNC(int) Py_SetStandardStreamEncoding(const char *encoding,
+ const char *errors);
+#endif
+
+PyAPI_FUNC(void) Py_Initialize(void);
+PyAPI_FUNC(void) Py_InitializeEx(int);
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(void) _Py_InitializeEx_Private(int, int);
+#endif
+PyAPI_FUNC(void) Py_Finalize(void);
+PyAPI_FUNC(int) Py_IsInitialized(void);
+PyAPI_FUNC(PyThreadState *) Py_NewInterpreter(void);
+PyAPI_FUNC(void) Py_EndInterpreter(PyThreadState *);
+
+
+/* Py_PyAtExit is for the atexit module, Py_AtExit is for low-level
+ * exit functions.
+ */
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(void) _Py_PyAtExit(void (*func)(void));
+#endif
+PyAPI_FUNC(int) Py_AtExit(void (*func)(void));
+
+PyAPI_FUNC(void) Py_Exit(int);
+
+/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. */
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(void) _Py_RestoreSignals(void);
+
+PyAPI_FUNC(int) Py_FdIsInteractive(FILE *, const char *);
+#endif
+
+/* Bootstrap __main__ (defined in Modules/main.c) */
+PyAPI_FUNC(int) Py_Main(int argc, wchar_t **argv);
+
+/* In getpath.c */
+PyAPI_FUNC(wchar_t *) Py_GetProgramFullPath(void);
+PyAPI_FUNC(wchar_t *) Py_GetPrefix(void);
+PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
+PyAPI_FUNC(wchar_t *) Py_GetPath(void);
+PyAPI_FUNC(void) Py_SetPath(const wchar_t *);
+#ifdef MS_WINDOWS
+int _Py_CheckPython3();
+#endif
+
+/* In their own files */
+PyAPI_FUNC(const char *) Py_GetVersion(void);
+PyAPI_FUNC(const char *) Py_GetPlatform(void);
+PyAPI_FUNC(const char *) Py_GetCopyright(void);
+PyAPI_FUNC(const char *) Py_GetCompiler(void);
+PyAPI_FUNC(const char *) Py_GetBuildInfo(void);
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(const char *) _Py_hgidentifier(void);
+PyAPI_FUNC(const char *) _Py_hgversion(void);
+#endif
+
+/* Internal -- various one-time initializations */
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void);
+PyAPI_FUNC(PyObject *) _PySys_Init(void);
+PyAPI_FUNC(void) _PyImport_Init(void);
+PyAPI_FUNC(void) _PyExc_Init(PyObject * bltinmod);
+PyAPI_FUNC(void) _PyImportHooks_Init(void);
+PyAPI_FUNC(int) _PyFrame_Init(void);
+PyAPI_FUNC(int) _PyFloat_Init(void);
+PyAPI_FUNC(int) PyByteArray_Init(void);
+PyAPI_FUNC(void) _PyRandom_Init(void);
+#endif
+
+/* Various internal finalizers */
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(void) _PyExc_Fini(void);
+PyAPI_FUNC(void) _PyImport_Fini(void);
+PyAPI_FUNC(void) PyMethod_Fini(void);
+PyAPI_FUNC(void) PyFrame_Fini(void);
+PyAPI_FUNC(void) PyCFunction_Fini(void);
+PyAPI_FUNC(void) PyDict_Fini(void);
+PyAPI_FUNC(void) PyTuple_Fini(void);
+PyAPI_FUNC(void) PyList_Fini(void);
+PyAPI_FUNC(void) PySet_Fini(void);
+PyAPI_FUNC(void) PyBytes_Fini(void);
+PyAPI_FUNC(void) PyByteArray_Fini(void);
+PyAPI_FUNC(void) PyFloat_Fini(void);
+PyAPI_FUNC(void) PyOS_FiniInterrupts(void);
+PyAPI_FUNC(void) _PyGC_DumpShutdownStats(void);
+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_DATA(PyThreadState *) _Py_Finalizing;
+#endif
+
+/* Signals */
+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);
+
+/* Random */
+PyAPI_FUNC(int) _PyOS_URandom (void *buffer, Py_ssize_t size);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_PYLIFECYCLE_H */
diff --git a/Include/pymacro.h b/Include/pymacro.h
index 7997c55..3f6f5dc 100644
--- a/Include/pymacro.h
+++ b/Include/pymacro.h
@@ -1,13 +1,26 @@
#ifndef Py_PYMACRO_H
#define Py_PYMACRO_H
+/* Minimum value between x and y */
#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
+
+/* Maximum value between x and y */
#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
+/* Absolute value of the number x */
+#define Py_ABS(x) ((x) < 0 ? -(x) : (x))
+
+#define _Py_XSTRINGIFY(x) #x
+
+/* Convert the argument to a string. For example, Py_STRINGIFY(123) is replaced
+ with "123" by the preprocessor. Defines are also replaced by their value.
+ For example Py_STRINGIFY(__LINE__) is replaced by the line number, not
+ by "__LINE__". */
+#define Py_STRINGIFY(x) _Py_XSTRINGIFY(x)
+
/* Argument must be a char or an int in [-128, 127] or [0, 255]. */
#define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
-
/* Assert a build-time dependency, as an expression.
Your compile will fail if the condition isn't true, or can't be evaluated
diff --git a/Include/pymem.h b/Include/pymem.h
index 2372b86..043db64 100644
--- a/Include/pymem.h
+++ b/Include/pymem.h
@@ -13,6 +13,7 @@ extern "C" {
#ifndef Py_LIMITED_API
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);
#endif
@@ -57,6 +58,7 @@ PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
*/
PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
+PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
PyAPI_FUNC(void) PyMem_Free(void *ptr);
@@ -126,22 +128,25 @@ typedef enum {
} PyMemAllocatorDomain;
typedef struct {
- /* user context passed as the first argument to the 3 functions */
+ /* user context passed as the first argument to the 4 functions */
void *ctx;
/* allocate a memory block */
void* (*malloc) (void *ctx, size_t size);
+ /* allocate a memory block initialized by zeros */
+ void* (*calloc) (void *ctx, size_t nelem, size_t elsize);
+
/* allocate or resize a memory block */
void* (*realloc) (void *ctx, void *ptr, size_t new_size);
/* release a memory block */
void (*free) (void *ctx, void *ptr);
-} PyMemAllocator;
+} PyMemAllocatorEx;
/* Get the memory block allocator of the specified domain. */
PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
- PyMemAllocator *allocator);
+ PyMemAllocatorEx *allocator);
/* Set the memory block allocator of the specified domain.
@@ -155,7 +160,7 @@ PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
on top on the new allocator. */
PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
- PyMemAllocator *allocator);
+ PyMemAllocatorEx *allocator);
/* Setup hooks to detect bugs in the following Python memory allocator
functions:
diff --git a/Include/pyport.h b/Include/pyport.h
index c706213..69deb9f 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -588,6 +588,25 @@ extern "C" {
} while (0)
#endif
+#ifdef HAVE_GCC_ASM_FOR_MC68881
+#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)); \
+ } while (0)
+#define _Py_SET_53BIT_PRECISION_END \
+ do { \
+ if (new_fpcr != old_fpcr) \
+ __asm__ volatile ("fmove.l %0,%%fpcr" : : "g" (old_fpcr)); \
+ } while (0)
+#endif
+
/* default definitions are empty */
#ifndef HAVE_PY_SET_53BIT_PRECISION
#define _Py_SET_53BIT_PRECISION_HEADER
diff --git a/Include/pythonrun.h b/Include/pythonrun.h
index 2fc5578..1d9b71b 100644
--- a/Include/pythonrun.h
+++ b/Include/pythonrun.h
@@ -22,30 +22,6 @@ typedef struct {
} PyCompilerFlags;
#endif
-PyAPI_FUNC(void) Py_SetProgramName(wchar_t *);
-PyAPI_FUNC(wchar_t *) Py_GetProgramName(void);
-
-PyAPI_FUNC(void) Py_SetPythonHome(wchar_t *);
-PyAPI_FUNC(wchar_t *) Py_GetPythonHome(void);
-
-#ifndef Py_LIMITED_API
-/* Only used by applications that embed the interpreter and need to
- * override the standard encoding determination mechanism
- */
-PyAPI_FUNC(int) Py_SetStandardStreamEncoding(const char *encoding,
- const char *errors);
-#endif
-
-PyAPI_FUNC(void) Py_Initialize(void);
-PyAPI_FUNC(void) Py_InitializeEx(int);
-#ifndef Py_LIMITED_API
-PyAPI_FUNC(void) _Py_InitializeEx_Private(int, int);
-#endif
-PyAPI_FUNC(void) Py_Finalize(void);
-PyAPI_FUNC(int) Py_IsInitialized(void);
-PyAPI_FUNC(PyThreadState *) Py_NewInterpreter(void);
-PyAPI_FUNC(void) Py_EndInterpreter(PyThreadState *);
-
#ifndef Py_LIMITED_API
PyAPI_FUNC(int) PyRun_SimpleStringFlags(const char *, PyCompilerFlags *);
PyAPI_FUNC(int) PyRun_AnyFileFlags(FILE *, const char *, PyCompilerFlags *);
@@ -166,26 +142,6 @@ PyAPI_FUNC(void) PyErr_Print(void);
PyAPI_FUNC(void) PyErr_PrintEx(int);
PyAPI_FUNC(void) PyErr_Display(PyObject *, PyObject *, PyObject *);
-/* Py_PyAtExit is for the atexit module, Py_AtExit is for low-level
- * exit functions.
- */
-#ifndef Py_LIMITED_API
-PyAPI_FUNC(void) _Py_PyAtExit(void (*func)(void));
-#endif
-PyAPI_FUNC(int) Py_AtExit(void (*func)(void));
-
-PyAPI_FUNC(void) Py_Exit(int);
-
-/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. */
-#ifndef Py_LIMITED_API
-PyAPI_FUNC(void) _Py_RestoreSignals(void);
-
-PyAPI_FUNC(int) Py_FdIsInteractive(FILE *, const char *);
-#endif
-
-/* Bootstrap */
-PyAPI_FUNC(int) Py_Main(int argc, wchar_t **argv);
-
#ifndef Py_LIMITED_API
/* Use macros for a bunch of old variants */
#define PyRun_String(str, s, g, l) PyRun_StringFlags(str, s, g, l, NULL)
@@ -207,64 +163,6 @@ PyAPI_FUNC(int) Py_Main(int argc, wchar_t **argv);
PyRun_FileExFlags(fp, p, s, g, l, 0, flags)
#endif
-/* In getpath.c */
-PyAPI_FUNC(wchar_t *) Py_GetProgramFullPath(void);
-PyAPI_FUNC(wchar_t *) Py_GetPrefix(void);
-PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
-PyAPI_FUNC(wchar_t *) Py_GetPath(void);
-PyAPI_FUNC(void) Py_SetPath(const wchar_t *);
-#ifdef MS_WINDOWS
-int _Py_CheckPython3();
-#endif
-
-/* In their own files */
-PyAPI_FUNC(const char *) Py_GetVersion(void);
-PyAPI_FUNC(const char *) Py_GetPlatform(void);
-PyAPI_FUNC(const char *) Py_GetCopyright(void);
-PyAPI_FUNC(const char *) Py_GetCompiler(void);
-PyAPI_FUNC(const char *) Py_GetBuildInfo(void);
-#ifndef Py_LIMITED_API
-PyAPI_FUNC(const char *) _Py_hgidentifier(void);
-PyAPI_FUNC(const char *) _Py_hgversion(void);
-#endif
-
-/* Internal -- various one-time initializations */
-#ifndef Py_LIMITED_API
-PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void);
-PyAPI_FUNC(PyObject *) _PySys_Init(void);
-PyAPI_FUNC(void) _PyImport_Init(void);
-PyAPI_FUNC(void) _PyExc_Init(PyObject * bltinmod);
-PyAPI_FUNC(void) _PyImportHooks_Init(void);
-PyAPI_FUNC(int) _PyFrame_Init(void);
-PyAPI_FUNC(int) _PyFloat_Init(void);
-PyAPI_FUNC(int) PyByteArray_Init(void);
-PyAPI_FUNC(void) _PyRandom_Init(void);
-#endif
-
-/* Various internal finalizers */
-#ifndef Py_LIMITED_API
-PyAPI_FUNC(void) _PyExc_Fini(void);
-PyAPI_FUNC(void) _PyImport_Fini(void);
-PyAPI_FUNC(void) PyMethod_Fini(void);
-PyAPI_FUNC(void) PyFrame_Fini(void);
-PyAPI_FUNC(void) PyCFunction_Fini(void);
-PyAPI_FUNC(void) PyDict_Fini(void);
-PyAPI_FUNC(void) PyTuple_Fini(void);
-PyAPI_FUNC(void) PyList_Fini(void);
-PyAPI_FUNC(void) PySet_Fini(void);
-PyAPI_FUNC(void) PyBytes_Fini(void);
-PyAPI_FUNC(void) PyByteArray_Fini(void);
-PyAPI_FUNC(void) PyFloat_Fini(void);
-PyAPI_FUNC(void) PyOS_FiniInterrupts(void);
-PyAPI_FUNC(void) _PyGC_DumpShutdownStats(void);
-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_DATA(PyThreadState *) _Py_Finalizing;
-#endif
-
/* Stuff with no proper home (yet) */
#ifndef Py_LIMITED_API
PyAPI_FUNC(char *) PyOS_Readline(FILE *, FILE *, const char *);
@@ -290,14 +188,6 @@ PyAPI_DATA(PyThreadState*) _PyOS_ReadlineTState;
PyAPI_FUNC(int) PyOS_CheckStack(void);
#endif
-/* Signals */
-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);
-
-/* Random */
-PyAPI_FUNC(int) _PyOS_URandom (void *buffer, Py_ssize_t size);
-
#ifdef __cplusplus
}
#endif
diff --git a/Include/pytime.h b/Include/pytime.h
index b0fc6d0..7a14456 100644
--- a/Include/pytime.h
+++ b/Include/pytime.h
@@ -37,7 +37,7 @@ PyAPI_FUNC(void) _PyTime_gettimeofday(_PyTime_timeval *tp);
/* Similar to _PyTime_gettimeofday() but retrieve also information on the
* clock used to get the current time. */
-PyAPI_FUNC(void) _PyTime_gettimeofday_info(
+PyAPI_FUNC(int) _PyTime_gettimeofday_info(
_PyTime_timeval *tp,
_Py_clock_info_t *info);
@@ -52,8 +52,6 @@ do { \
((tv_end.tv_sec - tv_start.tv_sec) + \
(tv_end.tv_usec - tv_start.tv_usec) * 0.000001)
-#ifndef Py_LIMITED_API
-
typedef enum {
/* Round towards zero. */
_PyTime_ROUND_DOWN=0,
@@ -92,10 +90,28 @@ PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
time_t *sec,
long *nsec,
_PyTime_round_t);
-#endif
-/* Dummy to force linking. */
-PyAPI_FUNC(void) _PyTime_Init(void);
+/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
+ The clock is not affected by system clock updates. The reference point of
+ the returned value is undefined, so that only the difference between the
+ results of consecutive calls is valid.
+
+ The function never fails. _PyTime_Init() ensures that a monotonic clock
+ is available and works. */
+PyAPI_FUNC(void) _PyTime_monotonic(
+ _PyTime_timeval *tp);
+
+/* Similar to _PyTime_monotonic(), fill also info (if set) with information of
+ the function used to get the time.
+
+ Return 0 on success, raise an exception and return -1 on error. */
+PyAPI_FUNC(int) _PyTime_monotonic_info(
+ _PyTime_timeval *tp,
+ _Py_clock_info_t *info);
+
+/* Initialize time.
+ Return 0 on success, raise an exception and return -1 on error. */
+PyAPI_FUNC(int) _PyTime_Init(void);
#ifdef __cplusplus
}
diff --git a/Include/setobject.h b/Include/setobject.h
index ae3f556..f17bc1b 100644
--- a/Include/setobject.h
+++ b/Include/setobject.h
@@ -6,38 +6,43 @@
extern "C" {
#endif
+#ifndef Py_LIMITED_API
-/*
-There are three kinds of slots in the table:
+/* There are three kinds of entries in the table:
1. Unused: key == NULL
2. Active: key != NULL and key != dummy
3. Dummy: key == dummy
-Note: .pop() abuses the hash field of an Unused or Dummy slot to
-hold a search finger. The hash field of Unused or Dummy slots has
-no meaning otherwise.
+The hash field of Unused slots have no meaning.
+The hash field of Dummny slots are set to -1
+meaning that dummy entries can be detected by
+either entry->key==dummy or by entry->hash==-1.
*/
-#ifndef Py_LIMITED_API
+
#define PySet_MINSIZE 8
typedef struct {
- /* Cached hash code of the key. */
PyObject *key;
- Py_hash_t hash;
+ Py_hash_t hash; /* Cached hash code of the key */
} setentry;
+/* The SetObject data structure is shared by set and frozenset objects.
+
+Invariant for sets:
+ - hash is -1
+
+Invariants for frozensets:
+ - data is immutable.
+ - hash is the hash of the frozenset or -1 if not computed yet.
-/*
-This data structure is shared by set and frozenset objects.
*/
-typedef struct _setobject PySetObject;
-struct _setobject {
+typedef struct {
PyObject_HEAD
- Py_ssize_t fill; /* # Active + # Dummy */
- Py_ssize_t used; /* # Active */
+ Py_ssize_t fill; /* Number active and dummy entries*/
+ Py_ssize_t used; /* Number active entries */
/* The table contains mask + 1 slots, and that's a power of 2.
* We store the mask instead of the size because the mask is more
@@ -45,33 +50,42 @@ struct _setobject {
*/
Py_ssize_t mask;
- /* table points to smalltable for small tables, else to
- * additional malloc'ed memory. table is never NULL! This rule
- * saves repeated runtime null-tests.
+ /* The table points to a fixed-size smalltable for small tables
+ * or to additional malloc'ed memory for bigger tables.
+ * The table pointer is never NULL which saves us from repeated
+ * runtime null-tests.
*/
setentry *table;
- setentry *(*lookup)(PySetObject *so, PyObject *key, Py_hash_t hash);
- Py_hash_t hash; /* only used by frozenset objects */
- setentry smalltable[PySet_MINSIZE];
+ Py_hash_t hash; /* Only used by frozenset objects */
+ Py_ssize_t finger; /* Search finger for pop() */
+ setentry smalltable[PySet_MINSIZE];
PyObject *weakreflist; /* List of weak references */
-};
-#endif /* Py_LIMITED_API */
+} PySetObject;
+
+#define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
+
+PyAPI_DATA(PyObject *) _PySet_Dummy;
+
+PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash);
+PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
+PyAPI_FUNC(int) PySet_ClearFreeList(void);
+
+#endif /* Section excluded by Py_LIMITED_API */
PyAPI_DATA(PyTypeObject) PySet_Type;
PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
PyAPI_DATA(PyTypeObject) PySetIter_Type;
-#ifndef Py_LIMITED_API
-PyAPI_DATA(PyObject *) _PySet_Dummy;
-#endif
+PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
+PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
-/* Invariants for frozensets:
- * data is immutable.
- * hash is the hash of the frozenset or -1 if not computed yet.
- * Invariants for sets:
- * hash is -1
- */
+PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key);
+PyAPI_FUNC(int) PySet_Clear(PyObject *set);
+PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key);
+PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key);
+PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
+PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
#define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
#define PyAnySet_CheckExact(ob) \
@@ -87,26 +101,6 @@ PyAPI_DATA(PyObject *) _PySet_Dummy;
(Py_TYPE(ob) == &PyFrozenSet_Type || \
PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
-PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
-PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
-PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
-#ifndef Py_LIMITED_API
-#define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
-#endif
-PyAPI_FUNC(int) PySet_Clear(PyObject *set);
-PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key);
-PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key);
-PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key);
-#ifndef Py_LIMITED_API
-PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash);
-#endif
-PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
-#ifndef Py_LIMITED_API
-PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
-
-PyAPI_FUNC(int) PySet_ClearFreeList(void);
-#endif
-
#ifdef __cplusplus
}
#endif
diff --git a/Include/token.h b/Include/token.h
index 905022b..2b213ee 100644
--- a/Include/token.h
+++ b/Include/token.h
@@ -58,13 +58,14 @@ extern "C" {
#define DOUBLESTAREQUAL 46
#define DOUBLESLASH 47
#define DOUBLESLASHEQUAL 48
-#define AT 49
-#define RARROW 50
-#define ELLIPSIS 51
+#define AT 49
+#define ATEQUAL 50
+#define RARROW 51
+#define ELLIPSIS 52
/* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */
-#define OP 52
-#define ERRORTOKEN 53
-#define N_TOKENS 54
+#define OP 53
+#define ERRORTOKEN 54
+#define N_TOKENS 55
/* Special definitions for cooperation with parser */
diff --git a/Include/typeslots.h b/Include/typeslots.h
index ad3cdfb..da2e87c 100644
--- a/Include/typeslots.h
+++ b/Include/typeslots.h
@@ -74,3 +74,5 @@
#define Py_tp_members 72
#define Py_tp_getset 73
#define Py_tp_free 74
+#define Py_nb_matrix_multiply 75
+#define Py_nb_inplace_matrix_multiply 76
diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
index d7b2ace..814d7c0 100644
--- a/Include/unicodeobject.h
+++ b/Include/unicodeobject.h
@@ -2245,6 +2245,8 @@ PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strrchr(
Py_UNICODE c
);
+PyAPI_FUNC(PyObject*) _PyUnicode_FormatLong(PyObject *, int, int, int);
+
/* Create a copy of a unicode string ending with a nul character. Return NULL
and raise a MemoryError exception on memory allocation failure, otherwise
return a new allocated buffer (use PyMem_Free() to free the buffer). */
diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py
index 33b59ab..3d3f07b 100644
--- a/Lib/_collections_abc.py
+++ b/Lib/_collections_abc.py
@@ -453,6 +453,8 @@ Mapping.register(mappingproxy)
class MappingView(Sized):
+ __slots__ = '_mapping',
+
def __init__(self, mapping):
self._mapping = mapping
@@ -465,6 +467,8 @@ class MappingView(Sized):
class KeysView(MappingView, Set):
+ __slots__ = ()
+
@classmethod
def _from_iterable(self, it):
return set(it)
@@ -480,6 +484,8 @@ KeysView.register(dict_keys)
class ItemsView(MappingView, Set):
+ __slots__ = ()
+
@classmethod
def _from_iterable(self, it):
return set(it)
@@ -502,6 +508,8 @@ ItemsView.register(dict_items)
class ValuesView(MappingView):
+ __slots__ = ()
+
def __contains__(self, value):
for key in self._mapping:
if value == self._mapping[key]:
diff --git a/Lib/_dummy_thread.py b/Lib/_dummy_thread.py
index b67cfb9..36e5f38 100644
--- a/Lib/_dummy_thread.py
+++ b/Lib/_dummy_thread.py
@@ -140,6 +140,14 @@ class LockType(object):
def locked(self):
return self.locked_status
+ def __repr__(self):
+ return "<%s %s.%s object at %s>" % (
+ "locked" if self.locked_status else "unlocked",
+ self.__class__.__module__,
+ self.__class__.__qualname__,
+ hex(id(self))
+ )
+
# Used to signal that interrupt_main was called in a "thread"
_interrupt = False
# True when not executing in a "thread"
diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py
new file mode 100644
index 0000000..ca6c4bd
--- /dev/null
+++ b/Lib/_pydecimal.py
@@ -0,0 +1,6381 @@
+# Copyright (c) 2004 Python Software Foundation.
+# All rights reserved.
+
+# Written by Eric Price <eprice at tjhsst.edu>
+# and Facundo Batista <facundo at taniquetil.com.ar>
+# and Raymond Hettinger <python at rcn.com>
+# and Aahz <aahz at pobox.com>
+# and Tim Peters
+
+# This module should be kept in sync with the latest updates of the
+# IBM specification as it evolves. Those updates will be treated
+# as bug fixes (deviation from the spec is a compatibility, usability
+# bug) and will be backported. At this point the spec is stabilizing
+# and the updates are becoming fewer, smaller, and less significant.
+
+"""
+This is an implementation of decimal floating point arithmetic based on
+the General Decimal Arithmetic Specification:
+
+ http://speleotrove.com/decimal/decarith.html
+
+and IEEE standard 854-1987:
+
+ http://en.wikipedia.org/wiki/IEEE_854-1987
+
+Decimal floating point has finite precision with arbitrarily large bounds.
+
+The purpose of this module is to support arithmetic using familiar
+"schoolhouse" rules and to avoid some of the tricky representation
+issues associated with binary floating point. The package is especially
+useful for financial applications or for contexts where users have
+expectations that are at odds with binary floating point (for instance,
+in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
+of 0.0; Decimal('1.00') % Decimal('0.1') returns the expected
+Decimal('0.00')).
+
+Here are some examples of using the decimal module:
+
+>>> from decimal import *
+>>> setcontext(ExtendedContext)
+>>> Decimal(0)
+Decimal('0')
+>>> Decimal('1')
+Decimal('1')
+>>> Decimal('-.0123')
+Decimal('-0.0123')
+>>> Decimal(123456)
+Decimal('123456')
+>>> Decimal('123.45e12345678')
+Decimal('1.2345E+12345680')
+>>> Decimal('1.33') + Decimal('1.27')
+Decimal('2.60')
+>>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41')
+Decimal('-2.20')
+>>> dig = Decimal(1)
+>>> print(dig / Decimal(3))
+0.333333333
+>>> getcontext().prec = 18
+>>> print(dig / Decimal(3))
+0.333333333333333333
+>>> print(dig.sqrt())
+1
+>>> print(Decimal(3).sqrt())
+1.73205080756887729
+>>> print(Decimal(3) ** 123)
+4.85192780976896427E+58
+>>> inf = Decimal(1) / Decimal(0)
+>>> print(inf)
+Infinity
+>>> neginf = Decimal(-1) / Decimal(0)
+>>> print(neginf)
+-Infinity
+>>> print(neginf + inf)
+NaN
+>>> print(neginf * inf)
+-Infinity
+>>> print(dig / 0)
+Infinity
+>>> getcontext().traps[DivisionByZero] = 1
+>>> print(dig / 0)
+Traceback (most recent call last):
+ ...
+ ...
+ ...
+decimal.DivisionByZero: x / 0
+>>> c = Context()
+>>> c.traps[InvalidOperation] = 0
+>>> print(c.flags[InvalidOperation])
+0
+>>> c.divide(Decimal(0), Decimal(0))
+Decimal('NaN')
+>>> c.traps[InvalidOperation] = 1
+>>> print(c.flags[InvalidOperation])
+1
+>>> c.flags[InvalidOperation] = 0
+>>> print(c.flags[InvalidOperation])
+0
+>>> print(c.divide(Decimal(0), Decimal(0)))
+Traceback (most recent call last):
+ ...
+ ...
+ ...
+decimal.InvalidOperation: 0 / 0
+>>> print(c.flags[InvalidOperation])
+1
+>>> c.flags[InvalidOperation] = 0
+>>> c.traps[InvalidOperation] = 0
+>>> print(c.divide(Decimal(0), Decimal(0)))
+NaN
+>>> print(c.flags[InvalidOperation])
+1
+>>>
+"""
+
+__all__ = [
+ # Two major classes
+ 'Decimal', 'Context',
+
+ # Named tuple representation
+ 'DecimalTuple',
+
+ # Contexts
+ 'DefaultContext', 'BasicContext', 'ExtendedContext',
+
+ # Exceptions
+ 'DecimalException', 'Clamped', 'InvalidOperation', 'DivisionByZero',
+ 'Inexact', 'Rounded', 'Subnormal', 'Overflow', 'Underflow',
+ 'FloatOperation',
+
+ # Exceptional conditions that trigger InvalidOperation
+ 'DivisionImpossible', 'InvalidContext', 'ConversionSyntax', 'DivisionUndefined',
+
+ # Constants for use in setting up contexts
+ 'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING',
+ 'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN', 'ROUND_05UP',
+
+ # Functions for manipulating contexts
+ 'setcontext', 'getcontext', 'localcontext',
+
+ # Limits for the C version for compatibility
+ 'MAX_PREC', 'MAX_EMAX', 'MIN_EMIN', 'MIN_ETINY',
+
+ # C version: compile time choice that enables the thread local context
+ 'HAVE_THREADS'
+]
+
+__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
+
+import math as _math
+import numbers as _numbers
+import sys
+
+try:
+ from collections import namedtuple as _namedtuple
+ DecimalTuple = _namedtuple('DecimalTuple', 'sign digits exponent')
+except ImportError:
+ DecimalTuple = lambda *args: args
+
+# Rounding
+ROUND_DOWN = 'ROUND_DOWN'
+ROUND_HALF_UP = 'ROUND_HALF_UP'
+ROUND_HALF_EVEN = 'ROUND_HALF_EVEN'
+ROUND_CEILING = 'ROUND_CEILING'
+ROUND_FLOOR = 'ROUND_FLOOR'
+ROUND_UP = 'ROUND_UP'
+ROUND_HALF_DOWN = 'ROUND_HALF_DOWN'
+ROUND_05UP = 'ROUND_05UP'
+
+# Compatibility with the C version
+HAVE_THREADS = True
+if sys.maxsize == 2**63-1:
+ MAX_PREC = 999999999999999999
+ MAX_EMAX = 999999999999999999
+ MIN_EMIN = -999999999999999999
+else:
+ MAX_PREC = 425000000
+ MAX_EMAX = 425000000
+ MIN_EMIN = -425000000
+
+MIN_ETINY = MIN_EMIN - (MAX_PREC-1)
+
+# Errors
+
+class DecimalException(ArithmeticError):
+ """Base exception class.
+
+ Used exceptions derive from this.
+ If an exception derives from another exception besides this (such as
+ Underflow (Inexact, Rounded, Subnormal) that indicates that it is only
+ called if the others are present. This isn't actually used for
+ anything, though.
+
+ handle -- Called when context._raise_error is called and the
+ trap_enabler is not set. First argument is self, second is the
+ context. More arguments can be given, those being after
+ the explanation in _raise_error (For example,
+ context._raise_error(NewError, '(-x)!', self._sign) would
+ call NewError().handle(context, self._sign).)
+
+ To define a new exception, it should be sufficient to have it derive
+ from DecimalException.
+ """
+ def handle(self, context, *args):
+ pass
+
+
+class Clamped(DecimalException):
+ """Exponent of a 0 changed to fit bounds.
+
+ This occurs and signals clamped if the exponent of a result has been
+ altered in order to fit the constraints of a specific concrete
+ representation. This may occur when the exponent of a zero result would
+ be outside the bounds of a representation, or when a large normal
+ number would have an encoded exponent that cannot be represented. In
+ this latter case, the exponent is reduced to fit and the corresponding
+ number of zero digits are appended to the coefficient ("fold-down").
+ """
+
+class InvalidOperation(DecimalException):
+ """An invalid operation was performed.
+
+ Various bad things cause this:
+
+ Something creates a signaling NaN
+ -INF + INF
+ 0 * (+-)INF
+ (+-)INF / (+-)INF
+ x % 0
+ (+-)INF % x
+ x._rescale( non-integer )
+ sqrt(-x) , x > 0
+ 0 ** 0
+ x ** (non-integer)
+ x ** (+-)INF
+ An operand is invalid
+
+ The result of the operation after these is a quiet positive NaN,
+ except when the cause is a signaling NaN, in which case the result is
+ also a quiet NaN, but with the original sign, and an optional
+ diagnostic information.
+ """
+ def handle(self, context, *args):
+ if args:
+ ans = _dec_from_triple(args[0]._sign, args[0]._int, 'n', True)
+ return ans._fix_nan(context)
+ return _NaN
+
+class ConversionSyntax(InvalidOperation):
+ """Trying to convert badly formed string.
+
+ This occurs and signals invalid-operation if an string is being
+ converted to a number and it does not conform to the numeric string
+ syntax. The result is [0,qNaN].
+ """
+ def handle(self, context, *args):
+ return _NaN
+
+class DivisionByZero(DecimalException, ZeroDivisionError):
+ """Division by 0.
+
+ This occurs and signals division-by-zero if division of a finite number
+ by zero was attempted (during a divide-integer or divide operation, or a
+ power operation with negative right-hand operand), and the dividend was
+ not zero.
+
+ The result of the operation is [sign,inf], where sign is the exclusive
+ or of the signs of the operands for divide, or is 1 for an odd power of
+ -0, for power.
+ """
+
+ def handle(self, context, sign, *args):
+ return _SignedInfinity[sign]
+
+class DivisionImpossible(InvalidOperation):
+ """Cannot perform the division adequately.
+
+ This occurs and signals invalid-operation if the integer result of a
+ divide-integer or remainder operation had too many digits (would be
+ longer than precision). The result is [0,qNaN].
+ """
+
+ def handle(self, context, *args):
+ return _NaN
+
+class DivisionUndefined(InvalidOperation, ZeroDivisionError):
+ """Undefined result of division.
+
+ This occurs and signals invalid-operation if division by zero was
+ attempted (during a divide-integer, divide, or remainder operation), and
+ the dividend is also zero. The result is [0,qNaN].
+ """
+
+ def handle(self, context, *args):
+ return _NaN
+
+class Inexact(DecimalException):
+ """Had to round, losing information.
+
+ This occurs and signals inexact whenever the result of an operation is
+ not exact (that is, it needed to be rounded and any discarded digits
+ were non-zero), or if an overflow or underflow condition occurs. The
+ result in all cases is unchanged.
+
+ The inexact signal may be tested (or trapped) to determine if a given
+ operation (or sequence of operations) was inexact.
+ """
+
+class InvalidContext(InvalidOperation):
+ """Invalid context. Unknown rounding, for example.
+
+ This occurs and signals invalid-operation if an invalid context was
+ detected during an operation. This can occur if contexts are not checked
+ on creation and either the precision exceeds the capability of the
+ underlying concrete representation or an unknown or unsupported rounding
+ was specified. These aspects of the context need only be checked when
+ the values are required to be used. The result is [0,qNaN].
+ """
+
+ def handle(self, context, *args):
+ return _NaN
+
+class Rounded(DecimalException):
+ """Number got rounded (not necessarily changed during rounding).
+
+ This occurs and signals rounded whenever the result of an operation is
+ rounded (that is, some zero or non-zero digits were discarded from the
+ coefficient), or if an overflow or underflow condition occurs. The
+ result in all cases is unchanged.
+
+ The rounded signal may be tested (or trapped) to determine if a given
+ operation (or sequence of operations) caused a loss of precision.
+ """
+
+class Subnormal(DecimalException):
+ """Exponent < Emin before rounding.
+
+ This occurs and signals subnormal whenever the result of a conversion or
+ operation is subnormal (that is, its adjusted exponent is less than
+ Emin, before any rounding). The result in all cases is unchanged.
+
+ The subnormal signal may be tested (or trapped) to determine if a given
+ or operation (or sequence of operations) yielded a subnormal result.
+ """
+
+class Overflow(Inexact, Rounded):
+ """Numerical overflow.
+
+ This occurs and signals overflow if the adjusted exponent of a result
+ (from a conversion or from an operation that is not an attempt to divide
+ by zero), after rounding, would be greater than the largest value that
+ can be handled by the implementation (the value Emax).
+
+ The result depends on the rounding mode:
+
+ For round-half-up and round-half-even (and for round-half-down and
+ round-up, if implemented), the result of the operation is [sign,inf],
+ where sign is the sign of the intermediate result. For round-down, the
+ result is the largest finite number that can be represented in the
+ current precision, with the sign of the intermediate result. For
+ round-ceiling, the result is the same as for round-down if the sign of
+ the intermediate result is 1, or is [0,inf] otherwise. For round-floor,
+ the result is the same as for round-down if the sign of the intermediate
+ result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded
+ will also be raised.
+ """
+
+ def handle(self, context, sign, *args):
+ if context.rounding in (ROUND_HALF_UP, ROUND_HALF_EVEN,
+ ROUND_HALF_DOWN, ROUND_UP):
+ return _SignedInfinity[sign]
+ if sign == 0:
+ if context.rounding == ROUND_CEILING:
+ return _SignedInfinity[sign]
+ return _dec_from_triple(sign, '9'*context.prec,
+ context.Emax-context.prec+1)
+ if sign == 1:
+ if context.rounding == ROUND_FLOOR:
+ return _SignedInfinity[sign]
+ return _dec_from_triple(sign, '9'*context.prec,
+ context.Emax-context.prec+1)
+
+
+class Underflow(Inexact, Rounded, Subnormal):
+ """Numerical underflow with result rounded to 0.
+
+ This occurs and signals underflow if a result is inexact and the
+ adjusted exponent of the result would be smaller (more negative) than
+ the smallest value that can be handled by the implementation (the value
+ Emin). That is, the result is both inexact and subnormal.
+
+ The result after an underflow will be a subnormal number rounded, if
+ necessary, so that its exponent is not less than Etiny. This may result
+ in 0 with the sign of the intermediate result and an exponent of Etiny.
+
+ In all cases, Inexact, Rounded, and Subnormal will also be raised.
+ """
+
+class FloatOperation(DecimalException, TypeError):
+ """Enable stricter semantics for mixing floats and Decimals.
+
+ If the signal is not trapped (default), mixing floats and Decimals is
+ permitted in the Decimal() constructor, context.create_decimal() and
+ all comparison operators. Both conversion and comparisons are exact.
+ Any occurrence of a mixed operation is silently recorded by setting
+ FloatOperation in the context flags. Explicit conversions with
+ Decimal.from_float() or context.create_decimal_from_float() do not
+ set the flag.
+
+ Otherwise (the signal is trapped), only equality comparisons and explicit
+ conversions are silent. All other mixed operations raise FloatOperation.
+ """
+
+# List of public traps and flags
+_signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded,
+ Underflow, InvalidOperation, Subnormal, FloatOperation]
+
+# Map conditions (per the spec) to signals
+_condition_map = {ConversionSyntax:InvalidOperation,
+ DivisionImpossible:InvalidOperation,
+ DivisionUndefined:InvalidOperation,
+ InvalidContext:InvalidOperation}
+
+# Valid rounding modes
+_rounding_modes = (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_CEILING,
+ ROUND_FLOOR, ROUND_UP, ROUND_HALF_DOWN, ROUND_05UP)
+
+##### Context Functions ##################################################
+
+# The getcontext() and setcontext() function manage access to a thread-local
+# current context. Py2.4 offers direct support for thread locals. If that
+# is not available, use threading.current_thread() which is slower but will
+# work for older Pythons. If threads are not part of the build, create a
+# mock threading object with threading.local() returning the module namespace.
+
+try:
+ import threading
+except ImportError:
+ # Python was compiled without threads; create a mock object instead
+ class MockThreading(object):
+ def local(self, sys=sys):
+ return sys.modules[__xname__]
+ threading = MockThreading()
+ del MockThreading
+
+try:
+ threading.local
+
+except AttributeError:
+
+ # To fix reloading, force it to create a new context
+ # Old contexts have different exceptions in their dicts, making problems.
+ if hasattr(threading.current_thread(), '__decimal_context__'):
+ del threading.current_thread().__decimal_context__
+
+ def setcontext(context):
+ """Set this thread's context to context."""
+ if context in (DefaultContext, BasicContext, ExtendedContext):
+ context = context.copy()
+ context.clear_flags()
+ threading.current_thread().__decimal_context__ = context
+
+ def getcontext():
+ """Returns this thread's context.
+
+ If this thread does not yet have a context, returns
+ a new context and sets this thread's context.
+ New contexts are copies of DefaultContext.
+ """
+ try:
+ return threading.current_thread().__decimal_context__
+ except AttributeError:
+ context = Context()
+ threading.current_thread().__decimal_context__ = context
+ return context
+
+else:
+
+ local = threading.local()
+ if hasattr(local, '__decimal_context__'):
+ del local.__decimal_context__
+
+ def getcontext(_local=local):
+ """Returns this thread's context.
+
+ If this thread does not yet have a context, returns
+ a new context and sets this thread's context.
+ New contexts are copies of DefaultContext.
+ """
+ try:
+ return _local.__decimal_context__
+ except AttributeError:
+ context = Context()
+ _local.__decimal_context__ = context
+ return context
+
+ def setcontext(context, _local=local):
+ """Set this thread's context to context."""
+ if context in (DefaultContext, BasicContext, ExtendedContext):
+ context = context.copy()
+ context.clear_flags()
+ _local.__decimal_context__ = context
+
+ del threading, local # Don't contaminate the namespace
+
+def localcontext(ctx=None):
+ """Return a context manager for a copy of the supplied context
+
+ Uses a copy of the current context if no context is specified
+ The returned context manager creates a local decimal context
+ in a with statement:
+ def sin(x):
+ with localcontext() as ctx:
+ ctx.prec += 2
+ # Rest of sin calculation algorithm
+ # uses a precision 2 greater than normal
+ return +s # Convert result to normal precision
+
+ def sin(x):
+ with localcontext(ExtendedContext):
+ # Rest of sin calculation algorithm
+ # uses the Extended Context from the
+ # General Decimal Arithmetic Specification
+ return +s # Convert result to normal context
+
+ >>> setcontext(DefaultContext)
+ >>> print(getcontext().prec)
+ 28
+ >>> with localcontext():
+ ... ctx = getcontext()
+ ... ctx.prec += 2
+ ... print(ctx.prec)
+ ...
+ 30
+ >>> with localcontext(ExtendedContext):
+ ... print(getcontext().prec)
+ ...
+ 9
+ >>> print(getcontext().prec)
+ 28
+ """
+ if ctx is None: ctx = getcontext()
+ return _ContextManager(ctx)
+
+
+##### Decimal class #######################################################
+
+# Do not subclass Decimal from numbers.Real and do not register it as such
+# (because Decimals are not interoperable with floats). See the notes in
+# numbers.py for more detail.
+
+class Decimal(object):
+ """Floating point class for decimal arithmetic."""
+
+ __slots__ = ('_exp','_int','_sign', '_is_special')
+ # Generally, the value of the Decimal instance is given by
+ # (-1)**_sign * _int * 10**_exp
+ # Special values are signified by _is_special == True
+
+ # We're immutable, so use __new__ not __init__
+ def __new__(cls, value="0", context=None):
+ """Create a decimal point instance.
+
+ >>> Decimal('3.14') # string input
+ Decimal('3.14')
+ >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
+ Decimal('3.14')
+ >>> Decimal(314) # int
+ Decimal('314')
+ >>> Decimal(Decimal(314)) # another decimal instance
+ Decimal('314')
+ >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
+ Decimal('3.14')
+ """
+
+ # Note that the coefficient, self._int, is actually stored as
+ # a string rather than as a tuple of digits. This speeds up
+ # the "digits to integer" and "integer to digits" conversions
+ # that are used in almost every arithmetic operation on
+ # Decimals. This is an internal detail: the as_tuple function
+ # and the Decimal constructor still deal with tuples of
+ # digits.
+
+ self = object.__new__(cls)
+
+ # From a string
+ # REs insist on real strings, so we can too.
+ if isinstance(value, str):
+ m = _parser(value.strip())
+ if m is None:
+ if context is None:
+ context = getcontext()
+ return context._raise_error(ConversionSyntax,
+ "Invalid literal for Decimal: %r" % value)
+
+ if m.group('sign') == "-":
+ self._sign = 1
+ else:
+ self._sign = 0
+ intpart = m.group('int')
+ if intpart is not None:
+ # finite number
+ fracpart = m.group('frac') or ''
+ exp = int(m.group('exp') or '0')
+ self._int = str(int(intpart+fracpart))
+ self._exp = exp - len(fracpart)
+ self._is_special = False
+ else:
+ diag = m.group('diag')
+ if diag is not None:
+ # NaN
+ self._int = str(int(diag or '0')).lstrip('0')
+ if m.group('signal'):
+ self._exp = 'N'
+ else:
+ self._exp = 'n'
+ else:
+ # infinity
+ self._int = '0'
+ self._exp = 'F'
+ self._is_special = True
+ return self
+
+ # From an integer
+ if isinstance(value, int):
+ if value >= 0:
+ self._sign = 0
+ else:
+ self._sign = 1
+ self._exp = 0
+ self._int = str(abs(value))
+ self._is_special = False
+ return self
+
+ # From another decimal
+ if isinstance(value, Decimal):
+ self._exp = value._exp
+ self._sign = value._sign
+ self._int = value._int
+ self._is_special = value._is_special
+ return self
+
+ # From an internal working value
+ if isinstance(value, _WorkRep):
+ self._sign = value.sign
+ self._int = str(value.int)
+ self._exp = int(value.exp)
+ self._is_special = False
+ return self
+
+ # tuple/list conversion (possibly from as_tuple())
+ if isinstance(value, (list,tuple)):
+ if len(value) != 3:
+ raise ValueError('Invalid tuple size in creation of Decimal '
+ 'from list or tuple. The list or tuple '
+ 'should have exactly three elements.')
+ # process sign. The isinstance test rejects floats
+ if not (isinstance(value[0], int) and value[0] in (0,1)):
+ raise ValueError("Invalid sign. The first value in the tuple "
+ "should be an integer; either 0 for a "
+ "positive number or 1 for a negative number.")
+ self._sign = value[0]
+ if value[2] == 'F':
+ # infinity: value[1] is ignored
+ self._int = '0'
+ self._exp = value[2]
+ self._is_special = True
+ else:
+ # process and validate the digits in value[1]
+ digits = []
+ for digit in value[1]:
+ if isinstance(digit, int) and 0 <= digit <= 9:
+ # skip leading zeros
+ if digits or digit != 0:
+ digits.append(digit)
+ else:
+ raise ValueError("The second value in the tuple must "
+ "be composed of integers in the range "
+ "0 through 9.")
+ if value[2] in ('n', 'N'):
+ # NaN: digits form the diagnostic
+ self._int = ''.join(map(str, digits))
+ self._exp = value[2]
+ self._is_special = True
+ elif isinstance(value[2], int):
+ # finite number: digits give the coefficient
+ self._int = ''.join(map(str, digits or [0]))
+ self._exp = value[2]
+ self._is_special = False
+ else:
+ raise ValueError("The third value in the tuple must "
+ "be an integer, or one of the "
+ "strings 'F', 'n', 'N'.")
+ return self
+
+ if isinstance(value, float):
+ if context is None:
+ context = getcontext()
+ context._raise_error(FloatOperation,
+ "strict semantics for mixing floats and Decimals are "
+ "enabled")
+ value = Decimal.from_float(value)
+ self._exp = value._exp
+ self._sign = value._sign
+ self._int = value._int
+ self._is_special = value._is_special
+ return self
+
+ raise TypeError("Cannot convert %r to Decimal" % value)
+
+ @classmethod
+ def from_float(cls, f):
+ """Converts a float to a decimal number, exactly.
+
+ Note that Decimal.from_float(0.1) is not the same as Decimal('0.1').
+ Since 0.1 is not exactly representable in binary floating point, the
+ value is stored as the nearest representable value which is
+ 0x1.999999999999ap-4. The exact equivalent of the value in decimal
+ is 0.1000000000000000055511151231257827021181583404541015625.
+
+ >>> Decimal.from_float(0.1)
+ Decimal('0.1000000000000000055511151231257827021181583404541015625')
+ >>> Decimal.from_float(float('nan'))
+ Decimal('NaN')
+ >>> Decimal.from_float(float('inf'))
+ Decimal('Infinity')
+ >>> Decimal.from_float(-float('inf'))
+ Decimal('-Infinity')
+ >>> Decimal.from_float(-0.0)
+ Decimal('-0')
+
+ """
+ if isinstance(f, int): # handle integer inputs
+ return cls(f)
+ if not isinstance(f, float):
+ raise TypeError("argument must be int or float.")
+ if _math.isinf(f) or _math.isnan(f):
+ return cls(repr(f))
+ if _math.copysign(1.0, f) == 1.0:
+ sign = 0
+ else:
+ sign = 1
+ n, d = abs(f).as_integer_ratio()
+ k = d.bit_length() - 1
+ result = _dec_from_triple(sign, str(n*5**k), -k)
+ if cls is Decimal:
+ return result
+ else:
+ return cls(result)
+
+ def _isnan(self):
+ """Returns whether the number is not actually one.
+
+ 0 if a number
+ 1 if NaN
+ 2 if sNaN
+ """
+ if self._is_special:
+ exp = self._exp
+ if exp == 'n':
+ return 1
+ elif exp == 'N':
+ return 2
+ return 0
+
+ def _isinfinity(self):
+ """Returns whether the number is infinite
+
+ 0 if finite or not a number
+ 1 if +INF
+ -1 if -INF
+ """
+ if self._exp == 'F':
+ if self._sign:
+ return -1
+ return 1
+ return 0
+
+ def _check_nans(self, other=None, context=None):
+ """Returns whether the number is not actually one.
+
+ if self, other are sNaN, signal
+ if self, other are NaN return nan
+ return 0
+
+ Done before operations.
+ """
+
+ self_is_nan = self._isnan()
+ if other is None:
+ other_is_nan = False
+ else:
+ other_is_nan = other._isnan()
+
+ if self_is_nan or other_is_nan:
+ if context is None:
+ context = getcontext()
+
+ if self_is_nan == 2:
+ return context._raise_error(InvalidOperation, 'sNaN',
+ self)
+ if other_is_nan == 2:
+ return context._raise_error(InvalidOperation, 'sNaN',
+ other)
+ if self_is_nan:
+ return self._fix_nan(context)
+
+ return other._fix_nan(context)
+ return 0
+
+ def _compare_check_nans(self, other, context):
+ """Version of _check_nans used for the signaling comparisons
+ compare_signal, __le__, __lt__, __ge__, __gt__.
+
+ Signal InvalidOperation if either self or other is a (quiet
+ or signaling) NaN. Signaling NaNs take precedence over quiet
+ NaNs.
+
+ Return 0 if neither operand is a NaN.
+
+ """
+ if context is None:
+ context = getcontext()
+
+ if self._is_special or other._is_special:
+ if self.is_snan():
+ return context._raise_error(InvalidOperation,
+ 'comparison involving sNaN',
+ self)
+ elif other.is_snan():
+ return context._raise_error(InvalidOperation,
+ 'comparison involving sNaN',
+ other)
+ elif self.is_qnan():
+ return context._raise_error(InvalidOperation,
+ 'comparison involving NaN',
+ self)
+ elif other.is_qnan():
+ return context._raise_error(InvalidOperation,
+ 'comparison involving NaN',
+ other)
+ return 0
+
+ def __bool__(self):
+ """Return True if self is nonzero; otherwise return False.
+
+ NaNs and infinities are considered nonzero.
+ """
+ return self._is_special or self._int != '0'
+
+ def _cmp(self, other):
+ """Compare the two non-NaN decimal instances self and other.
+
+ Returns -1 if self < other, 0 if self == other and 1
+ if self > other. This routine is for internal use only."""
+
+ if self._is_special or other._is_special:
+ self_inf = self._isinfinity()
+ other_inf = other._isinfinity()
+ if self_inf == other_inf:
+ return 0
+ elif self_inf < other_inf:
+ return -1
+ else:
+ return 1
+
+ # check for zeros; Decimal('0') == Decimal('-0')
+ if not self:
+ if not other:
+ return 0
+ else:
+ return -((-1)**other._sign)
+ if not other:
+ return (-1)**self._sign
+
+ # If different signs, neg one is less
+ if other._sign < self._sign:
+ return -1
+ if self._sign < other._sign:
+ return 1
+
+ self_adjusted = self.adjusted()
+ other_adjusted = other.adjusted()
+ if self_adjusted == other_adjusted:
+ self_padded = self._int + '0'*(self._exp - other._exp)
+ other_padded = other._int + '0'*(other._exp - self._exp)
+ if self_padded == other_padded:
+ return 0
+ elif self_padded < other_padded:
+ return -(-1)**self._sign
+ else:
+ return (-1)**self._sign
+ elif self_adjusted > other_adjusted:
+ return (-1)**self._sign
+ else: # self_adjusted < other_adjusted
+ return -((-1)**self._sign)
+
+ # Note: The Decimal standard doesn't cover rich comparisons for
+ # Decimals. In particular, the specification is silent on the
+ # subject of what should happen for a comparison involving a NaN.
+ # We take the following approach:
+ #
+ # == comparisons involving a quiet NaN always return False
+ # != comparisons involving a quiet NaN always return True
+ # == or != comparisons involving a signaling NaN signal
+ # InvalidOperation, and return False or True as above if the
+ # InvalidOperation is not trapped.
+ # <, >, <= and >= comparisons involving a (quiet or signaling)
+ # NaN signal InvalidOperation, and return False if the
+ # InvalidOperation is not trapped.
+ #
+ # This behavior is designed to conform as closely as possible to
+ # that specified by IEEE 754.
+
+ def __eq__(self, other, context=None):
+ self, other = _convert_for_comparison(self, other, equality_op=True)
+ if other is NotImplemented:
+ return other
+ if self._check_nans(other, context):
+ return False
+ return self._cmp(other) == 0
+
+ def __lt__(self, other, context=None):
+ self, other = _convert_for_comparison(self, other)
+ if other is NotImplemented:
+ return other
+ ans = self._compare_check_nans(other, context)
+ if ans:
+ return False
+ return self._cmp(other) < 0
+
+ def __le__(self, other, context=None):
+ self, other = _convert_for_comparison(self, other)
+ if other is NotImplemented:
+ return other
+ ans = self._compare_check_nans(other, context)
+ if ans:
+ return False
+ return self._cmp(other) <= 0
+
+ def __gt__(self, other, context=None):
+ self, other = _convert_for_comparison(self, other)
+ if other is NotImplemented:
+ return other
+ ans = self._compare_check_nans(other, context)
+ if ans:
+ return False
+ return self._cmp(other) > 0
+
+ def __ge__(self, other, context=None):
+ self, other = _convert_for_comparison(self, other)
+ if other is NotImplemented:
+ return other
+ ans = self._compare_check_nans(other, context)
+ if ans:
+ return False
+ return self._cmp(other) >= 0
+
+ def compare(self, other, context=None):
+ """Compares one to another.
+
+ -1 => a < b
+ 0 => a = b
+ 1 => a > b
+ NaN => one is NaN
+ Like __cmp__, but returns Decimal instances.
+ """
+ other = _convert_other(other, raiseit=True)
+
+ # Compare(NaN, NaN) = NaN
+ if (self._is_special or other and other._is_special):
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ return Decimal(self._cmp(other))
+
+ def __hash__(self):
+ """x.__hash__() <==> hash(x)"""
+
+ # In order to make sure that the hash of a Decimal instance
+ # agrees with the hash of a numerically equal integer, float
+ # or Fraction, we follow the rules for numeric hashes outlined
+ # in the documentation. (See library docs, 'Built-in Types').
+ if self._is_special:
+ if self.is_snan():
+ raise TypeError('Cannot hash a signaling NaN value.')
+ elif self.is_nan():
+ return _PyHASH_NAN
+ else:
+ if self._sign:
+ return -_PyHASH_INF
+ else:
+ return _PyHASH_INF
+
+ if self._exp >= 0:
+ exp_hash = pow(10, self._exp, _PyHASH_MODULUS)
+ else:
+ exp_hash = pow(_PyHASH_10INV, -self._exp, _PyHASH_MODULUS)
+ hash_ = int(self._int) * exp_hash % _PyHASH_MODULUS
+ ans = hash_ if self >= 0 else -hash_
+ return -2 if ans == -1 else ans
+
+ def as_tuple(self):
+ """Represents the number as a triple tuple.
+
+ To show the internals exactly as they are.
+ """
+ return DecimalTuple(self._sign, tuple(map(int, self._int)), self._exp)
+
+ def __repr__(self):
+ """Represents the number as an instance of Decimal."""
+ # Invariant: eval(repr(d)) == d
+ return "Decimal('%s')" % str(self)
+
+ def __str__(self, eng=False, context=None):
+ """Return string representation of the number in scientific notation.
+
+ Captures all of the information in the underlying representation.
+ """
+
+ sign = ['', '-'][self._sign]
+ if self._is_special:
+ if self._exp == 'F':
+ return sign + 'Infinity'
+ elif self._exp == 'n':
+ return sign + 'NaN' + self._int
+ else: # self._exp == 'N'
+ return sign + 'sNaN' + self._int
+
+ # number of digits of self._int to left of decimal point
+ leftdigits = self._exp + len(self._int)
+
+ # dotplace is number of digits of self._int to the left of the
+ # decimal point in the mantissa of the output string (that is,
+ # after adjusting the exponent)
+ if self._exp <= 0 and leftdigits > -6:
+ # no exponent required
+ dotplace = leftdigits
+ elif not eng:
+ # usual scientific notation: 1 digit on left of the point
+ dotplace = 1
+ elif self._int == '0':
+ # engineering notation, zero
+ dotplace = (leftdigits + 1) % 3 - 1
+ else:
+ # engineering notation, nonzero
+ dotplace = (leftdigits - 1) % 3 + 1
+
+ if dotplace <= 0:
+ intpart = '0'
+ fracpart = '.' + '0'*(-dotplace) + self._int
+ elif dotplace >= len(self._int):
+ intpart = self._int+'0'*(dotplace-len(self._int))
+ fracpart = ''
+ else:
+ intpart = self._int[:dotplace]
+ fracpart = '.' + self._int[dotplace:]
+ if leftdigits == dotplace:
+ exp = ''
+ else:
+ if context is None:
+ context = getcontext()
+ exp = ['e', 'E'][context.capitals] + "%+d" % (leftdigits-dotplace)
+
+ return sign + intpart + fracpart + exp
+
+ def to_eng_string(self, context=None):
+ """Convert to engineering-type string.
+
+ Engineering notation has an exponent which is a multiple of 3, so there
+ are up to 3 digits left of the decimal place.
+
+ Same rules for when in exponential and when as a value as in __str__.
+ """
+ return self.__str__(eng=True, context=context)
+
+ def __neg__(self, context=None):
+ """Returns a copy with the sign switched.
+
+ Rounds, if it has reason.
+ """
+ if self._is_special:
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ if context is None:
+ context = getcontext()
+
+ if not self and context.rounding != ROUND_FLOOR:
+ # -Decimal('0') is Decimal('0'), not Decimal('-0'), except
+ # in ROUND_FLOOR rounding mode.
+ ans = self.copy_abs()
+ else:
+ ans = self.copy_negate()
+
+ return ans._fix(context)
+
+ def __pos__(self, context=None):
+ """Returns a copy, unless it is a sNaN.
+
+ Rounds the number (if more then precision digits)
+ """
+ if self._is_special:
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ if context is None:
+ context = getcontext()
+
+ if not self and context.rounding != ROUND_FLOOR:
+ # + (-0) = 0, except in ROUND_FLOOR rounding mode.
+ ans = self.copy_abs()
+ else:
+ ans = Decimal(self)
+
+ return ans._fix(context)
+
+ def __abs__(self, round=True, context=None):
+ """Returns the absolute value of self.
+
+ If the keyword argument 'round' is false, do not round. The
+ expression self.__abs__(round=False) is equivalent to
+ self.copy_abs().
+ """
+ if not round:
+ return self.copy_abs()
+
+ if self._is_special:
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ if self._sign:
+ ans = self.__neg__(context=context)
+ else:
+ ans = self.__pos__(context=context)
+
+ return ans
+
+ def __add__(self, other, context=None):
+ """Returns self + other.
+
+ -INF + INF (or the reverse) cause InvalidOperation errors.
+ """
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+
+ if context is None:
+ context = getcontext()
+
+ if self._is_special or other._is_special:
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ if self._isinfinity():
+ # If both INF, same sign => same as both, opposite => error.
+ if self._sign != other._sign and other._isinfinity():
+ return context._raise_error(InvalidOperation, '-INF + INF')
+ return Decimal(self)
+ if other._isinfinity():
+ return Decimal(other) # Can't both be infinity here
+
+ exp = min(self._exp, other._exp)
+ negativezero = 0
+ if context.rounding == ROUND_FLOOR and self._sign != other._sign:
+ # If the answer is 0, the sign should be negative, in this case.
+ negativezero = 1
+
+ if not self and not other:
+ sign = min(self._sign, other._sign)
+ if negativezero:
+ sign = 1
+ ans = _dec_from_triple(sign, '0', exp)
+ ans = ans._fix(context)
+ return ans
+ if not self:
+ exp = max(exp, other._exp - context.prec-1)
+ ans = other._rescale(exp, context.rounding)
+ ans = ans._fix(context)
+ return ans
+ if not other:
+ exp = max(exp, self._exp - context.prec-1)
+ ans = self._rescale(exp, context.rounding)
+ ans = ans._fix(context)
+ return ans
+
+ op1 = _WorkRep(self)
+ op2 = _WorkRep(other)
+ op1, op2 = _normalize(op1, op2, context.prec)
+
+ result = _WorkRep()
+ if op1.sign != op2.sign:
+ # Equal and opposite
+ if op1.int == op2.int:
+ ans = _dec_from_triple(negativezero, '0', exp)
+ ans = ans._fix(context)
+ return ans
+ if op1.int < op2.int:
+ op1, op2 = op2, op1
+ # OK, now abs(op1) > abs(op2)
+ if op1.sign == 1:
+ result.sign = 1
+ op1.sign, op2.sign = op2.sign, op1.sign
+ else:
+ result.sign = 0
+ # So we know the sign, and op1 > 0.
+ elif op1.sign == 1:
+ result.sign = 1
+ op1.sign, op2.sign = (0, 0)
+ else:
+ result.sign = 0
+ # Now, op1 > abs(op2) > 0
+
+ if op2.sign == 0:
+ result.int = op1.int + op2.int
+ else:
+ result.int = op1.int - op2.int
+
+ result.exp = op1.exp
+ ans = Decimal(result)
+ ans = ans._fix(context)
+ return ans
+
+ __radd__ = __add__
+
+ def __sub__(self, other, context=None):
+ """Return self - other"""
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+
+ if self._is_special or other._is_special:
+ ans = self._check_nans(other, context=context)
+ if ans:
+ return ans
+
+ # self - other is computed as self + other.copy_negate()
+ return self.__add__(other.copy_negate(), context=context)
+
+ def __rsub__(self, other, context=None):
+ """Return other - self"""
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+
+ return other.__sub__(self, context=context)
+
+ def __mul__(self, other, context=None):
+ """Return self * other.
+
+ (+-) INF * 0 (or its reverse) raise InvalidOperation.
+ """
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+
+ if context is None:
+ context = getcontext()
+
+ resultsign = self._sign ^ other._sign
+
+ if self._is_special or other._is_special:
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ if self._isinfinity():
+ if not other:
+ return context._raise_error(InvalidOperation, '(+-)INF * 0')
+ return _SignedInfinity[resultsign]
+
+ if other._isinfinity():
+ if not self:
+ return context._raise_error(InvalidOperation, '0 * (+-)INF')
+ return _SignedInfinity[resultsign]
+
+ resultexp = self._exp + other._exp
+
+ # Special case for multiplying by zero
+ if not self or not other:
+ ans = _dec_from_triple(resultsign, '0', resultexp)
+ # Fixing in case the exponent is out of bounds
+ ans = ans._fix(context)
+ return ans
+
+ # Special case for multiplying by power of 10
+ if self._int == '1':
+ ans = _dec_from_triple(resultsign, other._int, resultexp)
+ ans = ans._fix(context)
+ return ans
+ if other._int == '1':
+ ans = _dec_from_triple(resultsign, self._int, resultexp)
+ ans = ans._fix(context)
+ return ans
+
+ op1 = _WorkRep(self)
+ op2 = _WorkRep(other)
+
+ ans = _dec_from_triple(resultsign, str(op1.int * op2.int), resultexp)
+ ans = ans._fix(context)
+
+ return ans
+ __rmul__ = __mul__
+
+ def __truediv__(self, other, context=None):
+ """Return self / other."""
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return NotImplemented
+
+ if context is None:
+ context = getcontext()
+
+ sign = self._sign ^ other._sign
+
+ if self._is_special or other._is_special:
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ if self._isinfinity() and other._isinfinity():
+ return context._raise_error(InvalidOperation, '(+-)INF/(+-)INF')
+
+ if self._isinfinity():
+ return _SignedInfinity[sign]
+
+ if other._isinfinity():
+ context._raise_error(Clamped, 'Division by infinity')
+ return _dec_from_triple(sign, '0', context.Etiny())
+
+ # Special cases for zeroes
+ if not other:
+ if not self:
+ return context._raise_error(DivisionUndefined, '0 / 0')
+ return context._raise_error(DivisionByZero, 'x / 0', sign)
+
+ if not self:
+ exp = self._exp - other._exp
+ coeff = 0
+ else:
+ # OK, so neither = 0, INF or NaN
+ shift = len(other._int) - len(self._int) + context.prec + 1
+ exp = self._exp - other._exp - shift
+ op1 = _WorkRep(self)
+ op2 = _WorkRep(other)
+ if shift >= 0:
+ coeff, remainder = divmod(op1.int * 10**shift, op2.int)
+ else:
+ coeff, remainder = divmod(op1.int, op2.int * 10**-shift)
+ if remainder:
+ # result is not exact; adjust to ensure correct rounding
+ if coeff % 5 == 0:
+ coeff += 1
+ else:
+ # result is exact; get as close to ideal exponent as possible
+ ideal_exp = self._exp - other._exp
+ while exp < ideal_exp and coeff % 10 == 0:
+ coeff //= 10
+ exp += 1
+
+ ans = _dec_from_triple(sign, str(coeff), exp)
+ return ans._fix(context)
+
+ def _divide(self, other, context):
+ """Return (self // other, self % other), to context.prec precision.
+
+ Assumes that neither self nor other is a NaN, that self is not
+ infinite and that other is nonzero.
+ """
+ sign = self._sign ^ other._sign
+ if other._isinfinity():
+ ideal_exp = self._exp
+ else:
+ ideal_exp = min(self._exp, other._exp)
+
+ expdiff = self.adjusted() - other.adjusted()
+ if not self or other._isinfinity() or expdiff <= -2:
+ return (_dec_from_triple(sign, '0', 0),
+ self._rescale(ideal_exp, context.rounding))
+ if expdiff <= context.prec:
+ op1 = _WorkRep(self)
+ op2 = _WorkRep(other)
+ if op1.exp >= op2.exp:
+ op1.int *= 10**(op1.exp - op2.exp)
+ else:
+ op2.int *= 10**(op2.exp - op1.exp)
+ q, r = divmod(op1.int, op2.int)
+ if q < 10**context.prec:
+ return (_dec_from_triple(sign, str(q), 0),
+ _dec_from_triple(self._sign, str(r), ideal_exp))
+
+ # Here the quotient is too large to be representable
+ ans = context._raise_error(DivisionImpossible,
+ 'quotient too large in //, % or divmod')
+ return ans, ans
+
+ def __rtruediv__(self, other, context=None):
+ """Swaps self/other and returns __truediv__."""
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+ return other.__truediv__(self, context=context)
+
+ def __divmod__(self, other, context=None):
+ """
+ Return (self // other, self % other)
+ """
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+
+ if context is None:
+ context = getcontext()
+
+ ans = self._check_nans(other, context)
+ if ans:
+ return (ans, ans)
+
+ sign = self._sign ^ other._sign
+ if self._isinfinity():
+ if other._isinfinity():
+ ans = context._raise_error(InvalidOperation, 'divmod(INF, INF)')
+ return ans, ans
+ else:
+ return (_SignedInfinity[sign],
+ context._raise_error(InvalidOperation, 'INF % x'))
+
+ if not other:
+ if not self:
+ ans = context._raise_error(DivisionUndefined, 'divmod(0, 0)')
+ return ans, ans
+ else:
+ return (context._raise_error(DivisionByZero, 'x // 0', sign),
+ context._raise_error(InvalidOperation, 'x % 0'))
+
+ quotient, remainder = self._divide(other, context)
+ remainder = remainder._fix(context)
+ return quotient, remainder
+
+ def __rdivmod__(self, other, context=None):
+ """Swaps self/other and returns __divmod__."""
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+ return other.__divmod__(self, context=context)
+
+ def __mod__(self, other, context=None):
+ """
+ self % other
+ """
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+
+ if context is None:
+ context = getcontext()
+
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ if self._isinfinity():
+ return context._raise_error(InvalidOperation, 'INF % x')
+ elif not other:
+ if self:
+ return context._raise_error(InvalidOperation, 'x % 0')
+ else:
+ return context._raise_error(DivisionUndefined, '0 % 0')
+
+ remainder = self._divide(other, context)[1]
+ remainder = remainder._fix(context)
+ return remainder
+
+ def __rmod__(self, other, context=None):
+ """Swaps self/other and returns __mod__."""
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+ return other.__mod__(self, context=context)
+
+ def remainder_near(self, other, context=None):
+ """
+ Remainder nearest to 0- abs(remainder-near) <= other/2
+ """
+ if context is None:
+ context = getcontext()
+
+ other = _convert_other(other, raiseit=True)
+
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ # self == +/-infinity -> InvalidOperation
+ if self._isinfinity():
+ return context._raise_error(InvalidOperation,
+ 'remainder_near(infinity, x)')
+
+ # other == 0 -> either InvalidOperation or DivisionUndefined
+ if not other:
+ if self:
+ return context._raise_error(InvalidOperation,
+ 'remainder_near(x, 0)')
+ else:
+ return context._raise_error(DivisionUndefined,
+ 'remainder_near(0, 0)')
+
+ # other = +/-infinity -> remainder = self
+ if other._isinfinity():
+ ans = Decimal(self)
+ return ans._fix(context)
+
+ # self = 0 -> remainder = self, with ideal exponent
+ ideal_exponent = min(self._exp, other._exp)
+ if not self:
+ ans = _dec_from_triple(self._sign, '0', ideal_exponent)
+ return ans._fix(context)
+
+ # catch most cases of large or small quotient
+ expdiff = self.adjusted() - other.adjusted()
+ if expdiff >= context.prec + 1:
+ # expdiff >= prec+1 => abs(self/other) > 10**prec
+ return context._raise_error(DivisionImpossible)
+ if expdiff <= -2:
+ # expdiff <= -2 => abs(self/other) < 0.1
+ ans = self._rescale(ideal_exponent, context.rounding)
+ return ans._fix(context)
+
+ # adjust both arguments to have the same exponent, then divide
+ op1 = _WorkRep(self)
+ op2 = _WorkRep(other)
+ if op1.exp >= op2.exp:
+ op1.int *= 10**(op1.exp - op2.exp)
+ else:
+ op2.int *= 10**(op2.exp - op1.exp)
+ q, r = divmod(op1.int, op2.int)
+ # remainder is r*10**ideal_exponent; other is +/-op2.int *
+ # 10**ideal_exponent. Apply correction to ensure that
+ # abs(remainder) <= abs(other)/2
+ if 2*r + (q&1) > op2.int:
+ r -= op2.int
+ q += 1
+
+ if q >= 10**context.prec:
+ return context._raise_error(DivisionImpossible)
+
+ # result has same sign as self unless r is negative
+ sign = self._sign
+ if r < 0:
+ sign = 1-sign
+ r = -r
+
+ ans = _dec_from_triple(sign, str(r), ideal_exponent)
+ return ans._fix(context)
+
+ def __floordiv__(self, other, context=None):
+ """self // other"""
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+
+ if context is None:
+ context = getcontext()
+
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ if self._isinfinity():
+ if other._isinfinity():
+ return context._raise_error(InvalidOperation, 'INF // INF')
+ else:
+ return _SignedInfinity[self._sign ^ other._sign]
+
+ if not other:
+ if self:
+ return context._raise_error(DivisionByZero, 'x // 0',
+ self._sign ^ other._sign)
+ else:
+ return context._raise_error(DivisionUndefined, '0 // 0')
+
+ return self._divide(other, context)[0]
+
+ def __rfloordiv__(self, other, context=None):
+ """Swaps self/other and returns __floordiv__."""
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+ return other.__floordiv__(self, context=context)
+
+ def __float__(self):
+ """Float representation."""
+ if self._isnan():
+ if self.is_snan():
+ raise ValueError("Cannot convert signaling NaN to float")
+ s = "-nan" if self._sign else "nan"
+ else:
+ s = str(self)
+ return float(s)
+
+ def __int__(self):
+ """Converts self to an int, truncating if necessary."""
+ if self._is_special:
+ if self._isnan():
+ raise ValueError("Cannot convert NaN to integer")
+ elif self._isinfinity():
+ raise OverflowError("Cannot convert infinity to integer")
+ s = (-1)**self._sign
+ if self._exp >= 0:
+ return s*int(self._int)*10**self._exp
+ else:
+ return s*int(self._int[:self._exp] or '0')
+
+ __trunc__ = __int__
+
+ def real(self):
+ return self
+ real = property(real)
+
+ def imag(self):
+ return Decimal(0)
+ imag = property(imag)
+
+ def conjugate(self):
+ return self
+
+ def __complex__(self):
+ return complex(float(self))
+
+ def _fix_nan(self, context):
+ """Decapitate the payload of a NaN to fit the context"""
+ payload = self._int
+
+ # maximum length of payload is precision if clamp=0,
+ # precision-1 if clamp=1.
+ max_payload_len = context.prec - context.clamp
+ if len(payload) > max_payload_len:
+ payload = payload[len(payload)-max_payload_len:].lstrip('0')
+ return _dec_from_triple(self._sign, payload, self._exp, True)
+ return Decimal(self)
+
+ def _fix(self, context):
+ """Round if it is necessary to keep self within prec precision.
+
+ Rounds and fixes the exponent. Does not raise on a sNaN.
+
+ Arguments:
+ self - Decimal instance
+ context - context used.
+ """
+
+ if self._is_special:
+ if self._isnan():
+ # decapitate payload if necessary
+ return self._fix_nan(context)
+ else:
+ # self is +/-Infinity; return unaltered
+ return Decimal(self)
+
+ # if self is zero then exponent should be between Etiny and
+ # Emax if clamp==0, and between Etiny and Etop if clamp==1.
+ Etiny = context.Etiny()
+ Etop = context.Etop()
+ if not self:
+ exp_max = [context.Emax, Etop][context.clamp]
+ new_exp = min(max(self._exp, Etiny), exp_max)
+ if new_exp != self._exp:
+ context._raise_error(Clamped)
+ return _dec_from_triple(self._sign, '0', new_exp)
+ else:
+ return Decimal(self)
+
+ # exp_min is the smallest allowable exponent of the result,
+ # equal to max(self.adjusted()-context.prec+1, Etiny)
+ exp_min = len(self._int) + self._exp - context.prec
+ if exp_min > Etop:
+ # overflow: exp_min > Etop iff self.adjusted() > Emax
+ ans = context._raise_error(Overflow, 'above Emax', self._sign)
+ context._raise_error(Inexact)
+ context._raise_error(Rounded)
+ return ans
+
+ self_is_subnormal = exp_min < Etiny
+ if self_is_subnormal:
+ exp_min = Etiny
+
+ # round if self has too many digits
+ if self._exp < exp_min:
+ digits = len(self._int) + self._exp - exp_min
+ if digits < 0:
+ self = _dec_from_triple(self._sign, '1', exp_min-1)
+ digits = 0
+ rounding_method = self._pick_rounding_function[context.rounding]
+ changed = rounding_method(self, digits)
+ coeff = self._int[:digits] or '0'
+ if changed > 0:
+ coeff = str(int(coeff)+1)
+ if len(coeff) > context.prec:
+ coeff = coeff[:-1]
+ exp_min += 1
+
+ # check whether the rounding pushed the exponent out of range
+ if exp_min > Etop:
+ ans = context._raise_error(Overflow, 'above Emax', self._sign)
+ else:
+ ans = _dec_from_triple(self._sign, coeff, exp_min)
+
+ # raise the appropriate signals, taking care to respect
+ # the precedence described in the specification
+ if changed and self_is_subnormal:
+ context._raise_error(Underflow)
+ if self_is_subnormal:
+ context._raise_error(Subnormal)
+ if changed:
+ context._raise_error(Inexact)
+ context._raise_error(Rounded)
+ if not ans:
+ # raise Clamped on underflow to 0
+ context._raise_error(Clamped)
+ return ans
+
+ if self_is_subnormal:
+ context._raise_error(Subnormal)
+
+ # fold down if clamp == 1 and self has too few digits
+ if context.clamp == 1 and self._exp > Etop:
+ context._raise_error(Clamped)
+ self_padded = self._int + '0'*(self._exp - Etop)
+ return _dec_from_triple(self._sign, self_padded, Etop)
+
+ # here self was representable to begin with; return unchanged
+ return Decimal(self)
+
+ # for each of the rounding functions below:
+ # self is a finite, nonzero Decimal
+ # prec is an integer satisfying 0 <= prec < len(self._int)
+ #
+ # each function returns either -1, 0, or 1, as follows:
+ # 1 indicates that self should be rounded up (away from zero)
+ # 0 indicates that self should be truncated, and that all the
+ # digits to be truncated are zeros (so the value is unchanged)
+ # -1 indicates that there are nonzero digits to be truncated
+
+ def _round_down(self, prec):
+ """Also known as round-towards-0, truncate."""
+ if _all_zeros(self._int, prec):
+ return 0
+ else:
+ return -1
+
+ def _round_up(self, prec):
+ """Rounds away from 0."""
+ return -self._round_down(prec)
+
+ def _round_half_up(self, prec):
+ """Rounds 5 up (away from 0)"""
+ if self._int[prec] in '56789':
+ return 1
+ elif _all_zeros(self._int, prec):
+ return 0
+ else:
+ return -1
+
+ def _round_half_down(self, prec):
+ """Round 5 down"""
+ if _exact_half(self._int, prec):
+ return -1
+ else:
+ return self._round_half_up(prec)
+
+ def _round_half_even(self, prec):
+ """Round 5 to even, rest to nearest."""
+ if _exact_half(self._int, prec) and \
+ (prec == 0 or self._int[prec-1] in '02468'):
+ return -1
+ else:
+ return self._round_half_up(prec)
+
+ def _round_ceiling(self, prec):
+ """Rounds up (not away from 0 if negative.)"""
+ if self._sign:
+ return self._round_down(prec)
+ else:
+ return -self._round_down(prec)
+
+ def _round_floor(self, prec):
+ """Rounds down (not towards 0 if negative)"""
+ if not self._sign:
+ return self._round_down(prec)
+ else:
+ return -self._round_down(prec)
+
+ def _round_05up(self, prec):
+ """Round down unless digit prec-1 is 0 or 5."""
+ if prec and self._int[prec-1] not in '05':
+ return self._round_down(prec)
+ else:
+ return -self._round_down(prec)
+
+ _pick_rounding_function = dict(
+ ROUND_DOWN = _round_down,
+ ROUND_UP = _round_up,
+ ROUND_HALF_UP = _round_half_up,
+ ROUND_HALF_DOWN = _round_half_down,
+ ROUND_HALF_EVEN = _round_half_even,
+ ROUND_CEILING = _round_ceiling,
+ ROUND_FLOOR = _round_floor,
+ ROUND_05UP = _round_05up,
+ )
+
+ def __round__(self, n=None):
+ """Round self to the nearest integer, or to a given precision.
+
+ If only one argument is supplied, round a finite Decimal
+ instance self to the nearest integer. If self is infinite or
+ a NaN then a Python exception is raised. If self is finite
+ and lies exactly halfway between two integers then it is
+ rounded to the integer with even last digit.
+
+ >>> round(Decimal('123.456'))
+ 123
+ >>> round(Decimal('-456.789'))
+ -457
+ >>> round(Decimal('-3.0'))
+ -3
+ >>> round(Decimal('2.5'))
+ 2
+ >>> round(Decimal('3.5'))
+ 4
+ >>> round(Decimal('Inf'))
+ Traceback (most recent call last):
+ ...
+ OverflowError: cannot round an infinity
+ >>> round(Decimal('NaN'))
+ Traceback (most recent call last):
+ ...
+ ValueError: cannot round a NaN
+
+ If a second argument n is supplied, self is rounded to n
+ decimal places using the rounding mode for the current
+ context.
+
+ For an integer n, round(self, -n) is exactly equivalent to
+ self.quantize(Decimal('1En')).
+
+ >>> round(Decimal('123.456'), 0)
+ Decimal('123')
+ >>> round(Decimal('123.456'), 2)
+ Decimal('123.46')
+ >>> round(Decimal('123.456'), -2)
+ Decimal('1E+2')
+ >>> round(Decimal('-Infinity'), 37)
+ Decimal('NaN')
+ >>> round(Decimal('sNaN123'), 0)
+ Decimal('NaN123')
+
+ """
+ if n is not None:
+ # two-argument form: use the equivalent quantize call
+ if not isinstance(n, int):
+ raise TypeError('Second argument to round should be integral')
+ exp = _dec_from_triple(0, '1', -n)
+ return self.quantize(exp)
+
+ # one-argument form
+ if self._is_special:
+ if self.is_nan():
+ raise ValueError("cannot round a NaN")
+ else:
+ raise OverflowError("cannot round an infinity")
+ return int(self._rescale(0, ROUND_HALF_EVEN))
+
+ def __floor__(self):
+ """Return the floor of self, as an integer.
+
+ For a finite Decimal instance self, return the greatest
+ integer n such that n <= self. If self is infinite or a NaN
+ then a Python exception is raised.
+
+ """
+ if self._is_special:
+ if self.is_nan():
+ raise ValueError("cannot round a NaN")
+ else:
+ raise OverflowError("cannot round an infinity")
+ return int(self._rescale(0, ROUND_FLOOR))
+
+ def __ceil__(self):
+ """Return the ceiling of self, as an integer.
+
+ For a finite Decimal instance self, return the least integer n
+ such that n >= self. If self is infinite or a NaN then a
+ Python exception is raised.
+
+ """
+ if self._is_special:
+ if self.is_nan():
+ raise ValueError("cannot round a NaN")
+ else:
+ raise OverflowError("cannot round an infinity")
+ return int(self._rescale(0, ROUND_CEILING))
+
+ def fma(self, other, third, context=None):
+ """Fused multiply-add.
+
+ Returns self*other+third with no rounding of the intermediate
+ product self*other.
+
+ self and other are multiplied together, with no rounding of
+ the result. The third operand is then added to the result,
+ and a single final rounding is performed.
+ """
+
+ other = _convert_other(other, raiseit=True)
+ third = _convert_other(third, raiseit=True)
+
+ # compute product; raise InvalidOperation if either operand is
+ # a signaling NaN or if the product is zero times infinity.
+ if self._is_special or other._is_special:
+ if context is None:
+ context = getcontext()
+ if self._exp == 'N':
+ return context._raise_error(InvalidOperation, 'sNaN', self)
+ if other._exp == 'N':
+ return context._raise_error(InvalidOperation, 'sNaN', other)
+ if self._exp == 'n':
+ product = self
+ elif other._exp == 'n':
+ product = other
+ elif self._exp == 'F':
+ if not other:
+ return context._raise_error(InvalidOperation,
+ 'INF * 0 in fma')
+ product = _SignedInfinity[self._sign ^ other._sign]
+ elif other._exp == 'F':
+ if not self:
+ return context._raise_error(InvalidOperation,
+ '0 * INF in fma')
+ product = _SignedInfinity[self._sign ^ other._sign]
+ else:
+ product = _dec_from_triple(self._sign ^ other._sign,
+ str(int(self._int) * int(other._int)),
+ self._exp + other._exp)
+
+ return product.__add__(third, context)
+
+ def _power_modulo(self, other, modulo, context=None):
+ """Three argument version of __pow__"""
+
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+ modulo = _convert_other(modulo)
+ if modulo is NotImplemented:
+ return modulo
+
+ if context is None:
+ context = getcontext()
+
+ # deal with NaNs: if there are any sNaNs then first one wins,
+ # (i.e. behaviour for NaNs is identical to that of fma)
+ self_is_nan = self._isnan()
+ other_is_nan = other._isnan()
+ modulo_is_nan = modulo._isnan()
+ if self_is_nan or other_is_nan or modulo_is_nan:
+ if self_is_nan == 2:
+ return context._raise_error(InvalidOperation, 'sNaN',
+ self)
+ if other_is_nan == 2:
+ return context._raise_error(InvalidOperation, 'sNaN',
+ other)
+ if modulo_is_nan == 2:
+ return context._raise_error(InvalidOperation, 'sNaN',
+ modulo)
+ if self_is_nan:
+ return self._fix_nan(context)
+ if other_is_nan:
+ return other._fix_nan(context)
+ return modulo._fix_nan(context)
+
+ # check inputs: we apply same restrictions as Python's pow()
+ if not (self._isinteger() and
+ other._isinteger() and
+ modulo._isinteger()):
+ return context._raise_error(InvalidOperation,
+ 'pow() 3rd argument not allowed '
+ 'unless all arguments are integers')
+ if other < 0:
+ return context._raise_error(InvalidOperation,
+ 'pow() 2nd argument cannot be '
+ 'negative when 3rd argument specified')
+ if not modulo:
+ return context._raise_error(InvalidOperation,
+ 'pow() 3rd argument cannot be 0')
+
+ # additional restriction for decimal: the modulus must be less
+ # than 10**prec in absolute value
+ if modulo.adjusted() >= context.prec:
+ return context._raise_error(InvalidOperation,
+ 'insufficient precision: pow() 3rd '
+ 'argument must not have more than '
+ 'precision digits')
+
+ # define 0**0 == NaN, for consistency with two-argument pow
+ # (even though it hurts!)
+ if not other and not self:
+ return context._raise_error(InvalidOperation,
+ 'at least one of pow() 1st argument '
+ 'and 2nd argument must be nonzero ;'
+ '0**0 is not defined')
+
+ # compute sign of result
+ if other._iseven():
+ sign = 0
+ else:
+ sign = self._sign
+
+ # convert modulo to a Python integer, and self and other to
+ # Decimal integers (i.e. force their exponents to be >= 0)
+ modulo = abs(int(modulo))
+ base = _WorkRep(self.to_integral_value())
+ exponent = _WorkRep(other.to_integral_value())
+
+ # compute result using integer pow()
+ base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo
+ for i in range(exponent.exp):
+ base = pow(base, 10, modulo)
+ base = pow(base, exponent.int, modulo)
+
+ return _dec_from_triple(sign, str(base), 0)
+
+ def _power_exact(self, other, p):
+ """Attempt to compute self**other exactly.
+
+ Given Decimals self and other and an integer p, attempt to
+ compute an exact result for the power self**other, with p
+ digits of precision. Return None if self**other is not
+ exactly representable in p digits.
+
+ Assumes that elimination of special cases has already been
+ performed: self and other must both be nonspecial; self must
+ be positive and not numerically equal to 1; other must be
+ nonzero. For efficiency, other._exp should not be too large,
+ so that 10**abs(other._exp) is a feasible calculation."""
+
+ # In the comments below, we write x for the value of self and y for the
+ # value of other. Write x = xc*10**xe and abs(y) = yc*10**ye, with xc
+ # and yc positive integers not divisible by 10.
+
+ # The main purpose of this method is to identify the *failure*
+ # of x**y to be exactly representable with as little effort as
+ # possible. So we look for cheap and easy tests that
+ # eliminate the possibility of x**y being exact. Only if all
+ # these tests are passed do we go on to actually compute x**y.
+
+ # Here's the main idea. Express y as a rational number m/n, with m and
+ # n relatively prime and n>0. Then for x**y to be exactly
+ # representable (at *any* precision), xc must be the nth power of a
+ # positive integer and xe must be divisible by n. If y is negative
+ # then additionally xc must be a power of either 2 or 5, hence a power
+ # of 2**n or 5**n.
+ #
+ # There's a limit to how small |y| can be: if y=m/n as above
+ # then:
+ #
+ # (1) if xc != 1 then for the result to be representable we
+ # need xc**(1/n) >= 2, and hence also xc**|y| >= 2. So
+ # if |y| <= 1/nbits(xc) then xc < 2**nbits(xc) <=
+ # 2**(1/|y|), hence xc**|y| < 2 and the result is not
+ # representable.
+ #
+ # (2) if xe != 0, |xe|*(1/n) >= 1, so |xe|*|y| >= 1. Hence if
+ # |y| < 1/|xe| then the result is not representable.
+ #
+ # Note that since x is not equal to 1, at least one of (1) and
+ # (2) must apply. Now |y| < 1/nbits(xc) iff |yc|*nbits(xc) <
+ # 10**-ye iff len(str(|yc|*nbits(xc)) <= -ye.
+ #
+ # There's also a limit to how large y can be, at least if it's
+ # positive: the normalized result will have coefficient xc**y,
+ # so if it's representable then xc**y < 10**p, and y <
+ # p/log10(xc). Hence if y*log10(xc) >= p then the result is
+ # not exactly representable.
+
+ # if len(str(abs(yc*xe)) <= -ye then abs(yc*xe) < 10**-ye,
+ # so |y| < 1/xe and the result is not representable.
+ # Similarly, len(str(abs(yc)*xc_bits)) <= -ye implies |y|
+ # < 1/nbits(xc).
+
+ x = _WorkRep(self)
+ xc, xe = x.int, x.exp
+ while xc % 10 == 0:
+ xc //= 10
+ xe += 1
+
+ y = _WorkRep(other)
+ yc, ye = y.int, y.exp
+ while yc % 10 == 0:
+ yc //= 10
+ ye += 1
+
+ # case where xc == 1: result is 10**(xe*y), with xe*y
+ # required to be an integer
+ if xc == 1:
+ xe *= yc
+ # result is now 10**(xe * 10**ye); xe * 10**ye must be integral
+ while xe % 10 == 0:
+ xe //= 10
+ ye += 1
+ if ye < 0:
+ return None
+ exponent = xe * 10**ye
+ if y.sign == 1:
+ exponent = -exponent
+ # if other is a nonnegative integer, use ideal exponent
+ if other._isinteger() and other._sign == 0:
+ ideal_exponent = self._exp*int(other)
+ zeros = min(exponent-ideal_exponent, p-1)
+ else:
+ zeros = 0
+ return _dec_from_triple(0, '1' + '0'*zeros, exponent-zeros)
+
+ # case where y is negative: xc must be either a power
+ # of 2 or a power of 5.
+ if y.sign == 1:
+ last_digit = xc % 10
+ if last_digit in (2,4,6,8):
+ # quick test for power of 2
+ if xc & -xc != xc:
+ return None
+ # now xc is a power of 2; e is its exponent
+ e = _nbits(xc)-1
+
+ # We now have:
+ #
+ # x = 2**e * 10**xe, e > 0, and y < 0.
+ #
+ # The exact result is:
+ #
+ # x**y = 5**(-e*y) * 10**(e*y + xe*y)
+ #
+ # provided that both e*y and xe*y are integers. Note that if
+ # 5**(-e*y) >= 10**p, then the result can't be expressed
+ # exactly with p digits of precision.
+ #
+ # Using the above, we can guard against large values of ye.
+ # 93/65 is an upper bound for log(10)/log(5), so if
+ #
+ # ye >= len(str(93*p//65))
+ #
+ # then
+ #
+ # -e*y >= -y >= 10**ye > 93*p/65 > p*log(10)/log(5),
+ #
+ # so 5**(-e*y) >= 10**p, and the coefficient of the result
+ # can't be expressed in p digits.
+
+ # emax >= largest e such that 5**e < 10**p.
+ emax = p*93//65
+ if ye >= len(str(emax)):
+ return None
+
+ # Find -e*y and -xe*y; both must be integers
+ e = _decimal_lshift_exact(e * yc, ye)
+ xe = _decimal_lshift_exact(xe * yc, ye)
+ if e is None or xe is None:
+ return None
+
+ if e > emax:
+ return None
+ xc = 5**e
+
+ elif last_digit == 5:
+ # e >= log_5(xc) if xc is a power of 5; we have
+ # equality all the way up to xc=5**2658
+ e = _nbits(xc)*28//65
+ xc, remainder = divmod(5**e, xc)
+ if remainder:
+ return None
+ while xc % 5 == 0:
+ xc //= 5
+ e -= 1
+
+ # Guard against large values of ye, using the same logic as in
+ # the 'xc is a power of 2' branch. 10/3 is an upper bound for
+ # log(10)/log(2).
+ emax = p*10//3
+ if ye >= len(str(emax)):
+ return None
+
+ e = _decimal_lshift_exact(e * yc, ye)
+ xe = _decimal_lshift_exact(xe * yc, ye)
+ if e is None or xe is None:
+ return None
+
+ if e > emax:
+ return None
+ xc = 2**e
+ else:
+ return None
+
+ if xc >= 10**p:
+ return None
+ xe = -e-xe
+ return _dec_from_triple(0, str(xc), xe)
+
+ # now y is positive; find m and n such that y = m/n
+ if ye >= 0:
+ m, n = yc*10**ye, 1
+ else:
+ if xe != 0 and len(str(abs(yc*xe))) <= -ye:
+ return None
+ xc_bits = _nbits(xc)
+ if xc != 1 and len(str(abs(yc)*xc_bits)) <= -ye:
+ return None
+ m, n = yc, 10**(-ye)
+ while m % 2 == n % 2 == 0:
+ m //= 2
+ n //= 2
+ while m % 5 == n % 5 == 0:
+ m //= 5
+ n //= 5
+
+ # compute nth root of xc*10**xe
+ if n > 1:
+ # if 1 < xc < 2**n then xc isn't an nth power
+ if xc != 1 and xc_bits <= n:
+ return None
+
+ xe, rem = divmod(xe, n)
+ if rem != 0:
+ return None
+
+ # compute nth root of xc using Newton's method
+ a = 1 << -(-_nbits(xc)//n) # initial estimate
+ while True:
+ q, r = divmod(xc, a**(n-1))
+ if a <= q:
+ break
+ else:
+ a = (a*(n-1) + q)//n
+ if not (a == q and r == 0):
+ return None
+ xc = a
+
+ # now xc*10**xe is the nth root of the original xc*10**xe
+ # compute mth power of xc*10**xe
+
+ # if m > p*100//_log10_lb(xc) then m > p/log10(xc), hence xc**m >
+ # 10**p and the result is not representable.
+ if xc > 1 and m > p*100//_log10_lb(xc):
+ return None
+ xc = xc**m
+ xe *= m
+ if xc > 10**p:
+ return None
+
+ # by this point the result *is* exactly representable
+ # adjust the exponent to get as close as possible to the ideal
+ # exponent, if necessary
+ str_xc = str(xc)
+ if other._isinteger() and other._sign == 0:
+ ideal_exponent = self._exp*int(other)
+ zeros = min(xe-ideal_exponent, p-len(str_xc))
+ else:
+ zeros = 0
+ return _dec_from_triple(0, str_xc+'0'*zeros, xe-zeros)
+
+ def __pow__(self, other, modulo=None, context=None):
+ """Return self ** other [ % modulo].
+
+ With two arguments, compute self**other.
+
+ With three arguments, compute (self**other) % modulo. For the
+ three argument form, the following restrictions on the
+ arguments hold:
+
+ - all three arguments must be integral
+ - other must be nonnegative
+ - either self or other (or both) must be nonzero
+ - modulo must be nonzero and must have at most p digits,
+ where p is the context precision.
+
+ If any of these restrictions is violated the InvalidOperation
+ flag is raised.
+
+ The result of pow(self, other, modulo) is identical to the
+ result that would be obtained by computing (self**other) %
+ modulo with unbounded precision, but is computed more
+ efficiently. It is always exact.
+ """
+
+ if modulo is not None:
+ return self._power_modulo(other, modulo, context)
+
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+
+ if context is None:
+ context = getcontext()
+
+ # either argument is a NaN => result is NaN
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ # 0**0 = NaN (!), x**0 = 1 for nonzero x (including +/-Infinity)
+ if not other:
+ if not self:
+ return context._raise_error(InvalidOperation, '0 ** 0')
+ else:
+ return _One
+
+ # result has sign 1 iff self._sign is 1 and other is an odd integer
+ result_sign = 0
+ if self._sign == 1:
+ if other._isinteger():
+ if not other._iseven():
+ result_sign = 1
+ else:
+ # -ve**noninteger = NaN
+ # (-0)**noninteger = 0**noninteger
+ if self:
+ return context._raise_error(InvalidOperation,
+ 'x ** y with x negative and y not an integer')
+ # negate self, without doing any unwanted rounding
+ self = self.copy_negate()
+
+ # 0**(+ve or Inf)= 0; 0**(-ve or -Inf) = Infinity
+ if not self:
+ if other._sign == 0:
+ return _dec_from_triple(result_sign, '0', 0)
+ else:
+ return _SignedInfinity[result_sign]
+
+ # Inf**(+ve or Inf) = Inf; Inf**(-ve or -Inf) = 0
+ if self._isinfinity():
+ if other._sign == 0:
+ return _SignedInfinity[result_sign]
+ else:
+ return _dec_from_triple(result_sign, '0', 0)
+
+ # 1**other = 1, but the choice of exponent and the flags
+ # depend on the exponent of self, and on whether other is a
+ # positive integer, a negative integer, or neither
+ if self == _One:
+ if other._isinteger():
+ # exp = max(self._exp*max(int(other), 0),
+ # 1-context.prec) but evaluating int(other) directly
+ # is dangerous until we know other is small (other
+ # could be 1e999999999)
+ if other._sign == 1:
+ multiplier = 0
+ elif other > context.prec:
+ multiplier = context.prec
+ else:
+ multiplier = int(other)
+
+ exp = self._exp * multiplier
+ if exp < 1-context.prec:
+ exp = 1-context.prec
+ context._raise_error(Rounded)
+ else:
+ context._raise_error(Inexact)
+ context._raise_error(Rounded)
+ exp = 1-context.prec
+
+ return _dec_from_triple(result_sign, '1'+'0'*-exp, exp)
+
+ # compute adjusted exponent of self
+ self_adj = self.adjusted()
+
+ # self ** infinity is infinity if self > 1, 0 if self < 1
+ # self ** -infinity is infinity if self < 1, 0 if self > 1
+ if other._isinfinity():
+ if (other._sign == 0) == (self_adj < 0):
+ return _dec_from_triple(result_sign, '0', 0)
+ else:
+ return _SignedInfinity[result_sign]
+
+ # from here on, the result always goes through the call
+ # to _fix at the end of this function.
+ ans = None
+ exact = False
+
+ # crude test to catch cases of extreme overflow/underflow. If
+ # log10(self)*other >= 10**bound and bound >= len(str(Emax))
+ # then 10**bound >= 10**len(str(Emax)) >= Emax+1 and hence
+ # self**other >= 10**(Emax+1), so overflow occurs. The test
+ # for underflow is similar.
+ bound = self._log10_exp_bound() + other.adjusted()
+ if (self_adj >= 0) == (other._sign == 0):
+ # self > 1 and other +ve, or self < 1 and other -ve
+ # possibility of overflow
+ if bound >= len(str(context.Emax)):
+ ans = _dec_from_triple(result_sign, '1', context.Emax+1)
+ else:
+ # self > 1 and other -ve, or self < 1 and other +ve
+ # possibility of underflow to 0
+ Etiny = context.Etiny()
+ if bound >= len(str(-Etiny)):
+ ans = _dec_from_triple(result_sign, '1', Etiny-1)
+
+ # try for an exact result with precision +1
+ if ans is None:
+ ans = self._power_exact(other, context.prec + 1)
+ if ans is not None:
+ if result_sign == 1:
+ ans = _dec_from_triple(1, ans._int, ans._exp)
+ exact = True
+
+ # usual case: inexact result, x**y computed directly as exp(y*log(x))
+ if ans is None:
+ p = context.prec
+ x = _WorkRep(self)
+ xc, xe = x.int, x.exp
+ y = _WorkRep(other)
+ yc, ye = y.int, y.exp
+ if y.sign == 1:
+ yc = -yc
+
+ # compute correctly rounded result: start with precision +3,
+ # then increase precision until result is unambiguously roundable
+ extra = 3
+ while True:
+ coeff, exp = _dpower(xc, xe, yc, ye, p+extra)
+ if coeff % (5*10**(len(str(coeff))-p-1)):
+ break
+ extra += 3
+
+ ans = _dec_from_triple(result_sign, str(coeff), exp)
+
+ # unlike exp, ln and log10, the power function respects the
+ # rounding mode; no need to switch to ROUND_HALF_EVEN here
+
+ # There's a difficulty here when 'other' is not an integer and
+ # the result is exact. In this case, the specification
+ # requires that the Inexact flag be raised (in spite of
+ # exactness), but since the result is exact _fix won't do this
+ # for us. (Correspondingly, the Underflow signal should also
+ # be raised for subnormal results.) We can't directly raise
+ # these signals either before or after calling _fix, since
+ # that would violate the precedence for signals. So we wrap
+ # the ._fix call in a temporary context, and reraise
+ # afterwards.
+ if exact and not other._isinteger():
+ # pad with zeros up to length context.prec+1 if necessary; this
+ # ensures that the Rounded signal will be raised.
+ if len(ans._int) <= context.prec:
+ expdiff = context.prec + 1 - len(ans._int)
+ ans = _dec_from_triple(ans._sign, ans._int+'0'*expdiff,
+ ans._exp-expdiff)
+
+ # create a copy of the current context, with cleared flags/traps
+ newcontext = context.copy()
+ newcontext.clear_flags()
+ for exception in _signals:
+ newcontext.traps[exception] = 0
+
+ # round in the new context
+ ans = ans._fix(newcontext)
+
+ # raise Inexact, and if necessary, Underflow
+ newcontext._raise_error(Inexact)
+ if newcontext.flags[Subnormal]:
+ newcontext._raise_error(Underflow)
+
+ # propagate signals to the original context; _fix could
+ # have raised any of Overflow, Underflow, Subnormal,
+ # Inexact, Rounded, Clamped. Overflow needs the correct
+ # arguments. Note that the order of the exceptions is
+ # important here.
+ if newcontext.flags[Overflow]:
+ context._raise_error(Overflow, 'above Emax', ans._sign)
+ for exception in Underflow, Subnormal, Inexact, Rounded, Clamped:
+ if newcontext.flags[exception]:
+ context._raise_error(exception)
+
+ else:
+ ans = ans._fix(context)
+
+ return ans
+
+ def __rpow__(self, other, context=None):
+ """Swaps self/other and returns __pow__."""
+ other = _convert_other(other)
+ if other is NotImplemented:
+ return other
+ return other.__pow__(self, context=context)
+
+ def normalize(self, context=None):
+ """Normalize- strip trailing 0s, change anything equal to 0 to 0e0"""
+
+ if context is None:
+ context = getcontext()
+
+ if self._is_special:
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ dup = self._fix(context)
+ if dup._isinfinity():
+ return dup
+
+ if not dup:
+ return _dec_from_triple(dup._sign, '0', 0)
+ exp_max = [context.Emax, context.Etop()][context.clamp]
+ end = len(dup._int)
+ exp = dup._exp
+ while dup._int[end-1] == '0' and exp < exp_max:
+ exp += 1
+ end -= 1
+ return _dec_from_triple(dup._sign, dup._int[:end], exp)
+
+ def quantize(self, exp, rounding=None, context=None):
+ """Quantize self so its exponent is the same as that of exp.
+
+ Similar to self._rescale(exp._exp) but with error checking.
+ """
+ exp = _convert_other(exp, raiseit=True)
+
+ if context is None:
+ context = getcontext()
+ if rounding is None:
+ rounding = context.rounding
+
+ if self._is_special or exp._is_special:
+ ans = self._check_nans(exp, context)
+ if ans:
+ return ans
+
+ if exp._isinfinity() or self._isinfinity():
+ if exp._isinfinity() and self._isinfinity():
+ return Decimal(self) # if both are inf, it is OK
+ return context._raise_error(InvalidOperation,
+ 'quantize with one INF')
+
+ # exp._exp should be between Etiny and Emax
+ if not (context.Etiny() <= exp._exp <= context.Emax):
+ return context._raise_error(InvalidOperation,
+ 'target exponent out of bounds in quantize')
+
+ if not self:
+ ans = _dec_from_triple(self._sign, '0', exp._exp)
+ return ans._fix(context)
+
+ self_adjusted = self.adjusted()
+ if self_adjusted > context.Emax:
+ return context._raise_error(InvalidOperation,
+ 'exponent of quantize result too large for current context')
+ if self_adjusted - exp._exp + 1 > context.prec:
+ return context._raise_error(InvalidOperation,
+ 'quantize result has too many digits for current context')
+
+ ans = self._rescale(exp._exp, rounding)
+ if ans.adjusted() > context.Emax:
+ return context._raise_error(InvalidOperation,
+ 'exponent of quantize result too large for current context')
+ if len(ans._int) > context.prec:
+ return context._raise_error(InvalidOperation,
+ 'quantize result has too many digits for current context')
+
+ # raise appropriate flags
+ if ans and ans.adjusted() < context.Emin:
+ context._raise_error(Subnormal)
+ if ans._exp > self._exp:
+ if ans != self:
+ context._raise_error(Inexact)
+ context._raise_error(Rounded)
+
+ # call to fix takes care of any necessary folddown, and
+ # signals Clamped if necessary
+ ans = ans._fix(context)
+ return ans
+
+ def same_quantum(self, other, context=None):
+ """Return True if self and other have the same exponent; otherwise
+ return False.
+
+ If either operand is a special value, the following rules are used:
+ * return True if both operands are infinities
+ * return True if both operands are NaNs
+ * otherwise, return False.
+ """
+ other = _convert_other(other, raiseit=True)
+ if self._is_special or other._is_special:
+ return (self.is_nan() and other.is_nan() or
+ self.is_infinite() and other.is_infinite())
+ return self._exp == other._exp
+
+ def _rescale(self, exp, rounding):
+ """Rescale self so that the exponent is exp, either by padding with zeros
+ or by truncating digits, using the given rounding mode.
+
+ Specials are returned without change. This operation is
+ quiet: it raises no flags, and uses no information from the
+ context.
+
+ exp = exp to scale to (an integer)
+ rounding = rounding mode
+ """
+ if self._is_special:
+ return Decimal(self)
+ if not self:
+ return _dec_from_triple(self._sign, '0', exp)
+
+ if self._exp >= exp:
+ # pad answer with zeros if necessary
+ return _dec_from_triple(self._sign,
+ self._int + '0'*(self._exp - exp), exp)
+
+ # too many digits; round and lose data. If self.adjusted() <
+ # exp-1, replace self by 10**(exp-1) before rounding
+ digits = len(self._int) + self._exp - exp
+ if digits < 0:
+ self = _dec_from_triple(self._sign, '1', exp-1)
+ digits = 0
+ this_function = self._pick_rounding_function[rounding]
+ changed = this_function(self, digits)
+ coeff = self._int[:digits] or '0'
+ if changed == 1:
+ coeff = str(int(coeff)+1)
+ return _dec_from_triple(self._sign, coeff, exp)
+
+ def _round(self, places, rounding):
+ """Round a nonzero, nonspecial Decimal to a fixed number of
+ significant figures, using the given rounding mode.
+
+ Infinities, NaNs and zeros are returned unaltered.
+
+ This operation is quiet: it raises no flags, and uses no
+ information from the context.
+
+ """
+ if places <= 0:
+ raise ValueError("argument should be at least 1 in _round")
+ if self._is_special or not self:
+ return Decimal(self)
+ ans = self._rescale(self.adjusted()+1-places, rounding)
+ # it can happen that the rescale alters the adjusted exponent;
+ # for example when rounding 99.97 to 3 significant figures.
+ # When this happens we end up with an extra 0 at the end of
+ # the number; a second rescale fixes this.
+ if ans.adjusted() != self.adjusted():
+ ans = ans._rescale(ans.adjusted()+1-places, rounding)
+ return ans
+
+ def to_integral_exact(self, rounding=None, context=None):
+ """Rounds to a nearby integer.
+
+ If no rounding mode is specified, take the rounding mode from
+ the context. This method raises the Rounded and Inexact flags
+ when appropriate.
+
+ See also: to_integral_value, which does exactly the same as
+ this method except that it doesn't raise Inexact or Rounded.
+ """
+ if self._is_special:
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+ return Decimal(self)
+ if self._exp >= 0:
+ return Decimal(self)
+ if not self:
+ return _dec_from_triple(self._sign, '0', 0)
+ if context is None:
+ context = getcontext()
+ if rounding is None:
+ rounding = context.rounding
+ ans = self._rescale(0, rounding)
+ if ans != self:
+ context._raise_error(Inexact)
+ context._raise_error(Rounded)
+ return ans
+
+ def to_integral_value(self, rounding=None, context=None):
+ """Rounds to the nearest integer, without raising inexact, rounded."""
+ if context is None:
+ context = getcontext()
+ if rounding is None:
+ rounding = context.rounding
+ if self._is_special:
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+ return Decimal(self)
+ if self._exp >= 0:
+ return Decimal(self)
+ else:
+ return self._rescale(0, rounding)
+
+ # the method name changed, but we provide also the old one, for compatibility
+ to_integral = to_integral_value
+
+ def sqrt(self, context=None):
+ """Return the square root of self."""
+ if context is None:
+ context = getcontext()
+
+ if self._is_special:
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ if self._isinfinity() and self._sign == 0:
+ return Decimal(self)
+
+ if not self:
+ # exponent = self._exp // 2. sqrt(-0) = -0
+ ans = _dec_from_triple(self._sign, '0', self._exp // 2)
+ return ans._fix(context)
+
+ if self._sign == 1:
+ return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0')
+
+ # At this point self represents a positive number. Let p be
+ # the desired precision and express self in the form c*100**e
+ # with c a positive real number and e an integer, c and e
+ # being chosen so that 100**(p-1) <= c < 100**p. Then the
+ # (exact) square root of self is sqrt(c)*10**e, and 10**(p-1)
+ # <= sqrt(c) < 10**p, so the closest representable Decimal at
+ # precision p is n*10**e where n = round_half_even(sqrt(c)),
+ # the closest integer to sqrt(c) with the even integer chosen
+ # in the case of a tie.
+ #
+ # To ensure correct rounding in all cases, we use the
+ # following trick: we compute the square root to an extra
+ # place (precision p+1 instead of precision p), rounding down.
+ # Then, if the result is inexact and its last digit is 0 or 5,
+ # we increase the last digit to 1 or 6 respectively; if it's
+ # exact we leave the last digit alone. Now the final round to
+ # p places (or fewer in the case of underflow) will round
+ # correctly and raise the appropriate flags.
+
+ # use an extra digit of precision
+ prec = context.prec+1
+
+ # write argument in the form c*100**e where e = self._exp//2
+ # is the 'ideal' exponent, to be used if the square root is
+ # exactly representable. l is the number of 'digits' of c in
+ # base 100, so that 100**(l-1) <= c < 100**l.
+ op = _WorkRep(self)
+ e = op.exp >> 1
+ if op.exp & 1:
+ c = op.int * 10
+ l = (len(self._int) >> 1) + 1
+ else:
+ c = op.int
+ l = len(self._int)+1 >> 1
+
+ # rescale so that c has exactly prec base 100 'digits'
+ shift = prec-l
+ if shift >= 0:
+ c *= 100**shift
+ exact = True
+ else:
+ c, remainder = divmod(c, 100**-shift)
+ exact = not remainder
+ e -= shift
+
+ # find n = floor(sqrt(c)) using Newton's method
+ n = 10**prec
+ while True:
+ q = c//n
+ if n <= q:
+ break
+ else:
+ n = n + q >> 1
+ exact = exact and n*n == c
+
+ if exact:
+ # result is exact; rescale to use ideal exponent e
+ if shift >= 0:
+ # assert n % 10**shift == 0
+ n //= 10**shift
+ else:
+ n *= 10**-shift
+ e += shift
+ else:
+ # result is not exact; fix last digit as described above
+ if n % 5 == 0:
+ n += 1
+
+ ans = _dec_from_triple(0, str(n), e)
+
+ # round, and fit to current context
+ context = context._shallow_copy()
+ rounding = context._set_rounding(ROUND_HALF_EVEN)
+ ans = ans._fix(context)
+ context.rounding = rounding
+
+ return ans
+
+ def max(self, other, context=None):
+ """Returns the larger value.
+
+ Like max(self, other) except if one is not a number, returns
+ NaN (and signals if one is sNaN). Also rounds.
+ """
+ other = _convert_other(other, raiseit=True)
+
+ if context is None:
+ context = getcontext()
+
+ if self._is_special or other._is_special:
+ # If one operand is a quiet NaN and the other is number, then the
+ # number is always returned
+ sn = self._isnan()
+ on = other._isnan()
+ if sn or on:
+ if on == 1 and sn == 0:
+ return self._fix(context)
+ if sn == 1 and on == 0:
+ return other._fix(context)
+ return self._check_nans(other, context)
+
+ c = self._cmp(other)
+ if c == 0:
+ # If both operands are finite and equal in numerical value
+ # then an ordering is applied:
+ #
+ # If the signs differ then max returns the operand with the
+ # positive sign and min returns the operand with the negative sign
+ #
+ # If the signs are the same then the exponent is used to select
+ # the result. This is exactly the ordering used in compare_total.
+ c = self.compare_total(other)
+
+ if c == -1:
+ ans = other
+ else:
+ ans = self
+
+ return ans._fix(context)
+
+ def min(self, other, context=None):
+ """Returns the smaller value.
+
+ Like min(self, other) except if one is not a number, returns
+ NaN (and signals if one is sNaN). Also rounds.
+ """
+ other = _convert_other(other, raiseit=True)
+
+ if context is None:
+ context = getcontext()
+
+ if self._is_special or other._is_special:
+ # If one operand is a quiet NaN and the other is number, then the
+ # number is always returned
+ sn = self._isnan()
+ on = other._isnan()
+ if sn or on:
+ if on == 1 and sn == 0:
+ return self._fix(context)
+ if sn == 1 and on == 0:
+ return other._fix(context)
+ return self._check_nans(other, context)
+
+ c = self._cmp(other)
+ if c == 0:
+ c = self.compare_total(other)
+
+ if c == -1:
+ ans = self
+ else:
+ ans = other
+
+ return ans._fix(context)
+
+ def _isinteger(self):
+ """Returns whether self is an integer"""
+ if self._is_special:
+ return False
+ if self._exp >= 0:
+ return True
+ rest = self._int[self._exp:]
+ return rest == '0'*len(rest)
+
+ def _iseven(self):
+ """Returns True if self is even. Assumes self is an integer."""
+ if not self or self._exp > 0:
+ return True
+ return self._int[-1+self._exp] in '02468'
+
+ def adjusted(self):
+ """Return the adjusted exponent of self"""
+ try:
+ return self._exp + len(self._int) - 1
+ # If NaN or Infinity, self._exp is string
+ except TypeError:
+ return 0
+
+ def canonical(self):
+ """Returns the same Decimal object.
+
+ As we do not have different encodings for the same number, the
+ received object already is in its canonical form.
+ """
+ return self
+
+ def compare_signal(self, other, context=None):
+ """Compares self to the other operand numerically.
+
+ It's pretty much like compare(), but all NaNs signal, with signaling
+ NaNs taking precedence over quiet NaNs.
+ """
+ other = _convert_other(other, raiseit = True)
+ ans = self._compare_check_nans(other, context)
+ if ans:
+ return ans
+ return self.compare(other, context=context)
+
+ def compare_total(self, other, context=None):
+ """Compares self to other using the abstract representations.
+
+ This is not like the standard compare, which use their numerical
+ value. Note that a total ordering is defined for all possible abstract
+ representations.
+ """
+ other = _convert_other(other, raiseit=True)
+
+ # if one is negative and the other is positive, it's easy
+ if self._sign and not other._sign:
+ return _NegativeOne
+ if not self._sign and other._sign:
+ return _One
+ sign = self._sign
+
+ # let's handle both NaN types
+ self_nan = self._isnan()
+ other_nan = other._isnan()
+ if self_nan or other_nan:
+ if self_nan == other_nan:
+ # compare payloads as though they're integers
+ self_key = len(self._int), self._int
+ other_key = len(other._int), other._int
+ if self_key < other_key:
+ if sign:
+ return _One
+ else:
+ return _NegativeOne
+ if self_key > other_key:
+ if sign:
+ return _NegativeOne
+ else:
+ return _One
+ return _Zero
+
+ if sign:
+ if self_nan == 1:
+ return _NegativeOne
+ if other_nan == 1:
+ return _One
+ if self_nan == 2:
+ return _NegativeOne
+ if other_nan == 2:
+ return _One
+ else:
+ if self_nan == 1:
+ return _One
+ if other_nan == 1:
+ return _NegativeOne
+ if self_nan == 2:
+ return _One
+ if other_nan == 2:
+ return _NegativeOne
+
+ if self < other:
+ return _NegativeOne
+ if self > other:
+ return _One
+
+ if self._exp < other._exp:
+ if sign:
+ return _One
+ else:
+ return _NegativeOne
+ if self._exp > other._exp:
+ if sign:
+ return _NegativeOne
+ else:
+ return _One
+ return _Zero
+
+
+ def compare_total_mag(self, other, context=None):
+ """Compares self to other using abstract repr., ignoring sign.
+
+ Like compare_total, but with operand's sign ignored and assumed to be 0.
+ """
+ other = _convert_other(other, raiseit=True)
+
+ s = self.copy_abs()
+ o = other.copy_abs()
+ return s.compare_total(o)
+
+ def copy_abs(self):
+ """Returns a copy with the sign set to 0. """
+ return _dec_from_triple(0, self._int, self._exp, self._is_special)
+
+ def copy_negate(self):
+ """Returns a copy with the sign inverted."""
+ if self._sign:
+ return _dec_from_triple(0, self._int, self._exp, self._is_special)
+ else:
+ return _dec_from_triple(1, self._int, self._exp, self._is_special)
+
+ def copy_sign(self, other, context=None):
+ """Returns self with the sign of other."""
+ other = _convert_other(other, raiseit=True)
+ return _dec_from_triple(other._sign, self._int,
+ self._exp, self._is_special)
+
+ def exp(self, context=None):
+ """Returns e ** self."""
+
+ if context is None:
+ context = getcontext()
+
+ # exp(NaN) = NaN
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ # exp(-Infinity) = 0
+ if self._isinfinity() == -1:
+ return _Zero
+
+ # exp(0) = 1
+ if not self:
+ return _One
+
+ # exp(Infinity) = Infinity
+ if self._isinfinity() == 1:
+ return Decimal(self)
+
+ # the result is now guaranteed to be inexact (the true
+ # mathematical result is transcendental). There's no need to
+ # raise Rounded and Inexact here---they'll always be raised as
+ # a result of the call to _fix.
+ p = context.prec
+ adj = self.adjusted()
+
+ # we only need to do any computation for quite a small range
+ # of adjusted exponents---for example, -29 <= adj <= 10 for
+ # the default context. For smaller exponent the result is
+ # indistinguishable from 1 at the given precision, while for
+ # larger exponent the result either overflows or underflows.
+ if self._sign == 0 and adj > len(str((context.Emax+1)*3)):
+ # overflow
+ ans = _dec_from_triple(0, '1', context.Emax+1)
+ elif self._sign == 1 and adj > len(str((-context.Etiny()+1)*3)):
+ # underflow to 0
+ ans = _dec_from_triple(0, '1', context.Etiny()-1)
+ elif self._sign == 0 and adj < -p:
+ # p+1 digits; final round will raise correct flags
+ ans = _dec_from_triple(0, '1' + '0'*(p-1) + '1', -p)
+ elif self._sign == 1 and adj < -p-1:
+ # p+1 digits; final round will raise correct flags
+ ans = _dec_from_triple(0, '9'*(p+1), -p-1)
+ # general case
+ else:
+ op = _WorkRep(self)
+ c, e = op.int, op.exp
+ if op.sign == 1:
+ c = -c
+
+ # compute correctly rounded result: increase precision by
+ # 3 digits at a time until we get an unambiguously
+ # roundable result
+ extra = 3
+ while True:
+ coeff, exp = _dexp(c, e, p+extra)
+ if coeff % (5*10**(len(str(coeff))-p-1)):
+ break
+ extra += 3
+
+ ans = _dec_from_triple(0, str(coeff), exp)
+
+ # at this stage, ans should round correctly with *any*
+ # rounding mode, not just with ROUND_HALF_EVEN
+ context = context._shallow_copy()
+ rounding = context._set_rounding(ROUND_HALF_EVEN)
+ ans = ans._fix(context)
+ context.rounding = rounding
+
+ return ans
+
+ def is_canonical(self):
+ """Return True if self is canonical; otherwise return False.
+
+ Currently, the encoding of a Decimal instance is always
+ canonical, so this method returns True for any Decimal.
+ """
+ return True
+
+ def is_finite(self):
+ """Return True if self is finite; otherwise return False.
+
+ A Decimal instance is considered finite if it is neither
+ infinite nor a NaN.
+ """
+ return not self._is_special
+
+ def is_infinite(self):
+ """Return True if self is infinite; otherwise return False."""
+ return self._exp == 'F'
+
+ def is_nan(self):
+ """Return True if self is a qNaN or sNaN; otherwise return False."""
+ return self._exp in ('n', 'N')
+
+ def is_normal(self, context=None):
+ """Return True if self is a normal number; otherwise return False."""
+ if self._is_special or not self:
+ return False
+ if context is None:
+ context = getcontext()
+ return context.Emin <= self.adjusted()
+
+ def is_qnan(self):
+ """Return True if self is a quiet NaN; otherwise return False."""
+ return self._exp == 'n'
+
+ def is_signed(self):
+ """Return True if self is negative; otherwise return False."""
+ return self._sign == 1
+
+ def is_snan(self):
+ """Return True if self is a signaling NaN; otherwise return False."""
+ return self._exp == 'N'
+
+ def is_subnormal(self, context=None):
+ """Return True if self is subnormal; otherwise return False."""
+ if self._is_special or not self:
+ return False
+ if context is None:
+ context = getcontext()
+ return self.adjusted() < context.Emin
+
+ def is_zero(self):
+ """Return True if self is a zero; otherwise return False."""
+ return not self._is_special and self._int == '0'
+
+ def _ln_exp_bound(self):
+ """Compute a lower bound for the adjusted exponent of self.ln().
+ In other words, compute r such that self.ln() >= 10**r. Assumes
+ that self is finite and positive and that self != 1.
+ """
+
+ # for 0.1 <= x <= 10 we use the inequalities 1-1/x <= ln(x) <= x-1
+ adj = self._exp + len(self._int) - 1
+ if adj >= 1:
+ # argument >= 10; we use 23/10 = 2.3 as a lower bound for ln(10)
+ return len(str(adj*23//10)) - 1
+ if adj <= -2:
+ # argument <= 0.1
+ return len(str((-1-adj)*23//10)) - 1
+ op = _WorkRep(self)
+ c, e = op.int, op.exp
+ if adj == 0:
+ # 1 < self < 10
+ num = str(c-10**-e)
+ den = str(c)
+ return len(num) - len(den) - (num < den)
+ # adj == -1, 0.1 <= self < 1
+ return e + len(str(10**-e - c)) - 1
+
+
+ def ln(self, context=None):
+ """Returns the natural (base e) logarithm of self."""
+
+ if context is None:
+ context = getcontext()
+
+ # ln(NaN) = NaN
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ # ln(0.0) == -Infinity
+ if not self:
+ return _NegativeInfinity
+
+ # ln(Infinity) = Infinity
+ if self._isinfinity() == 1:
+ return _Infinity
+
+ # ln(1.0) == 0.0
+ if self == _One:
+ return _Zero
+
+ # ln(negative) raises InvalidOperation
+ if self._sign == 1:
+ return context._raise_error(InvalidOperation,
+ 'ln of a negative value')
+
+ # result is irrational, so necessarily inexact
+ op = _WorkRep(self)
+ c, e = op.int, op.exp
+ p = context.prec
+
+ # correctly rounded result: repeatedly increase precision by 3
+ # until we get an unambiguously roundable result
+ places = p - self._ln_exp_bound() + 2 # at least p+3 places
+ while True:
+ coeff = _dlog(c, e, places)
+ # assert len(str(abs(coeff)))-p >= 1
+ if coeff % (5*10**(len(str(abs(coeff)))-p-1)):
+ break
+ places += 3
+ ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places)
+
+ context = context._shallow_copy()
+ rounding = context._set_rounding(ROUND_HALF_EVEN)
+ ans = ans._fix(context)
+ context.rounding = rounding
+ return ans
+
+ def _log10_exp_bound(self):
+ """Compute a lower bound for the adjusted exponent of self.log10().
+ In other words, find r such that self.log10() >= 10**r.
+ Assumes that self is finite and positive and that self != 1.
+ """
+
+ # For x >= 10 or x < 0.1 we only need a bound on the integer
+ # part of log10(self), and this comes directly from the
+ # exponent of x. For 0.1 <= x <= 10 we use the inequalities
+ # 1-1/x <= log(x) <= x-1. If x > 1 we have |log10(x)| >
+ # (1-1/x)/2.31 > 0. If x < 1 then |log10(x)| > (1-x)/2.31 > 0
+
+ adj = self._exp + len(self._int) - 1
+ if adj >= 1:
+ # self >= 10
+ return len(str(adj))-1
+ if adj <= -2:
+ # self < 0.1
+ return len(str(-1-adj))-1
+ op = _WorkRep(self)
+ c, e = op.int, op.exp
+ if adj == 0:
+ # 1 < self < 10
+ num = str(c-10**-e)
+ den = str(231*c)
+ return len(num) - len(den) - (num < den) + 2
+ # adj == -1, 0.1 <= self < 1
+ num = str(10**-e-c)
+ return len(num) + e - (num < "231") - 1
+
+ def log10(self, context=None):
+ """Returns the base 10 logarithm of self."""
+
+ if context is None:
+ context = getcontext()
+
+ # log10(NaN) = NaN
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ # log10(0.0) == -Infinity
+ if not self:
+ return _NegativeInfinity
+
+ # log10(Infinity) = Infinity
+ if self._isinfinity() == 1:
+ return _Infinity
+
+ # log10(negative or -Infinity) raises InvalidOperation
+ if self._sign == 1:
+ return context._raise_error(InvalidOperation,
+ 'log10 of a negative value')
+
+ # log10(10**n) = n
+ if self._int[0] == '1' and self._int[1:] == '0'*(len(self._int) - 1):
+ # answer may need rounding
+ ans = Decimal(self._exp + len(self._int) - 1)
+ else:
+ # result is irrational, so necessarily inexact
+ op = _WorkRep(self)
+ c, e = op.int, op.exp
+ p = context.prec
+
+ # correctly rounded result: repeatedly increase precision
+ # until result is unambiguously roundable
+ places = p-self._log10_exp_bound()+2
+ while True:
+ coeff = _dlog10(c, e, places)
+ # assert len(str(abs(coeff)))-p >= 1
+ if coeff % (5*10**(len(str(abs(coeff)))-p-1)):
+ break
+ places += 3
+ ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places)
+
+ context = context._shallow_copy()
+ rounding = context._set_rounding(ROUND_HALF_EVEN)
+ ans = ans._fix(context)
+ context.rounding = rounding
+ return ans
+
+ def logb(self, context=None):
+ """ Returns the exponent of the magnitude of self's MSD.
+
+ The result is the integer which is the exponent of the magnitude
+ of the most significant digit of self (as though it were truncated
+ to a single digit while maintaining the value of that digit and
+ without limiting the resulting exponent).
+ """
+ # logb(NaN) = NaN
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ if context is None:
+ context = getcontext()
+
+ # logb(+/-Inf) = +Inf
+ if self._isinfinity():
+ return _Infinity
+
+ # logb(0) = -Inf, DivisionByZero
+ if not self:
+ return context._raise_error(DivisionByZero, 'logb(0)', 1)
+
+ # otherwise, simply return the adjusted exponent of self, as a
+ # Decimal. Note that no attempt is made to fit the result
+ # into the current context.
+ ans = Decimal(self.adjusted())
+ return ans._fix(context)
+
+ def _islogical(self):
+ """Return True if self is a logical operand.
+
+ For being logical, it must be a finite number with a sign of 0,
+ an exponent of 0, and a coefficient whose digits must all be
+ either 0 or 1.
+ """
+ if self._sign != 0 or self._exp != 0:
+ return False
+ for dig in self._int:
+ if dig not in '01':
+ return False
+ return True
+
+ def _fill_logical(self, context, opa, opb):
+ dif = context.prec - len(opa)
+ if dif > 0:
+ opa = '0'*dif + opa
+ elif dif < 0:
+ opa = opa[-context.prec:]
+ dif = context.prec - len(opb)
+ if dif > 0:
+ opb = '0'*dif + opb
+ elif dif < 0:
+ opb = opb[-context.prec:]
+ return opa, opb
+
+ def logical_and(self, other, context=None):
+ """Applies an 'and' operation between self and other's digits."""
+ if context is None:
+ context = getcontext()
+
+ other = _convert_other(other, raiseit=True)
+
+ if not self._islogical() or not other._islogical():
+ return context._raise_error(InvalidOperation)
+
+ # fill to context.prec
+ (opa, opb) = self._fill_logical(context, self._int, other._int)
+
+ # make the operation, and clean starting zeroes
+ result = "".join([str(int(a)&int(b)) for a,b in zip(opa,opb)])
+ return _dec_from_triple(0, result.lstrip('0') or '0', 0)
+
+ def logical_invert(self, context=None):
+ """Invert all its digits."""
+ if context is None:
+ context = getcontext()
+ return self.logical_xor(_dec_from_triple(0,'1'*context.prec,0),
+ context)
+
+ def logical_or(self, other, context=None):
+ """Applies an 'or' operation between self and other's digits."""
+ if context is None:
+ context = getcontext()
+
+ other = _convert_other(other, raiseit=True)
+
+ if not self._islogical() or not other._islogical():
+ return context._raise_error(InvalidOperation)
+
+ # fill to context.prec
+ (opa, opb) = self._fill_logical(context, self._int, other._int)
+
+ # make the operation, and clean starting zeroes
+ result = "".join([str(int(a)|int(b)) for a,b in zip(opa,opb)])
+ return _dec_from_triple(0, result.lstrip('0') or '0', 0)
+
+ def logical_xor(self, other, context=None):
+ """Applies an 'xor' operation between self and other's digits."""
+ if context is None:
+ context = getcontext()
+
+ other = _convert_other(other, raiseit=True)
+
+ if not self._islogical() or not other._islogical():
+ return context._raise_error(InvalidOperation)
+
+ # fill to context.prec
+ (opa, opb) = self._fill_logical(context, self._int, other._int)
+
+ # make the operation, and clean starting zeroes
+ result = "".join([str(int(a)^int(b)) for a,b in zip(opa,opb)])
+ return _dec_from_triple(0, result.lstrip('0') or '0', 0)
+
+ def max_mag(self, other, context=None):
+ """Compares the values numerically with their sign ignored."""
+ other = _convert_other(other, raiseit=True)
+
+ if context is None:
+ context = getcontext()
+
+ if self._is_special or other._is_special:
+ # If one operand is a quiet NaN and the other is number, then the
+ # number is always returned
+ sn = self._isnan()
+ on = other._isnan()
+ if sn or on:
+ if on == 1 and sn == 0:
+ return self._fix(context)
+ if sn == 1 and on == 0:
+ return other._fix(context)
+ return self._check_nans(other, context)
+
+ c = self.copy_abs()._cmp(other.copy_abs())
+ if c == 0:
+ c = self.compare_total(other)
+
+ if c == -1:
+ ans = other
+ else:
+ ans = self
+
+ return ans._fix(context)
+
+ def min_mag(self, other, context=None):
+ """Compares the values numerically with their sign ignored."""
+ other = _convert_other(other, raiseit=True)
+
+ if context is None:
+ context = getcontext()
+
+ if self._is_special or other._is_special:
+ # If one operand is a quiet NaN and the other is number, then the
+ # number is always returned
+ sn = self._isnan()
+ on = other._isnan()
+ if sn or on:
+ if on == 1 and sn == 0:
+ return self._fix(context)
+ if sn == 1 and on == 0:
+ return other._fix(context)
+ return self._check_nans(other, context)
+
+ c = self.copy_abs()._cmp(other.copy_abs())
+ if c == 0:
+ c = self.compare_total(other)
+
+ if c == -1:
+ ans = self
+ else:
+ ans = other
+
+ return ans._fix(context)
+
+ def next_minus(self, context=None):
+ """Returns the largest representable number smaller than itself."""
+ if context is None:
+ context = getcontext()
+
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ if self._isinfinity() == -1:
+ return _NegativeInfinity
+ if self._isinfinity() == 1:
+ return _dec_from_triple(0, '9'*context.prec, context.Etop())
+
+ context = context.copy()
+ context._set_rounding(ROUND_FLOOR)
+ context._ignore_all_flags()
+ new_self = self._fix(context)
+ if new_self != self:
+ return new_self
+ return self.__sub__(_dec_from_triple(0, '1', context.Etiny()-1),
+ context)
+
+ def next_plus(self, context=None):
+ """Returns the smallest representable number larger than itself."""
+ if context is None:
+ context = getcontext()
+
+ ans = self._check_nans(context=context)
+ if ans:
+ return ans
+
+ if self._isinfinity() == 1:
+ return _Infinity
+ if self._isinfinity() == -1:
+ return _dec_from_triple(1, '9'*context.prec, context.Etop())
+
+ context = context.copy()
+ context._set_rounding(ROUND_CEILING)
+ context._ignore_all_flags()
+ new_self = self._fix(context)
+ if new_self != self:
+ return new_self
+ return self.__add__(_dec_from_triple(0, '1', context.Etiny()-1),
+ context)
+
+ def next_toward(self, other, context=None):
+ """Returns the number closest to self, in the direction towards other.
+
+ The result is the closest representable number to self
+ (excluding self) that is in the direction towards other,
+ unless both have the same value. If the two operands are
+ numerically equal, then the result is a copy of self with the
+ sign set to be the same as the sign of other.
+ """
+ other = _convert_other(other, raiseit=True)
+
+ if context is None:
+ context = getcontext()
+
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ comparison = self._cmp(other)
+ if comparison == 0:
+ return self.copy_sign(other)
+
+ if comparison == -1:
+ ans = self.next_plus(context)
+ else: # comparison == 1
+ ans = self.next_minus(context)
+
+ # decide which flags to raise using value of ans
+ if ans._isinfinity():
+ context._raise_error(Overflow,
+ 'Infinite result from next_toward',
+ ans._sign)
+ context._raise_error(Inexact)
+ context._raise_error(Rounded)
+ elif ans.adjusted() < context.Emin:
+ context._raise_error(Underflow)
+ context._raise_error(Subnormal)
+ context._raise_error(Inexact)
+ context._raise_error(Rounded)
+ # if precision == 1 then we don't raise Clamped for a
+ # result 0E-Etiny.
+ if not ans:
+ context._raise_error(Clamped)
+
+ return ans
+
+ def number_class(self, context=None):
+ """Returns an indication of the class of self.
+
+ The class is one of the following strings:
+ sNaN
+ NaN
+ -Infinity
+ -Normal
+ -Subnormal
+ -Zero
+ +Zero
+ +Subnormal
+ +Normal
+ +Infinity
+ """
+ if self.is_snan():
+ return "sNaN"
+ if self.is_qnan():
+ return "NaN"
+ inf = self._isinfinity()
+ if inf == 1:
+ return "+Infinity"
+ if inf == -1:
+ return "-Infinity"
+ if self.is_zero():
+ if self._sign:
+ return "-Zero"
+ else:
+ return "+Zero"
+ if context is None:
+ context = getcontext()
+ if self.is_subnormal(context=context):
+ if self._sign:
+ return "-Subnormal"
+ else:
+ return "+Subnormal"
+ # just a normal, regular, boring number, :)
+ if self._sign:
+ return "-Normal"
+ else:
+ return "+Normal"
+
+ def radix(self):
+ """Just returns 10, as this is Decimal, :)"""
+ return Decimal(10)
+
+ def rotate(self, other, context=None):
+ """Returns a rotated copy of self, value-of-other times."""
+ if context is None:
+ context = getcontext()
+
+ other = _convert_other(other, raiseit=True)
+
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ if other._exp != 0:
+ return context._raise_error(InvalidOperation)
+ if not (-context.prec <= int(other) <= context.prec):
+ return context._raise_error(InvalidOperation)
+
+ if self._isinfinity():
+ return Decimal(self)
+
+ # get values, pad if necessary
+ torot = int(other)
+ rotdig = self._int
+ topad = context.prec - len(rotdig)
+ if topad > 0:
+ rotdig = '0'*topad + rotdig
+ elif topad < 0:
+ rotdig = rotdig[-topad:]
+
+ # let's rotate!
+ rotated = rotdig[torot:] + rotdig[:torot]
+ return _dec_from_triple(self._sign,
+ rotated.lstrip('0') or '0', self._exp)
+
+ def scaleb(self, other, context=None):
+ """Returns self operand after adding the second value to its exp."""
+ if context is None:
+ context = getcontext()
+
+ other = _convert_other(other, raiseit=True)
+
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ if other._exp != 0:
+ return context._raise_error(InvalidOperation)
+ liminf = -2 * (context.Emax + context.prec)
+ limsup = 2 * (context.Emax + context.prec)
+ if not (liminf <= int(other) <= limsup):
+ return context._raise_error(InvalidOperation)
+
+ if self._isinfinity():
+ return Decimal(self)
+
+ d = _dec_from_triple(self._sign, self._int, self._exp + int(other))
+ d = d._fix(context)
+ return d
+
+ def shift(self, other, context=None):
+ """Returns a shifted copy of self, value-of-other times."""
+ if context is None:
+ context = getcontext()
+
+ other = _convert_other(other, raiseit=True)
+
+ ans = self._check_nans(other, context)
+ if ans:
+ return ans
+
+ if other._exp != 0:
+ return context._raise_error(InvalidOperation)
+ if not (-context.prec <= int(other) <= context.prec):
+ return context._raise_error(InvalidOperation)
+
+ if self._isinfinity():
+ return Decimal(self)
+
+ # get values, pad if necessary
+ torot = int(other)
+ rotdig = self._int
+ topad = context.prec - len(rotdig)
+ if topad > 0:
+ rotdig = '0'*topad + rotdig
+ elif topad < 0:
+ rotdig = rotdig[-topad:]
+
+ # let's shift!
+ if torot < 0:
+ shifted = rotdig[:torot]
+ else:
+ shifted = rotdig + '0'*torot
+ shifted = shifted[-context.prec:]
+
+ return _dec_from_triple(self._sign,
+ shifted.lstrip('0') or '0', self._exp)
+
+ # Support for pickling, copy, and deepcopy
+ def __reduce__(self):
+ return (self.__class__, (str(self),))
+
+ def __copy__(self):
+ if type(self) is Decimal:
+ return self # I'm immutable; therefore I am my own clone
+ return self.__class__(str(self))
+
+ def __deepcopy__(self, memo):
+ if type(self) is Decimal:
+ return self # My components are also immutable
+ return self.__class__(str(self))
+
+ # PEP 3101 support. the _localeconv keyword argument should be
+ # considered private: it's provided for ease of testing only.
+ def __format__(self, specifier, context=None, _localeconv=None):
+ """Format a Decimal instance according to the given specifier.
+
+ The specifier should be a standard format specifier, with the
+ form described in PEP 3101. Formatting types 'e', 'E', 'f',
+ 'F', 'g', 'G', 'n' and '%' are supported. If the formatting
+ type is omitted it defaults to 'g' or 'G', depending on the
+ value of context.capitals.
+ """
+
+ # Note: PEP 3101 says that if the type is not present then
+ # there should be at least one digit after the decimal point.
+ # We take the liberty of ignoring this requirement for
+ # Decimal---it's presumably there to make sure that
+ # format(float, '') behaves similarly to str(float).
+ if context is None:
+ context = getcontext()
+
+ spec = _parse_format_specifier(specifier, _localeconv=_localeconv)
+
+ # special values don't care about the type or precision
+ if self._is_special:
+ sign = _format_sign(self._sign, spec)
+ body = str(self.copy_abs())
+ if spec['type'] == '%':
+ body += '%'
+ return _format_align(sign, body, spec)
+
+ # a type of None defaults to 'g' or 'G', depending on context
+ if spec['type'] is None:
+ spec['type'] = ['g', 'G'][context.capitals]
+
+ # if type is '%', adjust exponent of self accordingly
+ if spec['type'] == '%':
+ self = _dec_from_triple(self._sign, self._int, self._exp+2)
+
+ # round if necessary, taking rounding mode from the context
+ rounding = context.rounding
+ precision = spec['precision']
+ if precision is not None:
+ if spec['type'] in 'eE':
+ self = self._round(precision+1, rounding)
+ elif spec['type'] in 'fF%':
+ self = self._rescale(-precision, rounding)
+ elif spec['type'] in 'gG' and len(self._int) > precision:
+ self = self._round(precision, rounding)
+ # special case: zeros with a positive exponent can't be
+ # represented in fixed point; rescale them to 0e0.
+ if not self and self._exp > 0 and spec['type'] in 'fF%':
+ self = self._rescale(0, rounding)
+
+ # figure out placement of the decimal point
+ leftdigits = self._exp + len(self._int)
+ if spec['type'] in 'eE':
+ if not self and precision is not None:
+ dotplace = 1 - precision
+ else:
+ dotplace = 1
+ elif spec['type'] in 'fF%':
+ dotplace = leftdigits
+ elif spec['type'] in 'gG':
+ if self._exp <= 0 and leftdigits > -6:
+ dotplace = leftdigits
+ else:
+ dotplace = 1
+
+ # find digits before and after decimal point, and get exponent
+ if dotplace < 0:
+ intpart = '0'
+ fracpart = '0'*(-dotplace) + self._int
+ elif dotplace > len(self._int):
+ intpart = self._int + '0'*(dotplace-len(self._int))
+ fracpart = ''
+ else:
+ intpart = self._int[:dotplace] or '0'
+ fracpart = self._int[dotplace:]
+ exp = leftdigits-dotplace
+
+ # done with the decimal-specific stuff; hand over the rest
+ # of the formatting to the _format_number function
+ return _format_number(self._sign, intpart, fracpart, exp, spec)
+
+def _dec_from_triple(sign, coefficient, exponent, special=False):
+ """Create a decimal instance directly, without any validation,
+ normalization (e.g. removal of leading zeros) or argument
+ conversion.
+
+ This function is for *internal use only*.
+ """
+
+ self = object.__new__(Decimal)
+ self._sign = sign
+ self._int = coefficient
+ self._exp = exponent
+ self._is_special = special
+
+ return self
+
+# Register Decimal as a kind of Number (an abstract base class).
+# However, do not register it as Real (because Decimals are not
+# interoperable with floats).
+_numbers.Number.register(Decimal)
+
+
+##### Context class #######################################################
+
+class _ContextManager(object):
+ """Context manager class to support localcontext().
+
+ Sets a copy of the supplied context in __enter__() and restores
+ the previous decimal context in __exit__()
+ """
+ def __init__(self, new_context):
+ self.new_context = new_context.copy()
+ def __enter__(self):
+ self.saved_context = getcontext()
+ setcontext(self.new_context)
+ return self.new_context
+ def __exit__(self, t, v, tb):
+ setcontext(self.saved_context)
+
+class Context(object):
+ """Contains the context for a Decimal instance.
+
+ Contains:
+ prec - precision (for use in rounding, division, square roots..)
+ rounding - rounding type (how you round)
+ traps - If traps[exception] = 1, then the exception is
+ raised when it is caused. Otherwise, a value is
+ substituted in.
+ flags - When an exception is caused, flags[exception] is set.
+ (Whether or not the trap_enabler is set)
+ Should be reset by user of Decimal instance.
+ Emin - Minimum exponent
+ Emax - Maximum exponent
+ capitals - If 1, 1*10^1 is printed as 1E+1.
+ If 0, printed as 1e1
+ clamp - If 1, change exponents if too high (Default 0)
+ """
+
+ def __init__(self, prec=None, rounding=None, Emin=None, Emax=None,
+ capitals=None, clamp=None, flags=None, traps=None,
+ _ignored_flags=None):
+ # Set defaults; for everything except flags and _ignored_flags,
+ # inherit from DefaultContext.
+ try:
+ dc = DefaultContext
+ except NameError:
+ pass
+
+ self.prec = prec if prec is not None else dc.prec
+ self.rounding = rounding if rounding is not None else dc.rounding
+ self.Emin = Emin if Emin is not None else dc.Emin
+ self.Emax = Emax if Emax is not None else dc.Emax
+ self.capitals = capitals if capitals is not None else dc.capitals
+ self.clamp = clamp if clamp is not None else dc.clamp
+
+ if _ignored_flags is None:
+ self._ignored_flags = []
+ else:
+ self._ignored_flags = _ignored_flags
+
+ if traps is None:
+ self.traps = dc.traps.copy()
+ elif not isinstance(traps, dict):
+ self.traps = dict((s, int(s in traps)) for s in _signals + traps)
+ else:
+ self.traps = traps
+
+ if flags is None:
+ self.flags = dict.fromkeys(_signals, 0)
+ elif not isinstance(flags, dict):
+ self.flags = dict((s, int(s in flags)) for s in _signals + flags)
+ else:
+ self.flags = flags
+
+ def _set_integer_check(self, name, value, vmin, vmax):
+ if not isinstance(value, int):
+ raise TypeError("%s must be an integer" % name)
+ if vmin == '-inf':
+ if value > vmax:
+ raise ValueError("%s must be in [%s, %d]. got: %s" % (name, vmin, vmax, value))
+ elif vmax == 'inf':
+ if value < vmin:
+ raise ValueError("%s must be in [%d, %s]. got: %s" % (name, vmin, vmax, value))
+ else:
+ if value < vmin or value > vmax:
+ raise ValueError("%s must be in [%d, %d]. got %s" % (name, vmin, vmax, value))
+ return object.__setattr__(self, name, value)
+
+ def _set_signal_dict(self, name, d):
+ if not isinstance(d, dict):
+ raise TypeError("%s must be a signal dict" % d)
+ for key in d:
+ if not key in _signals:
+ raise KeyError("%s is not a valid signal dict" % d)
+ for key in _signals:
+ if not key in d:
+ raise KeyError("%s is not a valid signal dict" % d)
+ return object.__setattr__(self, name, d)
+
+ def __setattr__(self, name, value):
+ if name == 'prec':
+ return self._set_integer_check(name, value, 1, 'inf')
+ elif name == 'Emin':
+ return self._set_integer_check(name, value, '-inf', 0)
+ elif name == 'Emax':
+ return self._set_integer_check(name, value, 0, 'inf')
+ elif name == 'capitals':
+ return self._set_integer_check(name, value, 0, 1)
+ elif name == 'clamp':
+ return self._set_integer_check(name, value, 0, 1)
+ elif name == 'rounding':
+ if not value in _rounding_modes:
+ # raise TypeError even for strings to have consistency
+ # among various implementations.
+ raise TypeError("%s: invalid rounding mode" % value)
+ return object.__setattr__(self, name, value)
+ elif name == 'flags' or name == 'traps':
+ return self._set_signal_dict(name, value)
+ elif name == '_ignored_flags':
+ return object.__setattr__(self, name, value)
+ else:
+ raise AttributeError(
+ "'decimal.Context' object has no attribute '%s'" % name)
+
+ def __delattr__(self, name):
+ raise AttributeError("%s cannot be deleted" % name)
+
+ # Support for pickling, copy, and deepcopy
+ def __reduce__(self):
+ flags = [sig for sig, v in self.flags.items() if v]
+ traps = [sig for sig, v in self.traps.items() if v]
+ return (self.__class__,
+ (self.prec, self.rounding, self.Emin, self.Emax,
+ self.capitals, self.clamp, flags, traps))
+
+ def __repr__(self):
+ """Show the current context."""
+ s = []
+ s.append('Context(prec=%(prec)d, rounding=%(rounding)s, '
+ 'Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d, '
+ 'clamp=%(clamp)d'
+ % vars(self))
+ names = [f.__name__ for f, v in self.flags.items() if v]
+ s.append('flags=[' + ', '.join(names) + ']')
+ names = [t.__name__ for t, v in self.traps.items() if v]
+ s.append('traps=[' + ', '.join(names) + ']')
+ return ', '.join(s) + ')'
+
+ def clear_flags(self):
+ """Reset all flags to zero"""
+ for flag in self.flags:
+ self.flags[flag] = 0
+
+ def clear_traps(self):
+ """Reset all traps to zero"""
+ for flag in self.traps:
+ self.traps[flag] = 0
+
+ def _shallow_copy(self):
+ """Returns a shallow copy from self."""
+ nc = Context(self.prec, self.rounding, self.Emin, self.Emax,
+ self.capitals, self.clamp, self.flags, self.traps,
+ self._ignored_flags)
+ return nc
+
+ def copy(self):
+ """Returns a deep copy from self."""
+ nc = Context(self.prec, self.rounding, self.Emin, self.Emax,
+ self.capitals, self.clamp,
+ self.flags.copy(), self.traps.copy(),
+ self._ignored_flags)
+ return nc
+ __copy__ = copy
+
+ def _raise_error(self, condition, explanation = None, *args):
+ """Handles an error
+
+ If the flag is in _ignored_flags, returns the default response.
+ Otherwise, it sets the flag, then, if the corresponding
+ trap_enabler is set, it reraises the exception. Otherwise, it returns
+ the default value after setting the flag.
+ """
+ error = _condition_map.get(condition, condition)
+ if error in self._ignored_flags:
+ # Don't touch the flag
+ return error().handle(self, *args)
+
+ self.flags[error] = 1
+ if not self.traps[error]:
+ # The errors define how to handle themselves.
+ return condition().handle(self, *args)
+
+ # Errors should only be risked on copies of the context
+ # self._ignored_flags = []
+ raise error(explanation)
+
+ def _ignore_all_flags(self):
+ """Ignore all flags, if they are raised"""
+ return self._ignore_flags(*_signals)
+
+ def _ignore_flags(self, *flags):
+ """Ignore the flags, if they are raised"""
+ # Do not mutate-- This way, copies of a context leave the original
+ # alone.
+ self._ignored_flags = (self._ignored_flags + list(flags))
+ return list(flags)
+
+ def _regard_flags(self, *flags):
+ """Stop ignoring the flags, if they are raised"""
+ if flags and isinstance(flags[0], (tuple,list)):
+ flags = flags[0]
+ for flag in flags:
+ self._ignored_flags.remove(flag)
+
+ # We inherit object.__hash__, so we must deny this explicitly
+ __hash__ = None
+
+ def Etiny(self):
+ """Returns Etiny (= Emin - prec + 1)"""
+ return int(self.Emin - self.prec + 1)
+
+ def Etop(self):
+ """Returns maximum exponent (= Emax - prec + 1)"""
+ return int(self.Emax - self.prec + 1)
+
+ def _set_rounding(self, type):
+ """Sets the rounding type.
+
+ Sets the rounding type, and returns the current (previous)
+ rounding type. Often used like:
+
+ context = context.copy()
+ # so you don't change the calling context
+ # if an error occurs in the middle.
+ rounding = context._set_rounding(ROUND_UP)
+ val = self.__sub__(other, context=context)
+ context._set_rounding(rounding)
+
+ This will make it round up for that operation.
+ """
+ rounding = self.rounding
+ self.rounding= type
+ return rounding
+
+ def create_decimal(self, num='0'):
+ """Creates a new Decimal instance but using self as context.
+
+ This method implements the to-number operation of the
+ IBM Decimal specification."""
+
+ if isinstance(num, str) and num != num.strip():
+ return self._raise_error(ConversionSyntax,
+ "no trailing or leading whitespace is "
+ "permitted.")
+
+ d = Decimal(num, context=self)
+ if d._isnan() and len(d._int) > self.prec - self.clamp:
+ return self._raise_error(ConversionSyntax,
+ "diagnostic info too long in NaN")
+ return d._fix(self)
+
+ def create_decimal_from_float(self, f):
+ """Creates a new Decimal instance from a float but rounding using self
+ as the context.
+
+ >>> context = Context(prec=5, rounding=ROUND_DOWN)
+ >>> context.create_decimal_from_float(3.1415926535897932)
+ Decimal('3.1415')
+ >>> context = Context(prec=5, traps=[Inexact])
+ >>> context.create_decimal_from_float(3.1415926535897932)
+ Traceback (most recent call last):
+ ...
+ decimal.Inexact: None
+
+ """
+ d = Decimal.from_float(f) # An exact conversion
+ return d._fix(self) # Apply the context rounding
+
+ # Methods
+ def abs(self, a):
+ """Returns the absolute value of the operand.
+
+ If the operand is negative, the result is the same as using the minus
+ operation on the operand. Otherwise, the result is the same as using
+ the plus operation on the operand.
+
+ >>> ExtendedContext.abs(Decimal('2.1'))
+ Decimal('2.1')
+ >>> ExtendedContext.abs(Decimal('-100'))
+ Decimal('100')
+ >>> ExtendedContext.abs(Decimal('101.5'))
+ Decimal('101.5')
+ >>> ExtendedContext.abs(Decimal('-101.5'))
+ Decimal('101.5')
+ >>> ExtendedContext.abs(-1)
+ Decimal('1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.__abs__(context=self)
+
+ def add(self, a, b):
+ """Return the sum of the two operands.
+
+ >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
+ Decimal('19.00')
+ >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
+ Decimal('1.02E+4')
+ >>> ExtendedContext.add(1, Decimal(2))
+ Decimal('3')
+ >>> ExtendedContext.add(Decimal(8), 5)
+ Decimal('13')
+ >>> ExtendedContext.add(5, 5)
+ Decimal('10')
+ """
+ a = _convert_other(a, raiseit=True)
+ r = a.__add__(b, context=self)
+ if r is NotImplemented:
+ raise TypeError("Unable to convert %s to Decimal" % b)
+ else:
+ return r
+
+ def _apply(self, a):
+ return str(a._fix(self))
+
+ def canonical(self, a):
+ """Returns the same Decimal object.
+
+ As we do not have different encodings for the same number, the
+ received object already is in its canonical form.
+
+ >>> ExtendedContext.canonical(Decimal('2.50'))
+ Decimal('2.50')
+ """
+ if not isinstance(a, Decimal):
+ raise TypeError("canonical requires a Decimal as an argument.")
+ return a.canonical()
+
+ def compare(self, a, b):
+ """Compares values numerically.
+
+ If the signs of the operands differ, a value representing each operand
+ ('-1' if the operand is less than zero, '0' if the operand is zero or
+ negative zero, or '1' if the operand is greater than zero) is used in
+ place of that operand for the comparison instead of the actual
+ operand.
+
+ The comparison is then effected by subtracting the second operand from
+ the first and then returning a value according to the result of the
+ subtraction: '-1' if the result is less than zero, '0' if the result is
+ zero or negative zero, or '1' if the result is greater than zero.
+
+ >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
+ Decimal('-1')
+ >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
+ Decimal('0')
+ >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
+ Decimal('0')
+ >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
+ Decimal('1')
+ >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
+ Decimal('1')
+ >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
+ Decimal('-1')
+ >>> ExtendedContext.compare(1, 2)
+ Decimal('-1')
+ >>> ExtendedContext.compare(Decimal(1), 2)
+ Decimal('-1')
+ >>> ExtendedContext.compare(1, Decimal(2))
+ Decimal('-1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.compare(b, context=self)
+
+ def compare_signal(self, a, b):
+ """Compares the values of the two operands numerically.
+
+ It's pretty much like compare(), but all NaNs signal, with signaling
+ NaNs taking precedence over quiet NaNs.
+
+ >>> c = ExtendedContext
+ >>> c.compare_signal(Decimal('2.1'), Decimal('3'))
+ Decimal('-1')
+ >>> c.compare_signal(Decimal('2.1'), Decimal('2.1'))
+ Decimal('0')
+ >>> c.flags[InvalidOperation] = 0
+ >>> print(c.flags[InvalidOperation])
+ 0
+ >>> c.compare_signal(Decimal('NaN'), Decimal('2.1'))
+ Decimal('NaN')
+ >>> print(c.flags[InvalidOperation])
+ 1
+ >>> c.flags[InvalidOperation] = 0
+ >>> print(c.flags[InvalidOperation])
+ 0
+ >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1'))
+ Decimal('NaN')
+ >>> print(c.flags[InvalidOperation])
+ 1
+ >>> c.compare_signal(-1, 2)
+ Decimal('-1')
+ >>> c.compare_signal(Decimal(-1), 2)
+ Decimal('-1')
+ >>> c.compare_signal(-1, Decimal(2))
+ Decimal('-1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.compare_signal(b, context=self)
+
+ def compare_total(self, a, b):
+ """Compares two operands using their abstract representation.
+
+ This is not like the standard compare, which use their numerical
+ value. Note that a total ordering is defined for all possible abstract
+ representations.
+
+ >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9'))
+ Decimal('-1')
+ >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12'))
+ Decimal('-1')
+ >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3'))
+ Decimal('-1')
+ >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30'))
+ Decimal('0')
+ >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300'))
+ Decimal('1')
+ >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN'))
+ Decimal('-1')
+ >>> ExtendedContext.compare_total(1, 2)
+ Decimal('-1')
+ >>> ExtendedContext.compare_total(Decimal(1), 2)
+ Decimal('-1')
+ >>> ExtendedContext.compare_total(1, Decimal(2))
+ Decimal('-1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.compare_total(b)
+
+ def compare_total_mag(self, a, b):
+ """Compares two operands using their abstract representation ignoring sign.
+
+ Like compare_total, but with operand's sign ignored and assumed to be 0.
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.compare_total_mag(b)
+
+ def copy_abs(self, a):
+ """Returns a copy of the operand with the sign set to 0.
+
+ >>> ExtendedContext.copy_abs(Decimal('2.1'))
+ Decimal('2.1')
+ >>> ExtendedContext.copy_abs(Decimal('-100'))
+ Decimal('100')
+ >>> ExtendedContext.copy_abs(-1)
+ Decimal('1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.copy_abs()
+
+ def copy_decimal(self, a):
+ """Returns a copy of the decimal object.
+
+ >>> ExtendedContext.copy_decimal(Decimal('2.1'))
+ Decimal('2.1')
+ >>> ExtendedContext.copy_decimal(Decimal('-1.00'))
+ Decimal('-1.00')
+ >>> ExtendedContext.copy_decimal(1)
+ Decimal('1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return Decimal(a)
+
+ def copy_negate(self, a):
+ """Returns a copy of the operand with the sign inverted.
+
+ >>> ExtendedContext.copy_negate(Decimal('101.5'))
+ Decimal('-101.5')
+ >>> ExtendedContext.copy_negate(Decimal('-101.5'))
+ Decimal('101.5')
+ >>> ExtendedContext.copy_negate(1)
+ Decimal('-1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.copy_negate()
+
+ def copy_sign(self, a, b):
+ """Copies the second operand's sign to the first one.
+
+ In detail, it returns a copy of the first operand with the sign
+ equal to the sign of the second operand.
+
+ >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33'))
+ Decimal('1.50')
+ >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33'))
+ Decimal('1.50')
+ >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33'))
+ Decimal('-1.50')
+ >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33'))
+ Decimal('-1.50')
+ >>> ExtendedContext.copy_sign(1, -2)
+ Decimal('-1')
+ >>> ExtendedContext.copy_sign(Decimal(1), -2)
+ Decimal('-1')
+ >>> ExtendedContext.copy_sign(1, Decimal(-2))
+ Decimal('-1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.copy_sign(b)
+
+ def divide(self, a, b):
+ """Decimal division in a specified context.
+
+ >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
+ Decimal('0.333333333')
+ >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
+ Decimal('0.666666667')
+ >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
+ Decimal('2.5')
+ >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
+ Decimal('0.1')
+ >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
+ Decimal('1')
+ >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
+ Decimal('4.00')
+ >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
+ Decimal('1.20')
+ >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
+ Decimal('10')
+ >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
+ Decimal('1000')
+ >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
+ Decimal('1.20E+6')
+ >>> ExtendedContext.divide(5, 5)
+ Decimal('1')
+ >>> ExtendedContext.divide(Decimal(5), 5)
+ Decimal('1')
+ >>> ExtendedContext.divide(5, Decimal(5))
+ Decimal('1')
+ """
+ a = _convert_other(a, raiseit=True)
+ r = a.__truediv__(b, context=self)
+ if r is NotImplemented:
+ raise TypeError("Unable to convert %s to Decimal" % b)
+ else:
+ return r
+
+ def divide_int(self, a, b):
+ """Divides two numbers and returns the integer part of the result.
+
+ >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
+ Decimal('0')
+ >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
+ Decimal('3')
+ >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
+ Decimal('3')
+ >>> ExtendedContext.divide_int(10, 3)
+ Decimal('3')
+ >>> ExtendedContext.divide_int(Decimal(10), 3)
+ Decimal('3')
+ >>> ExtendedContext.divide_int(10, Decimal(3))
+ Decimal('3')
+ """
+ a = _convert_other(a, raiseit=True)
+ r = a.__floordiv__(b, context=self)
+ if r is NotImplemented:
+ raise TypeError("Unable to convert %s to Decimal" % b)
+ else:
+ return r
+
+ def divmod(self, a, b):
+ """Return (a // b, a % b).
+
+ >>> ExtendedContext.divmod(Decimal(8), Decimal(3))
+ (Decimal('2'), Decimal('2'))
+ >>> ExtendedContext.divmod(Decimal(8), Decimal(4))
+ (Decimal('2'), Decimal('0'))
+ >>> ExtendedContext.divmod(8, 4)
+ (Decimal('2'), Decimal('0'))
+ >>> ExtendedContext.divmod(Decimal(8), 4)
+ (Decimal('2'), Decimal('0'))
+ >>> ExtendedContext.divmod(8, Decimal(4))
+ (Decimal('2'), Decimal('0'))
+ """
+ a = _convert_other(a, raiseit=True)
+ r = a.__divmod__(b, context=self)
+ if r is NotImplemented:
+ raise TypeError("Unable to convert %s to Decimal" % b)
+ else:
+ return r
+
+ def exp(self, a):
+ """Returns e ** a.
+
+ >>> c = ExtendedContext.copy()
+ >>> c.Emin = -999
+ >>> c.Emax = 999
+ >>> c.exp(Decimal('-Infinity'))
+ Decimal('0')
+ >>> c.exp(Decimal('-1'))
+ Decimal('0.367879441')
+ >>> c.exp(Decimal('0'))
+ Decimal('1')
+ >>> c.exp(Decimal('1'))
+ Decimal('2.71828183')
+ >>> c.exp(Decimal('0.693147181'))
+ Decimal('2.00000000')
+ >>> c.exp(Decimal('+Infinity'))
+ Decimal('Infinity')
+ >>> c.exp(10)
+ Decimal('22026.4658')
+ """
+ a =_convert_other(a, raiseit=True)
+ return a.exp(context=self)
+
+ def fma(self, a, b, c):
+ """Returns a multiplied by b, plus c.
+
+ The first two operands are multiplied together, using multiply,
+ the third operand is then added to the result of that
+ multiplication, using add, all with only one final rounding.
+
+ >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7'))
+ Decimal('22')
+ >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7'))
+ Decimal('-8')
+ >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578'))
+ Decimal('1.38435736E+12')
+ >>> ExtendedContext.fma(1, 3, 4)
+ Decimal('7')
+ >>> ExtendedContext.fma(1, Decimal(3), 4)
+ Decimal('7')
+ >>> ExtendedContext.fma(1, 3, Decimal(4))
+ Decimal('7')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.fma(b, c, context=self)
+
+ def is_canonical(self, a):
+ """Return True if the operand is canonical; otherwise return False.
+
+ Currently, the encoding of a Decimal instance is always
+ canonical, so this method returns True for any Decimal.
+
+ >>> ExtendedContext.is_canonical(Decimal('2.50'))
+ True
+ """
+ if not isinstance(a, Decimal):
+ raise TypeError("is_canonical requires a Decimal as an argument.")
+ return a.is_canonical()
+
+ def is_finite(self, a):
+ """Return True if the operand is finite; otherwise return False.
+
+ A Decimal instance is considered finite if it is neither
+ infinite nor a NaN.
+
+ >>> ExtendedContext.is_finite(Decimal('2.50'))
+ True
+ >>> ExtendedContext.is_finite(Decimal('-0.3'))
+ True
+ >>> ExtendedContext.is_finite(Decimal('0'))
+ True
+ >>> ExtendedContext.is_finite(Decimal('Inf'))
+ False
+ >>> ExtendedContext.is_finite(Decimal('NaN'))
+ False
+ >>> ExtendedContext.is_finite(1)
+ True
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.is_finite()
+
+ def is_infinite(self, a):
+ """Return True if the operand is infinite; otherwise return False.
+
+ >>> ExtendedContext.is_infinite(Decimal('2.50'))
+ False
+ >>> ExtendedContext.is_infinite(Decimal('-Inf'))
+ True
+ >>> ExtendedContext.is_infinite(Decimal('NaN'))
+ False
+ >>> ExtendedContext.is_infinite(1)
+ False
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.is_infinite()
+
+ def is_nan(self, a):
+ """Return True if the operand is a qNaN or sNaN;
+ otherwise return False.
+
+ >>> ExtendedContext.is_nan(Decimal('2.50'))
+ False
+ >>> ExtendedContext.is_nan(Decimal('NaN'))
+ True
+ >>> ExtendedContext.is_nan(Decimal('-sNaN'))
+ True
+ >>> ExtendedContext.is_nan(1)
+ False
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.is_nan()
+
+ def is_normal(self, a):
+ """Return True if the operand is a normal number;
+ otherwise return False.
+
+ >>> c = ExtendedContext.copy()
+ >>> c.Emin = -999
+ >>> c.Emax = 999
+ >>> c.is_normal(Decimal('2.50'))
+ True
+ >>> c.is_normal(Decimal('0.1E-999'))
+ False
+ >>> c.is_normal(Decimal('0.00'))
+ False
+ >>> c.is_normal(Decimal('-Inf'))
+ False
+ >>> c.is_normal(Decimal('NaN'))
+ False
+ >>> c.is_normal(1)
+ True
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.is_normal(context=self)
+
+ def is_qnan(self, a):
+ """Return True if the operand is a quiet NaN; otherwise return False.
+
+ >>> ExtendedContext.is_qnan(Decimal('2.50'))
+ False
+ >>> ExtendedContext.is_qnan(Decimal('NaN'))
+ True
+ >>> ExtendedContext.is_qnan(Decimal('sNaN'))
+ False
+ >>> ExtendedContext.is_qnan(1)
+ False
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.is_qnan()
+
+ def is_signed(self, a):
+ """Return True if the operand is negative; otherwise return False.
+
+ >>> ExtendedContext.is_signed(Decimal('2.50'))
+ False
+ >>> ExtendedContext.is_signed(Decimal('-12'))
+ True
+ >>> ExtendedContext.is_signed(Decimal('-0'))
+ True
+ >>> ExtendedContext.is_signed(8)
+ False
+ >>> ExtendedContext.is_signed(-8)
+ True
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.is_signed()
+
+ def is_snan(self, a):
+ """Return True if the operand is a signaling NaN;
+ otherwise return False.
+
+ >>> ExtendedContext.is_snan(Decimal('2.50'))
+ False
+ >>> ExtendedContext.is_snan(Decimal('NaN'))
+ False
+ >>> ExtendedContext.is_snan(Decimal('sNaN'))
+ True
+ >>> ExtendedContext.is_snan(1)
+ False
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.is_snan()
+
+ def is_subnormal(self, a):
+ """Return True if the operand is subnormal; otherwise return False.
+
+ >>> c = ExtendedContext.copy()
+ >>> c.Emin = -999
+ >>> c.Emax = 999
+ >>> c.is_subnormal(Decimal('2.50'))
+ False
+ >>> c.is_subnormal(Decimal('0.1E-999'))
+ True
+ >>> c.is_subnormal(Decimal('0.00'))
+ False
+ >>> c.is_subnormal(Decimal('-Inf'))
+ False
+ >>> c.is_subnormal(Decimal('NaN'))
+ False
+ >>> c.is_subnormal(1)
+ False
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.is_subnormal(context=self)
+
+ def is_zero(self, a):
+ """Return True if the operand is a zero; otherwise return False.
+
+ >>> ExtendedContext.is_zero(Decimal('0'))
+ True
+ >>> ExtendedContext.is_zero(Decimal('2.50'))
+ False
+ >>> ExtendedContext.is_zero(Decimal('-0E+2'))
+ True
+ >>> ExtendedContext.is_zero(1)
+ False
+ >>> ExtendedContext.is_zero(0)
+ True
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.is_zero()
+
+ def ln(self, a):
+ """Returns the natural (base e) logarithm of the operand.
+
+ >>> c = ExtendedContext.copy()
+ >>> c.Emin = -999
+ >>> c.Emax = 999
+ >>> c.ln(Decimal('0'))
+ Decimal('-Infinity')
+ >>> c.ln(Decimal('1.000'))
+ Decimal('0')
+ >>> c.ln(Decimal('2.71828183'))
+ Decimal('1.00000000')
+ >>> c.ln(Decimal('10'))
+ Decimal('2.30258509')
+ >>> c.ln(Decimal('+Infinity'))
+ Decimal('Infinity')
+ >>> c.ln(1)
+ Decimal('0')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.ln(context=self)
+
+ def log10(self, a):
+ """Returns the base 10 logarithm of the operand.
+
+ >>> c = ExtendedContext.copy()
+ >>> c.Emin = -999
+ >>> c.Emax = 999
+ >>> c.log10(Decimal('0'))
+ Decimal('-Infinity')
+ >>> c.log10(Decimal('0.001'))
+ Decimal('-3')
+ >>> c.log10(Decimal('1.000'))
+ Decimal('0')
+ >>> c.log10(Decimal('2'))
+ Decimal('0.301029996')
+ >>> c.log10(Decimal('10'))
+ Decimal('1')
+ >>> c.log10(Decimal('70'))
+ Decimal('1.84509804')
+ >>> c.log10(Decimal('+Infinity'))
+ Decimal('Infinity')
+ >>> c.log10(0)
+ Decimal('-Infinity')
+ >>> c.log10(1)
+ Decimal('0')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.log10(context=self)
+
+ def logb(self, a):
+ """ Returns the exponent of the magnitude of the operand's MSD.
+
+ The result is the integer which is the exponent of the magnitude
+ of the most significant digit of the operand (as though the
+ operand were truncated to a single digit while maintaining the
+ value of that digit and without limiting the resulting exponent).
+
+ >>> ExtendedContext.logb(Decimal('250'))
+ Decimal('2')
+ >>> ExtendedContext.logb(Decimal('2.50'))
+ Decimal('0')
+ >>> ExtendedContext.logb(Decimal('0.03'))
+ Decimal('-2')
+ >>> ExtendedContext.logb(Decimal('0'))
+ Decimal('-Infinity')
+ >>> ExtendedContext.logb(1)
+ Decimal('0')
+ >>> ExtendedContext.logb(10)
+ Decimal('1')
+ >>> ExtendedContext.logb(100)
+ Decimal('2')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.logb(context=self)
+
+ def logical_and(self, a, b):
+ """Applies the logical operation 'and' between each operand's digits.
+
+ The operands must be both logical numbers.
+
+ >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
+ Decimal('0')
+ >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1'))
+ Decimal('0')
+ >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0'))
+ Decimal('0')
+ >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1'))
+ Decimal('1')
+ >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010'))
+ Decimal('1000')
+ >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10'))
+ Decimal('10')
+ >>> ExtendedContext.logical_and(110, 1101)
+ Decimal('100')
+ >>> ExtendedContext.logical_and(Decimal(110), 1101)
+ Decimal('100')
+ >>> ExtendedContext.logical_and(110, Decimal(1101))
+ Decimal('100')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.logical_and(b, context=self)
+
+ def logical_invert(self, a):
+ """Invert all the digits in the operand.
+
+ The operand must be a logical number.
+
+ >>> ExtendedContext.logical_invert(Decimal('0'))
+ Decimal('111111111')
+ >>> ExtendedContext.logical_invert(Decimal('1'))
+ Decimal('111111110')
+ >>> ExtendedContext.logical_invert(Decimal('111111111'))
+ Decimal('0')
+ >>> ExtendedContext.logical_invert(Decimal('101010101'))
+ Decimal('10101010')
+ >>> ExtendedContext.logical_invert(1101)
+ Decimal('111110010')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.logical_invert(context=self)
+
+ def logical_or(self, a, b):
+ """Applies the logical operation 'or' between each operand's digits.
+
+ The operands must be both logical numbers.
+
+ >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
+ Decimal('0')
+ >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1'))
+ Decimal('1')
+ >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0'))
+ Decimal('1')
+ >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1'))
+ Decimal('1')
+ >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010'))
+ Decimal('1110')
+ >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10'))
+ Decimal('1110')
+ >>> ExtendedContext.logical_or(110, 1101)
+ Decimal('1111')
+ >>> ExtendedContext.logical_or(Decimal(110), 1101)
+ Decimal('1111')
+ >>> ExtendedContext.logical_or(110, Decimal(1101))
+ Decimal('1111')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.logical_or(b, context=self)
+
+ def logical_xor(self, a, b):
+ """Applies the logical operation 'xor' between each operand's digits.
+
+ The operands must be both logical numbers.
+
+ >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
+ Decimal('0')
+ >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1'))
+ Decimal('1')
+ >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0'))
+ Decimal('1')
+ >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1'))
+ Decimal('0')
+ >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010'))
+ Decimal('110')
+ >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10'))
+ Decimal('1101')
+ >>> ExtendedContext.logical_xor(110, 1101)
+ Decimal('1011')
+ >>> ExtendedContext.logical_xor(Decimal(110), 1101)
+ Decimal('1011')
+ >>> ExtendedContext.logical_xor(110, Decimal(1101))
+ Decimal('1011')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.logical_xor(b, context=self)
+
+ def max(self, a, b):
+ """max compares two values numerically and returns the maximum.
+
+ If either operand is a NaN then the general rules apply.
+ Otherwise, the operands are compared as though by the compare
+ operation. If they are numerically equal then the left-hand operand
+ is chosen as the result. Otherwise the maximum (closer to positive
+ infinity) of the two operands is chosen as the result.
+
+ >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
+ Decimal('3')
+ >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
+ Decimal('3')
+ >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
+ Decimal('1')
+ >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
+ Decimal('7')
+ >>> ExtendedContext.max(1, 2)
+ Decimal('2')
+ >>> ExtendedContext.max(Decimal(1), 2)
+ Decimal('2')
+ >>> ExtendedContext.max(1, Decimal(2))
+ Decimal('2')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.max(b, context=self)
+
+ def max_mag(self, a, b):
+ """Compares the values numerically with their sign ignored.
+
+ >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN'))
+ Decimal('7')
+ >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10'))
+ Decimal('-10')
+ >>> ExtendedContext.max_mag(1, -2)
+ Decimal('-2')
+ >>> ExtendedContext.max_mag(Decimal(1), -2)
+ Decimal('-2')
+ >>> ExtendedContext.max_mag(1, Decimal(-2))
+ Decimal('-2')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.max_mag(b, context=self)
+
+ def min(self, a, b):
+ """min compares two values numerically and returns the minimum.
+
+ If either operand is a NaN then the general rules apply.
+ Otherwise, the operands are compared as though by the compare
+ operation. If they are numerically equal then the left-hand operand
+ is chosen as the result. Otherwise the minimum (closer to negative
+ infinity) of the two operands is chosen as the result.
+
+ >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
+ Decimal('2')
+ >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
+ Decimal('-10')
+ >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
+ Decimal('1.0')
+ >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
+ Decimal('7')
+ >>> ExtendedContext.min(1, 2)
+ Decimal('1')
+ >>> ExtendedContext.min(Decimal(1), 2)
+ Decimal('1')
+ >>> ExtendedContext.min(1, Decimal(29))
+ Decimal('1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.min(b, context=self)
+
+ def min_mag(self, a, b):
+ """Compares the values numerically with their sign ignored.
+
+ >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2'))
+ Decimal('-2')
+ >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN'))
+ Decimal('-3')
+ >>> ExtendedContext.min_mag(1, -2)
+ Decimal('1')
+ >>> ExtendedContext.min_mag(Decimal(1), -2)
+ Decimal('1')
+ >>> ExtendedContext.min_mag(1, Decimal(-2))
+ Decimal('1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.min_mag(b, context=self)
+
+ def minus(self, a):
+ """Minus corresponds to unary prefix minus in Python.
+
+ The operation is evaluated using the same rules as subtract; the
+ operation minus(a) is calculated as subtract('0', a) where the '0'
+ has the same exponent as the operand.
+
+ >>> ExtendedContext.minus(Decimal('1.3'))
+ Decimal('-1.3')
+ >>> ExtendedContext.minus(Decimal('-1.3'))
+ Decimal('1.3')
+ >>> ExtendedContext.minus(1)
+ Decimal('-1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.__neg__(context=self)
+
+ def multiply(self, a, b):
+ """multiply multiplies two operands.
+
+ If either operand is a special value then the general rules apply.
+ Otherwise, the operands are multiplied together
+ ('long multiplication'), resulting in a number which may be as long as
+ the sum of the lengths of the two operands.
+
+ >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
+ Decimal('3.60')
+ >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
+ Decimal('21')
+ >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
+ Decimal('0.72')
+ >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
+ Decimal('-0.0')
+ >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
+ Decimal('4.28135971E+11')
+ >>> ExtendedContext.multiply(7, 7)
+ Decimal('49')
+ >>> ExtendedContext.multiply(Decimal(7), 7)
+ Decimal('49')
+ >>> ExtendedContext.multiply(7, Decimal(7))
+ Decimal('49')
+ """
+ a = _convert_other(a, raiseit=True)
+ r = a.__mul__(b, context=self)
+ if r is NotImplemented:
+ raise TypeError("Unable to convert %s to Decimal" % b)
+ else:
+ return r
+
+ def next_minus(self, a):
+ """Returns the largest representable number smaller than a.
+
+ >>> c = ExtendedContext.copy()
+ >>> c.Emin = -999
+ >>> c.Emax = 999
+ >>> ExtendedContext.next_minus(Decimal('1'))
+ Decimal('0.999999999')
+ >>> c.next_minus(Decimal('1E-1007'))
+ Decimal('0E-1007')
+ >>> ExtendedContext.next_minus(Decimal('-1.00000003'))
+ Decimal('-1.00000004')
+ >>> c.next_minus(Decimal('Infinity'))
+ Decimal('9.99999999E+999')
+ >>> c.next_minus(1)
+ Decimal('0.999999999')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.next_minus(context=self)
+
+ def next_plus(self, a):
+ """Returns the smallest representable number larger than a.
+
+ >>> c = ExtendedContext.copy()
+ >>> c.Emin = -999
+ >>> c.Emax = 999
+ >>> ExtendedContext.next_plus(Decimal('1'))
+ Decimal('1.00000001')
+ >>> c.next_plus(Decimal('-1E-1007'))
+ Decimal('-0E-1007')
+ >>> ExtendedContext.next_plus(Decimal('-1.00000003'))
+ Decimal('-1.00000002')
+ >>> c.next_plus(Decimal('-Infinity'))
+ Decimal('-9.99999999E+999')
+ >>> c.next_plus(1)
+ Decimal('1.00000001')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.next_plus(context=self)
+
+ def next_toward(self, a, b):
+ """Returns the number closest to a, in direction towards b.
+
+ The result is the closest representable number from the first
+ operand (but not the first operand) that is in the direction
+ towards the second operand, unless the operands have the same
+ value.
+
+ >>> c = ExtendedContext.copy()
+ >>> c.Emin = -999
+ >>> c.Emax = 999
+ >>> c.next_toward(Decimal('1'), Decimal('2'))
+ Decimal('1.00000001')
+ >>> c.next_toward(Decimal('-1E-1007'), Decimal('1'))
+ Decimal('-0E-1007')
+ >>> c.next_toward(Decimal('-1.00000003'), Decimal('0'))
+ Decimal('-1.00000002')
+ >>> c.next_toward(Decimal('1'), Decimal('0'))
+ Decimal('0.999999999')
+ >>> c.next_toward(Decimal('1E-1007'), Decimal('-100'))
+ Decimal('0E-1007')
+ >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10'))
+ Decimal('-1.00000004')
+ >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000'))
+ Decimal('-0.00')
+ >>> c.next_toward(0, 1)
+ Decimal('1E-1007')
+ >>> c.next_toward(Decimal(0), 1)
+ Decimal('1E-1007')
+ >>> c.next_toward(0, Decimal(1))
+ Decimal('1E-1007')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.next_toward(b, context=self)
+
+ def normalize(self, a):
+ """normalize reduces an operand to its simplest form.
+
+ Essentially a plus operation with all trailing zeros removed from the
+ result.
+
+ >>> ExtendedContext.normalize(Decimal('2.1'))
+ Decimal('2.1')
+ >>> ExtendedContext.normalize(Decimal('-2.0'))
+ Decimal('-2')
+ >>> ExtendedContext.normalize(Decimal('1.200'))
+ Decimal('1.2')
+ >>> ExtendedContext.normalize(Decimal('-120'))
+ Decimal('-1.2E+2')
+ >>> ExtendedContext.normalize(Decimal('120.00'))
+ Decimal('1.2E+2')
+ >>> ExtendedContext.normalize(Decimal('0.00'))
+ Decimal('0')
+ >>> ExtendedContext.normalize(6)
+ Decimal('6')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.normalize(context=self)
+
+ def number_class(self, a):
+ """Returns an indication of the class of the operand.
+
+ The class is one of the following strings:
+ -sNaN
+ -NaN
+ -Infinity
+ -Normal
+ -Subnormal
+ -Zero
+ +Zero
+ +Subnormal
+ +Normal
+ +Infinity
+
+ >>> c = ExtendedContext.copy()
+ >>> c.Emin = -999
+ >>> c.Emax = 999
+ >>> c.number_class(Decimal('Infinity'))
+ '+Infinity'
+ >>> c.number_class(Decimal('1E-10'))
+ '+Normal'
+ >>> c.number_class(Decimal('2.50'))
+ '+Normal'
+ >>> c.number_class(Decimal('0.1E-999'))
+ '+Subnormal'
+ >>> c.number_class(Decimal('0'))
+ '+Zero'
+ >>> c.number_class(Decimal('-0'))
+ '-Zero'
+ >>> c.number_class(Decimal('-0.1E-999'))
+ '-Subnormal'
+ >>> c.number_class(Decimal('-1E-10'))
+ '-Normal'
+ >>> c.number_class(Decimal('-2.50'))
+ '-Normal'
+ >>> c.number_class(Decimal('-Infinity'))
+ '-Infinity'
+ >>> c.number_class(Decimal('NaN'))
+ 'NaN'
+ >>> c.number_class(Decimal('-NaN'))
+ 'NaN'
+ >>> c.number_class(Decimal('sNaN'))
+ 'sNaN'
+ >>> c.number_class(123)
+ '+Normal'
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.number_class(context=self)
+
+ def plus(self, a):
+ """Plus corresponds to unary prefix plus in Python.
+
+ The operation is evaluated using the same rules as add; the
+ operation plus(a) is calculated as add('0', a) where the '0'
+ has the same exponent as the operand.
+
+ >>> ExtendedContext.plus(Decimal('1.3'))
+ Decimal('1.3')
+ >>> ExtendedContext.plus(Decimal('-1.3'))
+ Decimal('-1.3')
+ >>> ExtendedContext.plus(-1)
+ Decimal('-1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.__pos__(context=self)
+
+ def power(self, a, b, modulo=None):
+ """Raises a to the power of b, to modulo if given.
+
+ With two arguments, compute a**b. If a is negative then b
+ must be integral. The result will be inexact unless b is
+ integral and the result is finite and can be expressed exactly
+ in 'precision' digits.
+
+ With three arguments, compute (a**b) % modulo. For the
+ three argument form, the following restrictions on the
+ arguments hold:
+
+ - all three arguments must be integral
+ - b must be nonnegative
+ - at least one of a or b must be nonzero
+ - modulo must be nonzero and have at most 'precision' digits
+
+ The result of pow(a, b, modulo) is identical to the result
+ that would be obtained by computing (a**b) % modulo with
+ unbounded precision, but is computed more efficiently. It is
+ always exact.
+
+ >>> c = ExtendedContext.copy()
+ >>> c.Emin = -999
+ >>> c.Emax = 999
+ >>> c.power(Decimal('2'), Decimal('3'))
+ Decimal('8')
+ >>> c.power(Decimal('-2'), Decimal('3'))
+ Decimal('-8')
+ >>> c.power(Decimal('2'), Decimal('-3'))
+ Decimal('0.125')
+ >>> c.power(Decimal('1.7'), Decimal('8'))
+ Decimal('69.7575744')
+ >>> c.power(Decimal('10'), Decimal('0.301029996'))
+ Decimal('2.00000000')
+ >>> c.power(Decimal('Infinity'), Decimal('-1'))
+ Decimal('0')
+ >>> c.power(Decimal('Infinity'), Decimal('0'))
+ Decimal('1')
+ >>> c.power(Decimal('Infinity'), Decimal('1'))
+ Decimal('Infinity')
+ >>> c.power(Decimal('-Infinity'), Decimal('-1'))
+ Decimal('-0')
+ >>> c.power(Decimal('-Infinity'), Decimal('0'))
+ Decimal('1')
+ >>> c.power(Decimal('-Infinity'), Decimal('1'))
+ Decimal('-Infinity')
+ >>> c.power(Decimal('-Infinity'), Decimal('2'))
+ Decimal('Infinity')
+ >>> c.power(Decimal('0'), Decimal('0'))
+ Decimal('NaN')
+
+ >>> c.power(Decimal('3'), Decimal('7'), Decimal('16'))
+ Decimal('11')
+ >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16'))
+ Decimal('-11')
+ >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16'))
+ Decimal('1')
+ >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16'))
+ Decimal('11')
+ >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789'))
+ Decimal('11729830')
+ >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729'))
+ Decimal('-0')
+ >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537'))
+ Decimal('1')
+ >>> ExtendedContext.power(7, 7)
+ Decimal('823543')
+ >>> ExtendedContext.power(Decimal(7), 7)
+ Decimal('823543')
+ >>> ExtendedContext.power(7, Decimal(7), 2)
+ Decimal('1')
+ """
+ a = _convert_other(a, raiseit=True)
+ r = a.__pow__(b, modulo, context=self)
+ if r is NotImplemented:
+ raise TypeError("Unable to convert %s to Decimal" % b)
+ else:
+ return r
+
+ def quantize(self, a, b):
+ """Returns a value equal to 'a' (rounded), having the exponent of 'b'.
+
+ The coefficient of the result is derived from that of the left-hand
+ operand. It may be rounded using the current rounding setting (if the
+ exponent is being increased), multiplied by a positive power of ten (if
+ the exponent is being decreased), or is unchanged (if the exponent is
+ already equal to that of the right-hand operand).
+
+ Unlike other operations, if the length of the coefficient after the
+ quantize operation would be greater than precision then an Invalid
+ operation condition is raised. This guarantees that, unless there is
+ an error condition, the exponent of the result of a quantize is always
+ equal to that of the right-hand operand.
+
+ Also unlike other operations, quantize will never raise Underflow, even
+ if the result is subnormal and inexact.
+
+ >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
+ Decimal('2.170')
+ >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
+ Decimal('2.17')
+ >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
+ Decimal('2.2')
+ >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
+ Decimal('2')
+ >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
+ Decimal('0E+1')
+ >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
+ Decimal('-Infinity')
+ >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
+ Decimal('NaN')
+ >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
+ Decimal('-0')
+ >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
+ Decimal('-0E+5')
+ >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
+ Decimal('NaN')
+ >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
+ Decimal('NaN')
+ >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
+ Decimal('217.0')
+ >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
+ Decimal('217')
+ >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
+ Decimal('2.2E+2')
+ >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
+ Decimal('2E+2')
+ >>> ExtendedContext.quantize(1, 2)
+ Decimal('1')
+ >>> ExtendedContext.quantize(Decimal(1), 2)
+ Decimal('1')
+ >>> ExtendedContext.quantize(1, Decimal(2))
+ Decimal('1')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.quantize(b, context=self)
+
+ def radix(self):
+ """Just returns 10, as this is Decimal, :)
+
+ >>> ExtendedContext.radix()
+ Decimal('10')
+ """
+ return Decimal(10)
+
+ def remainder(self, a, b):
+ """Returns the remainder from integer division.
+
+ The result is the residue of the dividend after the operation of
+ calculating integer division as described for divide-integer, rounded
+ to precision digits if necessary. The sign of the result, if
+ non-zero, is the same as that of the original dividend.
+
+ This operation will fail under the same conditions as integer division
+ (that is, if integer division on the same two operands would fail, the
+ remainder cannot be calculated).
+
+ >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
+ Decimal('2.1')
+ >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
+ Decimal('1')
+ >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
+ Decimal('-1')
+ >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
+ Decimal('0.2')
+ >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
+ Decimal('0.1')
+ >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
+ Decimal('1.0')
+ >>> ExtendedContext.remainder(22, 6)
+ Decimal('4')
+ >>> ExtendedContext.remainder(Decimal(22), 6)
+ Decimal('4')
+ >>> ExtendedContext.remainder(22, Decimal(6))
+ Decimal('4')
+ """
+ a = _convert_other(a, raiseit=True)
+ r = a.__mod__(b, context=self)
+ if r is NotImplemented:
+ raise TypeError("Unable to convert %s to Decimal" % b)
+ else:
+ return r
+
+ def remainder_near(self, a, b):
+ """Returns to be "a - b * n", where n is the integer nearest the exact
+ value of "x / b" (if two integers are equally near then the even one
+ is chosen). If the result is equal to 0 then its sign will be the
+ sign of a.
+
+ This operation will fail under the same conditions as integer division
+ (that is, if integer division on the same two operands would fail, the
+ remainder cannot be calculated).
+
+ >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
+ Decimal('-0.9')
+ >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
+ Decimal('-2')
+ >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
+ Decimal('1')
+ >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
+ Decimal('-1')
+ >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
+ Decimal('0.2')
+ >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
+ Decimal('0.1')
+ >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
+ Decimal('-0.3')
+ >>> ExtendedContext.remainder_near(3, 11)
+ Decimal('3')
+ >>> ExtendedContext.remainder_near(Decimal(3), 11)
+ Decimal('3')
+ >>> ExtendedContext.remainder_near(3, Decimal(11))
+ Decimal('3')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.remainder_near(b, context=self)
+
+ def rotate(self, a, b):
+ """Returns a rotated copy of a, b times.
+
+ The coefficient of the result is a rotated copy of the digits in
+ the coefficient of the first operand. The number of places of
+ rotation is taken from the absolute value of the second operand,
+ with the rotation being to the left if the second operand is
+ positive or to the right otherwise.
+
+ >>> ExtendedContext.rotate(Decimal('34'), Decimal('8'))
+ Decimal('400000003')
+ >>> ExtendedContext.rotate(Decimal('12'), Decimal('9'))
+ Decimal('12')
+ >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2'))
+ Decimal('891234567')
+ >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0'))
+ Decimal('123456789')
+ >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2'))
+ Decimal('345678912')
+ >>> ExtendedContext.rotate(1333333, 1)
+ Decimal('13333330')
+ >>> ExtendedContext.rotate(Decimal(1333333), 1)
+ Decimal('13333330')
+ >>> ExtendedContext.rotate(1333333, Decimal(1))
+ Decimal('13333330')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.rotate(b, context=self)
+
+ def same_quantum(self, a, b):
+ """Returns True if the two operands have the same exponent.
+
+ The result is never affected by either the sign or the coefficient of
+ either operand.
+
+ >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
+ False
+ >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
+ True
+ >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
+ False
+ >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
+ True
+ >>> ExtendedContext.same_quantum(10000, -1)
+ True
+ >>> ExtendedContext.same_quantum(Decimal(10000), -1)
+ True
+ >>> ExtendedContext.same_quantum(10000, Decimal(-1))
+ True
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.same_quantum(b)
+
+ def scaleb (self, a, b):
+ """Returns the first operand after adding the second value its exp.
+
+ >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2'))
+ Decimal('0.0750')
+ >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0'))
+ Decimal('7.50')
+ >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3'))
+ Decimal('7.50E+3')
+ >>> ExtendedContext.scaleb(1, 4)
+ Decimal('1E+4')
+ >>> ExtendedContext.scaleb(Decimal(1), 4)
+ Decimal('1E+4')
+ >>> ExtendedContext.scaleb(1, Decimal(4))
+ Decimal('1E+4')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.scaleb(b, context=self)
+
+ def shift(self, a, b):
+ """Returns a shifted copy of a, b times.
+
+ The coefficient of the result is a shifted copy of the digits
+ in the coefficient of the first operand. The number of places
+ to shift is taken from the absolute value of the second operand,
+ with the shift being to the left if the second operand is
+ positive or to the right otherwise. Digits shifted into the
+ coefficient are zeros.
+
+ >>> ExtendedContext.shift(Decimal('34'), Decimal('8'))
+ Decimal('400000000')
+ >>> ExtendedContext.shift(Decimal('12'), Decimal('9'))
+ Decimal('0')
+ >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2'))
+ Decimal('1234567')
+ >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0'))
+ Decimal('123456789')
+ >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2'))
+ Decimal('345678900')
+ >>> ExtendedContext.shift(88888888, 2)
+ Decimal('888888800')
+ >>> ExtendedContext.shift(Decimal(88888888), 2)
+ Decimal('888888800')
+ >>> ExtendedContext.shift(88888888, Decimal(2))
+ Decimal('888888800')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.shift(b, context=self)
+
+ def sqrt(self, a):
+ """Square root of a non-negative number to context precision.
+
+ If the result must be inexact, it is rounded using the round-half-even
+ algorithm.
+
+ >>> ExtendedContext.sqrt(Decimal('0'))
+ Decimal('0')
+ >>> ExtendedContext.sqrt(Decimal('-0'))
+ Decimal('-0')
+ >>> ExtendedContext.sqrt(Decimal('0.39'))
+ Decimal('0.624499800')
+ >>> ExtendedContext.sqrt(Decimal('100'))
+ Decimal('10')
+ >>> ExtendedContext.sqrt(Decimal('1'))
+ Decimal('1')
+ >>> ExtendedContext.sqrt(Decimal('1.0'))
+ Decimal('1.0')
+ >>> ExtendedContext.sqrt(Decimal('1.00'))
+ Decimal('1.0')
+ >>> ExtendedContext.sqrt(Decimal('7'))
+ Decimal('2.64575131')
+ >>> ExtendedContext.sqrt(Decimal('10'))
+ Decimal('3.16227766')
+ >>> ExtendedContext.sqrt(2)
+ Decimal('1.41421356')
+ >>> ExtendedContext.prec
+ 9
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.sqrt(context=self)
+
+ def subtract(self, a, b):
+ """Return the difference between the two operands.
+
+ >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
+ Decimal('0.23')
+ >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
+ Decimal('0.00')
+ >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
+ Decimal('-0.77')
+ >>> ExtendedContext.subtract(8, 5)
+ Decimal('3')
+ >>> ExtendedContext.subtract(Decimal(8), 5)
+ Decimal('3')
+ >>> ExtendedContext.subtract(8, Decimal(5))
+ Decimal('3')
+ """
+ a = _convert_other(a, raiseit=True)
+ r = a.__sub__(b, context=self)
+ if r is NotImplemented:
+ raise TypeError("Unable to convert %s to Decimal" % b)
+ else:
+ return r
+
+ def to_eng_string(self, a):
+ """Converts a number to a string, using scientific notation.
+
+ The operation is not affected by the context.
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.to_eng_string(context=self)
+
+ def to_sci_string(self, a):
+ """Converts a number to a string, using scientific notation.
+
+ The operation is not affected by the context.
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.__str__(context=self)
+
+ def to_integral_exact(self, a):
+ """Rounds to an integer.
+
+ When the operand has a negative exponent, the result is the same
+ as using the quantize() operation using the given operand as the
+ left-hand-operand, 1E+0 as the right-hand-operand, and the precision
+ of the operand as the precision setting; Inexact and Rounded flags
+ are allowed in this operation. The rounding mode is taken from the
+ context.
+
+ >>> ExtendedContext.to_integral_exact(Decimal('2.1'))
+ Decimal('2')
+ >>> ExtendedContext.to_integral_exact(Decimal('100'))
+ Decimal('100')
+ >>> ExtendedContext.to_integral_exact(Decimal('100.0'))
+ Decimal('100')
+ >>> ExtendedContext.to_integral_exact(Decimal('101.5'))
+ Decimal('102')
+ >>> ExtendedContext.to_integral_exact(Decimal('-101.5'))
+ Decimal('-102')
+ >>> ExtendedContext.to_integral_exact(Decimal('10E+5'))
+ Decimal('1.0E+6')
+ >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77'))
+ Decimal('7.89E+77')
+ >>> ExtendedContext.to_integral_exact(Decimal('-Inf'))
+ Decimal('-Infinity')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.to_integral_exact(context=self)
+
+ def to_integral_value(self, a):
+ """Rounds to an integer.
+
+ When the operand has a negative exponent, the result is the same
+ as using the quantize() operation using the given operand as the
+ left-hand-operand, 1E+0 as the right-hand-operand, and the precision
+ of the operand as the precision setting, except that no flags will
+ be set. The rounding mode is taken from the context.
+
+ >>> ExtendedContext.to_integral_value(Decimal('2.1'))
+ Decimal('2')
+ >>> ExtendedContext.to_integral_value(Decimal('100'))
+ Decimal('100')
+ >>> ExtendedContext.to_integral_value(Decimal('100.0'))
+ Decimal('100')
+ >>> ExtendedContext.to_integral_value(Decimal('101.5'))
+ Decimal('102')
+ >>> ExtendedContext.to_integral_value(Decimal('-101.5'))
+ Decimal('-102')
+ >>> ExtendedContext.to_integral_value(Decimal('10E+5'))
+ Decimal('1.0E+6')
+ >>> ExtendedContext.to_integral_value(Decimal('7.89E+77'))
+ Decimal('7.89E+77')
+ >>> ExtendedContext.to_integral_value(Decimal('-Inf'))
+ Decimal('-Infinity')
+ """
+ a = _convert_other(a, raiseit=True)
+ return a.to_integral_value(context=self)
+
+ # the method name changed, but we provide also the old one, for compatibility
+ to_integral = to_integral_value
+
+class _WorkRep(object):
+ __slots__ = ('sign','int','exp')
+ # sign: 0 or 1
+ # int: int
+ # exp: None, int, or string
+
+ def __init__(self, value=None):
+ if value is None:
+ self.sign = None
+ self.int = 0
+ self.exp = None
+ elif isinstance(value, Decimal):
+ self.sign = value._sign
+ self.int = int(value._int)
+ self.exp = value._exp
+ else:
+ # assert isinstance(value, tuple)
+ self.sign = value[0]
+ self.int = value[1]
+ self.exp = value[2]
+
+ def __repr__(self):
+ return "(%r, %r, %r)" % (self.sign, self.int, self.exp)
+
+ __str__ = __repr__
+
+
+
+def _normalize(op1, op2, prec = 0):
+ """Normalizes op1, op2 to have the same exp and length of coefficient.
+
+ Done during addition.
+ """
+ if op1.exp < op2.exp:
+ tmp = op2
+ other = op1
+ else:
+ tmp = op1
+ other = op2
+
+ # Let exp = min(tmp.exp - 1, tmp.adjusted() - precision - 1).
+ # Then adding 10**exp to tmp has the same effect (after rounding)
+ # as adding any positive quantity smaller than 10**exp; similarly
+ # for subtraction. So if other is smaller than 10**exp we replace
+ # it with 10**exp. This avoids tmp.exp - other.exp getting too large.
+ tmp_len = len(str(tmp.int))
+ other_len = len(str(other.int))
+ exp = tmp.exp + min(-1, tmp_len - prec - 2)
+ if other_len + other.exp - 1 < exp:
+ other.int = 1
+ other.exp = exp
+
+ tmp.int *= 10 ** (tmp.exp - other.exp)
+ tmp.exp = other.exp
+ return op1, op2
+
+##### Integer arithmetic functions used by ln, log10, exp and __pow__ #####
+
+_nbits = int.bit_length
+
+def _decimal_lshift_exact(n, e):
+ """ Given integers n and e, return n * 10**e if it's an integer, else None.
+
+ The computation is designed to avoid computing large powers of 10
+ unnecessarily.
+
+ >>> _decimal_lshift_exact(3, 4)
+ 30000
+ >>> _decimal_lshift_exact(300, -999999999) # returns None
+
+ """
+ if n == 0:
+ return 0
+ elif e >= 0:
+ return n * 10**e
+ else:
+ # val_n = largest power of 10 dividing n.
+ str_n = str(abs(n))
+ val_n = len(str_n) - len(str_n.rstrip('0'))
+ return None if val_n < -e else n // 10**-e
+
+def _sqrt_nearest(n, a):
+ """Closest integer to the square root of the positive integer n. a is
+ an initial approximation to the square root. Any positive integer
+ will do for a, but the closer a is to the square root of n the
+ faster convergence will be.
+
+ """
+ if n <= 0 or a <= 0:
+ raise ValueError("Both arguments to _sqrt_nearest should be positive.")
+
+ b=0
+ while a != b:
+ b, a = a, a--n//a>>1
+ return a
+
+def _rshift_nearest(x, shift):
+ """Given an integer x and a nonnegative integer shift, return closest
+ integer to x / 2**shift; use round-to-even in case of a tie.
+
+ """
+ b, q = 1 << shift, x >> shift
+ return q + (2*(x & (b-1)) + (q&1) > b)
+
+def _div_nearest(a, b):
+ """Closest integer to a/b, a and b positive integers; rounds to even
+ in the case of a tie.
+
+ """
+ q, r = divmod(a, b)
+ return q + (2*r + (q&1) > b)
+
+def _ilog(x, M, L = 8):
+ """Integer approximation to M*log(x/M), with absolute error boundable
+ in terms only of x/M.
+
+ Given positive integers x and M, return an integer approximation to
+ M * log(x/M). For L = 8 and 0.1 <= x/M <= 10 the difference
+ between the approximation and the exact result is at most 22. For
+ L = 8 and 1.0 <= x/M <= 10.0 the difference is at most 15. In
+ both cases these are upper bounds on the error; it will usually be
+ much smaller."""
+
+ # The basic algorithm is the following: let log1p be the function
+ # log1p(x) = log(1+x). Then log(x/M) = log1p((x-M)/M). We use
+ # the reduction
+ #
+ # log1p(y) = 2*log1p(y/(1+sqrt(1+y)))
+ #
+ # repeatedly until the argument to log1p is small (< 2**-L in
+ # absolute value). For small y we can use the Taylor series
+ # expansion
+ #
+ # log1p(y) ~ y - y**2/2 + y**3/3 - ... - (-y)**T/T
+ #
+ # truncating at T such that y**T is small enough. The whole
+ # computation is carried out in a form of fixed-point arithmetic,
+ # with a real number z being represented by an integer
+ # approximation to z*M. To avoid loss of precision, the y below
+ # is actually an integer approximation to 2**R*y*M, where R is the
+ # number of reductions performed so far.
+
+ y = x-M
+ # argument reduction; R = number of reductions performed
+ R = 0
+ while (R <= L and abs(y) << L-R >= M or
+ R > L and abs(y) >> R-L >= M):
+ y = _div_nearest((M*y) << 1,
+ M + _sqrt_nearest(M*(M+_rshift_nearest(y, R)), M))
+ R += 1
+
+ # Taylor series with T terms
+ T = -int(-10*len(str(M))//(3*L))
+ yshift = _rshift_nearest(y, R)
+ w = _div_nearest(M, T)
+ for k in range(T-1, 0, -1):
+ w = _div_nearest(M, k) - _div_nearest(yshift*w, M)
+
+ return _div_nearest(w*y, M)
+
+def _dlog10(c, e, p):
+ """Given integers c, e and p with c > 0, p >= 0, compute an integer
+ approximation to 10**p * log10(c*10**e), with an absolute error of
+ at most 1. Assumes that c*10**e is not exactly 1."""
+
+ # increase precision by 2; compensate for this by dividing
+ # final result by 100
+ p += 2
+
+ # write c*10**e as d*10**f with either:
+ # f >= 0 and 1 <= d <= 10, or
+ # f <= 0 and 0.1 <= d <= 1.
+ # Thus for c*10**e close to 1, f = 0
+ l = len(str(c))
+ f = e+l - (e+l >= 1)
+
+ if p > 0:
+ M = 10**p
+ k = e+p-f
+ if k >= 0:
+ c *= 10**k
+ else:
+ c = _div_nearest(c, 10**-k)
+
+ log_d = _ilog(c, M) # error < 5 + 22 = 27
+ log_10 = _log10_digits(p) # error < 1
+ log_d = _div_nearest(log_d*M, log_10)
+ log_tenpower = f*M # exact
+ else:
+ log_d = 0 # error < 2.31
+ log_tenpower = _div_nearest(f, 10**-p) # error < 0.5
+
+ return _div_nearest(log_tenpower+log_d, 100)
+
+def _dlog(c, e, p):
+ """Given integers c, e and p with c > 0, compute an integer
+ approximation to 10**p * log(c*10**e), with an absolute error of
+ at most 1. Assumes that c*10**e is not exactly 1."""
+
+ # Increase precision by 2. The precision increase is compensated
+ # for at the end with a division by 100.
+ p += 2
+
+ # rewrite c*10**e as d*10**f with either f >= 0 and 1 <= d <= 10,
+ # or f <= 0 and 0.1 <= d <= 1. Then we can compute 10**p * log(c*10**e)
+ # as 10**p * log(d) + 10**p*f * log(10).
+ l = len(str(c))
+ f = e+l - (e+l >= 1)
+
+ # compute approximation to 10**p*log(d), with error < 27
+ if p > 0:
+ k = e+p-f
+ if k >= 0:
+ c *= 10**k
+ else:
+ c = _div_nearest(c, 10**-k) # error of <= 0.5 in c
+
+ # _ilog magnifies existing error in c by a factor of at most 10
+ log_d = _ilog(c, 10**p) # error < 5 + 22 = 27
+ else:
+ # p <= 0: just approximate the whole thing by 0; error < 2.31
+ log_d = 0
+
+ # compute approximation to f*10**p*log(10), with error < 11.
+ if f:
+ extra = len(str(abs(f)))-1
+ if p + extra >= 0:
+ # error in f * _log10_digits(p+extra) < |f| * 1 = |f|
+ # after division, error < |f|/10**extra + 0.5 < 10 + 0.5 < 11
+ f_log_ten = _div_nearest(f*_log10_digits(p+extra), 10**extra)
+ else:
+ f_log_ten = 0
+ else:
+ f_log_ten = 0
+
+ # error in sum < 11+27 = 38; error after division < 0.38 + 0.5 < 1
+ return _div_nearest(f_log_ten + log_d, 100)
+
+class _Log10Memoize(object):
+ """Class to compute, store, and allow retrieval of, digits of the
+ constant log(10) = 2.302585.... This constant is needed by
+ Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__."""
+ def __init__(self):
+ self.digits = "23025850929940456840179914546843642076011014886"
+
+ def getdigits(self, p):
+ """Given an integer p >= 0, return floor(10**p)*log(10).
+
+ For example, self.getdigits(3) returns 2302.
+ """
+ # digits are stored as a string, for quick conversion to
+ # integer in the case that we've already computed enough
+ # digits; the stored digits should always be correct
+ # (truncated, not rounded to nearest).
+ if p < 0:
+ raise ValueError("p should be nonnegative")
+
+ if p >= len(self.digits):
+ # compute p+3, p+6, p+9, ... digits; continue until at
+ # least one of the extra digits is nonzero
+ extra = 3
+ while True:
+ # compute p+extra digits, correct to within 1ulp
+ M = 10**(p+extra+2)
+ digits = str(_div_nearest(_ilog(10*M, M), 100))
+ if digits[-extra:] != '0'*extra:
+ break
+ extra += 3
+ # keep all reliable digits so far; remove trailing zeros
+ # and next nonzero digit
+ self.digits = digits.rstrip('0')[:-1]
+ return int(self.digits[:p+1])
+
+_log10_digits = _Log10Memoize().getdigits
+
+def _iexp(x, M, L=8):
+ """Given integers x and M, M > 0, such that x/M is small in absolute
+ value, compute an integer approximation to M*exp(x/M). For 0 <=
+ x/M <= 2.4, the absolute error in the result is bounded by 60 (and
+ is usually much smaller)."""
+
+ # Algorithm: to compute exp(z) for a real number z, first divide z
+ # by a suitable power R of 2 so that |z/2**R| < 2**-L. Then
+ # compute expm1(z/2**R) = exp(z/2**R) - 1 using the usual Taylor
+ # series
+ #
+ # expm1(x) = x + x**2/2! + x**3/3! + ...
+ #
+ # Now use the identity
+ #
+ # expm1(2x) = expm1(x)*(expm1(x)+2)
+ #
+ # R times to compute the sequence expm1(z/2**R),
+ # expm1(z/2**(R-1)), ... , exp(z/2), exp(z).
+
+ # Find R such that x/2**R/M <= 2**-L
+ R = _nbits((x<<L)//M)
+
+ # Taylor series. (2**L)**T > M
+ T = -int(-10*len(str(M))//(3*L))
+ y = _div_nearest(x, T)
+ Mshift = M<<R
+ for i in range(T-1, 0, -1):
+ y = _div_nearest(x*(Mshift + y), Mshift * i)
+
+ # Expansion
+ for k in range(R-1, -1, -1):
+ Mshift = M<<(k+2)
+ y = _div_nearest(y*(y+Mshift), Mshift)
+
+ return M+y
+
+def _dexp(c, e, p):
+ """Compute an approximation to exp(c*10**e), with p decimal places of
+ precision.
+
+ Returns integers d, f such that:
+
+ 10**(p-1) <= d <= 10**p, and
+ (d-1)*10**f < exp(c*10**e) < (d+1)*10**f
+
+ In other words, d*10**f is an approximation to exp(c*10**e) with p
+ digits of precision, and with an error in d of at most 1. This is
+ almost, but not quite, the same as the error being < 1ulp: when d
+ = 10**(p-1) the error could be up to 10 ulp."""
+
+ # we'll call iexp with M = 10**(p+2), giving p+3 digits of precision
+ p += 2
+
+ # compute log(10) with extra precision = adjusted exponent of c*10**e
+ extra = max(0, e + len(str(c)) - 1)
+ q = p + extra
+
+ # compute quotient c*10**e/(log(10)) = c*10**(e+q)/(log(10)*10**q),
+ # rounding down
+ shift = e+q
+ if shift >= 0:
+ cshift = c*10**shift
+ else:
+ cshift = c//10**-shift
+ quot, rem = divmod(cshift, _log10_digits(q))
+
+ # reduce remainder back to original precision
+ rem = _div_nearest(rem, 10**extra)
+
+ # error in result of _iexp < 120; error after division < 0.62
+ return _div_nearest(_iexp(rem, 10**p), 1000), quot - p + 3
+
+def _dpower(xc, xe, yc, ye, p):
+ """Given integers xc, xe, yc and ye representing Decimals x = xc*10**xe and
+ y = yc*10**ye, compute x**y. Returns a pair of integers (c, e) such that:
+
+ 10**(p-1) <= c <= 10**p, and
+ (c-1)*10**e < x**y < (c+1)*10**e
+
+ in other words, c*10**e is an approximation to x**y with p digits
+ of precision, and with an error in c of at most 1. (This is
+ almost, but not quite, the same as the error being < 1ulp: when c
+ == 10**(p-1) we can only guarantee error < 10ulp.)
+
+ We assume that: x is positive and not equal to 1, and y is nonzero.
+ """
+
+ # Find b such that 10**(b-1) <= |y| <= 10**b
+ b = len(str(abs(yc))) + ye
+
+ # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point
+ lxc = _dlog(xc, xe, p+b+1)
+
+ # compute product y*log(x) = yc*lxc*10**(-p-b-1+ye) = pc*10**(-p-1)
+ shift = ye-b
+ if shift >= 0:
+ pc = lxc*yc*10**shift
+ else:
+ pc = _div_nearest(lxc*yc, 10**-shift)
+
+ if pc == 0:
+ # we prefer a result that isn't exactly 1; this makes it
+ # easier to compute a correctly rounded result in __pow__
+ if ((len(str(xc)) + xe >= 1) == (yc > 0)): # if x**y > 1:
+ coeff, exp = 10**(p-1)+1, 1-p
+ else:
+ coeff, exp = 10**p-1, -p
+ else:
+ coeff, exp = _dexp(pc, -(p+1), p+1)
+ coeff = _div_nearest(coeff, 10)
+ exp += 1
+
+ return coeff, exp
+
+def _log10_lb(c, correction = {
+ '1': 100, '2': 70, '3': 53, '4': 40, '5': 31,
+ '6': 23, '7': 16, '8': 10, '9': 5}):
+ """Compute a lower bound for 100*log10(c) for a positive integer c."""
+ if c <= 0:
+ raise ValueError("The argument to _log10_lb should be nonnegative.")
+ str_c = str(c)
+ return 100*len(str_c) - correction[str_c[0]]
+
+##### Helper Functions ####################################################
+
+def _convert_other(other, raiseit=False, allow_float=False):
+ """Convert other to Decimal.
+
+ Verifies that it's ok to use in an implicit construction.
+ If allow_float is true, allow conversion from float; this
+ is used in the comparison methods (__eq__ and friends).
+
+ """
+ if isinstance(other, Decimal):
+ return other
+ if isinstance(other, int):
+ return Decimal(other)
+ if allow_float and isinstance(other, float):
+ return Decimal.from_float(other)
+
+ if raiseit:
+ raise TypeError("Unable to convert %s to Decimal" % other)
+ return NotImplemented
+
+def _convert_for_comparison(self, other, equality_op=False):
+ """Given a Decimal instance self and a Python object other, return
+ a pair (s, o) of Decimal instances such that "s op o" is
+ equivalent to "self op other" for any of the 6 comparison
+ operators "op".
+
+ """
+ if isinstance(other, Decimal):
+ return self, other
+
+ # Comparison with a Rational instance (also includes integers):
+ # self op n/d <=> self*d op n (for n and d integers, d positive).
+ # A NaN or infinity can be left unchanged without affecting the
+ # comparison result.
+ if isinstance(other, _numbers.Rational):
+ if not self._is_special:
+ self = _dec_from_triple(self._sign,
+ str(int(self._int) * other.denominator),
+ self._exp)
+ return self, Decimal(other.numerator)
+
+ # Comparisons with float and complex types. == and != comparisons
+ # with complex numbers should succeed, returning either True or False
+ # as appropriate. Other comparisons return NotImplemented.
+ if equality_op and isinstance(other, _numbers.Complex) and other.imag == 0:
+ other = other.real
+ if isinstance(other, float):
+ context = getcontext()
+ if equality_op:
+ context.flags[FloatOperation] = 1
+ else:
+ context._raise_error(FloatOperation,
+ "strict semantics for mixing floats and Decimals are enabled")
+ return self, Decimal.from_float(other)
+ return NotImplemented, NotImplemented
+
+
+##### Setup Specific Contexts ############################################
+
+# The default context prototype used by Context()
+# Is mutable, so that new contexts can have different default values
+
+DefaultContext = Context(
+ prec=28, rounding=ROUND_HALF_EVEN,
+ traps=[DivisionByZero, Overflow, InvalidOperation],
+ flags=[],
+ Emax=999999,
+ Emin=-999999,
+ capitals=1,
+ clamp=0
+)
+
+# Pre-made alternate contexts offered by the specification
+# Don't change these; the user should be able to select these
+# contexts and be able to reproduce results from other implementations
+# of the spec.
+
+BasicContext = Context(
+ prec=9, rounding=ROUND_HALF_UP,
+ traps=[DivisionByZero, Overflow, InvalidOperation, Clamped, Underflow],
+ flags=[],
+)
+
+ExtendedContext = Context(
+ prec=9, rounding=ROUND_HALF_EVEN,
+ traps=[],
+ flags=[],
+)
+
+
+##### crud for parsing strings #############################################
+#
+# Regular expression used for parsing numeric strings. Additional
+# comments:
+#
+# 1. Uncomment the two '\s*' lines to allow leading and/or trailing
+# whitespace. But note that the specification disallows whitespace in
+# a numeric string.
+#
+# 2. For finite numbers (not infinities and NaNs) the body of the
+# number between the optional sign and the optional exponent must have
+# at least one decimal digit, possibly after the decimal point. The
+# lookahead expression '(?=\d|\.\d)' checks this.
+
+import re
+_parser = re.compile(r""" # A numeric string consists of:
+# \s*
+ (?P<sign>[-+])? # an optional sign, followed by either...
+ (
+ (?=\d|\.\d) # ...a number (with at least one digit)
+ (?P<int>\d*) # having a (possibly empty) integer part
+ (\.(?P<frac>\d*))? # followed by an optional fractional part
+ (E(?P<exp>[-+]?\d+))? # followed by an optional exponent, or...
+ |
+ Inf(inity)? # ...an infinity, or...
+ |
+ (?P<signal>s)? # ...an (optionally signaling)
+ NaN # NaN
+ (?P<diag>\d*) # with (possibly empty) diagnostic info.
+ )
+# \s*
+ \Z
+""", re.VERBOSE | re.IGNORECASE).match
+
+_all_zeros = re.compile('0*$').match
+_exact_half = re.compile('50*$').match
+
+##### PEP3101 support functions ##############################################
+# The functions in this section have little to do with the Decimal
+# class, and could potentially be reused or adapted for other pure
+# Python numeric classes that want to implement __format__
+#
+# A format specifier for Decimal looks like:
+#
+# [[fill]align][sign][#][0][minimumwidth][,][.precision][type]
+
+_parse_format_specifier_regex = re.compile(r"""\A
+(?:
+ (?P<fill>.)?
+ (?P<align>[<>=^])
+)?
+(?P<sign>[-+ ])?
+(?P<alt>\#)?
+(?P<zeropad>0)?
+(?P<minimumwidth>(?!0)\d+)?
+(?P<thousands_sep>,)?
+(?:\.(?P<precision>0|(?!0)\d+))?
+(?P<type>[eEfFgGn%])?
+\Z
+""", re.VERBOSE|re.DOTALL)
+
+del re
+
+# The locale module is only needed for the 'n' format specifier. The
+# rest of the PEP 3101 code functions quite happily without it, so we
+# don't care too much if locale isn't present.
+try:
+ import locale as _locale
+except ImportError:
+ pass
+
+def _parse_format_specifier(format_spec, _localeconv=None):
+ """Parse and validate a format specifier.
+
+ Turns a standard numeric format specifier into a dict, with the
+ following entries:
+
+ fill: fill character to pad field to minimum width
+ align: alignment type, either '<', '>', '=' or '^'
+ sign: either '+', '-' or ' '
+ minimumwidth: nonnegative integer giving minimum width
+ zeropad: boolean, indicating whether to pad with zeros
+ thousands_sep: string to use as thousands separator, or ''
+ grouping: grouping for thousands separators, in format
+ used by localeconv
+ decimal_point: string to use for decimal point
+ precision: nonnegative integer giving precision, or None
+ type: one of the characters 'eEfFgG%', or None
+
+ """
+ m = _parse_format_specifier_regex.match(format_spec)
+ if m is None:
+ raise ValueError("Invalid format specifier: " + format_spec)
+
+ # get the dictionary
+ format_dict = m.groupdict()
+
+ # zeropad; defaults for fill and alignment. If zero padding
+ # is requested, the fill and align fields should be absent.
+ fill = format_dict['fill']
+ align = format_dict['align']
+ format_dict['zeropad'] = (format_dict['zeropad'] is not None)
+ if format_dict['zeropad']:
+ if fill is not None:
+ raise ValueError("Fill character conflicts with '0'"
+ " in format specifier: " + format_spec)
+ if align is not None:
+ raise ValueError("Alignment conflicts with '0' in "
+ "format specifier: " + format_spec)
+ format_dict['fill'] = fill or ' '
+ # PEP 3101 originally specified that the default alignment should
+ # be left; it was later agreed that right-aligned makes more sense
+ # for numeric types. See http://bugs.python.org/issue6857.
+ format_dict['align'] = align or '>'
+
+ # default sign handling: '-' for negative, '' for positive
+ if format_dict['sign'] is None:
+ format_dict['sign'] = '-'
+
+ # minimumwidth defaults to 0; precision remains None if not given
+ format_dict['minimumwidth'] = int(format_dict['minimumwidth'] or '0')
+ if format_dict['precision'] is not None:
+ format_dict['precision'] = int(format_dict['precision'])
+
+ # if format type is 'g' or 'G' then a precision of 0 makes little
+ # sense; convert it to 1. Same if format type is unspecified.
+ if format_dict['precision'] == 0:
+ if format_dict['type'] is None or format_dict['type'] in 'gGn':
+ format_dict['precision'] = 1
+
+ # determine thousands separator, grouping, and decimal separator, and
+ # add appropriate entries to format_dict
+ if format_dict['type'] == 'n':
+ # apart from separators, 'n' behaves just like 'g'
+ format_dict['type'] = 'g'
+ if _localeconv is None:
+ _localeconv = _locale.localeconv()
+ if format_dict['thousands_sep'] is not None:
+ raise ValueError("Explicit thousands separator conflicts with "
+ "'n' type in format specifier: " + format_spec)
+ format_dict['thousands_sep'] = _localeconv['thousands_sep']
+ format_dict['grouping'] = _localeconv['grouping']
+ format_dict['decimal_point'] = _localeconv['decimal_point']
+ else:
+ if format_dict['thousands_sep'] is None:
+ format_dict['thousands_sep'] = ''
+ format_dict['grouping'] = [3, 0]
+ format_dict['decimal_point'] = '.'
+
+ return format_dict
+
+def _format_align(sign, body, spec):
+ """Given an unpadded, non-aligned numeric string 'body' and sign
+ string 'sign', add padding and alignment conforming to the given
+ format specifier dictionary 'spec' (as produced by
+ parse_format_specifier).
+
+ """
+ # how much extra space do we have to play with?
+ minimumwidth = spec['minimumwidth']
+ fill = spec['fill']
+ padding = fill*(minimumwidth - len(sign) - len(body))
+
+ align = spec['align']
+ if align == '<':
+ result = sign + body + padding
+ elif align == '>':
+ result = padding + sign + body
+ elif align == '=':
+ result = sign + padding + body
+ elif align == '^':
+ half = len(padding)//2
+ result = padding[:half] + sign + body + padding[half:]
+ else:
+ raise ValueError('Unrecognised alignment field')
+
+ return result
+
+def _group_lengths(grouping):
+ """Convert a localeconv-style grouping into a (possibly infinite)
+ iterable of integers representing group lengths.
+
+ """
+ # The result from localeconv()['grouping'], and the input to this
+ # function, should be a list of integers in one of the
+ # following three forms:
+ #
+ # (1) an empty list, or
+ # (2) nonempty list of positive integers + [0]
+ # (3) list of positive integers + [locale.CHAR_MAX], or
+
+ from itertools import chain, repeat
+ if not grouping:
+ return []
+ elif grouping[-1] == 0 and len(grouping) >= 2:
+ return chain(grouping[:-1], repeat(grouping[-2]))
+ elif grouping[-1] == _locale.CHAR_MAX:
+ return grouping[:-1]
+ else:
+ raise ValueError('unrecognised format for grouping')
+
+def _insert_thousands_sep(digits, spec, min_width=1):
+ """Insert thousands separators into a digit string.
+
+ spec is a dictionary whose keys should include 'thousands_sep' and
+ 'grouping'; typically it's the result of parsing the format
+ specifier using _parse_format_specifier.
+
+ The min_width keyword argument gives the minimum length of the
+ result, which will be padded on the left with zeros if necessary.
+
+ If necessary, the zero padding adds an extra '0' on the left to
+ avoid a leading thousands separator. For example, inserting
+ commas every three digits in '123456', with min_width=8, gives
+ '0,123,456', even though that has length 9.
+
+ """
+
+ sep = spec['thousands_sep']
+ grouping = spec['grouping']
+
+ groups = []
+ for l in _group_lengths(grouping):
+ if l <= 0:
+ raise ValueError("group length should be positive")
+ # max(..., 1) forces at least 1 digit to the left of a separator
+ l = min(max(len(digits), min_width, 1), l)
+ groups.append('0'*(l - len(digits)) + digits[-l:])
+ digits = digits[:-l]
+ min_width -= l
+ if not digits and min_width <= 0:
+ break
+ min_width -= len(sep)
+ else:
+ l = max(len(digits), min_width, 1)
+ groups.append('0'*(l - len(digits)) + digits[-l:])
+ return sep.join(reversed(groups))
+
+def _format_sign(is_negative, spec):
+ """Determine sign character."""
+
+ if is_negative:
+ return '-'
+ elif spec['sign'] in ' +':
+ return spec['sign']
+ else:
+ return ''
+
+def _format_number(is_negative, intpart, fracpart, exp, spec):
+ """Format a number, given the following data:
+
+ is_negative: true if the number is negative, else false
+ intpart: string of digits that must appear before the decimal point
+ fracpart: string of digits that must come after the point
+ exp: exponent, as an integer
+ spec: dictionary resulting from parsing the format specifier
+
+ This function uses the information in spec to:
+ insert separators (decimal separator and thousands separators)
+ format the sign
+ format the exponent
+ add trailing '%' for the '%' type
+ zero-pad if necessary
+ fill and align if necessary
+ """
+
+ sign = _format_sign(is_negative, spec)
+
+ if fracpart or spec['alt']:
+ fracpart = spec['decimal_point'] + fracpart
+
+ if exp != 0 or spec['type'] in 'eE':
+ echar = {'E': 'E', 'e': 'e', 'G': 'E', 'g': 'e'}[spec['type']]
+ fracpart += "{0}{1:+}".format(echar, exp)
+ if spec['type'] == '%':
+ fracpart += '%'
+
+ if spec['zeropad']:
+ min_width = spec['minimumwidth'] - len(fracpart) - len(sign)
+ else:
+ min_width = 0
+ intpart = _insert_thousands_sep(intpart, spec, min_width)
+
+ return _format_align(sign, intpart+fracpart, spec)
+
+
+##### Useful Constants (internal use only) ################################
+
+# Reusable defaults
+_Infinity = Decimal('Inf')
+_NegativeInfinity = Decimal('-Inf')
+_NaN = Decimal('NaN')
+_Zero = Decimal(0)
+_One = Decimal(1)
+_NegativeOne = Decimal(-1)
+
+# _SignedInfinity[sign] is infinity w/ that sign
+_SignedInfinity = (_Infinity, _NegativeInfinity)
+
+# Constants related to the hash implementation; hash(x) is based
+# on the reduction of x modulo _PyHASH_MODULUS
+_PyHASH_MODULUS = sys.hash_info.modulus
+# hash values to use for positive and negative infinities, and nans
+_PyHASH_INF = sys.hash_info.inf
+_PyHASH_NAN = sys.hash_info.nan
+
+# _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS
+_PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS)
+del sys
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 577b600..b0f0114 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -6,6 +6,7 @@ import os
import abc
import codecs
import errno
+import array
# Import _thread instead of threading to reduce startup cost
try:
from _thread import allocate_lock as Lock
@@ -256,7 +257,7 @@ class OpenWrapper:
Trick so that open won't become a bound method when stored
as a class variable (as dbm.dumb does).
- See initstdio() in Python/pythonrun.c.
+ See initstdio() in Python/pylifecycle.c.
"""
__doc__ = DocDescriptor()
@@ -662,16 +663,33 @@ class BufferedIOBase(IOBase):
Raises BlockingIOError if the underlying raw stream has no
data at the moment.
"""
- # XXX This ought to work with anything that supports the buffer API
- data = self.read(len(b))
+
+ return self._readinto(b, read1=False)
+
+ def readinto1(self, b):
+ """Read up to len(b) bytes into *b*, using at most one system call
+
+ Returns an int representing the number of bytes read (0 for EOF).
+
+ Raises BlockingIOError if the underlying raw stream has no
+ data at the moment.
+ """
+
+ return self._readinto(b, read1=True)
+
+ def _readinto(self, b, read1):
+ if not isinstance(b, memoryview):
+ b = memoryview(b)
+ b = b.cast('B')
+
+ if read1:
+ data = self.read1(len(b))
+ else:
+ data = self.read(len(b))
n = len(data)
- try:
- b[:n] = data
- except TypeError as err:
- import array
- if not isinstance(b, array.array):
- raise err
- b[:n] = array.array('b', data)
+
+ b[:n] = data
+
return n
def write(self, b):
@@ -790,13 +808,14 @@ class _BufferedIOMixin(BufferedIOBase):
.format(self.__class__.__name__))
def __repr__(self):
- clsname = self.__class__.__name__
+ modname = self.__class__.__module__
+ clsname = self.__class__.__qualname__
try:
name = self.name
except Exception:
- return "<_pyio.{0}>".format(clsname)
+ return "<{}.{}>".format(modname, clsname)
else:
- return "<_pyio.{0} name={1!r}>".format(clsname, name)
+ return "<{}.{} name={!r}>".format(modname, clsname, name)
### Lower-level APIs ###
@@ -993,10 +1012,7 @@ class BufferedReader(_BufferedIOMixin):
current_size = 0
while True:
# Read until EOF or until read() would block.
- try:
- chunk = self.raw.read()
- except InterruptedError:
- continue
+ chunk = self.raw.read()
if chunk in empty_values:
nodata_val = chunk
break
@@ -1015,10 +1031,7 @@ class BufferedReader(_BufferedIOMixin):
chunks = [buf[pos:]]
wanted = max(self.buffer_size, n)
while avail < n:
- try:
- chunk = self.raw.read(wanted)
- except InterruptedError:
- continue
+ chunk = self.raw.read(wanted)
if chunk in empty_values:
nodata_val = chunk
break
@@ -1047,12 +1060,7 @@ class BufferedReader(_BufferedIOMixin):
have = len(self._read_buf) - self._read_pos
if have < want or have <= 0:
to_read = self.buffer_size - have
- while True:
- try:
- current = self.raw.read(to_read)
- except InterruptedError:
- continue
- break
+ current = self.raw.read(to_read)
if current:
self._read_buf = self._read_buf[self._read_pos:] + current
self._read_pos = 0
@@ -1071,6 +1079,58 @@ class BufferedReader(_BufferedIOMixin):
return self._read_unlocked(
min(size, len(self._read_buf) - self._read_pos))
+ # Implementing readinto() and readinto1() is not strictly necessary (we
+ # could rely on the base class that provides an implementation in terms of
+ # read() and read1()). We do it anyway to keep the _pyio implementation
+ # similar to the io implementation (which implements the methods for
+ # performance reasons).
+ def _readinto(self, buf, read1):
+ """Read data into *buf* with at most one system call."""
+
+ if len(buf) == 0:
+ return 0
+
+ # Need to create a memoryview object of type 'b', otherwise
+ # we may not be able to assign bytes to it, and slicing it
+ # would create a new object.
+ if not isinstance(buf, memoryview):
+ buf = memoryview(buf)
+ buf = buf.cast('B')
+
+ written = 0
+ with self._read_lock:
+ while written < len(buf):
+
+ # First try to read from internal buffer
+ avail = min(len(self._read_buf) - self._read_pos, len(buf))
+ if avail:
+ buf[written:written+avail] = \
+ self._read_buf[self._read_pos:self._read_pos+avail]
+ self._read_pos += avail
+ written += avail
+ if written == len(buf):
+ break
+
+ # If remaining space in callers buffer is larger than
+ # internal buffer, read directly into callers buffer
+ if len(buf) - written > self.buffer_size:
+ n = self.raw.readinto(buf[written:])
+ if not n:
+ break # eof
+ written += n
+
+ # Otherwise refill internal buffer - unless we're
+ # in read1 mode and already got some data
+ elif not (read1 and written):
+ if not self._peek_unlocked(1):
+ break # eof
+
+ # In readinto1 mode, return as soon as we have some data
+ if read1 and written:
+ break
+
+ return written
+
def tell(self):
return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos
@@ -1149,8 +1209,6 @@ class BufferedWriter(_BufferedIOMixin):
while self._write_buf:
try:
n = self.raw.write(self._write_buf)
- except InterruptedError:
- continue
except BlockingIOError:
raise RuntimeError("self.raw should implement RawIOBase: it "
"should not raise BlockingIOError")
@@ -1220,6 +1278,9 @@ class BufferedRWPair(BufferedIOBase):
def read1(self, size):
return self.reader.read1(size)
+ def readinto1(self, b):
+ return self.reader.readinto1(b)
+
def readable(self):
return self.reader.readable()
@@ -1302,6 +1363,10 @@ class BufferedRandom(BufferedWriter, BufferedReader):
self.flush()
return BufferedReader.read1(self, size)
+ def readinto1(self, b):
+ self.flush()
+ return BufferedReader.readinto1(self, b)
+
def write(self, b):
if self._read_buf:
# Undo readahead
@@ -1564,7 +1629,8 @@ class TextIOWrapper(TextIOBase):
# - "chars_..." for integer variables that count decoded characters
def __repr__(self):
- result = "<_pyio.TextIOWrapper"
+ result = "<{}.{}".format(self.__class__.__module__,
+ self.__class__.__qualname__)
try:
name = self.name
except Exception:
diff --git a/Lib/_strptime.py b/Lib/_strptime.py
index 53bd34b..42f23d3 100644
--- a/Lib/_strptime.py
+++ b/Lib/_strptime.py
@@ -167,9 +167,9 @@ class LocaleTime(object):
time.tzset()
except AttributeError:
pass
- no_saving = frozenset(["utc", "gmt", time.tzname[0].lower()])
+ no_saving = frozenset({"utc", "gmt", time.tzname[0].lower()})
if time.daylight:
- has_saving = frozenset([time.tzname[1].lower()])
+ has_saving = frozenset({time.tzname[1].lower()})
else:
has_saving = frozenset()
self.timezone = (no_saving, has_saving)
diff --git a/Lib/abc.py b/Lib/abc.py
index 0358a46..1cbf96a 100644
--- a/Lib/abc.py
+++ b/Lib/abc.py
@@ -168,7 +168,7 @@ class ABCMeta(type):
def _dump_registry(cls, file=None):
"""Debug helper to print the ABC registry."""
- print("Class: %s.%s" % (cls.__module__, cls.__name__), file=file)
+ print("Class: %s.%s" % (cls.__module__, cls.__qualname__), file=file)
print("Inv.counter: %s" % ABCMeta._abc_invalidation_counter, file=file)
for name in sorted(cls.__dict__.keys()):
if name.startswith("_abc_"):
diff --git a/Lib/argparse.py b/Lib/argparse.py
index be276bb..9a06719 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1209,11 +1209,6 @@ class Namespace(_AttributeHolder):
return NotImplemented
return vars(self) == vars(other)
- def __ne__(self, other):
- if not isinstance(other, Namespace):
- return NotImplemented
- return not (self == other)
-
def __contains__(self, key):
return key in self.__dict__
@@ -1595,6 +1590,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
- argument_default -- The default value for all arguments
- conflict_handler -- String indicating how to handle conflicts
- add_help -- Add a -h/-help option
+ - allow_abbrev -- Allow long options to be abbreviated unambiguously
"""
def __init__(self,
@@ -1608,7 +1604,8 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
fromfile_prefix_chars=None,
argument_default=None,
conflict_handler='error',
- add_help=True):
+ add_help=True,
+ allow_abbrev=True):
superinit = super(ArgumentParser, self).__init__
superinit(description=description,
@@ -1626,6 +1623,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
self.formatter_class = formatter_class
self.fromfile_prefix_chars = fromfile_prefix_chars
self.add_help = add_help
+ self.allow_abbrev = allow_abbrev
add_group = self.add_argument_group
self._positionals = add_group(_('positional arguments'))
@@ -2103,23 +2101,24 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
action = self._option_string_actions[option_string]
return action, option_string, explicit_arg
- # search through all possible prefixes of the option string
- # and all actions in the parser for possible interpretations
- option_tuples = self._get_option_tuples(arg_string)
-
- # if multiple actions match, the option string was ambiguous
- if len(option_tuples) > 1:
- options = ', '.join([option_string
- for action, option_string, explicit_arg in option_tuples])
- args = {'option': arg_string, 'matches': options}
- msg = _('ambiguous option: %(option)s could match %(matches)s')
- self.error(msg % args)
-
- # if exactly one action matched, this segmentation is good,
- # so return the parsed action
- elif len(option_tuples) == 1:
- option_tuple, = option_tuples
- return option_tuple
+ if self.allow_abbrev:
+ # search through all possible prefixes of the option string
+ # and all actions in the parser for possible interpretations
+ option_tuples = self._get_option_tuples(arg_string)
+
+ # if multiple actions match, the option string was ambiguous
+ if len(option_tuples) > 1:
+ options = ', '.join([option_string
+ for action, option_string, explicit_arg in option_tuples])
+ args = {'option': arg_string, 'matches': options}
+ msg = _('ambiguous option: %(option)s could match %(matches)s')
+ self.error(msg % args)
+
+ # if exactly one action matched, this segmentation is good,
+ # so return the parsed action
+ elif len(option_tuples) == 1:
+ option_tuple, = option_tuples
+ return option_tuple
# if it was not found as an option, but it looks like a negative
# number, it was meant to be positional
diff --git a/Lib/asynchat.py b/Lib/asynchat.py
index 14c152f..f728d1b 100644
--- a/Lib/asynchat.py
+++ b/Lib/asynchat.py
@@ -287,6 +287,9 @@ class simple_producer:
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:
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
index 8a7bb81..01320cd 100644
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -178,10 +178,6 @@ class TimerHandle(Handle):
self._cancelled == other._cancelled)
return NotImplemented
- def __ne__(self, other):
- equal = self.__eq__(other)
- return NotImplemented if equal is NotImplemented else not equal
-
def cancel(self):
if not self._cancelled:
self._loop._timer_handle_cancelled(self)
diff --git a/Lib/asyncore.py b/Lib/asyncore.py
index 00a6396..68efd45 100644
--- a/Lib/asyncore.py
+++ b/Lib/asyncore.py
@@ -57,8 +57,8 @@ from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \
ENOTCONN, ESHUTDOWN, EISCONN, EBADF, ECONNABORTED, EPIPE, EAGAIN, \
errorcode
-_DISCONNECTED = frozenset((ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE,
- EBADF))
+_DISCONNECTED = frozenset({ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE,
+ EBADF})
try:
socket_map
@@ -220,7 +220,7 @@ class dispatcher:
connecting = False
closing = False
addr = None
- ignore_log_types = frozenset(['warning'])
+ ignore_log_types = frozenset({'warning'})
def __init__(self, sock=None, map=None):
if map is None:
@@ -255,7 +255,7 @@ class dispatcher:
self.socket = None
def __repr__(self):
- status = [self.__class__.__module__+"."+self.__class__.__name__]
+ status = [self.__class__.__module__+"."+self.__class__.__qualname__]
if self.accepting and self.addr:
status.append('listening')
elif self.connected:
@@ -404,20 +404,6 @@ class dispatcher:
if why.args[0] not in (ENOTCONN, EBADF):
raise
- # cheap inheritance, used to pass all other attribute
- # references to the underlying socket object.
- def __getattr__(self, attr):
- try:
- retattr = getattr(self.socket, attr)
- except AttributeError:
- raise AttributeError("%s instance has no attribute '%s'"
- %(self.__class__.__name__, attr))
- else:
- msg = "%(me)s.%(attr)s is deprecated; use %(me)s.socket.%(attr)s " \
- "instead" % {'me' : self.__class__.__name__, 'attr' : attr}
- warnings.warn(msg, DeprecationWarning, stacklevel=2)
- return retattr
-
# log and log_info may be overridden to provide more sophisticated
# logging and warning methods. In general, log is for 'hit' logging
# and 'log_info' is for informational, warning and error logging.
@@ -604,8 +590,6 @@ def close_all(map=None, ignore_all=False):
# Regardless, this is useful for pipes, and stdin/stdout...
if os.name == 'posix':
- import fcntl
-
class file_wrapper:
# Here we override just enough to make a file
# look like a socket for the purposes of asyncore.
@@ -656,9 +640,7 @@ if os.name == 'posix':
pass
self.set_file(fd)
# set it to non-blocking mode
- flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
- flags = flags | os.O_NONBLOCK
- fcntl.fcntl(fd, fcntl.F_SETFL, flags)
+ os.set_blocking(fd, False)
def set_file(self, fd):
self.socket = file_wrapper(fd)
diff --git a/Lib/cgi.py b/Lib/cgi.py
index 1ef780c..a55232e 100755
--- a/Lib/cgi.py
+++ b/Lib/cgi.py
@@ -566,6 +566,12 @@ class FieldStorage:
except AttributeError:
pass
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self.file.close()
+
def __repr__(self):
"""Return a printable representation."""
return "FieldStorage(%r, %r, %r)" % (
diff --git a/Lib/code.py b/Lib/code.py
index f8184b6..86e1f03 100644
--- a/Lib/code.py
+++ b/Lib/code.py
@@ -7,6 +7,7 @@
import sys
import traceback
+import argparse
from codeop import CommandCompiler, compile_command
__all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
@@ -136,25 +137,35 @@ class InteractiveInterpreter:
The output is written by self.write(), below.
"""
+ sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
+ sys.last_traceback = last_tb
try:
- type, value, tb = sys.exc_info()
- sys.last_type = type
- sys.last_value = value
- sys.last_traceback = tb
- tblist = traceback.extract_tb(tb)
- del tblist[:1]
- lines = traceback.format_list(tblist)
- if lines:
- lines.insert(0, "Traceback (most recent call last):\n")
- lines.extend(traceback.format_exception_only(type, value))
+ lines = []
+ for value, tb in traceback._iter_chain(*ei[1:]):
+ if isinstance(value, str):
+ lines.append(value)
+ lines.append('\n')
+ continue
+ if tb:
+ tblist = traceback.extract_tb(tb)
+ if tb is last_tb:
+ # The last traceback includes the frame we
+ # exec'd in
+ del tblist[:1]
+ tblines = traceback.format_list(tblist)
+ if tblines:
+ lines.append("Traceback (most recent call last):\n")
+ lines.extend(tblines)
+ lines.extend(traceback.format_exception_only(type(value),
+ value))
finally:
- tblist = tb = None
+ tblist = last_tb = ei = None
if sys.excepthook is sys.__excepthook__:
self.write(''.join(lines))
else:
# If someone has set sys.excepthook, we let that take precedence
# over self.write
- sys.excepthook(type, value, tb)
+ sys.excepthook(type, value, last_tb)
def write(self, data):
"""Write a string.
@@ -299,4 +310,12 @@ def interact(banner=None, readfunc=None, local=None):
if __name__ == "__main__":
- interact()
+ parser = argparse.ArgumentParser()
+ parser.add_argument('-q', action='store_true',
+ help="don't print version and copyright messages")
+ args = parser.parse_args()
+ if args.q or sys.flags.quiet:
+ banner = ''
+ else:
+ banner = None
+ interact(banner)
diff --git a/Lib/codecs.py b/Lib/codecs.py
index 145bf12..c2b5d6c 100644
--- a/Lib/codecs.py
+++ b/Lib/codecs.py
@@ -27,7 +27,8 @@ __all__ = ["register", "lookup", "open", "EncodedFile", "BOM", "BOM_BE",
"getincrementaldecoder", "getreader", "getwriter",
"encode", "decode", "iterencode", "iterdecode",
"strict_errors", "ignore_errors", "replace_errors",
- "xmlcharrefreplace_errors", "backslashreplace_errors",
+ "xmlcharrefreplace_errors",
+ "backslashreplace_errors", "namereplace_errors",
"register_error", "lookup_error"]
### Constants
@@ -105,8 +106,8 @@ class CodecInfo(tuple):
return self
def __repr__(self):
- return "<%s.%s object for encoding %s at 0x%x>" % \
- (self.__class__.__module__, self.__class__.__name__,
+ return "<%s.%s object for encoding %s at %#x>" % \
+ (self.__class__.__module__, self.__class__.__qualname__,
self.name, id(self))
class Codec:
@@ -126,7 +127,8 @@ class Codec:
'surrogateescape' - replace with private code points U+DCnn.
'xmlcharrefreplace' - Replace with the appropriate XML
character reference (only for encoding).
- 'backslashreplace' - Replace with backslashed escape sequences
+ 'backslashreplace' - Replace with backslashed escape sequences.
+ 'namereplace' - Replace with \\N{...} escape sequences
(only for encoding).
The set of allowed values can be extended via register_error.
@@ -358,7 +360,8 @@ class StreamWriter(Codec):
'xmlcharrefreplace' - Replace with the appropriate XML
character reference.
'backslashreplace' - Replace with backslashed escape
- sequences (only for encoding).
+ sequences.
+ 'namereplace' - Replace with \\N{...} escape sequences.
The set of allowed parameter values can be extended via
register_error.
@@ -428,7 +431,8 @@ class StreamReader(Codec):
'strict' - raise a ValueError (or a subclass)
'ignore' - ignore the character and continue with the next
- 'replace'- replace with a suitable replacement character;
+ 'replace'- replace with a suitable replacement character
+ 'backslashreplace' - Replace with backslashed escape sequences;
The set of allowed parameter values can be extended via
register_error.
@@ -1080,6 +1084,7 @@ try:
replace_errors = lookup_error("replace")
xmlcharrefreplace_errors = lookup_error("xmlcharrefreplace")
backslashreplace_errors = lookup_error("backslashreplace")
+ namereplace_errors = lookup_error("namereplace")
except LookupError:
# In --disable-unicode builds, these error handler are missing
strict_errors = None
@@ -1087,6 +1092,7 @@ except LookupError:
replace_errors = None
xmlcharrefreplace_errors = None
backslashreplace_errors = None
+ namereplace_errors = None
# Tell modulefinder that using codecs probably needs the encodings
# package
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 565ae86..ce67f0b 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -20,6 +20,23 @@ from reprlib import recursive_repr as _recursive_repr
### OrderedDict
################################################################################
+class _OrderedDictKeysView(KeysView):
+
+ def __reversed__(self):
+ yield from reversed(self._mapping)
+
+class _OrderedDictItemsView(ItemsView):
+
+ def __reversed__(self):
+ for key in reversed(self._mapping):
+ yield (key, self._mapping[key])
+
+class _OrderedDictValuesView(ValuesView):
+
+ def __reversed__(self):
+ for key in reversed(self._mapping):
+ yield self._mapping[key]
+
class _Link(object):
__slots__ = 'prev', 'next', 'key', '__weakref__'
@@ -83,6 +100,8 @@ class OrderedDict(dict):
link_next = link.next
link_prev.next = link_next
link_next.prev = link_prev
+ link.prev = None
+ link.next = None
def __iter__(self):
'od.__iter__() <==> iter(od)'
@@ -166,9 +185,19 @@ class OrderedDict(dict):
return size
update = __update = MutableMapping.update
- keys = MutableMapping.keys
- values = MutableMapping.values
- items = MutableMapping.items
+
+ def keys(self):
+ "D.keys() -> a set-like object providing a view on D's keys"
+ return _OrderedDictKeysView(self)
+
+ def items(self):
+ "D.items() -> a set-like object providing a view on D's items"
+ return _OrderedDictItemsView(self)
+
+ def values(self):
+ "D.values() -> an object providing a view on D's values"
+ return _OrderedDictValuesView(self)
+
__ne__ = MutableMapping.__ne__
__marker = object()
@@ -958,7 +987,6 @@ class UserList(MutableSequence):
def __lt__(self, other): return self.data < self.__cast(other)
def __le__(self, other): return self.data <= self.__cast(other)
def __eq__(self, other): return self.data == self.__cast(other)
- def __ne__(self, other): return self.data != self.__cast(other)
def __gt__(self, other): return self.data > self.__cast(other)
def __ge__(self, other): return self.data >= self.__cast(other)
def __cast(self, other):
@@ -1035,10 +1063,6 @@ class UserString(Sequence):
if isinstance(string, UserString):
return self.data == string.data
return self.data == string
- def __ne__(self, string):
- if isinstance(string, UserString):
- return self.data != string.data
- return self.data != string
def __lt__(self, string):
if isinstance(string, UserString):
return self.data < string.data
diff --git a/Lib/compileall.py b/Lib/compileall.py
index d957ee5..4f46920 100644
--- a/Lib/compileall.py
+++ b/Lib/compileall.py
@@ -16,32 +16,24 @@ import importlib.util
import py_compile
import struct
-__all__ = ["compile_dir","compile_file","compile_path"]
-
-def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
- quiet=False, legacy=False, optimize=-1):
- """Byte-compile all modules in the given directory tree.
+try:
+ from concurrent.futures import ProcessPoolExecutor
+except ImportError:
+ ProcessPoolExecutor = None
+from functools import partial
- Arguments (only dir is required):
+__all__ = ["compile_dir","compile_file","compile_path"]
- dir: the directory to byte-compile
- maxlevels: maximum recursion level (default 10)
- ddir: the directory that will be prepended to the path to the
- file as it is compiled into each byte-code file.
- force: if True, force compilation, even if timestamps are up-to-date
- quiet: if True, be quiet during compilation
- legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
- optimize: optimization level or -1 for level of the interpreter
- """
+def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0):
if not quiet:
print('Listing {!r}...'.format(dir))
try:
names = os.listdir(dir)
except OSError:
- print("Can't list {!r}".format(dir))
+ if quiet < 2:
+ print("Can't list {!r}".format(dir))
names = []
names.sort()
- success = 1
for name in names:
if name == '__pycache__':
continue
@@ -51,17 +43,55 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
else:
dfile = None
if not os.path.isdir(fullname):
- if not compile_file(fullname, ddir, force, rx, quiet,
- legacy, optimize):
- success = 0
+ yield fullname
elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
os.path.isdir(fullname) and not os.path.islink(fullname)):
- if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
- quiet, legacy, optimize):
+ yield from _walk_dir(fullname, ddir=dfile,
+ maxlevels=maxlevels - 1, quiet=quiet)
+
+def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
+ quiet=0, legacy=False, optimize=-1, workers=1):
+ """Byte-compile all modules in the given directory tree.
+
+ Arguments (only dir is required):
+
+ dir: the directory to byte-compile
+ maxlevels: maximum recursion level (default 10)
+ ddir: the directory that will be prepended to the path to the
+ file as it is compiled into each byte-code file.
+ force: if True, force compilation, even if timestamps are up-to-date
+ quiet: full output with False or 0, errors only with 1,
+ no output with 2
+ legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
+ optimize: optimization level or -1 for level of the interpreter
+ workers: maximum number of parallel workers
+ """
+ files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels,
+ ddir=ddir)
+ success = 1
+ if workers is not None and workers != 1:
+ if workers < 0:
+ raise ValueError('workers must be greater or equal to 0')
+ if ProcessPoolExecutor is None:
+ raise NotImplementedError('multiprocessing support not available')
+
+ workers = workers or None
+ with ProcessPoolExecutor(max_workers=workers) as executor:
+ results = executor.map(partial(compile_file,
+ ddir=ddir, force=force,
+ rx=rx, quiet=quiet,
+ legacy=legacy,
+ optimize=optimize),
+ files)
+ success = min(results, default=1)
+ else:
+ for file in files:
+ if not compile_file(file, ddir, force, rx, quiet,
+ legacy, optimize):
success = 0
return success
-def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
+def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
legacy=False, optimize=-1):
"""Byte-compile one file.
@@ -71,7 +101,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
ddir: if given, the directory name compiled in to the
byte-code file.
force: if True, force compilation, even if timestamps are up-to-date
- quiet: if True, be quiet during compilation
+ quiet: full output with False or 0, errors only with 1,
+ no output with 2
legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
optimize: optimization level or -1 for level of the interpreter
"""
@@ -114,7 +145,10 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
ok = py_compile.compile(fullname, cfile, dfile, True,
optimize=optimize)
except py_compile.PyCompileError as err:
- if quiet:
+ success = 0
+ if quiet >= 2:
+ return success
+ elif quiet:
print('*** Error compiling {!r}...'.format(fullname))
else:
print('*** ', end='')
@@ -123,20 +157,21 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
errors='backslashreplace')
msg = msg.decode(sys.stdout.encoding)
print(msg)
- success = 0
except (SyntaxError, UnicodeError, OSError) as e:
- if quiet:
+ success = 0
+ if quiet >= 2:
+ return success
+ elif quiet:
print('*** Error compiling {!r}...'.format(fullname))
else:
print('*** ', end='')
print(e.__class__.__name__ + ':', e)
- success = 0
else:
if ok == 0:
success = 0
return success
-def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=False,
+def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
legacy=False, optimize=-1):
"""Byte-compile all module on sys.path.
@@ -145,14 +180,15 @@ def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=False,
skip_curdir: if true, skip current directory (default True)
maxlevels: max recursion level (default 0)
force: as for compile_dir() (default False)
- quiet: as for compile_dir() (default False)
+ quiet: as for compile_dir() (default 0)
legacy: as for compile_dir() (default False)
optimize: as for compile_dir() (default -1)
"""
success = 1
for dir in sys.path:
if (not dir or dir == os.curdir) and skip_curdir:
- print('Skipping current directory')
+ if quiet < 2:
+ print('Skipping current directory')
else:
success = success and compile_dir(dir, maxlevels, None,
force, quiet=quiet,
@@ -169,10 +205,15 @@ def main():
parser.add_argument('-l', action='store_const', const=0,
default=10, dest='maxlevels',
help="don't recurse into subdirectories")
+ parser.add_argument('-r', type=int, dest='recursion',
+ help=('control the maximum recursion level. '
+ 'if `-l` and `-r` options are specified, '
+ 'then `-r` takes precedence.'))
parser.add_argument('-f', action='store_true', dest='force',
help='force rebuild even if timestamps are up to date')
- parser.add_argument('-q', action='store_true', dest='quiet',
- help='output only error messages')
+ parser.add_argument('-q', action='count', dest='quiet', default=0,
+ help='output only error messages; -qq will suppress '
+ 'the error messages as well.')
parser.add_argument('-b', action='store_true', dest='legacy',
help='use legacy (pre-PEP3147) compiled file locations')
parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None,
@@ -192,8 +233,10 @@ def main():
help=('zero or more file and directory names '
'to compile; if no arguments given, defaults '
'to the equivalent of -l sys.path'))
- args = parser.parse_args()
+ parser.add_argument('-j', '--workers', default=1,
+ type=int, help='Run compileall concurrently')
+ args = parser.parse_args()
compile_dests = args.compile_dest
if (args.ddir and (len(compile_dests) != 1
@@ -203,6 +246,12 @@ def main():
import re
args.rx = re.compile(args.rx)
+
+ if args.recursion is not None:
+ maxlevels = args.recursion
+ else:
+ maxlevels = args.maxlevels
+
# if flist is provided then load it
if args.flist:
try:
@@ -210,9 +259,13 @@ def main():
for line in f:
compile_dests.append(line.strip())
except OSError:
- print("Error reading file list {}".format(args.flist))
+ if args.quiet < 2:
+ print("Error reading file list {}".format(args.flist))
return False
+ if args.workers is not None:
+ args.workers = args.workers or None
+
success = True
try:
if compile_dests:
@@ -222,16 +275,17 @@ def main():
args.quiet, args.legacy):
success = False
else:
- if not compile_dir(dest, args.maxlevels, args.ddir,
+ if not compile_dir(dest, maxlevels, args.ddir,
args.force, args.rx, args.quiet,
- args.legacy):
+ args.legacy, workers=args.workers):
success = False
return success
else:
return compile_path(legacy=args.legacy, force=args.force,
quiet=args.quiet)
except KeyboardInterrupt:
- print("\n[interrupted]")
+ if args.quiet < 2:
+ print("\n[interrupted]")
return False
return True
diff --git a/Lib/concurrent/futures/_base.py b/Lib/concurrent/futures/_base.py
index acd05d0..9e44713 100644
--- a/Lib/concurrent/futures/_base.py
+++ b/Lib/concurrent/futures/_base.py
@@ -302,17 +302,20 @@ class Future(object):
with self._condition:
if self._state == FINISHED:
if self._exception:
- return '<Future at %s state=%s raised %s>' % (
- hex(id(self)),
+ return '<%s at %#x state=%s raised %s>' % (
+ self.__class__.__name__,
+ id(self),
_STATE_TO_DESCRIPTION_MAP[self._state],
self._exception.__class__.__name__)
else:
- return '<Future at %s state=%s returned %s>' % (
- hex(id(self)),
+ return '<%s at %#x state=%s returned %s>' % (
+ self.__class__.__name__,
+ id(self),
_STATE_TO_DESCRIPTION_MAP[self._state],
self._result.__class__.__name__)
- return '<Future at %s state=%s>' % (
- hex(id(self)),
+ return '<%s at %#x state=%s>' % (
+ self.__class__.__name__,
+ id(self),
_STATE_TO_DESCRIPTION_MAP[self._state])
def cancel(self):
@@ -517,7 +520,7 @@ class Executor(object):
"""
raise NotImplementedError()
- def map(self, fn, *iterables, timeout=None):
+ def map(self, fn, *iterables, timeout=None, chunksize=1):
"""Returns a iterator equivalent to map(fn, iter).
Args:
@@ -525,6 +528,10 @@ class Executor(object):
passed iterables.
timeout: The maximum number of seconds to wait. If None, then there
is no limit on the wait time.
+ chunksize: The size of the chunks the iterable will be broken into
+ before being passed to a child process. This argument is only
+ used by ProcessPoolExecutor; it is ignored by
+ ThreadPoolExecutor.
Returns:
An iterator equivalent to: map(func, *iterables) but the calls may
diff --git a/Lib/concurrent/futures/process.py b/Lib/concurrent/futures/process.py
index 07b5225..3dd6da1 100644
--- a/Lib/concurrent/futures/process.py
+++ b/Lib/concurrent/futures/process.py
@@ -55,6 +55,9 @@ from multiprocessing import SimpleQueue
from multiprocessing.connection import wait
import threading
import weakref
+from functools import partial
+import itertools
+import traceback
# Workers are created as daemon threads and processes. This is done to allow the
# interpreter to exit when there are still idle processes in a
@@ -88,6 +91,27 @@ def _python_exit():
# (Futures in the call queue cannot be cancelled).
EXTRA_QUEUED_CALLS = 1
+# Hack to embed stringification of remote traceback in local traceback
+
+class _RemoteTraceback(Exception):
+ def __init__(self, tb):
+ self.tb = tb
+ def __str__(self):
+ return self.tb
+
+class _ExceptionWithTraceback:
+ def __init__(self, exc, tb):
+ tb = traceback.format_exception(type(exc), exc, tb)
+ tb = ''.join(tb)
+ self.exc = exc
+ self.tb = '\n"""\n%s"""' % tb
+ def __reduce__(self):
+ return _rebuild_exc, (self.exc, self.tb)
+
+def _rebuild_exc(exc, tb):
+ exc.__cause__ = _RemoteTraceback(tb)
+ return exc
+
class _WorkItem(object):
def __init__(self, future, fn, args, kwargs):
self.future = future
@@ -108,6 +132,26 @@ class _CallItem(object):
self.args = args
self.kwargs = kwargs
+def _get_chunks(*iterables, chunksize):
+ """ Iterates over zip()ed iterables in chunks. """
+ it = zip(*iterables)
+ while True:
+ chunk = tuple(itertools.islice(it, chunksize))
+ if not chunk:
+ return
+ yield chunk
+
+def _process_chunk(fn, chunk):
+ """ Processes a chunk of an iterable passed to map.
+
+ Runs the function passed to map() on a chunk of the
+ iterable passed to map.
+
+ This function is run in a separate process.
+
+ """
+ return [fn(*args) for args in chunk]
+
def _process_worker(call_queue, result_queue):
"""Evaluates calls from call_queue and places the results in result_queue.
@@ -130,8 +174,8 @@ def _process_worker(call_queue, result_queue):
try:
r = call_item.fn(*call_item.args, **call_item.kwargs)
except BaseException as e:
- result_queue.put(_ResultItem(call_item.work_id,
- exception=e))
+ exc = _ExceptionWithTraceback(e, e.__traceback__)
+ result_queue.put(_ResultItem(call_item.work_id, exception=exc))
else:
result_queue.put(_ResultItem(call_item.work_id,
result=r))
@@ -334,6 +378,9 @@ class ProcessPoolExecutor(_base.Executor):
if max_workers is None:
self._max_workers = os.cpu_count() or 1
else:
+ if max_workers <= 0:
+ raise ValueError("max_workers must be greater than 0")
+
self._max_workers = max_workers
# Make the call queue slightly larger than the number of processes to
@@ -408,6 +455,35 @@ class ProcessPoolExecutor(_base.Executor):
return f
submit.__doc__ = _base.Executor.submit.__doc__
+ def map(self, fn, *iterables, timeout=None, chunksize=1):
+ """Returns a iterator equivalent to map(fn, iter).
+
+ Args:
+ fn: A callable that will take as many arguments as there are
+ passed iterables.
+ timeout: The maximum number of seconds to wait. If None, then there
+ is no limit on the wait time.
+ chunksize: If greater than one, the iterables will be chopped into
+ chunks of size chunksize and submitted to the process pool.
+ If set to one, the items in the list will be sent one at a time.
+
+ Returns:
+ An iterator equivalent to: map(func, *iterables) but the calls may
+ be evaluated out-of-order.
+
+ Raises:
+ TimeoutError: If the entire result iterator could not be generated
+ before the given timeout.
+ Exception: If fn(*args) raises for any values.
+ """
+ if chunksize < 1:
+ raise ValueError("chunksize must be >= 1.")
+
+ results = super().map(partial(_process_chunk, fn),
+ _get_chunks(*iterables, chunksize=chunksize),
+ timeout=timeout)
+ return itertools.chain.from_iterable(results)
+
def shutdown(self, wait=True):
with self._shutdown_lock:
self._shutdown_thread = True
diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py
index f9beb0f..3ae442d 100644
--- a/Lib/concurrent/futures/thread.py
+++ b/Lib/concurrent/futures/thread.py
@@ -10,6 +10,7 @@ from concurrent.futures import _base
import queue
import threading
import weakref
+import os
# Workers are created as daemon threads. This is done to allow the interpreter
# to exit when there are still idle threads in a ThreadPoolExecutor's thread
@@ -80,13 +81,20 @@ def _worker(executor_reference, work_queue):
_base.LOGGER.critical('Exception in worker', exc_info=True)
class ThreadPoolExecutor(_base.Executor):
- def __init__(self, max_workers):
+ def __init__(self, max_workers=None):
"""Initializes a new ThreadPoolExecutor instance.
Args:
max_workers: The maximum number of threads that can be used to
execute the given calls.
"""
+ if max_workers is None:
+ # Use this number because ThreadPoolExecutor is often
+ # used to overlap I/O instead of CPU work.
+ max_workers = (os.cpu_count() or 1) * 5
+ if max_workers <= 0:
+ raise ValueError("max_workers must be greater than 0")
+
self._max_workers = max_workers
self._work_queue = queue.Queue()
self._threads = set()
diff --git a/Lib/configparser.py b/Lib/configparser.py
index 4ee8307..ecd0660 100644
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -17,7 +17,8 @@ ConfigParser -- responsible for parsing a list of
__init__(defaults=None, dict_type=_default_dict, allow_no_value=False,
delimiters=('=', ':'), comment_prefixes=('#', ';'),
inline_comment_prefixes=None, strict=True,
- empty_lines_in_values=True):
+ empty_lines_in_values=True, default_section='DEFAULT',
+ interpolation=<unset>, converters=<unset>):
Create the parser. When `defaults' is given, it is initialized into the
dictionary or intrinsic defaults. The keys must be strings, the values
must be appropriate for %()s string interpolation.
@@ -47,6 +48,25 @@ ConfigParser -- responsible for parsing a list of
When `allow_no_value' is True (default: False), options without
values are accepted; the value presented for these is None.
+ When `default_section' is given, the name of the special section is
+ named accordingly. By default it is called ``"DEFAULT"`` but this can
+ be customized to point to any other valid section name. Its current
+ value can be retrieved using the ``parser_instance.default_section``
+ attribute and may be modified at runtime.
+
+ When `interpolation` is given, it should be an Interpolation subclass
+ instance. It will be used as the handler for option value
+ pre-processing when using getters. RawConfigParser object s don't do
+ any sort of interpolation, whereas ConfigParser uses an instance of
+ BasicInterpolation. The library also provides a ``zc.buildbot``
+ inspired ExtendedInterpolation implementation.
+
+ When `converters` is given, it should be a dictionary where each key
+ represents the name of a type converter and each value is a callable
+ implementing the conversion from string to the desired datatype. Every
+ converter gets its corresponding get*() method on the parser object and
+ section proxies.
+
sections()
Return all the configuration section names, sans DEFAULT.
@@ -129,9 +149,11 @@ import warnings
__all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
"NoOptionError", "InterpolationError", "InterpolationDepthError",
- "InterpolationSyntaxError", "ParsingError",
- "MissingSectionHeaderError",
+ "InterpolationMissingOptionError", "InterpolationSyntaxError",
+ "ParsingError", "MissingSectionHeaderError",
"ConfigParser", "SafeConfigParser", "RawConfigParser",
+ "Interpolation", "BasicInterpolation", "ExtendedInterpolation",
+ "LegacyInterpolation", "SectionProxy", "ConverterMapping",
"DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
DEFAULTSECT = "DEFAULT"
@@ -410,7 +432,7 @@ class BasicInterpolation(Interpolation):
v = map[var]
except KeyError:
raise InterpolationMissingOptionError(
- option, section, rest, var)
+ option, section, rest, var) from None
if "%" in v:
self._interpolate_some(parser, option, accum, v,
section, map, depth + 1)
@@ -482,7 +504,7 @@ class ExtendedInterpolation(Interpolation):
"More than one ':' found: %r" % (rest,))
except (KeyError, NoSectionError, NoOptionError):
raise InterpolationMissingOptionError(
- option, section, rest, ":".join(path))
+ option, section, rest, ":".join(path)) from None
if "$" in v:
self._interpolate_some(parser, opt, accum, v, sect,
dict(parser.items(sect, raw=True)),
@@ -515,7 +537,7 @@ class LegacyInterpolation(Interpolation):
value = value % vars
except KeyError as e:
raise InterpolationMissingOptionError(
- option, section, rawval, e.args[0])
+ option, section, rawval, e.args[0]) from None
else:
break
if value and "%(" in value:
@@ -580,11 +602,12 @@ class RawConfigParser(MutableMapping):
comment_prefixes=('#', ';'), inline_comment_prefixes=None,
strict=True, empty_lines_in_values=True,
default_section=DEFAULTSECT,
- interpolation=_UNSET):
+ interpolation=_UNSET, converters=_UNSET):
self._dict = dict_type
self._sections = self._dict()
self._defaults = self._dict()
+ self._converters = ConverterMapping(self)
self._proxies = self._dict()
self._proxies[default_section] = SectionProxy(self, default_section)
if defaults:
@@ -612,6 +635,8 @@ class RawConfigParser(MutableMapping):
self._interpolation = self._DEFAULT_INTERPOLATION
if self._interpolation is None:
self._interpolation = Interpolation()
+ if converters is not _UNSET:
+ self._converters.update(converters)
def defaults(self):
return self._defaults
@@ -647,7 +672,7 @@ class RawConfigParser(MutableMapping):
try:
opts = self._sections[section].copy()
except KeyError:
- raise NoSectionError(section)
+ raise NoSectionError(section) from None
opts.update(self._defaults)
return list(opts.keys())
@@ -775,36 +800,31 @@ class RawConfigParser(MutableMapping):
def _get(self, section, conv, option, **kwargs):
return conv(self.get(section, option, **kwargs))
- def getint(self, section, option, *, raw=False, vars=None,
- fallback=_UNSET):
+ def _get_conv(self, section, option, conv, *, raw=False, vars=None,
+ fallback=_UNSET, **kwargs):
try:
- return self._get(section, int, option, raw=raw, vars=vars)
+ return self._get(section, conv, option, raw=raw, vars=vars,
+ **kwargs)
except (NoSectionError, NoOptionError):
if fallback is _UNSET:
raise
- else:
- return fallback
+ return fallback
+
+ # getint, getfloat and getboolean provided directly for backwards compat
+ def getint(self, section, option, *, raw=False, vars=None,
+ fallback=_UNSET, **kwargs):
+ return self._get_conv(section, option, int, raw=raw, vars=vars,
+ fallback=fallback, **kwargs)
def getfloat(self, section, option, *, raw=False, vars=None,
- fallback=_UNSET):
- try:
- return self._get(section, float, option, raw=raw, vars=vars)
- except (NoSectionError, NoOptionError):
- if fallback is _UNSET:
- raise
- else:
- return fallback
+ fallback=_UNSET, **kwargs):
+ return self._get_conv(section, option, float, raw=raw, vars=vars,
+ fallback=fallback, **kwargs)
def getboolean(self, section, option, *, raw=False, vars=None,
- fallback=_UNSET):
- try:
- return self._get(section, self._convert_to_boolean, option,
- raw=raw, vars=vars)
- except (NoSectionError, NoOptionError):
- if fallback is _UNSET:
- raise
- else:
- return fallback
+ fallback=_UNSET, **kwargs):
+ return self._get_conv(section, option, self._convert_to_boolean,
+ raw=raw, vars=vars, fallback=fallback, **kwargs)
def items(self, section=_UNSET, raw=False, vars=None):
"""Return a list of (name, value) tuples for each option in a section.
@@ -876,7 +896,7 @@ class RawConfigParser(MutableMapping):
try:
sectdict = self._sections[section]
except KeyError:
- raise NoSectionError(section)
+ raise NoSectionError(section) from None
sectdict[self.optionxform(option)] = value
def write(self, fp, space_around_delimiters=True):
@@ -917,7 +937,7 @@ class RawConfigParser(MutableMapping):
try:
sectdict = self._sections[section]
except KeyError:
- raise NoSectionError(section)
+ raise NoSectionError(section) from None
option = self.optionxform(option)
existed = option in sectdict
if existed:
@@ -1154,6 +1174,10 @@ class RawConfigParser(MutableMapping):
if not isinstance(value, str):
raise TypeError("option values must be strings")
+ @property
+ def converters(self):
+ return self._converters
+
class ConfigParser(RawConfigParser):
"""ConfigParser implementing interpolation."""
@@ -1194,6 +1218,10 @@ class SectionProxy(MutableMapping):
"""Creates a view on a section of the specified `name` in `parser`."""
self._parser = parser
self._name = name
+ for conv in parser.converters:
+ key = 'get' + conv
+ getter = functools.partial(self.get, _impl=getattr(parser, key))
+ setattr(self, key, getter)
def __repr__(self):
return '<Section: {}>'.format(self._name)
@@ -1227,22 +1255,6 @@ class SectionProxy(MutableMapping):
else:
return self._parser.defaults()
- def get(self, option, fallback=None, *, raw=False, vars=None):
- return self._parser.get(self._name, option, raw=raw, vars=vars,
- fallback=fallback)
-
- def getint(self, option, fallback=None, *, raw=False, vars=None):
- return self._parser.getint(self._name, option, raw=raw, vars=vars,
- fallback=fallback)
-
- def getfloat(self, option, fallback=None, *, raw=False, vars=None):
- return self._parser.getfloat(self._name, option, raw=raw, vars=vars,
- fallback=fallback)
-
- def getboolean(self, option, fallback=None, *, raw=False, vars=None):
- return self._parser.getboolean(self._name, option, raw=raw, vars=vars,
- fallback=fallback)
-
@property
def parser(self):
# The parser object of the proxy is read-only.
@@ -1252,3 +1264,77 @@ class SectionProxy(MutableMapping):
def name(self):
# The name of the section on a proxy is read-only.
return self._name
+
+ def get(self, option, fallback=None, *, raw=False, vars=None,
+ _impl=None, **kwargs):
+ """Get an option value.
+
+ Unless `fallback` is provided, `None` will be returned if the option
+ is not found.
+
+ """
+ # If `_impl` is provided, it should be a getter method on the parser
+ # object that provides the desired type conversion.
+ if not _impl:
+ _impl = self._parser.get
+ return _impl(self._name, option, raw=raw, vars=vars,
+ fallback=fallback, **kwargs)
+
+
+class ConverterMapping(MutableMapping):
+ """Enables reuse of get*() methods between the parser and section proxies.
+
+ If a parser class implements a getter directly, the value for the given
+ key will be ``None``. The presence of the converter name here enables
+ section proxies to find and use the implementation on the parser class.
+ """
+
+ GETTERCRE = re.compile(r"^get(?P<name>.+)$")
+
+ def __init__(self, parser):
+ self._parser = parser
+ self._data = {}
+ for getter in dir(self._parser):
+ m = self.GETTERCRE.match(getter)
+ if not m or not callable(getattr(self._parser, getter)):
+ continue
+ self._data[m.group('name')] = None # See class docstring.
+
+ def __getitem__(self, key):
+ return self._data[key]
+
+ def __setitem__(self, key, value):
+ try:
+ k = 'get' + key
+ except TypeError:
+ raise ValueError('Incompatible key: {} (type: {})'
+ ''.format(key, type(key)))
+ if k == 'get':
+ raise ValueError('Incompatible key: cannot use "" as a name')
+ self._data[key] = value
+ func = functools.partial(self._parser._get_conv, conv=value)
+ func.converter = value
+ setattr(self._parser, k, func)
+ for proxy in self._parser.values():
+ getter = functools.partial(proxy.get, _impl=func)
+ setattr(proxy, k, getter)
+
+ def __delitem__(self, key):
+ try:
+ k = 'get' + (key or None)
+ except TypeError:
+ raise KeyError(key)
+ del self._data[key]
+ for inst in itertools.chain((self._parser,), self._parser.values()):
+ try:
+ delattr(inst, k)
+ except AttributeError:
+ # don't raise since the entry was present in _data, silently
+ # clean up
+ continue
+
+ def __iter__(self):
+ return iter(self._data)
+
+ def __len__(self):
+ return len(self._data)
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index 82ee955..2fbc90c 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -5,7 +5,7 @@ from collections import deque
from functools import wraps
__all__ = ["contextmanager", "closing", "ContextDecorator", "ExitStack",
- "redirect_stdout", "suppress"]
+ "redirect_stdout", "redirect_stderr", "suppress"]
class ContextDecorator(object):
@@ -151,8 +151,27 @@ class closing(object):
def __exit__(self, *exc_info):
self.thing.close()
-class redirect_stdout:
- """Context manager for temporarily redirecting stdout to another file
+
+class _RedirectStream:
+
+ _stream = None
+
+ def __init__(self, new_target):
+ self._new_target = new_target
+ # We use a list of old targets to make this CM re-entrant
+ self._old_targets = []
+
+ def __enter__(self):
+ self._old_targets.append(getattr(sys, self._stream))
+ setattr(sys, self._stream, self._new_target)
+ return self._new_target
+
+ def __exit__(self, exctype, excinst, exctb):
+ setattr(sys, self._stream, self._old_targets.pop())
+
+
+class redirect_stdout(_RedirectStream):
+ """Context manager for temporarily redirecting stdout to another file.
# How to send help() to stderr
with redirect_stdout(sys.stderr):
@@ -164,18 +183,13 @@ class redirect_stdout:
help(pow)
"""
- def __init__(self, new_target):
- self._new_target = new_target
- # We use a list of old targets to make this CM re-entrant
- self._old_targets = []
+ _stream = "stdout"
- def __enter__(self):
- self._old_targets.append(sys.stdout)
- sys.stdout = self._new_target
- return self._new_target
- def __exit__(self, exctype, excinst, exctb):
- sys.stdout = self._old_targets.pop()
+class redirect_stderr(_RedirectStream):
+ """Context manager for temporarily redirecting stderr to another file."""
+
+ _stream = "stderr"
class suppress:
diff --git a/Lib/copy.py b/Lib/copy.py
index bb8840e..383609b 100644
--- a/Lib/copy.py
+++ b/Lib/copy.py
@@ -221,17 +221,15 @@ def _deepcopy_list(x, memo):
d[list] = _deepcopy_list
def _deepcopy_tuple(x, memo):
- y = []
- for a in x:
- y.append(deepcopy(a, memo))
+ 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.
try:
return memo[id(x)]
except KeyError:
pass
- for i in range(len(x)):
- if x[i] is not y[i]:
+ for k, j in zip(x, y):
+ if k is not j:
y = tuple(y)
break
else:
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py
index 5c803ff..055294c 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -353,7 +353,7 @@ class CDLL(object):
self._handle = handle
def __repr__(self):
- return "<%s '%s', handle %x at %x>" % \
+ return "<%s '%s', handle %x at %#x>" % \
(self.__class__.__name__, self._name,
(self._handle & (_sys.maxsize*2 + 1)),
id(self) & (_sys.maxsize*2 + 1))
diff --git a/Lib/ctypes/_endian.py b/Lib/ctypes/_endian.py
index dae65fc..37444bd 100644
--- a/Lib/ctypes/_endian.py
+++ b/Lib/ctypes/_endian.py
@@ -45,6 +45,7 @@ if sys.byteorder == "little":
class BigEndianStructure(Structure, metaclass=_swapped_meta):
"""Structure with big endian byte order"""
+ __slots__ = ()
_swappedbytes_ = None
elif sys.byteorder == "big":
@@ -53,6 +54,7 @@ elif sys.byteorder == "big":
BigEndianStructure = Structure
class LittleEndianStructure(Structure, metaclass=_swapped_meta):
"""Structure with little endian byte order"""
+ __slots__ = ()
_swappedbytes_ = None
else:
diff --git a/Lib/ctypes/test/test_byteswap.py b/Lib/ctypes/test/test_byteswap.py
index 427bb8b..01c97e8 100644
--- a/Lib/ctypes/test/test_byteswap.py
+++ b/Lib/ctypes/test/test_byteswap.py
@@ -22,6 +22,26 @@ class Test(unittest.TestCase):
setattr(bits, "i%s" % i, 1)
dump(bits)
+ def test_slots(self):
+ class BigPoint(BigEndianStructure):
+ __slots__ = ()
+ _fields_ = [("x", c_int), ("y", c_int)]
+
+ class LowPoint(LittleEndianStructure):
+ __slots__ = ()
+ _fields_ = [("x", c_int), ("y", c_int)]
+
+ big = BigPoint()
+ little = LowPoint()
+ big.x = 4
+ big.y = 2
+ little.x = 2
+ little.y = 4
+ with self.assertRaises(AttributeError):
+ big.z = 42
+ with self.assertRaises(AttributeError):
+ little.z = 24
+
def test_endian_short(self):
if sys.byteorder == "little":
self.assertIs(c_short.__ctype_le__, c_short)
diff --git a/Lib/ctypes/test/test_pointers.py b/Lib/ctypes/test/test_pointers.py
index 4cd3a7a..78e3357 100644
--- a/Lib/ctypes/test/test_pointers.py
+++ b/Lib/ctypes/test/test_pointers.py
@@ -24,7 +24,10 @@ class PointersTestCase(unittest.TestCase):
def test_pass_pointers(self):
dll = CDLL(_ctypes_test.__file__)
func = dll._testfunc_p_p
- func.restype = c_long
+ if sizeof(c_longlong) == sizeof(c_void_p):
+ func.restype = c_longlong
+ else:
+ func.restype = c_long
i = c_int(12345678)
## func.argtypes = (POINTER(c_int),)
diff --git a/Lib/ctypes/test/test_prototypes.py b/Lib/ctypes/test/test_prototypes.py
index 818c111..cd0c649 100644
--- a/Lib/ctypes/test/test_prototypes.py
+++ b/Lib/ctypes/test/test_prototypes.py
@@ -69,7 +69,10 @@ class CharPointersTestCase(unittest.TestCase):
def test_int_pointer_arg(self):
func = testdll._testfunc_p_p
- func.restype = c_long
+ if sizeof(c_longlong) == sizeof(c_void_p):
+ func.restype = c_longlong
+ else:
+ func.restype = c_long
self.assertEqual(0, func(0))
ci = c_int(0)
diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py
index 595113b..9c19899 100644
--- a/Lib/ctypes/util.py
+++ b/Lib/ctypes/util.py
@@ -19,6 +19,8 @@ if os.name == "nt":
i = i + len(prefix)
s, rest = sys.version[i:].split(" ", 1)
majorVersion = int(s[:-2]) - 6
+ if majorVersion >= 13:
+ majorVersion += 1
minorVersion = int(s[2:3]) / 10.0
# I don't think paths are affected by minor version in version 6
if majorVersion == 6:
@@ -36,8 +38,10 @@ if os.name == "nt":
return None
if version <= 6:
clibname = 'msvcrt'
- else:
+ elif version <= 13:
clibname = 'msvcr%d' % (version * 10)
+ else:
+ clibname = 'appcrt%d' % (version * 10)
# If python was built with in debug mode
import importlib.machinery
diff --git a/Lib/datetime.py b/Lib/datetime.py
index 1789714..4afe9a5 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -12,7 +12,7 @@ def _cmp(x, y):
MINYEAR = 1
MAXYEAR = 9999
-_MAXORDINAL = 3652059 # date.max.toordinal()
+_MAXORDINAL = 3652059 # date.max.toordinal()
# Utility functions, adapted from Python's Demo/classes/Dates.py, which
# also assumes the current Gregorian calendar indefinitely extended in
@@ -26,7 +26,7 @@ _MAXORDINAL = 3652059 # date.max.toordinal()
# -1 is a placeholder for indexing purposes.
_DAYS_IN_MONTH = [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
-_DAYS_BEFORE_MONTH = [-1] # -1 is a placeholder for indexing purposes.
+_DAYS_BEFORE_MONTH = [-1] # -1 is a placeholder for indexing purposes.
dbm = 0
for dim in _DAYS_IN_MONTH[1:]:
_DAYS_BEFORE_MONTH.append(dbm)
@@ -162,9 +162,9 @@ def _format_time(hh, mm, ss, us):
# Correctly substitute for %z and %Z escapes in strftime formats.
def _wrap_strftime(object, format, timetuple):
# Don't call utcoffset() or tzname() unless actually needed.
- freplace = None # the string to use for %f
- zreplace = None # the string to use for %z
- Zreplace = None # the string to use for %Z
+ freplace = None # the string to use for %f
+ zreplace = None # the string to use for %z
+ Zreplace = None # the string to use for %Z
# Scan format for %z and %Z escapes, replacing as needed.
newformat = []
@@ -217,11 +217,6 @@ def _wrap_strftime(object, format, timetuple):
newformat = "".join(newformat)
return _time.strftime(newformat, timetuple)
-def _call_tzinfo_method(tzinfo, methname, tzinfoarg):
- if tzinfo is None:
- return None
- return getattr(tzinfo, methname)(tzinfoarg)
-
# Just raise TypeError if the arg isn't None or a string.
def _check_tzname(name):
if name is not None and not isinstance(name, str):
@@ -245,13 +240,31 @@ def _check_utc_offset(name, offset):
raise ValueError("tzinfo.%s() must return a whole number "
"of minutes, got %s" % (name, offset))
if not -timedelta(1) < offset < timedelta(1):
- raise ValueError("%s()=%s, must be must be strictly between"
- " -timedelta(hours=24) and timedelta(hours=24)"
- % (name, offset))
+ raise ValueError("%s()=%s, must be must be strictly between "
+ "-timedelta(hours=24) and timedelta(hours=24)" %
+ (name, offset))
+
+def _check_int_field(value):
+ if isinstance(value, int):
+ return value
+ if not isinstance(value, float):
+ try:
+ value = value.__int__()
+ except AttributeError:
+ pass
+ else:
+ if isinstance(value, int):
+ return value
+ raise TypeError('__int__ returned non-int (type %s)' %
+ type(value).__name__)
+ raise TypeError('an integer is required (got type %s)' %
+ type(value).__name__)
+ raise TypeError('integer argument expected, got float')
def _check_date_fields(year, month, day):
- if not isinstance(year, int):
- raise TypeError('int expected')
+ year = _check_int_field(year)
+ month = _check_int_field(month)
+ day = _check_int_field(day)
if not MINYEAR <= year <= MAXYEAR:
raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR), year)
if not 1 <= month <= 12:
@@ -259,10 +272,13 @@ def _check_date_fields(year, month, day):
dim = _days_in_month(year, month)
if not 1 <= day <= dim:
raise ValueError('day must be in 1..%d' % dim, day)
+ return year, month, day
def _check_time_fields(hour, minute, second, microsecond):
- if not isinstance(hour, int):
- raise TypeError('int expected')
+ hour = _check_int_field(hour)
+ minute = _check_int_field(minute)
+ second = _check_int_field(second)
+ microsecond = _check_int_field(microsecond)
if not 0 <= hour <= 23:
raise ValueError('hour must be in 0..23', hour)
if not 0 <= minute <= 59:
@@ -271,6 +287,7 @@ 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
def _check_tzinfo_arg(tz):
if tz is not None and not isinstance(tz, tzinfo):
@@ -297,7 +314,7 @@ class timedelta:
Representation: (days, seconds, microseconds). Why? Because I
felt like it.
"""
- __slots__ = '_days', '_seconds', '_microseconds'
+ __slots__ = '_days', '_seconds', '_microseconds', '_hashcode'
def __new__(cls, days=0, seconds=0, microseconds=0,
milliseconds=0, minutes=0, hours=0, weeks=0):
@@ -363,38 +380,26 @@ class timedelta:
# secondsfrac isn't referenced again
if isinstance(microseconds, float):
- microseconds += usdouble
- microseconds = round(microseconds, 0)
- seconds, microseconds = divmod(microseconds, 1e6)
- assert microseconds == int(microseconds)
- assert seconds == int(seconds)
- days, seconds = divmod(seconds, 24.*3600.)
- assert days == int(days)
- assert seconds == int(seconds)
- d += int(days)
- s += int(seconds) # can't overflow
- assert isinstance(s, int)
- assert abs(s) <= 3 * 24 * 3600
+ microseconds = round(microseconds + usdouble)
+ seconds, microseconds = divmod(microseconds, 1000000)
+ days, seconds = divmod(seconds, 24*3600)
+ d += days
+ s += seconds
else:
+ microseconds = int(microseconds)
seconds, microseconds = divmod(microseconds, 1000000)
days, seconds = divmod(seconds, 24*3600)
d += days
- s += int(seconds) # can't overflow
- assert isinstance(s, int)
- assert abs(s) <= 3 * 24 * 3600
- microseconds = float(microseconds)
- microseconds += usdouble
- microseconds = round(microseconds, 0)
+ s += seconds
+ microseconds = round(microseconds + usdouble)
+ assert isinstance(s, int)
+ assert isinstance(microseconds, int)
assert abs(s) <= 3 * 24 * 3600
assert abs(microseconds) < 3.1e6
# Just a little bit of carrying possible for microseconds and seconds.
- assert isinstance(microseconds, float)
- assert int(microseconds) == microseconds
- us = int(microseconds)
- seconds, us = divmod(us, 1000000)
- s += seconds # cant't overflow
- assert isinstance(s, int)
+ seconds, us = divmod(microseconds, 1000000)
+ s += seconds
days, s = divmod(s, 24*3600)
d += days
@@ -402,27 +407,31 @@ class timedelta:
assert isinstance(s, int) and 0 <= s < 24*3600
assert isinstance(us, int) and 0 <= us < 1000000
- self = object.__new__(cls)
+ if abs(d) > 999999999:
+ raise OverflowError("timedelta # of days is too large: %d" % d)
+ self = object.__new__(cls)
self._days = d
self._seconds = s
self._microseconds = us
- if abs(d) > 999999999:
- raise OverflowError("timedelta # of days is too large: %d" % d)
-
+ self._hashcode = -1
return self
def __repr__(self):
if self._microseconds:
- return "%s(%d, %d, %d)" % ('datetime.' + self.__class__.__name__,
- self._days,
- self._seconds,
- self._microseconds)
+ return "%s.%s(%d, %d, %d)" % (self.__class__.__module__,
+ self.__class__.__qualname__,
+ self._days,
+ self._seconds,
+ self._microseconds)
if self._seconds:
- return "%s(%d, %d)" % ('datetime.' + self.__class__.__name__,
- self._days,
- self._seconds)
- return "%s(%d)" % ('datetime.' + self.__class__.__name__, self._days)
+ return "%s.%s(%d, %d)" % (self.__class__.__module__,
+ self.__class__.__qualname__,
+ self._days,
+ self._seconds)
+ return "%s.%s(%d)" % (self.__class__.__module__,
+ self.__class__.__qualname__,
+ self._days)
def __str__(self):
mm, ss = divmod(self._seconds, 60)
@@ -438,7 +447,7 @@ class timedelta:
def total_seconds(self):
"""Total seconds in the duration."""
- return ((self.days * 86400 + self.seconds)*10**6 +
+ return ((self.days * 86400 + self.seconds) * 10**6 +
self.microseconds) / 10**6
# Read-only field accessors
@@ -558,12 +567,6 @@ class timedelta:
else:
return False
- def __ne__(self, other):
- if isinstance(other, timedelta):
- return self._cmp(other) != 0
- else:
- return True
-
def __le__(self, other):
if isinstance(other, timedelta):
return self._cmp(other) <= 0
@@ -593,7 +596,9 @@ class timedelta:
return _cmp(self._getstate(), other._getstate())
def __hash__(self):
- return hash(self._getstate())
+ if self._hashcode == -1:
+ self._hashcode = hash(self._getstate())
+ return self._hashcode
def __bool__(self):
return (self._days != 0 or
@@ -641,7 +646,7 @@ class date:
Properties (readonly):
year, month, day
"""
- __slots__ = '_year', '_month', '_day'
+ __slots__ = '_year', '_month', '_day', '_hashcode'
def __new__(cls, year, month=None, day=None):
"""Constructor.
@@ -650,17 +655,19 @@ class date:
year, month, day (required, base 1)
"""
- if (isinstance(year, bytes) and len(year) == 4 and
- 1 <= year[2] <= 12 and month is None): # Month is sane
+ if month is None and isinstance(year, bytes) and len(year) == 4 and \
+ 1 <= year[2] <= 12:
# Pickle support
self = object.__new__(cls)
self.__setstate(year)
+ self._hashcode = -1
return self
- _check_date_fields(year, month, day)
+ year, month, day = _check_date_fields(year, month, day)
self = object.__new__(cls)
self._year = year
self._month = month
self._day = day
+ self._hashcode = -1
return self
# Additional constructors
@@ -700,10 +707,11 @@ class date:
>>> repr(dt)
'datetime.datetime(2010, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)'
"""
- return "%s(%d, %d, %d)" % ('datetime.' + self.__class__.__name__,
- self._year,
- self._month,
- self._day)
+ return "%s.%s(%d, %d, %d)" % (self.__class__.__module__,
+ self.__class__.__qualname__,
+ self._year,
+ self._month,
+ self._day)
# XXX These shouldn't depend on time.localtime(), because that
# clips the usable dates to [1970 .. 2038). At least ctime() is
# easily done without using strftime() -- that's better too because
@@ -723,6 +731,8 @@ class date:
return _wrap_strftime(self, fmt, self.timetuple())
def __format__(self, fmt):
+ if not isinstance(fmt, str):
+ raise TypeError("must be str, not %s" % type(fmt).__name__)
if len(fmt) != 0:
return self.strftime(fmt)
return str(self)
@@ -779,7 +789,6 @@ class date:
month = self._month
if day is None:
day = self._day
- _check_date_fields(year, month, day)
return date(year, month, day)
# Comparisons of date objects with other.
@@ -789,11 +798,6 @@ class date:
return self._cmp(other) == 0
return NotImplemented
- def __ne__(self, other):
- if isinstance(other, date):
- return self._cmp(other) != 0
- return NotImplemented
-
def __le__(self, other):
if isinstance(other, date):
return self._cmp(other) <= 0
@@ -822,7 +826,9 @@ class date:
def __hash__(self):
"Hash."
- return hash(self._getstate())
+ if self._hashcode == -1:
+ self._hashcode = hash(self._getstate())
+ return self._hashcode
# Computations
@@ -892,8 +898,6 @@ class date:
return bytes([yhi, ylo, self._month, self._day]),
def __setstate(self, string):
- if len(string) != 4 or not (1 <= string[2] <= 12):
- raise TypeError("not enough arguments")
yhi, ylo, self._month, self._day = string
self._year = yhi * 256 + ylo
@@ -912,6 +916,7 @@ class tzinfo:
Subclasses must override the name(), utcoffset() and dst() methods.
"""
__slots__ = ()
+
def tzname(self, dt):
"datetime -> string name of time zone."
raise NotImplementedError("tzinfo subclass must override tzname()")
@@ -998,6 +1003,7 @@ class time:
Properties (readonly):
hour, minute, second, microsecond, tzinfo
"""
+ __slots__ = '_hour', '_minute', '_second', '_microsecond', '_tzinfo', '_hashcode'
def __new__(cls, hour=0, minute=0, second=0, microsecond=0, tzinfo=None):
"""Constructor.
@@ -1008,18 +1014,22 @@ class time:
second, microsecond (default to zero)
tzinfo (default to None)
"""
- self = object.__new__(cls)
- if isinstance(hour, bytes) and len(hour) == 6:
+ if isinstance(hour, bytes) and len(hour) == 6 and hour[0] < 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)
_check_tzinfo_arg(tzinfo)
- _check_time_fields(hour, minute, second, microsecond)
+ self = object.__new__(cls)
self._hour = hour
self._minute = minute
self._second = second
self._microsecond = microsecond
self._tzinfo = tzinfo
+ self._hashcode = -1
return self
# Read-only field accessors
@@ -1058,12 +1068,6 @@ class time:
else:
return False
- def __ne__(self, other):
- if isinstance(other, time):
- return self._cmp(other, allow_mixed=True) != 0
- else:
- return True
-
def __le__(self, other):
if isinstance(other, time):
return self._cmp(other) <= 0
@@ -1104,8 +1108,8 @@ class time:
if base_compare:
return _cmp((self._hour, self._minute, self._second,
self._microsecond),
- (other._hour, other._minute, other._second,
- other._microsecond))
+ (other._hour, other._minute, other._second,
+ other._microsecond))
if myoff is None or otoff is None:
if allow_mixed:
return 2 # arbitrary non-zero value
@@ -1118,16 +1122,20 @@ class time:
def __hash__(self):
"""Hash."""
- tzoff = self.utcoffset()
- if not tzoff: # zero or None
- return hash(self._getstate()[0])
- h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff,
- timedelta(hours=1))
- assert not m % timedelta(minutes=1), "whole minute"
- m //= timedelta(minutes=1)
- if 0 <= h < 24:
- return hash(time(h, m, self.second, self.microsecond))
- return hash((h, m, self.second, self.microsecond))
+ if self._hashcode == -1:
+ tzoff = self.utcoffset()
+ if not tzoff: # zero or None
+ self._hashcode = hash(self._getstate()[0])
+ else:
+ h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff,
+ timedelta(hours=1))
+ assert not m % timedelta(minutes=1), "whole minute"
+ m //= timedelta(minutes=1)
+ if 0 <= h < 24:
+ self._hashcode = hash(time(h, m, self.second, self.microsecond))
+ else:
+ self._hashcode = hash((h, m, self.second, self.microsecond))
+ return self._hashcode
# Conversion to string
@@ -1155,8 +1163,9 @@ class time:
s = ", %d" % self._second
else:
s = ""
- s= "%s(%d, %d%s)" % ('datetime.' + self.__class__.__name__,
- self._hour, self._minute, s)
+ s= "%s.%s(%d, %d%s)" % (self.__class__.__module__,
+ self.__class__.__qualname__,
+ self._hour, self._minute, s)
if self._tzinfo is not None:
assert s[-1:] == ")"
s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")"
@@ -1189,6 +1198,8 @@ class time:
return _wrap_strftime(self, fmt, timetuple)
def __format__(self, fmt):
+ if not isinstance(fmt, str):
+ raise TypeError("must be str, not %s" % type(fmt).__name__)
if len(fmt) != 0:
return self.strftime(fmt)
return str(self)
@@ -1245,16 +1256,8 @@ class time:
microsecond = self.microsecond
if tzinfo is True:
tzinfo = self.tzinfo
- _check_time_fields(hour, minute, second, microsecond)
- _check_tzinfo_arg(tzinfo)
return time(hour, minute, second, microsecond, tzinfo)
- def __bool__(self):
- if self.second or self.microsecond:
- return True
- offset = self.utcoffset() or timedelta(0)
- return timedelta(hours=self.hour, minutes=self.minute) != offset
-
# Pickle support.
def _getstate(self):
@@ -1268,15 +1271,11 @@ class time:
return (basestate, self._tzinfo)
def __setstate(self, string, tzinfo):
- if len(string) != 6 or string[0] >= 24:
- raise TypeError("an integer is required")
- (self._hour, self._minute, self._second,
- us1, us2, us3) = string
+ 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
self._microsecond = (((us1 << 8) | us2) << 8) | us3
- if tzinfo is None or isinstance(tzinfo, _tzinfo_class):
- self._tzinfo = tzinfo
- else:
- raise TypeError("bad tzinfo state arg %r" % tzinfo)
+ self._tzinfo = tzinfo
def __reduce__(self):
return (time, self._getstate())
@@ -1293,25 +1292,30 @@ class datetime(date):
The year, month and day arguments are required. tzinfo may be None, or an
instance of a tzinfo subclass. The remaining arguments may be ints.
"""
+ __slots__ = date.__slots__ + time.__slots__
- __slots__ = date.__slots__ + (
- '_hour', '_minute', '_second',
- '_microsecond', '_tzinfo')
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:
+ if isinstance(year, bytes) and len(year) == 10 and 1 <= year[2] <= 12:
# Pickle support
- self = date.__new__(cls, year[:4])
+ 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)
_check_tzinfo_arg(tzinfo)
- _check_time_fields(hour, minute, second, microsecond)
- self = date.__new__(cls, year, month, day)
+ self = object.__new__(cls)
+ self._year = year
+ self._month = month
+ self._day = day
self._hour = hour
self._minute = minute
self._second = second
self._microsecond = microsecond
self._tzinfo = tzinfo
+ self._hashcode = -1
return self
# Read-only field accessors
@@ -1346,7 +1350,6 @@ class datetime(date):
A timezone info object may be passed in as well.
"""
-
_check_tzinfo_arg(tz)
converter = _time.localtime if tz is None else _time.gmtime
@@ -1385,11 +1388,6 @@ class datetime(date):
ss = min(ss, 59) # clamp out leap seconds if the platform has them
return cls(y, m, d, hh, mm, ss, us)
- # XXX This is supposed to do better than we *can* do by using time.time(),
- # XXX if the platform supports a more accurate way. The C implementation
- # XXX uses gettimeofday on platforms that have it, but that isn't
- # XXX available from Python. So now() may return different results
- # XXX across the implementations.
@classmethod
def now(cls, tz=None):
"Construct a datetime from time.time() and optional time zone info."
@@ -1476,11 +1474,8 @@ class datetime(date):
microsecond = self.microsecond
if tzinfo is True:
tzinfo = self.tzinfo
- _check_date_fields(year, month, day)
- _check_time_fields(hour, minute, second, microsecond)
- _check_tzinfo_arg(tzinfo)
- return datetime(year, month, day, hour, minute, second,
- microsecond, tzinfo)
+ return datetime(year, month, day, hour, minute, second, microsecond,
+ tzinfo)
def astimezone(self, tz=None):
if tz is None:
@@ -1550,10 +1545,9 @@ class datetime(date):
Optional argument sep specifies the separator between date and
time, default 'T'.
"""
- s = ("%04d-%02d-%02d%c" % (self._year, self._month, self._day,
- sep) +
- _format_time(self._hour, self._minute, self._second,
- self._microsecond))
+ s = ("%04d-%02d-%02d%c" % (self._year, self._month, self._day, sep) +
+ _format_time(self._hour, self._minute, self._second,
+ self._microsecond))
off = self.utcoffset()
if off is not None:
if off.days < 0:
@@ -1569,14 +1563,15 @@ class datetime(date):
def __repr__(self):
"""Convert to formal string, for repr()."""
- L = [self._year, self._month, self._day, # These are never zero
+ L = [self._year, self._month, self._day, # These are never zero
self._hour, self._minute, self._second, self._microsecond]
if L[-1] == 0:
del L[-1]
if L[-1] == 0:
del L[-1]
- s = ", ".join(map(str, L))
- s = "%s(%s)" % ('datetime.' + self.__class__.__name__, s)
+ s = "%s.%s(%s)" % (self.__class__.__module__,
+ self.__class__.__qualname__,
+ ", ".join(map(str, L)))
if self._tzinfo is not None:
assert s[-1:] == ")"
s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")"
@@ -1608,7 +1603,9 @@ class datetime(date):
it mean anything in particular. For example, "GMT", "UTC", "-500",
"-5:00", "EDT", "US/Eastern", "America/New York" are all valid replies.
"""
- name = _call_tzinfo_method(self._tzinfo, "tzname", self)
+ if self._tzinfo is None:
+ return None
+ name = self._tzinfo.tzname(self)
_check_tzname(name)
return name
@@ -1637,14 +1634,6 @@ class datetime(date):
else:
return False
- def __ne__(self, other):
- if isinstance(other, datetime):
- return self._cmp(other, allow_mixed=True) != 0
- elif not isinstance(other, date):
- return NotImplemented
- else:
- return True
-
def __le__(self, other):
if isinstance(other, datetime):
return self._cmp(other) <= 0
@@ -1694,9 +1683,9 @@ class datetime(date):
return _cmp((self._year, self._month, self._day,
self._hour, self._minute, self._second,
self._microsecond),
- (other._year, other._month, other._day,
- other._hour, other._minute, other._second,
- other._microsecond))
+ (other._year, other._month, other._day,
+ other._hour, other._minute, other._second,
+ other._microsecond))
if myoff is None or otoff is None:
if allow_mixed:
return 2 # arbitrary non-zero value
@@ -1754,12 +1743,15 @@ class datetime(date):
return base + otoff - myoff
def __hash__(self):
- tzoff = self.utcoffset()
- if tzoff is None:
- return hash(self._getstate()[0])
- days = _ymd2ord(self.year, self.month, self.day)
- seconds = self.hour * 3600 + self.minute * 60 + self.second
- return hash(timedelta(days, seconds, self.microsecond) - tzoff)
+ if self._hashcode == -1:
+ tzoff = self.utcoffset()
+ if tzoff is None:
+ self._hashcode = hash(self._getstate()[0])
+ else:
+ days = _ymd2ord(self.year, self.month, self.day)
+ seconds = self.hour * 3600 + self.minute * 60 + self.second
+ self._hashcode = hash(timedelta(days, seconds, self.microsecond) - tzoff)
+ return self._hashcode
# Pickle support.
@@ -1776,14 +1768,13 @@ class datetime(date):
return (basestate, self._tzinfo)
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,
self._minute, self._second, us1, us2, us3) = string
self._year = yhi * 256 + ylo
self._microsecond = (((us1 << 8) | us2) << 8) | us3
- if tzinfo is None or isinstance(tzinfo, _tzinfo_class):
- self._tzinfo = tzinfo
- else:
- raise TypeError("bad tzinfo state arg %r" % tzinfo)
+ self._tzinfo = tzinfo
def __reduce__(self):
return (self.__class__, self._getstate())
@@ -1799,7 +1790,7 @@ def _isoweek1monday(year):
# XXX This could be done more efficiently
THURSDAY = 3
firstday = _ymd2ord(year, 1, 1)
- firstweekday = (firstday + 6) % 7 # See weekday() above
+ firstweekday = (firstday + 6) % 7 # See weekday() above
week1monday = firstday - firstweekday
if firstweekday > THURSDAY:
week1monday += 7
@@ -1820,13 +1811,12 @@ class timezone(tzinfo):
elif not isinstance(name, str):
raise TypeError("name must be a string")
if not cls._minoffset <= offset <= cls._maxoffset:
- raise ValueError("offset must be a timedelta"
- " strictly between -timedelta(hours=24) and"
- " timedelta(hours=24).")
- if (offset.microseconds != 0 or
- offset.seconds % 60 != 0):
- raise ValueError("offset must be a timedelta"
- " representing a whole number of minutes")
+ raise ValueError("offset must be a timedelta "
+ "strictly between -timedelta(hours=24) and "
+ "timedelta(hours=24).")
+ if (offset.microseconds != 0 or offset.seconds % 60 != 0):
+ raise ValueError("offset must be a timedelta "
+ "representing a whole number of minutes")
return cls._create(offset, name)
@classmethod
@@ -1863,10 +1853,12 @@ class timezone(tzinfo):
if self is self.utc:
return 'datetime.timezone.utc'
if self._name is None:
- return "%s(%r)" % ('datetime.' + self.__class__.__name__,
- self._offset)
- return "%s(%r, %r)" % ('datetime.' + self.__class__.__name__,
- self._offset, self._name)
+ return "%s.%s(%r)" % (self.__class__.__module__,
+ self.__class__.__qualname__,
+ self._offset)
+ return "%s.%s(%r, %r)" % (self.__class__.__module__,
+ self.__class__.__qualname__,
+ self._offset, self._name)
def __str__(self):
return self.tzname(None)
@@ -2121,14 +2113,13 @@ except ImportError:
pass
else:
# Clean up unused names
- del (_DAYNAMES, _DAYS_BEFORE_MONTH, _DAYS_IN_MONTH,
- _DI100Y, _DI400Y, _DI4Y, _MAXORDINAL, _MONTHNAMES,
- _build_struct_time, _call_tzinfo_method, _check_date_fields,
- _check_time_fields, _check_tzinfo_arg, _check_tzname,
- _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month,
- _days_before_year, _days_in_month, _format_time, _is_leap,
- _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class,
- _wrap_strftime, _ymd2ord)
+ del (_DAYNAMES, _DAYS_BEFORE_MONTH, _DAYS_IN_MONTH, _DI100Y, _DI400Y,
+ _DI4Y, _EPOCH, _MAXORDINAL, _MONTHNAMES, _build_struct_time,
+ _check_date_fields, _check_int_field, _check_time_fields,
+ _check_tzinfo_arg, _check_tzname, _check_utc_offset, _cmp, _cmperror,
+ _date_class, _days_before_month, _days_before_year, _days_in_month,
+ _format_time, _is_leap, _isoweek1monday, _math, _ord2ymd,
+ _time, _time_class, _tzinfo_class, _wrap_strftime, _ymd2ord)
# XXX Since import * above excludes names that start with _,
# docstring does not get overwritten. In the future, it may be
# appropriate to maintain a single module level docstring and
diff --git a/Lib/dbm/dumb.py b/Lib/dbm/dumb.py
index a9ead68..3424096 100644
--- a/Lib/dbm/dumb.py
+++ b/Lib/dbm/dumb.py
@@ -45,7 +45,7 @@ class _Database(collections.MutableMapping):
_os = _os # for _commit()
_io = _io # for _commit()
- def __init__(self, filebasename, mode):
+ def __init__(self, filebasename, mode, flag='c'):
self._mode = mode
# The directory file is a text file. Each line looks like
@@ -65,6 +65,17 @@ class _Database(collections.MutableMapping):
# The index is an in-memory dict, mirroring the directory file.
self._index = None # maps keys to (pos, siz) pairs
+ # Handle the creation
+ self._create(flag)
+ self._update()
+
+ def _create(self, flag):
+ if flag == 'n':
+ for filename in (self._datfile, self._bakfile, self._dirfile):
+ try:
+ _os.remove(filename)
+ except OSError:
+ pass
# Mod by Jack: create data file if needed
try:
f = _io.open(self._datfile, 'r', encoding="Latin-1")
@@ -73,7 +84,6 @@ class _Database(collections.MutableMapping):
self._chmod(self._datfile)
else:
f.close()
- self._update()
# Read directory file into the in-memory index dict.
def _update(self):
@@ -264,20 +274,20 @@ class _Database(collections.MutableMapping):
self.close()
-def open(file, flag=None, mode=0o666):
+def open(file, flag='c', mode=0o666):
"""Open the database file, filename, and return corresponding object.
The flag argument, used to control how the database is opened in the
- other DBM implementations, is ignored in the dbm.dumb module; the
- database is always opened for update, and will be created if it does
- not exist.
+ other DBM implementations, supports only the semantics of 'c' and 'n'
+ values. Other values will default to the semantics of 'c' value:
+ the database will always opened for update and will be created if it
+ does not exist.
The optional mode argument is the UNIX mode of the file, used only when
the database has to be created. It defaults to octal code 0o666 (and
will be modified by the prevailing umask).
"""
- # flag argument is currently ignored
# Modify mode depending on the umask
try:
@@ -288,5 +298,4 @@ def open(file, flag=None, mode=0o666):
else:
# Turn off any bits that are set in the umask
mode = mode & (~um)
-
- return _Database(file, mode)
+ return _Database(file, mode, flag=flag)
diff --git a/Lib/decimal.py b/Lib/decimal.py
index b254f9c..7746ea2 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -1,6414 +1,11 @@
-# Copyright (c) 2004 Python Software Foundation.
-# All rights reserved.
-
-# Written by Eric Price <eprice at tjhsst.edu>
-# and Facundo Batista <facundo at taniquetil.com.ar>
-# and Raymond Hettinger <python at rcn.com>
-# and Aahz <aahz at pobox.com>
-# and Tim Peters
-
-# This module should be kept in sync with the latest updates of the
-# IBM specification as it evolves. Those updates will be treated
-# as bug fixes (deviation from the spec is a compatibility, usability
-# bug) and will be backported. At this point the spec is stabilizing
-# and the updates are becoming fewer, smaller, and less significant.
-
-"""
-This is an implementation of decimal floating point arithmetic based on
-the General Decimal Arithmetic Specification:
-
- http://speleotrove.com/decimal/decarith.html
-
-and IEEE standard 854-1987:
-
- http://en.wikipedia.org/wiki/IEEE_854-1987
-
-Decimal floating point has finite precision with arbitrarily large bounds.
-
-The purpose of this module is to support arithmetic using familiar
-"schoolhouse" rules and to avoid some of the tricky representation
-issues associated with binary floating point. The package is especially
-useful for financial applications or for contexts where users have
-expectations that are at odds with binary floating point (for instance,
-in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
-of 0.0; Decimal('1.00') % Decimal('0.1') returns the expected
-Decimal('0.00')).
-
-Here are some examples of using the decimal module:
-
->>> from decimal import *
->>> setcontext(ExtendedContext)
->>> Decimal(0)
-Decimal('0')
->>> Decimal('1')
-Decimal('1')
->>> Decimal('-.0123')
-Decimal('-0.0123')
->>> Decimal(123456)
-Decimal('123456')
->>> Decimal('123.45e12345678')
-Decimal('1.2345E+12345680')
->>> Decimal('1.33') + Decimal('1.27')
-Decimal('2.60')
->>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41')
-Decimal('-2.20')
->>> dig = Decimal(1)
->>> print(dig / Decimal(3))
-0.333333333
->>> getcontext().prec = 18
->>> print(dig / Decimal(3))
-0.333333333333333333
->>> print(dig.sqrt())
-1
->>> print(Decimal(3).sqrt())
-1.73205080756887729
->>> print(Decimal(3) ** 123)
-4.85192780976896427E+58
->>> inf = Decimal(1) / Decimal(0)
->>> print(inf)
-Infinity
->>> neginf = Decimal(-1) / Decimal(0)
->>> print(neginf)
--Infinity
->>> print(neginf + inf)
-NaN
->>> print(neginf * inf)
--Infinity
->>> print(dig / 0)
-Infinity
->>> getcontext().traps[DivisionByZero] = 1
->>> print(dig / 0)
-Traceback (most recent call last):
- ...
- ...
- ...
-decimal.DivisionByZero: x / 0
->>> c = Context()
->>> c.traps[InvalidOperation] = 0
->>> print(c.flags[InvalidOperation])
-0
->>> c.divide(Decimal(0), Decimal(0))
-Decimal('NaN')
->>> c.traps[InvalidOperation] = 1
->>> print(c.flags[InvalidOperation])
-1
->>> c.flags[InvalidOperation] = 0
->>> print(c.flags[InvalidOperation])
-0
->>> print(c.divide(Decimal(0), Decimal(0)))
-Traceback (most recent call last):
- ...
- ...
- ...
-decimal.InvalidOperation: 0 / 0
->>> print(c.flags[InvalidOperation])
-1
->>> c.flags[InvalidOperation] = 0
->>> c.traps[InvalidOperation] = 0
->>> print(c.divide(Decimal(0), Decimal(0)))
-NaN
->>> print(c.flags[InvalidOperation])
-1
->>>
-"""
-
-__all__ = [
- # Two major classes
- 'Decimal', 'Context',
-
- # Named tuple representation
- 'DecimalTuple',
-
- # Contexts
- 'DefaultContext', 'BasicContext', 'ExtendedContext',
-
- # Exceptions
- 'DecimalException', 'Clamped', 'InvalidOperation', 'DivisionByZero',
- 'Inexact', 'Rounded', 'Subnormal', 'Overflow', 'Underflow',
- 'FloatOperation',
-
- # Exceptional conditions that trigger InvalidOperation
- 'DivisionImpossible', 'InvalidContext', 'ConversionSyntax', 'DivisionUndefined',
-
- # Constants for use in setting up contexts
- 'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING',
- 'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN', 'ROUND_05UP',
-
- # Functions for manipulating contexts
- 'setcontext', 'getcontext', 'localcontext',
-
- # Limits for the C version for compatibility
- 'MAX_PREC', 'MAX_EMAX', 'MIN_EMIN', 'MIN_ETINY',
-
- # C version: compile time choice that enables the thread local context
- 'HAVE_THREADS'
-]
-
-__version__ = '1.70' # Highest version of the spec this complies with
- # See http://speleotrove.com/decimal/
-__libmpdec_version__ = "2.4.1" # compatible libmpdec version
-
-import math as _math
-import numbers as _numbers
-import sys
-
-try:
- from collections import namedtuple as _namedtuple
- DecimalTuple = _namedtuple('DecimalTuple', 'sign digits exponent')
-except ImportError:
- DecimalTuple = lambda *args: args
-
-# Rounding
-ROUND_DOWN = 'ROUND_DOWN'
-ROUND_HALF_UP = 'ROUND_HALF_UP'
-ROUND_HALF_EVEN = 'ROUND_HALF_EVEN'
-ROUND_CEILING = 'ROUND_CEILING'
-ROUND_FLOOR = 'ROUND_FLOOR'
-ROUND_UP = 'ROUND_UP'
-ROUND_HALF_DOWN = 'ROUND_HALF_DOWN'
-ROUND_05UP = 'ROUND_05UP'
-
-# Compatibility with the C version
-HAVE_THREADS = True
-if sys.maxsize == 2**63-1:
- MAX_PREC = 999999999999999999
- MAX_EMAX = 999999999999999999
- MIN_EMIN = -999999999999999999
-else:
- MAX_PREC = 425000000
- MAX_EMAX = 425000000
- MIN_EMIN = -425000000
-
-MIN_ETINY = MIN_EMIN - (MAX_PREC-1)
-
-# Errors
-
-class DecimalException(ArithmeticError):
- """Base exception class.
-
- Used exceptions derive from this.
- If an exception derives from another exception besides this (such as
- Underflow (Inexact, Rounded, Subnormal) that indicates that it is only
- called if the others are present. This isn't actually used for
- anything, though.
-
- handle -- Called when context._raise_error is called and the
- trap_enabler is not set. First argument is self, second is the
- context. More arguments can be given, those being after
- the explanation in _raise_error (For example,
- context._raise_error(NewError, '(-x)!', self._sign) would
- call NewError().handle(context, self._sign).)
-
- To define a new exception, it should be sufficient to have it derive
- from DecimalException.
- """
- def handle(self, context, *args):
- pass
-
-
-class Clamped(DecimalException):
- """Exponent of a 0 changed to fit bounds.
-
- This occurs and signals clamped if the exponent of a result has been
- altered in order to fit the constraints of a specific concrete
- representation. This may occur when the exponent of a zero result would
- be outside the bounds of a representation, or when a large normal
- number would have an encoded exponent that cannot be represented. In
- this latter case, the exponent is reduced to fit and the corresponding
- number of zero digits are appended to the coefficient ("fold-down").
- """
-
-class InvalidOperation(DecimalException):
- """An invalid operation was performed.
-
- Various bad things cause this:
-
- Something creates a signaling NaN
- -INF + INF
- 0 * (+-)INF
- (+-)INF / (+-)INF
- x % 0
- (+-)INF % x
- x._rescale( non-integer )
- sqrt(-x) , x > 0
- 0 ** 0
- x ** (non-integer)
- x ** (+-)INF
- An operand is invalid
-
- The result of the operation after these is a quiet positive NaN,
- except when the cause is a signaling NaN, in which case the result is
- also a quiet NaN, but with the original sign, and an optional
- diagnostic information.
- """
- def handle(self, context, *args):
- if args:
- ans = _dec_from_triple(args[0]._sign, args[0]._int, 'n', True)
- return ans._fix_nan(context)
- return _NaN
-
-class ConversionSyntax(InvalidOperation):
- """Trying to convert badly formed string.
-
- This occurs and signals invalid-operation if an string is being
- converted to a number and it does not conform to the numeric string
- syntax. The result is [0,qNaN].
- """
- def handle(self, context, *args):
- return _NaN
-
-class DivisionByZero(DecimalException, ZeroDivisionError):
- """Division by 0.
-
- This occurs and signals division-by-zero if division of a finite number
- by zero was attempted (during a divide-integer or divide operation, or a
- power operation with negative right-hand operand), and the dividend was
- not zero.
-
- The result of the operation is [sign,inf], where sign is the exclusive
- or of the signs of the operands for divide, or is 1 for an odd power of
- -0, for power.
- """
-
- def handle(self, context, sign, *args):
- return _SignedInfinity[sign]
-
-class DivisionImpossible(InvalidOperation):
- """Cannot perform the division adequately.
-
- This occurs and signals invalid-operation if the integer result of a
- divide-integer or remainder operation had too many digits (would be
- longer than precision). The result is [0,qNaN].
- """
-
- def handle(self, context, *args):
- return _NaN
-
-class DivisionUndefined(InvalidOperation, ZeroDivisionError):
- """Undefined result of division.
-
- This occurs and signals invalid-operation if division by zero was
- attempted (during a divide-integer, divide, or remainder operation), and
- the dividend is also zero. The result is [0,qNaN].
- """
-
- def handle(self, context, *args):
- return _NaN
-
-class Inexact(DecimalException):
- """Had to round, losing information.
-
- This occurs and signals inexact whenever the result of an operation is
- not exact (that is, it needed to be rounded and any discarded digits
- were non-zero), or if an overflow or underflow condition occurs. The
- result in all cases is unchanged.
-
- The inexact signal may be tested (or trapped) to determine if a given
- operation (or sequence of operations) was inexact.
- """
-
-class InvalidContext(InvalidOperation):
- """Invalid context. Unknown rounding, for example.
-
- This occurs and signals invalid-operation if an invalid context was
- detected during an operation. This can occur if contexts are not checked
- on creation and either the precision exceeds the capability of the
- underlying concrete representation or an unknown or unsupported rounding
- was specified. These aspects of the context need only be checked when
- the values are required to be used. The result is [0,qNaN].
- """
-
- def handle(self, context, *args):
- return _NaN
-
-class Rounded(DecimalException):
- """Number got rounded (not necessarily changed during rounding).
-
- This occurs and signals rounded whenever the result of an operation is
- rounded (that is, some zero or non-zero digits were discarded from the
- coefficient), or if an overflow or underflow condition occurs. The
- result in all cases is unchanged.
-
- The rounded signal may be tested (or trapped) to determine if a given
- operation (or sequence of operations) caused a loss of precision.
- """
-
-class Subnormal(DecimalException):
- """Exponent < Emin before rounding.
-
- This occurs and signals subnormal whenever the result of a conversion or
- operation is subnormal (that is, its adjusted exponent is less than
- Emin, before any rounding). The result in all cases is unchanged.
-
- The subnormal signal may be tested (or trapped) to determine if a given
- or operation (or sequence of operations) yielded a subnormal result.
- """
-
-class Overflow(Inexact, Rounded):
- """Numerical overflow.
-
- This occurs and signals overflow if the adjusted exponent of a result
- (from a conversion or from an operation that is not an attempt to divide
- by zero), after rounding, would be greater than the largest value that
- can be handled by the implementation (the value Emax).
-
- The result depends on the rounding mode:
-
- For round-half-up and round-half-even (and for round-half-down and
- round-up, if implemented), the result of the operation is [sign,inf],
- where sign is the sign of the intermediate result. For round-down, the
- result is the largest finite number that can be represented in the
- current precision, with the sign of the intermediate result. For
- round-ceiling, the result is the same as for round-down if the sign of
- the intermediate result is 1, or is [0,inf] otherwise. For round-floor,
- the result is the same as for round-down if the sign of the intermediate
- result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded
- will also be raised.
- """
-
- def handle(self, context, sign, *args):
- if context.rounding in (ROUND_HALF_UP, ROUND_HALF_EVEN,
- ROUND_HALF_DOWN, ROUND_UP):
- return _SignedInfinity[sign]
- if sign == 0:
- if context.rounding == ROUND_CEILING:
- return _SignedInfinity[sign]
- return _dec_from_triple(sign, '9'*context.prec,
- context.Emax-context.prec+1)
- if sign == 1:
- if context.rounding == ROUND_FLOOR:
- return _SignedInfinity[sign]
- return _dec_from_triple(sign, '9'*context.prec,
- context.Emax-context.prec+1)
-
-
-class Underflow(Inexact, Rounded, Subnormal):
- """Numerical underflow with result rounded to 0.
-
- This occurs and signals underflow if a result is inexact and the
- adjusted exponent of the result would be smaller (more negative) than
- the smallest value that can be handled by the implementation (the value
- Emin). That is, the result is both inexact and subnormal.
-
- The result after an underflow will be a subnormal number rounded, if
- necessary, so that its exponent is not less than Etiny. This may result
- in 0 with the sign of the intermediate result and an exponent of Etiny.
-
- In all cases, Inexact, Rounded, and Subnormal will also be raised.
- """
-
-class FloatOperation(DecimalException, TypeError):
- """Enable stricter semantics for mixing floats and Decimals.
-
- If the signal is not trapped (default), mixing floats and Decimals is
- permitted in the Decimal() constructor, context.create_decimal() and
- all comparison operators. Both conversion and comparisons are exact.
- Any occurrence of a mixed operation is silently recorded by setting
- FloatOperation in the context flags. Explicit conversions with
- Decimal.from_float() or context.create_decimal_from_float() do not
- set the flag.
-
- Otherwise (the signal is trapped), only equality comparisons and explicit
- conversions are silent. All other mixed operations raise FloatOperation.
- """
-
-# List of public traps and flags
-_signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded,
- Underflow, InvalidOperation, Subnormal, FloatOperation]
-
-# Map conditions (per the spec) to signals
-_condition_map = {ConversionSyntax:InvalidOperation,
- DivisionImpossible:InvalidOperation,
- DivisionUndefined:InvalidOperation,
- InvalidContext:InvalidOperation}
-
-# Valid rounding modes
-_rounding_modes = (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_CEILING,
- ROUND_FLOOR, ROUND_UP, ROUND_HALF_DOWN, ROUND_05UP)
-
-##### Context Functions ##################################################
-
-# The getcontext() and setcontext() function manage access to a thread-local
-# current context. Py2.4 offers direct support for thread locals. If that
-# is not available, use threading.current_thread() which is slower but will
-# work for older Pythons. If threads are not part of the build, create a
-# mock threading object with threading.local() returning the module namespace.
-
-try:
- import threading
-except ImportError:
- # Python was compiled without threads; create a mock object instead
- class MockThreading(object):
- def local(self, sys=sys):
- return sys.modules[__name__]
- threading = MockThreading()
- del MockThreading
-
-try:
- threading.local
-
-except AttributeError:
-
- # To fix reloading, force it to create a new context
- # Old contexts have different exceptions in their dicts, making problems.
- if hasattr(threading.current_thread(), '__decimal_context__'):
- del threading.current_thread().__decimal_context__
-
- def setcontext(context):
- """Set this thread's context to context."""
- if context in (DefaultContext, BasicContext, ExtendedContext):
- context = context.copy()
- context.clear_flags()
- threading.current_thread().__decimal_context__ = context
-
- def getcontext():
- """Returns this thread's context.
-
- If this thread does not yet have a context, returns
- a new context and sets this thread's context.
- New contexts are copies of DefaultContext.
- """
- try:
- return threading.current_thread().__decimal_context__
- except AttributeError:
- context = Context()
- threading.current_thread().__decimal_context__ = context
- return context
-
-else:
-
- local = threading.local()
- if hasattr(local, '__decimal_context__'):
- del local.__decimal_context__
-
- def getcontext(_local=local):
- """Returns this thread's context.
-
- If this thread does not yet have a context, returns
- a new context and sets this thread's context.
- New contexts are copies of DefaultContext.
- """
- try:
- return _local.__decimal_context__
- except AttributeError:
- context = Context()
- _local.__decimal_context__ = context
- return context
-
- def setcontext(context, _local=local):
- """Set this thread's context to context."""
- if context in (DefaultContext, BasicContext, ExtendedContext):
- context = context.copy()
- context.clear_flags()
- _local.__decimal_context__ = context
-
- del threading, local # Don't contaminate the namespace
-
-def localcontext(ctx=None):
- """Return a context manager for a copy of the supplied context
-
- Uses a copy of the current context if no context is specified
- The returned context manager creates a local decimal context
- in a with statement:
- def sin(x):
- with localcontext() as ctx:
- ctx.prec += 2
- # Rest of sin calculation algorithm
- # uses a precision 2 greater than normal
- return +s # Convert result to normal precision
-
- def sin(x):
- with localcontext(ExtendedContext):
- # Rest of sin calculation algorithm
- # uses the Extended Context from the
- # General Decimal Arithmetic Specification
- return +s # Convert result to normal context
-
- >>> setcontext(DefaultContext)
- >>> print(getcontext().prec)
- 28
- >>> with localcontext():
- ... ctx = getcontext()
- ... ctx.prec += 2
- ... print(ctx.prec)
- ...
- 30
- >>> with localcontext(ExtendedContext):
- ... print(getcontext().prec)
- ...
- 9
- >>> print(getcontext().prec)
- 28
- """
- if ctx is None: ctx = getcontext()
- return _ContextManager(ctx)
-
-
-##### Decimal class #######################################################
-
-# Do not subclass Decimal from numbers.Real and do not register it as such
-# (because Decimals are not interoperable with floats). See the notes in
-# numbers.py for more detail.
-
-class Decimal(object):
- """Floating point class for decimal arithmetic."""
-
- __slots__ = ('_exp','_int','_sign', '_is_special')
- # Generally, the value of the Decimal instance is given by
- # (-1)**_sign * _int * 10**_exp
- # Special values are signified by _is_special == True
-
- # We're immutable, so use __new__ not __init__
- def __new__(cls, value="0", context=None):
- """Create a decimal point instance.
-
- >>> Decimal('3.14') # string input
- Decimal('3.14')
- >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
- Decimal('3.14')
- >>> Decimal(314) # int
- Decimal('314')
- >>> Decimal(Decimal(314)) # another decimal instance
- Decimal('314')
- >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
- Decimal('3.14')
- """
-
- # Note that the coefficient, self._int, is actually stored as
- # a string rather than as a tuple of digits. This speeds up
- # the "digits to integer" and "integer to digits" conversions
- # that are used in almost every arithmetic operation on
- # Decimals. This is an internal detail: the as_tuple function
- # and the Decimal constructor still deal with tuples of
- # digits.
-
- self = object.__new__(cls)
-
- # From a string
- # REs insist on real strings, so we can too.
- if isinstance(value, str):
- m = _parser(value.strip())
- if m is None:
- if context is None:
- context = getcontext()
- return context._raise_error(ConversionSyntax,
- "Invalid literal for Decimal: %r" % value)
-
- if m.group('sign') == "-":
- self._sign = 1
- else:
- self._sign = 0
- intpart = m.group('int')
- if intpart is not None:
- # finite number
- fracpart = m.group('frac') or ''
- exp = int(m.group('exp') or '0')
- self._int = str(int(intpart+fracpart))
- self._exp = exp - len(fracpart)
- self._is_special = False
- else:
- diag = m.group('diag')
- if diag is not None:
- # NaN
- self._int = str(int(diag or '0')).lstrip('0')
- if m.group('signal'):
- self._exp = 'N'
- else:
- self._exp = 'n'
- else:
- # infinity
- self._int = '0'
- self._exp = 'F'
- self._is_special = True
- return self
-
- # From an integer
- if isinstance(value, int):
- if value >= 0:
- self._sign = 0
- else:
- self._sign = 1
- self._exp = 0
- self._int = str(abs(value))
- self._is_special = False
- return self
-
- # From another decimal
- if isinstance(value, Decimal):
- self._exp = value._exp
- self._sign = value._sign
- self._int = value._int
- self._is_special = value._is_special
- return self
-
- # From an internal working value
- if isinstance(value, _WorkRep):
- self._sign = value.sign
- self._int = str(value.int)
- self._exp = int(value.exp)
- self._is_special = False
- return self
-
- # tuple/list conversion (possibly from as_tuple())
- if isinstance(value, (list,tuple)):
- if len(value) != 3:
- raise ValueError('Invalid tuple size in creation of Decimal '
- 'from list or tuple. The list or tuple '
- 'should have exactly three elements.')
- # process sign. The isinstance test rejects floats
- if not (isinstance(value[0], int) and value[0] in (0,1)):
- raise ValueError("Invalid sign. The first value in the tuple "
- "should be an integer; either 0 for a "
- "positive number or 1 for a negative number.")
- self._sign = value[0]
- if value[2] == 'F':
- # infinity: value[1] is ignored
- self._int = '0'
- self._exp = value[2]
- self._is_special = True
- else:
- # process and validate the digits in value[1]
- digits = []
- for digit in value[1]:
- if isinstance(digit, int) and 0 <= digit <= 9:
- # skip leading zeros
- if digits or digit != 0:
- digits.append(digit)
- else:
- raise ValueError("The second value in the tuple must "
- "be composed of integers in the range "
- "0 through 9.")
- if value[2] in ('n', 'N'):
- # NaN: digits form the diagnostic
- self._int = ''.join(map(str, digits))
- self._exp = value[2]
- self._is_special = True
- elif isinstance(value[2], int):
- # finite number: digits give the coefficient
- self._int = ''.join(map(str, digits or [0]))
- self._exp = value[2]
- self._is_special = False
- else:
- raise ValueError("The third value in the tuple must "
- "be an integer, or one of the "
- "strings 'F', 'n', 'N'.")
- return self
-
- if isinstance(value, float):
- if context is None:
- context = getcontext()
- context._raise_error(FloatOperation,
- "strict semantics for mixing floats and Decimals are "
- "enabled")
- value = Decimal.from_float(value)
- self._exp = value._exp
- self._sign = value._sign
- self._int = value._int
- self._is_special = value._is_special
- return self
-
- raise TypeError("Cannot convert %r to Decimal" % value)
-
- @classmethod
- def from_float(cls, f):
- """Converts a float to a decimal number, exactly.
-
- Note that Decimal.from_float(0.1) is not the same as Decimal('0.1').
- Since 0.1 is not exactly representable in binary floating point, the
- value is stored as the nearest representable value which is
- 0x1.999999999999ap-4. The exact equivalent of the value in decimal
- is 0.1000000000000000055511151231257827021181583404541015625.
-
- >>> Decimal.from_float(0.1)
- Decimal('0.1000000000000000055511151231257827021181583404541015625')
- >>> Decimal.from_float(float('nan'))
- Decimal('NaN')
- >>> Decimal.from_float(float('inf'))
- Decimal('Infinity')
- >>> Decimal.from_float(-float('inf'))
- Decimal('-Infinity')
- >>> Decimal.from_float(-0.0)
- Decimal('-0')
-
- """
- if isinstance(f, int): # handle integer inputs
- return cls(f)
- if not isinstance(f, float):
- raise TypeError("argument must be int or float.")
- if _math.isinf(f) or _math.isnan(f):
- return cls(repr(f))
- if _math.copysign(1.0, f) == 1.0:
- sign = 0
- else:
- sign = 1
- n, d = abs(f).as_integer_ratio()
- k = d.bit_length() - 1
- result = _dec_from_triple(sign, str(n*5**k), -k)
- if cls is Decimal:
- return result
- else:
- return cls(result)
-
- def _isnan(self):
- """Returns whether the number is not actually one.
-
- 0 if a number
- 1 if NaN
- 2 if sNaN
- """
- if self._is_special:
- exp = self._exp
- if exp == 'n':
- return 1
- elif exp == 'N':
- return 2
- return 0
-
- def _isinfinity(self):
- """Returns whether the number is infinite
-
- 0 if finite or not a number
- 1 if +INF
- -1 if -INF
- """
- if self._exp == 'F':
- if self._sign:
- return -1
- return 1
- return 0
-
- def _check_nans(self, other=None, context=None):
- """Returns whether the number is not actually one.
-
- if self, other are sNaN, signal
- if self, other are NaN return nan
- return 0
-
- Done before operations.
- """
-
- self_is_nan = self._isnan()
- if other is None:
- other_is_nan = False
- else:
- other_is_nan = other._isnan()
-
- if self_is_nan or other_is_nan:
- if context is None:
- context = getcontext()
-
- if self_is_nan == 2:
- return context._raise_error(InvalidOperation, 'sNaN',
- self)
- if other_is_nan == 2:
- return context._raise_error(InvalidOperation, 'sNaN',
- other)
- if self_is_nan:
- return self._fix_nan(context)
-
- return other._fix_nan(context)
- return 0
-
- def _compare_check_nans(self, other, context):
- """Version of _check_nans used for the signaling comparisons
- compare_signal, __le__, __lt__, __ge__, __gt__.
-
- Signal InvalidOperation if either self or other is a (quiet
- or signaling) NaN. Signaling NaNs take precedence over quiet
- NaNs.
-
- Return 0 if neither operand is a NaN.
-
- """
- if context is None:
- context = getcontext()
-
- if self._is_special or other._is_special:
- if self.is_snan():
- return context._raise_error(InvalidOperation,
- 'comparison involving sNaN',
- self)
- elif other.is_snan():
- return context._raise_error(InvalidOperation,
- 'comparison involving sNaN',
- other)
- elif self.is_qnan():
- return context._raise_error(InvalidOperation,
- 'comparison involving NaN',
- self)
- elif other.is_qnan():
- return context._raise_error(InvalidOperation,
- 'comparison involving NaN',
- other)
- return 0
-
- def __bool__(self):
- """Return True if self is nonzero; otherwise return False.
-
- NaNs and infinities are considered nonzero.
- """
- return self._is_special or self._int != '0'
-
- def _cmp(self, other):
- """Compare the two non-NaN decimal instances self and other.
-
- Returns -1 if self < other, 0 if self == other and 1
- if self > other. This routine is for internal use only."""
-
- if self._is_special or other._is_special:
- self_inf = self._isinfinity()
- other_inf = other._isinfinity()
- if self_inf == other_inf:
- return 0
- elif self_inf < other_inf:
- return -1
- else:
- return 1
-
- # check for zeros; Decimal('0') == Decimal('-0')
- if not self:
- if not other:
- return 0
- else:
- return -((-1)**other._sign)
- if not other:
- return (-1)**self._sign
-
- # If different signs, neg one is less
- if other._sign < self._sign:
- return -1
- if self._sign < other._sign:
- return 1
-
- self_adjusted = self.adjusted()
- other_adjusted = other.adjusted()
- if self_adjusted == other_adjusted:
- self_padded = self._int + '0'*(self._exp - other._exp)
- other_padded = other._int + '0'*(other._exp - self._exp)
- if self_padded == other_padded:
- return 0
- elif self_padded < other_padded:
- return -(-1)**self._sign
- else:
- return (-1)**self._sign
- elif self_adjusted > other_adjusted:
- return (-1)**self._sign
- else: # self_adjusted < other_adjusted
- return -((-1)**self._sign)
-
- # Note: The Decimal standard doesn't cover rich comparisons for
- # Decimals. In particular, the specification is silent on the
- # subject of what should happen for a comparison involving a NaN.
- # We take the following approach:
- #
- # == comparisons involving a quiet NaN always return False
- # != comparisons involving a quiet NaN always return True
- # == or != comparisons involving a signaling NaN signal
- # InvalidOperation, and return False or True as above if the
- # InvalidOperation is not trapped.
- # <, >, <= and >= comparisons involving a (quiet or signaling)
- # NaN signal InvalidOperation, and return False if the
- # InvalidOperation is not trapped.
- #
- # This behavior is designed to conform as closely as possible to
- # that specified by IEEE 754.
-
- def __eq__(self, other, context=None):
- self, other = _convert_for_comparison(self, other, equality_op=True)
- if other is NotImplemented:
- return other
- if self._check_nans(other, context):
- return False
- return self._cmp(other) == 0
-
- def __ne__(self, other, context=None):
- self, other = _convert_for_comparison(self, other, equality_op=True)
- if other is NotImplemented:
- return other
- if self._check_nans(other, context):
- return True
- return self._cmp(other) != 0
-
-
- def __lt__(self, other, context=None):
- self, other = _convert_for_comparison(self, other)
- if other is NotImplemented:
- return other
- ans = self._compare_check_nans(other, context)
- if ans:
- return False
- return self._cmp(other) < 0
-
- def __le__(self, other, context=None):
- self, other = _convert_for_comparison(self, other)
- if other is NotImplemented:
- return other
- ans = self._compare_check_nans(other, context)
- if ans:
- return False
- return self._cmp(other) <= 0
-
- def __gt__(self, other, context=None):
- self, other = _convert_for_comparison(self, other)
- if other is NotImplemented:
- return other
- ans = self._compare_check_nans(other, context)
- if ans:
- return False
- return self._cmp(other) > 0
-
- def __ge__(self, other, context=None):
- self, other = _convert_for_comparison(self, other)
- if other is NotImplemented:
- return other
- ans = self._compare_check_nans(other, context)
- if ans:
- return False
- return self._cmp(other) >= 0
-
- def compare(self, other, context=None):
- """Compares one to another.
-
- -1 => a < b
- 0 => a = b
- 1 => a > b
- NaN => one is NaN
- Like __cmp__, but returns Decimal instances.
- """
- other = _convert_other(other, raiseit=True)
-
- # Compare(NaN, NaN) = NaN
- if (self._is_special or other and other._is_special):
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- return Decimal(self._cmp(other))
-
- def __hash__(self):
- """x.__hash__() <==> hash(x)"""
-
- # In order to make sure that the hash of a Decimal instance
- # agrees with the hash of a numerically equal integer, float
- # or Fraction, we follow the rules for numeric hashes outlined
- # in the documentation. (See library docs, 'Built-in Types').
- if self._is_special:
- if self.is_snan():
- raise TypeError('Cannot hash a signaling NaN value.')
- elif self.is_nan():
- return _PyHASH_NAN
- else:
- if self._sign:
- return -_PyHASH_INF
- else:
- return _PyHASH_INF
-
- if self._exp >= 0:
- exp_hash = pow(10, self._exp, _PyHASH_MODULUS)
- else:
- exp_hash = pow(_PyHASH_10INV, -self._exp, _PyHASH_MODULUS)
- hash_ = int(self._int) * exp_hash % _PyHASH_MODULUS
- ans = hash_ if self >= 0 else -hash_
- return -2 if ans == -1 else ans
-
- def as_tuple(self):
- """Represents the number as a triple tuple.
-
- To show the internals exactly as they are.
- """
- return DecimalTuple(self._sign, tuple(map(int, self._int)), self._exp)
-
- def __repr__(self):
- """Represents the number as an instance of Decimal."""
- # Invariant: eval(repr(d)) == d
- return "Decimal('%s')" % str(self)
-
- def __str__(self, eng=False, context=None):
- """Return string representation of the number in scientific notation.
-
- Captures all of the information in the underlying representation.
- """
-
- sign = ['', '-'][self._sign]
- if self._is_special:
- if self._exp == 'F':
- return sign + 'Infinity'
- elif self._exp == 'n':
- return sign + 'NaN' + self._int
- else: # self._exp == 'N'
- return sign + 'sNaN' + self._int
-
- # number of digits of self._int to left of decimal point
- leftdigits = self._exp + len(self._int)
-
- # dotplace is number of digits of self._int to the left of the
- # decimal point in the mantissa of the output string (that is,
- # after adjusting the exponent)
- if self._exp <= 0 and leftdigits > -6:
- # no exponent required
- dotplace = leftdigits
- elif not eng:
- # usual scientific notation: 1 digit on left of the point
- dotplace = 1
- elif self._int == '0':
- # engineering notation, zero
- dotplace = (leftdigits + 1) % 3 - 1
- else:
- # engineering notation, nonzero
- dotplace = (leftdigits - 1) % 3 + 1
-
- if dotplace <= 0:
- intpart = '0'
- fracpart = '.' + '0'*(-dotplace) + self._int
- elif dotplace >= len(self._int):
- intpart = self._int+'0'*(dotplace-len(self._int))
- fracpart = ''
- else:
- intpart = self._int[:dotplace]
- fracpart = '.' + self._int[dotplace:]
- if leftdigits == dotplace:
- exp = ''
- else:
- if context is None:
- context = getcontext()
- exp = ['e', 'E'][context.capitals] + "%+d" % (leftdigits-dotplace)
-
- return sign + intpart + fracpart + exp
-
- def to_eng_string(self, context=None):
- """Convert to engineering-type string.
-
- Engineering notation has an exponent which is a multiple of 3, so there
- are up to 3 digits left of the decimal place.
-
- Same rules for when in exponential and when as a value as in __str__.
- """
- return self.__str__(eng=True, context=context)
-
- def __neg__(self, context=None):
- """Returns a copy with the sign switched.
-
- Rounds, if it has reason.
- """
- if self._is_special:
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- if context is None:
- context = getcontext()
-
- if not self and context.rounding != ROUND_FLOOR:
- # -Decimal('0') is Decimal('0'), not Decimal('-0'), except
- # in ROUND_FLOOR rounding mode.
- ans = self.copy_abs()
- else:
- ans = self.copy_negate()
-
- return ans._fix(context)
-
- def __pos__(self, context=None):
- """Returns a copy, unless it is a sNaN.
-
- Rounds the number (if more then precision digits)
- """
- if self._is_special:
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- if context is None:
- context = getcontext()
-
- if not self and context.rounding != ROUND_FLOOR:
- # + (-0) = 0, except in ROUND_FLOOR rounding mode.
- ans = self.copy_abs()
- else:
- ans = Decimal(self)
-
- return ans._fix(context)
-
- def __abs__(self, round=True, context=None):
- """Returns the absolute value of self.
-
- If the keyword argument 'round' is false, do not round. The
- expression self.__abs__(round=False) is equivalent to
- self.copy_abs().
- """
- if not round:
- return self.copy_abs()
-
- if self._is_special:
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- if self._sign:
- ans = self.__neg__(context=context)
- else:
- ans = self.__pos__(context=context)
-
- return ans
-
- def __add__(self, other, context=None):
- """Returns self + other.
-
- -INF + INF (or the reverse) cause InvalidOperation errors.
- """
- other = _convert_other(other)
- if other is NotImplemented:
- return other
-
- if context is None:
- context = getcontext()
-
- if self._is_special or other._is_special:
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- if self._isinfinity():
- # If both INF, same sign => same as both, opposite => error.
- if self._sign != other._sign and other._isinfinity():
- return context._raise_error(InvalidOperation, '-INF + INF')
- return Decimal(self)
- if other._isinfinity():
- return Decimal(other) # Can't both be infinity here
-
- exp = min(self._exp, other._exp)
- negativezero = 0
- if context.rounding == ROUND_FLOOR and self._sign != other._sign:
- # If the answer is 0, the sign should be negative, in this case.
- negativezero = 1
-
- if not self and not other:
- sign = min(self._sign, other._sign)
- if negativezero:
- sign = 1
- ans = _dec_from_triple(sign, '0', exp)
- ans = ans._fix(context)
- return ans
- if not self:
- exp = max(exp, other._exp - context.prec-1)
- ans = other._rescale(exp, context.rounding)
- ans = ans._fix(context)
- return ans
- if not other:
- exp = max(exp, self._exp - context.prec-1)
- ans = self._rescale(exp, context.rounding)
- ans = ans._fix(context)
- return ans
-
- op1 = _WorkRep(self)
- op2 = _WorkRep(other)
- op1, op2 = _normalize(op1, op2, context.prec)
-
- result = _WorkRep()
- if op1.sign != op2.sign:
- # Equal and opposite
- if op1.int == op2.int:
- ans = _dec_from_triple(negativezero, '0', exp)
- ans = ans._fix(context)
- return ans
- if op1.int < op2.int:
- op1, op2 = op2, op1
- # OK, now abs(op1) > abs(op2)
- if op1.sign == 1:
- result.sign = 1
- op1.sign, op2.sign = op2.sign, op1.sign
- else:
- result.sign = 0
- # So we know the sign, and op1 > 0.
- elif op1.sign == 1:
- result.sign = 1
- op1.sign, op2.sign = (0, 0)
- else:
- result.sign = 0
- # Now, op1 > abs(op2) > 0
-
- if op2.sign == 0:
- result.int = op1.int + op2.int
- else:
- result.int = op1.int - op2.int
-
- result.exp = op1.exp
- ans = Decimal(result)
- ans = ans._fix(context)
- return ans
-
- __radd__ = __add__
-
- def __sub__(self, other, context=None):
- """Return self - other"""
- other = _convert_other(other)
- if other is NotImplemented:
- return other
-
- if self._is_special or other._is_special:
- ans = self._check_nans(other, context=context)
- if ans:
- return ans
-
- # self - other is computed as self + other.copy_negate()
- return self.__add__(other.copy_negate(), context=context)
-
- def __rsub__(self, other, context=None):
- """Return other - self"""
- other = _convert_other(other)
- if other is NotImplemented:
- return other
-
- return other.__sub__(self, context=context)
-
- def __mul__(self, other, context=None):
- """Return self * other.
-
- (+-) INF * 0 (or its reverse) raise InvalidOperation.
- """
- other = _convert_other(other)
- if other is NotImplemented:
- return other
-
- if context is None:
- context = getcontext()
-
- resultsign = self._sign ^ other._sign
-
- if self._is_special or other._is_special:
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- if self._isinfinity():
- if not other:
- return context._raise_error(InvalidOperation, '(+-)INF * 0')
- return _SignedInfinity[resultsign]
-
- if other._isinfinity():
- if not self:
- return context._raise_error(InvalidOperation, '0 * (+-)INF')
- return _SignedInfinity[resultsign]
-
- resultexp = self._exp + other._exp
-
- # Special case for multiplying by zero
- if not self or not other:
- ans = _dec_from_triple(resultsign, '0', resultexp)
- # Fixing in case the exponent is out of bounds
- ans = ans._fix(context)
- return ans
-
- # Special case for multiplying by power of 10
- if self._int == '1':
- ans = _dec_from_triple(resultsign, other._int, resultexp)
- ans = ans._fix(context)
- return ans
- if other._int == '1':
- ans = _dec_from_triple(resultsign, self._int, resultexp)
- ans = ans._fix(context)
- return ans
-
- op1 = _WorkRep(self)
- op2 = _WorkRep(other)
-
- ans = _dec_from_triple(resultsign, str(op1.int * op2.int), resultexp)
- ans = ans._fix(context)
-
- return ans
- __rmul__ = __mul__
-
- def __truediv__(self, other, context=None):
- """Return self / other."""
- other = _convert_other(other)
- if other is NotImplemented:
- return NotImplemented
-
- if context is None:
- context = getcontext()
-
- sign = self._sign ^ other._sign
-
- if self._is_special or other._is_special:
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- if self._isinfinity() and other._isinfinity():
- return context._raise_error(InvalidOperation, '(+-)INF/(+-)INF')
-
- if self._isinfinity():
- return _SignedInfinity[sign]
-
- if other._isinfinity():
- context._raise_error(Clamped, 'Division by infinity')
- return _dec_from_triple(sign, '0', context.Etiny())
-
- # Special cases for zeroes
- if not other:
- if not self:
- return context._raise_error(DivisionUndefined, '0 / 0')
- return context._raise_error(DivisionByZero, 'x / 0', sign)
-
- if not self:
- exp = self._exp - other._exp
- coeff = 0
- else:
- # OK, so neither = 0, INF or NaN
- shift = len(other._int) - len(self._int) + context.prec + 1
- exp = self._exp - other._exp - shift
- op1 = _WorkRep(self)
- op2 = _WorkRep(other)
- if shift >= 0:
- coeff, remainder = divmod(op1.int * 10**shift, op2.int)
- else:
- coeff, remainder = divmod(op1.int, op2.int * 10**-shift)
- if remainder:
- # result is not exact; adjust to ensure correct rounding
- if coeff % 5 == 0:
- coeff += 1
- else:
- # result is exact; get as close to ideal exponent as possible
- ideal_exp = self._exp - other._exp
- while exp < ideal_exp and coeff % 10 == 0:
- coeff //= 10
- exp += 1
-
- ans = _dec_from_triple(sign, str(coeff), exp)
- return ans._fix(context)
-
- def _divide(self, other, context):
- """Return (self // other, self % other), to context.prec precision.
-
- Assumes that neither self nor other is a NaN, that self is not
- infinite and that other is nonzero.
- """
- sign = self._sign ^ other._sign
- if other._isinfinity():
- ideal_exp = self._exp
- else:
- ideal_exp = min(self._exp, other._exp)
-
- expdiff = self.adjusted() - other.adjusted()
- if not self or other._isinfinity() or expdiff <= -2:
- return (_dec_from_triple(sign, '0', 0),
- self._rescale(ideal_exp, context.rounding))
- if expdiff <= context.prec:
- op1 = _WorkRep(self)
- op2 = _WorkRep(other)
- if op1.exp >= op2.exp:
- op1.int *= 10**(op1.exp - op2.exp)
- else:
- op2.int *= 10**(op2.exp - op1.exp)
- q, r = divmod(op1.int, op2.int)
- if q < 10**context.prec:
- return (_dec_from_triple(sign, str(q), 0),
- _dec_from_triple(self._sign, str(r), ideal_exp))
-
- # Here the quotient is too large to be representable
- ans = context._raise_error(DivisionImpossible,
- 'quotient too large in //, % or divmod')
- return ans, ans
-
- def __rtruediv__(self, other, context=None):
- """Swaps self/other and returns __truediv__."""
- other = _convert_other(other)
- if other is NotImplemented:
- return other
- return other.__truediv__(self, context=context)
-
- def __divmod__(self, other, context=None):
- """
- Return (self // other, self % other)
- """
- other = _convert_other(other)
- if other is NotImplemented:
- return other
-
- if context is None:
- context = getcontext()
-
- ans = self._check_nans(other, context)
- if ans:
- return (ans, ans)
-
- sign = self._sign ^ other._sign
- if self._isinfinity():
- if other._isinfinity():
- ans = context._raise_error(InvalidOperation, 'divmod(INF, INF)')
- return ans, ans
- else:
- return (_SignedInfinity[sign],
- context._raise_error(InvalidOperation, 'INF % x'))
-
- if not other:
- if not self:
- ans = context._raise_error(DivisionUndefined, 'divmod(0, 0)')
- return ans, ans
- else:
- return (context._raise_error(DivisionByZero, 'x // 0', sign),
- context._raise_error(InvalidOperation, 'x % 0'))
-
- quotient, remainder = self._divide(other, context)
- remainder = remainder._fix(context)
- return quotient, remainder
-
- def __rdivmod__(self, other, context=None):
- """Swaps self/other and returns __divmod__."""
- other = _convert_other(other)
- if other is NotImplemented:
- return other
- return other.__divmod__(self, context=context)
-
- def __mod__(self, other, context=None):
- """
- self % other
- """
- other = _convert_other(other)
- if other is NotImplemented:
- return other
-
- if context is None:
- context = getcontext()
-
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- if self._isinfinity():
- return context._raise_error(InvalidOperation, 'INF % x')
- elif not other:
- if self:
- return context._raise_error(InvalidOperation, 'x % 0')
- else:
- return context._raise_error(DivisionUndefined, '0 % 0')
-
- remainder = self._divide(other, context)[1]
- remainder = remainder._fix(context)
- return remainder
-
- def __rmod__(self, other, context=None):
- """Swaps self/other and returns __mod__."""
- other = _convert_other(other)
- if other is NotImplemented:
- return other
- return other.__mod__(self, context=context)
-
- def remainder_near(self, other, context=None):
- """
- Remainder nearest to 0- abs(remainder-near) <= other/2
- """
- if context is None:
- context = getcontext()
-
- other = _convert_other(other, raiseit=True)
-
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- # self == +/-infinity -> InvalidOperation
- if self._isinfinity():
- return context._raise_error(InvalidOperation,
- 'remainder_near(infinity, x)')
-
- # other == 0 -> either InvalidOperation or DivisionUndefined
- if not other:
- if self:
- return context._raise_error(InvalidOperation,
- 'remainder_near(x, 0)')
- else:
- return context._raise_error(DivisionUndefined,
- 'remainder_near(0, 0)')
-
- # other = +/-infinity -> remainder = self
- if other._isinfinity():
- ans = Decimal(self)
- return ans._fix(context)
-
- # self = 0 -> remainder = self, with ideal exponent
- ideal_exponent = min(self._exp, other._exp)
- if not self:
- ans = _dec_from_triple(self._sign, '0', ideal_exponent)
- return ans._fix(context)
-
- # catch most cases of large or small quotient
- expdiff = self.adjusted() - other.adjusted()
- if expdiff >= context.prec + 1:
- # expdiff >= prec+1 => abs(self/other) > 10**prec
- return context._raise_error(DivisionImpossible)
- if expdiff <= -2:
- # expdiff <= -2 => abs(self/other) < 0.1
- ans = self._rescale(ideal_exponent, context.rounding)
- return ans._fix(context)
-
- # adjust both arguments to have the same exponent, then divide
- op1 = _WorkRep(self)
- op2 = _WorkRep(other)
- if op1.exp >= op2.exp:
- op1.int *= 10**(op1.exp - op2.exp)
- else:
- op2.int *= 10**(op2.exp - op1.exp)
- q, r = divmod(op1.int, op2.int)
- # remainder is r*10**ideal_exponent; other is +/-op2.int *
- # 10**ideal_exponent. Apply correction to ensure that
- # abs(remainder) <= abs(other)/2
- if 2*r + (q&1) > op2.int:
- r -= op2.int
- q += 1
-
- if q >= 10**context.prec:
- return context._raise_error(DivisionImpossible)
-
- # result has same sign as self unless r is negative
- sign = self._sign
- if r < 0:
- sign = 1-sign
- r = -r
-
- ans = _dec_from_triple(sign, str(r), ideal_exponent)
- return ans._fix(context)
-
- def __floordiv__(self, other, context=None):
- """self // other"""
- other = _convert_other(other)
- if other is NotImplemented:
- return other
-
- if context is None:
- context = getcontext()
-
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- if self._isinfinity():
- if other._isinfinity():
- return context._raise_error(InvalidOperation, 'INF // INF')
- else:
- return _SignedInfinity[self._sign ^ other._sign]
-
- if not other:
- if self:
- return context._raise_error(DivisionByZero, 'x // 0',
- self._sign ^ other._sign)
- else:
- return context._raise_error(DivisionUndefined, '0 // 0')
-
- return self._divide(other, context)[0]
-
- def __rfloordiv__(self, other, context=None):
- """Swaps self/other and returns __floordiv__."""
- other = _convert_other(other)
- if other is NotImplemented:
- return other
- return other.__floordiv__(self, context=context)
-
- def __float__(self):
- """Float representation."""
- if self._isnan():
- if self.is_snan():
- raise ValueError("Cannot convert signaling NaN to float")
- s = "-nan" if self._sign else "nan"
- else:
- s = str(self)
- return float(s)
-
- def __int__(self):
- """Converts self to an int, truncating if necessary."""
- if self._is_special:
- if self._isnan():
- raise ValueError("Cannot convert NaN to integer")
- elif self._isinfinity():
- raise OverflowError("Cannot convert infinity to integer")
- s = (-1)**self._sign
- if self._exp >= 0:
- return s*int(self._int)*10**self._exp
- else:
- return s*int(self._int[:self._exp] or '0')
-
- __trunc__ = __int__
-
- def real(self):
- return self
- real = property(real)
-
- def imag(self):
- return Decimal(0)
- imag = property(imag)
-
- def conjugate(self):
- return self
-
- def __complex__(self):
- return complex(float(self))
-
- def _fix_nan(self, context):
- """Decapitate the payload of a NaN to fit the context"""
- payload = self._int
-
- # maximum length of payload is precision if clamp=0,
- # precision-1 if clamp=1.
- max_payload_len = context.prec - context.clamp
- if len(payload) > max_payload_len:
- payload = payload[len(payload)-max_payload_len:].lstrip('0')
- return _dec_from_triple(self._sign, payload, self._exp, True)
- return Decimal(self)
-
- def _fix(self, context):
- """Round if it is necessary to keep self within prec precision.
-
- Rounds and fixes the exponent. Does not raise on a sNaN.
-
- Arguments:
- self - Decimal instance
- context - context used.
- """
-
- if self._is_special:
- if self._isnan():
- # decapitate payload if necessary
- return self._fix_nan(context)
- else:
- # self is +/-Infinity; return unaltered
- return Decimal(self)
-
- # if self is zero then exponent should be between Etiny and
- # Emax if clamp==0, and between Etiny and Etop if clamp==1.
- Etiny = context.Etiny()
- Etop = context.Etop()
- if not self:
- exp_max = [context.Emax, Etop][context.clamp]
- new_exp = min(max(self._exp, Etiny), exp_max)
- if new_exp != self._exp:
- context._raise_error(Clamped)
- return _dec_from_triple(self._sign, '0', new_exp)
- else:
- return Decimal(self)
-
- # exp_min is the smallest allowable exponent of the result,
- # equal to max(self.adjusted()-context.prec+1, Etiny)
- exp_min = len(self._int) + self._exp - context.prec
- if exp_min > Etop:
- # overflow: exp_min > Etop iff self.adjusted() > Emax
- ans = context._raise_error(Overflow, 'above Emax', self._sign)
- context._raise_error(Inexact)
- context._raise_error(Rounded)
- return ans
-
- self_is_subnormal = exp_min < Etiny
- if self_is_subnormal:
- exp_min = Etiny
-
- # round if self has too many digits
- if self._exp < exp_min:
- digits = len(self._int) + self._exp - exp_min
- if digits < 0:
- self = _dec_from_triple(self._sign, '1', exp_min-1)
- digits = 0
- rounding_method = self._pick_rounding_function[context.rounding]
- changed = rounding_method(self, digits)
- coeff = self._int[:digits] or '0'
- if changed > 0:
- coeff = str(int(coeff)+1)
- if len(coeff) > context.prec:
- coeff = coeff[:-1]
- exp_min += 1
-
- # check whether the rounding pushed the exponent out of range
- if exp_min > Etop:
- ans = context._raise_error(Overflow, 'above Emax', self._sign)
- else:
- ans = _dec_from_triple(self._sign, coeff, exp_min)
-
- # raise the appropriate signals, taking care to respect
- # the precedence described in the specification
- if changed and self_is_subnormal:
- context._raise_error(Underflow)
- if self_is_subnormal:
- context._raise_error(Subnormal)
- if changed:
- context._raise_error(Inexact)
- context._raise_error(Rounded)
- if not ans:
- # raise Clamped on underflow to 0
- context._raise_error(Clamped)
- return ans
-
- if self_is_subnormal:
- context._raise_error(Subnormal)
-
- # fold down if clamp == 1 and self has too few digits
- if context.clamp == 1 and self._exp > Etop:
- context._raise_error(Clamped)
- self_padded = self._int + '0'*(self._exp - Etop)
- return _dec_from_triple(self._sign, self_padded, Etop)
-
- # here self was representable to begin with; return unchanged
- return Decimal(self)
-
- # for each of the rounding functions below:
- # self is a finite, nonzero Decimal
- # prec is an integer satisfying 0 <= prec < len(self._int)
- #
- # each function returns either -1, 0, or 1, as follows:
- # 1 indicates that self should be rounded up (away from zero)
- # 0 indicates that self should be truncated, and that all the
- # digits to be truncated are zeros (so the value is unchanged)
- # -1 indicates that there are nonzero digits to be truncated
-
- def _round_down(self, prec):
- """Also known as round-towards-0, truncate."""
- if _all_zeros(self._int, prec):
- return 0
- else:
- return -1
-
- def _round_up(self, prec):
- """Rounds away from 0."""
- return -self._round_down(prec)
-
- def _round_half_up(self, prec):
- """Rounds 5 up (away from 0)"""
- if self._int[prec] in '56789':
- return 1
- elif _all_zeros(self._int, prec):
- return 0
- else:
- return -1
-
- def _round_half_down(self, prec):
- """Round 5 down"""
- if _exact_half(self._int, prec):
- return -1
- else:
- return self._round_half_up(prec)
-
- def _round_half_even(self, prec):
- """Round 5 to even, rest to nearest."""
- if _exact_half(self._int, prec) and \
- (prec == 0 or self._int[prec-1] in '02468'):
- return -1
- else:
- return self._round_half_up(prec)
-
- def _round_ceiling(self, prec):
- """Rounds up (not away from 0 if negative.)"""
- if self._sign:
- return self._round_down(prec)
- else:
- return -self._round_down(prec)
-
- def _round_floor(self, prec):
- """Rounds down (not towards 0 if negative)"""
- if not self._sign:
- return self._round_down(prec)
- else:
- return -self._round_down(prec)
-
- def _round_05up(self, prec):
- """Round down unless digit prec-1 is 0 or 5."""
- if prec and self._int[prec-1] not in '05':
- return self._round_down(prec)
- else:
- return -self._round_down(prec)
-
- _pick_rounding_function = dict(
- ROUND_DOWN = _round_down,
- ROUND_UP = _round_up,
- ROUND_HALF_UP = _round_half_up,
- ROUND_HALF_DOWN = _round_half_down,
- ROUND_HALF_EVEN = _round_half_even,
- ROUND_CEILING = _round_ceiling,
- ROUND_FLOOR = _round_floor,
- ROUND_05UP = _round_05up,
- )
-
- def __round__(self, n=None):
- """Round self to the nearest integer, or to a given precision.
-
- If only one argument is supplied, round a finite Decimal
- instance self to the nearest integer. If self is infinite or
- a NaN then a Python exception is raised. If self is finite
- and lies exactly halfway between two integers then it is
- rounded to the integer with even last digit.
-
- >>> round(Decimal('123.456'))
- 123
- >>> round(Decimal('-456.789'))
- -457
- >>> round(Decimal('-3.0'))
- -3
- >>> round(Decimal('2.5'))
- 2
- >>> round(Decimal('3.5'))
- 4
- >>> round(Decimal('Inf'))
- Traceback (most recent call last):
- ...
- OverflowError: cannot round an infinity
- >>> round(Decimal('NaN'))
- Traceback (most recent call last):
- ...
- ValueError: cannot round a NaN
-
- If a second argument n is supplied, self is rounded to n
- decimal places using the rounding mode for the current
- context.
-
- For an integer n, round(self, -n) is exactly equivalent to
- self.quantize(Decimal('1En')).
-
- >>> round(Decimal('123.456'), 0)
- Decimal('123')
- >>> round(Decimal('123.456'), 2)
- Decimal('123.46')
- >>> round(Decimal('123.456'), -2)
- Decimal('1E+2')
- >>> round(Decimal('-Infinity'), 37)
- Decimal('NaN')
- >>> round(Decimal('sNaN123'), 0)
- Decimal('NaN123')
-
- """
- if n is not None:
- # two-argument form: use the equivalent quantize call
- if not isinstance(n, int):
- raise TypeError('Second argument to round should be integral')
- exp = _dec_from_triple(0, '1', -n)
- return self.quantize(exp)
-
- # one-argument form
- if self._is_special:
- if self.is_nan():
- raise ValueError("cannot round a NaN")
- else:
- raise OverflowError("cannot round an infinity")
- return int(self._rescale(0, ROUND_HALF_EVEN))
-
- def __floor__(self):
- """Return the floor of self, as an integer.
-
- For a finite Decimal instance self, return the greatest
- integer n such that n <= self. If self is infinite or a NaN
- then a Python exception is raised.
-
- """
- if self._is_special:
- if self.is_nan():
- raise ValueError("cannot round a NaN")
- else:
- raise OverflowError("cannot round an infinity")
- return int(self._rescale(0, ROUND_FLOOR))
-
- def __ceil__(self):
- """Return the ceiling of self, as an integer.
-
- For a finite Decimal instance self, return the least integer n
- such that n >= self. If self is infinite or a NaN then a
- Python exception is raised.
-
- """
- if self._is_special:
- if self.is_nan():
- raise ValueError("cannot round a NaN")
- else:
- raise OverflowError("cannot round an infinity")
- return int(self._rescale(0, ROUND_CEILING))
-
- def fma(self, other, third, context=None):
- """Fused multiply-add.
-
- Returns self*other+third with no rounding of the intermediate
- product self*other.
-
- self and other are multiplied together, with no rounding of
- the result. The third operand is then added to the result,
- and a single final rounding is performed.
- """
-
- other = _convert_other(other, raiseit=True)
- third = _convert_other(third, raiseit=True)
-
- # compute product; raise InvalidOperation if either operand is
- # a signaling NaN or if the product is zero times infinity.
- if self._is_special or other._is_special:
- if context is None:
- context = getcontext()
- if self._exp == 'N':
- return context._raise_error(InvalidOperation, 'sNaN', self)
- if other._exp == 'N':
- return context._raise_error(InvalidOperation, 'sNaN', other)
- if self._exp == 'n':
- product = self
- elif other._exp == 'n':
- product = other
- elif self._exp == 'F':
- if not other:
- return context._raise_error(InvalidOperation,
- 'INF * 0 in fma')
- product = _SignedInfinity[self._sign ^ other._sign]
- elif other._exp == 'F':
- if not self:
- return context._raise_error(InvalidOperation,
- '0 * INF in fma')
- product = _SignedInfinity[self._sign ^ other._sign]
- else:
- product = _dec_from_triple(self._sign ^ other._sign,
- str(int(self._int) * int(other._int)),
- self._exp + other._exp)
-
- return product.__add__(third, context)
-
- def _power_modulo(self, other, modulo, context=None):
- """Three argument version of __pow__"""
-
- other = _convert_other(other)
- if other is NotImplemented:
- return other
- modulo = _convert_other(modulo)
- if modulo is NotImplemented:
- return modulo
-
- if context is None:
- context = getcontext()
-
- # deal with NaNs: if there are any sNaNs then first one wins,
- # (i.e. behaviour for NaNs is identical to that of fma)
- self_is_nan = self._isnan()
- other_is_nan = other._isnan()
- modulo_is_nan = modulo._isnan()
- if self_is_nan or other_is_nan or modulo_is_nan:
- if self_is_nan == 2:
- return context._raise_error(InvalidOperation, 'sNaN',
- self)
- if other_is_nan == 2:
- return context._raise_error(InvalidOperation, 'sNaN',
- other)
- if modulo_is_nan == 2:
- return context._raise_error(InvalidOperation, 'sNaN',
- modulo)
- if self_is_nan:
- return self._fix_nan(context)
- if other_is_nan:
- return other._fix_nan(context)
- return modulo._fix_nan(context)
-
- # check inputs: we apply same restrictions as Python's pow()
- if not (self._isinteger() and
- other._isinteger() and
- modulo._isinteger()):
- return context._raise_error(InvalidOperation,
- 'pow() 3rd argument not allowed '
- 'unless all arguments are integers')
- if other < 0:
- return context._raise_error(InvalidOperation,
- 'pow() 2nd argument cannot be '
- 'negative when 3rd argument specified')
- if not modulo:
- return context._raise_error(InvalidOperation,
- 'pow() 3rd argument cannot be 0')
-
- # additional restriction for decimal: the modulus must be less
- # than 10**prec in absolute value
- if modulo.adjusted() >= context.prec:
- return context._raise_error(InvalidOperation,
- 'insufficient precision: pow() 3rd '
- 'argument must not have more than '
- 'precision digits')
-
- # define 0**0 == NaN, for consistency with two-argument pow
- # (even though it hurts!)
- if not other and not self:
- return context._raise_error(InvalidOperation,
- 'at least one of pow() 1st argument '
- 'and 2nd argument must be nonzero ;'
- '0**0 is not defined')
-
- # compute sign of result
- if other._iseven():
- sign = 0
- else:
- sign = self._sign
-
- # convert modulo to a Python integer, and self and other to
- # Decimal integers (i.e. force their exponents to be >= 0)
- modulo = abs(int(modulo))
- base = _WorkRep(self.to_integral_value())
- exponent = _WorkRep(other.to_integral_value())
-
- # compute result using integer pow()
- base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo
- for i in range(exponent.exp):
- base = pow(base, 10, modulo)
- base = pow(base, exponent.int, modulo)
-
- return _dec_from_triple(sign, str(base), 0)
-
- def _power_exact(self, other, p):
- """Attempt to compute self**other exactly.
-
- Given Decimals self and other and an integer p, attempt to
- compute an exact result for the power self**other, with p
- digits of precision. Return None if self**other is not
- exactly representable in p digits.
-
- Assumes that elimination of special cases has already been
- performed: self and other must both be nonspecial; self must
- be positive and not numerically equal to 1; other must be
- nonzero. For efficiency, other._exp should not be too large,
- so that 10**abs(other._exp) is a feasible calculation."""
-
- # In the comments below, we write x for the value of self and y for the
- # value of other. Write x = xc*10**xe and abs(y) = yc*10**ye, with xc
- # and yc positive integers not divisible by 10.
-
- # The main purpose of this method is to identify the *failure*
- # of x**y to be exactly representable with as little effort as
- # possible. So we look for cheap and easy tests that
- # eliminate the possibility of x**y being exact. Only if all
- # these tests are passed do we go on to actually compute x**y.
-
- # Here's the main idea. Express y as a rational number m/n, with m and
- # n relatively prime and n>0. Then for x**y to be exactly
- # representable (at *any* precision), xc must be the nth power of a
- # positive integer and xe must be divisible by n. If y is negative
- # then additionally xc must be a power of either 2 or 5, hence a power
- # of 2**n or 5**n.
- #
- # There's a limit to how small |y| can be: if y=m/n as above
- # then:
- #
- # (1) if xc != 1 then for the result to be representable we
- # need xc**(1/n) >= 2, and hence also xc**|y| >= 2. So
- # if |y| <= 1/nbits(xc) then xc < 2**nbits(xc) <=
- # 2**(1/|y|), hence xc**|y| < 2 and the result is not
- # representable.
- #
- # (2) if xe != 0, |xe|*(1/n) >= 1, so |xe|*|y| >= 1. Hence if
- # |y| < 1/|xe| then the result is not representable.
- #
- # Note that since x is not equal to 1, at least one of (1) and
- # (2) must apply. Now |y| < 1/nbits(xc) iff |yc|*nbits(xc) <
- # 10**-ye iff len(str(|yc|*nbits(xc)) <= -ye.
- #
- # There's also a limit to how large y can be, at least if it's
- # positive: the normalized result will have coefficient xc**y,
- # so if it's representable then xc**y < 10**p, and y <
- # p/log10(xc). Hence if y*log10(xc) >= p then the result is
- # not exactly representable.
-
- # if len(str(abs(yc*xe)) <= -ye then abs(yc*xe) < 10**-ye,
- # so |y| < 1/xe and the result is not representable.
- # Similarly, len(str(abs(yc)*xc_bits)) <= -ye implies |y|
- # < 1/nbits(xc).
-
- x = _WorkRep(self)
- xc, xe = x.int, x.exp
- while xc % 10 == 0:
- xc //= 10
- xe += 1
-
- y = _WorkRep(other)
- yc, ye = y.int, y.exp
- while yc % 10 == 0:
- yc //= 10
- ye += 1
-
- # case where xc == 1: result is 10**(xe*y), with xe*y
- # required to be an integer
- if xc == 1:
- xe *= yc
- # result is now 10**(xe * 10**ye); xe * 10**ye must be integral
- while xe % 10 == 0:
- xe //= 10
- ye += 1
- if ye < 0:
- return None
- exponent = xe * 10**ye
- if y.sign == 1:
- exponent = -exponent
- # if other is a nonnegative integer, use ideal exponent
- if other._isinteger() and other._sign == 0:
- ideal_exponent = self._exp*int(other)
- zeros = min(exponent-ideal_exponent, p-1)
- else:
- zeros = 0
- return _dec_from_triple(0, '1' + '0'*zeros, exponent-zeros)
-
- # case where y is negative: xc must be either a power
- # of 2 or a power of 5.
- if y.sign == 1:
- last_digit = xc % 10
- if last_digit in (2,4,6,8):
- # quick test for power of 2
- if xc & -xc != xc:
- return None
- # now xc is a power of 2; e is its exponent
- e = _nbits(xc)-1
-
- # We now have:
- #
- # x = 2**e * 10**xe, e > 0, and y < 0.
- #
- # The exact result is:
- #
- # x**y = 5**(-e*y) * 10**(e*y + xe*y)
- #
- # provided that both e*y and xe*y are integers. Note that if
- # 5**(-e*y) >= 10**p, then the result can't be expressed
- # exactly with p digits of precision.
- #
- # Using the above, we can guard against large values of ye.
- # 93/65 is an upper bound for log(10)/log(5), so if
- #
- # ye >= len(str(93*p//65))
- #
- # then
- #
- # -e*y >= -y >= 10**ye > 93*p/65 > p*log(10)/log(5),
- #
- # so 5**(-e*y) >= 10**p, and the coefficient of the result
- # can't be expressed in p digits.
-
- # emax >= largest e such that 5**e < 10**p.
- emax = p*93//65
- if ye >= len(str(emax)):
- return None
-
- # Find -e*y and -xe*y; both must be integers
- e = _decimal_lshift_exact(e * yc, ye)
- xe = _decimal_lshift_exact(xe * yc, ye)
- if e is None or xe is None:
- return None
-
- if e > emax:
- return None
- xc = 5**e
-
- elif last_digit == 5:
- # e >= log_5(xc) if xc is a power of 5; we have
- # equality all the way up to xc=5**2658
- e = _nbits(xc)*28//65
- xc, remainder = divmod(5**e, xc)
- if remainder:
- return None
- while xc % 5 == 0:
- xc //= 5
- e -= 1
-
- # Guard against large values of ye, using the same logic as in
- # the 'xc is a power of 2' branch. 10/3 is an upper bound for
- # log(10)/log(2).
- emax = p*10//3
- if ye >= len(str(emax)):
- return None
-
- e = _decimal_lshift_exact(e * yc, ye)
- xe = _decimal_lshift_exact(xe * yc, ye)
- if e is None or xe is None:
- return None
-
- if e > emax:
- return None
- xc = 2**e
- else:
- return None
-
- if xc >= 10**p:
- return None
- xe = -e-xe
- return _dec_from_triple(0, str(xc), xe)
-
- # now y is positive; find m and n such that y = m/n
- if ye >= 0:
- m, n = yc*10**ye, 1
- else:
- if xe != 0 and len(str(abs(yc*xe))) <= -ye:
- return None
- xc_bits = _nbits(xc)
- if xc != 1 and len(str(abs(yc)*xc_bits)) <= -ye:
- return None
- m, n = yc, 10**(-ye)
- while m % 2 == n % 2 == 0:
- m //= 2
- n //= 2
- while m % 5 == n % 5 == 0:
- m //= 5
- n //= 5
-
- # compute nth root of xc*10**xe
- if n > 1:
- # if 1 < xc < 2**n then xc isn't an nth power
- if xc != 1 and xc_bits <= n:
- return None
-
- xe, rem = divmod(xe, n)
- if rem != 0:
- return None
-
- # compute nth root of xc using Newton's method
- a = 1 << -(-_nbits(xc)//n) # initial estimate
- while True:
- q, r = divmod(xc, a**(n-1))
- if a <= q:
- break
- else:
- a = (a*(n-1) + q)//n
- if not (a == q and r == 0):
- return None
- xc = a
-
- # now xc*10**xe is the nth root of the original xc*10**xe
- # compute mth power of xc*10**xe
-
- # if m > p*100//_log10_lb(xc) then m > p/log10(xc), hence xc**m >
- # 10**p and the result is not representable.
- if xc > 1 and m > p*100//_log10_lb(xc):
- return None
- xc = xc**m
- xe *= m
- if xc > 10**p:
- return None
-
- # by this point the result *is* exactly representable
- # adjust the exponent to get as close as possible to the ideal
- # exponent, if necessary
- str_xc = str(xc)
- if other._isinteger() and other._sign == 0:
- ideal_exponent = self._exp*int(other)
- zeros = min(xe-ideal_exponent, p-len(str_xc))
- else:
- zeros = 0
- return _dec_from_triple(0, str_xc+'0'*zeros, xe-zeros)
-
- def __pow__(self, other, modulo=None, context=None):
- """Return self ** other [ % modulo].
-
- With two arguments, compute self**other.
-
- With three arguments, compute (self**other) % modulo. For the
- three argument form, the following restrictions on the
- arguments hold:
-
- - all three arguments must be integral
- - other must be nonnegative
- - either self or other (or both) must be nonzero
- - modulo must be nonzero and must have at most p digits,
- where p is the context precision.
-
- If any of these restrictions is violated the InvalidOperation
- flag is raised.
-
- The result of pow(self, other, modulo) is identical to the
- result that would be obtained by computing (self**other) %
- modulo with unbounded precision, but is computed more
- efficiently. It is always exact.
- """
-
- if modulo is not None:
- return self._power_modulo(other, modulo, context)
-
- other = _convert_other(other)
- if other is NotImplemented:
- return other
-
- if context is None:
- context = getcontext()
-
- # either argument is a NaN => result is NaN
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- # 0**0 = NaN (!), x**0 = 1 for nonzero x (including +/-Infinity)
- if not other:
- if not self:
- return context._raise_error(InvalidOperation, '0 ** 0')
- else:
- return _One
-
- # result has sign 1 iff self._sign is 1 and other is an odd integer
- result_sign = 0
- if self._sign == 1:
- if other._isinteger():
- if not other._iseven():
- result_sign = 1
- else:
- # -ve**noninteger = NaN
- # (-0)**noninteger = 0**noninteger
- if self:
- return context._raise_error(InvalidOperation,
- 'x ** y with x negative and y not an integer')
- # negate self, without doing any unwanted rounding
- self = self.copy_negate()
-
- # 0**(+ve or Inf)= 0; 0**(-ve or -Inf) = Infinity
- if not self:
- if other._sign == 0:
- return _dec_from_triple(result_sign, '0', 0)
- else:
- return _SignedInfinity[result_sign]
-
- # Inf**(+ve or Inf) = Inf; Inf**(-ve or -Inf) = 0
- if self._isinfinity():
- if other._sign == 0:
- return _SignedInfinity[result_sign]
- else:
- return _dec_from_triple(result_sign, '0', 0)
-
- # 1**other = 1, but the choice of exponent and the flags
- # depend on the exponent of self, and on whether other is a
- # positive integer, a negative integer, or neither
- if self == _One:
- if other._isinteger():
- # exp = max(self._exp*max(int(other), 0),
- # 1-context.prec) but evaluating int(other) directly
- # is dangerous until we know other is small (other
- # could be 1e999999999)
- if other._sign == 1:
- multiplier = 0
- elif other > context.prec:
- multiplier = context.prec
- else:
- multiplier = int(other)
-
- exp = self._exp * multiplier
- if exp < 1-context.prec:
- exp = 1-context.prec
- context._raise_error(Rounded)
- else:
- context._raise_error(Inexact)
- context._raise_error(Rounded)
- exp = 1-context.prec
-
- return _dec_from_triple(result_sign, '1'+'0'*-exp, exp)
-
- # compute adjusted exponent of self
- self_adj = self.adjusted()
-
- # self ** infinity is infinity if self > 1, 0 if self < 1
- # self ** -infinity is infinity if self < 1, 0 if self > 1
- if other._isinfinity():
- if (other._sign == 0) == (self_adj < 0):
- return _dec_from_triple(result_sign, '0', 0)
- else:
- return _SignedInfinity[result_sign]
-
- # from here on, the result always goes through the call
- # to _fix at the end of this function.
- ans = None
- exact = False
-
- # crude test to catch cases of extreme overflow/underflow. If
- # log10(self)*other >= 10**bound and bound >= len(str(Emax))
- # then 10**bound >= 10**len(str(Emax)) >= Emax+1 and hence
- # self**other >= 10**(Emax+1), so overflow occurs. The test
- # for underflow is similar.
- bound = self._log10_exp_bound() + other.adjusted()
- if (self_adj >= 0) == (other._sign == 0):
- # self > 1 and other +ve, or self < 1 and other -ve
- # possibility of overflow
- if bound >= len(str(context.Emax)):
- ans = _dec_from_triple(result_sign, '1', context.Emax+1)
- else:
- # self > 1 and other -ve, or self < 1 and other +ve
- # possibility of underflow to 0
- Etiny = context.Etiny()
- if bound >= len(str(-Etiny)):
- ans = _dec_from_triple(result_sign, '1', Etiny-1)
-
- # try for an exact result with precision +1
- if ans is None:
- ans = self._power_exact(other, context.prec + 1)
- if ans is not None:
- if result_sign == 1:
- ans = _dec_from_triple(1, ans._int, ans._exp)
- exact = True
-
- # usual case: inexact result, x**y computed directly as exp(y*log(x))
- if ans is None:
- p = context.prec
- x = _WorkRep(self)
- xc, xe = x.int, x.exp
- y = _WorkRep(other)
- yc, ye = y.int, y.exp
- if y.sign == 1:
- yc = -yc
-
- # compute correctly rounded result: start with precision +3,
- # then increase precision until result is unambiguously roundable
- extra = 3
- while True:
- coeff, exp = _dpower(xc, xe, yc, ye, p+extra)
- if coeff % (5*10**(len(str(coeff))-p-1)):
- break
- extra += 3
-
- ans = _dec_from_triple(result_sign, str(coeff), exp)
-
- # unlike exp, ln and log10, the power function respects the
- # rounding mode; no need to switch to ROUND_HALF_EVEN here
-
- # There's a difficulty here when 'other' is not an integer and
- # the result is exact. In this case, the specification
- # requires that the Inexact flag be raised (in spite of
- # exactness), but since the result is exact _fix won't do this
- # for us. (Correspondingly, the Underflow signal should also
- # be raised for subnormal results.) We can't directly raise
- # these signals either before or after calling _fix, since
- # that would violate the precedence for signals. So we wrap
- # the ._fix call in a temporary context, and reraise
- # afterwards.
- if exact and not other._isinteger():
- # pad with zeros up to length context.prec+1 if necessary; this
- # ensures that the Rounded signal will be raised.
- if len(ans._int) <= context.prec:
- expdiff = context.prec + 1 - len(ans._int)
- ans = _dec_from_triple(ans._sign, ans._int+'0'*expdiff,
- ans._exp-expdiff)
-
- # create a copy of the current context, with cleared flags/traps
- newcontext = context.copy()
- newcontext.clear_flags()
- for exception in _signals:
- newcontext.traps[exception] = 0
-
- # round in the new context
- ans = ans._fix(newcontext)
-
- # raise Inexact, and if necessary, Underflow
- newcontext._raise_error(Inexact)
- if newcontext.flags[Subnormal]:
- newcontext._raise_error(Underflow)
-
- # propagate signals to the original context; _fix could
- # have raised any of Overflow, Underflow, Subnormal,
- # Inexact, Rounded, Clamped. Overflow needs the correct
- # arguments. Note that the order of the exceptions is
- # important here.
- if newcontext.flags[Overflow]:
- context._raise_error(Overflow, 'above Emax', ans._sign)
- for exception in Underflow, Subnormal, Inexact, Rounded, Clamped:
- if newcontext.flags[exception]:
- context._raise_error(exception)
-
- else:
- ans = ans._fix(context)
-
- return ans
-
- def __rpow__(self, other, context=None):
- """Swaps self/other and returns __pow__."""
- other = _convert_other(other)
- if other is NotImplemented:
- return other
- return other.__pow__(self, context=context)
-
- def normalize(self, context=None):
- """Normalize- strip trailing 0s, change anything equal to 0 to 0e0"""
-
- if context is None:
- context = getcontext()
-
- if self._is_special:
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- dup = self._fix(context)
- if dup._isinfinity():
- return dup
-
- if not dup:
- return _dec_from_triple(dup._sign, '0', 0)
- exp_max = [context.Emax, context.Etop()][context.clamp]
- end = len(dup._int)
- exp = dup._exp
- while dup._int[end-1] == '0' and exp < exp_max:
- exp += 1
- end -= 1
- return _dec_from_triple(dup._sign, dup._int[:end], exp)
-
- def quantize(self, exp, rounding=None, context=None, watchexp=True):
- """Quantize self so its exponent is the same as that of exp.
-
- Similar to self._rescale(exp._exp) but with error checking.
- """
- exp = _convert_other(exp, raiseit=True)
-
- if context is None:
- context = getcontext()
- if rounding is None:
- rounding = context.rounding
-
- if self._is_special or exp._is_special:
- ans = self._check_nans(exp, context)
- if ans:
- return ans
-
- if exp._isinfinity() or self._isinfinity():
- if exp._isinfinity() and self._isinfinity():
- return Decimal(self) # if both are inf, it is OK
- return context._raise_error(InvalidOperation,
- 'quantize with one INF')
-
- # if we're not watching exponents, do a simple rescale
- if not watchexp:
- ans = self._rescale(exp._exp, rounding)
- # raise Inexact and Rounded where appropriate
- if ans._exp > self._exp:
- context._raise_error(Rounded)
- if ans != self:
- context._raise_error(Inexact)
- return ans
-
- # exp._exp should be between Etiny and Emax
- if not (context.Etiny() <= exp._exp <= context.Emax):
- return context._raise_error(InvalidOperation,
- 'target exponent out of bounds in quantize')
-
- if not self:
- ans = _dec_from_triple(self._sign, '0', exp._exp)
- return ans._fix(context)
-
- self_adjusted = self.adjusted()
- if self_adjusted > context.Emax:
- return context._raise_error(InvalidOperation,
- 'exponent of quantize result too large for current context')
- if self_adjusted - exp._exp + 1 > context.prec:
- return context._raise_error(InvalidOperation,
- 'quantize result has too many digits for current context')
-
- ans = self._rescale(exp._exp, rounding)
- if ans.adjusted() > context.Emax:
- return context._raise_error(InvalidOperation,
- 'exponent of quantize result too large for current context')
- if len(ans._int) > context.prec:
- return context._raise_error(InvalidOperation,
- 'quantize result has too many digits for current context')
-
- # raise appropriate flags
- if ans and ans.adjusted() < context.Emin:
- context._raise_error(Subnormal)
- if ans._exp > self._exp:
- if ans != self:
- context._raise_error(Inexact)
- context._raise_error(Rounded)
-
- # call to fix takes care of any necessary folddown, and
- # signals Clamped if necessary
- ans = ans._fix(context)
- return ans
-
- def same_quantum(self, other, context=None):
- """Return True if self and other have the same exponent; otherwise
- return False.
-
- If either operand is a special value, the following rules are used:
- * return True if both operands are infinities
- * return True if both operands are NaNs
- * otherwise, return False.
- """
- other = _convert_other(other, raiseit=True)
- if self._is_special or other._is_special:
- return (self.is_nan() and other.is_nan() or
- self.is_infinite() and other.is_infinite())
- return self._exp == other._exp
-
- def _rescale(self, exp, rounding):
- """Rescale self so that the exponent is exp, either by padding with zeros
- or by truncating digits, using the given rounding mode.
-
- Specials are returned without change. This operation is
- quiet: it raises no flags, and uses no information from the
- context.
-
- exp = exp to scale to (an integer)
- rounding = rounding mode
- """
- if self._is_special:
- return Decimal(self)
- if not self:
- return _dec_from_triple(self._sign, '0', exp)
-
- if self._exp >= exp:
- # pad answer with zeros if necessary
- return _dec_from_triple(self._sign,
- self._int + '0'*(self._exp - exp), exp)
-
- # too many digits; round and lose data. If self.adjusted() <
- # exp-1, replace self by 10**(exp-1) before rounding
- digits = len(self._int) + self._exp - exp
- if digits < 0:
- self = _dec_from_triple(self._sign, '1', exp-1)
- digits = 0
- this_function = self._pick_rounding_function[rounding]
- changed = this_function(self, digits)
- coeff = self._int[:digits] or '0'
- if changed == 1:
- coeff = str(int(coeff)+1)
- return _dec_from_triple(self._sign, coeff, exp)
-
- def _round(self, places, rounding):
- """Round a nonzero, nonspecial Decimal to a fixed number of
- significant figures, using the given rounding mode.
-
- Infinities, NaNs and zeros are returned unaltered.
-
- This operation is quiet: it raises no flags, and uses no
- information from the context.
-
- """
- if places <= 0:
- raise ValueError("argument should be at least 1 in _round")
- if self._is_special or not self:
- return Decimal(self)
- ans = self._rescale(self.adjusted()+1-places, rounding)
- # it can happen that the rescale alters the adjusted exponent;
- # for example when rounding 99.97 to 3 significant figures.
- # When this happens we end up with an extra 0 at the end of
- # the number; a second rescale fixes this.
- if ans.adjusted() != self.adjusted():
- ans = ans._rescale(ans.adjusted()+1-places, rounding)
- return ans
-
- def to_integral_exact(self, rounding=None, context=None):
- """Rounds to a nearby integer.
-
- If no rounding mode is specified, take the rounding mode from
- the context. This method raises the Rounded and Inexact flags
- when appropriate.
-
- See also: to_integral_value, which does exactly the same as
- this method except that it doesn't raise Inexact or Rounded.
- """
- if self._is_special:
- ans = self._check_nans(context=context)
- if ans:
- return ans
- return Decimal(self)
- if self._exp >= 0:
- return Decimal(self)
- if not self:
- return _dec_from_triple(self._sign, '0', 0)
- if context is None:
- context = getcontext()
- if rounding is None:
- rounding = context.rounding
- ans = self._rescale(0, rounding)
- if ans != self:
- context._raise_error(Inexact)
- context._raise_error(Rounded)
- return ans
-
- def to_integral_value(self, rounding=None, context=None):
- """Rounds to the nearest integer, without raising inexact, rounded."""
- if context is None:
- context = getcontext()
- if rounding is None:
- rounding = context.rounding
- if self._is_special:
- ans = self._check_nans(context=context)
- if ans:
- return ans
- return Decimal(self)
- if self._exp >= 0:
- return Decimal(self)
- else:
- return self._rescale(0, rounding)
-
- # the method name changed, but we provide also the old one, for compatibility
- to_integral = to_integral_value
-
- def sqrt(self, context=None):
- """Return the square root of self."""
- if context is None:
- context = getcontext()
-
- if self._is_special:
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- if self._isinfinity() and self._sign == 0:
- return Decimal(self)
-
- if not self:
- # exponent = self._exp // 2. sqrt(-0) = -0
- ans = _dec_from_triple(self._sign, '0', self._exp // 2)
- return ans._fix(context)
-
- if self._sign == 1:
- return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0')
-
- # At this point self represents a positive number. Let p be
- # the desired precision and express self in the form c*100**e
- # with c a positive real number and e an integer, c and e
- # being chosen so that 100**(p-1) <= c < 100**p. Then the
- # (exact) square root of self is sqrt(c)*10**e, and 10**(p-1)
- # <= sqrt(c) < 10**p, so the closest representable Decimal at
- # precision p is n*10**e where n = round_half_even(sqrt(c)),
- # the closest integer to sqrt(c) with the even integer chosen
- # in the case of a tie.
- #
- # To ensure correct rounding in all cases, we use the
- # following trick: we compute the square root to an extra
- # place (precision p+1 instead of precision p), rounding down.
- # Then, if the result is inexact and its last digit is 0 or 5,
- # we increase the last digit to 1 or 6 respectively; if it's
- # exact we leave the last digit alone. Now the final round to
- # p places (or fewer in the case of underflow) will round
- # correctly and raise the appropriate flags.
-
- # use an extra digit of precision
- prec = context.prec+1
-
- # write argument in the form c*100**e where e = self._exp//2
- # is the 'ideal' exponent, to be used if the square root is
- # exactly representable. l is the number of 'digits' of c in
- # base 100, so that 100**(l-1) <= c < 100**l.
- op = _WorkRep(self)
- e = op.exp >> 1
- if op.exp & 1:
- c = op.int * 10
- l = (len(self._int) >> 1) + 1
- else:
- c = op.int
- l = len(self._int)+1 >> 1
-
- # rescale so that c has exactly prec base 100 'digits'
- shift = prec-l
- if shift >= 0:
- c *= 100**shift
- exact = True
- else:
- c, remainder = divmod(c, 100**-shift)
- exact = not remainder
- e -= shift
-
- # find n = floor(sqrt(c)) using Newton's method
- n = 10**prec
- while True:
- q = c//n
- if n <= q:
- break
- else:
- n = n + q >> 1
- exact = exact and n*n == c
-
- if exact:
- # result is exact; rescale to use ideal exponent e
- if shift >= 0:
- # assert n % 10**shift == 0
- n //= 10**shift
- else:
- n *= 10**-shift
- e += shift
- else:
- # result is not exact; fix last digit as described above
- if n % 5 == 0:
- n += 1
-
- ans = _dec_from_triple(0, str(n), e)
-
- # round, and fit to current context
- context = context._shallow_copy()
- rounding = context._set_rounding(ROUND_HALF_EVEN)
- ans = ans._fix(context)
- context.rounding = rounding
-
- return ans
-
- def max(self, other, context=None):
- """Returns the larger value.
-
- Like max(self, other) except if one is not a number, returns
- NaN (and signals if one is sNaN). Also rounds.
- """
- other = _convert_other(other, raiseit=True)
-
- if context is None:
- context = getcontext()
-
- if self._is_special or other._is_special:
- # If one operand is a quiet NaN and the other is number, then the
- # number is always returned
- sn = self._isnan()
- on = other._isnan()
- if sn or on:
- if on == 1 and sn == 0:
- return self._fix(context)
- if sn == 1 and on == 0:
- return other._fix(context)
- return self._check_nans(other, context)
-
- c = self._cmp(other)
- if c == 0:
- # If both operands are finite and equal in numerical value
- # then an ordering is applied:
- #
- # If the signs differ then max returns the operand with the
- # positive sign and min returns the operand with the negative sign
- #
- # If the signs are the same then the exponent is used to select
- # the result. This is exactly the ordering used in compare_total.
- c = self.compare_total(other)
-
- if c == -1:
- ans = other
- else:
- ans = self
-
- return ans._fix(context)
-
- def min(self, other, context=None):
- """Returns the smaller value.
-
- Like min(self, other) except if one is not a number, returns
- NaN (and signals if one is sNaN). Also rounds.
- """
- other = _convert_other(other, raiseit=True)
-
- if context is None:
- context = getcontext()
-
- if self._is_special or other._is_special:
- # If one operand is a quiet NaN and the other is number, then the
- # number is always returned
- sn = self._isnan()
- on = other._isnan()
- if sn or on:
- if on == 1 and sn == 0:
- return self._fix(context)
- if sn == 1 and on == 0:
- return other._fix(context)
- return self._check_nans(other, context)
-
- c = self._cmp(other)
- if c == 0:
- c = self.compare_total(other)
-
- if c == -1:
- ans = self
- else:
- ans = other
-
- return ans._fix(context)
-
- def _isinteger(self):
- """Returns whether self is an integer"""
- if self._is_special:
- return False
- if self._exp >= 0:
- return True
- rest = self._int[self._exp:]
- return rest == '0'*len(rest)
-
- def _iseven(self):
- """Returns True if self is even. Assumes self is an integer."""
- if not self or self._exp > 0:
- return True
- return self._int[-1+self._exp] in '02468'
-
- def adjusted(self):
- """Return the adjusted exponent of self"""
- try:
- return self._exp + len(self._int) - 1
- # If NaN or Infinity, self._exp is string
- except TypeError:
- return 0
-
- def canonical(self):
- """Returns the same Decimal object.
-
- As we do not have different encodings for the same number, the
- received object already is in its canonical form.
- """
- return self
-
- def compare_signal(self, other, context=None):
- """Compares self to the other operand numerically.
-
- It's pretty much like compare(), but all NaNs signal, with signaling
- NaNs taking precedence over quiet NaNs.
- """
- other = _convert_other(other, raiseit = True)
- ans = self._compare_check_nans(other, context)
- if ans:
- return ans
- return self.compare(other, context=context)
-
- def compare_total(self, other, context=None):
- """Compares self to other using the abstract representations.
-
- This is not like the standard compare, which use their numerical
- value. Note that a total ordering is defined for all possible abstract
- representations.
- """
- other = _convert_other(other, raiseit=True)
-
- # if one is negative and the other is positive, it's easy
- if self._sign and not other._sign:
- return _NegativeOne
- if not self._sign and other._sign:
- return _One
- sign = self._sign
-
- # let's handle both NaN types
- self_nan = self._isnan()
- other_nan = other._isnan()
- if self_nan or other_nan:
- if self_nan == other_nan:
- # compare payloads as though they're integers
- self_key = len(self._int), self._int
- other_key = len(other._int), other._int
- if self_key < other_key:
- if sign:
- return _One
- else:
- return _NegativeOne
- if self_key > other_key:
- if sign:
- return _NegativeOne
- else:
- return _One
- return _Zero
-
- if sign:
- if self_nan == 1:
- return _NegativeOne
- if other_nan == 1:
- return _One
- if self_nan == 2:
- return _NegativeOne
- if other_nan == 2:
- return _One
- else:
- if self_nan == 1:
- return _One
- if other_nan == 1:
- return _NegativeOne
- if self_nan == 2:
- return _One
- if other_nan == 2:
- return _NegativeOne
-
- if self < other:
- return _NegativeOne
- if self > other:
- return _One
-
- if self._exp < other._exp:
- if sign:
- return _One
- else:
- return _NegativeOne
- if self._exp > other._exp:
- if sign:
- return _NegativeOne
- else:
- return _One
- return _Zero
-
-
- def compare_total_mag(self, other, context=None):
- """Compares self to other using abstract repr., ignoring sign.
-
- Like compare_total, but with operand's sign ignored and assumed to be 0.
- """
- other = _convert_other(other, raiseit=True)
-
- s = self.copy_abs()
- o = other.copy_abs()
- return s.compare_total(o)
-
- def copy_abs(self):
- """Returns a copy with the sign set to 0. """
- return _dec_from_triple(0, self._int, self._exp, self._is_special)
-
- def copy_negate(self):
- """Returns a copy with the sign inverted."""
- if self._sign:
- return _dec_from_triple(0, self._int, self._exp, self._is_special)
- else:
- return _dec_from_triple(1, self._int, self._exp, self._is_special)
-
- def copy_sign(self, other, context=None):
- """Returns self with the sign of other."""
- other = _convert_other(other, raiseit=True)
- return _dec_from_triple(other._sign, self._int,
- self._exp, self._is_special)
-
- def exp(self, context=None):
- """Returns e ** self."""
-
- if context is None:
- context = getcontext()
-
- # exp(NaN) = NaN
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- # exp(-Infinity) = 0
- if self._isinfinity() == -1:
- return _Zero
-
- # exp(0) = 1
- if not self:
- return _One
-
- # exp(Infinity) = Infinity
- if self._isinfinity() == 1:
- return Decimal(self)
-
- # the result is now guaranteed to be inexact (the true
- # mathematical result is transcendental). There's no need to
- # raise Rounded and Inexact here---they'll always be raised as
- # a result of the call to _fix.
- p = context.prec
- adj = self.adjusted()
-
- # we only need to do any computation for quite a small range
- # of adjusted exponents---for example, -29 <= adj <= 10 for
- # the default context. For smaller exponent the result is
- # indistinguishable from 1 at the given precision, while for
- # larger exponent the result either overflows or underflows.
- if self._sign == 0 and adj > len(str((context.Emax+1)*3)):
- # overflow
- ans = _dec_from_triple(0, '1', context.Emax+1)
- elif self._sign == 1 and adj > len(str((-context.Etiny()+1)*3)):
- # underflow to 0
- ans = _dec_from_triple(0, '1', context.Etiny()-1)
- elif self._sign == 0 and adj < -p:
- # p+1 digits; final round will raise correct flags
- ans = _dec_from_triple(0, '1' + '0'*(p-1) + '1', -p)
- elif self._sign == 1 and adj < -p-1:
- # p+1 digits; final round will raise correct flags
- ans = _dec_from_triple(0, '9'*(p+1), -p-1)
- # general case
- else:
- op = _WorkRep(self)
- c, e = op.int, op.exp
- if op.sign == 1:
- c = -c
-
- # compute correctly rounded result: increase precision by
- # 3 digits at a time until we get an unambiguously
- # roundable result
- extra = 3
- while True:
- coeff, exp = _dexp(c, e, p+extra)
- if coeff % (5*10**(len(str(coeff))-p-1)):
- break
- extra += 3
-
- ans = _dec_from_triple(0, str(coeff), exp)
-
- # at this stage, ans should round correctly with *any*
- # rounding mode, not just with ROUND_HALF_EVEN
- context = context._shallow_copy()
- rounding = context._set_rounding(ROUND_HALF_EVEN)
- ans = ans._fix(context)
- context.rounding = rounding
-
- return ans
-
- def is_canonical(self):
- """Return True if self is canonical; otherwise return False.
-
- Currently, the encoding of a Decimal instance is always
- canonical, so this method returns True for any Decimal.
- """
- return True
-
- def is_finite(self):
- """Return True if self is finite; otherwise return False.
-
- A Decimal instance is considered finite if it is neither
- infinite nor a NaN.
- """
- return not self._is_special
-
- def is_infinite(self):
- """Return True if self is infinite; otherwise return False."""
- return self._exp == 'F'
-
- def is_nan(self):
- """Return True if self is a qNaN or sNaN; otherwise return False."""
- return self._exp in ('n', 'N')
-
- def is_normal(self, context=None):
- """Return True if self is a normal number; otherwise return False."""
- if self._is_special or not self:
- return False
- if context is None:
- context = getcontext()
- return context.Emin <= self.adjusted()
-
- def is_qnan(self):
- """Return True if self is a quiet NaN; otherwise return False."""
- return self._exp == 'n'
-
- def is_signed(self):
- """Return True if self is negative; otherwise return False."""
- return self._sign == 1
-
- def is_snan(self):
- """Return True if self is a signaling NaN; otherwise return False."""
- return self._exp == 'N'
-
- def is_subnormal(self, context=None):
- """Return True if self is subnormal; otherwise return False."""
- if self._is_special or not self:
- return False
- if context is None:
- context = getcontext()
- return self.adjusted() < context.Emin
-
- def is_zero(self):
- """Return True if self is a zero; otherwise return False."""
- return not self._is_special and self._int == '0'
-
- def _ln_exp_bound(self):
- """Compute a lower bound for the adjusted exponent of self.ln().
- In other words, compute r such that self.ln() >= 10**r. Assumes
- that self is finite and positive and that self != 1.
- """
-
- # for 0.1 <= x <= 10 we use the inequalities 1-1/x <= ln(x) <= x-1
- adj = self._exp + len(self._int) - 1
- if adj >= 1:
- # argument >= 10; we use 23/10 = 2.3 as a lower bound for ln(10)
- return len(str(adj*23//10)) - 1
- if adj <= -2:
- # argument <= 0.1
- return len(str((-1-adj)*23//10)) - 1
- op = _WorkRep(self)
- c, e = op.int, op.exp
- if adj == 0:
- # 1 < self < 10
- num = str(c-10**-e)
- den = str(c)
- return len(num) - len(den) - (num < den)
- # adj == -1, 0.1 <= self < 1
- return e + len(str(10**-e - c)) - 1
-
-
- def ln(self, context=None):
- """Returns the natural (base e) logarithm of self."""
-
- if context is None:
- context = getcontext()
-
- # ln(NaN) = NaN
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- # ln(0.0) == -Infinity
- if not self:
- return _NegativeInfinity
-
- # ln(Infinity) = Infinity
- if self._isinfinity() == 1:
- return _Infinity
-
- # ln(1.0) == 0.0
- if self == _One:
- return _Zero
-
- # ln(negative) raises InvalidOperation
- if self._sign == 1:
- return context._raise_error(InvalidOperation,
- 'ln of a negative value')
-
- # result is irrational, so necessarily inexact
- op = _WorkRep(self)
- c, e = op.int, op.exp
- p = context.prec
-
- # correctly rounded result: repeatedly increase precision by 3
- # until we get an unambiguously roundable result
- places = p - self._ln_exp_bound() + 2 # at least p+3 places
- while True:
- coeff = _dlog(c, e, places)
- # assert len(str(abs(coeff)))-p >= 1
- if coeff % (5*10**(len(str(abs(coeff)))-p-1)):
- break
- places += 3
- ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places)
-
- context = context._shallow_copy()
- rounding = context._set_rounding(ROUND_HALF_EVEN)
- ans = ans._fix(context)
- context.rounding = rounding
- return ans
-
- def _log10_exp_bound(self):
- """Compute a lower bound for the adjusted exponent of self.log10().
- In other words, find r such that self.log10() >= 10**r.
- Assumes that self is finite and positive and that self != 1.
- """
-
- # For x >= 10 or x < 0.1 we only need a bound on the integer
- # part of log10(self), and this comes directly from the
- # exponent of x. For 0.1 <= x <= 10 we use the inequalities
- # 1-1/x <= log(x) <= x-1. If x > 1 we have |log10(x)| >
- # (1-1/x)/2.31 > 0. If x < 1 then |log10(x)| > (1-x)/2.31 > 0
-
- adj = self._exp + len(self._int) - 1
- if adj >= 1:
- # self >= 10
- return len(str(adj))-1
- if adj <= -2:
- # self < 0.1
- return len(str(-1-adj))-1
- op = _WorkRep(self)
- c, e = op.int, op.exp
- if adj == 0:
- # 1 < self < 10
- num = str(c-10**-e)
- den = str(231*c)
- return len(num) - len(den) - (num < den) + 2
- # adj == -1, 0.1 <= self < 1
- num = str(10**-e-c)
- return len(num) + e - (num < "231") - 1
-
- def log10(self, context=None):
- """Returns the base 10 logarithm of self."""
-
- if context is None:
- context = getcontext()
-
- # log10(NaN) = NaN
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- # log10(0.0) == -Infinity
- if not self:
- return _NegativeInfinity
-
- # log10(Infinity) = Infinity
- if self._isinfinity() == 1:
- return _Infinity
-
- # log10(negative or -Infinity) raises InvalidOperation
- if self._sign == 1:
- return context._raise_error(InvalidOperation,
- 'log10 of a negative value')
-
- # log10(10**n) = n
- if self._int[0] == '1' and self._int[1:] == '0'*(len(self._int) - 1):
- # answer may need rounding
- ans = Decimal(self._exp + len(self._int) - 1)
- else:
- # result is irrational, so necessarily inexact
- op = _WorkRep(self)
- c, e = op.int, op.exp
- p = context.prec
-
- # correctly rounded result: repeatedly increase precision
- # until result is unambiguously roundable
- places = p-self._log10_exp_bound()+2
- while True:
- coeff = _dlog10(c, e, places)
- # assert len(str(abs(coeff)))-p >= 1
- if coeff % (5*10**(len(str(abs(coeff)))-p-1)):
- break
- places += 3
- ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places)
-
- context = context._shallow_copy()
- rounding = context._set_rounding(ROUND_HALF_EVEN)
- ans = ans._fix(context)
- context.rounding = rounding
- return ans
-
- def logb(self, context=None):
- """ Returns the exponent of the magnitude of self's MSD.
-
- The result is the integer which is the exponent of the magnitude
- of the most significant digit of self (as though it were truncated
- to a single digit while maintaining the value of that digit and
- without limiting the resulting exponent).
- """
- # logb(NaN) = NaN
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- if context is None:
- context = getcontext()
-
- # logb(+/-Inf) = +Inf
- if self._isinfinity():
- return _Infinity
-
- # logb(0) = -Inf, DivisionByZero
- if not self:
- return context._raise_error(DivisionByZero, 'logb(0)', 1)
-
- # otherwise, simply return the adjusted exponent of self, as a
- # Decimal. Note that no attempt is made to fit the result
- # into the current context.
- ans = Decimal(self.adjusted())
- return ans._fix(context)
-
- def _islogical(self):
- """Return True if self is a logical operand.
-
- For being logical, it must be a finite number with a sign of 0,
- an exponent of 0, and a coefficient whose digits must all be
- either 0 or 1.
- """
- if self._sign != 0 or self._exp != 0:
- return False
- for dig in self._int:
- if dig not in '01':
- return False
- return True
-
- def _fill_logical(self, context, opa, opb):
- dif = context.prec - len(opa)
- if dif > 0:
- opa = '0'*dif + opa
- elif dif < 0:
- opa = opa[-context.prec:]
- dif = context.prec - len(opb)
- if dif > 0:
- opb = '0'*dif + opb
- elif dif < 0:
- opb = opb[-context.prec:]
- return opa, opb
-
- def logical_and(self, other, context=None):
- """Applies an 'and' operation between self and other's digits."""
- if context is None:
- context = getcontext()
-
- other = _convert_other(other, raiseit=True)
-
- if not self._islogical() or not other._islogical():
- return context._raise_error(InvalidOperation)
-
- # fill to context.prec
- (opa, opb) = self._fill_logical(context, self._int, other._int)
-
- # make the operation, and clean starting zeroes
- result = "".join([str(int(a)&int(b)) for a,b in zip(opa,opb)])
- return _dec_from_triple(0, result.lstrip('0') or '0', 0)
-
- def logical_invert(self, context=None):
- """Invert all its digits."""
- if context is None:
- context = getcontext()
- return self.logical_xor(_dec_from_triple(0,'1'*context.prec,0),
- context)
-
- def logical_or(self, other, context=None):
- """Applies an 'or' operation between self and other's digits."""
- if context is None:
- context = getcontext()
-
- other = _convert_other(other, raiseit=True)
-
- if not self._islogical() or not other._islogical():
- return context._raise_error(InvalidOperation)
-
- # fill to context.prec
- (opa, opb) = self._fill_logical(context, self._int, other._int)
-
- # make the operation, and clean starting zeroes
- result = "".join([str(int(a)|int(b)) for a,b in zip(opa,opb)])
- return _dec_from_triple(0, result.lstrip('0') or '0', 0)
-
- def logical_xor(self, other, context=None):
- """Applies an 'xor' operation between self and other's digits."""
- if context is None:
- context = getcontext()
-
- other = _convert_other(other, raiseit=True)
-
- if not self._islogical() or not other._islogical():
- return context._raise_error(InvalidOperation)
-
- # fill to context.prec
- (opa, opb) = self._fill_logical(context, self._int, other._int)
-
- # make the operation, and clean starting zeroes
- result = "".join([str(int(a)^int(b)) for a,b in zip(opa,opb)])
- return _dec_from_triple(0, result.lstrip('0') or '0', 0)
-
- def max_mag(self, other, context=None):
- """Compares the values numerically with their sign ignored."""
- other = _convert_other(other, raiseit=True)
-
- if context is None:
- context = getcontext()
-
- if self._is_special or other._is_special:
- # If one operand is a quiet NaN and the other is number, then the
- # number is always returned
- sn = self._isnan()
- on = other._isnan()
- if sn or on:
- if on == 1 and sn == 0:
- return self._fix(context)
- if sn == 1 and on == 0:
- return other._fix(context)
- return self._check_nans(other, context)
-
- c = self.copy_abs()._cmp(other.copy_abs())
- if c == 0:
- c = self.compare_total(other)
-
- if c == -1:
- ans = other
- else:
- ans = self
-
- return ans._fix(context)
-
- def min_mag(self, other, context=None):
- """Compares the values numerically with their sign ignored."""
- other = _convert_other(other, raiseit=True)
-
- if context is None:
- context = getcontext()
-
- if self._is_special or other._is_special:
- # If one operand is a quiet NaN and the other is number, then the
- # number is always returned
- sn = self._isnan()
- on = other._isnan()
- if sn or on:
- if on == 1 and sn == 0:
- return self._fix(context)
- if sn == 1 and on == 0:
- return other._fix(context)
- return self._check_nans(other, context)
-
- c = self.copy_abs()._cmp(other.copy_abs())
- if c == 0:
- c = self.compare_total(other)
-
- if c == -1:
- ans = self
- else:
- ans = other
-
- return ans._fix(context)
-
- def next_minus(self, context=None):
- """Returns the largest representable number smaller than itself."""
- if context is None:
- context = getcontext()
-
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- if self._isinfinity() == -1:
- return _NegativeInfinity
- if self._isinfinity() == 1:
- return _dec_from_triple(0, '9'*context.prec, context.Etop())
-
- context = context.copy()
- context._set_rounding(ROUND_FLOOR)
- context._ignore_all_flags()
- new_self = self._fix(context)
- if new_self != self:
- return new_self
- return self.__sub__(_dec_from_triple(0, '1', context.Etiny()-1),
- context)
-
- def next_plus(self, context=None):
- """Returns the smallest representable number larger than itself."""
- if context is None:
- context = getcontext()
-
- ans = self._check_nans(context=context)
- if ans:
- return ans
-
- if self._isinfinity() == 1:
- return _Infinity
- if self._isinfinity() == -1:
- return _dec_from_triple(1, '9'*context.prec, context.Etop())
-
- context = context.copy()
- context._set_rounding(ROUND_CEILING)
- context._ignore_all_flags()
- new_self = self._fix(context)
- if new_self != self:
- return new_self
- return self.__add__(_dec_from_triple(0, '1', context.Etiny()-1),
- context)
-
- def next_toward(self, other, context=None):
- """Returns the number closest to self, in the direction towards other.
-
- The result is the closest representable number to self
- (excluding self) that is in the direction towards other,
- unless both have the same value. If the two operands are
- numerically equal, then the result is a copy of self with the
- sign set to be the same as the sign of other.
- """
- other = _convert_other(other, raiseit=True)
-
- if context is None:
- context = getcontext()
-
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- comparison = self._cmp(other)
- if comparison == 0:
- return self.copy_sign(other)
-
- if comparison == -1:
- ans = self.next_plus(context)
- else: # comparison == 1
- ans = self.next_minus(context)
-
- # decide which flags to raise using value of ans
- if ans._isinfinity():
- context._raise_error(Overflow,
- 'Infinite result from next_toward',
- ans._sign)
- context._raise_error(Inexact)
- context._raise_error(Rounded)
- elif ans.adjusted() < context.Emin:
- context._raise_error(Underflow)
- context._raise_error(Subnormal)
- context._raise_error(Inexact)
- context._raise_error(Rounded)
- # if precision == 1 then we don't raise Clamped for a
- # result 0E-Etiny.
- if not ans:
- context._raise_error(Clamped)
-
- return ans
-
- def number_class(self, context=None):
- """Returns an indication of the class of self.
-
- The class is one of the following strings:
- sNaN
- NaN
- -Infinity
- -Normal
- -Subnormal
- -Zero
- +Zero
- +Subnormal
- +Normal
- +Infinity
- """
- if self.is_snan():
- return "sNaN"
- if self.is_qnan():
- return "NaN"
- inf = self._isinfinity()
- if inf == 1:
- return "+Infinity"
- if inf == -1:
- return "-Infinity"
- if self.is_zero():
- if self._sign:
- return "-Zero"
- else:
- return "+Zero"
- if context is None:
- context = getcontext()
- if self.is_subnormal(context=context):
- if self._sign:
- return "-Subnormal"
- else:
- return "+Subnormal"
- # just a normal, regular, boring number, :)
- if self._sign:
- return "-Normal"
- else:
- return "+Normal"
-
- def radix(self):
- """Just returns 10, as this is Decimal, :)"""
- return Decimal(10)
-
- def rotate(self, other, context=None):
- """Returns a rotated copy of self, value-of-other times."""
- if context is None:
- context = getcontext()
-
- other = _convert_other(other, raiseit=True)
-
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- if other._exp != 0:
- return context._raise_error(InvalidOperation)
- if not (-context.prec <= int(other) <= context.prec):
- return context._raise_error(InvalidOperation)
-
- if self._isinfinity():
- return Decimal(self)
-
- # get values, pad if necessary
- torot = int(other)
- rotdig = self._int
- topad = context.prec - len(rotdig)
- if topad > 0:
- rotdig = '0'*topad + rotdig
- elif topad < 0:
- rotdig = rotdig[-topad:]
-
- # let's rotate!
- rotated = rotdig[torot:] + rotdig[:torot]
- return _dec_from_triple(self._sign,
- rotated.lstrip('0') or '0', self._exp)
-
- def scaleb(self, other, context=None):
- """Returns self operand after adding the second value to its exp."""
- if context is None:
- context = getcontext()
-
- other = _convert_other(other, raiseit=True)
-
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- if other._exp != 0:
- return context._raise_error(InvalidOperation)
- liminf = -2 * (context.Emax + context.prec)
- limsup = 2 * (context.Emax + context.prec)
- if not (liminf <= int(other) <= limsup):
- return context._raise_error(InvalidOperation)
-
- if self._isinfinity():
- return Decimal(self)
-
- d = _dec_from_triple(self._sign, self._int, self._exp + int(other))
- d = d._fix(context)
- return d
-
- def shift(self, other, context=None):
- """Returns a shifted copy of self, value-of-other times."""
- if context is None:
- context = getcontext()
-
- other = _convert_other(other, raiseit=True)
-
- ans = self._check_nans(other, context)
- if ans:
- return ans
-
- if other._exp != 0:
- return context._raise_error(InvalidOperation)
- if not (-context.prec <= int(other) <= context.prec):
- return context._raise_error(InvalidOperation)
-
- if self._isinfinity():
- return Decimal(self)
-
- # get values, pad if necessary
- torot = int(other)
- rotdig = self._int
- topad = context.prec - len(rotdig)
- if topad > 0:
- rotdig = '0'*topad + rotdig
- elif topad < 0:
- rotdig = rotdig[-topad:]
-
- # let's shift!
- if torot < 0:
- shifted = rotdig[:torot]
- else:
- shifted = rotdig + '0'*torot
- shifted = shifted[-context.prec:]
-
- return _dec_from_triple(self._sign,
- shifted.lstrip('0') or '0', self._exp)
-
- # Support for pickling, copy, and deepcopy
- def __reduce__(self):
- return (self.__class__, (str(self),))
-
- def __copy__(self):
- if type(self) is Decimal:
- return self # I'm immutable; therefore I am my own clone
- return self.__class__(str(self))
-
- def __deepcopy__(self, memo):
- if type(self) is Decimal:
- return self # My components are also immutable
- return self.__class__(str(self))
-
- # PEP 3101 support. the _localeconv keyword argument should be
- # considered private: it's provided for ease of testing only.
- def __format__(self, specifier, context=None, _localeconv=None):
- """Format a Decimal instance according to the given specifier.
-
- The specifier should be a standard format specifier, with the
- form described in PEP 3101. Formatting types 'e', 'E', 'f',
- 'F', 'g', 'G', 'n' and '%' are supported. If the formatting
- type is omitted it defaults to 'g' or 'G', depending on the
- value of context.capitals.
- """
-
- # Note: PEP 3101 says that if the type is not present then
- # there should be at least one digit after the decimal point.
- # We take the liberty of ignoring this requirement for
- # Decimal---it's presumably there to make sure that
- # format(float, '') behaves similarly to str(float).
- if context is None:
- context = getcontext()
-
- spec = _parse_format_specifier(specifier, _localeconv=_localeconv)
-
- # special values don't care about the type or precision
- if self._is_special:
- sign = _format_sign(self._sign, spec)
- body = str(self.copy_abs())
- if spec['type'] == '%':
- body += '%'
- return _format_align(sign, body, spec)
-
- # a type of None defaults to 'g' or 'G', depending on context
- if spec['type'] is None:
- spec['type'] = ['g', 'G'][context.capitals]
-
- # if type is '%', adjust exponent of self accordingly
- if spec['type'] == '%':
- self = _dec_from_triple(self._sign, self._int, self._exp+2)
-
- # round if necessary, taking rounding mode from the context
- rounding = context.rounding
- precision = spec['precision']
- if precision is not None:
- if spec['type'] in 'eE':
- self = self._round(precision+1, rounding)
- elif spec['type'] in 'fF%':
- self = self._rescale(-precision, rounding)
- elif spec['type'] in 'gG' and len(self._int) > precision:
- self = self._round(precision, rounding)
- # special case: zeros with a positive exponent can't be
- # represented in fixed point; rescale them to 0e0.
- if not self and self._exp > 0 and spec['type'] in 'fF%':
- self = self._rescale(0, rounding)
-
- # figure out placement of the decimal point
- leftdigits = self._exp + len(self._int)
- if spec['type'] in 'eE':
- if not self and precision is not None:
- dotplace = 1 - precision
- else:
- dotplace = 1
- elif spec['type'] in 'fF%':
- dotplace = leftdigits
- elif spec['type'] in 'gG':
- if self._exp <= 0 and leftdigits > -6:
- dotplace = leftdigits
- else:
- dotplace = 1
-
- # find digits before and after decimal point, and get exponent
- if dotplace < 0:
- intpart = '0'
- fracpart = '0'*(-dotplace) + self._int
- elif dotplace > len(self._int):
- intpart = self._int + '0'*(dotplace-len(self._int))
- fracpart = ''
- else:
- intpart = self._int[:dotplace] or '0'
- fracpart = self._int[dotplace:]
- exp = leftdigits-dotplace
-
- # done with the decimal-specific stuff; hand over the rest
- # of the formatting to the _format_number function
- return _format_number(self._sign, intpart, fracpart, exp, spec)
-
-def _dec_from_triple(sign, coefficient, exponent, special=False):
- """Create a decimal instance directly, without any validation,
- normalization (e.g. removal of leading zeros) or argument
- conversion.
-
- This function is for *internal use only*.
- """
-
- self = object.__new__(Decimal)
- self._sign = sign
- self._int = coefficient
- self._exp = exponent
- self._is_special = special
-
- return self
-
-# Register Decimal as a kind of Number (an abstract base class).
-# However, do not register it as Real (because Decimals are not
-# interoperable with floats).
-_numbers.Number.register(Decimal)
-
-
-##### Context class #######################################################
-
-class _ContextManager(object):
- """Context manager class to support localcontext().
-
- Sets a copy of the supplied context in __enter__() and restores
- the previous decimal context in __exit__()
- """
- def __init__(self, new_context):
- self.new_context = new_context.copy()
- def __enter__(self):
- self.saved_context = getcontext()
- setcontext(self.new_context)
- return self.new_context
- def __exit__(self, t, v, tb):
- setcontext(self.saved_context)
-
-class Context(object):
- """Contains the context for a Decimal instance.
-
- Contains:
- prec - precision (for use in rounding, division, square roots..)
- rounding - rounding type (how you round)
- traps - If traps[exception] = 1, then the exception is
- raised when it is caused. Otherwise, a value is
- substituted in.
- flags - When an exception is caused, flags[exception] is set.
- (Whether or not the trap_enabler is set)
- Should be reset by user of Decimal instance.
- Emin - Minimum exponent
- Emax - Maximum exponent
- capitals - If 1, 1*10^1 is printed as 1E+1.
- If 0, printed as 1e1
- clamp - If 1, change exponents if too high (Default 0)
- """
-
- def __init__(self, prec=None, rounding=None, Emin=None, Emax=None,
- capitals=None, clamp=None, flags=None, traps=None,
- _ignored_flags=None):
- # Set defaults; for everything except flags and _ignored_flags,
- # inherit from DefaultContext.
- try:
- dc = DefaultContext
- except NameError:
- pass
-
- self.prec = prec if prec is not None else dc.prec
- self.rounding = rounding if rounding is not None else dc.rounding
- self.Emin = Emin if Emin is not None else dc.Emin
- self.Emax = Emax if Emax is not None else dc.Emax
- self.capitals = capitals if capitals is not None else dc.capitals
- self.clamp = clamp if clamp is not None else dc.clamp
-
- if _ignored_flags is None:
- self._ignored_flags = []
- else:
- self._ignored_flags = _ignored_flags
-
- if traps is None:
- self.traps = dc.traps.copy()
- elif not isinstance(traps, dict):
- self.traps = dict((s, int(s in traps)) for s in _signals + traps)
- else:
- self.traps = traps
-
- if flags is None:
- self.flags = dict.fromkeys(_signals, 0)
- elif not isinstance(flags, dict):
- self.flags = dict((s, int(s in flags)) for s in _signals + flags)
- else:
- self.flags = flags
-
- def _set_integer_check(self, name, value, vmin, vmax):
- if not isinstance(value, int):
- raise TypeError("%s must be an integer" % name)
- if vmin == '-inf':
- if value > vmax:
- raise ValueError("%s must be in [%s, %d]. got: %s" % (name, vmin, vmax, value))
- elif vmax == 'inf':
- if value < vmin:
- raise ValueError("%s must be in [%d, %s]. got: %s" % (name, vmin, vmax, value))
- else:
- if value < vmin or value > vmax:
- raise ValueError("%s must be in [%d, %d]. got %s" % (name, vmin, vmax, value))
- return object.__setattr__(self, name, value)
-
- def _set_signal_dict(self, name, d):
- if not isinstance(d, dict):
- raise TypeError("%s must be a signal dict" % d)
- for key in d:
- if not key in _signals:
- raise KeyError("%s is not a valid signal dict" % d)
- for key in _signals:
- if not key in d:
- raise KeyError("%s is not a valid signal dict" % d)
- return object.__setattr__(self, name, d)
-
- def __setattr__(self, name, value):
- if name == 'prec':
- return self._set_integer_check(name, value, 1, 'inf')
- elif name == 'Emin':
- return self._set_integer_check(name, value, '-inf', 0)
- elif name == 'Emax':
- return self._set_integer_check(name, value, 0, 'inf')
- elif name == 'capitals':
- return self._set_integer_check(name, value, 0, 1)
- elif name == 'clamp':
- return self._set_integer_check(name, value, 0, 1)
- elif name == 'rounding':
- if not value in _rounding_modes:
- # raise TypeError even for strings to have consistency
- # among various implementations.
- raise TypeError("%s: invalid rounding mode" % value)
- return object.__setattr__(self, name, value)
- elif name == 'flags' or name == 'traps':
- return self._set_signal_dict(name, value)
- elif name == '_ignored_flags':
- return object.__setattr__(self, name, value)
- else:
- raise AttributeError(
- "'decimal.Context' object has no attribute '%s'" % name)
-
- def __delattr__(self, name):
- raise AttributeError("%s cannot be deleted" % name)
-
- # Support for pickling, copy, and deepcopy
- def __reduce__(self):
- flags = [sig for sig, v in self.flags.items() if v]
- traps = [sig for sig, v in self.traps.items() if v]
- return (self.__class__,
- (self.prec, self.rounding, self.Emin, self.Emax,
- self.capitals, self.clamp, flags, traps))
-
- def __repr__(self):
- """Show the current context."""
- s = []
- s.append('Context(prec=%(prec)d, rounding=%(rounding)s, '
- 'Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d, '
- 'clamp=%(clamp)d'
- % vars(self))
- names = [f.__name__ for f, v in self.flags.items() if v]
- s.append('flags=[' + ', '.join(names) + ']')
- names = [t.__name__ for t, v in self.traps.items() if v]
- s.append('traps=[' + ', '.join(names) + ']')
- return ', '.join(s) + ')'
-
- def clear_flags(self):
- """Reset all flags to zero"""
- for flag in self.flags:
- self.flags[flag] = 0
-
- def clear_traps(self):
- """Reset all traps to zero"""
- for flag in self.traps:
- self.traps[flag] = 0
-
- def _shallow_copy(self):
- """Returns a shallow copy from self."""
- nc = Context(self.prec, self.rounding, self.Emin, self.Emax,
- self.capitals, self.clamp, self.flags, self.traps,
- self._ignored_flags)
- return nc
-
- def copy(self):
- """Returns a deep copy from self."""
- nc = Context(self.prec, self.rounding, self.Emin, self.Emax,
- self.capitals, self.clamp,
- self.flags.copy(), self.traps.copy(),
- self._ignored_flags)
- return nc
- __copy__ = copy
-
- def _raise_error(self, condition, explanation = None, *args):
- """Handles an error
-
- If the flag is in _ignored_flags, returns the default response.
- Otherwise, it sets the flag, then, if the corresponding
- trap_enabler is set, it reraises the exception. Otherwise, it returns
- the default value after setting the flag.
- """
- error = _condition_map.get(condition, condition)
- if error in self._ignored_flags:
- # Don't touch the flag
- return error().handle(self, *args)
-
- self.flags[error] = 1
- if not self.traps[error]:
- # The errors define how to handle themselves.
- return condition().handle(self, *args)
-
- # Errors should only be risked on copies of the context
- # self._ignored_flags = []
- raise error(explanation)
-
- def _ignore_all_flags(self):
- """Ignore all flags, if they are raised"""
- return self._ignore_flags(*_signals)
-
- def _ignore_flags(self, *flags):
- """Ignore the flags, if they are raised"""
- # Do not mutate-- This way, copies of a context leave the original
- # alone.
- self._ignored_flags = (self._ignored_flags + list(flags))
- return list(flags)
-
- def _regard_flags(self, *flags):
- """Stop ignoring the flags, if they are raised"""
- if flags and isinstance(flags[0], (tuple,list)):
- flags = flags[0]
- for flag in flags:
- self._ignored_flags.remove(flag)
-
- # We inherit object.__hash__, so we must deny this explicitly
- __hash__ = None
-
- def Etiny(self):
- """Returns Etiny (= Emin - prec + 1)"""
- return int(self.Emin - self.prec + 1)
-
- def Etop(self):
- """Returns maximum exponent (= Emax - prec + 1)"""
- return int(self.Emax - self.prec + 1)
-
- def _set_rounding(self, type):
- """Sets the rounding type.
-
- Sets the rounding type, and returns the current (previous)
- rounding type. Often used like:
-
- context = context.copy()
- # so you don't change the calling context
- # if an error occurs in the middle.
- rounding = context._set_rounding(ROUND_UP)
- val = self.__sub__(other, context=context)
- context._set_rounding(rounding)
-
- This will make it round up for that operation.
- """
- rounding = self.rounding
- self.rounding= type
- return rounding
-
- def create_decimal(self, num='0'):
- """Creates a new Decimal instance but using self as context.
-
- This method implements the to-number operation of the
- IBM Decimal specification."""
-
- if isinstance(num, str) and num != num.strip():
- return self._raise_error(ConversionSyntax,
- "no trailing or leading whitespace is "
- "permitted.")
-
- d = Decimal(num, context=self)
- if d._isnan() and len(d._int) > self.prec - self.clamp:
- return self._raise_error(ConversionSyntax,
- "diagnostic info too long in NaN")
- return d._fix(self)
-
- def create_decimal_from_float(self, f):
- """Creates a new Decimal instance from a float but rounding using self
- as the context.
-
- >>> context = Context(prec=5, rounding=ROUND_DOWN)
- >>> context.create_decimal_from_float(3.1415926535897932)
- Decimal('3.1415')
- >>> context = Context(prec=5, traps=[Inexact])
- >>> context.create_decimal_from_float(3.1415926535897932)
- Traceback (most recent call last):
- ...
- decimal.Inexact: None
-
- """
- d = Decimal.from_float(f) # An exact conversion
- return d._fix(self) # Apply the context rounding
-
- # Methods
- def abs(self, a):
- """Returns the absolute value of the operand.
-
- If the operand is negative, the result is the same as using the minus
- operation on the operand. Otherwise, the result is the same as using
- the plus operation on the operand.
-
- >>> ExtendedContext.abs(Decimal('2.1'))
- Decimal('2.1')
- >>> ExtendedContext.abs(Decimal('-100'))
- Decimal('100')
- >>> ExtendedContext.abs(Decimal('101.5'))
- Decimal('101.5')
- >>> ExtendedContext.abs(Decimal('-101.5'))
- Decimal('101.5')
- >>> ExtendedContext.abs(-1)
- Decimal('1')
- """
- a = _convert_other(a, raiseit=True)
- return a.__abs__(context=self)
-
- def add(self, a, b):
- """Return the sum of the two operands.
-
- >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
- Decimal('19.00')
- >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
- Decimal('1.02E+4')
- >>> ExtendedContext.add(1, Decimal(2))
- Decimal('3')
- >>> ExtendedContext.add(Decimal(8), 5)
- Decimal('13')
- >>> ExtendedContext.add(5, 5)
- Decimal('10')
- """
- a = _convert_other(a, raiseit=True)
- r = a.__add__(b, context=self)
- if r is NotImplemented:
- raise TypeError("Unable to convert %s to Decimal" % b)
- else:
- return r
-
- def _apply(self, a):
- return str(a._fix(self))
-
- def canonical(self, a):
- """Returns the same Decimal object.
-
- As we do not have different encodings for the same number, the
- received object already is in its canonical form.
-
- >>> ExtendedContext.canonical(Decimal('2.50'))
- Decimal('2.50')
- """
- if not isinstance(a, Decimal):
- raise TypeError("canonical requires a Decimal as an argument.")
- return a.canonical()
-
- def compare(self, a, b):
- """Compares values numerically.
-
- If the signs of the operands differ, a value representing each operand
- ('-1' if the operand is less than zero, '0' if the operand is zero or
- negative zero, or '1' if the operand is greater than zero) is used in
- place of that operand for the comparison instead of the actual
- operand.
-
- The comparison is then effected by subtracting the second operand from
- the first and then returning a value according to the result of the
- subtraction: '-1' if the result is less than zero, '0' if the result is
- zero or negative zero, or '1' if the result is greater than zero.
-
- >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
- Decimal('-1')
- >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
- Decimal('0')
- >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
- Decimal('0')
- >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
- Decimal('1')
- >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
- Decimal('1')
- >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
- Decimal('-1')
- >>> ExtendedContext.compare(1, 2)
- Decimal('-1')
- >>> ExtendedContext.compare(Decimal(1), 2)
- Decimal('-1')
- >>> ExtendedContext.compare(1, Decimal(2))
- Decimal('-1')
- """
- a = _convert_other(a, raiseit=True)
- return a.compare(b, context=self)
-
- def compare_signal(self, a, b):
- """Compares the values of the two operands numerically.
-
- It's pretty much like compare(), but all NaNs signal, with signaling
- NaNs taking precedence over quiet NaNs.
-
- >>> c = ExtendedContext
- >>> c.compare_signal(Decimal('2.1'), Decimal('3'))
- Decimal('-1')
- >>> c.compare_signal(Decimal('2.1'), Decimal('2.1'))
- Decimal('0')
- >>> c.flags[InvalidOperation] = 0
- >>> print(c.flags[InvalidOperation])
- 0
- >>> c.compare_signal(Decimal('NaN'), Decimal('2.1'))
- Decimal('NaN')
- >>> print(c.flags[InvalidOperation])
- 1
- >>> c.flags[InvalidOperation] = 0
- >>> print(c.flags[InvalidOperation])
- 0
- >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1'))
- Decimal('NaN')
- >>> print(c.flags[InvalidOperation])
- 1
- >>> c.compare_signal(-1, 2)
- Decimal('-1')
- >>> c.compare_signal(Decimal(-1), 2)
- Decimal('-1')
- >>> c.compare_signal(-1, Decimal(2))
- Decimal('-1')
- """
- a = _convert_other(a, raiseit=True)
- return a.compare_signal(b, context=self)
-
- def compare_total(self, a, b):
- """Compares two operands using their abstract representation.
-
- This is not like the standard compare, which use their numerical
- value. Note that a total ordering is defined for all possible abstract
- representations.
-
- >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9'))
- Decimal('-1')
- >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12'))
- Decimal('-1')
- >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3'))
- Decimal('-1')
- >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30'))
- Decimal('0')
- >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300'))
- Decimal('1')
- >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN'))
- Decimal('-1')
- >>> ExtendedContext.compare_total(1, 2)
- Decimal('-1')
- >>> ExtendedContext.compare_total(Decimal(1), 2)
- Decimal('-1')
- >>> ExtendedContext.compare_total(1, Decimal(2))
- Decimal('-1')
- """
- a = _convert_other(a, raiseit=True)
- return a.compare_total(b)
-
- def compare_total_mag(self, a, b):
- """Compares two operands using their abstract representation ignoring sign.
-
- Like compare_total, but with operand's sign ignored and assumed to be 0.
- """
- a = _convert_other(a, raiseit=True)
- return a.compare_total_mag(b)
-
- def copy_abs(self, a):
- """Returns a copy of the operand with the sign set to 0.
-
- >>> ExtendedContext.copy_abs(Decimal('2.1'))
- Decimal('2.1')
- >>> ExtendedContext.copy_abs(Decimal('-100'))
- Decimal('100')
- >>> ExtendedContext.copy_abs(-1)
- Decimal('1')
- """
- a = _convert_other(a, raiseit=True)
- return a.copy_abs()
-
- def copy_decimal(self, a):
- """Returns a copy of the decimal object.
-
- >>> ExtendedContext.copy_decimal(Decimal('2.1'))
- Decimal('2.1')
- >>> ExtendedContext.copy_decimal(Decimal('-1.00'))
- Decimal('-1.00')
- >>> ExtendedContext.copy_decimal(1)
- Decimal('1')
- """
- a = _convert_other(a, raiseit=True)
- return Decimal(a)
-
- def copy_negate(self, a):
- """Returns a copy of the operand with the sign inverted.
-
- >>> ExtendedContext.copy_negate(Decimal('101.5'))
- Decimal('-101.5')
- >>> ExtendedContext.copy_negate(Decimal('-101.5'))
- Decimal('101.5')
- >>> ExtendedContext.copy_negate(1)
- Decimal('-1')
- """
- a = _convert_other(a, raiseit=True)
- return a.copy_negate()
-
- def copy_sign(self, a, b):
- """Copies the second operand's sign to the first one.
-
- In detail, it returns a copy of the first operand with the sign
- equal to the sign of the second operand.
-
- >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33'))
- Decimal('1.50')
- >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33'))
- Decimal('1.50')
- >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33'))
- Decimal('-1.50')
- >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33'))
- Decimal('-1.50')
- >>> ExtendedContext.copy_sign(1, -2)
- Decimal('-1')
- >>> ExtendedContext.copy_sign(Decimal(1), -2)
- Decimal('-1')
- >>> ExtendedContext.copy_sign(1, Decimal(-2))
- Decimal('-1')
- """
- a = _convert_other(a, raiseit=True)
- return a.copy_sign(b)
-
- def divide(self, a, b):
- """Decimal division in a specified context.
-
- >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
- Decimal('0.333333333')
- >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
- Decimal('0.666666667')
- >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
- Decimal('2.5')
- >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
- Decimal('0.1')
- >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
- Decimal('1')
- >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
- Decimal('4.00')
- >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
- Decimal('1.20')
- >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
- Decimal('10')
- >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
- Decimal('1000')
- >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
- Decimal('1.20E+6')
- >>> ExtendedContext.divide(5, 5)
- Decimal('1')
- >>> ExtendedContext.divide(Decimal(5), 5)
- Decimal('1')
- >>> ExtendedContext.divide(5, Decimal(5))
- Decimal('1')
- """
- a = _convert_other(a, raiseit=True)
- r = a.__truediv__(b, context=self)
- if r is NotImplemented:
- raise TypeError("Unable to convert %s to Decimal" % b)
- else:
- return r
-
- def divide_int(self, a, b):
- """Divides two numbers and returns the integer part of the result.
-
- >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
- Decimal('0')
- >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
- Decimal('3')
- >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
- Decimal('3')
- >>> ExtendedContext.divide_int(10, 3)
- Decimal('3')
- >>> ExtendedContext.divide_int(Decimal(10), 3)
- Decimal('3')
- >>> ExtendedContext.divide_int(10, Decimal(3))
- Decimal('3')
- """
- a = _convert_other(a, raiseit=True)
- r = a.__floordiv__(b, context=self)
- if r is NotImplemented:
- raise TypeError("Unable to convert %s to Decimal" % b)
- else:
- return r
-
- def divmod(self, a, b):
- """Return (a // b, a % b).
-
- >>> ExtendedContext.divmod(Decimal(8), Decimal(3))
- (Decimal('2'), Decimal('2'))
- >>> ExtendedContext.divmod(Decimal(8), Decimal(4))
- (Decimal('2'), Decimal('0'))
- >>> ExtendedContext.divmod(8, 4)
- (Decimal('2'), Decimal('0'))
- >>> ExtendedContext.divmod(Decimal(8), 4)
- (Decimal('2'), Decimal('0'))
- >>> ExtendedContext.divmod(8, Decimal(4))
- (Decimal('2'), Decimal('0'))
- """
- a = _convert_other(a, raiseit=True)
- r = a.__divmod__(b, context=self)
- if r is NotImplemented:
- raise TypeError("Unable to convert %s to Decimal" % b)
- else:
- return r
-
- def exp(self, a):
- """Returns e ** a.
-
- >>> c = ExtendedContext.copy()
- >>> c.Emin = -999
- >>> c.Emax = 999
- >>> c.exp(Decimal('-Infinity'))
- Decimal('0')
- >>> c.exp(Decimal('-1'))
- Decimal('0.367879441')
- >>> c.exp(Decimal('0'))
- Decimal('1')
- >>> c.exp(Decimal('1'))
- Decimal('2.71828183')
- >>> c.exp(Decimal('0.693147181'))
- Decimal('2.00000000')
- >>> c.exp(Decimal('+Infinity'))
- Decimal('Infinity')
- >>> c.exp(10)
- Decimal('22026.4658')
- """
- a =_convert_other(a, raiseit=True)
- return a.exp(context=self)
-
- def fma(self, a, b, c):
- """Returns a multiplied by b, plus c.
-
- The first two operands are multiplied together, using multiply,
- the third operand is then added to the result of that
- multiplication, using add, all with only one final rounding.
-
- >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7'))
- Decimal('22')
- >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7'))
- Decimal('-8')
- >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578'))
- Decimal('1.38435736E+12')
- >>> ExtendedContext.fma(1, 3, 4)
- Decimal('7')
- >>> ExtendedContext.fma(1, Decimal(3), 4)
- Decimal('7')
- >>> ExtendedContext.fma(1, 3, Decimal(4))
- Decimal('7')
- """
- a = _convert_other(a, raiseit=True)
- return a.fma(b, c, context=self)
-
- def is_canonical(self, a):
- """Return True if the operand is canonical; otherwise return False.
-
- Currently, the encoding of a Decimal instance is always
- canonical, so this method returns True for any Decimal.
-
- >>> ExtendedContext.is_canonical(Decimal('2.50'))
- True
- """
- if not isinstance(a, Decimal):
- raise TypeError("is_canonical requires a Decimal as an argument.")
- return a.is_canonical()
-
- def is_finite(self, a):
- """Return True if the operand is finite; otherwise return False.
-
- A Decimal instance is considered finite if it is neither
- infinite nor a NaN.
-
- >>> ExtendedContext.is_finite(Decimal('2.50'))
- True
- >>> ExtendedContext.is_finite(Decimal('-0.3'))
- True
- >>> ExtendedContext.is_finite(Decimal('0'))
- True
- >>> ExtendedContext.is_finite(Decimal('Inf'))
- False
- >>> ExtendedContext.is_finite(Decimal('NaN'))
- False
- >>> ExtendedContext.is_finite(1)
- True
- """
- a = _convert_other(a, raiseit=True)
- return a.is_finite()
-
- def is_infinite(self, a):
- """Return True if the operand is infinite; otherwise return False.
-
- >>> ExtendedContext.is_infinite(Decimal('2.50'))
- False
- >>> ExtendedContext.is_infinite(Decimal('-Inf'))
- True
- >>> ExtendedContext.is_infinite(Decimal('NaN'))
- False
- >>> ExtendedContext.is_infinite(1)
- False
- """
- a = _convert_other(a, raiseit=True)
- return a.is_infinite()
-
- def is_nan(self, a):
- """Return True if the operand is a qNaN or sNaN;
- otherwise return False.
-
- >>> ExtendedContext.is_nan(Decimal('2.50'))
- False
- >>> ExtendedContext.is_nan(Decimal('NaN'))
- True
- >>> ExtendedContext.is_nan(Decimal('-sNaN'))
- True
- >>> ExtendedContext.is_nan(1)
- False
- """
- a = _convert_other(a, raiseit=True)
- return a.is_nan()
-
- def is_normal(self, a):
- """Return True if the operand is a normal number;
- otherwise return False.
-
- >>> c = ExtendedContext.copy()
- >>> c.Emin = -999
- >>> c.Emax = 999
- >>> c.is_normal(Decimal('2.50'))
- True
- >>> c.is_normal(Decimal('0.1E-999'))
- False
- >>> c.is_normal(Decimal('0.00'))
- False
- >>> c.is_normal(Decimal('-Inf'))
- False
- >>> c.is_normal(Decimal('NaN'))
- False
- >>> c.is_normal(1)
- True
- """
- a = _convert_other(a, raiseit=True)
- return a.is_normal(context=self)
-
- def is_qnan(self, a):
- """Return True if the operand is a quiet NaN; otherwise return False.
-
- >>> ExtendedContext.is_qnan(Decimal('2.50'))
- False
- >>> ExtendedContext.is_qnan(Decimal('NaN'))
- True
- >>> ExtendedContext.is_qnan(Decimal('sNaN'))
- False
- >>> ExtendedContext.is_qnan(1)
- False
- """
- a = _convert_other(a, raiseit=True)
- return a.is_qnan()
-
- def is_signed(self, a):
- """Return True if the operand is negative; otherwise return False.
-
- >>> ExtendedContext.is_signed(Decimal('2.50'))
- False
- >>> ExtendedContext.is_signed(Decimal('-12'))
- True
- >>> ExtendedContext.is_signed(Decimal('-0'))
- True
- >>> ExtendedContext.is_signed(8)
- False
- >>> ExtendedContext.is_signed(-8)
- True
- """
- a = _convert_other(a, raiseit=True)
- return a.is_signed()
-
- def is_snan(self, a):
- """Return True if the operand is a signaling NaN;
- otherwise return False.
-
- >>> ExtendedContext.is_snan(Decimal('2.50'))
- False
- >>> ExtendedContext.is_snan(Decimal('NaN'))
- False
- >>> ExtendedContext.is_snan(Decimal('sNaN'))
- True
- >>> ExtendedContext.is_snan(1)
- False
- """
- a = _convert_other(a, raiseit=True)
- return a.is_snan()
-
- def is_subnormal(self, a):
- """Return True if the operand is subnormal; otherwise return False.
-
- >>> c = ExtendedContext.copy()
- >>> c.Emin = -999
- >>> c.Emax = 999
- >>> c.is_subnormal(Decimal('2.50'))
- False
- >>> c.is_subnormal(Decimal('0.1E-999'))
- True
- >>> c.is_subnormal(Decimal('0.00'))
- False
- >>> c.is_subnormal(Decimal('-Inf'))
- False
- >>> c.is_subnormal(Decimal('NaN'))
- False
- >>> c.is_subnormal(1)
- False
- """
- a = _convert_other(a, raiseit=True)
- return a.is_subnormal(context=self)
-
- def is_zero(self, a):
- """Return True if the operand is a zero; otherwise return False.
-
- >>> ExtendedContext.is_zero(Decimal('0'))
- True
- >>> ExtendedContext.is_zero(Decimal('2.50'))
- False
- >>> ExtendedContext.is_zero(Decimal('-0E+2'))
- True
- >>> ExtendedContext.is_zero(1)
- False
- >>> ExtendedContext.is_zero(0)
- True
- """
- a = _convert_other(a, raiseit=True)
- return a.is_zero()
-
- def ln(self, a):
- """Returns the natural (base e) logarithm of the operand.
-
- >>> c = ExtendedContext.copy()
- >>> c.Emin = -999
- >>> c.Emax = 999
- >>> c.ln(Decimal('0'))
- Decimal('-Infinity')
- >>> c.ln(Decimal('1.000'))
- Decimal('0')
- >>> c.ln(Decimal('2.71828183'))
- Decimal('1.00000000')
- >>> c.ln(Decimal('10'))
- Decimal('2.30258509')
- >>> c.ln(Decimal('+Infinity'))
- Decimal('Infinity')
- >>> c.ln(1)
- Decimal('0')
- """
- a = _convert_other(a, raiseit=True)
- return a.ln(context=self)
-
- def log10(self, a):
- """Returns the base 10 logarithm of the operand.
-
- >>> c = ExtendedContext.copy()
- >>> c.Emin = -999
- >>> c.Emax = 999
- >>> c.log10(Decimal('0'))
- Decimal('-Infinity')
- >>> c.log10(Decimal('0.001'))
- Decimal('-3')
- >>> c.log10(Decimal('1.000'))
- Decimal('0')
- >>> c.log10(Decimal('2'))
- Decimal('0.301029996')
- >>> c.log10(Decimal('10'))
- Decimal('1')
- >>> c.log10(Decimal('70'))
- Decimal('1.84509804')
- >>> c.log10(Decimal('+Infinity'))
- Decimal('Infinity')
- >>> c.log10(0)
- Decimal('-Infinity')
- >>> c.log10(1)
- Decimal('0')
- """
- a = _convert_other(a, raiseit=True)
- return a.log10(context=self)
-
- def logb(self, a):
- """ Returns the exponent of the magnitude of the operand's MSD.
-
- The result is the integer which is the exponent of the magnitude
- of the most significant digit of the operand (as though the
- operand were truncated to a single digit while maintaining the
- value of that digit and without limiting the resulting exponent).
-
- >>> ExtendedContext.logb(Decimal('250'))
- Decimal('2')
- >>> ExtendedContext.logb(Decimal('2.50'))
- Decimal('0')
- >>> ExtendedContext.logb(Decimal('0.03'))
- Decimal('-2')
- >>> ExtendedContext.logb(Decimal('0'))
- Decimal('-Infinity')
- >>> ExtendedContext.logb(1)
- Decimal('0')
- >>> ExtendedContext.logb(10)
- Decimal('1')
- >>> ExtendedContext.logb(100)
- Decimal('2')
- """
- a = _convert_other(a, raiseit=True)
- return a.logb(context=self)
-
- def logical_and(self, a, b):
- """Applies the logical operation 'and' between each operand's digits.
-
- The operands must be both logical numbers.
-
- >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
- Decimal('0')
- >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1'))
- Decimal('0')
- >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0'))
- Decimal('0')
- >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1'))
- Decimal('1')
- >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010'))
- Decimal('1000')
- >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10'))
- Decimal('10')
- >>> ExtendedContext.logical_and(110, 1101)
- Decimal('100')
- >>> ExtendedContext.logical_and(Decimal(110), 1101)
- Decimal('100')
- >>> ExtendedContext.logical_and(110, Decimal(1101))
- Decimal('100')
- """
- a = _convert_other(a, raiseit=True)
- return a.logical_and(b, context=self)
-
- def logical_invert(self, a):
- """Invert all the digits in the operand.
-
- The operand must be a logical number.
-
- >>> ExtendedContext.logical_invert(Decimal('0'))
- Decimal('111111111')
- >>> ExtendedContext.logical_invert(Decimal('1'))
- Decimal('111111110')
- >>> ExtendedContext.logical_invert(Decimal('111111111'))
- Decimal('0')
- >>> ExtendedContext.logical_invert(Decimal('101010101'))
- Decimal('10101010')
- >>> ExtendedContext.logical_invert(1101)
- Decimal('111110010')
- """
- a = _convert_other(a, raiseit=True)
- return a.logical_invert(context=self)
-
- def logical_or(self, a, b):
- """Applies the logical operation 'or' between each operand's digits.
-
- The operands must be both logical numbers.
-
- >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
- Decimal('0')
- >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1'))
- Decimal('1')
- >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0'))
- Decimal('1')
- >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1'))
- Decimal('1')
- >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010'))
- Decimal('1110')
- >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10'))
- Decimal('1110')
- >>> ExtendedContext.logical_or(110, 1101)
- Decimal('1111')
- >>> ExtendedContext.logical_or(Decimal(110), 1101)
- Decimal('1111')
- >>> ExtendedContext.logical_or(110, Decimal(1101))
- Decimal('1111')
- """
- a = _convert_other(a, raiseit=True)
- return a.logical_or(b, context=self)
-
- def logical_xor(self, a, b):
- """Applies the logical operation 'xor' between each operand's digits.
-
- The operands must be both logical numbers.
-
- >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
- Decimal('0')
- >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1'))
- Decimal('1')
- >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0'))
- Decimal('1')
- >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1'))
- Decimal('0')
- >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010'))
- Decimal('110')
- >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10'))
- Decimal('1101')
- >>> ExtendedContext.logical_xor(110, 1101)
- Decimal('1011')
- >>> ExtendedContext.logical_xor(Decimal(110), 1101)
- Decimal('1011')
- >>> ExtendedContext.logical_xor(110, Decimal(1101))
- Decimal('1011')
- """
- a = _convert_other(a, raiseit=True)
- return a.logical_xor(b, context=self)
-
- def max(self, a, b):
- """max compares two values numerically and returns the maximum.
-
- If either operand is a NaN then the general rules apply.
- Otherwise, the operands are compared as though by the compare
- operation. If they are numerically equal then the left-hand operand
- is chosen as the result. Otherwise the maximum (closer to positive
- infinity) of the two operands is chosen as the result.
-
- >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
- Decimal('3')
- >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
- Decimal('3')
- >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
- Decimal('1')
- >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
- Decimal('7')
- >>> ExtendedContext.max(1, 2)
- Decimal('2')
- >>> ExtendedContext.max(Decimal(1), 2)
- Decimal('2')
- >>> ExtendedContext.max(1, Decimal(2))
- Decimal('2')
- """
- a = _convert_other(a, raiseit=True)
- return a.max(b, context=self)
-
- def max_mag(self, a, b):
- """Compares the values numerically with their sign ignored.
-
- >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN'))
- Decimal('7')
- >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10'))
- Decimal('-10')
- >>> ExtendedContext.max_mag(1, -2)
- Decimal('-2')
- >>> ExtendedContext.max_mag(Decimal(1), -2)
- Decimal('-2')
- >>> ExtendedContext.max_mag(1, Decimal(-2))
- Decimal('-2')
- """
- a = _convert_other(a, raiseit=True)
- return a.max_mag(b, context=self)
-
- def min(self, a, b):
- """min compares two values numerically and returns the minimum.
-
- If either operand is a NaN then the general rules apply.
- Otherwise, the operands are compared as though by the compare
- operation. If they are numerically equal then the left-hand operand
- is chosen as the result. Otherwise the minimum (closer to negative
- infinity) of the two operands is chosen as the result.
-
- >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
- Decimal('2')
- >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
- Decimal('-10')
- >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
- Decimal('1.0')
- >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
- Decimal('7')
- >>> ExtendedContext.min(1, 2)
- Decimal('1')
- >>> ExtendedContext.min(Decimal(1), 2)
- Decimal('1')
- >>> ExtendedContext.min(1, Decimal(29))
- Decimal('1')
- """
- a = _convert_other(a, raiseit=True)
- return a.min(b, context=self)
-
- def min_mag(self, a, b):
- """Compares the values numerically with their sign ignored.
-
- >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2'))
- Decimal('-2')
- >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN'))
- Decimal('-3')
- >>> ExtendedContext.min_mag(1, -2)
- Decimal('1')
- >>> ExtendedContext.min_mag(Decimal(1), -2)
- Decimal('1')
- >>> ExtendedContext.min_mag(1, Decimal(-2))
- Decimal('1')
- """
- a = _convert_other(a, raiseit=True)
- return a.min_mag(b, context=self)
-
- def minus(self, a):
- """Minus corresponds to unary prefix minus in Python.
-
- The operation is evaluated using the same rules as subtract; the
- operation minus(a) is calculated as subtract('0', a) where the '0'
- has the same exponent as the operand.
-
- >>> ExtendedContext.minus(Decimal('1.3'))
- Decimal('-1.3')
- >>> ExtendedContext.minus(Decimal('-1.3'))
- Decimal('1.3')
- >>> ExtendedContext.minus(1)
- Decimal('-1')
- """
- a = _convert_other(a, raiseit=True)
- return a.__neg__(context=self)
-
- def multiply(self, a, b):
- """multiply multiplies two operands.
-
- If either operand is a special value then the general rules apply.
- Otherwise, the operands are multiplied together
- ('long multiplication'), resulting in a number which may be as long as
- the sum of the lengths of the two operands.
-
- >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
- Decimal('3.60')
- >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
- Decimal('21')
- >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
- Decimal('0.72')
- >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
- Decimal('-0.0')
- >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
- Decimal('4.28135971E+11')
- >>> ExtendedContext.multiply(7, 7)
- Decimal('49')
- >>> ExtendedContext.multiply(Decimal(7), 7)
- Decimal('49')
- >>> ExtendedContext.multiply(7, Decimal(7))
- Decimal('49')
- """
- a = _convert_other(a, raiseit=True)
- r = a.__mul__(b, context=self)
- if r is NotImplemented:
- raise TypeError("Unable to convert %s to Decimal" % b)
- else:
- return r
-
- def next_minus(self, a):
- """Returns the largest representable number smaller than a.
-
- >>> c = ExtendedContext.copy()
- >>> c.Emin = -999
- >>> c.Emax = 999
- >>> ExtendedContext.next_minus(Decimal('1'))
- Decimal('0.999999999')
- >>> c.next_minus(Decimal('1E-1007'))
- Decimal('0E-1007')
- >>> ExtendedContext.next_minus(Decimal('-1.00000003'))
- Decimal('-1.00000004')
- >>> c.next_minus(Decimal('Infinity'))
- Decimal('9.99999999E+999')
- >>> c.next_minus(1)
- Decimal('0.999999999')
- """
- a = _convert_other(a, raiseit=True)
- return a.next_minus(context=self)
-
- def next_plus(self, a):
- """Returns the smallest representable number larger than a.
-
- >>> c = ExtendedContext.copy()
- >>> c.Emin = -999
- >>> c.Emax = 999
- >>> ExtendedContext.next_plus(Decimal('1'))
- Decimal('1.00000001')
- >>> c.next_plus(Decimal('-1E-1007'))
- Decimal('-0E-1007')
- >>> ExtendedContext.next_plus(Decimal('-1.00000003'))
- Decimal('-1.00000002')
- >>> c.next_plus(Decimal('-Infinity'))
- Decimal('-9.99999999E+999')
- >>> c.next_plus(1)
- Decimal('1.00000001')
- """
- a = _convert_other(a, raiseit=True)
- return a.next_plus(context=self)
-
- def next_toward(self, a, b):
- """Returns the number closest to a, in direction towards b.
-
- The result is the closest representable number from the first
- operand (but not the first operand) that is in the direction
- towards the second operand, unless the operands have the same
- value.
-
- >>> c = ExtendedContext.copy()
- >>> c.Emin = -999
- >>> c.Emax = 999
- >>> c.next_toward(Decimal('1'), Decimal('2'))
- Decimal('1.00000001')
- >>> c.next_toward(Decimal('-1E-1007'), Decimal('1'))
- Decimal('-0E-1007')
- >>> c.next_toward(Decimal('-1.00000003'), Decimal('0'))
- Decimal('-1.00000002')
- >>> c.next_toward(Decimal('1'), Decimal('0'))
- Decimal('0.999999999')
- >>> c.next_toward(Decimal('1E-1007'), Decimal('-100'))
- Decimal('0E-1007')
- >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10'))
- Decimal('-1.00000004')
- >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000'))
- Decimal('-0.00')
- >>> c.next_toward(0, 1)
- Decimal('1E-1007')
- >>> c.next_toward(Decimal(0), 1)
- Decimal('1E-1007')
- >>> c.next_toward(0, Decimal(1))
- Decimal('1E-1007')
- """
- a = _convert_other(a, raiseit=True)
- return a.next_toward(b, context=self)
-
- def normalize(self, a):
- """normalize reduces an operand to its simplest form.
-
- Essentially a plus operation with all trailing zeros removed from the
- result.
-
- >>> ExtendedContext.normalize(Decimal('2.1'))
- Decimal('2.1')
- >>> ExtendedContext.normalize(Decimal('-2.0'))
- Decimal('-2')
- >>> ExtendedContext.normalize(Decimal('1.200'))
- Decimal('1.2')
- >>> ExtendedContext.normalize(Decimal('-120'))
- Decimal('-1.2E+2')
- >>> ExtendedContext.normalize(Decimal('120.00'))
- Decimal('1.2E+2')
- >>> ExtendedContext.normalize(Decimal('0.00'))
- Decimal('0')
- >>> ExtendedContext.normalize(6)
- Decimal('6')
- """
- a = _convert_other(a, raiseit=True)
- return a.normalize(context=self)
-
- def number_class(self, a):
- """Returns an indication of the class of the operand.
-
- The class is one of the following strings:
- -sNaN
- -NaN
- -Infinity
- -Normal
- -Subnormal
- -Zero
- +Zero
- +Subnormal
- +Normal
- +Infinity
-
- >>> c = ExtendedContext.copy()
- >>> c.Emin = -999
- >>> c.Emax = 999
- >>> c.number_class(Decimal('Infinity'))
- '+Infinity'
- >>> c.number_class(Decimal('1E-10'))
- '+Normal'
- >>> c.number_class(Decimal('2.50'))
- '+Normal'
- >>> c.number_class(Decimal('0.1E-999'))
- '+Subnormal'
- >>> c.number_class(Decimal('0'))
- '+Zero'
- >>> c.number_class(Decimal('-0'))
- '-Zero'
- >>> c.number_class(Decimal('-0.1E-999'))
- '-Subnormal'
- >>> c.number_class(Decimal('-1E-10'))
- '-Normal'
- >>> c.number_class(Decimal('-2.50'))
- '-Normal'
- >>> c.number_class(Decimal('-Infinity'))
- '-Infinity'
- >>> c.number_class(Decimal('NaN'))
- 'NaN'
- >>> c.number_class(Decimal('-NaN'))
- 'NaN'
- >>> c.number_class(Decimal('sNaN'))
- 'sNaN'
- >>> c.number_class(123)
- '+Normal'
- """
- a = _convert_other(a, raiseit=True)
- return a.number_class(context=self)
-
- def plus(self, a):
- """Plus corresponds to unary prefix plus in Python.
-
- The operation is evaluated using the same rules as add; the
- operation plus(a) is calculated as add('0', a) where the '0'
- has the same exponent as the operand.
-
- >>> ExtendedContext.plus(Decimal('1.3'))
- Decimal('1.3')
- >>> ExtendedContext.plus(Decimal('-1.3'))
- Decimal('-1.3')
- >>> ExtendedContext.plus(-1)
- Decimal('-1')
- """
- a = _convert_other(a, raiseit=True)
- return a.__pos__(context=self)
-
- def power(self, a, b, modulo=None):
- """Raises a to the power of b, to modulo if given.
-
- With two arguments, compute a**b. If a is negative then b
- must be integral. The result will be inexact unless b is
- integral and the result is finite and can be expressed exactly
- in 'precision' digits.
-
- With three arguments, compute (a**b) % modulo. For the
- three argument form, the following restrictions on the
- arguments hold:
-
- - all three arguments must be integral
- - b must be nonnegative
- - at least one of a or b must be nonzero
- - modulo must be nonzero and have at most 'precision' digits
-
- The result of pow(a, b, modulo) is identical to the result
- that would be obtained by computing (a**b) % modulo with
- unbounded precision, but is computed more efficiently. It is
- always exact.
-
- >>> c = ExtendedContext.copy()
- >>> c.Emin = -999
- >>> c.Emax = 999
- >>> c.power(Decimal('2'), Decimal('3'))
- Decimal('8')
- >>> c.power(Decimal('-2'), Decimal('3'))
- Decimal('-8')
- >>> c.power(Decimal('2'), Decimal('-3'))
- Decimal('0.125')
- >>> c.power(Decimal('1.7'), Decimal('8'))
- Decimal('69.7575744')
- >>> c.power(Decimal('10'), Decimal('0.301029996'))
- Decimal('2.00000000')
- >>> c.power(Decimal('Infinity'), Decimal('-1'))
- Decimal('0')
- >>> c.power(Decimal('Infinity'), Decimal('0'))
- Decimal('1')
- >>> c.power(Decimal('Infinity'), Decimal('1'))
- Decimal('Infinity')
- >>> c.power(Decimal('-Infinity'), Decimal('-1'))
- Decimal('-0')
- >>> c.power(Decimal('-Infinity'), Decimal('0'))
- Decimal('1')
- >>> c.power(Decimal('-Infinity'), Decimal('1'))
- Decimal('-Infinity')
- >>> c.power(Decimal('-Infinity'), Decimal('2'))
- Decimal('Infinity')
- >>> c.power(Decimal('0'), Decimal('0'))
- Decimal('NaN')
-
- >>> c.power(Decimal('3'), Decimal('7'), Decimal('16'))
- Decimal('11')
- >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16'))
- Decimal('-11')
- >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16'))
- Decimal('1')
- >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16'))
- Decimal('11')
- >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789'))
- Decimal('11729830')
- >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729'))
- Decimal('-0')
- >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537'))
- Decimal('1')
- >>> ExtendedContext.power(7, 7)
- Decimal('823543')
- >>> ExtendedContext.power(Decimal(7), 7)
- Decimal('823543')
- >>> ExtendedContext.power(7, Decimal(7), 2)
- Decimal('1')
- """
- a = _convert_other(a, raiseit=True)
- r = a.__pow__(b, modulo, context=self)
- if r is NotImplemented:
- raise TypeError("Unable to convert %s to Decimal" % b)
- else:
- return r
-
- def quantize(self, a, b):
- """Returns a value equal to 'a' (rounded), having the exponent of 'b'.
-
- The coefficient of the result is derived from that of the left-hand
- operand. It may be rounded using the current rounding setting (if the
- exponent is being increased), multiplied by a positive power of ten (if
- the exponent is being decreased), or is unchanged (if the exponent is
- already equal to that of the right-hand operand).
-
- Unlike other operations, if the length of the coefficient after the
- quantize operation would be greater than precision then an Invalid
- operation condition is raised. This guarantees that, unless there is
- an error condition, the exponent of the result of a quantize is always
- equal to that of the right-hand operand.
-
- Also unlike other operations, quantize will never raise Underflow, even
- if the result is subnormal and inexact.
-
- >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
- Decimal('2.170')
- >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
- Decimal('2.17')
- >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
- Decimal('2.2')
- >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
- Decimal('2')
- >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
- Decimal('0E+1')
- >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
- Decimal('-Infinity')
- >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
- Decimal('NaN')
- >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
- Decimal('-0')
- >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
- Decimal('-0E+5')
- >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
- Decimal('NaN')
- >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
- Decimal('NaN')
- >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
- Decimal('217.0')
- >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
- Decimal('217')
- >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
- Decimal('2.2E+2')
- >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
- Decimal('2E+2')
- >>> ExtendedContext.quantize(1, 2)
- Decimal('1')
- >>> ExtendedContext.quantize(Decimal(1), 2)
- Decimal('1')
- >>> ExtendedContext.quantize(1, Decimal(2))
- Decimal('1')
- """
- a = _convert_other(a, raiseit=True)
- return a.quantize(b, context=self)
-
- def radix(self):
- """Just returns 10, as this is Decimal, :)
-
- >>> ExtendedContext.radix()
- Decimal('10')
- """
- return Decimal(10)
-
- def remainder(self, a, b):
- """Returns the remainder from integer division.
-
- The result is the residue of the dividend after the operation of
- calculating integer division as described for divide-integer, rounded
- to precision digits if necessary. The sign of the result, if
- non-zero, is the same as that of the original dividend.
-
- This operation will fail under the same conditions as integer division
- (that is, if integer division on the same two operands would fail, the
- remainder cannot be calculated).
-
- >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
- Decimal('2.1')
- >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
- Decimal('1')
- >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
- Decimal('-1')
- >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
- Decimal('0.2')
- >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
- Decimal('0.1')
- >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
- Decimal('1.0')
- >>> ExtendedContext.remainder(22, 6)
- Decimal('4')
- >>> ExtendedContext.remainder(Decimal(22), 6)
- Decimal('4')
- >>> ExtendedContext.remainder(22, Decimal(6))
- Decimal('4')
- """
- a = _convert_other(a, raiseit=True)
- r = a.__mod__(b, context=self)
- if r is NotImplemented:
- raise TypeError("Unable to convert %s to Decimal" % b)
- else:
- return r
-
- def remainder_near(self, a, b):
- """Returns to be "a - b * n", where n is the integer nearest the exact
- value of "x / b" (if two integers are equally near then the even one
- is chosen). If the result is equal to 0 then its sign will be the
- sign of a.
-
- This operation will fail under the same conditions as integer division
- (that is, if integer division on the same two operands would fail, the
- remainder cannot be calculated).
-
- >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
- Decimal('-0.9')
- >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
- Decimal('-2')
- >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
- Decimal('1')
- >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
- Decimal('-1')
- >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
- Decimal('0.2')
- >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
- Decimal('0.1')
- >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
- Decimal('-0.3')
- >>> ExtendedContext.remainder_near(3, 11)
- Decimal('3')
- >>> ExtendedContext.remainder_near(Decimal(3), 11)
- Decimal('3')
- >>> ExtendedContext.remainder_near(3, Decimal(11))
- Decimal('3')
- """
- a = _convert_other(a, raiseit=True)
- return a.remainder_near(b, context=self)
-
- def rotate(self, a, b):
- """Returns a rotated copy of a, b times.
-
- The coefficient of the result is a rotated copy of the digits in
- the coefficient of the first operand. The number of places of
- rotation is taken from the absolute value of the second operand,
- with the rotation being to the left if the second operand is
- positive or to the right otherwise.
-
- >>> ExtendedContext.rotate(Decimal('34'), Decimal('8'))
- Decimal('400000003')
- >>> ExtendedContext.rotate(Decimal('12'), Decimal('9'))
- Decimal('12')
- >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2'))
- Decimal('891234567')
- >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0'))
- Decimal('123456789')
- >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2'))
- Decimal('345678912')
- >>> ExtendedContext.rotate(1333333, 1)
- Decimal('13333330')
- >>> ExtendedContext.rotate(Decimal(1333333), 1)
- Decimal('13333330')
- >>> ExtendedContext.rotate(1333333, Decimal(1))
- Decimal('13333330')
- """
- a = _convert_other(a, raiseit=True)
- return a.rotate(b, context=self)
-
- def same_quantum(self, a, b):
- """Returns True if the two operands have the same exponent.
-
- The result is never affected by either the sign or the coefficient of
- either operand.
-
- >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
- False
- >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
- True
- >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
- False
- >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
- True
- >>> ExtendedContext.same_quantum(10000, -1)
- True
- >>> ExtendedContext.same_quantum(Decimal(10000), -1)
- True
- >>> ExtendedContext.same_quantum(10000, Decimal(-1))
- True
- """
- a = _convert_other(a, raiseit=True)
- return a.same_quantum(b)
-
- def scaleb (self, a, b):
- """Returns the first operand after adding the second value its exp.
-
- >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2'))
- Decimal('0.0750')
- >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0'))
- Decimal('7.50')
- >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3'))
- Decimal('7.50E+3')
- >>> ExtendedContext.scaleb(1, 4)
- Decimal('1E+4')
- >>> ExtendedContext.scaleb(Decimal(1), 4)
- Decimal('1E+4')
- >>> ExtendedContext.scaleb(1, Decimal(4))
- Decimal('1E+4')
- """
- a = _convert_other(a, raiseit=True)
- return a.scaleb(b, context=self)
-
- def shift(self, a, b):
- """Returns a shifted copy of a, b times.
-
- The coefficient of the result is a shifted copy of the digits
- in the coefficient of the first operand. The number of places
- to shift is taken from the absolute value of the second operand,
- with the shift being to the left if the second operand is
- positive or to the right otherwise. Digits shifted into the
- coefficient are zeros.
-
- >>> ExtendedContext.shift(Decimal('34'), Decimal('8'))
- Decimal('400000000')
- >>> ExtendedContext.shift(Decimal('12'), Decimal('9'))
- Decimal('0')
- >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2'))
- Decimal('1234567')
- >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0'))
- Decimal('123456789')
- >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2'))
- Decimal('345678900')
- >>> ExtendedContext.shift(88888888, 2)
- Decimal('888888800')
- >>> ExtendedContext.shift(Decimal(88888888), 2)
- Decimal('888888800')
- >>> ExtendedContext.shift(88888888, Decimal(2))
- Decimal('888888800')
- """
- a = _convert_other(a, raiseit=True)
- return a.shift(b, context=self)
-
- def sqrt(self, a):
- """Square root of a non-negative number to context precision.
-
- If the result must be inexact, it is rounded using the round-half-even
- algorithm.
-
- >>> ExtendedContext.sqrt(Decimal('0'))
- Decimal('0')
- >>> ExtendedContext.sqrt(Decimal('-0'))
- Decimal('-0')
- >>> ExtendedContext.sqrt(Decimal('0.39'))
- Decimal('0.624499800')
- >>> ExtendedContext.sqrt(Decimal('100'))
- Decimal('10')
- >>> ExtendedContext.sqrt(Decimal('1'))
- Decimal('1')
- >>> ExtendedContext.sqrt(Decimal('1.0'))
- Decimal('1.0')
- >>> ExtendedContext.sqrt(Decimal('1.00'))
- Decimal('1.0')
- >>> ExtendedContext.sqrt(Decimal('7'))
- Decimal('2.64575131')
- >>> ExtendedContext.sqrt(Decimal('10'))
- Decimal('3.16227766')
- >>> ExtendedContext.sqrt(2)
- Decimal('1.41421356')
- >>> ExtendedContext.prec
- 9
- """
- a = _convert_other(a, raiseit=True)
- return a.sqrt(context=self)
-
- def subtract(self, a, b):
- """Return the difference between the two operands.
-
- >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
- Decimal('0.23')
- >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
- Decimal('0.00')
- >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
- Decimal('-0.77')
- >>> ExtendedContext.subtract(8, 5)
- Decimal('3')
- >>> ExtendedContext.subtract(Decimal(8), 5)
- Decimal('3')
- >>> ExtendedContext.subtract(8, Decimal(5))
- Decimal('3')
- """
- a = _convert_other(a, raiseit=True)
- r = a.__sub__(b, context=self)
- if r is NotImplemented:
- raise TypeError("Unable to convert %s to Decimal" % b)
- else:
- return r
-
- def to_eng_string(self, a):
- """Converts a number to a string, using scientific notation.
-
- The operation is not affected by the context.
- """
- a = _convert_other(a, raiseit=True)
- return a.to_eng_string(context=self)
-
- def to_sci_string(self, a):
- """Converts a number to a string, using scientific notation.
-
- The operation is not affected by the context.
- """
- a = _convert_other(a, raiseit=True)
- return a.__str__(context=self)
-
- def to_integral_exact(self, a):
- """Rounds to an integer.
-
- When the operand has a negative exponent, the result is the same
- as using the quantize() operation using the given operand as the
- left-hand-operand, 1E+0 as the right-hand-operand, and the precision
- of the operand as the precision setting; Inexact and Rounded flags
- are allowed in this operation. The rounding mode is taken from the
- context.
-
- >>> ExtendedContext.to_integral_exact(Decimal('2.1'))
- Decimal('2')
- >>> ExtendedContext.to_integral_exact(Decimal('100'))
- Decimal('100')
- >>> ExtendedContext.to_integral_exact(Decimal('100.0'))
- Decimal('100')
- >>> ExtendedContext.to_integral_exact(Decimal('101.5'))
- Decimal('102')
- >>> ExtendedContext.to_integral_exact(Decimal('-101.5'))
- Decimal('-102')
- >>> ExtendedContext.to_integral_exact(Decimal('10E+5'))
- Decimal('1.0E+6')
- >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77'))
- Decimal('7.89E+77')
- >>> ExtendedContext.to_integral_exact(Decimal('-Inf'))
- Decimal('-Infinity')
- """
- a = _convert_other(a, raiseit=True)
- return a.to_integral_exact(context=self)
-
- def to_integral_value(self, a):
- """Rounds to an integer.
-
- When the operand has a negative exponent, the result is the same
- as using the quantize() operation using the given operand as the
- left-hand-operand, 1E+0 as the right-hand-operand, and the precision
- of the operand as the precision setting, except that no flags will
- be set. The rounding mode is taken from the context.
-
- >>> ExtendedContext.to_integral_value(Decimal('2.1'))
- Decimal('2')
- >>> ExtendedContext.to_integral_value(Decimal('100'))
- Decimal('100')
- >>> ExtendedContext.to_integral_value(Decimal('100.0'))
- Decimal('100')
- >>> ExtendedContext.to_integral_value(Decimal('101.5'))
- Decimal('102')
- >>> ExtendedContext.to_integral_value(Decimal('-101.5'))
- Decimal('-102')
- >>> ExtendedContext.to_integral_value(Decimal('10E+5'))
- Decimal('1.0E+6')
- >>> ExtendedContext.to_integral_value(Decimal('7.89E+77'))
- Decimal('7.89E+77')
- >>> ExtendedContext.to_integral_value(Decimal('-Inf'))
- Decimal('-Infinity')
- """
- a = _convert_other(a, raiseit=True)
- return a.to_integral_value(context=self)
-
- # the method name changed, but we provide also the old one, for compatibility
- to_integral = to_integral_value
-
-class _WorkRep(object):
- __slots__ = ('sign','int','exp')
- # sign: 0 or 1
- # int: int
- # exp: None, int, or string
-
- def __init__(self, value=None):
- if value is None:
- self.sign = None
- self.int = 0
- self.exp = None
- elif isinstance(value, Decimal):
- self.sign = value._sign
- self.int = int(value._int)
- self.exp = value._exp
- else:
- # assert isinstance(value, tuple)
- self.sign = value[0]
- self.int = value[1]
- self.exp = value[2]
-
- def __repr__(self):
- return "(%r, %r, %r)" % (self.sign, self.int, self.exp)
-
- __str__ = __repr__
-
-
-
-def _normalize(op1, op2, prec = 0):
- """Normalizes op1, op2 to have the same exp and length of coefficient.
-
- Done during addition.
- """
- if op1.exp < op2.exp:
- tmp = op2
- other = op1
- else:
- tmp = op1
- other = op2
-
- # Let exp = min(tmp.exp - 1, tmp.adjusted() - precision - 1).
- # Then adding 10**exp to tmp has the same effect (after rounding)
- # as adding any positive quantity smaller than 10**exp; similarly
- # for subtraction. So if other is smaller than 10**exp we replace
- # it with 10**exp. This avoids tmp.exp - other.exp getting too large.
- tmp_len = len(str(tmp.int))
- other_len = len(str(other.int))
- exp = tmp.exp + min(-1, tmp_len - prec - 2)
- if other_len + other.exp - 1 < exp:
- other.int = 1
- other.exp = exp
-
- tmp.int *= 10 ** (tmp.exp - other.exp)
- tmp.exp = other.exp
- return op1, op2
-
-##### Integer arithmetic functions used by ln, log10, exp and __pow__ #####
-
-_nbits = int.bit_length
-
-def _decimal_lshift_exact(n, e):
- """ Given integers n and e, return n * 10**e if it's an integer, else None.
-
- The computation is designed to avoid computing large powers of 10
- unnecessarily.
-
- >>> _decimal_lshift_exact(3, 4)
- 30000
- >>> _decimal_lshift_exact(300, -999999999) # returns None
-
- """
- if n == 0:
- return 0
- elif e >= 0:
- return n * 10**e
- else:
- # val_n = largest power of 10 dividing n.
- str_n = str(abs(n))
- val_n = len(str_n) - len(str_n.rstrip('0'))
- return None if val_n < -e else n // 10**-e
-
-def _sqrt_nearest(n, a):
- """Closest integer to the square root of the positive integer n. a is
- an initial approximation to the square root. Any positive integer
- will do for a, but the closer a is to the square root of n the
- faster convergence will be.
-
- """
- if n <= 0 or a <= 0:
- raise ValueError("Both arguments to _sqrt_nearest should be positive.")
-
- b=0
- while a != b:
- b, a = a, a--n//a>>1
- return a
-
-def _rshift_nearest(x, shift):
- """Given an integer x and a nonnegative integer shift, return closest
- integer to x / 2**shift; use round-to-even in case of a tie.
-
- """
- b, q = 1 << shift, x >> shift
- return q + (2*(x & (b-1)) + (q&1) > b)
-
-def _div_nearest(a, b):
- """Closest integer to a/b, a and b positive integers; rounds to even
- in the case of a tie.
-
- """
- q, r = divmod(a, b)
- return q + (2*r + (q&1) > b)
-
-def _ilog(x, M, L = 8):
- """Integer approximation to M*log(x/M), with absolute error boundable
- in terms only of x/M.
-
- Given positive integers x and M, return an integer approximation to
- M * log(x/M). For L = 8 and 0.1 <= x/M <= 10 the difference
- between the approximation and the exact result is at most 22. For
- L = 8 and 1.0 <= x/M <= 10.0 the difference is at most 15. In
- both cases these are upper bounds on the error; it will usually be
- much smaller."""
-
- # The basic algorithm is the following: let log1p be the function
- # log1p(x) = log(1+x). Then log(x/M) = log1p((x-M)/M). We use
- # the reduction
- #
- # log1p(y) = 2*log1p(y/(1+sqrt(1+y)))
- #
- # repeatedly until the argument to log1p is small (< 2**-L in
- # absolute value). For small y we can use the Taylor series
- # expansion
- #
- # log1p(y) ~ y - y**2/2 + y**3/3 - ... - (-y)**T/T
- #
- # truncating at T such that y**T is small enough. The whole
- # computation is carried out in a form of fixed-point arithmetic,
- # with a real number z being represented by an integer
- # approximation to z*M. To avoid loss of precision, the y below
- # is actually an integer approximation to 2**R*y*M, where R is the
- # number of reductions performed so far.
-
- y = x-M
- # argument reduction; R = number of reductions performed
- R = 0
- while (R <= L and abs(y) << L-R >= M or
- R > L and abs(y) >> R-L >= M):
- y = _div_nearest((M*y) << 1,
- M + _sqrt_nearest(M*(M+_rshift_nearest(y, R)), M))
- R += 1
-
- # Taylor series with T terms
- T = -int(-10*len(str(M))//(3*L))
- yshift = _rshift_nearest(y, R)
- w = _div_nearest(M, T)
- for k in range(T-1, 0, -1):
- w = _div_nearest(M, k) - _div_nearest(yshift*w, M)
-
- return _div_nearest(w*y, M)
-
-def _dlog10(c, e, p):
- """Given integers c, e and p with c > 0, p >= 0, compute an integer
- approximation to 10**p * log10(c*10**e), with an absolute error of
- at most 1. Assumes that c*10**e is not exactly 1."""
-
- # increase precision by 2; compensate for this by dividing
- # final result by 100
- p += 2
-
- # write c*10**e as d*10**f with either:
- # f >= 0 and 1 <= d <= 10, or
- # f <= 0 and 0.1 <= d <= 1.
- # Thus for c*10**e close to 1, f = 0
- l = len(str(c))
- f = e+l - (e+l >= 1)
-
- if p > 0:
- M = 10**p
- k = e+p-f
- if k >= 0:
- c *= 10**k
- else:
- c = _div_nearest(c, 10**-k)
-
- log_d = _ilog(c, M) # error < 5 + 22 = 27
- log_10 = _log10_digits(p) # error < 1
- log_d = _div_nearest(log_d*M, log_10)
- log_tenpower = f*M # exact
- else:
- log_d = 0 # error < 2.31
- log_tenpower = _div_nearest(f, 10**-p) # error < 0.5
-
- return _div_nearest(log_tenpower+log_d, 100)
-
-def _dlog(c, e, p):
- """Given integers c, e and p with c > 0, compute an integer
- approximation to 10**p * log(c*10**e), with an absolute error of
- at most 1. Assumes that c*10**e is not exactly 1."""
-
- # Increase precision by 2. The precision increase is compensated
- # for at the end with a division by 100.
- p += 2
-
- # rewrite c*10**e as d*10**f with either f >= 0 and 1 <= d <= 10,
- # or f <= 0 and 0.1 <= d <= 1. Then we can compute 10**p * log(c*10**e)
- # as 10**p * log(d) + 10**p*f * log(10).
- l = len(str(c))
- f = e+l - (e+l >= 1)
-
- # compute approximation to 10**p*log(d), with error < 27
- if p > 0:
- k = e+p-f
- if k >= 0:
- c *= 10**k
- else:
- c = _div_nearest(c, 10**-k) # error of <= 0.5 in c
-
- # _ilog magnifies existing error in c by a factor of at most 10
- log_d = _ilog(c, 10**p) # error < 5 + 22 = 27
- else:
- # p <= 0: just approximate the whole thing by 0; error < 2.31
- log_d = 0
-
- # compute approximation to f*10**p*log(10), with error < 11.
- if f:
- extra = len(str(abs(f)))-1
- if p + extra >= 0:
- # error in f * _log10_digits(p+extra) < |f| * 1 = |f|
- # after division, error < |f|/10**extra + 0.5 < 10 + 0.5 < 11
- f_log_ten = _div_nearest(f*_log10_digits(p+extra), 10**extra)
- else:
- f_log_ten = 0
- else:
- f_log_ten = 0
-
- # error in sum < 11+27 = 38; error after division < 0.38 + 0.5 < 1
- return _div_nearest(f_log_ten + log_d, 100)
-
-class _Log10Memoize(object):
- """Class to compute, store, and allow retrieval of, digits of the
- constant log(10) = 2.302585.... This constant is needed by
- Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__."""
- def __init__(self):
- self.digits = "23025850929940456840179914546843642076011014886"
-
- def getdigits(self, p):
- """Given an integer p >= 0, return floor(10**p)*log(10).
-
- For example, self.getdigits(3) returns 2302.
- """
- # digits are stored as a string, for quick conversion to
- # integer in the case that we've already computed enough
- # digits; the stored digits should always be correct
- # (truncated, not rounded to nearest).
- if p < 0:
- raise ValueError("p should be nonnegative")
-
- if p >= len(self.digits):
- # compute p+3, p+6, p+9, ... digits; continue until at
- # least one of the extra digits is nonzero
- extra = 3
- while True:
- # compute p+extra digits, correct to within 1ulp
- M = 10**(p+extra+2)
- digits = str(_div_nearest(_ilog(10*M, M), 100))
- if digits[-extra:] != '0'*extra:
- break
- extra += 3
- # keep all reliable digits so far; remove trailing zeros
- # and next nonzero digit
- self.digits = digits.rstrip('0')[:-1]
- return int(self.digits[:p+1])
-
-_log10_digits = _Log10Memoize().getdigits
-
-def _iexp(x, M, L=8):
- """Given integers x and M, M > 0, such that x/M is small in absolute
- value, compute an integer approximation to M*exp(x/M). For 0 <=
- x/M <= 2.4, the absolute error in the result is bounded by 60 (and
- is usually much smaller)."""
-
- # Algorithm: to compute exp(z) for a real number z, first divide z
- # by a suitable power R of 2 so that |z/2**R| < 2**-L. Then
- # compute expm1(z/2**R) = exp(z/2**R) - 1 using the usual Taylor
- # series
- #
- # expm1(x) = x + x**2/2! + x**3/3! + ...
- #
- # Now use the identity
- #
- # expm1(2x) = expm1(x)*(expm1(x)+2)
- #
- # R times to compute the sequence expm1(z/2**R),
- # expm1(z/2**(R-1)), ... , exp(z/2), exp(z).
-
- # Find R such that x/2**R/M <= 2**-L
- R = _nbits((x<<L)//M)
-
- # Taylor series. (2**L)**T > M
- T = -int(-10*len(str(M))//(3*L))
- y = _div_nearest(x, T)
- Mshift = M<<R
- for i in range(T-1, 0, -1):
- y = _div_nearest(x*(Mshift + y), Mshift * i)
-
- # Expansion
- for k in range(R-1, -1, -1):
- Mshift = M<<(k+2)
- y = _div_nearest(y*(y+Mshift), Mshift)
-
- return M+y
-
-def _dexp(c, e, p):
- """Compute an approximation to exp(c*10**e), with p decimal places of
- precision.
-
- Returns integers d, f such that:
-
- 10**(p-1) <= d <= 10**p, and
- (d-1)*10**f < exp(c*10**e) < (d+1)*10**f
-
- In other words, d*10**f is an approximation to exp(c*10**e) with p
- digits of precision, and with an error in d of at most 1. This is
- almost, but not quite, the same as the error being < 1ulp: when d
- = 10**(p-1) the error could be up to 10 ulp."""
-
- # we'll call iexp with M = 10**(p+2), giving p+3 digits of precision
- p += 2
-
- # compute log(10) with extra precision = adjusted exponent of c*10**e
- extra = max(0, e + len(str(c)) - 1)
- q = p + extra
-
- # compute quotient c*10**e/(log(10)) = c*10**(e+q)/(log(10)*10**q),
- # rounding down
- shift = e+q
- if shift >= 0:
- cshift = c*10**shift
- else:
- cshift = c//10**-shift
- quot, rem = divmod(cshift, _log10_digits(q))
-
- # reduce remainder back to original precision
- rem = _div_nearest(rem, 10**extra)
-
- # error in result of _iexp < 120; error after division < 0.62
- return _div_nearest(_iexp(rem, 10**p), 1000), quot - p + 3
-
-def _dpower(xc, xe, yc, ye, p):
- """Given integers xc, xe, yc and ye representing Decimals x = xc*10**xe and
- y = yc*10**ye, compute x**y. Returns a pair of integers (c, e) such that:
-
- 10**(p-1) <= c <= 10**p, and
- (c-1)*10**e < x**y < (c+1)*10**e
-
- in other words, c*10**e is an approximation to x**y with p digits
- of precision, and with an error in c of at most 1. (This is
- almost, but not quite, the same as the error being < 1ulp: when c
- == 10**(p-1) we can only guarantee error < 10ulp.)
-
- We assume that: x is positive and not equal to 1, and y is nonzero.
- """
-
- # Find b such that 10**(b-1) <= |y| <= 10**b
- b = len(str(abs(yc))) + ye
-
- # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point
- lxc = _dlog(xc, xe, p+b+1)
-
- # compute product y*log(x) = yc*lxc*10**(-p-b-1+ye) = pc*10**(-p-1)
- shift = ye-b
- if shift >= 0:
- pc = lxc*yc*10**shift
- else:
- pc = _div_nearest(lxc*yc, 10**-shift)
-
- if pc == 0:
- # we prefer a result that isn't exactly 1; this makes it
- # easier to compute a correctly rounded result in __pow__
- if ((len(str(xc)) + xe >= 1) == (yc > 0)): # if x**y > 1:
- coeff, exp = 10**(p-1)+1, 1-p
- else:
- coeff, exp = 10**p-1, -p
- else:
- coeff, exp = _dexp(pc, -(p+1), p+1)
- coeff = _div_nearest(coeff, 10)
- exp += 1
-
- return coeff, exp
-
-def _log10_lb(c, correction = {
- '1': 100, '2': 70, '3': 53, '4': 40, '5': 31,
- '6': 23, '7': 16, '8': 10, '9': 5}):
- """Compute a lower bound for 100*log10(c) for a positive integer c."""
- if c <= 0:
- raise ValueError("The argument to _log10_lb should be nonnegative.")
- str_c = str(c)
- return 100*len(str_c) - correction[str_c[0]]
-
-##### Helper Functions ####################################################
-
-def _convert_other(other, raiseit=False, allow_float=False):
- """Convert other to Decimal.
-
- Verifies that it's ok to use in an implicit construction.
- If allow_float is true, allow conversion from float; this
- is used in the comparison methods (__eq__ and friends).
-
- """
- if isinstance(other, Decimal):
- return other
- if isinstance(other, int):
- return Decimal(other)
- if allow_float and isinstance(other, float):
- return Decimal.from_float(other)
-
- if raiseit:
- raise TypeError("Unable to convert %s to Decimal" % other)
- return NotImplemented
-
-def _convert_for_comparison(self, other, equality_op=False):
- """Given a Decimal instance self and a Python object other, return
- a pair (s, o) of Decimal instances such that "s op o" is
- equivalent to "self op other" for any of the 6 comparison
- operators "op".
-
- """
- if isinstance(other, Decimal):
- return self, other
-
- # Comparison with a Rational instance (also includes integers):
- # self op n/d <=> self*d op n (for n and d integers, d positive).
- # A NaN or infinity can be left unchanged without affecting the
- # comparison result.
- if isinstance(other, _numbers.Rational):
- if not self._is_special:
- self = _dec_from_triple(self._sign,
- str(int(self._int) * other.denominator),
- self._exp)
- return self, Decimal(other.numerator)
-
- # Comparisons with float and complex types. == and != comparisons
- # with complex numbers should succeed, returning either True or False
- # as appropriate. Other comparisons return NotImplemented.
- if equality_op and isinstance(other, _numbers.Complex) and other.imag == 0:
- other = other.real
- if isinstance(other, float):
- context = getcontext()
- if equality_op:
- context.flags[FloatOperation] = 1
- else:
- context._raise_error(FloatOperation,
- "strict semantics for mixing floats and Decimals are enabled")
- return self, Decimal.from_float(other)
- return NotImplemented, NotImplemented
-
-
-##### Setup Specific Contexts ############################################
-
-# The default context prototype used by Context()
-# Is mutable, so that new contexts can have different default values
-
-DefaultContext = Context(
- prec=28, rounding=ROUND_HALF_EVEN,
- traps=[DivisionByZero, Overflow, InvalidOperation],
- flags=[],
- Emax=999999,
- Emin=-999999,
- capitals=1,
- clamp=0
-)
-
-# Pre-made alternate contexts offered by the specification
-# Don't change these; the user should be able to select these
-# contexts and be able to reproduce results from other implementations
-# of the spec.
-
-BasicContext = Context(
- prec=9, rounding=ROUND_HALF_UP,
- traps=[DivisionByZero, Overflow, InvalidOperation, Clamped, Underflow],
- flags=[],
-)
-
-ExtendedContext = Context(
- prec=9, rounding=ROUND_HALF_EVEN,
- traps=[],
- flags=[],
-)
-
-
-##### crud for parsing strings #############################################
-#
-# Regular expression used for parsing numeric strings. Additional
-# comments:
-#
-# 1. Uncomment the two '\s*' lines to allow leading and/or trailing
-# whitespace. But note that the specification disallows whitespace in
-# a numeric string.
-#
-# 2. For finite numbers (not infinities and NaNs) the body of the
-# number between the optional sign and the optional exponent must have
-# at least one decimal digit, possibly after the decimal point. The
-# lookahead expression '(?=\d|\.\d)' checks this.
-
-import re
-_parser = re.compile(r""" # A numeric string consists of:
-# \s*
- (?P<sign>[-+])? # an optional sign, followed by either...
- (
- (?=\d|\.\d) # ...a number (with at least one digit)
- (?P<int>\d*) # having a (possibly empty) integer part
- (\.(?P<frac>\d*))? # followed by an optional fractional part
- (E(?P<exp>[-+]?\d+))? # followed by an optional exponent, or...
- |
- Inf(inity)? # ...an infinity, or...
- |
- (?P<signal>s)? # ...an (optionally signaling)
- NaN # NaN
- (?P<diag>\d*) # with (possibly empty) diagnostic info.
- )
-# \s*
- \Z
-""", re.VERBOSE | re.IGNORECASE).match
-
-_all_zeros = re.compile('0*$').match
-_exact_half = re.compile('50*$').match
-
-##### PEP3101 support functions ##############################################
-# The functions in this section have little to do with the Decimal
-# class, and could potentially be reused or adapted for other pure
-# Python numeric classes that want to implement __format__
-#
-# A format specifier for Decimal looks like:
-#
-# [[fill]align][sign][#][0][minimumwidth][,][.precision][type]
-
-_parse_format_specifier_regex = re.compile(r"""\A
-(?:
- (?P<fill>.)?
- (?P<align>[<>=^])
-)?
-(?P<sign>[-+ ])?
-(?P<alt>\#)?
-(?P<zeropad>0)?
-(?P<minimumwidth>(?!0)\d+)?
-(?P<thousands_sep>,)?
-(?:\.(?P<precision>0|(?!0)\d+))?
-(?P<type>[eEfFgGn%])?
-\Z
-""", re.VERBOSE|re.DOTALL)
-
-del re
-
-# The locale module is only needed for the 'n' format specifier. The
-# rest of the PEP 3101 code functions quite happily without it, so we
-# don't care too much if locale isn't present.
-try:
- import locale as _locale
-except ImportError:
- pass
-
-def _parse_format_specifier(format_spec, _localeconv=None):
- """Parse and validate a format specifier.
-
- Turns a standard numeric format specifier into a dict, with the
- following entries:
-
- fill: fill character to pad field to minimum width
- align: alignment type, either '<', '>', '=' or '^'
- sign: either '+', '-' or ' '
- minimumwidth: nonnegative integer giving minimum width
- zeropad: boolean, indicating whether to pad with zeros
- thousands_sep: string to use as thousands separator, or ''
- grouping: grouping for thousands separators, in format
- used by localeconv
- decimal_point: string to use for decimal point
- precision: nonnegative integer giving precision, or None
- type: one of the characters 'eEfFgG%', or None
-
- """
- m = _parse_format_specifier_regex.match(format_spec)
- if m is None:
- raise ValueError("Invalid format specifier: " + format_spec)
-
- # get the dictionary
- format_dict = m.groupdict()
-
- # zeropad; defaults for fill and alignment. If zero padding
- # is requested, the fill and align fields should be absent.
- fill = format_dict['fill']
- align = format_dict['align']
- format_dict['zeropad'] = (format_dict['zeropad'] is not None)
- if format_dict['zeropad']:
- if fill is not None:
- raise ValueError("Fill character conflicts with '0'"
- " in format specifier: " + format_spec)
- if align is not None:
- raise ValueError("Alignment conflicts with '0' in "
- "format specifier: " + format_spec)
- format_dict['fill'] = fill or ' '
- # PEP 3101 originally specified that the default alignment should
- # be left; it was later agreed that right-aligned makes more sense
- # for numeric types. See http://bugs.python.org/issue6857.
- format_dict['align'] = align or '>'
-
- # default sign handling: '-' for negative, '' for positive
- if format_dict['sign'] is None:
- format_dict['sign'] = '-'
-
- # minimumwidth defaults to 0; precision remains None if not given
- format_dict['minimumwidth'] = int(format_dict['minimumwidth'] or '0')
- if format_dict['precision'] is not None:
- format_dict['precision'] = int(format_dict['precision'])
-
- # if format type is 'g' or 'G' then a precision of 0 makes little
- # sense; convert it to 1. Same if format type is unspecified.
- if format_dict['precision'] == 0:
- if format_dict['type'] is None or format_dict['type'] in 'gGn':
- format_dict['precision'] = 1
-
- # determine thousands separator, grouping, and decimal separator, and
- # add appropriate entries to format_dict
- if format_dict['type'] == 'n':
- # apart from separators, 'n' behaves just like 'g'
- format_dict['type'] = 'g'
- if _localeconv is None:
- _localeconv = _locale.localeconv()
- if format_dict['thousands_sep'] is not None:
- raise ValueError("Explicit thousands separator conflicts with "
- "'n' type in format specifier: " + format_spec)
- format_dict['thousands_sep'] = _localeconv['thousands_sep']
- format_dict['grouping'] = _localeconv['grouping']
- format_dict['decimal_point'] = _localeconv['decimal_point']
- else:
- if format_dict['thousands_sep'] is None:
- format_dict['thousands_sep'] = ''
- format_dict['grouping'] = [3, 0]
- format_dict['decimal_point'] = '.'
-
- return format_dict
-
-def _format_align(sign, body, spec):
- """Given an unpadded, non-aligned numeric string 'body' and sign
- string 'sign', add padding and alignment conforming to the given
- format specifier dictionary 'spec' (as produced by
- parse_format_specifier).
-
- """
- # how much extra space do we have to play with?
- minimumwidth = spec['minimumwidth']
- fill = spec['fill']
- padding = fill*(minimumwidth - len(sign) - len(body))
-
- align = spec['align']
- if align == '<':
- result = sign + body + padding
- elif align == '>':
- result = padding + sign + body
- elif align == '=':
- result = sign + padding + body
- elif align == '^':
- half = len(padding)//2
- result = padding[:half] + sign + body + padding[half:]
- else:
- raise ValueError('Unrecognised alignment field')
-
- return result
-
-def _group_lengths(grouping):
- """Convert a localeconv-style grouping into a (possibly infinite)
- iterable of integers representing group lengths.
-
- """
- # The result from localeconv()['grouping'], and the input to this
- # function, should be a list of integers in one of the
- # following three forms:
- #
- # (1) an empty list, or
- # (2) nonempty list of positive integers + [0]
- # (3) list of positive integers + [locale.CHAR_MAX], or
-
- from itertools import chain, repeat
- if not grouping:
- return []
- elif grouping[-1] == 0 and len(grouping) >= 2:
- return chain(grouping[:-1], repeat(grouping[-2]))
- elif grouping[-1] == _locale.CHAR_MAX:
- return grouping[:-1]
- else:
- raise ValueError('unrecognised format for grouping')
-
-def _insert_thousands_sep(digits, spec, min_width=1):
- """Insert thousands separators into a digit string.
-
- spec is a dictionary whose keys should include 'thousands_sep' and
- 'grouping'; typically it's the result of parsing the format
- specifier using _parse_format_specifier.
-
- The min_width keyword argument gives the minimum length of the
- result, which will be padded on the left with zeros if necessary.
-
- If necessary, the zero padding adds an extra '0' on the left to
- avoid a leading thousands separator. For example, inserting
- commas every three digits in '123456', with min_width=8, gives
- '0,123,456', even though that has length 9.
-
- """
-
- sep = spec['thousands_sep']
- grouping = spec['grouping']
-
- groups = []
- for l in _group_lengths(grouping):
- if l <= 0:
- raise ValueError("group length should be positive")
- # max(..., 1) forces at least 1 digit to the left of a separator
- l = min(max(len(digits), min_width, 1), l)
- groups.append('0'*(l - len(digits)) + digits[-l:])
- digits = digits[:-l]
- min_width -= l
- if not digits and min_width <= 0:
- break
- min_width -= len(sep)
- else:
- l = max(len(digits), min_width, 1)
- groups.append('0'*(l - len(digits)) + digits[-l:])
- return sep.join(reversed(groups))
-
-def _format_sign(is_negative, spec):
- """Determine sign character."""
-
- if is_negative:
- return '-'
- elif spec['sign'] in ' +':
- return spec['sign']
- else:
- return ''
-
-def _format_number(is_negative, intpart, fracpart, exp, spec):
- """Format a number, given the following data:
-
- is_negative: true if the number is negative, else false
- intpart: string of digits that must appear before the decimal point
- fracpart: string of digits that must come after the point
- exp: exponent, as an integer
- spec: dictionary resulting from parsing the format specifier
-
- This function uses the information in spec to:
- insert separators (decimal separator and thousands separators)
- format the sign
- format the exponent
- add trailing '%' for the '%' type
- zero-pad if necessary
- fill and align if necessary
- """
-
- sign = _format_sign(is_negative, spec)
-
- if fracpart or spec['alt']:
- fracpart = spec['decimal_point'] + fracpart
-
- if exp != 0 or spec['type'] in 'eE':
- echar = {'E': 'E', 'e': 'e', 'G': 'E', 'g': 'e'}[spec['type']]
- fracpart += "{0}{1:+}".format(echar, exp)
- if spec['type'] == '%':
- fracpart += '%'
-
- if spec['zeropad']:
- min_width = spec['minimumwidth'] - len(fracpart) - len(sign)
- else:
- min_width = 0
- intpart = _insert_thousands_sep(intpart, spec, min_width)
-
- return _format_align(sign, intpart+fracpart, spec)
-
-
-##### Useful Constants (internal use only) ################################
-
-# Reusable defaults
-_Infinity = Decimal('Inf')
-_NegativeInfinity = Decimal('-Inf')
-_NaN = Decimal('NaN')
-_Zero = Decimal(0)
-_One = Decimal(1)
-_NegativeOne = Decimal(-1)
-
-# _SignedInfinity[sign] is infinity w/ that sign
-_SignedInfinity = (_Infinity, _NegativeInfinity)
-
-# Constants related to the hash implementation; hash(x) is based
-# on the reduction of x modulo _PyHASH_MODULUS
-_PyHASH_MODULUS = sys.hash_info.modulus
-# hash values to use for positive and negative infinities, and nans
-_PyHASH_INF = sys.hash_info.inf
-_PyHASH_NAN = sys.hash_info.nan
-
-# _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS
-_PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS)
-del sys
try:
- import _decimal
-except ImportError:
- pass
-else:
- s1 = set(dir())
- s2 = set(dir(_decimal))
- for name in s1 - s2:
- del globals()[name]
- del s1, s2, name
from _decimal import *
-
-if __name__ == '__main__':
- import doctest, decimal
- doctest.testmod(decimal)
+ from _decimal import __doc__
+ from _decimal import __version__
+ from _decimal import __libmpdec_version__
+except ImportError:
+ from _pydecimal import *
+ from _pydecimal import __doc__
+ from _pydecimal import __version__
+ from _pydecimal import __libmpdec_version__
diff --git a/Lib/difflib.py b/Lib/difflib.py
index 7eb42a9..ae3479d 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -30,7 +30,7 @@ __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher',
'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff',
'unified_diff', 'HtmlDiff', 'Match']
-import heapq
+from heapq import nlargest as _nlargest
from collections import namedtuple as _namedtuple
Match = _namedtuple('Match', 'a b size')
@@ -729,7 +729,7 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
result.append((s.ratio(), x))
# Move the best scorers to head of list
- result = heapq.nlargest(n, result)
+ result = _nlargest(n, result)
# Strip scores for the best n matches
return [x for score, x in result]
@@ -852,10 +852,9 @@ class Differ:
and return true iff the string is junk. The module-level function
`IS_LINE_JUNK` may be used to filter out lines without visible
characters, except for at most one splat ('#'). It is recommended
- to leave linejunk None; as of Python 2.3, the underlying
- SequenceMatcher class has grown an adaptive notion of "noise" lines
- that's better than any static definition the author has ever been
- able to craft.
+ to leave linejunk None; the underlying SequenceMatcher class has
+ an adaptive notion of "noise" lines that's better than any static
+ definition the author has ever been able to craft.
- `charjunk`: A function that should accept a string of length 1. The
module-level function `IS_CHARACTER_JUNK` may be used to filter out
@@ -1298,17 +1297,18 @@ def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK):
Compare `a` and `b` (lists of strings); return a `Differ`-style delta.
Optional keyword parameters `linejunk` and `charjunk` are for filter
- functions (or None):
+ functions, or can be None:
- - linejunk: A function that should accept a single string argument, and
+ - linejunk: A function that should accept a single string argument and
return true iff the string is junk. The default is None, and is
- recommended; as of Python 2.3, an adaptive notion of "noise" lines is
- used that does a good job on its own.
+ recommended; the underlying SequenceMatcher class has an adaptive
+ notion of "noise" lines.
- - charjunk: A function that should accept a string of length 1. The
- default is module-level function IS_CHARACTER_JUNK, which filters out
- whitespace characters (a blank or tab; note: bad idea to include newline
- in this!).
+ - charjunk: A function that accepts a character (string of length
+ 1), and returns true iff the character is junk. The default is
+ the module-level function IS_CHARACTER_JUNK, which filters out
+ whitespace characters (a blank or tab; note: it's a bad idea to
+ include newline in this!).
Tools/scripts/ndiff.py is a command-line front-end to this function.
@@ -1410,7 +1410,7 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
change_re.sub(record_sub_info,markers)
# process each tuple inserting our special marks that won't be
# noticed by an xml/html escaper.
- for key,(begin,end) in sub_info[::-1]:
+ for key,(begin,end) in reversed(sub_info):
text = text[0:begin]+'\0'+key+text[begin:end]+'\1'+text[end:]
text = text[2:]
# Handle case of add/delete entire line
@@ -1448,10 +1448,7 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
# are a concatenation of the first character of each of the 4 lines
# so we can do some very readable comparisons.
while len(lines) < 4:
- try:
- lines.append(next(diff_lines_iterator))
- except StopIteration:
- lines.append('X')
+ lines.append(next(diff_lines_iterator, 'X'))
s = ''.join([line[0] for line in lines])
if s.startswith('X'):
# When no more lines, pump out any remaining blank lines so the
@@ -1514,7 +1511,7 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
num_blanks_to_yield -= 1
yield ('','\n'),None,True
if s.startswith('X'):
- raise StopIteration
+ return
else:
yield from_line,to_line,True
@@ -1679,7 +1676,7 @@ class HtmlDiff(object):
tabsize -- tab stop spacing, defaults to 8.
wrapcolumn -- column number where lines are broken and wrapped,
defaults to None where lines are not wrapped.
- linejunk,charjunk -- keyword arguments passed into ndiff() (used to by
+ linejunk,charjunk -- keyword arguments passed into ndiff() (used by
HtmlDiff() to generate the side by side HTML differences). See
ndiff() documentation for argument default values and descriptions.
"""
diff --git a/Lib/dis.py b/Lib/dis.py
index 81cbe7f..d215bc5 100644
--- a/Lib/dis.py
+++ b/Lib/dis.py
@@ -29,7 +29,7 @@ def _try_compile(source, name):
return c
def dis(x=None, *, file=None):
- """Disassemble classes, methods, functions, or code.
+ """Disassemble classes, methods, functions, generators, or code.
With no argument, disassemble the last traceback.
@@ -41,6 +41,8 @@ def dis(x=None, *, file=None):
x = x.__func__
if hasattr(x, '__code__'): # Function
x = x.__code__
+ if hasattr(x, 'gi_code'): # Generator
+ x = x.gi_code
if hasattr(x, '__dict__'): # Class or module
items = sorted(x.__dict__.items())
for name, x1 in items:
@@ -99,11 +101,13 @@ def pretty_flags(flags):
return ", ".join(names)
def _get_code_object(x):
- """Helper to handle methods, functions, strings and raw code objects"""
+ """Helper to handle methods, functions, generators, strings and raw code objects"""
if hasattr(x, '__func__'): # Method
x = x.__func__
if hasattr(x, '__code__'): # Function
x = x.__code__
+ if hasattr(x, 'gi_code'): # Generator
+ x = x.gi_code
if isinstance(x, str): # Source code
x = _try_compile(x, "<disassembly>")
if hasattr(x, 'co_code'): # Code object
diff --git a/Lib/distutils/__init__.py b/Lib/distutils/__init__.py
index e878281..67ec78b 100644
--- a/Lib/distutils/__init__.py
+++ b/Lib/distutils/__init__.py
@@ -13,5 +13,5 @@ used from a setup script as
# Updated automatically by the Python release process.
#
#--start constants--
-__version__ = "3.4.3rc1"
+__version__ = "3.5.0a1"
#--end constants--
diff --git a/Lib/distutils/command/build.py b/Lib/distutils/command/build.py
index cfc15cf..337dd0b 100644
--- a/Lib/distutils/command/build.py
+++ b/Lib/distutils/command/build.py
@@ -36,6 +36,8 @@ class build(Command):
"(default: %s)" % get_platform()),
('compiler=', 'c',
"specify the compiler type"),
+ ('parallel=', 'j',
+ "number of parallel build jobs"),
('debug', 'g',
"compile extensions and libraries with debugging information"),
('force', 'f',
@@ -65,6 +67,7 @@ class build(Command):
self.debug = None
self.force = 0
self.executable = None
+ self.parallel = None
def finalize_options(self):
if self.plat_name is None:
@@ -116,6 +119,12 @@ class build(Command):
if self.executable is None:
self.executable = os.path.normpath(sys.executable)
+ if isinstance(self.parallel, str):
+ try:
+ self.parallel = int(self.parallel)
+ except ValueError:
+ raise DistutilsOptionError("parallel should be an integer")
+
def run(self):
# Run all relevant sub-commands. This will be some subset of:
# - build_py - pure Python modules
diff --git a/Lib/distutils/command/build_ext.py b/Lib/distutils/command/build_ext.py
index acbe648..c5a3ce1 100644
--- a/Lib/distutils/command/build_ext.py
+++ b/Lib/distutils/command/build_ext.py
@@ -4,7 +4,10 @@ Implements the Distutils 'build_ext' command, for building extension
modules (currently limited to C extensions, should accommodate C++
extensions ASAP)."""
-import sys, os, re
+import contextlib
+import os
+import re
+import sys
from distutils.core import Command
from distutils.errors import *
from distutils.sysconfig import customize_compiler, get_python_version
@@ -85,6 +88,8 @@ class build_ext(Command):
"forcibly build everything (ignore file timestamps)"),
('compiler=', 'c',
"specify the compiler type"),
+ ('parallel=', 'j',
+ "number of parallel build jobs"),
('swig-cpp', None,
"make SWIG create C++ files (default is C)"),
('swig-opts=', None,
@@ -124,6 +129,7 @@ class build_ext(Command):
self.swig_cpp = None
self.swig_opts = None
self.user = None
+ self.parallel = None
def finalize_options(self):
from distutils import sysconfig
@@ -134,6 +140,7 @@ class build_ext(Command):
('compiler', 'compiler'),
('debug', 'debug'),
('force', 'force'),
+ ('parallel', 'parallel'),
('plat_name', 'plat_name'),
)
@@ -202,7 +209,7 @@ class build_ext(Command):
if MSVC_VERSION >= 9:
# Use the .lib files for the correct architecture
if self.plat_name == 'win32':
- suffix = ''
+ suffix = 'win32'
else:
# win-amd64 or win-ia64
suffix = self.plat_name[4:]
@@ -274,6 +281,12 @@ class build_ext(Command):
self.library_dirs.append(user_lib)
self.rpath.append(user_lib)
+ if isinstance(self.parallel, str):
+ try:
+ self.parallel = int(self.parallel)
+ except ValueError:
+ raise DistutilsOptionError("parallel should be an integer")
+
def run(self):
from distutils.ccompiler import new_compiler
@@ -442,15 +455,45 @@ class build_ext(Command):
def build_extensions(self):
# First, sanity-check the 'extensions' list
self.check_extensions_list(self.extensions)
+ if self.parallel:
+ self._build_extensions_parallel()
+ else:
+ self._build_extensions_serial()
+
+ def _build_extensions_parallel(self):
+ workers = self.parallel
+ if self.parallel is True:
+ workers = os.cpu_count() # may return None
+ try:
+ from concurrent.futures import ThreadPoolExecutor
+ except ImportError:
+ workers = None
+
+ if workers is None:
+ self._build_extensions_serial()
+ return
+
+ with ThreadPoolExecutor(max_workers=workers) as executor:
+ futures = [executor.submit(self.build_extension, ext)
+ for ext in self.extensions]
+ for ext, fut in zip(self.extensions, futures):
+ with self._filter_build_errors(ext):
+ fut.result()
+ def _build_extensions_serial(self):
for ext in self.extensions:
- try:
+ with self._filter_build_errors(ext):
self.build_extension(ext)
- except (CCompilerError, DistutilsError, CompileError) as e:
- if not ext.optional:
- raise
- self.warn('building extension "%s" failed: %s' %
- (ext.name, e))
+
+ @contextlib.contextmanager
+ def _filter_build_errors(self, ext):
+ try:
+ yield
+ except (CCompilerError, DistutilsError, CompileError) as e:
+ if not ext.optional:
+ raise
+ self.warn('building extension "%s" failed: %s' %
+ (ext.name, e))
def build_extension(self, ext):
sources = ext.sources
@@ -502,15 +545,8 @@ class build_ext(Command):
extra_postargs=extra_args,
depends=ext.depends)
- # XXX -- this is a Vile HACK!
- #
- # The setup.py script for Python on Unix needs to be able to
- # get this list so it can perform all the clean up needed to
- # avoid keeping object files around when cleaning out a failed
- # build of an extension module. Since Distutils does not
- # track dependencies, we have to get rid of intermediates to
- # ensure all the intermediates will be properly re-built.
- #
+ # XXX outdated variable, kept here in case third-part code
+ # needs it.
self._built_objects = objects[:]
# Now link the object files together into a "shared object" --
@@ -655,10 +691,7 @@ class build_ext(Command):
"""
from distutils.sysconfig import get_config_var
ext_path = ext_name.split('.')
- # extensions in debug_mode are named 'module_d.pyd' under windows
ext_suffix = get_config_var('EXT_SUFFIX')
- if os.name == 'nt' and self.debug:
- return os.path.join(*ext_path) + '_d' + ext_suffix
return os.path.join(*ext_path) + ext_suffix
def get_export_symbols(self, ext):
diff --git a/Lib/distutils/command/install.py b/Lib/distutils/command/install.py
index d768dc5..67db007 100644
--- a/Lib/distutils/command/install.py
+++ b/Lib/distutils/command/install.py
@@ -51,7 +51,7 @@ if HAS_USER_SITE:
'purelib': '$usersite',
'platlib': '$usersite',
'headers': '$userbase/Python$py_version_nodot/Include/$dist_name',
- 'scripts': '$userbase/Scripts',
+ 'scripts': '$userbase/Python$py_version_nodot/Scripts',
'data' : '$userbase',
}
diff --git a/Lib/distutils/command/upload.py b/Lib/distutils/command/upload.py
index 1a96e22..1c4fc48 100644
--- a/Lib/distutils/command/upload.py
+++ b/Lib/distutils/command/upload.py
@@ -1,11 +1,14 @@
-"""distutils.command.upload
+"""
+distutils.command.upload
-Implements the Distutils 'upload' subcommand (upload package to PyPI)."""
+Implements the Distutils 'upload' subcommand (upload package to a package
+index).
+"""
-import sys
-import os, io
-import socket
+import os
+import io
import platform
+import hashlib
from base64 import standard_b64encode
from urllib.request import urlopen, Request, HTTPError
from urllib.parse import urlparse
@@ -14,12 +17,6 @@ from distutils.core import PyPIRCCommand
from distutils.spawn import spawn
from distutils import log
-# this keeps compatibility for 2.3 and 2.4
-if sys.version < "2.5":
- from md5 import md5
-else:
- from hashlib import md5
-
class upload(PyPIRCCommand):
description = "upload binary package to PyPI"
@@ -60,7 +57,8 @@ class upload(PyPIRCCommand):
def run(self):
if not self.distribution.dist_files:
- raise DistutilsOptionError("No dist file created in earlier command")
+ msg = "No dist file created in earlier command"
+ raise DistutilsOptionError(msg)
for command, pyversion, filename in self.distribution.dist_files:
self.upload_file(command, pyversion, filename)
@@ -103,10 +101,10 @@ class upload(PyPIRCCommand):
'content': (os.path.basename(filename),content),
'filetype': command,
'pyversion': pyversion,
- 'md5_digest': md5(content).hexdigest(),
+ 'md5_digest': hashlib.md5(content).hexdigest(),
# additional meta-data
- 'metadata_version' : '1.0',
+ 'metadata_version': '1.0',
'summary': meta.get_description(),
'home_page': meta.get_url(),
'author': meta.get_contact(),
@@ -149,7 +147,7 @@ class upload(PyPIRCCommand):
for key, value in data.items():
title = '\r\nContent-Disposition: form-data; name="%s"' % key
# handle multiple entries for the same name
- if type(value) != type([]):
+ if not isinstance(value, list):
value = [value]
for value in value:
if type(value) is tuple:
@@ -166,13 +164,15 @@ class upload(PyPIRCCommand):
body.write(end_boundary)
body = body.getvalue()
- self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
+ msg = "Submitting %s to %s" % (filename, self.repository)
+ self.announce(msg, log.INFO)
# build the Request
- headers = {'Content-type':
- 'multipart/form-data; boundary=%s' % boundary,
- 'Content-length': str(len(body)),
- 'Authorization': auth}
+ headers = {
+ 'Content-type': 'multipart/form-data; boundary=%s' % boundary,
+ 'Content-length': str(len(body)),
+ 'Authorization': auth,
+ }
request = Request(self.repository, data=body,
headers=headers)
diff --git a/Lib/distutils/command/wininst-14.0-amd64.exe b/Lib/distutils/command/wininst-14.0-amd64.exe
new file mode 100644
index 0000000..43b85b6
--- /dev/null
+++ b/Lib/distutils/command/wininst-14.0-amd64.exe
Binary files differ
diff --git a/Lib/distutils/command/wininst-14.0.exe b/Lib/distutils/command/wininst-14.0.exe
new file mode 100644
index 0000000..764524d
--- /dev/null
+++ b/Lib/distutils/command/wininst-14.0.exe
Binary files differ
diff --git a/Lib/distutils/dist.py b/Lib/distutils/dist.py
index 7eb04bc..ffb33ff6 100644
--- a/Lib/distutils/dist.py
+++ b/Lib/distutils/dist.py
@@ -4,7 +4,9 @@ Provides the Distribution class, which represents the module distribution
being built/installed/distributed.
"""
-import sys, os, re
+import sys
+import os
+import re
from email import message_from_file
try:
@@ -22,7 +24,7 @@ from distutils.debug import DEBUG
# the same as a Python NAME -- I don't allow leading underscores. The fact
# that they're very similar is no coincidence; the default naming scheme is
# to look for a Python module named after the command.
-command_re = re.compile (r'^[a-zA-Z]([a-zA-Z0-9_]*)$')
+command_re = re.compile(r'^[a-zA-Z]([a-zA-Z0-9_]*)$')
class Distribution:
@@ -39,7 +41,6 @@ class Distribution:
See the code for 'setup()', in core.py, for details.
"""
-
# 'global_options' describes the command-line options that may be
# supplied to the setup script prior to any actual commands.
# Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of
@@ -48,12 +49,13 @@ class Distribution:
# don't want to pollute the commands with too many options that they
# have minimal control over.
# The fourth entry for verbose means that it can be repeated.
- global_options = [('verbose', 'v', "run verbosely (default)", 1),
- ('quiet', 'q', "run quietly (turns verbosity off)"),
- ('dry-run', 'n', "don't actually do anything"),
- ('help', 'h', "show detailed help message"),
- ('no-user-cfg', None,
- 'ignore pydistutils.cfg in your home directory'),
+ global_options = [
+ ('verbose', 'v', "run verbosely (default)", 1),
+ ('quiet', 'q', "run quietly (turns verbosity off)"),
+ ('dry-run', 'n', "don't actually do anything"),
+ ('help', 'h', "show detailed help message"),
+ ('no-user-cfg', None,
+ 'ignore pydistutils.cfg in your home directory'),
]
# 'common_usage' is a short (2-3 line) string describing the common
@@ -115,10 +117,9 @@ Common commands: (see '--help-commands' for more)
# negative options are options that exclude other options
negative_opt = {'quiet': 'verbose'}
-
# -- Creation/initialization methods -------------------------------
- def __init__ (self, attrs=None):
+ def __init__(self, attrs=None):
"""Construct a new Distribution instance: initialize all the
attributes of a Distribution, and then use 'attrs' (a dictionary
mapping attribute names to values) to assign some of those
@@ -532,15 +533,15 @@ Common commands: (see '--help-commands' for more)
# to be sure that the basic "command" interface is implemented.
if not issubclass(cmd_class, Command):
raise DistutilsClassError(
- "command class %s must subclass Command" % cmd_class)
+ "command class %s must subclass Command" % cmd_class)
# Also make sure that the command object provides a list of its
# known options.
if not (hasattr(cmd_class, 'user_options') and
isinstance(cmd_class.user_options, list)):
- raise DistutilsClassError(("command class %s must provide " +
- "'user_options' attribute (a list of tuples)") % \
- cmd_class)
+ msg = ("command class %s must provide "
+ "'user_options' attribute (a list of tuples)")
+ raise DistutilsClassError(msg % cmd_class)
# If the command class has a list of negative alias options,
# merge it in with the global negative aliases.
@@ -552,12 +553,11 @@ Common commands: (see '--help-commands' for more)
# Check for help_options in command class. They have a different
# format (tuple of four) so we need to preprocess them here.
if (hasattr(cmd_class, 'help_options') and
- isinstance(cmd_class.help_options, list)):
+ isinstance(cmd_class.help_options, list)):
help_options = fix_help_options(cmd_class.help_options)
else:
help_options = []
-
# All commands support the global options too, just by adding
# in 'global_options'.
parser.set_option_table(self.global_options +
@@ -570,7 +570,7 @@ Common commands: (see '--help-commands' for more)
return
if (hasattr(cmd_class, 'help_options') and
- isinstance(cmd_class.help_options, list)):
+ isinstance(cmd_class.help_options, list)):
help_option_found=0
for (help_option, short, desc, func) in cmd_class.help_options:
if hasattr(opts, parser.get_attr_name(help_option)):
@@ -647,7 +647,7 @@ Common commands: (see '--help-commands' for more)
else:
klass = self.get_command_class(command)
if (hasattr(klass, 'help_options') and
- isinstance(klass.help_options, list)):
+ isinstance(klass.help_options, list)):
parser.set_option_table(klass.user_options +
fix_help_options(klass.help_options))
else:
@@ -814,7 +814,7 @@ Common commands: (see '--help-commands' for more)
klass_name = command
try:
- __import__ (module_name)
+ __import__(module_name)
module = sys.modules[module_name]
except ImportError:
continue
@@ -823,8 +823,8 @@ Common commands: (see '--help-commands' for more)
klass = getattr(module, klass_name)
except AttributeError:
raise DistutilsModuleError(
- "invalid command '%s' (no class '%s' in module '%s')"
- % (command, klass_name, module_name))
+ "invalid command '%s' (no class '%s' in module '%s')"
+ % (command, klass_name, module_name))
self.cmdclass[command] = klass
return klass
@@ -840,7 +840,7 @@ Common commands: (see '--help-commands' for more)
cmd_obj = self.command_obj.get(command)
if not cmd_obj and create:
if DEBUG:
- self.announce("Distribution.get_command_obj(): " \
+ self.announce("Distribution.get_command_obj(): "
"creating '%s' command object" % command)
klass = self.get_command_class(command)
@@ -897,8 +897,8 @@ Common commands: (see '--help-commands' for more)
setattr(command_obj, option, value)
else:
raise DistutilsOptionError(
- "error in %s: command '%s' has no such option '%s'"
- % (source, command_name, option))
+ "error in %s: command '%s' has no such option '%s'"
+ % (source, command_name, option))
except ValueError as msg:
raise DistutilsOptionError(msg)
@@ -974,7 +974,6 @@ Common commands: (see '--help-commands' for more)
cmd_obj.run()
self.have_run[command] = 1
-
# -- Distribution query methods ------------------------------------
def has_pure_modules(self):
@@ -1112,17 +1111,17 @@ class DistributionMetadata:
"""
version = '1.0'
if (self.provides or self.requires or self.obsoletes or
- self.classifiers or self.download_url):
+ self.classifiers or self.download_url):
version = '1.1'
file.write('Metadata-Version: %s\n' % version)
- file.write('Name: %s\n' % self.get_name() )
- file.write('Version: %s\n' % self.get_version() )
- file.write('Summary: %s\n' % self.get_description() )
- file.write('Home-page: %s\n' % self.get_url() )
- file.write('Author: %s\n' % self.get_contact() )
- file.write('Author-email: %s\n' % self.get_contact_email() )
- file.write('License: %s\n' % self.get_license() )
+ file.write('Name: %s\n' % self.get_name())
+ file.write('Version: %s\n' % self.get_version())
+ file.write('Summary: %s\n' % self.get_description())
+ file.write('Home-page: %s\n' % self.get_url())
+ file.write('Author: %s\n' % self.get_contact())
+ file.write('Author-email: %s\n' % self.get_contact_email())
+ file.write('License: %s\n' % self.get_license())
if self.download_url:
file.write('Download-URL: %s\n' % self.download_url)
@@ -1131,7 +1130,7 @@ class DistributionMetadata:
keywords = ','.join(self.get_keywords())
if keywords:
- file.write('Keywords: %s\n' % keywords )
+ file.write('Keywords: %s\n' % keywords)
self._write_list(file, 'Platform', self.get_platforms())
self._write_list(file, 'Classifier', self.get_classifiers())
diff --git a/Lib/distutils/extension.py b/Lib/distutils/extension.py
index a93655a..7efbb74 100644
--- a/Lib/distutils/extension.py
+++ b/Lib/distutils/extension.py
@@ -131,6 +131,14 @@ class Extension:
msg = "Unknown Extension options: %s" % options
warnings.warn(msg)
+ def __repr__(self):
+ return '<%s.%s(%r) at %#x>' % (
+ self.__class__.__module__,
+ self.__class__.__qualname__,
+ self.name,
+ id(self))
+
+
def read_setup_file(filename):
"""Reads a Setup file and returns Extension instances."""
from distutils.sysconfig import (parse_makefile, expand_makefile_vars,
diff --git a/Lib/distutils/msvc9compiler.py b/Lib/distutils/msvc9compiler.py
index 9688f20..d1374ef 100644
--- a/Lib/distutils/msvc9compiler.py
+++ b/Lib/distutils/msvc9compiler.py
@@ -179,6 +179,9 @@ def get_build_version():
i = i + len(prefix)
s, rest = sys.version[i:].split(" ", 1)
majorVersion = int(s[:-2]) - 6
+ if majorVersion >= 13:
+ # v13 was skipped and should be v14
+ majorVersion += 1
minorVersion = int(s[2:3]) / 10.0
# I don't think paths are affected by minor version in version 6
if majorVersion == 6:
diff --git a/Lib/distutils/msvccompiler.py b/Lib/distutils/msvccompiler.py
index 8116656..1048cd4 100644
--- a/Lib/distutils/msvccompiler.py
+++ b/Lib/distutils/msvccompiler.py
@@ -157,6 +157,9 @@ def get_build_version():
i = i + len(prefix)
s, rest = sys.version[i:].split(" ", 1)
majorVersion = int(s[:-2]) - 6
+ if majorVersion >= 13:
+ # v13 was skipped and should be v14
+ majorVersion += 1
minorVersion = int(s[2:3]) / 10.0
# I don't think paths are affected by minor version in version 6
if majorVersion == 6:
diff --git a/Lib/distutils/spawn.py b/Lib/distutils/spawn.py
index 22e87e8..5dd415a 100644
--- a/Lib/distutils/spawn.py
+++ b/Lib/distutils/spawn.py
@@ -137,9 +137,6 @@ def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
try:
pid, status = os.waitpid(pid, 0)
except OSError as exc:
- import errno
- if exc.errno == errno.EINTR:
- continue
if not DEBUG:
cmd = executable
raise DistutilsExecError(
diff --git a/Lib/distutils/sysconfig.py b/Lib/distutils/sysconfig.py
index a1452fe..573724d 100644
--- a/Lib/distutils/sysconfig.py
+++ b/Lib/distutils/sysconfig.py
@@ -9,6 +9,7 @@ Written by: Fred L. Drake, Jr.
Email: <fdrake@acm.org>
"""
+import _imp
import os
import re
import sys
@@ -22,23 +23,15 @@ BASE_PREFIX = os.path.normpath(sys.base_prefix)
BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix)
# Path to the base directory of the project. On Windows the binary may
-# live in project/PCBuild9. If we're dealing with an x64 Windows build,
-# it'll live in project/PCbuild/amd64.
+# live in project/PCBuild/win32 or project/PCBuild/amd64.
# set for cross builds
if "_PYTHON_PROJECT_BASE" in os.environ:
project_base = os.path.abspath(os.environ["_PYTHON_PROJECT_BASE"])
else:
project_base = os.path.dirname(os.path.abspath(sys.executable))
-if os.name == "nt" and "pcbuild" in project_base[-8:].lower():
- project_base = os.path.abspath(os.path.join(project_base, os.path.pardir))
-# PC/VS7.1
-if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower():
- project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
- os.path.pardir))
-# PC/AMD64
-if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower():
- project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
- os.path.pardir))
+if (os.name == 'nt' and
+ project_base.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))):
+ project_base = os.path.dirname(os.path.dirname(project_base))
# python_build: (Boolean) if true, we're either building Python or
# building an extension with an un-installed Python, so we use
@@ -51,11 +44,9 @@ def _is_python_source_dir(d):
return True
return False
_sys_home = getattr(sys, '_home', None)
-if _sys_home and os.name == 'nt' and \
- _sys_home.lower().endswith(('pcbuild', 'pcbuild\\amd64')):
- _sys_home = os.path.dirname(_sys_home)
- if _sys_home.endswith('pcbuild'): # must be amd64
- _sys_home = os.path.dirname(_sys_home)
+if (_sys_home and os.name == 'nt' and
+ _sys_home.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))):
+ _sys_home = os.path.dirname(os.path.dirname(_sys_home))
def _python_build():
if _sys_home:
return _is_python_source_dir(_sys_home)
@@ -468,7 +459,7 @@ def _init_nt():
# XXX hmmm.. a normal install puts include files here
g['INCLUDEPY'] = get_python_inc(plat_specific=0)
- g['EXT_SUFFIX'] = '.pyd'
+ g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
g['EXE'] = ".exe"
g['VERSION'] = get_python_version().replace(".", "")
g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
diff --git a/Lib/distutils/tests/test_build_ext.py b/Lib/distutils/tests/test_build_ext.py
index b9f407f..366ffbe 100644
--- a/Lib/distutils/tests/test_build_ext.py
+++ b/Lib/distutils/tests/test_build_ext.py
@@ -37,6 +37,9 @@ class BuildExtTestCase(TempdirManager,
from distutils.command import build_ext
build_ext.USER_BASE = site.USER_BASE
+ def build_ext(self, *args, **kwargs):
+ return build_ext(*args, **kwargs)
+
def test_build_ext(self):
global ALREADY_TESTED
copy_xxmodule_c(self.tmp_dir)
@@ -44,7 +47,7 @@ class BuildExtTestCase(TempdirManager,
xx_ext = Extension('xx', [xx_c])
dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
dist.package_dir = self.tmp_dir
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
fixup_build_ext(cmd)
cmd.build_lib = self.tmp_dir
cmd.build_temp = self.tmp_dir
@@ -91,7 +94,7 @@ class BuildExtTestCase(TempdirManager,
def test_solaris_enable_shared(self):
dist = Distribution({'name': 'xx'})
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
old = sys.platform
sys.platform = 'sunos' # fooling finalize_options
@@ -113,7 +116,7 @@ class BuildExtTestCase(TempdirManager,
def test_user_site(self):
import site
dist = Distribution({'name': 'xx'})
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
# making sure the user option is there
options = [name for name, short, lable in
@@ -144,14 +147,14 @@ class BuildExtTestCase(TempdirManager,
# with the optional argument.
modules = [Extension('foo', ['xxx'], optional=False)]
dist = Distribution({'name': 'xx', 'ext_modules': modules})
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.ensure_finalized()
self.assertRaises((UnknownFileError, CompileError),
cmd.run) # should raise an error
modules = [Extension('foo', ['xxx'], optional=True)]
dist = Distribution({'name': 'xx', 'ext_modules': modules})
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.ensure_finalized()
cmd.run() # should pass
@@ -160,7 +163,7 @@ class BuildExtTestCase(TempdirManager,
# etc.) are in the include search path.
modules = [Extension('foo', ['xxx'], optional=False)]
dist = Distribution({'name': 'xx', 'ext_modules': modules})
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.finalize_options()
from distutils import sysconfig
@@ -172,14 +175,14 @@ class BuildExtTestCase(TempdirManager,
# make sure cmd.libraries is turned into a list
# if it's a string
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.libraries = 'my_lib, other_lib lastlib'
cmd.finalize_options()
self.assertEqual(cmd.libraries, ['my_lib', 'other_lib', 'lastlib'])
# make sure cmd.library_dirs is turned into a list
# if it's a string
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.library_dirs = 'my_lib_dir%sother_lib_dir' % os.pathsep
cmd.finalize_options()
self.assertIn('my_lib_dir', cmd.library_dirs)
@@ -187,7 +190,7 @@ class BuildExtTestCase(TempdirManager,
# make sure rpath is turned into a list
# if it's a string
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.rpath = 'one%stwo' % os.pathsep
cmd.finalize_options()
self.assertEqual(cmd.rpath, ['one', 'two'])
@@ -196,32 +199,32 @@ class BuildExtTestCase(TempdirManager,
# make sure define is turned into 2-tuples
# strings if they are ','-separated strings
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.define = 'one,two'
cmd.finalize_options()
self.assertEqual(cmd.define, [('one', '1'), ('two', '1')])
# make sure undef is turned into a list of
# strings if they are ','-separated strings
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.undef = 'one,two'
cmd.finalize_options()
self.assertEqual(cmd.undef, ['one', 'two'])
# make sure swig_opts is turned into a list
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.swig_opts = None
cmd.finalize_options()
self.assertEqual(cmd.swig_opts, [])
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.swig_opts = '1 2'
cmd.finalize_options()
self.assertEqual(cmd.swig_opts, ['1', '2'])
def test_check_extensions_list(self):
dist = Distribution()
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.finalize_options()
#'extensions' option must be a list of Extension instances
@@ -270,7 +273,7 @@ class BuildExtTestCase(TempdirManager,
def test_get_source_files(self):
modules = [Extension('foo', ['xxx'], optional=False)]
dist = Distribution({'name': 'xx', 'ext_modules': modules})
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.ensure_finalized()
self.assertEqual(cmd.get_source_files(), ['xxx'])
@@ -279,7 +282,7 @@ class BuildExtTestCase(TempdirManager,
# should not be overriden by a compiler instance
# when the command is run
dist = Distribution()
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.compiler = 'unix'
cmd.ensure_finalized()
cmd.run()
@@ -292,7 +295,7 @@ class BuildExtTestCase(TempdirManager,
ext = Extension('foo', [c_file], optional=False)
dist = Distribution({'name': 'xx',
'ext_modules': [ext]})
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
fixup_build_ext(cmd)
cmd.ensure_finalized()
self.assertEqual(len(cmd.get_outputs()), 1)
@@ -355,7 +358,7 @@ class BuildExtTestCase(TempdirManager,
#etree_ext = Extension('lxml.etree', [etree_c])
#dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
dist = Distribution()
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.inplace = 1
cmd.distribution.package_dir = {'': 'src'}
cmd.distribution.packages = ['lxml', 'lxml.html']
@@ -462,7 +465,7 @@ class BuildExtTestCase(TempdirManager,
'ext_modules': [deptarget_ext]
})
dist.package_dir = self.tmp_dir
- cmd = build_ext(dist)
+ cmd = self.build_ext(dist)
cmd.build_lib = self.tmp_dir
cmd.build_temp = self.tmp_dir
@@ -481,8 +484,19 @@ class BuildExtTestCase(TempdirManager,
self.fail("Wrong deployment target during compilation")
+class ParallelBuildExtTestCase(BuildExtTestCase):
+
+ def build_ext(self, *args, **kwargs):
+ build_ext = super().build_ext(*args, **kwargs)
+ build_ext.parallel = True
+ return build_ext
+
+
def test_suite():
- return unittest.makeSuite(BuildExtTestCase)
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(BuildExtTestCase))
+ suite.addTest(unittest.makeSuite(ParallelBuildExtTestCase))
+ return suite
if __name__ == '__main__':
- support.run_unittest(test_suite())
+ support.run_unittest(__name__)
diff --git a/Lib/distutils/tests/test_install.py b/Lib/distutils/tests/test_install.py
index 18e1e57..9313330 100644
--- a/Lib/distutils/tests/test_install.py
+++ b/Lib/distutils/tests/test_install.py
@@ -20,8 +20,6 @@ from distutils.tests import support
def _make_ext_name(modname):
- if os.name == 'nt' and sys.executable.endswith('_d.exe'):
- modname += '_d'
return modname + sysconfig.get_config_var('EXT_SUFFIX')
diff --git a/Lib/distutils/version.py b/Lib/distutils/version.py
index ebcab84..af14cc1 100644
--- a/Lib/distutils/version.py
+++ b/Lib/distutils/version.py
@@ -48,12 +48,6 @@ class Version:
return c
return c == 0
- def __ne__(self, other):
- c = self._cmp(other)
- if c is NotImplemented:
- return c
- return c != 0
-
def __lt__(self, other):
c = self._cmp(other)
if c is NotImplemented:
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 64e6d71..7d5bcf4 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -530,8 +530,9 @@ class DocTest:
examples = '1 example'
else:
examples = '%d examples' % len(self.examples)
- return ('<DocTest %s from %s:%s (%s)>' %
- (self.name, self.filename, self.lineno, examples))
+ return ('<%s %s from %s:%s (%s)>' %
+ (self.__class__.__name__,
+ self.name, self.filename, self.lineno, examples))
def __eq__(self, other):
if type(self) is not type(other):
@@ -978,7 +979,8 @@ class DocTestFinder:
for valname, val in obj.__dict__.items():
valname = '%s.%s' % (name, valname)
# Recurse to functions & classes.
- if ((inspect.isroutine(val) or inspect.isclass(val)) and
+ if ((inspect.isroutine(inspect.unwrap(val))
+ or inspect.isclass(val)) and
self._from_module(module, val)):
self._find(tests, val, valname, module, source_lines,
globs, seen)
@@ -2367,15 +2369,6 @@ def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None,
suite = _DocTestSuite()
suite.addTest(SkipDocTestCase(module))
return suite
- elif not tests:
- # Why do we want to do this? Because it reveals a bug that might
- # otherwise be hidden.
- # It is probably a bug that this exception is not also raised if the
- # number of doctest examples in tests is zero (i.e. if no doctest
- # examples were found). However, we should probably not be raising
- # an exception at all here, though it is too late to make this change
- # for a maintenance release. See also issue #14649.
- raise ValueError(module, "has no docstrings")
tests.sort()
suite = _DocTestSuite()
diff --git a/Lib/email/__init__.py b/Lib/email/__init__.py
index ff16f6a..fae8724 100644
--- a/Lib/email/__init__.py
+++ b/Lib/email/__init__.py
@@ -4,8 +4,6 @@
"""A package for parsing, handling, and generating email messages."""
-__version__ = '5.1.0'
-
__all__ = [
'base64mime',
'charset',
diff --git a/Lib/email/charset.py b/Lib/email/charset.py
index e999472..ee56404 100644
--- a/Lib/email/charset.py
+++ b/Lib/email/charset.py
@@ -249,9 +249,6 @@ class Charset:
def __eq__(self, other):
return str(self) == str(other).lower()
- def __ne__(self, other):
- return not self.__eq__(other)
-
def get_body_encoding(self):
"""Return the content-transfer-encoding used for body encoding.
diff --git a/Lib/email/header.py b/Lib/email/header.py
index 9c89589..6820ea1 100644
--- a/Lib/email/header.py
+++ b/Lib/email/header.py
@@ -262,9 +262,6 @@ class Header:
# args and do another comparison.
return other == str(self)
- def __ne__(self, other):
- return not self == other
-
def append(self, s, charset=None, errors='strict'):
"""Append a string to the MIME header.
diff --git a/Lib/email/headerregistry.py b/Lib/email/headerregistry.py
index 911a2af..468ca9e 100644
--- a/Lib/email/headerregistry.py
+++ b/Lib/email/headerregistry.py
@@ -81,7 +81,8 @@ class Address:
return lp
def __repr__(self):
- return "Address(display_name={!r}, username={!r}, domain={!r})".format(
+ return "{}(display_name={!r}, username={!r}, domain={!r})".format(
+ self.__class__.__name__,
self.display_name, self.username, self.domain)
def __str__(self):
@@ -132,7 +133,8 @@ class Group:
return self._addresses
def __repr__(self):
- return "Group(display_name={!r}, addresses={!r}".format(
+ return "{}(display_name={!r}, addresses={!r}".format(
+ self.__class__.__name__,
self.display_name, self.addresses)
def __str__(self):
diff --git a/Lib/email/message.py b/Lib/email/message.py
index 2f37dbb..3d3138f 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -930,17 +930,6 @@ class Message:
# I.e. def walk(self): ...
from email.iterators import walk
-# XXX Support for temporary deprecation hack for is_attachment property.
-class _IsAttachment:
- def __init__(self, value):
- self.value = value
- def __call__(self):
- return self.value
- def __bool__(self):
- warnings.warn("is_attachment will be a method, not a property, in 3.5",
- DeprecationWarning,
- stacklevel=3)
- return self.value
class MIMEPart(Message):
@@ -950,12 +939,9 @@ class MIMEPart(Message):
policy = default
Message.__init__(self, policy)
- @property
def is_attachment(self):
c_d = self.get('content-disposition')
- result = False if c_d is None else c_d.content_disposition == 'attachment'
- # XXX transitional hack to raise deprecation if not called.
- return _IsAttachment(result)
+ return False if c_d is None else c_d.content_disposition == 'attachment'
def _find_body(self, part, preferencelist):
if part.is_attachment():
diff --git a/Lib/email/mime/text.py b/Lib/email/mime/text.py
index ec18b85..479928e 100644
--- a/Lib/email/mime/text.py
+++ b/Lib/email/mime/text.py
@@ -6,6 +6,7 @@
__all__ = ['MIMEText']
+from email.charset import Charset
from email.mime.nonmultipart import MIMENonMultipart
@@ -34,6 +35,8 @@ class MIMEText(MIMENonMultipart):
_charset = 'us-ascii'
except UnicodeEncodeError:
_charset = 'utf-8'
+ if isinstance(_charset, Charset):
+ _charset = str(_charset)
MIMENonMultipart.__init__(self, 'text', _subtype,
**{'charset': _charset})
diff --git a/Lib/encodings/cp65001.py b/Lib/encodings/cp65001.py
index 287eb87..95cb2ae 100644
--- a/Lib/encodings/cp65001.py
+++ b/Lib/encodings/cp65001.py
@@ -11,20 +11,23 @@ if not hasattr(codecs, 'code_page_encode'):
### Codec APIs
encode = functools.partial(codecs.code_page_encode, 65001)
-decode = functools.partial(codecs.code_page_decode, 65001)
+_decode = functools.partial(codecs.code_page_decode, 65001)
+
+def decode(input, errors='strict'):
+ return codecs.code_page_decode(65001, input, errors, True)
class IncrementalEncoder(codecs.IncrementalEncoder):
def encode(self, input, final=False):
return encode(input, self.errors)[0]
class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
- _buffer_decode = decode
+ _buffer_decode = _decode
class StreamWriter(codecs.StreamWriter):
encode = encode
class StreamReader(codecs.StreamReader):
- decode = decode
+ decode = _decode
### encodings module API
diff --git a/Lib/enum.py b/Lib/enum.py
index 9b19c1d..d744f8f 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -193,7 +193,7 @@ class EnumMeta(type):
enum_class.__new__ = Enum.__new__
return enum_class
- def __call__(cls, value, names=None, *, module=None, qualname=None, type=None):
+ def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1):
"""Either returns an existing member, or creates a new enum class.
This method is used both when an enum class is given a value to match
@@ -205,7 +205,7 @@ class EnumMeta(type):
`value` will be the name of the new class.
`names` should be either a string of white-space/comma delimited names
- (values will start at 1), or an iterator/mapping of name, value pairs.
+ (values will start at `start`), or an iterator/mapping of name, value pairs.
`module` should be set to the module this class is being created in;
if it is not set, an attempt to find that module will be made, but if
@@ -221,7 +221,7 @@ class EnumMeta(type):
if names is None: # simple value lookup
return cls.__new__(cls, value)
# otherwise, functional API: we're creating a new Enum type
- return cls._create_(value, names, module=module, qualname=qualname, type=type)
+ return cls._create_(value, names, module=module, qualname=qualname, type=type, start=start)
def __contains__(cls, member):
return isinstance(member, cls) and member._name_ in cls._member_map_
@@ -292,16 +292,16 @@ class EnumMeta(type):
raise AttributeError('Cannot reassign members.')
super().__setattr__(name, value)
- def _create_(cls, class_name, names=None, *, module=None, qualname=None, type=None):
+ def _create_(cls, class_name, names=None, *, module=None, qualname=None, type=None, start=1):
"""Convenience method to create a new Enum class.
`names` can be:
* A string containing member names, separated either with spaces or
- commas. Values are auto-numbered from 1.
- * An iterable of member names. Values are auto-numbered from 1.
+ commas. Values are incremented by 1 from `start`.
+ * An iterable of member names. Values are incremented by 1 from `start`.
* An iterable of (member name, value) pairs.
- * A mapping of member name -> value.
+ * A mapping of member name -> value pairs.
"""
metacls = cls.__class__
@@ -312,7 +312,7 @@ class EnumMeta(type):
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, 1)]
+ names = [(e, i) for (i, e) in enumerate(names, start)]
# Here, names is either an iterable of (name, value) or a mapping.
for item in names:
@@ -470,8 +470,7 @@ class Enum(metaclass=EnumMeta):
for m in cls.__dict__
if m[0] != '_'
]
- return (['__class__', '__doc__', '__module__', 'name', 'value'] +
- added_behavior)
+ return (['__class__', '__doc__', '__module__'] + added_behavior)
def __format__(self, format_spec):
# mixed-in Enums should use the mixed-in type's __format__, otherwise
diff --git a/Lib/formatter.py b/Lib/formatter.py
index 9338261..cc2f4ad 100644
--- a/Lib/formatter.py
+++ b/Lib/formatter.py
@@ -21,7 +21,7 @@ manage and inserting data into the output.
import sys
import warnings
warnings.warn('the formatter module is deprecated and will be removed in '
- 'Python 3.6', PendingDeprecationWarning)
+ 'Python 3.6', DeprecationWarning)
AS_IS = None
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 79e83ff..5ddc84c 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -70,7 +70,7 @@ class Fraction(numbers.Rational):
__slots__ = ('_numerator', '_denominator')
# We're immutable, so use __new__ not __init__
- def __new__(cls, numerator=0, denominator=None):
+ 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
@@ -104,7 +104,12 @@ class Fraction(numbers.Rational):
self = super(Fraction, cls).__new__(cls)
if denominator is None:
- if isinstance(numerator, numbers.Rational):
+ if type(numerator) is int:
+ self._numerator = numerator
+ self._denominator = 1
+ return self
+
+ elif isinstance(numerator, numbers.Rational):
self._numerator = numerator.numerator
self._denominator = numerator.denominator
return self
@@ -153,6 +158,9 @@ class Fraction(numbers.Rational):
raise TypeError("argument should be a string "
"or a Rational instance")
+ elif type(numerator) is int is type(denominator):
+ pass # *very* normal case
+
elif (isinstance(numerator, numbers.Rational) and
isinstance(denominator, numbers.Rational)):
numerator, denominator = (
@@ -165,9 +173,12 @@ class Fraction(numbers.Rational):
if denominator == 0:
raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
- g = gcd(numerator, denominator)
- self._numerator = numerator // g
- self._denominator = denominator // g
+ if _normalize:
+ g = gcd(numerator, denominator)
+ numerator //= g
+ denominator //= g
+ self._numerator = numerator
+ self._denominator = denominator
return self
@classmethod
@@ -277,7 +288,8 @@ class Fraction(numbers.Rational):
def __repr__(self):
"""repr(self)"""
- return ('Fraction(%s, %s)' % (self._numerator, self._denominator))
+ return '%s(%s, %s)' % (self.__class__.__name__,
+ self._numerator, self._denominator)
def __str__(self):
"""str(self)"""
@@ -395,17 +407,17 @@ class Fraction(numbers.Rational):
def _add(a, b):
"""a + b"""
- return Fraction(a.numerator * b.denominator +
- b.numerator * a.denominator,
- a.denominator * b.denominator)
+ da, db = a.denominator, b.denominator
+ return Fraction(a.numerator * db + b.numerator * da,
+ da * db)
__add__, __radd__ = _operator_fallbacks(_add, operator.add)
def _sub(a, b):
"""a - b"""
- return Fraction(a.numerator * b.denominator -
- b.numerator * a.denominator,
- a.denominator * b.denominator)
+ da, db = a.denominator, b.denominator
+ return Fraction(a.numerator * db - b.numerator * da,
+ da * db)
__sub__, __rsub__ = _operator_fallbacks(_sub, operator.sub)
@@ -453,10 +465,12 @@ class Fraction(numbers.Rational):
power = b.numerator
if power >= 0:
return Fraction(a._numerator ** power,
- a._denominator ** power)
+ a._denominator ** power,
+ _normalize=False)
else:
return Fraction(a._denominator ** -power,
- a._numerator ** -power)
+ a._numerator ** -power,
+ _normalize=False)
else:
# A fractional power will generally produce an
# irrational number.
@@ -480,15 +494,15 @@ class Fraction(numbers.Rational):
def __pos__(a):
"""+a: Coerces a subclass instance to Fraction"""
- return Fraction(a._numerator, a._denominator)
+ return Fraction(a._numerator, a._denominator, _normalize=False)
def __neg__(a):
"""-a"""
- return Fraction(-a._numerator, a._denominator)
+ return Fraction(-a._numerator, a._denominator, _normalize=False)
def __abs__(a):
"""abs(a)"""
- return Fraction(abs(a._numerator), a._denominator)
+ return Fraction(abs(a._numerator), a._denominator, _normalize=False)
def __trunc__(a):
"""trunc(a)"""
@@ -555,6 +569,8 @@ class Fraction(numbers.Rational):
def __eq__(a, b):
"""a == b"""
+ if type(b) is int:
+ return a._numerator == b and a._denominator == 1
if isinstance(b, numbers.Rational):
return (a._numerator == b.numerator and
a._denominator == b.denominator)
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
index cd8c1a9..135ec9c 100644
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -42,7 +42,7 @@ import socket
import warnings
from socket import _GLOBAL_DEFAULT_TIMEOUT
-__all__ = ["FTP", "Netrc"]
+__all__ = ["FTP"]
# Magic number from <socket.h>
MSG_OOB = 0x1 # Process data out of band
@@ -918,115 +918,6 @@ def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
target.voidresp()
-class Netrc:
- """Class to parse & provide access to 'netrc' format files.
-
- See the netrc(4) man page for information on the file format.
-
- WARNING: This class is obsolete -- use module netrc instead.
-
- """
- __defuser = None
- __defpasswd = None
- __defacct = None
-
- def __init__(self, filename=None):
- warnings.warn("This class is deprecated, use the netrc module instead",
- DeprecationWarning, 2)
- if filename is None:
- if "HOME" in os.environ:
- filename = os.path.join(os.environ["HOME"],
- ".netrc")
- else:
- raise OSError("specify file to load or set $HOME")
- self.__hosts = {}
- self.__macros = {}
- fp = open(filename, "r")
- in_macro = 0
- while 1:
- line = fp.readline()
- if not line:
- break
- if in_macro and line.strip():
- macro_lines.append(line)
- continue
- elif in_macro:
- self.__macros[macro_name] = tuple(macro_lines)
- in_macro = 0
- words = line.split()
- host = user = passwd = acct = None
- default = 0
- i = 0
- while i < len(words):
- w1 = words[i]
- if i+1 < len(words):
- w2 = words[i + 1]
- else:
- w2 = None
- if w1 == 'default':
- default = 1
- elif w1 == 'machine' and w2:
- host = w2.lower()
- i = i + 1
- elif w1 == 'login' and w2:
- user = w2
- i = i + 1
- elif w1 == 'password' and w2:
- passwd = w2
- i = i + 1
- elif w1 == 'account' and w2:
- acct = w2
- i = i + 1
- elif w1 == 'macdef' and w2:
- macro_name = w2
- macro_lines = []
- in_macro = 1
- break
- i = i + 1
- if default:
- self.__defuser = user or self.__defuser
- self.__defpasswd = passwd or self.__defpasswd
- self.__defacct = acct or self.__defacct
- if host:
- if host in self.__hosts:
- ouser, opasswd, oacct = \
- self.__hosts[host]
- user = user or ouser
- passwd = passwd or opasswd
- acct = acct or oacct
- self.__hosts[host] = user, passwd, acct
- fp.close()
-
- def get_hosts(self):
- """Return a list of hosts mentioned in the .netrc file."""
- return self.__hosts.keys()
-
- def get_account(self, host):
- """Returns login information for the named host.
-
- The return value is a triple containing userid,
- password, and the accounting field.
-
- """
- host = host.lower()
- user = passwd = acct = None
- if host in self.__hosts:
- user, passwd, acct = self.__hosts[host]
- user = user or self.__defuser
- passwd = passwd or self.__defpasswd
- acct = acct or self.__defacct
- return user, passwd, acct
-
- def get_macros(self):
- """Return a list of all defined macro names."""
- return self.__macros.keys()
-
- def get_macro(self, macro):
- """Return a sequence of lines which define a named macro."""
- return self.__macros[macro]
-
-
-
def test():
'''Test program.
Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
@@ -1040,6 +931,8 @@ def test():
print(test.__doc__)
sys.exit(0)
+ import netrc
+
debugging = 0
rcfile = None
while sys.argv[1] == '-d':
@@ -1054,14 +947,14 @@ def test():
ftp.set_debuglevel(debugging)
userid = passwd = acct = ''
try:
- netrc = Netrc(rcfile)
+ netrcobj = netrc.netrc(rcfile)
except OSError:
if rcfile is not None:
sys.stderr.write("Could not open account file"
" -- using anonymous login.")
else:
try:
- userid, passwd, acct = netrc.get_account(host)
+ userid, acct, passwd = netrcobj.authenticators(host)
except KeyError:
# no account for host
sys.stderr.write(
diff --git a/Lib/functools.py b/Lib/functools.py
index 3e93a3d..20a26f9 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -98,7 +98,7 @@ def _gt_from_lt(self, other):
'Return a > b. Computed by @total_ordering from (not a < b) and (a != b).'
op_result = self.__lt__(other)
if op_result is NotImplemented:
- return NotImplemented
+ return op_result
return not op_result and self != other
def _le_from_lt(self, other):
@@ -110,35 +110,35 @@ def _ge_from_lt(self, other):
'Return a >= b. Computed by @total_ordering from (not a < b).'
op_result = self.__lt__(other)
if op_result is NotImplemented:
- return NotImplemented
+ return op_result
return not op_result
def _ge_from_le(self, other):
'Return a >= b. Computed by @total_ordering from (not a <= b) or (a == b).'
op_result = self.__le__(other)
if op_result is NotImplemented:
- return NotImplemented
+ return op_result
return not op_result or self == other
def _lt_from_le(self, other):
'Return a < b. Computed by @total_ordering from (a <= b) and (a != b).'
op_result = self.__le__(other)
if op_result is NotImplemented:
- return NotImplemented
+ return op_result
return op_result and self != other
def _gt_from_le(self, other):
'Return a > b. Computed by @total_ordering from (not a <= b).'
op_result = self.__le__(other)
if op_result is NotImplemented:
- return NotImplemented
+ return op_result
return not op_result
def _lt_from_gt(self, other):
'Return a < b. Computed by @total_ordering from (not a > b) and (a != b).'
op_result = self.__gt__(other)
if op_result is NotImplemented:
- return NotImplemented
+ return op_result
return not op_result and self != other
def _ge_from_gt(self, other):
@@ -150,52 +150,53 @@ def _le_from_gt(self, other):
'Return a <= b. Computed by @total_ordering from (not a > b).'
op_result = self.__gt__(other)
if op_result is NotImplemented:
- return NotImplemented
+ return op_result
return not op_result
def _le_from_ge(self, other):
'Return a <= b. Computed by @total_ordering from (not a >= b) or (a == b).'
op_result = self.__ge__(other)
if op_result is NotImplemented:
- return NotImplemented
+ return op_result
return not op_result or self == other
def _gt_from_ge(self, other):
'Return a > b. Computed by @total_ordering from (a >= b) and (a != b).'
op_result = self.__ge__(other)
if op_result is NotImplemented:
- return NotImplemented
+ return op_result
return op_result and self != other
def _lt_from_ge(self, other):
'Return a < b. Computed by @total_ordering from (not a >= b).'
op_result = self.__ge__(other)
if op_result is NotImplemented:
- return NotImplemented
+ return op_result
return not op_result
+_convert = {
+ '__lt__': [('__gt__', _gt_from_lt),
+ ('__le__', _le_from_lt),
+ ('__ge__', _ge_from_lt)],
+ '__le__': [('__ge__', _ge_from_le),
+ ('__lt__', _lt_from_le),
+ ('__gt__', _gt_from_le)],
+ '__gt__': [('__lt__', _lt_from_gt),
+ ('__ge__', _ge_from_gt),
+ ('__le__', _le_from_gt)],
+ '__ge__': [('__le__', _le_from_ge),
+ ('__gt__', _gt_from_ge),
+ ('__lt__', _lt_from_ge)]
+}
+
def total_ordering(cls):
"""Class decorator that fills in missing ordering methods"""
- convert = {
- '__lt__': [('__gt__', _gt_from_lt),
- ('__le__', _le_from_lt),
- ('__ge__', _ge_from_lt)],
- '__le__': [('__ge__', _ge_from_le),
- ('__lt__', _lt_from_le),
- ('__gt__', _gt_from_le)],
- '__gt__': [('__lt__', _lt_from_gt),
- ('__ge__', _ge_from_gt),
- ('__le__', _le_from_gt)],
- '__ge__': [('__le__', _le_from_ge),
- ('__gt__', _gt_from_ge),
- ('__lt__', _lt_from_ge)]
- }
# Find user-defined comparisons (not those inherited from object).
- roots = [op for op in convert if getattr(cls, op, None) is not getattr(object, op, None)]
+ roots = [op for op in _convert if getattr(cls, op, None) is not getattr(object, op, None)]
if not roots:
raise ValueError('must define at least one ordering operation: < > <= >=')
root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
- for opname, opfunc in convert[root]:
+ for opname, opfunc in _convert[root]:
if opname not in roots:
opfunc.__name__ = opname
setattr(cls, opname, opfunc)
@@ -222,8 +223,6 @@ def cmp_to_key(mycmp):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
- def __ne__(self, other):
- return mycmp(self.obj, other.obj) != 0
__hash__ = None
return K
@@ -291,7 +290,7 @@ class partialmethod(object):
for k, v in self.keywords.items())
format_string = "{module}.{cls}({func}, {args}, {keywords})"
return format_string.format(module=self.__class__.__module__,
- cls=self.__class__.__name__,
+ cls=self.__class__.__qualname__,
func=self.func,
args=args,
keywords=keywords)
diff --git a/Lib/genericpath.py b/Lib/genericpath.py
index ca4a510..6714061 100644
--- a/Lib/genericpath.py
+++ b/Lib/genericpath.py
@@ -130,3 +130,16 @@ def _splitext(p, sep, altsep, extsep):
filenameIndex += 1
return p, p[:0]
+
+def _check_arg_types(funcname, *args):
+ hasstr = hasbytes = False
+ for s in args:
+ if isinstance(s, str):
+ hasstr = True
+ elif isinstance(s, bytes):
+ hasbytes = True
+ else:
+ raise TypeError('%s() argument must be str or bytes, not %r' %
+ (funcname, s.__class__.__name__)) from None
+ if hasstr and hasbytes:
+ raise TypeError("Can't mix strings and bytes in path components") from None
diff --git a/Lib/gettext.py b/Lib/gettext.py
index 05d9c1e..15378bc 100644
--- a/Lib/gettext.py
+++ b/Lib/gettext.py
@@ -225,6 +225,13 @@ class GNUTranslations(NullTranslations):
LE_MAGIC = 0x950412de
BE_MAGIC = 0xde120495
+ # Acceptable .mo versions
+ VERSIONS = (0, 1)
+
+ def _get_versions(self, version):
+ """Returns a tuple of major version, minor version"""
+ return (version >> 16, version & 0xffff)
+
def _parse(self, fp):
"""Override this method to support alternative .mo formats."""
unpack = struct.unpack
@@ -245,6 +252,12 @@ class GNUTranslations(NullTranslations):
ii = '>II'
else:
raise OSError(0, 'Bad magic number', filename)
+
+ major_version, minor_version = self._get_versions(version)
+
+ if major_version not in self.VERSIONS:
+ raise OSError(0, 'Bad version number ' + str(major_version), filename)
+
# Now put all messages from the .mo file buffer into the catalog
# dictionary.
for i in range(0, msgcount):
diff --git a/Lib/glob.py b/Lib/glob.py
index d6eca24..56d6704 100644
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -6,7 +6,7 @@ import fnmatch
__all__ = ["glob", "iglob"]
-def glob(pathname):
+def glob(pathname, *, recursive=False):
"""Return a list of paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la
@@ -14,10 +14,12 @@ def glob(pathname):
dot are special cases that are not matched by '*' and '?'
patterns.
+ If recursive is true, the pattern '**' will match any files and
+ zero or more directories and subdirectories.
"""
- return list(iglob(pathname))
+ return list(iglob(pathname, recursive=recursive))
-def iglob(pathname):
+def iglob(pathname, *, recursive=False):
"""Return an iterator which yields the paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la
@@ -25,6 +27,8 @@ def iglob(pathname):
dot are special cases that are not matched by '*' and '?'
patterns.
+ If recursive is true, the pattern '**' will match any files and
+ zero or more directories and subdirectories.
"""
dirname, basename = os.path.split(pathname)
if not has_magic(pathname):
@@ -37,17 +41,23 @@ def iglob(pathname):
yield pathname
return
if not dirname:
- yield from glob1(None, basename)
+ if recursive and _isrecursive(basename):
+ yield from glob2(dirname, basename)
+ else:
+ yield from glob1(dirname, basename)
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)
+ dirs = iglob(dirname, recursive=recursive)
else:
dirs = [dirname]
if has_magic(basename):
- glob_in_dir = glob1
+ if recursive and _isrecursive(basename):
+ glob_in_dir = glob2
+ else:
+ glob_in_dir = glob1
else:
glob_in_dir = glob0
for dirname in dirs:
@@ -83,6 +93,34 @@ def glob0(dirname, basename):
return [basename]
return []
+# This helper function recursively yields relative pathnames inside a literal
+# directory.
+
+def glob2(dirname, pattern):
+ assert _isrecursive(pattern)
+ if dirname:
+ yield pattern[:0]
+ yield from _rlistdir(dirname)
+
+# Recursively yields relative pathnames inside a literal directory.
+
+def _rlistdir(dirname):
+ if not dirname:
+ if isinstance(dirname, bytes):
+ dirname = bytes(os.curdir, 'ASCII')
+ else:
+ dirname = os.curdir
+ try:
+ names = os.listdir(dirname)
+ except os.error:
+ return
+ 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):
+ yield os.path.join(x, y)
+
magic_check = re.compile('([*?[])')
magic_check_bytes = re.compile(b'([*?[])')
@@ -97,6 +135,12 @@ def has_magic(s):
def _ishidden(path):
return path[0] in ('.', b'.'[0])
+def _isrecursive(pattern):
+ if isinstance(pattern, bytes):
+ return pattern == b'**'
+ else:
+ return pattern == '**'
+
def escape(pathname):
"""Escape all special characters.
"""
diff --git a/Lib/heapq.py b/Lib/heapq.py
index d615239..07af37e 100644
--- a/Lib/heapq.py
+++ b/Lib/heapq.py
@@ -127,8 +127,6 @@ From all times, sorting has always been a Great Art! :-)
__all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge',
'nlargest', 'nsmallest', 'heappushpop']
-from itertools import islice, count, tee, chain
-
def heappush(heap, item):
"""Push item onto heap, maintaining the heap invariant."""
heap.append(item)
@@ -141,9 +139,8 @@ def heappop(heap):
returnitem = heap[0]
heap[0] = lastelt
_siftup(heap, 0)
- else:
- returnitem = lastelt
- return returnitem
+ return returnitem
+ return lastelt
def heapreplace(heap, item):
"""Pop and return the current smallest value, and add the new item.
@@ -179,12 +176,22 @@ def heapify(x):
for i in reversed(range(n//2)):
_siftup(x, i)
-def _heappushpop_max(heap, item):
- """Maxheap version of a heappush followed by a heappop."""
- if heap and item < heap[0]:
- item, heap[0] = heap[0], item
+def _heappop_max(heap):
+ """Maxheap version of a heappop."""
+ lastelt = heap.pop() # raises appropriate IndexError if heap is empty
+ if heap:
+ returnitem = heap[0]
+ heap[0] = lastelt
_siftup_max(heap, 0)
- return item
+ return returnitem
+ return lastelt
+
+def _heapreplace_max(heap, item):
+ """Maxheap version of a heappop followed by a heappush."""
+ returnitem = heap[0] # raises appropriate IndexError if heap is empty
+ heap[0] = item
+ _siftup_max(heap, 0)
+ return returnitem
def _heapify_max(x):
"""Transform list into a maxheap, in-place, in O(len(x)) time."""
@@ -192,42 +199,6 @@ def _heapify_max(x):
for i in reversed(range(n//2)):
_siftup_max(x, i)
-def nlargest(n, iterable):
- """Find the n largest elements in a dataset.
-
- Equivalent to: sorted(iterable, reverse=True)[:n]
- """
- if n < 0:
- return []
- it = iter(iterable)
- result = list(islice(it, n))
- if not result:
- return result
- heapify(result)
- _heappushpop = heappushpop
- for elem in it:
- _heappushpop(result, elem)
- result.sort(reverse=True)
- return result
-
-def nsmallest(n, iterable):
- """Find the n smallest elements in a dataset.
-
- Equivalent to: sorted(iterable)[:n]
- """
- if n < 0:
- return []
- it = iter(iterable)
- result = list(islice(it, n))
- if not result:
- return result
- _heapify_max(result)
- _heappushpop = _heappushpop_max
- for elem in it:
- _heappushpop(result, elem)
- result.sort()
- return result
-
# 'heap' is a heap at all indices >= startpos, except possibly for pos. pos
# is the index of a leaf with a possibly out-of-order value. Restore the
# heap invariant.
@@ -340,13 +311,7 @@ def _siftup_max(heap, pos):
heap[pos] = newitem
_siftdown_max(heap, startpos, pos)
-# If available, use C implementation
-try:
- from _heapq import *
-except ImportError:
- pass
-
-def merge(*iterables):
+def merge(*iterables, key=None, reverse=False):
'''Merge multiple sorted inputs into a single sorted output.
Similar to sorted(itertools.chain(*iterables)) but returns a generator,
@@ -356,51 +321,158 @@ def merge(*iterables):
>>> list(merge([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25]))
[0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25]
+ If *key* is not None, applies a key function to each element to determine
+ its sort order.
+
+ >>> list(merge(['dog', 'horse'], ['cat', 'fish', 'kangaroo'], key=len))
+ ['dog', 'cat', 'fish', 'horse', 'kangaroo']
+
'''
- _heappop, _heapreplace, _StopIteration = heappop, heapreplace, StopIteration
- _len = len
h = []
h_append = h.append
- for itnum, it in enumerate(map(iter, iterables)):
+
+ if reverse:
+ _heapify = _heapify_max
+ _heappop = _heappop_max
+ _heapreplace = _heapreplace_max
+ direction = -1
+ else:
+ _heapify = heapify
+ _heappop = heappop
+ _heapreplace = heapreplace
+ direction = 1
+
+ if key is None:
+ for order, it in enumerate(map(iter, iterables)):
+ try:
+ next = it.__next__
+ h_append([next(), order * direction, next])
+ except StopIteration:
+ pass
+ _heapify(h)
+ while len(h) > 1:
+ try:
+ while True:
+ value, order, next = s = h[0]
+ yield value
+ s[0] = next() # raises StopIteration when exhausted
+ _heapreplace(h, s) # restore heap condition
+ except StopIteration:
+ _heappop(h) # remove empty iterator
+ if h:
+ # fast case when only a single iterator remains
+ value, order, next = h[0]
+ yield value
+ yield from next.__self__
+ return
+
+ for order, it in enumerate(map(iter, iterables)):
try:
next = it.__next__
- h_append([next(), itnum, next])
- except _StopIteration:
+ value = next()
+ h_append([key(value), order * direction, value, next])
+ except StopIteration:
pass
- heapify(h)
-
- while _len(h) > 1:
+ _heapify(h)
+ while len(h) > 1:
try:
while True:
- v, itnum, next = s = h[0]
- yield v
- s[0] = next() # raises StopIteration when exhausted
- _heapreplace(h, s) # restore heap condition
- except _StopIteration:
- _heappop(h) # remove empty iterator
+ key_value, order, value, next = s = h[0]
+ yield value
+ value = next()
+ s[0] = key(value)
+ s[2] = value
+ _heapreplace(h, s)
+ except StopIteration:
+ _heappop(h)
if h:
- # fast case when only a single iterator remains
- v, itnum, next = h[0]
- yield v
+ key_value, order, value, next = h[0]
+ yield value
yield from next.__self__
-# Extend the implementations of nsmallest and nlargest to use a key= argument
-_nsmallest = nsmallest
+
+# Algorithm notes for nlargest() and nsmallest()
+# ==============================================
+#
+# Make a single pass over the data while keeping the k most extreme values
+# in a heap. Memory consumption is limited to keeping k values in a list.
+#
+# Measured performance for random inputs:
+#
+# number of comparisons
+# n inputs k-extreme values (average of 5 trials) % more than min()
+# ------------- ---------------- --------------------- -----------------
+# 1,000 100 3,317 231.7%
+# 10,000 100 14,046 40.5%
+# 100,000 100 105,749 5.7%
+# 1,000,000 100 1,007,751 0.8%
+# 10,000,000 100 10,009,401 0.1%
+#
+# Theoretical number of comparisons for k smallest of n random inputs:
+#
+# Step Comparisons Action
+# ---- -------------------------- ---------------------------
+# 1 1.66 * k heapify the first k-inputs
+# 2 n - k compare remaining elements to top of heap
+# 3 k * (1 + lg2(k)) * ln(n/k) replace the topmost value on the heap
+# 4 k * lg2(k) - (k/2) final sort of the k most extreme values
+#
+# Combining and simplifying for a rough estimate gives:
+#
+# comparisons = n + k * (log(k, 2) * log(n/k) + log(k, 2) + log(n/k))
+#
+# Computing the number of comparisons for step 3:
+# -----------------------------------------------
+# * For the i-th new value from the iterable, the probability of being in the
+# k most extreme values is k/i. For example, the probability of the 101st
+# value seen being in the 100 most extreme values is 100/101.
+# * If the value is a new extreme value, the cost of inserting it into the
+# heap is 1 + log(k, 2).
+# * The probability times the cost gives:
+# (k/i) * (1 + log(k, 2))
+# * Summing across the remaining n-k elements gives:
+# sum((k/i) * (1 + log(k, 2)) for i in range(k+1, n+1))
+# * This reduces to:
+# (H(n) - H(k)) * k * (1 + log(k, 2))
+# * Where H(n) is the n-th harmonic number estimated by:
+# gamma = 0.5772156649
+# H(n) = log(n, e) + gamma + 1 / (2 * n)
+# http://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#Rate_of_divergence
+# * Substituting the H(n) formula:
+# comparisons = k * (1 + log(k, 2)) * (log(n/k, e) + (1/n - 1/k) / 2)
+#
+# Worst-case for step 3:
+# ----------------------
+# In the worst case, the input data is reversed sorted so that every new element
+# must be inserted in the heap:
+#
+# comparisons = 1.66 * k + log(k, 2) * (n - k)
+#
+# Alternative Algorithms
+# ----------------------
+# Other algorithms were not used because they:
+# 1) Took much more auxiliary memory,
+# 2) Made multiple passes over the data.
+# 3) Made more comparisons in common cases (small k, large n, semi-random input).
+# See the more detailed comparison of approach at:
+# http://code.activestate.com/recipes/577573-compare-algorithms-for-heapqsmallest
+
def nsmallest(n, iterable, key=None):
"""Find the n smallest elements in a dataset.
Equivalent to: sorted(iterable, key=key)[:n]
"""
- # Short-cut for n==1 is to use min() when len(iterable)>0
+
+ # Short-cut for n==1 is to use min()
if n == 1:
it = iter(iterable)
- head = list(islice(it, 1))
- if not head:
- return []
+ sentinel = object()
if key is None:
- return [min(chain(head, it))]
- return [min(chain(head, it), key=key)]
+ result = min(it, default=sentinel)
+ else:
+ result = min(it, default=sentinel, key=key)
+ return [] if result is sentinel else [result]
# When n>=size, it's faster to use sorted()
try:
@@ -413,32 +485,57 @@ def nsmallest(n, iterable, key=None):
# When key is none, use simpler decoration
if key is None:
- it = zip(iterable, count()) # decorate
- result = _nsmallest(n, it)
- return [r[0] for r in result] # undecorate
+ it = iter(iterable)
+ # put the range(n) first so that zip() doesn't
+ # consume one too many elements from the iterator
+ result = [(elem, i) for i, elem in zip(range(n), it)]
+ if not result:
+ return result
+ _heapify_max(result)
+ top = result[0][0]
+ order = n
+ _heapreplace = _heapreplace_max
+ for elem in it:
+ if elem < top:
+ _heapreplace(result, (elem, order))
+ top = result[0][0]
+ order += 1
+ result.sort()
+ return [r[0] for r in result]
# General case, slowest method
- in1, in2 = tee(iterable)
- it = zip(map(key, in1), count(), in2) # decorate
- result = _nsmallest(n, it)
- return [r[2] for r in result] # undecorate
+ it = iter(iterable)
+ result = [(key(elem), i, elem) for i, elem in zip(range(n), it)]
+ if not result:
+ return result
+ _heapify_max(result)
+ top = result[0][0]
+ order = n
+ _heapreplace = _heapreplace_max
+ for elem in it:
+ k = key(elem)
+ if k < top:
+ _heapreplace(result, (k, order, elem))
+ top = result[0][0]
+ order += 1
+ result.sort()
+ return [r[2] for r in result]
-_nlargest = nlargest
def nlargest(n, iterable, key=None):
"""Find the n largest elements in a dataset.
Equivalent to: sorted(iterable, key=key, reverse=True)[:n]
"""
- # Short-cut for n==1 is to use max() when len(iterable)>0
+ # Short-cut for n==1 is to use max()
if n == 1:
it = iter(iterable)
- head = list(islice(it, 1))
- if not head:
- return []
+ sentinel = object()
if key is None:
- return [max(chain(head, it))]
- return [max(chain(head, it), key=key)]
+ result = max(it, default=sentinel)
+ else:
+ result = max(it, default=sentinel, key=key)
+ return [] if result is sentinel else [result]
# When n>=size, it's faster to use sorted()
try:
@@ -451,26 +548,60 @@ def nlargest(n, iterable, key=None):
# When key is none, use simpler decoration
if key is None:
- it = zip(iterable, count(0,-1)) # decorate
- result = _nlargest(n, it)
- return [r[0] for r in result] # undecorate
+ it = iter(iterable)
+ result = [(elem, i) for i, elem in zip(range(0, -n, -1), it)]
+ if not result:
+ return result
+ heapify(result)
+ top = result[0][0]
+ order = -n
+ _heapreplace = heapreplace
+ for elem in it:
+ if top < elem:
+ _heapreplace(result, (elem, order))
+ top = result[0][0]
+ order -= 1
+ result.sort(reverse=True)
+ return [r[0] for r in result]
# General case, slowest method
- in1, in2 = tee(iterable)
- it = zip(map(key, in1), count(0,-1), in2) # decorate
- result = _nlargest(n, it)
- return [r[2] for r in result] # undecorate
+ it = iter(iterable)
+ result = [(key(elem), i, elem) for i, elem in zip(range(0, -n, -1), it)]
+ if not result:
+ return result
+ heapify(result)
+ top = result[0][0]
+ order = -n
+ _heapreplace = heapreplace
+ for elem in it:
+ k = key(elem)
+ if top < k:
+ _heapreplace(result, (k, order, elem))
+ top = result[0][0]
+ order -= 1
+ result.sort(reverse=True)
+ return [r[2] for r in result]
+
+# If available, use C implementation
+try:
+ from _heapq import *
+except ImportError:
+ pass
+try:
+ from _heapq import _heapreplace_max
+except ImportError:
+ pass
+try:
+ from _heapq import _heapify_max
+except ImportError:
+ pass
+try:
+ from _heapq import _heappop_max
+except ImportError:
+ pass
+
if __name__ == "__main__":
- # Simple sanity test
- heap = []
- data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
- for item in data:
- heappush(heap, item)
- sort = []
- while heap:
- sort.append(heappop(heap))
- print(sort)
import doctest
- doctest.testmod()
+ print(doctest.testmod())
diff --git a/Lib/html/entities.py b/Lib/html/entities.py
index f7deae6..3e1778b 100644
--- a/Lib/html/entities.py
+++ b/Lib/html/entities.py
@@ -1,5 +1,8 @@
"""HTML character entity references."""
+__all__ = ['html5', 'name2codepoint', 'codepoint2name', 'entitydefs']
+
+
# maps the HTML entity name to the Unicode code point
name2codepoint = {
'AElig': 0x00c6, # latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1
diff --git a/Lib/html/parser.py b/Lib/html/parser.py
index a650d5e..390d4cc 100644
--- a/Lib/html/parser.py
+++ b/Lib/html/parser.py
@@ -29,35 +29,15 @@ starttagopen = re.compile('<[a-zA-Z]')
piclose = re.compile('>')
commentclose = re.compile(r'--\s*>')
# Note:
-# 1) the strict attrfind isn't really strict, but we can't make it
-# correctly strict without breaking backward compatibility;
-# 2) if you change tagfind/attrfind remember to update locatestarttagend too;
-# 3) if you change tagfind/attrfind and/or locatestarttagend the parser will
+# 1) if you change tagfind/attrfind remember to update locatestarttagend too;
+# 2) if you change tagfind/attrfind and/or locatestarttagend the parser will
# explode, so don't do it.
-tagfind = re.compile('([a-zA-Z][-.a-zA-Z0-9:_]*)(?:\s|/(?!>))*')
# 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|/(?!>))*')
-attrfind = re.compile(
- r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
- r'(\'[^\']*\'|"[^"]*"|[^\s"\'=<>`]*))?')
attrfind_tolerant = re.compile(
r'((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*'
r'(\'[^\']*\'|"[^"]*"|(?![\'"])[^>\s]*))?(?:\s|/(?!>))*')
-locatestarttagend = re.compile(r"""
- <[a-zA-Z][-.a-zA-Z0-9:_]* # tag name
- (?:\s+ # whitespace before attribute name
- (?:[a-zA-Z_][-.:a-zA-Z0-9_]* # attribute name
- (?:\s*=\s* # value indicator
- (?:'[^']*' # LITA-enclosed value
- |\"[^\"]*\" # LIT-enclosed value
- |[^'\">\s]+ # bare value
- )
- )?
- )
- )*
- \s* # trailing whitespace
-""", re.VERBOSE)
locatestarttagend_tolerant = re.compile(r"""
<[a-zA-Z][^\t\n\r\f />\x00]* # tag name
(?:[\s/]* # optional whitespace before attribute name
@@ -79,25 +59,6 @@ endendtag = re.compile('>')
endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
-class HTMLParseError(Exception):
- """Exception raised for all parse errors."""
-
- def __init__(self, msg, position=(None, None)):
- assert msg
- self.msg = msg
- self.lineno = position[0]
- self.offset = position[1]
-
- def __str__(self):
- result = self.msg
- if self.lineno is not None:
- result = result + ", at line %d" % self.lineno
- if self.offset is not None:
- result = result + ", column %d" % (self.offset + 1)
- return result
-
-
-_default_sentinel = object()
class HTMLParser(_markupbase.ParserBase):
"""Find tags and other markup and call handler functions.
@@ -123,27 +84,12 @@ class HTMLParser(_markupbase.ParserBase):
CDATA_CONTENT_ELEMENTS = ("script", "style")
- def __init__(self, strict=_default_sentinel, *,
- convert_charrefs=_default_sentinel):
+ def __init__(self, *, convert_charrefs=True):
"""Initialize and reset this instance.
- If convert_charrefs is True (default: False), all character references
+ If convert_charrefs is True (the default), all character references
are automatically converted to the corresponding Unicode characters.
- If strict is set to False (the default) the parser will parse invalid
- markup, otherwise it will raise an error. Note that the strict mode
- and argument are deprecated.
"""
- if strict is not _default_sentinel:
- warnings.warn("The strict argument and mode are deprecated.",
- DeprecationWarning, stacklevel=2)
- else:
- strict = False # default
- self.strict = strict
- if convert_charrefs is _default_sentinel:
- convert_charrefs = False # default
- warnings.warn("The value of convert_charrefs will become True in "
- "3.5. You are encouraged to set the value explicitly.",
- DeprecationWarning, stacklevel=2)
self.convert_charrefs = convert_charrefs
self.reset()
@@ -168,11 +114,6 @@ class HTMLParser(_markupbase.ParserBase):
"""Handle any buffered data."""
self.goahead(1)
- def error(self, message):
- warnings.warn("The 'error' method is deprecated.",
- DeprecationWarning, stacklevel=2)
- raise HTMLParseError(message, self.getpos())
-
__starttag_text = None
def get_starttag_text(self):
@@ -227,10 +168,7 @@ class HTMLParser(_markupbase.ParserBase):
elif startswith("<?", i):
k = self.parse_pi(i)
elif startswith("<!", i):
- if self.strict:
- k = self.parse_declaration(i)
- else:
- k = self.parse_html_declaration(i)
+ k = self.parse_html_declaration(i)
elif (i + 1) < n:
self.handle_data("<")
k = i + 1
@@ -239,8 +177,6 @@ class HTMLParser(_markupbase.ParserBase):
if k < 0:
if not end:
break
- if self.strict:
- self.error("EOF in middle of construct")
k = rawdata.find('>', i + 1)
if k < 0:
k = rawdata.find('<', i + 1)
@@ -282,13 +218,10 @@ class HTMLParser(_markupbase.ParserBase):
if match:
# match.group() will contain at least 2 chars
if end and match.group() == rawdata[i:]:
- if self.strict:
- self.error("EOF in middle of entity or char ref")
- else:
- k = match.end()
- if k <= i:
- k = n
- i = self.updatepos(i, i + 1)
+ k = match.end()
+ if k <= i:
+ k = n
+ i = self.updatepos(i, i + 1)
# incomplete
break
elif (i + 1) < n:
@@ -367,18 +300,12 @@ class HTMLParser(_markupbase.ParserBase):
# Now parse the data between i+1 and j into a tag and attrs
attrs = []
- if self.strict:
- match = tagfind.match(rawdata, i+1)
- else:
- match = tagfind_tolerant.match(rawdata, i+1)
+ match = tagfind_tolerant.match(rawdata, i+1)
assert match, 'unexpected call to parse_starttag()'
k = match.end()
self.lasttag = tag = match.group(1).lower()
while k < endpos:
- if self.strict:
- m = attrfind.match(rawdata, k)
- else:
- m = attrfind_tolerant.match(rawdata, k)
+ m = attrfind_tolerant.match(rawdata, k)
if not m:
break
attrname, rest, attrvalue = m.group(1, 2, 3)
@@ -401,9 +328,6 @@ class HTMLParser(_markupbase.ParserBase):
- self.__starttag_text.rfind("\n")
else:
offset = offset + len(self.__starttag_text)
- if self.strict:
- self.error("junk characters in start tag: %r"
- % (rawdata[k:endpos][:20],))
self.handle_data(rawdata[i:endpos])
return endpos
if end.endswith('/>'):
@@ -419,10 +343,7 @@ class HTMLParser(_markupbase.ParserBase):
# or -1 if incomplete.
def check_for_whole_start_tag(self, i):
rawdata = self.rawdata
- if self.strict:
- m = locatestarttagend.match(rawdata, i)
- else:
- m = locatestarttagend_tolerant.match(rawdata, i)
+ m = locatestarttagend_tolerant.match(rawdata, i)
if m:
j = m.end()
next = rawdata[j:j+1]
@@ -435,9 +356,6 @@ class HTMLParser(_markupbase.ParserBase):
# buffer boundary
return -1
# else bogus input
- if self.strict:
- self.updatepos(i, j + 1)
- self.error("malformed empty start tag")
if j > i:
return j
else:
@@ -450,9 +368,6 @@ class HTMLParser(_markupbase.ParserBase):
# end of input in or before attribute value, or we have the
# '/' from a '/>' ending
return -1
- if self.strict:
- self.updatepos(i, j)
- self.error("malformed start tag")
if j > i:
return j
else:
@@ -472,8 +387,6 @@ class HTMLParser(_markupbase.ParserBase):
if self.cdata_elem is not None:
self.handle_data(rawdata[i:gtpos])
return gtpos
- if self.strict:
- self.error("bad end tag: %r" % (rawdata[i:gtpos],))
# find the name: w3.org/TR/html5/tokenization.html#tag-name-state
namematch = tagfind_tolerant.match(rawdata, i+2)
if not namematch:
@@ -539,8 +452,7 @@ class HTMLParser(_markupbase.ParserBase):
pass
def unknown_decl(self, data):
- if self.strict:
- self.error("unknown declaration: %r" % (data,))
+ pass
# Internal -- helper to remove special character quoting
def unescape(self, s):
diff --git a/Lib/http/__init__.py b/Lib/http/__init__.py
index 196d378..475f1c0 100644
--- a/Lib/http/__init__.py
+++ b/Lib/http/__init__.py
@@ -1 +1,134 @@
-# This directory is a Python package.
+from enum import IntEnum
+
+__all__ = ['HTTPStatus']
+
+class HTTPStatus(IntEnum):
+ """HTTP status codes and reason phrases
+
+ Status codes from the following RFCs are all observed:
+
+ * RFC 7231: Hypertext Transfer Protocol (HTTP/1.1), obsoletes 2616
+ * RFC 6585: Additional HTTP Status Codes
+ * RFC 3229: Delta encoding in HTTP
+ * RFC 4918: HTTP Extensions for WebDAV, obsoletes 2518
+ * RFC 5842: Binding Extensions to WebDAV
+ * RFC 7238: Permanent Redirect
+ * RFC 2295: Transparent Content Negotiation in HTTP
+ * RFC 2774: An HTTP Extension Framework
+ """
+ def __new__(cls, value, phrase, description=''):
+ obj = int.__new__(cls, value)
+ obj._value_ = value
+
+ obj.phrase = phrase
+ obj.description = description
+ return obj
+
+ # informational
+ CONTINUE = 100, 'Continue', 'Request received, please continue'
+ SWITCHING_PROTOCOLS = (101, 'Switching Protocols',
+ 'Switching to new protocol; obey Upgrade header')
+ PROCESSING = 102, 'Processing'
+
+ # success
+ OK = 200, 'OK', 'Request fulfilled, document follows'
+ CREATED = 201, 'Created', 'Document created, URL follows'
+ ACCEPTED = (202, 'Accepted',
+ 'Request accepted, processing continues off-line')
+ NON_AUTHORITATIVE_INFORMATION = (203,
+ 'Non-Authoritative Information', 'Request fulfilled from cache')
+ NO_CONTENT = 204, 'No Content', 'Request fulfilled, nothing follows'
+ RESET_CONTENT = 205, 'Reset Content', 'Clear input form for further input'
+ PARTIAL_CONTENT = 206, 'Partial Content', 'Partial content follows'
+ MULTI_STATUS = 207, 'Multi-Status'
+ ALREADY_REPORTED = 208, 'Already Reported'
+ IM_USED = 226, 'IM Used'
+
+ # redirection
+ MULTIPLE_CHOICES = (300, 'Multiple Choices',
+ 'Object has several resources -- see URI list')
+ MOVED_PERMANENTLY = (301, 'Moved Permanently',
+ 'Object moved permanently -- see URI list')
+ FOUND = 302, 'Found', 'Object moved temporarily -- see URI list'
+ SEE_OTHER = 303, 'See Other', 'Object moved -- see Method and URL list'
+ NOT_MODIFIED = (304, 'Not Modified',
+ 'Document has not changed since given time')
+ USE_PROXY = (305, 'Use Proxy',
+ 'You must use proxy specified in Location to access this resource')
+ TEMPORARY_REDIRECT = (307, 'Temporary Redirect',
+ 'Object moved temporarily -- see URI list')
+ PERMANENT_REDIRECT = (308, 'Permanent Redirect',
+ 'Object moved temporarily -- see URI list')
+
+ # client error
+ BAD_REQUEST = (400, 'Bad Request',
+ 'Bad request syntax or unsupported method')
+ UNAUTHORIZED = (401, 'Unauthorized',
+ 'No permission -- see authorization schemes')
+ PAYMENT_REQUIRED = (402, 'Payment Required',
+ 'No payment -- see charging schemes')
+ FORBIDDEN = (403, 'Forbidden',
+ 'Request forbidden -- authorization will not help')
+ NOT_FOUND = (404, 'Not Found',
+ 'Nothing matches the given URI')
+ METHOD_NOT_ALLOWED = (405, 'Method Not Allowed',
+ 'Specified method is invalid for this resource')
+ NOT_ACCEPTABLE = (406, 'Not Acceptable',
+ 'URI not available in preferred format')
+ PROXY_AUTHENTICATION_REQUIRED = (407,
+ 'Proxy Authentication Required',
+ 'You must authenticate with this proxy before proceeding')
+ REQUEST_TIMEOUT = (408, 'Request Timeout',
+ 'Request timed out; try again later')
+ CONFLICT = 409, 'Conflict', 'Request conflict'
+ GONE = (410, 'Gone',
+ 'URI no longer exists and has been permanently removed')
+ LENGTH_REQUIRED = (411, 'Length Required',
+ 'Client must specify Content-Length')
+ PRECONDITION_FAILED = (412, 'Precondition Failed',
+ 'Precondition in headers is false')
+ REQUEST_ENTITY_TOO_LARGE = (413, 'Request Entity Too Large',
+ 'Entity is too large')
+ REQUEST_URI_TOO_LONG = (414, 'Request-URI Too Long',
+ 'URI is too long')
+ UNSUPPORTED_MEDIA_TYPE = (415, 'Unsupported Media Type',
+ 'Entity body in unsupported format')
+ REQUEST_RANGE_NOT_SATISFIABLE = (416,
+ 'Request Range Not Satisfiable',
+ 'Cannot satisfy request range')
+ EXPECTATION_FAILED = (417, 'Expectation Failed',
+ 'Expect condition could not be satisfied')
+ UNPROCESSABLE_ENTITY = 422, 'Unprocessable Entity'
+ LOCKED = 423, 'Locked'
+ FAILED_DEPENDENCY = 424, 'Failed Dependency'
+ UPGRADE_REQUIRED = 426, 'Upgrade Required'
+ PRECONDITION_REQUIRED = (428, 'Precondition Required',
+ 'The origin server requires the request to be conditional')
+ TOO_MANY_REQUESTS = (429, 'Too Many Requests',
+ 'The user has sent too many requests in '
+ 'a given amount of time ("rate limiting")')
+ REQUEST_HEADER_FIELD_TOO_LARGE = (431,
+ 'Request Header Field Too Large',
+ 'The server is unwilling to process the request because its header '
+ 'fields are too large')
+
+ # server errors
+ INTERNAL_SERVER_ERROR = (500, 'Internal Server Error',
+ 'Server got itself in trouble')
+ NOT_IMPLEMENTED = (501, 'Not Implemented',
+ 'Server does not support this operation')
+ BAD_GATEWAY = (502, 'Bad Gateway',
+ 'Invalid responses from another server/proxy')
+ SERVICE_UNAVAILABLE = (503, 'Service Unavailable',
+ 'The server cannot process the request due to a high load')
+ GATEWAY_TIMEOUT = (504, 'Gateway Timeout',
+ 'The gateway server did not receive a timely response')
+ HTTP_VERSION_NOT_SUPPORTED = (505, 'HTTP Version Not Supported',
+ 'Cannot fulfill request')
+ VARIANT_ALSO_NEGOTIATES = 506, 'Variant Also Negotiates'
+ INSUFFICIENT_STORAGE = 507, 'Insufficient Storage'
+ LOOP_DETECTED = 508, 'Loop Detected'
+ NOT_EXTENDED = 510, 'Not Extended'
+ NETWORK_AUTHENTICATION_REQUIRED = (511,
+ 'Network Authentication Required',
+ 'The client needs to authenticate to gain network access')
diff --git a/Lib/http/client.py b/Lib/http/client.py
index 6de4b0e..a77e501 100644
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -68,6 +68,7 @@ Req-sent-unread-response _CS_REQ_SENT <response_class>
import email.parser
import email.message
+import http
import io
import os
import socket
@@ -91,122 +92,13 @@ _CS_IDLE = 'Idle'
_CS_REQ_STARTED = 'Request-started'
_CS_REQ_SENT = 'Request-sent'
-# status codes
-# informational
-CONTINUE = 100
-SWITCHING_PROTOCOLS = 101
-PROCESSING = 102
-
-# successful
-OK = 200
-CREATED = 201
-ACCEPTED = 202
-NON_AUTHORITATIVE_INFORMATION = 203
-NO_CONTENT = 204
-RESET_CONTENT = 205
-PARTIAL_CONTENT = 206
-MULTI_STATUS = 207
-IM_USED = 226
-
-# redirection
-MULTIPLE_CHOICES = 300
-MOVED_PERMANENTLY = 301
-FOUND = 302
-SEE_OTHER = 303
-NOT_MODIFIED = 304
-USE_PROXY = 305
-TEMPORARY_REDIRECT = 307
-
-# client error
-BAD_REQUEST = 400
-UNAUTHORIZED = 401
-PAYMENT_REQUIRED = 402
-FORBIDDEN = 403
-NOT_FOUND = 404
-METHOD_NOT_ALLOWED = 405
-NOT_ACCEPTABLE = 406
-PROXY_AUTHENTICATION_REQUIRED = 407
-REQUEST_TIMEOUT = 408
-CONFLICT = 409
-GONE = 410
-LENGTH_REQUIRED = 411
-PRECONDITION_FAILED = 412
-REQUEST_ENTITY_TOO_LARGE = 413
-REQUEST_URI_TOO_LONG = 414
-UNSUPPORTED_MEDIA_TYPE = 415
-REQUESTED_RANGE_NOT_SATISFIABLE = 416
-EXPECTATION_FAILED = 417
-UNPROCESSABLE_ENTITY = 422
-LOCKED = 423
-FAILED_DEPENDENCY = 424
-UPGRADE_REQUIRED = 426
-PRECONDITION_REQUIRED = 428
-TOO_MANY_REQUESTS = 429
-REQUEST_HEADER_FIELDS_TOO_LARGE = 431
-
-# server error
-INTERNAL_SERVER_ERROR = 500
-NOT_IMPLEMENTED = 501
-BAD_GATEWAY = 502
-SERVICE_UNAVAILABLE = 503
-GATEWAY_TIMEOUT = 504
-HTTP_VERSION_NOT_SUPPORTED = 505
-INSUFFICIENT_STORAGE = 507
-NOT_EXTENDED = 510
-NETWORK_AUTHENTICATION_REQUIRED = 511
+# hack to maintain backwards compatibility
+globals().update(http.HTTPStatus.__members__)
+
+# another hack to maintain backwards compatibility
# Mapping status codes to official W3C names
-responses = {
- 100: 'Continue',
- 101: 'Switching Protocols',
-
- 200: 'OK',
- 201: 'Created',
- 202: 'Accepted',
- 203: 'Non-Authoritative Information',
- 204: 'No Content',
- 205: 'Reset Content',
- 206: 'Partial Content',
-
- 300: 'Multiple Choices',
- 301: 'Moved Permanently',
- 302: 'Found',
- 303: 'See Other',
- 304: 'Not Modified',
- 305: 'Use Proxy',
- 306: '(Unused)',
- 307: 'Temporary Redirect',
-
- 400: 'Bad Request',
- 401: 'Unauthorized',
- 402: 'Payment Required',
- 403: 'Forbidden',
- 404: 'Not Found',
- 405: 'Method Not Allowed',
- 406: 'Not Acceptable',
- 407: 'Proxy Authentication Required',
- 408: 'Request Timeout',
- 409: 'Conflict',
- 410: 'Gone',
- 411: 'Length Required',
- 412: 'Precondition Failed',
- 413: 'Request Entity Too Large',
- 414: 'Request-URI Too Long',
- 415: 'Unsupported Media Type',
- 416: 'Requested Range Not Satisfiable',
- 417: 'Expectation Failed',
- 428: 'Precondition Required',
- 429: 'Too Many Requests',
- 431: 'Request Header Fields Too Large',
-
- 500: 'Internal Server Error',
- 501: 'Not Implemented',
- 502: 'Bad Gateway',
- 503: 'Service Unavailable',
- 504: 'Gateway Timeout',
- 505: 'HTTP Version Not Supported',
- 511: 'Network Authentication Required',
-}
+responses = {v: v.phrase for v in http.HTTPStatus.__members__.values()}
# maximal amount of data to read at one time in _safe_read
MAXAMOUNT = 1048576
@@ -270,7 +162,7 @@ def parse_headers(fp, _class=HTTPMessage):
return email.parser.Parser(_class=_class).parsestr(hstring)
-class HTTPResponse(io.RawIOBase):
+class HTTPResponse(io.BufferedIOBase):
# See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details.
@@ -495,9 +387,10 @@ class HTTPResponse(io.RawIOBase):
return b""
if amt is not None:
- # Amount is given, so call base class version
- # (which is implemented in terms of self.readinto)
- return super(HTTPResponse, self).read(amt)
+ # Amount is given, implement using readinto
+ b = bytearray(amt)
+ n = self.readinto(b)
+ return memoryview(b)[:n].tobytes()
else:
# Amount is not given (unbounded read) so we must check self.length
# and self.chunked
@@ -577,71 +470,67 @@ class HTTPResponse(io.RawIOBase):
if line in (b'\r\n', b'\n', b''):
break
+ def _get_chunk_left(self):
+ # return self.chunk_left, reading a new chunk if necessary.
+ # chunk_left == 0: at the end of the current chunk, need to close it
+ # chunk_left == None: No current chunk, should read next.
+ # This function returns non-zero or None if the last chunk has
+ # been read.
+ chunk_left = self.chunk_left
+ if not chunk_left: # Can be 0 or None
+ if chunk_left is not None:
+ # We are at the end of chunk. dicard chunk end
+ self._safe_read(2) # toss the CRLF at the end of the chunk
+ try:
+ chunk_left = self._read_next_chunk_size()
+ except ValueError:
+ raise IncompleteRead(b'')
+ if chunk_left == 0:
+ # last chunk: 1*("0") [ chunk-extension ] CRLF
+ self._read_and_discard_trailer()
+ # we read everything; close the "file"
+ self._close_conn()
+ chunk_left = None
+ self.chunk_left = chunk_left
+ return chunk_left
+
def _readall_chunked(self):
assert self.chunked != _UNKNOWN
- chunk_left = self.chunk_left
value = []
- while True:
- if chunk_left is None:
- try:
- chunk_left = self._read_next_chunk_size()
- if chunk_left == 0:
- break
- except ValueError:
- raise IncompleteRead(b''.join(value))
- value.append(self._safe_read(chunk_left))
-
- # we read the whole chunk, get another
- self._safe_read(2) # toss the CRLF at the end of the chunk
- chunk_left = None
-
- self._read_and_discard_trailer()
-
- # we read everything; close the "file"
- self._close_conn()
-
- return b''.join(value)
+ try:
+ while True:
+ chunk_left = self._get_chunk_left()
+ if chunk_left is None:
+ break
+ value.append(self._safe_read(chunk_left))
+ self.chunk_left = 0
+ return b''.join(value)
+ except IncompleteRead:
+ raise IncompleteRead(b''.join(value))
def _readinto_chunked(self, b):
assert self.chunked != _UNKNOWN
- chunk_left = self.chunk_left
-
total_bytes = 0
mvb = memoryview(b)
- while True:
- if chunk_left is None:
- try:
- chunk_left = self._read_next_chunk_size()
- if chunk_left == 0:
- break
- except ValueError:
- raise IncompleteRead(bytes(b[0:total_bytes]))
-
- if len(mvb) < chunk_left:
- n = self._safe_readinto(mvb)
- self.chunk_left = chunk_left - n
- return total_bytes + n
- elif len(mvb) == chunk_left:
- n = self._safe_readinto(mvb)
- self._safe_read(2) # toss the CRLF at the end of the chunk
- self.chunk_left = None
- return total_bytes + n
- else:
- temp_mvb = mvb[0:chunk_left]
+ try:
+ while True:
+ chunk_left = self._get_chunk_left()
+ if chunk_left is None:
+ return total_bytes
+
+ if len(mvb) <= chunk_left:
+ n = self._safe_readinto(mvb)
+ self.chunk_left = chunk_left - n
+ return total_bytes + n
+
+ temp_mvb = mvb[:chunk_left]
n = self._safe_readinto(temp_mvb)
mvb = mvb[n:]
total_bytes += n
+ self.chunk_left = 0
- # we read the whole chunk, get another
- self._safe_read(2) # toss the CRLF at the end of the chunk
- chunk_left = None
-
- self._read_and_discard_trailer()
-
- # we read everything; close the "file"
- self._close_conn()
-
- return total_bytes
+ except IncompleteRead:
+ raise IncompleteRead(bytes(b[0:total_bytes]))
def _safe_read(self, amt):
"""Read the number of bytes requested, compensating for partial reads.
@@ -682,6 +571,73 @@ class HTTPResponse(io.RawIOBase):
total_bytes += n
return total_bytes
+ def read1(self, n=-1):
+ """Read with at most one underlying system call. If at least one
+ byte is buffered, return that instead.
+ """
+ if self.fp is None or self._method == "HEAD":
+ return b""
+ if self.chunked:
+ return self._read1_chunked(n)
+ try:
+ result = self.fp.read1(n)
+ except ValueError:
+ if n >= 0:
+ raise
+ # some implementations, like BufferedReader, don't support -1
+ # Read an arbitrarily selected largeish chunk.
+ result = self.fp.read1(16*1024)
+ if not result and n:
+ self._close_conn()
+ return result
+
+ def peek(self, n=-1):
+ # Having this enables IOBase.readline() to read more than one
+ # byte at a time
+ if self.fp is None or self._method == "HEAD":
+ return b""
+ if self.chunked:
+ return self._peek_chunked(n)
+ return self.fp.peek(n)
+
+ def readline(self, limit=-1):
+ if self.fp is None or self._method == "HEAD":
+ return b""
+ if self.chunked:
+ # Fallback to IOBase readline which uses peek() and read()
+ return super().readline(limit)
+ result = self.fp.readline(limit)
+ if not result and limit:
+ self._close_conn()
+ return result
+
+ def _read1_chunked(self, n):
+ # Strictly speaking, _get_chunk_left() may cause more than one read,
+ # but that is ok, since that is to satisfy the chunked protocol.
+ chunk_left = self._get_chunk_left()
+ if chunk_left is None or n == 0:
+ return b''
+ if not (0 <= n <= chunk_left):
+ n = chunk_left # if n is negative or larger than chunk_left
+ read = self.fp.read1(n)
+ self.chunk_left -= len(read)
+ if not read:
+ raise IncompleteRead(b"")
+ return read
+
+ def _peek_chunked(self, n):
+ # Strictly speaking, _get_chunk_left() may cause more than one read,
+ # but that is ok, since that is to satisfy the chunked protocol.
+ try:
+ chunk_left = self._get_chunk_left()
+ except IncompleteRead:
+ return b'' # peek doesn't worry about protocol
+ if chunk_left is None:
+ return b'' # eof
+ # peek is allowed to return more than requested. Just request the
+ # entire chunk, and truncate what we get.
+ return self.fp.peek(chunk_left)[:chunk_left]
+
def fileno(self):
return self.fp.fileno()
@@ -725,14 +681,6 @@ class HTTPConnection:
default_port = HTTP_PORT
auto_open = 1
debuglevel = 0
- # TCP Maximum Segment Size (MSS) is determined by the TCP stack on
- # a per-connection basis. There is no simple and efficient
- # platform independent mechanism for determining the MSS, so
- # instead a reasonable estimate is chosen. The getsockopt()
- # interface using the TCP_MAXSEG parameter may be a suitable
- # approach on some operating systems. A value of 16KiB is chosen
- # as a reasonable estimate of the maximum MSS.
- mss = 16384
def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
source_address=None):
@@ -814,7 +762,7 @@ class HTTPConnection:
response = self.response_class(self.sock, method=self._method)
(version, code, message) = response._read_status()
- if code != 200:
+ if code != http.HTTPStatus.OK:
self.close()
raise OSError("Tunnel connection failed: %d %s" % (code,
message.strip()))
@@ -828,10 +776,14 @@ class HTTPConnection:
if line in (b'\r\n', b'\n', b''):
break
+ if self.debuglevel > 0:
+ print('header:', line.decode())
+
def connect(self):
"""Connect to the host and port specified in __init__."""
- self.sock = self._create_connection((self.host,self.port),
- self.timeout, self.source_address)
+ self.sock = self._create_connection(
+ (self.host,self.port), self.timeout, self.source_address)
+ self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
if self._tunnel_host:
self._tunnel()
@@ -910,19 +862,9 @@ class HTTPConnection:
self._buffer.extend((b"", b""))
msg = b"\r\n".join(self._buffer)
del self._buffer[:]
- # If msg and message_body are sent in a single send() call,
- # it will avoid performance problems caused by the interaction
- # between delayed ack and the Nagle algorithm. However,
- # there is no performance gain if the message is larger
- # than MSS (and there is a memory penalty for the message
- # copy).
- if isinstance(message_body, bytes) and len(message_body) < self.mss:
- msg += message_body
- message_body = None
+
self.send(msg)
if message_body is not None:
- # message_body was not a string (i.e. it is a file), and
- # we must run the risk of Nagle.
self.send(message_body)
def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0):
@@ -1271,7 +1213,8 @@ class IncompleteRead(HTTPException):
e = ', %i more expected' % self.expected
else:
e = ''
- return 'IncompleteRead(%i bytes read%s)' % (len(self.partial), e)
+ return '%s(%i bytes read%s)' % (self.__class__.__name__,
+ len(self.partial), e)
def __str__(self):
return repr(self)
diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
index 7e4982f..22f2833 100644
--- a/Lib/http/cookiejar.py
+++ b/Lib/http/cookiejar.py
@@ -478,7 +478,7 @@ def parse_ns_headers(ns_headers):
if "=" not in param:
k, v = param, None
else:
- k, v = re.split(r"\s*=\s*", param, 1)
+ k, v = re.split(r"\s*=\s*", param, maxsplit=1)
k = k.lstrip()
if ii != 0:
lc = k.lower()
@@ -805,7 +805,7 @@ class Cookie:
args.append("%s=%s" % (name, repr(attr)))
args.append("rest=%s" % repr(self._rest))
args.append("rfc2109=%s" % repr(self.rfc2109))
- return "Cookie(%s)" % ", ".join(args)
+ return "%s(%s)" % (self.__class__.__name__, ", ".join(args))
class CookiePolicy:
diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py
index 3e1abd7..73acbc7 100644
--- a/Lib/http/cookies.py
+++ b/Lib/http/cookies.py
@@ -533,10 +533,17 @@ class BaseCookie(dict):
return
def __parse_string(self, str, patt=_CookiePattern):
- i = 0 # Our starting point
- n = len(str) # Length of string
- M = None # current morsel
+ i = 0 # Our starting point
+ n = len(str) # Length of string
+ parsed_items = [] # Parsed (type, key, value) triples
+ morsel_seen = False # A key=value pair was previously encountered
+
+ TYPE_ATTRIBUTE = 1
+ TYPE_KEYVALUE = 2
+ # We first parse the whole cookie string and reject it if it's
+ # syntactically invalid (this helps avoid some classes of injection
+ # attacks).
while 0 <= i < n:
# Start looking for a cookie
match = patt.match(str, i)
@@ -547,22 +554,41 @@ class BaseCookie(dict):
key, value = match.group("key"), match.group("val")
i = match.end(0)
- # Parse the key, value in case it's metainfo
if key[0] == "$":
- # We ignore attributes which pertain to the cookie
- # mechanism as a whole. See RFC 2109.
- # (Does anyone care?)
- if M:
- M[key[1:]] = value
+ if not morsel_seen:
+ # We ignore attributes which pertain to the cookie
+ # mechanism as a whole, such as "$Version".
+ # See RFC 2965. (Does anyone care?)
+ continue
+ parsed_items.append((TYPE_ATTRIBUTE, key[1:], value))
elif key.lower() in Morsel._reserved:
- if M:
- if value is None:
- if key.lower() in Morsel._flags:
- M[key] = True
+ if not morsel_seen:
+ # Invalid cookie string
+ return
+ if value is None:
+ if key.lower() in Morsel._flags:
+ parsed_items.append((TYPE_ATTRIBUTE, key, True))
else:
- M[key] = _unquote(value)
+ # Invalid cookie string
+ return
+ else:
+ parsed_items.append((TYPE_ATTRIBUTE, key, _unquote(value)))
elif value is not None:
- rval, cval = self.value_decode(value)
+ parsed_items.append((TYPE_KEYVALUE, key, self.value_decode(value)))
+ morsel_seen = True
+ else:
+ # Invalid cookie string
+ return
+
+ # The cookie string is valid, apply it.
+ M = None # current morsel
+ for tp, key, value in parsed_items:
+ if tp == TYPE_ATTRIBUTE:
+ assert M is not None
+ M[key] = value
+ else:
+ assert tp == TYPE_KEYVALUE
+ rval, cval = value
self.__set(key, rval, cval)
M = self[key]
diff --git a/Lib/http/server.py b/Lib/http/server.py
index a27890e..a2425f2 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -103,6 +103,8 @@ import urllib.parse
import copy
import argparse
+from http import HTTPStatus
+
# Default error message template
DEFAULT_ERROR_MESSAGE = """\
@@ -281,7 +283,9 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
if len(words) == 3:
command, path, version = words
if version[:5] != 'HTTP/':
- self.send_error(400, "Bad request version (%r)" % version)
+ self.send_error(
+ HTTPStatus.BAD_REQUEST,
+ "Bad request version (%r)" % version)
return False
try:
base_version_number = version.split('/', 1)[1]
@@ -296,25 +300,31 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
raise ValueError
version_number = int(version_number[0]), int(version_number[1])
except (ValueError, IndexError):
- self.send_error(400, "Bad request version (%r)" % version)
+ self.send_error(
+ HTTPStatus.BAD_REQUEST,
+ "Bad request version (%r)" % version)
return False
if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1":
self.close_connection = 0
if version_number >= (2, 0):
- self.send_error(505,
- "Invalid HTTP Version (%s)" % base_version_number)
+ self.send_error(
+ HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
+ "Invalid HTTP Version (%s)" % base_version_number)
return False
elif len(words) == 2:
command, path = words
self.close_connection = 1
if command != 'GET':
- self.send_error(400,
- "Bad HTTP/0.9 request type (%r)" % command)
+ self.send_error(
+ HTTPStatus.BAD_REQUEST,
+ "Bad HTTP/0.9 request type (%r)" % command)
return False
elif not words:
return False
else:
- self.send_error(400, "Bad request syntax (%r)" % requestline)
+ self.send_error(
+ HTTPStatus.BAD_REQUEST,
+ "Bad request syntax (%r)" % requestline)
return False
self.command, self.path, self.request_version = command, path, version
@@ -323,7 +333,9 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
self.headers = http.client.parse_headers(self.rfile,
_class=self.MessageClass)
except http.client.LineTooLong:
- self.send_error(400, "Line too long")
+ self.send_error(
+ HTTPStatus.BAD_REQUEST,
+ "Line too long")
return False
conntype = self.headers.get('Connection', "")
@@ -355,7 +367,7 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
False.
"""
- self.send_response_only(100)
+ self.send_response_only(HTTPStatus.CONTINUE)
self.end_headers()
return True
@@ -373,7 +385,7 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
self.requestline = ''
self.request_version = ''
self.command = ''
- self.send_error(414)
+ self.send_error(HTTPStatus.REQUEST_URI_TOO_LONG)
return
if not self.raw_requestline:
self.close_connection = 1
@@ -383,7 +395,9 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
return
mname = 'do_' + self.command
if not hasattr(self, mname):
- self.send_error(501, "Unsupported method (%r)" % self.command)
+ self.send_error(
+ HTTPStatus.NOT_IMPLEMENTED,
+ "Unsupported method (%r)" % self.command)
return
method = getattr(self, mname)
method()
@@ -438,7 +452,11 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
self.send_header('Connection', 'close')
self.send_header('Content-Length', int(len(body)))
self.end_headers()
- if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
+
+ if (self.command != 'HEAD' and
+ code >= 200 and
+ code not in (
+ HTTPStatus.NO_CONTENT, HTTPStatus.NOT_MODIFIED)):
self.wfile.write(body)
def send_response(self, code, message=None):
@@ -582,82 +600,11 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
# MessageClass used to parse headers
MessageClass = http.client.HTTPMessage
- # Table mapping response codes to messages; entries have the
- # form {code: (shortmessage, longmessage)}.
- # See RFC 2616 and 6585.
+ # hack to maintain backwards compatibility
responses = {
- 100: ('Continue', 'Request received, please continue'),
- 101: ('Switching Protocols',
- 'Switching to new protocol; obey Upgrade header'),
-
- 200: ('OK', 'Request fulfilled, document follows'),
- 201: ('Created', 'Document created, URL follows'),
- 202: ('Accepted',
- 'Request accepted, processing continues off-line'),
- 203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
- 204: ('No Content', 'Request fulfilled, nothing follows'),
- 205: ('Reset Content', 'Clear input form for further input.'),
- 206: ('Partial Content', 'Partial content follows.'),
-
- 300: ('Multiple Choices',
- 'Object has several resources -- see URI list'),
- 301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
- 302: ('Found', 'Object moved temporarily -- see URI list'),
- 303: ('See Other', 'Object moved -- see Method and URL list'),
- 304: ('Not Modified',
- 'Document has not changed since given time'),
- 305: ('Use Proxy',
- 'You must use proxy specified in Location to access this '
- 'resource.'),
- 307: ('Temporary Redirect',
- 'Object moved temporarily -- see URI list'),
-
- 400: ('Bad Request',
- 'Bad request syntax or unsupported method'),
- 401: ('Unauthorized',
- 'No permission -- see authorization schemes'),
- 402: ('Payment Required',
- 'No payment -- see charging schemes'),
- 403: ('Forbidden',
- 'Request forbidden -- authorization will not help'),
- 404: ('Not Found', 'Nothing matches the given URI'),
- 405: ('Method Not Allowed',
- 'Specified method is invalid for this resource.'),
- 406: ('Not Acceptable', 'URI not available in preferred format.'),
- 407: ('Proxy Authentication Required', 'You must authenticate with '
- 'this proxy before proceeding.'),
- 408: ('Request Timeout', 'Request timed out; try again later.'),
- 409: ('Conflict', 'Request conflict.'),
- 410: ('Gone',
- 'URI no longer exists and has been permanently removed.'),
- 411: ('Length Required', 'Client must specify Content-Length.'),
- 412: ('Precondition Failed', 'Precondition in headers is false.'),
- 413: ('Request Entity Too Large', 'Entity is too large.'),
- 414: ('Request-URI Too Long', 'URI is too long.'),
- 415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
- 416: ('Requested Range Not Satisfiable',
- 'Cannot satisfy request range.'),
- 417: ('Expectation Failed',
- 'Expect condition could not be satisfied.'),
- 428: ('Precondition Required',
- 'The origin server requires the request to be conditional.'),
- 429: ('Too Many Requests', 'The user has sent too many requests '
- 'in a given amount of time ("rate limiting").'),
- 431: ('Request Header Fields Too Large', 'The server is unwilling to '
- 'process the request because its header fields are too large.'),
-
- 500: ('Internal Server Error', 'Server got itself in trouble'),
- 501: ('Not Implemented',
- 'Server does not support this operation'),
- 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
- 503: ('Service Unavailable',
- 'The server cannot process the request due to a high load'),
- 504: ('Gateway Timeout',
- 'The gateway server did not receive a timely response'),
- 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
- 511: ('Network Authentication Required',
- 'The client needs to authenticate to gain network access.'),
- }
+ v: (v.phrase, v.description)
+ for v in HTTPStatus.__members__.values()
+ }
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
@@ -707,7 +654,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
parts = urllib.parse.urlsplit(self.path)
if not parts.path.endswith('/'):
# redirect browser - doing basically what apache does
- self.send_response(301)
+ self.send_response(HTTPStatus.MOVED_PERMANENTLY)
new_parts = (parts[0], parts[1], parts[2] + '/',
parts[3], parts[4])
new_url = urllib.parse.urlunsplit(new_parts)
@@ -725,10 +672,10 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
try:
f = open(path, 'rb')
except OSError:
- self.send_error(404, "File not found")
+ self.send_error(HTTPStatus.NOT_FOUND, "File not found")
return None
try:
- self.send_response(200)
+ self.send_response(HTTPStatus.OK)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
@@ -750,7 +697,9 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
try:
list = os.listdir(path)
except OSError:
- self.send_error(404, "No permission to list directory")
+ self.send_error(
+ HTTPStatus.NOT_FOUND,
+ "No permission to list directory")
return None
list.sort(key=lambda a: a.lower())
r = []
@@ -789,7 +738,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
f = io.BytesIO()
f.write(encoded)
f.seek(0)
- self.send_response(200)
+ self.send_response(HTTPStatus.OK)
self.send_header("Content-type", "text/html; charset=%s" % enc)
self.send_header("Content-Length", str(len(encoded)))
self.end_headers()
@@ -971,7 +920,9 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
if self.is_cgi():
self.run_cgi()
else:
- self.send_error(501, "Can only POST to CGI scripts")
+ self.send_error(
+ HTTPStatus.NOT_IMPLEMENTED,
+ "Can only POST to CGI scripts")
def send_head(self):
"""Version of send_head that support CGI scripts"""
@@ -1049,17 +1000,21 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
scriptname = dir + '/' + script
scriptfile = self.translate_path(scriptname)
if not os.path.exists(scriptfile):
- self.send_error(404, "No such CGI script (%r)" % scriptname)
+ self.send_error(
+ HTTPStatus.NOT_FOUND,
+ "No such CGI script (%r)" % scriptname)
return
if not os.path.isfile(scriptfile):
- self.send_error(403, "CGI script is not a plain file (%r)" %
- scriptname)
+ self.send_error(
+ HTTPStatus.FORBIDDEN,
+ "CGI script is not a plain file (%r)" % scriptname)
return
ispy = self.is_python(scriptname)
if self.have_fork or not ispy:
if not self.is_executable(scriptfile):
- self.send_error(403, "CGI script is not executable (%r)" %
- scriptname)
+ self.send_error(
+ HTTPStatus.FORBIDDEN,
+ "CGI script is not executable (%r)" % scriptname)
return
# Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
@@ -1127,7 +1082,7 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
env.setdefault(k, "")
- self.send_response(200, "Script output follows")
+ self.send_response(HTTPStatus.OK, "Script output follows")
self.flush_headers()
decoded_query = query.replace('+', ' ')
diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt
index a3a8fbf..828142c 100644
--- a/Lib/idlelib/NEWS.txt
+++ b/Lib/idlelib/NEWS.txt
@@ -1,4 +1,4 @@
-What's New in Idle 3.4.3?
+What's New in IDLE 3.5.0?
=========================
- Issue #16893: Update Idle doc chapter to match current Idle and add new
@@ -21,10 +21,6 @@ What's New in Idle 3.4.3?
- Issue #21986: Code objects are not normally pickled by the pickle module.
To match this, they are no longer pickled when running under Idle.
-
-What's New in IDLE 3.4.2?
-=========================
-
- Issue #17390: Adjust Editor window title; remove 'Python',
move version to end.
@@ -59,13 +55,8 @@ What's New in IDLE 3.4.2?
- Issue #18409: Add unittest for AutoComplete. Patch by Phil Webster.
-- Issue #18104: Add idlelib/idle_test/htest.py with a few sample tests to begin
- consolidating and improving human-validated tests of Idle. Change other files
- as needed to work with htest. Running the module as __main__ runs all tests.
-
-
-What's New in IDLE 3.4.1?
-=========================
+- Issue #21477: htest.py - Improve framework, complete set of tests.
+ Patches by Saimadhav Heblikar
- Issue #18104: Add idlelib/idle_test/htest.py with a few sample tests to begin
consolidating and improving human-validated tests of Idle. Change other files
diff --git a/Lib/idlelib/WidgetRedirector.py b/Lib/idlelib/WidgetRedirector.py
index b3d7bfa..67d7f61 100644
--- a/Lib/idlelib/WidgetRedirector.py
+++ b/Lib/idlelib/WidgetRedirector.py
@@ -47,8 +47,9 @@ class WidgetRedirector:
tk.createcommand(w, self.dispatch)
def __repr__(self):
- return "WidgetRedirector(%s<%s>)" % (self.widget.__class__.__name__,
- self.widget._w)
+ return "%s(%s<%s>)" % (self.__class__.__name__,
+ self.widget.__class__.__name__,
+ self.widget._w)
def close(self):
"Unregister operations and revert redirection created by .__init__."
@@ -142,7 +143,8 @@ class OriginalCommand:
self.orig_and_operation = (redir.orig, operation)
def __repr__(self):
- return "OriginalCommand(%r, %r)" % (self.redir, self.operation)
+ return "%s(%r, %r)" % (self.__class__.__name__,
+ self.redir, self.operation)
def __call__(self, *args):
return self.tk_call(self.orig_and_operation + args)
diff --git a/Lib/idlelib/idle_test/test_searchengine.py b/Lib/idlelib/idle_test/test_searchengine.py
index 129a5a3..2c10461 100644
--- a/Lib/idlelib/idle_test/test_searchengine.py
+++ b/Lib/idlelib/idle_test/test_searchengine.py
@@ -178,7 +178,7 @@ class SearchEngineTest(unittest.TestCase):
engine.revar.set(1)
Equal(engine.getprog(), None)
self.assertEqual(Mbox.showerror.message,
- 'Error: nothing to repeat\nPattern: +')
+ 'Error: nothing to repeat at position 0\nPattern: +')
def test_report_error(self):
showerror = Mbox.showerror
diff --git a/Lib/idlelib/idlever.py b/Lib/idlelib/idlever.py
index 87d84fc..1ffbc76 100644
--- a/Lib/idlelib/idlever.py
+++ b/Lib/idlelib/idlever.py
@@ -1 +1 @@
-IDLE_VERSION = "3.4.3rc1"
+IDLE_VERSION = "3.5.0a1"
diff --git a/Lib/imaplib.py b/Lib/imaplib.py
index 72ab5d7..49ee39d 100644
--- a/Lib/imaplib.py
+++ b/Lib/imaplib.py
@@ -238,6 +238,14 @@ class IMAP4:
return getattr(self, attr.lower())
raise AttributeError("Unknown IMAP4 command: '%s'" % attr)
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ try:
+ self.logout()
+ except OSError:
+ pass
# Overridable methods
diff --git a/Lib/imghdr.py b/Lib/imghdr.py
index add2ea8..b267925 100644
--- a/Lib/imghdr.py
+++ b/Lib/imghdr.py
@@ -110,6 +110,18 @@ def test_bmp(h, f):
tests.append(test_bmp)
+def test_webp(h, f):
+ if h.startswith(b'RIFF') and h[8:12] == b'WEBP':
+ return 'webp'
+
+tests.append(test_webp)
+
+def test_exr(h, f):
+ if h.startswith(b'\x76\x2f\x31\x01'):
+ return 'exr'
+
+tests.append(test_exr)
+
#--------------------#
# Small test program #
#--------------------#
diff --git a/Lib/imp.py b/Lib/imp.py
index c8449c6..59ce41c 100644
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -16,7 +16,7 @@ except ImportError:
# Platform doesn't support dynamic loading.
load_dynamic = None
-from importlib._bootstrap import SourcelessFileLoader, _ERR_MSG, _SpecMethods
+from importlib._bootstrap import SourcelessFileLoader, _ERR_MSG, _exec, _load
from importlib import machinery
from importlib import util
@@ -164,11 +164,10 @@ class _LoadSourceCompatibility(_HackedGetData, machinery.SourceFileLoader):
def load_source(name, pathname, file=None):
loader = _LoadSourceCompatibility(name, pathname, file)
spec = util.spec_from_file_location(name, pathname, loader=loader)
- methods = _SpecMethods(spec)
if name in sys.modules:
- module = methods.exec(sys.modules[name])
+ module = _exec(spec, sys.modules[name])
else:
- module = methods.load()
+ module = _load(spec)
# To allow reloading to potentially work, use a non-hacked loader which
# won't rely on a now-closed file object.
module.__loader__ = machinery.SourceFileLoader(name, pathname)
@@ -185,11 +184,10 @@ def load_compiled(name, pathname, file=None):
"""**DEPRECATED**"""
loader = _LoadCompiledCompatibility(name, pathname, file)
spec = util.spec_from_file_location(name, pathname, loader=loader)
- methods = _SpecMethods(spec)
if name in sys.modules:
- module = methods.exec(sys.modules[name])
+ module = _exec(spec, sys.modules[name])
else:
- module = methods.load()
+ module = _load(spec)
# To allow reloading to potentially work, use a non-hacked loader which
# won't rely on a now-closed file object.
module.__loader__ = SourcelessFileLoader(name, pathname)
@@ -210,11 +208,10 @@ def load_package(name, path):
raise ValueError('{!r} is not a package'.format(path))
spec = util.spec_from_file_location(name, path,
submodule_search_locations=[])
- methods = _SpecMethods(spec)
if name in sys.modules:
- return methods.exec(sys.modules[name])
+ return _exec(spec, sys.modules[name])
else:
- return methods.load()
+ return _load(spec)
def load_module(name, file, filename, details):
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py
index 1bc9947..e99f50e 100644
--- a/Lib/importlib/__init__.py
+++ b/Lib/importlib/__init__.py
@@ -73,7 +73,7 @@ def find_loader(name, path=None):
except KeyError:
pass
except AttributeError:
- raise ValueError('{}.__loader__ is not set'.format(name))
+ raise ValueError('{}.__loader__ is not set'.format(name)) from None
spec = _bootstrap._find_spec(name, path)
# We won't worry about malformed specs (missing attributes).
@@ -138,15 +138,15 @@ def reload(module):
parent = sys.modules[parent_name]
except KeyError:
msg = "parent {!r} not in sys.modules"
- raise ImportError(msg.format(parent_name), name=parent_name)
+ raise ImportError(msg.format(parent_name),
+ name=parent_name) from None
else:
pkgpath = parent.__path__
else:
pkgpath = None
target = module
spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target)
- methods = _bootstrap._SpecMethods(spec)
- methods.exec(module)
+ _bootstrap._exec(spec, module)
# The module may have replaced itself in sys.modules!
return sys.modules[name]
finally:
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 5b91c05..0ed7cc6 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -9,7 +9,7 @@ work. One should use importlib as the public-facing version of this module.
#
# IMPORTANT: Whenever making changes to this module, be sure to run
# a top-level make in order to get the frozen version of the module
-# update. Not doing so will result in the Makefile to fail for
+# updated. Not doing so will result in the Makefile to fail for
# all others who don't have a ./python around to freeze the module
# in the early stages of compilation.
#
@@ -419,12 +419,13 @@ def _call_with_frames_removed(f, *args, **kwds):
# Python 3.4a4 3290 (changes to __qualname__ computation)
# Python 3.4a4 3300 (more changes to __qualname__ computation)
# Python 3.4rc2 3310 (alter __qualname__ computation)
+# Python 3.5a0 3320 (matrix multiplication operator)
#
# MAGIC must change whenever the bytecode emitted by the compiler may no
# longer be understood by older implementations of the eval loop (usually
# due to the addition of new opcodes).
-MAGIC_NUMBER = (3310).to_bytes(2, 'little') + b'\r\n'
+MAGIC_NUMBER = (3320).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
_PYCACHE = '__pycache__'
@@ -580,6 +581,7 @@ def _find_module_shim(self, fullname):
return loader
+# Typically used by loader classes as a method replacement.
def _load_module_shim(self, fullname):
"""Load the specified module into sys.modules and return it.
@@ -587,13 +589,12 @@ def _load_module_shim(self, fullname):
"""
spec = spec_from_loader(fullname, self)
- methods = _SpecMethods(spec)
if fullname in sys.modules:
module = sys.modules[fullname]
- methods.exec(module)
+ _exec(spec, module)
return sys.modules[fullname]
else:
- return methods.load()
+ return _load(spec)
def _validate_bytecode_header(data, source_stats=None, name=None, path=None):
@@ -704,7 +705,7 @@ def _module_repr(module):
pass
else:
if spec is not None:
- return _SpecMethods(spec).module_repr()
+ return _module_repr_from_spec(spec)
# We could use module.__class__.__name__ instead of 'module' in the
# various repr permutations.
@@ -990,234 +991,186 @@ def _spec_from_module(module, loader=None, origin=None):
return spec
-class _SpecMethods:
-
- """Convenience wrapper around spec objects to provide spec-specific
- methods."""
-
- # The various spec_from_* functions could be made factory methods here.
-
- def __init__(self, spec):
- self.spec = spec
-
- def module_repr(self):
- """Return the repr to use for the module."""
- # We mostly replicate _module_repr() using the spec attributes.
- spec = self.spec
- name = '?' if spec.name is None else spec.name
- if spec.origin is None:
- if spec.loader is None:
- return '<module {!r}>'.format(name)
- else:
- return '<module {!r} ({!r})>'.format(name, spec.loader)
- else:
- if spec.has_location:
- return '<module {!r} from {!r}>'.format(name, spec.origin)
- else:
- return '<module {!r} ({})>'.format(spec.name, spec.origin)
-
- def init_module_attrs(self, module, *, _override=False, _force_name=True):
- """Set the module's attributes.
-
- All missing import-related module attributes will be set. Here
- is how the spec attributes map onto the module:
-
- spec.name -> module.__name__
- spec.loader -> module.__loader__
- spec.parent -> module.__package__
- spec -> module.__spec__
-
- Optional:
- spec.origin -> module.__file__ (if spec.set_fileattr is true)
- spec.cached -> module.__cached__ (if __file__ also set)
- spec.submodule_search_locations -> module.__path__ (if set)
-
- """
- spec = self.spec
-
- # The passed in module may be not support attribute assignment,
- # in which case we simply don't set the attributes.
-
- # __name__
- if (_override or _force_name or
- getattr(module, '__name__', None) is None):
- try:
- module.__name__ = spec.name
- except AttributeError:
- pass
-
- # __loader__
- if _override or getattr(module, '__loader__', None) is None:
- loader = spec.loader
- if loader is None:
- # A backward compatibility hack.
- if spec.submodule_search_locations is not None:
- loader = _NamespaceLoader.__new__(_NamespaceLoader)
- loader._path = spec.submodule_search_locations
+def _init_module_attrs(spec, module, *, override=False):
+ # The passed-in module may be not support attribute assignment,
+ # in which case we simply don't set the attributes.
+ # __name__
+ if (override or getattr(module, '__name__', None) is None):
+ try:
+ module.__name__ = spec.name
+ except AttributeError:
+ pass
+ # __loader__
+ if override or getattr(module, '__loader__', None) is None:
+ loader = spec.loader
+ if loader is None:
+ # A backward compatibility hack.
+ if spec.submodule_search_locations is not None:
+ loader = _NamespaceLoader.__new__(_NamespaceLoader)
+ loader._path = spec.submodule_search_locations
+ try:
+ module.__loader__ = loader
+ except AttributeError:
+ pass
+ # __package__
+ if override or getattr(module, '__package__', None) is None:
+ try:
+ module.__package__ = spec.parent
+ except AttributeError:
+ pass
+ # __spec__
+ try:
+ module.__spec__ = spec
+ except AttributeError:
+ pass
+ # __path__
+ if override or getattr(module, '__path__', None) is None:
+ if spec.submodule_search_locations is not None:
try:
- module.__loader__ = loader
+ module.__path__ = spec.submodule_search_locations
except AttributeError:
pass
-
- # __package__
- if _override or getattr(module, '__package__', None) is None:
+ # __file__/__cached__
+ if spec.has_location:
+ if override or getattr(module, '__file__', None) is None:
try:
- module.__package__ = spec.parent
+ module.__file__ = spec.origin
except AttributeError:
pass
- # __spec__
- try:
- module.__spec__ = spec
- except AttributeError:
- pass
-
- # __path__
- if _override or getattr(module, '__path__', None) is None:
- if spec.submodule_search_locations is not None:
- try:
- module.__path__ = spec.submodule_search_locations
- except AttributeError:
- pass
-
- if spec.has_location:
- # __file__
- if _override or getattr(module, '__file__', None) is None:
+ if override or getattr(module, '__cached__', None) is None:
+ if spec.cached is not None:
try:
- module.__file__ = spec.origin
+ module.__cached__ = spec.cached
except AttributeError:
pass
+ return module
- # __cached__
- if _override or getattr(module, '__cached__', None) is None:
- if spec.cached is not None:
- try:
- module.__cached__ = spec.cached
- except AttributeError:
- pass
- def create(self):
- """Return a new module to be loaded.
+def module_from_spec(spec):
+ """Create a module based on the provided spec."""
+ # Typically loaders will not implement create_module().
+ module = None
+ if hasattr(spec.loader, 'create_module'):
+ # If create_module() returns `None` then it means default
+ # 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)
+ if module is None:
+ module = _new_module(spec.name)
+ _init_module_attrs(spec, module)
+ return module
- The import-related module attributes are also set with the
- appropriate values from the spec.
- """
- spec = self.spec
- # Typically loaders will not implement create_module().
- if hasattr(spec.loader, 'create_module'):
- # If create_module() returns `None` it means the default
- # module creation should be used.
- module = spec.loader.create_module(spec)
+def _module_repr_from_spec(spec):
+ """Return the repr to use for the module."""
+ # We mostly replicate _module_repr() using the spec attributes.
+ name = '?' if spec.name is None else spec.name
+ if spec.origin is None:
+ if spec.loader is None:
+ return '<module {!r}>'.format(name)
else:
- module = None
- if module is None:
- # This must be done before open() is ever called as the 'io'
- # module implicitly imports 'locale' and would otherwise
- # trigger an infinite loop.
- module = _new_module(spec.name)
- self.init_module_attrs(module)
- return module
-
- def _exec(self, module):
- """Do everything necessary to execute the module.
-
- The namespace of `module` is used as the target of execution.
- This method uses the loader's `exec_module()` method.
+ return '<module {!r} ({!r})>'.format(name, spec.loader)
+ else:
+ if spec.has_location:
+ return '<module {!r} from {!r}>'.format(name, spec.origin)
+ else:
+ return '<module {!r} ({})>'.format(spec.name, spec.origin)
- """
- self.spec.loader.exec_module(module)
- # Used by importlib.reload() and _load_module_shim().
- def exec(self, module):
- """Execute the spec in an existing module's namespace."""
- name = self.spec.name
- _imp.acquire_lock()
- with _ModuleLockManager(name):
- if sys.modules.get(name) is not module:
- msg = 'module {!r} not in sys.modules'.format(name)
- raise ImportError(msg, name=name)
- if self.spec.loader is None:
- if self.spec.submodule_search_locations is None:
- raise ImportError('missing loader', name=self.spec.name)
- # namespace package
- self.init_module_attrs(module, _override=True)
- return module
- self.init_module_attrs(module, _override=True)
- if not hasattr(self.spec.loader, 'exec_module'):
- # (issue19713) Once BuiltinImporter and ExtensionFileLoader
- # have exec_module() implemented, we can add a deprecation
- # warning here.
- self.spec.loader.load_module(name)
- else:
- self._exec(module)
- return sys.modules[name]
-
- def _load_backward_compatible(self):
- # (issue19713) Once BuiltinImporter and ExtensionFileLoader
- # have exec_module() implemented, we can add a deprecation
- # warning here.
- spec = self.spec
- spec.loader.load_module(spec.name)
- # The module must be in sys.modules at this point!
- module = sys.modules[spec.name]
- if getattr(module, '__loader__', None) is None:
- try:
- module.__loader__ = spec.loader
- except AttributeError:
- pass
- if getattr(module, '__package__', None) is None:
- try:
- # Since module.__path__ may not line up with
- # spec.submodule_search_paths, we can't necessarily rely
- # on spec.parent here.
- module.__package__ = module.__name__
- if not hasattr(module, '__path__'):
- module.__package__ = spec.name.rpartition('.')[0]
- except AttributeError:
- pass
- if getattr(module, '__spec__', None) is None:
- try:
- module.__spec__ = spec
- except AttributeError:
- pass
- return module
+# Used by importlib.reload() and _load_module_shim().
+def _exec(spec, module):
+ """Execute the spec in an existing module's namespace."""
+ name = spec.name
+ _imp.acquire_lock()
+ with _ModuleLockManager(name):
+ if sys.modules.get(name) is not module:
+ msg = 'module {!r} not in sys.modules'.format(name)
+ raise ImportError(msg, name=name)
+ if spec.loader is None:
+ if spec.submodule_search_locations is None:
+ raise ImportError('missing loader', name=spec.name)
+ # namespace package
+ _init_module_attrs(spec, module, override=True)
+ return module
+ _init_module_attrs(spec, module, override=True)
+ if not hasattr(spec.loader, 'exec_module'):
+ # (issue19713) Once BuiltinImporter and ExtensionFileLoader
+ # have exec_module() implemented, we can add a deprecation
+ # warning here.
+ spec.loader.load_module(name)
+ else:
+ spec.loader.exec_module(module)
+ return sys.modules[name]
+
+
+def _load_backward_compatible(spec):
+ # (issue19713) Once BuiltinImporter and ExtensionFileLoader
+ # have exec_module() implemented, we can add a deprecation
+ # warning here.
+ spec.loader.load_module(spec.name)
+ # The module must be in sys.modules at this point!
+ module = sys.modules[spec.name]
+ if getattr(module, '__loader__', None) is None:
+ try:
+ module.__loader__ = spec.loader
+ except AttributeError:
+ pass
+ if getattr(module, '__package__', None) is None:
+ try:
+ # Since module.__path__ may not line up with
+ # spec.submodule_search_paths, we can't necessarily rely
+ # on spec.parent here.
+ module.__package__ = module.__name__
+ if not hasattr(module, '__path__'):
+ module.__package__ = spec.name.rpartition('.')[0]
+ except AttributeError:
+ pass
+ if getattr(module, '__spec__', None) is None:
+ try:
+ module.__spec__ = spec
+ except AttributeError:
+ pass
+ return module
- def _load_unlocked(self):
- # A helper for direct use by the import system.
- if self.spec.loader is not None:
- # not a namespace package
- if not hasattr(self.spec.loader, 'exec_module'):
- return self._load_backward_compatible()
-
- module = self.create()
- with _installed_safely(module):
- if self.spec.loader is None:
- if self.spec.submodule_search_locations is None:
- raise ImportError('missing loader', name=self.spec.name)
- # A namespace package so do nothing.
- else:
- self._exec(module)
+def _load_unlocked(spec):
+ # A helper for direct use by the import system.
+ if spec.loader is not None:
+ # not a namespace package
+ if not hasattr(spec.loader, 'exec_module'):
+ return _load_backward_compatible(spec)
+
+ module = module_from_spec(spec)
+ with _installed_safely(module):
+ if spec.loader is None:
+ if spec.submodule_search_locations is None:
+ raise ImportError('missing loader', name=spec.name)
+ # A namespace package so do nothing.
+ else:
+ spec.loader.exec_module(module)
- # We don't ensure that the import-related module attributes get
- # set in the sys.modules replacement case. Such modules are on
- # their own.
- return sys.modules[self.spec.name]
+ # We don't ensure that the import-related module attributes get
+ # set in the sys.modules replacement case. Such modules are on
+ # their own.
+ return sys.modules[spec.name]
- # A method used during testing of _load_unlocked() and by
- # _load_module_shim().
- def load(self):
- """Return a new module object, loaded by the spec's loader.
+# A method used during testing of _load_unlocked() and by
+# _load_module_shim().
+def _load(spec):
+ """Return a new module object, loaded by the spec's loader.
- The module is not added to its parent.
+ The module is not added to its parent.
- If a module is already in sys.modules, that existing module gets
- clobbered.
+ If a module is already in sys.modules, that existing module gets
+ clobbered.
- """
- _imp.acquire_lock()
- with _ModuleLockManager(self.spec.name):
- return self._load_unlocked()
+ """
+ _imp.acquire_lock()
+ with _ModuleLockManager(spec.name):
+ return _load_unlocked(spec)
def _fix_up_module(ns, name, pathname, cpathname=None):
@@ -1349,6 +1302,10 @@ class FrozenImporter:
"""
return cls if _imp.is_frozen(fullname) else None
+ @classmethod
+ def create_module(cls, spec):
+ """Use default semantics for module creation."""
+
@staticmethod
def exec_module(module):
name = module.__spec__.name
@@ -1462,6 +1419,9 @@ class _LoaderBasics:
tail_name = fullname.rpartition('.')[2]
return filename_base == '__init__' and tail_name != '__init__'
+ def create_module(self, spec):
+ """Use default semantics for module creation."""
+
def exec_module(self, module):
"""Execute the module."""
code = self.get_code(module.__name__)
@@ -1799,7 +1759,7 @@ class _NamespacePath:
self._path.append(item)
-# We use this exclusively in init_module_attrs() for backward-compatibility.
+# We use this exclusively in module_from_spec() for backward-compatibility.
class _NamespaceLoader:
def __init__(self, name, path, path_finder):
self._path = _NamespacePath(name, path, path_finder)
@@ -1822,6 +1782,9 @@ class _NamespaceLoader:
def get_code(self, fullname):
return compile('', '<string>', 'exec', dont_inherit=True)
+ def create_module(self, spec):
+ """Use default semantics for module creation."""
+
def exec_module(self, module):
pass
@@ -1857,7 +1820,7 @@ class PathFinder:
If 'hooks' is false then use sys.path_hooks.
"""
- if not sys.path_hooks:
+ 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:
try:
@@ -1876,7 +1839,12 @@ class PathFinder:
"""
if path == '':
- path = _os.getcwd()
+ try:
+ path = _os.getcwd()
+ except FileNotFoundError:
+ # Don't cache the failure as the cwd can easily change to
+ # a valid directory later on.
+ return None
try:
finder = sys.path_importer_cache[path]
except KeyError:
@@ -2146,7 +2114,7 @@ def _find_spec_legacy(finder, name, path):
def _find_spec(name, path, target=None):
"""Find a module's loader."""
- if not sys.meta_path:
+ if sys.meta_path is not None and not sys.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
@@ -2218,12 +2186,12 @@ 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)
+ raise ImportError(msg, name=name) from None
spec = _find_spec(name, path)
if spec is None:
raise ImportError(_ERR_MSG.format(name), name=name)
else:
- module = _SpecMethods(spec)._load_unlocked()
+ module = _load_unlocked(spec)
if parent:
# Set the module as an attribute on its parent.
parent_module = sys.modules[parent]
@@ -2358,8 +2326,7 @@ def _builtin_from_name(name):
spec = BuiltinImporter.find_spec(name)
if spec is None:
raise ImportError('no built-in module named ' + name)
- methods = _SpecMethods(spec)
- return methods._load_unlocked()
+ return _load_unlocked(spec)
def _setup(sys_module, _imp_module):
@@ -2390,8 +2357,7 @@ def _setup(sys_module, _imp_module):
else:
continue
spec = _spec_from_module(module, loader)
- methods = _SpecMethods(spec)
- methods.init_module_attrs(module)
+ _init_module_attrs(spec, module)
# Directly load built-in modules needed during bootstrap.
self_module = sys.modules[__name__]
diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py
index 558abd3..2878488 100644
--- a/Lib/importlib/abc.py
+++ b/Lib/importlib/abc.py
@@ -122,11 +122,8 @@ class Loader(metaclass=abc.ABCMeta):
This method should raise ImportError if anything prevents it
from creating a new module. It may return None to indicate
that the spec should create the new module.
-
- create_module() is optional.
-
"""
- # By default, defer to _SpecMethods.create() for the new module.
+ # By default, defer to default semantics for the new module.
return None
# We don't define exec_module() here since that would break
@@ -217,7 +214,8 @@ class InspectLoader(Loader):
"""
raise ImportError
- def source_to_code(self, data, path='<string>'):
+ @staticmethod
+ def source_to_code(data, path='<string>'):
"""Compile 'data' into a code object.
The 'data' argument can be anything that compile() can handle. The'path'
diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py
index 6d73b1d..c42ef14 100644
--- a/Lib/importlib/util.py
+++ b/Lib/importlib/util.py
@@ -1,8 +1,9 @@
"""Utility code for constructing importers, etc."""
-
+from . import abc
from ._bootstrap import MAGIC_NUMBER
from ._bootstrap import cache_from_source
from ._bootstrap import decode_source
+from ._bootstrap import module_from_spec
from ._bootstrap import source_from_cache
from ._bootstrap import spec_from_loader
from ._bootstrap import spec_from_file_location
@@ -12,6 +13,7 @@ from ._bootstrap import _find_spec
from contextlib import contextmanager
import functools
import sys
+import types
import warnings
@@ -54,7 +56,7 @@ def _find_spec_from_path(name, path=None):
try:
spec = module.__spec__
except AttributeError:
- raise ValueError('{}.__spec__ is not set'.format(name))
+ raise ValueError('{}.__spec__ is not set'.format(name)) from None
else:
if spec is None:
raise ValueError('{}.__spec__ is None'.format(name))
@@ -94,7 +96,7 @@ def find_spec(name, package=None):
try:
spec = module.__spec__
except AttributeError:
- raise ValueError('{}.__spec__ is not set'.format(name))
+ raise ValueError('{}.__spec__ is not set'.format(name)) from None
else:
if spec is None:
raise ValueError('{}.__spec__ is None'.format(name))
@@ -200,3 +202,94 @@ def module_for_loader(fxn):
return fxn(self, module, *args, **kwargs)
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."""
+
+ def __getattribute__(self, attr):
+ """Trigger the load of the module and return the attribute."""
+ # All module metadata must be garnered from __spec__ in order to avoid
+ # using mutated values.
+ # Stop triggering this method.
+ self.__class__ = _Module
+ # 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_now = self.__dict__
+ attrs_updated = {}
+ for key, value in attrs_now.items():
+ # Code that set the attribute may have kept a reference to the
+ # assigned object, making identity more important than equality.
+ if key not in attrs_then:
+ attrs_updated[key] = value
+ elif id(attrs_now[key]) != id(attrs_then[key]):
+ attrs_updated[key] = value
+ self.__spec__.loader.exec_module(self)
+ # If exec_module() was used directly there is no guarantee the module
+ # 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))
+ # Update after loading since that's what would happen in an eager
+ # loading situation.
+ self.__dict__.update(attrs_updated)
+ return getattr(self, attr)
+
+ def __delattr__(self, attr):
+ """Trigger the load and then perform the deletion."""
+ # To trigger the load and raise an exception if the attribute
+ # doesn't exist.
+ self.__getattribute__(attr)
+ delattr(self, attr)
+
+
+class LazyLoader(abc.Loader):
+
+ """A loader that creates a module which defers loading until attribute access."""
+
+ @staticmethod
+ def __check_eager_loader(loader):
+ if not hasattr(loader, 'exec_module'):
+ raise TypeError('loader must define exec_module()')
+ elif hasattr(loader.__class__, 'create_module'):
+ if abc.Loader.create_module != loader.__class__.create_module:
+ # Only care if create_module() is overridden in a subclass of
+ # importlib.abc.Loader.
+ raise TypeError('loader cannot define create_module()')
+
+ @classmethod
+ def factory(cls, loader):
+ """Construct a callable which returns the eager loader made lazy."""
+ cls.__check_eager_loader(loader)
+ return lambda *args, **kwargs: cls(loader(*args, **kwargs))
+
+ def __init__(self, loader):
+ self.__check_eager_loader(loader)
+ self.loader = loader
+
+ def create_module(self, spec):
+ """Create a module which can have its __class__ manipulated."""
+ return _Module(spec.name)
+
+ def exec_module(self, module):
+ """Make the module load lazily."""
+ module.__spec__.loader = self.loader
+ module.__loader__ = self.loader
+ # Don't need to worry about deep-copying as trying to set an attribute
+ # 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()
+ module.__class__ = _LazyModule
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 1641824..98d665d 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -17,7 +17,7 @@ Here are some of the useful functions provided by this module:
getclasstree() - arrange classes so as to represent their hierarchy
getargspec(), getargvalues(), getcallargs() - get info about function arguments
- getfullargspec() - same, with support for Python-3000 features
+ getfullargspec() - same, with support for Python 3 features
formatargspec(), formatargvalues() - format an argument spec
getouterframes(), getinnerframes() - get info about frames
currentframe() - get the current stack frame
@@ -32,6 +32,7 @@ __author__ = ('Ka-Ping Yee <ping@lfw.org>',
'Yury Selivanov <yselivanov@sprymix.com>')
import ast
+import enum
import importlib.machinery
import itertools
import linecache
@@ -822,6 +823,7 @@ def getsourcelines(object):
corresponding to the object and the line number indicates where in the
original source file the first line of code was found. An OSError is
raised if the source code cannot be retrieved."""
+ object = unwrap(object)
lines, lnum = findsource(object)
if ismodule(object): return lines, 0
@@ -919,13 +921,12 @@ ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
def getargspec(func):
"""Get the names and default values of a function's arguments.
- A tuple of four things is returned: (args, varargs, varkw, defaults).
- 'args' is a list of the argument names.
- 'args' will include keyword-only argument names.
- 'varargs' and 'varkw' are the names of the * and ** arguments or None.
+ 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.
- Use the getfullargspec() API for Python-3000 code, as annotations
+ 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.
"""
@@ -972,9 +973,10 @@ def getfullargspec(func):
# getfullargspec() historically ignored __wrapped__ attributes,
# so we ensure that remains the case in 3.3+
- sig = _signature_internal(func,
- follow_wrapper_chains=False,
- skip_bound_arg=False)
+ sig = _signature_from_callable(func,
+ follow_wrapper_chains=False,
+ skip_bound_arg=False,
+ sigcls=Signature)
except Exception as ex:
# Most of the times 'signature' will raise ValueError.
# But, it can also raise AttributeError, and, maybe something
@@ -1043,8 +1045,8 @@ def getargvalues(frame):
def formatannotation(annotation, base_module=None):
if isinstance(annotation, type):
if annotation.__module__ in ('builtins', base_module):
- return annotation.__name__
- return annotation.__module__+'.'+annotation.__name__
+ return annotation.__qualname__
+ return annotation.__module__+'.'+annotation.__qualname__
return repr(annotation)
def formatannotationrelativeto(object):
@@ -1317,6 +1319,8 @@ def getlineno(frame):
# FrameType.f_lineno is now a descriptor that grovels co_lnotab
return frame.f_lineno
+FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
+
def getouterframes(frame, context=1):
"""Get a list of records for a frame and all higher (calling) frames.
@@ -1324,7 +1328,8 @@ def getouterframes(frame, context=1):
name, a list of lines of context, and index within the context."""
framelist = []
while frame:
- framelist.append((frame,) + getframeinfo(frame, context))
+ frameinfo = (frame,) + getframeinfo(frame, context)
+ framelist.append(FrameInfo(*frameinfo))
frame = frame.f_back
return framelist
@@ -1335,7 +1340,8 @@ def getinnerframes(tb, context=1):
name, a list of lines of context, and index within the context."""
framelist = []
while tb:
- framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
+ frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
+ framelist.append(FrameInfo(*frameinfo))
tb = tb.tb_next
return framelist
@@ -1501,6 +1507,10 @@ _NonUserDefinedCallables = (_WrapperDescriptor,
def _signature_get_user_defined_method(cls, method_name):
+ """Private helper. Checks if ``cls`` has an attribute
+ named ``method_name`` and returns it only if it is a
+ pure python function.
+ """
try:
meth = getattr(cls, method_name)
except AttributeError:
@@ -1513,9 +1523,10 @@ def _signature_get_user_defined_method(cls, method_name):
def _signature_get_partial(wrapped_sig, partial, extra_args=()):
- # Internal helper to calculate how 'wrapped_sig' signature will
- # look like after applying a 'functools.partial' object (or alike)
- # on it.
+ """Private helper to calculate how 'wrapped_sig' signature will
+ look like after applying a 'functools.partial' object (or alike)
+ on it.
+ """
old_params = wrapped_sig.parameters
new_params = OrderedDict(old_params.items())
@@ -1588,8 +1599,9 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
def _signature_bound_method(sig):
- # Internal helper to transform signatures for unbound
- # functions to bound methods
+ """Private helper to transform signatures for unbound
+ functions to bound methods.
+ """
params = tuple(sig.parameters.values())
@@ -1613,8 +1625,9 @@ def _signature_bound_method(sig):
def _signature_is_builtin(obj):
- # Internal helper to test if `obj` is a callable that might
- # support Argument Clinic's __text_signature__ protocol.
+ """Private helper to test if `obj` is a callable that might
+ support Argument Clinic's __text_signature__ protocol.
+ """
return (isbuiltin(obj) or
ismethoddescriptor(obj) or
isinstance(obj, _NonUserDefinedCallables) or
@@ -1624,10 +1637,11 @@ def _signature_is_builtin(obj):
def _signature_is_functionlike(obj):
- # Internal helper to test if `obj` is a duck type of FunctionType.
- # A good example of such objects are functions compiled with
- # Cython, which have all attributes that a pure Python function
- # would have, but have their code statically compiled.
+ """Private helper to test if `obj` is a duck type of FunctionType.
+ A good example of such objects are functions compiled with
+ Cython, which have all attributes that a pure Python function
+ would have, but have their code statically compiled.
+ """
if not callable(obj) or isclass(obj):
# All function-like objects are obviously callables,
@@ -1648,11 +1662,12 @@ def _signature_is_functionlike(obj):
def _signature_get_bound_param(spec):
- # Internal helper to get first parameter name from a
- # __text_signature__ of a builtin method, which should
- # be in the following format: '($param1, ...)'.
- # Assumptions are that the first argument won't have
- # a default value or an annotation.
+ """ Private helper to get first parameter name from a
+ __text_signature__ of a builtin method, which should
+ be in the following format: '($param1, ...)'.
+ Assumptions are that the first argument won't have
+ a default value or an annotation.
+ """
assert spec.startswith('($')
@@ -1671,7 +1686,9 @@ def _signature_get_bound_param(spec):
def _signature_strip_non_python_syntax(signature):
"""
- Takes a signature in Argument Clinic's extended signature format.
+ Private helper function. Takes a signature in Argument Clinic's
+ extended signature format.
+
Returns a tuple of three things:
* that signature re-rendered in standard Python syntax,
* the index of the "self" parameter (generally 0), or None if
@@ -1740,8 +1757,10 @@ def _signature_strip_non_python_syntax(signature):
def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
- # Internal helper to parse content of '__text_signature__'
- # and return a Signature based on it
+ """Private helper to parse content of '__text_signature__'
+ and return a Signature based on it.
+ """
+
Parameter = cls._parameter_cls
clean_signature, self_parameter, last_positional_only = \
@@ -1879,8 +1898,10 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
def _signature_from_builtin(cls, func, skip_bound_arg=True):
- # Internal helper function to get signature for
- # builtin callables
+ """Private helper function to get signature for
+ builtin callables.
+ """
+
if not _signature_is_builtin(func):
raise TypeError("{!r} is not a Python builtin "
"function".format(func))
@@ -1892,7 +1913,14 @@ def _signature_from_builtin(cls, func, skip_bound_arg=True):
return _signature_fromstr(cls, func, s, skip_bound_arg)
-def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
+def _signature_from_callable(obj, *,
+ follow_wrapper_chains=True,
+ skip_bound_arg=True,
+ sigcls):
+
+ """Private helper function to get signature for arbitrary
+ callable objects.
+ """
if not callable(obj):
raise TypeError('{!r} is not a callable object'.format(obj))
@@ -1900,9 +1928,12 @@ def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
if isinstance(obj, types.MethodType):
# In this case we skip the first parameter of the underlying
# function (usually `self` or `cls`).
- sig = _signature_internal(obj.__func__,
- follow_wrapper_chains,
- skip_bound_arg)
+ sig = _signature_from_callable(
+ obj.__func__,
+ follow_wrapper_chains=follow_wrapper_chains,
+ skip_bound_arg=skip_bound_arg,
+ sigcls=sigcls)
+
if skip_bound_arg:
return _signature_bound_method(sig)
else:
@@ -1937,9 +1968,12 @@ def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
# (usually `self`, or `cls`) will not be passed
# automatically (as for boundmethods)
- wrapped_sig = _signature_internal(partialmethod.func,
- follow_wrapper_chains,
- skip_bound_arg)
+ wrapped_sig = _signature_from_callable(
+ partialmethod.func,
+ follow_wrapper_chains=follow_wrapper_chains,
+ skip_bound_arg=skip_bound_arg,
+ sigcls=sigcls)
+
sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
@@ -1950,16 +1984,18 @@ def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
if isfunction(obj) or _signature_is_functionlike(obj):
# If it's a pure Python function, or an object that is duck type
# of a Python function (Cython functions, for instance), then:
- return Signature.from_function(obj)
+ return sigcls.from_function(obj)
if _signature_is_builtin(obj):
- return _signature_from_builtin(Signature, obj,
+ return _signature_from_builtin(sigcls, obj,
skip_bound_arg=skip_bound_arg)
if isinstance(obj, functools.partial):
- wrapped_sig = _signature_internal(obj.func,
- follow_wrapper_chains,
- skip_bound_arg)
+ wrapped_sig = _signature_from_callable(
+ obj.func,
+ follow_wrapper_chains=follow_wrapper_chains,
+ skip_bound_arg=skip_bound_arg,
+ sigcls=sigcls)
return _signature_get_partial(wrapped_sig, obj)
sig = None
@@ -1970,23 +2006,29 @@ def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
# in its metaclass
call = _signature_get_user_defined_method(type(obj), '__call__')
if call is not None:
- sig = _signature_internal(call,
- follow_wrapper_chains,
- skip_bound_arg)
+ sig = _signature_from_callable(
+ call,
+ follow_wrapper_chains=follow_wrapper_chains,
+ skip_bound_arg=skip_bound_arg,
+ sigcls=sigcls)
else:
# Now we check if the 'obj' class has a '__new__' method
new = _signature_get_user_defined_method(obj, '__new__')
if new is not None:
- sig = _signature_internal(new,
- follow_wrapper_chains,
- skip_bound_arg)
+ sig = _signature_from_callable(
+ new,
+ follow_wrapper_chains=follow_wrapper_chains,
+ skip_bound_arg=skip_bound_arg,
+ sigcls=sigcls)
else:
# Finally, we should have at least __init__ implemented
init = _signature_get_user_defined_method(obj, '__init__')
if init is not None:
- sig = _signature_internal(init,
- follow_wrapper_chains,
- skip_bound_arg)
+ sig = _signature_from_callable(
+ init,
+ follow_wrapper_chains=follow_wrapper_chains,
+ skip_bound_arg=skip_bound_arg,
+ sigcls=sigcls)
if sig is None:
# At this point we know, that `obj` is a class, with no user-
@@ -2008,7 +2050,7 @@ def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
if text_sig:
# If 'obj' class has a __text_signature__ attribute:
# return a signature based on it
- return _signature_fromstr(Signature, obj, text_sig)
+ return _signature_fromstr(sigcls, obj, text_sig)
# No '__text_signature__' was found for the 'obj' class.
# Last option is to check if its '__init__' is
@@ -2028,9 +2070,11 @@ def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
call = _signature_get_user_defined_method(type(obj), '__call__')
if call is not None:
try:
- sig = _signature_internal(call,
- follow_wrapper_chains,
- skip_bound_arg)
+ sig = _signature_from_callable(
+ call,
+ follow_wrapper_chains=follow_wrapper_chains,
+ skip_bound_arg=skip_bound_arg,
+ sigcls=sigcls)
except ValueError as ex:
msg = 'no signature found for {!r}'.format(obj)
raise ValueError(msg) from ex
@@ -2050,41 +2094,35 @@ def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
raise ValueError('callable {!r} is not supported by signature'.format(obj))
-def signature(obj):
- '''Get a signature object for the passed callable.'''
- return _signature_internal(obj)
-
class _void:
- '''A private marker - used in Parameter & Signature'''
+ """A private marker - used in Parameter & Signature."""
class _empty:
- pass
+ """Marker object for Signature.empty and Parameter.empty."""
-class _ParameterKind(int):
- def __new__(self, *args, name):
- obj = int.__new__(self, *args)
- obj._name = name
- return obj
+class _ParameterKind(enum.IntEnum):
+ POSITIONAL_ONLY = 0
+ POSITIONAL_OR_KEYWORD = 1
+ VAR_POSITIONAL = 2
+ KEYWORD_ONLY = 3
+ VAR_KEYWORD = 4
def __str__(self):
- return self._name
-
- def __repr__(self):
- return '<_ParameterKind: {!r}>'.format(self._name)
+ return self._name_
-_POSITIONAL_ONLY = _ParameterKind(0, name='POSITIONAL_ONLY')
-_POSITIONAL_OR_KEYWORD = _ParameterKind(1, name='POSITIONAL_OR_KEYWORD')
-_VAR_POSITIONAL = _ParameterKind(2, name='VAR_POSITIONAL')
-_KEYWORD_ONLY = _ParameterKind(3, name='KEYWORD_ONLY')
-_VAR_KEYWORD = _ParameterKind(4, name='VAR_KEYWORD')
+_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
+_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
+_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
+_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
+_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
class Parameter:
- '''Represents a parameter in a function signature.
+ """Represents a parameter in a function signature.
Has the following public attributes:
@@ -2103,7 +2141,7 @@ class Parameter:
Possible values: `Parameter.POSITIONAL_ONLY`,
`Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
`Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
- '''
+ """
__slots__ = ('_name', '_kind', '_default', '_annotation')
@@ -2140,6 +2178,16 @@ class Parameter:
self._name = name
+ def __reduce__(self):
+ return (type(self),
+ (self._name, self._kind),
+ {'_default': self._default,
+ '_annotation': self._annotation})
+
+ def __setstate__(self, state):
+ self._default = state['_default']
+ self._annotation = state['_annotation']
+
@property
def name(self):
return self._name
@@ -2158,7 +2206,7 @@ class Parameter:
def replace(self, *, name=_void, kind=_void,
annotation=_void, default=_void):
- '''Creates a customized copy of the Parameter.'''
+ """Creates a customized copy of the Parameter."""
if name is _void:
name = self._name
@@ -2194,8 +2242,11 @@ class Parameter:
return formatted
def __repr__(self):
- return '<{} at {:#x} {!r}>'.format(self.__class__.__name__,
- id(self), self.name)
+ return '<{} at {:#x} "{}">'.format(self.__class__.__name__,
+ id(self), self)
+
+ def __hash__(self):
+ return hash((self.name, self.kind, self.annotation, self.default))
def __eq__(self, other):
return (issubclass(other.__class__, Parameter) and
@@ -2204,12 +2255,9 @@ class Parameter:
self._default == other._default and
self._annotation == other._annotation)
- def __ne__(self, other):
- return not self.__eq__(other)
-
class BoundArguments:
- '''Result of `Signature.bind` call. Holds the mapping of arguments
+ """Result of `Signature.bind` call. Holds the mapping of arguments
to the function's parameters.
Has the following public attributes:
@@ -2223,7 +2271,7 @@ class BoundArguments:
Tuple of positional arguments values.
* kwargs : dict
Dict of keyword arguments values.
- '''
+ """
def __init__(self, signature, arguments):
self.arguments = arguments
@@ -2291,12 +2339,9 @@ class BoundArguments:
self.signature == other.signature and
self.arguments == other.arguments)
- def __ne__(self, other):
- return not self.__eq__(other)
-
class Signature:
- '''A Signature object represents the overall signature of a function.
+ """A Signature object represents the overall signature of a function.
It stores a Parameter object for each parameter accepted by the
function, as well as information specific to the function itself.
@@ -2316,7 +2361,7 @@ class Signature:
* bind_partial(*args, **kwargs) -> BoundArguments
Creates a partial mapping from positional and keyword arguments
to parameters (simulating 'functools.partial' behavior.)
- '''
+ """
__slots__ = ('_return_annotation', '_parameters')
@@ -2327,9 +2372,9 @@ class Signature:
def __init__(self, parameters=None, *, return_annotation=_empty,
__validate_parameters__=True):
- '''Constructs Signature from the given list of Parameter
+ """Constructs Signature from the given list of Parameter
objects and 'return_annotation'. All arguments are optional.
- '''
+ """
if parameters is None:
params = OrderedDict()
@@ -2378,7 +2423,7 @@ class Signature:
@classmethod
def from_function(cls, func):
- '''Constructs Signature for the given python function'''
+ """Constructs Signature for the given python function."""
is_duck_function = False
if not isfunction(func):
@@ -2459,8 +2504,14 @@ class Signature:
@classmethod
def from_builtin(cls, func):
+ """Constructs Signature for the given builtin function."""
return _signature_from_builtin(cls, func)
+ @classmethod
+ def from_callable(cls, obj):
+ """Constructs Signature for the given callable object."""
+ return _signature_from_callable(obj, sigcls=cls)
+
@property
def parameters(self):
return self._parameters
@@ -2470,10 +2521,10 @@ class Signature:
return self._return_annotation
def replace(self, *, parameters=_void, return_annotation=_void):
- '''Creates a customized copy of the Signature.
+ """Creates a customized copy of the Signature.
Pass 'parameters' and/or 'return_annotation' arguments
to override them in the new copy.
- '''
+ """
if parameters is _void:
parameters = self.parameters.values()
@@ -2484,41 +2535,26 @@ class Signature:
return type(self)(parameters,
return_annotation=return_annotation)
- def __eq__(self, other):
- if (not issubclass(type(other), Signature) or
- self.return_annotation != other.return_annotation or
- len(self.parameters) != len(other.parameters)):
- return False
+ def _hash_basis(self):
+ params = tuple(param for param in self.parameters.values()
+ if param.kind != _KEYWORD_ONLY)
- other_positions = {param: idx
- for idx, param in enumerate(other.parameters.keys())}
+ kwo_params = {param.name: param for param in self.parameters.values()
+ if param.kind == _KEYWORD_ONLY}
- for idx, (param_name, param) in enumerate(self.parameters.items()):
- if param.kind == _KEYWORD_ONLY:
- try:
- other_param = other.parameters[param_name]
- except KeyError:
- return False
- else:
- if param != other_param:
- return False
- else:
- try:
- other_idx = other_positions[param_name]
- except KeyError:
- return False
- else:
- if (idx != other_idx or
- param != other.parameters[param_name]):
- return False
+ return params, kwo_params, self.return_annotation
- return True
+ def __hash__(self):
+ params, kwo_params, return_annotation = self._hash_basis()
+ kwo_params = frozenset(kwo_params.values())
+ return hash((params, kwo_params, return_annotation))
- def __ne__(self, other):
- return not self.__eq__(other)
+ def __eq__(self, other):
+ return (isinstance(other, Signature) and
+ self._hash_basis() == other._hash_basis())
def _bind(self, args, kwargs, *, partial=False):
- '''Private method. Don't use directly.'''
+ """Private method. Don't use directly."""
arguments = OrderedDict()
@@ -2645,19 +2681,31 @@ class Signature:
return self._bound_arguments_cls(self, arguments)
def bind(*args, **kwargs):
- '''Get a BoundArguments object, that maps the passed `args`
+ """Get a BoundArguments object, that maps the passed `args`
and `kwargs` to the function's signature. Raises `TypeError`
if the passed arguments can not be bound.
- '''
+ """
return args[0]._bind(args[1:], kwargs)
def bind_partial(*args, **kwargs):
- '''Get a BoundArguments object, that partially maps the
+ """Get a BoundArguments object, that partially maps the
passed `args` and `kwargs` to the function's signature.
Raises `TypeError` if the passed arguments can not be bound.
- '''
+ """
return args[0]._bind(args[1:], kwargs, partial=True)
+ def __reduce__(self):
+ return (type(self),
+ (tuple(self._parameters.values()),),
+ {'_return_annotation': self._return_annotation})
+
+ def __setstate__(self, state):
+ self._return_annotation = state['_return_annotation']
+
+ def __repr__(self):
+ return '<{} at {:#x} "{}">'.format(self.__class__.__name__,
+ id(self), self)
+
def __str__(self):
result = []
render_pos_only_separator = False
@@ -2703,6 +2751,12 @@ class Signature:
return rendered
+
+def signature(obj):
+ """Get a signature object for the passed callable."""
+ return Signature.from_callable(obj)
+
+
def _main():
""" Logic for inspecting an object given at command line """
import argparse
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index ac03c36..6c225f3 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -164,22 +164,23 @@ def _split_optional_netmask(address):
def _find_address_range(addresses):
- """Find a sequence of IPv#Address.
+ """Find a sequence of sorted deduplicated IPv#Address.
Args:
addresses: a list of IPv#Address objects.
- Returns:
+ Yields:
A tuple containing the first and last IP addresses in the sequence.
"""
- first = last = addresses[0]
- for ip in addresses[1:]:
- if ip._ip == last._ip + 1:
- last = ip
- else:
- break
- return (first, last)
+ it = iter(addresses)
+ first = last = next(it)
+ for ip in it:
+ if ip._ip != last._ip + 1:
+ yield first, last
+ first = ip
+ last = ip
+ yield first, last
def _count_righthand_zero_bits(number, bits):
@@ -195,11 +196,7 @@ def _count_righthand_zero_bits(number, bits):
"""
if number == 0:
return bits
- for i in range(bits):
- if (number >> i) & 1:
- return i
- # All bits of interest were zero, even if there are more in the number
- return bits
+ return min(bits, (~number & (number-1)).bit_length())
def summarize_address_range(first, last):
@@ -250,15 +247,14 @@ def summarize_address_range(first, last):
while first_int <= last_int:
nbits = min(_count_righthand_zero_bits(first_int, ip_bits),
(last_int - first_int + 1).bit_length() - 1)
- net = ip('%s/%d' % (first, ip_bits - nbits))
+ net = ip((first_int, ip_bits - nbits))
yield net
first_int += 1 << nbits
if first_int - 1 == ip._ALL_ONES:
break
- first = first.__class__(first_int)
-def _collapse_addresses_recursive(addresses):
+def _collapse_addresses_internal(addresses):
"""Loops through the addresses, collapsing concurrent netblocks.
Example:
@@ -268,7 +264,7 @@ def _collapse_addresses_recursive(addresses):
ip3 = IPv4Network('192.0.2.128/26')
ip4 = IPv4Network('192.0.2.192/26')
- _collapse_addresses_recursive([ip1, ip2, ip3, ip4]) ->
+ _collapse_addresses_internal([ip1, ip2, ip3, ip4]) ->
[IPv4Network('192.0.2.0/24')]
This shouldn't be called directly; it is called via
@@ -282,28 +278,29 @@ def _collapse_addresses_recursive(addresses):
passed.
"""
- while True:
- last_addr = None
- ret_array = []
- optimized = False
-
- for cur_addr in addresses:
- if not ret_array:
- last_addr = cur_addr
- ret_array.append(cur_addr)
- elif (cur_addr.network_address >= last_addr.network_address and
- cur_addr.broadcast_address <= last_addr.broadcast_address):
- optimized = True
- elif cur_addr == list(last_addr.supernet().subnets())[1]:
- ret_array[-1] = last_addr = last_addr.supernet()
- optimized = True
- else:
- last_addr = cur_addr
- ret_array.append(cur_addr)
-
- addresses = ret_array
- if not optimized:
- return addresses
+ # First merge
+ to_merge = list(addresses)
+ subnets = {}
+ while to_merge:
+ net = to_merge.pop()
+ supernet = net.supernet()
+ existing = subnets.get(supernet)
+ if existing is None:
+ subnets[supernet] = net
+ elif existing != net:
+ # Merge consecutive subnets
+ del subnets[supernet]
+ to_merge.append(supernet)
+ # Then iterate over resulting networks, skipping subsumed subnets
+ last = None
+ for net in sorted(subnets.values()):
+ if last is not None:
+ # Since they are sorted, last.network_address <= net.network_address
+ # is a given.
+ if last.broadcast_address >= net.broadcast_address:
+ continue
+ yield net
+ last = net
def collapse_addresses(addresses):
@@ -324,7 +321,6 @@ def collapse_addresses(addresses):
TypeError: If passed a list of mixed version objects.
"""
- i = 0
addrs = []
ips = []
nets = []
@@ -352,15 +348,13 @@ def collapse_addresses(addresses):
# sort and dedup
ips = sorted(set(ips))
- nets = sorted(set(nets))
- while i < len(ips):
- (first, last) = _find_address_range(ips[i:])
- i = ips.index(last) + 1
- addrs.extend(summarize_address_range(first, last))
+ # find consecutive address ranges in the sorted sequence and summarize them
+ if ips:
+ for first, last in _find_address_range(ips):
+ addrs.extend(summarize_address_range(first, last))
- return iter(_collapse_addresses_recursive(sorted(
- addrs + nets, key=_BaseNetwork._get_networks_key)))
+ return _collapse_addresses_internal(addrs + nets)
def get_mixed_type_key(obj):
@@ -403,6 +397,17 @@ class _IPAddressBase:
return str(self)
@property
+ def reverse_pointer(self):
+ """The name of the reverse DNS pointer for the IP address, e.g.:
+ >>> ipaddress.ip_address("127.0.0.1").reverse_pointer
+ '1.0.0.127.in-addr.arpa'
+ >>> ipaddress.ip_address("2001:db8::1").reverse_pointer
+ '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa'
+
+ """
+ return self._reverse_pointer()
+
+ @property
def version(self):
msg = '%200s has no version specified' % (type(self),)
raise NotImplementedError(msg)
@@ -423,7 +428,8 @@ class _IPAddressBase:
raise AddressValueError(msg % (address, address_len,
expected_len, self._version))
- def _ip_int_from_prefix(self, prefixlen):
+ @classmethod
+ def _ip_int_from_prefix(cls, prefixlen):
"""Turn the prefix length into a bitwise netmask
Args:
@@ -433,9 +439,10 @@ class _IPAddressBase:
An integer.
"""
- return self._ALL_ONES ^ (self._ALL_ONES >> prefixlen)
+ return cls._ALL_ONES ^ (cls._ALL_ONES >> prefixlen)
- def _prefix_from_ip_int(self, ip_int):
+ @classmethod
+ def _prefix_from_ip_int(cls, ip_int):
"""Return prefix length from the bitwise netmask.
Args:
@@ -448,22 +455,24 @@ class _IPAddressBase:
ValueError: If the input intermingles zeroes & ones
"""
trailing_zeroes = _count_righthand_zero_bits(ip_int,
- self._max_prefixlen)
- prefixlen = self._max_prefixlen - trailing_zeroes
+ cls._max_prefixlen)
+ prefixlen = cls._max_prefixlen - trailing_zeroes
leading_ones = ip_int >> trailing_zeroes
all_ones = (1 << prefixlen) - 1
if leading_ones != all_ones:
- byteslen = self._max_prefixlen // 8
+ byteslen = cls._max_prefixlen // 8
details = ip_int.to_bytes(byteslen, 'big')
msg = 'Netmask pattern %r mixes zeroes & ones'
raise ValueError(msg % details)
return prefixlen
- def _report_invalid_netmask(self, netmask_str):
+ @classmethod
+ def _report_invalid_netmask(cls, netmask_str):
msg = '%r is not a valid netmask' % netmask_str
raise NetmaskValueError(msg) from None
- def _prefix_from_prefix_string(self, prefixlen_str):
+ @classmethod
+ def _prefix_from_prefix_string(cls, prefixlen_str):
"""Return prefix length from a numeric string
Args:
@@ -478,16 +487,17 @@ class _IPAddressBase:
# int allows a leading +/- as well as surrounding whitespace,
# so we ensure that isn't the case
if not _BaseV4._DECIMAL_DIGITS.issuperset(prefixlen_str):
- self._report_invalid_netmask(prefixlen_str)
+ cls._report_invalid_netmask(prefixlen_str)
try:
prefixlen = int(prefixlen_str)
except ValueError:
- self._report_invalid_netmask(prefixlen_str)
- if not (0 <= prefixlen <= self._max_prefixlen):
- self._report_invalid_netmask(prefixlen_str)
+ cls._report_invalid_netmask(prefixlen_str)
+ if not (0 <= prefixlen <= cls._max_prefixlen):
+ cls._report_invalid_netmask(prefixlen_str)
return prefixlen
- def _prefix_from_ip_string(self, ip_str):
+ @classmethod
+ def _prefix_from_ip_string(cls, ip_str):
"""Turn a netmask/hostmask string into a prefix length
Args:
@@ -501,24 +511,27 @@ class _IPAddressBase:
"""
# Parse the netmask/hostmask like an IP address.
try:
- ip_int = self._ip_int_from_string(ip_str)
+ ip_int = cls._ip_int_from_string(ip_str)
except AddressValueError:
- self._report_invalid_netmask(ip_str)
+ cls._report_invalid_netmask(ip_str)
# Try matching a netmask (this would be /1*0*/ as a bitwise regexp).
# Note that the two ambiguous cases (all-ones and all-zeroes) are
# treated as netmasks.
try:
- return self._prefix_from_ip_int(ip_int)
+ return cls._prefix_from_ip_int(ip_int)
except ValueError:
pass
# Invert the bits, and try matching a /0+1+/ hostmask instead.
- ip_int ^= self._ALL_ONES
+ ip_int ^= cls._ALL_ONES
try:
- return self._prefix_from_ip_int(ip_int)
+ return cls._prefix_from_ip_int(ip_int)
except ValueError:
- self._report_invalid_netmask(ip_str)
+ cls._report_invalid_netmask(ip_str)
+
+ def __reduce__(self):
+ return self.__class__, (str(self),)
@functools.total_ordering
@@ -530,11 +543,6 @@ class _BaseAddress(_IPAddressBase):
used by single IP addresses.
"""
- def __init__(self, address):
- if (not isinstance(address, bytes)
- and '/' in str(address)):
- raise AddressValueError("Unexpected '/' in %r" % address)
-
def __int__(self):
return self._ip
@@ -579,6 +587,9 @@ class _BaseAddress(_IPAddressBase):
def _get_address_key(self):
return (self._version, self)
+ def __reduce__(self):
+ return self.__class__, (self._ip,)
+
@functools.total_ordering
class _BaseNetwork(_IPAddressBase):
@@ -765,7 +776,7 @@ class _BaseNetwork(_IPAddressBase):
other.broadcast_address <= self.broadcast_address):
raise ValueError('%s not contained in %s' % (other, self))
if other == self:
- raise StopIteration
+ return
# Make sure we're comparing the network of other.
other = other.__class__('%s/%s' % (other.network_address,
@@ -900,20 +911,11 @@ class _BaseNetwork(_IPAddressBase):
'prefix length diff %d is invalid for netblock %s' % (
new_prefixlen, self))
- first = self.__class__('%s/%s' %
- (self.network_address,
- self._prefixlen + prefixlen_diff))
-
- yield first
- current = first
- while True:
- broadcast = current.broadcast_address
- if broadcast == self.broadcast_address:
- return
- new_addr = self._address_class(int(broadcast) + 1)
- current = self.__class__('%s/%s' % (new_addr,
- new_prefixlen))
-
+ start = int(self.network_address)
+ end = int(self.broadcast_address)
+ step = (int(self.hostmask) + 1) >> prefixlen_diff
+ for new_addr in range(start, end, step):
+ current = self.__class__((new_addr, new_prefixlen))
yield current
def supernet(self, prefixlen_diff=1, new_prefix=None):
@@ -947,15 +949,15 @@ class _BaseNetwork(_IPAddressBase):
raise ValueError('cannot set prefixlen_diff and new_prefix')
prefixlen_diff = self._prefixlen - new_prefix
- if self.prefixlen - prefixlen_diff < 0:
+ new_prefixlen = self.prefixlen - prefixlen_diff
+ if new_prefixlen < 0:
raise ValueError(
'current prefixlen is %d, cannot have a prefixlen_diff of %d' %
(self.prefixlen, prefixlen_diff))
- # TODO (pmoody): optimize this.
- t = self.__class__('%s/%d' % (self.network_address,
- self.prefixlen - prefixlen_diff),
- strict=False)
- return t.__class__('%s/%d' % (t.network_address, t.prefixlen))
+ return self.__class__((
+ int(self.network_address) & (int(self.netmask) << prefixlen_diff),
+ new_prefixlen
+ ))
@property
def is_multicast(self):
@@ -1054,16 +1056,45 @@ class _BaseV4:
_DECIMAL_DIGITS = frozenset('0123456789')
# the valid octets for host and netmasks. only useful for IPv4.
- _valid_mask_octets = frozenset((255, 254, 252, 248, 240, 224, 192, 128, 0))
+ _valid_mask_octets = frozenset({255, 254, 252, 248, 240, 224, 192, 128, 0})
+
+ _max_prefixlen = IPV4LENGTH
+ # There are only a handful of valid v4 netmasks, so we cache them all
+ # when constructed (see _make_netmask()).
+ _netmask_cache = {}
def __init__(self, address):
self._version = 4
- self._max_prefixlen = IPV4LENGTH
def _explode_shorthand_ip_string(self):
return str(self)
- def _ip_int_from_string(self, ip_str):
+ @classmethod
+ def _make_netmask(cls, arg):
+ """Make a (netmask, prefix_len) tuple from the given argument.
+
+ Argument can be:
+ - an integer (the prefix length)
+ - a string representing the prefix length (e.g. "24")
+ - a string representing the prefix netmask (e.g. "255.255.255.0")
+ """
+ if arg not in cls._netmask_cache:
+ if isinstance(arg, int):
+ prefixlen = arg
+ else:
+ try:
+ # Check for a netmask in prefix length form
+ prefixlen = cls._prefix_from_prefix_string(arg)
+ except NetmaskValueError:
+ # Check for a netmask or hostmask in dotted-quad form.
+ # This may raise NetmaskValueError.
+ prefixlen = cls._prefix_from_ip_string(arg)
+ netmask = IPv4Address(cls._ip_int_from_prefix(prefixlen))
+ cls._netmask_cache[arg] = netmask, prefixlen
+ return cls._netmask_cache[arg]
+
+ @classmethod
+ def _ip_int_from_string(cls, ip_str):
"""Turn the given IP string into an integer for comparison.
Args:
@@ -1084,11 +1115,12 @@ class _BaseV4:
raise AddressValueError("Expected 4 octets in %r" % ip_str)
try:
- return int.from_bytes(map(self._parse_octet, octets), 'big')
+ return int.from_bytes(map(cls._parse_octet, octets), 'big')
except ValueError as exc:
raise AddressValueError("%s in %r" % (exc, ip_str)) from None
- def _parse_octet(self, octet_str):
+ @classmethod
+ def _parse_octet(cls, octet_str):
"""Convert a decimal octet into an integer.
Args:
@@ -1104,7 +1136,7 @@ class _BaseV4:
if not octet_str:
raise ValueError("Empty octet not permitted")
# Whitelist the characters, since int() allows a lot of bizarre stuff.
- if not self._DECIMAL_DIGITS.issuperset(octet_str):
+ if not cls._DECIMAL_DIGITS.issuperset(octet_str):
msg = "Only decimal digits permitted in %r"
raise ValueError(msg % octet_str)
# We do the length check second, since the invalid character error
@@ -1124,7 +1156,8 @@ class _BaseV4:
raise ValueError("Octet %d (> 255) not permitted" % octet_int)
return octet_int
- def _string_from_ip_int(self, ip_int):
+ @classmethod
+ def _string_from_ip_int(cls, ip_int):
"""Turns a 32-bit integer into dotted decimal notation.
Args:
@@ -1188,6 +1221,15 @@ class _BaseV4:
return True
return False
+ def _reverse_pointer(self):
+ """Return the reverse DNS pointer name for the IPv4 address.
+
+ This implements the method described in RFC1035 3.5.
+
+ """
+ reverse_octets = str(self).split('.')[::-1]
+ return '.'.join(reverse_octets) + '.in-addr.arpa'
+
@property
def max_prefixlen(self):
return self._max_prefixlen
@@ -1217,7 +1259,6 @@ class IPv4Address(_BaseV4, _BaseAddress):
AddressValueError: If ipaddress isn't a valid IPv4 address.
"""
- _BaseAddress.__init__(self, address)
_BaseV4.__init__(self, address)
# Efficient constructor from integer.
@@ -1235,6 +1276,8 @@ class IPv4Address(_BaseV4, _BaseAddress):
# Assume input argument to be string or any object representation
# which converts into a formatted IP string.
addr_str = str(address)
+ if '/' in addr_str:
+ raise AddressValueError("Unexpected '/' in %r" % address)
self._ip = self._ip_int_from_string(addr_str)
@property
@@ -1251,8 +1294,7 @@ class IPv4Address(_BaseV4, _BaseAddress):
reserved IPv4 Network range.
"""
- reserved_network = IPv4Network('240.0.0.0/4')
- return self in reserved_network
+ return self in self._constants._reserved_network
@property
@functools.lru_cache()
@@ -1264,21 +1306,7 @@ class IPv4Address(_BaseV4, _BaseAddress):
iana-ipv4-special-registry.
"""
- return (self in IPv4Network('0.0.0.0/8') or
- self in IPv4Network('10.0.0.0/8') or
- self in IPv4Network('127.0.0.0/8') or
- self in IPv4Network('169.254.0.0/16') or
- self in IPv4Network('172.16.0.0/12') or
- self in IPv4Network('192.0.0.0/29') or
- self in IPv4Network('192.0.0.170/31') or
- self in IPv4Network('192.0.2.0/24') or
- self in IPv4Network('192.168.0.0/16') or
- self in IPv4Network('198.18.0.0/15') or
- self in IPv4Network('198.51.100.0/24') or
- self in IPv4Network('203.0.113.0/24') or
- self in IPv4Network('240.0.0.0/4') or
- self in IPv4Network('255.255.255.255/32'))
-
+ return any(self in net for net in self._constants._private_networks)
@property
def is_multicast(self):
@@ -1289,8 +1317,7 @@ class IPv4Address(_BaseV4, _BaseAddress):
See RFC 3171 for details.
"""
- multicast_network = IPv4Network('224.0.0.0/4')
- return self in multicast_network
+ return self in self._constants._multicast_network
@property
def is_unspecified(self):
@@ -1301,8 +1328,7 @@ class IPv4Address(_BaseV4, _BaseAddress):
RFC 5735 3.
"""
- unspecified_address = IPv4Address('0.0.0.0')
- return self == unspecified_address
+ return self == self._constants._unspecified_address
@property
def is_loopback(self):
@@ -1312,8 +1338,7 @@ class IPv4Address(_BaseV4, _BaseAddress):
A boolean, True if the address is a loopback per RFC 3330.
"""
- loopback_network = IPv4Network('127.0.0.0/8')
- return self in loopback_network
+ return self in self._constants._loopback_network
@property
def is_link_local(self):
@@ -1323,8 +1348,7 @@ class IPv4Address(_BaseV4, _BaseAddress):
A boolean, True if the address is link-local per RFC 3927.
"""
- linklocal_network = IPv4Network('169.254.0.0/16')
- return self in linklocal_network
+ return self in self._constants._linklocal_network
class IPv4Interface(IPv4Address):
@@ -1336,6 +1360,18 @@ class IPv4Interface(IPv4Address):
self._prefixlen = self._max_prefixlen
return
+ if isinstance(address, tuple):
+ IPv4Address.__init__(self, address[0])
+ if len(address) > 1:
+ self._prefixlen = int(address[1])
+ else:
+ self._prefixlen = self._max_prefixlen
+
+ self.network = IPv4Network(address, strict=False)
+ self.netmask = self.network.netmask
+ self.hostmask = self.network.hostmask
+ return
+
addr = _split_optional_netmask(address)
IPv4Address.__init__(self, addr[0])
@@ -1375,6 +1411,8 @@ class IPv4Interface(IPv4Address):
def __hash__(self):
return self._ip ^ self._prefixlen ^ int(self.network.network_address)
+ __reduce__ = _IPAddressBase.__reduce__
+
@property
def ip(self):
return IPv4Address(self._ip)
@@ -1451,20 +1489,28 @@ class IPv4Network(_BaseV4, _BaseNetwork):
_BaseV4.__init__(self, address)
_BaseNetwork.__init__(self, address)
- # Constructing from a packed address
- if isinstance(address, bytes):
+ # Constructing from a packed address or integer
+ if isinstance(address, (int, bytes)):
self.network_address = IPv4Address(address)
- self._prefixlen = self._max_prefixlen
- self.netmask = IPv4Address(self._ALL_ONES)
- #fixme: address/network test here
+ self.netmask, self._prefixlen = self._make_netmask(self._max_prefixlen)
+ #fixme: address/network test here.
return
- # Efficient constructor from integer.
- if isinstance(address, int):
- self.network_address = IPv4Address(address)
- self._prefixlen = self._max_prefixlen
- self.netmask = IPv4Address(self._ALL_ONES)
- #fixme: address/network test here.
+ if isinstance(address, tuple):
+ if len(address) > 1:
+ arg = address[1]
+ else:
+ # We weren't given an address[1]
+ arg = self._max_prefixlen
+ self.network_address = IPv4Address(address[0])
+ self.netmask, self._prefixlen = self._make_netmask(arg)
+ packed = int(self.network_address)
+ if packed & int(self.netmask) != packed:
+ if strict:
+ raise ValueError('%s has host bits set' % self)
+ else:
+ self.network_address = IPv4Address(packed &
+ int(self.netmask))
return
# Assume input argument to be string or any object representation
@@ -1473,16 +1519,10 @@ class IPv4Network(_BaseV4, _BaseNetwork):
self.network_address = IPv4Address(self._ip_int_from_string(addr[0]))
if len(addr) == 2:
- try:
- # Check for a netmask in prefix length form
- self._prefixlen = self._prefix_from_prefix_string(addr[1])
- except NetmaskValueError:
- # Check for a netmask or hostmask in dotted-quad form.
- # This may raise NetmaskValueError.
- self._prefixlen = self._prefix_from_ip_string(addr[1])
+ arg = addr[1]
else:
- self._prefixlen = self._max_prefixlen
- self.netmask = IPv4Address(self._ip_int_from_prefix(self._prefixlen))
+ arg = self._max_prefixlen
+ self.netmask, self._prefixlen = self._make_netmask(arg)
if strict:
if (IPv4Address(int(self.network_address) & int(self.netmask)) !=
@@ -1509,6 +1549,37 @@ class IPv4Network(_BaseV4, _BaseNetwork):
not self.is_private)
+class _IPv4Constants:
+ _linklocal_network = IPv4Network('169.254.0.0/16')
+
+ _loopback_network = IPv4Network('127.0.0.0/8')
+
+ _multicast_network = IPv4Network('224.0.0.0/4')
+
+ _private_networks = [
+ IPv4Network('0.0.0.0/8'),
+ IPv4Network('10.0.0.0/8'),
+ IPv4Network('127.0.0.0/8'),
+ IPv4Network('169.254.0.0/16'),
+ IPv4Network('172.16.0.0/12'),
+ IPv4Network('192.0.0.0/29'),
+ IPv4Network('192.0.0.170/31'),
+ IPv4Network('192.0.2.0/24'),
+ IPv4Network('192.168.0.0/16'),
+ IPv4Network('198.18.0.0/15'),
+ IPv4Network('198.51.100.0/24'),
+ IPv4Network('203.0.113.0/24'),
+ IPv4Network('240.0.0.0/4'),
+ IPv4Network('255.255.255.255/32'),
+ ]
+
+ _reserved_network = IPv4Network('240.0.0.0/4')
+
+ _unspecified_address = IPv4Address('0.0.0.0')
+
+
+IPv4Address._constants = _IPv4Constants
+
class _BaseV6:
@@ -1522,12 +1593,35 @@ class _BaseV6:
_ALL_ONES = (2**IPV6LENGTH) - 1
_HEXTET_COUNT = 8
_HEX_DIGITS = frozenset('0123456789ABCDEFabcdef')
+ _max_prefixlen = IPV6LENGTH
+
+ # There are only a bunch of valid v6 netmasks, so we cache them all
+ # when constructed (see _make_netmask()).
+ _netmask_cache = {}
def __init__(self, address):
self._version = 6
- self._max_prefixlen = IPV6LENGTH
- def _ip_int_from_string(self, ip_str):
+ @classmethod
+ def _make_netmask(cls, arg):
+ """Make a (netmask, prefix_len) tuple from the given argument.
+
+ Argument can be:
+ - an integer (the prefix length)
+ - a string representing the prefix length (e.g. "24")
+ - a string representing the prefix netmask (e.g. "255.255.255.0")
+ """
+ if arg not in cls._netmask_cache:
+ if isinstance(arg, int):
+ prefixlen = arg
+ else:
+ prefixlen = cls._prefix_from_prefix_string(arg)
+ netmask = IPv6Address(cls._ip_int_from_prefix(prefixlen))
+ cls._netmask_cache[arg] = netmask, prefixlen
+ return cls._netmask_cache[arg]
+
+ @classmethod
+ def _ip_int_from_string(cls, ip_str):
"""Turn an IPv6 ip_str into an integer.
Args:
@@ -1563,7 +1657,7 @@ class _BaseV6:
# An IPv6 address can't have more than 8 colons (9 parts).
# The extra colon comes from using the "::" notation for a single
# leading or trailing zero part.
- _max_parts = self._HEXTET_COUNT + 1
+ _max_parts = cls._HEXTET_COUNT + 1
if len(parts) > _max_parts:
msg = "At most %d colons permitted in %r" % (_max_parts-1, ip_str)
raise AddressValueError(msg)
@@ -1595,17 +1689,17 @@ class _BaseV6:
if parts_lo:
msg = "Trailing ':' only permitted as part of '::' in %r"
raise AddressValueError(msg % ip_str) # :$ requires ::$
- parts_skipped = self._HEXTET_COUNT - (parts_hi + parts_lo)
+ parts_skipped = cls._HEXTET_COUNT - (parts_hi + parts_lo)
if parts_skipped < 1:
msg = "Expected at most %d other parts with '::' in %r"
- raise AddressValueError(msg % (self._HEXTET_COUNT-1, ip_str))
+ raise AddressValueError(msg % (cls._HEXTET_COUNT-1, ip_str))
else:
# Otherwise, allocate the entire address to parts_hi. The
# endpoints could still be empty, but _parse_hextet() will check
# for that.
- if len(parts) != self._HEXTET_COUNT:
+ if len(parts) != cls._HEXTET_COUNT:
msg = "Exactly %d parts expected without '::' in %r"
- raise AddressValueError(msg % (self._HEXTET_COUNT, ip_str))
+ raise AddressValueError(msg % (cls._HEXTET_COUNT, ip_str))
if not parts[0]:
msg = "Leading ':' only permitted as part of '::' in %r"
raise AddressValueError(msg % ip_str) # ^: requires ^::
@@ -1621,16 +1715,17 @@ class _BaseV6:
ip_int = 0
for i in range(parts_hi):
ip_int <<= 16
- ip_int |= self._parse_hextet(parts[i])
+ ip_int |= cls._parse_hextet(parts[i])
ip_int <<= 16 * parts_skipped
for i in range(-parts_lo, 0):
ip_int <<= 16
- ip_int |= self._parse_hextet(parts[i])
+ ip_int |= cls._parse_hextet(parts[i])
return ip_int
except ValueError as exc:
raise AddressValueError("%s in %r" % (exc, ip_str)) from None
- def _parse_hextet(self, hextet_str):
+ @classmethod
+ def _parse_hextet(cls, hextet_str):
"""Convert an IPv6 hextet string into an integer.
Args:
@@ -1645,7 +1740,7 @@ class _BaseV6:
"""
# Whitelist the characters, since int() allows a lot of bizarre stuff.
- if not self._HEX_DIGITS.issuperset(hextet_str):
+ if not cls._HEX_DIGITS.issuperset(hextet_str):
raise ValueError("Only hex digits permitted in %r" % hextet_str)
# We do the length check second, since the invalid character error
# is likely to be more informative for the user
@@ -1655,7 +1750,8 @@ class _BaseV6:
# Length check means we can skip checking the integer value
return int(hextet_str, 16)
- def _compress_hextets(self, hextets):
+ @classmethod
+ def _compress_hextets(cls, hextets):
"""Compresses a list of hextets.
Compresses a list of strings, replacing the longest continuous
@@ -1702,7 +1798,8 @@ class _BaseV6:
return hextets
- def _string_from_ip_int(self, ip_int=None):
+ @classmethod
+ def _string_from_ip_int(cls, ip_int=None):
"""Turns a 128-bit integer into hexadecimal notation.
Args:
@@ -1716,15 +1813,15 @@ class _BaseV6:
"""
if ip_int is None:
- ip_int = int(self._ip)
+ ip_int = int(cls._ip)
- if ip_int > self._ALL_ONES:
+ if ip_int > cls._ALL_ONES:
raise ValueError('IPv6 address is too large')
hex_str = '%032x' % ip_int
hextets = ['%x' % int(hex_str[x:x+4], 16) for x in range(0, 32, 4)]
- hextets = self._compress_hextets(hextets)
+ hextets = cls._compress_hextets(hextets)
return ':'.join(hextets)
def _explode_shorthand_ip_string(self):
@@ -1751,6 +1848,15 @@ class _BaseV6:
return '%s/%d' % (':'.join(parts), self._prefixlen)
return ':'.join(parts)
+ def _reverse_pointer(self):
+ """Return the reverse DNS pointer name for the IPv6 address.
+
+ This implements the method described in RFC3596 2.5.
+
+ """
+ reverse_chars = self.exploded[::-1].replace(':', '')
+ return '.'.join(reverse_chars) + '.ip6.arpa'
+
@property
def max_prefixlen(self):
return self._max_prefixlen
@@ -1781,7 +1887,6 @@ class IPv6Address(_BaseV6, _BaseAddress):
AddressValueError: If address isn't a valid IPv6 address.
"""
- _BaseAddress.__init__(self, address)
_BaseV6.__init__(self, address)
# Efficient constructor from integer.
@@ -1799,6 +1904,8 @@ class IPv6Address(_BaseV6, _BaseAddress):
# Assume input argument to be string or any object representation
# which converts into a formatted IP string.
addr_str = str(address)
+ if '/' in addr_str:
+ raise AddressValueError("Unexpected '/' in %r" % address)
self._ip = self._ip_int_from_string(addr_str)
@property
@@ -1815,8 +1922,7 @@ class IPv6Address(_BaseV6, _BaseAddress):
See RFC 2373 2.7 for details.
"""
- multicast_network = IPv6Network('ff00::/8')
- return self in multicast_network
+ return self in self._constants._multicast_network
@property
def is_reserved(self):
@@ -1827,16 +1933,7 @@ class IPv6Address(_BaseV6, _BaseAddress):
reserved IPv6 Network ranges.
"""
- reserved_networks = [IPv6Network('::/8'), IPv6Network('100::/8'),
- IPv6Network('200::/7'), IPv6Network('400::/6'),
- IPv6Network('800::/5'), IPv6Network('1000::/4'),
- IPv6Network('4000::/3'), IPv6Network('6000::/3'),
- IPv6Network('8000::/3'), IPv6Network('A000::/3'),
- IPv6Network('C000::/3'), IPv6Network('E000::/4'),
- IPv6Network('F000::/5'), IPv6Network('F800::/6'),
- IPv6Network('FE00::/9')]
-
- return any(self in x for x in reserved_networks)
+ return any(self in x for x in self._constants._reserved_networks)
@property
def is_link_local(self):
@@ -1846,8 +1943,7 @@ class IPv6Address(_BaseV6, _BaseAddress):
A boolean, True if the address is reserved per RFC 4291.
"""
- linklocal_network = IPv6Network('fe80::/10')
- return self in linklocal_network
+ return self in self._constants._linklocal_network
@property
def is_site_local(self):
@@ -1861,8 +1957,7 @@ class IPv6Address(_BaseV6, _BaseAddress):
A boolean, True if the address is reserved per RFC 3513 2.5.6.
"""
- sitelocal_network = IPv6Network('fec0::/10')
- return self in sitelocal_network
+ return self in self._constants._sitelocal_network
@property
@functools.lru_cache()
@@ -1874,16 +1969,7 @@ class IPv6Address(_BaseV6, _BaseAddress):
iana-ipv6-special-registry.
"""
- return (self in IPv6Network('::1/128') or
- self in IPv6Network('::/128') or
- self in IPv6Network('::ffff:0:0/96') or
- self in IPv6Network('100::/64') or
- self in IPv6Network('2001::/23') or
- self in IPv6Network('2001:2::/48') or
- self in IPv6Network('2001:db8::/32') or
- self in IPv6Network('2001:10::/28') or
- self in IPv6Network('fc00::/7') or
- self in IPv6Network('fe80::/10'))
+ return any(self in net for net in self._constants._private_networks)
@property
def is_global(self):
@@ -1968,6 +2054,16 @@ class IPv6Interface(IPv6Address):
self.network = IPv6Network(self._ip)
self._prefixlen = self._max_prefixlen
return
+ if isinstance(address, tuple):
+ IPv6Address.__init__(self, address[0])
+ if len(address) > 1:
+ self._prefixlen = int(address[1])
+ else:
+ self._prefixlen = self._max_prefixlen
+ self.network = IPv6Network(address, strict=False)
+ self.netmask = self.network.netmask
+ self.hostmask = self.network.hostmask
+ return
addr = _split_optional_netmask(address)
IPv6Address.__init__(self, addr[0])
@@ -2006,6 +2102,8 @@ class IPv6Interface(IPv6Address):
def __hash__(self):
return self._ip ^ self._prefixlen ^ int(self.network.network_address)
+ __reduce__ = _IPAddressBase.__reduce__
+
@property
def ip(self):
return IPv6Address(self._ip)
@@ -2085,18 +2183,26 @@ class IPv6Network(_BaseV6, _BaseNetwork):
_BaseV6.__init__(self, address)
_BaseNetwork.__init__(self, address)
- # Efficient constructor from integer.
- if isinstance(address, int):
+ # Efficient constructor from integer or packed address
+ if isinstance(address, (bytes, int)):
self.network_address = IPv6Address(address)
- self._prefixlen = self._max_prefixlen
- self.netmask = IPv6Address(self._ALL_ONES)
+ self.netmask, self._prefixlen = self._make_netmask(self._max_prefixlen)
return
- # Constructing from a packed address
- if isinstance(address, bytes):
- self.network_address = IPv6Address(address)
- self._prefixlen = self._max_prefixlen
- self.netmask = IPv6Address(self._ALL_ONES)
+ if isinstance(address, tuple):
+ if len(address) > 1:
+ arg = address[1]
+ else:
+ arg = self._max_prefixlen
+ self.netmask, self._prefixlen = self._make_netmask(arg)
+ self.network_address = IPv6Address(address[0])
+ packed = int(self.network_address)
+ if packed & int(self.netmask) != packed:
+ if strict:
+ raise ValueError('%s has host bits set' % self)
+ else:
+ self.network_address = IPv6Address(packed &
+ int(self.netmask))
return
# Assume input argument to be string or any object representation
@@ -2106,12 +2212,11 @@ class IPv6Network(_BaseV6, _BaseNetwork):
self.network_address = IPv6Address(self._ip_int_from_string(addr[0]))
if len(addr) == 2:
- # This may raise NetmaskValueError
- self._prefixlen = self._prefix_from_prefix_string(addr[1])
+ arg = addr[1]
else:
- self._prefixlen = self._max_prefixlen
+ arg = self._max_prefixlen
+ self.netmask, self._prefixlen = self._make_netmask(arg)
- self.netmask = IPv6Address(self._ip_int_from_prefix(self._prefixlen))
if strict:
if (IPv6Address(int(self.network_address) & int(self.netmask)) !=
self.network_address):
@@ -2148,3 +2253,39 @@ class IPv6Network(_BaseV6, _BaseNetwork):
"""
return (self.network_address.is_site_local and
self.broadcast_address.is_site_local)
+
+
+class _IPv6Constants:
+
+ _linklocal_network = IPv6Network('fe80::/10')
+
+ _multicast_network = IPv6Network('ff00::/8')
+
+ _private_networks = [
+ IPv6Network('::1/128'),
+ IPv6Network('::/128'),
+ IPv6Network('::ffff:0:0/96'),
+ IPv6Network('100::/64'),
+ IPv6Network('2001::/23'),
+ IPv6Network('2001:2::/48'),
+ IPv6Network('2001:db8::/32'),
+ IPv6Network('2001:10::/28'),
+ IPv6Network('fc00::/7'),
+ IPv6Network('fe80::/10'),
+ ]
+
+ _reserved_networks = [
+ IPv6Network('::/8'), IPv6Network('100::/8'),
+ IPv6Network('200::/7'), IPv6Network('400::/6'),
+ IPv6Network('800::/5'), IPv6Network('1000::/4'),
+ IPv6Network('4000::/3'), IPv6Network('6000::/3'),
+ IPv6Network('8000::/3'), IPv6Network('A000::/3'),
+ IPv6Network('C000::/3'), IPv6Network('E000::/4'),
+ IPv6Network('F000::/5'), IPv6Network('F800::/6'),
+ IPv6Network('FE00::/9'),
+ ]
+
+ _sitelocal_network = IPv6Network('fec0::/10')
+
+
+IPv6Address._constants = _IPv6Constants
diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py
index 94f7d8c..6ce9880 100644
--- a/Lib/json/__init__.py
+++ b/Lib/json/__init__.py
@@ -98,12 +98,12 @@ Using json.tool from the shell to validate and pretty-print::
__version__ = '2.0.9'
__all__ = [
'dump', 'dumps', 'load', 'loads',
- 'JSONDecoder', 'JSONEncoder',
+ 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
]
__author__ = 'Bob Ippolito <bob@redivi.com>'
-from .decoder import JSONDecoder
+from .decoder import JSONDecoder, JSONDecodeError
from .encoder import JSONEncoder
_default_encoder = JSONEncoder(
@@ -311,7 +311,8 @@ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
raise TypeError('the JSON object must be str, not {!r}'.format(
s.__class__.__name__))
if s.startswith(u'\ufeff'):
- raise ValueError("Unexpected UTF-8 BOM (decode using utf-8-sig)")
+ raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
+ s, 0)
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 59e5f41..0f03f20 100644
--- a/Lib/json/decoder.py
+++ b/Lib/json/decoder.py
@@ -8,7 +8,7 @@ try:
except ImportError:
c_scanstring = None
-__all__ = ['JSONDecoder']
+__all__ = ['JSONDecoder', 'JSONDecodeError']
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
@@ -17,32 +17,30 @@ PosInf = float('inf')
NegInf = float('-inf')
-def linecol(doc, pos):
- if isinstance(doc, bytes):
- newline = b'\n'
- else:
- newline = '\n'
- lineno = doc.count(newline, 0, pos) + 1
- if lineno == 1:
- colno = pos + 1
- else:
- colno = pos - doc.rindex(newline, 0, pos)
- return lineno, colno
-
-
-def errmsg(msg, doc, pos, end=None):
- # Note that this function is called from _json
- lineno, colno = linecol(doc, pos)
- if end is None:
- fmt = '{0}: line {1} column {2} (char {3})'
- return fmt.format(msg, lineno, colno, pos)
- #fmt = '%s: line %d column %d (char %d)'
- #return fmt % (msg, lineno, colno, pos)
- endlineno, endcolno = linecol(doc, end)
- fmt = '{0}: line {1} column {2} - line {3} column {4} (char {5} - {6})'
- return fmt.format(msg, lineno, colno, endlineno, endcolno, pos, end)
- #fmt = '%s: line %d column %d - line %d column %d (char %d - %d)'
- #return fmt % (msg, lineno, colno, endlineno, endcolno, pos, end)
+class JSONDecodeError(ValueError):
+ """Subclass of ValueError with the following additional properties:
+
+ msg: The unformatted error message
+ doc: The JSON document being parsed
+ pos: The start index of doc where parsing failed
+ lineno: The line corresponding to pos
+ colno: The column corresponding to pos
+
+ """
+ # Note that this exception is used from _json
+ def __init__(self, msg, doc, pos):
+ lineno = doc.count('\n', 0, pos) + 1
+ colno = pos - doc.rfind('\n', 0, pos)
+ errmsg = '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos)
+ ValueError.__init__(self, errmsg)
+ self.msg = msg
+ self.doc = doc
+ self.pos = pos
+ self.lineno = lineno
+ self.colno = colno
+
+ def __reduce__(self):
+ return self.__class__, (self.msg, self.doc, self.pos)
_CONSTANTS = {
@@ -66,7 +64,7 @@ def _decode_uXXXX(s, pos):
except ValueError:
pass
msg = "Invalid \\uXXXX escape"
- raise ValueError(errmsg(msg, s, pos))
+ raise JSONDecodeError(msg, s, pos)
def py_scanstring(s, end, strict=True,
_b=BACKSLASH, _m=STRINGCHUNK.match):
@@ -84,8 +82,7 @@ def py_scanstring(s, end, strict=True,
while 1:
chunk = _m(s, end)
if chunk is None:
- raise ValueError(
- errmsg("Unterminated string starting at", s, begin))
+ raise JSONDecodeError("Unterminated string starting at", s, begin)
end = chunk.end()
content, terminator = chunk.groups()
# Content is contains zero or more unescaped string characters
@@ -99,22 +96,21 @@ def py_scanstring(s, end, strict=True,
if strict:
#msg = "Invalid control character %r at" % (terminator,)
msg = "Invalid control character {0!r} at".format(terminator)
- raise ValueError(errmsg(msg, s, end))
+ raise JSONDecodeError(msg, s, end)
else:
_append(terminator)
continue
try:
esc = s[end]
except IndexError:
- raise ValueError(
- errmsg("Unterminated string starting at", s, begin))
+ raise JSONDecodeError("Unterminated string starting at", s, begin)
# If not a unicode escape sequence, must be in the lookup table
if esc != 'u':
try:
char = _b[esc]
except KeyError:
msg = "Invalid \\escape: {0!r}".format(esc)
- raise ValueError(errmsg(msg, s, end))
+ raise JSONDecodeError(msg, s, end)
end += 1
else:
uni = _decode_uXXXX(s, end)
@@ -163,8 +159,8 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
pairs = object_hook(pairs)
return pairs, end + 1
elif nextchar != '"':
- raise ValueError(errmsg(
- "Expecting property name enclosed in double quotes", s, end))
+ raise JSONDecodeError(
+ "Expecting property name enclosed in double quotes", s, end)
end += 1
while True:
key, end = scanstring(s, end, strict)
@@ -174,7 +170,7 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
if s[end:end + 1] != ':':
end = _w(s, end).end()
if s[end:end + 1] != ':':
- raise ValueError(errmsg("Expecting ':' delimiter", s, end))
+ raise JSONDecodeError("Expecting ':' delimiter", s, end)
end += 1
try:
@@ -188,7 +184,7 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
try:
value, end = scan_once(s, end)
except StopIteration as err:
- raise ValueError(errmsg("Expecting value", s, err.value)) from None
+ raise JSONDecodeError("Expecting value", s, err.value) from None
pairs_append((key, value))
try:
nextchar = s[end]
@@ -202,13 +198,13 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
if nextchar == '}':
break
elif nextchar != ',':
- raise ValueError(errmsg("Expecting ',' delimiter", s, end - 1))
+ raise JSONDecodeError("Expecting ',' delimiter", s, end - 1)
end = _w(s, end).end()
nextchar = s[end:end + 1]
end += 1
if nextchar != '"':
- raise ValueError(errmsg(
- "Expecting property name enclosed in double quotes", s, end - 1))
+ raise JSONDecodeError(
+ "Expecting property name enclosed in double quotes", s, end - 1)
if object_pairs_hook is not None:
result = object_pairs_hook(pairs)
return result, end
@@ -232,7 +228,7 @@ def JSONArray(s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
try:
value, end = scan_once(s, end)
except StopIteration as err:
- raise ValueError(errmsg("Expecting value", s, err.value)) from None
+ raise JSONDecodeError("Expecting value", s, err.value) from None
_append(value)
nextchar = s[end:end + 1]
if nextchar in _ws:
@@ -242,7 +238,7 @@ def JSONArray(s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
if nextchar == ']':
break
elif nextchar != ',':
- raise ValueError(errmsg("Expecting ',' delimiter", s, end - 1))
+ raise JSONDecodeError("Expecting ',' delimiter", s, end - 1)
try:
if s[end] in _ws:
end += 1
@@ -343,7 +339,7 @@ class JSONDecoder(object):
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
end = _w(s, end).end()
if end != len(s):
- raise ValueError(errmsg("Extra data", s, end, len(s)))
+ raise JSONDecodeError("Extra data", s, end)
return obj
def raw_decode(self, s, idx=0):
@@ -358,5 +354,5 @@ class JSONDecoder(object):
try:
obj, end = self.scan_once(s, idx)
except StopIteration as err:
- raise ValueError(errmsg("Expecting value", s, err.value)) from None
+ raise JSONDecodeError("Expecting value", s, err.value) from None
return obj, end
diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py
index 0513838..26e9eb2 100644
--- a/Lib/json/encoder.py
+++ b/Lib/json/encoder.py
@@ -7,6 +7,10 @@ try:
except ImportError:
c_encode_basestring_ascii = None
try:
+ from _json import encode_basestring as c_encode_basestring
+except ImportError:
+ c_encode_basestring = None
+try:
from _json import make_encoder as c_make_encoder
except ImportError:
c_make_encoder = None
@@ -30,7 +34,7 @@ for i in range(0x20):
INFINITY = float('inf')
FLOAT_REPR = repr
-def encode_basestring(s):
+def py_encode_basestring(s):
"""Return a JSON representation of a Python string
"""
@@ -39,6 +43,9 @@ def encode_basestring(s):
return '"' + ESCAPE.sub(replace, s) + '"'
+encode_basestring = (c_encode_basestring or py_encode_basestring)
+
+
def py_encode_basestring_ascii(s):
"""Return an ASCII-only JSON representation of a Python string
diff --git a/Lib/json/tool.py b/Lib/json/tool.py
index 7db4528..4f3182c 100644
--- a/Lib/json/tool.py
+++ b/Lib/json/tool.py
@@ -10,28 +10,39 @@ Usage::
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
"""
-import sys
+import argparse
+import collections
import json
+import sys
+
def main():
- if len(sys.argv) == 1:
- infile = sys.stdin
- outfile = sys.stdout
- elif len(sys.argv) == 2:
- infile = open(sys.argv[1], 'r')
- outfile = sys.stdout
- elif len(sys.argv) == 3:
- infile = open(sys.argv[1], 'r')
- outfile = open(sys.argv[2], 'w')
- else:
- raise SystemExit(sys.argv[0] + " [infile [outfile]]")
+ prog = 'python -m json.tool'
+ description = ('A simple command line interface for json module '
+ 'to validate and pretty-print JSON objects.')
+ parser = argparse.ArgumentParser(prog=prog, description=description)
+ parser.add_argument('infile', nargs='?', type=argparse.FileType(),
+ help='a JSON file to be validated or pretty-printed')
+ parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
+ help='write the output of infile to outfile')
+ parser.add_argument('--sort-keys', action='store_true', default=False,
+ help='sort the output of dictionaries alphabetically by key')
+ options = parser.parse_args()
+
+ infile = options.infile or sys.stdin
+ outfile = options.outfile or sys.stdout
+ sort_keys = options.sort_keys
with infile:
try:
- obj = json.load(infile)
+ if sort_keys:
+ obj = json.load(infile)
+ else:
+ obj = json.load(infile,
+ object_pairs_hook=collections.OrderedDict)
except ValueError as e:
raise SystemExit(e)
with outfile:
- json.dump(obj, outfile, sort_keys=True, indent=4)
+ json.dump(obj, outfile, sort_keys=sort_keys, indent=4)
outfile.write('\n')
diff --git a/Lib/locale.py b/Lib/locale.py
index 7ff4356..6b9eb3a 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -301,8 +301,8 @@ def str(val):
"""Convert float to integer, taking the locale into account."""
return format("%.12g", val)
-def atof(string, func=float):
- "Parses a string as a float according to the locale settings."
+def delocalize(string):
+ "Parses a string as a normalized number according to the locale settings."
#First, get rid of the grouping
ts = localeconv()['thousands_sep']
if ts:
@@ -311,12 +311,15 @@ def atof(string, func=float):
dd = localeconv()['decimal_point']
if dd:
string = string.replace(dd, '.')
- #finally, parse the string
- return func(string)
+ return string
+
+def atof(string, func=float):
+ "Parses a string as a float according to the locale settings."
+ return func(delocalize(string))
-def atoi(str):
+def atoi(string):
"Converts a string to an integer according to the locale settings."
- return atof(str, int)
+ return int(delocalize(string))
def _test():
setlocale(LC_ALL, "")
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 9a8685b..b1dc319 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -1,4 +1,4 @@
-# Copyright 2001-2014 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2015 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,
@@ -18,7 +18,7 @@
Logging package for Python. Based on PEP 282 and comments thereto in
comp.lang.python.
-Copyright (C) 2001-2014 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2015 Vinay Sajip. All Rights Reserved.
To use, simply 'import logging' and log away!
"""
@@ -316,6 +316,8 @@ class LogRecord(object):
return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
self.pathname, self.lineno, self.msg)
+ __repr__ = __str__
+
def getMessage(self):
"""
Return the message for this LogRecord.
@@ -1086,7 +1088,6 @@ class PlaceHolder(object):
#
# Determine which class to use when instantiating loggers.
#
-_loggerClass = None
def setLoggerClass(klass):
"""
@@ -1105,7 +1106,6 @@ def getLoggerClass():
"""
Return the class to be used when instantiating a logger.
"""
-
return _loggerClass
class Manager(object):
@@ -1302,12 +1302,11 @@ class Logger(Filterer):
if self.isEnabledFor(ERROR):
self._log(ERROR, msg, args, **kwargs)
- def exception(self, msg, *args, **kwargs):
+ def exception(self, msg, *args, exc_info=True, **kwargs):
"""
Convenience method for logging an ERROR with exception information.
"""
- kwargs['exc_info'] = True
- self.error(msg, *args, **kwargs)
+ self.error(msg, *args, exc_info=exc_info, **kwargs)
def critical(self, msg, *args, **kwargs):
"""
@@ -1402,7 +1401,9 @@ class Logger(Filterer):
else: # pragma: no cover
fn, lno, func = "(unknown file)", 0, "(unknown function)"
if exc_info:
- if not isinstance(exc_info, tuple):
+ if isinstance(exc_info, BaseException):
+ exc_info = (type(exc_info), exc_info, exc_info.__traceback__)
+ elif not isinstance(exc_info, tuple):
exc_info = sys.exc_info()
record = self.makeRecord(self.name, level, fn, lno, msg, args,
exc_info, func, extra, sinfo)
@@ -1612,12 +1613,11 @@ class LoggerAdapter(object):
"""
self.log(ERROR, msg, *args, **kwargs)
- def exception(self, msg, *args, **kwargs):
+ def exception(self, msg, *args, exc_info=True, **kwargs):
"""
Delegate an exception call to the underlying logger.
"""
- kwargs["exc_info"] = True
- self.log(ERROR, msg, *args, **kwargs)
+ self.log(ERROR, msg, *args, exc_info=exc_info, **kwargs)
def critical(self, msg, *args, **kwargs):
"""
@@ -1799,14 +1799,13 @@ def error(msg, *args, **kwargs):
basicConfig()
root.error(msg, *args, **kwargs)
-def exception(msg, *args, **kwargs):
+def exception(msg, *args, exc_info=True, **kwargs):
"""
Log a message with severity 'ERROR' on the root logger, with exception
information. If the logger has no handlers, basicConfig() is called to add
a console handler with a pre-defined format.
"""
- kwargs['exc_info'] = True
- error(msg, *args, **kwargs)
+ error(msg, *args, exc_info=exc_info, **kwargs)
def warning(msg, *args, **kwargs):
"""
diff --git a/Lib/logging/config.py b/Lib/logging/config.py
index 895fb26..8a99923 100644
--- a/Lib/logging/config.py
+++ b/Lib/logging/config.py
@@ -116,11 +116,12 @@ def _create_formatters(cp):
sectname = "formatter_%s" % form
fs = cp.get(sectname, "format", raw=True, fallback=None)
dfs = cp.get(sectname, "datefmt", raw=True, fallback=None)
+ stl = cp.get(sectname, "style", raw=True, fallback='%')
c = logging.Formatter
class_name = cp[sectname].get("class")
if class_name:
c = _resolve(class_name)
- f = c(fs, dfs)
+ f = c(fs, dfs, stl)
formatters[form] = f
return formatters
@@ -660,7 +661,12 @@ class DictConfigurator(BaseConfigurator):
fmt = config.get('format', None)
dfmt = config.get('datefmt', None)
style = config.get('style', '%')
- result = logging.Formatter(fmt, dfmt, style)
+ cname = config.get('class', None)
+ if not cname:
+ c = logging.Formatter
+ else:
+ c = _resolve(cname)
+ result = c(fmt, dfmt, style)
return result
def configure_filter(self, config):
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
index c67ac99..d4f8aef 100644
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -1,4 +1,4 @@
-# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2015 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,
@@ -18,7 +18,7 @@
Additional handlers for the logging package for Python. The core package is
based on PEP 282 and comments thereto in comp.lang.python.
-Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2015 Vinay Sajip. All Rights Reserved.
To use, simply 'import logging.handlers' and log away!
"""
@@ -1350,7 +1350,7 @@ if threading:
"""
_sentinel = None
- def __init__(self, queue, *handlers):
+ def __init__(self, queue, *handlers, respect_handler_level=False):
"""
Initialise an instance with the specified queue and
handlers.
@@ -1359,6 +1359,7 @@ if threading:
self.handlers = handlers
self._stop = threading.Event()
self._thread = None
+ self.respect_handler_level = respect_handler_level
def dequeue(self, block):
"""
@@ -1399,7 +1400,12 @@ if threading:
"""
record = self.prepare(record)
for handler in self.handlers:
- handler.handle(record)
+ if not self.respect_handler_level:
+ process = True
+ else:
+ process = record.levelno >= handler.level
+ if process:
+ handler.handle(record)
def _monitor(self):
"""
diff --git a/Lib/macpath.py b/Lib/macpath.py
index 5ca0097..dbcf368 100644
--- a/Lib/macpath.py
+++ b/Lib/macpath.py
@@ -50,20 +50,24 @@ def isabs(s):
def join(s, *p):
- colon = _get_colon(s)
- path = s
- for t in p:
- if (not path) or isabs(t):
- path = t
- continue
- if t[:1] == colon:
- t = t[1:]
- if colon not in path:
- path = colon + path
- if path[-1:] != colon:
- path = path + colon
- path = path + t
- return path
+ try:
+ colon = _get_colon(s)
+ path = s
+ for t in p:
+ if (not path) or isabs(t):
+ path = t
+ continue
+ if t[:1] == colon:
+ t = t[1:]
+ if colon not in path:
+ path = colon + path
+ if path[-1:] != colon:
+ path = path + colon
+ path = path + t
+ return path
+ except (TypeError, AttributeError, BytesWarning):
+ genericpath._check_arg_types('join', s, *p)
+ raise
def split(s):
diff --git a/Lib/mailbox.py b/Lib/mailbox.py
index 2eee76c..e7f31df 100644
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -1230,8 +1230,8 @@ class MH(Mailbox):
class Babyl(_singlefileMailbox):
"""An Rmail-style Babyl mailbox."""
- _special_labels = frozenset(('unseen', 'deleted', 'filed', 'answered',
- 'forwarded', 'edited', 'resent'))
+ _special_labels = frozenset({'unseen', 'deleted', 'filed', 'answered',
+ 'forwarded', 'edited', 'resent'})
def __init__(self, path, factory=None, create=True):
"""Initialize a Babyl mailbox."""
@@ -1949,7 +1949,7 @@ class _ProxyFile:
while True:
line = self.readline()
if not line:
- raise StopIteration
+ return
yield line
def tell(self):
diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py
index 87117d9..9a8ae29 100644
--- a/Lib/multiprocessing/connection.py
+++ b/Lib/multiprocessing/connection.py
@@ -365,10 +365,7 @@ class Connection(_ConnectionBase):
def _send(self, buf, write=_write):
remaining = len(buf)
while True:
- try:
- n = write(self._handle, buf)
- except InterruptedError:
- continue
+ n = write(self._handle, buf)
remaining -= n
if remaining == 0:
break
@@ -379,10 +376,7 @@ class Connection(_ConnectionBase):
handle = self._handle
remaining = size
while remaining > 0:
- try:
- chunk = read(handle, remaining)
- except InterruptedError:
- continue
+ chunk = read(handle, remaining)
n = len(chunk)
if n == 0:
if remaining == size:
@@ -400,17 +394,14 @@ class Connection(_ConnectionBase):
if n > 16384:
# The payload is large so Nagle's algorithm won't be triggered
# and we'd better avoid the cost of concatenation.
- chunks = [header, buf]
- elif n > 0:
+ self._send(header)
+ self._send(buf)
+ else:
# Issue # 20540: concatenate before sending, to avoid delays due
# to Nagle's algorithm on a TCP socket.
- chunks = [header + buf]
- else:
- # This code path is necessary to avoid "broken pipe" errors
- # when sending a 0-length buffer if the other end closed the pipe.
- chunks = [header]
- for chunk in chunks:
- self._send(chunk)
+ # Also note we want to avoid sending a 0-length buffer separately,
+ # to avoid "broken pipe" errors if the other end closed the pipe.
+ self._send(header + buf)
def _recv_bytes(self, maxsize=None):
buf = self._recv(4)
@@ -598,13 +589,7 @@ class SocketListener(object):
self._unlink = None
def accept(self):
- while True:
- try:
- s, self._last_accepted = self._socket.accept()
- except InterruptedError:
- pass
- else:
- break
+ s, self._last_accepted = self._socket.accept()
s.setblocking(True)
return Connection(s.detach())
diff --git a/Lib/multiprocessing/dummy/__init__.py b/Lib/multiprocessing/dummy/__init__.py
index 135db7f..1abea64 100644
--- a/Lib/multiprocessing/dummy/__init__.py
+++ b/Lib/multiprocessing/dummy/__init__.py
@@ -86,7 +86,7 @@ class Namespace(object):
if not name.startswith('_'):
temp.append('%s=%r' % (name, value))
temp.sort()
- return 'Namespace(%s)' % str.join(', ', temp)
+ return '%s(%s)' % (self.__class__.__name__, ', '.join(temp))
dict = dict
list = list
diff --git a/Lib/multiprocessing/dummy/connection.py b/Lib/multiprocessing/dummy/connection.py
index 694ef96..1984375 100644
--- a/Lib/multiprocessing/dummy/connection.py
+++ b/Lib/multiprocessing/dummy/connection.py
@@ -59,9 +59,8 @@ class Connection(object):
return True
if timeout <= 0.0:
return False
- self._in.not_empty.acquire()
- self._in.not_empty.wait(timeout)
- self._in.not_empty.release()
+ with self._in.not_empty:
+ self._in.not_empty.wait(timeout)
return self._in.qsize() > 0
def close(self):
diff --git a/Lib/multiprocessing/forkserver.py b/Lib/multiprocessing/forkserver.py
index 387517e..b27cba5 100644
--- a/Lib/multiprocessing/forkserver.py
+++ b/Lib/multiprocessing/forkserver.py
@@ -107,7 +107,7 @@ class ForkServer(object):
address = connection.arbitrary_address('AF_UNIX')
listener.bind(address)
os.chmod(address, 0o600)
- listener.listen(100)
+ listener.listen()
# all client processes own the write end of the "alive" pipe;
# when they all terminate the read end becomes ready.
@@ -188,8 +188,6 @@ def main(listener_fd, alive_r, preload, main_path=None, sys_path=None):
finally:
os._exit(code)
- except InterruptedError:
- pass
except OSError as e:
if e.errno != errno.ECONNABORTED:
raise
@@ -230,13 +228,7 @@ def read_unsigned(fd):
data = b''
length = UNSIGNED_STRUCT.size
while len(data) < length:
- while True:
- try:
- s = os.read(fd, length - len(data))
- except InterruptedError:
- pass
- else:
- break
+ s = os.read(fd, length - len(data))
if not s:
raise EOFError('unexpected EOF')
data += s
@@ -245,13 +237,7 @@ def read_unsigned(fd):
def write_unsigned(fd, n):
msg = UNSIGNED_STRUCT.pack(n)
while msg:
- while True:
- try:
- nbytes = os.write(fd, msg)
- except InterruptedError:
- pass
- else:
- break
+ nbytes = os.write(fd, msg)
if nbytes == 0:
raise RuntimeError('should not get here')
msg = msg[nbytes:]
diff --git a/Lib/multiprocessing/heap.py b/Lib/multiprocessing/heap.py
index 344a45f..333b3ba 100644
--- a/Lib/multiprocessing/heap.py
+++ b/Lib/multiprocessing/heap.py
@@ -54,7 +54,9 @@ if sys.platform == 'win32':
def __setstate__(self, state):
self.size, self.name = self._state = state
self.buffer = mmap.mmap(-1, self.size, tagname=self.name)
- assert _winapi.GetLastError() == _winapi.ERROR_ALREADY_EXISTS
+ # XXX Temporarily preventing buildbot failures while determining
+ # XXX the correct long-term fix. See issue 23060
+ #assert _winapi.GetLastError() == _winapi.ERROR_ALREADY_EXISTS
else:
@@ -216,9 +218,8 @@ class Heap(object):
assert 0 <= size < sys.maxsize
if os.getpid() != self._lastpid:
self.__init__() # reinitialize after fork
- self._lock.acquire()
- self._free_pending_blocks()
- try:
+ with self._lock:
+ self._free_pending_blocks()
size = self._roundup(max(size,1), self._alignment)
(arena, start, stop) = self._malloc(size)
new_stop = start + size
@@ -227,8 +228,6 @@ class Heap(object):
block = (arena, start, new_stop)
self._allocated_blocks.add(block)
return block
- finally:
- self._lock.release()
#
# Class representing a chunk of an mmap -- can be inherited by child process
diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py
index 66d46fc..776656e 100644
--- a/Lib/multiprocessing/managers.py
+++ b/Lib/multiprocessing/managers.py
@@ -65,8 +65,8 @@ class Token(object):
(self.typeid, self.address, self.id) = state
def __repr__(self):
- return 'Token(typeid=%r, address=%r, id=%r)' % \
- (self.typeid, self.address, self.id)
+ return '%s(typeid=%r, address=%r, id=%r)' % \
+ (self.__class__.__name__, self.typeid, self.address, self.id)
#
# Function for communication with a manager's server process
@@ -306,8 +306,7 @@ class Server(object):
'''
Return some info --- useful to spot problems with refcounting
'''
- self.mutex.acquire()
- try:
+ with self.mutex:
result = []
keys = list(self.id_to_obj.keys())
keys.sort()
@@ -317,8 +316,6 @@ class Server(object):
(ident, self.id_to_refcount[ident],
str(self.id_to_obj[ident][0])[:75]))
return '\n'.join(result)
- finally:
- self.mutex.release()
def number_of_objects(self, c):
'''
@@ -343,8 +340,7 @@ class Server(object):
'''
Create a new shared object and return its id
'''
- self.mutex.acquire()
- try:
+ with self.mutex:
callable, exposed, method_to_typeid, proxytype = \
self.registry[typeid]
@@ -374,8 +370,6 @@ class Server(object):
# has been created.
self.incref(c, ident)
return ident, tuple(exposed)
- finally:
- self.mutex.release()
def get_methods(self, c, token):
'''
@@ -392,22 +386,16 @@ class Server(object):
self.serve_client(c)
def incref(self, c, ident):
- self.mutex.acquire()
- try:
+ with self.mutex:
self.id_to_refcount[ident] += 1
- finally:
- self.mutex.release()
def decref(self, c, ident):
- self.mutex.acquire()
- try:
+ 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)
- finally:
- self.mutex.release()
#
# Class to represent state of a manager
@@ -671,14 +659,11 @@ class BaseProxy(object):
def __init__(self, token, serializer, manager=None,
authkey=None, exposed=None, incref=True):
- BaseProxy._mutex.acquire()
- try:
+ with BaseProxy._mutex:
tls_idset = BaseProxy._address_to_local.get(token.address, None)
if tls_idset is None:
tls_idset = util.ForkAwareLocal(), ProcessLocalSet()
BaseProxy._address_to_local[token.address] = tls_idset
- finally:
- BaseProxy._mutex.release()
# self._tls is used to record the connection used by this
# thread to communicate with the manager at token.address
@@ -818,8 +803,8 @@ class BaseProxy(object):
return self._getvalue()
def __repr__(self):
- return '<%s object, typeid %r at %s>' % \
- (type(self).__name__, self._token.typeid, '0x%x' % id(self))
+ return '<%s object, typeid %r at %#x>' % \
+ (type(self).__name__, self._token.typeid, id(self))
def __str__(self):
'''
@@ -916,7 +901,7 @@ class Namespace(object):
if not name.startswith('_'):
temp.append('%s=%r' % (name, value))
temp.sort()
- return 'Namespace(%s)' % str.join(', ', temp)
+ return '%s(%s)' % (self.__class__.__name__, ', '.join(temp))
class Value(object):
def __init__(self, typecode, value, lock=True):
diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py
index 8832a5c..75a76a4 100644
--- a/Lib/multiprocessing/pool.py
+++ b/Lib/multiprocessing/pool.py
@@ -87,7 +87,7 @@ class MaybeEncodingError(Exception):
self.exc)
def __repr__(self):
- return "<MaybeEncodingError: %s>" % str(self)
+ return "<%s: %s>" % (self.__class__.__name__, self)
def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None,
@@ -666,8 +666,7 @@ class IMapIterator(object):
return self
def next(self, timeout=None):
- self._cond.acquire()
- try:
+ with self._cond:
try:
item = self._items.popleft()
except IndexError:
@@ -680,8 +679,6 @@ class IMapIterator(object):
if self._index == self._length:
raise StopIteration
raise TimeoutError
- finally:
- self._cond.release()
success, value = item
if success:
@@ -691,8 +688,7 @@ class IMapIterator(object):
__next__ = next # XXX
def _set(self, i, obj):
- self._cond.acquire()
- try:
+ with self._cond:
if self._index == i:
self._items.append(obj)
self._index += 1
@@ -706,18 +702,13 @@ class IMapIterator(object):
if self._index == self._length:
del self._cache[self._job]
- finally:
- self._cond.release()
def _set_length(self, length):
- self._cond.acquire()
- try:
+ with self._cond:
self._length = length
if self._index == self._length:
self._cond.notify()
del self._cache[self._job]
- finally:
- self._cond.release()
#
# Class whose instances are returned by `Pool.imap_unordered()`
@@ -726,15 +717,12 @@ class IMapIterator(object):
class IMapUnorderedIterator(IMapIterator):
def _set(self, i, obj):
- self._cond.acquire()
- try:
+ with self._cond:
self._items.append(obj)
self._index += 1
self._cond.notify()
if self._index == self._length:
del self._cache[self._job]
- finally:
- self._cond.release()
#
#
@@ -760,10 +748,7 @@ class ThreadPool(Pool):
@staticmethod
def _help_stuff_finish(inqueue, task_handler, size):
# put sentinels at head of inqueue to make workers finish
- inqueue.not_empty.acquire()
- try:
+ with inqueue.not_empty:
inqueue.queue.clear()
inqueue.queue.extend([None] * size)
inqueue.not_empty.notify_all()
- finally:
- inqueue.not_empty.release()
diff --git a/Lib/multiprocessing/popen_fork.py b/Lib/multiprocessing/popen_fork.py
index 367e72e..d2ebd7c 100644
--- a/Lib/multiprocessing/popen_fork.py
+++ b/Lib/multiprocessing/popen_fork.py
@@ -1,7 +1,6 @@
import os
import sys
import signal
-import errno
from . import util
@@ -29,8 +28,6 @@ class Popen(object):
try:
pid, sts = os.waitpid(self.pid, flag)
except OSError as e:
- if e.errno == errno.EINTR:
- continue
# Child process not yet created. See #1731717
# e.errno == errno.ECHILD == 10
return None
diff --git a/Lib/multiprocessing/queues.py b/Lib/multiprocessing/queues.py
index f650771..c07ad40 100644
--- a/Lib/multiprocessing/queues.py
+++ b/Lib/multiprocessing/queues.py
@@ -81,14 +81,11 @@ class Queue(object):
if not self._sem.acquire(block, timeout):
raise Full
- self._notempty.acquire()
- try:
+ with self._notempty:
if self._thread is None:
self._start_thread()
self._buffer.append(obj)
self._notempty.notify()
- finally:
- self._notempty.release()
def get(self, block=True, timeout=None):
if block and timeout is None:
@@ -201,12 +198,9 @@ class Queue(object):
@staticmethod
def _finalize_close(buffer, notempty):
debug('telling queue thread to quit')
- notempty.acquire()
- try:
+ with notempty:
buffer.append(_sentinel)
notempty.notify()
- finally:
- notempty.release()
@staticmethod
def _feed(buffer, notempty, send_bytes, writelock, close, ignore_epipe):
@@ -295,35 +289,24 @@ class JoinableQueue(Queue):
if not self._sem.acquire(block, timeout):
raise Full
- self._notempty.acquire()
- self._cond.acquire()
- try:
+ with self._notempty, self._cond:
if self._thread is None:
self._start_thread()
self._buffer.append(obj)
self._unfinished_tasks.release()
self._notempty.notify()
- finally:
- self._cond.release()
- self._notempty.release()
def task_done(self):
- self._cond.acquire()
- try:
+ with self._cond:
if not self._unfinished_tasks.acquire(False):
raise ValueError('task_done() called too many times')
if self._unfinished_tasks._semlock._is_zero():
self._cond.notify_all()
- finally:
- self._cond.release()
def join(self):
- self._cond.acquire()
- try:
+ with self._cond:
if not self._unfinished_tasks._semlock._is_zero():
self._cond.wait()
- finally:
- self._cond.release()
#
# Simplified Queue type -- really just a locked pipe
diff --git a/Lib/multiprocessing/sharedctypes.py b/Lib/multiprocessing/sharedctypes.py
index 0c17825..4258f59 100644
--- a/Lib/multiprocessing/sharedctypes.py
+++ b/Lib/multiprocessing/sharedctypes.py
@@ -188,6 +188,12 @@ class SynchronizedBase(object):
self.acquire = self._lock.acquire
self.release = self._lock.release
+ def __enter__(self):
+ return self._lock.__enter__()
+
+ def __exit__(self, *args):
+ return self._lock.__exit__(*args)
+
def __reduce__(self):
assert_spawning(self)
return synchronized, (self._obj, self._lock)
@@ -212,32 +218,20 @@ class SynchronizedArray(SynchronizedBase):
return len(self._obj)
def __getitem__(self, i):
- self.acquire()
- try:
+ with self:
return self._obj[i]
- finally:
- self.release()
def __setitem__(self, i, value):
- self.acquire()
- try:
+ with self:
self._obj[i] = value
- finally:
- self.release()
def __getslice__(self, start, stop):
- self.acquire()
- try:
+ with self:
return self._obj[start:stop]
- finally:
- self.release()
def __setslice__(self, start, stop, values):
- self.acquire()
- try:
+ with self:
self._obj[start:stop] = values
- finally:
- self.release()
class SynchronizedString(SynchronizedArray):
diff --git a/Lib/multiprocessing/synchronize.py b/Lib/multiprocessing/synchronize.py
index dea1cbd..d4bdf0e 100644
--- a/Lib/multiprocessing/synchronize.py
+++ b/Lib/multiprocessing/synchronize.py
@@ -134,7 +134,7 @@ class Semaphore(SemLock):
value = self._semlock._get_value()
except Exception:
value = 'unknown'
- return '<Semaphore(value=%s)>' % value
+ return '<%s(value=%s)>' % (self.__class__.__name__, value)
#
# Bounded semaphore
@@ -150,8 +150,8 @@ class BoundedSemaphore(Semaphore):
value = self._semlock._get_value()
except Exception:
value = 'unknown'
- return '<BoundedSemaphore(value=%s, maxvalue=%s)>' % \
- (value, self._semlock.maxvalue)
+ return '<%s(value=%s, maxvalue=%s)>' % \
+ (self.__class__.__name__, value, self._semlock.maxvalue)
#
# Non-recursive lock
@@ -176,7 +176,7 @@ class Lock(SemLock):
name = 'SomeOtherProcess'
except Exception:
name = 'unknown'
- return '<Lock(owner=%s)>' % name
+ return '<%s(owner=%s)>' % (self.__class__.__name__, name)
#
# Recursive lock
@@ -202,7 +202,7 @@ class RLock(SemLock):
name, count = 'SomeOtherProcess', 'nonzero'
except Exception:
name, count = 'unknown', 'unknown'
- return '<RLock(%s, %s)>' % (name, count)
+ return '<%s(%s, %s)>' % (self.__class__.__name__, name, count)
#
# Condition variable
@@ -243,7 +243,7 @@ class Condition(object):
self._woken_count._semlock._get_value())
except Exception:
num_waiters = 'unknown'
- return '<Condition(%s, %s)>' % (self._lock, num_waiters)
+ return '<%s(%s, %s)>' % (self.__class__.__name__, self._lock, num_waiters)
def wait(self, timeout=None):
assert self._lock._semlock._is_mine(), \
@@ -337,34 +337,24 @@ class Event(object):
self._flag = ctx.Semaphore(0)
def is_set(self):
- self._cond.acquire()
- try:
+ with self._cond:
if self._flag.acquire(False):
self._flag.release()
return True
return False
- finally:
- self._cond.release()
def set(self):
- self._cond.acquire()
- try:
+ with self._cond:
self._flag.acquire(False)
self._flag.release()
self._cond.notify_all()
- finally:
- self._cond.release()
def clear(self):
- self._cond.acquire()
- try:
+ with self._cond:
self._flag.acquire(False)
- finally:
- self._cond.release()
def wait(self, timeout=None):
- self._cond.acquire()
- try:
+ with self._cond:
if self._flag.acquire(False):
self._flag.release()
else:
@@ -374,8 +364,6 @@ class Event(object):
self._flag.release()
return True
return False
- finally:
- self._cond.release()
#
# Barrier
diff --git a/Lib/multiprocessing/util.py b/Lib/multiprocessing/util.py
index 0b695e4..ea5443d 100644
--- a/Lib/multiprocessing/util.py
+++ b/Lib/multiprocessing/util.py
@@ -212,10 +212,11 @@ class Finalize(object):
obj = None
if obj is None:
- return '<Finalize object, dead>'
+ return '<%s object, dead>' % self.__class__.__name__
- x = '<Finalize object, callback=%s' % \
- getattr(self._callback, '__name__', self._callback)
+ x = '<%s object, callback=%s' % (
+ self.__class__.__name__,
+ getattr(self._callback, '__name__', self._callback))
if self._args:
x += ', args=' + str(self._args)
if self._kwargs:
@@ -327,6 +328,13 @@ class ForkAwareThreadLock(object):
self.acquire = self._lock.acquire
self.release = self._lock.release
+ def __enter__(self):
+ return self._lock.__enter__()
+
+ def __exit__(self, *args):
+ return self._lock.__exit__(*args)
+
+
class ForkAwareLocal(threading.local):
def __init__(self):
register_after_fork(self, lambda obj : obj.__dict__.clear())
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index af3fb87..ee1a215 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -32,48 +32,12 @@ if 'ce' in sys.builtin_module_names:
defpath = '\\Windows'
devnull = 'nul'
-def _get_empty(path):
- if isinstance(path, bytes):
- return b''
- else:
- return ''
-
-def _get_sep(path):
- if isinstance(path, bytes):
- return b'\\'
- else:
- return '\\'
-
-def _get_altsep(path):
- if isinstance(path, bytes):
- return b'/'
- else:
- return '/'
-
def _get_bothseps(path):
if isinstance(path, bytes):
return b'\\/'
else:
return '\\/'
-def _get_dot(path):
- if isinstance(path, bytes):
- return b'.'
- else:
- return '.'
-
-def _get_colon(path):
- if isinstance(path, bytes):
- return b':'
- else:
- return ':'
-
-def _get_special(path):
- if isinstance(path, bytes):
- return (b'\\\\.\\', b'\\\\?\\')
- else:
- return ('\\\\.\\', '\\\\?\\')
-
# Normalize the case of a pathname and map slashes to backslashes.
# Other normalizations (such as optimizing '../' away) are not done
# (this is done by normpath).
@@ -82,10 +46,16 @@ def normcase(s):
"""Normalize case of pathname.
Makes all characters lowercase and all slashes into backslashes."""
- if not isinstance(s, (bytes, str)):
- raise TypeError("normcase() argument must be str or bytes, "
- "not '{}'".format(s.__class__.__name__))
- return s.replace(_get_altsep(s), _get_sep(s)).lower()
+ try:
+ if isinstance(s, bytes):
+ return s.replace(b'/', b'\\').lower()
+ else:
+ return s.replace('/', '\\').lower()
+ except (TypeError, AttributeError):
+ if not isinstance(s, (bytes, str)):
+ raise TypeError("normcase() argument must be str or bytes, "
+ "not %r" % s.__class__.__name__) from None
+ raise
# Return whether a path is absolute.
@@ -97,40 +67,49 @@ def normcase(s):
def isabs(s):
"""Test whether a path is absolute"""
s = splitdrive(s)[1]
- return len(s) > 0 and s[:1] in _get_bothseps(s)
+ return len(s) > 0 and s[0] in _get_bothseps(s)
# Join two (or more) paths.
def join(path, *paths):
- sep = _get_sep(path)
- seps = _get_bothseps(path)
- colon = _get_colon(path)
- result_drive, result_path = splitdrive(path)
- for p in paths:
- p_drive, p_path = splitdrive(p)
- if p_path and p_path[0] in seps:
- # Second path is absolute
- if p_drive or not result_drive:
- result_drive = p_drive
- result_path = p_path
- continue
- elif p_drive and p_drive != result_drive:
- if p_drive.lower() != result_drive.lower():
- # Different drives => ignore the first path entirely
- result_drive = p_drive
+ if isinstance(path, bytes):
+ sep = b'\\'
+ seps = b'\\/'
+ colon = b':'
+ else:
+ sep = '\\'
+ seps = '\\/'
+ colon = ':'
+ try:
+ result_drive, result_path = splitdrive(path)
+ for p in paths:
+ p_drive, p_path = splitdrive(p)
+ if p_path and p_path[0] in seps:
+ # Second path is absolute
+ if p_drive or not result_drive:
+ result_drive = p_drive
result_path = p_path
continue
- # Same drive in different case
- result_drive = p_drive
- # Second path is relative to the first
- if result_path and result_path[-1] not in seps:
- result_path = result_path + sep
- result_path = result_path + p_path
- ## add separator between UNC and non-absolute path
- if (result_path and result_path[0] not in seps and
- result_drive and result_drive[-1:] != colon):
- return result_drive + sep + result_path
- return result_drive + result_path
+ elif p_drive and p_drive != result_drive:
+ if p_drive.lower() != result_drive.lower():
+ # Different drives => ignore the first path entirely
+ result_drive = p_drive
+ result_path = p_path
+ continue
+ # Same drive in different case
+ result_drive = p_drive
+ # Second path is relative to the first
+ if result_path and result_path[-1] not in seps:
+ result_path = result_path + sep
+ result_path = result_path + p_path
+ ## add separator between UNC and non-absolute path
+ if (result_path and result_path[0] not in seps and
+ result_drive and result_drive[-1:] != colon):
+ return result_drive + sep + result_path
+ return result_drive + result_path
+ except (TypeError, AttributeError, BytesWarning):
+ genericpath._check_arg_types('join', path, *paths)
+ raise
# Split a path in a drive specification (a drive letter followed by a
@@ -155,10 +134,16 @@ def splitdrive(p):
Paths cannot contain both a drive letter and a UNC path.
"""
- empty = _get_empty(p)
- if len(p) > 1:
- sep = _get_sep(p)
- normp = p.replace(_get_altsep(p), sep)
+ if len(p) >= 2:
+ if isinstance(p, bytes):
+ sep = b'\\'
+ altsep = b'/'
+ colon = b':'
+ else:
+ sep = '\\'
+ altsep = '/'
+ colon = ':'
+ normp = p.replace(altsep, sep)
if (normp[0:2] == sep*2) and (normp[2:3] != sep):
# is a UNC path:
# vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
@@ -166,18 +151,18 @@ def splitdrive(p):
# directory ^^^^^^^^^^^^^^^
index = normp.find(sep, 2)
if index == -1:
- return empty, p
+ return p[:0], p
index2 = normp.find(sep, index + 1)
# a UNC path can't have two slashes in a row
# (after the initial two)
if index2 == index + 1:
- return empty, p
+ return p[:0], p
if index2 == -1:
index2 = len(p)
return p[:index2], p[index2:]
- if normp[1:2] == _get_colon(p):
+ if normp[1:2] == colon:
return p[:2], p[2:]
- return empty, p
+ return p[:0], p
# Parse UNC paths
@@ -221,10 +206,7 @@ def split(p):
i -= 1
head, tail = p[:i], p[i:] # now tail has no slashes
# remove trailing slashes from head, unless it's all slashes
- head2 = head
- while head2 and head2[-1:] in seps:
- head2 = head2[:-1]
- head = head2 or head
+ head = head.rstrip(seps) or head
return d + head, tail
@@ -234,8 +216,10 @@ def split(p):
# It is always true that root + ext == p.
def splitext(p):
- return genericpath._splitext(p, _get_sep(p), _get_altsep(p),
- _get_dot(p))
+ if isinstance(p, bytes):
+ return genericpath._splitext(p, b'\\', b'/', b'.')
+ else:
+ return genericpath._splitext(p, '\\', '/', '.')
splitext.__doc__ = genericpath._splitext.__doc__
@@ -343,7 +327,7 @@ def expanduser(path):
userhome = join(drive, os.environ['HOMEPATH'])
if isinstance(path, bytes):
- userhome = userhome.encode(sys.getfilesystemencoding())
+ userhome = os.fsencode(userhome)
if i != 1: #~user
userhome = join(dirname(userhome), path[1:i])
@@ -369,13 +353,14 @@ def expandvars(path):
Unknown variables are left unchanged."""
if isinstance(path, bytes):
- if ord('$') not in path and ord('%') not in path:
+ if b'$' not in path and b'%' not in path:
return path
import string
varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii')
quote = b'\''
percent = b'%'
brace = b'{'
+ rbrace = b'}'
dollar = b'$'
environ = getattr(os, 'environb', None)
else:
@@ -386,6 +371,7 @@ def expandvars(path):
quote = '\''
percent = '%'
brace = '{'
+ rbrace = '}'
dollar = '$'
environ = os.environ
res = path[:0]
@@ -432,15 +418,9 @@ def expandvars(path):
path = path[index+2:]
pathlen = len(path)
try:
- if isinstance(path, bytes):
- index = path.index(b'}')
- else:
- index = path.index('}')
+ index = path.index(rbrace)
except ValueError:
- if isinstance(path, bytes):
- res += b'${' + path
- else:
- res += '${' + path
+ res += dollar + brace + path
index = pathlen - 1
else:
var = path[:index]
@@ -450,10 +430,7 @@ def expandvars(path):
else:
value = environ[var]
except KeyError:
- if isinstance(path, bytes):
- value = b'${' + var + b'}'
- else:
- value = '${' + var + '}'
+ value = dollar + brace + var + rbrace
res += value
else:
var = path[:0]
@@ -485,16 +462,25 @@ def expandvars(path):
def normpath(path):
"""Normalize path, eliminating double slashes, etc."""
- sep = _get_sep(path)
- dotdot = _get_dot(path) * 2
- special_prefixes = _get_special(path)
+ if isinstance(path, bytes):
+ sep = b'\\'
+ altsep = b'/'
+ curdir = b'.'
+ pardir = b'..'
+ special_prefixes = (b'\\\\.\\', b'\\\\?\\')
+ else:
+ sep = '\\'
+ altsep = '/'
+ curdir = '.'
+ pardir = '..'
+ special_prefixes = ('\\\\.\\', '\\\\?\\')
if path.startswith(special_prefixes):
# in the case of paths with these prefixes:
# \\.\ -> device names
# \\?\ -> literal paths
# do not do any normalization, but return the path unchanged
return path
- path = path.replace(_get_altsep(path), sep)
+ path = path.replace(altsep, sep)
prefix, path = splitdrive(path)
# collapse initial backslashes
@@ -505,13 +491,13 @@ def normpath(path):
comps = path.split(sep)
i = 0
while i < len(comps):
- if not comps[i] or comps[i] == _get_dot(path):
+ if not comps[i] or comps[i] == curdir:
del comps[i]
- elif comps[i] == dotdot:
- if i > 0 and comps[i-1] != dotdot:
+ elif comps[i] == pardir:
+ if i > 0 and comps[i-1] != pardir:
del comps[i-1:i+1]
i -= 1
- elif i == 0 and prefix.endswith(_get_sep(path)):
+ elif i == 0 and prefix.endswith(sep):
del comps[i]
else:
i += 1
@@ -519,7 +505,7 @@ def normpath(path):
i += 1
# If the path is now empty, substitute '.'
if not prefix and not comps:
- comps.append(_get_dot(path))
+ comps.append(curdir)
return prefix + sep.join(comps)
@@ -559,42 +545,48 @@ realpath = abspath
supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and
sys.getwindowsversion()[3] >= 2)
-def relpath(path, start=curdir):
+def relpath(path, start=None):
"""Return a relative version of a path"""
- sep = _get_sep(path)
+ if isinstance(path, bytes):
+ sep = b'\\'
+ curdir = b'.'
+ pardir = b'..'
+ else:
+ sep = '\\'
+ curdir = '.'
+ pardir = '..'
- if start is curdir:
- start = _get_dot(path)
+ if start is None:
+ start = curdir
if not path:
raise ValueError("no path specified")
- start_abs = abspath(normpath(start))
- path_abs = abspath(normpath(path))
- start_drive, start_rest = splitdrive(start_abs)
- path_drive, path_rest = splitdrive(path_abs)
- if normcase(start_drive) != normcase(path_drive):
- error = "path is on mount '{0}', start on mount '{1}'".format(
- path_drive, start_drive)
- raise ValueError(error)
-
- start_list = [x for x in start_rest.split(sep) if x]
- path_list = [x for x in path_rest.split(sep) if x]
- # Work out how much of the filepath is shared by start and path.
- i = 0
- for e1, e2 in zip(start_list, path_list):
- if normcase(e1) != normcase(e2):
- break
- i += 1
+ try:
+ start_abs = abspath(normpath(start))
+ path_abs = abspath(normpath(path))
+ start_drive, start_rest = splitdrive(start_abs)
+ path_drive, path_rest = splitdrive(path_abs)
+ if normcase(start_drive) != normcase(path_drive):
+ raise ValueError("path is on mount %r, start on mount %r" % (
+ path_drive, start_drive))
+
+ start_list = [x for x in start_rest.split(sep) if x]
+ path_list = [x for x in path_rest.split(sep) if x]
+ # Work out how much of the filepath is shared by start and path.
+ i = 0
+ for e1, e2 in zip(start_list, path_list):
+ if normcase(e1) != normcase(e2):
+ break
+ i += 1
- if isinstance(path, bytes):
- pardir = b'..'
- else:
- pardir = '..'
- rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
- if not rel_list:
- return _get_dot(path)
- return join(*rel_list)
+ rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
+ if not rel_list:
+ return curdir
+ return join(*rel_list)
+ except (TypeError, ValueError, AttributeError, BytesWarning, DeprecationWarning):
+ genericpath._check_arg_types('relpath', path, start)
+ raise
# determine if two files are in fact the same file
diff --git a/Lib/opcode.py b/Lib/opcode.py
index 0bd1ee6..bfd3c4d 100644
--- a/Lib/opcode.py
+++ b/Lib/opcode.py
@@ -70,6 +70,9 @@ def_op('UNARY_NOT', 12)
def_op('UNARY_INVERT', 15)
+def_op('BINARY_MATRIX_MULTIPLY', 16)
+def_op('INPLACE_MATRIX_MULTIPLY', 17)
+
def_op('BINARY_POWER', 19)
def_op('BINARY_MULTIPLY', 20)
diff --git a/Lib/operator.py b/Lib/operator.py
index b60349f..856036d 100644
--- a/Lib/operator.py
+++ b/Lib/operator.py
@@ -105,6 +105,10 @@ def mul(a, b):
"Same as a * b."
return a * b
+def matmul(a, b):
+ "Same as a @ b."
+ return a @ b
+
def neg(a):
"Same as -a."
return -a
@@ -326,6 +330,11 @@ def imul(a, b):
a *= b
return a
+def imatmul(a, b):
+ "Same as a @= b."
+ a @= b
+ return a
+
def ior(a, b):
"Same as a |= b."
a |= b
@@ -383,6 +392,7 @@ __invert__ = invert
__lshift__ = lshift
__mod__ = mod
__mul__ = mul
+__matmul__ = matmul
__neg__ = neg
__or__ = or_
__pos__ = pos
@@ -403,6 +413,7 @@ __ifloordiv__ = ifloordiv
__ilshift__ = ilshift
__imod__ = imod
__imul__ = imul
+__imatmul__ = imatmul
__ior__ = ior
__ipow__ = ipow
__irshift__ = irshift
diff --git a/Lib/os.py b/Lib/os.py
index 556f592..3bb0f4e 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -61,6 +61,10 @@ if 'posix' in _names:
except ImportError:
pass
+ import posix
+ __all__.extend(_get_exports_list(posix))
+ del posix
+
elif 'nt' in _names:
name = 'nt'
linesep = '\r\n'
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 918ac8d..01e66a0 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -225,6 +225,36 @@ class _WindowsFlavour(_Flavour):
# It's a path on a network drive => 'file://host/share/a/b'
return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8'))
+ def gethomedir(self, username):
+ if 'HOME' in os.environ:
+ userhome = os.environ['HOME']
+ elif 'USERPROFILE' in os.environ:
+ userhome = os.environ['USERPROFILE']
+ elif 'HOMEPATH' in os.environ:
+ try:
+ drv = os.environ['HOMEDRIVE']
+ except KeyError:
+ drv = ''
+ userhome = drv + os.environ['HOMEPATH']
+ else:
+ raise RuntimeError("Can't determine home directory")
+
+ if username:
+ # Try to guess user home directory. By default all users
+ # directories are located in the same place and are named by
+ # corresponding usernames. If current user home directory points
+ # to nonstandard place, this guess is likely wrong.
+ if os.environ['USERNAME'] != username:
+ drv, root, parts = self.parse_parts((userhome,))
+ if parts[-1] != os.environ['USERNAME']:
+ raise RuntimeError("Can't determine home directory "
+ "for %r" % username)
+ parts[-1] = username
+ if drv or root:
+ userhome = drv + root + self.join(parts[1:])
+ else:
+ userhome = self.join(parts)
+ return userhome
class _PosixFlavour(_Flavour):
sep = '/'
@@ -308,6 +338,21 @@ class _PosixFlavour(_Flavour):
bpath = bytes(path)
return 'file://' + urlquote_from_bytes(bpath)
+ def gethomedir(self, username):
+ if not username:
+ try:
+ return os.environ['HOME']
+ except KeyError:
+ import pwd
+ return pwd.getpwuid(os.getuid()).pw_dir
+ else:
+ import pwd
+ try:
+ return pwd.getpwnam(username).pw_dir
+ except KeyError:
+ raise RuntimeError("Can't determine home directory "
+ "for %r" % username)
+
_windows_flavour = _WindowsFlavour()
_posix_flavour = _PosixFlavour()
@@ -964,6 +1009,24 @@ class Path(PurePath):
"""
return cls(os.getcwd())
+ @classmethod
+ def home(cls):
+ """Return a new path pointing to the user's home directory (as
+ returned by os.path.expanduser('~')).
+ """
+ return cls(cls()._flavour.gethomedir(None))
+
+ def samefile(self, other_path):
+ """Return whether `other_file` is the same or not as this file.
+ (as returned by os.path.samefile(file, other_file)).
+ """
+ st = self.stat()
+ try:
+ other_st = other_path.stat()
+ except AttributeError:
+ other_st = os.stat(other_path)
+ return os.path.samestat(st, other_st)
+
def iterdir(self):
"""Iterate over the files in this directory. Does not yield any
result for the special paths '.' and '..'.
@@ -1072,6 +1135,39 @@ class Path(PurePath):
return io.open(str(self), mode, buffering, encoding, errors, newline,
opener=self._opener)
+ def read_bytes(self):
+ """
+ Open the file in bytes mode, read it, and close the file.
+ """
+ with self.open(mode='rb') as f:
+ return f.read()
+
+ def read_text(self, encoding=None, errors=None):
+ """
+ Open the file in text mode, read it, and close the file.
+ """
+ with self.open(mode='r', encoding=encoding, errors=errors) as f:
+ return f.read()
+
+ def write_bytes(self, data):
+ """
+ Open the file in bytes mode, write to it, and close the file.
+ """
+ # type-check for the buffer interface before truncating the file
+ view = memoryview(data)
+ with self.open(mode='wb') as f:
+ return f.write(view)
+
+ def write_text(self, data, encoding=None, errors=None):
+ """
+ Open the file in text mode, write to it, and close the file.
+ """
+ if not isinstance(data, str):
+ raise TypeError('data must be str, not %s' %
+ data.__class__.__name__)
+ with self.open(mode='w', encoding=encoding, errors=errors) as f:
+ return f.write(data)
+
def touch(self, mode=0o666, exist_ok=True):
"""
Create this file with the given access mode, if it doesn't exist.
@@ -1095,14 +1191,21 @@ class Path(PurePath):
fd = self._raw_open(flags, mode)
os.close(fd)
- def mkdir(self, mode=0o777, parents=False):
+ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
if self._closed:
self._raise_closed()
if not parents:
- self._accessor.mkdir(self, mode)
+ try:
+ self._accessor.mkdir(self, mode)
+ except FileExistsError:
+ if not exist_ok or not self.is_dir():
+ raise
else:
try:
self._accessor.mkdir(self, mode)
+ except FileExistsError:
+ if not exist_ok or not self.is_dir():
+ raise
except OSError as e:
if e.errno != ENOENT:
raise
@@ -1283,6 +1386,17 @@ class Path(PurePath):
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
return False
+ def expanduser(self):
+ """ Return a new path with expanded ~ and ~user constructs
+ (as returned by os.path.expanduser)
+ """
+ if (not (self._drv or self._root) and
+ self._parts and self._parts[0][:1] == '~'):
+ homedir = self._flavour.gethomedir(self._parts[0][1:])
+ return self._from_parts([homedir] + self._parts[1:])
+
+ return self
+
class PosixPath(Path, PurePosixPath):
__slots__ = ()
diff --git a/Lib/pdb.py b/Lib/pdb.py
index e28564b..cf2edbf 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -1316,7 +1316,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
return
# Is it a class?
if value.__class__ is type:
- self.message('Class %s.%s' % (value.__module__, value.__name__))
+ self.message('Class %s.%s' % (value.__module__, value.__qualname__))
return
# None of the above...
self.message(type(value))
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index a54e947..fc4a074 100644
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -616,7 +616,7 @@ def get_data(package, resource):
return None
# XXX needs test
mod = (sys.modules.get(package) or
- importlib._bootstrap._SpecMethods(spec).load())
+ importlib._bootstrap._load(spec))
if mod is None or not hasattr(mod, '__file__'):
return None
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index 0aa53fe..44ed838 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -82,11 +82,8 @@ def join(a, *p):
path += b
else:
path += sep + b
- except TypeError:
- if all(isinstance(s, (str, bytes)) for s in (a,) + p):
- # Must have a mixture of text and binary data
- raise TypeError("Can't mix strings and bytes in path "
- "components") from None
+ except (TypeError, AttributeError, BytesWarning):
+ genericpath._check_arg_types('join', a, *p)
raise
return path
@@ -445,13 +442,16 @@ def relpath(path, start=None):
if start is None:
start = curdir
- start_list = [x for x in abspath(start).split(sep) if x]
- path_list = [x for x in abspath(path).split(sep) if x]
-
- # Work out how much of the filepath is shared by start and path.
- i = len(commonprefix([start_list, path_list]))
-
- rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
- if not rel_list:
- return curdir
- return join(*rel_list)
+ try:
+ start_list = [x for x in abspath(start).split(sep) if x]
+ path_list = [x for x in abspath(path).split(sep) if x]
+ # Work out how much of the filepath is shared by start and path.
+ i = len(commonprefix([start_list, path_list]))
+
+ rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
+ if not rel_list:
+ return curdir
+ return join(*rel_list)
+ except (TypeError, AttributeError, BytesWarning, DeprecationWarning):
+ genericpath._check_arg_types('relpath', path, start)
+ raise
diff --git a/Lib/pprint.py b/Lib/pprint.py
index 2cbffed..0091e69 100644
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -161,7 +161,7 @@ class PrettyPrinter:
return
rep = self._repr(object, context, level - 1)
typ = type(object)
- max_width = self._width - 1 - indent - allowance
+ max_width = self._width - indent - allowance
sepLines = len(rep) > max_width
write = stream.write
@@ -174,24 +174,14 @@ class PrettyPrinter:
length = len(object)
if length:
context[objid] = 1
- indent = indent + self._indent_per_level
if issubclass(typ, _OrderedDict):
items = list(object.items())
else:
items = sorted(object.items(), key=_safe_tuple)
- key, ent = items[0]
- rep = self._repr(key, context, level)
- write(rep)
- write(': ')
- self._format(ent, stream, indent + len(rep) + 2,
- allowance + 1, context, level)
- if length > 1:
- for key, ent in items[1:]:
- rep = self._repr(key, context, level)
- write(',\n%s%s: ' % (' '*indent, rep))
- self._format(ent, stream, indent + len(rep) + 2,
- allowance + 1, context, level)
- indent = indent - self._indent_per_level
+ self._format_dict_items(items, stream,
+ indent + self._indent_per_level,
+ allowance + 1,
+ context, level)
del context[objid]
write('}')
return
@@ -207,7 +197,10 @@ class PrettyPrinter:
endchar = ']'
elif issubclass(typ, tuple):
write('(')
- endchar = ')'
+ if length == 1:
+ endchar = ',)'
+ else:
+ endchar = ')'
else:
if not length:
write(rep)
@@ -227,10 +220,9 @@ class PrettyPrinter:
context[objid] = 1
self._format_items(object, stream,
indent + self._indent_per_level,
- allowance + 1, context, level)
+ allowance + len(endchar),
+ context, level)
del context[objid]
- if issubclass(typ, tuple) and length == 1:
- write(',')
write(endchar)
return
@@ -239,19 +231,27 @@ class PrettyPrinter:
lines = object.splitlines(True)
if level == 1:
indent += 1
- max_width -= 2
+ allowance += 1
+ max_width1 = max_width = self._width - indent
for i, line in enumerate(lines):
rep = repr(line)
- if len(rep) <= max_width:
+ if i == len(lines) - 1:
+ max_width1 -= allowance
+ if len(rep) <= max_width1:
chunks.append(rep)
else:
# A list of alternating (non-space, space) strings
- parts = re.split(r'(\s+)', line) + ['']
+ parts = re.findall(r'\S*\s*', line)
+ assert parts
+ assert not parts[-1]
+ parts.pop() # drop empty last part
+ max_width2 = max_width
current = ''
- for i in range(0, len(parts), 2):
- part = parts[i] + parts[i+1]
+ for j, part in enumerate(parts):
candidate = current + part
- if len(repr(candidate)) > max_width:
+ if j == len(parts) - 1 and i == len(lines) - 1:
+ max_width2 -= allowance
+ if len(repr(candidate)) > max_width2:
if current:
chunks.append(repr(current))
current = part
@@ -273,12 +273,41 @@ class PrettyPrinter:
return
write(rep)
+ def _format_dict_items(self, items, stream, indent, allowance, context,
+ level):
+ write = stream.write
+ delimnl = ',\n' + ' ' * indent
+ last_index = len(items) - 1
+ for i, (key, ent) in enumerate(items):
+ last = i == last_index
+ rep = self._repr(key, context, level)
+ write(rep)
+ write(': ')
+ self._format(ent, stream, indent + len(rep) + 2,
+ allowance if last else 1,
+ context, level)
+ if not last:
+ write(delimnl)
+
def _format_items(self, items, stream, indent, allowance, context, level):
write = stream.write
delimnl = ',\n' + ' ' * indent
delim = ''
- width = max_width = self._width - indent - allowance + 2
- for ent in items:
+ width = max_width = self._width - indent + 1
+ it = iter(items)
+ try:
+ next_ent = next(it)
+ except StopIteration:
+ return
+ last = False
+ while not last:
+ ent = next_ent
+ try:
+ next_ent = next(it)
+ except StopIteration:
+ last = True
+ max_width -= allowance
+ width -= allowance
if self._compact:
rep = self._repr(ent, context, level)
w = len(rep) + 2
@@ -294,7 +323,9 @@ class PrettyPrinter:
continue
write(delim)
delim = delimnl
- self._format(ent, stream, indent, allowance, context, level)
+ self._format(ent, stream, indent,
+ allowance if last else 1,
+ context, level)
def _repr(self, object, context, level):
repr, readable, recursive = self.format(object, context.copy(),
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index d53a1b4..60b0a9e5 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -264,9 +264,8 @@ def synopsis(filename, cache={}):
# XXX We probably don't need to pass in the loader here.
spec = importlib.util.spec_from_file_location('__temp__', filename,
loader=loader)
- _spec = importlib._bootstrap._SpecMethods(spec)
try:
- module = _spec.load()
+ module = importlib._bootstrap._load(spec)
except:
return None
del sys.modules['__temp__']
@@ -298,9 +297,8 @@ def importfile(path):
loader = importlib._bootstrap.SourceFileLoader(name, path)
# XXX We probably don't need to pass in the loader here.
spec = importlib.util.spec_from_file_location(name, path, loader=loader)
- _spec = importlib._bootstrap._SpecMethods(spec)
try:
- return _spec.load()
+ return importlib._bootstrap._load(spec)
except:
raise ErrorDuringImport(path, sys.exc_info())
@@ -1819,7 +1817,8 @@ class Helper:
if inspect.stack()[1][3] == '?':
self()
return ''
- return '<pydoc.Helper instance>'
+ return '<%s.%s instance>' % (self.__class__.__module__,
+ self.__class__.__qualname__)
_GoInteractive = object()
def __call__(self, request=_GoInteractive):
@@ -2068,9 +2067,8 @@ class ModuleScanner:
else:
path = None
else:
- _spec = importlib._bootstrap._SpecMethods(spec)
try:
- module = _spec.load()
+ module = importlib._bootstrap._load(spec)
except ImportError:
if onerror:
onerror(modname)
diff --git a/Lib/pydoc_data/topics.py b/Lib/pydoc_data/topics.py
index 8c20eeb..489259e 100644
--- a/Lib/pydoc_data/topics.py
+++ b/Lib/pydoc_data/topics.py
@@ -1,13 +1,13 @@
# -*- coding: utf-8 -*-
-# Autogenerated by Sphinx on Sat Feb 7 15:53:49 2015
+# Autogenerated by Sphinx on Sat Feb 7 15:53:56 2015
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 "=")+ (expression_list | 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 a single target: The object is assigned to\n that target.\n\n* If the target list is a comma-separated list of targets: The\n object must be an iterable with the same number of items as there\n are targets in the target list, and the items are assigned, from\n left to right, to the corresponding targets.\n\n * If the target list contains one target prefixed with an\n asterisk, called a "starred" target: The object must be a sequence\n with at least as many items as there are targets in the target\n list, minus one. The first items of the sequence are assigned,\n from left to right, to the targets before the starred target. The\n final items of the sequence are assigned to the targets after the\n starred target. A list of the remaining items in the sequence is\n then assigned to the starred target (the list can be empty).\n\n * Else: The object must be a sequence 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 a target list enclosed in parentheses or in\n square brackets: The object must be an iterable with the same number\n of items as there are targets in the target list, and its items are\n assigned, from left to right, to the corresponding targets.\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 \'simultanenous\' (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',
+ '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 "=")+ (expression_list | 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 a single target: The object is assigned to\n that target.\n\n* If the target list is a comma-separated list of targets: The\n object must be an iterable with the same number of items as there\n are targets in the target list, and the items are assigned, from\n left to right, to the corresponding targets.\n\n * If the target list contains one target prefixed with an\n asterisk, called a "starred" target: The object must be a sequence\n with at least as many items as there are targets in the target\n list, minus one. The first items of the sequence are assigned,\n from left to right, to the targets before the starred target. The\n final items of the sequence are assigned to the targets after the\n starred target. A list of the remaining items in the sequence is\n then assigned to the starred target (the list can be empty).\n\n * Else: The object must be a sequence 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 a target list enclosed in parentheses or in\n square brackets: The object must be an iterable with the same number\n of items as there are targets in the target list, and its items are\n assigned, from left to right, to the corresponding targets.\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 \'simultanenous\' (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 "//" 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 "/" (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',
+ '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',
@@ -49,9 +49,9 @@ topics = {'assert': u'\nThe "assert" statement\n**********************\n\nAssert
'naming': u'\nNaming and binding\n******************\n\n*Names* refer to objects. Names are introduced by name binding\noperations. Each occurrence of a name in the program text refers to\nthe *binding* of that name established in the innermost function block\ncontaining the use.\n\nA *block* is a piece of Python program text that is executed as a\nunit. The following are blocks: a module, a function body, and a class\ndefinition. Each command typed interactively is a block. A script\nfile (a file given as standard input to the interpreter or specified\nas a command line argument to the interpreter) is a code block. A\nscript command (a command specified on the interpreter command line\nwith the \'**-c**\' option) is a code block. The string argument passed\nto the built-in functions "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\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. The scope of names defined in a\nclass block is limited to the class block; it does not extend to the\ncode blocks of methods -- this includes comprehensions and generator\nexpressions since they are implemented using a function scope. This\nmeans that the following will fail:\n\n class A:\n a = 42\n b = list(a + i for i in range(10))\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\nIf a name is bound in a block, it is a local variable of that block,\nunless declared as "nonlocal". If a name is bound at the module\nlevel, it is a global variable. (The variables of the module code\nblock are local and global.) If a variable is used in a code block\nbut not defined there, it is a *free variable*.\n\nWhen a name is not found at all, a "NameError" exception is raised.\nIf the name refers to a local variable that has not been bound, an\n"UnboundLocalError" exception is raised. "UnboundLocalError" is a\nsubclass of "NameError".\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 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 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\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\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\nA class definition is an executable statement that may use and define\nnames. These references follow the normal rules for name resolution.\nThe namespace of the class definition becomes the attribute dictionary\nof the class. Names defined at the class scope are not visible in\nmethods.\n\n\nInteraction with dynamic features\n=================================\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\nIf the wild card form of import --- "import *" --- is used in a\nfunction and the function contains or is a nested block with free\nvariables, the compiler will raise a "SyntaxError".\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.__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()", "pow()",\n "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the\n expression "x + y", where *x* is an instance of a class that has an\n "__add__()" method, "x.__add__(y)" is called. The "__divmod__()"\n method should be the equivalent to using "__floordiv__()" and\n "__mod__()"; it should not be related to "__truediv__()". Note\n that "__pow__()" should be defined to accept an optional third\n argument if the ternary version of the built-in "pow()" function is\n 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.__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()", "pow()",\n "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands.\n These functions are only called if the left operand does not\n 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.__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 do the\n operation in-place (modifying *self*) and return the result (which\n could be, but does not have to be, *self*). If a specific method\n is not defined, the augmented assignment falls back to the normal\n methods. For instance, if *x* is an instance of a class with an\n "__iadd__()" method, "x += y" is equivalent to "x = x.__iadd__(y)"\n . Otherwise, "x.__add__(y)" and "y.__radd__(x)" are considered, as\n with the evaluation of "x + y". In certain situations, augmented\n assignment can result in unexpected errors (see *Why does\n a_tuple[i] += [\'item\'] raise an exception when the addition\n works?*), but this behavior is in fact part of the data 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',
+ '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, division, remainder |\n| | [5] |\n+-------------------------------------------------+---------------------------------------+\n| "+x", "-x", "~x" | Positive, negative, bitwise NOT |\n+-------------------------------------------------+---------------------------------------+\n| "**" | Exponentiation [6] |\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] While comparisons between strings make sense at the byte\n level, they may be counter-intuitive to users. For example, the\n strings ""\\u00C7"" and ""\\u0327\\u0043"" compare differently, even\n though they both represent the same unicode character (LATIN\n CAPITAL LETTER C WITH CEDILLA). To compare strings in a human\n recognizable way, compare using "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',
+ '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| "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] While comparisons between strings make sense at the byte\n level, they may be counter-intuitive to users. For example, the\n strings ""\\u00C7"" and ""\\u0327\\u0043"" compare differently, even\n though they both represent the same unicode character (LATIN\n CAPITAL LETTER C WITH CEDILLA). To compare strings in a human\n recognizable way, compare using "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 ::= 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',
@@ -60,7 +60,7 @@ topics = {'assert': u'\nThe "assert" statement\n**********************\n\nAssert
'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\nclass.__name__\n\n The name of the class or type.\n\nclass.__qualname__\n\n The *qualified name* of the class or type.\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 customise\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 There are no implied relationships among the comparison operators.\n The truth of "x==y" does not imply that "x!=y" is false.\n Accordingly, when defining "__eq__()", one should also define\n "__ne__()" so that the operators will behave as expected. See the\n paragraph on "__hash__()" for some important notes on creating\n *hashable* objects which support custom comparison operations and\n 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.\n\n Arguments to rich comparison methods are never coerced.\n\n To automatically generate ordering operations from a single root\n operation, see "functools.total_ordering()".\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 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\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 customised 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\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.__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()", "pow()",\n "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the\n expression "x + y", where *x* is an instance of a class that has an\n "__add__()" method, "x.__add__(y)" is called. The "__divmod__()"\n method should be the equivalent to using "__floordiv__()" and\n "__mod__()"; it should not be related to "__truediv__()". Note\n that "__pow__()" should be defined to accept an optional third\n argument if the ternary version of the built-in "pow()" function is\n 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.__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()", "pow()",\n "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands.\n These functions are only called if the left operand does not\n 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.__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 do the\n operation in-place (modifying *self*) and return the result (which\n could be, but does not have to be, *self*). If a specific method\n is not defined, the augmented assignment falls back to the normal\n methods. For instance, if *x* is an instance of a class with an\n "__iadd__()" method, "x += y" is equivalent to "x = x.__iadd__(y)"\n . Otherwise, "x.__add__(y)" and "y.__radd__(x)" are considered, as\n with the evaluation of "x + y". In certain situations, augmented\n assignment can result in unexpected errors (see *Why does\n a_tuple[i] += [\'item\'] raise an exception when the addition\n works?*), but this behavior is in fact part of the data 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 0343** - 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\n-[ Footnotes ]-\n\n[1] It *is* possible in some cases to change an object\'s type,\n under certain controlled conditions. It generally isn\'t a good\n idea though, since it can lead to some very strange behaviour if\n it is handled incorrectly.\n\n[2] For operands of the same type, it is assumed that if the non-\n reflected method (such as "__add__()") fails the operation is not\n supported, which is why the reflected method is not called.\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 customise\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 There are no implied relationships among the comparison operators.\n The truth of "x==y" does not imply that "x!=y" is false.\n Accordingly, when defining "__eq__()", one should also define\n "__ne__()" so that the operators will behave as expected. See the\n paragraph on "__hash__()" for some important notes on creating\n *hashable* objects which support custom comparison operations and\n 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.\n\n Arguments to rich comparison methods are never coerced.\n\n To automatically generate ordering operations from a single root\n operation, see "functools.total_ordering()".\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 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\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 customised 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\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 0343** - 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\n-[ Footnotes ]-\n\n[1] It *is* possible in some cases to change an object\'s type,\n under certain controlled conditions. It generally isn\'t a good\n idea though, since it can lead to some very strange behaviour if\n it is handled incorrectly.\n\n[2] For operands of the same type, it is assumed that if the non-\n reflected method (such as "__add__()") fails the operation is not\n supported, which is why the reflected method is not called.\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 *String Formatting*) and the other based on\nC "printf" style formatting that handles a narrower range of types and\nis slightly harder to use correctly, but is often faster for the cases\nit 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, such that *sub* is contained in the slice "s[start:end]".\n Optional arguments *start* and *end* are interpreted as in slice\n notation. Return "-1" if *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 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\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. Formally, a digit is a character\n that has the property value Numeric_Type=Digit or\n 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. This method uses the *universal newlines* approach to\n splitting lines. Line breaks are not included in the resulting list\n unless *keepends* is given and true.\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\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(map)\n\n Return a copy of the *s* where all characters have been mapped\n through the *map* which must be a dictionary of Unicode ordinals\n (integers) to Unicode ordinals, strings or "None". Unmapped\n characters are left untouched. Characters mapped to "None" are\n deleted.\n\n You can use "str.maketrans()" to create a translation map from\n character-to-character mappings in different formats.\n\n Note: An even more flexible approach is to create a custom\n character mapping codec using the "codecs" module (see\n "encodings.cp1251" for an example).\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 character\n rather than before. The original string is returned if *width* is\n 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. Individual code units which form parts of a surrogate pair can\n be encoded using this escape sequence. Exactly four hex digits are\n 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',
diff --git a/Lib/queue.py b/Lib/queue.py
index 3cee36b..572425e 100644
--- a/Lib/queue.py
+++ b/Lib/queue.py
@@ -6,10 +6,7 @@ except ImportError:
import dummy_threading as threading
from collections import deque
from heapq import heappush, heappop
-try:
- from time import monotonic as time
-except ImportError:
- from time import time
+from time import monotonic as time
__all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue']
diff --git a/Lib/random.py b/Lib/random.py
index 4642928..1f5be45 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -687,7 +687,7 @@ def _test_generator(n, func, args):
print(round(t1-t0, 3), 'sec,', end=' ')
avg = total/n
stddev = _sqrt(sqsum/n - avg*avg)
- print('avg %g, stddev %g, min %g, max %g' % \
+ print('avg %g, stddev %g, min %g, max %g\n' % \
(avg, stddev, smallest, largest))
diff --git a/Lib/re.py b/Lib/re.py
index 199afee..bc3056f 100644
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -128,10 +128,13 @@ except ImportError:
_locale = None
# public symbols
-__all__ = [ "match", "fullmatch", "search", "sub", "subn", "split", "findall",
- "compile", "purge", "template", "escape", "A", "I", "L", "M", "S", "X",
- "U", "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
- "UNICODE", "error" ]
+__all__ = [
+ "match", "fullmatch", "search", "sub", "subn", "split",
+ "findall", "finditer", "compile", "purge", "template", "escape",
+ "error", "A", "I", "L", "M", "S", "X", "U",
+ "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
+ "UNICODE",
+]
__version__ = "2.2.1"
@@ -209,14 +212,12 @@ def findall(pattern, string, flags=0):
Empty matches are included in the result."""
return _compile(pattern, flags).findall(string)
-if sys.hexversion >= 0x02020000:
- __all__.append("finditer")
- def finditer(pattern, string, flags=0):
- """Return an iterator over all non-overlapping matches in the
- string. For each match, the iterator returns a match object.
+def finditer(pattern, string, flags=0):
+ """Return an iterator over all non-overlapping matches in the
+ string. For each match, the iterator returns a match object.
- Empty matches are included in the result."""
- return _compile(pattern, flags).finditer(string)
+ Empty matches are included in the result."""
+ return _compile(pattern, flags).finditer(string)
def compile(pattern, flags=0):
"Compile a regular expression pattern, returning a pattern object."
@@ -276,14 +277,12 @@ _pattern_type = type(sre_compile.compile("", 0))
_MAXCACHE = 512
def _compile(pattern, flags):
# internal: compile pattern
- bypass_cache = flags & DEBUG
- if not bypass_cache:
- try:
- p, loc = _cache[type(pattern), pattern, flags]
- if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE):
- return p
- except KeyError:
- pass
+ try:
+ p, loc = _cache[type(pattern), pattern, flags]
+ if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE):
+ return p
+ except KeyError:
+ pass
if isinstance(pattern, _pattern_type):
if flags:
raise ValueError(
@@ -292,7 +291,7 @@ def _compile(pattern, flags):
if not sre_compile.isstring(pattern):
raise TypeError("first argument must be string or compiled pattern")
p = sre_compile.compile(pattern, flags)
- if not bypass_cache:
+ if not (flags & DEBUG):
if len(_cache) >= _MAXCACHE:
_cache.clear()
if p.flags & LOCALE:
@@ -363,7 +362,7 @@ class Scanner:
append = result.append
match = self.scanner.scanner(string).match
i = 0
- while 1:
+ while True:
m = match()
if not m:
break
diff --git a/Lib/reprlib.py b/Lib/reprlib.py
index f803360..ecbd2cc 100644
--- a/Lib/reprlib.py
+++ b/Lib/reprlib.py
@@ -83,16 +83,22 @@ class Repr:
return self._repr_iterable(x, level, '[', ']', self.maxlist)
def repr_array(self, x, level):
+ if not x:
+ return "array('%s')" % x.typecode
header = "array('%s', [" % x.typecode
return self._repr_iterable(x, level, header, '])', self.maxarray)
def repr_set(self, x, level):
+ if not x:
+ return 'set()'
x = _possibly_sorted(x)
- return self._repr_iterable(x, level, 'set([', '])', self.maxset)
+ return self._repr_iterable(x, level, '{', '}', self.maxset)
def repr_frozenset(self, x, level):
+ if not x:
+ return 'frozenset()'
x = _possibly_sorted(x)
- return self._repr_iterable(x, level, 'frozenset([', '])',
+ return self._repr_iterable(x, level, 'frozenset({', '})',
self.maxfrozenset)
def repr_deque(self, x, level):
@@ -136,7 +142,7 @@ class Repr:
# Bugs in x.__repr__() can cause arbitrary
# exceptions -- then make up something
except Exception:
- return '<%s instance at %x>' % (x.__class__.__name__, id(x))
+ return '<%s instance at %#x>' % (x.__class__.__name__, id(x))
if len(s) > self.maxother:
i = max(0, (self.maxother-3)//2)
j = max(0, self.maxother-3-i)
diff --git a/Lib/runpy.py b/Lib/runpy.py
index 0bb57d7..d9c643d 100644
--- a/Lib/runpy.py
+++ b/Lib/runpy.py
@@ -58,7 +58,7 @@ class _ModifiedArgv0(object):
self.value = self._sentinel
sys.argv[0] = self._saved_value
-# TODO: Replace these helpers with importlib._bootstrap._SpecMethods
+# TODO: Replace these helpers with importlib._bootstrap functions
def _run_code(code, run_globals, init_globals=None,
mod_name=None, mod_spec=None,
pkg_name=None, script_name=None):
diff --git a/Lib/sched.py b/Lib/sched.py
index 2e6b00a..b47648d 100644
--- a/Lib/sched.py
+++ b/Lib/sched.py
@@ -35,16 +35,12 @@ try:
import threading
except ImportError:
import dummy_threading as threading
-try:
- from time import monotonic as _time
-except ImportError:
- from time import time as _time
+from time import monotonic as _time
__all__ = ["scheduler"]
class Event(namedtuple('Event', 'time, priority, action, argument, kwargs')):
def __eq__(s, o): return (s.time, s.priority) == (o.time, o.priority)
- def __ne__(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)
diff --git a/Lib/selectors.py b/Lib/selectors.py
index beb7ef7..6d569c3 100644
--- a/Lib/selectors.py
+++ b/Lib/selectors.py
@@ -174,9 +174,9 @@ class BaseSelector(metaclass=ABCMeta):
SelectorKey for this file object
"""
mapping = self.get_map()
+ if mapping is None:
+ raise RuntimeError('Selector is closed')
try:
- if mapping is None:
- raise KeyError
return mapping[fileobj]
except KeyError:
raise KeyError("{!r} is not registered".format(fileobj)) from None
@@ -449,6 +449,64 @@ if hasattr(select, 'epoll'):
super().close()
+if hasattr(select, 'devpoll'):
+
+ class DevpollSelector(_BaseSelectorImpl):
+ """Solaris /dev/poll selector."""
+
+ def __init__(self):
+ super().__init__()
+ self._devpoll = select.devpoll()
+
+ def fileno(self):
+ return self._devpoll.fileno()
+
+ def register(self, fileobj, events, data=None):
+ key = super().register(fileobj, events, data)
+ poll_events = 0
+ if events & EVENT_READ:
+ poll_events |= select.POLLIN
+ if events & EVENT_WRITE:
+ poll_events |= select.POLLOUT
+ self._devpoll.register(key.fd, poll_events)
+ return key
+
+ def unregister(self, fileobj):
+ key = super().unregister(fileobj)
+ self._devpoll.unregister(key.fd)
+ return key
+
+ def select(self, timeout=None):
+ if timeout is None:
+ timeout = None
+ elif timeout <= 0:
+ timeout = 0
+ else:
+ # devpoll() has a resolution of 1 millisecond, round away from
+ # zero to wait *at least* timeout seconds.
+ timeout = math.ceil(timeout * 1e3)
+ ready = []
+ try:
+ fd_event_list = self._devpoll.poll(timeout)
+ except InterruptedError:
+ return ready
+ for fd, event in fd_event_list:
+ events = 0
+ if event & ~select.POLLIN:
+ events |= EVENT_WRITE
+ if event & ~select.POLLOUT:
+ events |= EVENT_READ
+
+ key = self._key_from_fd(fd)
+ if key:
+ ready.append((key, events & key.events))
+ return ready
+
+ def close(self):
+ self._devpoll.close()
+ super().close()
+
+
if hasattr(select, 'kqueue'):
class KqueueSelector(_BaseSelectorImpl):
@@ -521,12 +579,15 @@ if hasattr(select, 'kqueue'):
super().close()
-# Choose the best implementation: roughly, epoll|kqueue > poll > select.
+# Choose the best implementation, roughly:
+# epoll|kqueue|devpoll > poll > select.
# select() also can't accept a FD > FD_SETSIZE (usually around 1024)
if 'KqueueSelector' in globals():
DefaultSelector = KqueueSelector
elif 'EpollSelector' in globals():
DefaultSelector = EpollSelector
+elif 'DevpollSelector' in globals():
+ DefaultSelector = DevpollSelector
elif 'PollSelector' in globals():
DefaultSelector = PollSelector
else:
diff --git a/Lib/shutil.py b/Lib/shutil.py
index ac06ae5..61dc804 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -7,7 +7,6 @@ XXX The functions here don't copy the resource fork or other metadata on Mac.
import os
import sys
import stat
-from os.path import abspath
import fnmatch
import collections
import errno
@@ -21,6 +20,13 @@ except ImportError:
_BZ2_SUPPORTED = False
try:
+ import lzma
+ del lzma
+ _LZMA_SUPPORTED = True
+except ImportError:
+ _LZMA_SUPPORTED = False
+
+try:
from pwd import getpwnam
except ImportError:
getpwnam = None
@@ -487,7 +493,7 @@ def _basename(path):
sep = os.path.sep + (os.path.altsep or '')
return os.path.basename(path.rstrip(sep))
-def move(src, dst):
+def move(src, dst, copy_function=copy2):
"""Recursively move a file or directory to another location. This is
similar to the Unix "mv" command. Return the file or directory's
destination.
@@ -504,6 +510,11 @@ def move(src, dst):
recreated under the new name if os.rename() fails because of cross
filesystem renames.
+ The optional `copy_function` argument is a callable that will be used
+ to copy the source or it will be delegated to `copytree`.
+ By default, copy2() is used, but any function that supports the same
+ signature (like copy()) can be used.
+
A lot more could be done here... A look at a mv.c shows a lot of
the issues this implementation glosses over.
@@ -528,17 +539,19 @@ def move(src, dst):
os.unlink(src)
elif os.path.isdir(src):
if _destinsrc(src, dst):
- raise Error("Cannot move a directory '%s' into itself '%s'." % (src, dst))
- copytree(src, real_dst, symlinks=True)
+ raise Error("Cannot move a directory '%s' into itself"
+ " '%s'." % (src, dst))
+ copytree(src, real_dst, copy_function=copy_function,
+ symlinks=True)
rmtree(src)
else:
- copy2(src, real_dst)
+ copy_function(src, real_dst)
os.unlink(src)
return real_dst
def _destinsrc(src, dst):
- src = abspath(src)
- dst = abspath(dst)
+ src = os.path.abspath(src)
+ dst = os.path.abspath(dst)
if not src.endswith(os.path.sep):
src += os.path.sep
if not dst.endswith(os.path.sep):
@@ -574,14 +587,14 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
"""Create a (possibly compressed) tar file from all the files under
'base_dir'.
- 'compress' must be "gzip" (the default), "bzip2", or None.
+ 'compress' must be "gzip" (the default), "bzip2", "xz", or None.
'owner' and 'group' can be used to define an owner and a group for the
archive that is being built. If not provided, the current owner and group
will be used.
The output tar file will be named 'base_name' + ".tar", possibly plus
- the appropriate compression extension (".gz", or ".bz2").
+ the appropriate compression extension (".gz", ".bz2", or ".xz").
Returns the output filename.
"""
@@ -592,6 +605,10 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
tar_compression['bzip2'] = 'bz2'
compress_ext['bzip2'] = '.bz2'
+ if _LZMA_SUPPORTED:
+ tar_compression['xz'] = 'xz'
+ compress_ext['xz'] = '.xz'
+
# flags for compression program, each element of list will be an argument
if compress is not None and compress not in compress_ext:
raise ValueError("bad value for 'compress', or compression format not "
@@ -631,23 +648,6 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
return archive_name
-def _call_external_zip(base_dir, zip_filename, verbose=False, dry_run=False):
- # XXX see if we want to keep an external call here
- if verbose:
- zipoptions = "-r"
- else:
- zipoptions = "-rq"
- from distutils.errors import DistutilsExecError
- from distutils.spawn import spawn
- try:
- spawn(["zip", zipoptions, zip_filename, base_dir], dry_run=dry_run)
- except DistutilsExecError:
- # XXX really should distinguish between "couldn't find
- # external 'zip' command" and "zip failed".
- raise ExecError("unable to create zip file '%s': "
- "could neither import the 'zipfile' module nor "
- "find a standalone zip utility") % zip_filename
-
def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
"""Create a zip file from all the files under 'base_dir'.
@@ -657,6 +657,8 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
available, raises ExecError. Returns the name of the output zip
file.
"""
+ import zipfile
+
zip_filename = base_name + ".zip"
archive_dir = os.path.dirname(base_name)
@@ -666,30 +668,20 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
if not dry_run:
os.makedirs(archive_dir)
- # If zipfile module is not available, try spawning an external 'zip'
- # command.
- try:
- import zipfile
- except ImportError:
- zipfile = None
-
- if zipfile is None:
- _call_external_zip(base_dir, zip_filename, verbose, dry_run)
- else:
- if logger is not None:
- logger.info("creating '%s' and adding '%s' to it",
- zip_filename, base_dir)
+ if logger is not None:
+ logger.info("creating '%s' and adding '%s' to it",
+ zip_filename, base_dir)
- if not dry_run:
- with zipfile.ZipFile(zip_filename, "w",
- compression=zipfile.ZIP_DEFLATED) as zf:
- for dirpath, dirnames, filenames in os.walk(base_dir):
- for name in filenames:
- path = os.path.normpath(os.path.join(dirpath, name))
- if os.path.isfile(path):
- zf.write(path, path)
- if logger is not None:
- logger.info("adding '%s'", path)
+ if not dry_run:
+ with zipfile.ZipFile(zip_filename, "w",
+ compression=zipfile.ZIP_DEFLATED) as zf:
+ for dirpath, dirnames, filenames in os.walk(base_dir):
+ for name in filenames:
+ path = os.path.normpath(os.path.join(dirpath, name))
+ if os.path.isfile(path):
+ zf.write(path, path)
+ if logger is not None:
+ logger.info("adding '%s'", path)
return zip_filename
@@ -703,6 +695,10 @@ if _BZ2_SUPPORTED:
_ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bzip2')],
"bzip2'ed tar-file")
+if _LZMA_SUPPORTED:
+ _ARCHIVE_FORMATS['xztar'] = (_make_tarball, [('compress', 'xz')],
+ "xz'ed tar-file")
+
def get_archive_formats():
"""Returns a list of supported formats for archiving and unarchiving.
@@ -891,7 +887,7 @@ def _unpack_zipfile(filename, extract_dir):
zip.close()
def _unpack_tarfile(filename, extract_dir):
- """Unpack tar/tar.gz/tar.bz2 `filename` to `extract_dir`
+ """Unpack tar/tar.gz/tar.bz2/tar.xz `filename` to `extract_dir`
"""
try:
tarobj = tarfile.open(filename)
@@ -910,9 +906,13 @@ _UNPACK_FORMATS = {
}
if _BZ2_SUPPORTED:
- _UNPACK_FORMATS['bztar'] = (['.bz2'], _unpack_tarfile, [],
+ _UNPACK_FORMATS['bztar'] = (['.tar.bz2', '.tbz2'], _unpack_tarfile, [],
"bzip2'ed tar-file")
+if _LZMA_SUPPORTED:
+ _UNPACK_FORMATS['xztar'] = (['.tar.xz', '.txz'], _unpack_tarfile, [],
+ "xz'ed tar-file")
+
def _find_unpack_format(filename):
for name, info in _UNPACK_FORMATS.items():
for extension in info[0]:
diff --git a/Lib/signal.py b/Lib/signal.py
new file mode 100644
index 0000000..0db3df8
--- /dev/null
+++ b/Lib/signal.py
@@ -0,0 +1,85 @@
+import _signal
+from _signal import *
+from functools import wraps as _wraps
+from enum import IntEnum as _IntEnum
+
+_globals = globals()
+
+Signals = _IntEnum(
+ 'Signals',
+ {name: value for name, value in _globals.items()
+ if name.isupper()
+ and (name.startswith('SIG') and not name.startswith('SIG_'))
+ or name.startswith('CTRL_')})
+
+class Handlers(_IntEnum):
+ SIG_DFL = _signal.SIG_DFL
+ SIG_IGN = _signal.SIG_IGN
+
+_globals.update(Signals.__members__)
+_globals.update(Handlers.__members__)
+
+if 'pthread_sigmask' in _globals:
+ class Sigmasks(_IntEnum):
+ SIG_BLOCK = _signal.SIG_BLOCK
+ SIG_UNBLOCK = _signal.SIG_UNBLOCK
+ SIG_SETMASK = _signal.SIG_SETMASK
+
+ _globals.update(Sigmasks.__members__)
+
+
+def _int_to_enum(value, enum_klass):
+ """Convert a numeric value to an IntEnum member.
+ If it's not a known member, return the numeric value itself.
+ """
+ try:
+ return enum_klass(value)
+ except ValueError:
+ return value
+
+
+def _enum_to_int(value):
+ """Convert an IntEnum member to a numeric value.
+ If it's not a IntEnum member return the value itself.
+ """
+ try:
+ return int(value)
+ except (ValueError, TypeError):
+ return value
+
+
+@_wraps(_signal.signal)
+def signal(signalnum, handler):
+ handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
+ return _int_to_enum(handler, Handlers)
+
+
+@_wraps(_signal.getsignal)
+def getsignal(signalnum):
+ handler = _signal.getsignal(signalnum)
+ return _int_to_enum(handler, Handlers)
+
+
+if 'pthread_sigmask' in _globals:
+ @_wraps(_signal.pthread_sigmask)
+ def pthread_sigmask(how, mask):
+ sigs_set = _signal.pthread_sigmask(how, mask)
+ return set(_int_to_enum(x, Signals) for x in sigs_set)
+ pthread_sigmask.__doc__ = _signal.pthread_sigmask.__doc__
+
+
+if 'sigpending' in _globals:
+ @_wraps(_signal.sigpending)
+ def sigpending():
+ sigs = _signal.sigpending()
+ return set(_int_to_enum(x, Signals) for x in sigs)
+
+
+if 'sigwait' in _globals:
+ @_wraps(_signal.sigwait)
+ def sigwait(sigset):
+ retsig = _signal.sigwait(sigset)
+ return _int_to_enum(retsig, Signals)
+ sigwait.__doc__ = _signal.sigwait
+
+del _globals, _wraps
diff --git a/Lib/site.py b/Lib/site.py
index ad5d136..4959cfc 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -7,7 +7,7 @@
This will append site-specific paths to the module search path. On
Unix (including Mac OSX), it starts with sys.prefix and
sys.exec_prefix (if different) and appends
-lib/python<version>/site-packages as well as lib/site-python.
+lib/python<version>/site-packages.
On other platforms (such as Windows), it tries each of the
prefixes directly, as well as with lib/site-packages appended. The
resulting directories, if they exist, are appended to sys.path, and
@@ -15,7 +15,7 @@ also inspected for path configuration files.
If a file named "pyvenv.cfg" exists one directory above sys.executable,
sys.prefix and sys.exec_prefix are set to that directory and
-it is also checked for site-packages and site-python (sys.base_prefix and
+it is also checked for site-packages (sys.base_prefix and
sys.base_exec_prefix will always be the "real" prefixes of the Python
installation). If "pyvenv.cfg" (a bootstrap configuration file) contains
the key "include-system-site-packages" set to anything other than "false"
@@ -285,8 +285,7 @@ def addusersitepackages(known_paths):
return known_paths
def getsitepackages(prefixes=None):
- """Returns a list containing all global site-packages directories
- (and possibly site-python).
+ """Returns a list containing all global site-packages directories.
For each directory present in ``prefixes`` (or the global ``PREFIXES``),
this function will find its `site-packages` subdirectory depending on the
@@ -307,7 +306,6 @@ def getsitepackages(prefixes=None):
sitepackages.append(os.path.join(prefix, "lib",
"python" + sys.version[:3],
"site-packages"))
- sitepackages.append(os.path.join(prefix, "lib", "site-python"))
else:
sitepackages.append(prefix)
sitepackages.append(os.path.join(prefix, "lib", "site-packages"))
@@ -323,14 +321,9 @@ def getsitepackages(prefixes=None):
return sitepackages
def addsitepackages(known_paths, prefixes=None):
- """Add site-packages (and possibly site-python) to sys.path"""
+ """Add site-packages to sys.path"""
for sitedir in getsitepackages(prefixes):
if os.path.isdir(sitedir):
- if "site-python" in sitedir:
- import warnings
- warnings.warn('"site-python" directories will not be '
- 'supported in 3.5 anymore',
- DeprecationWarning)
addsitedir(sitedir, known_paths)
return known_paths
diff --git a/Lib/smtpd.py b/Lib/smtpd.py
index 1fa157a..33653d4 100755
--- a/Lib/smtpd.py
+++ b/Lib/smtpd.py
@@ -1,5 +1,5 @@
#! /usr/bin/env python3
-"""An RFC 5321 smtp proxy.
+"""An RFC 5321 smtp proxy with optional RFC 1870 and RFC 6531 extensions.
Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]]
@@ -25,6 +25,10 @@ Options:
Restrict the total size of the incoming message to "limit" number of
bytes via the RFC 1870 SIZE extension. Defaults to 33554432 bytes.
+ --smtputf8
+ -u
+ Enable the SMTPUTF8 extension and behave as an RFC 6531 smtp proxy.
+
--debug
-d
Turn on debugging prints.
@@ -98,7 +102,6 @@ class Devnull:
DEBUGSTREAM = Devnull()
NEWLINE = '\n'
-EMPTYSTRING = ''
COMMASPACE = ', '
DATA_SIZE_DEFAULT = 33554432
@@ -116,26 +119,48 @@ class SMTPChannel(asynchat.async_chat):
command_size_limit = 512
command_size_limits = collections.defaultdict(lambda x=command_size_limit: x)
- command_size_limits.update({
- 'MAIL': command_size_limit + 26,
- })
- max_command_size_limit = max(command_size_limits.values())
+
+ @property
+ def max_command_size_limit(self):
+ try:
+ return max(self.command_size_limits.values())
+ except ValueError:
+ return self.command_size_limit
def __init__(self, server, conn, addr, data_size_limit=DATA_SIZE_DEFAULT,
- map=None):
+ map=None, enable_SMTPUTF8=False, decode_data=None):
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.received_lines = []
- self.smtp_state = self.COMMAND
+ self.enable_SMTPUTF8 = enable_SMTPUTF8
+ if enable_SMTPUTF8:
+ if decode_data:
+ 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 decode_data:
+ self._emptystring = ''
+ self._linesep = '\r\n'
+ self._dotsep = '.'
+ self._newline = NEWLINE
+ else:
+ self._emptystring = b''
+ self._linesep = b'\r\n'
+ self._dotsep = b'.'
+ self._newline = b'\n'
+ self._set_rset_state()
self.seen_greeting = ''
- self.mailfrom = None
- self.rcpttos = []
- self.received_data = ''
+ self.extended_smtp = False
+ self.command_size_limits.clear()
self.fqdn = socket.getfqdn()
- self.num_bytes = 0
try:
self.peer = conn.getpeername()
except OSError as err:
@@ -147,8 +172,22 @@ class SMTPChannel(asynchat.async_chat):
return
print('Peer:', repr(self.peer), file=DEBUGSTREAM)
self.push('220 %s %s' % (self.fqdn, __version__))
+
+ def _set_post_data_state(self):
+ """Reset state variables to their post-DATA state."""
+ self.smtp_state = self.COMMAND
+ self.mailfrom = None
+ self.rcpttos = []
+ self.require_SMTPUTF8 = False
+ self.num_bytes = 0
self.set_terminator(b'\r\n')
- self.extended_smtp = False
+
+ def _set_rset_state(self):
+ """Reset all state variables except the greeting."""
+ self._set_post_data_state()
+ self.received_data = ''
+ self.received_lines = []
+
# properties for backwards-compatibility
@property
@@ -272,9 +311,10 @@ class SMTPChannel(asynchat.async_chat):
"set 'addr' instead", DeprecationWarning, 2)
self.addr = value
- # Overrides base class for convenience
+ # Overrides base class for convenience.
def push(self, msg):
- asynchat.async_chat.push(self, bytes(msg + '\r\n', 'ascii'))
+ asynchat.async_chat.push(self, bytes(
+ msg + '\r\n', 'utf-8' if self.require_SMTPUTF8 else 'ascii'))
# Implementation of base class abstract method
def collect_incoming_data(self, data):
@@ -287,11 +327,14 @@ class SMTPChannel(asynchat.async_chat):
return
elif limit:
self.num_bytes += len(data)
- self.received_lines.append(str(data, "utf-8"))
+ if self._decode_data:
+ self.received_lines.append(str(data, 'utf-8'))
+ else:
+ self.received_lines.append(data)
# Implementation of base class abstract method
def found_terminator(self):
- line = EMPTYSTRING.join(self.received_lines)
+ line = self._emptystring.join(self.received_lines)
print('Data:', repr(line), file=DEBUGSTREAM)
self.received_lines = []
if self.smtp_state == self.COMMAND:
@@ -299,7 +342,8 @@ class SMTPChannel(asynchat.async_chat):
if not line:
self.push('500 Error: bad syntax')
return
- method = None
+ if not self._decode_data:
+ line = str(line, 'utf-8')
i = line.find(' ')
if i < 0:
command = line.upper()
@@ -330,21 +374,18 @@ class SMTPChannel(asynchat.async_chat):
# Remove extraneous carriage returns and de-transparency according
# to RFC 5321, Section 4.5.2.
data = []
- for text in line.split('\r\n'):
- if text and text[0] == '.':
+ for text in line.split(self._linesep):
+ if text and text[0] == self._dotsep:
data.append(text[1:])
else:
data.append(text)
- self.received_data = NEWLINE.join(data)
- status = self.smtp_server.process_message(self.peer,
- self.mailfrom,
- self.rcpttos,
- self.received_data)
- self.rcpttos = []
- self.mailfrom = None
- self.smtp_state = self.COMMAND
- self.num_bytes = 0
- self.set_terminator(b'\r\n')
+ self.received_data = self._newline.join(data)
+ args = (self.peer, self.mailfrom, self.rcpttos, self.received_data)
+ if self.require_SMTPUTF8:
+ status = self.smtp_server.process_smtputf8_message(*args)
+ else:
+ status = self.smtp_server.process_message(*args)
+ self._set_post_data_state()
if not status:
self.push('250 OK')
else:
@@ -355,26 +396,34 @@ class SMTPChannel(asynchat.async_chat):
if not arg:
self.push('501 Syntax: HELO hostname')
return
+ # See issue #21783 for a discussion of this behavior.
if self.seen_greeting:
self.push('503 Duplicate HELO/EHLO')
- else:
- self.seen_greeting = arg
- self.extended_smtp = False
- self.push('250 %s' % self.fqdn)
+ return
+ self._set_rset_state()
+ self.seen_greeting = arg
+ self.push('250 %s' % self.fqdn)
def smtp_EHLO(self, arg):
if not arg:
self.push('501 Syntax: EHLO hostname')
return
+ # See issue #21783 for a discussion of this behavior.
if self.seen_greeting:
self.push('503 Duplicate HELO/EHLO')
- else:
- self.seen_greeting = arg
- self.extended_smtp = True
- self.push('250-%s' % self.fqdn)
- if self.data_size_limit:
- self.push('250-SIZE %s' % self.data_size_limit)
- self.push('250 HELP')
+ return
+ self._set_rset_state()
+ self.seen_greeting = arg
+ self.extended_smtp = True
+ self.push('250-%s' % self.fqdn)
+ if self.data_size_limit:
+ self.push('250-SIZE %s' % self.data_size_limit)
+ self.command_size_limits['MAIL'] += 26
+ if self.enable_SMTPUTF8:
+ self.push('250-8BITMIME')
+ self.push('250-SMTPUTF8')
+ self.command_size_limits['MAIL'] += 10
+ self.push('250 HELP')
def smtp_NOOP(self, arg):
if arg:
@@ -407,8 +456,8 @@ class SMTPChannel(asynchat.async_chat):
def _getparams(self, params):
# Return any parameters that appear to be syntactically valid according
# to RFC 1869, ignore all others. (Postel rule: accept what we can.)
- params = [param.split('=', 1) for param in params.split()
- if '=' in param]
+ params = [param.split('=', 1) if '=' in param else (param, True)
+ for param in params.split()]
return {k: v for k, v in params if k.isalnum()}
def smtp_HELP(self, arg):
@@ -486,6 +535,14 @@ class SMTPChannel(asynchat.async_chat):
if params is None:
self.push(syntaxerr)
return
+ body = params.pop('BODY', '7BIT')
+ if self.enable_SMTPUTF8 and params.pop('SMTPUTF8', False):
+ if body != '8BITMIME':
+ self.push('501 Syntax: MAIL FROM: <address>'
+ ' [BODY=8BITMIME SMTPUTF8]')
+ return
+ else:
+ self.require_SMTPUTF8 = True
size = params.pop('SIZE', None)
if size:
if not size.isdigit():
@@ -546,11 +603,7 @@ class SMTPChannel(asynchat.async_chat):
if arg:
self.push('501 Syntax: RSET')
return
- # Resets the sender, recipients, and data, but not the greeting
- self.mailfrom = None
- self.rcpttos = []
- self.received_data = ''
- self.smtp_state = self.COMMAND
+ self._set_rset_state()
self.push('250 OK')
def smtp_DATA(self, arg):
@@ -577,13 +630,29 @@ class SMTPServer(asyncore.dispatcher):
channel_class = SMTPChannel
def __init__(self, localaddr, remoteaddr,
- data_size_limit=DATA_SIZE_DEFAULT, map=None):
+ data_size_limit=DATA_SIZE_DEFAULT, map=None,
+ enable_SMTPUTF8=False, decode_data=None):
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
asyncore.dispatcher.__init__(self, map=map)
try:
- self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
+ gai_results = socket.getaddrinfo(*localaddr,
+ type=socket.SOCK_STREAM)
+ self.create_socket(gai_results[0][0], gai_results[0][1])
# try to re-use a server port if possible
self.set_reuse_addr()
self.bind(localaddr)
@@ -598,8 +667,13 @@ class SMTPServer(asyncore.dispatcher):
def handle_accepted(self, conn, addr):
print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM)
- channel = self.channel_class(self, conn, addr, self.data_size_limit,
- self._map)
+ channel = self.channel_class(self,
+ conn,
+ addr,
+ self.data_size_limit,
+ self._map,
+ self.enable_SMTPUTF8,
+ self._decode_data)
# API for "doing something useful with the message"
def process_message(self, peer, mailfrom, rcpttos, data):
@@ -620,29 +694,63 @@ class SMTPServer(asyncore.dispatcher):
containing a `.' followed by other text has had the leading dot
removed.
- This function should return None, for a normal `250 Ok' response;
- otherwise it returns the desired response string in RFC 821 format.
+ This function should return None for a normal `250 Ok' response;
+ otherwise, it should return the desired response string in RFC 821
+ format.
+
+ """
+ raise NotImplementedError
+
+ # API for processing messeges needing Unicode support (RFC 6531, RFC 6532).
+ def process_smtputf8_message(self, peer, mailfrom, rcpttos, data):
+ """Same as ``process_message`` but for messages for which the client
+ has sent the SMTPUTF8 parameter with the MAIL command (see the
+ enable_SMTPUTF8 parameter of the constructor).
+
+ This function should return None for a normal `250 Ok' response;
+ otherwise, it should return the desired response string in RFC 6531
+ format.
"""
raise NotImplementedError
class DebuggingServer(SMTPServer):
- # Do something with the gathered message
- def process_message(self, peer, mailfrom, rcpttos, data):
+
+ def _print_message_content(self, peer, data):
inheaders = 1
- lines = data.split('\n')
- print('---------- MESSAGE FOLLOWS ----------')
+ lines = data.splitlines()
for line in lines:
# headers first
if inheaders and not line:
- print('X-Peer:', peer[0])
+ peerheader = 'X-Peer: ' + peer[0]
+ if not isinstance(data, str):
+ # decoded_data=false; make header match other binary output
+ peerheader = repr(peerheader.encode('utf-8'))
+ print(peerheader)
inheaders = 0
+ if not isinstance(data, str):
+ # Avoid spurious 'str on bytes instance' warning.
+ line = repr(line)
print(line)
+
+ def process_message(self, peer, mailfrom, rcpttos, data):
+ print('---------- MESSAGE FOLLOWS ----------')
+ self._print_message_content(peer, data)
+ print('------------ END MESSAGE ------------')
+
+ def process_smtputf8_message(self, peer, mailfrom, rcpttos, data):
+ print('----- SMTPUTF8 MESSAGE FOLLOWS ------')
+ self._print_message_content(peer, data)
print('------------ END MESSAGE ------------')
class PureProxy(SMTPServer):
+ def __init__(self, *args, **kwargs):
+ if 'enable_SMTPUTF8' in kwargs and kwargs['enable_SMTPUTF8']:
+ raise ValueError("PureProxy does not support SMTPUTF8.")
+ super(PureProxy, self).__init__(*args, **kwargs)
+
def process_message(self, peer, mailfrom, rcpttos, data):
lines = data.split('\n')
# Look for the last header
@@ -683,6 +791,11 @@ class PureProxy(SMTPServer):
class MailmanProxy(PureProxy):
+ def __init__(self, *args, **kwargs):
+ if 'enable_SMTPUTF8' in kwargs and kwargs['enable_SMTPUTF8']:
+ raise ValueError("MailmanProxy does not support SMTPUTF8.")
+ super(PureProxy, self).__init__(*args, **kwargs)
+
def process_message(self, peer, mailfrom, rcpttos, data):
from io import StringIO
from Mailman import Utils
@@ -761,17 +874,19 @@ class MailmanProxy(PureProxy):
class Options:
- setuid = 1
+ setuid = True
classname = 'PureProxy'
size_limit = None
+ enable_SMTPUTF8 = False
def parseargs():
global DEBUGSTREAM
try:
opts, args = getopt.getopt(
- sys.argv[1:], 'nVhc:s:d',
- ['class=', 'nosetuid', 'version', 'help', 'size=', 'debug'])
+ sys.argv[1:], 'nVhc:s:du',
+ ['class=', 'nosetuid', 'version', 'help', 'size=', 'debug',
+ 'smtputf8'])
except getopt.error as e:
usage(1, e)
@@ -783,11 +898,13 @@ def parseargs():
print(__version__)
sys.exit(0)
elif opt in ('-n', '--nosetuid'):
- options.setuid = 0
+ options.setuid = False
elif opt in ('-c', '--class'):
options.classname = arg
elif opt in ('-d', '--debug'):
DEBUGSTREAM = sys.stderr
+ elif opt in ('-u', '--smtputf8'):
+ options.enable_SMTPUTF8 = True
elif opt in ('-s', '--size'):
try:
int_size = int(arg)
@@ -842,7 +959,7 @@ if __name__ == '__main__':
class_ = getattr(mod, classname)
proxy = class_((options.localhost, options.localport),
(options.remotehost, options.remoteport),
- options.size_limit)
+ options.size_limit, enable_SMTPUTF8=options.enable_SMTPUTF8)
if options.setuid:
try:
import pwd
diff --git a/Lib/smtplib.py b/Lib/smtplib.py
index fa7f9e6..327a454 100755
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -571,12 +571,60 @@ class SMTP:
if not (200 <= code <= 299):
raise SMTPHeloError(code, resp)
+ def auth(self, mechanism, authobject):
+ """Authentication command - requires response processing.
+
+ 'mechanism' specifies which authentication mechanism is to
+ be used - the valid values are those listed in the 'auth'
+ element of 'esmtp_features'.
+
+ 'authobject' must be a callable object taking a single argument:
+
+ data = authobject(challenge)
+
+ It will be called to process the server's challenge response; the
+ challenge argument it is passed will be a bytes. It should return
+ bytes data that will be base64 encoded and sent to the server.
+ """
+
+ mechanism = mechanism.upper()
+ (code, resp) = self.docmd("AUTH", mechanism)
+ # Server replies with 334 (challenge) or 535 (not supported)
+ if code == 334:
+ challenge = base64.decodebytes(resp)
+ response = encode_base64(
+ authobject(challenge).encode('ascii'), eol='')
+ (code, resp) = self.docmd(response)
+ if code in (235, 503):
+ return (code, resp)
+ raise SMTPAuthenticationError(code, resp)
+
+ def auth_cram_md5(self, challenge):
+ """ Authobject to use with CRAM-MD5 authentication. Requires self.user
+ and self.password to be set."""
+ return self.user + " " + hmac.HMAC(
+ self.password.encode('ascii'), challenge, 'md5').hexdigest()
+
+ def auth_plain(self, challenge):
+ """ Authobject to use with PLAIN authentication. Requires self.user and
+ self.password to be set."""
+ return "\0%s\0%s" % (self.user, self.password)
+
+ def auth_login(self, challenge):
+ """ Authobject to use with LOGIN authentication. Requires self.user and
+ self.password to be set."""
+ (code, resp) = self.docmd(
+ encode_base64(self.user.encode('ascii'), eol=''))
+ if code == 334:
+ return self.password
+ raise SMTPAuthenticationError(code, resp)
+
def login(self, user, password):
"""Log in on an SMTP server that requires authentication.
The arguments are:
- - user: The user name to authenticate with.
- - password: The password for the authentication.
+ - user: The user name to authenticate with.
+ - password: The password for the authentication.
If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.
@@ -593,63 +641,40 @@ class SMTP:
found.
"""
- def encode_cram_md5(challenge, user, password):
- challenge = base64.decodebytes(challenge)
- response = user + " " + hmac.HMAC(password.encode('ascii'),
- challenge, 'md5').hexdigest()
- return encode_base64(response.encode('ascii'), eol='')
-
- def encode_plain(user, password):
- s = "\0%s\0%s" % (user, password)
- return encode_base64(s.encode('ascii'), eol='')
-
- AUTH_PLAIN = "PLAIN"
- AUTH_CRAM_MD5 = "CRAM-MD5"
- AUTH_LOGIN = "LOGIN"
-
self.ehlo_or_helo_if_needed()
-
if not self.has_extn("auth"):
raise SMTPException("SMTP AUTH extension not supported by server.")
# Authentication methods the server claims to support
advertised_authlist = self.esmtp_features["auth"].split()
- # List of authentication methods we support: from preferred to
- # less preferred methods. Except for the purpose of testing the weaker
- # ones, we prefer stronger methods like CRAM-MD5:
- preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]
+ # Authentication methods we can handle in our preferred order:
+ preferred_auths = ['CRAM-MD5', 'PLAIN', 'LOGIN']
- # We try the authentication methods the server advertises, but only the
- # ones *we* support. And in our preferred order.
- authlist = [auth for auth in preferred_auths if auth in advertised_authlist]
+ # We try the supported authentications in our preferred order, if
+ # the server supports them.
+ authlist = [auth for auth in preferred_auths
+ if auth in advertised_authlist]
if not authlist:
raise SMTPException("No suitable authentication method found.")
# Some servers advertise authentication methods they don't really
# support, so if authentication fails, we continue until we've tried
# all methods.
+ self.user, self.password = user, password
for authmethod in authlist:
- if authmethod == AUTH_CRAM_MD5:
- (code, resp) = self.docmd("AUTH", AUTH_CRAM_MD5)
- if code == 334:
- (code, resp) = self.docmd(encode_cram_md5(resp, user, password))
- elif authmethod == AUTH_PLAIN:
- (code, resp) = self.docmd("AUTH",
- AUTH_PLAIN + " " + encode_plain(user, password))
- elif authmethod == AUTH_LOGIN:
- (code, resp) = self.docmd("AUTH",
- "%s %s" % (AUTH_LOGIN, encode_base64(user.encode('ascii'), eol='')))
- if code == 334:
- (code, resp) = self.docmd(encode_base64(password.encode('ascii'), eol=''))
-
- # 235 == 'Authentication successful'
- # 503 == 'Error: already authenticated'
- if code in (235, 503):
- return (code, resp)
-
- # We could not login sucessfully. Return result of last attempt.
- raise SMTPAuthenticationError(code, resp)
+ method_name = 'auth_' + authmethod.lower().replace('-', '_')
+ try:
+ (code, resp) = self.auth(authmethod, getattr(self, method_name))
+ # 235 == 'Authentication successful'
+ # 503 == 'Error: already authenticated'
+ if code in (235, 503):
+ return (code, resp)
+ except SMTPAuthenticationError as e:
+ last_exception = e
+
+ # We could not login successfully. Return result of last attempt.
+ raise last_exception
def starttls(self, keyfile=None, certfile=None, context=None):
"""Puts the connection to the SMTP server into TLS mode.
diff --git a/Lib/sndhdr.py b/Lib/sndhdr.py
index 240e507..e5901ec 100644
--- a/Lib/sndhdr.py
+++ b/Lib/sndhdr.py
@@ -32,6 +32,11 @@ explicitly given directories.
__all__ = ['what', 'whathdr']
+from collections import namedtuple
+
+SndHeaders = namedtuple('SndHeaders',
+ 'filetype framerate nchannels nframes sampwidth')
+
def what(filename):
"""Guess the type of a sound file."""
res = whathdr(filename)
@@ -45,7 +50,7 @@ def whathdr(filename):
for tf in tests:
res = tf(h, f)
if res:
- return res
+ return SndHeaders(*res)
return None
diff --git a/Lib/socket.py b/Lib/socket.py
index 8efd760..9c39c69 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -49,7 +49,7 @@ the setsockopt() and getsockopt() methods.
import _socket
from _socket import *
-import os, sys, io
+import os, sys, io, selectors
from enum import IntEnum
try:
@@ -79,6 +79,11 @@ SocketKind = IntEnum('SocketKind',
if name.isupper() and name.startswith('SOCK_')})
globals().update(SocketKind.__members__)
+
+_LOCALHOST = '127.0.0.1'
+_LOCALHOST_V6 = '::1'
+
+
def _intenum_converter(value, enum_klass):
"""Convert a numeric family value to an IntEnum member.
@@ -112,6 +117,9 @@ if sys.platform.lower().startswith("win"):
__all__.append("errorTab")
+class _GiveupOnSendfile(Exception): pass
+
+
class socket(_socket.socket):
"""A subclass of _socket.socket adding the makefile() method."""
@@ -141,7 +149,7 @@ class socket(_socket.socket):
closed = getattr(self, '_closed', False)
s = "<%s.%s%s fd=%i, family=%s, type=%s, proto=%i" \
% (self.__class__.__module__,
- self.__class__.__name__,
+ self.__class__.__qualname__,
" [closed]" if closed else "",
self.fileno(),
self.family,
@@ -235,6 +243,149 @@ class socket(_socket.socket):
text.mode = mode
return text
+ if hasattr(os, 'sendfile'):
+
+ def _sendfile_use_sendfile(self, file, offset=0, count=None):
+ self._check_sendfile_params(file, offset, count)
+ sockno = self.fileno()
+ try:
+ fileno = file.fileno()
+ except (AttributeError, io.UnsupportedOperation) as err:
+ raise _GiveupOnSendfile(err) # not a regular file
+ try:
+ fsize = os.fstat(fileno).st_size
+ except OSError:
+ raise _GiveupOnSendfile(err) # not a regular file
+ if not fsize:
+ return 0 # empty file
+ blocksize = fsize if not count else count
+
+ timeout = self.gettimeout()
+ if timeout == 0:
+ raise ValueError("non-blocking sockets are not supported")
+ # poll/select have the advantage of not requiring any
+ # extra file descriptor, contrarily to epoll/kqueue
+ # (also, they require a single syscall).
+ if hasattr(selectors, 'PollSelector'):
+ selector = selectors.PollSelector()
+ else:
+ selector = selectors.SelectSelector()
+ selector.register(sockno, selectors.EVENT_WRITE)
+
+ total_sent = 0
+ # localize variable access to minimize overhead
+ selector_select = selector.select
+ os_sendfile = os.sendfile
+ try:
+ while True:
+ if timeout and not selector_select(timeout):
+ raise _socket.timeout('timed out')
+ if count:
+ blocksize = count - total_sent
+ if blocksize <= 0:
+ break
+ try:
+ sent = os_sendfile(sockno, fileno, offset, blocksize)
+ except BlockingIOError:
+ if not timeout:
+ # Block until the socket is ready to send some
+ # data; avoids hogging CPU resources.
+ selector_select()
+ continue
+ except OSError as err:
+ if total_sent == 0:
+ # We can get here for different reasons, the main
+ # one being 'file' is not a regular mmap(2)-like
+ # file, in which case we'll fall back on using
+ # plain send().
+ raise _GiveupOnSendfile(err)
+ raise err from None
+ else:
+ if sent == 0:
+ break # EOF
+ offset += sent
+ total_sent += sent
+ return total_sent
+ finally:
+ if total_sent > 0 and hasattr(file, 'seek'):
+ file.seek(offset)
+ else:
+ def _sendfile_use_sendfile(self, file, offset=0, count=None):
+ raise _GiveupOnSendfile(
+ "os.sendfile() not available on this platform")
+
+ def _sendfile_use_send(self, file, offset=0, count=None):
+ self._check_sendfile_params(file, offset, count)
+ if self.gettimeout() == 0:
+ raise ValueError("non-blocking sockets are not supported")
+ if offset:
+ file.seek(offset)
+ blocksize = min(count, 8192) if count else 8192
+ total_sent = 0
+ # localize variable access to minimize overhead
+ file_read = file.read
+ sock_send = self.send
+ try:
+ while True:
+ if count:
+ blocksize = min(count - total_sent, blocksize)
+ if blocksize <= 0:
+ break
+ data = memoryview(file_read(blocksize))
+ if not data:
+ break # EOF
+ while True:
+ try:
+ sent = sock_send(data)
+ except BlockingIOError:
+ continue
+ else:
+ total_sent += sent
+ if sent < len(data):
+ data = data[sent:]
+ else:
+ break
+ return total_sent
+ finally:
+ if total_sent > 0 and hasattr(file, 'seek'):
+ file.seek(offset + total_sent)
+
+ def _check_sendfile_params(self, file, offset, count):
+ if 'b' not in getattr(file, 'mode', 'b'):
+ raise ValueError("file should be opened in binary mode")
+ if not self.type & SOCK_STREAM:
+ raise ValueError("only SOCK_STREAM type sockets are supported")
+ if count is not None:
+ if not isinstance(count, int):
+ raise TypeError(
+ "count must be a positive integer (got {!r})".format(count))
+ if count <= 0:
+ raise ValueError(
+ "count must be a positive integer (got {!r})".format(count))
+
+ def sendfile(self, file, offset=0, count=None):
+ """sendfile(file[, offset[, count]]) -> sent
+
+ Send a file until EOF is reached by using high-performance
+ os.sendfile() and return the total number of bytes which
+ were sent.
+ *file* must be a regular file object opened in binary mode.
+ If os.sendfile() is not available (e.g. Windows) or file is
+ not a regular file socket.send() will be used instead.
+ *offset* tells from where to start reading the file.
+ If specified, *count* is the total number of bytes to transmit
+ as opposed to sending the file until EOF is reached.
+ File position is updated on return or also in case of error in
+ which case file.tell() can be used to figure out the number of
+ bytes which were sent.
+ The socket must be of SOCK_STREAM type.
+ Non-blocking sockets are not supported.
+ """
+ try:
+ return self._sendfile_use_sendfile(file, offset, count)
+ except _GiveupOnSendfile:
+ return self._sendfile_use_send(file, offset, count)
+
def _decref_socketios(self):
if self._io_refs > 0:
self._io_refs -= 1
@@ -325,6 +476,52 @@ if hasattr(_socket, "socketpair"):
b = socket(family, type, proto, b.detach())
return a, b
+else:
+
+ # Origin: https://gist.github.com/4325783, by Geert Jansen. Public domain.
+ def socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
+ if family == AF_INET:
+ host = _LOCALHOST
+ elif family == AF_INET6:
+ host = _LOCALHOST_V6
+ else:
+ raise ValueError("Only AF_INET and AF_INET6 socket address families "
+ "are supported")
+ if type != SOCK_STREAM:
+ raise ValueError("Only SOCK_STREAM socket type is supported")
+ if proto != 0:
+ raise ValueError("Only protocol zero is supported")
+
+ # We create a connected TCP socket. Note the trick with
+ # setblocking(False) that prevents us from having to create a thread.
+ lsock = socket(family, type, proto)
+ try:
+ lsock.bind((host, 0))
+ lsock.listen()
+ # On IPv6, ignore flow_info and scope_id
+ addr, port = lsock.getsockname()[:2]
+ csock = socket(family, type, proto)
+ try:
+ csock.setblocking(False)
+ try:
+ csock.connect((addr, port))
+ except (BlockingIOError, InterruptedError):
+ pass
+ csock.setblocking(True)
+ ssock, _ = lsock.accept()
+ except:
+ csock.close()
+ raise
+ finally:
+ lsock.close()
+ return (ssock, csock)
+
+socketpair.__doc__ = """socketpair([family[, type[, proto]]]) -> (socket object, socket object)
+Create a pair of socket objects from the sockets returned by the platform
+socketpair() function.
+The arguments are the same as for socket() except the default family is AF_UNIX
+if defined on the platform; otherwise, the default is AF_INET.
+"""
_blocking_errnos = { EAGAIN, EWOULDBLOCK }
@@ -375,8 +572,6 @@ class SocketIO(io.RawIOBase):
except timeout:
self._timeout_occurred = True
raise
- except InterruptedError:
- continue
except error as e:
if e.args[0] in _blocking_errnos:
return None
diff --git a/Lib/socketserver.py b/Lib/socketserver.py
index 5cb89be..0ce8e81 100644
--- a/Lib/socketserver.py
+++ b/Lib/socketserver.py
@@ -94,7 +94,7 @@ handle() method.
Another approach to handling multiple simultaneous requests in an
environment that supports neither threads nor fork (or where these are
too expensive or inappropriate for the service) is to maintain an
-explicit table of partially finished requests and to use select() to
+explicit table of partially finished requests and to use a selector to
decide which request to work on next (or whether to handle a new
incoming request). This is particularly important for stream services
where each client can potentially be connected for a long time (if
@@ -104,7 +104,6 @@ Future work:
- Standard classes for Sun RPC (which uses either UDP or TCP)
- Standard mix-in classes to implement various authentication
and encryption schemes
-- Standard framework for select-based multiplexing
XXX Open problems:
- What to do with out-of-band data?
@@ -130,13 +129,14 @@ __version__ = "0.4"
import socket
-import select
+import selectors
import os
import errno
try:
import threading
except ImportError:
import dummy_threading as threading
+from time import monotonic as time
__all__ = ["BaseServer", "TCPServer", "UDPServer", "ForkingUDPServer",
"ForkingTCPServer", "ThreadingUDPServer", "ThreadingTCPServer",
@@ -147,14 +147,13 @@ if hasattr(socket, "AF_UNIX"):
"ThreadingUnixStreamServer",
"ThreadingUnixDatagramServer"])
-def _eintr_retry(func, *args):
- """restart a system call interrupted by EINTR"""
- while True:
- try:
- return func(*args)
- except OSError as e:
- if e.errno != errno.EINTR:
- raise
+# poll/select have the advantage of not requiring any extra file descriptor,
+# contrarily to epoll/kqueue (also, they require a single syscall).
+if hasattr(selectors, 'PollSelector'):
+ _ServerSelector = selectors.PollSelector
+else:
+ _ServerSelector = selectors.SelectSelector
+
class BaseServer:
@@ -166,7 +165,7 @@ class BaseServer:
- serve_forever(poll_interval=0.5)
- shutdown()
- handle_request() # if you do not use serve_forever()
- - fileno() -> int # for select()
+ - fileno() -> int # for selector
Methods that may be overridden:
@@ -227,17 +226,19 @@ class BaseServer:
"""
self.__is_shut_down.clear()
try:
- while not self.__shutdown_request:
- # XXX: Consider using another file descriptor or
- # connecting to the socket to wake this up instead of
- # polling. Polling reduces our responsiveness to a
- # shutdown request and wastes cpu at all other times.
- r, w, e = _eintr_retry(select.select, [self], [], [],
- poll_interval)
- if self in r:
- self._handle_request_noblock()
-
- self.service_actions()
+ # XXX: Consider using another file descriptor or connecting to the
+ # socket to wake this up instead of polling. Polling reduces our
+ # responsiveness to a shutdown request and wastes cpu at all other
+ # times.
+ with _ServerSelector() as selector:
+ selector.register(self, selectors.EVENT_READ)
+
+ while not self.__shutdown_request:
+ ready = selector.select(poll_interval)
+ if ready:
+ self._handle_request_noblock()
+
+ self.service_actions()
finally:
self.__shutdown_request = False
self.__is_shut_down.set()
@@ -260,16 +261,16 @@ class BaseServer:
"""
pass
- # The distinction between handling, getting, processing and
- # finishing a request is fairly arbitrary. Remember:
+ # The distinction between handling, getting, processing and finishing a
+ # request is fairly arbitrary. Remember:
#
- # - handle_request() is the top-level call. It calls
- # select, get_request(), verify_request() and process_request()
+ # - handle_request() is the top-level call. It calls selector.select(),
+ # get_request(), verify_request() and process_request()
# - get_request() is different for stream or datagram sockets
- # - process_request() is the place that may fork a new process
- # or create a new thread to finish the request
- # - finish_request() instantiates the request handler class;
- # this constructor will handle the request all by itself
+ # - process_request() is the place that may fork a new process or create a
+ # new thread to finish the request
+ # - finish_request() instantiates the request handler class; this
+ # constructor will handle the request all by itself
def handle_request(self):
"""Handle one request, possibly blocking.
@@ -283,18 +284,30 @@ class BaseServer:
timeout = self.timeout
elif self.timeout is not None:
timeout = min(timeout, self.timeout)
- fd_sets = _eintr_retry(select.select, [self], [], [], timeout)
- if not fd_sets[0]:
- self.handle_timeout()
- return
- self._handle_request_noblock()
+ if timeout is not None:
+ deadline = time() + timeout
+
+ # Wait until a request arrives or the timeout expires - the loop is
+ # necessary to accommodate early wakeups due to EINTR.
+ with _ServerSelector() as selector:
+ selector.register(self, selectors.EVENT_READ)
+
+ while True:
+ ready = selector.select(timeout)
+ if ready:
+ return self._handle_request_noblock()
+ else:
+ if timeout is not None:
+ timeout = deadline - time()
+ if timeout < 0:
+ return self.handle_timeout()
def _handle_request_noblock(self):
"""Handle one request, without blocking.
- I assume that select.select has returned that the socket is
- readable before this function was called, so there should be
- no risk of blocking in get_request().
+ I assume that selector.select() has returned that the socket is
+ readable before this function was called, so there should be no risk of
+ blocking in get_request().
"""
try:
request, client_address = self.get_request()
@@ -377,7 +390,7 @@ class TCPServer(BaseServer):
- serve_forever(poll_interval=0.5)
- shutdown()
- handle_request() # if you don't use serve_forever()
- - fileno() -> int # for select()
+ - fileno() -> int # for selector
Methods that may be overridden:
@@ -463,7 +476,7 @@ class TCPServer(BaseServer):
def fileno(self):
"""Return socket file number.
- Interface required by select().
+ Interface required by selector.
"""
return self.socket.fileno()
@@ -540,8 +553,6 @@ class ForkingMixIn:
try:
pid, _ = os.waitpid(-1, 0)
self.active_children.discard(pid)
- except InterruptedError:
- pass
except ChildProcessError:
# we don't have any children, we're done
self.active_children.clear()
diff --git a/Lib/sre_compile.py b/Lib/sre_compile.py
index 550ea15..30a5fae 100644
--- a/Lib/sre_compile.py
+++ b/Lib/sre_compile.py
@@ -13,19 +13,13 @@
import _sre
import sre_parse
from sre_constants import *
-from _sre import MAXREPEAT
assert _sre.MAGIC == MAGIC, "SRE module mismatch"
-if _sre.CODESIZE == 2:
- MAXCODE = 65535
-else:
- MAXCODE = 0xFFFFFFFF
-
-_LITERAL_CODES = set([LITERAL, NOT_LITERAL])
-_REPEATING_CODES = set([REPEAT, MIN_REPEAT, MAX_REPEAT])
-_SUCCESS_CODES = set([SUCCESS, FAILURE])
-_ASSERT_CODES = set([ASSERT, ASSERT_NOT])
+_LITERAL_CODES = {LITERAL, NOT_LITERAL}
+_REPEATING_CODES = {REPEAT, MIN_REPEAT, MAX_REPEAT}
+_SUCCESS_CODES = {SUCCESS, FAILURE}
+_ASSERT_CODES = {ASSERT, ASSERT_NOT}
# Sets of lowercase characters which have the same uppercase.
_equivalences = (
@@ -86,75 +80,75 @@ def _compile(code, pattern, flags):
if flags & SRE_FLAG_IGNORECASE:
lo = _sre.getlower(av, flags)
if fixes and lo in fixes:
- emit(OPCODES[IN_IGNORE])
+ emit(IN_IGNORE)
skip = _len(code); emit(0)
if op is NOT_LITERAL:
- emit(OPCODES[NEGATE])
+ emit(NEGATE)
for k in (lo,) + fixes[lo]:
- emit(OPCODES[LITERAL])
+ emit(LITERAL)
emit(k)
- emit(OPCODES[FAILURE])
+ emit(FAILURE)
code[skip] = _len(code) - skip
else:
- emit(OPCODES[OP_IGNORE[op]])
+ emit(OP_IGNORE[op])
emit(lo)
else:
- emit(OPCODES[op])
+ emit(op)
emit(av)
elif op is IN:
if flags & SRE_FLAG_IGNORECASE:
- emit(OPCODES[OP_IGNORE[op]])
+ emit(OP_IGNORE[op])
def fixup(literal, flags=flags):
return _sre.getlower(literal, flags)
else:
- emit(OPCODES[op])
+ emit(op)
fixup = None
skip = _len(code); emit(0)
_compile_charset(av, flags, code, fixup, fixes)
code[skip] = _len(code) - skip
elif op is ANY:
if flags & SRE_FLAG_DOTALL:
- emit(OPCODES[ANY_ALL])
+ emit(ANY_ALL)
else:
- emit(OPCODES[ANY])
+ emit(ANY)
elif op in REPEATING_CODES:
if flags & SRE_FLAG_TEMPLATE:
raise error("internal: unsupported template operator")
elif _simple(av) and op is not REPEAT:
if op is MAX_REPEAT:
- emit(OPCODES[REPEAT_ONE])
+ emit(REPEAT_ONE)
else:
- emit(OPCODES[MIN_REPEAT_ONE])
+ emit(MIN_REPEAT_ONE)
skip = _len(code); emit(0)
emit(av[0])
emit(av[1])
_compile(code, av[2], flags)
- emit(OPCODES[SUCCESS])
+ emit(SUCCESS)
code[skip] = _len(code) - skip
else:
- emit(OPCODES[REPEAT])
+ emit(REPEAT)
skip = _len(code); emit(0)
emit(av[0])
emit(av[1])
_compile(code, av[2], flags)
code[skip] = _len(code) - skip
if op is MAX_REPEAT:
- emit(OPCODES[MAX_UNTIL])
+ emit(MAX_UNTIL)
else:
- emit(OPCODES[MIN_UNTIL])
+ emit(MIN_UNTIL)
elif op is SUBPATTERN:
if av[0]:
- emit(OPCODES[MARK])
+ emit(MARK)
emit((av[0]-1)*2)
# _compile_info(code, av[1], flags)
_compile(code, av[1], flags)
if av[0]:
- emit(OPCODES[MARK])
+ emit(MARK)
emit((av[0]-1)*2+1)
elif op in SUCCESS_CODES:
- emit(OPCODES[op])
+ emit(op)
elif op in ASSERT_CODES:
- emit(OPCODES[op])
+ emit(op)
skip = _len(code); emit(0)
if av[0] >= 0:
emit(0) # look ahead
@@ -164,57 +158,57 @@ def _compile(code, pattern, flags):
raise error("look-behind requires fixed-width pattern")
emit(lo) # look behind
_compile(code, av[1], flags)
- emit(OPCODES[SUCCESS])
+ emit(SUCCESS)
code[skip] = _len(code) - skip
elif op is CALL:
- emit(OPCODES[op])
+ emit(op)
skip = _len(code); emit(0)
_compile(code, av, flags)
- emit(OPCODES[SUCCESS])
+ emit(SUCCESS)
code[skip] = _len(code) - skip
elif op is AT:
- emit(OPCODES[op])
+ emit(op)
if flags & SRE_FLAG_MULTILINE:
av = AT_MULTILINE.get(av, av)
if flags & SRE_FLAG_LOCALE:
av = AT_LOCALE.get(av, av)
elif flags & SRE_FLAG_UNICODE:
av = AT_UNICODE.get(av, av)
- emit(ATCODES[av])
+ emit(av)
elif op is BRANCH:
- emit(OPCODES[op])
+ emit(op)
tail = []
tailappend = tail.append
for av in av[1]:
skip = _len(code); emit(0)
# _compile_info(code, av, flags)
_compile(code, av, flags)
- emit(OPCODES[JUMP])
+ emit(JUMP)
tailappend(_len(code)); emit(0)
code[skip] = _len(code) - skip
- emit(0) # end of branch
+ emit(FAILURE) # end of branch
for tail in tail:
code[tail] = _len(code) - tail
elif op is CATEGORY:
- emit(OPCODES[op])
+ emit(op)
if flags & SRE_FLAG_LOCALE:
av = CH_LOCALE[av]
elif flags & SRE_FLAG_UNICODE:
av = CH_UNICODE[av]
- emit(CHCODES[av])
+ emit(av)
elif op is GROUPREF:
if flags & SRE_FLAG_IGNORECASE:
- emit(OPCODES[OP_IGNORE[op]])
+ emit(OP_IGNORE[op])
else:
- emit(OPCODES[op])
+ emit(op)
emit(av-1)
elif op is GROUPREF_EXISTS:
- emit(OPCODES[op])
+ emit(op)
emit(av[0]-1)
skipyes = _len(code); emit(0)
_compile(code, av[1], flags)
if av[2]:
- emit(OPCODES[JUMP])
+ emit(JUMP)
skipno = _len(code); emit(0)
code[skipyes] = _len(code) - skipyes + 1
_compile(code, av[2], flags)
@@ -227,14 +221,13 @@ def _compile(code, pattern, flags):
def _compile_charset(charset, flags, code, fixup=None, fixes=None):
# compile charset subprogram
emit = code.append
- for op, av in _optimize_charset(charset, fixup, fixes,
- flags & SRE_FLAG_UNICODE):
- emit(OPCODES[op])
+ for op, av in _optimize_charset(charset, fixup, fixes):
+ emit(op)
if op is NEGATE:
pass
elif op is LITERAL:
emit(av)
- elif op is RANGE:
+ elif op is RANGE or op is RANGE_IGNORE:
emit(av[0])
emit(av[1])
elif op is CHARSET:
@@ -243,16 +236,16 @@ def _compile_charset(charset, flags, code, fixup=None, fixes=None):
code.extend(av)
elif op is CATEGORY:
if flags & SRE_FLAG_LOCALE:
- emit(CHCODES[CH_LOCALE[av]])
+ emit(CH_LOCALE[av])
elif flags & SRE_FLAG_UNICODE:
- emit(CHCODES[CH_UNICODE[av]])
+ emit(CH_UNICODE[av])
else:
- emit(CHCODES[av])
+ emit(av)
else:
raise error("internal: unsupported set operator")
- emit(OPCODES[FAILURE])
+ emit(FAILURE)
-def _optimize_charset(charset, fixup, fixes, isunicode):
+def _optimize_charset(charset, fixup, fixes):
# internal: optimize character set
out = []
tail = []
@@ -262,10 +255,10 @@ def _optimize_charset(charset, fixup, fixes, isunicode):
try:
if op is LITERAL:
if fixup:
- i = fixup(av)
- charmap[i] = 1
- if fixes and i in fixes:
- for k in fixes[i]:
+ lo = fixup(av)
+ charmap[lo] = 1
+ if fixes and lo in fixes:
+ for k in fixes[lo]:
charmap[k] = 1
else:
charmap[av] = 1
@@ -291,21 +284,13 @@ def _optimize_charset(charset, fixup, fixes, isunicode):
# character set contains non-UCS1 character codes
charmap += b'\0' * 0xff00
continue
- # character set contains non-BMP character codes
- if fixup and isunicode and op is RANGE:
- lo, hi = av
- ranges = [av]
- # There are only two ranges of cased astral characters:
- # 10400-1044F (Deseret) and 118A0-118DF (Warang Citi).
- _fixup_range(max(0x10000, lo), min(0x11fff, hi),
- ranges, fixup)
- for lo, hi in ranges:
- if lo == hi:
- tail.append((LITERAL, hi))
- else:
- tail.append((RANGE, (lo, hi)))
- else:
- tail.append((op, av))
+ # Character set contains non-BMP character codes.
+ # There are only two ranges of cased non-BMP characters:
+ # 10400-1044F (Deseret) and 118A0-118DF (Warang Citi),
+ # and for both ranges RANGE_IGNORE works.
+ if fixup and op is RANGE:
+ op = RANGE_IGNORE
+ tail.append((op, av))
break
# compress character map
@@ -383,25 +368,8 @@ def _optimize_charset(charset, fixup, fixes, isunicode):
out += tail
return out
-def _fixup_range(lo, hi, ranges, fixup):
- for i in map(fixup, range(lo, hi+1)):
- for k, (lo, hi) in enumerate(ranges):
- if i < lo:
- if l == lo - 1:
- ranges[k] = (i, hi)
- else:
- ranges.insert(k, (i, i))
- break
- elif i > hi:
- if i == hi + 1:
- ranges[k] = (lo, i)
- break
- else:
- break
- else:
- ranges.append((i, i))
-
_CODEBITS = _sre.CODESIZE * 8
+MAXCODE = (1 << _CODEBITS) - 1
_BITS_TRANS = b'0' + b'1' * 255
def _mk_bitmap(bits, _CODEBITS=_CODEBITS, _int=int):
s = bits.translate(_BITS_TRANS)[::-1]
@@ -446,8 +414,11 @@ def _compile_info(code, pattern, flags):
# 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:
- return # not worth it
+ code.extend([INFO, 4, 0, lo, hi])
+ return
# look for a literal prefix
prefix = []
prefixappend = prefix.append
@@ -505,21 +476,21 @@ def _compile_info(code, pattern, flags):
elif op is IN:
charset = av
## if prefix:
-## print "*** PREFIX", prefix, prefix_skip
+## print("*** PREFIX", prefix, prefix_skip)
## if charset:
-## print "*** CHARSET", charset
+## print("*** CHARSET", charset)
# add an info block
emit = code.append
- emit(OPCODES[INFO])
+ emit(INFO)
skip = len(code); emit(0)
# literal flag
mask = 0
if prefix:
mask = SRE_INFO_PREFIX
if len(prefix) == prefix_skip == len(pattern.data):
- mask = mask + SRE_INFO_LITERAL
+ mask = mask | SRE_INFO_LITERAL
elif charset:
- mask = mask + SRE_INFO_CHARSET
+ mask = mask | SRE_INFO_CHARSET
emit(mask)
# pattern length
if lo < MAXCODE:
@@ -527,10 +498,7 @@ def _compile_info(code, pattern, flags):
else:
emit(MAXCODE)
prefix = prefix[:MAXCODE]
- if hi < MAXCODE:
- emit(hi)
- else:
- emit(0)
+ emit(min(hi, MAXCODE))
# add literal prefix
if prefix:
emit(len(prefix)) # length
@@ -556,7 +524,7 @@ def _code(p, flags):
# compile the pattern
_compile(code, p.data, flags)
- code.append(OPCODES[SUCCESS])
+ code.append(SUCCESS)
return code
@@ -571,13 +539,7 @@ def compile(p, flags=0):
code = _code(p, flags)
- # print code
-
- # XXX: <fl> get rid of this limitation!
- if p.pattern.groups > 100:
- raise AssertionError(
- "sorry, but this version only supports 100 named groups"
- )
+ # print(code)
# map in either direction
groupindex = p.pattern.groupdict
diff --git a/Lib/sre_constants.py b/Lib/sre_constants.py
index 23e3516..8b6bbfa 100644
--- a/Lib/sre_constants.py
+++ b/Lib/sre_constants.py
@@ -13,153 +13,115 @@
# update when constants are added or removed
-MAGIC = 20031017
+MAGIC = 20140917
-from _sre import MAXREPEAT
+from _sre import MAXREPEAT, MAXGROUPS
# SRE standard exception (access as sre.error)
# should this really be here?
class error(Exception):
- pass
+ def __init__(self, msg, pattern=None, pos=None):
+ self.msg = msg
+ self.pattern = pattern
+ self.pos = pos
+ if pattern is not None and pos is not None:
+ msg = '%s at position %d' % (msg, pos)
+ if isinstance(pattern, str):
+ newline = '\n'
+ else:
+ newline = b'\n'
+ self.lineno = pattern.count(newline, 0, pos) + 1
+ self.colno = pos - pattern.rfind(newline, 0, pos)
+ if newline in pattern:
+ msg = '%s (line %d, column %d)' % (msg, self.lineno, self.colno)
+ else:
+ self.lineno = self.colno = None
+ super().__init__(msg)
+
+
+class _NamedIntConstant(int):
+ def __new__(cls, value, name):
+ self = super(_NamedIntConstant, cls).__new__(cls, value)
+ self.name = name
+ return self
+
+ def __str__(self):
+ return self.name
+
+ __repr__ = __str__
+
+MAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT')
+
+def _makecodes(names):
+ names = names.strip().split()
+ items = [_NamedIntConstant(i, name) for i, name in enumerate(names)]
+ globals().update({item.name: item for item in items})
+ return items
# operators
+# failure=0 success=1 (just because it looks better that way :-)
+OPCODES = _makecodes("""
+ FAILURE SUCCESS
+
+ ANY ANY_ALL
+ ASSERT ASSERT_NOT
+ AT
+ BRANCH
+ CALL
+ CATEGORY
+ CHARSET BIGCHARSET
+ GROUPREF GROUPREF_EXISTS GROUPREF_IGNORE
+ IN IN_IGNORE
+ INFO
+ JUMP
+ LITERAL LITERAL_IGNORE
+ MARK
+ MAX_UNTIL
+ MIN_UNTIL
+ NOT_LITERAL NOT_LITERAL_IGNORE
+ NEGATE
+ RANGE
+ REPEAT
+ REPEAT_ONE
+ SUBPATTERN
+ MIN_REPEAT_ONE
+ RANGE_IGNORE
-FAILURE = "failure"
-SUCCESS = "success"
-
-ANY = "any"
-ANY_ALL = "any_all"
-ASSERT = "assert"
-ASSERT_NOT = "assert_not"
-AT = "at"
-BIGCHARSET = "bigcharset"
-BRANCH = "branch"
-CALL = "call"
-CATEGORY = "category"
-CHARSET = "charset"
-GROUPREF = "groupref"
-GROUPREF_IGNORE = "groupref_ignore"
-GROUPREF_EXISTS = "groupref_exists"
-IN = "in"
-IN_IGNORE = "in_ignore"
-INFO = "info"
-JUMP = "jump"
-LITERAL = "literal"
-LITERAL_IGNORE = "literal_ignore"
-MARK = "mark"
-MAX_REPEAT = "max_repeat"
-MAX_UNTIL = "max_until"
-MIN_REPEAT = "min_repeat"
-MIN_UNTIL = "min_until"
-NEGATE = "negate"
-NOT_LITERAL = "not_literal"
-NOT_LITERAL_IGNORE = "not_literal_ignore"
-RANGE = "range"
-REPEAT = "repeat"
-REPEAT_ONE = "repeat_one"
-SUBPATTERN = "subpattern"
-MIN_REPEAT_ONE = "min_repeat_one"
+ MIN_REPEAT MAX_REPEAT
+""")
+del OPCODES[-2:] # remove MIN_REPEAT and MAX_REPEAT
# positions
-AT_BEGINNING = "at_beginning"
-AT_BEGINNING_LINE = "at_beginning_line"
-AT_BEGINNING_STRING = "at_beginning_string"
-AT_BOUNDARY = "at_boundary"
-AT_NON_BOUNDARY = "at_non_boundary"
-AT_END = "at_end"
-AT_END_LINE = "at_end_line"
-AT_END_STRING = "at_end_string"
-AT_LOC_BOUNDARY = "at_loc_boundary"
-AT_LOC_NON_BOUNDARY = "at_loc_non_boundary"
-AT_UNI_BOUNDARY = "at_uni_boundary"
-AT_UNI_NON_BOUNDARY = "at_uni_non_boundary"
+ATCODES = _makecodes("""
+ AT_BEGINNING AT_BEGINNING_LINE AT_BEGINNING_STRING
+ AT_BOUNDARY AT_NON_BOUNDARY
+ AT_END AT_END_LINE AT_END_STRING
+ AT_LOC_BOUNDARY AT_LOC_NON_BOUNDARY
+ AT_UNI_BOUNDARY AT_UNI_NON_BOUNDARY
+""")
# categories
-CATEGORY_DIGIT = "category_digit"
-CATEGORY_NOT_DIGIT = "category_not_digit"
-CATEGORY_SPACE = "category_space"
-CATEGORY_NOT_SPACE = "category_not_space"
-CATEGORY_WORD = "category_word"
-CATEGORY_NOT_WORD = "category_not_word"
-CATEGORY_LINEBREAK = "category_linebreak"
-CATEGORY_NOT_LINEBREAK = "category_not_linebreak"
-CATEGORY_LOC_WORD = "category_loc_word"
-CATEGORY_LOC_NOT_WORD = "category_loc_not_word"
-CATEGORY_UNI_DIGIT = "category_uni_digit"
-CATEGORY_UNI_NOT_DIGIT = "category_uni_not_digit"
-CATEGORY_UNI_SPACE = "category_uni_space"
-CATEGORY_UNI_NOT_SPACE = "category_uni_not_space"
-CATEGORY_UNI_WORD = "category_uni_word"
-CATEGORY_UNI_NOT_WORD = "category_uni_not_word"
-CATEGORY_UNI_LINEBREAK = "category_uni_linebreak"
-CATEGORY_UNI_NOT_LINEBREAK = "category_uni_not_linebreak"
-
-OPCODES = [
-
- # failure=0 success=1 (just because it looks better that way :-)
- FAILURE, SUCCESS,
-
- ANY, ANY_ALL,
- ASSERT, ASSERT_NOT,
- AT,
- BRANCH,
- CALL,
- CATEGORY,
- CHARSET, BIGCHARSET,
- GROUPREF, GROUPREF_EXISTS, GROUPREF_IGNORE,
- IN, IN_IGNORE,
- INFO,
- JUMP,
- LITERAL, LITERAL_IGNORE,
- MARK,
- MAX_UNTIL,
- MIN_UNTIL,
- NOT_LITERAL, NOT_LITERAL_IGNORE,
- NEGATE,
- RANGE,
- REPEAT,
- REPEAT_ONE,
- SUBPATTERN,
- MIN_REPEAT_ONE
+CHCODES = _makecodes("""
+ CATEGORY_DIGIT CATEGORY_NOT_DIGIT
+ CATEGORY_SPACE CATEGORY_NOT_SPACE
+ CATEGORY_WORD CATEGORY_NOT_WORD
+ CATEGORY_LINEBREAK CATEGORY_NOT_LINEBREAK
+ CATEGORY_LOC_WORD CATEGORY_LOC_NOT_WORD
+ CATEGORY_UNI_DIGIT CATEGORY_UNI_NOT_DIGIT
+ CATEGORY_UNI_SPACE CATEGORY_UNI_NOT_SPACE
+ CATEGORY_UNI_WORD CATEGORY_UNI_NOT_WORD
+ CATEGORY_UNI_LINEBREAK CATEGORY_UNI_NOT_LINEBREAK
+""")
-]
-
-ATCODES = [
- AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT_BOUNDARY,
- AT_NON_BOUNDARY, AT_END, AT_END_LINE, AT_END_STRING,
- AT_LOC_BOUNDARY, AT_LOC_NON_BOUNDARY, AT_UNI_BOUNDARY,
- AT_UNI_NON_BOUNDARY
-]
-
-CHCODES = [
- CATEGORY_DIGIT, CATEGORY_NOT_DIGIT, CATEGORY_SPACE,
- CATEGORY_NOT_SPACE, CATEGORY_WORD, CATEGORY_NOT_WORD,
- CATEGORY_LINEBREAK, CATEGORY_NOT_LINEBREAK, CATEGORY_LOC_WORD,
- CATEGORY_LOC_NOT_WORD, CATEGORY_UNI_DIGIT, CATEGORY_UNI_NOT_DIGIT,
- CATEGORY_UNI_SPACE, CATEGORY_UNI_NOT_SPACE, CATEGORY_UNI_WORD,
- CATEGORY_UNI_NOT_WORD, CATEGORY_UNI_LINEBREAK,
- CATEGORY_UNI_NOT_LINEBREAK
-]
-
-def makedict(list):
- d = {}
- i = 0
- for item in list:
- d[item] = i
- i = i + 1
- return d
-
-OPCODES = makedict(OPCODES)
-ATCODES = makedict(ATCODES)
-CHCODES = makedict(CHCODES)
# replacement operations for "ignore case" mode
OP_IGNORE = {
GROUPREF: GROUPREF_IGNORE,
IN: IN_IGNORE,
LITERAL: LITERAL_IGNORE,
- NOT_LITERAL: NOT_LITERAL_IGNORE
+ NOT_LITERAL: NOT_LITERAL_IGNORE,
+ RANGE: RANGE_IGNORE,
}
AT_MULTILINE = {
@@ -217,9 +179,9 @@ SRE_INFO_CHARSET = 4 # pattern starts with character from given set
if __name__ == "__main__":
def dump(f, d, prefix):
- items = sorted(d.items(), key=lambda a: a[1])
- for k, v in items:
- f.write("#define %s_%s %s\n" % (prefix, k.upper(), v))
+ items = sorted(d)
+ for item in items:
+ f.write("#define %s_%s %d\n" % (prefix, item, item))
f = open("sre_constants.h", "w")
f.write("""\
/*
diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py
index b56d437..1a7d316 100644
--- a/Lib/sre_parse.py
+++ b/Lib/sre_parse.py
@@ -13,17 +13,19 @@
# XXX: show string offset and offending character for all errors
from sre_constants import *
-from _sre import MAXREPEAT
SPECIAL_CHARS = ".\\[{()*+?^$|"
REPEAT_CHARS = "*+?{"
-DIGITS = set("0123456789")
+DIGITS = frozenset("0123456789")
-OCTDIGITS = set("01234567")
-HEXDIGITS = set("0123456789abcdefABCDEF")
+OCTDIGITS = frozenset("01234567")
+HEXDIGITS = frozenset("0123456789abcdefABCDEF")
-WHITESPACE = set(" \t\n\r\v\f")
+WHITESPACE = frozenset(" \t\n\r\v\f")
+
+_REPEATCODES = frozenset({MIN_REPEAT, MAX_REPEAT})
+_UNITCODES = frozenset({ANY, RANGE, IN, LITERAL, NOT_LITERAL, CATEGORY})
ESCAPES = {
r"\a": (LITERAL, ord("\a")),
@@ -72,11 +74,13 @@ class Pattern:
def opengroup(self, name=None):
gid = self.groups
self.groups = gid + 1
+ if self.groups > MAXGROUPS:
+ raise error("groups number is too large")
if name is not None:
ogid = self.groupdict.get(name, None)
if ogid is not None:
- raise error("redefinition of group name %s as group %d; "
- "was group %d" % (repr(name), gid, ogid))
+ raise error("redefinition of group name %r as group %d; "
+ "was group %d" % (name, gid, ogid))
self.groupdict[name] = gid
self.open.append(gid)
return gid
@@ -97,24 +101,24 @@ class SubPattern:
nl = True
seqtypes = (tuple, list)
for op, av in self.data:
- print(level*" " + op, end='')
- if op == IN:
+ print(level*" " + str(op), end='')
+ if op is IN:
# member sublanguage
print()
for op, a in av:
- print((level+1)*" " + op, a)
- elif op == BRANCH:
+ print((level+1)*" " + str(op), a)
+ elif op is BRANCH:
print()
for i, a in enumerate(av[1]):
if i:
- print(level*" " + "or")
+ print(level*" " + "OR")
a.dump(level+1)
- elif op == GROUPREF_EXISTS:
+ elif op is GROUPREF_EXISTS:
condgroup, item_yes, item_no = av
print('', condgroup)
item_yes.dump(level+1)
if item_no:
- print(level*" " + "else")
+ print(level*" " + "ELSE")
item_no.dump(level+1)
elif isinstance(av, seqtypes):
nl = False
@@ -151,11 +155,9 @@ class SubPattern:
self.data.append(code)
def getwidth(self):
# determine the width (min, max) for this subpattern
- if self.width:
+ if self.width is not None:
return self.width
lo = hi = 0
- UNITCODES = (ANY, RANGE, IN, LITERAL, NOT_LITERAL, CATEGORY)
- REPEATCODES = (MIN_REPEAT, MAX_REPEAT)
for op, av in self.data:
if op is BRANCH:
i = MAXREPEAT - 1
@@ -174,11 +176,11 @@ class SubPattern:
i, j = av[1].getwidth()
lo = lo + i
hi = hi + j
- elif op in REPEATCODES:
+ elif op in _REPEATCODES:
i, j = av[2].getwidth()
lo = lo + i * av[0]
hi = hi + j * av[1]
- elif op in UNITCODES:
+ elif op in _UNITCODES:
lo = lo + 1
hi = hi + 1
elif op == SUCCESS:
@@ -190,33 +192,33 @@ class Tokenizer:
def __init__(self, string):
self.istext = isinstance(string, str)
self.string = string
+ if not self.istext:
+ string = str(string, 'latin1')
+ self.decoded_string = string
self.index = 0
+ self.next = None
self.__next()
def __next(self):
- if self.index >= len(self.string):
+ index = self.index
+ try:
+ char = self.decoded_string[index]
+ except IndexError:
self.next = None
return
- char = self.string[self.index:self.index+1]
- # Special case for the str8, since indexing returns a integer
- # XXX This is only needed for test_bug_926075 in test_re.py
- if char and not self.istext:
- char = chr(char[0])
if char == "\\":
+ index += 1
try:
- c = self.string[self.index + 1]
+ char += self.decoded_string[index]
except IndexError:
- raise error("bogus escape (end of line)")
- if not self.istext:
- c = chr(c)
- char = char + c
- self.index = self.index + len(char)
+ raise error("bogus escape (end of line)",
+ self.string, len(self.string) - 1) from None
+ self.index = index + 1
self.next = char
- def match(self, char, skip=1):
+ def match(self, char):
if char == self.next:
- if skip:
- self.__next()
- return 1
- return 0
+ self.__next()
+ return True
+ return False
def get(self):
this = self.next
self.__next()
@@ -230,10 +232,25 @@ class Tokenizer:
result += c
self.__next()
return result
+ def getuntil(self, terminator):
+ result = ''
+ while True:
+ c = self.next
+ self.__next()
+ if c is None:
+ raise self.error("unterminated name")
+ if c == terminator:
+ break
+ result += c
+ return result
def tell(self):
- return self.index, self.next
+ return self.index - len(self.next or '')
def seek(self, index):
- self.index, self.next = index
+ self.index = index
+ self.__next()
+
+ 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.
@@ -268,7 +285,7 @@ def _class_escape(source, escape):
if code:
return code
code = CATEGORIES.get(escape)
- if code and code[0] == IN:
+ if code and code[0] is IN:
return code
try:
c = escape[1:2]
@@ -277,7 +294,7 @@ def _class_escape(source, escape):
escape += source.getwhile(2, HEXDIGITS)
if len(escape) != 4:
raise ValueError
- return LITERAL, int(escape[2:], 16) & 0xff
+ return LITERAL, int(escape[2:], 16)
elif c == "u" and source.istext:
# unicode escape (exactly four digits)
escape += source.getwhile(4, HEXDIGITS)
@@ -295,14 +312,18 @@ def _class_escape(source, escape):
elif c in OCTDIGITS:
# octal escape (up to three digits)
escape += source.getwhile(2, OCTDIGITS)
- return LITERAL, int(escape[1:], 8) & 0xff
+ c = int(escape[1:], 8)
+ if c > 0o377:
+ raise source.error('octal escape value %r outside of '
+ 'range 0-0o377' % escape, len(escape))
+ return LITERAL, c
elif c in DIGITS:
raise ValueError
if len(escape) == 2:
return LITERAL, ord(escape[1])
except ValueError:
pass
- raise error("bogus escape: %s" % repr(escape))
+ raise source.error("bogus escape: %r" % escape, len(escape))
def _escape(source, escape, state):
# handle escape code in expression
@@ -319,7 +340,7 @@ def _escape(source, escape, state):
escape += source.getwhile(2, HEXDIGITS)
if len(escape) != 4:
raise ValueError
- return LITERAL, int(escape[2:], 16) & 0xff
+ return LITERAL, int(escape[2:], 16)
elif c == "u" and source.istext:
# unicode escape (exactly four digits)
escape += source.getwhile(4, HEXDIGITS)
@@ -337,45 +358,47 @@ def _escape(source, escape, state):
elif c == "0":
# octal escape
escape += source.getwhile(2, OCTDIGITS)
- return LITERAL, int(escape[1:], 8) & 0xff
+ return LITERAL, int(escape[1:], 8)
elif c in DIGITS:
# octal escape *or* decimal group reference (sigh)
if source.next in DIGITS:
- escape = escape + source.get()
+ escape += source.get()
if (escape[1] in OCTDIGITS and escape[2] in OCTDIGITS and
source.next in OCTDIGITS):
# got three octal digits; this is an octal escape
- escape = escape + source.get()
- return LITERAL, int(escape[1:], 8) & 0xff
+ escape += source.get()
+ c = int(escape[1:], 8)
+ if c > 0o377:
+ raise source.error('octal escape value %r outside of '
+ 'range 0-0o377' % escape,
+ len(escape))
+ return LITERAL, c
# not an octal escape, so this is a group reference
group = int(escape[1:])
if group < state.groups:
if not state.checkgroup(group):
- raise error("cannot refer to open group")
+ raise source.error("cannot refer to open group",
+ len(escape))
return GROUPREF, group
raise ValueError
if len(escape) == 2:
return LITERAL, ord(escape[1])
except ValueError:
pass
- raise error("bogus escape: %s" % repr(escape))
+ raise source.error("bogus escape: %r" % escape, len(escape))
-def _parse_sub(source, state, nested=1):
+def _parse_sub(source, state, nested=True):
# parse an alternation: a|b|c
items = []
itemsappend = items.append
sourcematch = source.match
- while 1:
+ while True:
itemsappend(_parse(source, state))
- if sourcematch("|"):
- continue
- if not nested:
+ if not sourcematch("|"):
break
- if not source.next or sourcematch(")", 0):
- break
- else:
- raise error("pattern not properly closed")
+ if nested and source.next is not None and source.next != ")":
+ raise source.error("pattern not properly closed")
if len(items) == 1:
return items[0]
@@ -384,7 +407,7 @@ def _parse_sub(source, state, nested=1):
subpatternappend = subpattern.append
# check if all items share a common prefix
- while 1:
+ while True:
prefix = None
for item in items:
if not item:
@@ -404,16 +427,12 @@ def _parse_sub(source, state, nested=1):
# check if the branch can be replaced by a character set
for item in items:
- if len(item) != 1 or item[0][0] != LITERAL:
+ if len(item) != 1 or item[0][0] is not LITERAL:
break
else:
# we can store this as a character set instead of a
# branch (the compiler may optimize this even more)
- set = []
- setappend = set.append
- for item in items:
- setappend(item[0])
- subpatternappend((IN, set))
+ subpatternappend((IN, [item[0] for item in items]))
return subpattern
subpattern.append((BRANCH, (None, items)))
@@ -423,21 +442,16 @@ def _parse_sub_cond(source, state, condgroup):
item_yes = _parse(source, state)
if source.match("|"):
item_no = _parse(source, state)
- if source.match("|"):
- raise error("conditional backref with more than two branches")
+ if source.next == "|":
+ raise source.error("conditional backref with more than two branches")
else:
item_no = None
- if source.next and not source.match(")", 0):
- raise error("pattern not properly closed")
+ if source.next is not None and source.next != ")":
+ raise source.error("pattern not properly closed")
subpattern = SubPattern(state)
subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no)))
return subpattern
-_PATTERNENDERS = set("|)")
-_ASSERTCHARS = set("=!<")
-_LOOKBEHINDASSERTCHARS = set("=!")
-_REPEATCODES = set([MIN_REPEAT, MAX_REPEAT])
-
def _parse(source, state):
# parse a simple pattern
subpattern = SubPattern(state)
@@ -447,32 +461,35 @@ def _parse(source, state):
sourceget = source.get
sourcematch = source.match
_len = len
- PATTERNENDERS = _PATTERNENDERS
- ASSERTCHARS = _ASSERTCHARS
- LOOKBEHINDASSERTCHARS = _LOOKBEHINDASSERTCHARS
- REPEATCODES = _REPEATCODES
+ _ord = ord
+ verbose = state.flags & SRE_FLAG_VERBOSE
- while 1:
+ while True:
- if source.next in PATTERNENDERS:
- break # end of subpattern
- this = sourceget()
+ this = source.next
if this is None:
break # end of pattern
+ if this in "|)":
+ break # end of subpattern
+ sourceget()
- if state.flags & SRE_FLAG_VERBOSE:
+ if verbose:
# skip whitespace and comments
if this in WHITESPACE:
continue
if this == "#":
- while 1:
+ while True:
this = sourceget()
- if this in (None, "\n"):
+ if this is None or this == "\n":
break
continue
- if this and this[0] not in SPECIAL_CHARS:
- subpatternappend((LITERAL, ord(this)))
+ if this[0] == "\\":
+ code = _escape(source, this, state)
+ subpatternappend(code)
+
+ elif this not in SPECIAL_CHARS:
+ subpatternappend((LITERAL, _ord(this)))
elif this == "[":
# character set
@@ -484,39 +501,38 @@ def _parse(source, state):
setappend((NEGATE, None))
# check remaining characters
start = set[:]
- while 1:
+ while True:
this = sourceget()
+ if this is None:
+ raise source.error("unexpected end of regular expression")
if this == "]" and set != start:
break
- elif this and this[0] == "\\":
+ elif this[0] == "\\":
code1 = _class_escape(source, this)
- elif this:
- code1 = LITERAL, ord(this)
else:
- raise error("unexpected end of regular expression")
+ code1 = LITERAL, _ord(this)
if sourcematch("-"):
# potential range
this = sourceget()
+ if this is None:
+ raise source.error("unexpected end of regular expression")
if this == "]":
if code1[0] is IN:
code1 = code1[1][0]
setappend(code1)
- setappend((LITERAL, ord("-")))
+ setappend((LITERAL, _ord("-")))
break
- elif this:
- if this[0] == "\\":
- code2 = _class_escape(source, this)
- else:
- code2 = LITERAL, ord(this)
- if code1[0] != LITERAL or code2[0] != LITERAL:
- raise error("bad character range")
- lo = code1[1]
- hi = code2[1]
- if hi < lo:
- raise error("bad character range")
- setappend((RANGE, (lo, hi)))
+ if this[0] == "\\":
+ code2 = _class_escape(source, this)
else:
- raise error("unexpected end of regular expression")
+ code2 = LITERAL, _ord(this)
+ if code1[0] != LITERAL or code2[0] != LITERAL:
+ raise source.error("bad character range", len(this))
+ lo = code1[1]
+ hi = code2[1]
+ if hi < lo:
+ raise source.error("bad character range", len(this))
+ setappend((RANGE, (lo, hi)))
else:
if code1[0] is IN:
code1 = code1[1][0]
@@ -531,8 +547,9 @@ def _parse(source, state):
# XXX: <fl> should add charmap optimization here
subpatternappend((IN, set))
- elif this and this[0] in REPEAT_CHARS:
+ elif this in REPEAT_CHARS:
# repeat previous item
+ here = source.tell()
if this == "?":
min, max = 0, 1
elif this == "*":
@@ -542,20 +559,19 @@ def _parse(source, state):
min, max = 1, MAXREPEAT
elif this == "{":
if source.next == "}":
- subpatternappend((LITERAL, ord(this)))
+ subpatternappend((LITERAL, _ord(this)))
continue
- here = source.tell()
min, max = 0, MAXREPEAT
lo = hi = ""
while source.next in DIGITS:
- lo = lo + source.get()
+ lo += sourceget()
if sourcematch(","):
while source.next in DIGITS:
- hi = hi + sourceget()
+ hi += sourceget()
else:
hi = lo
if not sourcematch("}"):
- subpatternappend((LITERAL, ord(this)))
+ subpatternappend((LITERAL, _ord(this)))
source.seek(here)
continue
if lo:
@@ -567,18 +583,21 @@ def _parse(source, state):
if max >= MAXREPEAT:
raise OverflowError("the repetition number is too large")
if max < min:
- raise error("bad repeat interval")
+ raise source.error("bad repeat interval",
+ source.tell() - here)
else:
- raise error("not supported")
+ raise source.error("not supported", len(this))
# figure out which item to repeat
if subpattern:
item = subpattern[-1:]
else:
item = None
- if not item or (_len(item) == 1 and item[0][0] == AT):
- raise error("nothing to repeat")
- if item[0][0] in REPEATCODES:
- raise error("multiple repeat")
+ if not item or (_len(item) == 1 and item[0][0] is AT):
+ raise source.error("nothing to repeat",
+ source.tell() - here + len(this))
+ if item[0][0] in _REPEATCODES:
+ raise source.error("multiple repeat",
+ source.tell() - here + len(this))
if sourcematch("?"):
subpattern[-1] = (MIN_REPEAT, (min, max, item))
else:
@@ -594,131 +613,129 @@ def _parse(source, state):
if sourcematch("?"):
group = 0
# options
- if sourcematch("P"):
+ char = sourceget()
+ if char is None:
+ raise self.error("unexpected end of pattern")
+ if char == "P":
# python extensions
if sourcematch("<"):
# named group: skip forward to end of name
- name = ""
- while 1:
- char = sourceget()
- if char is None:
- raise error("unterminated name")
- if char == ">":
- break
- name = name + char
+ name = source.getuntil(">")
group = 1
if not name:
- raise error("missing group name")
+ raise source.error("missing group name", 1)
if not name.isidentifier():
- raise error("bad character in group name %r" % name)
+ raise source.error("bad character in group name "
+ "%r" % name,
+ len(name) + 1)
elif sourcematch("="):
# named backreference
- name = ""
- while 1:
- char = sourceget()
- if char is None:
- raise error("unterminated name")
- if char == ")":
- break
- name = name + char
+ name = source.getuntil(")")
if not name:
- raise error("missing group name")
+ raise source.error("missing group name", 1)
if not name.isidentifier():
- raise error("bad character in backref group name "
- "%r" % name)
+ raise source.error("bad character in backref "
+ "group name %r" % name,
+ len(name) + 1)
gid = state.groupdict.get(name)
if gid is None:
msg = "unknown group name: {0!r}".format(name)
- raise error(msg)
+ raise source.error(msg, len(name) + 1)
subpatternappend((GROUPREF, gid))
continue
else:
char = sourceget()
if char is None:
- raise error("unexpected end of pattern")
- raise error("unknown specifier: ?P%s" % char)
- elif sourcematch(":"):
+ raise source.error("unexpected end of pattern")
+ raise source.error("unknown specifier: ?P%s" % char,
+ len(char))
+ elif char == ":":
# non-capturing group
group = 2
- elif sourcematch("#"):
+ elif char == "#":
# comment
- while 1:
- if source.next is None or source.next == ")":
+ while True:
+ if source.next is None:
+ raise source.error("unbalanced parenthesis")
+ if sourceget() == ")":
break
- sourceget()
- if not sourcematch(")"):
- raise error("unbalanced parenthesis")
continue
- elif source.next in ASSERTCHARS:
+ elif char in "=!<":
# lookahead assertions
- char = sourceget()
dir = 1
if char == "<":
- if source.next not in LOOKBEHINDASSERTCHARS:
- raise error("syntax error")
- dir = -1 # lookbehind
char = sourceget()
+ if char is None or char not in "=!":
+ raise source.error("syntax error")
+ dir = -1 # lookbehind
p = _parse_sub(source, state)
if not sourcematch(")"):
- raise error("unbalanced parenthesis")
+ raise source.error("unbalanced parenthesis")
if char == "=":
subpatternappend((ASSERT, (dir, p)))
else:
subpatternappend((ASSERT_NOT, (dir, p)))
continue
- elif sourcematch("("):
+ elif char == "(":
# conditional backreference group
- condname = ""
- while 1:
- char = sourceget()
- if char is None:
- raise error("unterminated name")
- if char == ")":
- break
- condname = condname + char
+ condname = source.getuntil(")")
group = 2
if not condname:
- raise error("missing group name")
+ raise source.error("missing group name", 1)
if condname.isidentifier():
condgroup = state.groupdict.get(condname)
if condgroup is None:
msg = "unknown group name: {0!r}".format(condname)
- raise error(msg)
+ raise source.error(msg, len(condname) + 1)
else:
try:
condgroup = int(condname)
+ if condgroup < 0:
+ raise ValueError
except ValueError:
- raise error("bad character in group name")
- else:
+ raise source.error("bad character in group name",
+ len(condname) + 1)
+ if not condgroup:
+ raise source.error("bad group number",
+ len(condname) + 1)
+ if condgroup >= MAXGROUPS:
+ raise source.error("the group number is too large",
+ len(condname) + 1)
+ elif char in FLAGS:
# flags
- if not source.next in FLAGS:
- raise error("unexpected end of pattern")
+ state.flags |= FLAGS[char]
while source.next in FLAGS:
- state.flags = state.flags | FLAGS[sourceget()]
+ state.flags |= FLAGS[sourceget()]
+ verbose = state.flags & SRE_FLAG_VERBOSE
+ else:
+ raise source.error("unexpected end of pattern")
if group:
# parse group contents
if group == 2:
# anonymous group
group = None
else:
- group = state.opengroup(name)
+ try:
+ group = state.opengroup(name)
+ except error as err:
+ raise source.error(err.msg, len(name) + 1)
if condgroup:
p = _parse_sub_cond(source, state, condgroup)
else:
p = _parse_sub(source, state)
if not sourcematch(")"):
- raise error("unbalanced parenthesis")
+ raise source.error("unbalanced parenthesis")
if group is not None:
state.closegroup(group)
subpatternappend((SUBPATTERN, (group, p)))
else:
- while 1:
+ while True:
char = sourceget()
if char is None:
- raise error("unexpected end of pattern")
+ raise source.error("unexpected end of pattern")
if char == ")":
break
- raise error("unknown extension")
+ raise source.error("unknown extension", len(char))
elif this == "^":
subpatternappend((AT, AT_BEGINNING))
@@ -726,18 +743,19 @@ def _parse(source, state):
elif this == "$":
subpattern.append((AT, AT_END))
- elif this and this[0] == "\\":
- code = _escape(source, this, state)
- subpatternappend(code)
-
else:
- raise error("parser error")
+ raise source.error("parser error", len(this))
return subpattern
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)
if not flags & SRE_FLAG_ASCII:
flags |= SRE_FLAG_UNICODE
elif flags & SRE_FLAG_UNICODE:
@@ -745,6 +763,11 @@ def fix_flags(src, flags):
else:
if flags & SRE_FLAG_UNICODE:
raise ValueError("can't 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)
return flags
def parse(str, flags=0, pattern=None):
@@ -760,11 +783,12 @@ def parse(str, flags=0, pattern=None):
p = _parse_sub(source, pattern, 0)
p.pattern.flags = fix_flags(str, p.pattern.flags)
- tail = source.get()
- if tail == ")":
- raise error("unbalanced parenthesis")
- elif tail:
- raise error("bogus characters at end of regular expression")
+ if source.next is not None:
+ if source.next == ")":
+ raise source.error("unbalanced parenthesis")
+ else:
+ raise source.error("bogus characters at end of regular expression",
+ len(tail))
if flags & SRE_FLAG_DEBUG:
p.dump()
@@ -801,22 +825,20 @@ def parse_template(source, pattern):
if c == "g":
name = ""
if s.match("<"):
- while True:
- char = sget()
- if char is None:
- raise error("unterminated group name")
- if char == ">":
- break
- name += char
+ name = s.getuntil(">")
if not name:
- raise error("missing group name")
+ raise s.error("missing group name", 1)
try:
index = int(name)
if index < 0:
- raise error("negative group number")
+ raise s.error("negative group number", len(name) + 1)
+ if index >= MAXGROUPS:
+ raise s.error("the group number is too large",
+ len(name) + 1)
except ValueError:
if not name.isidentifier():
- raise error("bad character in group name")
+ raise s.error("bad character in group name",
+ len(name) + 1)
try:
index = pattern.groupindex[name]
except KeyError:
@@ -837,7 +859,11 @@ def parse_template(source, pattern):
s.next in OCTDIGITS):
this += sget()
isoctal = True
- lappend(chr(int(this[1:], 8) & 0xff))
+ c = int(this[1:], 8)
+ if c > 0o377:
+ raise s.error('octal escape value %r outside of '
+ 'range 0-0o377' % this, len(this))
+ lappend(chr(c))
if not isoctal:
addgroup(int(this[1:]))
else:
@@ -858,14 +884,12 @@ def parse_template(source, pattern):
def expand_template(template, match):
g = match.group
- sep = match.string[:0]
+ empty = match.string[:0]
groups, literals = template
literals = literals[:]
try:
for index, group in groups:
- literals[index] = s = g(group)
- if s is None:
- raise error("unmatched group")
+ literals[index] = g(group) or empty
except IndexError:
raise error("invalid group reference")
- return sep.join(literals)
+ return empty.join(literals)
diff --git a/Lib/ssl.py b/Lib/ssl.py
index 8c75f00..56bc38e 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -87,17 +87,18 @@ ALERT_DESCRIPTION_BAD_CERTIFICATE_HASH_VALUE
ALERT_DESCRIPTION_UNKNOWN_PSK_IDENTITY
"""
+import ipaddress
import textwrap
import re
import sys
import os
from collections import namedtuple
-from enum import Enum as _Enum
+from enum import Enum as _Enum, IntEnum as _IntEnum
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
+from _ssl import _SSLContext, MemoryBIO
from _ssl import (
SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError,
SSLSyscallError, SSLEOFError,
@@ -121,29 +122,22 @@ def _import_symbols(prefix):
_import_symbols('OP_')
_import_symbols('ALERT_DESCRIPTION_')
_import_symbols('SSL_ERROR_')
-_import_symbols('PROTOCOL_')
-from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN
+from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN
from _ssl import _OPENSSL_API_VERSION
+_SSLMethod = _IntEnum('_SSLMethod',
+ {name: value for name, value in vars(_ssl).items()
+ if name.startswith('PROTOCOL_')})
+globals().update(_SSLMethod.__members__)
+
+_PROTOCOL_NAMES = {value: name for name, value in _SSLMethod.__members__.items()}
-_PROTOCOL_NAMES = {value: name for name, value in globals().items() if name.startswith('PROTOCOL_')}
try:
- from _ssl import PROTOCOL_SSLv2
_SSLv2_IF_EXISTS = PROTOCOL_SSLv2
-except ImportError:
+except NameError:
_SSLv2_IF_EXISTS = None
-else:
- _PROTOCOL_NAMES[PROTOCOL_SSLv2] = "SSLv2"
-
-try:
- from _ssl import PROTOCOL_TLSv1_1, PROTOCOL_TLSv1_2
-except ImportError:
- pass
-else:
- _PROTOCOL_NAMES[PROTOCOL_TLSv1_1] = "TLSv1.1"
- _PROTOCOL_NAMES[PROTOCOL_TLSv1_2] = "TLSv1.2"
if sys.platform == "win32":
from _ssl import enum_certificates, enum_crls
@@ -249,6 +243,17 @@ def _dnsname_match(dn, hostname, max_wildcards=1):
return pat.match(hostname)
+def _ipaddress_match(ipname, host_ip):
+ """Exact matching of IP addresses.
+
+ RFC 6125 explicitly doesn't define an algorithm for this
+ (section 1.7.2 - "Out of Scope").
+ """
+ # OpenSSL may add a trailing newline to a subjectAltName's IP address
+ ip = ipaddress.ip_address(ipname.rstrip())
+ return ip == host_ip
+
+
def match_hostname(cert, hostname):
"""Verify that *cert* (in decoded format as returned by
SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
@@ -261,11 +266,20 @@ def match_hostname(cert, hostname):
raise ValueError("empty or no certificate, match_hostname needs a "
"SSL socket or SSL context with either "
"CERT_OPTIONAL or CERT_REQUIRED")
+ try:
+ host_ip = ipaddress.ip_address(hostname)
+ except ValueError:
+ # Not an IP address (common case)
+ host_ip = None
dnsnames = []
san = cert.get('subjectAltName', ())
for key, value in san:
if key == 'DNS':
- if _dnsname_match(value, hostname):
+ if host_ip is None and _dnsname_match(value, hostname):
+ return
+ dnsnames.append(value)
+ elif key == 'IP Address':
+ if host_ip is not None and _ipaddress_match(value, host_ip):
return
dnsnames.append(value)
if not dnsnames:
@@ -364,6 +378,12 @@ class SSLContext(_SSLContext):
server_hostname=server_hostname,
_context=self)
+ def wrap_bio(self, incoming, outgoing, server_side=False,
+ server_hostname=None):
+ sslobj = self._wrap_bio(incoming, outgoing, server_side=server_side,
+ server_hostname=server_hostname)
+ return SSLObject(sslobj)
+
def set_npn_protocols(self, npn_protocols):
protos = bytearray()
for protocol in npn_protocols:
@@ -375,6 +395,17 @@ class SSLContext(_SSLContext):
self._set_npn_protocols(protos)
+ def set_alpn_protocols(self, alpn_protocols):
+ protos = bytearray()
+ for protocol in alpn_protocols:
+ b = bytes(protocol, 'ascii')
+ if len(b) == 0 or len(b) > 255:
+ raise SSLError('ALPN protocols must be 1 to 255 in length')
+ protos.append(len(b))
+ protos.extend(b)
+
+ self._set_alpn_protocols(protos)
+
def _load_windows_store_certs(self, storename, purpose):
certs = bytearray()
for cert, encoding, trust in enum_certificates(storename):
@@ -491,6 +522,141 @@ _create_default_https_context = create_default_context
_create_stdlib_context = _create_unverified_context
+class SSLObject:
+ """This class implements an interface on top of a low-level SSL object as
+ implemented by OpenSSL. This object captures the state of an SSL connection
+ but does not provide any network IO itself. IO needs to be performed
+ through separate "BIO" objects which are OpenSSL's IO abstraction layer.
+
+ This class does not have a public constructor. Instances are returned by
+ ``SSLContext.wrap_bio``. This class is typically used by framework authors
+ that want to implement asynchronous IO for SSL through memory buffers.
+
+ When compared to ``SSLSocket``, this object lacks the following features:
+
+ * Any form of network IO incluging methods such as ``recv`` and ``send``.
+ * The ``do_handshake_on_connect`` and ``suppress_ragged_eofs`` machinery.
+ """
+
+ def __init__(self, sslobj, owner=None):
+ self._sslobj = sslobj
+ # Note: _sslobj takes a weak reference to owner
+ self._sslobj.owner = owner or self
+
+ @property
+ def context(self):
+ """The SSLContext that is currently in use."""
+ return self._sslobj.context
+
+ @context.setter
+ def context(self, ctx):
+ self._sslobj.context = ctx
+
+ @property
+ def server_side(self):
+ """Whether this is a server-side socket."""
+ return self._sslobj.server_side
+
+ @property
+ def server_hostname(self):
+ """The currently set server hostname (for SNI), or ``None`` if no
+ server hostame is set."""
+ return self._sslobj.server_hostname
+
+ def read(self, len=0, buffer=None):
+ """Read up to 'len' bytes from the SSL object and return them.
+
+ If 'buffer' is provided, read into this buffer and return the number of
+ bytes read.
+ """
+ if buffer is not None:
+ v = self._sslobj.read(len, buffer)
+ else:
+ v = self._sslobj.read(len or 1024)
+ return v
+
+ def write(self, data):
+ """Write 'data' to the SSL object and return the number of bytes
+ written.
+
+ The 'data' argument must support the buffer interface.
+ """
+ return self._sslobj.write(data)
+
+ def getpeercert(self, binary_form=False):
+ """Returns a formatted version of the data in the certificate provided
+ by the other end of the SSL channel.
+
+ Return None if no certificate was provided, {} if a certificate was
+ provided, but not validated.
+ """
+ return self._sslobj.peer_certificate(binary_form)
+
+ def selected_npn_protocol(self):
+ """Return the currently selected NPN protocol as a string, or ``None``
+ if a next protocol was not negotiated or if NPN is not supported by one
+ of the peers."""
+ if _ssl.HAS_NPN:
+ return self._sslobj.selected_npn_protocol()
+
+ def selected_alpn_protocol(self):
+ """Return the currently selected ALPN protocol as a string, or ``None``
+ if a next protocol was not negotiated or if ALPN is not supported by one
+ of the peers."""
+ if _ssl.HAS_ALPN:
+ return self._sslobj.selected_alpn_protocol()
+
+ def cipher(self):
+ """Return the currently selected cipher as a 3-tuple ``(name,
+ ssl_version, secret_bits)``."""
+ return self._sslobj.cipher()
+
+ def shared_ciphers(self):
+ """Return a list of ciphers shared by the client during the handshake or
+ None if this is not a valid server connection.
+ """
+ return self._sslobj.shared_ciphers()
+
+ def compression(self):
+ """Return the current compression algorithm in use, or ``None`` if
+ compression was not negotiated or not supported by one of the peers."""
+ return self._sslobj.compression()
+
+ def pending(self):
+ """Return the number of bytes that can be read immediately."""
+ return self._sslobj.pending()
+
+ def do_handshake(self):
+ """Start the SSL/TLS handshake."""
+ self._sslobj.do_handshake()
+ if self.context.check_hostname:
+ if not self.server_hostname:
+ raise ValueError("check_hostname needs server_hostname "
+ "argument")
+ match_hostname(self.getpeercert(), self.server_hostname)
+
+ def unwrap(self):
+ """Start the SSL shutdown handshake."""
+ return self._sslobj.shutdown()
+
+ def get_channel_binding(self, cb_type="tls-unique"):
+ """Get channel binding data for current connection. Raise ValueError
+ if the requested `cb_type` is not supported. Return bytes of the data
+ or None if the data is not available (e.g. before the handshake)."""
+ if cb_type not in CHANNEL_BINDING_TYPES:
+ raise ValueError("Unsupported channel binding type")
+ if cb_type != "tls-unique":
+ raise NotImplementedError(
+ "{0} channel binding type not implemented"
+ .format(cb_type))
+ return self._sslobj.tls_unique_cb()
+
+ def version(self):
+ """Return a string identifying the protocol version used by the
+ current SSL channel. """
+ return self._sslobj.version()
+
+
class SSLSocket(socket):
"""This class implements a subtype of socket.socket that wraps
the underlying OS socket in an SSL context when necessary, and
@@ -573,8 +739,9 @@ class SSLSocket(socket):
if connected:
# create the SSL object
try:
- self._sslobj = self._context._wrap_socket(self, server_side,
- server_hostname)
+ sslobj = self._context._wrap_socket(self, server_side,
+ server_hostname)
+ self._sslobj = SSLObject(sslobj, owner=self)
if do_handshake_on_connect:
timeout = self.gettimeout()
if timeout == 0.0:
@@ -619,11 +786,7 @@ class SSLSocket(socket):
if not self._sslobj:
raise ValueError("Read on closed or unwrapped SSL socket.")
try:
- if buffer is not None:
- v = self._sslobj.read(len, buffer)
- else:
- v = self._sslobj.read(len or 1024)
- return v
+ return self._sslobj.read(len, buffer)
except SSLError as x:
if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
if buffer is not None:
@@ -650,7 +813,7 @@ class SSLSocket(socket):
self._checkClosed()
self._check_connected()
- return self._sslobj.peer_certificate(binary_form)
+ return self._sslobj.getpeercert(binary_form)
def selected_npn_protocol(self):
self._checkClosed()
@@ -659,6 +822,13 @@ class SSLSocket(socket):
else:
return self._sslobj.selected_npn_protocol()
+ def selected_alpn_protocol(self):
+ self._checkClosed()
+ if not self._sslobj or not _ssl.HAS_ALPN:
+ return None
+ else:
+ return self._sslobj.selected_alpn_protocol()
+
def cipher(self):
self._checkClosed()
if not self._sslobj:
@@ -666,6 +836,12 @@ class SSLSocket(socket):
else:
return self._sslobj.cipher()
+ def shared_ciphers(self):
+ self._checkClosed()
+ if not self._sslobj:
+ return None
+ return self._sslobj.shared_ciphers()
+
def compression(self):
self._checkClosed()
if not self._sslobj:
@@ -680,17 +856,7 @@ class SSLSocket(socket):
raise ValueError(
"non-zero flags not allowed in calls to send() on %s" %
self.__class__)
- try:
- v = self._sslobj.write(data)
- except SSLError as x:
- if x.args[0] == SSL_ERROR_WANT_READ:
- return 0
- elif x.args[0] == SSL_ERROR_WANT_WRITE:
- return 0
- else:
- raise
- else:
- return v
+ return self._sslobj.write(data)
else:
return socket.send(self, data, flags)
@@ -726,6 +892,16 @@ class SSLSocket(socket):
else:
return socket.sendall(self, data, flags)
+ def sendfile(self, file, offset=0, count=None):
+ """Send a file, possibly by using os.sendfile() if this is a
+ clear-text socket. Return the total number of bytes sent.
+ """
+ if self._sslobj is None:
+ # os.sendfile() works with plain sockets only
+ return super().sendfile(file, offset, count)
+ else:
+ return self._sendfile_use_send(file, offset, count)
+
def recv(self, buflen=1024, flags=0):
self._checkClosed()
if self._sslobj:
@@ -790,7 +966,7 @@ class SSLSocket(socket):
def unwrap(self):
if self._sslobj:
- s = self._sslobj.shutdown()
+ s = self._sslobj.unwrap()
self._sslobj = None
return s
else:
@@ -811,12 +987,6 @@ class SSLSocket(socket):
finally:
self.settimeout(timeout)
- if self.context.check_hostname:
- if not self.server_hostname:
- raise ValueError("check_hostname needs server_hostname "
- "argument")
- match_hostname(self.getpeercert(), self.server_hostname)
-
def _real_connect(self, addr, connect_ex):
if self.server_side:
raise ValueError("can't connect in server-side mode")
@@ -824,7 +994,8 @@ class SSLSocket(socket):
# connected at the time of the call. We connect it, then wrap it.
if self._connected:
raise ValueError("attempt to connect already-connected SSLSocket!")
- self._sslobj = self.context._wrap_socket(self, False, self.server_hostname)
+ sslobj = self.context._wrap_socket(self, False, self.server_hostname)
+ self._sslobj = SSLObject(sslobj, owner=self)
try:
if connect_ex:
rc = socket.connect_ex(self, addr)
@@ -867,15 +1038,18 @@ class SSLSocket(socket):
if the requested `cb_type` is not supported. Return bytes of the data
or None if the data is not available (e.g. before the handshake).
"""
- if cb_type not in CHANNEL_BINDING_TYPES:
- raise ValueError("Unsupported channel binding type")
- if cb_type != "tls-unique":
- raise NotImplementedError(
- "{0} channel binding type not implemented"
- .format(cb_type))
if self._sslobj is None:
return None
- return self._sslobj.tls_unique_cb()
+ return self._sslobj.get_channel_binding(cb_type)
+
+ def version(self):
+ """
+ Return a string identifying the protocol version used by the
+ current SSL channel, or None if there is no established channel.
+ """
+ if self._sslobj is None:
+ return None
+ return self._sslobj.version()
def wrap_socket(sock, keyfile=None, certfile=None,
@@ -895,12 +1069,34 @@ def wrap_socket(sock, keyfile=None, certfile=None,
# some utility functions
def cert_time_to_seconds(cert_time):
- """Takes a date-time string in standard ASN1_print form
- ("MON DAY 24HOUR:MINUTE:SEC YEAR TIMEZONE") and return
- a Python time value in seconds past the epoch."""
+ """Return the time in seconds since the Epoch, given the timestring
+ representing the "notBefore" or "notAfter" date from a certificate
+ in ``"%b %d %H:%M:%S %Y %Z"`` strptime format (C locale).
+
+ "notBefore" or "notAfter" dates must use UTC (RFC 5280).
- import time
- return time.mktime(time.strptime(cert_time, "%b %d %H:%M:%S %Y GMT"))
+ Month is one of: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
+ UTC should be specified as GMT (see ASN1_TIME_print())
+ """
+ from time import strptime
+ from calendar import timegm
+
+ months = (
+ "Jan","Feb","Mar","Apr","May","Jun",
+ "Jul","Aug","Sep","Oct","Nov","Dec"
+ )
+ time_format = ' %d %H:%M:%S %Y GMT' # NOTE: no month, fixed GMT
+ try:
+ month_number = months.index(cert_time[:3].title()) + 1
+ except ValueError:
+ raise ValueError('time data %r does not match '
+ 'format "%%b%s"' % (cert_time, time_format))
+ else:
+ # found valid month
+ tt = strptime(cert_time[3:], time_format)
+ # return an integer, the previous mktime()-based implementation
+ # returned a float (fractional seconds are always zero here).
+ return timegm((tt[0], month_number) + tt[2:6])
PEM_HEADER = "-----BEGIN CERTIFICATE-----"
PEM_FOOTER = "-----END CERTIFICATE-----"
diff --git a/Lib/stat.py b/Lib/stat.py
index 3eecc3e..46837c0 100644
--- a/Lib/stat.py
+++ b/Lib/stat.py
@@ -148,6 +148,29 @@ def filemode(mode):
perm.append("-")
return "".join(perm)
+
+# Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s
+# "st_file_attributes" member
+
+FILE_ATTRIBUTE_ARCHIVE = 32
+FILE_ATTRIBUTE_COMPRESSED = 2048
+FILE_ATTRIBUTE_DEVICE = 64
+FILE_ATTRIBUTE_DIRECTORY = 16
+FILE_ATTRIBUTE_ENCRYPTED = 16384
+FILE_ATTRIBUTE_HIDDEN = 2
+FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768
+FILE_ATTRIBUTE_NORMAL = 128
+FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192
+FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072
+FILE_ATTRIBUTE_OFFLINE = 4096
+FILE_ATTRIBUTE_READONLY = 1
+FILE_ATTRIBUTE_REPARSE_POINT = 1024
+FILE_ATTRIBUTE_SPARSE_FILE = 512
+FILE_ATTRIBUTE_SYSTEM = 4
+FILE_ATTRIBUTE_TEMPORARY = 256
+FILE_ATTRIBUTE_VIRTUAL = 65536
+
+
# If available, use C implementation
try:
from _stat import *
diff --git a/Lib/statistics.py b/Lib/statistics.py
index 25a26d4..3972ed2 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -150,7 +150,7 @@ def _sum(data, start=0):
# We fail as soon as we reach a value that is not an int or the type of
# the first value which is not an int. E.g. _sum([int, int, float, int])
# is okay, but sum([int, int, float, Fraction]) is not.
- allowed_types = set([int, type(start)])
+ allowed_types = {int, type(start)}
n, d = _exact_ratio(start)
partials = {d: n} # map {denominator: sum of numerators}
# Micro-optimizations.
@@ -168,7 +168,7 @@ def _sum(data, start=0):
assert allowed_types.pop() is int
T = int
else:
- T = (allowed_types - set([int])).pop()
+ T = (allowed_types - {int}).pop()
if None in partials:
assert issubclass(T, (float, Decimal))
assert not math.isfinite(partials[None])
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index bc0ef0b..25ffeff 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -104,17 +104,21 @@ in the child process prior to executing the command.
If env is not None, it defines the environment variables for the new
process.
-If universal_newlines is false, the file objects stdin, stdout and stderr
+If universal_newlines is False, the file objects stdin, stdout and stderr
are opened as binary files, and no line ending conversion is done.
-If universal_newlines is true, the file objects stdout and stderr are
-opened as a text files, but lines may be terminated by any of '\n',
+If universal_newlines is True, the file objects stdout and stderr are
+opened as a text file, but lines may be terminated by any of '\n',
the Unix end-of-line convention, '\r', the old Macintosh convention or
'\r\n', the Windows convention. All of these external representations
are seen as '\n' by the Python program. Also, the newlines attribute
of the file objects stdout, stdin and stderr are not updated by the
communicate() method.
+In either case, the process being communicated with should start up
+expecting to receive bytes on its standard input and decode them with
+the same encoding they are sent in.
+
The startupinfo and creationflags, if given, will be passed to the
underlying CreateProcess() function. They can specify things such as
appearance of the main window and priority for the new process.
@@ -184,6 +188,9 @@ check_output(*popenargs, **kwargs):
pass a string to the subprocess's stdin. If you use this argument
you may not also use the Popen constructor's "stdin" argument.
+ If universal_newlines is set to True, the "input" argument must
+ be a string rather than bytes, and the return value will be a string.
+
Exceptions
----------
Exceptions raised in the child process, before the new program has
@@ -225,9 +232,13 @@ wait()
communicate(input=None)
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 a string to be
+ 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 child. If the Popen instance was constructed with universal_newlines
+ set to True, the input argument should be a string and will be encoded
+ using the preferred system encoding (see locale.getpreferredencoding);
+ if universal_newlines is False, the input argument should be a
+ byte string.
communicate() returns a tuple (stdout, stderr).
@@ -354,10 +365,7 @@ import signal
import builtins
import warnings
import errno
-try:
- from time import monotonic as _time
-except ImportError:
- from time import time as _time
+from time import monotonic as _time
# Exception classes used by this module.
class SubprocessError(Exception): pass
@@ -453,15 +461,11 @@ if mswindows:
raise ValueError("already closed")
def __repr__(self):
- return "Handle(%d)" % int(self)
+ return "%s(%d)" % (self.__class__.__name__, int(self))
__del__ = Close
__str__ = __repr__
-try:
- MAXFD = os.sysconf("SC_OPEN_MAX")
-except:
- MAXFD = 256
# This lists holds Popen instances for which the underlying process had not
# exited at the time its __del__ method got called: those processes are wait()ed
@@ -485,14 +489,6 @@ STDOUT = -2
DEVNULL = -3
-def _eintr_retry_call(func, *args):
- while True:
- try:
- return func(*args)
- except InterruptedError:
- continue
-
-
# XXX This function is only used by multiprocessing and the test suite,
# but it's here so that it can be imported when Python is compiled without
# threads.
@@ -591,8 +587,8 @@ def check_output(*popenargs, timeout=None, **kwargs):
... input=b"when in the course of fooman events\n")
b'when in the course of barman events\n'
- If universal_newlines=True is passed, the return value will be a
- string rather than bytes.
+ If universal_newlines=True is passed, the "input" argument must be a
+ string and the return value will be a string rather than bytes.
"""
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
@@ -916,14 +912,35 @@ class Popen(object):
self._devnull = os.open(os.devnull, os.O_RDWR)
return self._devnull
+ def _stdin_write(self, input):
+ if input:
+ try:
+ self.stdin.write(input)
+ except BrokenPipeError:
+ # communicate() must ignore broken pipe error
+ pass
+ except OSError as e:
+ if e.errno == errno.EINVAL and self.poll() is not None:
+ # Issue #19612: On Windows, stdin.write() fails with EINVAL
+ # if the process already exited before the write
+ pass
+ else:
+ raise
+ self.stdin.close()
+
def communicate(self, input=None, timeout=None):
"""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
- bytes to be sent to the child process, or None, if no data
- should be sent to the child.
+ process to terminate.
- communicate() returns a tuple (stdout, stderr)."""
+ The optional "input" argument should be data to be sent to the
+ child process (if self.universal_newlines is True, this should
+ be a string; if it is False, "input" should be bytes), or
+ None, if no data should be sent to the child.
+
+ communicate() returns a tuple (stdout, stderr). These will be
+ bytes or, if self.universal_newlines was True, a string.
+ """
if self._communication_started and input:
raise ValueError("Cannot send input after starting communication")
@@ -936,18 +953,12 @@ class Popen(object):
stdout = None
stderr = None
if self.stdin:
- if input:
- try:
- self.stdin.write(input)
- except OSError as e:
- if e.errno != errno.EPIPE and e.errno != errno.EINVAL:
- raise
- self.stdin.close()
+ self._stdin_write(input)
elif self.stdout:
- stdout = _eintr_retry_call(self.stdout.read)
+ stdout = self.stdout.read()
self.stdout.close()
elif self.stderr:
- stderr = _eintr_retry_call(self.stderr.read)
+ stderr = self.stderr.read()
self.stderr.close()
self.wait()
else:
@@ -1191,21 +1202,7 @@ class Popen(object):
self.stderr_thread.start()
if self.stdin:
- if input is not None:
- try:
- self.stdin.write(input)
- except OSError as e:
- if e.errno == errno.EPIPE:
- # communicate() should ignore pipe full error
- pass
- elif (e.errno == errno.EINVAL
- and self.poll() is not None):
- # Issue #19612: stdin.write() fails with EINVAL
- # if the process already exited before the write
- pass
- else:
- raise
- self.stdin.close()
+ self._stdin_write(input)
# Wait for the reader threads, or time out. If we time out, the
# threads remain reading and the fds left open in case the user
@@ -1320,16 +1317,6 @@ class Popen(object):
errread, errwrite)
- def _close_fds(self, fds_to_keep):
- start_fd = 3
- for fd in sorted(fds_to_keep):
- if fd >= start_fd:
- os.closerange(start_fd, fd)
- start_fd = fd + 1
- if start_fd <= MAXFD:
- os.closerange(start_fd, MAXFD)
-
-
def _execute_child(self, args, executable, preexec_fn, close_fds,
pass_fds, cwd, env,
startupinfo, creationflags, shell,
@@ -1415,7 +1402,7 @@ class Popen(object):
# exception (limited in size)
errpipe_data = bytearray()
while True:
- part = _eintr_retry_call(os.read, errpipe_read, 50000)
+ part = os.read(errpipe_read, 50000)
errpipe_data += part
if not part or len(errpipe_data) > 50000:
break
@@ -1425,10 +1412,9 @@ class Popen(object):
if errpipe_data:
try:
- _eintr_retry_call(os.waitpid, self.pid, 0)
- except OSError as e:
- if e.errno != errno.ECHILD:
- raise
+ os.waitpid(self.pid, 0)
+ except ChildProcessError:
+ pass
try:
exception_name, hex_errno, err_msg = (
errpipe_data.split(b':', 2))
@@ -1511,10 +1497,8 @@ class Popen(object):
def _try_wait(self, wait_flags):
"""All callers to this function MUST hold self._waitpid_lock."""
try:
- (pid, sts) = _eintr_retry_call(os.waitpid, self.pid, wait_flags)
- except OSError as e:
- if e.errno != errno.ECHILD:
- raise
+ (pid, sts) = os.waitpid(self.pid, wait_flags)
+ except ChildProcessError:
# This happens if SIGCLD is set to be ignored or waiting
# for child processes has otherwise been disabled for our
# process. This child is dead, we can't get the status.
@@ -1626,12 +1610,9 @@ class Popen(object):
self._input_offset + _PIPE_BUF]
try:
self._input_offset += os.write(key.fd, chunk)
- except OSError as e:
- if e.errno == errno.EPIPE:
- selector.unregister(key.fileobj)
- key.fileobj.close()
- else:
- raise
+ except BrokenPipeError:
+ selector.unregister(key.fileobj)
+ key.fileobj.close()
else:
if self._input_offset >= len(self._input):
selector.unregister(key.fileobj)
diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py
index dbf7767..137932e 100644
--- a/Lib/sysconfig.py
+++ b/Lib/sysconfig.py
@@ -57,7 +57,7 @@ _INSTALL_SCHEMES = {
'purelib': '{userbase}/Python{py_version_nodot}/site-packages',
'platlib': '{userbase}/Python{py_version_nodot}/site-packages',
'include': '{userbase}/Python{py_version_nodot}/Include',
- 'scripts': '{userbase}/Scripts',
+ 'scripts': '{userbase}/Python{py_version_nodot}/Scripts',
'data': '{userbase}',
},
'posix_user': {
@@ -109,13 +109,8 @@ else:
# unable to retrieve the real program name
_PROJECT_BASE = _safe_realpath(os.getcwd())
-if os.name == "nt" and "pcbuild" in _PROJECT_BASE[-8:].lower():
- _PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir))
-# PC/VS7.1
-if os.name == "nt" and "\\pc\\v" in _PROJECT_BASE[-10:].lower():
- _PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir, pardir))
-# PC/AMD64
-if os.name == "nt" and "\\pcbuild\\amd64" in _PROJECT_BASE[-14:].lower():
+if (os.name == 'nt' and
+ _PROJECT_BASE.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))):
_PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir, pardir))
# set for cross builds
@@ -129,11 +124,9 @@ def _is_python_source_dir(d):
return False
_sys_home = getattr(sys, '_home', None)
-if _sys_home and os.name == 'nt' and \
- _sys_home.lower().endswith(('pcbuild', 'pcbuild\\amd64')):
- _sys_home = os.path.dirname(_sys_home)
- if _sys_home.endswith('pcbuild'): # must be amd64
- _sys_home = os.path.dirname(_sys_home)
+if (_sys_home and os.name == 'nt' and
+ _sys_home.lower().endswith(('\\pcbuild\\win32', '\\pcbuild\\amd64'))):
+ _sys_home = os.path.dirname(os.path.dirname(_sys_home))
def is_python_build(check_home=False):
if check_home and _sys_home:
return _is_python_source_dir(_sys_home)
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 37e4dcd..ea7a89a 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1409,9 +1409,9 @@ class TarFile(object):
can be determined, `mode' is overridden by `fileobj's mode.
`fileobj' is not closed, when TarFile is closed.
"""
- modes = {"r": "rb", "a": "r+b", "w": "wb"}
+ modes = {"r": "rb", "a": "r+b", "w": "wb", "x": "xb"}
if mode not in modes:
- raise ValueError("mode must be 'r', 'a' or 'w'")
+ raise ValueError("mode must be 'r', 'a', 'w' or 'x'")
self.mode = mode
self._mode = modes[mode]
@@ -1524,6 +1524,15 @@ class TarFile(object):
'w:bz2' open for writing with bzip2 compression
'w:xz' open for writing with lzma compression
+ 'x' or 'x:' create a tarfile exclusively without compression, raise
+ an exception if the file is already created
+ 'x:gz' create an gzip compressed tarfile, raise an exception
+ if the file is already created
+ 'x:bz2' create an bzip2 compressed tarfile, raise an exception
+ if the file is already created
+ 'x:xz' create an lzma compressed tarfile, raise an exception
+ if the file is already created
+
'r|*' open a stream of tar blocks with transparent compression
'r|' open an uncompressed stream of tar blocks for reading
'r|gz' open a gzip compressed stream of tar blocks
@@ -1582,7 +1591,7 @@ class TarFile(object):
t._extfileobj = False
return t
- elif mode in ("a", "w"):
+ elif mode in ("a", "w", "x"):
return cls.taropen(name, mode, fileobj, **kwargs)
raise ValueError("undiscernible mode")
@@ -1591,8 +1600,8 @@ class TarFile(object):
def taropen(cls, name, mode="r", fileobj=None, **kwargs):
"""Open uncompressed tar archive name for reading or writing.
"""
- if mode not in ("r", "a", "w"):
- raise ValueError("mode must be 'r', 'a' or 'w'")
+ if mode not in ("r", "a", "w", "x"):
+ raise ValueError("mode must be 'r', 'a', 'w' or 'x'")
return cls(name, mode, fileobj, **kwargs)
@classmethod
@@ -1600,8 +1609,8 @@ class TarFile(object):
"""Open gzip compressed tar archive name for reading or writing.
Appending is not allowed.
"""
- if mode not in ("r", "w"):
- raise ValueError("mode must be 'r' or 'w'")
+ if mode not in ("r", "w", "x"):
+ raise ValueError("mode must be 'r', 'w' or 'x'")
try:
import gzip
@@ -1634,8 +1643,8 @@ class TarFile(object):
"""Open bzip2 compressed tar archive name for reading or writing.
Appending is not allowed.
"""
- if mode not in ("r", "w"):
- raise ValueError("mode must be 'r' or 'w'.")
+ if mode not in ("r", "w", "x"):
+ raise ValueError("mode must be 'r', 'w' or 'x'")
try:
import bz2
@@ -1663,8 +1672,8 @@ class TarFile(object):
"""Open lzma compressed tar archive name for reading or writing.
Appending is not allowed.
"""
- if mode not in ("r", "w"):
- raise ValueError("mode must be 'r' or 'w'")
+ if mode not in ("r", "w", "x"):
+ raise ValueError("mode must be 'r', 'w' or 'x'")
try:
import lzma
@@ -1751,7 +1760,7 @@ class TarFile(object):
addfile(). If given, `arcname' specifies an alternative name for the
file in the archive.
"""
- self._check("aw")
+ self._check("awx")
# When fileobj is given, replace name by
# fileobj's real name.
@@ -1842,14 +1851,17 @@ class TarFile(object):
tarinfo.devminor = os.minor(statres.st_rdev)
return tarinfo
- def list(self, verbose=True):
+ def list(self, verbose=True, *, members=None):
"""Print a table of contents to sys.stdout. If `verbose' is False, only
the names of the members are printed. If it is True, an `ls -l'-like
- output is produced.
+ output is produced. `members' is optional and must be a subset of the
+ list returned by getmembers().
"""
self._check()
- for tarinfo in self:
+ if members is None:
+ members = self
+ for tarinfo in members:
if verbose:
_safe_print(stat.filemode(tarinfo.mode))
_safe_print("%s/%s" % (tarinfo.uname or tarinfo.uid,
@@ -1882,7 +1894,7 @@ class TarFile(object):
TarInfo object, if it returns None the TarInfo object will be
excluded from the archive.
"""
- self._check("aw")
+ self._check("awx")
if arcname is None:
arcname = name
@@ -1939,7 +1951,7 @@ class TarFile(object):
On Windows platforms, `fileobj' should always be opened with mode
'rb' to avoid irritation about the file size.
"""
- self._check("aw")
+ self._check("awx")
tarinfo = copy.copy(tarinfo)
diff --git a/Lib/telnetlib.py b/Lib/telnetlib.py
index 0cacac8..eebb952 100644
--- a/Lib/telnetlib.py
+++ b/Lib/telnetlib.py
@@ -36,10 +36,7 @@ To do:
import sys
import socket
import selectors
-try:
- from time import monotonic as _time
-except ImportError:
- from time import time as _time
+from time import monotonic as _time
__all__ = ["Telnet"]
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index b6eeca3..0522c79 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -473,6 +473,11 @@ if _os.name != 'posix' or _os.sys.platform == 'cygwin':
TemporaryFile = NamedTemporaryFile
else:
+ # Is the O_TMPFILE flag available and does it work?
+ # The flag is set to False if os.open(dir, os.O_TMPFILE) raises an
+ # IsADirectoryError exception
+ _O_TMPFILE_WORKS = hasattr(_os, 'O_TMPFILE')
+
def TemporaryFile(mode='w+b', buffering=-1, encoding=None,
newline=None, suffix="", prefix=template,
dir=None):
@@ -488,11 +493,32 @@ else:
Returns an object with a file-like interface. The file has no
name, and will cease to exist when it is closed.
"""
+ global _O_TMPFILE_WORKS
if dir is None:
dir = gettempdir()
flags = _bin_openflags
+ if _O_TMPFILE_WORKS:
+ try:
+ flags2 = (flags | _os.O_TMPFILE) & ~_os.O_CREAT
+ fd = _os.open(dir, flags2, 0o600)
+ except IsADirectoryError:
+ # Linux kernel older than 3.11 ignores O_TMPFILE flag.
+ # Set flag to False to not try again.
+ _O_TMPFILE_WORKS = False
+ except OSError:
+ # The filesystem of the directory does not support O_TMPFILE.
+ # For example, OSError(95, 'Operation not supported').
+ pass
+ else:
+ try:
+ return _io.open(fd, mode, buffering=buffering,
+ newline=newline, encoding=encoding)
+ except:
+ _os.close(fd)
+ raise
+ # Fallback to _mkstemp_inner().
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
try:
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 9466d4e..d048f6f 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -2551,7 +2551,7 @@ class _TestPicklingConnections(BaseTestCase):
l = socket.socket()
l.bind((test.support.HOST, 0))
- l.listen(1)
+ l.listen()
conn.send(l.getsockname())
new_conn, addr = l.accept()
conn.send(new_conn)
@@ -3198,7 +3198,7 @@ class TestWait(unittest.TestCase):
from multiprocessing.connection import wait
l = socket.socket()
l.bind((test.support.HOST, 0))
- l.listen(4)
+ l.listen()
addr = l.getsockname()
readers = []
procs = []
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index f42b0f9..7935cf2 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -3,6 +3,7 @@
See http://www.zope.org/Members/fdrake/DateTimeWiki/TestCases
"""
+import decimal
import sys
import pickle
import random
@@ -50,6 +51,17 @@ class TestModule(unittest.TestCase):
self.assertEqual(datetime.MINYEAR, 1)
self.assertEqual(datetime.MAXYEAR, 9999)
+ def test_name_cleanup(self):
+ if '_Fast' not in str(self):
+ return
+ datetime = datetime_module
+ names = set(name for name in dir(datetime)
+ if not name.startswith('__') and not name.endswith('__'))
+ allowed = set(['MAXYEAR', 'MINYEAR', 'date', 'datetime',
+ 'datetime_CAPI', 'time', 'timedelta', 'timezone',
+ 'tzinfo'])
+ self.assertEqual(names - allowed, set([]))
+
#############################################################################
# tzinfo tests
@@ -616,8 +628,12 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
# Single-field rounding.
eq(td(milliseconds=0.4/1000), td(0)) # rounds to 0
eq(td(milliseconds=-0.4/1000), td(0)) # rounds to 0
+ eq(td(milliseconds=0.5/1000), td(microseconds=0))
+ eq(td(milliseconds=-0.5/1000), td(microseconds=0))
eq(td(milliseconds=0.6/1000), td(microseconds=1))
eq(td(milliseconds=-0.6/1000), td(microseconds=-1))
+ eq(td(seconds=0.5/10**6), td(microseconds=0))
+ eq(td(seconds=-0.5/10**6), td(microseconds=0))
# Rounding due to contributions from more than one field.
us_per_hour = 3600e6
@@ -1131,11 +1147,13 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
#check that this standard extension works
t.strftime("%f")
-
def test_format(self):
dt = self.theclass(2007, 9, 10)
self.assertEqual(dt.__format__(''), str(dt))
+ with self.assertRaisesRegex(TypeError, '^must be str, not int$'):
+ dt.__format__(123)
+
# check that a derived class's __str__() gets called
class A(self.theclass):
def __str__(self):
@@ -1287,8 +1305,6 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
return isinstance(other, LargerThanAnything)
def __eq__(self, other):
return isinstance(other, LargerThanAnything)
- def __ne__(self, other):
- return not isinstance(other, LargerThanAnything)
def __gt__(self, other):
return not isinstance(other, LargerThanAnything)
def __ge__(self, other):
@@ -1391,9 +1407,10 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
for month_byte in b'9', b'\0', b'\r', b'\xff':
self.assertRaises(TypeError, self.theclass,
base[:2] + month_byte + base[3:])
- # Good bytes, but bad tzinfo:
- self.assertRaises(TypeError, self.theclass,
- bytes([1] * len(base)), 'EST')
+ if issubclass(self.theclass, datetime):
+ # Good bytes, but bad tzinfo:
+ with self.assertRaisesRegex(TypeError, '^bad tzinfo state arg$'):
+ self.theclass(bytes([1] * len(base)), 'EST')
for ord_byte in range(1, 13):
# This shouldn't blow up because of the month byte alone. If
@@ -1469,6 +1486,9 @@ class TestDateTime(TestDate):
dt = self.theclass(2007, 9, 10, 4, 5, 1, 123)
self.assertEqual(dt.__format__(''), str(dt))
+ with self.assertRaisesRegex(TypeError, '^must be str, not int$'):
+ dt.__format__(123)
+
# check that a derived class's __str__() gets called
class A(self.theclass):
def __str__(self):
@@ -1790,6 +1810,7 @@ class TestDateTime(TestDate):
tzinfo=timezone(timedelta(hours=-5), 'EST'))
self.assertEqual(t.timestamp(),
18000 + 3600 + 2*60 + 3 + 4*1e-6)
+
def test_microsecond_rounding(self):
for fts in [self.theclass.fromtimestamp,
self.theclass.utcfromtimestamp]:
@@ -1840,6 +1861,7 @@ class TestDateTime(TestDate):
for insane in -1e200, 1e200:
self.assertRaises(OverflowError, self.theclass.utcfromtimestamp,
insane)
+
@unittest.skipIf(sys.platform == "win32", "Windows doesn't accept negative timestamps")
def test_negative_float_fromtimestamp(self):
# The result is tz-dependent; at least test that this doesn't
@@ -2219,6 +2241,9 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
t = self.theclass(1, 2, 3, 4)
self.assertEqual(t.__format__(''), str(t))
+ with self.assertRaisesRegex(TypeError, '^must be str, not int$'):
+ t.__format__(123)
+
# check that a derived class's __str__() gets called
class A(self.theclass):
def __str__(self):
@@ -2282,13 +2307,14 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
self.assertEqual(orig, derived)
def test_bool(self):
+ # time is always True.
cls = self.theclass
self.assertTrue(cls(1))
self.assertTrue(cls(0, 1))
self.assertTrue(cls(0, 0, 1))
self.assertTrue(cls(0, 0, 0, 1))
- self.assertFalse(cls(0))
- self.assertFalse(cls())
+ self.assertTrue(cls(0))
+ self.assertTrue(cls())
def test_replace(self):
cls = self.theclass
@@ -2347,6 +2373,9 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
for hour_byte in ' ', '9', chr(24), '\xff':
self.assertRaises(TypeError, self.theclass,
hour_byte + base[1:])
+ # Good bytes, but bad tzinfo:
+ with self.assertRaisesRegex(TypeError, '^bad tzinfo state arg$'):
+ self.theclass(bytes([1] * len(base)), 'EST')
# A mixin for classes with a tzinfo= argument. Subclasses must define
# theclass as a class atribute, and theclass(1, 1, 1, tzinfo=whatever)
@@ -2606,7 +2635,7 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
self.assertRaises(TypeError, t.strftime, "%Z")
# Issue #6697:
- if '_Fast' in str(type(self)):
+ if '_Fast' in str(self):
Badtzname.tz = '\ud800'
self.assertRaises(ValueError, t.strftime, "%Z")
@@ -2641,7 +2670,7 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
self.assertEqual(derived.tzname(), 'cookie')
def test_more_bool(self):
- # Test cases with non-None tzinfo.
+ # time is always True.
cls = self.theclass
t = cls(0, tzinfo=FixedOffset(-300, ""))
@@ -2651,23 +2680,11 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
self.assertTrue(t)
t = cls(5, tzinfo=FixedOffset(300, ""))
- self.assertFalse(t)
+ self.assertTrue(t)
t = cls(23, 59, tzinfo=FixedOffset(23*60 + 59, ""))
- self.assertFalse(t)
-
- # Mostly ensuring this doesn't overflow internally.
- t = cls(0, tzinfo=FixedOffset(23*60 + 59, ""))
self.assertTrue(t)
- # But this should yield a value error -- the utcoffset is bogus.
- t = cls(0, tzinfo=FixedOffset(24*60, ""))
- self.assertRaises(ValueError, lambda: bool(t))
-
- # Likewise.
- t = cls(0, tzinfo=FixedOffset(-24*60, ""))
- self.assertRaises(ValueError, lambda: bool(t))
-
def test_replace(self):
cls = self.theclass
z100 = FixedOffset(100, "+100")
@@ -3780,6 +3797,60 @@ class Oddballs(unittest.TestCase):
self.assertEqual(as_datetime, datetime_sc)
self.assertEqual(datetime_sc, as_datetime)
+ def test_extra_attributes(self):
+ for x in [date.today(),
+ time(),
+ datetime.utcnow(),
+ timedelta(),
+ tzinfo(),
+ timezone(timedelta())]:
+ with self.assertRaises(AttributeError):
+ x.abc = 1
+
+ def test_check_arg_types(self):
+ class Number:
+ def __init__(self, value):
+ self.value = value
+ def __int__(self):
+ return self.value
+
+ for xx in [decimal.Decimal(10),
+ decimal.Decimal('10.9'),
+ Number(10)]:
+ self.assertEqual(datetime(10, 10, 10, 10, 10, 10, 10),
+ datetime(xx, xx, xx, xx, xx, xx, xx))
+
+ with self.assertRaisesRegex(TypeError, '^an integer is required '
+ '\(got type str\)$'):
+ datetime(10, 10, '10')
+
+ f10 = Number(10.9)
+ with self.assertRaisesRegex(TypeError, '^__int__ returned non-int '
+ '\(type float\)$'):
+ datetime(10, 10, f10)
+
+ class Float(float):
+ pass
+ s10 = Float(10.9)
+ with self.assertRaisesRegex(TypeError, '^integer argument expected, '
+ 'got float$'):
+ datetime(10, 10, s10)
+
+ with self.assertRaises(TypeError):
+ datetime(10., 10, 10)
+ with self.assertRaises(TypeError):
+ datetime(10, 10., 10)
+ with self.assertRaises(TypeError):
+ datetime(10, 10, 10.)
+ with self.assertRaises(TypeError):
+ datetime(10, 10, 10, 10.)
+ with self.assertRaises(TypeError):
+ datetime(10, 10, 10, 10, 10.)
+ with self.assertRaises(TypeError):
+ datetime(10, 10, 10, 10, 10, 10.)
+ with self.assertRaises(TypeError):
+ datetime(10, 10, 10, 10, 10, 10, 10.)
+
def test_main():
support.run_unittest(__name__)
diff --git a/Lib/test/eintrdata/eintr_tester.py b/Lib/test/eintrdata/eintr_tester.py
new file mode 100644
index 0000000..40dca84
--- /dev/null
+++ b/Lib/test/eintrdata/eintr_tester.py
@@ -0,0 +1,260 @@
+"""
+This test suite exercises some system calls subject to interruption with EINTR,
+to check that it is actually handled transparently.
+It is intended to be run by the main test suite within a child process, to
+ensure there is no background thread running (so that signals are delivered to
+the correct thread).
+Signals are generated in-process using setitimer(ITIMER_REAL), which allows
+sub-second periodicity (contrarily to signal()).
+"""
+
+import io
+import os
+import signal
+import socket
+import time
+import unittest
+
+from test import support
+
+
+@unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
+class EINTRBaseTest(unittest.TestCase):
+ """ Base class for EINTR tests. """
+
+ # delay for initial signal delivery
+ signal_delay = 0.1
+ # signal delivery periodicity
+ signal_period = 0.1
+ # default sleep time for tests - should obviously have:
+ # sleep_time > signal_period
+ sleep_time = 0.2
+
+ @classmethod
+ def setUpClass(cls):
+ cls.orig_handler = signal.signal(signal.SIGALRM, lambda *args: None)
+ signal.setitimer(signal.ITIMER_REAL, cls.signal_delay,
+ cls.signal_period)
+
+ @classmethod
+ def tearDownClass(cls):
+ signal.setitimer(signal.ITIMER_REAL, 0, 0)
+ signal.signal(signal.SIGALRM, cls.orig_handler)
+
+ @classmethod
+ def _sleep(cls):
+ # default sleep time
+ time.sleep(cls.sleep_time)
+
+
+@unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
+class OSEINTRTest(EINTRBaseTest):
+ """ EINTR tests for the os module. """
+
+ def _test_wait_multiple(self, wait_func):
+ num = 3
+ for _ in range(num):
+ pid = os.fork()
+ if pid == 0:
+ self._sleep()
+ os._exit(0)
+ for _ in range(num):
+ wait_func()
+
+ def test_wait(self):
+ self._test_wait_multiple(os.wait)
+
+ @unittest.skipUnless(hasattr(os, 'wait3'), 'requires wait3()')
+ def test_wait3(self):
+ self._test_wait_multiple(lambda: os.wait3(0))
+
+ def _test_wait_single(self, wait_func):
+ pid = os.fork()
+ if pid == 0:
+ self._sleep()
+ os._exit(0)
+ else:
+ wait_func(pid)
+
+ def test_waitpid(self):
+ self._test_wait_single(lambda pid: os.waitpid(pid, 0))
+
+ @unittest.skipUnless(hasattr(os, 'wait4'), 'requires wait4()')
+ def test_wait4(self):
+ self._test_wait_single(lambda pid: os.wait4(pid, 0))
+
+ def test_read(self):
+ rd, wr = os.pipe()
+ self.addCleanup(os.close, rd)
+ # wr closed explicitly by parent
+
+ # the payload below are smaller than PIPE_BUF, hence the writes will be
+ # atomic
+ datas = [b"hello", b"world", b"spam"]
+
+ pid = os.fork()
+ if pid == 0:
+ os.close(rd)
+ for data in datas:
+ # let the parent block on read()
+ self._sleep()
+ os.write(wr, data)
+ os._exit(0)
+ else:
+ self.addCleanup(os.waitpid, pid, 0)
+ os.close(wr)
+ for data in datas:
+ self.assertEqual(data, os.read(rd, len(data)))
+
+ def test_write(self):
+ rd, wr = os.pipe()
+ self.addCleanup(os.close, wr)
+ # rd closed explicitly by parent
+
+ # we must write enough data for the write() to block
+ data = b"xyz" * support.PIPE_MAX_SIZE
+
+ pid = os.fork()
+ if pid == 0:
+ os.close(wr)
+ read_data = io.BytesIO()
+ # let the parent block on write()
+ self._sleep()
+ while len(read_data.getvalue()) < len(data):
+ chunk = os.read(rd, 2 * len(data))
+ read_data.write(chunk)
+ self.assertEqual(read_data.getvalue(), data)
+ os._exit(0)
+ else:
+ os.close(rd)
+ written = 0
+ while written < len(data):
+ written += os.write(wr, memoryview(data)[written:])
+ self.assertEqual(0, os.waitpid(pid, 0)[1])
+
+
+@unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
+class SocketEINTRTest(EINTRBaseTest):
+ """ EINTR tests for the socket module. """
+
+ @unittest.skipUnless(hasattr(socket, 'socketpair'), 'needs socketpair()')
+ def _test_recv(self, recv_func):
+ rd, wr = socket.socketpair()
+ self.addCleanup(rd.close)
+ # wr closed explicitly by parent
+
+ # single-byte payload guard us against partial recv
+ datas = [b"x", b"y", b"z"]
+
+ pid = os.fork()
+ if pid == 0:
+ rd.close()
+ for data in datas:
+ # let the parent block on recv()
+ self._sleep()
+ wr.sendall(data)
+ os._exit(0)
+ else:
+ self.addCleanup(os.waitpid, pid, 0)
+ wr.close()
+ for data in datas:
+ self.assertEqual(data, recv_func(rd, len(data)))
+
+ def test_recv(self):
+ self._test_recv(socket.socket.recv)
+
+ @unittest.skipUnless(hasattr(socket.socket, 'recvmsg'), 'needs recvmsg()')
+ def test_recvmsg(self):
+ self._test_recv(lambda sock, data: sock.recvmsg(data)[0])
+
+ def _test_send(self, send_func):
+ rd, wr = socket.socketpair()
+ self.addCleanup(wr.close)
+ # rd closed explicitly by parent
+
+ # we must send enough data for the send() to block
+ data = b"xyz" * (support.SOCK_MAX_SIZE // 3)
+
+ pid = os.fork()
+ if pid == 0:
+ wr.close()
+ # let the parent block on send()
+ self._sleep()
+ received_data = bytearray(len(data))
+ n = 0
+ while n < len(data):
+ n += rd.recv_into(memoryview(received_data)[n:])
+ self.assertEqual(received_data, data)
+ os._exit(0)
+ else:
+ rd.close()
+ written = 0
+ while written < len(data):
+ sent = send_func(wr, memoryview(data)[written:])
+ # sendall() returns None
+ written += len(data) if sent is None else sent
+ self.assertEqual(0, os.waitpid(pid, 0)[1])
+
+ def test_send(self):
+ self._test_send(socket.socket.send)
+
+ def test_sendall(self):
+ self._test_send(socket.socket.sendall)
+
+ @unittest.skipUnless(hasattr(socket.socket, 'sendmsg'), 'needs sendmsg()')
+ def test_sendmsg(self):
+ self._test_send(lambda sock, data: sock.sendmsg([data]))
+
+ def test_accept(self):
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.addCleanup(sock.close)
+
+ sock.bind((support.HOST, 0))
+ _, port = sock.getsockname()
+ sock.listen()
+
+ pid = os.fork()
+ if pid == 0:
+ # let parent block on accept()
+ self._sleep()
+ with socket.create_connection((support.HOST, port)):
+ self._sleep()
+ os._exit(0)
+ else:
+ self.addCleanup(os.waitpid, pid, 0)
+ client_sock, _ = sock.accept()
+ client_sock.close()
+
+ @unittest.skipUnless(hasattr(os, 'mkfifo'), 'needs mkfifo()')
+ def _test_open(self, do_open_close_reader, do_open_close_writer):
+ # Use a fifo: until the child opens it for reading, the parent will
+ # block when trying to open it for writing.
+ support.unlink(support.TESTFN)
+ os.mkfifo(support.TESTFN)
+ self.addCleanup(support.unlink, support.TESTFN)
+
+ pid = os.fork()
+ if pid == 0:
+ # let the parent block
+ self._sleep()
+ do_open_close_reader(support.TESTFN)
+ os._exit(0)
+ else:
+ self.addCleanup(os.waitpid, pid, 0)
+ do_open_close_writer(support.TESTFN)
+
+ def test_open(self):
+ self._test_open(lambda path: open(path, 'r').close(),
+ lambda path: open(path, 'w').close())
+
+ def test_os_open(self):
+ self._test_open(lambda path: os.close(os.open(path, os.O_RDONLY)),
+ lambda path: os.close(os.open(path, os.O_WRONLY)))
+
+
+def test_main():
+ support.run_unittest(OSEINTRTest, SocketEINTRTest)
+
+
+if __name__ == "__main__":
+ test_main()
diff --git a/Lib/test/fork_wait.py b/Lib/test/fork_wait.py
index 19b54ec..713039d 100644
--- a/Lib/test/fork_wait.py
+++ b/Lib/test/fork_wait.py
@@ -48,7 +48,12 @@ class ForkWait(unittest.TestCase):
for i in range(NUM_THREADS):
_thread.start_new(self.f, (i,))
- time.sleep(LONGSLEEP)
+ # busy-loop to wait for threads
+ deadline = time.monotonic() + 10.0
+ while len(self.alive) < NUM_THREADS:
+ time.sleep(0.1)
+ if deadline < time.monotonic():
+ break
a = sorted(self.alive.keys())
self.assertEqual(a, list(range(NUM_THREADS)))
diff --git a/Lib/test/imghdrdata/python.exr b/Lib/test/imghdrdata/python.exr
new file mode 100644
index 0000000..773c81e
--- /dev/null
+++ b/Lib/test/imghdrdata/python.exr
Binary files differ
diff --git a/Lib/test/imghdrdata/python.webp b/Lib/test/imghdrdata/python.webp
new file mode 100644
index 0000000..e824ec7
--- /dev/null
+++ b/Lib/test/imghdrdata/python.webp
Binary files differ
diff --git a/Lib/test/inspect_fodder2.py b/Lib/test/inspect_fodder2.py
index bd7106f..e452235 100644
--- a/Lib/test/inspect_fodder2.py
+++ b/Lib/test/inspect_fodder2.py
@@ -109,3 +109,16 @@ def annotated(arg1: list):
#line 109
def keyword_only_arg(*, arg):
pass
+
+from functools import wraps
+
+def decorator(func):
+ @wraps(func)
+ def fake():
+ return 42
+ return fake
+
+#line 121
+@decorator
+def real():
+ return 20
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py
index 42e118b..9069337 100644
--- a/Lib/test/list_tests.py
+++ b/Lib/test/list_tests.py
@@ -30,6 +30,12 @@ class CommonTest(seq_tests.CommonTest):
self.assertNotEqual(id(a), id(b))
self.assertEqual(a, b)
+ def test_getitem_error(self):
+ msg = "list indices must be integers or slices"
+ with self.assertRaisesRegex(TypeError, msg):
+ a = []
+ a['a'] = "python"
+
def test_repr(self):
l0 = []
l2 = [0, 1, 2]
@@ -120,6 +126,10 @@ class CommonTest(seq_tests.CommonTest):
a[-1] = 9
self.assertEqual(a, self.type2test([5,6,7,8,9]))
+ msg = "list indices must be integers or slices"
+ with self.assertRaisesRegex(TypeError, msg):
+ a['a'] = "python"
+
def test_delitem(self):
a = self.type2test([0, 1])
del a[1]
diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py
index 1cbcea2..136f1c3 100644
--- a/Lib/test/lock_tests.py
+++ b/Lib/test/lock_tests.py
@@ -82,7 +82,13 @@ class BaseLockTests(BaseTestCase):
def test_repr(self):
lock = self.locktype()
- repr(lock)
+ self.assertRegex(repr(lock), "<unlocked .* object (.*)?at .*>")
+ del lock
+
+ def test_locked_repr(self):
+ lock = self.locktype()
+ lock.acquire()
+ self.assertRegex(repr(lock), "<locked .* object (.*)?at .*>")
del lock
def test_acquire_destroy(self):
diff --git a/Lib/test/mock_socket.py b/Lib/test/mock_socket.py
index e36724f..b28c473 100644
--- a/Lib/test/mock_socket.py
+++ b/Lib/test/mock_socket.py
@@ -35,8 +35,9 @@ class MockFile:
class MockSocket:
"""Mock socket object used by smtpd and smtplib tests.
"""
- def __init__(self):
+ def __init__(self, family=None):
global _reply_data
+ self.family = family
self.output = []
self.lines = []
if _reply_data:
@@ -101,15 +102,14 @@ class MockSocket:
return len(data)
def getpeername(self):
- return 'peer'
+ return ('peer-address', 'peer-port')
def close(self):
pass
def socket(family=None, type=None, proto=None):
- return MockSocket()
-
+ return MockSocket(family)
def create_connection(address, timeout=socket_module._GLOBAL_DEFAULT_TIMEOUT,
source_address=None):
@@ -144,13 +144,16 @@ def gethostname():
def gethostbyname(name):
return ""
+def getaddrinfo(*args, **kw):
+ return socket_module.getaddrinfo(*args, **kw)
gaierror = socket_module.gaierror
error = socket_module.error
# Constants
-AF_INET = None
-SOCK_STREAM = None
+AF_INET = socket_module.AF_INET
+AF_INET6 = socket_module.AF_INET6
+SOCK_STREAM = socket_module.SOCK_STREAM
SOL_SOCKET = None
SO_REUSEADDR = None
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 55205d1..f8372e3 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -1154,16 +1154,62 @@ class AbstractPickleTests(unittest.TestCase):
self.assertGreaterEqual(num_additems, 2)
def test_simple_newobj(self):
- x = object.__new__(SimpleNewObj) # avoid __init__
+ x = SimpleNewObj.__new__(SimpleNewObj, 0xface) # avoid __init__
x.abc = 666
for proto in protocols:
- s = self.dumps(x, proto)
- self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s),
- 2 <= proto < 4)
- self.assertEqual(opcode_in_pickle(pickle.NEWOBJ_EX, s),
- proto >= 4)
- y = self.loads(s) # will raise TypeError if __init__ called
- self.assert_is_copy(x, y)
+ with self.subTest(proto=proto):
+ s = self.dumps(x, proto)
+ if proto < 1:
+ self.assertIn(b'\nL64206', s) # LONG
+ else:
+ self.assertIn(b'M\xce\xfa', s) # BININT2
+ self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s),
+ 2 <= proto)
+ self.assertFalse(opcode_in_pickle(pickle.NEWOBJ_EX, s))
+ y = self.loads(s) # will raise TypeError if __init__ called
+ self.assert_is_copy(x, y)
+
+ def test_complex_newobj(self):
+ x = ComplexNewObj.__new__(ComplexNewObj, 0xface) # avoid __init__
+ x.abc = 666
+ for proto in protocols:
+ with self.subTest(proto=proto):
+ 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:
+ self.assertIn(b'\x8c\x04FACE', s) # SHORT_BINUNICODE
+ self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s),
+ 2 <= proto)
+ self.assertFalse(opcode_in_pickle(pickle.NEWOBJ_EX, s))
+ y = self.loads(s) # will raise TypeError if __init__ called
+ self.assert_is_copy(x, y)
+
+ def test_complex_newobj_ex(self):
+ x = ComplexNewObjEx.__new__(ComplexNewObjEx, 0xface) # avoid __init__
+ 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
+ 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),
+ 4 <= proto)
+ y = self.loads(s) # will raise TypeError if __init__ called
+ self.assert_is_copy(x, y)
def test_newobj_list_slots(self):
x = SlotList([1, 2, 3])
@@ -1645,6 +1691,27 @@ class AbstractPickleTests(unittest.TestCase):
unpickled = self.loads(self.dumps(method, proto))
self.assertEqual(method(*args), unpickled(*args))
+ def test_local_lookup_error(self):
+ # Test that whichmodule() errors out cleanly when looking up
+ # an assumed globally-reachable object fails.
+ def f():
+ pass
+ # Since the function is local, lookup will fail
+ for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
+ with self.assertRaises((AttributeError, pickle.PicklingError)):
+ pickletools.dis(self.dumps(f, proto))
+ # Same without a __module__ attribute (exercises a different path
+ # in _pickle.c).
+ del f.__module__
+ for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
+ with self.assertRaises((AttributeError, pickle.PicklingError)):
+ pickletools.dis(self.dumps(f, proto))
+ # Yet a different path.
+ f.__name__ = f.__qualname__
+ for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
+ with self.assertRaises((AttributeError, pickle.PicklingError)):
+ pickletools.dis(self.dumps(f, proto))
+
class BigmemPickleTests(unittest.TestCase):
@@ -1879,12 +1946,20 @@ myclasses = [MyInt, MyFloat,
class SlotList(MyList):
__slots__ = ["foo"]
-class SimpleNewObj(object):
- def __init__(self, a, b, c):
+class SimpleNewObj(int):
+ def __init__(self, *args, **kwargs):
# raise an error, to make sure this isn't called
raise TypeError("SimpleNewObj.__init__() didn't expect to get called")
def __eq__(self, other):
- return self.__dict__ == other.__dict__
+ return int(self) == int(other) and self.__dict__ == other.__dict__
+
+class ComplexNewObj(SimpleNewObj):
+ def __getnewargs__(self):
+ return ('%X' % self, 16)
+
+class ComplexNewObjEx(SimpleNewObj):
+ def __getnewargs_ex__(self):
+ return ('%X' % self,), {'base': 16}
class BadGetattr:
def __getattr__(self, key):
diff --git a/Lib/test/pystone.py b/Lib/test/pystone.py
index a41f1e5..1f67e66 100755
--- a/Lib/test/pystone.py
+++ b/Lib/test/pystone.py
@@ -41,7 +41,7 @@ Version History:
LOOPS = 50000
-from time import clock
+from time import time
__version__ = "1.2"
@@ -93,10 +93,10 @@ def Proc0(loops=LOOPS):
global PtrGlb
global PtrGlbNext
- starttime = clock()
+ starttime = time()
for i in range(loops):
pass
- nulltime = clock() - starttime
+ nulltime = time() - starttime
PtrGlbNext = Record()
PtrGlb = Record()
@@ -108,7 +108,7 @@ def Proc0(loops=LOOPS):
String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING"
Array2Glob[8][7] = 10
- starttime = clock()
+ starttime = time()
for i in range(loops):
Proc5()
@@ -134,7 +134,7 @@ def Proc0(loops=LOOPS):
IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1
IntLoc1 = Proc2(IntLoc1)
- benchtime = clock() - starttime - nulltime
+ benchtime = time() - starttime - nulltime
if benchtime == 0.0:
loopsPerBenchtime = 0.0
else:
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
index b633631..ea1287d 100755
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -1276,6 +1276,10 @@ def runtest_inner(test, verbose, quiet,
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:
diff --git a/Lib/test/script_helper.py b/Lib/test/script_helper.py
index 8c599d1..5531a34 100644
--- a/Lib/test/script_helper.py
+++ b/Lib/test/script_helper.py
@@ -19,7 +19,7 @@ from test.support import make_legacy_pyc, strip_python_stderr, temp_dir
# Cached result of the expensive test performed in the function below.
__cached_interp_requires_environment = None
-def _interpreter_requires_environment():
+def interpreter_requires_environment():
"""
Returns True if our sys.executable interpreter requires environment
variables in order to be able to run at all.
@@ -52,7 +52,7 @@ def _interpreter_requires_environment():
# Executing the interpreter in a subprocess
def _assert_python(expected_success, *args, **env_vars):
- env_required = _interpreter_requires_environment()
+ env_required = interpreter_requires_environment()
if '__isolated' in env_vars:
isolated = env_vars.pop('__isolated')
else:
diff --git a/Lib/test/ssl_servers.py b/Lib/test/ssl_servers.py
index 759b3f4..f9d30cf 100644
--- a/Lib/test/ssl_servers.py
+++ b/Lib/test/ssl_servers.py
@@ -150,7 +150,7 @@ class HTTPSServerThread(threading.Thread):
def make_https_server(case, *, context=None, certfile=CERTFILE,
host=HOST, handler_class=None):
if context is None:
- context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
# We assume the certfile contains both private key and certificate
context.load_cert_chain(certfile)
server = HTTPSServerThread(context, host, handler_class)
@@ -182,6 +182,8 @@ if __name__ == "__main__":
parser.add_argument('--curve-name', dest='curve_name', type=str,
action='store',
help='curve name for EC-based Diffie-Hellman')
+ parser.add_argument('--ciphers', dest='ciphers', type=str,
+ help='allowed cipher list')
parser.add_argument('--dh', dest='dh_file', type=str, action='store',
help='PEM file containing DH parameters')
args = parser.parse_args()
@@ -192,12 +194,14 @@ if __name__ == "__main__":
else:
handler_class = RootedHTTPRequestHandler
handler_class.root = os.getcwd()
- context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(CERTFILE)
if args.curve_name:
context.set_ecdh_curve(args.curve_name)
if args.dh_file:
context.load_dh_params(args.dh_file)
+ if args.ciphers:
+ context.set_ciphers(args.ciphers)
server = HTTPSServer(("", args.port), handler_class, context)
if args.verbose:
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 242a931..6e26474 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -1176,8 +1176,7 @@ class MixinStrUnicodeUserStringTest:
self.checkraises(TypeError, 'abc', '__mod__')
self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
self.checkraises(TypeError, '%s%s', '__mod__', (42,))
- with self.assertWarns(DeprecationWarning):
- self.checkraises(TypeError, '%c', '__mod__', (None,))
+ self.checkraises(TypeError, '%c', '__mod__', (None,))
self.checkraises(ValueError, '%(foo', '__mod__', {})
self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index d98068c..205c47c 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -1040,7 +1040,8 @@ def open_urlresource(url, *args, **kw):
# Verify the requirement before downloading the file
requires('urlfetch')
- print('\tfetching %s ...' % url, file=get_original_stdout())
+ if verbose:
+ print('\tfetching %s ...' % url, file=get_original_stdout())
opener = urllib.request.build_opener()
if gzip:
opener.addheaders.append(('Accept-Encoding', 'gzip'))
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 1164f3f..d7f90cd 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -20,15 +20,6 @@ class StdIOBuffer(StringIO):
class TestCase(unittest.TestCase):
- def assertEqual(self, obj1, obj2):
- if obj1 != obj2:
- print('')
- print(repr(obj1))
- print(repr(obj2))
- print(obj1)
- print(obj2)
- super(TestCase, self).assertEqual(obj1, obj2)
-
def setUp(self):
# The tests assume that line wrapping occurs at 80 columns, but this
# behaviour can be overridden by setting the COLUMNS environment
@@ -78,9 +69,6 @@ class NS(object):
def __eq__(self, other):
return vars(self) == vars(other)
- def __ne__(self, other):
- return not (self == other)
-
class ArgumentParserError(Exception):
@@ -765,6 +753,39 @@ class TestOptionalsActionCount(ParserTestCase):
]
+class TestOptionalsAllowLongAbbreviation(ParserTestCase):
+ """Allow long options to be abbreviated unambiguously"""
+
+ argument_signatures = [
+ Sig('--foo'),
+ Sig('--foobaz'),
+ Sig('--fooble', action='store_true'),
+ ]
+ failures = ['--foob 5', '--foob']
+ successes = [
+ ('', NS(foo=None, foobaz=None, fooble=False)),
+ ('--foo 7', NS(foo='7', foobaz=None, fooble=False)),
+ ('--fooba a', NS(foo=None, foobaz='a', fooble=False)),
+ ('--foobl --foo g', NS(foo='g', foobaz=None, fooble=True)),
+ ]
+
+
+class TestOptionalsDisallowLongAbbreviation(ParserTestCase):
+ """Do not allow abbreviations of long options at all"""
+
+ parser_signature = Sig(allow_abbrev=False)
+ argument_signatures = [
+ Sig('--foo'),
+ Sig('--foodle', action='store_true'),
+ Sig('--foonly'),
+ ]
+ failures = ['-foon 3', '--foon 3', '--food', '--food --foo 2']
+ successes = [
+ ('', NS(foo=None, foodle=False, foonly=None)),
+ ('--foo 3', NS(foo='3', foodle=False, foonly=None)),
+ ('--foonly 7 --foodle --foo 2', NS(foo='2', foodle=True, foonly='7')),
+ ]
+
# ================
# Positional tests
# ================
@@ -1993,14 +2014,9 @@ class TestAddSubparsers(TestCase):
'''))
def _test_subparser_help(self, args_str, expected_help):
- try:
+ with self.assertRaises(ArgumentParserError) as cm:
self.parser.parse_args(args_str.split())
- except ArgumentParserError:
- err = sys.exc_info()[1]
- if err.stdout != expected_help:
- print(repr(expected_help))
- print(repr(err.stdout))
- self.assertEqual(err.stdout, expected_help)
+ self.assertEqual(expected_help, cm.exception.stdout)
def test_subparser1_help(self):
self._test_subparser_help('5.0 1 -h', textwrap.dedent('''\
@@ -2846,15 +2862,15 @@ class TestGetDefault(TestCase):
def test_get_default(self):
parser = ErrorRaisingArgumentParser()
- self.assertEqual(None, parser.get_default("foo"))
- self.assertEqual(None, parser.get_default("bar"))
+ self.assertIsNone(parser.get_default("foo"))
+ self.assertIsNone(parser.get_default("bar"))
parser.add_argument("--foo")
- self.assertEqual(None, parser.get_default("foo"))
- self.assertEqual(None, parser.get_default("bar"))
+ self.assertIsNone(parser.get_default("foo"))
+ self.assertIsNone(parser.get_default("bar"))
parser.add_argument("--bar", type=int, default=42)
- self.assertEqual(None, parser.get_default("foo"))
+ self.assertIsNone(parser.get_default("foo"))
self.assertEqual(42, parser.get_default("bar"))
parser.set_defaults(foo="badger")
@@ -2869,18 +2885,16 @@ class TestNamespaceContainsSimple(TestCase):
def test_empty(self):
ns = argparse.Namespace()
- self.assertEqual('' in ns, False)
- self.assertEqual('' not in ns, True)
- self.assertEqual('x' in ns, False)
+ self.assertNotIn('', ns)
+ self.assertNotIn('x', ns)
def test_non_empty(self):
ns = argparse.Namespace(x=1, y=2)
- self.assertEqual('x' in ns, True)
- self.assertEqual('x' not in ns, False)
- self.assertEqual('y' in ns, True)
- self.assertEqual('' in ns, False)
- self.assertEqual('xx' in ns, False)
- self.assertEqual('z' in ns, False)
+ self.assertNotIn('', ns)
+ self.assertIn('x', ns)
+ self.assertIn('y', ns)
+ self.assertNotIn('xx', ns)
+ self.assertNotIn('z', ns)
# =====================
# Help formatting tests
@@ -2936,13 +2950,6 @@ class TestHelpFormattingMetaclass(type):
def _test(self, tester, parser_text):
expected_text = getattr(tester, self.func_suffix)
expected_text = textwrap.dedent(expected_text)
- if expected_text != parser_text:
- print(repr(expected_text))
- print(repr(parser_text))
- for char1, char2 in zip(expected_text, parser_text):
- if char1 != char2:
- print('first diff: %r %r' % (char1, char2))
- break
tester.assertEqual(expected_text, parser_text)
def test_format(self, tester):
@@ -4223,24 +4230,17 @@ class TestInvalidArgumentConstructors(TestCase):
self.assertValueError('foo', action='baz')
self.assertValueError('--foo', action=('store', 'append'))
parser = argparse.ArgumentParser()
- try:
+ with self.assertRaises(ValueError) as cm:
parser.add_argument("--foo", action="store-true")
- except ValueError:
- e = sys.exc_info()[1]
- expected = 'unknown action'
- msg = 'expected %r, found %r' % (expected, e)
- self.assertTrue(expected in str(e), msg)
+ self.assertIn('unknown action', str(cm.exception))
def test_multiple_dest(self):
parser = argparse.ArgumentParser()
parser.add_argument(dest='foo')
- try:
+ with self.assertRaises(ValueError) as cm:
parser.add_argument('bar', dest='baz')
- except ValueError:
- e = sys.exc_info()[1]
- expected = 'dest supplied twice for positional argument'
- msg = 'expected %r, found %r' % (expected, e)
- self.assertTrue(expected in str(e), msg)
+ self.assertIn('dest supplied twice for positional argument',
+ str(cm.exception))
def test_no_argument_actions(self):
for action in ['store_const', 'store_true', 'store_false',
@@ -4397,18 +4397,10 @@ class TestConflictHandling(TestCase):
class TestOptionalsHelpVersionActions(TestCase):
"""Test the help and version actions"""
- def _get_error(self, func, *args, **kwargs):
- try:
- func(*args, **kwargs)
- except ArgumentParserError:
- return sys.exc_info()[1]
- else:
- self.assertRaises(ArgumentParserError, func, *args, **kwargs)
-
def assertPrintHelpExit(self, parser, args_str):
- self.assertEqual(
- parser.format_help(),
- self._get_error(parser.parse_args, args_str.split()).stdout)
+ with self.assertRaises(ArgumentParserError) as cm:
+ parser.parse_args(args_str.split())
+ self.assertEqual(parser.format_help(), cm.exception.stdout)
def assertArgumentParserError(self, parser, *args):
self.assertRaises(ArgumentParserError, parser.parse_args, args)
@@ -4423,8 +4415,9 @@ class TestOptionalsHelpVersionActions(TestCase):
def test_version_format(self):
parser = ErrorRaisingArgumentParser(prog='PPP')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 3.5')
- msg = self._get_error(parser.parse_args, ['-v']).stdout
- self.assertEqual('PPP 3.5\n', msg)
+ with self.assertRaises(ArgumentParserError) as cm:
+ parser.parse_args(['-v'])
+ self.assertEqual('PPP 3.5\n', cm.exception.stdout)
def test_version_no_help(self):
parser = ErrorRaisingArgumentParser(add_help=False)
@@ -4436,8 +4429,9 @@ class TestOptionalsHelpVersionActions(TestCase):
def test_version_action(self):
parser = ErrorRaisingArgumentParser(prog='XXX')
parser.add_argument('-V', action='version', version='%(prog)s 3.7')
- msg = self._get_error(parser.parse_args, ['-V']).stdout
- self.assertEqual('XXX 3.7\n', msg)
+ with self.assertRaises(ArgumentParserError) as cm:
+ parser.parse_args(['-V'])
+ self.assertEqual('XXX 3.7\n', cm.exception.stdout)
def test_no_help(self):
parser = ErrorRaisingArgumentParser(add_help=False)
@@ -4607,14 +4601,10 @@ class TestArgumentTypeError(TestCase):
parser = ErrorRaisingArgumentParser(prog='PROG', add_help=False)
parser.add_argument('x', type=spam)
- try:
+ with self.assertRaises(ArgumentParserError) as cm:
parser.parse_args(['XXX'])
- except ArgumentParserError:
- expected = 'usage: PROG x\nPROG: error: argument x: spam!\n'
- msg = sys.exc_info()[1].stderr
- self.assertEqual(expected, msg)
- else:
- self.fail()
+ self.assertEqual('usage: PROG x\nPROG: error: argument x: spam!\n',
+ cm.exception.stderr)
# =========================
# MessageContentError tests
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index 07c9bf9..10d9946 100644
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -394,7 +394,9 @@ class BaseTest:
self.assertEqual(a, b)
def test_tofromstring(self):
- nb_warnings = 4
+ # Warnings not raised when arguments are incorrect as Argument Clinic
+ # handles that before the warning can be raised.
+ nb_warnings = 2
with warnings.catch_warnings(record=True) as r:
warnings.filterwarnings("always",
message=r"(to|from)string\(\) is deprecated",
@@ -1039,6 +1041,11 @@ class BaseTest:
a = array.array(self.typecode, "foo")
a = array.array(self.typecode, array.array('u', 'foo'))
+ @support.cpython_only
+ def test_obsolete_write_lock(self):
+ from _testcapi import getbuffer_with_null_view
+ a = array.array('B', b"")
+ self.assertRaises(BufferError, getbuffer_with_null_view, a)
class StringTest(BaseTest):
diff --git a/Lib/test/test_asdl_parser.py b/Lib/test/test_asdl_parser.py
new file mode 100644
index 0000000..7a6426a
--- /dev/null
+++ b/Lib/test/test_asdl_parser.py
@@ -0,0 +1,122 @@
+"""Tests for the asdl parser in Parser/asdl.py"""
+
+import importlib.machinery
+import os
+from os.path import dirname
+import sys
+import sysconfig
+import unittest
+
+
+# This test is only relevant for from-source builds of Python.
+if not sysconfig.is_python_build():
+ raise unittest.SkipTest('test irrelevant for an installed Python')
+
+src_base = dirname(dirname(dirname(__file__)))
+parser_dir = os.path.join(src_base, 'Parser')
+
+
+class TestAsdlParser(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ # Loads the asdl module dynamically, since it's not in a real importable
+ # package.
+ # Parses Python.asdl into a ast.Module and run the check on it.
+ # There's no need to do this for each test method, hence setUpClass.
+ sys.path.insert(0, parser_dir)
+ loader = importlib.machinery.SourceFileLoader(
+ 'asdl', os.path.join(parser_dir, 'asdl.py'))
+ cls.asdl = loader.load_module()
+ cls.mod = cls.asdl.parse(os.path.join(parser_dir, 'Python.asdl'))
+ cls.assertTrue(cls.asdl.check(cls.mod), 'Module validation failed')
+
+ @classmethod
+ def tearDownClass(cls):
+ del sys.path[0]
+
+ def setUp(self):
+ # alias stuff from the class, for convenience
+ self.asdl = TestAsdlParser.asdl
+ self.mod = TestAsdlParser.mod
+ self.types = self.mod.types
+
+ def test_module(self):
+ self.assertEqual(self.mod.name, 'Python')
+ self.assertIn('stmt', self.types)
+ self.assertIn('expr', self.types)
+ self.assertIn('mod', self.types)
+
+ def test_definitions(self):
+ defs = self.mod.dfns
+ self.assertIsInstance(defs[0], self.asdl.Type)
+ self.assertIsInstance(defs[0].value, self.asdl.Sum)
+
+ self.assertIsInstance(self.types['withitem'], self.asdl.Product)
+ self.assertIsInstance(self.types['alias'], self.asdl.Product)
+
+ def test_product(self):
+ alias = self.types['alias']
+ self.assertEqual(
+ str(alias),
+ 'Product([Field(identifier, name), Field(identifier, asname, opt=True)])')
+
+ def test_attributes(self):
+ stmt = self.types['stmt']
+ self.assertEqual(len(stmt.attributes), 2)
+ self.assertEqual(str(stmt.attributes[0]), 'Field(int, lineno)')
+ self.assertEqual(str(stmt.attributes[1]), 'Field(int, col_offset)')
+
+ def test_constructor_fields(self):
+ ehandler = self.types['excepthandler']
+ self.assertEqual(len(ehandler.types), 1)
+ self.assertEqual(len(ehandler.attributes), 2)
+
+ cons = ehandler.types[0]
+ self.assertIsInstance(cons, self.asdl.Constructor)
+ self.assertEqual(len(cons.fields), 3)
+
+ f0 = cons.fields[0]
+ self.assertEqual(f0.type, 'expr')
+ self.assertEqual(f0.name, 'type')
+ self.assertTrue(f0.opt)
+
+ f1 = cons.fields[1]
+ self.assertEqual(f1.type, 'identifier')
+ self.assertEqual(f1.name, 'name')
+ self.assertTrue(f1.opt)
+
+ f2 = cons.fields[2]
+ self.assertEqual(f2.type, 'stmt')
+ self.assertEqual(f2.name, 'body')
+ self.assertFalse(f2.opt)
+ self.assertTrue(f2.seq)
+
+ def test_visitor(self):
+ class CustomVisitor(self.asdl.VisitorBase):
+ def __init__(self):
+ super().__init__()
+ self.names_with_seq = []
+
+ def visitModule(self, mod):
+ for dfn in mod.dfns:
+ self.visit(dfn)
+
+ def visitType(self, type):
+ self.visit(type.value)
+
+ def visitSum(self, sum):
+ for t in sum.types:
+ self.visit(t)
+
+ def visitConstructor(self, cons):
+ for f in cons.fields:
+ if f.seq:
+ self.names_with_seq.append(cons.name)
+
+ v = CustomVisitor()
+ v.visit(self.types['mod'])
+ self.assertEqual(v.names_with_seq, ['Module', 'Interactive', 'Suite'])
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/Lib/test/test_asynchat.py b/Lib/test/test_asynchat.py
index 2dc9d0c..3a33fc8 100644
--- a/Lib/test/test_asynchat.py
+++ b/Lib/test/test_asynchat.py
@@ -12,6 +12,7 @@ import socket
import sys
import time
import unittest
+import warnings
import unittest.mock
try:
import threading
@@ -38,7 +39,7 @@ if threading:
self.start_resend_event = None
def run(self):
- self.sock.listen(1)
+ self.sock.listen()
self.event.set()
conn, client = self.sock.accept()
self.buffer = b""
@@ -298,7 +299,10 @@ class TestHelperFunctions(unittest.TestCase):
class TestFifo(unittest.TestCase):
def test_basic(self):
- f = asynchat.fifo()
+ 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)
@@ -313,7 +317,10 @@ class TestFifo(unittest.TestCase):
self.assertEqual(f.pop(), (0, None))
def test_given_list(self):
- f = asynchat.fifo([b'x', 17, 3])
+ 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))
diff --git a/Lib/test/test_asyncore.py b/Lib/test/test_asyncore.py
index d44726d..3857916 100644
--- a/Lib/test/test_asyncore.py
+++ b/Lib/test/test_asyncore.py
@@ -7,7 +7,6 @@ import sys
import time
import errno
import struct
-import warnings
from test import support
from io import BytesIO
@@ -65,7 +64,7 @@ class crashingdummy:
# used when testing senders; just collects what it gets until newline is sent
def capture_server(evt, buf, serv):
try:
- serv.listen(5)
+ serv.listen()
conn, addr = serv.accept()
except socket.timeout:
pass
@@ -298,23 +297,6 @@ class DispatcherTests(unittest.TestCase):
'warning: unhandled connect event']
self.assertEqual(lines, expected)
- def test_issue_8594(self):
- # XXX - this test is supposed to be removed in next major Python
- # version
- d = asyncore.dispatcher(socket.socket())
- # make sure the error message no longer refers to the socket
- # object but the dispatcher instance instead
- self.assertRaisesRegex(AttributeError, 'dispatcher instance',
- getattr, d, 'foo')
- # cheap inheritance with the underlying socket is supposed
- # to still work but a DeprecationWarning is expected
- with warnings.catch_warnings(record=True) as w:
- warnings.simplefilter("always")
- family = d.family
- self.assertEqual(family, socket.AF_INET)
- self.assertEqual(len(w), 1)
- self.assertTrue(issubclass(w[0].category, DeprecationWarning))
-
def test_strerror(self):
# refers to bug #8573
err = asyncore._strerror(errno.EPERM)
@@ -331,9 +313,8 @@ class dispatcherwithsend_noread(asyncore.dispatcher_with_send):
def handle_connect(self):
pass
-class DispatcherWithSendTests(unittest.TestCase):
- usepoll = False
+class DispatcherWithSendTests(unittest.TestCase):
def setUp(self):
pass
@@ -383,10 +364,6 @@ class DispatcherWithSendTests(unittest.TestCase):
self.fail("join() timed out")
-
-class DispatcherWithSendTests_UsePoll(DispatcherWithSendTests):
- usepoll = True
-
@unittest.skipUnless(hasattr(asyncore, 'file_wrapper'),
'asyncore.file_wrapper required')
class FileWrapperTest(unittest.TestCase):
diff --git a/Lib/test/test_augassign.py b/Lib/test/test_augassign.py
index 9a59c58..19b7687 100644
--- a/Lib/test/test_augassign.py
+++ b/Lib/test/test_augassign.py
@@ -136,6 +136,14 @@ class AugAssignTest(unittest.TestCase):
output.append("__imul__ called")
return self
+ def __matmul__(self, val):
+ output.append("__matmul__ called")
+ def __rmatmul__(self, val):
+ output.append("__rmatmul__ called")
+ def __imatmul__(self, val):
+ output.append("__imatmul__ called")
+ return self
+
def __div__(self, val):
output.append("__div__ called")
def __rdiv__(self, val):
@@ -233,6 +241,10 @@ class AugAssignTest(unittest.TestCase):
1 * x
x *= 1
+ x @ 1
+ 1 @ x
+ x @= 1
+
x / 1
1 / x
x /= 1
@@ -279,6 +291,9 @@ __isub__ called
__mul__ called
__rmul__ called
__imul__ called
+__matmul__ called
+__rmatmul__ called
+__imatmul__ called
__truediv__ called
__rtruediv__ called
__itruediv__ called
diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py
index 1667847..0976fa9 100644
--- a/Lib/test/test_buffer.py
+++ b/Lib/test/test_buffer.py
@@ -216,7 +216,7 @@ def iter_format(nitems, testobj='ndarray'):
for t in iter_mode(nitems, testobj):
yield t
if testobj != 'ndarray':
- raise StopIteration
+ return
yield struct_items(nitems, testobj)
@@ -1007,6 +1007,7 @@ class TestBufferProtocol(unittest.TestCase):
# shape, strides, offset
structure = (
([], [], 0),
+ ([1,3,1], [], 0),
([12], [], 0),
([12], [-1], 11),
([6], [2], 0),
@@ -1078,6 +1079,18 @@ class TestBufferProtocol(unittest.TestCase):
self.assertRaises(BufferError, ndarray, ex, getbuf=PyBUF_ANY_CONTIGUOUS)
nd = ndarray(ex, getbuf=PyBUF_SIMPLE)
+ # Issue #22445: New precise contiguity definition.
+ for shape in [1,12,1], [7,0,7]:
+ for order in 0, ND_FORTRAN:
+ ex = ndarray(items, shape=shape, flags=order|ND_WRITABLE)
+ self.assertTrue(is_contiguous(ex, 'F'))
+ self.assertTrue(is_contiguous(ex, 'C'))
+
+ for flags in requests:
+ nd = ndarray(ex, getbuf=flags)
+ self.assertTrue(is_contiguous(nd, 'F'))
+ self.assertTrue(is_contiguous(nd, 'C'))
+
def test_ndarray_exceptions(self):
nd = ndarray([9], [1])
ndm = ndarray([9], [1], flags=ND_VAREXPORT)
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 14366c6..6166da5 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -312,11 +312,11 @@ class BuiltinTest(unittest.TestCase):
self.assertRaises(TypeError, compile)
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'badmode')
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'single', 0xff)
- self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
+ self.assertRaises(ValueError, compile, chr(0), 'f', 'exec')
self.assertRaises(TypeError, compile, 'pass', '?', 'exec',
mode='eval', source='0', filename='tmp')
compile('print("\xe5")\n', '', 'exec')
- self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
+ self.assertRaises(ValueError, compile, chr(0), 'f', 'exec')
self.assertRaises(ValueError, compile, str('a = 1'), 'f', 'bad')
# test the optimize argument
@@ -1094,7 +1094,7 @@ class BuiltinTest(unittest.TestCase):
self.assertAlmostEqual(pow(-1, 0.5), 1j)
self.assertAlmostEqual(pow(-1, 1/3), 0.5 + 0.8660254037844386j)
- self.assertRaises(TypeError, pow, -1, -2, 3)
+ self.assertRaises(ValueError, pow, -1, -2, 3)
self.assertRaises(ValueError, pow, 1, 2, 0)
self.assertRaises(TypeError, pow)
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index e15807e..a9f64a0 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -461,6 +461,28 @@ class BaseBytesTest:
self.assertEqual(b.rindex(i, 3, 9), 7)
self.assertRaises(ValueError, b.rindex, w, 1, 3)
+ def test_mod(self):
+ b = b'hello, %b!'
+ orig = b
+ b = b % b'world'
+ self.assertEqual(b, b'hello, world!')
+ self.assertEqual(orig, b'hello, %b!')
+ self.assertFalse(b is orig)
+ b = b'%s / 100 = %d%%'
+ a = b % (b'seventy-nine', 79)
+ self.assertEqual(a, b'seventy-nine / 100 = 79%')
+
+ def test_imod(self):
+ b = b'hello, %b!'
+ orig = b
+ b %= b'world'
+ self.assertEqual(b, b'hello, world!')
+ self.assertEqual(orig, b'hello, %b!')
+ self.assertFalse(b is orig)
+ b = b'%s / 100 = %d%%'
+ b %= (b'seventy-nine', 79)
+ self.assertEqual(b, b'seventy-nine / 100 = 79%')
+
def test_replace(self):
b = self.type2test(b'mississippi')
self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
@@ -722,6 +744,11 @@ class BaseBytesTest:
class BytesTest(BaseBytesTest, unittest.TestCase):
type2test = bytes
+ def test_getitem_error(self):
+ msg = "byte indices must be integers or slices"
+ with self.assertRaisesRegex(TypeError, msg):
+ b'python'['a']
+
def test_buffer_is_readonly(self):
fd = os.open(__file__, os.O_RDONLY)
with open(fd, "rb", buffering=0) as f:
@@ -776,6 +803,17 @@ class BytesTest(BaseBytesTest, unittest.TestCase):
class ByteArrayTest(BaseBytesTest, unittest.TestCase):
type2test = bytearray
+ def test_getitem_error(self):
+ msg = "bytearray indices must be integers or slices"
+ with self.assertRaisesRegex(TypeError, msg):
+ bytearray(b'python')['a']
+
+ def test_setitem_error(self):
+ msg = "bytearray indices must be integers or slices"
+ with self.assertRaisesRegex(TypeError, msg):
+ b = bytearray(b'python')
+ b['a'] = "python"
+
def test_nohash(self):
self.assertRaises(TypeError, hash, bytearray())
@@ -974,6 +1012,28 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):
b[8:] = b
self.assertEqual(b, bytearray(list(range(8)) + list(range(256))))
+ def test_mod(self):
+ b = bytearray(b'hello, %b!')
+ orig = b
+ b = b % b'world'
+ self.assertEqual(b, b'hello, world!')
+ self.assertEqual(orig, bytearray(b'hello, %b!'))
+ self.assertFalse(b is orig)
+ b = bytearray(b'%s / 100 = %d%%')
+ a = b % (b'seventy-nine', 79)
+ self.assertEqual(a, bytearray(b'seventy-nine / 100 = 79%'))
+
+ def test_imod(self):
+ b = bytearray(b'hello, %b!')
+ orig = b
+ b %= b'world'
+ self.assertEqual(b, b'hello, world!')
+ self.assertEqual(orig, bytearray(b'hello, %b!'))
+ self.assertFalse(b is orig)
+ b = bytearray(b'%s / 100 = %d%%')
+ b %= (b'seventy-nine', 79)
+ self.assertEqual(b, bytearray(b'seventy-nine / 100 = 79%'))
+
def test_iconcat(self):
b = bytearray(b"abc")
b1 = b
@@ -1164,6 +1224,10 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):
self.assertRaises(BufferError, delslice)
self.assertEqual(b, orig)
+ @test.support.cpython_only
+ def test_obsolete_write_lock(self):
+ from _testcapi import getbuffer_with_null_view
+ self.assertRaises(BufferError, getbuffer_with_null_view, bytearray())
class AssortedBytesTest(unittest.TestCase):
#
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index ba7c38d..de8d65a 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -150,6 +150,23 @@ class CAPITest(unittest.TestCase):
self.assertEqual(_testcapi.docstring_with_signature_and_extra_newlines.__text_signature__,
"($module, /, parameter)")
+ def test_c_type_with_matrix_multiplication(self):
+ M = _testcapi.matmulType
+ m1 = M()
+ m2 = M()
+ self.assertEqual(m1 @ m2, ("matmul", m1, m2))
+ self.assertEqual(m1 @ 42, ("matmul", m1, 42))
+ self.assertEqual(42 @ m1, ("matmul", 42, m1))
+ o = m1
+ o @= m2
+ self.assertEqual(o, ("imatmul", m1, m2))
+ o = m1
+ o @= 42
+ self.assertEqual(o, ("imatmul", m1, 42))
+ o = 42
+ o @= m1
+ self.assertEqual(o, ("matmul", 42, m1))
+
@unittest.skipUnless(threading, 'Threading required for this test.')
class TestPendingCalls(unittest.TestCase):
@@ -268,7 +285,7 @@ class EmbeddingTests(unittest.TestCase):
exename += ext
exepath = os.path.dirname(sys.executable)
else:
- exepath = os.path.join(basepath, "Modules")
+ exepath = os.path.join(basepath, "Programs")
self.test_exe = exe = os.path.join(exepath, exename)
if not os.path.exists(exe):
self.skipTest("%r doesn't exist" % exe)
@@ -287,12 +304,13 @@ class EmbeddingTests(unittest.TestCase):
cmd.extend(args)
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
+ stderr=subprocess.PIPE,
+ universal_newlines=True)
(out, err) = p.communicate()
self.assertEqual(p.returncode, 0,
"bad returncode %d, stderr is %r" %
(p.returncode, err))
- return out.decode("latin1"), err.decode("latin1")
+ return out, err
def test_subinterps(self):
# This is just a "don't crash" test
@@ -319,34 +337,38 @@ class EmbeddingTests(unittest.TestCase):
print()
print(out)
print(err)
+ expected_errors = sys.__stdout__.errors
expected_stdin_encoding = sys.__stdin__.encoding
expected_pipe_encoding = self._get_default_pipe_encoding()
- expected_output = os.linesep.join([
+ expected_output = '\n'.join([
"--- Use defaults ---",
"Expected encoding: default",
"Expected errors: default",
- "stdin: {0}:strict",
- "stdout: {1}:strict",
- "stderr: {1}:backslashreplace",
+ "stdin: {in_encoding}:{errors}",
+ "stdout: {out_encoding}:{errors}",
+ "stderr: {out_encoding}:backslashreplace",
"--- Set errors only ---",
"Expected encoding: default",
- "Expected errors: surrogateescape",
- "stdin: {0}:surrogateescape",
- "stdout: {1}:surrogateescape",
- "stderr: {1}:backslashreplace",
+ "Expected errors: ignore",
+ "stdin: {in_encoding}:ignore",
+ "stdout: {out_encoding}:ignore",
+ "stderr: {out_encoding}:backslashreplace",
"--- Set encoding only ---",
"Expected encoding: latin-1",
"Expected errors: default",
- "stdin: latin-1:strict",
- "stdout: latin-1:strict",
+ "stdin: latin-1:{errors}",
+ "stdout: latin-1:{errors}",
"stderr: latin-1:backslashreplace",
"--- Set encoding and errors ---",
"Expected encoding: latin-1",
- "Expected errors: surrogateescape",
- "stdin: latin-1:surrogateescape",
- "stdout: latin-1:surrogateescape",
- "stderr: latin-1:backslashreplace"]).format(expected_stdin_encoding,
- expected_pipe_encoding)
+ "Expected errors: replace",
+ "stdin: latin-1:replace",
+ "stdout: latin-1:replace",
+ "stderr: latin-1:backslashreplace"])
+ expected_output = expected_output.format(
+ in_encoding=expected_stdin_encoding,
+ out_encoding=expected_pipe_encoding,
+ errors=expected_errors)
# This is useful if we ever trip over odd platform behaviour
self.maxDiff = None
self.assertEqual(out.strip(), expected_output)
diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py
index 1127dd1..715bd73 100644
--- a/Lib/test/test_cgi.py
+++ b/Lib/test/test_cgi.py
@@ -1,4 +1,4 @@
-from test.support import run_unittest, check_warnings
+from test.support import check_warnings
import cgi
import os
import sys
@@ -307,6 +307,17 @@ Content-Type: text/plain
got = getattr(files[x], k)
self.assertEqual(got, exp)
+ def test_fieldstorage_as_context_manager(self):
+ fp = BytesIO(b'x' * 10)
+ env = {'REQUEST_METHOD': 'PUT'}
+ with cgi.FieldStorage(fp=fp, environ=env) as fs:
+ content = fs.file.read()
+ self.assertFalse(fs.file.closed)
+ self.assertTrue(fs.file.closed)
+ self.assertEqual(content, 'x' * 10)
+ with self.assertRaisesRegex(ValueError, 'I/O operation on closed file'):
+ fs.file.read()
+
_qs_result = {
'key1': 'value1',
'key2': ['value2x', 'value2y'],
@@ -481,9 +492,5 @@ Content-Transfer-Encoding: binary
--AaB03x--
"""
-
-def test_main():
- run_unittest(CgiTests)
-
if __name__ == '__main__':
- test_main()
+ unittest.main()
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index 3683a48..ebe557e 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -440,7 +440,7 @@ class CmdLineTest(unittest.TestCase):
self.assertEqual(err.splitlines().count(b'Unknown option: -a'), 1)
self.assertEqual(b'', out)
- @unittest.skipIf(script_helper._interpreter_requires_environment(),
+ @unittest.skipIf(script_helper.interpreter_requires_environment(),
'Cannot run -I tests when PYTHON env vars are required.')
def test_isolatedmode(self):
self.verify_valid_flag('-I')
diff --git a/Lib/test/test_code_module.py b/Lib/test/test_code_module.py
index 7a80a80..9b17767 100644
--- a/Lib/test/test_code_module.py
+++ b/Lib/test/test_code_module.py
@@ -1,6 +1,7 @@
"Test InteractiveConsole and InteractiveInterpreter from code module"
import sys
import unittest
+from textwrap import dedent
from contextlib import ExitStack
from unittest import mock
from test import support
@@ -78,6 +79,40 @@ class TestInteractiveConsole(unittest.TestCase):
self.console.interact(banner='')
self.assertEqual(len(self.stderr.method_calls), 1)
+ def test_cause_tb(self):
+ self.infunc.side_effect = ["raise ValueError('') from AttributeError",
+ EOFError('Finished')]
+ self.console.interact()
+ output = ''.join(''.join(call[1]) for call in self.stderr.method_calls)
+ expected = dedent("""
+ AttributeError
+
+ The above exception was the direct cause of the following exception:
+
+ Traceback (most recent call last):
+ File "<console>", line 1, in <module>
+ ValueError
+ """)
+ self.assertIn(expected, output)
+
+ def test_context_tb(self):
+ self.infunc.side_effect = ["try: ham\nexcept: eggs\n",
+ EOFError('Finished')]
+ self.console.interact()
+ output = ''.join(''.join(call[1]) for call in self.stderr.method_calls)
+ expected = dedent("""
+ Traceback (most recent call last):
+ File "<console>", line 1, in <module>
+ NameError: name 'ham' is not defined
+
+ During handling of the above exception, another exception occurred:
+
+ Traceback (most recent call last):
+ File "<console>", line 2, in <module>
+ NameError: name 'eggs' is not defined
+ """)
+ self.assertIn(expected, output)
+
def test_main():
support.run_unittest(TestInteractiveConsole)
diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py
index 84804bb..e29ac53 100644
--- a/Lib/test/test_codeccallbacks.py
+++ b/Lib/test/test_codeccallbacks.py
@@ -158,6 +158,22 @@ class CodecCallbackTest(unittest.TestCase):
sout = b"a\xac\\u1234\xa4\\u8000\\U0010ffff"
self.assertEqual(sin.encode("iso-8859-15", "backslashreplace"), sout)
+ def test_nameescape(self):
+ # Does the same as backslashescape, but prefers ``\N{...}`` escape
+ # sequences.
+ sin = "a\xac\u1234\u20ac\u8000\U0010ffff"
+ sout = (b'a\\N{NOT SIGN}\\N{ETHIOPIC SYLLABLE SEE}\\N{EURO SIGN}'
+ b'\\N{CJK UNIFIED IDEOGRAPH-8000}\\U0010ffff')
+ self.assertEqual(sin.encode("ascii", "namereplace"), sout)
+
+ sout = (b'a\xac\\N{ETHIOPIC SYLLABLE SEE}\\N{EURO SIGN}'
+ b'\\N{CJK UNIFIED IDEOGRAPH-8000}\\U0010ffff')
+ self.assertEqual(sin.encode("latin-1", "namereplace"), sout)
+
+ sout = (b'a\xac\\N{ETHIOPIC SYLLABLE SEE}\xa4'
+ b'\\N{CJK UNIFIED IDEOGRAPH-8000}\\U0010ffff')
+ self.assertEqual(sin.encode("iso-8859-15", "namereplace"), sout)
+
def test_decoding_callbacks(self):
# This is a test for a decoding callback handler
# that allows the decoding of the invalid sequence
@@ -230,6 +246,11 @@ class CodecCallbackTest(unittest.TestCase):
"\u0000\ufffd"
)
+ self.assertEqual(
+ b"\x00\x00\x00\x00\x00".decode("unicode-internal", "backslashreplace"),
+ "\u0000\\x00"
+ )
+
codecs.register_error("test.hui", handler_unicodeinternal)
self.assertEqual(
@@ -297,7 +318,7 @@ class CodecCallbackTest(unittest.TestCase):
def test_longstrings(self):
# test long strings to check for memory overflow problems
errors = [ "strict", "ignore", "replace", "xmlcharrefreplace",
- "backslashreplace"]
+ "backslashreplace", "namereplace"]
# register the handlers under different names,
# to prevent the codec from recognizing the name
for err in errors:
@@ -549,17 +570,6 @@ class CodecCallbackTest(unittest.TestCase):
codecs.backslashreplace_errors,
UnicodeError("ouch")
)
- # "backslashreplace" can only be used for encoding
- self.assertRaises(
- TypeError,
- codecs.backslashreplace_errors,
- UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")
- )
- self.assertRaises(
- TypeError,
- codecs.backslashreplace_errors,
- UnicodeTranslateError("\u3042", 0, 1, "ouch")
- )
# Use the correct exception
self.assertEqual(
codecs.backslashreplace_errors(
@@ -611,6 +621,91 @@ class CodecCallbackTest(unittest.TestCase):
("\\udfff", 1)
)
+ def test_badandgoodnamereplaceexceptions(self):
+ # "namereplace" complains about a non-exception passed in
+ self.assertRaises(
+ TypeError,
+ codecs.namereplace_errors,
+ 42
+ )
+ # "namereplace" complains about the wrong exception types
+ self.assertRaises(
+ TypeError,
+ codecs.namereplace_errors,
+ UnicodeError("ouch")
+ )
+ # "namereplace" can only be used for encoding
+ self.assertRaises(
+ TypeError,
+ codecs.namereplace_errors,
+ UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")
+ )
+ self.assertRaises(
+ TypeError,
+ codecs.namereplace_errors,
+ UnicodeTranslateError("\u3042", 0, 1, "ouch")
+ )
+ # Use the correct exception
+ self.assertEqual(
+ codecs.namereplace_errors(
+ UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
+ ("\\N{HIRAGANA LETTER A}", 1)
+ )
+ self.assertEqual(
+ codecs.namereplace_errors(
+ UnicodeEncodeError("ascii", "\x00", 0, 1, "ouch")),
+ ("\\x00", 1)
+ )
+ self.assertEqual(
+ codecs.namereplace_errors(
+ UnicodeEncodeError("ascii", "\xff", 0, 1, "ouch")),
+ ("\\N{LATIN SMALL LETTER Y WITH DIAERESIS}", 1)
+ )
+ self.assertEqual(
+ codecs.namereplace_errors(
+ UnicodeEncodeError("ascii", "\u0100", 0, 1, "ouch")),
+ ("\\N{LATIN CAPITAL LETTER A WITH MACRON}", 1)
+ )
+ self.assertEqual(
+ codecs.namereplace_errors(
+ UnicodeEncodeError("ascii", "\uffff", 0, 1, "ouch")),
+ ("\\uffff", 1)
+ )
+ if SIZEOF_WCHAR_T > 0:
+ self.assertEqual(
+ codecs.namereplace_errors(
+ UnicodeEncodeError("ascii", "\U00010000",
+ 0, 1, "ouch")),
+ ("\\N{LINEAR B SYLLABLE B008 A}", 1)
+ )
+ self.assertEqual(
+ codecs.namereplace_errors(
+ UnicodeEncodeError("ascii", "\U0010ffff",
+ 0, 1, "ouch")),
+ ("\\U0010ffff", 1)
+ )
+ # Lone surrogates (regardless of unicode width)
+ self.assertEqual(
+ codecs.namereplace_errors(
+ UnicodeEncodeError("ascii", "\ud800", 0, 1, "ouch")),
+ ("\\ud800", 1)
+ )
+ self.assertEqual(
+ codecs.namereplace_errors(
+ UnicodeEncodeError("ascii", "\udfff", 0, 1, "ouch")),
+ ("\\udfff", 1)
+ )
+ self.assertEqual(
+ codecs.backslashreplace_errors(
+ UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")),
+ ("\\xff", 1)
+ )
+ self.assertEqual(
+ codecs.backslashreplace_errors(
+ UnicodeTranslateError("\u3042", 0, 1, "ouch")),
+ ("\\u3042", 1)
+ )
+
def test_badhandlerresults(self):
results = ( 42, "foo", (1,2,3), ("foo", 1, 3), ("foo", None), ("foo",), ("foo", 1, 3), ("foo", None), ("foo",) )
encs = ("ascii", "latin-1", "iso-8859-1", "iso-8859-15")
@@ -651,6 +746,10 @@ class CodecCallbackTest(unittest.TestCase):
codecs.backslashreplace_errors,
codecs.lookup_error("backslashreplace")
)
+ self.assertEqual(
+ codecs.namereplace_errors,
+ codecs.lookup_error("namereplace")
+ )
def test_unencodablereplacement(self):
def unencrepl(exc):
@@ -804,7 +903,8 @@ class CodecCallbackTest(unittest.TestCase):
class D(dict):
def __getitem__(self, key):
raise ValueError
- for err in ("strict", "replace", "xmlcharrefreplace", "backslashreplace", "test.posreturn"):
+ for err in ("strict", "replace", "xmlcharrefreplace",
+ "backslashreplace", "namereplace", "test.posreturn"):
self.assertRaises(UnicodeError, codecs.charmap_encode, "\xff", err, {0xff: None})
self.assertRaises(ValueError, codecs.charmap_encode, "\xff", err, D())
self.assertRaises(TypeError, codecs.charmap_encode, "\xff", err, {0xff: 300})
@@ -819,7 +919,7 @@ class CodecCallbackTest(unittest.TestCase):
def __getitem__(self, key):
raise ValueError
#self.assertRaises(ValueError, "\xff".translate, D())
- self.assertRaises(TypeError, "\xff".translate, {0xff: sys.maxunicode+1})
+ self.assertRaises(ValueError, "\xff".translate, {0xff: sys.maxunicode+1})
self.assertRaises(TypeError, "\xff".translate, {0xff: ()})
def test_bug828737(self):
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index 6629ccd..4d5d7bb 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -349,6 +349,8 @@ class ReadTest(MixInCheckStateHandling):
self.assertRaises(UnicodeEncodeError, "\ud800".encode, self.encoding)
self.assertEqual("[\uDC80]".encode(self.encoding, "backslashreplace"),
"[\\udc80]".encode(self.encoding))
+ self.assertEqual("[\uDC80]".encode(self.encoding, "namereplace"),
+ "[\\udc80]".encode(self.encoding))
self.assertEqual("[\uDC80]".encode(self.encoding, "xmlcharrefreplace"),
"[&#56448;]".encode(self.encoding))
self.assertEqual("[\uDC80]".encode(self.encoding, "ignore"),
@@ -376,6 +378,10 @@ class ReadTest(MixInCheckStateHandling):
before + after)
self.assertEqual(test_sequence.decode(self.encoding, "replace"),
before + self.ill_formed_sequence_replace + after)
+ backslashreplace = ''.join('\\x%02x' % b
+ for b in self.ill_formed_sequence)
+ self.assertEqual(test_sequence.decode(self.encoding, "backslashreplace"),
+ before + backslashreplace + after)
class UTF32Test(ReadTest, unittest.TestCase):
encoding = "utf-32"
@@ -808,6 +814,7 @@ class CP65001Test(ReadTest, unittest.TestCase):
('\udc80', 'ignore', b''),
('\udc80', 'replace', b'?'),
('\udc80', 'backslashreplace', b'\\udc80'),
+ ('\udc80', 'namereplace', b'\\udc80'),
('\udc80', 'surrogatepass', b'\xed\xb2\x80'),
))
else:
@@ -869,6 +876,8 @@ class CP65001Test(ReadTest, unittest.TestCase):
self.assertRaises(UnicodeDecodeError, b"\xed\xa0\x80".decode, "cp65001")
self.assertEqual("[\uDC80]".encode("cp65001", "backslashreplace"),
b'[\\udc80]')
+ self.assertEqual("[\uDC80]".encode("cp65001", "namereplace"),
+ b'[\\udc80]')
self.assertEqual("[\uDC80]".encode("cp65001", "xmlcharrefreplace"),
b'[&#56448;]')
self.assertEqual("[\uDC80]".encode("cp65001", "surrogateescape"),
@@ -890,10 +899,6 @@ class CP65001Test(ReadTest, unittest.TestCase):
"\U00010fff\uD800")
self.assertTrue(codecs.lookup_error("surrogatepass"))
- def test_readline(self):
- self.skipTest("issue #20571: code page 65001 codec does not "
- "support partial decoder yet")
-
class UTF7Test(ReadTest, unittest.TestCase):
encoding = "utf-7"
@@ -1299,14 +1304,19 @@ class UnicodeInternalTest(unittest.TestCase):
"unicode_internal")
if sys.byteorder == "little":
invalid = b"\x00\x00\x11\x00"
+ invalid_backslashreplace = r"\x00\x00\x11\x00"
else:
invalid = b"\x00\x11\x00\x00"
+ invalid_backslashreplace = r"\x00\x11\x00\x00"
with support.check_warnings():
self.assertRaises(UnicodeDecodeError,
invalid.decode, "unicode_internal")
with support.check_warnings():
self.assertEqual(invalid.decode("unicode_internal", "replace"),
'\ufffd')
+ with support.check_warnings():
+ self.assertEqual(invalid.decode("unicode_internal", "backslashreplace"),
+ invalid_backslashreplace)
@unittest.skipUnless(SIZEOF_WCHAR_T == 4, 'specific to 32-bit wchar_t')
def test_decode_error_attributes(self):
@@ -1612,6 +1622,12 @@ class CodecsModuleTest(unittest.TestCase):
self.assertEqual(codecs.decode(b'abc'), 'abc')
self.assertRaises(UnicodeDecodeError, codecs.decode, b'\xff', 'ascii')
+ # test keywords
+ self.assertEqual(codecs.decode(obj=b'\xe4\xf6\xfc', encoding='latin-1'),
+ '\xe4\xf6\xfc')
+ self.assertEqual(codecs.decode(b'[\xff]', 'ascii', errors='ignore'),
+ '[]')
+
def test_encode(self):
self.assertEqual(codecs.encode('\xe4\xf6\xfc', 'latin-1'),
b'\xe4\xf6\xfc')
@@ -1620,6 +1636,12 @@ class CodecsModuleTest(unittest.TestCase):
self.assertEqual(codecs.encode('abc'), b'abc')
self.assertRaises(UnicodeEncodeError, codecs.encode, '\xffff', 'ascii')
+ # test keywords
+ self.assertEqual(codecs.encode(obj='\xe4\xf6\xfc', encoding='latin-1'),
+ b'\xe4\xf6\xfc')
+ self.assertEqual(codecs.encode('[\xff]', 'ascii', errors='ignore'),
+ b'[]')
+
def test_register(self):
self.assertRaises(TypeError, codecs.register)
self.assertRaises(TypeError, codecs.register, 42)
@@ -1668,6 +1690,7 @@ class CodecsModuleTest(unittest.TestCase):
"register_error", "lookup_error",
"strict_errors", "replace_errors", "ignore_errors",
"xmlcharrefreplace_errors", "backslashreplace_errors",
+ "namereplace_errors",
"open", "EncodedFile",
"iterencode", "iterdecode",
"BOM", "BOM_BE", "BOM_LE",
@@ -2029,6 +2052,16 @@ class CharmapTest(unittest.TestCase):
)
self.assertEqual(
+ codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace", "ab"),
+ ("ab\\x02", 3)
+ )
+
+ self.assertEqual(
+ codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace", "ab\ufffe"),
+ ("ab\\x02", 3)
+ )
+
+ self.assertEqual(
codecs.charmap_decode(b"\x00\x01\x02", "ignore", "ab"),
("ab", 3)
)
@@ -2105,6 +2138,25 @@ class CharmapTest(unittest.TestCase):
)
self.assertEqual(
+ codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace",
+ {0: 'a', 1: 'b'}),
+ ("ab\\x02", 3)
+ )
+
+ self.assertEqual(
+ codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace",
+ {0: 'a', 1: 'b', 2: None}),
+ ("ab\\x02", 3)
+ )
+
+ # Issue #14850
+ self.assertEqual(
+ codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace",
+ {0: 'a', 1: 'b', 2: '\ufffe'}),
+ ("ab\\x02", 3)
+ )
+
+ self.assertEqual(
codecs.charmap_decode(b"\x00\x01\x02", "ignore",
{0: 'a', 1: 'b'}),
("ab", 3)
@@ -2181,6 +2233,18 @@ class CharmapTest(unittest.TestCase):
)
self.assertEqual(
+ codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace",
+ {0: a, 1: b}),
+ ("ab\\x02", 3)
+ )
+
+ self.assertEqual(
+ codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace",
+ {0: a, 1: b, 2: 0xFFFE}),
+ ("ab\\x02", 3)
+ )
+
+ self.assertEqual(
codecs.charmap_decode(b"\x00\x01\x02", "ignore",
{0: a, 1: b}),
("ab", 3)
@@ -2239,9 +2303,13 @@ class TypesTest(unittest.TestCase):
self.assertRaises(UnicodeDecodeError, codecs.unicode_escape_decode, br"\U00110000")
self.assertEqual(codecs.unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10))
+ self.assertEqual(codecs.unicode_escape_decode(r"\U00110000", "backslashreplace"),
+ (r"\x5c\x55\x30\x30\x31\x31\x30\x30\x30\x30", 10))
self.assertRaises(UnicodeDecodeError, codecs.raw_unicode_escape_decode, br"\U00110000")
self.assertEqual(codecs.raw_unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10))
+ self.assertEqual(codecs.raw_unicode_escape_decode(r"\U00110000", "backslashreplace"),
+ (r"\x5c\x55\x30\x30\x31\x31\x30\x30\x30\x30", 10))
class UnicodeEscapeTest(unittest.TestCase):
@@ -2818,15 +2886,15 @@ class CodePageTest(unittest.TestCase):
self.assertRaisesRegex(UnicodeEncodeError, 'cp932',
codecs.code_page_encode, 932, '\xff')
self.assertRaisesRegex(UnicodeDecodeError, 'cp932',
- codecs.code_page_decode, 932, b'\x81\x00')
+ codecs.code_page_decode, 932, b'\x81\x00', 'strict', True)
self.assertRaisesRegex(UnicodeDecodeError, 'CP_UTF8',
- codecs.code_page_decode, self.CP_UTF8, b'\xff')
+ codecs.code_page_decode, self.CP_UTF8, b'\xff', 'strict', True)
def check_decode(self, cp, tests):
for raw, errors, expected in tests:
if expected is not None:
try:
- decoded = codecs.code_page_decode(cp, raw, errors)
+ decoded = codecs.code_page_decode(cp, raw, errors, True)
except UnicodeDecodeError as err:
self.fail('Unable to decode %a from "cp%s" with '
'errors=%r: %s' % (raw, cp, errors, err))
@@ -2838,7 +2906,7 @@ class CodePageTest(unittest.TestCase):
self.assertLessEqual(decoded[1], len(raw))
else:
self.assertRaises(UnicodeDecodeError,
- codecs.code_page_decode, cp, raw, errors)
+ codecs.code_page_decode, cp, raw, errors, True)
def check_encode(self, cp, tests):
for text, errors, expected in tests:
@@ -2866,7 +2934,12 @@ class CodePageTest(unittest.TestCase):
('[\xff]', 'replace', b'[y]'),
('[\u20ac]', 'replace', b'[?]'),
('[\xff]', 'backslashreplace', b'[\\xff]'),
+ ('[\xff]', 'namereplace',
+ b'[\\N{LATIN SMALL LETTER Y WITH DIAERESIS}]'),
('[\xff]', 'xmlcharrefreplace', b'[&#255;]'),
+ ('\udcff', 'strict', None),
+ ('[\udcff]', 'surrogateescape', b'[\xff]'),
+ ('[\udcff]', 'surrogatepass', None),
))
self.check_decode(932, (
(b'abc', 'strict', 'abc'),
@@ -2875,10 +2948,13 @@ class CodePageTest(unittest.TestCase):
(b'[\xff]', 'strict', None),
(b'[\xff]', 'ignore', '[]'),
(b'[\xff]', 'replace', '[\ufffd]'),
+ (b'[\xff]', 'backslashreplace', '[\\xff]'),
(b'[\xff]', 'surrogateescape', '[\udcff]'),
+ (b'[\xff]', 'surrogatepass', None),
(b'\x81\x00abc', 'strict', None),
(b'\x81\x00abc', 'ignore', '\x00abc'),
(b'\x81\x00abc', 'replace', '\ufffd\x00abc'),
+ (b'\x81\x00abc', 'backslashreplace', '\\x81\x00abc'),
))
def test_cp1252(self):
@@ -2886,9 +2962,12 @@ class CodePageTest(unittest.TestCase):
('abc', 'strict', b'abc'),
('\xe9\u20ac', 'strict', b'\xe9\x80'),
('\xff', 'strict', b'\xff'),
+ # test error handlers
('\u0141', 'strict', None),
('\u0141', 'ignore', b''),
('\u0141', 'replace', b'L'),
+ ('\udc98', 'surrogateescape', b'\x98'),
+ ('\udc98', 'surrogatepass', None),
))
self.check_decode(1252, (
(b'abc', 'strict', 'abc'),
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index df1c63c..a94b5da 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -518,7 +518,7 @@ class TestOneTrickPonyABCs(ABCTestCase):
class NextOnly:
def __next__(self):
yield 1
- raise StopIteration
+ return
self.assertNotIsInstance(NextOnly(), Iterator)
def test_Sized(self):
@@ -647,6 +647,59 @@ class TestCollectionABCs(ABCTestCase):
a, b = OneTwoThreeSet(), OneTwoThreeSet()
self.assertTrue(hash(a) == hash(b))
+ def test_isdisjoint_Set(self):
+ class MySet(Set):
+ def __init__(self, itr):
+ self.contents = itr
+ def __contains__(self, x):
+ return x in self.contents
+ def __iter__(self):
+ return iter(self.contents)
+ def __len__(self):
+ return len([x for x in self.contents])
+ s1 = MySet((1, 2, 3))
+ s2 = MySet((4, 5, 6))
+ s3 = MySet((1, 5, 6))
+ self.assertTrue(s1.isdisjoint(s2))
+ self.assertFalse(s1.isdisjoint(s3))
+
+ def test_equality_Set(self):
+ class MySet(Set):
+ def __init__(self, itr):
+ self.contents = itr
+ def __contains__(self, x):
+ return x in self.contents
+ def __iter__(self):
+ return iter(self.contents)
+ def __len__(self):
+ return len([x for x in self.contents])
+ s1 = MySet((1,))
+ s2 = MySet((1, 2))
+ s3 = MySet((3, 4))
+ s4 = MySet((3, 4))
+ self.assertTrue(s2 > s1)
+ self.assertTrue(s1 < s2)
+ self.assertFalse(s2 <= s1)
+ self.assertFalse(s2 <= s3)
+ self.assertFalse(s1 >= s2)
+ self.assertEqual(s3, s4)
+ self.assertNotEqual(s2, s3)
+
+ def test_arithmetic_Set(self):
+ class MySet(Set):
+ def __init__(self, itr):
+ self.contents = itr
+ def __contains__(self, x):
+ return x in self.contents
+ def __iter__(self):
+ return iter(self.contents)
+ def __len__(self):
+ return len([x for x in self.contents])
+ s1 = MySet((1, 2, 3))
+ s2 = MySet((3, 4, 5))
+ s3 = s1 & s2
+ self.assertEqual(s3, MySet((3,)))
+
def test_MutableSet(self):
self.assertIsInstance(set(), MutableSet)
self.assertTrue(issubclass(set, MutableSet))
@@ -1378,6 +1431,21 @@ class TestOrderedDict(unittest.TestCase):
self.assertEqual(list(od.items()), pairs)
self.assertEqual(list(reversed(od)),
[t[0] for t in reversed(pairs)])
+ self.assertEqual(list(reversed(od.keys())),
+ [t[0] for t in reversed(pairs)])
+ self.assertEqual(list(reversed(od.values())),
+ [t[1] for t in reversed(pairs)])
+ self.assertEqual(list(reversed(od.items())), list(reversed(pairs)))
+
+ def test_detect_deletion_during_iteration(self):
+ od = OrderedDict.fromkeys('abc')
+ it = iter(od)
+ key = next(it)
+ del od[key]
+ with self.assertRaises(Exception):
+ # Note, the exact exception raised is not guaranteed
+ # The only guarantee that the next() will not succeed
+ next(it)
def test_popitem(self):
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py
index 2a42238..7506c70 100644
--- a/Lib/test/test_compileall.py
+++ b/Lib/test/test_compileall.py
@@ -10,6 +10,13 @@ import time
import unittest
import io
+from unittest import mock, skipUnless
+try:
+ from concurrent.futures import ProcessPoolExecutor
+ _have_multiprocessing = True
+except ImportError:
+ _have_multiprocessing = False
+
from test import support, script_helper
class CompileallTests(unittest.TestCase):
@@ -106,6 +113,33 @@ class CompileallTests(unittest.TestCase):
debug_override=not optimize)
self.assertTrue(os.path.isfile(cached3))
+ @mock.patch('compileall.ProcessPoolExecutor')
+ def test_compile_pool_called(self, pool_mock):
+ compileall.compile_dir(self.directory, quiet=True, workers=5)
+ self.assertTrue(pool_mock.called)
+
+ def test_compile_workers_non_positive(self):
+ with self.assertRaisesRegex(ValueError,
+ "workers must be greater or equal to 0"):
+ compileall.compile_dir(self.directory, workers=-1)
+
+ @mock.patch('compileall.ProcessPoolExecutor')
+ def test_compile_workers_cpu_count(self, pool_mock):
+ compileall.compile_dir(self.directory, quiet=True, workers=0)
+ self.assertEqual(pool_mock.call_args[1]['max_workers'], None)
+
+ @mock.patch('compileall.ProcessPoolExecutor')
+ @mock.patch('compileall.compile_file')
+ def test_compile_one_worker(self, compile_file_mock, pool_mock):
+ compileall.compile_dir(self.directory, quiet=True)
+ self.assertFalse(pool_mock.called)
+ self.assertTrue(compile_file_mock.called)
+
+ @mock.patch('compileall.ProcessPoolExecutor', new=None)
+ def test_compile_missing_multiprocessing(self):
+ with self.assertRaisesRegex(NotImplementedError,
+ "multiprocessing support not available"):
+ compileall.compile_dir(self.directory, quiet=True, workers=5)
class EncodingTest(unittest.TestCase):
"""Issue 6716: compileall should escape source code when printing errors
@@ -273,12 +307,53 @@ class CommandLineTests(unittest.TestCase):
self.assertCompiled(subinitfn)
self.assertCompiled(hamfn)
+ def test_recursion_limit(self):
+ subpackage = os.path.join(self.pkgdir, 'spam')
+ subpackage2 = os.path.join(subpackage, 'ham')
+ subpackage3 = os.path.join(subpackage2, 'eggs')
+ for pkg in (subpackage, subpackage2, subpackage3):
+ script_helper.make_pkg(pkg)
+
+ subinitfn = os.path.join(subpackage, '__init__.py')
+ hamfn = script_helper.make_script(subpackage, 'ham', '')
+ spamfn = script_helper.make_script(subpackage2, 'spam', '')
+ eggfn = script_helper.make_script(subpackage3, 'egg', '')
+
+ self.assertRunOK('-q', '-r 0', self.pkgdir)
+ self.assertNotCompiled(subinitfn)
+ self.assertFalse(
+ os.path.exists(os.path.join(subpackage, '__pycache__')))
+
+ self.assertRunOK('-q', '-r 1', self.pkgdir)
+ self.assertCompiled(subinitfn)
+ self.assertCompiled(hamfn)
+ self.assertNotCompiled(spamfn)
+
+ self.assertRunOK('-q', '-r 2', self.pkgdir)
+ self.assertCompiled(subinitfn)
+ self.assertCompiled(hamfn)
+ self.assertCompiled(spamfn)
+ self.assertNotCompiled(eggfn)
+
+ self.assertRunOK('-q', '-r 5', self.pkgdir)
+ self.assertCompiled(subinitfn)
+ self.assertCompiled(hamfn)
+ self.assertCompiled(spamfn)
+ self.assertCompiled(eggfn)
+
def test_quiet(self):
noisy = self.assertRunOK(self.pkgdir)
quiet = self.assertRunOK('-q', self.pkgdir)
self.assertNotEqual(b'', noisy)
self.assertEqual(b'', quiet)
+ def test_silent(self):
+ script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
+ _, quiet, _ = self.assertRunNotOK('-q', self.pkgdir)
+ _, silent, _ = self.assertRunNotOK('-qq', self.pkgdir)
+ self.assertNotEqual(b'', quiet)
+ self.assertEqual(b'', silent)
+
def test_regexp(self):
self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir)
self.assertNotCompiled(self.barfn)
@@ -379,6 +454,29 @@ class CommandLineTests(unittest.TestCase):
out = self.assertRunOK('badfilename')
self.assertRegex(out, b"Can't list 'badfilename'")
+ @skipUnless(_have_multiprocessing, "requires multiprocessing")
+ def test_workers(self):
+ bar2fn = script_helper.make_script(self.directory, 'bar2', '')
+ files = []
+ for suffix in range(5):
+ pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
+ os.mkdir(pkgdir)
+ fn = script_helper.make_script(pkgdir, '__init__', '')
+ files.append(script_helper.make_script(pkgdir, 'bar2', ''))
+
+ self.assertRunOK(self.directory, '-j', '0')
+ self.assertCompiled(bar2fn)
+ for file in files:
+ self.assertCompiled(file)
+
+ @mock.patch('compileall.compile_dir')
+ def test_workers_available_cores(self, compile_dir):
+ with mock.patch("sys.argv",
+ new=[sys.executable, self.directory, "-j0"]):
+ compileall.main()
+ self.assertTrue(compile_dir.called)
+ self.assertEqual(compile_dir.call_args[-1]['workers'], None)
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py
index c74b2ca..86802c2 100644
--- a/Lib/test/test_concurrent_futures.py
+++ b/Lib/test/test_concurrent_futures.py
@@ -11,6 +11,7 @@ test.support.import_module('threading')
from test.script_helper import assert_python_ok
+import os
import sys
import threading
import time
@@ -425,6 +426,13 @@ class ExecutorTest:
self.assertTrue(collected,
"Stale reference not collected within timeout.")
+ def test_max_workers_negative(self):
+ for number in (0, -1):
+ with self.assertRaisesRegex(ValueError,
+ "max_workers must be greater "
+ "than 0"):
+ self.executor_type(max_workers=number)
+
class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest, unittest.TestCase):
def test_map_submits_without_iteration(self):
@@ -437,6 +445,11 @@ class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest, unittest.TestCase):
self.executor.shutdown(wait=True)
self.assertCountEqual(finished, range(10))
+ def test_default_workers(self):
+ executor = self.executor_type()
+ self.assertEqual(executor._max_workers,
+ (os.cpu_count() or 1) * 5)
+
class ProcessPoolExecutorTest(ProcessPoolMixin, ExecutorTest, unittest.TestCase):
def test_killed_child(self):
@@ -451,6 +464,48 @@ class ProcessPoolExecutorTest(ProcessPoolMixin, ExecutorTest, unittest.TestCase)
# Submitting other jobs fails as well.
self.assertRaises(BrokenProcessPool, self.executor.submit, pow, 2, 8)
+ def test_map_chunksize(self):
+ def bad_map():
+ list(self.executor.map(pow, range(40), range(40), chunksize=-1))
+
+ ref = list(map(pow, range(40), range(40)))
+ self.assertEqual(
+ list(self.executor.map(pow, range(40), range(40), chunksize=6)),
+ ref)
+ self.assertEqual(
+ list(self.executor.map(pow, range(40), range(40), chunksize=50)),
+ ref)
+ self.assertEqual(
+ list(self.executor.map(pow, range(40), range(40), chunksize=40)),
+ ref)
+ self.assertRaises(ValueError, bad_map)
+
+ @classmethod
+ def _test_traceback(cls):
+ raise RuntimeError(123) # some comment
+
+ def test_traceback(self):
+ # We want ensure that the traceback from the child process is
+ # contained in the traceback raised in the main process.
+ future = self.executor.submit(self._test_traceback)
+ with self.assertRaises(Exception) as cm:
+ future.result()
+
+ exc = cm.exception
+ self.assertIs(type(exc), RuntimeError)
+ self.assertEqual(exc.args, (123,))
+ cause = exc.__cause__
+ self.assertIs(type(cause), futures.process._RemoteTraceback)
+ self.assertIn('raise RuntimeError(123) # some comment', cause.tb)
+
+ with test.support.captured_stderr() as f1:
+ try:
+ raise exc
+ except RuntimeError:
+ sys.excepthook(*sys.exc_info())
+ self.assertIn('raise RuntimeError(123) # some comment',
+ f1.getvalue())
+
class FutureTests(unittest.TestCase):
def test_done_callback_with_result(self):
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
index 09c229a..470d2cd 100644
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -579,7 +579,7 @@ boolean {0[0]} NO
return e
else:
self.fail("expected exception type %s.%s"
- % (exc.__module__, exc.__name__))
+ % (exc.__module__, exc.__qualname__))
def test_boolean(self):
cf = self.fromstring(
@@ -1584,6 +1584,34 @@ class CoverageOneHundredTestCase(unittest.TestCase):
""")
self.assertEqual(repr(parser['section']), '<Section: section>')
+ def test_inconsistent_converters_state(self):
+ parser = configparser.ConfigParser()
+ import decimal
+ parser.converters['decimal'] = decimal.Decimal
+ parser.read_string("""
+ [s1]
+ one = 1
+ [s2]
+ two = 2
+ """)
+ self.assertIn('decimal', parser.converters)
+ self.assertEqual(parser.getdecimal('s1', 'one'), 1)
+ self.assertEqual(parser.getdecimal('s2', 'two'), 2)
+ self.assertEqual(parser['s1'].getdecimal('one'), 1)
+ self.assertEqual(parser['s2'].getdecimal('two'), 2)
+ del parser.getdecimal
+ with self.assertRaises(AttributeError):
+ parser.getdecimal('s1', 'one')
+ self.assertIn('decimal', parser.converters)
+ del parser.converters['decimal']
+ self.assertNotIn('decimal', parser.converters)
+ with self.assertRaises(AttributeError):
+ parser.getdecimal('s1', 'one')
+ with self.assertRaises(AttributeError):
+ parser['s1'].getdecimal('one')
+ with self.assertRaises(AttributeError):
+ parser['s2'].getdecimal('two')
+
class ExceptionPicklingTestCase(unittest.TestCase):
"""Tests for issue #13760: ConfigParser exceptions are not picklable."""
@@ -1776,5 +1804,252 @@ class InlineCommentStrippingTestCase(unittest.TestCase):
self.assertEqual(s['k3'], 'v3;#//still v3# and still v3')
+class ExceptionContextTestCase(unittest.TestCase):
+ """ Test that implementation details doesn't leak
+ through raising exceptions. """
+
+ def test_get_basic_interpolation(self):
+ parser = configparser.ConfigParser()
+ parser.read_string("""
+ [Paths]
+ home_dir: /Users
+ my_dir: %(home_dir1)s/lumberjack
+ my_pictures: %(my_dir)s/Pictures
+ """)
+ cm = self.assertRaises(configparser.InterpolationMissingOptionError)
+ with cm:
+ parser.get('Paths', 'my_dir')
+ self.assertIs(cm.exception.__suppress_context__, True)
+
+ def test_get_extended_interpolation(self):
+ parser = configparser.ConfigParser(
+ interpolation=configparser.ExtendedInterpolation())
+ parser.read_string("""
+ [Paths]
+ home_dir: /Users
+ my_dir: ${home_dir1}/lumberjack
+ my_pictures: ${my_dir}/Pictures
+ """)
+ cm = self.assertRaises(configparser.InterpolationMissingOptionError)
+ with cm:
+ parser.get('Paths', 'my_dir')
+ self.assertIs(cm.exception.__suppress_context__, True)
+
+ def test_missing_options(self):
+ parser = configparser.ConfigParser()
+ parser.read_string("""
+ [Paths]
+ home_dir: /Users
+ """)
+ with self.assertRaises(configparser.NoSectionError) as cm:
+ parser.options('test')
+ self.assertIs(cm.exception.__suppress_context__, True)
+
+ def test_missing_section(self):
+ config = configparser.ConfigParser()
+ with self.assertRaises(configparser.NoSectionError) as cm:
+ config.set('Section1', 'an_int', '15')
+ self.assertIs(cm.exception.__suppress_context__, True)
+
+ def test_remove_option(self):
+ config = configparser.ConfigParser()
+ with self.assertRaises(configparser.NoSectionError) as cm:
+ config.remove_option('Section1', 'an_int')
+ self.assertIs(cm.exception.__suppress_context__, True)
+
+
+class ConvertersTestCase(BasicTestCase, unittest.TestCase):
+ """Introduced in 3.5, issue #18159."""
+
+ config_class = configparser.ConfigParser
+
+ def newconfig(self, defaults=None):
+ instance = super().newconfig(defaults=defaults)
+ instance.converters['list'] = lambda v: [e.strip() for e in v.split()
+ if e.strip()]
+ return instance
+
+ def test_converters(self):
+ cfg = self.newconfig()
+ self.assertIn('boolean', cfg.converters)
+ self.assertIn('list', cfg.converters)
+ self.assertIsNone(cfg.converters['int'])
+ self.assertIsNone(cfg.converters['float'])
+ self.assertIsNone(cfg.converters['boolean'])
+ self.assertIsNotNone(cfg.converters['list'])
+ self.assertEqual(len(cfg.converters), 4)
+ with self.assertRaises(ValueError):
+ cfg.converters[''] = lambda v: v
+ with self.assertRaises(ValueError):
+ cfg.converters[None] = lambda v: v
+ cfg.read_string("""
+ [s]
+ str = string
+ int = 1
+ float = 0.5
+ list = a b c d e f g
+ bool = yes
+ """)
+ s = cfg['s']
+ self.assertEqual(s['str'], 'string')
+ self.assertEqual(s['int'], '1')
+ self.assertEqual(s['float'], '0.5')
+ self.assertEqual(s['list'], 'a b c d e f g')
+ self.assertEqual(s['bool'], 'yes')
+ self.assertEqual(cfg.get('s', 'str'), 'string')
+ self.assertEqual(cfg.get('s', 'int'), '1')
+ self.assertEqual(cfg.get('s', 'float'), '0.5')
+ self.assertEqual(cfg.get('s', 'list'), 'a b c d e f g')
+ self.assertEqual(cfg.get('s', 'bool'), 'yes')
+ self.assertEqual(cfg.get('s', 'str'), 'string')
+ self.assertEqual(cfg.getint('s', 'int'), 1)
+ self.assertEqual(cfg.getfloat('s', 'float'), 0.5)
+ self.assertEqual(cfg.getlist('s', 'list'), ['a', 'b', 'c', 'd',
+ 'e', 'f', 'g'])
+ self.assertEqual(cfg.getboolean('s', 'bool'), True)
+ self.assertEqual(s.get('str'), 'string')
+ self.assertEqual(s.getint('int'), 1)
+ self.assertEqual(s.getfloat('float'), 0.5)
+ self.assertEqual(s.getlist('list'), ['a', 'b', 'c', 'd',
+ 'e', 'f', 'g'])
+ self.assertEqual(s.getboolean('bool'), True)
+ with self.assertRaises(AttributeError):
+ cfg.getdecimal('s', 'float')
+ with self.assertRaises(AttributeError):
+ s.getdecimal('float')
+ import decimal
+ cfg.converters['decimal'] = decimal.Decimal
+ self.assertIn('decimal', cfg.converters)
+ self.assertIsNotNone(cfg.converters['decimal'])
+ self.assertEqual(len(cfg.converters), 5)
+ dec0_5 = decimal.Decimal('0.5')
+ self.assertEqual(cfg.getdecimal('s', 'float'), dec0_5)
+ self.assertEqual(s.getdecimal('float'), dec0_5)
+ del cfg.converters['decimal']
+ self.assertNotIn('decimal', cfg.converters)
+ self.assertEqual(len(cfg.converters), 4)
+ with self.assertRaises(AttributeError):
+ cfg.getdecimal('s', 'float')
+ with self.assertRaises(AttributeError):
+ s.getdecimal('float')
+ with self.assertRaises(KeyError):
+ del cfg.converters['decimal']
+ with self.assertRaises(KeyError):
+ del cfg.converters['']
+ with self.assertRaises(KeyError):
+ del cfg.converters[None]
+
+
+class BlatantOverrideConvertersTestCase(unittest.TestCase):
+ """What if somebody overrode a getboolean()? We want to make sure that in
+ this case the automatic converters do not kick in."""
+
+ config = """
+ [one]
+ one = false
+ two = false
+ three = long story short
+
+ [two]
+ one = false
+ two = false
+ three = four
+ """
+
+ def test_converters_at_init(self):
+ cfg = configparser.ConfigParser(converters={'len': len})
+ cfg.read_string(self.config)
+ self._test_len(cfg)
+ self.assertIsNotNone(cfg.converters['len'])
+
+ def test_inheritance(self):
+ class StrangeConfigParser(configparser.ConfigParser):
+ gettysburg = 'a historic borough in south central Pennsylvania'
+
+ def getboolean(self, section, option, *, raw=False, vars=None,
+ fallback=configparser._UNSET):
+ if section == option:
+ return True
+ return super().getboolean(section, option, raw=raw, vars=vars,
+ fallback=fallback)
+ def getlen(self, section, option, *, raw=False, vars=None,
+ fallback=configparser._UNSET):
+ return self._get_conv(section, option, len, raw=raw, vars=vars,
+ fallback=fallback)
+
+ cfg = StrangeConfigParser()
+ cfg.read_string(self.config)
+ self._test_len(cfg)
+ self.assertIsNone(cfg.converters['len'])
+ self.assertTrue(cfg.getboolean('one', 'one'))
+ self.assertTrue(cfg.getboolean('two', 'two'))
+ self.assertFalse(cfg.getboolean('one', 'two'))
+ self.assertFalse(cfg.getboolean('two', 'one'))
+ cfg.converters['boolean'] = cfg._convert_to_boolean
+ self.assertFalse(cfg.getboolean('one', 'one'))
+ self.assertFalse(cfg.getboolean('two', 'two'))
+ self.assertFalse(cfg.getboolean('one', 'two'))
+ self.assertFalse(cfg.getboolean('two', 'one'))
+
+ def _test_len(self, cfg):
+ self.assertEqual(len(cfg.converters), 4)
+ self.assertIn('boolean', cfg.converters)
+ self.assertIn('len', cfg.converters)
+ self.assertNotIn('tysburg', cfg.converters)
+ self.assertIsNone(cfg.converters['int'])
+ self.assertIsNone(cfg.converters['float'])
+ self.assertIsNone(cfg.converters['boolean'])
+ self.assertEqual(cfg.getlen('one', 'one'), 5)
+ self.assertEqual(cfg.getlen('one', 'two'), 5)
+ self.assertEqual(cfg.getlen('one', 'three'), 16)
+ self.assertEqual(cfg.getlen('two', 'one'), 5)
+ self.assertEqual(cfg.getlen('two', 'two'), 5)
+ self.assertEqual(cfg.getlen('two', 'three'), 4)
+ self.assertEqual(cfg.getlen('two', 'four', fallback=0), 0)
+ with self.assertRaises(configparser.NoOptionError):
+ cfg.getlen('two', 'four')
+ self.assertEqual(cfg['one'].getlen('one'), 5)
+ self.assertEqual(cfg['one'].getlen('two'), 5)
+ self.assertEqual(cfg['one'].getlen('three'), 16)
+ self.assertEqual(cfg['two'].getlen('one'), 5)
+ self.assertEqual(cfg['two'].getlen('two'), 5)
+ self.assertEqual(cfg['two'].getlen('three'), 4)
+ self.assertEqual(cfg['two'].getlen('four', 0), 0)
+ self.assertEqual(cfg['two'].getlen('four'), None)
+
+ def test_instance_assignment(self):
+ cfg = configparser.ConfigParser()
+ cfg.getboolean = lambda section, option: True
+ cfg.getlen = lambda section, option: len(cfg[section][option])
+ cfg.read_string(self.config)
+ self.assertEqual(len(cfg.converters), 3)
+ self.assertIn('boolean', cfg.converters)
+ self.assertNotIn('len', cfg.converters)
+ self.assertIsNone(cfg.converters['int'])
+ self.assertIsNone(cfg.converters['float'])
+ self.assertIsNone(cfg.converters['boolean'])
+ self.assertTrue(cfg.getboolean('one', 'one'))
+ self.assertTrue(cfg.getboolean('two', 'two'))
+ self.assertTrue(cfg.getboolean('one', 'two'))
+ self.assertTrue(cfg.getboolean('two', 'one'))
+ cfg.converters['boolean'] = cfg._convert_to_boolean
+ self.assertFalse(cfg.getboolean('one', 'one'))
+ self.assertFalse(cfg.getboolean('two', 'two'))
+ self.assertFalse(cfg.getboolean('one', 'two'))
+ self.assertFalse(cfg.getboolean('two', 'one'))
+ self.assertEqual(cfg.getlen('one', 'one'), 5)
+ self.assertEqual(cfg.getlen('one', 'two'), 5)
+ self.assertEqual(cfg.getlen('one', 'three'), 16)
+ self.assertEqual(cfg.getlen('two', 'one'), 5)
+ self.assertEqual(cfg.getlen('two', 'two'), 5)
+ self.assertEqual(cfg.getlen('two', 'three'), 4)
+ # If a getter impl is assigned straight to the instance, it won't
+ # be available on the section proxies.
+ with self.assertRaises(AttributeError):
+ self.assertEqual(cfg['one'].getlen('one'), 5)
+ with self.assertRaises(AttributeError):
+ self.assertEqual(cfg['two'].getlen('one'), 5)
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index 39cc776..c52066b 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -718,60 +718,76 @@ class TestExitStack(unittest.TestCase):
stack.push(cm)
self.assertIs(stack._exit_callbacks[-1], cm)
-class TestRedirectStdout(unittest.TestCase):
+
+class TestRedirectStream:
+
+ redirect_stream = None
+ orig_stream = None
@support.requires_docstrings
def test_instance_docs(self):
# Issue 19330: ensure context manager instances have good docstrings
- cm_docstring = redirect_stdout.__doc__
- obj = redirect_stdout(None)
+ cm_docstring = self.redirect_stream.__doc__
+ obj = self.redirect_stream(None)
self.assertEqual(obj.__doc__, cm_docstring)
def test_no_redirect_in_init(self):
- orig_stdout = sys.stdout
- redirect_stdout(None)
- self.assertIs(sys.stdout, orig_stdout)
+ orig_stdout = getattr(sys, self.orig_stream)
+ self.redirect_stream(None)
+ self.assertIs(getattr(sys, self.orig_stream), orig_stdout)
def test_redirect_to_string_io(self):
f = io.StringIO()
msg = "Consider an API like help(), which prints directly to stdout"
- orig_stdout = sys.stdout
- with redirect_stdout(f):
- print(msg)
- self.assertIs(sys.stdout, orig_stdout)
+ orig_stdout = getattr(sys, self.orig_stream)
+ with self.redirect_stream(f):
+ print(msg, file=getattr(sys, self.orig_stream))
+ self.assertIs(getattr(sys, self.orig_stream), orig_stdout)
s = f.getvalue().strip()
self.assertEqual(s, msg)
def test_enter_result_is_target(self):
f = io.StringIO()
- with redirect_stdout(f) as enter_result:
+ with self.redirect_stream(f) as enter_result:
self.assertIs(enter_result, f)
def test_cm_is_reusable(self):
f = io.StringIO()
- write_to_f = redirect_stdout(f)
- orig_stdout = sys.stdout
+ write_to_f = self.redirect_stream(f)
+ orig_stdout = getattr(sys, self.orig_stream)
with write_to_f:
- print("Hello", end=" ")
+ print("Hello", end=" ", file=getattr(sys, self.orig_stream))
with write_to_f:
- print("World!")
- self.assertIs(sys.stdout, orig_stdout)
+ print("World!", file=getattr(sys, self.orig_stream))
+ self.assertIs(getattr(sys, self.orig_stream), orig_stdout)
s = f.getvalue()
self.assertEqual(s, "Hello World!\n")
def test_cm_is_reentrant(self):
f = io.StringIO()
- write_to_f = redirect_stdout(f)
- orig_stdout = sys.stdout
+ write_to_f = self.redirect_stream(f)
+ orig_stdout = getattr(sys, self.orig_stream)
with write_to_f:
- print("Hello", end=" ")
+ print("Hello", end=" ", file=getattr(sys, self.orig_stream))
with write_to_f:
- print("World!")
- self.assertIs(sys.stdout, orig_stdout)
+ print("World!", file=getattr(sys, self.orig_stream))
+ self.assertIs(getattr(sys, self.orig_stream), orig_stdout)
s = f.getvalue()
self.assertEqual(s, "Hello World!\n")
+class TestRedirectStdout(TestRedirectStream, unittest.TestCase):
+
+ redirect_stream = redirect_stdout
+ orig_stream = "stdout"
+
+
+class TestRedirectStderr(TestRedirectStream, unittest.TestCase):
+
+ redirect_stream = redirect_stderr
+ orig_stream = "stderr"
+
+
class TestSuppress(unittest.TestCase):
@support.requires_docstrings
diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py
index ce5d27e..f18983f 100644
--- a/Lib/test/test_cprofile.py
+++ b/Lib/test/test_cprofile.py
@@ -11,7 +11,7 @@ from test.profilee import testfunc
class CProfileTest(ProfileTest):
profilerclass = cProfile.Profile
profilermodule = cProfile
- expected_max_output = "{built-in method max}"
+ expected_max_output = "{built-in method builtins.max}"
def get_expected_output(self):
return _ProfileOutput
@@ -72,9 +72,9 @@ profilee.py:84(helper2_indirect) <- 2 0.000 0.140
profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper)
2 0.078 0.100 profilee.py:84(helper2_indirect)
profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2)
-{built-in method exc_info} <- 4 0.000 0.000 profilee.py:73(helper1)
-{built-in method hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
+{built-in method builtins.hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
8 0.000 0.008 profilee.py:88(helper2)
+{built-in method sys.exc_info} <- 4 0.000 0.000 profilee.py:73(helper1)
{method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)"""
_ProfileOutput['print_callees'] = """\
<string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc)
@@ -87,12 +87,12 @@ profilee.py:48(mul) ->
profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1)
2 0.000 0.140 profilee.py:84(helper2_indirect)
6 0.234 0.300 profilee.py:88(helper2)
-profilee.py:73(helper1) -> 4 0.000 0.000 {built-in method exc_info}
+profilee.py:73(helper1) -> 4 0.000 0.004 {built-in method builtins.hasattr}
profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial)
2 0.078 0.100 profilee.py:88(helper2)
profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper)
profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__)
-{built-in method hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)"""
+{built-in method builtins.hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)"""
if __name__ == "__main__":
main()
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index 7e2485f..3f3f328 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -575,6 +575,16 @@ class TestDictFields(unittest.TestCase):
fileobj.readline() # header
self.assertEqual(fileobj.read(), "10,,abc\r\n")
+ def test_write_multiple_dict_rows(self):
+ fileobj = StringIO()
+ writer = csv.DictWriter(fileobj, fieldnames=["f1", "f2", "f3"])
+ writer.writeheader()
+ self.assertEqual(fileobj.getvalue(), "f1,f2,f3\r\n")
+ writer.writerows([{"f1": 1, "f2": "abc", "f3": "f"},
+ {"f1": 2, "f2": 5, "f3": "xyz"}])
+ self.assertEqual(fileobj.getvalue(),
+ "f1,f2,f3\r\n1,abc,f\r\n2,5,xyz\r\n")
+
def test_write_no_fields(self):
fileobj = StringIO()
self.assertRaises(TypeError, csv.DictWriter, fileobj)
@@ -773,7 +783,7 @@ class TestDialectValidity(unittest.TestCase):
with self.assertRaises(csv.Error) as cm:
mydialect()
self.assertEqual(str(cm.exception),
- '"quotechar" must be an 1-character string')
+ '"quotechar" must be a 1-character string')
mydialect.quotechar = 4
with self.assertRaises(csv.Error) as cm:
@@ -796,13 +806,13 @@ class TestDialectValidity(unittest.TestCase):
with self.assertRaises(csv.Error) as cm:
mydialect()
self.assertEqual(str(cm.exception),
- '"delimiter" must be an 1-character string')
+ '"delimiter" must be a 1-character string')
mydialect.delimiter = ""
with self.assertRaises(csv.Error) as cm:
mydialect()
self.assertEqual(str(cm.exception),
- '"delimiter" must be an 1-character string')
+ '"delimiter" must be a 1-character string')
mydialect.delimiter = b","
with self.assertRaises(csv.Error) as cm:
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index d9ddb32..2d4eb52 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -1,20 +1,20 @@
import unittest
import sys
+
from test.support import import_fresh_module, run_unittest
TESTS = 'test.datetimetester'
-# XXX: import_fresh_module() is supposed to leave sys.module cache untouched,
-# XXX: but it does not, so we have to save and restore it ourselves.
-save_sys_modules = sys.modules.copy()
try:
pure_tests = import_fresh_module(TESTS, fresh=['datetime', '_strptime'],
blocked=['_datetime'])
fast_tests = import_fresh_module(TESTS, fresh=['datetime',
'_datetime', '_strptime'])
finally:
- sys.modules.clear()
- sys.modules.update(save_sys_modules)
+ # XXX: import_fresh_module() is supposed to leave sys.module cache untouched,
+ # XXX: but it does not, so we have to cleanup ourselves.
+ for modname in ['datetime', '_datetime', '_strptime']:
+ sys.modules.pop(modname, None)
test_modules = [pure_tests, fast_tests]
test_suffixes = ["_Pure", "_Fast"]
# XXX(gb) First run all the _Pure tests, then all the _Fast tests. You might
diff --git a/Lib/test/test_dbm_dumb.py b/Lib/test/test_dbm_dumb.py
index dc88ca6..ff63c88 100644
--- a/Lib/test/test_dbm_dumb.py
+++ b/Lib/test/test_dbm_dumb.py
@@ -217,6 +217,14 @@ class DumbDBMTestCase(unittest.TestCase):
self.assertEqual(str(cm.exception),
"DBM object has already been closed")
+ def test_create_new(self):
+ with dumbdbm.open(_fname, 'n') as f:
+ for k in self._dict:
+ f[k] = self._dict[k]
+
+ with dumbdbm.open(_fname, 'n') as f:
+ self.assertEqual(f.keys(), [])
+
def test_eval(self):
with open(_fname + '.dir', 'w') as stream:
stream.write("str(print('Hacked!')), 0\n")
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index a178f6f..137aaa5 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -33,12 +33,13 @@ import unittest
import numbers
import locale
from test.support import (run_unittest, run_doctest, is_resource_enabled,
- requires_IEEE_754)
+ requires_IEEE_754, requires_docstrings)
from test.support import (check_warnings, import_fresh_module, TestFailed,
run_with_locale, cpython_only)
import random
import time
import warnings
+import inspect
try:
import threading
except ImportError:
@@ -4174,9 +4175,7 @@ class CheckAttributes(unittest.TestCase):
self.assertEqual(C.__version__, P.__version__)
self.assertEqual(C.__libmpdec_version__, P.__libmpdec_version__)
- x = dir(C)
- y = [s for s in dir(P) if '__' in s or not s.startswith('_')]
- self.assertEqual(set(x) - set(y), set())
+ self.assertEqual(dir(C), dir(P))
def test_context_attributes(self):
@@ -4455,18 +4454,6 @@ class PyCoverage(Coverage):
class PyFunctionality(unittest.TestCase):
"""Extra functionality in decimal.py"""
- def test_py_quantize_watchexp(self):
- # watchexp functionality
- Decimal = P.Decimal
- localcontext = P.localcontext
-
- with localcontext() as c:
- c.prec = 1
- c.Emax = 1
- c.Emin = -1
- x = Decimal(99999).quantize(Decimal("1e3"), watchexp=False)
- self.assertEqual(x, Decimal('1.00E+5'))
-
def test_py_alternate_formatting(self):
# triples giving a format, a Decimal, and the expected result
Decimal = P.Decimal
@@ -5409,6 +5396,143 @@ class CWhitebox(unittest.TestCase):
y = Decimal(10**(9*25)).__sizeof__()
self.assertEqual(y, x+4)
+@requires_docstrings
+@unittest.skipUnless(C, "test requires C version")
+class SignatureTest(unittest.TestCase):
+ """Function signatures"""
+
+ def test_inspect_module(self):
+ for attr in dir(P):
+ if attr.startswith('_'):
+ continue
+ p_func = getattr(P, attr)
+ c_func = getattr(C, attr)
+ if (attr == 'Decimal' or attr == 'Context' or
+ inspect.isfunction(p_func)):
+ p_sig = inspect.signature(p_func)
+ c_sig = inspect.signature(c_func)
+
+ # parameter names:
+ c_names = list(c_sig.parameters.keys())
+ p_names = [x for x in p_sig.parameters.keys() if not
+ x.startswith('_')]
+
+ self.assertEqual(c_names, p_names,
+ msg="parameter name mismatch in %s" % p_func)
+
+ c_kind = [x.kind for x in c_sig.parameters.values()]
+ p_kind = [x[1].kind for x in p_sig.parameters.items() if not
+ x[0].startswith('_')]
+
+ # parameters:
+ if attr != 'setcontext':
+ self.assertEqual(c_kind, p_kind,
+ msg="parameter kind mismatch in %s" % p_func)
+
+ def test_inspect_types(self):
+
+ POS = inspect._ParameterKind.POSITIONAL_ONLY
+ POS_KWD = inspect._ParameterKind.POSITIONAL_OR_KEYWORD
+
+ # Type heuristic (type annotations would help!):
+ pdict = {C: {'other': C.Decimal(1),
+ 'third': C.Decimal(1),
+ 'x': C.Decimal(1),
+ 'y': C.Decimal(1),
+ 'z': C.Decimal(1),
+ 'a': C.Decimal(1),
+ 'b': C.Decimal(1),
+ 'c': C.Decimal(1),
+ 'exp': C.Decimal(1),
+ 'modulo': C.Decimal(1),
+ 'num': "1",
+ 'f': 1.0,
+ 'rounding': C.ROUND_HALF_UP,
+ 'context': C.getcontext()},
+ P: {'other': P.Decimal(1),
+ 'third': P.Decimal(1),
+ 'a': P.Decimal(1),
+ 'b': P.Decimal(1),
+ 'c': P.Decimal(1),
+ 'exp': P.Decimal(1),
+ 'modulo': P.Decimal(1),
+ 'num': "1",
+ 'f': 1.0,
+ 'rounding': P.ROUND_HALF_UP,
+ 'context': P.getcontext()}}
+
+ def mkargs(module, sig):
+ args = []
+ kwargs = {}
+ for name, param in sig.parameters.items():
+ if name == 'self': continue
+ if param.kind == POS:
+ args.append(pdict[module][name])
+ elif param.kind == POS_KWD:
+ kwargs[name] = pdict[module][name]
+ else:
+ raise TestFailed("unexpected parameter kind")
+ return args, kwargs
+
+ def tr(s):
+ """The C Context docstrings use 'x' in order to prevent confusion
+ with the article 'a' in the descriptions."""
+ if s == 'x': return 'a'
+ if s == 'y': return 'b'
+ if s == 'z': return 'c'
+ return s
+
+ def doit(ty):
+ p_type = getattr(P, ty)
+ c_type = getattr(C, ty)
+ for attr in dir(p_type):
+ if attr.startswith('_'):
+ continue
+ p_func = getattr(p_type, attr)
+ c_func = getattr(c_type, attr)
+ if inspect.isfunction(p_func):
+ p_sig = inspect.signature(p_func)
+ c_sig = inspect.signature(c_func)
+
+ # parameter names:
+ p_names = list(p_sig.parameters.keys())
+ c_names = [tr(x) for x in c_sig.parameters.keys()]
+
+ self.assertEqual(c_names, p_names,
+ msg="parameter name mismatch in %s" % p_func)
+
+ p_kind = [x.kind for x in p_sig.parameters.values()]
+ c_kind = [x.kind for x in c_sig.parameters.values()]
+
+ # 'self' parameter:
+ self.assertIs(p_kind[0], POS_KWD)
+ self.assertIs(c_kind[0], POS)
+
+ # remaining parameters:
+ if ty == 'Decimal':
+ self.assertEqual(c_kind[1:], p_kind[1:],
+ msg="parameter kind mismatch in %s" % p_func)
+ else: # Context methods are positional only in the C version.
+ self.assertEqual(len(c_kind), len(p_kind),
+ msg="parameter kind mismatch in %s" % p_func)
+
+ # Run the function:
+ args, kwds = mkargs(C, c_sig)
+ try:
+ getattr(c_type(9), attr)(*args, **kwds)
+ except Exception as err:
+ raise TestFailed("invalid signature for %s: %s %s" % (c_func, args, kwds))
+
+ args, kwds = mkargs(P, p_sig)
+ try:
+ getattr(p_type(9), attr)(*args, **kwds)
+ except Exception as err:
+ raise TestFailed("invalid signature for %s: %s %s" % (p_func, args, kwds))
+
+ doit('Decimal')
+ doit('Context')
+
+
all_tests = [
CExplicitConstructionTest, PyExplicitConstructionTest,
CImplicitConstructionTest, PyImplicitConstructionTest,
@@ -5434,6 +5558,7 @@ if not C:
all_tests = all_tests[1::2]
else:
all_tests.insert(0, CheckAttributes)
+ all_tests.insert(1, SignatureTest)
def test_main(arith=None, verbose=None, todo_tests=None, debug=None):
diff --git a/Lib/test/test_defaultdict.py b/Lib/test/test_defaultdict.py
index 532d535..48d1cb5 100644
--- a/Lib/test/test_defaultdict.py
+++ b/Lib/test/test_defaultdict.py
@@ -157,8 +157,9 @@ class TestDefaultDict(unittest.TestCase):
def _factory(self):
return []
d = sub()
- self.assertTrue(repr(d).startswith(
- "defaultdict(<bound method sub._factory of defaultdict(..."))
+ self.assertRegex(repr(d),
+ r"defaultdict\(<bound method .*sub\._factory "
+ r"of defaultdict\(\.\.\., \{\}\)>, \{\}\)")
# NOTE: printing a subclass of a builtin type does not call its
# tp_print slot. So this part is essentially the same test as above.
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
index 5ecbc73..e336e5a 100644
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -508,6 +508,11 @@ class TestBasic(unittest.TestCase):
for s in ('abcd', range(2000)):
self.assertEqual(list(reversed(deque(s))), list(reversed(s)))
+ def test_reversed_new(self):
+ klass = type(reversed(deque()))
+ for s in ('abcd', range(2000)):
+ self.assertEqual(list(klass(deque(s))), list(reversed(s)))
+
def test_gc_doesnt_blowup(self):
import gc
# This used to assert-fail in deque_traverse() under a debug
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 1de2a99..8d6205a 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -1026,6 +1026,22 @@ order (MRO) for bases """
self.assertEqual(x.foo, 1)
self.assertEqual(x.__dict__, {'foo': 1})
+ def test_object_class_assignment_between_heaptypes_and_nonheaptypes(self):
+ class SubType(types.ModuleType):
+ a = 1
+
+ m = types.ModuleType("m")
+ self.assertTrue(m.__class__ is types.ModuleType)
+ self.assertFalse(hasattr(m, "a"))
+
+ m.__class__ = SubType
+ self.assertTrue(m.__class__ is SubType)
+ self.assertTrue(hasattr(m, "a"))
+
+ m.__class__ = types.ModuleType
+ self.assertTrue(m.__class__ is types.ModuleType)
+ self.assertFalse(hasattr(m, "a"))
+
def test_slots(self):
# Testing __slots__...
class C0(object):
@@ -4160,6 +4176,7 @@ order (MRO) for bases """
('__add__', 'x + y', 'x += y'),
('__sub__', 'x - y', 'x -= y'),
('__mul__', 'x * y', 'x *= y'),
+ ('__matmul__', 'x @ y', 'x @= y'),
('__truediv__', 'operator.truediv(x, y)', None),
('__floordiv__', 'operator.floordiv(x, y)', None),
('__div__', 'x / y', 'x /= y'),
@@ -4422,6 +4439,61 @@ order (MRO) for bases """
self.assertIn("__dict__", Base.__dict__)
self.assertNotIn("__dict__", Sub.__dict__)
+ def test_bound_method_repr(self):
+ class Foo:
+ def method(self):
+ pass
+ self.assertRegex(repr(Foo().method),
+ r"<bound method .*Foo\.method of <.*Foo object at .*>>")
+
+
+ class Base:
+ def method(self):
+ pass
+ class Derived1(Base):
+ pass
+ class Derived2(Base):
+ def method(self):
+ pass
+ base = Base()
+ derived1 = Derived1()
+ derived2 = Derived2()
+ super_d2 = super(Derived2, derived2)
+ self.assertRegex(repr(base.method),
+ r"<bound method .*Base\.method of <.*Base object at .*>>")
+ self.assertRegex(repr(derived1.method),
+ r"<bound method .*Base\.method of <.*Derived1 object at .*>>")
+ self.assertRegex(repr(derived2.method),
+ r"<bound method .*Derived2\.method of <.*Derived2 object at .*>>")
+ self.assertRegex(repr(super_d2.method),
+ r"<bound method .*Base\.method of <.*Derived2 object at .*>>")
+
+ class Foo:
+ @classmethod
+ def method(cls):
+ pass
+ foo = Foo()
+ self.assertRegex(repr(foo.method), # access via instance
+ r"<bound method .*Foo\.method of <class '.*Foo'>>")
+ self.assertRegex(repr(Foo.method), # access via the class
+ r"<bound method .*Foo\.method of <class '.*Foo'>>")
+
+
+ class MyCallable:
+ def __call__(self, arg):
+ pass
+ func = MyCallable() # func has no __name__ or __qualname__ attributes
+ instance = object()
+ method = types.MethodType(func, instance)
+ self.assertRegex(repr(method),
+ r"<bound method \? of <object object at .*>>")
+ func.__name__ = "name"
+ self.assertRegex(repr(method),
+ r"<bound method name of <object object at .*>>")
+ func.__qualname__ = "qualname"
+ self.assertRegex(repr(method),
+ r"<bound method qualname of <object object at .*>>")
+
class DictProxyTests(unittest.TestCase):
def setUp(self):
@@ -4536,26 +4608,15 @@ class PicklingTests(unittest.TestCase):
def _check_reduce(self, proto, obj, args=(), kwargs={}, state=None,
listitems=None, dictitems=None):
- if proto >= 4:
+ if proto >= 2:
reduce_value = obj.__reduce_ex__(proto)
- self.assertEqual(reduce_value[:3],
- (copyreg.__newobj_ex__,
- (type(obj), args, kwargs),
- state))
- if listitems is not None:
- self.assertListEqual(list(reduce_value[3]), listitems)
+ if kwargs:
+ self.assertEqual(reduce_value[0], copyreg.__newobj_ex__)
+ self.assertEqual(reduce_value[1], (type(obj), args, kwargs))
else:
- self.assertIsNone(reduce_value[3])
- if dictitems is not None:
- self.assertDictEqual(dict(reduce_value[4]), dictitems)
- else:
- self.assertIsNone(reduce_value[4])
- elif proto >= 2:
- reduce_value = obj.__reduce_ex__(proto)
- self.assertEqual(reduce_value[:3],
- (copyreg.__newobj__,
- (type(obj),) + args,
- state))
+ self.assertEqual(reduce_value[0], copyreg.__newobj__)
+ self.assertEqual(reduce_value[1], (type(obj),) + args)
+ self.assertEqual(reduce_value[2], state)
if listitems is not None:
self.assertListEqual(list(reduce_value[3]), listitems)
else:
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index b8daff7..4f30183 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -230,6 +230,9 @@ dis_traceback = """\
TRACEBACK_CODE.co_firstlineno + 4,
TRACEBACK_CODE.co_firstlineno + 5)
+def _g(x):
+ yield x
+
class DisTests(unittest.TestCase):
def get_disassembly(self, func, lasti=-1, wrapper=True):
@@ -315,6 +318,11 @@ class DisTests(unittest.TestCase):
method_bytecode = _C(1).__init__.__code__.co_code
self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
+ def test_disassemble_generator(self):
+ gen_func_disas = self.get_disassembly(_g) # Disassemble generator function
+ gen_disas = self.get_disassembly(_g(1)) # Disassemble generator itself
+ self.assertEqual(gen_disas, gen_func_disas)
+
def test_dis_none(self):
try:
del sys.last_traceback
@@ -561,10 +569,10 @@ expected_jumpy_line = 1
#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
#print('expected_opinfo_outer = [\n ',
#',\n '.join(map(str, _instructions)), ',\n]', sep='')
-#_instructions = dis.get_instructions(outer(), first_line=expected_outer_line)
+#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
#print('expected_opinfo_f = [\n ',
#',\n '.join(map(str, _instructions)), ',\n]', sep='')
-#_instructions = dis.get_instructions(outer()(), first_line=expected_outer_line)
+#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
#print('expected_opinfo_inner = [\n ',
#',\n '.join(map(str, _instructions)), ',\n]', sep='')
#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
@@ -635,12 +643,12 @@ expected_opinfo_inner = [
]
expected_opinfo_jumpy = [
- Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=77, argrepr='to 77', offset=0, starts_line=3, is_jump_target=False),
+ 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=50, argval=66, argrepr='to 66', offset=13, starts_line=None, is_jump_target=True),
+ 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),
@@ -649,92 +657,88 @@ expected_opinfo_jumpy = [
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=47, argval=47, argrepr='', offset=38, 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='JUMP_FORWARD', opcode=110, arg=0, argval=47, argrepr='to 47', offset=44, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=47, starts_line=7, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=50, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=53, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=56, starts_line=None, is_jump_target=False),
- Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=59, starts_line=8, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=60, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=63, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=True),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=67, 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=70, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=73, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=76, starts_line=None, is_jump_target=False),
- Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=154, argrepr='to 154', offset=77, starts_line=11, is_jump_target=True),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=None, is_jump_target=True),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=143, argval=143, argrepr='', offset=83, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=86, starts_line=12, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=89, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=92, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=95, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=96, starts_line=13, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=99, starts_line=None, is_jump_target=False),
- Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=102, starts_line=None, is_jump_target=False),
- Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=103, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=106, starts_line=14, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=109, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=112, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=124, argval=124, argrepr='', offset=115, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=118, starts_line=15, is_jump_target=False),
- Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=124, argrepr='to 124', offset=121, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=124, starts_line=16, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=127, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=130, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=133, starts_line=None, is_jump_target=False),
- Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=136, starts_line=17, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=137, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=140, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=143, starts_line=None, is_jump_target=True),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=144, 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=147, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=150, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=153, starts_line=None, is_jump_target=False),
- Instruction(opname='SETUP_FINALLY', opcode=122, arg=72, argval=229, argrepr='to 229', offset=154, starts_line=20, is_jump_target=True),
- Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=172, argrepr='to 172', offset=157, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=160, starts_line=21, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=163, starts_line=None, is_jump_target=False),
- Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=167, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=200, argrepr='to 200', offset=169, starts_line=None, is_jump_target=False),
- Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=172, starts_line=22, is_jump_target=True),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=173, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=176, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=199, argval=199, argrepr='', offset=179, starts_line=None, 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=72, argval=217, argrepr='to 217', 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_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=185, 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=188, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=191, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=194, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=195, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=225, argrepr='to 225', offset=196, starts_line=None, is_jump_target=False),
- Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=199, starts_line=None, is_jump_target=True),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=200, starts_line=25, is_jump_target=True),
- Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=223, argrepr='to 223', offset=203, starts_line=None, is_jump_target=False),
- Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=206, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=209, starts_line=26, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=212, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=215, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=218, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=219, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=220, starts_line=None, is_jump_target=False),
- Instruction(opname='WITH_CLEANUP', opcode=81, arg=None, argval=None, argrepr='', offset=223, starts_line=None, is_jump_target=True),
- Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=224, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=225, starts_line=None, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=226, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=229, 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=232, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=235, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=238, starts_line=None, is_jump_target=False),
- Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=239, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=240, starts_line=None, is_jump_target=False),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=243, 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=26, argval=213, argrepr='to 213', 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', opcode=81, arg=None, argval=None, argrepr='', offset=211, starts_line=None, is_jump_target=True),
+ Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=212, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=213, starts_line=None, is_jump_target=True),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=214, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=217, 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=220, starts_line=None, is_jump_target=False),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=223, starts_line=None, is_jump_target=False),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=226, starts_line=None, is_jump_target=False),
+ Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=227, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=228, starts_line=None, is_jump_target=False),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=231, starts_line=None, is_jump_target=False),
]
# One last piece of inspect fodder to check the default line number handling
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index 4137d5a..95700a3 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -4,6 +4,7 @@ Test script for doctest.
from test import support
import doctest
+import functools
import os
import sys
@@ -434,7 +435,7 @@ We'll simulate a __file__ attr that ends in pyc:
>>> tests = finder.find(sample_func)
>>> print(tests) # doctest: +ELLIPSIS
- [<DocTest sample_func from ...:18 (1 example)>]
+ [<DocTest sample_func from ...:19 (1 example)>]
The exact name depends on how test_doctest was invoked, so allow for
leading path components.
@@ -2096,22 +2097,9 @@ def test_DocTestSuite():
>>> suite.run(unittest.TestResult())
<unittest.result.TestResult run=0 errors=0 failures=0>
- However, if DocTestSuite finds no docstrings, it raises an error:
+ The module need not contain any docstrings either:
- >>> try:
- ... doctest.DocTestSuite('test.sample_doctest_no_docstrings')
- ... except ValueError as e:
- ... error = e
-
- >>> print(error.args[1])
- has no docstrings
-
- You can prevent this error by passing a DocTestFinder instance with
- the `exclude_empty` keyword argument set to False:
-
- >>> finder = doctest.DocTestFinder(exclude_empty=False)
- >>> suite = doctest.DocTestSuite('test.sample_doctest_no_docstrings',
- ... test_finder=finder)
+ >>> suite = doctest.DocTestSuite('test.sample_doctest_no_docstrings')
>>> suite.run(unittest.TestResult())
<unittest.result.TestResult run=0 errors=0 failures=0>
@@ -2121,6 +2109,22 @@ def test_DocTestSuite():
>>> suite.run(unittest.TestResult())
<unittest.result.TestResult run=9 errors=0 failures=4>
+ We can also provide a DocTestFinder:
+
+ >>> finder = doctest.DocTestFinder()
+ >>> suite = doctest.DocTestSuite('test.sample_doctest',
+ ... test_finder=finder)
+ >>> suite.run(unittest.TestResult())
+ <unittest.result.TestResult run=9 errors=0 failures=4>
+
+ The DocTestFinder need not return any tests:
+
+ >>> finder = doctest.DocTestFinder()
+ >>> suite = doctest.DocTestSuite('test.sample_doctest_no_docstrings',
+ ... test_finder=finder)
+ >>> suite.run(unittest.TestResult())
+ <unittest.result.TestResult run=0 errors=0 failures=0>
+
We can supply global variables. If we pass globs, they will be
used instead of the module globals. Here we'll pass an empty
globals, triggering an extra error:
@@ -2168,7 +2172,7 @@ def test_DocTestSuite():
>>> test.test_doctest.sillySetup
Traceback (most recent call last):
...
- AttributeError: 'module' object has no attribute 'sillySetup'
+ AttributeError: module 'test.test_doctest' has no attribute 'sillySetup'
The setUp and tearDown funtions are passed test objects. Here
we'll use the setUp function to supply the missing variable y:
@@ -2314,7 +2318,7 @@ def test_DocFileSuite():
>>> test.test_doctest.sillySetup
Traceback (most recent call last):
...
- AttributeError: 'module' object has no attribute 'sillySetup'
+ AttributeError: module 'test.test_doctest' has no attribute 'sillySetup'
The setUp and tearDown funtions are passed test objects.
Here, we'll use a setUp function to set the favorite color in
@@ -2361,6 +2365,22 @@ def test_trailing_space_in_test():
foo \n
"""
+class Wrapper:
+ def __init__(self, func):
+ self.func = func
+ functools.update_wrapper(self, func)
+
+ def __call__(self, *args, **kwargs):
+ self.func(*args, **kwargs)
+
+@Wrapper
+def test_look_in_unwrapped():
+ """
+ Docstrings in wrapped functions must be detected as well.
+
+ >>> 'one other test'
+ 'one other test'
+ """
def test_unittest_reportflags():
"""Default unittest reporting flags can be set to control reporting
@@ -2927,7 +2947,7 @@ Invalid doctest option:
def test_main():
# Check the doctest cases in doctest itself:
- support.run_doctest(doctest, verbosity=True)
+ 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)
diff --git a/Lib/test/test_docxmlrpc.py b/Lib/test/test_docxmlrpc.py
index 06161f2..d9fd917 100644
--- a/Lib/test/test_docxmlrpc.py
+++ b/Lib/test/test_docxmlrpc.py
@@ -87,10 +87,11 @@ class DocXMLRPCHTTPGETServer(unittest.TestCase):
threading.Thread(target=server, args=(self.evt, 1)).start()
# wait for port to be assigned
- n = 1000
- while n > 0 and PORT is None:
- time.sleep(0.001)
- n -= 1
+ deadline = time.monotonic() + 10.0
+ while PORT is None:
+ time.sleep(0.010)
+ if time.monotonic() > deadline:
+ break
self.client = http.client.HTTPConnection("localhost:%d" % PORT)
diff --git a/Lib/test/test_eintr.py b/Lib/test/test_eintr.py
new file mode 100644
index 0000000..8b5f507
--- /dev/null
+++ b/Lib/test/test_eintr.py
@@ -0,0 +1,20 @@
+import os
+import signal
+import unittest
+
+from test import script_helper, support
+
+
+@unittest.skipUnless(os.name == "posix", "only supported on Unix")
+class EINTRTests(unittest.TestCase):
+
+ @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
+ def test_all(self):
+ # 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")
+ script_helper.assert_python_ok(tester)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index 227110f..218ce0c 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -1636,6 +1636,10 @@ class TestMIMEText(unittest.TestCase):
msg = MIMEText('hello there', _charset='us-ascii')
eq(msg.get_charset().input_charset, 'us-ascii')
eq(msg['content-type'], 'text/plain; charset="us-ascii"')
+ # Also accept a Charset instance
+ msg = MIMEText('hello there', _charset=Charset('utf-8'))
+ eq(msg.get_charset().input_charset, 'utf-8')
+ eq(msg['content-type'], 'text/plain; charset="utf-8"')
def test_7bit_input(self):
eq = self.assertEqual
diff --git a/Lib/test/test_email/test_message.py b/Lib/test/test_email/test_message.py
index 50e1a63..d78049e 100644
--- a/Lib/test/test_email/test_message.py
+++ b/Lib/test/test_email/test_message.py
@@ -723,24 +723,14 @@ class TestEmailMessageBase:
def test_is_attachment(self):
m = self._make_message()
self.assertFalse(m.is_attachment())
- with self.assertWarns(DeprecationWarning):
- self.assertFalse(m.is_attachment)
m['Content-Disposition'] = 'inline'
self.assertFalse(m.is_attachment())
- with self.assertWarns(DeprecationWarning):
- self.assertFalse(m.is_attachment)
m.replace_header('Content-Disposition', 'attachment')
self.assertTrue(m.is_attachment())
- with self.assertWarns(DeprecationWarning):
- self.assertTrue(m.is_attachment)
m.replace_header('Content-Disposition', 'AtTachMent')
self.assertTrue(m.is_attachment())
- with self.assertWarns(DeprecationWarning):
- self.assertTrue(m.is_attachment)
m.set_param('filename', 'abc.png', 'Content-Disposition')
self.assertTrue(m.is_attachment())
- with self.assertWarns(DeprecationWarning):
- self.assertTrue(m.is_attachment)
class TestEmailMessage(TestEmailMessageBase, TestEmailBase):
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index dccaa4f..7d172c8 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -646,6 +646,23 @@ class TestEnum(unittest.TestCase):
self.assertIn(e, SummerMonth)
self.assertIs(type(e), SummerMonth)
+ def test_programatic_function_string_with_start(self):
+ SummerMonth = Enum('SummerMonth', 'june july august', start=10)
+ lst = list(SummerMonth)
+ self.assertEqual(len(lst), len(SummerMonth))
+ self.assertEqual(len(SummerMonth), 3, SummerMonth)
+ self.assertEqual(
+ [SummerMonth.june, SummerMonth.july, SummerMonth.august],
+ lst,
+ )
+ for i, month in enumerate('june july august'.split(), 10):
+ e = SummerMonth(i)
+ self.assertEqual(int(e.value), i)
+ self.assertNotEqual(e, i)
+ self.assertEqual(e.name, month)
+ self.assertIn(e, SummerMonth)
+ self.assertIs(type(e), SummerMonth)
+
def test_programatic_function_string_list(self):
SummerMonth = Enum('SummerMonth', ['june', 'july', 'august'])
lst = list(SummerMonth)
@@ -663,6 +680,23 @@ class TestEnum(unittest.TestCase):
self.assertIn(e, SummerMonth)
self.assertIs(type(e), SummerMonth)
+ def test_programatic_function_string_list_with_start(self):
+ SummerMonth = Enum('SummerMonth', ['june', 'july', 'august'], start=20)
+ lst = list(SummerMonth)
+ self.assertEqual(len(lst), len(SummerMonth))
+ self.assertEqual(len(SummerMonth), 3, SummerMonth)
+ self.assertEqual(
+ [SummerMonth.june, SummerMonth.july, SummerMonth.august],
+ lst,
+ )
+ for i, month in enumerate('june july august'.split(), 20):
+ e = SummerMonth(i)
+ self.assertEqual(int(e.value), i)
+ self.assertNotEqual(e, i)
+ self.assertEqual(e.name, month)
+ self.assertIn(e, SummerMonth)
+ self.assertIs(type(e), SummerMonth)
+
def test_programatic_function_iterable(self):
SummerMonth = Enum(
'SummerMonth',
@@ -719,6 +753,22 @@ class TestEnum(unittest.TestCase):
self.assertIn(e, SummerMonth)
self.assertIs(type(e), SummerMonth)
+ def test_programatic_function_type_with_start(self):
+ SummerMonth = Enum('SummerMonth', 'june july august', type=int, start=30)
+ lst = list(SummerMonth)
+ self.assertEqual(len(lst), len(SummerMonth))
+ self.assertEqual(len(SummerMonth), 3, SummerMonth)
+ self.assertEqual(
+ [SummerMonth.june, SummerMonth.july, SummerMonth.august],
+ lst,
+ )
+ for i, month in enumerate('june july august'.split(), 30):
+ e = SummerMonth(i)
+ self.assertEqual(e, i)
+ self.assertEqual(e.name, month)
+ self.assertIn(e, SummerMonth)
+ self.assertIs(type(e), SummerMonth)
+
def test_programatic_function_type_from_subclass(self):
SummerMonth = IntEnum('SummerMonth', 'june july august')
lst = list(SummerMonth)
@@ -735,6 +785,22 @@ class TestEnum(unittest.TestCase):
self.assertIn(e, SummerMonth)
self.assertIs(type(e), SummerMonth)
+ def test_programatic_function_type_from_subclass_with_start(self):
+ SummerMonth = IntEnum('SummerMonth', 'june july august', start=40)
+ lst = list(SummerMonth)
+ self.assertEqual(len(lst), len(SummerMonth))
+ self.assertEqual(len(SummerMonth), 3, SummerMonth)
+ self.assertEqual(
+ [SummerMonth.june, SummerMonth.july, SummerMonth.august],
+ lst,
+ )
+ for i, month in enumerate('june july august'.split(), 40):
+ e = SummerMonth(i)
+ self.assertEqual(e, i)
+ self.assertEqual(e.name, month)
+ self.assertIn(e, SummerMonth)
+ self.assertIs(type(e), SummerMonth)
+
def test_subclassing(self):
if isinstance(Name, Exception):
raise Name
diff --git a/Lib/test/test_epoll.py b/Lib/test/test_epoll.py
index b37f033..d727403 100644
--- a/Lib/test/test_epoll.py
+++ b/Lib/test/test_epoll.py
@@ -44,7 +44,7 @@ class TestEPoll(unittest.TestCase):
def setUp(self):
self.serverSocket = socket.socket()
self.serverSocket.bind(('127.0.0.1', 0))
- self.serverSocket.listen(1)
+ self.serverSocket.listen()
self.connections = [self.serverSocket]
def tearDown(self):
diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py
index e68a09e..35129b6 100644
--- a/Lib/test/test_faulthandler.py
+++ b/Lib/test/test_faulthandler.py
@@ -17,6 +17,10 @@ try:
HAVE_THREADS = True
except ImportError:
HAVE_THREADS = False
+try:
+ import _testcapi
+except ImportError:
+ _testcapi = None
TIMEOUT = 0.5
@@ -135,26 +139,32 @@ class FaultHandlerTests(unittest.TestCase):
3,
'Floating point exception')
- @unittest.skipIf(not hasattr(faulthandler, '_sigbus'),
- "need faulthandler._sigbus()")
+ @unittest.skipIf(_testcapi is None, 'need _testcapi')
+ @unittest.skipUnless(hasattr(signal, 'SIGBUS'), 'need signal.SIGBUS')
def test_sigbus(self):
self.check_fatal_error("""
+ import _testcapi
import faulthandler
+ import signal
+
faulthandler.enable()
- faulthandler._sigbus()
+ _testcapi.raise_signal(signal.SIGBUS)
""",
- 3,
+ 6,
'Bus error')
- @unittest.skipIf(not hasattr(faulthandler, '_sigill'),
- "need faulthandler._sigill()")
+ @unittest.skipIf(_testcapi is None, 'need _testcapi')
+ @unittest.skipUnless(hasattr(signal, 'SIGILL'), 'need signal.SIGILL')
def test_sigill(self):
self.check_fatal_error("""
+ import _testcapi
import faulthandler
+ import signal
+
faulthandler.enable()
- faulthandler._sigill()
+ _testcapi.raise_signal(signal.SIGILL)
""",
- 3,
+ 6,
'Illegal instruction')
def test_fatal_error(self):
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
index c37482e..743ca5c 100644
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -60,6 +60,15 @@ class AutoFileTests(unittest.TestCase):
self.assertRaises((AttributeError, TypeError),
setattr, f, attr, 'oops')
+ def testBlksize(self):
+ # test private _blksize attribute
+ blksize = io.DEFAULT_BUFFER_SIZE
+ # try to get preferred blksize from stat.st_blksize, if available
+ if hasattr(os, 'fstat'):
+ fst = os.fstat(self.f.fileno())
+ blksize = getattr(fst, 'st_blksize', blksize)
+ self.assertEqual(self.f._blksize, blksize)
+
def testReadinto(self):
# verify readinto
self.f.write(bytes([1, 2]))
@@ -103,14 +112,26 @@ class AutoFileTests(unittest.TestCase):
self.assertRaises(TypeError, self.f.write, "Hello!")
def testRepr(self):
- self.assertEqual(repr(self.f), "<_io.FileIO name=%r mode=%r>"
- % (self.f.name, self.f.mode))
+ self.assertEqual(
+ repr(self.f), "<_io.FileIO name=%r mode=%r closefd=True>"
+ % (self.f.name, self.f.mode))
del self.f.name
- self.assertEqual(repr(self.f), "<_io.FileIO fd=%r mode=%r>"
- % (self.f.fileno(), self.f.mode))
+ self.assertEqual(
+ repr(self.f), "<_io.FileIO fd=%r mode=%r closefd=True>"
+ % (self.f.fileno(), self.f.mode))
self.f.close()
self.assertEqual(repr(self.f), "<_io.FileIO [closed]>")
+ def testReprNoCloseFD(self):
+ fd = os.open(TESTFN, os.O_RDONLY)
+ try:
+ with _FileIO(fd, 'r', closefd=False) as f:
+ self.assertEqual(repr(f),
+ "<_io.FileIO name=%r mode=%r closefd=False>"
+ % (f.name, f.mode))
+ finally:
+ os.close(fd)
+
def testErrors(self):
f = self.f
self.assertTrue(not f.isatty())
@@ -141,7 +162,7 @@ class AutoFileTests(unittest.TestCase):
def testOpendir(self):
# Issue 3703: opening a directory should fill the errno
# Windows always returns "[Errno 13]: Permission denied
- # Unix calls dircheck() and returns "[Errno 21]: Is a directory"
+ # Unix uses fstat and returns "[Errno 21]: Is a directory"
try:
_FileIO('.', 'r')
except OSError as e:
@@ -352,8 +373,8 @@ class OtherFileTests(unittest.TestCase):
def testConstructorHandlesNULChars(self):
fn_with_NUL = 'foo\0bar'
- self.assertRaises(TypeError, _FileIO, fn_with_NUL, 'w')
- self.assertRaises(TypeError, _FileIO, bytes(fn_with_NUL, 'ascii'), 'w')
+ self.assertRaises(ValueError, _FileIO, fn_with_NUL, 'w')
+ self.assertRaises(ValueError, _FileIO, bytes(fn_with_NUL, 'ascii'), 'w')
def testInvalidFd(self):
self.assertRaises(ValueError, _FileIO, -10)
diff --git a/Lib/test/test_fork1.py b/Lib/test/test_fork1.py
index e0626df..8bcbd46 100644
--- a/Lib/test/test_fork1.py
+++ b/Lib/test/test_fork1.py
@@ -18,13 +18,14 @@ get_attribute(os, 'fork')
class ForkTest(ForkWait):
def wait_impl(self, cpid):
- for i in range(10):
+ deadline = time.monotonic() + 10.0
+ while time.monotonic() <= deadline:
# waitpid() shouldn't hang, but some of the buildbots seem to hang
# in the forking tests. This is an attempt to fix the problem.
spid, status = os.waitpid(cpid, os.WNOHANG)
if spid == cpid:
break
- time.sleep(1.0)
+ time.sleep(0.1)
self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index fc71e48..9d1f5d3 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -9,7 +9,7 @@ maxsize = support.MAX_Py_ssize_t
# test string formatting operator (I am not sure if this is being tested
# elsewhere but, surely, some of the given cases are *not* tested because
# they crash python)
-# test on unicode strings as well
+# test on bytes object as well
def testformat(formatstr, args, output=None, limit=None, overflowok=False):
if verbose:
@@ -46,191 +46,209 @@ def testformat(formatstr, args, output=None, limit=None, overflowok=False):
if verbose:
print('yes')
+def testcommon(formatstr, args, output=None, limit=None, overflowok=False):
+ # if formatstr is a str, test str, bytes, and bytearray;
+ # otherwise, test bytes and bytearry
+ if isinstance(formatstr, str):
+ testformat(formatstr, args, output, limit, overflowok)
+ b_format = formatstr.encode('ascii')
+ else:
+ b_format = formatstr
+ ba_format = bytearray(b_format)
+ b_args = []
+ if not isinstance(args, tuple):
+ args = (args, )
+ b_args = tuple(args)
+ if output is None:
+ b_output = ba_output = None
+ else:
+ if isinstance(output, str):
+ b_output = output.encode('ascii')
+ else:
+ b_output = output
+ ba_output = bytearray(b_output)
+ testformat(b_format, b_args, b_output, limit, overflowok)
+ testformat(ba_format, b_args, ba_output, limit, overflowok)
+
class FormatTest(unittest.TestCase):
- def test_format(self):
- testformat("%.1d", (1,), "1")
- testformat("%.*d", (sys.maxsize,1), overflowok=True) # expect overflow
- testformat("%.100d", (1,), '00000000000000000000000000000000000000'
+
+ def test_common_format(self):
+ # test the format identifiers that work the same across
+ # str, bytes, and bytearrays (integer, float, oct, hex)
+ testcommon("%.1d", (1,), "1")
+ testcommon("%.*d", (sys.maxsize,1), overflowok=True) # expect overflow
+ testcommon("%.100d", (1,), '00000000000000000000000000000000000000'
'000000000000000000000000000000000000000000000000000000'
'00000001', overflowok=True)
- testformat("%#.117x", (1,), '0x00000000000000000000000000000000000'
+ testcommon("%#.117x", (1,), '0x00000000000000000000000000000000000'
'000000000000000000000000000000000000000000000000000000'
'0000000000000000000000000001',
overflowok=True)
- testformat("%#.118x", (1,), '0x00000000000000000000000000000000000'
+ testcommon("%#.118x", (1,), '0x00000000000000000000000000000000000'
'000000000000000000000000000000000000000000000000000000'
'00000000000000000000000000001',
overflowok=True)
- testformat("%f", (1.0,), "1.000000")
+ testcommon("%f", (1.0,), "1.000000")
# these are trying to test the limits of the internal magic-number-length
# formatting buffer, if that number changes then these tests are less
# effective
- testformat("%#.*g", (109, -1.e+49/3.))
- testformat("%#.*g", (110, -1.e+49/3.))
- testformat("%#.*g", (110, -1.e+100/3.))
+ testcommon("%#.*g", (109, -1.e+49/3.))
+ testcommon("%#.*g", (110, -1.e+49/3.))
+ testcommon("%#.*g", (110, -1.e+100/3.))
# test some ridiculously large precision, expect overflow
- testformat('%12.*f', (123456, 1.0))
+ testcommon('%12.*f', (123456, 1.0))
# check for internal overflow validation on length of precision
# these tests should no longer cause overflow in Python
# 2.7/3.1 and later.
- testformat("%#.*g", (110, -1.e+100/3.))
- testformat("%#.*G", (110, -1.e+100/3.))
- testformat("%#.*f", (110, -1.e+100/3.))
- testformat("%#.*F", (110, -1.e+100/3.))
+ testcommon("%#.*g", (110, -1.e+100/3.))
+ testcommon("%#.*G", (110, -1.e+100/3.))
+ testcommon("%#.*f", (110, -1.e+100/3.))
+ testcommon("%#.*F", (110, -1.e+100/3.))
# Formatting of integers. Overflow is not ok
- testformat("%x", 10, "a")
- testformat("%x", 100000000000, "174876e800")
- testformat("%o", 10, "12")
- testformat("%o", 100000000000, "1351035564000")
- testformat("%d", 10, "10")
- testformat("%d", 100000000000, "100000000000")
+ testcommon("%x", 10, "a")
+ testcommon("%x", 100000000000, "174876e800")
+ testcommon("%o", 10, "12")
+ testcommon("%o", 100000000000, "1351035564000")
+ testcommon("%d", 10, "10")
+ testcommon("%d", 100000000000, "100000000000")
big = 123456789012345678901234567890
- testformat("%d", big, "123456789012345678901234567890")
- testformat("%d", -big, "-123456789012345678901234567890")
- testformat("%5d", -big, "-123456789012345678901234567890")
- testformat("%31d", -big, "-123456789012345678901234567890")
- testformat("%32d", -big, " -123456789012345678901234567890")
- testformat("%-32d", -big, "-123456789012345678901234567890 ")
- testformat("%032d", -big, "-0123456789012345678901234567890")
- testformat("%-032d", -big, "-123456789012345678901234567890 ")
- testformat("%034d", -big, "-000123456789012345678901234567890")
- testformat("%034d", big, "0000123456789012345678901234567890")
- testformat("%0+34d", big, "+000123456789012345678901234567890")
- testformat("%+34d", big, " +123456789012345678901234567890")
- testformat("%34d", big, " 123456789012345678901234567890")
- testformat("%.2d", big, "123456789012345678901234567890")
- testformat("%.30d", big, "123456789012345678901234567890")
- testformat("%.31d", big, "0123456789012345678901234567890")
- testformat("%32.31d", big, " 0123456789012345678901234567890")
- testformat("%d", float(big), "123456________________________", 6)
+ testcommon("%d", big, "123456789012345678901234567890")
+ testcommon("%d", -big, "-123456789012345678901234567890")
+ testcommon("%5d", -big, "-123456789012345678901234567890")
+ testcommon("%31d", -big, "-123456789012345678901234567890")
+ testcommon("%32d", -big, " -123456789012345678901234567890")
+ testcommon("%-32d", -big, "-123456789012345678901234567890 ")
+ testcommon("%032d", -big, "-0123456789012345678901234567890")
+ testcommon("%-032d", -big, "-123456789012345678901234567890 ")
+ testcommon("%034d", -big, "-000123456789012345678901234567890")
+ testcommon("%034d", big, "0000123456789012345678901234567890")
+ testcommon("%0+34d", big, "+000123456789012345678901234567890")
+ testcommon("%+34d", big, " +123456789012345678901234567890")
+ testcommon("%34d", big, " 123456789012345678901234567890")
+ testcommon("%.2d", big, "123456789012345678901234567890")
+ testcommon("%.30d", big, "123456789012345678901234567890")
+ testcommon("%.31d", big, "0123456789012345678901234567890")
+ testcommon("%32.31d", big, " 0123456789012345678901234567890")
+ testcommon("%d", float(big), "123456________________________", 6)
big = 0x1234567890abcdef12345 # 21 hex digits
- testformat("%x", big, "1234567890abcdef12345")
- testformat("%x", -big, "-1234567890abcdef12345")
- testformat("%5x", -big, "-1234567890abcdef12345")
- testformat("%22x", -big, "-1234567890abcdef12345")
- testformat("%23x", -big, " -1234567890abcdef12345")
- testformat("%-23x", -big, "-1234567890abcdef12345 ")
- testformat("%023x", -big, "-01234567890abcdef12345")
- testformat("%-023x", -big, "-1234567890abcdef12345 ")
- testformat("%025x", -big, "-0001234567890abcdef12345")
- testformat("%025x", big, "00001234567890abcdef12345")
- testformat("%0+25x", big, "+0001234567890abcdef12345")
- testformat("%+25x", big, " +1234567890abcdef12345")
- testformat("%25x", big, " 1234567890abcdef12345")
- testformat("%.2x", big, "1234567890abcdef12345")
- testformat("%.21x", big, "1234567890abcdef12345")
- testformat("%.22x", big, "01234567890abcdef12345")
- testformat("%23.22x", big, " 01234567890abcdef12345")
- testformat("%-23.22x", big, "01234567890abcdef12345 ")
- testformat("%X", big, "1234567890ABCDEF12345")
- testformat("%#X", big, "0X1234567890ABCDEF12345")
- testformat("%#x", big, "0x1234567890abcdef12345")
- testformat("%#x", -big, "-0x1234567890abcdef12345")
- testformat("%#.23x", -big, "-0x001234567890abcdef12345")
- testformat("%#+.23x", big, "+0x001234567890abcdef12345")
- testformat("%# .23x", big, " 0x001234567890abcdef12345")
- testformat("%#+.23X", big, "+0X001234567890ABCDEF12345")
- testformat("%#-+.23X", big, "+0X001234567890ABCDEF12345")
- testformat("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
- testformat("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
- testformat("%#+27.23X", big, " +0X001234567890ABCDEF12345")
+ testcommon("%x", big, "1234567890abcdef12345")
+ testcommon("%x", -big, "-1234567890abcdef12345")
+ testcommon("%5x", -big, "-1234567890abcdef12345")
+ testcommon("%22x", -big, "-1234567890abcdef12345")
+ testcommon("%23x", -big, " -1234567890abcdef12345")
+ testcommon("%-23x", -big, "-1234567890abcdef12345 ")
+ testcommon("%023x", -big, "-01234567890abcdef12345")
+ testcommon("%-023x", -big, "-1234567890abcdef12345 ")
+ testcommon("%025x", -big, "-0001234567890abcdef12345")
+ testcommon("%025x", big, "00001234567890abcdef12345")
+ testcommon("%0+25x", big, "+0001234567890abcdef12345")
+ testcommon("%+25x", big, " +1234567890abcdef12345")
+ testcommon("%25x", big, " 1234567890abcdef12345")
+ testcommon("%.2x", big, "1234567890abcdef12345")
+ testcommon("%.21x", big, "1234567890abcdef12345")
+ testcommon("%.22x", big, "01234567890abcdef12345")
+ testcommon("%23.22x", big, " 01234567890abcdef12345")
+ testcommon("%-23.22x", big, "01234567890abcdef12345 ")
+ testcommon("%X", big, "1234567890ABCDEF12345")
+ testcommon("%#X", big, "0X1234567890ABCDEF12345")
+ testcommon("%#x", big, "0x1234567890abcdef12345")
+ testcommon("%#x", -big, "-0x1234567890abcdef12345")
+ testcommon("%#.23x", -big, "-0x001234567890abcdef12345")
+ testcommon("%#+.23x", big, "+0x001234567890abcdef12345")
+ testcommon("%# .23x", big, " 0x001234567890abcdef12345")
+ testcommon("%#+.23X", big, "+0X001234567890ABCDEF12345")
+ testcommon("%#-+.23X", big, "+0X001234567890ABCDEF12345")
+ testcommon("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
+ testcommon("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
+ testcommon("%#+27.23X", big, " +0X001234567890ABCDEF12345")
# next one gets two leading zeroes from precision, and another from the
# 0 flag and the width
- testformat("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
+ testcommon("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
# same, except no 0 flag
- testformat("%#+27.23X", big, " +0X001234567890ABCDEF12345")
- with self.assertWarns(DeprecationWarning):
- testformat("%x", float(big), "123456_______________", 6)
+ testcommon("%#+27.23X", big, " +0X001234567890ABCDEF12345")
big = 0o12345670123456701234567012345670 # 32 octal digits
- testformat("%o", big, "12345670123456701234567012345670")
- testformat("%o", -big, "-12345670123456701234567012345670")
- testformat("%5o", -big, "-12345670123456701234567012345670")
- testformat("%33o", -big, "-12345670123456701234567012345670")
- testformat("%34o", -big, " -12345670123456701234567012345670")
- testformat("%-34o", -big, "-12345670123456701234567012345670 ")
- testformat("%034o", -big, "-012345670123456701234567012345670")
- testformat("%-034o", -big, "-12345670123456701234567012345670 ")
- testformat("%036o", -big, "-00012345670123456701234567012345670")
- testformat("%036o", big, "000012345670123456701234567012345670")
- testformat("%0+36o", big, "+00012345670123456701234567012345670")
- testformat("%+36o", big, " +12345670123456701234567012345670")
- testformat("%36o", big, " 12345670123456701234567012345670")
- testformat("%.2o", big, "12345670123456701234567012345670")
- testformat("%.32o", big, "12345670123456701234567012345670")
- testformat("%.33o", big, "012345670123456701234567012345670")
- testformat("%34.33o", big, " 012345670123456701234567012345670")
- testformat("%-34.33o", big, "012345670123456701234567012345670 ")
- testformat("%o", big, "12345670123456701234567012345670")
- testformat("%#o", big, "0o12345670123456701234567012345670")
- testformat("%#o", -big, "-0o12345670123456701234567012345670")
- testformat("%#.34o", -big, "-0o0012345670123456701234567012345670")
- testformat("%#+.34o", big, "+0o0012345670123456701234567012345670")
- testformat("%# .34o", big, " 0o0012345670123456701234567012345670")
- testformat("%#+.34o", big, "+0o0012345670123456701234567012345670")
- testformat("%#-+.34o", big, "+0o0012345670123456701234567012345670")
- testformat("%#-+37.34o", big, "+0o0012345670123456701234567012345670")
- testformat("%#+37.34o", big, "+0o0012345670123456701234567012345670")
+ testcommon("%o", big, "12345670123456701234567012345670")
+ testcommon("%o", -big, "-12345670123456701234567012345670")
+ testcommon("%5o", -big, "-12345670123456701234567012345670")
+ testcommon("%33o", -big, "-12345670123456701234567012345670")
+ testcommon("%34o", -big, " -12345670123456701234567012345670")
+ testcommon("%-34o", -big, "-12345670123456701234567012345670 ")
+ testcommon("%034o", -big, "-012345670123456701234567012345670")
+ testcommon("%-034o", -big, "-12345670123456701234567012345670 ")
+ testcommon("%036o", -big, "-00012345670123456701234567012345670")
+ testcommon("%036o", big, "000012345670123456701234567012345670")
+ testcommon("%0+36o", big, "+00012345670123456701234567012345670")
+ testcommon("%+36o", big, " +12345670123456701234567012345670")
+ testcommon("%36o", big, " 12345670123456701234567012345670")
+ testcommon("%.2o", big, "12345670123456701234567012345670")
+ testcommon("%.32o", big, "12345670123456701234567012345670")
+ testcommon("%.33o", big, "012345670123456701234567012345670")
+ testcommon("%34.33o", big, " 012345670123456701234567012345670")
+ testcommon("%-34.33o", big, "012345670123456701234567012345670 ")
+ testcommon("%o", big, "12345670123456701234567012345670")
+ testcommon("%#o", big, "0o12345670123456701234567012345670")
+ testcommon("%#o", -big, "-0o12345670123456701234567012345670")
+ testcommon("%#.34o", -big, "-0o0012345670123456701234567012345670")
+ testcommon("%#+.34o", big, "+0o0012345670123456701234567012345670")
+ testcommon("%# .34o", big, " 0o0012345670123456701234567012345670")
+ testcommon("%#+.34o", big, "+0o0012345670123456701234567012345670")
+ testcommon("%#-+.34o", big, "+0o0012345670123456701234567012345670")
+ testcommon("%#-+37.34o", big, "+0o0012345670123456701234567012345670")
+ testcommon("%#+37.34o", big, "+0o0012345670123456701234567012345670")
# next one gets one leading zero from precision
- testformat("%.33o", big, "012345670123456701234567012345670")
+ testcommon("%.33o", big, "012345670123456701234567012345670")
# base marker shouldn't change that, since "0" is redundant
- testformat("%#.33o", big, "0o012345670123456701234567012345670")
+ testcommon("%#.33o", big, "0o012345670123456701234567012345670")
# but reduce precision, and base marker should add a zero
- testformat("%#.32o", big, "0o12345670123456701234567012345670")
+ testcommon("%#.32o", big, "0o12345670123456701234567012345670")
# one leading zero from precision, and another from "0" flag & width
- testformat("%034.33o", big, "0012345670123456701234567012345670")
+ testcommon("%034.33o", big, "0012345670123456701234567012345670")
# base marker shouldn't change that
- testformat("%0#34.33o", big, "0o012345670123456701234567012345670")
- with self.assertWarns(DeprecationWarning):
- testformat("%o", float(big), "123456__________________________", 6)
+ testcommon("%0#34.33o", big, "0o012345670123456701234567012345670")
# Some small ints, in both Python int and flavors).
- testformat("%d", 42, "42")
- testformat("%d", -42, "-42")
- testformat("%d", 42, "42")
- testformat("%d", -42, "-42")
- testformat("%d", 42.0, "42")
- testformat("%#x", 1, "0x1")
- testformat("%#x", 1, "0x1")
- testformat("%#X", 1, "0X1")
- testformat("%#X", 1, "0X1")
- with self.assertWarns(DeprecationWarning):
- testformat("%#x", 1.0, "0x1")
- testformat("%#o", 1, "0o1")
- testformat("%#o", 1, "0o1")
- testformat("%#o", 0, "0o0")
- testformat("%#o", 0, "0o0")
- testformat("%o", 0, "0")
- testformat("%o", 0, "0")
- testformat("%d", 0, "0")
- testformat("%d", 0, "0")
- testformat("%#x", 0, "0x0")
- testformat("%#x", 0, "0x0")
- testformat("%#X", 0, "0X0")
- testformat("%#X", 0, "0X0")
- testformat("%x", 0x42, "42")
- testformat("%x", -0x42, "-42")
- testformat("%x", 0x42, "42")
- testformat("%x", -0x42, "-42")
- with self.assertWarns(DeprecationWarning):
- testformat("%x", float(0x42), "42")
- testformat("%o", 0o42, "42")
- testformat("%o", -0o42, "-42")
- testformat("%o", 0o42, "42")
- testformat("%o", -0o42, "-42")
- with self.assertWarns(DeprecationWarning):
- testformat("%o", float(0o42), "42")
+ testcommon("%d", 42, "42")
+ testcommon("%d", -42, "-42")
+ testcommon("%d", 42, "42")
+ testcommon("%d", -42, "-42")
+ testcommon("%d", 42.0, "42")
+ testcommon("%#x", 1, "0x1")
+ testcommon("%#x", 1, "0x1")
+ testcommon("%#X", 1, "0X1")
+ testcommon("%#X", 1, "0X1")
+ testcommon("%#o", 1, "0o1")
+ testcommon("%#o", 1, "0o1")
+ testcommon("%#o", 0, "0o0")
+ testcommon("%#o", 0, "0o0")
+ testcommon("%o", 0, "0")
+ testcommon("%o", 0, "0")
+ testcommon("%d", 0, "0")
+ testcommon("%d", 0, "0")
+ testcommon("%#x", 0, "0x0")
+ testcommon("%#x", 0, "0x0")
+ testcommon("%#X", 0, "0X0")
+ testcommon("%#X", 0, "0X0")
+ testcommon("%x", 0x42, "42")
+ testcommon("%x", -0x42, "-42")
+ testcommon("%x", 0x42, "42")
+ testcommon("%x", -0x42, "-42")
+ testcommon("%o", 0o42, "42")
+ testcommon("%o", -0o42, "-42")
+ testcommon("%o", 0o42, "42")
+ testcommon("%o", -0o42, "-42")
+ # alternate float formatting
+ testcommon('%g', 1.1, '1.1')
+ testcommon('%#g', 1.1, '1.10000')
+
+ def test_str_format(self):
testformat("%r", "\u0378", "'\\u0378'") # non printable
testformat("%a", "\u0378", "'\\u0378'") # non printable
testformat("%r", "\u0374", "'\u0374'") # printable
testformat("%a", "\u0374", "'\\u0374'") # printable
- # alternate float formatting
- testformat('%g', 1.1, '1.1')
- testformat('%#g', 1.1, '1.10000')
-
- # Test exception for unknown format characters
+ # Test exception for unknown format characters, etc.
if verbose:
print('Testing exceptions')
def test_exc(formatstr, args, exception, excmsg):
@@ -257,8 +275,83 @@ class FormatTest(unittest.TestCase):
test_exc('%g', '1', TypeError, "a float is required")
test_exc('no format', '1', TypeError,
"not all arguments converted during string formatting")
- test_exc('no format', '1', TypeError,
- "not all arguments converted during string formatting")
+
+ if maxsize == 2**31-1:
+ # crashes 2.2.1 and earlier:
+ try:
+ "%*d"%(maxsize, -127)
+ except MemoryError:
+ pass
+ else:
+ raise TestFailed('"%*d"%(maxsize, -127) should fail')
+
+ def test_bytes_and_bytearray_format(self):
+ # %c will insert a single byte, either from an int in range(256), or
+ # from a bytes argument of length 1, not from a str.
+ testcommon(b"%c", 7, b"\x07")
+ testcommon(b"%c", b"Z", b"Z")
+ testcommon(b"%c", bytearray(b"Z"), b"Z")
+ # %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):
+ def __bytes__(self):
+ return b'123'
+ fb = FakeBytes()
+ testcommon(b"%b", b"abc", b"abc")
+ testcommon(b"%b", bytearray(b"def"), b"def")
+ testcommon(b"%b", fb, b"123")
+ # # %s is an alias for %b -- should only be used for Py2/3 code
+ testcommon(b"%s", b"abc", b"abc")
+ testcommon(b"%s", bytearray(b"def"), b"def")
+ testcommon(b"%s", fb, b"123")
+ # %a will give the equivalent of
+ # repr(some_obj).encode('ascii', 'backslashreplace')
+ testcommon(b"%a", 3.14, b"3.14")
+ testcommon(b"%a", b"ghi", b"b'ghi'")
+ testcommon(b"%a", "jkl", b"'jkl'")
+ testcommon(b"%a", "\u0544", b"'\\u0544'")
+
+ # Test exception for unknown format characters, etc.
+ if verbose:
+ print('Testing exceptions')
+ def test_exc(formatstr, args, exception, excmsg):
+ try:
+ testformat(formatstr, args)
+ except exception as exc:
+ if str(exc) == excmsg:
+ if verbose:
+ print("yes")
+ else:
+ if verbose: print('no')
+ print('Unexpected ', exception, ':', repr(str(exc)))
+ except:
+ if verbose: print('no')
+ print('Unexpected exception')
+ raise
+ else:
+ raise TestFailed('did not get expected exception: %s' % excmsg)
+ test_exc(b'%d', '1', TypeError,
+ "%d format: a number is required, not str")
+ test_exc(b'%d', b'1', TypeError,
+ "%d format: a number is required, not bytes")
+ test_exc(b'%g', '1', TypeError, "float argument required, not str")
+ test_exc(b'%g', b'1', TypeError, "float argument required, not bytes")
+ test_exc(b'no format', 7, TypeError,
+ "not all arguments converted during bytes formatting")
+ test_exc(b'no format', b'1', TypeError,
+ "not all arguments converted during bytes formatting")
+ test_exc(b'no format', bytearray(b'1'), TypeError,
+ "not all arguments converted during bytes formatting")
+ test_exc(b"%c", 256, TypeError,
+ "%c requires an integer in range(256) or a single byte")
+ test_exc(b"%c", b"Za", TypeError,
+ "%c requires an integer in range(256) or a single byte")
+ test_exc(b"%c", "Yb", TypeError,
+ "%c requires an integer in range(256) or a single byte")
+ test_exc(b"%b", "Xc", TypeError,
+ "%b requires bytes, or an object that implements __bytes__, not 'str'")
+ test_exc(b"%s", "Wd", TypeError,
+ "%b requires bytes, or an object that implements __bytes__, not 'str'")
if maxsize == 2**31-1:
# crashes 2.2.1 and earlier:
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index 3336532..e86d5ce 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -330,7 +330,6 @@ class FractionTest(unittest.TestCase):
self.assertTypedEquals(F(-2, 10), round(F(-15, 100), 1))
self.assertTypedEquals(F(-2, 10), round(F(-25, 100), 1))
-
def testArithmetic(self):
self.assertEqual(F(1, 2), F(1, 10) + F(2, 5))
self.assertEqual(F(-3, 10), F(1, 10) - F(2, 5))
@@ -402,6 +401,8 @@ class FractionTest(unittest.TestCase):
self.assertTypedEquals(2.0 , 4 ** F(1, 2))
self.assertTypedEquals(0.25, 2.0 ** F(-2, 1))
self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10))
+ self.assertRaises(ZeroDivisionError, operator.pow,
+ F(0, 1), -2)
def testMixingWithDecimal(self):
# Decimal refuses mixed arithmetic (but not mixed comparisons)
diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
index d3be7d6..aef66da 100644
--- a/Lib/test/test_ftplib.py
+++ b/Lib/test/test_ftplib.py
@@ -73,7 +73,7 @@ class DummyDTPHandler(asynchat.async_chat):
super(DummyDTPHandler, self).push(what.encode('ascii'))
def handle_error(self):
- raise
+ raise Exception
class DummyFTPHandler(asynchat.async_chat):
@@ -118,7 +118,7 @@ class DummyFTPHandler(asynchat.async_chat):
self.push('550 command "%s" not understood.' %cmd)
def handle_error(self):
- raise
+ raise Exception
def push(self, data):
asynchat.async_chat.push(self, data.encode('ascii') + b'\r\n')
@@ -134,7 +134,7 @@ class DummyFTPHandler(asynchat.async_chat):
def cmd_pasv(self, arg):
with socket.socket() as sock:
sock.bind((self.socket.getsockname()[0], 0))
- sock.listen(5)
+ sock.listen()
sock.settimeout(TIMEOUT)
ip, port = sock.getsockname()[:2]
ip = ip.replace('.', ','); p1 = port / 256; p2 = port % 256
@@ -152,7 +152,7 @@ class DummyFTPHandler(asynchat.async_chat):
def cmd_epsv(self, arg):
with socket.socket(socket.AF_INET6) as sock:
sock.bind((self.socket.getsockname()[0], 0))
- sock.listen(5)
+ sock.listen()
sock.settimeout(TIMEOUT)
port = sock.getsockname()[1]
self.push('229 entering extended passive mode (|||%d|)' %port)
@@ -296,7 +296,7 @@ class DummyFTPServer(asyncore.dispatcher, threading.Thread):
return 0
def handle_error(self):
- raise
+ raise Exception
if ssl is not None:
@@ -394,7 +394,7 @@ if ssl is not None:
raise
def handle_error(self):
- raise
+ raise Exception
def close(self):
if (isinstance(self.socket, ssl.SSLSocket) and
@@ -670,7 +670,7 @@ class TestFTPClass(TestCase):
self.assertRaises(StopIteration, next, self.client.mlsd())
set_data('')
for x in self.client.mlsd():
- self.fail("unexpected data %s" % data)
+ self.fail("unexpected data %s" % x)
def test_makeport(self):
with self.client.makeport():
@@ -979,7 +979,7 @@ class TestTimeouts(TestCase):
# 1) when the connection is ready to be accepted.
# 2) when it is safe for the caller to close the connection
# 3) when we have closed the socket
- self.sock.listen(5)
+ self.sock.listen()
# (1) Signal the caller that we are ready to accept the connection.
self.evt.set()
try:
@@ -1049,19 +1049,8 @@ class TestTimeouts(TestCase):
ftp.close()
-class TestNetrcDeprecation(TestCase):
-
- def test_deprecation(self):
- with support.temp_cwd(), support.EnvironmentVarGuard() as env:
- env['HOME'] = os.getcwd()
- open('.netrc', 'w').close()
- with self.assertWarns(DeprecationWarning):
- ftplib.Netrc()
-
-
-
def test_main():
- tests = [TestFTPClass, TestTimeouts, TestNetrcDeprecation,
+ tests = [TestFTPClass, TestTimeouts,
TestIPv6Environment,
TestTLS_FTPClassMixin, TestTLS_FTPClass]
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 0375601c..55b96b4 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -882,6 +882,24 @@ class TestTotalOrdering(unittest.TestCase):
with self.assertRaises(TypeError):
a <= b
+ def test_pickle(self):
+ for proto in range(4, pickle.HIGHEST_PROTOCOL + 1):
+ for name in '__lt__', '__gt__', '__le__', '__ge__':
+ with self.subTest(method=name, proto=proto):
+ method = getattr(Orderable_LT, name)
+ method_copy = pickle.loads(pickle.dumps(method, proto))
+ self.assertIs(method_copy, method)
+
+@functools.total_ordering
+class Orderable_LT:
+ def __init__(self, value):
+ self.value = value
+ def __lt__(self, other):
+ return self.value < other.value
+ def __eq__(self, other):
+ return self.value == other.value
+
+
class TestLRU(unittest.TestCase):
def test_lru(self):
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index 7825a77..9e61013 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -50,6 +50,45 @@ class FinalizationTest(unittest.TestCase):
self.assertEqual(gc.garbage, old_garbage)
+class GeneratorTest(unittest.TestCase):
+
+ def test_name(self):
+ def func():
+ yield 1
+
+ # check generator names
+ gen = func()
+ self.assertEqual(gen.__name__, "func")
+ self.assertEqual(gen.__qualname__,
+ "GeneratorTest.test_name.<locals>.func")
+
+ # modify generator names
+ gen.__name__ = "name"
+ gen.__qualname__ = "qualname"
+ self.assertEqual(gen.__name__, "name")
+ self.assertEqual(gen.__qualname__, "qualname")
+
+ # generator names must be a string and cannot be deleted
+ self.assertRaises(TypeError, setattr, gen, '__name__', 123)
+ self.assertRaises(TypeError, setattr, gen, '__qualname__', 123)
+ self.assertRaises(TypeError, delattr, gen, '__name__')
+ self.assertRaises(TypeError, delattr, gen, '__qualname__')
+
+ # modify names of the function creating the generator
+ func.__qualname__ = "func_qualname"
+ func.__name__ = "func_name"
+ gen = func()
+ self.assertEqual(gen.__name__, "func_name")
+ self.assertEqual(gen.__qualname__, "func_qualname")
+
+ # unnamed generator
+ gen = (x for x in range(10))
+ self.assertEqual(gen.__name__,
+ "<genexpr>")
+ self.assertEqual(gen.__qualname__,
+ "GeneratorTest.test_name.<locals>.<genexpr>")
+
+
class ExceptionTest(unittest.TestCase):
# Tests for the issue #23353: check that the currently handled exception
# is correctly saved/restored in PyEval_EvalFrameEx().
diff --git a/Lib/test/test_genericpath.py b/Lib/test/test_genericpath.py
index e59ed4d..f2722bc 100644
--- a/Lib/test/test_genericpath.py
+++ b/Lib/test/test_genericpath.py
@@ -434,6 +434,40 @@ class CommonTest(GenericTest):
with support.temp_cwd(name):
self.test_abspath()
+ def test_join_errors(self):
+ # Check join() raises friendly TypeErrors.
+ with support.check_warnings(('', BytesWarning), quiet=True):
+ errmsg = "Can't mix strings and bytes in path components"
+ with self.assertRaisesRegex(TypeError, errmsg):
+ self.pathmodule.join(b'bytes', 'str')
+ 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'):
+ self.pathmodule.join(42, 'str')
+ with self.assertRaisesRegex(TypeError, errmsg % 'int'):
+ self.pathmodule.join('str', 42)
+ with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'):
+ self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
+
+ def test_relpath_errors(self):
+ # Check relpath() raises friendly TypeErrors.
+ with support.check_warnings(('', (BytesWarning, DeprecationWarning)),
+ quiet=True):
+ errmsg = "Can't mix strings and bytes in path components"
+ with self.assertRaisesRegex(TypeError, errmsg):
+ 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'):
+ self.pathmodule.relpath(42, 'str')
+ with self.assertRaisesRegex(TypeError, errmsg % 'int'):
+ self.pathmodule.relpath('str', 42)
+ with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'):
+ self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
+
if __name__=="__main__":
unittest.main()
diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py
index 1853a2d..cf1279d 100644
--- a/Lib/test/test_getargs2.py
+++ b/Lib/test/test_getargs2.py
@@ -482,7 +482,7 @@ class Bytes_TestCase(unittest.TestCase):
def test_s(self):
from _testcapi import getargs_s
self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9')
- self.assertRaises(TypeError, getargs_s, 'nul:\0')
+ self.assertRaises(ValueError, getargs_s, 'nul:\0')
self.assertRaises(TypeError, getargs_s, b'bytes')
self.assertRaises(TypeError, getargs_s, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview'))
@@ -509,7 +509,7 @@ class Bytes_TestCase(unittest.TestCase):
def test_z(self):
from _testcapi import getargs_z
self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9')
- self.assertRaises(TypeError, getargs_z, 'nul:\0')
+ self.assertRaises(ValueError, getargs_z, 'nul:\0')
self.assertRaises(TypeError, getargs_z, b'bytes')
self.assertRaises(TypeError, getargs_z, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview'))
@@ -537,7 +537,7 @@ class Bytes_TestCase(unittest.TestCase):
from _testcapi import getargs_y
self.assertRaises(TypeError, getargs_y, 'abc\xe9')
self.assertEqual(getargs_y(b'bytes'), b'bytes')
- self.assertRaises(TypeError, getargs_y, b'nul:\0')
+ self.assertRaises(ValueError, getargs_y, b'nul:\0')
self.assertRaises(TypeError, getargs_y, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_y, memoryview(b'memoryview'))
self.assertRaises(TypeError, getargs_y, None)
@@ -577,7 +577,7 @@ class Unicode_TestCase(unittest.TestCase):
def test_u(self):
from _testcapi import getargs_u
self.assertEqual(getargs_u('abc\xe9'), 'abc\xe9')
- self.assertRaises(TypeError, getargs_u, 'nul:\0')
+ self.assertRaises(ValueError, getargs_u, 'nul:\0')
self.assertRaises(TypeError, getargs_u, b'bytes')
self.assertRaises(TypeError, getargs_u, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_u, memoryview(b'memoryview'))
@@ -595,7 +595,7 @@ class Unicode_TestCase(unittest.TestCase):
def test_Z(self):
from _testcapi import getargs_Z
self.assertEqual(getargs_Z('abc\xe9'), 'abc\xe9')
- self.assertRaises(TypeError, getargs_Z, 'nul:\0')
+ self.assertRaises(ValueError, getargs_Z, 'nul:\0')
self.assertRaises(TypeError, getargs_Z, b'bytes')
self.assertRaises(TypeError, getargs_Z, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_Z, memoryview(b'memoryview'))
diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py
index abb312f..c7ceb3f 100644
--- a/Lib/test/test_gettext.py
+++ b/Lib/test/test_gettext.py
@@ -33,6 +33,55 @@ IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1
ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
'''
+# This data contains an invalid major version number (5)
+# An unexpected major version number should be treated as an error when
+# parsing a .mo file
+
+GNU_MO_DATA_BAD_MAJOR_VERSION = b'''\
+3hIElQAABQAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj
+AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD
+AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh
+eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU
+aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgbG9jYWxpemF0aW9u
+CnN1cHBvcnQgZm9yIHlvdXIgUHl0aG9uIHByb2dyYW1zIGJ5IHByb3ZpZGluZyBhbiBpbnRlcmZh
+Y2UgdG8gdGhlIEdOVQpnZXR0ZXh0IG1lc3NhZ2UgY2F0YWxvZyBsaWJyYXJ5LgBtdWxsdXNrAG51
+ZGdlIG51ZGdlAFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDAt
+MDgtMjkgMTI6MTktMDQ6MDAKTGFzdC1UcmFuc2xhdG9yOiBKLiBEYXZpZCBJYsOhw7FleiA8ai1k
+YXZpZEBub29zLmZyPgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpN
+SU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9aXNvLTg4
+NTktMQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiBub25lCkdlbmVyYXRlZC1CeTogcHlnZXR0
+ZXh0LnB5IDEuMQpQbHVyYWwtRm9ybXM6IG5wbHVyYWxzPTI7IHBsdXJhbD1uIT0xOwoAVGhyb2F0
+d29iYmxlciBNYW5ncm92ZQBIYXkgJXMgZmljaGVybwBIYXkgJXMgZmljaGVyb3MAR3V2ZiB6YnFo
+eXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVn
+IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1
+ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
+'''
+
+# This data contains an invalid minor version number (7)
+# An unexpected minor version number only indicates that some of the file's
+# contents may not be able to be read. It does not indicate an error.
+
+GNU_MO_DATA_BAD_MINOR_VERSION = b'''\
+3hIElQcAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj
+AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD
+AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh
+eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU
+aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgbG9jYWxpemF0aW9u
+CnN1cHBvcnQgZm9yIHlvdXIgUHl0aG9uIHByb2dyYW1zIGJ5IHByb3ZpZGluZyBhbiBpbnRlcmZh
+Y2UgdG8gdGhlIEdOVQpnZXR0ZXh0IG1lc3NhZ2UgY2F0YWxvZyBsaWJyYXJ5LgBtdWxsdXNrAG51
+ZGdlIG51ZGdlAFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDAt
+MDgtMjkgMTI6MTktMDQ6MDAKTGFzdC1UcmFuc2xhdG9yOiBKLiBEYXZpZCBJYsOhw7FleiA8ai1k
+YXZpZEBub29zLmZyPgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpN
+SU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9aXNvLTg4
+NTktMQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiBub25lCkdlbmVyYXRlZC1CeTogcHlnZXR0
+ZXh0LnB5IDEuMQpQbHVyYWwtRm9ybXM6IG5wbHVyYWxzPTI7IHBsdXJhbD1uIT0xOwoAVGhyb2F0
+d29iYmxlciBNYW5ncm92ZQBIYXkgJXMgZmljaGVybwBIYXkgJXMgZmljaGVyb3MAR3V2ZiB6YnFo
+eXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVn
+IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1
+ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
+'''
+
+
UMO_DATA = b'''\
3hIElQAAAAACAAAAHAAAACwAAAAFAAAAPAAAAAAAAABQAAAABAAAAFEAAAAPAQAAVgAAAAQAAABm
AQAAAQAAAAIAAAAAAAAAAAAAAAAAAAAAYWLDngBQcm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1S
@@ -56,6 +105,8 @@ bGUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4zCgA=
LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
+MOFILE_BAD_MAJOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_major_version.mo')
+MOFILE_BAD_MINOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_minor_version.mo')
UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
MMOFILE = os.path.join(LOCALEDIR, 'metadata.mo')
@@ -66,6 +117,10 @@ class GettextBaseTest(unittest.TestCase):
os.makedirs(LOCALEDIR)
with open(MOFILE, 'wb') as fp:
fp.write(base64.decodebytes(GNU_MO_DATA))
+ with open(MOFILE_BAD_MAJOR_VERSION, 'wb') as fp:
+ fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAJOR_VERSION))
+ with open(MOFILE_BAD_MINOR_VERSION, 'wb') as fp:
+ fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MINOR_VERSION))
with open(UMOFILE, 'wb') as fp:
fp.write(base64.decodebytes(UMO_DATA))
with open(MMOFILE, 'wb') as fp:
@@ -166,6 +221,21 @@ class GettextTestCase2(GettextBaseTest):
def test_textdomain(self):
self.assertEqual(gettext.textdomain(), 'gettext')
+ def test_bad_major_version(self):
+ with open(MOFILE_BAD_MAJOR_VERSION, 'rb') as fp:
+ with self.assertRaises(OSError) as cm:
+ gettext.GNUTranslations(fp)
+
+ exception = cm.exception
+ self.assertEqual(exception.errno, 0)
+ self.assertEqual(exception.strerror, "Bad version number 5")
+ self.assertEqual(exception.filename, MOFILE_BAD_MAJOR_VERSION)
+
+ def test_bad_minor_version(self):
+ with open(MOFILE_BAD_MINOR_VERSION, 'rb') as fp:
+ # Check that no error is thrown with a bad minor version number
+ gettext.GNUTranslations(fp)
+
def test_some_translations(self):
eq = self.assertEqual
# test some translations
diff --git a/Lib/test/test_glob.py b/Lib/test/test_glob.py
index a5ab8d6..21b0153 100644
--- a/Lib/test/test_glob.py
+++ b/Lib/test/test_glob.py
@@ -4,8 +4,8 @@ import shutil
import sys
import unittest
-from test.support import (run_unittest, TESTFN, skip_unless_symlink,
- can_symlink, create_empty_file)
+from test.support import (TESTFN, skip_unless_symlink,
+ can_symlink, create_empty_file, change_cwd)
class GlobTests(unittest.TestCase):
@@ -13,6 +13,9 @@ class GlobTests(unittest.TestCase):
def norm(self, *parts):
return os.path.normpath(os.path.join(self.tempdir, *parts))
+ def joins(self, *tuples):
+ return [os.path.join(self.tempdir, *parts) for parts in tuples]
+
def mktemp(self, *parts):
filename = self.norm(*parts)
base, file = os.path.split(filename)
@@ -38,17 +41,17 @@ class GlobTests(unittest.TestCase):
def tearDown(self):
shutil.rmtree(self.tempdir)
- def glob(self, *parts):
+ def glob(self, *parts, **kwargs):
if len(parts) == 1:
pattern = parts[0]
else:
pattern = os.path.join(*parts)
p = os.path.join(self.tempdir, pattern)
- res = glob.glob(p)
- self.assertEqual(list(glob.iglob(p)), res)
+ res = glob.glob(p, **kwargs)
+ self.assertEqual(list(glob.iglob(p, **kwargs)), res)
bres = [os.fsencode(x) for x in res]
- self.assertEqual(glob.glob(os.fsencode(p)), bres)
- self.assertEqual(list(glob.iglob(os.fsencode(p))), bres)
+ self.assertEqual(glob.glob(os.fsencode(p), **kwargs), bres)
+ self.assertEqual(list(glob.iglob(os.fsencode(p), **kwargs)), bres)
return res
def assertSequencesEqual_noorder(self, l1, l2):
@@ -192,9 +195,118 @@ class GlobTests(unittest.TestCase):
check('//?/c:/?', '//?/c:/[?]')
check('//*/*/*', '//*/*/[*]')
-def test_main():
- run_unittest(GlobTests)
+ def rglob(self, *parts, **kwargs):
+ return self.glob(*parts, recursive=True, **kwargs)
+
+ def test_recursive_glob(self):
+ eq = self.assertSequencesEqual_noorder
+ full = [('ZZZ',),
+ ('a',), ('a', 'D'),
+ ('a', 'bcd'),
+ ('a', 'bcd', 'EF'),
+ ('a', 'bcd', 'efg'),
+ ('a', 'bcd', 'efg', 'ha'),
+ ('aaa',), ('aaa', 'zzzF'),
+ ('aab',), ('aab', 'F'),
+ ]
+ if can_symlink():
+ full += [('sym1',), ('sym2',),
+ ('sym3',),
+ ('sym3', 'EF'),
+ ('sym3', 'efg'),
+ ('sym3', 'efg', 'ha'),
+ ]
+ eq(self.rglob('**'), self.joins(('',), *full))
+ eq(self.rglob('.', '**'), self.joins(('.',''),
+ *(('.',) + i for i in full)))
+ dirs = [('a', ''), ('a', 'bcd', ''), ('a', 'bcd', 'efg', ''),
+ ('aaa', ''), ('aab', '')]
+ if can_symlink():
+ dirs += [('sym3', ''), ('sym3', 'efg', '')]
+ eq(self.rglob('**', ''), self.joins(('',), *dirs))
+
+ eq(self.rglob('a', '**'), self.joins(
+ ('a', ''), ('a', 'D'), ('a', 'bcd'), ('a', 'bcd', 'EF'),
+ ('a', 'bcd', 'efg'), ('a', 'bcd', 'efg', 'ha')))
+ eq(self.rglob('a**'), self.joins(('a',), ('aaa',), ('aab',)))
+ expect = [('a', 'bcd', 'EF')]
+ if can_symlink():
+ expect += [('sym3', 'EF')]
+ eq(self.rglob('**', 'EF'), self.joins(*expect))
+ expect = [('a', 'bcd', 'EF'), ('aaa', 'zzzF'), ('aab', 'F')]
+ if can_symlink():
+ expect += [('sym3', 'EF')]
+ eq(self.rglob('**', '*F'), self.joins(*expect))
+ eq(self.rglob('**', '*F', ''), [])
+ eq(self.rglob('**', 'bcd', '*'), self.joins(
+ ('a', 'bcd', 'EF'), ('a', 'bcd', 'efg')))
+ eq(self.rglob('a', '**', 'bcd'), self.joins(('a', 'bcd')))
+
+ predir = os.path.abspath(os.curdir)
+ try:
+ os.chdir(self.tempdir)
+ join = os.path.join
+ eq(glob.glob('**', recursive=True), [join(*i) for i in full])
+ eq(glob.glob(join('**', ''), recursive=True),
+ [join(*i) for i in dirs])
+ eq(glob.glob(join('**','zz*F'), recursive=True),
+ [join('aaa', 'zzzF')])
+ eq(glob.glob('**zz*F', recursive=True), [])
+ expect = [join('a', 'bcd', 'EF')]
+ if can_symlink():
+ expect += [join('sym3', 'EF')]
+ eq(glob.glob(join('**', 'EF'), recursive=True), expect)
+ finally:
+ os.chdir(predir)
+
+
+@skip_unless_symlink
+class SymlinkLoopGlobTests(unittest.TestCase):
+
+ def test_selflink(self):
+ tempdir = TESTFN + "_dir"
+ os.makedirs(tempdir)
+ self.addCleanup(shutil.rmtree, tempdir)
+ with change_cwd(tempdir):
+ os.makedirs('dir')
+ create_empty_file(os.path.join('dir', 'file'))
+ os.symlink(os.curdir, os.path.join('dir', 'link'))
+
+ results = glob.glob('**', recursive=True)
+ self.assertEqual(len(results), len(set(results)))
+ results = set(results)
+ depth = 0
+ while results:
+ path = os.path.join(*(['dir'] + ['link'] * depth))
+ self.assertIn(path, results)
+ results.remove(path)
+ if not results:
+ break
+ path = os.path.join(path, 'file')
+ self.assertIn(path, results)
+ results.remove(path)
+ depth += 1
+
+ results = glob.glob(os.path.join('**', 'file'), recursive=True)
+ self.assertEqual(len(results), len(set(results)))
+ results = set(results)
+ depth = 0
+ while results:
+ path = os.path.join(*(['dir'] + ['link'] * depth + ['file']))
+ self.assertIn(path, results)
+ results.remove(path)
+ depth += 1
+
+ results = glob.glob(os.path.join('**', ''), recursive=True)
+ self.assertEqual(len(results), len(set(results)))
+ results = set(results)
+ depth = 0
+ while results:
+ path = os.path.join(*(['dir'] + ['link'] * depth + ['']))
+ self.assertIn(path, results)
+ results.remove(path)
+ depth += 1
if __name__ == "__main__":
- test_main()
+ unittest.main()
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index 70d85b1..7069fb9 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -1016,6 +1016,20 @@ class GrammarTests(unittest.TestCase):
self.assertFalse((False is 2) is 3)
self.assertFalse(False is 2 is 3)
+ def test_matrix_mul(self):
+ # This is not intended to be a comprehensive test, rather just to be few
+ # samples of the @ operator in test_grammar.py.
+ class M:
+ def __matmul__(self, o):
+ return 4
+ def __imatmul__(self, o):
+ self.other = o
+ return self
+ m = M()
+ self.assertEqual(m @ m, 4)
+ m @= 42
+ self.assertEqual(m.other, 42)
+
def test_main():
run_unittest(TokenTests, GrammarTests)
diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py
index b5a2fd8..0dcd8c5 100644
--- a/Lib/test/test_heapq.py
+++ b/Lib/test/test_heapq.py
@@ -6,14 +6,15 @@ import unittest
from test import support
from unittest import TestCase, skipUnless
+from operator import itemgetter
py_heapq = support.import_fresh_module('heapq', blocked=['_heapq'])
c_heapq = support.import_fresh_module('heapq', fresh=['_heapq'])
# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
# _heapq is imported, so check them there
-func_names = ['heapify', 'heappop', 'heappush', 'heappushpop',
- 'heapreplace', '_nlargest', '_nsmallest']
+func_names = ['heapify', 'heappop', 'heappush', 'heappushpop', 'heapreplace',
+ '_heappop_max', '_heapreplace_max', '_heapify_max']
class TestModules(TestCase):
def test_py_functions(self):
@@ -152,11 +153,21 @@ class TestHeap:
def test_merge(self):
inputs = []
- for i in range(random.randrange(5)):
- row = sorted(random.randrange(1000) for j in range(random.randrange(10)))
+ for i in range(random.randrange(25)):
+ row = []
+ for j in range(random.randrange(100)):
+ tup = random.choice('ABC'), random.randrange(-500, 500)
+ row.append(tup)
inputs.append(row)
- self.assertEqual(sorted(chain(*inputs)), list(self.module.merge(*inputs)))
- self.assertEqual(list(self.module.merge()), [])
+
+ for key in [None, itemgetter(0), itemgetter(1), itemgetter(1, 0)]:
+ for reverse in [False, True]:
+ seqs = []
+ for seq in inputs:
+ seqs.append(sorted(seq, key=key, reverse=reverse))
+ self.assertEqual(sorted(chain(*inputs), key=key, reverse=reverse),
+ list(self.module.merge(*seqs, key=key, reverse=reverse)))
+ self.assertEqual(list(self.module.merge()), [])
def test_merge_does_not_suppress_index_error(self):
# Issue 19018: Heapq.merge suppresses IndexError from user generator
diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py
index 2d771a2..de8f3e8 100644
--- a/Lib/test/test_htmlparser.py
+++ b/Lib/test/test_htmlparser.py
@@ -85,7 +85,7 @@ class EventCollectorCharrefs(EventCollector):
class TestCaseBase(unittest.TestCase):
def get_collector(self):
- raise NotImplementedError
+ return EventCollector(convert_charrefs=False)
def _run_check(self, source, expected_events, collector=None):
if collector is None:
@@ -105,21 +105,8 @@ class TestCaseBase(unittest.TestCase):
self._run_check(source, events,
EventCollectorExtra(convert_charrefs=False))
- def _parse_error(self, source):
- def parse(source=source):
- parser = self.get_collector()
- parser.feed(source)
- parser.close()
- with self.assertRaises(html.parser.HTMLParseError):
- with self.assertWarns(DeprecationWarning):
- parse()
-
-class HTMLParserStrictTestCase(TestCaseBase):
-
- def get_collector(self):
- with support.check_warnings(("", DeprecationWarning), quite=False):
- return EventCollector(strict=True, convert_charrefs=False)
+class HTMLParserTestCase(TestCaseBase):
def test_processing_instruction_only(self):
self._run_check("<?processing instruction>", [
@@ -201,9 +188,6 @@ text
("data", "this < text > contains < bare>pointy< brackets"),
])
- def test_illegal_declarations(self):
- self._parse_error('<!spacer type="block" height="25">')
-
def test_starttag_end_boundary(self):
self._run_check("""<a b='<'>""", [("starttag", "a", [("b", "<")])])
self._run_check("""<a b='>'>""", [("starttag", "a", [("b", ">")])])
@@ -238,25 +222,6 @@ text
self._run_check(["<!--abc--", ">"], output)
self._run_check(["<!--abc-->", ""], output)
- def test_starttag_junk_chars(self):
- self._parse_error("</>")
- self._parse_error("</$>")
- self._parse_error("</")
- self._parse_error("</a")
- self._parse_error("<a<a>")
- self._parse_error("</a<a>")
- self._parse_error("<!")
- self._parse_error("<a")
- self._parse_error("<a foo='bar'")
- self._parse_error("<a foo='bar")
- self._parse_error("<a foo='>'")
- self._parse_error("<a foo='>")
- self._parse_error("<a$>")
- self._parse_error("<a$b>")
- self._parse_error("<a$b/>")
- self._parse_error("<a$b >")
- self._parse_error("<a$b />")
-
def test_valid_doctypes(self):
# from http://www.w3.org/QA/2002/04/valid-dtd-list.html
dtds = ['HTML', # HTML5 doctype
@@ -281,9 +246,6 @@ text
self._run_check("<!DOCTYPE %s>" % dtd,
[('decl', 'DOCTYPE ' + dtd)])
- def test_declaration_junk_chars(self):
- self._parse_error("<!DOCTYPE foo $ >")
-
def test_startendtag(self):
self._run_check("<p/>", [
("startendtag", "p", []),
@@ -384,7 +346,8 @@ text
self._run_check(html, expected)
def test_convert_charrefs(self):
- collector = lambda: EventCollectorCharrefs(convert_charrefs=True)
+ # default value for convert_charrefs is now True
+ collector = lambda: EventCollectorCharrefs()
self.assertTrue(collector().convert_charrefs)
charrefs = ['&quot;', '&#34;', '&#x22;', '&quot', '&#34', '&#x22']
# check charrefs in the middle of the text/attributes
@@ -421,23 +384,8 @@ text
self._run_check('no charrefs here', [('data', 'no charrefs here')],
collector=collector())
-
-class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
-
- def get_collector(self):
- return EventCollector(convert_charrefs=False)
-
- def test_deprecation_warnings(self):
- with self.assertWarns(DeprecationWarning):
- EventCollector() # convert_charrefs not passed explicitly
- with self.assertWarns(DeprecationWarning):
- EventCollector(strict=True)
- with self.assertWarns(DeprecationWarning):
- EventCollector(strict=False)
- with self.assertRaises(html.parser.HTMLParseError):
- with self.assertWarns(DeprecationWarning):
- EventCollector().error('test')
-
+ # the remaining tests were for the "tolerant" parser (which is now
+ # the default), and check various kind of broken markup
def test_tolerant_parsing(self):
self._run_check('<html <html>te>>xt&a<<bc</a></html>\n'
'<img src="URL><//img></html</html>', [
@@ -686,11 +634,7 @@ class HTMLParserTolerantTestCase(HTMLParserStrictTestCase):
self._run_check(html, expected)
-class AttributesStrictTestCase(TestCaseBase):
-
- def get_collector(self):
- with support.check_warnings(("", DeprecationWarning), quite=False):
- return EventCollector(strict=True, convert_charrefs=False)
+class AttributesTestCase(TestCaseBase):
def test_attr_syntax(self):
output = [
@@ -747,12 +691,6 @@ class AttributesStrictTestCase(TestCaseBase):
[("starttag", "html", [("foo", "\u20AC&aa&unsupported;")])])
-
-class AttributesTolerantTestCase(AttributesStrictTestCase):
-
- def get_collector(self):
- return EventCollector(convert_charrefs=False)
-
def test_attr_funky_names2(self):
self._run_check(
"<a $><b $=%><c \=/>",
diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py
index e984252..ee30f17 100644
--- a/Lib/test/test_http_cookies.py
+++ b/Lib/test/test_http_cookies.py
@@ -141,13 +141,6 @@ class CookieTests(unittest.TestCase):
self.assertEqual(C['eggs']['httponly'], 'foo')
self.assertEqual(C['eggs']['secure'], 'bar')
- def test_bad_attrs(self):
- # issue 16611: make sure we don't break backward compatibility.
- C = cookies.SimpleCookie()
- C.load('cookie=with; invalid; version; second=cookie;')
- self.assertEqual(C.output(),
- 'Set-Cookie: cookie=with\r\nSet-Cookie: second=cookie')
-
def test_extra_spaces(self):
C = cookies.SimpleCookie()
C.load('eggs = scrambled ; secure ; path = bar ; foo=foo ')
@@ -182,7 +175,10 @@ class CookieTests(unittest.TestCase):
def test_invalid_cookies(self):
# Accepting these could be a security issue
C = cookies.SimpleCookie()
- for s in (']foo=x', '[foo=x', 'blah]foo=x', 'blah[foo=x'):
+ for s in (']foo=x', '[foo=x', 'blah]foo=x', 'blah[foo=x',
+ 'Set-Cookie: foo=bar', 'Set-Cookie: foo',
+ 'foo=bar; baz', 'baz; foo=bar',
+ 'secure;foo=bar', 'Version=1;foo=bar'):
C.load(s)
self.assertEqual(dict(C), {})
self.assertEqual(C.output(), '')
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index 3fc3466..40ef250 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -18,6 +18,26 @@ CERT_fakehostname = os.path.join(here, 'keycert2.pem')
# Self-signed cert file for self-signed.pythontest.net
CERT_selfsigned_pythontestdotnet = os.path.join(here, 'selfsigned_pythontestdotnet.pem')
+# constants for testing chunked encoding
+chunked_start = (
+ 'HTTP/1.1 200 OK\r\n'
+ 'Transfer-Encoding: chunked\r\n\r\n'
+ 'a\r\n'
+ 'hello worl\r\n'
+ '3\r\n'
+ 'd! \r\n'
+ '8\r\n'
+ 'and now \r\n'
+ '22\r\n'
+ 'for something completely different\r\n'
+)
+chunked_expected = b'hello world! and now for something completely different'
+chunk_extension = ";foo=bar"
+last_chunk = "0\r\n"
+last_chunk_extended = "0" + chunk_extension + "\r\n"
+trailers = "X-Dummy: foo\r\nX-Dumm2: bar\r\n"
+chunked_end = "\r\n"
+
HOST = support.HOST
class FakeSocket:
@@ -50,6 +70,9 @@ class FakeSocket:
def close(self):
pass
+ def setsockopt(self, level, optname, value):
+ pass
+
class EPipeSocket(FakeSocket):
def __init__(self, text, pipe_trigger):
@@ -452,20 +475,8 @@ class BasicTest(TestCase):
conn.request('POST', 'test', conn)
def test_chunked(self):
- chunked_start = (
- 'HTTP/1.1 200 OK\r\n'
- 'Transfer-Encoding: chunked\r\n\r\n'
- 'a\r\n'
- 'hello worl\r\n'
- '3\r\n'
- 'd! \r\n'
- '8\r\n'
- 'and now \r\n'
- '22\r\n'
- 'for something completely different\r\n'
- )
- expected = b'hello world! and now for something completely different'
- sock = FakeSocket(chunked_start + '0\r\n')
+ expected = chunked_expected
+ sock = FakeSocket(chunked_start + last_chunk + chunked_end)
resp = client.HTTPResponse(sock, method="GET")
resp.begin()
self.assertEqual(resp.read(), expected)
@@ -473,7 +484,7 @@ class BasicTest(TestCase):
# Various read sizes
for n in range(1, 12):
- sock = FakeSocket(chunked_start + '0\r\n')
+ sock = FakeSocket(chunked_start + last_chunk + chunked_end)
resp = client.HTTPResponse(sock, method="GET")
resp.begin()
self.assertEqual(resp.read(n) + resp.read(n) + resp.read(), expected)
@@ -496,23 +507,12 @@ class BasicTest(TestCase):
resp.close()
def test_readinto_chunked(self):
- chunked_start = (
- 'HTTP/1.1 200 OK\r\n'
- 'Transfer-Encoding: chunked\r\n\r\n'
- 'a\r\n'
- 'hello worl\r\n'
- '3\r\n'
- 'd! \r\n'
- '8\r\n'
- 'and now \r\n'
- '22\r\n'
- 'for something completely different\r\n'
- )
- expected = b'hello world! and now for something completely different'
+
+ expected = chunked_expected
nexpected = len(expected)
b = bytearray(128)
- sock = FakeSocket(chunked_start + '0\r\n')
+ sock = FakeSocket(chunked_start + last_chunk + chunked_end)
resp = client.HTTPResponse(sock, method="GET")
resp.begin()
n = resp.readinto(b)
@@ -522,7 +522,7 @@ class BasicTest(TestCase):
# Various read sizes
for n in range(1, 12):
- sock = FakeSocket(chunked_start + '0\r\n')
+ sock = FakeSocket(chunked_start + last_chunk + chunked_end)
resp = client.HTTPResponse(sock, method="GET")
resp.begin()
m = memoryview(b)
@@ -558,7 +558,7 @@ class BasicTest(TestCase):
'1\r\n'
'd\r\n'
)
- sock = FakeSocket(chunked_start + '0\r\n')
+ sock = FakeSocket(chunked_start + last_chunk + chunked_end)
resp = client.HTTPResponse(sock, method="HEAD")
resp.begin()
self.assertEqual(resp.read(), b'')
@@ -578,7 +578,7 @@ class BasicTest(TestCase):
'1\r\n'
'd\r\n'
)
- sock = FakeSocket(chunked_start + '0\r\n')
+ sock = FakeSocket(chunked_start + last_chunk + chunked_end)
resp = client.HTTPResponse(sock, method="HEAD")
resp.begin()
b = bytearray(5)
@@ -653,6 +653,7 @@ class BasicTest(TestCase):
+ '0' * 65536 + 'a\r\n'
'hello world\r\n'
'0\r\n'
+ '\r\n'
)
resp = client.HTTPResponse(FakeSocket(body))
resp.begin()
@@ -670,28 +671,6 @@ class BasicTest(TestCase):
resp.close()
self.assertTrue(resp.closed)
- def test_delayed_ack_opt(self):
- # Test that Nagle/delayed_ack optimistaion works correctly.
-
- # For small payloads, it should coalesce the body with
- # headers, resulting in a single sendall() call
- conn = client.HTTPConnection('example.com')
- sock = FakeSocket(None)
- conn.sock = sock
- body = b'x' * (conn.mss - 1)
- conn.request('POST', '/', body)
- self.assertEqual(sock.sendall_calls, 1)
-
- # For large payloads, it should send the headers and
- # then the body, resulting in more than one sendall()
- # call
- conn = client.HTTPConnection('example.com')
- sock = FakeSocket(None)
- conn.sock = sock
- body = b'x' * conn.mss
- conn.request('POST', '/', body)
- self.assertGreater(sock.sendall_calls, 1)
-
def test_error_leak(self):
# Test that the socket is not leaked if getresponse() fails
conn = client.HTTPConnection('example.com')
@@ -708,6 +687,239 @@ class BasicTest(TestCase):
self.assertTrue(response.closed)
self.assertTrue(conn.sock.file_closed)
+ def test_chunked_extension(self):
+ extra = '3;foo=bar\r\n' + 'abc\r\n'
+ expected = chunked_expected + b'abc'
+
+ sock = FakeSocket(chunked_start + extra + last_chunk_extended + chunked_end)
+ resp = client.HTTPResponse(sock, method="GET")
+ resp.begin()
+ self.assertEqual(resp.read(), expected)
+ resp.close()
+
+ def test_chunked_missing_end(self):
+ """some servers may serve up a short chunked encoding stream"""
+ expected = chunked_expected
+ sock = FakeSocket(chunked_start + last_chunk) #no terminating crlf
+ resp = client.HTTPResponse(sock, method="GET")
+ resp.begin()
+ self.assertEqual(resp.read(), expected)
+ resp.close()
+
+ def test_chunked_trailers(self):
+ """See that trailers are read and ignored"""
+ expected = chunked_expected
+ sock = FakeSocket(chunked_start + last_chunk + trailers + chunked_end)
+ resp = client.HTTPResponse(sock, method="GET")
+ resp.begin()
+ self.assertEqual(resp.read(), expected)
+ # we should have reached the end of the file
+ self.assertEqual(sock.file.read(100), b"") #we read to the end
+ resp.close()
+
+ def test_chunked_sync(self):
+ """Check that we don't read past the end of the chunked-encoding stream"""
+ expected = chunked_expected
+ extradata = "extradata"
+ sock = FakeSocket(chunked_start + last_chunk + trailers + chunked_end + extradata)
+ resp = client.HTTPResponse(sock, method="GET")
+ resp.begin()
+ self.assertEqual(resp.read(), expected)
+ # the file should now have our extradata ready to be read
+ self.assertEqual(sock.file.read(100), extradata.encode("ascii")) #we read to the end
+ resp.close()
+
+ def test_content_length_sync(self):
+ """Check that we don't read past the end of the Content-Length stream"""
+ extradata = "extradata"
+ expected = b"Hello123\r\n"
+ sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello123\r\n' + extradata)
+ resp = client.HTTPResponse(sock, method="GET")
+ resp.begin()
+ self.assertEqual(resp.read(), expected)
+ # the file should now have our extradata ready to be read
+ self.assertEqual(sock.file.read(100), extradata.encode("ascii")) #we read to the end
+ resp.close()
+
+class ExtendedReadTest(TestCase):
+ """
+ Test peek(), read1(), readline()
+ """
+ lines = (
+ 'HTTP/1.1 200 OK\r\n'
+ '\r\n'
+ 'hello world!\n'
+ 'and now \n'
+ 'for something completely different\n'
+ 'foo'
+ )
+ lines_expected = lines[lines.find('hello'):].encode("ascii")
+ lines_chunked = (
+ 'HTTP/1.1 200 OK\r\n'
+ 'Transfer-Encoding: chunked\r\n\r\n'
+ 'a\r\n'
+ 'hello worl\r\n'
+ '3\r\n'
+ 'd!\n\r\n'
+ '9\r\n'
+ 'and now \n\r\n'
+ '23\r\n'
+ 'for something completely different\n\r\n'
+ '3\r\n'
+ 'foo\r\n'
+ '0\r\n' # terminating chunk
+ '\r\n' # end of trailers
+ )
+
+ def setUp(self):
+ sock = FakeSocket(self.lines)
+ resp = client.HTTPResponse(sock, method="GET")
+ resp.begin()
+ resp.fp = io.BufferedReader(resp.fp)
+ self.resp = resp
+
+
+
+ def test_peek(self):
+ resp = self.resp
+ # patch up the buffered peek so that it returns not too much stuff
+ oldpeek = resp.fp.peek
+ def mypeek(n=-1):
+ p = oldpeek(n)
+ if n >= 0:
+ return p[:n]
+ return p[:10]
+ resp.fp.peek = mypeek
+
+ all = []
+ while True:
+ # try a short peek
+ p = resp.peek(3)
+ if p:
+ self.assertGreater(len(p), 0)
+ # then unbounded peek
+ p2 = resp.peek()
+ self.assertGreaterEqual(len(p2), len(p))
+ self.assertTrue(p2.startswith(p))
+ next = resp.read(len(p2))
+ self.assertEqual(next, p2)
+ else:
+ next = resp.read()
+ self.assertFalse(next)
+ all.append(next)
+ if not next:
+ break
+ self.assertEqual(b"".join(all), self.lines_expected)
+
+ def test_readline(self):
+ resp = self.resp
+ self._verify_readline(self.resp.readline, self.lines_expected)
+
+ def _verify_readline(self, readline, expected):
+ all = []
+ while True:
+ # short readlines
+ line = readline(5)
+ if line and line != b"foo":
+ if len(line) < 5:
+ self.assertTrue(line.endswith(b"\n"))
+ all.append(line)
+ if not line:
+ break
+ self.assertEqual(b"".join(all), expected)
+
+ def test_read1(self):
+ resp = self.resp
+ def r():
+ res = resp.read1(4)
+ self.assertLessEqual(len(res), 4)
+ return res
+ readliner = Readliner(r)
+ self._verify_readline(readliner.readline, self.lines_expected)
+
+ def test_read1_unbounded(self):
+ resp = self.resp
+ all = []
+ while True:
+ data = resp.read1()
+ if not data:
+ break
+ all.append(data)
+ self.assertEqual(b"".join(all), self.lines_expected)
+
+ def test_read1_bounded(self):
+ resp = self.resp
+ all = []
+ while True:
+ data = resp.read1(10)
+ if not data:
+ break
+ self.assertLessEqual(len(data), 10)
+ all.append(data)
+ self.assertEqual(b"".join(all), self.lines_expected)
+
+ def test_read1_0(self):
+ self.assertEqual(self.resp.read1(0), b"")
+
+ def test_peek_0(self):
+ p = self.resp.peek(0)
+ self.assertLessEqual(0, len(p))
+
+class ExtendedReadTestChunked(ExtendedReadTest):
+ """
+ Test peek(), read1(), readline() in chunked mode
+ """
+ lines = (
+ 'HTTP/1.1 200 OK\r\n'
+ 'Transfer-Encoding: chunked\r\n\r\n'
+ 'a\r\n'
+ 'hello worl\r\n'
+ '3\r\n'
+ 'd!\n\r\n'
+ '9\r\n'
+ 'and now \n\r\n'
+ '23\r\n'
+ 'for something completely different\n\r\n'
+ '3\r\n'
+ 'foo\r\n'
+ '0\r\n' # terminating chunk
+ '\r\n' # end of trailers
+ )
+
+
+class Readliner:
+ """
+ a simple readline class that uses an arbitrary read function and buffering
+ """
+ def __init__(self, readfunc):
+ self.readfunc = readfunc
+ self.remainder = b""
+
+ def readline(self, limit):
+ data = []
+ datalen = 0
+ read = self.remainder
+ try:
+ while True:
+ idx = read.find(b'\n')
+ if idx != -1:
+ break
+ if datalen + len(read) >= limit:
+ idx = limit - datalen - 1
+ # read more data
+ data.append(read)
+ read = self.readfunc()
+ if not read:
+ idx = 0 #eof condition
+ break
+ idx += 1
+ data.append(read[:idx])
+ self.remainder = read[idx:]
+ return b"".join(data)
+ except:
+ self.remainder = b"".join(data)
+ raise
+
class OfflineTest(TestCase):
def test_responses(self):
self.assertEqual(client.responses[client.NOT_FOUND], "Not Found")
@@ -718,7 +930,7 @@ class SourceAddressTest(TestCase):
self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.port = support.bind_port(self.serv)
self.source_port = support.find_unused_port()
- self.serv.listen(5)
+ self.serv.listen()
self.conn = None
def tearDown(self):
@@ -750,7 +962,7 @@ class TimeoutTest(TestCase):
def setUp(self):
self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
TimeoutTest.PORT = support.bind_port(self.serv)
- self.serv.listen(5)
+ self.serv.listen()
def tearDown(self):
self.serv.close()
@@ -1057,17 +1269,18 @@ class TunnelTests(TestCase):
'HTTP/1.1 200 OK\r\n' # Reply to HEAD
'Content-Length: 42\r\n\r\n'
)
-
- def create_connection(address, timeout=None, source_address=None):
- return FakeSocket(response_text, host=address[0], port=address[1])
-
self.host = 'proxy.com'
self.conn = client.HTTPConnection(self.host)
- self.conn._create_connection = create_connection
+ self.conn._create_connection = self._create_connection(response_text)
def tearDown(self):
self.conn.close()
+ def _create_connection(self, response_text):
+ def create_connection(address, timeout=None, source_address=None):
+ return FakeSocket(response_text, host=address[0], port=address[1])
+ return create_connection
+
def test_set_tunnel_host_port_headers(self):
tunnel_host = 'destination.com'
tunnel_port = 8888
@@ -1108,13 +1321,26 @@ class TunnelTests(TestCase):
self.assertIn(b'CONNECT destination.com', self.conn.sock.data)
self.assertIn(b'Host: destination.com', self.conn.sock.data)
+ def test_tunnel_debuglog(self):
+ expected_header = 'X-Dummy: 1'
+ response_text = 'HTTP/1.0 200 OK\r\n{}\r\n\r\n'.format(expected_header)
+
+ self.conn.set_debuglevel(1)
+ self.conn._create_connection = self._create_connection(response_text)
+ self.conn.set_tunnel('destination.com')
+
+ with support.captured_stdout() as output:
+ self.conn.request('PUT', '/', '')
+ lines = output.getvalue().splitlines()
+ self.assertIn('header: {}'.format(expected_header), lines)
@support.reap_threads
def test_main(verbose=None):
support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
HTTPSTest, RequestBodyTest, SourceAddressTest,
- HTTPResponseTest, TunnelTests)
+ HTTPResponseTest, ExtendedReadTest,
+ ExtendedReadTestChunked, TunnelTests)
if __name__ == '__main__':
test_main()
diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py
index b34e652..5485a2a 100644
--- a/Lib/test/test_imaplib.py
+++ b/Lib/test/test_imaplib.py
@@ -11,7 +11,8 @@ import socketserver
import time
import calendar
-from test.support import reap_threads, verbose, transient_internet, run_with_tz, run_with_locale
+from test.support import (reap_threads, verbose, transient_internet,
+ run_with_tz, run_with_locale)
import unittest
from datetime import datetime, timezone, timedelta
try:
@@ -19,8 +20,8 @@ try:
except ImportError:
ssl = None
-CERTFILE = None
-CAFILE = None
+CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert3.pem")
+CAFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "pycacert.pem")
class TestImaplib(unittest.TestCase):
@@ -41,17 +42,15 @@ class TestImaplib(unittest.TestCase):
def test_Internaldate2tuple_issue10941(self):
self.assertNotEqual(imaplib.Internaldate2tuple(
b'25 (INTERNALDATE "02-Apr-2000 02:30:00 +0000")'),
- imaplib.Internaldate2tuple(
- b'25 (INTERNALDATE "02-Apr-2000 03:30:00 +0000")'))
-
-
+ imaplib.Internaldate2tuple(
+ b'25 (INTERNALDATE "02-Apr-2000 03:30:00 +0000")'))
def timevalues(self):
return [2000000000, 2000000000.0, time.localtime(2000000000),
(2033, 5, 18, 5, 33, 20, -1, -1, -1),
(2033, 5, 18, 5, 33, 20, -1, -1, 1),
datetime.fromtimestamp(2000000000,
- timezone(timedelta(0, 2*60*60))),
+ timezone(timedelta(0, 2 * 60 * 60))),
'"18-May-2033 05:33:20 +0200"']
@run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
@@ -72,7 +71,6 @@ class TestImaplib(unittest.TestCase):
if ssl:
-
class SecureTCPServer(socketserver.TCPServer):
def get_request(self):
@@ -93,13 +91,17 @@ else:
class SimpleIMAPHandler(socketserver.StreamRequestHandler):
-
timeout = 1
continuation = None
capabilities = ''
+ def setup(self):
+ super().setup()
+ self.server.logged = None
+
def _send(self, message):
- if verbose: print("SENT: %r" % message.strip())
+ if verbose:
+ print("SENT: %r" % message.strip())
self.wfile.write(message)
def _send_line(self, message):
@@ -132,7 +134,8 @@ class SimpleIMAPHandler(socketserver.StreamRequestHandler):
if line.endswith(b'\r\n'):
break
- if verbose: print('GOT: %r' % line.strip())
+ if verbose:
+ print('GOT: %r' % line.strip())
if self.continuation:
try:
self.continuation.send(line)
@@ -144,8 +147,8 @@ class SimpleIMAPHandler(socketserver.StreamRequestHandler):
cmd = splitline[1]
args = splitline[2:]
- if hasattr(self, 'cmd_'+cmd):
- continuation = getattr(self, 'cmd_'+cmd)(tag, args)
+ if hasattr(self, 'cmd_' + cmd):
+ continuation = getattr(self, 'cmd_' + cmd)(tag, args)
if continuation:
self.continuation = continuation
next(continuation)
@@ -153,16 +156,25 @@ class SimpleIMAPHandler(socketserver.StreamRequestHandler):
self._send_tagged(tag, 'BAD', cmd + ' unknown')
def cmd_CAPABILITY(self, tag, args):
- caps = 'IMAP4rev1 ' + self.capabilities if self.capabilities else 'IMAP4rev1'
+ caps = ('IMAP4rev1 ' + self.capabilities
+ if self.capabilities
+ else 'IMAP4rev1')
self._send_textline('* CAPABILITY ' + caps)
self._send_tagged(tag, 'OK', 'CAPABILITY completed')
def cmd_LOGOUT(self, tag, args):
+ self.server.logged = None
self._send_textline('* BYE IMAP4ref1 Server logging out')
self._send_tagged(tag, 'OK', 'LOGOUT completed')
+ def cmd_LOGIN(self, tag, args):
+ self.server.logged = args[0]
+ self._send_tagged(tag, 'OK', 'LOGIN completed')
-class BaseThreadedNetworkedTests(unittest.TestCase):
+
+class ThreadedNetworkedTests(unittest.TestCase):
+ server_class = socketserver.TCPServer
+ imap_class = imaplib.IMAP4
def make_server(self, addr, hdlr):
@@ -172,7 +184,8 @@ class BaseThreadedNetworkedTests(unittest.TestCase):
self.server_close()
raise
- if verbose: print("creating server")
+ if verbose:
+ print("creating server")
server = MyServer(addr, hdlr)
self.assertEqual(server.server_address, server.socket.getsockname())
@@ -188,18 +201,21 @@ class BaseThreadedNetworkedTests(unittest.TestCase):
# Short poll interval to make the test finish quickly.
# Time between requests is short enough that we won't wake
# up spuriously too many times.
- kwargs={'poll_interval':0.01})
+ kwargs={'poll_interval': 0.01})
t.daemon = True # In case this function raises.
t.start()
- if verbose: print("server running")
+ if verbose:
+ print("server running")
return server, t
def reap_server(self, server, thread):
- if verbose: print("waiting for server")
+ if verbose:
+ print("waiting for server")
server.shutdown()
server.server_close()
thread.join()
- if verbose: print("done")
+ if verbose:
+ print("done")
@contextmanager
def reaped_server(self, hdlr):
@@ -256,7 +272,7 @@ class BaseThreadedNetworkedTests(unittest.TestCase):
def cmd_AUTHENTICATE(self, tag, args):
self._send_tagged(tag, 'NO', 'unrecognized authentication '
- 'type {}'.format(args[0]))
+ 'type {}'.format(args[0]))
with self.reaped_pair(MyServer) as (server, client):
with self.assertRaises(imaplib.IMAP4.error):
@@ -290,13 +306,13 @@ class BaseThreadedNetworkedTests(unittest.TestCase):
code, data = client.authenticate('MYAUTH', lambda x: b'fake')
self.assertEqual(code, 'OK')
self.assertEqual(server.response,
- b'ZmFrZQ==\r\n') #b64 encoded 'fake'
+ b'ZmFrZQ==\r\n') # b64 encoded 'fake'
with self.reaped_pair(MyServer) as (server, client):
code, data = client.authenticate('MYAUTH', lambda x: 'fake')
self.assertEqual(code, 'OK')
self.assertEqual(server.response,
- b'ZmFrZQ==\r\n') #b64 encoded 'fake'
+ b'ZmFrZQ==\r\n') # b64 encoded 'fake'
@reap_threads
def test_login_cram_md5(self):
@@ -307,9 +323,10 @@ class BaseThreadedNetworkedTests(unittest.TestCase):
def cmd_AUTHENTICATE(self, tag, args):
self._send_textline('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm'
- 'VzdG9uLm1jaS5uZXQ=')
+ 'VzdG9uLm1jaS5uZXQ=')
r = yield
- if r == b'dGltIGYxY2E2YmU0NjRiOWVmYTFjY2E2ZmZkNmNmMmQ5ZjMy\r\n':
+ if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT'
+ b'FjY2E2ZmZkNmNmMmQ5ZjMy\r\n'):
self._send_tagged(tag, 'OK', 'CRAM-MD5 successful')
else:
self._send_tagged(tag, 'NO', 'No access')
@@ -324,27 +341,45 @@ class BaseThreadedNetworkedTests(unittest.TestCase):
ret, data = client.login_cram_md5("tim", b"tanstaaftanstaaf")
self.assertEqual(ret, "OK")
-
def test_linetoolong(self):
class TooLongHandler(SimpleIMAPHandler):
def handle(self):
# Send a very long response line
- self.wfile.write(b'* OK ' + imaplib._MAXLINE*b'x' + b'\r\n')
+ self.wfile.write(b'* OK ' + imaplib._MAXLINE * b'x' + b'\r\n')
with self.reaped_server(TooLongHandler) as server:
self.assertRaises(imaplib.IMAP4.error,
self.imap_class, *server.server_address)
+ @reap_threads
+ def test_simple_with_statement(self):
+ # simplest call
+ with self.reaped_server(SimpleIMAPHandler) as server:
+ with self.imap_class(*server.server_address):
+ pass
-class ThreadedNetworkedTests(BaseThreadedNetworkedTests):
+ @reap_threads
+ def test_with_statement(self):
+ with self.reaped_server(SimpleIMAPHandler) as server:
+ with self.imap_class(*server.server_address) as imap:
+ imap.login('user', 'pass')
+ self.assertEqual(server.logged, 'user')
+ self.assertIsNone(server.logged)
- server_class = socketserver.TCPServer
- imap_class = imaplib.IMAP4
+ @reap_threads
+ def test_with_statement_logout(self):
+ # what happens if already logout in the block?
+ with self.reaped_server(SimpleIMAPHandler) as server:
+ with self.imap_class(*server.server_address) as imap:
+ imap.login('user', 'pass')
+ self.assertEqual(server.logged, 'user')
+ imap.logout()
+ self.assertIsNone(server.logged)
+ self.assertIsNone(server.logged)
@unittest.skipUnless(ssl, "SSL not available")
-class ThreadedNetworkedTestsSSL(BaseThreadedNetworkedTests):
-
+class ThreadedNetworkedTestsSSL(ThreadedNetworkedTests):
server_class = SecureTCPServer
imap_class = IMAP4_SSL
@@ -355,8 +390,9 @@ class ThreadedNetworkedTestsSSL(BaseThreadedNetworkedTests):
ssl_context.check_hostname = True
ssl_context.load_verify_locations(CAFILE)
- with self.assertRaisesRegex(ssl.CertificateError,
- "hostname '127.0.0.1' doesn't match 'localhost'"):
+ with self.assertRaisesRegex(
+ ssl.CertificateError,
+ "hostname '127.0.0.1' doesn't match 'localhost'"):
with self.reaped_server(SimpleIMAPHandler) as server:
client = self.imap_class(*server.server_address,
ssl_context=ssl_context)
@@ -368,6 +404,8 @@ class ThreadedNetworkedTestsSSL(BaseThreadedNetworkedTests):
client.shutdown()
+@unittest.skipUnless(
+ support.is_resource_enabled('network'), 'network resource disabled')
class RemoteIMAPTest(unittest.TestCase):
host = 'cyrus.andrew.cmu.edu'
port = 143
@@ -401,6 +439,8 @@ class RemoteIMAPTest(unittest.TestCase):
@unittest.skipUnless(ssl, "SSL not available")
+@unittest.skipUnless(
+ support.is_resource_enabled('network'), 'network resource disabled')
class RemoteIMAP_STARTTLSTest(RemoteIMAPTest):
def setUp(self):
@@ -454,7 +494,8 @@ class RemoteIMAP_SSLTest(RemoteIMAPTest):
def test_logincapa_with_client_ssl_context(self):
with transient_internet(self.host):
- _server = self.imap_class(self.host, self.port, ssl_context=self.create_ssl_context())
+ _server = self.imap_class(
+ self.host, self.port, ssl_context=self.create_ssl_context())
self.check_logincapa(_server)
def test_logout(self):
@@ -465,35 +506,15 @@ class RemoteIMAP_SSLTest(RemoteIMAPTest):
def test_ssl_context_certfile_exclusive(self):
with transient_internet(self.host):
- self.assertRaises(ValueError, self.imap_class, self.host, self.port,
- certfile=CERTFILE, ssl_context=self.create_ssl_context())
+ self.assertRaises(
+ ValueError, self.imap_class, self.host, self.port,
+ certfile=CERTFILE, ssl_context=self.create_ssl_context())
def test_ssl_context_keyfile_exclusive(self):
with transient_internet(self.host):
- self.assertRaises(ValueError, self.imap_class, self.host, self.port,
- keyfile=CERTFILE, ssl_context=self.create_ssl_context())
-
-
-def load_tests(*args):
- tests = [TestImaplib]
-
- if support.is_resource_enabled('network'):
- if ssl:
- global CERTFILE, CAFILE
- CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir,
- "keycert3.pem")
- if not os.path.exists(CERTFILE):
- raise support.TestFailed("Can't read certificate files!")
- CAFILE = os.path.join(os.path.dirname(__file__) or os.curdir,
- "pycacert.pem")
- if not os.path.exists(CAFILE):
- raise support.TestFailed("Can't read CA file!")
- tests.extend([
- ThreadedNetworkedTests, ThreadedNetworkedTestsSSL,
- RemoteIMAPTest, RemoteIMAP_SSLTest, RemoteIMAP_STARTTLSTest,
- ])
-
- return unittest.TestSuite([unittest.makeSuite(test) for test in tests])
+ self.assertRaises(
+ ValueError, self.imap_class, self.host, self.port,
+ keyfile=CERTFILE, ssl_context=self.create_ssl_context())
if __name__ == "__main__":
diff --git a/Lib/test/test_imghdr.py b/Lib/test/test_imghdr.py
index 0ad4343..b54daf8 100644
--- a/Lib/test/test_imghdr.py
+++ b/Lib/test/test_imghdr.py
@@ -16,7 +16,9 @@ TEST_FILES = (
('python.ras', 'rast'),
('python.sgi', 'rgb'),
('python.tiff', 'tiff'),
- ('python.xbm', 'xbm')
+ ('python.xbm', 'xbm'),
+ ('python.webp', 'webp'),
+ ('python.exr', 'exr'),
)
class UnseekableIO(io.FileIO):
diff --git a/Lib/test/test_import.py b/Lib/test/test_import/__init__.py
index b4842c5..fd21fa2 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import/__init__.py
@@ -568,7 +568,7 @@ class RelativeImportTests(unittest.TestCase):
def test_relimport_star(self):
# This will import * from .test_import.
- from . import relimport
+ from .. import relimport
self.assertTrue(hasattr(relimport, "RelativeImportTests"))
def test_issue3221(self):
@@ -1068,6 +1068,46 @@ class ImportTracebackTests(unittest.TestCase):
__isolated=False)
+class CircularImportTests(unittest.TestCase):
+
+ """See the docstrings of the modules being imported for the purpose of the
+ test."""
+
+ def tearDown(self):
+ """Make sure no modules pre-exist in sys.modules which are being used to
+ test."""
+ for key in list(sys.modules.keys()):
+ if key.startswith('test.test_import.data.circular_imports'):
+ del sys.modules[key]
+
+ def test_direct(self):
+ try:
+ import test.test_import.data.circular_imports.basic
+ except ImportError:
+ self.fail('circular import through relative imports failed')
+
+ def test_indirect(self):
+ try:
+ import test.test_import.data.circular_imports.indirect
+ except ImportError:
+ self.fail('relative import in module contributing to circular '
+ 'import failed')
+
+ def test_subpackage(self):
+ try:
+ import test.test_import.data.circular_imports.subpackage
+ except ImportError:
+ self.fail('circular import involving a subpackage failed')
+
+ def test_rebinding(self):
+ try:
+ import test.test_import.data.circular_imports.rebinding as rebinding
+ except ImportError:
+ self.fail('circular import with rebinding of module attribute failed')
+ from test.test_import.data.circular_imports.subpkg import util
+ self.assertIs(util.util, rebinding.util)
+
+
if __name__ == '__main__':
# Test needs to be a package, so we can do relative imports.
unittest.main()
diff --git a/Lib/test/test_import/__main__.py b/Lib/test/test_import/__main__.py
new file mode 100644
index 0000000..24f02a1
--- /dev/null
+++ b/Lib/test/test_import/__main__.py
@@ -0,0 +1,3 @@
+import unittest
+
+unittest.main('test.test_import')
diff --git a/Lib/test/test_import/data/circular_imports/basic.py b/Lib/test/test_import/data/circular_imports/basic.py
new file mode 100644
index 0000000..3e41e39
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/basic.py
@@ -0,0 +1,2 @@
+"""Circular imports through direct, relative imports."""
+from . import basic2
diff --git a/Lib/test/test_import/data/circular_imports/basic2.py b/Lib/test/test_import/data/circular_imports/basic2.py
new file mode 100644
index 0000000..00bd2f2
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/basic2.py
@@ -0,0 +1 @@
+from . import basic
diff --git a/Lib/test/test_import/data/circular_imports/indirect.py b/Lib/test/test_import/data/circular_imports/indirect.py
new file mode 100644
index 0000000..6925788
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/indirect.py
@@ -0,0 +1 @@
+from . import basic, basic2
diff --git a/Lib/test/test_import/data/circular_imports/rebinding.py b/Lib/test/test_import/data/circular_imports/rebinding.py
new file mode 100644
index 0000000..2b77375
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/rebinding.py
@@ -0,0 +1,3 @@
+"""Test the binding of names when a circular import shares the same name as an
+attribute."""
+from .rebinding2 import util
diff --git a/Lib/test/test_import/data/circular_imports/rebinding2.py b/Lib/test/test_import/data/circular_imports/rebinding2.py
new file mode 100644
index 0000000..57a9e69
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/rebinding2.py
@@ -0,0 +1,3 @@
+from .subpkg import util
+from . import rebinding
+util = util.util
diff --git a/Lib/test/test_import/data/circular_imports/subpackage.py b/Lib/test/test_import/data/circular_imports/subpackage.py
new file mode 100644
index 0000000..7b412f7
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/subpackage.py
@@ -0,0 +1,2 @@
+"""Circular import involving a sub-package."""
+from .subpkg import subpackage2
diff --git a/Lib/test/test_import/data/circular_imports/subpkg/subpackage2.py b/Lib/test/test_import/data/circular_imports/subpkg/subpackage2.py
new file mode 100644
index 0000000..17b893a
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/subpkg/subpackage2.py
@@ -0,0 +1,2 @@
+#from .util import util
+from .. import subpackage
diff --git a/Lib/test/test_import/data/circular_imports/subpkg/util.py b/Lib/test/test_import/data/circular_imports/subpkg/util.py
new file mode 100644
index 0000000..343bd84
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/subpkg/util.py
@@ -0,0 +1,2 @@
+def util():
+ pass
diff --git a/Lib/test/test_import/data/circular_imports/util.py b/Lib/test/test_import/data/circular_imports/util.py
new file mode 100644
index 0000000..343bd84
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/util.py
@@ -0,0 +1,2 @@
+def util():
+ pass
diff --git a/Lib/test/test_importlib/builtin/test_finder.py b/Lib/test/test_importlib/builtin/test_finder.py
index 934562f..a2e6e1e 100644
--- a/Lib/test/test_importlib/builtin/test_finder.py
+++ b/Lib/test/test_importlib/builtin/test_finder.py
@@ -1,21 +1,21 @@
from .. import abc
from .. import util
-from . import util as builtin_util
-frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
+machinery = util.import_importlib('importlib.machinery')
import sys
import unittest
+@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
class FindSpecTests(abc.FinderTests):
"""Test find_spec() for built-in modules."""
def test_module(self):
# Common case.
- with util.uncache(builtin_util.NAME):
- found = self.machinery.BuiltinImporter.find_spec(builtin_util.NAME)
+ with util.uncache(util.BUILTINS.good_name):
+ found = self.machinery.BuiltinImporter.find_spec(util.BUILTINS.good_name)
self.assertTrue(found)
self.assertEqual(found.origin, 'built-in')
@@ -39,23 +39,26 @@ class FindSpecTests(abc.FinderTests):
def test_ignore_path(self):
# The value for 'path' should always trigger a failed import.
- with util.uncache(builtin_util.NAME):
- spec = self.machinery.BuiltinImporter.find_spec(builtin_util.NAME,
+ with util.uncache(util.BUILTINS.good_name):
+ spec = self.machinery.BuiltinImporter.find_spec(util.BUILTINS.good_name,
['pkg'])
self.assertIsNone(spec)
-Frozen_FindSpecTests, Source_FindSpecTests = util.test_both(FindSpecTests,
- machinery=[frozen_machinery, source_machinery])
+(Frozen_FindSpecTests,
+ Source_FindSpecTests
+ ) = util.test_both(FindSpecTests, machinery=machinery)
+
+@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
class FinderTests(abc.FinderTests):
"""Test find_module() for built-in modules."""
def test_module(self):
# Common case.
- with util.uncache(builtin_util.NAME):
- found = self.machinery.BuiltinImporter.find_module(builtin_util.NAME)
+ with util.uncache(util.BUILTINS.good_name):
+ found = self.machinery.BuiltinImporter.find_module(util.BUILTINS.good_name)
self.assertTrue(found)
self.assertTrue(hasattr(found, 'load_module'))
@@ -72,13 +75,15 @@ class FinderTests(abc.FinderTests):
def test_ignore_path(self):
# The value for 'path' should always trigger a failed import.
- with util.uncache(builtin_util.NAME):
- loader = self.machinery.BuiltinImporter.find_module(builtin_util.NAME,
+ with util.uncache(util.BUILTINS.good_name):
+ loader = self.machinery.BuiltinImporter.find_module(util.BUILTINS.good_name,
['pkg'])
self.assertIsNone(loader)
-Frozen_FinderTests, Source_FinderTests = util.test_both(FinderTests,
- machinery=[frozen_machinery, source_machinery])
+
+(Frozen_FinderTests,
+ Source_FinderTests
+ ) = util.test_both(FinderTests, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/builtin/test_loader.py b/Lib/test/test_importlib/builtin/test_loader.py
index 1f83574..1684ab6 100644
--- a/Lib/test/test_importlib/builtin/test_loader.py
+++ b/Lib/test/test_importlib/builtin/test_loader.py
@@ -1,14 +1,13 @@
from .. import abc
from .. import util
-from . import util as builtin_util
-frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
+machinery = util.import_importlib('importlib.machinery')
import sys
import types
import unittest
-
+@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
class LoaderTests(abc.LoaderTests):
"""Test load_module() for built-in modules."""
@@ -29,8 +28,8 @@ class LoaderTests(abc.LoaderTests):
def test_module(self):
# Common case.
- with util.uncache(builtin_util.NAME):
- module = self.load_module(builtin_util.NAME)
+ with util.uncache(util.BUILTINS.good_name):
+ module = self.load_module(util.BUILTINS.good_name)
self.verify(module)
# Built-in modules cannot be a package.
@@ -41,9 +40,9 @@ class LoaderTests(abc.LoaderTests):
def test_module_reuse(self):
# Test that the same module is used in a reload.
- with util.uncache(builtin_util.NAME):
- module1 = self.load_module(builtin_util.NAME)
- module2 = self.load_module(builtin_util.NAME)
+ with util.uncache(util.BUILTINS.good_name):
+ module1 = self.load_module(util.BUILTINS.good_name)
+ module2 = self.load_module(util.BUILTINS.good_name)
self.assertIs(module1, module2)
def test_unloadable(self):
@@ -66,40 +65,44 @@ class LoaderTests(abc.LoaderTests):
self.assertEqual(cm.exception.name, module_name)
-Frozen_LoaderTests, Source_LoaderTests = util.test_both(LoaderTests,
- machinery=[frozen_machinery, source_machinery])
+(Frozen_LoaderTests,
+ Source_LoaderTests
+ ) = util.test_both(LoaderTests, machinery=machinery)
+@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
class InspectLoaderTests:
"""Tests for InspectLoader methods for BuiltinImporter."""
def test_get_code(self):
# There is no code object.
- result = self.machinery.BuiltinImporter.get_code(builtin_util.NAME)
+ result = self.machinery.BuiltinImporter.get_code(util.BUILTINS.good_name)
self.assertIsNone(result)
def test_get_source(self):
# There is no source.
- result = self.machinery.BuiltinImporter.get_source(builtin_util.NAME)
+ result = self.machinery.BuiltinImporter.get_source(util.BUILTINS.good_name)
self.assertIsNone(result)
def test_is_package(self):
# Cannot be a package.
- result = self.machinery.BuiltinImporter.is_package(builtin_util.NAME)
+ result = self.machinery.BuiltinImporter.is_package(util.BUILTINS.good_name)
self.assertFalse(result)
+ @unittest.skipIf(util.BUILTINS.bad_name is None, 'all modules are built in')
def test_not_builtin(self):
# Modules not built-in should raise ImportError.
for meth_name in ('get_code', 'get_source', 'is_package'):
method = getattr(self.machinery.BuiltinImporter, meth_name)
with self.assertRaises(ImportError) as cm:
- method(builtin_util.BAD_NAME)
- self.assertRaises(builtin_util.BAD_NAME)
+ method(util.BUILTINS.bad_name)
+ self.assertRaises(util.BUILTINS.bad_name)
+
-Frozen_InspectLoaderTests, Source_InspectLoaderTests = util.test_both(
- InspectLoaderTests,
- machinery=[frozen_machinery, source_machinery])
+(Frozen_InspectLoaderTests,
+ Source_InspectLoaderTests
+ ) = util.test_both(InspectLoaderTests, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/builtin/util.py b/Lib/test/test_importlib/builtin/util.py
deleted file mode 100644
index 5704699..0000000
--- a/Lib/test/test_importlib/builtin/util.py
+++ /dev/null
@@ -1,7 +0,0 @@
-import sys
-
-assert 'errno' in sys.builtin_module_names
-NAME = 'errno'
-
-assert 'importlib' not in sys.builtin_module_names
-BAD_NAME = 'importlib'
diff --git a/Lib/test/test_importlib/extension/test_case_sensitivity.py b/Lib/test/test_importlib/extension/test_case_sensitivity.py
index bb2528e..c7d6ca6 100644
--- a/Lib/test/test_importlib/extension/test_case_sensitivity.py
+++ b/Lib/test/test_importlib/extension/test_case_sensitivity.py
@@ -4,22 +4,21 @@ from test import support
import unittest
from .. import util
-from . import util as ext_util
-frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
+machinery = util.import_importlib('importlib.machinery')
# XXX find_spec tests
-@unittest.skipIf(ext_util.FILENAME is None, '_testcapi not available')
+@unittest.skipIf(util.EXTENSIONS.filename is None, '_testcapi not available')
@util.case_insensitive_tests
class ExtensionModuleCaseSensitivityTest:
def find_module(self):
- good_name = ext_util.NAME
+ good_name = util.EXTENSIONS.name
bad_name = good_name.upper()
assert good_name != bad_name
- finder = self.machinery.FileFinder(ext_util.PATH,
+ finder = self.machinery.FileFinder(util.EXTENSIONS.path,
(self.machinery.ExtensionFileLoader,
self.machinery.EXTENSION_SUFFIXES))
return finder.find_module(bad_name)
@@ -42,9 +41,10 @@ class ExtensionModuleCaseSensitivityTest:
loader = self.find_module()
self.assertTrue(hasattr(loader, 'load_module'))
-Frozen_ExtensionCaseSensitivity, Source_ExtensionCaseSensitivity = util.test_both(
- ExtensionModuleCaseSensitivityTest,
- machinery=[frozen_machinery, source_machinery])
+
+(Frozen_ExtensionCaseSensitivity,
+ Source_ExtensionCaseSensitivity
+ ) = util.test_both(ExtensionModuleCaseSensitivityTest, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/extension/test_finder.py b/Lib/test/test_importlib/extension/test_finder.py
index 990f29c..71bf67f 100644
--- a/Lib/test/test_importlib/extension/test_finder.py
+++ b/Lib/test/test_importlib/extension/test_finder.py
@@ -1,8 +1,7 @@
from .. import abc
-from .. import util as test_util
-from . import util
+from .. import util
-machinery = test_util.import_importlib('importlib.machinery')
+machinery = util.import_importlib('importlib.machinery')
import unittest
import warnings
@@ -14,7 +13,7 @@ class FinderTests(abc.FinderTests):
"""Test the finder for extension modules."""
def find_module(self, fullname):
- importer = self.machinery.FileFinder(util.PATH,
+ importer = self.machinery.FileFinder(util.EXTENSIONS.path,
(self.machinery.ExtensionFileLoader,
self.machinery.EXTENSION_SUFFIXES))
with warnings.catch_warnings():
@@ -22,7 +21,7 @@ class FinderTests(abc.FinderTests):
return importer.find_module(fullname)
def test_module(self):
- self.assertTrue(self.find_module(util.NAME))
+ self.assertTrue(self.find_module(util.EXTENSIONS.name))
# No extension module as an __init__ available for testing.
test_package = test_package_in_package = None
@@ -36,8 +35,10 @@ class FinderTests(abc.FinderTests):
def test_failure(self):
self.assertIsNone(self.find_module('asdfjkl;'))
-Frozen_FinderTests, Source_FinderTests = test_util.test_both(
- FinderTests, machinery=machinery)
+
+(Frozen_FinderTests,
+ Source_FinderTests
+ ) = util.test_both(FinderTests, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/extension/test_loader.py b/Lib/test/test_importlib/extension/test_loader.py
index fd9abf2..aefd050 100644
--- a/Lib/test/test_importlib/extension/test_loader.py
+++ b/Lib/test/test_importlib/extension/test_loader.py
@@ -1,4 +1,3 @@
-from . import util as ext_util
from .. import abc
from .. import util
@@ -15,8 +14,8 @@ class LoaderTests(abc.LoaderTests):
"""Test load_module() for extension modules."""
def setUp(self):
- self.loader = self.machinery.ExtensionFileLoader(ext_util.NAME,
- ext_util.FILEPATH)
+ self.loader = self.machinery.ExtensionFileLoader(util.EXTENSIONS.name,
+ util.EXTENSIONS.file_path)
def load_module(self, fullname):
return self.loader.load_module(fullname)
@@ -29,23 +28,23 @@ class LoaderTests(abc.LoaderTests):
self.load_module('XXX')
def test_equality(self):
- other = self.machinery.ExtensionFileLoader(ext_util.NAME,
- ext_util.FILEPATH)
+ other = self.machinery.ExtensionFileLoader(util.EXTENSIONS.name,
+ util.EXTENSIONS.file_path)
self.assertEqual(self.loader, other)
def test_inequality(self):
- other = self.machinery.ExtensionFileLoader('_' + ext_util.NAME,
- ext_util.FILEPATH)
+ other = self.machinery.ExtensionFileLoader('_' + util.EXTENSIONS.name,
+ util.EXTENSIONS.file_path)
self.assertNotEqual(self.loader, other)
def test_module(self):
- with util.uncache(ext_util.NAME):
- module = self.load_module(ext_util.NAME)
- for attr, value in [('__name__', ext_util.NAME),
- ('__file__', ext_util.FILEPATH),
+ with util.uncache(util.EXTENSIONS.name):
+ module = self.load_module(util.EXTENSIONS.name)
+ for attr, value in [('__name__', util.EXTENSIONS.name),
+ ('__file__', util.EXTENSIONS.file_path),
('__package__', '')]:
self.assertEqual(getattr(module, attr), value)
- self.assertIn(ext_util.NAME, sys.modules)
+ self.assertIn(util.EXTENSIONS.name, sys.modules)
self.assertIsInstance(module.__loader__,
self.machinery.ExtensionFileLoader)
@@ -56,9 +55,9 @@ class LoaderTests(abc.LoaderTests):
test_lacking_parent = None
def test_module_reuse(self):
- with util.uncache(ext_util.NAME):
- module1 = self.load_module(ext_util.NAME)
- module2 = self.load_module(ext_util.NAME)
+ with util.uncache(util.EXTENSIONS.name):
+ module1 = self.load_module(util.EXTENSIONS.name)
+ module2 = self.load_module(util.EXTENSIONS.name)
self.assertIs(module1, module2)
# No easy way to trigger a failure after a successful import.
@@ -71,14 +70,15 @@ class LoaderTests(abc.LoaderTests):
self.assertEqual(cm.exception.name, name)
def test_is_package(self):
- self.assertFalse(self.loader.is_package(ext_util.NAME))
+ self.assertFalse(self.loader.is_package(util.EXTENSIONS.name))
for suffix in self.machinery.EXTENSION_SUFFIXES:
path = os.path.join('some', 'path', 'pkg', '__init__' + suffix)
loader = self.machinery.ExtensionFileLoader('pkg', path)
self.assertTrue(loader.is_package('pkg'))
-Frozen_LoaderTests, Source_LoaderTests = util.test_both(
- LoaderTests, machinery=machinery)
+(Frozen_LoaderTests,
+ Source_LoaderTests
+ ) = util.test_both(LoaderTests, machinery=machinery)
diff --git a/Lib/test/test_importlib/extension/test_path_hook.py b/Lib/test/test_importlib/extension/test_path_hook.py
index 49d6734..8f4b8bb 100644
--- a/Lib/test/test_importlib/extension/test_path_hook.py
+++ b/Lib/test/test_importlib/extension/test_path_hook.py
@@ -1,7 +1,6 @@
-from .. import util as test_util
-from . import util
+from .. import util
-machinery = test_util.import_importlib('importlib.machinery')
+machinery = util.import_importlib('importlib.machinery')
import collections
import sys
@@ -22,10 +21,12 @@ class PathHookTests:
def test_success(self):
# Path hook should handle a directory where a known extension module
# exists.
- self.assertTrue(hasattr(self.hook(util.PATH), 'find_module'))
+ self.assertTrue(hasattr(self.hook(util.EXTENSIONS.path), 'find_module'))
-Frozen_PathHooksTests, Source_PathHooksTests = test_util.test_both(
- PathHookTests, machinery=machinery)
+
+(Frozen_PathHooksTests,
+ Source_PathHooksTests
+ ) = util.test_both(PathHookTests, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/extension/util.py b/Lib/test/test_importlib/extension/util.py
deleted file mode 100644
index 8d089f0..0000000
--- a/Lib/test/test_importlib/extension/util.py
+++ /dev/null
@@ -1,19 +0,0 @@
-from importlib import machinery
-import os
-import sys
-
-PATH = None
-EXT = None
-FILENAME = None
-NAME = '_testcapi'
-try:
- for PATH in sys.path:
- for EXT in machinery.EXTENSION_SUFFIXES:
- FILENAME = NAME + EXT
- FILEPATH = os.path.join(PATH, FILENAME)
- if os.path.exists(os.path.join(PATH, FILENAME)):
- raise StopIteration
- else:
- PATH = EXT = FILENAME = FILEPATH = None
-except StopIteration:
- pass
diff --git a/Lib/test/test_importlib/frozen/test_finder.py b/Lib/test/test_importlib/frozen/test_finder.py
index f9f97f3..519aa02 100644
--- a/Lib/test/test_importlib/frozen/test_finder.py
+++ b/Lib/test/test_importlib/frozen/test_finder.py
@@ -37,8 +37,10 @@ class FindSpecTests(abc.FinderTests):
spec = self.find('<not real>')
self.assertIsNone(spec)
-Frozen_FindSpecTests, Source_FindSpecTests = util.test_both(FindSpecTests,
- machinery=machinery)
+
+(Frozen_FindSpecTests,
+ Source_FindSpecTests
+ ) = util.test_both(FindSpecTests, machinery=machinery)
class FinderTests(abc.FinderTests):
@@ -72,8 +74,10 @@ class FinderTests(abc.FinderTests):
loader = self.find('<not real>')
self.assertIsNone(loader)
-Frozen_FinderTests, Source_FinderTests = util.test_both(FinderTests,
- machinery=machinery)
+
+(Frozen_FinderTests,
+ Source_FinderTests
+ ) = util.test_both(FinderTests, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/frozen/test_loader.py b/Lib/test/test_importlib/frozen/test_loader.py
index 7c01464..603c7d7 100644
--- a/Lib/test/test_importlib/frozen/test_loader.py
+++ b/Lib/test/test_importlib/frozen/test_loader.py
@@ -85,8 +85,10 @@ class ExecModuleTests(abc.LoaderTests):
self.exec_module('_not_real')
self.assertEqual(cm.exception.name, '_not_real')
-Frozen_ExecModuleTests, Source_ExecModuleTests = util.test_both(ExecModuleTests,
- machinery=machinery)
+
+(Frozen_ExecModuleTests,
+ Source_ExecModuleTests
+ ) = util.test_both(ExecModuleTests, machinery=machinery)
class LoaderTests(abc.LoaderTests):
@@ -175,8 +177,10 @@ class LoaderTests(abc.LoaderTests):
self.machinery.FrozenImporter.load_module('_not_real')
self.assertEqual(cm.exception.name, '_not_real')
-Frozen_LoaderTests, Source_LoaderTests = util.test_both(LoaderTests,
- machinery=machinery)
+
+(Frozen_LoaderTests,
+ Source_LoaderTests
+ ) = util.test_both(LoaderTests, machinery=machinery)
class InspectLoaderTests:
@@ -214,8 +218,9 @@ class InspectLoaderTests:
method('importlib')
self.assertEqual(cm.exception.name, 'importlib')
-Frozen_ILTests, Source_ILTests = util.test_both(InspectLoaderTests,
- machinery=machinery)
+(Frozen_ILTests,
+ Source_ILTests
+ ) = util.test_both(InspectLoaderTests, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/import_/test___loader__.py b/Lib/test/test_importlib/import_/test___loader__.py
index 6df8010..4b18093 100644
--- a/Lib/test/test_importlib/import_/test___loader__.py
+++ b/Lib/test/test_importlib/import_/test___loader__.py
@@ -4,7 +4,6 @@ import types
import unittest
from .. import util
-from . import util as import_util
class SpecLoaderMock:
@@ -12,6 +11,9 @@ class SpecLoaderMock:
def find_spec(self, fullname, path=None, target=None):
return machinery.ModuleSpec(fullname, self)
+ def create_module(self, spec):
+ return None
+
def exec_module(self, module):
pass
@@ -24,8 +26,10 @@ class SpecLoaderAttributeTests:
module = self.__import__('blah')
self.assertEqual(loader, module.__loader__)
-Frozen_SpecTests, Source_SpecTests = util.test_both(
- SpecLoaderAttributeTests, __import__=import_util.__import__)
+
+(Frozen_SpecTests,
+ Source_SpecTests
+ ) = util.test_both(SpecLoaderAttributeTests, __import__=util.__import__)
class LoaderMock:
@@ -62,8 +66,9 @@ class LoaderAttributeTests:
self.assertEqual(loader, module.__loader__)
-Frozen_Tests, Source_Tests = util.test_both(LoaderAttributeTests,
- __import__=import_util.__import__)
+(Frozen_Tests,
+ Source_Tests
+ ) = util.test_both(LoaderAttributeTests, __import__=util.__import__)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/import_/test___package__.py b/Lib/test/test_importlib/import_/test___package__.py
index 2e19725..c7d3a2a 100644
--- a/Lib/test/test_importlib/import_/test___package__.py
+++ b/Lib/test/test_importlib/import_/test___package__.py
@@ -6,7 +6,6 @@ of using the typical __path__/__name__ test).
"""
import unittest
from .. import util
-from . import util as import_util
class Using__package__:
@@ -70,17 +69,23 @@ class Using__package__:
with self.assertRaises(TypeError):
self.__import__('', globals, {}, ['relimport'], 1)
+
class Using__package__PEP302(Using__package__):
mock_modules = util.mock_modules
-Frozen_UsingPackagePEP302, Source_UsingPackagePEP302 = util.test_both(
- Using__package__PEP302, __import__=import_util.__import__)
-class Using__package__PEP302(Using__package__):
+(Frozen_UsingPackagePEP302,
+ Source_UsingPackagePEP302
+ ) = util.test_both(Using__package__PEP302, __import__=util.__import__)
+
+
+class Using__package__PEP451(Using__package__):
mock_modules = util.mock_spec
-Frozen_UsingPackagePEP451, Source_UsingPackagePEP451 = util.test_both(
- Using__package__PEP302, __import__=import_util.__import__)
+
+(Frozen_UsingPackagePEP451,
+ Source_UsingPackagePEP451
+ ) = util.test_both(Using__package__PEP451, __import__=util.__import__)
class Setting__package__:
@@ -95,7 +100,7 @@ class Setting__package__:
"""
- __import__ = import_util.__import__[1]
+ __import__ = util.__import__['Source']
# [top-level]
def test_top_level(self):
diff --git a/Lib/test/test_importlib/import_/test_api.py b/Lib/test/test_importlib/import_/test_api.py
index 439c105..7069d9e 100644
--- a/Lib/test/test_importlib/import_/test_api.py
+++ b/Lib/test/test_importlib/import_/test_api.py
@@ -1,5 +1,4 @@
from .. import util
-from . import util as import_util
from importlib import machinery
import sys
@@ -18,6 +17,10 @@ class BadSpecFinderLoader:
return spec
@staticmethod
+ def create_module(spec):
+ return None
+
+ @staticmethod
def exec_module(module):
if module.__name__ == SUBMOD_NAME:
raise ImportError('I cannot be loaded!')
@@ -79,15 +82,19 @@ class APITest:
class OldAPITests(APITest):
bad_finder_loader = BadLoaderFinder
-Frozen_OldAPITests, Source_OldAPITests = util.test_both(
- OldAPITests, __import__=import_util.__import__)
+
+(Frozen_OldAPITests,
+ Source_OldAPITests
+ ) = util.test_both(OldAPITests, __import__=util.__import__)
class SpecAPITests(APITest):
bad_finder_loader = BadSpecFinderLoader
-Frozen_SpecAPITests, Source_SpecAPITests = util.test_both(
- SpecAPITests, __import__=import_util.__import__)
+
+(Frozen_SpecAPITests,
+ Source_SpecAPITests
+ ) = util.test_both(SpecAPITests, __import__=util.__import__)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/import_/test_caching.py b/Lib/test/test_importlib/import_/test_caching.py
index c292ee4..8079add 100644
--- a/Lib/test/test_importlib/import_/test_caching.py
+++ b/Lib/test/test_importlib/import_/test_caching.py
@@ -1,6 +1,5 @@
"""Test that sys.modules is used properly by import."""
from .. import util
-from . import util as import_util
import sys
from types import MethodType
import unittest
@@ -39,15 +38,17 @@ class UseCache:
self.__import__(name)
self.assertEqual(cm.exception.name, name)
-Frozen_UseCache, Source_UseCache = util.test_both(
- UseCache, __import__=import_util.__import__)
+
+(Frozen_UseCache,
+ Source_UseCache
+ ) = util.test_both(UseCache, __import__=util.__import__)
class ImportlibUseCache(UseCache, unittest.TestCase):
# Pertinent only to PEP 302; exec_module() doesn't return a module.
- __import__ = import_util.__import__[1]
+ __import__ = util.__import__['Source']
def create_mock(self, *names, return_=None):
mock = util.mock_modules(*names)
diff --git a/Lib/test/test_importlib/import_/test_fromlist.py b/Lib/test/test_importlib/import_/test_fromlist.py
index a755b75..8045465 100644
--- a/Lib/test/test_importlib/import_/test_fromlist.py
+++ b/Lib/test/test_importlib/import_/test_fromlist.py
@@ -1,6 +1,5 @@
"""Test that the semantics relating to the 'fromlist' argument are correct."""
from .. import util
-from . import util as import_util
import unittest
@@ -29,8 +28,10 @@ class ReturnValue:
module = self.__import__('pkg.module', fromlist=['attr'])
self.assertEqual(module.__name__, 'pkg.module')
-Frozen_ReturnValue, Source_ReturnValue = util.test_both(
- ReturnValue, __import__=import_util.__import__)
+
+(Frozen_ReturnValue,
+ Source_ReturnValue
+ ) = util.test_both(ReturnValue, __import__=util.__import__)
class HandlingFromlist:
@@ -121,8 +122,10 @@ class HandlingFromlist:
self.assertEqual(module.module1.__name__, 'pkg.module1')
self.assertEqual(module.module2.__name__, 'pkg.module2')
-Frozen_FromList, Source_FromList = util.test_both(
- HandlingFromlist, __import__=import_util.__import__)
+
+(Frozen_FromList,
+ Source_FromList
+ ) = util.test_both(HandlingFromlist, __import__=util.__import__)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/import_/test_meta_path.py b/Lib/test/test_importlib/import_/test_meta_path.py
index 5eeb145..c452cdd 100644
--- a/Lib/test/test_importlib/import_/test_meta_path.py
+++ b/Lib/test/test_importlib/import_/test_meta_path.py
@@ -1,5 +1,4 @@
from .. import util
-from . import util as import_util
import importlib._bootstrap
import sys
from types import MethodType
@@ -46,8 +45,10 @@ class CallingOrder:
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, ImportWarning))
-Frozen_CallingOrder, Source_CallingOrder = util.test_both(
- CallingOrder, __import__=import_util.__import__)
+
+(Frozen_CallingOrder,
+ Source_CallingOrder
+ ) = util.test_both(CallingOrder, __import__=util.__import__)
class CallSignature:
@@ -100,19 +101,25 @@ class CallSignature:
self.assertEqual(args[0], mod_name)
self.assertIs(args[1], path)
+
class CallSignaturePEP302(CallSignature):
mock_modules = util.mock_modules
finder_name = 'find_module'
-Frozen_CallSignaturePEP302, Source_CallSignaturePEP302 = util.test_both(
- CallSignaturePEP302, __import__=import_util.__import__)
+
+(Frozen_CallSignaturePEP302,
+ Source_CallSignaturePEP302
+ ) = util.test_both(CallSignaturePEP302, __import__=util.__import__)
+
class CallSignaturePEP451(CallSignature):
mock_modules = util.mock_spec
finder_name = 'find_spec'
-Frozen_CallSignaturePEP451, Source_CallSignaturePEP451 = util.test_both(
- CallSignaturePEP451, __import__=import_util.__import__)
+
+(Frozen_CallSignaturePEP451,
+ Source_CallSignaturePEP451
+ ) = util.test_both(CallSignaturePEP451, __import__=util.__import__)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/import_/test_packages.py b/Lib/test/test_importlib/import_/test_packages.py
index 55a5d14..3755b84 100644
--- a/Lib/test/test_importlib/import_/test_packages.py
+++ b/Lib/test/test_importlib/import_/test_packages.py
@@ -1,5 +1,4 @@
from .. import util
-from . import util as import_util
import sys
import unittest
import importlib
@@ -102,8 +101,10 @@ class ParentModuleTests:
finally:
support.unload(subname)
-Frozen_ParentTests, Source_ParentTests = util.test_both(
- ParentModuleTests, __import__=import_util.__import__)
+
+(Frozen_ParentTests,
+ Source_ParentTests
+ ) = util.test_both(ParentModuleTests, __import__=util.__import__)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/import_/test_path.py b/Lib/test/test_importlib/import_/test_path.py
index 1274f8c..9a3c4fe 100644
--- a/Lib/test/test_importlib/import_/test_path.py
+++ b/Lib/test/test_importlib/import_/test_path.py
@@ -1,11 +1,11 @@
from .. import util
-from . import util as import_util
importlib = util.import_importlib('importlib')
machinery = util.import_importlib('importlib.machinery')
import os
import sys
+import tempfile
from types import ModuleType
import unittest
import warnings
@@ -58,7 +58,7 @@ class FinderTests:
module = '<test module>'
path = '<test path>'
importer = util.mock_spec(module)
- hook = import_util.mock_path_hook(path, importer=importer)
+ 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)
@@ -83,7 +83,7 @@ class FinderTests:
path = ''
module = '<test module>'
importer = util.mock_spec(module)
- hook = import_util.mock_path_hook(os.getcwd(), importer=importer)
+ 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)
@@ -112,8 +112,69 @@ class FinderTests:
if email is not missing:
sys.modules['email'] = email
-Frozen_FinderTests, Source_FinderTests = util.test_both(
- FinderTests, importlib=importlib, machinery=machinery)
+ def test_finder_with_find_module(self):
+ class TestFinder:
+ def find_module(self, fullname):
+ return self.to_return
+ failing_finder = TestFinder()
+ failing_finder.to_return = None
+ path = 'testing path'
+ with util.import_state(path_importer_cache={path: failing_finder}):
+ self.assertIsNone(
+ self.machinery.PathFinder.find_spec('whatever', [path]))
+ success_finder = TestFinder()
+ success_finder.to_return = __loader__
+ with util.import_state(path_importer_cache={path: success_finder}):
+ spec = self.machinery.PathFinder.find_spec('whatever', [path])
+ self.assertEqual(spec.loader, __loader__)
+
+ def test_finder_with_find_loader(self):
+ class TestFinder:
+ loader = None
+ portions = []
+ def find_loader(self, fullname):
+ return self.loader, self.portions
+ path = 'testing path'
+ with util.import_state(path_importer_cache={path: TestFinder()}):
+ self.assertIsNone(
+ self.machinery.PathFinder.find_spec('whatever', [path]))
+ success_finder = TestFinder()
+ success_finder.loader = __loader__
+ with util.import_state(path_importer_cache={path: success_finder}):
+ spec = self.machinery.PathFinder.find_spec('whatever', [path])
+ self.assertEqual(spec.loader, __loader__)
+
+ def test_finder_with_find_spec(self):
+ class TestFinder:
+ spec = None
+ def find_spec(self, fullname, target=None):
+ return self.spec
+ path = 'testing path'
+ with util.import_state(path_importer_cache={path: TestFinder()}):
+ self.assertIsNone(
+ self.machinery.PathFinder.find_spec('whatever', [path]))
+ success_finder = TestFinder()
+ success_finder.spec = self.machinery.ModuleSpec('whatever', __loader__)
+ with util.import_state(path_importer_cache={path: success_finder}):
+ got = self.machinery.PathFinder.find_spec('whatever', [path])
+ self.assertEqual(got, success_finder.spec)
+
+ @unittest.skipIf(sys.platform == 'win32', "cwd can't not exist on Windows")
+ def test_deleted_cwd(self):
+ # Issue #22834
+ self.addCleanup(os.chdir, os.getcwd())
+ with tempfile.TemporaryDirectory() as path:
+ os.chdir(path)
+ with util.import_state(path=['']):
+ # Do not want FileNotFoundError raised.
+ self.assertIsNone(self.machinery.PathFinder.find_spec('whatever'))
+
+
+
+
+(Frozen_FinderTests,
+ Source_FinderTests
+ ) = util.test_both(FinderTests, importlib=importlib, machinery=machinery)
class PathEntryFinderTests:
@@ -136,8 +197,10 @@ class PathEntryFinderTests:
path_hooks=[Finder]):
self.machinery.PathFinder.find_spec('importlib')
-Frozen_PEFTests, Source_PEFTests = util.test_both(
- PathEntryFinderTests, machinery=machinery)
+
+(Frozen_PEFTests,
+ Source_PEFTests
+ ) = util.test_both(PathEntryFinderTests, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/import_/test_relative_imports.py b/Lib/test/test_importlib/import_/test_relative_imports.py
index b216e9c..28bb6f7 100644
--- a/Lib/test/test_importlib/import_/test_relative_imports.py
+++ b/Lib/test/test_importlib/import_/test_relative_imports.py
@@ -1,6 +1,5 @@
"""Test relative imports (PEP 328)."""
from .. import util
-from . import util as import_util
import sys
import unittest
@@ -208,8 +207,10 @@ class RelativeImports:
with self.assertRaises(KeyError):
self.__import__('sys', level=1)
-Frozen_RelativeImports, Source_RelativeImports = util.test_both(
- RelativeImports, __import__=import_util.__import__)
+
+(Frozen_RelativeImports,
+ Source_RelativeImports
+ ) = util.test_both(RelativeImports, __import__=util.__import__)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/import_/util.py b/Lib/test/test_importlib/import_/util.py
deleted file mode 100644
index dcb490f..0000000
--- a/Lib/test/test_importlib/import_/util.py
+++ /dev/null
@@ -1,20 +0,0 @@
-from .. import util
-
-frozen_importlib, source_importlib = util.import_importlib('importlib')
-
-import builtins
-import functools
-import importlib
-import unittest
-
-
-__import__ = staticmethod(builtins.__import__), staticmethod(source_importlib.__import__)
-
-
-def mock_path_hook(*entries, importer):
- """A mock sys.path_hooks entry."""
- def hook(entry):
- if entry not in entries:
- raise ImportError
- return importer
- return hook
diff --git a/Lib/test/test_importlib/source/test_case_sensitivity.py b/Lib/test/test_importlib/source/test_case_sensitivity.py
index efd3146..29e95b2 100644
--- a/Lib/test/test_importlib/source/test_case_sensitivity.py
+++ b/Lib/test/test_importlib/source/test_case_sensitivity.py
@@ -1,6 +1,5 @@
"""Test case-sensitivity (PEP 235)."""
from .. import util
-from . import util as source_util
importlib = util.import_importlib('importlib')
machinery = util.import_importlib('importlib.machinery')
@@ -32,7 +31,7 @@ class CaseSensitivityTest:
"""Look for a module with matching and non-matching sensitivity."""
sensitive_pkg = 'sensitive.{0}'.format(self.name)
insensitive_pkg = 'insensitive.{0}'.format(self.name.lower())
- context = source_util.create_modules(insensitive_pkg, sensitive_pkg)
+ context = util.create_modules(insensitive_pkg, sensitive_pkg)
with context as mapping:
sensitive_path = os.path.join(mapping['.root'], 'sensitive')
insensitive_path = os.path.join(mapping['.root'], 'insensitive')
@@ -63,20 +62,28 @@ class CaseSensitivityTest:
self.assertIsNotNone(insensitive)
self.assertIn(self.name, insensitive.get_filename(self.name))
+
class CaseSensitivityTestPEP302(CaseSensitivityTest):
def find(self, finder):
return finder.find_module(self.name)
-Frozen_CaseSensitivityTestPEP302, Source_CaseSensitivityTestPEP302 = util.test_both(
- CaseSensitivityTestPEP302, importlib=importlib, machinery=machinery)
+
+(Frozen_CaseSensitivityTestPEP302,
+ Source_CaseSensitivityTestPEP302
+ ) = util.test_both(CaseSensitivityTestPEP302, importlib=importlib,
+ machinery=machinery)
+
class CaseSensitivityTestPEP451(CaseSensitivityTest):
def find(self, finder):
found = finder.find_spec(self.name)
return found.loader if found is not None else found
-Frozen_CaseSensitivityTestPEP451, Source_CaseSensitivityTestPEP451 = util.test_both(
- CaseSensitivityTestPEP451, importlib=importlib, machinery=machinery)
+
+(Frozen_CaseSensitivityTestPEP451,
+ Source_CaseSensitivityTestPEP451
+ ) = util.test_both(CaseSensitivityTestPEP451, importlib=importlib,
+ machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/source/test_file_loader.py b/Lib/test/test_importlib/source/test_file_loader.py
index 2d415f9..73f4c62 100644
--- a/Lib/test/test_importlib/source/test_file_loader.py
+++ b/Lib/test/test_importlib/source/test_file_loader.py
@@ -1,6 +1,5 @@
from .. import abc
from .. import util
-from . import util as source_util
importlib = util.import_importlib('importlib')
importlib_abc = util.import_importlib('importlib.abc')
@@ -71,7 +70,7 @@ class SimpleTest(abc.LoaderTests):
# [basic]
def test_module(self):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
@@ -83,7 +82,7 @@ class SimpleTest(abc.LoaderTests):
self.assertEqual(getattr(module, attr), value)
def test_package(self):
- with source_util.create_modules('_pkg.__init__') as mapping:
+ with util.create_modules('_pkg.__init__') as mapping:
loader = self.machinery.SourceFileLoader('_pkg',
mapping['_pkg.__init__'])
with warnings.catch_warnings():
@@ -98,7 +97,7 @@ class SimpleTest(abc.LoaderTests):
def test_lacking_parent(self):
- with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
+ with util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
loader = self.machinery.SourceFileLoader('_pkg.mod',
mapping['_pkg.mod'])
with warnings.catch_warnings():
@@ -115,7 +114,7 @@ class SimpleTest(abc.LoaderTests):
return lambda name: fxn(name) + 1
def test_module_reuse(self):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
@@ -139,7 +138,7 @@ class SimpleTest(abc.LoaderTests):
attributes = ('__file__', '__path__', '__package__')
value = '<test>'
name = '_temp'
- with source_util.create_modules(name) as mapping:
+ with util.create_modules(name) as mapping:
orig_module = types.ModuleType(name)
for attr in attributes:
setattr(orig_module, attr, value)
@@ -159,7 +158,7 @@ class SimpleTest(abc.LoaderTests):
# [syntax error]
def test_bad_syntax(self):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
with open(mapping['_temp'], 'w') as file:
file.write('=')
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
@@ -190,11 +189,11 @@ class SimpleTest(abc.LoaderTests):
if os.path.exists(pycache):
shutil.rmtree(pycache)
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_timestamp_overflow(self):
# When a modification timestamp is larger than 2**32, it should be
# truncated rather than raise an OverflowError.
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
source = mapping['_temp']
compiled = self.util.cache_from_source(source)
with open(source, 'w') as f:
@@ -236,9 +235,11 @@ class SimpleTest(abc.LoaderTests):
warnings.simplefilter('ignore', DeprecationWarning)
loader.load_module('bad name')
-Frozen_SimpleTest, Source_SimpleTest = util.test_both(
- SimpleTest, importlib=importlib, machinery=machinery, abc=importlib_abc,
- util=importlib_util)
+
+(Frozen_SimpleTest,
+ Source_SimpleTest
+ ) = util.test_both(SimpleTest, importlib=importlib, machinery=machinery,
+ abc=importlib_abc, util=importlib_util)
class BadBytecodeTest:
@@ -275,45 +276,45 @@ class BadBytecodeTest:
return bytecode_path
def _test_empty_file(self, test, *, del_source=False):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
bc_path = self.manipulate_bytecode('_temp', mapping,
lambda bc: b'',
del_source=del_source)
test('_temp', mapping, bc_path)
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def _test_partial_magic(self, test, *, del_source=False):
# When their are less than 4 bytes to a .pyc, regenerate it if
# possible, else raise ImportError.
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
bc_path = self.manipulate_bytecode('_temp', mapping,
lambda bc: bc[:3],
del_source=del_source)
test('_temp', mapping, bc_path)
def _test_magic_only(self, test, *, del_source=False):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
bc_path = self.manipulate_bytecode('_temp', mapping,
lambda bc: bc[:4],
del_source=del_source)
test('_temp', mapping, bc_path)
def _test_partial_timestamp(self, test, *, del_source=False):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
bc_path = self.manipulate_bytecode('_temp', mapping,
lambda bc: bc[:7],
del_source=del_source)
test('_temp', mapping, bc_path)
def _test_partial_size(self, test, *, del_source=False):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
bc_path = self.manipulate_bytecode('_temp', mapping,
lambda bc: bc[:11],
del_source=del_source)
test('_temp', mapping, bc_path)
def _test_no_marshal(self, *, del_source=False):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
bc_path = self.manipulate_bytecode('_temp', mapping,
lambda bc: bc[:12],
del_source=del_source)
@@ -322,7 +323,7 @@ class BadBytecodeTest:
self.import_(file_path, '_temp')
def _test_non_code_marshal(self, *, del_source=False):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
bytecode_path = self.manipulate_bytecode('_temp', mapping,
lambda bc: bc[:12] + marshal.dumps(b'abcd'),
del_source=del_source)
@@ -333,7 +334,7 @@ class BadBytecodeTest:
self.assertEqual(cm.exception.path, bytecode_path)
def _test_bad_marshal(self, *, del_source=False):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
bytecode_path = self.manipulate_bytecode('_temp', mapping,
lambda bc: bc[:12] + b'<test>',
del_source=del_source)
@@ -342,11 +343,12 @@ class BadBytecodeTest:
self.import_(file_path, '_temp')
def _test_bad_magic(self, test, *, del_source=False):
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
bc_path = self.manipulate_bytecode('_temp', mapping,
lambda bc: b'\x00\x00\x00\x00' + bc[4:])
test('_temp', mapping, bc_path)
+
class BadBytecodeTestPEP451(BadBytecodeTest):
def import_(self, file, module_name):
@@ -355,6 +357,7 @@ class BadBytecodeTestPEP451(BadBytecodeTest):
module.__spec__ = self.util.spec_from_loader(module_name, loader)
loader.exec_module(module)
+
class BadBytecodeTestPEP302(BadBytecodeTest):
def import_(self, file, module_name):
@@ -371,7 +374,7 @@ class SourceLoaderBadBytecodeTest:
def setUpClass(cls):
cls.loader = cls.machinery.SourceFileLoader
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_empty_file(self):
# When a .pyc is empty, regenerate it if possible, else raise
# ImportError.
@@ -390,7 +393,7 @@ class SourceLoaderBadBytecodeTest:
self._test_partial_magic(test)
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_magic_only(self):
# When there is only the magic number, regenerate the .pyc if possible,
# else raise EOFError.
@@ -401,7 +404,7 @@ class SourceLoaderBadBytecodeTest:
self._test_magic_only(test)
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_bad_magic(self):
# When the magic number is different, the bytecode should be
# regenerated.
@@ -413,7 +416,7 @@ class SourceLoaderBadBytecodeTest:
self._test_bad_magic(test)
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_partial_timestamp(self):
# When the timestamp is partial, regenerate the .pyc, else
# raise EOFError.
@@ -424,7 +427,7 @@ class SourceLoaderBadBytecodeTest:
self._test_partial_timestamp(test)
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_partial_size(self):
# When the size is partial, regenerate the .pyc, else
# raise EOFError.
@@ -435,29 +438,29 @@ class SourceLoaderBadBytecodeTest:
self._test_partial_size(test)
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_no_marshal(self):
# When there is only the magic number and timestamp, raise EOFError.
self._test_no_marshal()
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_non_code_marshal(self):
self._test_non_code_marshal()
# XXX ImportError when sourceless
# [bad marshal]
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_bad_marshal(self):
# Bad marshal data should raise a ValueError.
self._test_bad_marshal()
# [bad timestamp]
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_old_timestamp(self):
# When the timestamp is older than the source, bytecode should be
# regenerated.
zeros = b'\x00\x00\x00\x00'
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
py_compile.compile(mapping['_temp'])
bytecode_path = self.util.cache_from_source(mapping['_temp'])
with open(bytecode_path, 'r+b') as bytecode_file:
@@ -471,10 +474,10 @@ class SourceLoaderBadBytecodeTest:
self.assertEqual(bytecode_file.read(4), source_timestamp)
# [bytecode read-only]
- @source_util.writes_bytecode_files
+ @util.writes_bytecode_files
def test_read_only_bytecode(self):
# When bytecode is read-only but should be rewritten, fail silently.
- with source_util.create_modules('_temp') as mapping:
+ with util.create_modules('_temp') as mapping:
# Create bytecode that will need to be re-created.
py_compile.compile(mapping['_temp'])
bytecode_path = self.util.cache_from_source(mapping['_temp'])
@@ -491,21 +494,29 @@ class SourceLoaderBadBytecodeTest:
# Make writable for eventual clean-up.
os.chmod(bytecode_path, stat.S_IWUSR)
+
class SourceLoaderBadBytecodeTestPEP451(
SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451):
pass
-Frozen_SourceBadBytecodePEP451, Source_SourceBadBytecodePEP451 = util.test_both(
- SourceLoaderBadBytecodeTestPEP451, importlib=importlib, machinery=machinery,
- abc=importlib_abc, util=importlib_util)
+
+(Frozen_SourceBadBytecodePEP451,
+ Source_SourceBadBytecodePEP451
+ ) = util.test_both(SourceLoaderBadBytecodeTestPEP451, importlib=importlib,
+ machinery=machinery, abc=importlib_abc,
+ util=importlib_util)
+
class SourceLoaderBadBytecodeTestPEP302(
SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302):
pass
-Frozen_SourceBadBytecodePEP302, Source_SourceBadBytecodePEP302 = util.test_both(
- SourceLoaderBadBytecodeTestPEP302, importlib=importlib, machinery=machinery,
- abc=importlib_abc, util=importlib_util)
+
+(Frozen_SourceBadBytecodePEP302,
+ Source_SourceBadBytecodePEP302
+ ) = util.test_both(SourceLoaderBadBytecodeTestPEP302, importlib=importlib,
+ machinery=machinery, abc=importlib_abc,
+ util=importlib_util)
class SourcelessLoaderBadBytecodeTest:
@@ -567,21 +578,29 @@ class SourcelessLoaderBadBytecodeTest:
def test_non_code_marshal(self):
self._test_non_code_marshal(del_source=True)
+
class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest,
BadBytecodeTestPEP451):
pass
-Frozen_SourcelessBadBytecodePEP451, Source_SourcelessBadBytecodePEP451 = util.test_both(
- SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib,
- machinery=machinery, abc=importlib_abc, util=importlib_util)
+
+(Frozen_SourcelessBadBytecodePEP451,
+ Source_SourcelessBadBytecodePEP451
+ ) = util.test_both(SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib,
+ machinery=machinery, abc=importlib_abc,
+ util=importlib_util)
+
class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest,
BadBytecodeTestPEP302):
pass
-Frozen_SourcelessBadBytecodePEP302, Source_SourcelessBadBytecodePEP302 = util.test_both(
- SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib,
- machinery=machinery, abc=importlib_abc, util=importlib_util)
+
+(Frozen_SourcelessBadBytecodePEP302,
+ Source_SourcelessBadBytecodePEP302
+ ) = util.test_both(SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib,
+ machinery=machinery, abc=importlib_abc,
+ util=importlib_util)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/source/test_finder.py b/Lib/test/test_importlib/source/test_finder.py
index 473297b..f372b85 100644
--- a/Lib/test/test_importlib/source/test_finder.py
+++ b/Lib/test/test_importlib/source/test_finder.py
@@ -1,6 +1,5 @@
from .. import abc
from .. import util
-from . import util as source_util
machinery = util.import_importlib('importlib.machinery')
@@ -60,7 +59,7 @@ class FinderTests(abc.FinderTests):
"""
if create is None:
create = {test}
- with source_util.create_modules(*create) as mapping:
+ with util.create_modules(*create) as mapping:
if compile_:
for name in compile_:
py_compile.compile(mapping[name])
@@ -100,14 +99,14 @@ class FinderTests(abc.FinderTests):
# [sub module]
def test_module_in_package(self):
- with source_util.create_modules('pkg.__init__', 'pkg.sub') as mapping:
+ with util.create_modules('pkg.__init__', 'pkg.sub') as mapping:
pkg_dir = os.path.dirname(mapping['pkg.__init__'])
loader = self.import_(pkg_dir, 'pkg.sub')
self.assertTrue(hasattr(loader, 'load_module'))
# [sub package]
def test_package_in_package(self):
- context = source_util.create_modules('pkg.__init__', 'pkg.sub.__init__')
+ context = util.create_modules('pkg.__init__', 'pkg.sub.__init__')
with context as mapping:
pkg_dir = os.path.dirname(mapping['pkg.__init__'])
loader = self.import_(pkg_dir, 'pkg.sub')
@@ -120,7 +119,7 @@ class FinderTests(abc.FinderTests):
self.assertIn('__init__', loader.get_filename(name))
def test_failure(self):
- with source_util.create_modules('blah') as mapping:
+ with util.create_modules('blah') as mapping:
nothing = self.import_(mapping['.root'], 'sdfsadsadf')
self.assertIsNone(nothing)
@@ -147,7 +146,7 @@ class FinderTests(abc.FinderTests):
# Regression test for http://bugs.python.org/issue14846
def test_dir_removal_handling(self):
mod = 'mod'
- with source_util.create_modules(mod) as mapping:
+ with util.create_modules(mod) as mapping:
finder = self.get_finder(mapping['.root'])
found = self._find(finder, 'mod', loader_only=True)
self.assertIsNotNone(found)
@@ -196,8 +195,10 @@ class FinderTestsPEP451(FinderTests):
spec = finder.find_spec(name)
return spec.loader if spec is not None else spec
-Frozen_FinderTestsPEP451, Source_FinderTestsPEP451 = util.test_both(
- FinderTestsPEP451, machinery=machinery)
+
+(Frozen_FinderTestsPEP451,
+ Source_FinderTestsPEP451
+ ) = util.test_both(FinderTestsPEP451, machinery=machinery)
class FinderTestsPEP420(FinderTests):
@@ -210,8 +211,10 @@ class FinderTestsPEP420(FinderTests):
loader_portions = finder.find_loader(name)
return loader_portions[0] if loader_only else loader_portions
-Frozen_FinderTestsPEP420, Source_FinderTestsPEP420 = util.test_both(
- FinderTestsPEP420, machinery=machinery)
+
+(Frozen_FinderTestsPEP420,
+ Source_FinderTestsPEP420
+ ) = util.test_both(FinderTestsPEP420, machinery=machinery)
class FinderTestsPEP302(FinderTests):
@@ -223,9 +226,10 @@ class FinderTestsPEP302(FinderTests):
warnings.simplefilter("ignore", DeprecationWarning)
return finder.find_module(name)
-Frozen_FinderTestsPEP302, Source_FinderTestsPEP302 = util.test_both(
- FinderTestsPEP302, machinery=machinery)
+(Frozen_FinderTestsPEP302,
+ Source_FinderTestsPEP302
+ ) = util.test_both(FinderTestsPEP302, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/source/test_path_hook.py b/Lib/test/test_importlib/source/test_path_hook.py
index 92da772..e6a2415 100644
--- a/Lib/test/test_importlib/source/test_path_hook.py
+++ b/Lib/test/test_importlib/source/test_path_hook.py
@@ -1,5 +1,4 @@
from .. import util
-from . import util as source_util
machinery = util.import_importlib('importlib.machinery')
@@ -15,7 +14,7 @@ class PathHookTest:
self.machinery.SOURCE_SUFFIXES))
def test_success(self):
- with source_util.create_modules('dummy') as mapping:
+ with util.create_modules('dummy') as mapping:
self.assertTrue(hasattr(self.path_hook()(mapping['.root']),
'find_module'))
@@ -23,7 +22,10 @@ class PathHookTest:
# The empty string represents the cwd.
self.assertTrue(hasattr(self.path_hook()(''), 'find_module'))
-Frozen_PathHookTest, Source_PathHooktest = util.test_both(PathHookTest, machinery=machinery)
+
+(Frozen_PathHookTest,
+ Source_PathHooktest
+ ) = util.test_both(PathHookTest, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/source/test_source_encoding.py b/Lib/test/test_importlib/source/test_source_encoding.py
index c62dfa1..b604afb 100644
--- a/Lib/test/test_importlib/source/test_source_encoding.py
+++ b/Lib/test/test_importlib/source/test_source_encoding.py
@@ -1,5 +1,4 @@
from .. import util
-from . import util as source_util
machinery = util.import_importlib('importlib.machinery')
@@ -37,7 +36,7 @@ class EncodingTest:
module_name = '_temp'
def run_test(self, source):
- with source_util.create_modules(self.module_name) as mapping:
+ with util.create_modules(self.module_name) as mapping:
with open(mapping[self.module_name], 'wb') as file:
file.write(source)
loader = self.machinery.SourceFileLoader(self.module_name,
@@ -89,6 +88,7 @@ class EncodingTest:
with self.assertRaises(SyntaxError):
self.run_test(source)
+
class EncodingTestPEP451(EncodingTest):
def load(self, loader):
@@ -97,8 +97,11 @@ class EncodingTestPEP451(EncodingTest):
loader.exec_module(module)
return module
-Frozen_EncodingTestPEP451, Source_EncodingTestPEP451 = util.test_both(
- EncodingTestPEP451, machinery=machinery)
+
+(Frozen_EncodingTestPEP451,
+ Source_EncodingTestPEP451
+ ) = util.test_both(EncodingTestPEP451, machinery=machinery)
+
class EncodingTestPEP302(EncodingTest):
@@ -107,8 +110,10 @@ class EncodingTestPEP302(EncodingTest):
warnings.simplefilter('ignore', DeprecationWarning)
return loader.load_module(self.module_name)
-Frozen_EncodingTestPEP302, Source_EncodingTestPEP302 = util.test_both(
- EncodingTestPEP302, machinery=machinery)
+
+(Frozen_EncodingTestPEP302,
+ Source_EncodingTestPEP302
+ ) = util.test_both(EncodingTestPEP302, machinery=machinery)
class LineEndingTest:
@@ -120,7 +125,7 @@ class LineEndingTest:
module_name = '_temp'
source_lines = [b"a = 42", b"b = -13", b'']
source = line_ending.join(source_lines)
- with source_util.create_modules(module_name) as mapping:
+ with util.create_modules(module_name) as mapping:
with open(mapping[module_name], 'wb') as file:
file.write(source)
loader = self.machinery.SourceFileLoader(module_name,
@@ -139,6 +144,7 @@ class LineEndingTest:
def test_lf(self):
self.run_test(b'\n')
+
class LineEndingTestPEP451(LineEndingTest):
def load(self, loader, module_name):
@@ -147,8 +153,11 @@ class LineEndingTestPEP451(LineEndingTest):
loader.exec_module(module)
return module
-Frozen_LineEndingTestPEP451, Source_LineEndingTestPEP451 = util.test_both(
- LineEndingTestPEP451, machinery=machinery)
+
+(Frozen_LineEndingTestPEP451,
+ Source_LineEndingTestPEP451
+ ) = util.test_both(LineEndingTestPEP451, machinery=machinery)
+
class LineEndingTestPEP302(LineEndingTest):
@@ -157,8 +166,10 @@ class LineEndingTestPEP302(LineEndingTest):
warnings.simplefilter('ignore', DeprecationWarning)
return loader.load_module(module_name)
-Frozen_LineEndingTestPEP302, Source_LineEndingTestPEP302 = util.test_both(
- LineEndingTestPEP302, machinery=machinery)
+
+(Frozen_LineEndingTestPEP302,
+ Source_LineEndingTestPEP302
+ ) = util.test_both(LineEndingTestPEP302, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/source/util.py b/Lib/test/test_importlib/source/util.py
deleted file mode 100644
index 63cd25a..0000000
--- a/Lib/test/test_importlib/source/util.py
+++ /dev/null
@@ -1,96 +0,0 @@
-from .. import util
-import contextlib
-import errno
-import functools
-import os
-import os.path
-import sys
-import tempfile
-from test import support
-
-
-def writes_bytecode_files(fxn):
- """Decorator to protect sys.dont_write_bytecode from mutation and to skip
- tests that require it to be set to False."""
- if sys.dont_write_bytecode:
- return lambda *args, **kwargs: None
- @functools.wraps(fxn)
- def wrapper(*args, **kwargs):
- original = sys.dont_write_bytecode
- sys.dont_write_bytecode = False
- try:
- to_return = fxn(*args, **kwargs)
- finally:
- sys.dont_write_bytecode = original
- return to_return
- return wrapper
-
-
-def ensure_bytecode_path(bytecode_path):
- """Ensure that the __pycache__ directory for PEP 3147 pyc file exists.
-
- :param bytecode_path: File system path to PEP 3147 pyc file.
- """
- try:
- os.mkdir(os.path.dirname(bytecode_path))
- except OSError as error:
- if error.errno != errno.EEXIST:
- raise
-
-
-@contextlib.contextmanager
-def create_modules(*names):
- """Temporarily create each named module with an attribute (named 'attr')
- that contains the name passed into the context manager that caused the
- creation of the module.
-
- All files are created in a temporary directory returned by
- tempfile.mkdtemp(). This directory is inserted at the beginning of
- sys.path. When the context manager exits all created files (source and
- bytecode) are explicitly deleted.
-
- No magic is performed when creating packages! This means that if you create
- a module within a package you must also create the package's __init__ as
- well.
-
- """
- source = 'attr = {0!r}'
- created_paths = []
- mapping = {}
- state_manager = None
- uncache_manager = None
- try:
- temp_dir = tempfile.mkdtemp()
- mapping['.root'] = temp_dir
- import_names = set()
- for name in names:
- if not name.endswith('__init__'):
- import_name = name
- else:
- import_name = name[:-len('.__init__')]
- import_names.add(import_name)
- if import_name in sys.modules:
- del sys.modules[import_name]
- name_parts = name.split('.')
- file_path = temp_dir
- for directory in name_parts[:-1]:
- file_path = os.path.join(file_path, directory)
- if not os.path.exists(file_path):
- os.mkdir(file_path)
- created_paths.append(file_path)
- file_path = os.path.join(file_path, name_parts[-1] + '.py')
- with open(file_path, 'w') as file:
- file.write(source.format(name))
- created_paths.append(file_path)
- mapping[name] = file_path
- uncache_manager = util.uncache(*import_names)
- uncache_manager.__enter__()
- state_manager = util.import_state(path=[temp_dir])
- state_manager.__enter__()
- yield mapping
- finally:
- if state_manager is not None:
- state_manager.__exit__(None, None, None)
- if uncache_manager is not None:
- uncache_manager.__exit__(None, None, None)
- support.rmtree(temp_dir)
diff --git a/Lib/test/test_importlib/test_abc.py b/Lib/test/test_importlib/test_abc.py
index a1f8e76..d4bf915 100644
--- a/Lib/test/test_importlib/test_abc.py
+++ b/Lib/test/test_importlib/test_abc.py
@@ -10,12 +10,13 @@ import unittest
from unittest import mock
import warnings
-from . import util
+from . import util as test_util
+
+init = test_util.import_importlib('importlib')
+abc = test_util.import_importlib('importlib.abc')
+machinery = test_util.import_importlib('importlib.machinery')
+util = test_util.import_importlib('importlib.util')
-frozen_init, source_init = util.import_importlib('importlib')
-frozen_abc, source_abc = util.import_importlib('importlib.abc')
-machinery = util.import_importlib('importlib.machinery')
-frozen_util, source_util = util.import_importlib('importlib.util')
##### Inheritance ##############################################################
class InheritanceTests:
@@ -26,8 +27,7 @@ class InheritanceTests:
subclasses = []
superclasses = []
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
+ def setUp(self):
self.superclasses = [getattr(self.abc, class_name)
for class_name in self.superclass_names]
if hasattr(self, 'subclass_names'):
@@ -36,11 +36,11 @@ class InheritanceTests:
# checking across module boundaries (i.e. the _bootstrap in abc is
# not the same as the one in machinery). That means stealing one of
# the modules from the other to make sure the same instance is used.
- self.subclasses = [getattr(self.abc.machinery, class_name)
- for class_name in self.subclass_names]
+ machinery = self.abc.machinery
+ self.subclasses = [getattr(machinery, class_name)
+ for class_name in self.subclass_names]
assert self.subclasses or self.superclasses, self.__class__
- testing = self.__class__.__name__.partition('_')[2]
- self.__test = getattr(self.abc, testing)
+ self.__test = getattr(self.abc, self._NAME)
def test_subclasses(self):
# Test that the expected subclasses inherit.
@@ -54,94 +54,97 @@ class InheritanceTests:
self.assertTrue(issubclass(self.__test, superclass),
"{0} is not a superclass of {1}".format(superclass, self.__test))
-def create_inheritance_tests(base_class):
- def set_frozen(ns):
- ns['abc'] = frozen_abc
- def set_source(ns):
- ns['abc'] = source_abc
-
- classes = []
- for prefix, ns_set in [('Frozen', set_frozen), ('Source', set_source)]:
- classes.append(types.new_class('_'.join([prefix, base_class.__name__]),
- (base_class, unittest.TestCase),
- exec_body=ns_set))
- return classes
-
class MetaPathFinder(InheritanceTests):
superclass_names = ['Finder']
subclass_names = ['BuiltinImporter', 'FrozenImporter', 'PathFinder',
'WindowsRegistryFinder']
-tests = create_inheritance_tests(MetaPathFinder)
-Frozen_MetaPathFinderInheritanceTests, Source_MetaPathFinderInheritanceTests = tests
+
+(Frozen_MetaPathFinderInheritanceTests,
+ Source_MetaPathFinderInheritanceTests
+ ) = test_util.test_both(MetaPathFinder, abc=abc)
class PathEntryFinder(InheritanceTests):
superclass_names = ['Finder']
subclass_names = ['FileFinder']
-tests = create_inheritance_tests(PathEntryFinder)
-Frozen_PathEntryFinderInheritanceTests, Source_PathEntryFinderInheritanceTests = tests
+
+(Frozen_PathEntryFinderInheritanceTests,
+ Source_PathEntryFinderInheritanceTests
+ ) = test_util.test_both(PathEntryFinder, abc=abc)
class ResourceLoader(InheritanceTests):
superclass_names = ['Loader']
-tests = create_inheritance_tests(ResourceLoader)
-Frozen_ResourceLoaderInheritanceTests, Source_ResourceLoaderInheritanceTests = tests
+
+(Frozen_ResourceLoaderInheritanceTests,
+ Source_ResourceLoaderInheritanceTests
+ ) = test_util.test_both(ResourceLoader, abc=abc)
class InspectLoader(InheritanceTests):
superclass_names = ['Loader']
subclass_names = ['BuiltinImporter', 'FrozenImporter', 'ExtensionFileLoader']
-tests = create_inheritance_tests(InspectLoader)
-Frozen_InspectLoaderInheritanceTests, Source_InspectLoaderInheritanceTests = tests
+
+(Frozen_InspectLoaderInheritanceTests,
+ Source_InspectLoaderInheritanceTests
+ ) = test_util.test_both(InspectLoader, abc=abc)
class ExecutionLoader(InheritanceTests):
superclass_names = ['InspectLoader']
subclass_names = ['ExtensionFileLoader']
-tests = create_inheritance_tests(ExecutionLoader)
-Frozen_ExecutionLoaderInheritanceTests, Source_ExecutionLoaderInheritanceTests = tests
+
+(Frozen_ExecutionLoaderInheritanceTests,
+ Source_ExecutionLoaderInheritanceTests
+ ) = test_util.test_both(ExecutionLoader, abc=abc)
class FileLoader(InheritanceTests):
superclass_names = ['ResourceLoader', 'ExecutionLoader']
subclass_names = ['SourceFileLoader', 'SourcelessFileLoader']
-tests = create_inheritance_tests(FileLoader)
-Frozen_FileLoaderInheritanceTests, Source_FileLoaderInheritanceTests = tests
+
+(Frozen_FileLoaderInheritanceTests,
+ Source_FileLoaderInheritanceTests
+ ) = test_util.test_both(FileLoader, abc=abc)
class SourceLoader(InheritanceTests):
superclass_names = ['ResourceLoader', 'ExecutionLoader']
subclass_names = ['SourceFileLoader']
-tests = create_inheritance_tests(SourceLoader)
-Frozen_SourceLoaderInheritanceTests, Source_SourceLoaderInheritanceTests = tests
+
+(Frozen_SourceLoaderInheritanceTests,
+ Source_SourceLoaderInheritanceTests
+ ) = test_util.test_both(SourceLoader, abc=abc)
+
##### Default return values ####################################################
-def make_abc_subclasses(base_class):
- classes = []
- for kind, abc in [('Frozen', frozen_abc), ('Source', source_abc)]:
- name = '_'.join([kind, base_class.__name__])
- base_classes = base_class, getattr(abc, base_class.__name__)
- classes.append(types.new_class(name, base_classes))
- return classes
-
-def make_return_value_tests(base_class, test_class):
- frozen_class, source_class = make_abc_subclasses(base_class)
- tests = []
- for prefix, class_in_test in [('Frozen', frozen_class), ('Source', source_class)]:
- def set_ns(ns):
- ns['ins'] = class_in_test()
- tests.append(types.new_class('_'.join([prefix, test_class.__name__]),
- (test_class, unittest.TestCase),
- exec_body=set_ns))
- return tests
+
+def make_abc_subclasses(base_class, name=None, inst=False, **kwargs):
+ if name is None:
+ name = base_class.__name__
+ base = {kind: getattr(splitabc, name)
+ for kind, splitabc in abc.items()}
+ return {cls._KIND: cls() if inst else cls
+ for cls in test_util.split_frozen(base_class, base, **kwargs)}
+
+
+class ABCTestHarness:
+
+ @property
+ def ins(self):
+ # Lazily set ins on the class.
+ cls = self.SPLIT[self._KIND]
+ ins = cls()
+ self.__class__.ins = ins
+ return ins
class MetaPathFinder:
@@ -149,10 +152,10 @@ class MetaPathFinder:
def find_module(self, fullname, path):
return super().find_module(fullname, path)
-Frozen_MPF, Source_MPF = make_abc_subclasses(MetaPathFinder)
+class MetaPathFinderDefaultsTests(ABCTestHarness):
-class MetaPathFinderDefaultsTests:
+ SPLIT = make_abc_subclasses(MetaPathFinder)
def test_find_module(self):
# Default should return None.
@@ -163,8 +166,9 @@ class MetaPathFinderDefaultsTests:
self.ins.invalidate_caches()
-tests = make_return_value_tests(MetaPathFinder, MetaPathFinderDefaultsTests)
-Frozen_MPFDefaultTests, Source_MPFDefaultTests = tests
+(Frozen_MPFDefaultTests,
+ Source_MPFDefaultTests
+ ) = test_util.test_both(MetaPathFinderDefaultsTests)
class PathEntryFinder:
@@ -172,10 +176,10 @@ class PathEntryFinder:
def find_loader(self, fullname):
return super().find_loader(fullname)
-Frozen_PEF, Source_PEF = make_abc_subclasses(PathEntryFinder)
+class PathEntryFinderDefaultsTests(ABCTestHarness):
-class PathEntryFinderDefaultsTests:
+ SPLIT = make_abc_subclasses(PathEntryFinder)
def test_find_loader(self):
self.assertEqual((None, []), self.ins.find_loader('something'))
@@ -188,8 +192,9 @@ class PathEntryFinderDefaultsTests:
self.ins.invalidate_caches()
-tests = make_return_value_tests(PathEntryFinder, PathEntryFinderDefaultsTests)
-Frozen_PEFDefaultTests, Source_PEFDefaultTests = tests
+(Frozen_PEFDefaultTests,
+ Source_PEFDefaultTests
+ ) = test_util.test_both(PathEntryFinderDefaultsTests)
class Loader:
@@ -198,10 +203,9 @@ class Loader:
return super().load_module(fullname)
-Frozen_L, Source_L = make_abc_subclasses(Loader)
+class LoaderDefaultsTests(ABCTestHarness):
-
-class LoaderDefaultsTests:
+ SPLIT = make_abc_subclasses(Loader)
def test_load_module(self):
with self.assertRaises(ImportError):
@@ -217,8 +221,9 @@ class LoaderDefaultsTests:
self.assertTrue(repr(mod))
-tests = make_return_value_tests(Loader, LoaderDefaultsTests)
-Frozen_LDefaultTests, SourceLDefaultTests = tests
+(Frozen_LDefaultTests,
+ SourceLDefaultTests
+ ) = test_util.test_both(LoaderDefaultsTests)
class ResourceLoader(Loader):
@@ -227,18 +232,18 @@ class ResourceLoader(Loader):
return super().get_data(path)
-Frozen_RL, Source_RL = make_abc_subclasses(ResourceLoader)
-
+class ResourceLoaderDefaultsTests(ABCTestHarness):
-class ResourceLoaderDefaultsTests:
+ SPLIT = make_abc_subclasses(ResourceLoader)
def test_get_data(self):
with self.assertRaises(IOError):
self.ins.get_data('/some/path')
-tests = make_return_value_tests(ResourceLoader, ResourceLoaderDefaultsTests)
-Frozen_RLDefaultTests, Source_RLDefaultTests = tests
+(Frozen_RLDefaultTests,
+ Source_RLDefaultTests
+ ) = test_util.test_both(ResourceLoaderDefaultsTests)
class InspectLoader(Loader):
@@ -250,10 +255,12 @@ class InspectLoader(Loader):
return super().get_source(fullname)
-Frozen_IL, Source_IL = make_abc_subclasses(InspectLoader)
+SPLIT_IL = make_abc_subclasses(InspectLoader)
-class InspectLoaderDefaultsTests:
+class InspectLoaderDefaultsTests(ABCTestHarness):
+
+ SPLIT = SPLIT_IL
def test_is_package(self):
with self.assertRaises(ImportError):
@@ -264,8 +271,9 @@ class InspectLoaderDefaultsTests:
self.ins.get_source('blah')
-tests = make_return_value_tests(InspectLoader, InspectLoaderDefaultsTests)
-Frozen_ILDefaultTests, Source_ILDefaultTests = tests
+(Frozen_ILDefaultTests,
+ Source_ILDefaultTests
+ ) = test_util.test_both(InspectLoaderDefaultsTests)
class ExecutionLoader(InspectLoader):
@@ -273,21 +281,25 @@ class ExecutionLoader(InspectLoader):
def get_filename(self, fullname):
return super().get_filename(fullname)
-Frozen_EL, Source_EL = make_abc_subclasses(ExecutionLoader)
+
+SPLIT_EL = make_abc_subclasses(ExecutionLoader)
-class ExecutionLoaderDefaultsTests:
+class ExecutionLoaderDefaultsTests(ABCTestHarness):
+
+ SPLIT = SPLIT_EL
def test_get_filename(self):
with self.assertRaises(ImportError):
self.ins.get_filename('blah')
-tests = make_return_value_tests(ExecutionLoader, InspectLoaderDefaultsTests)
-Frozen_ELDefaultTests, Source_ELDefaultsTests = tests
+(Frozen_ELDefaultTests,
+ Source_ELDefaultsTests
+ ) = test_util.test_both(InspectLoaderDefaultsTests)
-##### MetaPathFinder concrete methods ##########################################
+##### MetaPathFinder concrete methods ##########################################
class MetaPathFinderFindModuleTests:
@classmethod
@@ -317,13 +329,12 @@ class MetaPathFinderFindModuleTests:
self.assertIs(found, spec.loader)
-Frozen_MPFFindModuleTests, Source_MPFFindModuleTests = util.test_both(
- MetaPathFinderFindModuleTests,
- abc=(frozen_abc, source_abc),
- util=(frozen_util, source_util))
+(Frozen_MPFFindModuleTests,
+ Source_MPFFindModuleTests
+ ) = test_util.test_both(MetaPathFinderFindModuleTests, abc=abc, util=util)
-##### PathEntryFinder concrete methods #########################################
+##### PathEntryFinder concrete methods #########################################
class PathEntryFinderFindLoaderTests:
@classmethod
@@ -361,11 +372,10 @@ class PathEntryFinderFindLoaderTests:
self.assertEqual(paths, found[1])
-Frozen_PEFFindLoaderTests, Source_PEFFindLoaderTests = util.test_both(
- PathEntryFinderFindLoaderTests,
- abc=(frozen_abc, source_abc),
- machinery=machinery,
- util=(frozen_util, source_util))
+(Frozen_PEFFindLoaderTests,
+ Source_PEFFindLoaderTests
+ ) = test_util.test_both(PathEntryFinderFindLoaderTests, abc=abc, util=util,
+ machinery=machinery)
##### Loader concrete methods ##################################################
@@ -386,7 +396,7 @@ class LoaderLoadModuleTests:
def test_fresh(self):
loader = self.loader()
name = 'blah'
- with util.uncache(name):
+ with test_util.uncache(name):
loader.load_module(name)
module = loader.found
self.assertIs(sys.modules[name], module)
@@ -404,7 +414,7 @@ class LoaderLoadModuleTests:
module = types.ModuleType(name)
module.__spec__ = self.util.spec_from_loader(name, loader)
module.__loader__ = loader
- with util.uncache(name):
+ with test_util.uncache(name):
sys.modules[name] = module
loader.load_module(name)
found = loader.found
@@ -412,10 +422,9 @@ class LoaderLoadModuleTests:
self.assertIs(module, sys.modules[name])
-Frozen_LoaderLoadModuleTests, Source_LoaderLoadModuleTests = util.test_both(
- LoaderLoadModuleTests,
- abc=(frozen_abc, source_abc),
- util=(frozen_util, source_util))
+(Frozen_LoaderLoadModuleTests,
+ Source_LoaderLoadModuleTests
+ ) = test_util.test_both(LoaderLoadModuleTests, abc=abc, util=util)
##### InspectLoader concrete methods ###########################################
@@ -461,11 +470,10 @@ class InspectLoaderSourceToCodeTests:
self.assertEqual(code.co_filename, '<string>')
-class Frozen_ILSourceToCodeTests(InspectLoaderSourceToCodeTests, unittest.TestCase):
- InspectLoaderSubclass = Frozen_IL
-
-class Source_ILSourceToCodeTests(InspectLoaderSourceToCodeTests, unittest.TestCase):
- InspectLoaderSubclass = Source_IL
+(Frozen_ILSourceToCodeTests,
+ Source_ILSourceToCodeTests
+ ) = test_util.test_both(InspectLoaderSourceToCodeTests,
+ InspectLoaderSubclass=SPLIT_IL)
class InspectLoaderGetCodeTests:
@@ -495,11 +503,10 @@ class InspectLoaderGetCodeTests:
loader.get_code('blah')
-class Frozen_ILGetCodeTests(InspectLoaderGetCodeTests, unittest.TestCase):
- InspectLoaderSubclass = Frozen_IL
-
-class Source_ILGetCodeTests(InspectLoaderGetCodeTests, unittest.TestCase):
- InspectLoaderSubclass = Source_IL
+(Frozen_ILGetCodeTests,
+ Source_ILGetCodeTests
+ ) = test_util.test_both(InspectLoaderGetCodeTests,
+ InspectLoaderSubclass=SPLIT_IL)
class InspectLoaderLoadModuleTests:
@@ -543,11 +550,10 @@ class InspectLoaderLoadModuleTests:
self.assertEqual(module, sys.modules[self.module_name])
-class Frozen_ILLoadModuleTests(InspectLoaderLoadModuleTests, unittest.TestCase):
- InspectLoaderSubclass = Frozen_IL
-
-class Source_ILLoadModuleTests(InspectLoaderLoadModuleTests, unittest.TestCase):
- InspectLoaderSubclass = Source_IL
+(Frozen_ILLoadModuleTests,
+ Source_ILLoadModuleTests
+ ) = test_util.test_both(InspectLoaderLoadModuleTests,
+ InspectLoaderSubclass=SPLIT_IL)
##### ExecutionLoader concrete methods #########################################
@@ -608,15 +614,14 @@ class ExecutionLoaderGetCodeTests:
self.assertEqual(module.attr, 42)
-class Frozen_ELGetCodeTests(ExecutionLoaderGetCodeTests, unittest.TestCase):
- ExecutionLoaderSubclass = Frozen_EL
-
-class Source_ELGetCodeTests(ExecutionLoaderGetCodeTests, unittest.TestCase):
- ExecutionLoaderSubclass = Source_EL
+(Frozen_ELGetCodeTests,
+ Source_ELGetCodeTests
+ ) = test_util.test_both(ExecutionLoaderGetCodeTests,
+ ExecutionLoaderSubclass=SPLIT_EL)
##### SourceLoader concrete methods ############################################
-class SourceLoader:
+class SourceOnlyLoader:
# Globals that should be defined for all modules.
source = (b"_ = '::'.join([__name__, __file__, __cached__, __package__, "
@@ -637,10 +642,10 @@ class SourceLoader:
return '<module>'
-Frozen_SourceOnlyL, Source_SourceOnlyL = make_abc_subclasses(SourceLoader)
+SPLIT_SOL = make_abc_subclasses(SourceOnlyLoader, 'SourceLoader')
-class SourceLoader(SourceLoader):
+class SourceLoader(SourceOnlyLoader):
source_mtime = 1
@@ -677,11 +682,7 @@ class SourceLoader(SourceLoader):
return path == self.bytecode_path
-Frozen_SL, Source_SL = make_abc_subclasses(SourceLoader)
-Frozen_SL.util = frozen_util
-Source_SL.util = source_util
-Frozen_SL.init = frozen_init
-Source_SL.init = source_init
+SPLIT_SL = make_abc_subclasses(SourceLoader, util=util, init=init)
class SourceLoaderTestHarness:
@@ -765,7 +766,7 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
# Loading a module should set __name__, __loader__, __package__,
# __path__ (for packages), __file__, and __cached__.
# The module should also be put into sys.modules.
- with util.uncache(self.name):
+ with test_util.uncache(self.name):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
module = self.loader.load_module(self.name)
@@ -778,7 +779,7 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
# is a package.
# Testing the values for a package are covered by test_load_module.
self.setUp(is_package=False)
- with util.uncache(self.name):
+ with test_util.uncache(self.name):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
module = self.loader.load_module(self.name)
@@ -798,13 +799,10 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
self.assertEqual(returned_source, source)
-class Frozen_SourceOnlyLTests(SourceOnlyLoaderTests, unittest.TestCase):
- loader_mock = Frozen_SourceOnlyL
- util = frozen_util
-
-class Source_SourceOnlyLTests(SourceOnlyLoaderTests, unittest.TestCase):
- loader_mock = Source_SourceOnlyL
- util = source_util
+(Frozen_SourceOnlyLoaderTests,
+ Source_SourceOnlyLoaderTests
+ ) = test_util.test_both(SourceOnlyLoaderTests, util=util,
+ loader_mock=SPLIT_SOL)
@unittest.skipIf(sys.dont_write_bytecode, "sys.dont_write_bytecode is true")
@@ -896,15 +894,10 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
self.verify_code(code_object)
-class Frozen_SLBytecodeTests(SourceLoaderBytecodeTests, unittest.TestCase):
- loader_mock = Frozen_SL
- init = frozen_init
- util = frozen_util
-
-class SourceSLBytecodeTests(SourceLoaderBytecodeTests, unittest.TestCase):
- loader_mock = Source_SL
- init = source_init
- util = source_util
+(Frozen_SLBytecodeTests,
+ SourceSLBytecodeTests
+ ) = test_util.test_both(SourceLoaderBytecodeTests, init=init, util=util,
+ loader_mock=SPLIT_SL)
class SourceLoaderGetSourceTests:
@@ -940,11 +933,10 @@ class SourceLoaderGetSourceTests:
self.assertEqual(mock.get_source(name), expect)
-class Frozen_SourceOnlyLGetSourceTests(SourceLoaderGetSourceTests, unittest.TestCase):
- SourceOnlyLoaderMock = Frozen_SourceOnlyL
-
-class Source_SourceOnlyLGetSourceTests(SourceLoaderGetSourceTests, unittest.TestCase):
- SourceOnlyLoaderMock = Source_SourceOnlyL
+(Frozen_SourceOnlyLoaderGetSourceTests,
+ Source_SourceOnlyLoaderGetSourceTests
+ ) = test_util.test_both(SourceLoaderGetSourceTests,
+ SourceOnlyLoaderMock=SPLIT_SOL)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py
index 2a2d42b..6bc3c56 100644
--- a/Lib/test/test_importlib/test_api.py
+++ b/Lib/test/test_importlib/test_api.py
@@ -1,8 +1,8 @@
-from . import util
+from . import util as test_util
-frozen_init, source_init = util.import_importlib('importlib')
-frozen_util, source_util = util.import_importlib('importlib.util')
-frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
+init = test_util.import_importlib('importlib')
+util = test_util.import_importlib('importlib.util')
+machinery = test_util.import_importlib('importlib.machinery')
import os.path
import sys
@@ -18,8 +18,8 @@ class ImportModuleTests:
def test_module_import(self):
# Test importing a top-level module.
- with util.mock_modules('top_level') as mock:
- with util.import_state(meta_path=[mock]):
+ with test_util.mock_modules('top_level') as mock:
+ with test_util.import_state(meta_path=[mock]):
module = self.init.import_module('top_level')
self.assertEqual(module.__name__, 'top_level')
@@ -28,8 +28,8 @@ class ImportModuleTests:
pkg_name = 'pkg'
pkg_long_name = '{0}.__init__'.format(pkg_name)
name = '{0}.mod'.format(pkg_name)
- with util.mock_modules(pkg_long_name, name) as mock:
- with util.import_state(meta_path=[mock]):
+ with test_util.mock_modules(pkg_long_name, name) as mock:
+ with test_util.import_state(meta_path=[mock]):
module = self.init.import_module(name)
self.assertEqual(module.__name__, name)
@@ -40,16 +40,16 @@ class ImportModuleTests:
module_name = 'mod'
absolute_name = '{0}.{1}'.format(pkg_name, module_name)
relative_name = '.{0}'.format(module_name)
- with util.mock_modules(pkg_long_name, absolute_name) as mock:
- with util.import_state(meta_path=[mock]):
+ with test_util.mock_modules(pkg_long_name, absolute_name) as mock:
+ with test_util.import_state(meta_path=[mock]):
self.init.import_module(pkg_name)
module = self.init.import_module(relative_name, pkg_name)
self.assertEqual(module.__name__, absolute_name)
def test_deep_relative_package_import(self):
modules = ['a.__init__', 'a.b.__init__', 'a.c']
- with util.mock_modules(*modules) as mock:
- with util.import_state(meta_path=[mock]):
+ with test_util.mock_modules(*modules) as mock:
+ with test_util.import_state(meta_path=[mock]):
self.init.import_module('a')
self.init.import_module('a.b')
module = self.init.import_module('..c', 'a.b')
@@ -61,8 +61,8 @@ class ImportModuleTests:
pkg_name = 'pkg'
pkg_long_name = '{0}.__init__'.format(pkg_name)
name = '{0}.mod'.format(pkg_name)
- with util.mock_modules(pkg_long_name, name) as mock:
- with util.import_state(meta_path=[mock]):
+ with test_util.mock_modules(pkg_long_name, name) as mock:
+ with test_util.import_state(meta_path=[mock]):
self.init.import_module(pkg_name)
module = self.init.import_module(name, pkg_name)
self.assertEqual(module.__name__, name)
@@ -86,16 +86,15 @@ class ImportModuleTests:
b_load_count += 1
code = {'a': load_a, 'a.b': load_b}
modules = ['a.__init__', 'a.b']
- with util.mock_modules(*modules, module_code=code) as mock:
- with util.import_state(meta_path=[mock]):
+ with test_util.mock_modules(*modules, module_code=code) as mock:
+ with test_util.import_state(meta_path=[mock]):
self.init.import_module('a.b')
self.assertEqual(b_load_count, 1)
-class Frozen_ImportModuleTests(ImportModuleTests, unittest.TestCase):
- init = frozen_init
-class Source_ImportModuleTests(ImportModuleTests, unittest.TestCase):
- init = source_init
+(Frozen_ImportModuleTests,
+ Source_ImportModuleTests
+ ) = test_util.test_both(ImportModuleTests, init=init)
class FindLoaderTests:
@@ -107,7 +106,7 @@ class FindLoaderTests:
def test_sys_modules(self):
# If a module with __loader__ is in sys.modules, then return it.
name = 'some_mod'
- with util.uncache(name):
+ with test_util.uncache(name):
module = types.ModuleType(name)
loader = 'a loader!'
module.__loader__ = loader
@@ -120,7 +119,7 @@ class FindLoaderTests:
def test_sys_modules_loader_is_None(self):
# If sys.modules[name].__loader__ is None, raise ValueError.
name = 'some_mod'
- with util.uncache(name):
+ with test_util.uncache(name):
module = types.ModuleType(name)
module.__loader__ = None
sys.modules[name] = module
@@ -133,7 +132,7 @@ class FindLoaderTests:
# Should raise ValueError
# Issue #17099
name = 'some_mod'
- with util.uncache(name):
+ with test_util.uncache(name):
module = types.ModuleType(name)
try:
del module.__loader__
@@ -148,8 +147,8 @@ class FindLoaderTests:
def test_success(self):
# Return the loader found on sys.meta_path.
name = 'some_mod'
- with util.uncache(name):
- with util.import_state(meta_path=[self.FakeMetaFinder]):
+ with test_util.uncache(name):
+ with test_util.import_state(meta_path=[self.FakeMetaFinder]):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
self.assertEqual((name, None), self.init.find_loader(name))
@@ -158,8 +157,8 @@ class FindLoaderTests:
# Searching on a path should work.
name = 'some_mod'
path = 'path to some place'
- with util.uncache(name):
- with util.import_state(meta_path=[self.FakeMetaFinder]):
+ with test_util.uncache(name):
+ with test_util.import_state(meta_path=[self.FakeMetaFinder]):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
self.assertEqual((name, path),
@@ -171,11 +170,10 @@ class FindLoaderTests:
warnings.simplefilter('ignore', DeprecationWarning)
self.assertIsNone(self.init.find_loader('nevergoingtofindthismodule'))
-class Frozen_FindLoaderTests(FindLoaderTests, unittest.TestCase):
- init = frozen_init
-class Source_FindLoaderTests(FindLoaderTests, unittest.TestCase):
- init = source_init
+(Frozen_FindLoaderTests,
+ Source_FindLoaderTests
+ ) = test_util.test_both(FindLoaderTests, init=init)
class ReloadTests:
@@ -195,10 +193,10 @@ class ReloadTests:
module = type(sys)('top_level')
module.spam = 3
sys.modules['top_level'] = module
- mock = util.mock_modules('top_level',
- module_code={'top_level': code})
+ mock = test_util.mock_modules('top_level',
+ module_code={'top_level': code})
with mock:
- with util.import_state(meta_path=[mock]):
+ with test_util.import_state(meta_path=[mock]):
module = self.init.import_module('top_level')
reloaded = self.init.reload(module)
actual = sys.modules['top_level']
@@ -230,7 +228,7 @@ class ReloadTests:
def test_reload_location_changed(self):
name = 'spam'
with support.temp_cwd(None) as cwd:
- with util.uncache('spam'):
+ with test_util.uncache('spam'):
with support.DirsOnSysPath(cwd):
# Start as a plain module.
self.init.invalidate_caches()
@@ -281,7 +279,7 @@ class ReloadTests:
def test_reload_namespace_changed(self):
name = 'spam'
with support.temp_cwd(None) as cwd:
- with util.uncache('spam'):
+ with test_util.uncache('spam'):
with support.DirsOnSysPath(cwd):
# Start as a namespace package.
self.init.invalidate_caches()
@@ -338,20 +336,16 @@ class ReloadTests:
# See #19851.
name = 'spam'
subname = 'ham'
- with util.temp_module(name, pkg=True) as pkg_dir:
- fullname, _ = util.submodule(name, subname, pkg_dir)
+ with test_util.temp_module(name, pkg=True) as pkg_dir:
+ fullname, _ = test_util.submodule(name, subname, pkg_dir)
ham = self.init.import_module(fullname)
reloaded = self.init.reload(ham)
self.assertIs(reloaded, ham)
-class Frozen_ReloadTests(ReloadTests, unittest.TestCase):
- init = frozen_init
- util = frozen_util
-
-class Source_ReloadTests(ReloadTests, unittest.TestCase):
- init = source_init
- util = source_util
+(Frozen_ReloadTests,
+ Source_ReloadTests
+ ) = test_util.test_both(ReloadTests, init=init, util=util)
class InvalidateCacheTests:
@@ -384,11 +378,10 @@ class InvalidateCacheTests:
self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key))
self.init.invalidate_caches() # Shouldn't trigger an exception.
-class Frozen_InvalidateCacheTests(InvalidateCacheTests, unittest.TestCase):
- init = frozen_init
-class Source_InvalidateCacheTests(InvalidateCacheTests, unittest.TestCase):
- init = source_init
+(Frozen_InvalidateCacheTests,
+ Source_InvalidateCacheTests
+ ) = test_util.test_both(InvalidateCacheTests, init=init)
class FrozenImportlibTests(unittest.TestCase):
@@ -398,6 +391,7 @@ class FrozenImportlibTests(unittest.TestCase):
# Can't do an isinstance() check since separate copies of importlib
# may have been used for import, so just check the name is not for the
# frozen loader.
+ source_init = init['Source']
self.assertNotEqual(source_init.__loader__.__class__.__name__,
'FrozenImporter')
@@ -426,11 +420,10 @@ class StartupTests:
elif self.machinery.FrozenImporter.find_module(name):
self.assertIsNot(module.__spec__, None)
-class Frozen_StartupTests(StartupTests, unittest.TestCase):
- machinery = frozen_machinery
-class Source_StartupTests(StartupTests, unittest.TestCase):
- machinery = source_machinery
+(Frozen_StartupTests,
+ Source_StartupTests
+ ) = test_util.test_both(StartupTests, machinery=machinery)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/test_lazy.py b/Lib/test/test_importlib/test_lazy.py
new file mode 100644
index 0000000..2e191bb
--- /dev/null
+++ b/Lib/test/test_importlib/test_lazy.py
@@ -0,0 +1,132 @@
+import importlib
+from importlib import abc
+from importlib import util
+import unittest
+
+from . import util as test_util
+
+
+class CollectInit:
+
+ def __init__(self, *args, **kwargs):
+ self.args = args
+ self.kwargs = kwargs
+
+ def exec_module(self, module):
+ return self
+
+
+class LazyLoaderFactoryTests(unittest.TestCase):
+
+ def test_init(self):
+ factory = util.LazyLoader.factory(CollectInit)
+ # E.g. what importlib.machinery.FileFinder instantiates loaders with
+ # plus keyword arguments.
+ lazy_loader = factory('module name', 'module path', kw='kw')
+ loader = lazy_loader.loader
+ self.assertEqual(('module name', 'module path'), loader.args)
+ self.assertEqual({'kw': 'kw'}, loader.kwargs)
+
+ def test_validation(self):
+ # No exec_module(), no lazy loading.
+ with self.assertRaises(TypeError):
+ util.LazyLoader.factory(object)
+
+
+class TestingImporter(abc.MetaPathFinder, abc.Loader):
+
+ module_name = 'lazy_loader_test'
+ mutated_name = 'changed'
+ loaded = None
+ source_code = 'attr = 42; __name__ = {!r}'.format(mutated_name)
+
+ def find_spec(self, name, path, target=None):
+ if name != self.module_name:
+ return None
+ return util.spec_from_loader(name, util.LazyLoader(self))
+
+ def exec_module(self, module):
+ exec(self.source_code, module.__dict__)
+ self.loaded = module
+
+
+class LazyLoaderTests(unittest.TestCase):
+
+ def test_init(self):
+ with self.assertRaises(TypeError):
+ util.LazyLoader(object)
+
+ def new_module(self, source_code=None):
+ loader = TestingImporter()
+ if source_code is not None:
+ loader.source_code = source_code
+ spec = util.spec_from_loader(TestingImporter.module_name,
+ util.LazyLoader(loader))
+ module = spec.loader.create_module(spec)
+ module.__spec__ = spec
+ module.__loader__ = spec.loader
+ spec.loader.exec_module(module)
+ # Module is now lazy.
+ self.assertIsNone(loader.loaded)
+ return module
+
+ def test_e2e(self):
+ # End-to-end test to verify the load is in fact lazy.
+ importer = TestingImporter()
+ assert importer.loaded is None
+ with test_util.uncache(importer.module_name):
+ with test_util.import_state(meta_path=[importer]):
+ module = importlib.import_module(importer.module_name)
+ self.assertIsNone(importer.loaded)
+ # Trigger load.
+ self.assertEqual(module.__loader__, importer)
+ self.assertIsNotNone(importer.loaded)
+ self.assertEqual(module, importer.loaded)
+
+ def test_attr_unchanged(self):
+ # An attribute only mutated as a side-effect of import should not be
+ # changed needlessly.
+ module = self.new_module()
+ self.assertEqual(TestingImporter.mutated_name, module.__name__)
+
+ def test_new_attr(self):
+ # A new attribute should persist.
+ module = self.new_module()
+ module.new_attr = 42
+ self.assertEqual(42, module.new_attr)
+
+ def test_mutated_preexisting_attr(self):
+ # Changing an attribute that already existed on the module --
+ # e.g. __name__ -- should persist.
+ module = self.new_module()
+ module.__name__ = 'bogus'
+ self.assertEqual('bogus', module.__name__)
+
+ def test_mutated_attr(self):
+ # Changing an attribute that comes into existence after an import
+ # should persist.
+ module = self.new_module()
+ module.attr = 6
+ self.assertEqual(6, module.attr)
+
+ def test_delete_eventual_attr(self):
+ # Deleting an attribute should stay deleted.
+ module = self.new_module()
+ del module.attr
+ self.assertFalse(hasattr(module, 'attr'))
+
+ def test_delete_preexisting_attr(self):
+ module = self.new_module()
+ del module.__name__
+ self.assertFalse(hasattr(module, '__name__'))
+
+ def test_module_substitution_error(self):
+ source_code = 'import sys; sys.modules[__name__] = 42'
+ module = self.new_module(source_code)
+ with test_util.uncache(TestingImporter.module_name):
+ with self.assertRaises(ValueError):
+ module.__name__
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/Lib/test/test_importlib/test_locks.py b/Lib/test/test_importlib/test_locks.py
index dc97ba1..df0af12 100644
--- a/Lib/test/test_importlib/test_locks.py
+++ b/Lib/test/test_importlib/test_locks.py
@@ -1,7 +1,6 @@
-from . import util
-frozen_init, source_init = util.import_importlib('importlib')
-frozen_bootstrap = frozen_init._bootstrap
-source_bootstrap = source_init._bootstrap
+from . import util as test_util
+
+init = test_util.import_importlib('importlib')
import sys
import time
@@ -32,14 +31,20 @@ if threading is not None:
test_timeout = None
# _release_save() unsupported
test_release_save_unacquired = None
+ # lock status in repr unsupported
+ test_repr = None
+ test_locked_repr = None
- class Frozen_ModuleLockAsRLockTests(ModuleLockAsRLockTests, lock_tests.RLockTests):
- LockType = frozen_bootstrap._ModuleLock
-
- class Source_ModuleLockAsRLockTests(ModuleLockAsRLockTests, lock_tests.RLockTests):
- LockType = source_bootstrap._ModuleLock
+ LOCK_TYPES = {kind: splitinit._bootstrap._ModuleLock
+ for kind, splitinit in init.items()}
+ (Frozen_ModuleLockAsRLockTests,
+ Source_ModuleLockAsRLockTests
+ ) = test_util.test_both(ModuleLockAsRLockTests, lock_tests.RLockTests,
+ LockType=LOCK_TYPES)
else:
+ LOCK_TYPES = {}
+
class Frozen_ModuleLockAsRLockTests(unittest.TestCase):
pass
@@ -47,78 +52,94 @@ else:
pass
-class DeadlockAvoidanceTests:
-
- def setUp(self):
- try:
- self.old_switchinterval = sys.getswitchinterval()
- sys.setswitchinterval(0.000001)
- except AttributeError:
- self.old_switchinterval = None
-
- def tearDown(self):
- if self.old_switchinterval is not None:
- sys.setswitchinterval(self.old_switchinterval)
-
- def run_deadlock_avoidance_test(self, create_deadlock):
- NLOCKS = 10
- locks = [self.LockType(str(i)) for i in range(NLOCKS)]
- pairs = [(locks[i], locks[(i+1)%NLOCKS]) for i in range(NLOCKS)]
- if create_deadlock:
- NTHREADS = NLOCKS
- else:
- NTHREADS = NLOCKS - 1
- barrier = threading.Barrier(NTHREADS)
- results = []
- def _acquire(lock):
- """Try to acquire the lock. Return True on success, False on deadlock."""
+if threading is not None:
+ class DeadlockAvoidanceTests:
+
+ def setUp(self):
try:
- lock.acquire()
- except self.DeadlockError:
- return False
+ self.old_switchinterval = sys.getswitchinterval()
+ sys.setswitchinterval(0.000001)
+ except AttributeError:
+ self.old_switchinterval = None
+
+ def tearDown(self):
+ if self.old_switchinterval is not None:
+ sys.setswitchinterval(self.old_switchinterval)
+
+ def run_deadlock_avoidance_test(self, create_deadlock):
+ NLOCKS = 10
+ locks = [self.LockType(str(i)) for i in range(NLOCKS)]
+ pairs = [(locks[i], locks[(i+1)%NLOCKS]) for i in range(NLOCKS)]
+ if create_deadlock:
+ NTHREADS = NLOCKS
else:
- return True
- def f():
- a, b = pairs.pop()
- ra = _acquire(a)
- barrier.wait()
- rb = _acquire(b)
- results.append((ra, rb))
- if rb:
- b.release()
- if ra:
- a.release()
- lock_tests.Bunch(f, NTHREADS).wait_for_finished()
- self.assertEqual(len(results), NTHREADS)
- return results
-
- def test_deadlock(self):
- results = self.run_deadlock_avoidance_test(True)
- # At least one of the threads detected a potential deadlock on its
- # second acquire() call. It may be several of them, because the
- # deadlock avoidance mechanism is conservative.
- nb_deadlocks = results.count((True, False))
- self.assertGreaterEqual(nb_deadlocks, 1)
- self.assertEqual(results.count((True, True)), len(results) - nb_deadlocks)
-
- def test_no_deadlock(self):
- results = self.run_deadlock_avoidance_test(False)
- self.assertEqual(results.count((True, False)), 0)
- self.assertEqual(results.count((True, True)), len(results))
-
-@unittest.skipUnless(threading, "threads needed for this test")
-class Frozen_DeadlockAvoidanceTests(DeadlockAvoidanceTests, unittest.TestCase):
- LockType = frozen_bootstrap._ModuleLock
- DeadlockError = frozen_bootstrap._DeadlockError
-
-@unittest.skipUnless(threading, "threads needed for this test")
-class Source_DeadlockAvoidanceTests(DeadlockAvoidanceTests, unittest.TestCase):
- LockType = source_bootstrap._ModuleLock
- DeadlockError = source_bootstrap._DeadlockError
+ NTHREADS = NLOCKS - 1
+ barrier = threading.Barrier(NTHREADS)
+ results = []
+
+ def _acquire(lock):
+ """Try to acquire the lock. Return True on success,
+ False on deadlock."""
+ try:
+ lock.acquire()
+ except self.DeadlockError:
+ return False
+ else:
+ return True
+
+ def f():
+ a, b = pairs.pop()
+ ra = _acquire(a)
+ barrier.wait()
+ rb = _acquire(b)
+ results.append((ra, rb))
+ if rb:
+ b.release()
+ if ra:
+ a.release()
+ lock_tests.Bunch(f, NTHREADS).wait_for_finished()
+ self.assertEqual(len(results), NTHREADS)
+ return results
+
+ def test_deadlock(self):
+ results = self.run_deadlock_avoidance_test(True)
+ # At least one of the threads detected a potential deadlock on its
+ # second acquire() call. It may be several of them, because the
+ # deadlock avoidance mechanism is conservative.
+ nb_deadlocks = results.count((True, False))
+ self.assertGreaterEqual(nb_deadlocks, 1)
+ self.assertEqual(results.count((True, True)), len(results) - nb_deadlocks)
+
+ def test_no_deadlock(self):
+ results = self.run_deadlock_avoidance_test(False)
+ self.assertEqual(results.count((True, False)), 0)
+ self.assertEqual(results.count((True, True)), len(results))
+
+
+ DEADLOCK_ERRORS = {kind: splitinit._bootstrap._DeadlockError
+ for kind, splitinit in init.items()}
+
+ (Frozen_DeadlockAvoidanceTests,
+ Source_DeadlockAvoidanceTests
+ ) = test_util.test_both(DeadlockAvoidanceTests,
+ LockType=LOCK_TYPES,
+ DeadlockError=DEADLOCK_ERRORS)
+else:
+ DEADLOCK_ERRORS = {}
+
+ class Frozen_DeadlockAvoidanceTests(unittest.TestCase):
+ pass
+
+ class Source_DeadlockAvoidanceTests(unittest.TestCase):
+ pass
class LifetimeTests:
+ @property
+ def bootstrap(self):
+ return self.init._bootstrap
+
def test_lock_lifetime(self):
name = "xyzzy"
self.assertNotIn(name, self.bootstrap._module_locks)
@@ -135,11 +156,10 @@ class LifetimeTests:
self.assertEqual(0, len(self.bootstrap._module_locks),
self.bootstrap._module_locks)
-class Frozen_LifetimeTests(LifetimeTests, unittest.TestCase):
- bootstrap = frozen_bootstrap
-class Source_LifetimeTests(LifetimeTests, unittest.TestCase):
- bootstrap = source_bootstrap
+(Frozen_LifetimeTests,
+ Source_LifetimeTests
+ ) = test_util.test_both(LifetimeTests, init=init)
@support.reap_threads
diff --git a/Lib/test/test_importlib/test_spec.py b/Lib/test/test_importlib/test_spec.py
index 71541f6..8b333e8 100644
--- a/Lib/test/test_importlib/test_spec.py
+++ b/Lib/test/test_importlib/test_spec.py
@@ -1,10 +1,8 @@
-from . import util
+from . import util as test_util
-frozen_init, source_init = util.import_importlib('importlib')
-frozen_bootstrap = frozen_init._bootstrap
-source_bootstrap = source_init._bootstrap
-frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
-frozen_util, source_util = util.import_importlib('importlib.util')
+init = test_util.import_importlib('importlib')
+machinery = test_util.import_importlib('importlib.machinery')
+util = test_util.import_importlib('importlib.util')
import os.path
from test.support import CleanImport
@@ -36,6 +34,9 @@ class TestLoader:
def _is_package(self, name):
return self.package
+ def create_module(self, spec):
+ return None
+
class NewLoader(TestLoader):
@@ -52,6 +53,8 @@ class LegacyLoader(TestLoader):
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
+ frozen_util = util['Frozen']
+
@frozen_util.module_for_loader
def load_module(self, module):
module.ham = self.HAM
@@ -221,18 +224,17 @@ class ModuleSpecTests:
self.assertEqual(self.loc_spec.cached, 'spam.pyc')
-class Frozen_ModuleSpecTests(ModuleSpecTests, unittest.TestCase):
- util = frozen_util
- machinery = frozen_machinery
-
-
-class Source_ModuleSpecTests(ModuleSpecTests, unittest.TestCase):
- util = source_util
- machinery = source_machinery
+(Frozen_ModuleSpecTests,
+ Source_ModuleSpecTests
+ ) = test_util.test_both(ModuleSpecTests, util=util, machinery=machinery)
class ModuleSpecMethodsTests:
+ @property
+ def bootstrap(self):
+ return self.init._bootstrap
+
def setUp(self):
self.name = 'spam'
self.path = 'spam.py'
@@ -243,152 +245,14 @@ class ModuleSpecMethodsTests:
origin=self.path)
self.loc_spec._set_fileattr = True
- # init_module_attrs
-
- def test_init_module_attrs(self):
- module = type(sys)(self.name)
- spec = self.machinery.ModuleSpec(self.name, self.loader)
- self.bootstrap._SpecMethods(spec).init_module_attrs(module)
-
- self.assertEqual(module.__name__, spec.name)
- self.assertIs(module.__loader__, spec.loader)
- self.assertEqual(module.__package__, spec.parent)
- self.assertIs(module.__spec__, spec)
- self.assertFalse(hasattr(module, '__path__'))
- self.assertFalse(hasattr(module, '__file__'))
- self.assertFalse(hasattr(module, '__cached__'))
-
- def test_init_module_attrs_package(self):
- module = type(sys)(self.name)
- spec = self.machinery.ModuleSpec(self.name, self.loader)
- spec.submodule_search_locations = ['spam', 'ham']
- self.bootstrap._SpecMethods(spec).init_module_attrs(module)
-
- self.assertEqual(module.__name__, spec.name)
- self.assertIs(module.__loader__, spec.loader)
- self.assertEqual(module.__package__, spec.parent)
- self.assertIs(module.__spec__, spec)
- self.assertIs(module.__path__, spec.submodule_search_locations)
- self.assertFalse(hasattr(module, '__file__'))
- self.assertFalse(hasattr(module, '__cached__'))
-
- def test_init_module_attrs_location(self):
- module = type(sys)(self.name)
- spec = self.loc_spec
- self.bootstrap._SpecMethods(spec).init_module_attrs(module)
-
- self.assertEqual(module.__name__, spec.name)
- self.assertIs(module.__loader__, spec.loader)
- self.assertEqual(module.__package__, spec.parent)
- self.assertIs(module.__spec__, spec)
- self.assertFalse(hasattr(module, '__path__'))
- self.assertEqual(module.__file__, spec.origin)
- self.assertEqual(module.__cached__,
- self.util.cache_from_source(spec.origin))
-
- def test_init_module_attrs_different_name(self):
- module = type(sys)('eggs')
- spec = self.machinery.ModuleSpec(self.name, self.loader)
- self.bootstrap._SpecMethods(spec).init_module_attrs(module)
-
- self.assertEqual(module.__name__, spec.name)
-
- def test_init_module_attrs_different_spec(self):
- module = type(sys)(self.name)
- module.__spec__ = self.machinery.ModuleSpec('eggs', object())
- spec = self.machinery.ModuleSpec(self.name, self.loader)
- self.bootstrap._SpecMethods(spec).init_module_attrs(module)
-
- self.assertEqual(module.__name__, spec.name)
- self.assertIs(module.__loader__, spec.loader)
- self.assertEqual(module.__package__, spec.parent)
- self.assertIs(module.__spec__, spec)
-
- def test_init_module_attrs_already_set(self):
- module = type(sys)('ham.eggs')
- module.__loader__ = object()
- module.__package__ = 'ham'
- module.__path__ = ['eggs']
- module.__file__ = 'ham/eggs/__init__.py'
- module.__cached__ = self.util.cache_from_source(module.__file__)
- original = vars(module).copy()
- spec = self.loc_spec
- spec.submodule_search_locations = ['']
- self.bootstrap._SpecMethods(spec).init_module_attrs(module)
-
- self.assertIs(module.__loader__, original['__loader__'])
- self.assertEqual(module.__package__, original['__package__'])
- self.assertIs(module.__path__, original['__path__'])
- self.assertEqual(module.__file__, original['__file__'])
- self.assertEqual(module.__cached__, original['__cached__'])
-
- def test_init_module_attrs_immutable(self):
- module = object()
- spec = self.loc_spec
- spec.submodule_search_locations = ['']
- self.bootstrap._SpecMethods(spec).init_module_attrs(module)
-
- self.assertFalse(hasattr(module, '__name__'))
- self.assertFalse(hasattr(module, '__loader__'))
- self.assertFalse(hasattr(module, '__package__'))
- self.assertFalse(hasattr(module, '__spec__'))
- self.assertFalse(hasattr(module, '__path__'))
- self.assertFalse(hasattr(module, '__file__'))
- self.assertFalse(hasattr(module, '__cached__'))
-
- # create()
-
- def test_create(self):
- created = self.bootstrap._SpecMethods(self.spec).create()
-
- self.assertEqual(created.__name__, self.spec.name)
- self.assertIs(created.__loader__, self.spec.loader)
- self.assertEqual(created.__package__, self.spec.parent)
- self.assertIs(created.__spec__, self.spec)
- self.assertFalse(hasattr(created, '__path__'))
- self.assertFalse(hasattr(created, '__file__'))
- self.assertFalse(hasattr(created, '__cached__'))
-
- def test_create_from_loader(self):
- module = type(sys.implementation)()
- class CreatingLoader(TestLoader):
- def create_module(self, spec):
- return module
- self.spec.loader = CreatingLoader()
- created = self.bootstrap._SpecMethods(self.spec).create()
-
- self.assertIs(created, module)
- self.assertEqual(created.__name__, self.spec.name)
- self.assertIs(created.__loader__, self.spec.loader)
- self.assertEqual(created.__package__, self.spec.parent)
- self.assertIs(created.__spec__, self.spec)
- self.assertFalse(hasattr(created, '__path__'))
- self.assertFalse(hasattr(created, '__file__'))
- self.assertFalse(hasattr(created, '__cached__'))
-
- def test_create_from_loader_not_handled(self):
- class CreatingLoader(TestLoader):
- def create_module(self, spec):
- return None
- self.spec.loader = CreatingLoader()
- created = self.bootstrap._SpecMethods(self.spec).create()
-
- self.assertEqual(created.__name__, self.spec.name)
- self.assertIs(created.__loader__, self.spec.loader)
- self.assertEqual(created.__package__, self.spec.parent)
- self.assertIs(created.__spec__, self.spec)
- self.assertFalse(hasattr(created, '__path__'))
- self.assertFalse(hasattr(created, '__file__'))
- self.assertFalse(hasattr(created, '__cached__'))
-
# exec()
def test_exec(self):
self.spec.loader = NewLoader()
- module = self.bootstrap._SpecMethods(self.spec).create()
+ module = self.util.module_from_spec(self.spec)
sys.modules[self.name] = module
self.assertFalse(hasattr(module, 'eggs'))
- self.bootstrap._SpecMethods(self.spec).exec(module)
+ self.bootstrap._exec(self.spec, module)
self.assertEqual(module.eggs, 1)
@@ -397,7 +261,7 @@ class ModuleSpecMethodsTests:
def test_load(self):
self.spec.loader = NewLoader()
with CleanImport(self.spec.name):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
+ loaded = self.bootstrap._load(self.spec)
installed = sys.modules[self.spec.name]
self.assertEqual(loaded.eggs, 1)
@@ -410,7 +274,7 @@ class ModuleSpecMethodsTests:
sys.modules[module.__name__] = replacement
self.spec.loader = ReplacingLoader()
with CleanImport(self.spec.name):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
+ loaded = self.bootstrap._load(self.spec)
installed = sys.modules[self.spec.name]
self.assertIs(loaded, replacement)
@@ -423,7 +287,7 @@ class ModuleSpecMethodsTests:
self.spec.loader = FailedLoader()
with CleanImport(self.spec.name):
with self.assertRaises(RuntimeError):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
+ loaded = self.bootstrap._load(self.spec)
self.assertNotIn(self.spec.name, sys.modules)
def test_load_failed_removed(self):
@@ -434,20 +298,20 @@ class ModuleSpecMethodsTests:
self.spec.loader = FailedLoader()
with CleanImport(self.spec.name):
with self.assertRaises(RuntimeError):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
+ loaded = self.bootstrap._load(self.spec)
self.assertNotIn(self.spec.name, sys.modules)
def test_load_legacy(self):
self.spec.loader = LegacyLoader()
with CleanImport(self.spec.name):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
+ loaded = self.bootstrap._load(self.spec)
self.assertEqual(loaded.ham, -1)
def test_load_legacy_attributes(self):
self.spec.loader = LegacyLoader()
with CleanImport(self.spec.name):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
+ loaded = self.bootstrap._load(self.spec)
self.assertIs(loaded.__loader__, self.spec.loader)
self.assertEqual(loaded.__package__, self.spec.parent)
@@ -461,7 +325,7 @@ class ModuleSpecMethodsTests:
return module
self.spec.loader = ImmutableLoader()
with CleanImport(self.spec.name):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
+ loaded = self.bootstrap._load(self.spec)
self.assertIs(sys.modules[self.spec.name], module)
@@ -470,8 +334,8 @@ class ModuleSpecMethodsTests:
def test_reload(self):
self.spec.loader = NewLoader()
with CleanImport(self.spec.name):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
- reloaded = self.bootstrap._SpecMethods(self.spec).exec(loaded)
+ loaded = self.bootstrap._load(self.spec)
+ reloaded = self.bootstrap._exec(self.spec, loaded)
installed = sys.modules[self.spec.name]
self.assertEqual(loaded.eggs, 1)
@@ -481,9 +345,9 @@ class ModuleSpecMethodsTests:
def test_reload_modified(self):
self.spec.loader = NewLoader()
with CleanImport(self.spec.name):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
+ loaded = self.bootstrap._load(self.spec)
loaded.eggs = 2
- reloaded = self.bootstrap._SpecMethods(self.spec).exec(loaded)
+ reloaded = self.bootstrap._exec(self.spec, loaded)
self.assertEqual(loaded.eggs, 1)
self.assertIs(reloaded, loaded)
@@ -491,9 +355,9 @@ class ModuleSpecMethodsTests:
def test_reload_extra_attributes(self):
self.spec.loader = NewLoader()
with CleanImport(self.spec.name):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
+ loaded = self.bootstrap._load(self.spec)
loaded.available = False
- reloaded = self.bootstrap._SpecMethods(self.spec).exec(loaded)
+ reloaded = self.bootstrap._exec(self.spec, loaded)
self.assertFalse(loaded.available)
self.assertIs(reloaded, loaded)
@@ -501,12 +365,12 @@ class ModuleSpecMethodsTests:
def test_reload_init_module_attrs(self):
self.spec.loader = NewLoader()
with CleanImport(self.spec.name):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
+ loaded = self.bootstrap._load(self.spec)
loaded.__name__ = 'ham'
del loaded.__loader__
del loaded.__package__
del loaded.__spec__
- self.bootstrap._SpecMethods(self.spec).exec(loaded)
+ self.bootstrap._exec(self.spec, loaded)
self.assertEqual(loaded.__name__, self.spec.name)
self.assertIs(loaded.__loader__, self.spec.loader)
@@ -519,8 +383,8 @@ class ModuleSpecMethodsTests:
def test_reload_legacy(self):
self.spec.loader = LegacyLoader()
with CleanImport(self.spec.name):
- loaded = self.bootstrap._SpecMethods(self.spec).load()
- reloaded = self.bootstrap._SpecMethods(self.spec).exec(loaded)
+ loaded = self.bootstrap._load(self.spec)
+ reloaded = self.bootstrap._exec(self.spec, loaded)
installed = sys.modules[self.spec.name]
self.assertEqual(loaded.ham, -1)
@@ -528,20 +392,18 @@ class ModuleSpecMethodsTests:
self.assertIs(installed, loaded)
-class Frozen_ModuleSpecMethodsTests(ModuleSpecMethodsTests, unittest.TestCase):
- bootstrap = frozen_bootstrap
- machinery = frozen_machinery
- util = frozen_util
-
-
-class Source_ModuleSpecMethodsTests(ModuleSpecMethodsTests, unittest.TestCase):
- bootstrap = source_bootstrap
- machinery = source_machinery
- util = source_util
+(Frozen_ModuleSpecMethodsTests,
+ Source_ModuleSpecMethodsTests
+ ) = test_util.test_both(ModuleSpecMethodsTests, init=init, util=util,
+ machinery=machinery)
class ModuleReprTests:
+ @property
+ def bootstrap(self):
+ return self.init._bootstrap
+
def setUp(self):
self.module = type(os)('spam')
self.spec = self.machinery.ModuleSpec('spam', TestLoader())
@@ -625,16 +487,10 @@ class ModuleReprTests:
self.assertEqual(modrepr, '<module {!r}>'.format('spam'))
-class Frozen_ModuleReprTests(ModuleReprTests, unittest.TestCase):
- bootstrap = frozen_bootstrap
- machinery = frozen_machinery
- util = frozen_util
-
-
-class Source_ModuleReprTests(ModuleReprTests, unittest.TestCase):
- bootstrap = source_bootstrap
- machinery = source_machinery
- util = source_util
+(Frozen_ModuleReprTests,
+ Source_ModuleReprTests
+ ) = test_util.test_both(ModuleReprTests, init=init, util=util,
+ machinery=machinery)
class FactoryTests:
@@ -787,13 +643,14 @@ class FactoryTests:
# spec_from_file_location()
def test_spec_from_file_location_default(self):
- if self.machinery is source_machinery:
- raise unittest.SkipTest('not sure why this is breaking...')
spec = self.util.spec_from_file_location(self.name, self.path)
self.assertEqual(spec.name, self.name)
+ # Need to use a circuitous route to get at importlib.machinery to make
+ # sure the same class object is used in the isinstance() check as
+ # would have been used to create the loader.
self.assertIsInstance(spec.loader,
- self.machinery.SourceFileLoader)
+ self.util.abc.machinery.SourceFileLoader)
self.assertEqual(spec.loader.name, self.name)
self.assertEqual(spec.loader.path, self.path)
self.assertEqual(spec.origin, self.path)
@@ -947,11 +804,10 @@ class FactoryTests:
self.assertTrue(spec.has_location)
-class Frozen_FactoryTests(FactoryTests, unittest.TestCase):
- util = frozen_util
- machinery = frozen_machinery
+(Frozen_FactoryTests,
+ Source_FactoryTests
+ ) = test_util.test_both(FactoryTests, util=util, machinery=machinery)
-class Source_FactoryTests(FactoryTests, unittest.TestCase):
- util = source_util
- machinery = source_machinery
+if __name__ == '__main__':
+ unittest.main()
diff --git a/Lib/test/test_importlib/test_util.py b/Lib/test/test_importlib/test_util.py
index b2823c6..2493af7 100644
--- a/Lib/test/test_importlib/test_util.py
+++ b/Lib/test/test_importlib/test_util.py
@@ -1,8 +1,8 @@
-from importlib import util
-from . import util as test_util
-frozen_init, source_init = test_util.import_importlib('importlib')
-frozen_machinery, source_machinery = test_util.import_importlib('importlib.machinery')
-frozen_util, source_util = test_util.import_importlib('importlib.util')
+from . import util
+abc = util.import_importlib('importlib.abc')
+init = util.import_importlib('importlib')
+machinery = util.import_importlib('importlib.machinery')
+importlib_util = util.import_importlib('importlib.util')
import os
import sys
@@ -32,8 +32,94 @@ class DecodeSourceBytesTests:
self.assertEqual(self.util.decode_source(source_bytes),
'\n'.join([self.source, self.source]))
-Frozen_DecodeSourceBytesTests, Source_DecodeSourceBytesTests = test_util.test_both(
- DecodeSourceBytesTests, util=[frozen_util, source_util])
+
+(Frozen_DecodeSourceBytesTests,
+ Source_DecodeSourceBytesTests
+ ) = util.test_both(DecodeSourceBytesTests, util=importlib_util)
+
+
+class ModuleFromSpecTests:
+
+ def test_no_create_module(self):
+ class Loader:
+ def exec_module(self, module):
+ pass
+ spec = self.machinery.ModuleSpec('test', Loader())
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ 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):
+ def create_module(self, spec):
+ return None
+ spec = self.machinery.ModuleSpec('test', Loader())
+ module = self.util.module_from_spec(spec)
+ self.assertIsInstance(module, types.ModuleType)
+ self.assertEqual(module.__name__, spec.name)
+
+ def test_create_module(self):
+ name = 'already set'
+ class CustomModule(types.ModuleType):
+ pass
+ class Loader(self.abc.Loader):
+ def create_module(self, spec):
+ module = CustomModule(spec.name)
+ module.__name__ = name
+ return module
+ spec = self.machinery.ModuleSpec('test', Loader())
+ module = self.util.module_from_spec(spec)
+ self.assertIsInstance(module, CustomModule)
+ self.assertEqual(module.__name__, name)
+
+ def test___name__(self):
+ spec = self.machinery.ModuleSpec('test', object())
+ module = self.util.module_from_spec(spec)
+ self.assertEqual(module.__name__, spec.name)
+
+ def test___spec__(self):
+ spec = self.machinery.ModuleSpec('test', object())
+ module = self.util.module_from_spec(spec)
+ self.assertEqual(module.__spec__, spec)
+
+ def test___loader__(self):
+ loader = object()
+ spec = self.machinery.ModuleSpec('test', loader)
+ module = self.util.module_from_spec(spec)
+ self.assertIs(module.__loader__, loader)
+
+ def test___package__(self):
+ spec = self.machinery.ModuleSpec('test.pkg', object())
+ module = self.util.module_from_spec(spec)
+ self.assertEqual(module.__package__, spec.parent)
+
+ def test___path__(self):
+ spec = self.machinery.ModuleSpec('test', object(), is_package=True)
+ module = self.util.module_from_spec(spec)
+ self.assertEqual(module.__path__, spec.submodule_search_locations)
+
+ def test___file__(self):
+ spec = self.machinery.ModuleSpec('test', object(), origin='some/path')
+ spec.has_location = True
+ module = self.util.module_from_spec(spec)
+ self.assertEqual(module.__file__, spec.origin)
+
+ def test___cached__(self):
+ spec = self.machinery.ModuleSpec('test', object())
+ spec.cached = 'some/path'
+ spec.has_location = True
+ module = self.util.module_from_spec(spec)
+ self.assertEqual(module.__cached__, spec.cached)
+
+(Frozen_ModuleFromSpecTests,
+ Source_ModuleFromSpecTests
+) = util.test_both(ModuleFromSpecTests, abc=abc, machinery=machinery,
+ util=importlib_util)
class ModuleForLoaderTests:
@@ -70,7 +156,7 @@ class ModuleForLoaderTests:
# Test that when no module exists in sys.modules a new module is
# created.
module_name = 'a.b.c'
- with test_util.uncache(module_name):
+ with util.uncache(module_name):
module = self.return_module(module_name)
self.assertIn(module_name, sys.modules)
self.assertIsInstance(module, types.ModuleType)
@@ -88,7 +174,7 @@ class ModuleForLoaderTests:
module = types.ModuleType('a.b.c')
module.__loader__ = 42
module.__package__ = 42
- with test_util.uncache(name):
+ with util.uncache(name):
sys.modules[name] = module
loader = FakeLoader()
returned_module = loader.load_module(name)
@@ -100,7 +186,7 @@ class ModuleForLoaderTests:
# Test that a module is removed from sys.modules if added but an
# exception is raised.
name = 'a.b.c'
- with test_util.uncache(name):
+ with util.uncache(name):
self.raise_exception(name)
self.assertNotIn(name, sys.modules)
@@ -108,7 +194,7 @@ class ModuleForLoaderTests:
# Test that a failure on reload leaves the module in-place.
name = 'a.b.c'
module = types.ModuleType(name)
- with test_util.uncache(name):
+ with util.uncache(name):
sys.modules[name] = module
self.raise_exception(name)
self.assertIs(module, sys.modules[name])
@@ -127,7 +213,7 @@ class ModuleForLoaderTests:
name = 'mod'
module = FalseModule(name)
- with test_util.uncache(name):
+ with util.uncache(name):
self.assertFalse(module)
sys.modules[name] = module
given = self.return_module(name)
@@ -146,7 +232,7 @@ class ModuleForLoaderTests:
return module
name = 'pkg.mod'
- with test_util.uncache(name):
+ with util.uncache(name):
loader = FakeLoader(False)
module = loader.load_module(name)
self.assertEqual(module.__name__, name)
@@ -154,15 +240,17 @@ class ModuleForLoaderTests:
self.assertEqual(module.__package__, 'pkg')
name = 'pkg.sub'
- with test_util.uncache(name):
+ with util.uncache(name):
loader = FakeLoader(True)
module = loader.load_module(name)
self.assertEqual(module.__name__, name)
self.assertIs(module.__loader__, loader)
self.assertEqual(module.__package__, name)
-Frozen_ModuleForLoaderTests, Source_ModuleForLoaderTests = test_util.test_both(
- ModuleForLoaderTests, util=[frozen_util, source_util])
+
+(Frozen_ModuleForLoaderTests,
+ Source_ModuleForLoaderTests
+ ) = util.test_both(ModuleForLoaderTests, util=importlib_util)
class SetPackageTests:
@@ -222,18 +310,25 @@ class SetPackageTests:
self.assertEqual(wrapped.__name__, fxn.__name__)
self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
-Frozen_SetPackageTests, Source_SetPackageTests = test_util.test_both(
- SetPackageTests, util=[frozen_util, source_util])
+
+(Frozen_SetPackageTests,
+ Source_SetPackageTests
+ ) = util.test_both(SetPackageTests, util=importlib_util)
class SetLoaderTests:
"""Tests importlib.util.set_loader()."""
- class DummyLoader:
- @util.set_loader
- def load_module(self, module):
- return self.module
+ @property
+ def DummyLoader(self):
+ # Set DummyLoader on the class lazily.
+ class DummyLoader:
+ @self.util.set_loader
+ def load_module(self, module):
+ return self.module
+ self.__class__.DummyLoader = DummyLoader
+ return DummyLoader
def test_no_attribute(self):
loader = self.DummyLoader()
@@ -262,17 +357,10 @@ class SetLoaderTests:
warnings.simplefilter('ignore', DeprecationWarning)
self.assertEqual(42, loader.load_module('blah').__loader__)
-class Frozen_SetLoaderTests(SetLoaderTests, unittest.TestCase):
- class DummyLoader:
- @frozen_util.set_loader
- def load_module(self, module):
- return self.module
-class Source_SetLoaderTests(SetLoaderTests, unittest.TestCase):
- class DummyLoader:
- @source_util.set_loader
- def load_module(self, module):
- return self.module
+(Frozen_SetLoaderTests,
+ Source_SetLoaderTests
+ ) = util.test_both(SetLoaderTests, util=importlib_util)
class ResolveNameTests:
@@ -307,9 +395,10 @@ class ResolveNameTests:
with self.assertRaises(ValueError):
self.util.resolve_name('..bacon', 'spam')
-Frozen_ResolveNameTests, Source_ResolveNameTests = test_util.test_both(
- ResolveNameTests,
- util=[frozen_util, source_util])
+
+(Frozen_ResolveNameTests,
+ Source_ResolveNameTests
+ ) = util.test_both(ResolveNameTests, util=importlib_util)
class FindSpecTests:
@@ -320,7 +409,7 @@ class FindSpecTests:
def test_sys_modules(self):
name = 'some_mod'
- with test_util.uncache(name):
+ with util.uncache(name):
module = types.ModuleType(name)
loader = 'a loader!'
spec = self.machinery.ModuleSpec(name, loader)
@@ -332,7 +421,7 @@ class FindSpecTests:
def test_sys_modules_without___loader__(self):
name = 'some_mod'
- with test_util.uncache(name):
+ with util.uncache(name):
module = types.ModuleType(name)
del module.__loader__
loader = 'a loader!'
@@ -344,7 +433,7 @@ class FindSpecTests:
def test_sys_modules_spec_is_None(self):
name = 'some_mod'
- with test_util.uncache(name):
+ with util.uncache(name):
module = types.ModuleType(name)
module.__spec__ = None
sys.modules[name] = module
@@ -353,7 +442,7 @@ class FindSpecTests:
def test_sys_modules_loader_is_None(self):
name = 'some_mod'
- with test_util.uncache(name):
+ with util.uncache(name):
module = types.ModuleType(name)
spec = self.machinery.ModuleSpec(name, None)
module.__spec__ = spec
@@ -363,7 +452,7 @@ class FindSpecTests:
def test_sys_modules_spec_is_not_set(self):
name = 'some_mod'
- with test_util.uncache(name):
+ with util.uncache(name):
module = types.ModuleType(name)
try:
del module.__spec__
@@ -375,8 +464,8 @@ class FindSpecTests:
def test_success(self):
name = 'some_mod'
- with test_util.uncache(name):
- with test_util.import_state(meta_path=[self.FakeMetaFinder]):
+ with util.uncache(name):
+ with util.import_state(meta_path=[self.FakeMetaFinder]):
self.assertEqual((name, None, None),
self.util.find_spec(name))
@@ -384,8 +473,8 @@ class FindSpecTests:
# # Searching on a path should work.
# name = 'some_mod'
# path = 'path to some place'
-# with test_util.uncache(name):
-# with test_util.import_state(meta_path=[self.FakeMetaFinder]):
+# with util.uncache(name):
+# with util.import_state(meta_path=[self.FakeMetaFinder]):
# self.assertEqual((name, path, None),
# self.util.find_spec(name, path))
@@ -396,8 +485,8 @@ class FindSpecTests:
def test_find_submodule(self):
name = 'spam'
subname = 'ham'
- with test_util.temp_module(name, pkg=True) as pkg_dir:
- fullname, _ = test_util.submodule(name, subname, pkg_dir)
+ with util.temp_module(name, pkg=True) as pkg_dir:
+ fullname, _ = util.submodule(name, subname, pkg_dir)
spec = self.util.find_spec(fullname)
self.assertIsNot(spec, None)
self.assertIn(name, sorted(sys.modules))
@@ -409,9 +498,9 @@ class FindSpecTests:
def test_find_submodule_parent_already_imported(self):
name = 'spam'
subname = 'ham'
- with test_util.temp_module(name, pkg=True) as pkg_dir:
+ with util.temp_module(name, pkg=True) as pkg_dir:
self.init.import_module(name)
- fullname, _ = test_util.submodule(name, subname, pkg_dir)
+ fullname, _ = util.submodule(name, subname, pkg_dir)
spec = self.util.find_spec(fullname)
self.assertIsNot(spec, None)
self.assertIn(name, sorted(sys.modules))
@@ -423,8 +512,8 @@ class FindSpecTests:
def test_find_relative_module(self):
name = 'spam'
subname = 'ham'
- with test_util.temp_module(name, pkg=True) as pkg_dir:
- fullname, _ = test_util.submodule(name, subname, pkg_dir)
+ with util.temp_module(name, pkg=True) as pkg_dir:
+ fullname, _ = util.submodule(name, subname, pkg_dir)
relname = '.' + subname
spec = self.util.find_spec(relname, name)
self.assertIsNot(spec, None)
@@ -437,8 +526,8 @@ class FindSpecTests:
def test_find_relative_module_missing_package(self):
name = 'spam'
subname = 'ham'
- with test_util.temp_module(name, pkg=True) as pkg_dir:
- fullname, _ = test_util.submodule(name, subname, pkg_dir)
+ with util.temp_module(name, pkg=True) as pkg_dir:
+ fullname, _ = util.submodule(name, subname, pkg_dir)
relname = '.' + subname
with self.assertRaises(ValueError):
self.util.find_spec(relname)
@@ -446,15 +535,10 @@ class FindSpecTests:
self.assertNotIn(fullname, sorted(sys.modules))
-class Frozen_FindSpecTests(FindSpecTests, unittest.TestCase):
- init = frozen_init
- machinery = frozen_machinery
- util = frozen_util
-
-class Source_FindSpecTests(FindSpecTests, unittest.TestCase):
- init = source_init
- machinery = source_machinery
- util = source_util
+(Frozen_FindSpecTests,
+ Source_FindSpecTests
+ ) = util.test_both(FindSpecTests, init=init, util=importlib_util,
+ machinery=machinery)
class MagicNumberTests:
@@ -467,8 +551,10 @@ class MagicNumberTests:
# The magic number uses \r\n to come out wrong when splitting on lines.
self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n'))
-Frozen_MagicNumberTests, Source_MagicNumberTests = test_util.test_both(
- MagicNumberTests, util=[frozen_util, source_util])
+
+(Frozen_MagicNumberTests,
+ Source_MagicNumberTests
+ ) = util.test_both(MagicNumberTests, util=importlib_util)
class PEP3147Tests:
@@ -583,9 +669,10 @@ class PEP3147Tests:
ValueError, self.util.source_from_cache,
'/foo/bar/foo.cpython-32.foo.pyc')
-Frozen_PEP3147Tests, Source_PEP3147Tests = test_util.test_both(
- PEP3147Tests,
- util=[frozen_util, source_util])
+
+(Frozen_PEP3147Tests,
+ Source_PEP3147Tests
+ ) = util.test_both(PEP3147Tests, util=importlib_util)
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/test_windows.py b/Lib/test/test_importlib/test_windows.py
index 96b4adc..c893bcf 100644
--- a/Lib/test/test_importlib/test_windows.py
+++ b/Lib/test/test_importlib/test_windows.py
@@ -1,14 +1,64 @@
-from . import util
-frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
+from . import util as test_util
+machinery = test_util.import_importlib('importlib.machinery')
+import os
+import re
import sys
import unittest
+from test import support
+from distutils.util import get_platform
+from contextlib import contextmanager
+from .util import temp_module
+
+support.import_module('winreg', required_on=['win'])
+from winreg import (
+ CreateKey, HKEY_CURRENT_USER,
+ SetValue, REG_SZ, KEY_ALL_ACCESS,
+ EnumKey, CloseKey, DeleteKey, OpenKey
+)
+
+def delete_registry_tree(root, subkey):
+ try:
+ hkey = OpenKey(root, subkey, access=KEY_ALL_ACCESS)
+ except OSError:
+ # subkey does not exist
+ return
+ while True:
+ try:
+ subsubkey = EnumKey(hkey, 0)
+ except OSError:
+ # no more subkeys
+ break
+ delete_registry_tree(hkey, subsubkey)
+ CloseKey(hkey)
+ DeleteKey(root, subkey)
+
+@contextmanager
+def setup_module(machinery, name, path=None):
+ if machinery.WindowsRegistryFinder.DEBUG_BUILD:
+ root = machinery.WindowsRegistryFinder.REGISTRY_KEY_DEBUG
+ else:
+ root = machinery.WindowsRegistryFinder.REGISTRY_KEY
+ key = root.format(fullname=name,
+ sys_version=sys.version[:3])
+ try:
+ with temp_module(name, "a = 1") as location:
+ subkey = CreateKey(HKEY_CURRENT_USER, key)
+ if path is None:
+ path = location + ".py"
+ SetValue(subkey, "", REG_SZ, path)
+ yield
+ finally:
+ if machinery.WindowsRegistryFinder.DEBUG_BUILD:
+ key = os.path.dirname(key)
+ delete_registry_tree(HKEY_CURRENT_USER, key)
@unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
class WindowsRegistryFinderTests:
-
- # XXX Need a test that finds the spec via the registry.
+ # The module name is process-specific, allowing for
+ # simultaneous runs of the same test on a single machine.
+ test_module = "spamham{}".format(os.getpid())
def test_find_spec_missing(self):
spec = self.machinery.WindowsRegistryFinder.find_spec('spam')
@@ -18,12 +68,42 @@ class WindowsRegistryFinderTests:
loader = self.machinery.WindowsRegistryFinder.find_module('spam')
self.assertIs(loader, None)
+ def test_module_found(self):
+ with setup_module(self.machinery, self.test_module):
+ loader = self.machinery.WindowsRegistryFinder.find_module(self.test_module)
+ spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
+ self.assertIsNot(loader, None)
+ self.assertIsNot(spec, None)
+
+ def test_module_not_found(self):
+ with setup_module(self.machinery, self.test_module, path="."):
+ loader = self.machinery.WindowsRegistryFinder.find_module(self.test_module)
+ spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
+ self.assertIsNone(loader)
+ self.assertIsNone(spec)
+
+(Frozen_WindowsRegistryFinderTests,
+ Source_WindowsRegistryFinderTests
+ ) = test_util.test_both(WindowsRegistryFinderTests, machinery=machinery)
+
+@unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
+class WindowsExtensionSuffixTests:
+ def test_tagged_suffix(self):
+ suffixes = self.machinery.EXTENSION_SUFFIXES
+ expected_tag = ".cp{0.major}{0.minor}-{1}.pyd".format(sys.version_info,
+ re.sub('[^a-zA-Z0-9]', '_', get_platform()))
+ try:
+ untagged_i = suffixes.index(".pyd")
+ except ValueError:
+ untagged_i = suffixes.index("_d.pyd")
+ expected_tag = "_d" + expected_tag
-class Frozen_WindowsRegistryFinderTests(WindowsRegistryFinderTests,
- unittest.TestCase):
- machinery = frozen_machinery
+ self.assertIn(expected_tag, suffixes)
+ # Ensure the tags are in the correct order
+ tagged_i = suffixes.index(expected_tag)
+ self.assertLess(tagged_i, untagged_i)
-class Source_WindowsRegistryFinderTests(WindowsRegistryFinderTests,
- unittest.TestCase):
- machinery = source_machinery
+(Frozen_WindowsExtensionSuffixTests,
+ Source_WindowsExtensionSuffixTests
+ ) = test_util.test_both(WindowsExtensionSuffixTests, machinery=machinery)
diff --git a/Lib/test/test_importlib/util.py b/Lib/test/test_importlib/util.py
index 885cec3..aa4cd7e 100644
--- a/Lib/test/test_importlib/util.py
+++ b/Lib/test/test_importlib/util.py
@@ -1,31 +1,85 @@
-from contextlib import contextmanager
-from importlib import util, invalidate_caches
+import builtins
+import contextlib
+import errno
+import functools
+import importlib
+from importlib import machinery, util, invalidate_caches
+import os
import os.path
from test import support
import unittest
import sys
+import tempfile
import types
+BUILTINS = types.SimpleNamespace()
+BUILTINS.good_name = None
+BUILTINS.bad_name = None
+if 'errno' in sys.builtin_module_names:
+ BUILTINS.good_name = 'errno'
+if 'importlib' not in sys.builtin_module_names:
+ BUILTINS.bad_name = 'importlib'
+
+EXTENSIONS = types.SimpleNamespace()
+EXTENSIONS.path = None
+EXTENSIONS.ext = None
+EXTENSIONS.filename = None
+EXTENSIONS.file_path = None
+EXTENSIONS.name = '_testcapi'
+
+def _extension_details():
+ global EXTENSIONS
+ for path in sys.path:
+ for ext in machinery.EXTENSION_SUFFIXES:
+ filename = EXTENSIONS.name + ext
+ file_path = os.path.join(path, filename)
+ if os.path.exists(file_path):
+ EXTENSIONS.path = path
+ EXTENSIONS.ext = ext
+ EXTENSIONS.filename = filename
+ EXTENSIONS.file_path = file_path
+ return
+
+_extension_details()
+
+
def import_importlib(module_name):
"""Import a module from importlib both w/ and w/o _frozen_importlib."""
fresh = ('importlib',) if '.' in module_name else ()
frozen = support.import_fresh_module(module_name)
source = support.import_fresh_module(module_name, fresh=fresh,
blocked=('_frozen_importlib',))
+ return {'Frozen': frozen, 'Source': source}
+
+
+def specialize_class(cls, kind, base=None, **kwargs):
+ # XXX Support passing in submodule names--load (and cache) them?
+ # That would clean up the test modules a bit more.
+ if base is None:
+ base = unittest.TestCase
+ elif not isinstance(base, type):
+ base = base[kind]
+ name = '{}_{}'.format(kind, cls.__name__)
+ bases = (cls, base)
+ specialized = types.new_class(name, bases)
+ specialized.__module__ = cls.__module__
+ specialized._NAME = cls.__name__
+ specialized._KIND = kind
+ for attr, values in kwargs.items():
+ value = values[kind]
+ setattr(specialized, attr, value)
+ return specialized
+
+
+def split_frozen(cls, base=None, **kwargs):
+ frozen = specialize_class(cls, 'Frozen', base, **kwargs)
+ source = specialize_class(cls, 'Source', base, **kwargs)
return frozen, source
-def test_both(test_class, **kwargs):
- frozen_tests = types.new_class('Frozen_'+test_class.__name__,
- (test_class, unittest.TestCase))
- source_tests = types.new_class('Source_'+test_class.__name__,
- (test_class, unittest.TestCase))
- frozen_tests.__module__ = source_tests.__module__ = test_class.__module__
- for attr, (frozen_value, source_value) in kwargs.items():
- setattr(frozen_tests, attr, frozen_value)
- setattr(source_tests, attr, source_value)
- return frozen_tests, source_tests
+def test_both(test_class, base=None, **kwargs):
+ return split_frozen(test_class, base, **kwargs)
CASE_INSENSITIVE_FS = True
@@ -38,6 +92,10 @@ if sys.platform not in ('win32', 'cygwin'):
if not os.path.exists(changed_name):
CASE_INSENSITIVE_FS = False
+source_importlib = import_importlib('importlib')['Source']
+__import__ = {'Frozen': staticmethod(builtins.__import__),
+ 'Source': staticmethod(source_importlib.__import__)}
+
def case_insensitive_tests(test):
"""Class decorator that nullifies tests requiring a case-insensitive
@@ -53,7 +111,7 @@ def submodule(parent, name, pkg_dir, content=''):
return '{}.{}'.format(parent, name), path
-@contextmanager
+@contextlib.contextmanager
def uncache(*names):
"""Uncache a module from sys.modules.
@@ -79,7 +137,7 @@ def uncache(*names):
pass
-@contextmanager
+@contextlib.contextmanager
def temp_module(name, content='', *, pkg=False):
conflicts = [n for n in sys.modules if n.partition('.')[0] == name]
with support.temp_cwd(None) as cwd:
@@ -103,7 +161,7 @@ def temp_module(name, content='', *, pkg=False):
yield location
-@contextmanager
+@contextlib.contextmanager
def import_state(**kwargs):
"""Context manager to manage the various importers and stored state in the
sys module.
@@ -198,6 +256,7 @@ class mock_modules(_ImporterMock):
raise
return self.modules[fullname]
+
class mock_spec(_ImporterMock):
"""Importer mock using PEP 451 APIs."""
@@ -223,3 +282,99 @@ class mock_spec(_ImporterMock):
self.module_code[module.__spec__.name]()
except KeyError:
pass
+
+
+def writes_bytecode_files(fxn):
+ """Decorator to protect sys.dont_write_bytecode from mutation and to skip
+ tests that require it to be set to False."""
+ if sys.dont_write_bytecode:
+ return lambda *args, **kwargs: None
+ @functools.wraps(fxn)
+ def wrapper(*args, **kwargs):
+ original = sys.dont_write_bytecode
+ sys.dont_write_bytecode = False
+ try:
+ to_return = fxn(*args, **kwargs)
+ finally:
+ sys.dont_write_bytecode = original
+ return to_return
+ return wrapper
+
+
+def ensure_bytecode_path(bytecode_path):
+ """Ensure that the __pycache__ directory for PEP 3147 pyc file exists.
+
+ :param bytecode_path: File system path to PEP 3147 pyc file.
+ """
+ try:
+ os.mkdir(os.path.dirname(bytecode_path))
+ except OSError as error:
+ if error.errno != errno.EEXIST:
+ raise
+
+
+@contextlib.contextmanager
+def create_modules(*names):
+ """Temporarily create each named module with an attribute (named 'attr')
+ that contains the name passed into the context manager that caused the
+ creation of the module.
+
+ All files are created in a temporary directory returned by
+ tempfile.mkdtemp(). This directory is inserted at the beginning of
+ sys.path. When the context manager exits all created files (source and
+ bytecode) are explicitly deleted.
+
+ No magic is performed when creating packages! This means that if you create
+ a module within a package you must also create the package's __init__ as
+ well.
+
+ """
+ source = 'attr = {0!r}'
+ created_paths = []
+ mapping = {}
+ state_manager = None
+ uncache_manager = None
+ try:
+ temp_dir = tempfile.mkdtemp()
+ mapping['.root'] = temp_dir
+ import_names = set()
+ for name in names:
+ if not name.endswith('__init__'):
+ import_name = name
+ else:
+ import_name = name[:-len('.__init__')]
+ import_names.add(import_name)
+ if import_name in sys.modules:
+ del sys.modules[import_name]
+ name_parts = name.split('.')
+ file_path = temp_dir
+ for directory in name_parts[:-1]:
+ file_path = os.path.join(file_path, directory)
+ if not os.path.exists(file_path):
+ os.mkdir(file_path)
+ created_paths.append(file_path)
+ file_path = os.path.join(file_path, name_parts[-1] + '.py')
+ with open(file_path, 'w') as file:
+ file.write(source.format(name))
+ created_paths.append(file_path)
+ mapping[name] = file_path
+ uncache_manager = uncache(*import_names)
+ uncache_manager.__enter__()
+ state_manager = import_state(path=[temp_dir])
+ state_manager.__enter__()
+ yield mapping
+ finally:
+ if state_manager is not None:
+ state_manager.__exit__(None, None, None)
+ if uncache_manager is not None:
+ uncache_manager.__exit__(None, None, None)
+ support.rmtree(temp_dir)
+
+
+def mock_path_hook(*entries, importer):
+ """A mock sys.path_hooks entry."""
+ def hook(entry):
+ if entry not in entries:
+ raise ImportError
+ return importer
+ return hook
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index fc3bf07..bbc5385 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -1,3 +1,4 @@
+import builtins
import collections
import datetime
import functools
@@ -8,6 +9,7 @@ import linecache
import os
from os.path import normcase
import _pickle
+import pickle
import re
import shutil
import sys
@@ -23,7 +25,7 @@ except ImportError:
ThreadPoolExecutor = None
from test.support import run_unittest, TESTFN, DirsOnSysPath, cpython_only
-from test.support import MISSING_C_DOCSTRINGS
+from test.support import MISSING_C_DOCSTRINGS, cpython_only
from test.script_helper import assert_python_ok, assert_python_failure
from test import inspect_fodder as mod
from test import inspect_fodder2 as mod2
@@ -76,6 +78,7 @@ def generator_function_example(self):
for i in range(2):
yield i
+
class TestPredicates(IsTestBase):
def test_sixteen(self):
count = len([x for x in dir(inspect) if x.startswith('is')])
@@ -182,6 +185,14 @@ class TestInterpreterStack(IsTestBase):
(modfile, 43, 'argue', [' spam(a, b, c)\n'], 0))
self.assertEqual(revise(*mod.st[3][1:]),
(modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0))
+ # Test named tuple fields
+ record = mod.st[0]
+ self.assertIs(record.frame, mod.fr)
+ self.assertEqual(record.lineno, 16)
+ self.assertEqual(record.filename, mod.__file__)
+ self.assertEqual(record.function, 'eggs')
+ self.assertIn('inspect.stack()', record.code_context[0])
+ self.assertEqual(record.index, 0)
def test_trace(self):
self.assertEqual(len(git.tr), 3)
@@ -369,6 +380,9 @@ class TestDecorators(GetSourceBase):
def test_replacing_decorator(self):
self.assertSourceEqual(mod2.gone, 9, 10)
+ def test_getsource_unwrap(self):
+ self.assertSourceEqual(mod2.real, 122, 124)
+
class TestOneliners(GetSourceBase):
fodderModule = mod2
def test_oneline_lambda(self):
@@ -1615,6 +1629,68 @@ class TestGetGeneratorState(unittest.TestCase):
self.assertRaises(TypeError, inspect.getgeneratorlocals, (2,3))
+class MySignature(inspect.Signature):
+ # Top-level to make it picklable;
+ # used in test_signature_object_pickle
+ pass
+
+class MyParameter(inspect.Parameter):
+ # Top-level to make it picklable;
+ # used in test_signature_object_pickle
+ pass
+
+ @cpython_only
+ @unittest.skipIf(MISSING_C_DOCSTRINGS,
+ "Signature information for builtins requires docstrings")
+ def test_builtins_have_signatures(self):
+ # This checks all builtin callables in CPython have signatures
+ # A few have signatures Signature can't yet handle, so we skip those
+ # since they will have to wait until PEP 457 adds the required
+ # introspection support to the inspect module
+ # Some others also haven't been converted yet for various other
+ # reasons, so we also skip those for the time being, but design
+ # the test to fail in order to indicate when it needs to be
+ # updated.
+ no_signature = set()
+ # These need PEP 457 groups
+ needs_groups = ["range", "slice", "dir", "getattr",
+ "next", "iter", "vars"]
+ no_signature |= needs_groups
+ # These need PEP 457 groups or a signature change to accept None
+ needs_semantic_update = ["round"]
+ no_signature |= needs_semantic_update
+ # These need *args support in Argument Clinic
+ needs_varargs = ["min", "max", "print", "__build_class__"]
+ no_signature |= needs_varargs
+ # These simply weren't covered in the initial AC conversion
+ # for builtin callables
+ not_converted_yet = ["open", "__import__"]
+ no_signature |= not_converted_yet
+ # These builtin types are expected to provide introspection info
+ types_with_signatures = set()
+ # Check the signatures we expect to be there
+ ns = vars(builtins)
+ for name, obj in sorted(ns.items()):
+ if not callable(obj):
+ continue
+ # The builtin types haven't been converted to AC yet
+ if isinstance(obj, type) and (name not in types_with_signatures):
+ # Note that this also skips all the exception types
+ no_signature.append(name)
+ if (name in no_signature):
+ # Not yet converted
+ continue
+ with self.subTest(builtin=name):
+ self.assertIsNotNone(inspect.signature(obj))
+ # Check callables that haven't been converted don't claim a signature
+ # This ensures this test will start failing as more signatures are
+ # added, so the affected items can be moved into the scope of the
+ # regression test above
+ for name in no_signature:
+ with self.subTest(builtin=name):
+ self.assertIsNone(ns[name].__text_signature__)
+
+
class TestSignatureObject(unittest.TestCase):
@staticmethod
def signature(func):
@@ -1672,6 +1748,37 @@ class TestSignatureObject(unittest.TestCase):
with self.assertRaisesRegex(ValueError, 'follows default argument'):
S((pkd, pk))
+ self.assertTrue(repr(sig).startswith('<Signature'))
+ self.assertTrue('"(po, pk' in repr(sig))
+
+ def test_signature_object_pickle(self):
+ def foo(a, b, *, c:1={}, **kw) -> {42:'ham'}: pass
+ foo_partial = functools.partial(foo, a=1)
+
+ sig = inspect.signature(foo_partial)
+
+ for ver in range(pickle.HIGHEST_PROTOCOL + 1):
+ with self.subTest(pickle_ver=ver, subclass=False):
+ sig_pickled = pickle.loads(pickle.dumps(sig, ver))
+ self.assertEqual(sig, sig_pickled)
+
+ # Test that basic sub-classing works
+ sig = inspect.signature(foo)
+ myparam = MyParameter(name='z', kind=inspect.Parameter.POSITIONAL_ONLY)
+ myparams = collections.OrderedDict(sig.parameters, a=myparam)
+ mysig = MySignature().replace(parameters=myparams.values(),
+ return_annotation=sig.return_annotation)
+ self.assertTrue(isinstance(mysig, MySignature))
+ self.assertTrue(isinstance(mysig.parameters['z'], MyParameter))
+
+ for ver in range(pickle.HIGHEST_PROTOCOL + 1):
+ with self.subTest(pickle_ver=ver, subclass=True):
+ sig_pickled = pickle.loads(pickle.dumps(mysig, ver))
+ self.assertEqual(mysig, sig_pickled)
+ self.assertTrue(isinstance(sig_pickled, MySignature))
+ self.assertTrue(isinstance(sig_pickled.parameters['z'],
+ MyParameter))
+
def test_signature_immutability(self):
def test(a):
pass
@@ -2435,49 +2542,91 @@ class TestSignatureObject(unittest.TestCase):
def bar(a, *, b:int) -> float: pass
self.assertEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
def bar(a, *, b:int) -> int: pass
self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertNotEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
def bar(a, *, b:int): pass
self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertNotEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
def bar(a, *, b:int=42) -> float: pass
self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertNotEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
def bar(a, *, c) -> float: pass
self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertNotEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
def bar(a, b:int) -> float: pass
self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertNotEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
def spam(b:int, a) -> float: pass
self.assertNotEqual(inspect.signature(spam), inspect.signature(bar))
+ self.assertNotEqual(
+ hash(inspect.signature(spam)), hash(inspect.signature(bar)))
def foo(*, a, b, c): pass
def bar(*, c, b, a): pass
self.assertEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
def foo(*, a=1, b, c): pass
def bar(*, c, b, a=1): pass
self.assertEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
def foo(pos, *, a=1, b, c): pass
def bar(pos, *, c, b, a=1): pass
self.assertEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
def foo(pos, *, a, b, c): pass
def bar(pos, *, c, b, a=1): pass
self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertNotEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
def foo(pos, *args, a=42, b, c, **kwargs:int): pass
def bar(pos, *args, c, b, a=42, **kwargs:int): pass
self.assertEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertEqual(
+ hash(inspect.signature(foo)), hash(inspect.signature(bar)))
+
+ def test_signature_hashable(self):
+ S = inspect.Signature
+ P = inspect.Parameter
- def test_signature_unhashable(self):
def foo(a): pass
- sig = inspect.signature(foo)
+ foo_sig = inspect.signature(foo)
+
+ manual_sig = S(parameters=[P('a', P.POSITIONAL_OR_KEYWORD)])
+
+ self.assertEqual(hash(foo_sig), hash(manual_sig))
+ self.assertNotEqual(hash(foo_sig),
+ hash(manual_sig.replace(return_annotation='spam')))
+
+ def bar(a) -> 1: pass
+ self.assertNotEqual(hash(foo_sig), hash(inspect.signature(bar)))
+
+ def foo(a={}): pass
with self.assertRaisesRegex(TypeError, 'unhashable type'):
- hash(sig)
+ hash(inspect.signature(foo))
+
+ def foo(a) -> {}: pass
+ with self.assertRaisesRegex(TypeError, 'unhashable type'):
+ hash(inspect.signature(foo))
def test_signature_str(self):
def foo(a:int=1, *, b, c=None, **kwargs) -> 42:
@@ -2551,6 +2700,19 @@ class TestSignatureObject(unittest.TestCase):
self.assertEqual(self.signature(Spam.foo),
self.signature(Ham.foo))
+ def test_signature_from_callable_python_obj(self):
+ class MySignature(inspect.Signature): pass
+ def foo(a, *, b:1): pass
+ foo_sig = MySignature.from_callable(foo)
+ self.assertTrue(isinstance(foo_sig, MySignature))
+
+ @unittest.skipIf(MISSING_C_DOCSTRINGS,
+ "Signature information for builtins requires docstrings")
+ def test_signature_from_callable_builtin_obj(self):
+ class MySignature(inspect.Signature): pass
+ sig = MySignature.from_callable(_pickle.Pickler)
+ self.assertTrue(isinstance(sig, MySignature))
+
class TestParameterObject(unittest.TestCase):
def test_signature_parameter_kinds(self):
@@ -2596,6 +2758,16 @@ class TestParameterObject(unittest.TestCase):
p.replace(kind=inspect.Parameter.VAR_POSITIONAL)
self.assertTrue(repr(p).startswith('<Parameter'))
+ self.assertTrue('"a=42"' in repr(p))
+
+ def test_signature_parameter_hashable(self):
+ P = inspect.Parameter
+ foo = P('foo', kind=P.POSITIONAL_ONLY)
+ self.assertEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY)))
+ self.assertNotEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY,
+ default=42)))
+ self.assertNotEqual(hash(foo),
+ hash(foo.replace(kind=P.VAR_POSITIONAL)))
def test_signature_parameter_equality(self):
P = inspect.Parameter
@@ -2607,13 +2779,6 @@ class TestParameterObject(unittest.TestCase):
self.assertEqual(p, P('foo', default=42,
kind=inspect.Parameter.KEYWORD_ONLY))
- def test_signature_parameter_unhashable(self):
- p = inspect.Parameter('foo', default=42,
- kind=inspect.Parameter.KEYWORD_ONLY)
-
- with self.assertRaisesRegex(TypeError, 'unhashable type'):
- hash(p)
-
def test_signature_parameter_replace(self):
p = inspect.Parameter('foo', default=42,
kind=inspect.Parameter.KEYWORD_ONLY)
@@ -2922,6 +3087,16 @@ class TestBoundArguments(unittest.TestCase):
ba4 = inspect.signature(bar).bind(1)
self.assertNotEqual(ba, ba4)
+ def test_signature_bound_arguments_pickle(self):
+ def foo(a, b, *, c:1={}, **kw) -> {42:'ham'}: pass
+ sig = inspect.signature(foo)
+ ba = sig.bind(20, 30, z={})
+
+ for ver in range(pickle.HIGHEST_PROTOCOL + 1):
+ with self.subTest(pickle_ver=ver):
+ ba_pickled = pickle.loads(pickle.dumps(ba, ver))
+ self.assertEqual(ba, ba_pickled)
+
class TestSignaturePrivateHelpers(unittest.TestCase):
def test_signature_get_bound_param(self):
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index a424f76..c867502 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -44,10 +44,6 @@ try:
import threading
except ImportError:
threading = None
-try:
- import fcntl
-except ImportError:
- fcntl = None
def _default_chunk_size():
"""Get the default TextIOWrapper chunk size"""
@@ -367,8 +363,8 @@ class IOTest(unittest.TestCase):
def test_open_handles_NUL_chars(self):
fn_with_NUL = 'foo\0bar'
- self.assertRaises(TypeError, self.open, fn_with_NUL, 'w')
- self.assertRaises(TypeError, self.open, bytes(fn_with_NUL, 'ascii'), 'w')
+ self.assertRaises(ValueError, self.open, fn_with_NUL, 'w')
+ self.assertRaises(ValueError, self.open, bytes(fn_with_NUL, 'ascii'), 'w')
def test_raw_file_io(self):
with self.open(support.TESTFN, "wb", buffering=0) as f:
@@ -780,7 +776,7 @@ class CommonBufferedTests:
def test_repr(self):
raw = self.MockRawIO()
b = self.tp(raw)
- clsname = "%s.%s" % (self.tp.__module__, self.tp.__name__)
+ clsname = "%s.%s" % (self.tp.__module__, self.tp.__qualname__)
self.assertEqual(repr(b), "<%s>" % clsname)
raw.name = "dummy"
self.assertEqual(repr(b), "<%s name='dummy'>" % clsname)
@@ -945,6 +941,71 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
self.assertEqual(bufio.readinto(b), 1)
self.assertEqual(b, b"cb")
+ def test_readinto1(self):
+ buffer_size = 10
+ rawio = self.MockRawIO((b"abc", b"de", b"fgh", b"jkl"))
+ bufio = self.tp(rawio, buffer_size=buffer_size)
+ b = bytearray(2)
+ self.assertEqual(bufio.peek(3), b'abc')
+ self.assertEqual(rawio._reads, 1)
+ self.assertEqual(bufio.readinto1(b), 2)
+ self.assertEqual(b, b"ab")
+ self.assertEqual(rawio._reads, 1)
+ self.assertEqual(bufio.readinto1(b), 1)
+ self.assertEqual(b[:1], b"c")
+ self.assertEqual(rawio._reads, 1)
+ self.assertEqual(bufio.readinto1(b), 2)
+ self.assertEqual(b, b"de")
+ self.assertEqual(rawio._reads, 2)
+ b = bytearray(2*buffer_size)
+ self.assertEqual(bufio.peek(3), b'fgh')
+ self.assertEqual(rawio._reads, 3)
+ self.assertEqual(bufio.readinto1(b), 6)
+ self.assertEqual(b[:6], b"fghjkl")
+ self.assertEqual(rawio._reads, 4)
+
+ def test_readinto_array(self):
+ buffer_size = 60
+ data = b"a" * 26
+ rawio = self.MockRawIO((data,))
+ bufio = self.tp(rawio, buffer_size=buffer_size)
+
+ # Create an array with element size > 1 byte
+ b = array.array('i', b'x' * 32)
+ assert len(b) != 16
+
+ # Read into it. We should get as many *bytes* as we can fit into b
+ # (which is more than the number of elements)
+ n = bufio.readinto(b)
+ self.assertGreater(n, len(b))
+
+ # Check that old contents of b are preserved
+ bm = memoryview(b).cast('B')
+ self.assertLess(n, len(bm))
+ self.assertEqual(bm[:n], data[:n])
+ self.assertEqual(bm[n:], b'x' * (len(bm[n:])))
+
+ def test_readinto1_array(self):
+ buffer_size = 60
+ data = b"a" * 26
+ rawio = self.MockRawIO((data,))
+ bufio = self.tp(rawio, buffer_size=buffer_size)
+
+ # Create an array with element size > 1 byte
+ b = array.array('i', b'x' * 32)
+ assert len(b) != 16
+
+ # Read into it. We should get as many *bytes* as we can fit into b
+ # (which is more than the number of elements)
+ n = bufio.readinto1(b)
+ self.assertGreater(n, len(b))
+
+ # Check that old contents of b are preserved
+ bm = memoryview(b).cast('B')
+ self.assertLess(n, len(bm))
+ self.assertEqual(bm[:n], data[:n])
+ self.assertEqual(bm[n:], b'x' * (len(bm[n:])))
+
def test_readlines(self):
def bufio():
rawio = self.MockRawIO((b"abc\n", b"d\n", b"ef"))
@@ -2795,6 +2856,17 @@ class TextIOWrapperTest(unittest.TestCase):
self.assertFalse(err)
self.assertEqual("ok", out.decode().strip())
+ def test_read_byteslike(self):
+ r = MemviewBytesIO(b'Just some random string\n')
+ t = self.TextIOWrapper(r, 'utf-8')
+
+ # TextIOwrapper will not read the full string, because
+ # we truncate it to a multiple of the native int size
+ # so that we can construct a more complex memoryview.
+ bytes_val = _to_memoryview(r.getvalue()).tobytes()
+
+ self.assertEqual(t.read(200), bytes_val.decode('utf-8'))
+
def test_issue22849(self):
class F(object):
def readable(self): return True
@@ -2811,6 +2883,25 @@ class TextIOWrapperTest(unittest.TestCase):
t = self.TextIOWrapper(F(), encoding='utf-8')
+class MemviewBytesIO(io.BytesIO):
+ '''A BytesIO object whose read method returns memoryviews
+ rather than bytes'''
+
+ def read1(self, len_):
+ return _to_memoryview(super().read1(len_))
+
+ def read(self, len_):
+ return _to_memoryview(super().read(len_))
+
+def _to_memoryview(buf):
+ '''Convert bytes-object *buf* to a non-trivial memoryview'''
+
+ arr = array.array('i')
+ idx = len(buf) - len(buf) % arr.itemsize
+ arr.frombytes(buf[:idx])
+ return memoryview(arr)
+
+
class CTextIOWrapperTest(TextIOWrapperTest):
io = io
shutdown_error = "RuntimeError: could not find io module state"
@@ -3057,6 +3148,8 @@ class MiscIOTest(unittest.TestCase):
self.assertRaises(ValueError, f.readall)
if hasattr(f, "readinto"):
self.assertRaises(ValueError, f.readinto, bytearray(1024))
+ if hasattr(f, "readinto1"):
+ self.assertRaises(ValueError, f.readinto1, bytearray(1024))
self.assertRaises(ValueError, f.readline)
self.assertRaises(ValueError, f.readlines)
self.assertRaises(ValueError, f.seek, 0)
@@ -3170,26 +3263,20 @@ class MiscIOTest(unittest.TestCase):
with self.open(support.TESTFN, **kwargs) as f:
self.assertRaises(TypeError, pickle.dumps, f, protocol)
- @unittest.skipUnless(fcntl, 'fcntl required for this test')
def test_nonblock_pipe_write_bigbuf(self):
self._test_nonblock_pipe_write(16*1024)
- @unittest.skipUnless(fcntl, 'fcntl required for this test')
def test_nonblock_pipe_write_smallbuf(self):
self._test_nonblock_pipe_write(1024)
- def _set_non_blocking(self, fd):
- flags = fcntl.fcntl(fd, fcntl.F_GETFL)
- self.assertNotEqual(flags, -1)
- res = fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
- self.assertEqual(res, 0)
-
+ @unittest.skipUnless(hasattr(os, 'set_blocking'),
+ 'os.set_blocking() required for this test')
def _test_nonblock_pipe_write(self, bufsize):
sent = []
received = []
r, w = os.pipe()
- self._set_non_blocking(r)
- self._set_non_blocking(w)
+ os.set_blocking(r, False)
+ os.set_blocking(w, False)
# To exercise all code paths in the C implementation we need
# to play with buffer sizes. For instance, if we choose a
diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py
index bfb5699..e985329 100644
--- a/Lib/test/test_ipaddress.py
+++ b/Lib/test/test_ipaddress.py
@@ -9,6 +9,7 @@ import re
import contextlib
import functools
import operator
+import pickle
import ipaddress
@@ -83,6 +84,13 @@ class CommonTestMixin:
self.assertRaises(TypeError, hex, self.factory(1))
self.assertRaises(TypeError, bytes, self.factory(1))
+ def pickle_test(self, addr):
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ with self.subTest(proto=proto):
+ x = self.factory(addr)
+ y = pickle.loads(pickle.dumps(x, proto))
+ self.assertEqual(y, x)
+
class CommonTestMixin_v4(CommonTestMixin):
@@ -248,6 +256,9 @@ class AddressTestCase_v4(BaseTestCase, CommonTestMixin_v4):
assertBadOctet("257.0.0.0", 257)
assertBadOctet("192.168.0.999", 999)
+ def test_pickle(self):
+ self.pickle_test('192.0.2.1')
+
class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6):
factory = ipaddress.IPv6Address
@@ -380,6 +391,9 @@ class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6):
assertBadPart("02001:db8::", "02001")
assertBadPart('2001:888888::1', "888888")
+ def test_pickle(self):
+ self.pickle_test('2001:db8::')
+
class NetmaskTestMixin_v4(CommonTestMixin_v4):
"""Input validation on interfaces and networks is very similar"""
@@ -443,6 +457,11 @@ class NetmaskTestMixin_v4(CommonTestMixin_v4):
assertBadNetmask("1.1.1.1", "pudding")
assertBadNetmask("1.1.1.1", "::")
+ def test_pickle(self):
+ self.pickle_test('192.0.2.0/27')
+ self.pickle_test('192.0.2.0/31') # IPV4LENGTH - 1
+ self.pickle_test('192.0.2.0') # IPV4LENGTH
+
class InterfaceTestCase_v4(BaseTestCase, NetmaskTestMixin_v4):
factory = ipaddress.IPv4Interface
@@ -501,6 +520,11 @@ class NetmaskTestMixin_v6(CommonTestMixin_v6):
assertBadNetmask("::1", "pudding")
assertBadNetmask("::", "::")
+ def test_pickle(self):
+ self.pickle_test('2001:db8::1000/124')
+ self.pickle_test('2001:db8::1000/127') # IPV6LENGTH - 1
+ self.pickle_test('2001:db8::1000') # IPV6LENGTH
+
class InterfaceTestCase_v6(BaseTestCase, NetmaskTestMixin_v6):
factory = ipaddress.IPv6Interface
@@ -670,6 +694,119 @@ class IpaddrUnitTest(unittest.TestCase):
self.assertEqual("IPv6Interface('::1/128')",
repr(ipaddress.IPv6Interface('::1')))
+ # issue #16531: constructing IPv4Network from a (address, mask) tuple
+ def testIPv4Tuple(self):
+ # /32
+ ip = ipaddress.IPv4Address('192.0.2.1')
+ net = ipaddress.IPv4Network('192.0.2.1/32')
+ self.assertEqual(ipaddress.IPv4Network(('192.0.2.1', 32)), net)
+ self.assertEqual(ipaddress.IPv4Network((ip, 32)), net)
+ self.assertEqual(ipaddress.IPv4Network((3221225985, 32)), net)
+ self.assertEqual(ipaddress.IPv4Network(('192.0.2.1',
+ '255.255.255.255')), net)
+ self.assertEqual(ipaddress.IPv4Network((ip,
+ '255.255.255.255')), net)
+ self.assertEqual(ipaddress.IPv4Network((3221225985,
+ '255.255.255.255')), net)
+ # strict=True and host bits set
+ with self.assertRaises(ValueError):
+ ipaddress.IPv4Network(('192.0.2.1', 24))
+ with self.assertRaises(ValueError):
+ ipaddress.IPv4Network((ip, 24))
+ with self.assertRaises(ValueError):
+ ipaddress.IPv4Network((3221225985, 24))
+ with self.assertRaises(ValueError):
+ ipaddress.IPv4Network(('192.0.2.1', '255.255.255.0'))
+ with self.assertRaises(ValueError):
+ ipaddress.IPv4Network((ip, '255.255.255.0'))
+ with self.assertRaises(ValueError):
+ ipaddress.IPv4Network((3221225985, '255.255.255.0'))
+ # strict=False and host bits set
+ net = ipaddress.IPv4Network('192.0.2.0/24')
+ self.assertEqual(ipaddress.IPv4Network(('192.0.2.1', 24),
+ strict=False), net)
+ self.assertEqual(ipaddress.IPv4Network((ip, 24),
+ strict=False), net)
+ self.assertEqual(ipaddress.IPv4Network((3221225985, 24),
+ strict=False), net)
+ self.assertEqual(ipaddress.IPv4Network(('192.0.2.1',
+ '255.255.255.0'),
+ strict=False), net)
+ self.assertEqual(ipaddress.IPv4Network((ip,
+ '255.255.255.0'),
+ strict=False), net)
+ self.assertEqual(ipaddress.IPv4Network((3221225985,
+ '255.255.255.0'),
+ strict=False), net)
+
+ # /24
+ ip = ipaddress.IPv4Address('192.0.2.0')
+ net = ipaddress.IPv4Network('192.0.2.0/24')
+ self.assertEqual(ipaddress.IPv4Network(('192.0.2.0',
+ '255.255.255.0')), net)
+ self.assertEqual(ipaddress.IPv4Network((ip,
+ '255.255.255.0')), net)
+ self.assertEqual(ipaddress.IPv4Network((3221225984,
+ '255.255.255.0')), net)
+ self.assertEqual(ipaddress.IPv4Network(('192.0.2.0', 24)), net)
+ self.assertEqual(ipaddress.IPv4Network((ip, 24)), net)
+ self.assertEqual(ipaddress.IPv4Network((3221225984, 24)), net)
+
+ self.assertEqual(ipaddress.IPv4Interface(('192.0.2.1', 24)),
+ ipaddress.IPv4Interface('192.0.2.1/24'))
+ self.assertEqual(ipaddress.IPv4Interface((3221225985, 24)),
+ ipaddress.IPv4Interface('192.0.2.1/24'))
+
+ # issue #16531: constructing IPv6Network from a (address, mask) tuple
+ def testIPv6Tuple(self):
+ # /128
+ ip = ipaddress.IPv6Address('2001:db8::')
+ net = ipaddress.IPv6Network('2001:db8::/128')
+ self.assertEqual(ipaddress.IPv6Network(('2001:db8::', '128')),
+ net)
+ self.assertEqual(ipaddress.IPv6Network(
+ (42540766411282592856903984951653826560, 128)),
+ net)
+ self.assertEqual(ipaddress.IPv6Network((ip, '128')),
+ net)
+ ip = ipaddress.IPv6Address('2001:db8::')
+ net = ipaddress.IPv6Network('2001:db8::/96')
+ self.assertEqual(ipaddress.IPv6Network(('2001:db8::', '96')),
+ net)
+ self.assertEqual(ipaddress.IPv6Network(
+ (42540766411282592856903984951653826560, 96)),
+ net)
+ self.assertEqual(ipaddress.IPv6Network((ip, '96')),
+ net)
+
+ # strict=True and host bits set
+ ip = ipaddress.IPv6Address('2001:db8::1')
+ with self.assertRaises(ValueError):
+ ipaddress.IPv6Network(('2001:db8::1', 96))
+ with self.assertRaises(ValueError):
+ ipaddress.IPv6Network((
+ 42540766411282592856903984951653826561, 96))
+ with self.assertRaises(ValueError):
+ ipaddress.IPv6Network((ip, 96))
+ # strict=False and host bits set
+ net = ipaddress.IPv6Network('2001:db8::/96')
+ self.assertEqual(ipaddress.IPv6Network(('2001:db8::1', 96),
+ strict=False),
+ net)
+ self.assertEqual(ipaddress.IPv6Network(
+ (42540766411282592856903984951653826561, 96),
+ strict=False),
+ net)
+ self.assertEqual(ipaddress.IPv6Network((ip, 96), strict=False),
+ net)
+
+ # /96
+ self.assertEqual(ipaddress.IPv6Interface(('2001:db8::1', '96')),
+ ipaddress.IPv6Interface('2001:db8::1/96'))
+ self.assertEqual(ipaddress.IPv6Interface(
+ (42540766411282592856903984951653826561, '96')),
+ ipaddress.IPv6Interface('2001:db8::1/96'))
+
# issue57
def testAddressIntMath(self):
self.assertEqual(ipaddress.IPv4Address('1.1.1.1') + 255,
@@ -690,20 +827,18 @@ class IpaddrUnitTest(unittest.TestCase):
2 ** ipaddress.IPV6LENGTH)
def testInternals(self):
- first, last = ipaddress._find_address_range([
- ipaddress.IPv4Address('10.10.10.10'),
- ipaddress.IPv4Address('10.10.10.12')])
- self.assertEqual(first, last)
+ ip1 = ipaddress.IPv4Address('10.10.10.10')
+ ip2 = ipaddress.IPv4Address('10.10.10.11')
+ ip3 = ipaddress.IPv4Address('10.10.10.12')
+ self.assertEqual(list(ipaddress._find_address_range([ip1])),
+ [(ip1, ip1)])
+ self.assertEqual(list(ipaddress._find_address_range([ip1, ip3])),
+ [(ip1, ip1), (ip3, ip3)])
+ self.assertEqual(list(ipaddress._find_address_range([ip1, ip2, ip3])),
+ [(ip1, ip3)])
self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128))
self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network))
- def testMissingAddressVersion(self):
- class Broken(ipaddress._BaseAddress):
- pass
- broken = Broken('127.0.0.1')
- with self.assertRaisesRegex(NotImplementedError, "Broken.*version"):
- broken.version
-
def testMissingNetworkVersion(self):
class Broken(ipaddress._BaseNetwork):
pass
@@ -1635,6 +1770,14 @@ class IpaddrUnitTest(unittest.TestCase):
addr3.exploded)
self.assertEqual('192.168.178.1', addr4.exploded)
+ def testReversePointer(self):
+ addr1 = ipaddress.IPv4Address('127.0.0.1')
+ addr2 = ipaddress.IPv6Address('2001:db8::1')
+ self.assertEqual('1.0.0.127.in-addr.arpa', addr1.reverse_pointer)
+ self.assertEqual('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.' +
+ 'b.d.0.1.0.0.2.ip6.arpa',
+ addr2.reverse_pointer)
+
def testIntRepresentation(self):
self.assertEqual(16909060, int(self.ipv4_address))
self.assertEqual(42540616829182469433547762482097946625,
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 244598b..6a0130e 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -1856,8 +1856,6 @@ class RegressionTests(unittest.TestCase):
hist.append(3)
yield 2
hist.append(4)
- if x:
- raise StopIteration
hist = []
self.assertRaises(AssertionError, list, chain(gen1(), gen2(False)))
diff --git a/Lib/test/test_json/__init__.py b/Lib/test/test_json/__init__.py
index 2cf1032..0807e6f 100644
--- a/Lib/test/test_json/__init__.py
+++ b/Lib/test/test_json/__init__.py
@@ -9,12 +9,15 @@ from test import support
# import json with and without accelerations
cjson = support.import_fresh_module('json', fresh=['_json'])
pyjson = support.import_fresh_module('json', blocked=['_json'])
+# JSONDecodeError is cached inside the _json module
+cjson.JSONDecodeError = cjson.decoder.JSONDecodeError = json.JSONDecodeError
# create two base classes that will be used by the other tests
class PyTest(unittest.TestCase):
json = pyjson
loads = staticmethod(pyjson.loads)
dumps = staticmethod(pyjson.dumps)
+ JSONDecodeError = staticmethod(pyjson.JSONDecodeError)
@unittest.skipUnless(cjson, 'requires _json')
class CTest(unittest.TestCase):
@@ -22,6 +25,7 @@ class CTest(unittest.TestCase):
json = cjson
loads = staticmethod(cjson.loads)
dumps = staticmethod(cjson.dumps)
+ JSONDecodeError = staticmethod(cjson.JSONDecodeError)
# test PyTest and CTest checking if the functions come from the right module
class TestPyTest(PyTest):
diff --git a/Lib/test/test_json/test_decode.py b/Lib/test/test_json/test_decode.py
index 591b2e2..cc83b45 100644
--- a/Lib/test/test_json/test_decode.py
+++ b/Lib/test/test_json/test_decode.py
@@ -63,12 +63,12 @@ class TestDecode:
def test_extra_data(self):
s = '[1, 2, 3]5'
msg = 'Extra data'
- self.assertRaisesRegex(ValueError, msg, self.loads, s)
+ self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s)
def test_invalid_escape(self):
s = '["abc\\y"]'
msg = 'escape'
- self.assertRaisesRegex(ValueError, msg, self.loads, s)
+ self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s)
def test_invalid_input_type(self):
msg = 'the JSON object must be str'
@@ -80,10 +80,10 @@ class TestDecode:
def test_string_with_utf8_bom(self):
# see #18958
bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8')
- with self.assertRaises(ValueError) as cm:
+ with self.assertRaises(self.JSONDecodeError) as cm:
self.loads(bom_json)
self.assertIn('BOM', str(cm.exception))
- with self.assertRaises(ValueError) as cm:
+ with self.assertRaises(self.JSONDecodeError) as cm:
self.json.load(StringIO(bom_json))
self.assertIn('BOM', str(cm.exception))
# make sure that the BOM is not detected in the middle of a string
diff --git a/Lib/test/test_json/test_encode_basestring_ascii.py b/Lib/test/test_json/test_encode_basestring_ascii.py
index 2122da1..4bbc6c7 100644
--- a/Lib/test/test_json/test_encode_basestring_ascii.py
+++ b/Lib/test/test_json/test_encode_basestring_ascii.py
@@ -12,9 +12,6 @@ CASES = [
(' s p a c e d ', '" s p a c e d "'),
('\U0001d120', '"\\ud834\\udd20"'),
('\u03b1\u03a9', '"\\u03b1\\u03a9"'),
- ('\u03b1\u03a9', '"\\u03b1\\u03a9"'),
- ('\u03b1\u03a9', '"\\u03b1\\u03a9"'),
- ('\u03b1\u03a9', '"\\u03b1\\u03a9"'),
("`1~!@#$%^&*()_+-={':[,]}|;.</>?", '"`1~!@#$%^&*()_+-={\':[,]}|;.</>?"'),
('\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'),
('\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'),
diff --git a/Lib/test/test_json/test_fail.py b/Lib/test/test_json/test_fail.py
index 7caafdb..95ff5b8 100644
--- a/Lib/test/test_json/test_fail.py
+++ b/Lib/test/test_json/test_fail.py
@@ -87,7 +87,7 @@ class TestFail:
continue
try:
self.loads(doc)
- except ValueError:
+ except self.JSONDecodeError:
pass
else:
self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
@@ -124,10 +124,16 @@ class TestFail:
('"spam', 'Unterminated string starting at', 0),
]
for data, msg, idx in test_cases:
- self.assertRaisesRegex(ValueError,
- r'^{0}: line 1 column {1} \(char {2}\)'.format(
- re.escape(msg), idx + 1, idx),
- self.loads, data)
+ with self.assertRaises(self.JSONDecodeError) as cm:
+ self.loads(data)
+ err = cm.exception
+ self.assertEqual(err.msg, msg)
+ self.assertEqual(err.pos, idx)
+ self.assertEqual(err.lineno, 1)
+ self.assertEqual(err.colno, idx + 1)
+ self.assertEqual(str(err),
+ '%s: line 1 column %d (char %d)' %
+ (msg, idx + 1, idx))
def test_unexpected_data(self):
test_cases = [
@@ -154,10 +160,16 @@ class TestFail:
('{"spam":42,}', 'Expecting property name enclosed in double quotes', 11),
]
for data, msg, idx in test_cases:
- self.assertRaisesRegex(ValueError,
- r'^{0}: line 1 column {1} \(char {2}\)'.format(
- re.escape(msg), idx + 1, idx),
- self.loads, data)
+ with self.assertRaises(self.JSONDecodeError) as cm:
+ self.loads(data)
+ err = cm.exception
+ self.assertEqual(err.msg, msg)
+ self.assertEqual(err.pos, idx)
+ self.assertEqual(err.lineno, 1)
+ self.assertEqual(err.colno, idx + 1)
+ self.assertEqual(str(err),
+ '%s: line 1 column %d (char %d)' %
+ (msg, idx + 1, idx))
def test_extra_data(self):
test_cases = [
@@ -171,11 +183,16 @@ class TestFail:
('"spam",42', 'Extra data', 6),
]
for data, msg, idx in test_cases:
- self.assertRaisesRegex(ValueError,
- r'^{0}: line 1 column {1} - line 1 column {2}'
- r' \(char {3} - {4}\)'.format(
- re.escape(msg), idx + 1, len(data) + 1, idx, len(data)),
- self.loads, data)
+ with self.assertRaises(self.JSONDecodeError) as cm:
+ self.loads(data)
+ err = cm.exception
+ self.assertEqual(err.msg, msg)
+ self.assertEqual(err.pos, idx)
+ self.assertEqual(err.lineno, 1)
+ self.assertEqual(err.colno, idx + 1)
+ self.assertEqual(str(err),
+ '%s: line 1 column %d (char %d)' %
+ (msg, idx + 1, idx))
def test_linecol(self):
test_cases = [
@@ -185,10 +202,16 @@ class TestFail:
('\n \n\n !', 4, 6, 10),
]
for data, line, col, idx in test_cases:
- self.assertRaisesRegex(ValueError,
- r'^Expecting value: line {0} column {1}'
- r' \(char {2}\)$'.format(line, col, idx),
- self.loads, data)
+ with self.assertRaises(self.JSONDecodeError) as cm:
+ self.loads(data)
+ err = cm.exception
+ self.assertEqual(err.msg, 'Expecting value')
+ self.assertEqual(err.pos, idx)
+ self.assertEqual(err.lineno, line)
+ self.assertEqual(err.colno, col)
+ self.assertEqual(str(err),
+ 'Expecting value: line %s column %d (char %d)' %
+ (line, col, idx))
class TestPyFail(TestFail, PyTest): pass
class TestCFail(TestFail, CTest): pass
diff --git a/Lib/test/test_json/test_scanstring.py b/Lib/test/test_json/test_scanstring.py
index 07f4358..2d3ee8a 100644
--- a/Lib/test/test_json/test_scanstring.py
+++ b/Lib/test/test_json/test_scanstring.py
@@ -129,7 +129,7 @@ class TestScanstring:
'"\\ud834\\u0X20"',
]
for s in bad_escapes:
- with self.assertRaises(ValueError, msg=s):
+ with self.assertRaises(self.JSONDecodeError, msg=s):
scanstring(s, 1, True)
def test_overflow(self):
diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py
index 0c39e56..bd63e2b 100644
--- a/Lib/test/test_json/test_tool.py
+++ b/Lib/test/test_json/test_tool.py
@@ -6,6 +6,7 @@ import subprocess
from test import support
from test.script_helper import assert_python_ok
+
class TestTool(unittest.TestCase):
data = """
@@ -15,7 +16,7 @@ class TestTool(unittest.TestCase):
:"yes"} ]
"""
- expect = textwrap.dedent("""\
+ expect_without_sort_keys = textwrap.dedent("""\
[
[
"blorpie"
@@ -37,6 +38,28 @@ class TestTool(unittest.TestCase):
]
""")
+ expect = textwrap.dedent("""\
+ [
+ [
+ "blorpie"
+ ],
+ [
+ "whoops"
+ ],
+ [],
+ "d-shtaeou",
+ "d-nthiouh",
+ "i-vhbjkhnth",
+ {
+ "nifty": 87
+ },
+ {
+ "morefield": false,
+ "field": "yes"
+ }
+ ]
+ """)
+
def test_stdin_stdout(self):
with subprocess.Popen(
(sys.executable, '-m', 'json.tool'),
@@ -55,6 +78,7 @@ class TestTool(unittest.TestCase):
def test_infile_stdout(self):
infile = self._create_infile()
rc, out, err = assert_python_ok('-m', 'json.tool', infile)
+ self.assertEqual(rc, 0)
self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
self.assertEqual(err, b'')
@@ -65,5 +89,20 @@ class TestTool(unittest.TestCase):
self.addCleanup(os.remove, outfile)
with open(outfile, "r") as fp:
self.assertEqual(fp.read(), self.expect)
+ self.assertEqual(rc, 0)
self.assertEqual(out, b'')
self.assertEqual(err, b'')
+
+ def test_help_flag(self):
+ rc, out, err = assert_python_ok('-m', 'json.tool', '-h')
+ self.assertEqual(rc, 0)
+ self.assertTrue(out.startswith(b'usage: '))
+ self.assertEqual(err, b'')
+
+ def test_sort_keys_flag(self):
+ infile = self._create_infile()
+ rc, out, err = assert_python_ok('-m', 'json.tool', '--sort-keys', infile)
+ self.assertEqual(rc, 0)
+ self.assertEqual(out.splitlines(),
+ self.expect_without_sort_keys.encode().splitlines())
+ self.assertEqual(err, b'')
diff --git a/Lib/test/test_kqueue.py b/Lib/test/test_kqueue.py
index f79bd89..2585ca6 100644
--- a/Lib/test/test_kqueue.py
+++ b/Lib/test/test_kqueue.py
@@ -114,7 +114,7 @@ class TestKQueue(unittest.TestCase):
def test_queue_event(self):
serverSocket = socket.socket()
serverSocket.bind(('127.0.0.1', 0))
- serverSocket.listen(1)
+ serverSocket.listen()
client = socket.socket()
client.setblocking(False)
try:
diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py
index e979753..81fe57d 100644
--- a/Lib/test/test_locale.py
+++ b/Lib/test/test_locale.py
@@ -524,5 +524,59 @@ class TestMiscellaneous(unittest.TestCase):
locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
+class BaseDelocalizeTest(BaseLocalizedTest):
+
+ def _test_delocalize(self, value, out):
+ self.assertEqual(locale.delocalize(value), out)
+
+ def _test_atof(self, value, out):
+ self.assertEqual(locale.atof(value), out)
+
+ def _test_atoi(self, value, out):
+ self.assertEqual(locale.atoi(value), out)
+
+
+class TestEnUSDelocalize(EnUSCookedTest, BaseDelocalizeTest):
+
+ def test_delocalize(self):
+ self._test_delocalize('50000.00', '50000.00')
+ self._test_delocalize('50,000.00', '50000.00')
+
+ def test_atof(self):
+ self._test_atof('50000.00', 50000.)
+ self._test_atof('50,000.00', 50000.)
+
+ def test_atoi(self):
+ self._test_atoi('50000', 50000)
+ self._test_atoi('50,000', 50000)
+
+
+class TestCDelocalizeTest(CCookedTest, BaseDelocalizeTest):
+
+ def test_delocalize(self):
+ self._test_delocalize('50000.00', '50000.00')
+
+ def test_atof(self):
+ self._test_atof('50000.00', 50000.)
+
+ def test_atoi(self):
+ self._test_atoi('50000', 50000)
+
+
+class TestfrFRDelocalizeTest(FrFRCookedTest, BaseDelocalizeTest):
+
+ def test_delocalize(self):
+ self._test_delocalize('50000,00', '50000.00')
+ self._test_delocalize('50 000,00', '50000.00')
+
+ def test_atof(self):
+ self._test_atof('50000,00', 50000.)
+ self._test_atof('50 000,00', 50000.)
+
+ def test_atoi(self):
+ self._test_atoi('50000', 50000)
+ self._test_atoi('50 000', 50000)
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 54be217..c8b6a98 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -1,4 +1,4 @@
-# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2014 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-2013 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2014 Vinay Sajip. All Rights Reserved.
"""
import logging
@@ -34,14 +34,12 @@ import os
import queue
import random
import re
-import select
import socket
import struct
import sys
import tempfile
from test.script_helper import assert_python_ok
-from test.support import (captured_stdout, run_with_locale, run_unittest,
- patch, requires_zlib, TestHandler, Matcher, HOST)
+from test import support
import textwrap
import time
import unittest
@@ -51,16 +49,12 @@ try:
import threading
# The following imports are needed only for tests which
# require threading
- import asynchat
import asyncore
- import errno
from http.server import HTTPServer, BaseHTTPRequestHandler
import smtpd
from urllib.parse import urlparse, parse_qs
from socketserver import (ThreadingUDPServer, DatagramRequestHandler,
- ThreadingTCPServer, StreamRequestHandler,
- ThreadingUnixStreamServer,
- ThreadingUnixDatagramServer)
+ ThreadingTCPServer, StreamRequestHandler)
except ImportError:
threading = None
try:
@@ -645,22 +639,23 @@ class StreamHandlerTest(BaseTest):
h = TestStreamHandler(BadStream())
r = logging.makeLogRecord({})
old_raise = logging.raiseExceptions
- old_stderr = sys.stderr
+
try:
h.handle(r)
self.assertIs(h.error_record, r)
+
h = logging.StreamHandler(BadStream())
- sys.stderr = sio = io.StringIO()
- h.handle(r)
- self.assertIn('\nRuntimeError: deliberate mistake\n',
- sio.getvalue())
+ with support.captured_stderr() as stderr:
+ h.handle(r)
+ msg = '\nRuntimeError: deliberate mistake\n'
+ self.assertIn(msg, stderr.getvalue())
+
logging.raiseExceptions = False
- sys.stderr = sio = io.StringIO()
- h.handle(r)
- self.assertEqual('', sio.getvalue())
+ with support.captured_stderr() as stderr:
+ h.handle(r)
+ self.assertEqual('', stderr.getvalue())
finally:
logging.raiseExceptions = old_raise
- sys.stderr = old_stderr
# -- The following section could be moved into a server_helper.py module
# -- if it proves to be of wider utility than just test_logging
@@ -688,7 +683,8 @@ if threading:
"""
def __init__(self, addr, handler, poll_interval, sockmap):
- smtpd.SMTPServer.__init__(self, addr, None, map=sockmap)
+ smtpd.SMTPServer.__init__(self, addr, None, map=sockmap,
+ decode_data=True)
self.port = self.socket.getsockname()[1]
self._handler = handler
self._thread = None
@@ -930,10 +926,10 @@ class SMTPHandlerTest(BaseTest):
TIMEOUT = 8.0
def test_basic(self):
sockmap = {}
- server = TestSMTPServer((HOST, 0), self.process_message, 0.001,
+ server = TestSMTPServer((support.HOST, 0), self.process_message, 0.001,
sockmap)
server.start()
- addr = (HOST, server.port)
+ addr = (support.HOST, server.port)
h = logging.handlers.SMTPHandler(addr, 'me', 'you', 'Log',
timeout=self.TIMEOUT)
self.assertEqual(h.toaddrs, ['you'])
@@ -1249,7 +1245,7 @@ class ConfigFileTest(BaseTest):
def test_config0_ok(self):
# A simple config file which overrides the default settings.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config0)
logger = logging.getLogger()
# Won't output anything
@@ -1264,7 +1260,7 @@ class ConfigFileTest(BaseTest):
def test_config0_using_cp_ok(self):
# A simple config file which overrides the default settings.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
file = io.StringIO(textwrap.dedent(self.config0))
cp = configparser.ConfigParser()
cp.read_file(file)
@@ -1282,7 +1278,7 @@ class ConfigFileTest(BaseTest):
def test_config1_ok(self, config=config1):
# A config file defining a sub-parser as well.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(config)
logger = logging.getLogger("compiler.parser")
# Both will output a message
@@ -1305,7 +1301,7 @@ class ConfigFileTest(BaseTest):
def test_config4_ok(self):
# A config file specifying a custom formatter class.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config4)
logger = logging.getLogger()
try:
@@ -1325,7 +1321,7 @@ class ConfigFileTest(BaseTest):
self.test_config1_ok(config=self.config6)
def test_config7_ok(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config1a)
logger = logging.getLogger("compiler.parser")
# See issue #11424. compiler-hyphenated sorts
@@ -1345,7 +1341,7 @@ class ConfigFileTest(BaseTest):
], stream=output)
# Original logger output is empty.
self.assert_log_lines([])
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config7)
logger = logging.getLogger("compiler.parser")
self.assertFalse(logger.disabled)
@@ -2492,7 +2488,7 @@ class ConfigDictTest(BaseTest):
def test_config0_ok(self):
# A simple config which overrides the default settings.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config0)
logger = logging.getLogger()
# Won't output anything
@@ -2507,7 +2503,7 @@ class ConfigDictTest(BaseTest):
def test_config1_ok(self, config=config1):
# A config defining a sub-parser as well.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(config)
logger = logging.getLogger("compiler.parser")
# Both will output a message
@@ -2538,7 +2534,7 @@ class ConfigDictTest(BaseTest):
def test_config4_ok(self):
# A config specifying a custom formatter class.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config4)
#logger = logging.getLogger()
try:
@@ -2553,7 +2549,7 @@ class ConfigDictTest(BaseTest):
def test_config4a_ok(self):
# A config specifying a custom formatter class.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config4a)
#logger = logging.getLogger()
try:
@@ -2573,7 +2569,7 @@ class ConfigDictTest(BaseTest):
self.assertRaises(Exception, self.apply_config, self.config6)
def test_config7_ok(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config1)
logger = logging.getLogger("compiler.parser")
# Both will output a message
@@ -2585,7 +2581,7 @@ class ConfigDictTest(BaseTest):
], stream=output)
# Original logger output is empty.
self.assert_log_lines([])
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config7)
logger = logging.getLogger("compiler.parser")
self.assertTrue(logger.disabled)
@@ -2602,7 +2598,7 @@ class ConfigDictTest(BaseTest):
#Same as test_config_7_ok but don't disable old loggers.
def test_config_8_ok(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config1)
logger = logging.getLogger("compiler.parser")
# All will output a message
@@ -2614,7 +2610,7 @@ class ConfigDictTest(BaseTest):
], stream=output)
# Original logger output is empty.
self.assert_log_lines([])
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config8)
logger = logging.getLogger("compiler.parser")
self.assertFalse(logger.disabled)
@@ -2635,7 +2631,7 @@ class ConfigDictTest(BaseTest):
self.assert_log_lines([])
def test_config_8a_ok(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config1a)
logger = logging.getLogger("compiler.parser")
# See issue #11424. compiler-hyphenated sorts
@@ -2655,7 +2651,7 @@ class ConfigDictTest(BaseTest):
], stream=output)
# Original logger output is empty.
self.assert_log_lines([])
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config8a)
logger = logging.getLogger("compiler.parser")
self.assertFalse(logger.disabled)
@@ -2678,7 +2674,7 @@ class ConfigDictTest(BaseTest):
self.assert_log_lines([])
def test_config_9_ok(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config9)
logger = logging.getLogger("compiler.parser")
#Nothing will be output since both handler and logger are set to WARNING
@@ -2696,7 +2692,7 @@ class ConfigDictTest(BaseTest):
], stream=output)
def test_config_10_ok(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config10)
logger = logging.getLogger("compiler.parser")
logger.warning(self.next_message())
@@ -2724,7 +2720,7 @@ class ConfigDictTest(BaseTest):
self.assertRaises(Exception, self.apply_config, self.config13)
def test_config14_ok(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.apply_config(self.config14)
h = logging._handlers['hand1']
self.assertEqual(h.foo, 'bar')
@@ -2763,7 +2759,7 @@ class ConfigDictTest(BaseTest):
@unittest.skipUnless(threading, 'Threading required for this test.')
def test_listen_config_10_ok(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.setup_via_listener(json.dumps(self.config10))
logger = logging.getLogger("compiler.parser")
logger.warning(self.next_message())
@@ -2783,7 +2779,7 @@ class ConfigDictTest(BaseTest):
@unittest.skipUnless(threading, 'Threading required for this test.')
def test_listen_config_1_ok(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.setup_via_listener(textwrap.dedent(ConfigFileTest.config1))
logger = logging.getLogger("compiler.parser")
# Both will output a message
@@ -2810,7 +2806,7 @@ class ConfigDictTest(BaseTest):
# First, specify a verification function that will fail.
# We expect to see no output, since our configuration
# never took effect.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.setup_via_listener(to_send, verify_fail)
# Both will output a message
logger.info(self.next_message())
@@ -2825,7 +2821,7 @@ class ConfigDictTest(BaseTest):
# Now, perform no verification. Our configuration
# should take effect.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.setup_via_listener(to_send) # no verify callable specified
logger = logging.getLogger("compiler.parser")
# Both will output a message
@@ -2843,7 +2839,7 @@ class ConfigDictTest(BaseTest):
# Now, perform verification which transforms the bytes.
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
self.setup_via_listener(to_send[::-1], verify_reverse)
logger = logging.getLogger("compiler.parser")
# Both will output a message
@@ -2998,7 +2994,7 @@ class QueueHandlerTest(BaseTest):
@unittest.skipUnless(hasattr(logging.handlers, 'QueueListener'),
'logging.handlers.QueueListener required for this test')
def test_queue_listener(self):
- handler = TestHandler(Matcher())
+ handler = support.TestHandler(support.Matcher())
listener = logging.handlers.QueueListener(self.queue, handler)
listener.start()
try:
@@ -3010,6 +3006,25 @@ class QueueHandlerTest(BaseTest):
self.assertTrue(handler.matches(levelno=logging.WARNING, message='1'))
self.assertTrue(handler.matches(levelno=logging.ERROR, message='2'))
self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='3'))
+ handler.close()
+
+ # Now test with respect_handler_level set
+
+ handler = support.TestHandler(support.Matcher())
+ handler.setLevel(logging.CRITICAL)
+ listener = logging.handlers.QueueListener(self.queue, handler,
+ respect_handler_level=True)
+ listener.start()
+ try:
+ self.que_logger.warning(self.next_message())
+ self.que_logger.error(self.next_message())
+ self.que_logger.critical(self.next_message())
+ finally:
+ listener.stop()
+ self.assertFalse(handler.matches(levelno=logging.WARNING, message='4'))
+ self.assertFalse(handler.matches(levelno=logging.ERROR, message='5'))
+ self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='6'))
+
ZERO = datetime.timedelta(0)
@@ -3166,32 +3181,35 @@ class LastResortTest(BaseTest):
# Test the last resort handler
root = self.root_logger
root.removeHandler(self.root_hdlr)
- old_stderr = sys.stderr
old_lastresort = logging.lastResort
old_raise_exceptions = logging.raiseExceptions
+
try:
- sys.stderr = sio = io.StringIO()
- root.debug('This should not appear')
- self.assertEqual(sio.getvalue(), '')
- root.warning('This is your final chance!')
- self.assertEqual(sio.getvalue(), 'This is your final chance!\n')
- #No handlers and no last resort, so 'No handlers' message
+ with support.captured_stderr() as stderr:
+ root.debug('This should not appear')
+ self.assertEqual(stderr.getvalue(), '')
+ root.warning('Final chance!')
+ self.assertEqual(stderr.getvalue(), 'Final chance!\n')
+
+ # No handlers and no last resort, so 'No handlers' message
logging.lastResort = None
- sys.stderr = sio = io.StringIO()
- root.warning('This is your final chance!')
- self.assertEqual(sio.getvalue(), 'No handlers could be found for logger "root"\n')
+ with support.captured_stderr() as stderr:
+ root.warning('Final chance!')
+ msg = 'No handlers could be found for logger "root"\n'
+ self.assertEqual(stderr.getvalue(), msg)
+
# 'No handlers' message only printed once
- sys.stderr = sio = io.StringIO()
- root.warning('This is your final chance!')
- self.assertEqual(sio.getvalue(), '')
+ with support.captured_stderr() as stderr:
+ root.warning('Final chance!')
+ self.assertEqual(stderr.getvalue(), '')
+
+ # If raiseExceptions is False, no message is printed
root.manager.emittedNoHandlerWarning = False
- #If raiseExceptions is False, no message is printed
logging.raiseExceptions = False
- sys.stderr = sio = io.StringIO()
- root.warning('This is your final chance!')
- self.assertEqual(sio.getvalue(), '')
+ with support.captured_stderr() as stderr:
+ root.warning('Final chance!')
+ self.assertEqual(stderr.getvalue(), '')
finally:
- sys.stderr = old_stderr
root.addHandler(self.root_hdlr)
logging.lastResort = old_lastresort
logging.raiseExceptions = old_raise_exceptions
@@ -3322,8 +3340,8 @@ class ModuleLevelMiscTest(BaseTest):
def _test_log(self, method, level=None):
called = []
- patch(self, logging, 'basicConfig',
- lambda *a, **kw: called.append((a, kw)))
+ support.patch(self, logging, 'basicConfig',
+ lambda *a, **kw: called.append((a, kw)))
recording = RecordingHandler()
logging.root.addHandler(recording)
@@ -3494,7 +3512,7 @@ class BasicConfigTest(unittest.TestCase):
self.assertEqual(logging.root.level, self.original_logging_level)
def test_strformatstyle(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
logging.basicConfig(stream=sys.stdout, style="{")
logging.error("Log an error")
sys.stdout.seek(0)
@@ -3502,7 +3520,7 @@ class BasicConfigTest(unittest.TestCase):
"ERROR:root:Log an error")
def test_stringtemplatestyle(self):
- with captured_stdout() as output:
+ with support.captured_stdout() as output:
logging.basicConfig(stream=sys.stdout, style="$")
logging.error("Log an error")
sys.stdout.seek(0)
@@ -3619,7 +3637,7 @@ class BasicConfigTest(unittest.TestCase):
self.addCleanup(logging.root.setLevel, old_level)
called.append((a, kw))
- patch(self, logging, 'basicConfig', my_basic_config)
+ support.patch(self, logging, 'basicConfig', my_basic_config)
log_method = getattr(logging, method)
if level is not None:
@@ -3685,6 +3703,19 @@ class LoggerAdapterTest(unittest.TestCase):
self.assertEqual(record.exc_info,
(exc.__class__, exc, exc.__traceback__))
+ def test_exception_excinfo(self):
+ try:
+ 1 / 0
+ except ZeroDivisionError as e:
+ exc = e
+
+ self.adapter.exception('exc_info test', exc_info=exc)
+
+ self.assertEqual(len(self.recording.records), 1)
+ record = self.recording.records[0]
+ self.assertEqual(record.exc_info,
+ (exc.__class__, exc, exc.__traceback__))
+
def test_critical(self):
msg = 'critical test! %r'
self.adapter.critical(msg, self.recording)
@@ -3759,8 +3790,8 @@ class LoggerTest(BaseTest):
def test_find_caller_with_stack_info(self):
called = []
- patch(self, logging.traceback, 'print_stack',
- lambda f, file: called.append(file.getvalue()))
+ support.patch(self, logging.traceback, 'print_stack',
+ lambda f, file: called.append(file.getvalue()))
self.logger.findCaller(stack_info=True)
@@ -3897,7 +3928,7 @@ class RotatingFileHandlerTest(BaseFileTest):
self.assertFalse(os.path.exists(namer(self.fn + ".3")))
rh.close()
- @requires_zlib
+ @support.requires_zlib
def test_rotator(self):
def namer(name):
return name + ".gz"
@@ -4130,22 +4161,20 @@ class NTEventLogHandlerTest(BaseTest):
# 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.
-@run_with_locale('LC_ALL', '')
+@support.run_with_locale('LC_ALL', '')
def test_main():
- run_unittest(BuiltinLevelsTest, BasicFilterTest,
- CustomLevelsAndFiltersTest, HandlerTest, MemoryHandlerTest,
- ConfigFileTest, SocketHandlerTest, DatagramHandlerTest,
- MemoryTest, EncodingTest, WarningsTest, ConfigDictTest,
- ManagerTest, FormatterTest, BufferingFormatterTest,
- StreamHandlerTest, LogRecordFactoryTest, ChildLoggerTest,
- QueueHandlerTest, ShutdownTest, ModuleLevelMiscTest,
- BasicConfigTest, LoggerAdapterTest, LoggerTest,
- SMTPHandlerTest, FileHandlerTest, RotatingFileHandlerTest,
- LastResortTest, LogRecordTest, ExceptionTest,
- SysLogHandlerTest, HTTPHandlerTest, NTEventLogHandlerTest,
- TimedRotatingFileHandlerTest, UnixSocketHandlerTest,
- UnixDatagramHandlerTest, UnixSysLogHandlerTest
- )
+ support.run_unittest(
+ BuiltinLevelsTest, BasicFilterTest, CustomLevelsAndFiltersTest,
+ HandlerTest, MemoryHandlerTest, ConfigFileTest, SocketHandlerTest,
+ DatagramHandlerTest, MemoryTest, EncodingTest, WarningsTest,
+ ConfigDictTest, ManagerTest, FormatterTest, BufferingFormatterTest,
+ StreamHandlerTest, LogRecordFactoryTest, ChildLoggerTest,
+ QueueHandlerTest, ShutdownTest, ModuleLevelMiscTest, BasicConfigTest,
+ LoggerAdapterTest, LoggerTest, SMTPHandlerTest, FileHandlerTest,
+ RotatingFileHandlerTest, LastResortTest, LogRecordTest,
+ ExceptionTest, SysLogHandlerTest, HTTPHandlerTest,
+ NTEventLogHandlerTest, TimedRotatingFileHandlerTest,
+ UnixSocketHandlerTest, UnixDatagramHandlerTest, UnixSysLogHandlerTest)
if __name__ == "__main__":
test_main()
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index 5f14795..57847d8 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -599,8 +599,6 @@ class LongTest(unittest.TestCase):
return (x > y) - (x < y)
def __eq__(self, other):
return self._cmp__(other) == 0
- def __ne__(self, other):
- return self._cmp__(other) != 0
def __ge__(self, other):
return self._cmp__(other) >= 0
def __gt__(self, other):
diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py
index 07fadbd..cded28c 100644
--- a/Lib/test/test_lzma.py
+++ b/Lib/test/test_lzma.py
@@ -135,6 +135,97 @@ class CompressorDecompressorTestCase(unittest.TestCase):
self.assertTrue(lzd.eof)
self.assertEqual(lzd.unused_data, b"")
+ def test_decompressor_chunks_maxsize(self):
+ lzd = LZMADecompressor()
+ max_length = 100
+ out = []
+
+ # Feed first half the input
+ len_ = len(COMPRESSED_XZ) // 2
+ out.append(lzd.decompress(COMPRESSED_XZ[:len_],
+ max_length=max_length))
+ self.assertFalse(lzd.needs_input)
+ self.assertEqual(len(out[-1]), max_length)
+
+ # Retrieve more data without providing more input
+ out.append(lzd.decompress(b'', max_length=max_length))
+ self.assertFalse(lzd.needs_input)
+ self.assertEqual(len(out[-1]), max_length)
+
+ # Retrieve more data while providing more input
+ out.append(lzd.decompress(COMPRESSED_XZ[len_:],
+ max_length=max_length))
+ self.assertLessEqual(len(out[-1]), max_length)
+
+ # Retrieve remaining uncompressed data
+ while not lzd.eof:
+ out.append(lzd.decompress(b'', max_length=max_length))
+ self.assertLessEqual(len(out[-1]), max_length)
+
+ out = b"".join(out)
+ self.assertEqual(out, INPUT)
+ self.assertEqual(lzd.check, lzma.CHECK_CRC64)
+ self.assertEqual(lzd.unused_data, b"")
+
+ def test_decompressor_inputbuf_1(self):
+ # Test reusing input buffer after moving existing
+ # contents to beginning
+ lzd = LZMADecompressor()
+ out = []
+
+ # Create input buffer and fill it
+ self.assertEqual(lzd.decompress(COMPRESSED_XZ[:100],
+ max_length=0), b'')
+
+ # Retrieve some results, freeing capacity at beginning
+ # of input buffer
+ out.append(lzd.decompress(b'', 2))
+
+ # Add more data that fits into input buffer after
+ # moving existing data to beginning
+ out.append(lzd.decompress(COMPRESSED_XZ[100:105], 15))
+
+ # Decompress rest of data
+ out.append(lzd.decompress(COMPRESSED_XZ[105:]))
+ self.assertEqual(b''.join(out), INPUT)
+
+ def test_decompressor_inputbuf_2(self):
+ # Test reusing input buffer by appending data at the
+ # end right away
+ lzd = LZMADecompressor()
+ out = []
+
+ # Create input buffer and empty it
+ self.assertEqual(lzd.decompress(COMPRESSED_XZ[:200],
+ max_length=0), b'')
+ out.append(lzd.decompress(b''))
+
+ # Fill buffer with new data
+ out.append(lzd.decompress(COMPRESSED_XZ[200:280], 2))
+
+ # Append some more data, not enough to require resize
+ out.append(lzd.decompress(COMPRESSED_XZ[280:300], 2))
+
+ # Decompress rest of data
+ out.append(lzd.decompress(COMPRESSED_XZ[300:]))
+ self.assertEqual(b''.join(out), INPUT)
+
+ def test_decompressor_inputbuf_3(self):
+ # Test reusing input buffer after extending it
+
+ lzd = LZMADecompressor()
+ out = []
+
+ # Create almost full input buffer
+ out.append(lzd.decompress(COMPRESSED_XZ[:200], 5))
+
+ # Add even more data to it, requiring resize
+ out.append(lzd.decompress(COMPRESSED_XZ[200:300], 5))
+
+ # Decompress rest of data
+ out.append(lzd.decompress(COMPRESSED_XZ[300:]))
+ self.assertEqual(b''.join(out), INPUT)
+
def test_decompressor_unused_data(self):
lzd = LZMADecompressor()
extra = b"fooblibar"
diff --git a/Lib/test/test_macpath.py b/Lib/test/test_macpath.py
index 22f8491..80bec7a 100644
--- a/Lib/test/test_macpath.py
+++ b/Lib/test/test_macpath.py
@@ -142,6 +142,8 @@ class MacPathTestCase(unittest.TestCase):
class MacCommonTest(test_genericpath.CommonTest, unittest.TestCase):
pathmodule = macpath
+ test_relpath_errors = None
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py
index 903e12c..c7def9a 100644
--- a/Lib/test/test_marshal.py
+++ b/Lib/test/test_marshal.py
@@ -193,7 +193,7 @@ class BugsTestCase(unittest.TestCase):
head = last = []
# The max stack depth should match the value in Python/marshal.c.
if os.name == 'nt' and hasattr(sys, 'gettotalrefcount'):
- MAX_MARSHAL_STACK_DEPTH = 1500
+ MAX_MARSHAL_STACK_DEPTH = 1000
else:
MAX_MARSHAL_STACK_DEPTH = 2000
for i in range(MAX_MARSHAL_STACK_DEPTH - 2):
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index 48f84ba..023dea9 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -422,9 +422,17 @@ class MathTests(unittest.TestCase):
self.assertEqual(math.factorial(i), py_factorial(i))
self.assertRaises(ValueError, math.factorial, -1)
self.assertRaises(ValueError, math.factorial, -1.0)
+ self.assertRaises(ValueError, math.factorial, -10**100)
+ self.assertRaises(ValueError, math.factorial, -1e100)
self.assertRaises(ValueError, math.factorial, math.pi)
- self.assertRaises(OverflowError, math.factorial, sys.maxsize+1)
- self.assertRaises(OverflowError, math.factorial, 10e100)
+
+ # Other implementations may place different upper bounds.
+ @support.cpython_only
+ def testFactorialHugeInputs(self):
+ # Currently raises ValueError for inputs that are too large
+ # to fit into a C long.
+ self.assertRaises(OverflowError, math.factorial, 10**100)
+ self.assertRaises(OverflowError, math.factorial, 1e100)
def testFloor(self):
self.assertRaises(TypeError, math.floor)
@@ -975,6 +983,17 @@ class MathTests(unittest.TestCase):
self.assertFalse(math.isinf(0.))
self.assertFalse(math.isinf(1.))
+ @requires_IEEE_754
+ def test_nan_constant(self):
+ self.assertTrue(math.isnan(math.nan))
+
+ @requires_IEEE_754
+ def test_inf_constant(self):
+ self.assertTrue(math.isinf(math.inf))
+ self.assertGreater(math.inf, 0.0)
+ self.assertEqual(math.inf, float("inf"))
+ self.assertEqual(-math.inf, float("-inf"))
+
# RED_FLAG 16-Oct-2000 Tim
# While 2.0 is more consistent about exceptions than previous releases, it
# still fails this part of the test on some platforms. For now, we only
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py
index 9a2461d..77749e7 100644
--- a/Lib/test/test_memoryio.py
+++ b/Lib/test/test_memoryio.py
@@ -9,6 +9,7 @@ from test import support
import io
import _pyio as pyio
import pickle
+import sys
class MemorySeekTestMixin:
@@ -717,12 +718,56 @@ class CBytesIOTest(PyBytesIOTest):
@support.cpython_only
def test_sizeof(self):
- basesize = support.calcobjsize('P2nN2Pn')
+ basesize = support.calcobjsize('P2n2Pn')
check = self.check_sizeof
self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
check(io.BytesIO(), basesize )
- check(io.BytesIO(b'a'), basesize + 1 + 1 )
- check(io.BytesIO(b'a' * 1000), basesize + 1000 + 1 )
+ check(io.BytesIO(b'a' * 1000), basesize + sys.getsizeof(b'a' * 1000))
+
+ # Various tests of copy-on-write behaviour for BytesIO.
+
+ def _test_cow_mutation(self, mutation):
+ # Common code for all BytesIO copy-on-write mutation tests.
+ imm = b' ' * 1024
+ old_rc = sys.getrefcount(imm)
+ memio = self.ioclass(imm)
+ self.assertEqual(sys.getrefcount(imm), old_rc + 1)
+ mutation(memio)
+ self.assertEqual(sys.getrefcount(imm), old_rc)
+
+ @support.cpython_only
+ def test_cow_truncate(self):
+ # Ensure truncate causes a copy.
+ def mutation(memio):
+ memio.truncate(1)
+ self._test_cow_mutation(mutation)
+
+ @support.cpython_only
+ def test_cow_write(self):
+ # Ensure write that would not cause a resize still results in a copy.
+ def mutation(memio):
+ memio.seek(0)
+ memio.write(b'foo')
+ self._test_cow_mutation(mutation)
+
+ @support.cpython_only
+ def test_cow_setstate(self):
+ # __setstate__ should cause buffer to be released.
+ memio = self.ioclass(b'foooooo')
+ state = memio.__getstate__()
+ def mutation(memio):
+ memio.__setstate__(state)
+ self._test_cow_mutation(mutation)
+
+ @support.cpython_only
+ def test_cow_mutable(self):
+ # BytesIO should accept only Bytes for copy-on-write sharing, since
+ # arbitrary buffer-exporting objects like bytearray() aren't guaranteed
+ # to be immutable.
+ ba = bytearray(1024)
+ old_rc = sys.getrefcount(ba)
+ memio = self.ioclass(ba)
+ self.assertEqual(sys.getrefcount(ba), old_rc)
class CStringIOTest(PyStringIOTest):
ioclass = io.StringIO
diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py
index 4bc3133..e9bd9fd 100644
--- a/Lib/test/test_memoryview.py
+++ b/Lib/test/test_memoryview.py
@@ -369,12 +369,12 @@ class AbstractMemoryTests:
d = memoryview(b)
del b
-
+
self.assertEqual(c[0], 256)
self.assertEqual(d[0], 256)
self.assertEqual(c.format, "H")
self.assertEqual(d.format, "H")
-
+
_ = m.cast('I')
self.assertEqual(c[0], 256)
self.assertEqual(d[0], 256)
diff --git a/Lib/test/test_module.py b/Lib/test/test_module.py
index 1230293..9da3536 100644
--- a/Lib/test/test_module.py
+++ b/Lib/test/test_module.py
@@ -30,6 +30,22 @@ class ModuleTests(unittest.TestCase):
pass
self.assertEqual(foo.__doc__, ModuleType.__doc__)
+ def test_unintialized_missing_getattr(self):
+ # Issue 8297
+ # test the text in the AttributeError of an uninitialized module
+ foo = ModuleType.__new__(ModuleType)
+ self.assertRaisesRegex(
+ AttributeError, "module has no attribute 'not_here'",
+ getattr, foo, "not_here")
+
+ def test_missing_getattr(self):
+ # Issue 8297
+ # test the text in the AttributeError
+ foo = ModuleType("foo")
+ self.assertRaisesRegex(
+ AttributeError, "module 'foo' has no attribute 'not_here'",
+ getattr, foo, "not_here")
+
def test_no_docstring(self):
# Regularly initialized module, no docstring
foo = ModuleType("foo")
@@ -211,6 +227,14 @@ a = A(destroyed)"""
b"len = len",
b"shutil.rmtree = rmtree"})
+ def test_descriptor_errors_propogate(self):
+ class Descr:
+ def __get__(self, o, t):
+ raise RuntimeError
+ class M(ModuleType):
+ melon = Descr()
+ self.assertRaises(RuntimeError, getattr, M("mymod"), "melon")
+
# frozen and namespace module reprs are tested in importlib.
diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py
index ab58a98..1bd0391 100644
--- a/Lib/test/test_operator.py
+++ b/Lib/test/test_operator.py
@@ -203,6 +203,15 @@ class OperatorTestCase:
self.assertRaises(TypeError, operator.mul, None, None)
self.assertTrue(operator.mul(5, 2) == 10)
+ def test_matmul(self):
+ operator = self.module
+ self.assertRaises(TypeError, operator.matmul)
+ self.assertRaises(TypeError, operator.matmul, 42, 42)
+ class M:
+ def __matmul__(self, other):
+ return other - 1
+ self.assertEqual(M() @ 42, 41)
+
def test_neg(self):
operator = self.module
self.assertRaises(TypeError, operator.neg)
@@ -416,6 +425,7 @@ class OperatorTestCase:
def __ilshift__ (self, other): return "ilshift"
def __imod__ (self, other): return "imod"
def __imul__ (self, other): return "imul"
+ def __imatmul__ (self, other): return "imatmul"
def __ior__ (self, other): return "ior"
def __ipow__ (self, other): return "ipow"
def __irshift__ (self, other): return "irshift"
@@ -430,6 +440,7 @@ class OperatorTestCase:
self.assertEqual(operator.ilshift (c, 5), "ilshift")
self.assertEqual(operator.imod (c, 5), "imod")
self.assertEqual(operator.imul (c, 5), "imul")
+ self.assertEqual(operator.imatmul (c, 5), "imatmul")
self.assertEqual(operator.ior (c, 5), "ior")
self.assertEqual(operator.ipow (c, 5), "ipow")
self.assertEqual(operator.irshift (c, 5), "irshift")
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 152ba93..e28a3db 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -4,6 +4,7 @@
import os
import errno
+import getpass
import unittest
import warnings
import sys
@@ -40,9 +41,35 @@ try:
import fcntl
except ImportError:
fcntl = None
+try:
+ import _winapi
+except ImportError:
+ _winapi = None
+try:
+ import grp
+ groups = [g.gr_gid for g in grp.getgrall() if getpass.getuser() in g.gr_mem]
+ if hasattr(os, 'getgid'):
+ process_gid = os.getgid()
+ if process_gid not in groups:
+ groups.append(process_gid)
+except ImportError:
+ groups = []
+try:
+ import pwd
+ all_users = [u.pw_uid for u in pwd.getpwall()]
+except ImportError:
+ all_users = []
+try:
+ from _testcapi import INT_MAX, PY_SSIZE_T_MAX
+except ImportError:
+ INT_MAX = PY_SSIZE_T_MAX = sys.maxsize
from test.script_helper import assert_python_ok
+root_in_posix = False
+if hasattr(os, 'geteuid'):
+ root_in_posix = (os.geteuid() == 0)
+
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
os.stat_float_times(True)
@@ -116,6 +143,26 @@ class FileTests(unittest.TestCase):
self.assertEqual(type(s), bytes)
self.assertEqual(s, b"spam")
+ @support.cpython_only
+ # Skip the test on 32-bit platforms: the number of bytes must fit in a
+ # Py_ssize_t type
+ @unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX,
+ "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)
+
+ # Issue #21932: Make sure that os.read() does not raise an
+ # OverflowError for size larger than INT_MAX
+ with open(support.TESTFN, "rb") as fp:
+ data = os.read(fp.fileno(), size)
+
+ # The test does not try to read more than 2 GB at once because the
+ # operating system is free to return less bytes than requested.
+ self.assertEqual(data, b'test')
+
def test_write(self):
# os.write() accepts bytes- and buffer-like objects but not strings
fd = os.open(support.TESTFN, os.O_CREAT | os.O_WRONLY)
@@ -533,6 +580,28 @@ class StatAttributeTests(unittest.TestCase):
os.stat(r)
self.assertEqual(ctx.exception.errno, errno.EBADF)
+ def check_file_attributes(self, result):
+ self.assertTrue(hasattr(result, 'st_file_attributes'))
+ self.assertTrue(isinstance(result.st_file_attributes, int))
+ self.assertTrue(0 <= result.st_file_attributes <= 0xFFFFFFFF)
+
+ @unittest.skipUnless(sys.platform == "win32",
+ "st_file_attributes is Win32 specific")
+ def test_file_attributes(self):
+ # test file st_file_attributes (FILE_ATTRIBUTE_DIRECTORY not set)
+ result = os.stat(self.fname)
+ self.check_file_attributes(result)
+ self.assertEqual(
+ result.st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY,
+ 0)
+
+ # test directory st_file_attributes (FILE_ATTRIBUTE_DIRECTORY set)
+ result = os.stat(support.TESTFN)
+ self.check_file_attributes(result)
+ self.assertEqual(
+ result.st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY,
+ stat.FILE_ATTRIBUTE_DIRECTORY)
+
from test import mapping_tests
class EnvironTests(mapping_tests.BasicTestMappingProtocol):
@@ -912,17 +981,6 @@ class MakedirTests(unittest.TestCase):
os.makedirs(path, mode=mode, exist_ok=True)
os.umask(old_mask)
- @unittest.skipUnless(hasattr(os, 'chown'), 'test needs os.chown')
- def test_chown_uid_gid_arguments_must_be_index(self):
- stat = os.stat(support.TESTFN)
- uid = stat.st_uid
- gid = stat.st_gid
- for value in (-1.0, -1j, decimal.Decimal(-1), fractions.Fraction(-2, 2)):
- self.assertRaises(TypeError, os.chown, support.TESTFN, value, gid)
- self.assertRaises(TypeError, os.chown, support.TESTFN, uid, value)
- self.assertIsNone(os.chown(support.TESTFN, uid, gid))
- self.assertIsNone(os.chown(support.TESTFN, -1, -1))
-
def test_exist_ok_s_isgid_directory(self):
path = os.path.join(support.TESTFN, 'dir1')
S_ISGID = stat.S_ISGID
@@ -973,6 +1031,58 @@ class MakedirTests(unittest.TestCase):
os.removedirs(path)
+@unittest.skipUnless(hasattr(os, 'chown'), "Test needs chown")
+class ChownFileTests(unittest.TestCase):
+
+ def setUpClass():
+ os.mkdir(support.TESTFN)
+
+ def test_chown_uid_gid_arguments_must_be_index(self):
+ stat = os.stat(support.TESTFN)
+ uid = stat.st_uid
+ gid = stat.st_gid
+ for value in (-1.0, -1j, decimal.Decimal(-1), fractions.Fraction(-2, 2)):
+ self.assertRaises(TypeError, os.chown, support.TESTFN, value, gid)
+ self.assertRaises(TypeError, os.chown, support.TESTFN, uid, value)
+ self.assertIsNone(os.chown(support.TESTFN, uid, gid))
+ self.assertIsNone(os.chown(support.TESTFN, -1, -1))
+
+ @unittest.skipUnless(len(groups) > 1, "test needs more than one group")
+ def test_chown(self):
+ gid_1, gid_2 = groups[:2]
+ uid = os.stat(support.TESTFN).st_uid
+ os.chown(support.TESTFN, uid, gid_1)
+ gid = os.stat(support.TESTFN).st_gid
+ self.assertEqual(gid, gid_1)
+ os.chown(support.TESTFN, uid, gid_2)
+ gid = os.stat(support.TESTFN).st_gid
+ self.assertEqual(gid, gid_2)
+
+ @unittest.skipUnless(root_in_posix and len(all_users) > 1,
+ "test needs root privilege and more than one user")
+ def test_chown_with_root(self):
+ uid_1, uid_2 = all_users[:2]
+ gid = os.stat(support.TESTFN).st_gid
+ os.chown(support.TESTFN, uid_1, gid)
+ uid = os.stat(support.TESTFN).st_uid
+ self.assertEqual(uid, uid_1)
+ os.chown(support.TESTFN, uid_2, gid)
+ uid = os.stat(support.TESTFN).st_uid
+ self.assertEqual(uid, uid_2)
+
+ @unittest.skipUnless(not root_in_posix and len(all_users) > 1,
+ "test needs non-root account and more than one user")
+ def test_chown_without_permission(self):
+ uid_1, uid_2 = all_users[:2]
+ gid = os.stat(support.TESTFN).st_gid
+ with self.assertRaisesRegex(PermissionError, "Operation not permitted"):
+ os.chown(support.TESTFN, uid_1, gid)
+ os.chown(support.TESTFN, uid_2, gid)
+
+ def tearDownClass():
+ os.rmdir(support.TESTFN)
+
+
class RemoveDirsTests(unittest.TestCase):
def setUp(self):
os.makedirs(support.TESTFN)
@@ -1339,6 +1449,16 @@ class TestInvalidFD(unittest.TestCase):
def test_writev(self):
self.check(os.writev, [b'abc'])
+ def test_inheritable(self):
+ self.check(os.get_inheritable)
+ self.check(os.set_inheritable, True)
+
+ @unittest.skipUnless(hasattr(os, 'get_blocking'),
+ 'needs os.get_blocking() and os.set_blocking()')
+ def test_blocking(self):
+ self.check(os.get_blocking)
+ self.check(os.set_blocking, True)
+
class LinkTests(unittest.TestCase):
def setUp(self):
@@ -1786,6 +1906,37 @@ class Win32SymlinkTests(unittest.TestCase):
shutil.rmtree(level1)
+@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
+class Win32JunctionTests(unittest.TestCase):
+ junction = 'junctiontest'
+ junction_target = os.path.dirname(os.path.abspath(__file__))
+
+ def setUp(self):
+ assert os.path.exists(self.junction_target)
+ assert not os.path.exists(self.junction)
+
+ def tearDown(self):
+ if os.path.exists(self.junction):
+ # os.rmdir delegates to Windows' RemoveDirectoryW,
+ # which removes junction points safely.
+ os.rmdir(self.junction)
+
+ def test_create_junction(self):
+ _winapi.CreateJunction(self.junction_target, self.junction)
+ self.assertTrue(os.path.exists(self.junction))
+ self.assertTrue(os.path.isdir(self.junction))
+
+ # Junctions are not recognized as links.
+ self.assertFalse(os.path.islink(self.junction))
+
+ def test_unlink_removes_junction(self):
+ _winapi.CreateJunction(self.junction_target, self.junction)
+ self.assertTrue(os.path.exists(self.junction))
+
+ os.unlink(self.junction)
+ self.assertFalse(os.path.exists(self.junction))
+
+
@support.skip_unless_symlink
class NonLocalSymlinkTests(unittest.TestCase):
@@ -1992,11 +2143,13 @@ class TestSendfile(unittest.TestCase):
@classmethod
def setUpClass(cls):
+ cls.key = support.threading_setup()
with open(support.TESTFN, "wb") as f:
f.write(cls.DATA)
@classmethod
def tearDownClass(cls):
+ support.threading_cleanup(*cls.key)
support.unlink(support.TESTFN)
def setUp(self):
@@ -2523,41 +2676,27 @@ class FDInheritanceTests(unittest.TestCase):
self.assertEqual(os.get_inheritable(slave_fd), False)
-@support.reap_threads
-def test_main():
- support.run_unittest(
- FileTests,
- StatAttributeTests,
- EnvironTests,
- WalkTests,
- FwalkTests,
- MakedirTests,
- DevNullTests,
- URandomTests,
- ExecTests,
- Win32ErrorTests,
- TestInvalidFD,
- PosixUidGidTests,
- Pep383Tests,
- Win32KillTests,
- Win32ListdirTests,
- Win32SymlinkTests,
- NonLocalSymlinkTests,
- FSEncodingTests,
- DeviceEncodingTests,
- PidTests,
- LoginTests,
- LinkTests,
- TestSendfile,
- ProgramPriorityTests,
- ExtendedAttributeTests,
- Win32DeprecatedBytesAPI,
- TermsizeTests,
- OSErrorTests,
- RemoveDirsTests,
- CPUCountTests,
- FDInheritanceTests,
- )
+@unittest.skipUnless(hasattr(os, 'get_blocking'),
+ 'needs os.get_blocking() and os.set_blocking()')
+class BlockingTests(unittest.TestCase):
+ def test_blocking(self):
+ fd = os.open(__file__, os.O_RDONLY)
+ self.addCleanup(os.close, fd)
+ self.assertEqual(os.get_blocking(fd), True)
+
+ os.set_blocking(fd, False)
+ self.assertEqual(os.get_blocking(fd), False)
+
+ os.set_blocking(fd, True)
+ self.assertEqual(os.get_blocking(fd), True)
+
+
+
+class ExportsTests(unittest.TestCase):
+ def test_os_all(self):
+ self.assertIn('open', os.__all__)
+ self.assertIn('walk', os.__all__)
+
if __name__ == "__main__":
- test_main()
+ unittest.main()
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 11420e2..1c53ab7 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -4,13 +4,10 @@ import os
import errno
import pathlib
import pickle
-import shutil
import socket
import stat
-import sys
import tempfile
import unittest
-from contextlib import contextmanager
from test import support
TESTFN = support.TESTFN
@@ -747,7 +744,6 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
self.assertEqual(P('//Some/SHARE/a/B'), P('//somE/share/A/b'))
def test_as_uri(self):
- from urllib.parse import quote_from_bytes
P = self.cls
with self.assertRaises(ValueError):
P('/a/b').as_uri()
@@ -1269,11 +1265,55 @@ class _BasePathTest(object):
p = self.cls.cwd()
self._test_cwd(p)
+ def _test_home(self, p):
+ q = self.cls(os.path.expanduser('~'))
+ self.assertEqual(p, q)
+ self.assertEqual(str(p), str(q))
+ self.assertIs(type(p), type(q))
+ self.assertTrue(p.is_absolute())
+
+ def test_home(self):
+ p = self.cls.home()
+ self._test_home(p)
+
+ def test_samefile(self):
+ fileA_path = os.path.join(BASE, 'fileA')
+ fileB_path = os.path.join(BASE, 'dirB', 'fileB')
+ p = self.cls(fileA_path)
+ pp = self.cls(fileA_path)
+ q = self.cls(fileB_path)
+ self.assertTrue(p.samefile(fileA_path))
+ self.assertTrue(p.samefile(pp))
+ self.assertFalse(p.samefile(fileB_path))
+ self.assertFalse(p.samefile(q))
+ # Test the non-existent file case
+ non_existent = os.path.join(BASE, 'foo')
+ r = self.cls(non_existent)
+ self.assertRaises(FileNotFoundError, p.samefile, r)
+ self.assertRaises(FileNotFoundError, p.samefile, non_existent)
+ self.assertRaises(FileNotFoundError, r.samefile, p)
+ self.assertRaises(FileNotFoundError, r.samefile, non_existent)
+ self.assertRaises(FileNotFoundError, r.samefile, r)
+ self.assertRaises(FileNotFoundError, r.samefile, non_existent)
+
def test_empty_path(self):
# The empty path points to '.'
p = self.cls('')
self.assertEqual(p.stat(), os.stat('.'))
+ def test_expanduser_common(self):
+ P = self.cls
+ p = P('~')
+ self.assertEqual(p.expanduser(), P(os.path.expanduser('~')))
+ p = P('foo')
+ self.assertEqual(p.expanduser(), p)
+ p = P('/~')
+ self.assertEqual(p.expanduser(), p)
+ p = P('../~')
+ self.assertEqual(p.expanduser(), p)
+ p = P(P('').absolute().anchor) / '~'
+ self.assertEqual(p.expanduser(), p)
+
def test_exists(self):
P = self.cls
p = P(BASE)
@@ -1301,6 +1341,23 @@ class _BasePathTest(object):
self.assertIsInstance(f, io.RawIOBase)
self.assertEqual(f.read().strip(), b"this is file A")
+ def test_read_write_bytes(self):
+ p = self.cls(BASE)
+ (p / 'fileA').write_bytes(b'abcdefg')
+ self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg')
+ # check that trying to write str does not truncate the file
+ self.assertRaises(TypeError, (p / 'fileA').write_bytes, 'somestr')
+ self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg')
+
+ def test_read_write_text(self):
+ p = self.cls(BASE)
+ (p / 'fileA').write_text('äbcdefg', encoding='latin-1')
+ self.assertEqual((p / 'fileA').read_text(
+ encoding='utf-8', errors='ignore'), 'bcdefg')
+ # check that trying to write bytes does not truncate the file
+ self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes')
+ self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg')
+
def test_iterdir(self):
P = self.cls
p = P(BASE)
@@ -1604,6 +1661,59 @@ class _BasePathTest(object):
# the parent's permissions follow the default process settings
self.assertEqual(stat.S_IMODE(p.parent.stat().st_mode), mode)
+ def test_mkdir_exist_ok(self):
+ p = self.cls(BASE, 'dirB')
+ st_ctime_first = p.stat().st_ctime
+ self.assertTrue(p.exists())
+ self.assertTrue(p.is_dir())
+ with self.assertRaises(FileExistsError) as cm:
+ p.mkdir()
+ self.assertEqual(cm.exception.errno, errno.EEXIST)
+ p.mkdir(exist_ok=True)
+ self.assertTrue(p.exists())
+ self.assertEqual(p.stat().st_ctime, st_ctime_first)
+
+ def test_mkdir_exist_ok_with_parent(self):
+ p = self.cls(BASE, 'dirC')
+ self.assertTrue(p.exists())
+ with self.assertRaises(FileExistsError) as cm:
+ p.mkdir()
+ self.assertEqual(cm.exception.errno, errno.EEXIST)
+ p = p / 'newdirC'
+ p.mkdir(parents=True)
+ st_ctime_first = p.stat().st_ctime
+ self.assertTrue(p.exists())
+ with self.assertRaises(FileExistsError) as cm:
+ p.mkdir(parents=True)
+ self.assertEqual(cm.exception.errno, errno.EEXIST)
+ p.mkdir(parents=True, exist_ok=True)
+ self.assertTrue(p.exists())
+ self.assertEqual(p.stat().st_ctime, st_ctime_first)
+
+ def test_mkdir_with_child_file(self):
+ p = self.cls(BASE, 'dirB', 'fileB')
+ self.assertTrue(p.exists())
+ # An exception is raised when the last path component is an existing
+ # regular file, regardless of whether exist_ok is true or not.
+ with self.assertRaises(FileExistsError) as cm:
+ p.mkdir(parents=True)
+ self.assertEqual(cm.exception.errno, errno.EEXIST)
+ with self.assertRaises(FileExistsError) as cm:
+ p.mkdir(parents=True, exist_ok=True)
+ self.assertEqual(cm.exception.errno, errno.EEXIST)
+
+ def test_mkdir_no_parents_file(self):
+ p = self.cls(BASE, 'fileA')
+ self.assertTrue(p.exists())
+ # An exception is raised when the last path component is an existing
+ # regular file, regardless of whether exist_ok is true or not.
+ with self.assertRaises(FileExistsError) as cm:
+ p.mkdir()
+ self.assertEqual(cm.exception.errno, errno.EEXIST)
+ with self.assertRaises(FileExistsError) as cm:
+ p.mkdir(exist_ok=True)
+ self.assertEqual(cm.exception.errno, errno.EEXIST)
+
@with_symlinks
def test_symlink_to(self):
P = self.cls(BASE)
@@ -1846,7 +1956,6 @@ class PosixPathTest(_BasePathTest, unittest.TestCase):
@with_symlinks
def test_resolve_loop(self):
# Loop detection for broken symlinks under POSIX
- P = self.cls
# Loops with relative symlinks
os.symlink('linkX/inside', join('linkX'))
self._check_symlink_loop(BASE, 'linkX')
@@ -1878,6 +1987,48 @@ class PosixPathTest(_BasePathTest, unittest.TestCase):
self.assertEqual(given, expect)
self.assertEqual(set(p.rglob("FILEd*")), set())
+ def test_expanduser(self):
+ P = self.cls
+ support.import_module('pwd')
+ import pwd
+ pwdent = pwd.getpwuid(os.getuid())
+ username = pwdent.pw_name
+ userhome = pwdent.pw_dir.rstrip('/')
+ # find arbitrary different user (if exists)
+ for pwdent in pwd.getpwall():
+ othername = pwdent.pw_name
+ otherhome = pwdent.pw_dir.rstrip('/')
+ if othername != username and otherhome:
+ break
+
+ p1 = P('~/Documents')
+ p2 = P('~' + username + '/Documents')
+ p3 = P('~' + othername + '/Documents')
+ p4 = P('../~' + username + '/Documents')
+ p5 = P('/~' + username + '/Documents')
+ p6 = P('')
+ p7 = P('~fakeuser/Documents')
+
+ with support.EnvironmentVarGuard() as env:
+ env.pop('HOME', None)
+
+ self.assertEqual(p1.expanduser(), P(userhome) / 'Documents')
+ self.assertEqual(p2.expanduser(), P(userhome) / 'Documents')
+ self.assertEqual(p3.expanduser(), P(otherhome) / 'Documents')
+ self.assertEqual(p4.expanduser(), p4)
+ self.assertEqual(p5.expanduser(), p5)
+ self.assertEqual(p6.expanduser(), p6)
+ self.assertRaises(RuntimeError, p7.expanduser)
+
+ env['HOME'] = '/tmp'
+ self.assertEqual(p1.expanduser(), P('/tmp/Documents'))
+ self.assertEqual(p2.expanduser(), P(userhome) / 'Documents')
+ self.assertEqual(p3.expanduser(), P(otherhome) / 'Documents')
+ self.assertEqual(p4.expanduser(), p4)
+ self.assertEqual(p5.expanduser(), p5)
+ self.assertEqual(p6.expanduser(), p6)
+ self.assertRaises(RuntimeError, p7.expanduser)
+
@only_nt
class WindowsPathTest(_BasePathTest, unittest.TestCase):
@@ -1893,6 +2044,61 @@ class WindowsPathTest(_BasePathTest, unittest.TestCase):
p = P(BASE, "dirC")
self.assertEqual(set(p.rglob("FILEd")), { P(BASE, "dirC/dirD/fileD") })
+ def test_expanduser(self):
+ P = self.cls
+ with support.EnvironmentVarGuard() as env:
+ env.pop('HOME', None)
+ env.pop('USERPROFILE', None)
+ env.pop('HOMEPATH', None)
+ env.pop('HOMEDRIVE', None)
+ env['USERNAME'] = 'alice'
+
+ # test that the path returns unchanged
+ p1 = P('~/My Documents')
+ p2 = P('~alice/My Documents')
+ p3 = P('~bob/My Documents')
+ p4 = P('/~/My Documents')
+ p5 = P('d:~/My Documents')
+ p6 = P('')
+ self.assertRaises(RuntimeError, p1.expanduser)
+ self.assertRaises(RuntimeError, p2.expanduser)
+ self.assertRaises(RuntimeError, p3.expanduser)
+ self.assertEqual(p4.expanduser(), p4)
+ self.assertEqual(p5.expanduser(), p5)
+ self.assertEqual(p6.expanduser(), p6)
+
+ def check():
+ env.pop('USERNAME', None)
+ self.assertEqual(p1.expanduser(),
+ P('C:/Users/alice/My Documents'))
+ self.assertRaises(KeyError, p2.expanduser)
+ env['USERNAME'] = 'alice'
+ self.assertEqual(p2.expanduser(),
+ P('C:/Users/alice/My Documents'))
+ self.assertEqual(p3.expanduser(),
+ P('C:/Users/bob/My Documents'))
+ self.assertEqual(p4.expanduser(), p4)
+ self.assertEqual(p5.expanduser(), p5)
+ self.assertEqual(p6.expanduser(), p6)
+
+ # test the first lookup key in the env vars
+ env['HOME'] = 'C:\\Users\\alice'
+ check()
+
+ # test that HOMEPATH is available instead
+ env.pop('HOME', None)
+ env['HOMEPATH'] = 'C:\\Users\\alice'
+ check()
+
+ env['HOMEDRIVE'] = 'C:\\'
+ env['HOMEPATH'] = 'Users\\alice'
+ check()
+
+ env.pop('HOMEDRIVE', None)
+ env.pop('HOMEPATH', None)
+ env['USERPROFILE'] = 'C:\\Users\\alice'
+ check()
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py
index e0c8635de..57ebf1f 100644
--- a/Lib/test/test_pkgutil.py
+++ b/Lib/test/test_pkgutil.py
@@ -104,6 +104,9 @@ class PkgutilTests(unittest.TestCase):
class PkgutilPEP302Tests(unittest.TestCase):
class MyTestLoader(object):
+ def create_module(self, spec):
+ return None
+
def exec_module(self, mod):
# Count how many times the module is reloaded
mod.__dict__['loads'] = mod.__dict__.get('loads', 0) + 1
diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py
index b3de43b..ededbdb 100644
--- a/Lib/test/test_platform.py
+++ b/Lib/test/test_platform.py
@@ -307,7 +307,7 @@ class PlatformTest(unittest.TestCase):
with mock.patch('platform._UNIXCONFDIR', tempdir):
distname, version, distid = platform.linux_distribution()
- self.assertEqual(distname, 'Fedora')
+ self.assertEqual(distname, 'Fedora')
self.assertEqual(version, '19')
self.assertEqual(distid, 'Schr\xf6dinger\u2019s Cat')
diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py
index 8a3c9f4..14a519d 100644
--- a/Lib/test/test_poplib.py
+++ b/Lib/test/test_poplib.py
@@ -345,23 +345,18 @@ class TestPOP3Class(TestCase):
if SUPPORTS_SSL:
+ from test.test_ftplib import SSLConnection
- class DummyPOP3_SSLHandler(DummyPOP3Handler):
+ class DummyPOP3_SSLHandler(SSLConnection, DummyPOP3Handler):
def __init__(self, conn):
asynchat.async_chat.__init__(self, conn)
- ssl_socket = ssl.wrap_socket(self.socket, certfile=CERTFILE,
- server_side=True,
- do_handshake_on_connect=False)
- self.del_channel()
- self.set_socket(ssl_socket)
- # Must try handshake before calling push()
- self.tls_active = True
- self.tls_starting = True
- self._do_tls_handshake()
+ self.secure_connection()
self.set_terminator(b"\r\n")
self.in_buffer = []
self.push('+OK dummy pop3 server ready. <timestamp>')
+ self.tls_active = True
+ self.tls_starting = False
@requires_ssl
@@ -452,7 +447,7 @@ class TestTimeouts(TestCase):
del self.thread # Clear out any dangling Thread objects.
def server(self, evt, serv):
- serv.listen(5)
+ serv.listen()
evt.set()
try:
conn, addr = serv.accept()
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 72fdd16..f37f2de 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -9,7 +9,6 @@ import errno
import sys
import time
import os
-import fcntl
import platform
import pwd
import shutil
@@ -355,7 +354,7 @@ class PosixTester(unittest.TestCase):
def test_oscloexec(self):
fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
self.addCleanup(os.close, fd)
- self.assertTrue(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC)
+ self.assertFalse(os.get_inheritable(fd))
@unittest.skipUnless(hasattr(posix, 'O_EXLOCK'),
'test needs posix.O_EXLOCK')
@@ -605,8 +604,8 @@ class PosixTester(unittest.TestCase):
self.addCleanup(os.close, w)
self.assertFalse(os.get_inheritable(r))
self.assertFalse(os.get_inheritable(w))
- self.assertTrue(fcntl.fcntl(r, fcntl.F_GETFL) & os.O_NONBLOCK)
- self.assertTrue(fcntl.fcntl(w, fcntl.F_GETFL) & os.O_NONBLOCK)
+ self.assertFalse(os.get_blocking(r))
+ self.assertFalse(os.get_blocking(w))
# try reading from an empty pipe: this should fail, not block
self.assertRaises(OSError, os.read, r, 1)
# try a write big enough to fill-up the pipe: this should either
diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py
index ec2fbae..b2454794 100644
--- a/Lib/test/test_posixpath.py
+++ b/Lib/test/test_posixpath.py
@@ -57,18 +57,6 @@ class PosixPathTest(unittest.TestCase):
self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"),
b"/foo/bar/baz/")
- def test_join_errors(self):
- # Check posixpath.join raises friendly TypeErrors.
- errmsg = "Can't mix strings and bytes in path components"
- with self.assertRaisesRegex(TypeError, errmsg):
- posixpath.join(b'bytes', 'str')
- with self.assertRaisesRegex(TypeError, errmsg):
- posixpath.join('str', b'bytes')
- # regression, see #15377
- with self.assertRaises(TypeError) as cm:
- posixpath.join(None, 'str')
- self.assertNotEqual(cm.exception.args[0], errmsg)
-
def test_split(self):
self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
self.assertEqual(posixpath.split("/"), ("/", ""))
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py
index ad6a7a1..c056880 100644
--- a/Lib/test/test_pprint.py
+++ b/Lib/test/test_pprint.py
@@ -192,10 +192,52 @@ class QueryTestCase(unittest.TestCase):
o = [o1, o2]
expected = """\
[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
+ {'first': 1, 'second': 2, 'third': 3}]"""
+ self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
+ expected = """\
+[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
{ 'first': 1,
'second': 2,
'third': 3}]"""
- self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
+ self.assertEqual(pprint.pformat(o, indent=4, width=41), expected)
+
+ def test_width(self):
+ expected = """\
+[[[[[[1, 2, 3],
+ '1 2']]]],
+ {1: [1, 2, 3],
+ 2: [12, 34]},
+ 'abc def ghi',
+ ('ab cd ef',),
+ set2({1, 23}),
+ [[[[[1, 2, 3],
+ '1 2']]]]]"""
+ o = eval(expected)
+ self.assertEqual(pprint.pformat(o, width=15), expected)
+ self.assertEqual(pprint.pformat(o, width=16), expected)
+ self.assertEqual(pprint.pformat(o, width=25), expected)
+ self.assertEqual(pprint.pformat(o, width=14), """\
+[[[[[[1,
+ 2,
+ 3],
+ '1 '
+ '2']]]],
+ {1: [1,
+ 2,
+ 3],
+ 2: [12,
+ 34]},
+ 'abc def '
+ 'ghi',
+ ('ab cd '
+ 'ef',),
+ set2({1,
+ 23}),
+ [[[[[1,
+ 2,
+ 3],
+ '1 '
+ '2']]]]]""")
def test_sorted_dict(self):
# Starting in Python 2.5, pprint sorts dict displays by key regardless
@@ -535,13 +577,12 @@ frozenset2({0,
def test_str_wrap(self):
# pprint tries to wrap strings intelligently
fox = 'the quick brown fox jumped over a lazy dog'
- self.assertEqual(pprint.pformat(fox, width=20), """\
-('the quick '
- 'brown fox '
- 'jumped over a '
- 'lazy dog')""")
+ self.assertEqual(pprint.pformat(fox, width=19), """\
+('the quick brown '
+ 'fox jumped over '
+ 'a lazy dog')""")
self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
- width=26), """\
+ width=25), """\
{'a': 1,
'b': 'the quick brown '
'fox jumped over '
@@ -553,12 +594,34 @@ frozenset2({0,
# - non-ASCII is allowed
# - an apostrophe doesn't disrupt the pprint
special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo"
- self.assertEqual(pprint.pformat(special, width=21), """\
-('Portons dix '
- 'bons "whiskys"\\n'
+ self.assertEqual(pprint.pformat(special, width=68), repr(special))
+ self.assertEqual(pprint.pformat(special, width=31), """\
+('Portons dix bons "whiskys"\\n'
+ "à l'avocat goujat\\t qui "
+ 'fumait au zoo')""")
+ self.assertEqual(pprint.pformat(special, width=20), """\
+('Portons dix bons '
+ '"whiskys"\\n'
"à l'avocat "
'goujat\\t qui '
'fumait au zoo')""")
+ self.assertEqual(pprint.pformat([[[[[special]]]]], width=35), """\
+[[[[['Portons dix bons "whiskys"\\n'
+ "à l'avocat goujat\\t qui "
+ 'fumait au zoo']]]]]""")
+ self.assertEqual(pprint.pformat([[[[[special]]]]], width=25), """\
+[[[[['Portons dix bons '
+ '"whiskys"\\n'
+ "à l'avocat "
+ 'goujat\\t qui '
+ 'fumait au zoo']]]]]""")
+ self.assertEqual(pprint.pformat([[[[[special]]]]], width=23), """\
+[[[[['Portons dix '
+ 'bons "whiskys"\\n'
+ "à l'avocat "
+ 'goujat\\t qui '
+ 'fumait au '
+ 'zoo']]]]]""")
# An unwrappable string is formatted as its repr
unwrappable = "x" * 100
self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable))
@@ -581,7 +644,19 @@ frozenset2({0,
14, 15],
[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3],
[0, 1, 2, 3, 4]]"""
- self.assertEqual(pprint.pformat(o, width=48, compact=True), expected)
+ self.assertEqual(pprint.pformat(o, width=47, compact=True), expected)
+
+ def test_compact_width(self):
+ levels = 20
+ number = 10
+ o = [0] * number
+ for i in range(levels - 1):
+ o = [o]
+ for w in range(levels * 2 + 1, levels + 3 * number - 1):
+ lines = pprint.pformat(o, width=w, compact=True).splitlines()
+ maxwidth = max(map(len, lines))
+ self.assertLessEqual(maxwidth, w)
+ self.assertGreater(maxwidth, w - 3)
class DottedPrettyPrinter(pprint.PrettyPrinter):
diff --git a/Lib/test/test_pty.py b/Lib/test/test_pty.py
index 8916861..9b783c3 100644
--- a/Lib/test/test_pty.py
+++ b/Lib/test/test_pty.py
@@ -1,7 +1,6 @@
from test.support import verbose, run_unittest, import_module, reap_children
-#Skip these tests if either fcntl or termios is not available
-fcntl = import_module('fcntl')
+# Skip these tests if termios is not available
import_module('termios')
import errno
@@ -84,16 +83,18 @@ class PtyTest(unittest.TestCase):
# in master_open(), we need to read the EOF.
# Ensure the fd is non-blocking in case there's nothing to read.
- orig_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL)
- fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags | os.O_NONBLOCK)
+ blocking = os.get_blocking(master_fd)
try:
- s1 = os.read(master_fd, 1024)
- self.assertEqual(b'', s1)
- except OSError as e:
- if e.errno != errno.EAGAIN:
- raise
- # Restore the original flags.
- fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags)
+ os.set_blocking(master_fd, False)
+ try:
+ s1 = os.read(master_fd, 1024)
+ self.assertEqual(b'', s1)
+ except OSError as e:
+ if e.errno != errno.EAGAIN:
+ raise
+ finally:
+ # Restore the original flags.
+ os.set_blocking(master_fd, blocking)
debug("Writing to slave_fd")
os.write(slave_fd, TEST_STRING_1)
diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py
index 8bf9b20..6742444 100644
--- a/Lib/test/test_pydoc.py
+++ b/Lib/test/test_pydoc.py
@@ -2,7 +2,6 @@ import os
import sys
import builtins
import contextlib
-import difflib
import inspect
import pydoc
import keyword
@@ -357,15 +356,6 @@ def get_pydoc_text(module):
output = patt.sub('', output)
return output.strip(), loc
-def print_diffs(text1, text2):
- "Prints unified diffs for two texts"
- # XXX now obsolete, use unittest built-in support
- lines1 = text1.splitlines(keepends=True)
- lines2 = text2.splitlines(keepends=True)
- diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
- tofile='got')
- print('\n' + ''.join(diffs))
-
def get_html_title(text):
# Bit of hack, but good enough for test purposes
header, _, _ = text.partition("</head>")
@@ -411,9 +401,7 @@ class PydocDocTest(unittest.TestCase):
expected_html = expected_html_pattern % (
(mod_url, mod_file, doc_loc) +
expected_html_data_docstrings)
- if result != expected_html:
- print_diffs(expected_html, result)
- self.fail("outputs are not equal, see diff above")
+ self.assertEqual(result, expected_html)
@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
@@ -426,9 +414,7 @@ class PydocDocTest(unittest.TestCase):
(doc_loc,) +
expected_text_data_docstrings +
(inspect.getabsfile(pydoc_mod),))
- if result != expected_text:
- print_diffs(expected_text, result)
- self.fail("outputs are not equal, see diff above")
+ self.assertEqual(expected_text, result)
def test_text_enum_member_with_value_zero(self):
# Test issue #20654 to ensure enum member with value 0 can be
@@ -886,9 +872,7 @@ class PydocWithMetaClasses(unittest.TestCase):
expected_text = expected_dynamicattribute_pattern % (
(__name__,) + expected_text_data_docstrings[:2])
result = output.getvalue().strip()
- if result != expected_text:
- print_diffs(expected_text, result)
- self.fail("outputs are not equal, see diff above")
+ self.assertEqual(expected_text, result)
@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
@@ -909,9 +893,7 @@ class PydocWithMetaClasses(unittest.TestCase):
helper(Class)
expected_text = expected_virtualattribute_pattern1 % __name__
result = output.getvalue().strip()
- if result != expected_text:
- print_diffs(expected_text, result)
- self.fail("outputs are not equal, see diff above")
+ self.assertEqual(expected_text, result)
@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
@@ -951,19 +933,13 @@ class PydocWithMetaClasses(unittest.TestCase):
helper(Class1)
expected_text1 = expected_virtualattribute_pattern2 % __name__
result1 = output.getvalue().strip()
- if result1 != expected_text1:
- print_diffs(expected_text1, result1)
- fail1 = True
+ self.assertEqual(expected_text1, result1)
output = StringIO()
helper = pydoc.Helper(output=output)
helper(Class2)
expected_text2 = expected_virtualattribute_pattern3 % __name__
result2 = output.getvalue().strip()
- if result2 != expected_text2:
- print_diffs(expected_text2, result2)
- fail2 = True
- if fail1 or fail2:
- self.fail("outputs are not equal, see diff above")
+ self.assertEqual(expected_text2, result2)
@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
@@ -980,9 +956,7 @@ class PydocWithMetaClasses(unittest.TestCase):
helper(C)
expected_text = expected_missingattribute_pattern % __name__
result = output.getvalue().strip()
- if result != expected_text:
- print_diffs(expected_text, result)
- self.fail("outputs are not equal, see diff above")
+ self.assertEqual(expected_text, result)
@reap_threads
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index d2547d4..2fb4764 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -84,7 +84,7 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.sub("(?i)b+", "x", "bbbb BBBB"), 'x x')
self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y'),
'9.3 -3 24x100y')
- self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', 3),
+ self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', count=3),
'9.3 -3 23x99y')
self.assertEqual(re.sub('.', lambda m: r"\n", 'x'), '\\n')
@@ -155,8 +155,8 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.sub('x', r'\09', 'x'), '\0' + '9')
self.assertEqual(re.sub('x', r'\0a', 'x'), '\0' + 'a')
- self.assertEqual(re.sub('x', r'\400', 'x'), '\0')
- self.assertEqual(re.sub('x', r'\777', 'x'), '\377')
+ self.assertRaises(re.error, re.sub, 'x', r'\400', 'x')
+ self.assertRaises(re.error, re.sub, 'x', r'\777', 'x')
self.assertRaises(re.error, re.sub, 'x', r'\1', 'x')
self.assertRaises(re.error, re.sub, 'x', r'\8', 'x')
@@ -180,7 +180,7 @@ class ReTests(unittest.TestCase):
def test_qualified_re_sub(self):
self.assertEqual(re.sub('a', 'b', 'aaaaa'), 'bbbbb')
- self.assertEqual(re.sub('a', 'b', 'aaaaa', 1), 'baaaa')
+ self.assertEqual(re.sub('a', 'b', 'aaaaa', count=1), 'baaaa')
def test_bug_114660(self):
self.assertEqual(re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there'),
@@ -194,6 +194,7 @@ class ReTests(unittest.TestCase):
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.assertRaises(re.error, re.compile, '(?P<a>)(?P<a>)')
self.assertRaises(re.error, re.compile, '(?Px)')
self.assertRaises(re.error, re.compile, '(?P=)')
@@ -213,6 +214,10 @@ class ReTests(unittest.TestCase):
re.compile('(?P<µ>x)(?P=µ)(?(µ)y)')
re.compile('(?P<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>x)(?P=𝔘𝔫𝔦𝔠𝔬𝔡𝔢)(?(𝔘𝔫𝔦𝔠𝔬𝔡𝔢)y)')
self.assertRaises(re.error, re.compile, '(?P<©>x)')
+ # Support > 100 groups.
+ pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1))
+ pat = '(?:%s)(?(200)z|t)' % pat
+ self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5))
def test_symbolic_refs(self):
self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx')
@@ -221,48 +226,53 @@ class ReTests(unittest.TestCase):
self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx')
self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<>', 'xx')
self.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx')
+ self.assertRaises(re.error, re.sub, '(?P<a>x)', r'\g<2>', 'xx')
+ self.assertRaises(re.error, re.sub, '(?P<a>x)', r'\2', 'xx')
self.assertRaises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx')
- self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
- self.assertRaises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\\2', '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.assertRaises(re.error, re.sub, '(?P<a>x)', '\g<-1>', 'xx')
# 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.assertRaises(re.error, re.sub, '(?P<a>x)', r'\g<©>', 'xx')
+ # 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')
def test_re_subn(self):
self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2))
self.assertEqual(re.subn("b+", "x", "bbbb BBBB"), ('x BBBB', 1))
self.assertEqual(re.subn("b+", "x", "xyz"), ('xyz', 0))
self.assertEqual(re.subn("b*", "x", "xyz"), ('xxxyxzx', 4))
- self.assertEqual(re.subn("b*", "x", "xyz", 2), ('xxxyz', 2))
+ self.assertEqual(re.subn("b*", "x", "xyz", count=2), ('xxxyz', 2))
def test_re_split(self):
for string in ":a:b::c", S(":a:b::c"):
self.assertTypedEqual(re.split(":", string),
['', 'a', 'b', '', 'c'])
- self.assertTypedEqual(re.split(":*", string),
+ self.assertTypedEqual(re.split(":+", string),
['', 'a', 'b', 'c'])
- self.assertTypedEqual(re.split("(:*)", string),
+ self.assertTypedEqual(re.split("(:+)", string),
['', ':', 'a', ':', 'b', '::', 'c'])
for string in (b":a:b::c", B(b":a:b::c"), bytearray(b":a:b::c"),
memoryview(b":a:b::c")):
self.assertTypedEqual(re.split(b":", string),
[b'', b'a', b'b', b'', b'c'])
- self.assertTypedEqual(re.split(b":*", string),
+ self.assertTypedEqual(re.split(b":+", string),
[b'', b'a', b'b', b'c'])
- self.assertTypedEqual(re.split(b"(:*)", string),
+ self.assertTypedEqual(re.split(b"(:+)", string),
[b'', b':', b'a', b':', b'b', b'::', b'c'])
for a, b, c in ("\xe0\xdf\xe7", "\u0430\u0431\u0432",
"\U0001d49c\U0001d49e\U0001d4b5"):
string = ":%s:%s::%s" % (a, b, c)
self.assertEqual(re.split(":", string), ['', a, b, '', c])
- self.assertEqual(re.split(":*", string), ['', a, b, c])
- self.assertEqual(re.split("(:*)", string),
+ self.assertEqual(re.split(":+", string), ['', a, b, c])
+ self.assertEqual(re.split("(:+)", string),
['', ':', a, ':', b, '::', c])
- self.assertEqual(re.split("(?::*)", ":a:b::c"), ['', 'a', 'b', 'c'])
- self.assertEqual(re.split("(:)*", ":a:b::c"),
+ self.assertEqual(re.split("(?::+)", ":a:b::c"), ['', 'a', 'b', 'c'])
+ self.assertEqual(re.split("(:)+", ":a:b::c"),
['', ':', 'a', ':', 'b', ':', 'c'])
self.assertEqual(re.split("([b:]+)", ":a:b::c"),
['', ':', 'a', ':b::', 'c'])
@@ -272,13 +282,34 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.split("(?:b)|(?::+)", ":a:b::c"),
['', 'a', '', '', 'c'])
+ for sep, expected in [
+ (':*', ['', 'a', 'b', 'c']),
+ ('(?::*)', ['', 'a', 'b', 'c']),
+ ('(:*)', ['', ':', 'a', ':', 'b', '::', 'c']),
+ ('(:)*', ['', ':', 'a', ':', 'b', ':', 'c']),
+ ]:
+ with self.subTest(sep=sep), self.assertWarns(FutureWarning):
+ self.assertTypedEqual(re.split(sep, ':a:b::c'), expected)
+
+ for sep, expected in [
+ ('', [':a:b::c']),
+ (r'\b', [':a:b::c']),
+ (r'(?=:)', [':a:b::c']),
+ (r'(?<=:)', [':a:b::c']),
+ ]:
+ with self.subTest(sep=sep), self.assertRaises(ValueError):
+ self.assertTypedEqual(re.split(sep, ':a:b::c'), expected)
+
def test_qualified_re_split(self):
- self.assertEqual(re.split(":", ":a:b::c", 2), ['', 'a', 'b::c'])
- self.assertEqual(re.split(':', 'a:b:c:d', 2), ['a', 'b', 'c:d'])
- self.assertEqual(re.split("(:)", ":a:b::c", 2),
+ self.assertEqual(re.split(":", ":a:b::c", maxsplit=2), ['', 'a', 'b::c'])
+ self.assertEqual(re.split(':', 'a:b:c:d', maxsplit=2), ['a', 'b', 'c:d'])
+ self.assertEqual(re.split("(:)", ":a:b::c", maxsplit=2),
['', ':', 'a', ':', 'b::c'])
- self.assertEqual(re.split("(:*)", ":a:b::c", 2),
+ self.assertEqual(re.split("(:+)", ":a:b::c", maxsplit=2),
['', ':', 'a', ':', 'b::c'])
+ with self.assertWarns(FutureWarning):
+ self.assertEqual(re.split("(:*)", ":a:b::c", maxsplit=2),
+ ['', ':', 'a', ':', 'b::c'])
def test_re_findall(self):
self.assertEqual(re.findall(":+", "abc"), [])
@@ -405,6 +436,10 @@ class ReTests(unittest.TestCase):
self.assertIsNone(p.match('abd'))
self.assertIsNone(p.match('ac'))
+ # Support > 100 groups.
+ pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1))
+ pat = '(?:%s)(?(200)z)' % pat
+ self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5))
def test_re_groupref(self):
self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a|').groups(),
@@ -428,6 +463,10 @@ class ReTests(unittest.TestCase):
"first second")
.expand(r"\2 \1 \g<second> \g<first>"),
"second first second first")
+ self.assertEqual(re.match("(?P<first>first)|(?P<second>second)",
+ "first")
+ .expand(r"\2 \g<second>"),
+ " ")
def test_repeat_minmax(self):
self.assertIsNone(re.match("^(\w){1}$", "abc"))
@@ -484,10 +523,6 @@ class ReTests(unittest.TestCase):
"abcd abc bcd bx", re.ASCII).group(1), "bx")
self.assertEqual(re.search(r"\B(b.)\B",
"abc bcd bc abxd", re.ASCII).group(1), "bx")
- self.assertEqual(re.search(r"\b(b.)\b",
- "abcd abc bcd bx", re.LOCALE).group(1), "bx")
- self.assertEqual(re.search(r"\B(b.)\B",
- "abc bcd bc abxd", re.LOCALE).group(1), "bx")
self.assertEqual(re.search(r"^abc$", "\nabc\n", re.M).group(0), "abc")
self.assertEqual(re.search(r"^\Aabc\Z$", "abc", re.M).group(0), "abc")
self.assertIsNone(re.search(r"^\Aabc\Z$", "\nabc\n", re.M))
@@ -508,11 +543,23 @@ class ReTests(unittest.TestCase):
b"1aa! a").group(0), b"1aa! a")
self.assertEqual(re.search(r"\d\D\w\W\s\S",
"1aa! a", re.ASCII).group(0), "1aa! a")
- self.assertEqual(re.search(r"\d\D\w\W\s\S",
- "1aa! a", re.LOCALE).group(0), "1aa! a")
self.assertEqual(re.search(br"\d\D\w\W\s\S",
b"1aa! a", re.LOCALE).group(0), b"1aa! a")
+ def test_other_escapes(self):
+ self.assertRaises(re.error, re.compile, "\\")
+ self.assertEqual(re.match(r"\(", '(').group(), '(')
+ self.assertIsNone(re.match(r"\(", ')'))
+ self.assertEqual(re.match(r"\\", '\\').group(), '\\')
+ self.assertEqual(re.match(r"\y", 'y').group(), 'y')
+ self.assertIsNone(re.match(r"\y", 'z'))
+ self.assertEqual(re.match(r"[\]]", ']').group(), ']')
+ self.assertIsNone(re.match(r"[\]]", '['))
+ self.assertEqual(re.match(r"[a\-c]", '-').group(), '-')
+ self.assertIsNone(re.match(r"[a\-c]", 'b'))
+ self.assertEqual(re.match(r"[\^a]+", 'a^').group(), 'a^')
+ self.assertIsNone(re.match(r"[\^a]+", 'b'))
+
def test_string_boundaries(self):
# See http://bugs.python.org/issue10713
self.assertEqual(re.search(r"\b(abc)\b", "abc").group(1),
@@ -661,9 +708,12 @@ class ReTests(unittest.TestCase):
self.assertEqual(_sre.getlower(ord('A'), 0), ord('a'))
self.assertEqual(_sre.getlower(ord('A'), re.LOCALE), ord('a'))
self.assertEqual(_sre.getlower(ord('A'), re.UNICODE), ord('a'))
+ self.assertEqual(_sre.getlower(ord('A'), re.ASCII), ord('a'))
self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
self.assertEqual(re.match(b"abc", b"ABC", re.I).group(0), b"ABC")
+ self.assertEqual(re.match("abc", "ABC", re.I|re.A).group(0), "ABC")
+ 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")
@@ -748,8 +798,10 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.X, re.VERBOSE)
def test_flags(self):
- for flag in [re.I, re.M, re.X, re.S, re.L]:
+ for flag in [re.I, re.M, re.X, re.S, re.A, re.U]:
self.assertTrue(re.compile('^pattern$', flag))
+ for flag in [re.I, re.M, re.X, re.S, re.A, re.L]:
+ self.assertTrue(re.compile(b'^pattern$', flag))
def test_sre_character_literals(self):
for i in [0, 8, 16, 32, 64, 127, 128, 255, 256, 0xFFFF, 0x10000, 0x10FFFF]:
@@ -771,7 +823,7 @@ class ReTests(unittest.TestCase):
self.assertTrue(re.match(r"\08", "\0008"))
self.assertTrue(re.match(r"\01", "\001"))
self.assertTrue(re.match(r"\018", "\0018"))
- self.assertTrue(re.match(r"\567", chr(0o167)))
+ self.assertRaises(re.error, re.match, r"\567", "")
self.assertRaises(re.error, re.match, r"\911", "")
self.assertRaises(re.error, re.match, r"\x1", "")
self.assertRaises(re.error, re.match, r"\x1z", "")
@@ -799,12 +851,13 @@ class ReTests(unittest.TestCase):
self.assertTrue(re.match(r"[\U%08x]" % i, chr(i)))
self.assertTrue(re.match(r"[\U%08x0]" % i, chr(i)+"0"))
self.assertTrue(re.match(r"[\U%08xz]" % i, chr(i)+"z"))
- self.assertTrue(re.match(r"[\U0001d49c-\U0001d4b5]", "\U0001d49e"))
+ self.assertRaises(re.error, re.match, r"[\567]", "")
self.assertRaises(re.error, re.match, r"[\911]", "")
self.assertRaises(re.error, re.match, r"[\x1z]", "")
self.assertRaises(re.error, re.match, r"[\u123z]", "")
self.assertRaises(re.error, re.match, r"[\U0001234z]", "")
self.assertRaises(re.error, re.match, r"[\U00110000]", "")
+ self.assertTrue(re.match(r"[\U0001d49c-\U0001d4b5]", "\U0001d49e"))
def test_sre_byte_literals(self):
for i in [0, 8, 16, 32, 64, 127, 128, 255]:
@@ -820,7 +873,7 @@ class ReTests(unittest.TestCase):
self.assertTrue(re.match(br"\08", b"\0008"))
self.assertTrue(re.match(br"\01", b"\001"))
self.assertTrue(re.match(br"\018", b"\0018"))
- self.assertTrue(re.match(br"\567", bytes([0o167])))
+ self.assertRaises(re.error, re.match, br"\567", b"")
self.assertRaises(re.error, re.match, br"\911", b"")
self.assertRaises(re.error, re.match, br"\x1", b"")
self.assertRaises(re.error, re.match, br"\x1z", b"")
@@ -837,6 +890,7 @@ class ReTests(unittest.TestCase):
self.assertTrue(re.match((r"[\x%02xz]" % i).encode(), bytes([i])))
self.assertTrue(re.match(br"[\u]", b'u'))
self.assertTrue(re.match(br"[\U]", b'U'))
+ self.assertRaises(re.error, re.match, br"[\567]", b"")
self.assertRaises(re.error, re.match, br"[\911]", b"")
self.assertRaises(re.error, re.match, br"[\x1z]", b"")
@@ -1031,8 +1085,8 @@ class ReTests(unittest.TestCase):
def test_inline_flags(self):
# Bug #1700
- upper_char = chr(0x1ea0) # Latin Capital Letter A with Dot Bellow
- lower_char = chr(0x1ea1) # Latin Small Letter A with Dot Bellow
+ upper_char = '\u1ea0' # Latin Capital Letter A with Dot Below
+ lower_char = '\u1ea1' # Latin Small Letter A with Dot Below
p = re.compile(upper_char, re.I | re.U)
q = p.match(lower_char)
@@ -1112,6 +1166,52 @@ class ReTests(unittest.TestCase):
self.assertRaises(ValueError, re.compile, '(?a)\w', re.UNICODE)
self.assertRaises(ValueError, re.compile, '(?au)\w')
+ def test_locale_flag(self):
+ import locale
+ _, enc = locale.getlocale(locale.LC_CTYPE)
+ # Search non-ASCII letter
+ for i in range(128, 256):
+ try:
+ c = bytes([i]).decode(enc)
+ sletter = c.lower()
+ if sletter == c: continue
+ bletter = sletter.encode(enc)
+ if len(bletter) != 1: continue
+ if bletter.decode(enc) != sletter: continue
+ bpat = re.escape(bytes([i]))
+ break
+ except (UnicodeError, TypeError):
+ pass
+ else:
+ bletter = None
+ bpat = b'A'
+ # Bytes patterns
+ pat = re.compile(bpat, re.LOCALE | re.IGNORECASE)
+ if bletter:
+ self.assertTrue(pat.match(bletter))
+ pat = re.compile(b'(?L)' + bpat, re.IGNORECASE)
+ if bletter:
+ self.assertTrue(pat.match(bletter))
+ pat = re.compile(bpat, re.IGNORECASE)
+ if bletter:
+ self.assertIsNone(pat.match(bletter))
+ pat = re.compile(b'\w', re.LOCALE)
+ if bletter:
+ self.assertTrue(pat.match(bletter))
+ pat = re.compile(b'(?L)\w')
+ if bletter:
+ self.assertTrue(pat.match(bletter))
+ pat = re.compile(b'\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)')
+
def test_bug_6509(self):
# Replacement strings of both types must parse properly.
# all strings
@@ -1139,8 +1239,10 @@ class ReTests(unittest.TestCase):
# a RuntimeError is raised instead of OverflowError.
long_overflow = 2**128
self.assertRaises(TypeError, re.finditer, "a", {})
- self.assertRaises(OverflowError, _sre.compile, "abc", 0, [long_overflow])
- self.assertRaises(TypeError, _sre.compile, {}, 0, [])
+ with self.assertRaises(OverflowError):
+ _sre.compile("abc", 0, [long_overflow], 0, [], [])
+ with self.assertRaises(TypeError):
+ _sre.compile({}, 0, [], 0, [], [])
def test_search_dot_unicode(self):
self.assertTrue(re.search("123.*-", '123abc-'))
@@ -1278,22 +1380,22 @@ class ReTests(unittest.TestCase):
with captured_stdout() as out:
re.compile(pat, re.DEBUG)
dump = '''\
-subpattern 1
- literal 46
-subpattern None
- branch
- in
- literal 99
- literal 104
- or
- literal 112
- literal 121
-subpattern None
- groupref_exists 1
- at at_end
- else
- literal 58
- literal 32
+SUBPATTERN 1
+ LITERAL 46
+SUBPATTERN None
+ BRANCH
+ IN
+ LITERAL 99
+ LITERAL 104
+ OR
+ LITERAL 112
+ LITERAL 121
+SUBPATTERN None
+ GROUPREF_EXISTS 1
+ AT AT_END
+ ELSE
+ LITERAL 58
+ LITERAL 32
'''
self.assertEqual(out.getvalue(), dump)
# Debug output is output again even a second time (bypassing
@@ -1361,6 +1463,42 @@ subpattern None
self.assertIsNone(re.match(b'(?Li)\xc5', b'\xe5'))
self.assertIsNone(re.match(b'(?Li)\xe5', b'\xc5'))
+ def test_error(self):
+ with self.assertRaises(re.error) as cm:
+ re.compile('(\u20ac))')
+ err = cm.exception
+ self.assertIsInstance(err.pattern, str)
+ self.assertEqual(err.pattern, '(\u20ac))')
+ self.assertEqual(err.pos, 3)
+ self.assertEqual(err.lineno, 1)
+ self.assertEqual(err.colno, 4)
+ self.assertIn(err.msg, str(err))
+ self.assertIn(' at position 3', str(err))
+ self.assertNotIn(' at position 3', err.msg)
+ # Bytes pattern
+ with self.assertRaises(re.error) as cm:
+ re.compile(b'(\xa4))')
+ err = cm.exception
+ self.assertIsInstance(err.pattern, bytes)
+ self.assertEqual(err.pattern, b'(\xa4))')
+ self.assertEqual(err.pos, 3)
+ # Multiline pattern
+ with self.assertRaises(re.error) as cm:
+ re.compile("""
+ (
+ abc
+ )
+ )
+ (
+ """, re.VERBOSE)
+ err = cm.exception
+ self.assertEqual(err.pos, 77)
+ self.assertEqual(err.lineno, 5)
+ self.assertEqual(err.colno, 17)
+ self.assertIn(err.msg, str(err))
+ self.assertIn(' at position 77', str(err))
+ self.assertIn('(line 5, column 17)', str(err))
+
class PatternReprTests(unittest.TestCase):
def check(self, pattern, expected):
@@ -1405,6 +1543,10 @@ class PatternReprTests(unittest.TestCase):
self.check_flags(b'bytes pattern', re.A,
"re.compile(b'bytes pattern', re.ASCII)")
+ def test_locale(self):
+ self.check_flags(b'bytes pattern', re.L,
+ "re.compile(b'bytes pattern', re.LOCALE)")
+
def test_quotes(self):
self.check('random "double quoted" pattern',
'''re.compile('random "double quoted" pattern')''')
@@ -1518,8 +1660,16 @@ class ExternalTests(unittest.TestCase):
pass
else:
with self.subTest('bytes pattern match'):
- bpat = re.compile(bpat)
- self.assertTrue(bpat.search(bs))
+ obj = re.compile(bpat)
+ self.assertTrue(obj.search(bs))
+
+ # Try the match with LOCALE enabled, and check that it
+ # still succeeds.
+ with self.subTest('locale-sensitive match'):
+ obj = re.compile(bpat, re.LOCALE)
+ result = obj.search(bs)
+ if result is None:
+ print('=== Fails on locale-sensitive match', t)
# Try the match with the search area limited to the extent
# of the match and see if it still succeeds. \B will
@@ -1537,13 +1687,6 @@ class ExternalTests(unittest.TestCase):
obj = re.compile(pattern, re.IGNORECASE)
self.assertTrue(obj.search(s))
- # Try the match with LOCALE enabled, and check that it
- # still succeeds.
- if '(?u)' not in pattern:
- with self.subTest('locale-sensitive match'):
- obj = re.compile(pattern, re.LOCALE)
- self.assertTrue(obj.search(s))
-
# Try the match with UNICODE locale enabled, and check
# that it still succeeds.
with self.subTest('unicode-sensitive match'):
diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py
index 0b2b0a5..23d6194 100644
--- a/Lib/test/test_readline.py
+++ b/Lib/test/test_readline.py
@@ -2,8 +2,9 @@
Very minimal unittests for parts of the readline module.
"""
import os
+import tempfile
import unittest
-from test.support import run_unittest, import_module
+from test.support import run_unittest, import_module, unlink
from test.script_helper import assert_python_ok
# Skip tests if there is no readline module
@@ -42,6 +43,45 @@ class TestHistoryManipulation (unittest.TestCase):
self.assertEqual(readline.get_current_history_length(), 1)
+ @unittest.skipUnless(hasattr(readline, "append_history_file"),
+ "append_history not available")
+ def test_write_read_append(self):
+ hfile = tempfile.NamedTemporaryFile(delete=False)
+ hfile.close()
+ hfilename = hfile.name
+ self.addCleanup(unlink, hfilename)
+
+ # test write-clear-read == nop
+ readline.clear_history()
+ readline.add_history("first line")
+ readline.add_history("second line")
+ readline.write_history_file(hfilename)
+
+ readline.clear_history()
+ self.assertEqual(readline.get_current_history_length(), 0)
+
+ readline.read_history_file(hfilename)
+ self.assertEqual(readline.get_current_history_length(), 2)
+ self.assertEqual(readline.get_history_item(1), "first line")
+ self.assertEqual(readline.get_history_item(2), "second line")
+
+ # test append
+ readline.append_history_file(1, hfilename)
+ readline.clear_history()
+ readline.read_history_file(hfilename)
+ self.assertEqual(readline.get_current_history_length(), 3)
+ self.assertEqual(readline.get_history_item(1), "first line")
+ self.assertEqual(readline.get_history_item(2), "second line")
+ self.assertEqual(readline.get_history_item(3), "second line")
+
+ # test 'no such file' behaviour
+ os.unlink(hfilename)
+ with self.assertRaises(FileNotFoundError):
+ readline.append_history_file(1, hfilename)
+
+ # write_history_file can create the target
+ readline.write_history_file(hfilename)
+
class TestReadline(unittest.TestCase):
diff --git a/Lib/test/test_reprlib.py b/Lib/test/test_reprlib.py
index ae67f06..a51c4d7 100644
--- a/Lib/test/test_reprlib.py
+++ b/Lib/test/test_reprlib.py
@@ -10,7 +10,7 @@ import importlib
import importlib.util
import unittest
-from test.support import run_unittest, create_empty_file, verbose
+from test.support import create_empty_file, verbose
from reprlib import repr as r # Don't shadow builtin repr
from reprlib import Repr
from reprlib import recursive_repr
@@ -70,18 +70,18 @@ class ReprTests(unittest.TestCase):
eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]")
# Sets give up after 6 as well
- eq(r(set([])), "set([])")
- eq(r(set([1])), "set([1])")
- eq(r(set([1, 2, 3])), "set([1, 2, 3])")
- eq(r(set([1, 2, 3, 4, 5, 6])), "set([1, 2, 3, 4, 5, 6])")
- eq(r(set([1, 2, 3, 4, 5, 6, 7])), "set([1, 2, 3, 4, 5, 6, ...])")
+ eq(r(set([])), "set()")
+ eq(r(set([1])), "{1}")
+ eq(r(set([1, 2, 3])), "{1, 2, 3}")
+ eq(r(set([1, 2, 3, 4, 5, 6])), "{1, 2, 3, 4, 5, 6}")
+ eq(r(set([1, 2, 3, 4, 5, 6, 7])), "{1, 2, 3, 4, 5, 6, ...}")
# Frozensets give up after 6 as well
- eq(r(frozenset([])), "frozenset([])")
- eq(r(frozenset([1])), "frozenset([1])")
- eq(r(frozenset([1, 2, 3])), "frozenset([1, 2, 3])")
- eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])")
- eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])")
+ eq(r(frozenset([])), "frozenset()")
+ eq(r(frozenset([1])), "frozenset({1})")
+ eq(r(frozenset([1, 2, 3])), "frozenset({1, 2, 3})")
+ eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset({1, 2, 3, 4, 5, 6})")
+ eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset({1, 2, 3, 4, 5, 6, ...})")
# collections.deque after 6
eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])")
@@ -94,7 +94,7 @@ class ReprTests(unittest.TestCase):
eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")
# array.array after 5.
- eq(r(array('i')), "array('i', [])")
+ eq(r(array('i')), "array('i')")
eq(r(array('i', [1])), "array('i', [1])")
eq(r(array('i', [1, 2])), "array('i', [1, 2])")
eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])")
@@ -103,6 +103,20 @@ class ReprTests(unittest.TestCase):
eq(r(array('i', [1, 2, 3, 4, 5, 6])),
"array('i', [1, 2, 3, 4, 5, ...])")
+ def test_set_literal(self):
+ eq = self.assertEqual
+ eq(r({1}), "{1}")
+ eq(r({1, 2, 3}), "{1, 2, 3}")
+ eq(r({1, 2, 3, 4, 5, 6}), "{1, 2, 3, 4, 5, 6}")
+ eq(r({1, 2, 3, 4, 5, 6, 7}), "{1, 2, 3, 4, 5, 6, ...}")
+
+ def test_frozenset(self):
+ eq = self.assertEqual
+ eq(r(frozenset({1})), "frozenset({1})")
+ eq(r(frozenset({1, 2, 3})), "frozenset({1, 2, 3})")
+ eq(r(frozenset({1, 2, 3, 4, 5, 6})), "frozenset({1, 2, 3, 4, 5, 6})")
+ eq(r(frozenset({1, 2, 3, 4, 5, 6, 7})), "frozenset({1, 2, 3, 4, 5, 6, ...})")
+
def test_numbers(self):
eq = self.assertEqual
eq(r(123), repr(123))
@@ -123,7 +137,7 @@ class ReprTests(unittest.TestCase):
eq(r(i2), expected)
i3 = ClassWithFailingRepr()
- eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3)))
+ eq(r(i3), ("<ClassWithFailingRepr instance at %#x>"%id(i3)))
s = r(ClassWithFailingRepr)
self.assertTrue(s.startswith("<class "))
@@ -373,11 +387,5 @@ class TestRecursiveRepr(unittest.TestCase):
m.append(m)
self.assertEqual(repr(m), '<a, b, c, d, e, +++, x, +++>')
-def test_main():
- run_unittest(ReprTests)
- run_unittest(LongReprTest)
- run_unittest(TestRecursiveRepr)
-
-
if __name__ == "__main__":
- test_main()
+ unittest.main()
diff --git a/Lib/test/test_script_helper.py b/Lib/test/test_script_helper.py
index 7540e2e..74333c9 100755
--- a/Lib/test/test_script_helper.py
+++ b/Lib/test/test_script_helper.py
@@ -36,7 +36,7 @@ class TestScriptHelper(unittest.TestCase):
@mock.patch('subprocess.Popen')
def test_assert_python_isolated_when_env_not_required(self, mock_popen):
with mock.patch.object(script_helper,
- '_interpreter_requires_environment',
+ 'interpreter_requires_environment',
return_value=False) as mock_ire_func:
mock_popen.side_effect = RuntimeError('bail out of unittest')
try:
@@ -55,7 +55,7 @@ class TestScriptHelper(unittest.TestCase):
def test_assert_python_not_isolated_when_env_is_required(self, mock_popen):
"""Ensure that -I is not passed when the environment is required."""
with mock.patch.object(script_helper,
- '_interpreter_requires_environment',
+ 'interpreter_requires_environment',
return_value=True) as mock_ire_func:
mock_popen.side_effect = RuntimeError('bail out of unittest')
try:
@@ -68,7 +68,7 @@ class TestScriptHelper(unittest.TestCase):
class TestScriptHelperEnvironment(unittest.TestCase):
- """Code coverage for _interpreter_requires_environment()."""
+ """Code coverage for interpreter_requires_environment()."""
def setUp(self):
self.assertTrue(
@@ -83,22 +83,22 @@ class TestScriptHelperEnvironment(unittest.TestCase):
@mock.patch('subprocess.check_call')
def test_interpreter_requires_environment_true(self, mock_check_call):
mock_check_call.side_effect = subprocess.CalledProcessError('', '')
- self.assertTrue(script_helper._interpreter_requires_environment())
- self.assertTrue(script_helper._interpreter_requires_environment())
+ self.assertTrue(script_helper.interpreter_requires_environment())
+ self.assertTrue(script_helper.interpreter_requires_environment())
self.assertEqual(1, mock_check_call.call_count)
@mock.patch('subprocess.check_call')
def test_interpreter_requires_environment_false(self, mock_check_call):
# The mocked subprocess.check_call fakes a no-error process.
- script_helper._interpreter_requires_environment()
- self.assertFalse(script_helper._interpreter_requires_environment())
+ script_helper.interpreter_requires_environment()
+ self.assertFalse(script_helper.interpreter_requires_environment())
self.assertEqual(1, mock_check_call.call_count)
@mock.patch('subprocess.check_call')
def test_interpreter_requires_environment_details(self, mock_check_call):
- script_helper._interpreter_requires_environment()
- self.assertFalse(script_helper._interpreter_requires_environment())
- self.assertFalse(script_helper._interpreter_requires_environment())
+ script_helper.interpreter_requires_environment()
+ self.assertFalse(script_helper.interpreter_requires_environment())
+ self.assertFalse(script_helper.interpreter_requires_environment())
self.assertEqual(1, mock_check_call.call_count)
check_call_command = mock_check_call.call_args[0][0]
self.assertEqual(sys.executable, check_call_command[0])
diff --git a/Lib/test/test_selectors.py b/Lib/test/test_selectors.py
index 952fda6..9521481 100644
--- a/Lib/test/test_selectors.py
+++ b/Lib/test/test_selectors.py
@@ -9,10 +9,7 @@ from test import support
from time import sleep
import unittest
import unittest.mock
-try:
- from time import monotonic as time
-except ImportError:
- from time import time as time
+from time import monotonic as time
try:
import resource
except ImportError:
@@ -25,7 +22,7 @@ else:
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
with socket.socket(family, type, proto) as l:
l.bind((support.HOST, 0))
- l.listen(3)
+ l.listen()
c = socket.socket(family, type, proto)
try:
c.connect(l.getsockname())
@@ -188,8 +185,8 @@ class BaseSelectorTestCase(unittest.TestCase):
s.register(wr, selectors.EVENT_WRITE)
s.close()
- self.assertRaises(KeyError, s.get_key, rd)
- self.assertRaises(KeyError, s.get_key, wr)
+ self.assertRaises(RuntimeError, s.get_key, rd)
+ self.assertRaises(RuntimeError, s.get_key, wr)
self.assertRaises(KeyError, mapping.__getitem__, rd)
self.assertRaises(KeyError, mapping.__getitem__, wr)
@@ -258,8 +255,8 @@ class BaseSelectorTestCase(unittest.TestCase):
sel.register(rd, selectors.EVENT_READ)
sel.register(wr, selectors.EVENT_WRITE)
- self.assertRaises(KeyError, s.get_key, rd)
- self.assertRaises(KeyError, s.get_key, wr)
+ self.assertRaises(RuntimeError, s.get_key, rd)
+ self.assertRaises(RuntimeError, s.get_key, wr)
def test_fileno(self):
s = self.SELECTOR()
@@ -455,10 +452,18 @@ class KqueueSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn):
SELECTOR = getattr(selectors, 'KqueueSelector', None)
+@unittest.skipUnless(hasattr(selectors, 'DevpollSelector'),
+ "Test needs selectors.DevpollSelector")
+class DevpollSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn):
+
+ SELECTOR = getattr(selectors, 'DevpollSelector', None)
+
+
+
def test_main():
tests = [DefaultSelectorTestCase, SelectSelectorTestCase,
PollSelectorTestCase, EpollSelectorTestCase,
- KqueueSelectorTestCase]
+ KqueueSelectorTestCase, DevpollSelectorTestCase]
support.run_unittest(*tests)
support.reap_children()
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py
index 65e4243..c25386f 100644
--- a/Lib/test/test_set.py
+++ b/Lib/test/test_set.py
@@ -931,7 +931,7 @@ class TestBasicOpsString(TestBasicOps, unittest.TestCase):
class TestBasicOpsBytes(TestBasicOps, unittest.TestCase):
def setUp(self):
- self.case = "string set"
+ self.case = "bytes set"
self.values = [b"a", b"b", b"c"]
self.set = set(self.values)
self.dup = set(self.values)
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index 9325bc7..c5545ba 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -34,6 +34,12 @@ try:
except ImportError:
BZ2_SUPPORTED = False
+try:
+ import lzma
+ LZMA_SUPPORTED = True
+except ImportError:
+ LZMA_SUPPORTED = False
+
TESTFN2 = TESTFN + "2"
try:
@@ -1189,6 +1195,8 @@ class TestShutil(unittest.TestCase):
formats = ['tar', 'gztar', 'zip']
if BZ2_SUPPORTED:
formats.append('bztar')
+ if LZMA_SUPPORTED:
+ formats.append('xztar')
for format in formats:
tmpdir = self.mkdtemp()
@@ -1625,6 +1633,24 @@ class TestMove(unittest.TestCase):
rv = shutil.move(self.src_file, os.path.join(self.dst_dir, 'bar'))
self.assertEqual(rv, os.path.join(self.dst_dir, 'bar'))
+ @mock_rename
+ def test_move_file_special_function(self):
+ moved = []
+ def _copy(src, dst):
+ moved.append((src, dst))
+ shutil.move(self.src_file, self.dst_dir, copy_function=_copy)
+ self.assertEqual(len(moved), 1)
+
+ @mock_rename
+ def test_move_dir_special_function(self):
+ moved = []
+ def _copy(src, dst):
+ moved.append((src, dst))
+ support.create_empty_file(os.path.join(self.src_dir, 'child'))
+ support.create_empty_file(os.path.join(self.src_dir, 'child1'))
+ shutil.move(self.src_dir, self.dst_dir, copy_function=_copy)
+ self.assertEqual(len(moved), 3)
+
class TestCopyFile(unittest.TestCase):
diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py
index 74f74af..774fc80 100644
--- a/Lib/test/test_signal.py
+++ b/Lib/test/test_signal.py
@@ -1,10 +1,12 @@
import unittest
from test import support
from contextlib import closing
+import enum
import gc
import pickle
import select
import signal
+import socket
import struct
import subprocess
import traceback
@@ -14,6 +16,10 @@ try:
import threading
except ImportError:
threading = None
+try:
+ import _testcapi
+except ImportError:
+ _testcapi = None
class HandlerBCalled(Exception):
@@ -39,6 +45,23 @@ def ignoring_eintr(__func, *args, **kwargs):
return None
+class GenericTests(unittest.TestCase):
+
+ @unittest.skipIf(threading is None, "test needs threading module")
+ def test_enums(self):
+ for name in dir(signal):
+ sig = getattr(signal, name)
+ if name in {'SIG_DFL', 'SIG_IGN'}:
+ self.assertIsInstance(sig, signal.Handlers)
+ elif name in {'SIG_BLOCK', 'SIG_UNBLOCK', 'SIG_SETMASK'}:
+ self.assertIsInstance(sig, signal.Sigmasks)
+ elif name.startswith('SIG') and not name.startswith('SIG_'):
+ self.assertIsInstance(sig, signal.Signals)
+ elif name.startswith('CTRL_'):
+ self.assertIsInstance(sig, signal.Signals)
+ self.assertEqual(sys.platform, "win32")
+
+
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
class InterProcessSignalTests(unittest.TestCase):
MAX_DURATION = 20 # Entire test should last at most 20 sec.
@@ -195,6 +218,7 @@ class PosixTests(unittest.TestCase):
def test_getsignal(self):
hup = signal.signal(signal.SIGHUP, self.trivial_signal_handler)
+ self.assertIsInstance(hup, signal.Handlers)
self.assertEqual(signal.getsignal(signal.SIGHUP),
self.trivial_signal_handler)
signal.signal(signal.SIGHUP, hup)
@@ -229,15 +253,77 @@ class WakeupFDTests(unittest.TestCase):
def test_invalid_fd(self):
fd = support.make_bad_fd()
- self.assertRaises(ValueError, signal.set_wakeup_fd, fd)
+ self.assertRaises((ValueError, OSError),
+ signal.set_wakeup_fd, fd)
+
+ def test_invalid_socket(self):
+ sock = socket.socket()
+ fd = sock.fileno()
+ sock.close()
+ self.assertRaises((ValueError, OSError),
+ signal.set_wakeup_fd, fd)
+
+ def test_set_wakeup_fd_result(self):
+ r1, w1 = os.pipe()
+ self.addCleanup(os.close, r1)
+ self.addCleanup(os.close, w1)
+ r2, w2 = os.pipe()
+ self.addCleanup(os.close, r2)
+ self.addCleanup(os.close, w2)
+
+ if hasattr(os, 'set_blocking'):
+ os.set_blocking(w1, False)
+ os.set_blocking(w2, False)
+
+ signal.set_wakeup_fd(w1)
+ self.assertEqual(signal.set_wakeup_fd(w2), w1)
+ self.assertEqual(signal.set_wakeup_fd(-1), w2)
+ self.assertEqual(signal.set_wakeup_fd(-1), -1)
+
+ def test_set_wakeup_fd_socket_result(self):
+ sock1 = socket.socket()
+ self.addCleanup(sock1.close)
+ sock1.setblocking(False)
+ fd1 = sock1.fileno()
+
+ sock2 = socket.socket()
+ self.addCleanup(sock2.close)
+ sock2.setblocking(False)
+ fd2 = sock2.fileno()
+
+ signal.set_wakeup_fd(fd1)
+ self.assertEqual(signal.set_wakeup_fd(fd2), fd1)
+ self.assertEqual(signal.set_wakeup_fd(-1), fd2)
+ self.assertEqual(signal.set_wakeup_fd(-1), -1)
+
+ # On Windows, files are always blocking and Windows does not provide a
+ # function to test if a socket is in non-blocking mode.
+ @unittest.skipIf(sys.platform == "win32", "tests specific to POSIX")
+ def test_set_wakeup_fd_blocking(self):
+ rfd, wfd = os.pipe()
+ self.addCleanup(os.close, rfd)
+ self.addCleanup(os.close, wfd)
+
+ # fd must be non-blocking
+ os.set_blocking(wfd, True)
+ with self.assertRaises(ValueError) as cm:
+ signal.set_wakeup_fd(wfd)
+ self.assertEqual(str(cm.exception),
+ "the fd %s must be in non-blocking mode" % wfd)
+
+ # non-blocking is ok
+ os.set_blocking(wfd, False)
+ signal.set_wakeup_fd(wfd)
+ signal.set_wakeup_fd(-1)
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
class WakeupSignalTests(unittest.TestCase):
+ @unittest.skipIf(_testcapi is None, 'need _testcapi')
def check_wakeup(self, test_body, *signals, ordered=True):
# use a subprocess to have only one thread
code = """if 1:
- import fcntl
+ import _testcapi
import os
import signal
import struct
@@ -260,10 +346,7 @@ class WakeupSignalTests(unittest.TestCase):
signal.signal(signal.SIGALRM, handler)
read, write = os.pipe()
- for fd in (read, write):
- flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
- flags = flags | os.O_NONBLOCK
- fcntl.fcntl(fd, fcntl.F_SETFL, flags)
+ os.set_blocking(write, False)
signal.set_wakeup_fd(write)
test()
@@ -271,21 +354,21 @@ class WakeupSignalTests(unittest.TestCase):
os.close(read)
os.close(write)
- """.format(signals, ordered, test_body)
+ """.format(tuple(map(int, signals)), ordered, test_body)
assert_python_ok('-c', code)
+ @unittest.skipIf(_testcapi is None, 'need _testcapi')
def test_wakeup_write_error(self):
# Issue #16105: write() errors in the C signal handler should not
# pass silently.
# Use a subprocess to have only one thread.
code = """if 1:
+ import _testcapi
import errno
- import fcntl
import os
import signal
import sys
- import time
from test.support import captured_stderr
def handler(signum, frame):
@@ -293,15 +376,13 @@ class WakeupSignalTests(unittest.TestCase):
signal.signal(signal.SIGALRM, handler)
r, w = os.pipe()
- flags = fcntl.fcntl(r, fcntl.F_GETFL, 0)
- fcntl.fcntl(r, fcntl.F_SETFL, flags | os.O_NONBLOCK)
+ os.set_blocking(r, False)
# Set wakeup_fd a read-only file descriptor to trigger the error
signal.set_wakeup_fd(r)
try:
with captured_stderr() as err:
- signal.alarm(1)
- time.sleep(5.0)
+ _testcapi.raise_signal(signal.SIGALRM)
except ZeroDivisionError:
# An ignored exception should have been printed out on stderr
err = err.getvalue()
@@ -312,6 +393,9 @@ class WakeupSignalTests(unittest.TestCase):
raise AssertionError(err)
else:
raise AssertionError("ZeroDivisionError not raised")
+
+ os.close(r)
+ os.close(w)
"""
r, w = os.pipe()
try:
@@ -375,9 +459,10 @@ class WakeupSignalTests(unittest.TestCase):
def test_signum(self):
self.check_wakeup("""def test():
+ import _testcapi
signal.signal(signal.SIGUSR1, handler)
- os.kill(os.getpid(), signal.SIGUSR1)
- os.kill(os.getpid(), signal.SIGALRM)
+ _testcapi.raise_signal(signal.SIGUSR1)
+ _testcapi.raise_signal(signal.SIGALRM)
""", signal.SIGUSR1, signal.SIGALRM)
@unittest.skipUnless(hasattr(signal, 'pthread_sigmask'),
@@ -391,13 +476,97 @@ class WakeupSignalTests(unittest.TestCase):
signal.signal(signum2, handler)
signal.pthread_sigmask(signal.SIG_BLOCK, (signum1, signum2))
- os.kill(os.getpid(), signum1)
- os.kill(os.getpid(), signum2)
+ _testcapi.raise_signal(signum1)
+ _testcapi.raise_signal(signum2)
# Unblocking the 2 signals calls the C signal handler twice
signal.pthread_sigmask(signal.SIG_UNBLOCK, (signum1, signum2))
""", signal.SIGUSR1, signal.SIGUSR2, ordered=False)
+@unittest.skipUnless(hasattr(socket, 'socketpair'), 'need socket.socketpair')
+class WakeupSocketSignalTests(unittest.TestCase):
+
+ @unittest.skipIf(_testcapi is None, 'need _testcapi')
+ def test_socket(self):
+ # use a subprocess to have only one thread
+ code = """if 1:
+ import signal
+ import socket
+ import struct
+ import _testcapi
+
+ signum = signal.SIGINT
+ signals = (signum,)
+
+ def handler(signum, frame):
+ pass
+
+ signal.signal(signum, handler)
+
+ read, write = socket.socketpair()
+ read.setblocking(False)
+ write.setblocking(False)
+ signal.set_wakeup_fd(write.fileno())
+
+ _testcapi.raise_signal(signum)
+
+ data = read.recv(1)
+ if not data:
+ raise Exception("no signum written")
+ raised = struct.unpack('B', data)
+ if raised != signals:
+ raise Exception("%r != %r" % (raised, signals))
+
+ read.close()
+ write.close()
+ """
+
+ assert_python_ok('-c', code)
+
+ @unittest.skipIf(_testcapi is None, 'need _testcapi')
+ def test_send_error(self):
+ # Use a subprocess to have only one thread.
+ if os.name == 'nt':
+ action = 'send'
+ else:
+ action = 'write'
+ code = """if 1:
+ import errno
+ import signal
+ import socket
+ import sys
+ import time
+ import _testcapi
+ from test.support import captured_stderr
+
+ signum = signal.SIGINT
+
+ def handler(signum, frame):
+ pass
+
+ signal.signal(signum, handler)
+
+ read, write = socket.socketpair()
+ read.setblocking(False)
+ write.setblocking(False)
+
+ signal.set_wakeup_fd(write.fileno())
+
+ # Close sockets: send() will fail
+ read.close()
+ write.close()
+
+ with captured_stderr() as err:
+ _testcapi.raise_signal(signum)
+
+ err = err.getvalue()
+ if ('Exception ignored when trying to {action} to the signal wakeup fd'
+ not in err):
+ raise AssertionError(err)
+ """.format(action=action)
+ assert_python_ok('-c', code)
+
+
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
class SiginterruptTest(unittest.TestCase):
@@ -418,7 +587,7 @@ class SiginterruptTest(unittest.TestCase):
r, w = os.pipe()
def handler(signum, frame):
- pass
+ 1 / 0
signal.signal(signal.SIGALRM, handler)
if interrupt is not None:
@@ -428,18 +597,21 @@ class SiginterruptTest(unittest.TestCase):
sys.stdout.flush()
# run the test twice
- for loop in range(2):
- # send a SIGALRM in a second (during the read)
- signal.alarm(1)
- try:
- # blocking call: read from a pipe without data
- os.read(r, 1)
- except OSError as err:
- if err.errno != errno.EINTR:
- raise
- else:
- sys.exit(2)
- sys.exit(3)
+ try:
+ for loop in range(2):
+ # send a SIGALRM in a second (during the read)
+ signal.alarm(1)
+ try:
+ # blocking call: read from a pipe without data
+ os.read(r, 1)
+ except ZeroDivisionError:
+ pass
+ else:
+ sys.exit(2)
+ sys.exit(3)
+ finally:
+ os.close(r)
+ os.close(w)
""" % (interrupt,)
with spawn_python('-c', code) as process:
try:
@@ -604,6 +776,8 @@ class PendingSignalsTests(unittest.TestCase):
signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
os.kill(os.getpid(), signum)
pending = signal.sigpending()
+ for sig in pending:
+ assert isinstance(sig, signal.Signals), repr(pending)
if pending != {signum}:
raise Exception('%s != {%s}' % (pending, signum))
try:
@@ -660,6 +834,7 @@ class PendingSignalsTests(unittest.TestCase):
code = '''if 1:
import signal
import sys
+ from signal import Signals
def handler(signum, frame):
1/0
@@ -702,6 +877,7 @@ class PendingSignalsTests(unittest.TestCase):
def test(signum):
signal.alarm(1)
received = signal.sigwait([signum])
+ assert isinstance(received, signal.Signals), received
if received != signum:
raise Exception('received %s, not %s' % (received, signum))
''')
@@ -842,8 +1018,14 @@ class PendingSignalsTests(unittest.TestCase):
def kill(signum):
os.kill(os.getpid(), signum)
+ def check_mask(mask):
+ for sig in mask:
+ assert isinstance(sig, signal.Signals), repr(sig)
+
def read_sigmask():
- return signal.pthread_sigmask(signal.SIG_BLOCK, [])
+ sigmask = signal.pthread_sigmask(signal.SIG_BLOCK, [])
+ check_mask(sigmask)
+ return sigmask
signum = signal.SIGUSR1
@@ -852,6 +1034,7 @@ class PendingSignalsTests(unittest.TestCase):
# Unblock SIGUSR1 (and copy the old mask) to test our signal handler
old_mask = signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum])
+ check_mask(old_mask)
try:
kill(signum)
except ZeroDivisionError:
@@ -861,11 +1044,13 @@ class PendingSignalsTests(unittest.TestCase):
# Block and then raise SIGUSR1. The signal is blocked: the signal
# handler is not called, and the signal is now pending
- signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
+ mask = signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
+ check_mask(mask)
kill(signum)
# Check the new mask
blocked = read_sigmask()
+ check_mask(blocked)
if signum not in blocked:
raise Exception("%s not in %s" % (signum, blocked))
if old_mask ^ blocked != {signum}:
@@ -928,8 +1113,9 @@ class PendingSignalsTests(unittest.TestCase):
def test_main():
try:
- support.run_unittest(PosixTests, InterProcessSignalTests,
+ support.run_unittest(GenericTests, PosixTests, InterProcessSignalTests,
WakeupFDTests, WakeupSignalTests,
+ WakeupSocketSignalTests,
SiginterruptTest, ItimerTest, WindowsSignalTests,
PendingSignalsTests)
finally:
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index f71cf73..e234164 100644
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -147,7 +147,7 @@ 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(), 'TypeError')
+ self.assertRegex(err_out.getvalue(), 'ValueError')
def test_addsitedir(self):
# Same tests for test_addpackage since addsitedir() essentially just
@@ -235,20 +235,18 @@ class HelperFunctionsTests(unittest.TestCase):
# OS X framework builds
site.PREFIXES = ['Python.framework']
dirs = site.getsitepackages()
- self.assertEqual(len(dirs), 3)
+ self.assertEqual(len(dirs), 2)
wanted = os.path.join('/Library',
sysconfig.get_config_var("PYTHONFRAMEWORK"),
sys.version[:3],
'site-packages')
- self.assertEqual(dirs[2], wanted)
+ self.assertEqual(dirs[1], wanted)
elif os.sep == '/':
# OS X non-framwework builds, Linux, FreeBSD, etc
- self.assertEqual(len(dirs), 2)
+ self.assertEqual(len(dirs), 1)
wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3],
'site-packages')
self.assertEqual(dirs[0], wanted)
- wanted = os.path.join('xoxo', 'lib', 'site-python')
- self.assertEqual(dirs[1], wanted)
else:
# other platforms
self.assertEqual(len(dirs), 2)
diff --git a/Lib/test/test_smtpd.py b/Lib/test/test_smtpd.py
index 93f14c4..6eb47f1 100644
--- a/Lib/test/test_smtpd.py
+++ b/Lib/test/test_smtpd.py
@@ -1,4 +1,5 @@
import unittest
+import textwrap
from test import support, mock_socket
import socket
import io
@@ -7,15 +8,22 @@ import asyncore
class DummyServer(smtpd.SMTPServer):
- def __init__(self, localaddr, remoteaddr):
- smtpd.SMTPServer.__init__(self, localaddr, remoteaddr)
+ def __init__(self, *args, **kwargs):
+ smtpd.SMTPServer.__init__(self, *args, **kwargs)
self.messages = []
+ if self._decode_data:
+ self.return_status = 'return status'
+ else:
+ self.return_status = b'return status'
def process_message(self, peer, mailfrom, rcpttos, data):
self.messages.append((peer, mailfrom, rcpttos, data))
- if data == 'return status':
+ if data == self.return_status:
return '250 Okish'
+ def process_smtputf8_message(self, *args, **kwargs):
+ return '250 SMTPUTF8 message okish'
+
class DummyDispatcherBroken(Exception):
pass
@@ -31,9 +39,10 @@ class SMTPDServerTest(unittest.TestCase):
smtpd.socket = asyncore.socket = mock_socket
def test_process_message_unimplemented(self):
- server = smtpd.SMTPServer('a', 'b')
+ server = smtpd.SMTPServer((support.HOST, 0), ('b', 0),
+ decode_data=True)
conn, addr = server.accept()
- channel = smtpd.SMTPChannel(server, conn, addr)
+ channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
def write_line(line):
channel.socket.queue_recv(line)
@@ -45,19 +54,163 @@ class SMTPDServerTest(unittest.TestCase):
write_line(b'DATA')
self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n')
+ def test_process_smtputf8_message_unimplemented(self):
+ server = smtpd.SMTPServer((support.HOST, 0), ('b', 0),
+ enable_SMTPUTF8=True)
+ conn, addr = server.accept()
+ channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
+
+ def write_line(line):
+ channel.socket.queue_recv(line)
+ channel.handle_read()
+
+ write_line(b'EHLO example')
+ write_line(b'MAIL From: <eggs@example> BODY=8BITMIME SMTPUTF8')
+ write_line(b'RCPT To: <spam@example>')
+ 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,
+ smtpd.SMTPServer,
+ (support.HOST, 0),
+ ('b', 0),
+ enable_SMTPUTF8=True,
+ decode_data=True)
+
+ def tearDown(self):
+ asyncore.close_all()
+ asyncore.socket = smtpd.socket = socket
+
+
+class DebuggingServerTest(unittest.TestCase):
+
+ def setUp(self):
+ smtpd.socket = asyncore.socket = mock_socket
+
+ def send_data(self, channel, data, enable_SMTPUTF8=False):
+ def write_line(line):
+ channel.socket.queue_recv(line)
+ channel.handle_read()
+ write_line(b'EHLO example')
+ if enable_SMTPUTF8:
+ write_line(b'MAIL From:eggs@example BODY=8BITMIME SMTPUTF8')
+ else:
+ write_line(b'MAIL From:eggs@example')
+ write_line(b'RCPT To:spam@example')
+ write_line(b'DATA')
+ write_line(data)
+ write_line(b'.')
+
+ def test_process_message_with_decode_data_true(self):
+ server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
+ decode_data=True)
+ conn, addr = server.accept()
+ channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
+ with support.captured_stdout() as s:
+ self.send_data(channel, b'From: test\n\nhello\n')
+ stdout = s.getvalue()
+ self.assertEqual(stdout, textwrap.dedent("""\
+ ---------- MESSAGE FOLLOWS ----------
+ From: test
+ X-Peer: peer-address
+
+ hello
+ ------------ END MESSAGE ------------
+ """))
+
+ def test_process_message_with_decode_data_false(self):
+ server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
+ decode_data=False)
+ conn, addr = server.accept()
+ channel = smtpd.SMTPChannel(server, conn, addr, decode_data=False)
+ with support.captured_stdout() as s:
+ self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
+ stdout = s.getvalue()
+ self.assertEqual(stdout, textwrap.dedent("""\
+ ---------- MESSAGE FOLLOWS ----------
+ b'From: test'
+ b'X-Peer: peer-address'
+ b''
+ b'h\\xc3\\xa9llo\\xff'
+ ------------ END MESSAGE ------------
+ """))
+
+ def test_process_message_with_enable_SMTPUTF8_true(self):
+ server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
+ enable_SMTPUTF8=True)
+ conn, addr = server.accept()
+ channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
+ with support.captured_stdout() as s:
+ self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
+ stdout = s.getvalue()
+ self.assertEqual(stdout, textwrap.dedent("""\
+ ---------- MESSAGE FOLLOWS ----------
+ b'From: test'
+ b'X-Peer: peer-address'
+ b''
+ b'h\\xc3\\xa9llo\\xff'
+ ------------ END MESSAGE ------------
+ """))
+
+ def test_process_SMTPUTF8_message_with_enable_SMTPUTF8_true(self):
+ server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
+ enable_SMTPUTF8=True)
+ conn, addr = server.accept()
+ channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
+ with support.captured_stdout() as s:
+ self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n',
+ enable_SMTPUTF8=True)
+ stdout = s.getvalue()
+ self.assertEqual(stdout, textwrap.dedent("""\
+ ----- SMTPUTF8 MESSAGE FOLLOWS ------
+ b'From: test'
+ b'X-Peer: peer-address'
+ b''
+ b'h\\xc3\\xa9llo\\xff'
+ ------------ END MESSAGE ------------
+ """))
+
def tearDown(self):
asyncore.close_all()
asyncore.socket = smtpd.socket = socket
+class TestFamilyDetection(unittest.TestCase):
+ def setUp(self):
+ smtpd.socket = asyncore.socket = mock_socket
+
+ def tearDown(self):
+ asyncore.close_all()
+ asyncore.socket = smtpd.socket = socket
+
+ @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)
+ 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)
+ self.assertEqual(server.socket.family, socket.AF_INET)
+
+
class SMTPDChannelTest(unittest.TestCase):
def setUp(self):
smtpd.socket = asyncore.socket = mock_socket
self.old_debugstream = smtpd.DEBUGSTREAM
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
- self.server = DummyServer('a', 'b')
+ self.server = DummyServer((support.HOST, 0), ('b', 0),
+ decode_data=True)
conn, addr = self.server.accept()
- self.channel = smtpd.SMTPChannel(self.server, conn, addr)
+ self.channel = smtpd.SMTPChannel(self.server, conn, addr,
+ decode_data=True)
def tearDown(self):
asyncore.close_all()
@@ -69,7 +222,9 @@ class SMTPDChannelTest(unittest.TestCase):
self.channel.handle_read()
def test_broken_connect(self):
- self.assertRaises(DummyDispatcherBroken, BrokenDummyServer, 'a', 'b')
+ self.assertRaises(
+ DummyDispatcherBroken, BrokenDummyServer,
+ (support.HOST, 0), ('b', 0), decode_data=True)
def test_server_accept(self):
self.server.handle_accept()
@@ -214,6 +369,12 @@ class SMTPDChannelTest(unittest.TestCase):
self.assertEqual(self.channel.socket.last,
b'500 Error: line too long\r\n')
+ def test_MAIL_command_rejects_SMTPUTF8_by_default(self):
+ self.write_line(b'EHLO example')
+ self.write_line(
+ b'MAIL from: <naive@example.com> BODY=8BITMIME SMTPUTF8')
+ self.assertEqual(self.channel.socket.last[0:1], b'5')
+
def test_data_longer_than_default_data_size_limit(self):
# Hack the default so we don't have to generate so much data.
self.channel.data_size_limit = 1048
@@ -387,7 +548,10 @@ class SMTPDChannelTest(unittest.TestCase):
self.write_line(b'data\r\nmore\r\n.')
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
self.assertEqual(self.server.messages,
- [('peer', 'eggs@example', ['spam@example'], 'data\nmore')])
+ [(('peer-address', 'peer-port'),
+ 'eggs@example',
+ ['spam@example'],
+ 'data\nmore')])
def test_DATA_syntax(self):
self.write_line(b'HELO example')
@@ -417,7 +581,10 @@ class SMTPDChannelTest(unittest.TestCase):
self.write_line(b'DATA')
self.write_line(b'data\r\n.')
self.assertEqual(self.server.messages,
- [('peer', 'eggs@example', ['spam@example','ham@example'], 'data')])
+ [(('peer-address', 'peer-port'),
+ 'eggs@example',
+ ['spam@example','ham@example'],
+ 'data')])
def test_manual_status(self):
# checks that the Channel is able to return a custom status message
@@ -439,7 +606,10 @@ class SMTPDChannelTest(unittest.TestCase):
self.write_line(b'DATA')
self.write_line(b'data\r\n.')
self.assertEqual(self.server.messages,
- [('peer', 'foo@example', ['eggs@example'], 'data')])
+ [(('peer-address', 'peer-port'),
+ 'foo@example',
+ ['eggs@example'],
+ 'data')])
def test_HELO_RSET(self):
self.write_line(b'HELO example')
@@ -502,6 +672,24 @@ 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):
+ smtpd.socket = asyncore.socket = mock_socket
+ self.old_debugstream = smtpd.DEBUGSTREAM
+ self.debug = smtpd.DEBUGSTREAM = io.StringIO()
+ self.server = DummyServer((support.HOSTv6, 0), ('b', 0),
+ decode_data=True)
+ conn, addr = self.server.accept()
+ self.channel = smtpd.SMTPChannel(self.server, conn, addr,
+ decode_data=True)
class SMTPDChannelWithDataSizeLimitTest(unittest.TestCase):
@@ -509,10 +697,12 @@ class SMTPDChannelWithDataSizeLimitTest(unittest.TestCase):
smtpd.socket = asyncore.socket = mock_socket
self.old_debugstream = smtpd.DEBUGSTREAM
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
- self.server = DummyServer('a', 'b')
+ self.server = DummyServer((support.HOST, 0), ('b', 0),
+ decode_data=True)
conn, addr = self.server.accept()
# Set DATA size limit to 32 bytes for easy testing
- self.channel = smtpd.SMTPChannel(self.server, conn, addr, 32)
+ self.channel = smtpd.SMTPChannel(self.server, conn, addr, 32,
+ decode_data=True)
def tearDown(self):
asyncore.close_all()
@@ -536,7 +726,10 @@ class SMTPDChannelWithDataSizeLimitTest(unittest.TestCase):
self.write_line(b'data\r\nmore\r\n.')
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
self.assertEqual(self.server.messages,
- [('peer', 'eggs@example', ['spam@example'], 'data\nmore')])
+ [(('peer-address', 'peer-port'),
+ 'eggs@example',
+ ['spam@example'],
+ 'data\nmore')])
def test_data_limit_dialog_too_much_data(self):
self.write_line(b'HELO example')
@@ -553,5 +746,181 @@ class SMTPDChannelWithDataSizeLimitTest(unittest.TestCase):
b'552 Error: Too much mail data\r\n')
+class SMTPDChannelWithDecodeDataFalse(unittest.TestCase):
+
+ def setUp(self):
+ 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)
+ conn, addr = self.server.accept()
+ # Set decode_data to False
+ self.channel = smtpd.SMTPChannel(self.server, conn, addr,
+ decode_data=False)
+
+ def tearDown(self):
+ asyncore.close_all()
+ asyncore.socket = smtpd.socket = socket
+ smtpd.DEBUGSTREAM = self.old_debugstream
+
+ def write_line(self, line):
+ self.channel.socket.queue_recv(line)
+ self.channel.handle_read()
+
+ def test_ascii_data(self):
+ self.write_line(b'HELO example')
+ self.write_line(b'MAIL From:eggs@example')
+ self.write_line(b'RCPT To:spam@example')
+ self.write_line(b'DATA')
+ self.write_line(b'plain ascii text')
+ self.write_line(b'.')
+ self.assertEqual(self.channel.received_data, b'plain ascii text')
+
+ def test_utf8_data(self):
+ self.write_line(b'HELO example')
+ self.write_line(b'MAIL From:eggs@example')
+ self.write_line(b'RCPT To:spam@example')
+ self.write_line(b'DATA')
+ self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
+ self.write_line(b'and some plain ascii')
+ self.write_line(b'.')
+ self.assertEqual(
+ self.channel.received_data,
+ b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87\n'
+ b'and some plain ascii')
+
+
+class SMTPDChannelWithDecodeDataTrue(unittest.TestCase):
+
+ def setUp(self):
+ 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=True)
+ conn, addr = self.server.accept()
+ # Set decode_data to True
+ self.channel = smtpd.SMTPChannel(self.server, conn, addr,
+ decode_data=True)
+
+ def tearDown(self):
+ asyncore.close_all()
+ asyncore.socket = smtpd.socket = socket
+ smtpd.DEBUGSTREAM = self.old_debugstream
+
+ def write_line(self, line):
+ self.channel.socket.queue_recv(line)
+ self.channel.handle_read()
+
+ def test_ascii_data(self):
+ self.write_line(b'HELO example')
+ self.write_line(b'MAIL From:eggs@example')
+ self.write_line(b'RCPT To:spam@example')
+ self.write_line(b'DATA')
+ self.write_line(b'plain ascii text')
+ self.write_line(b'.')
+ self.assertEqual(self.channel.received_data, 'plain ascii text')
+
+ def test_utf8_data(self):
+ self.write_line(b'HELO example')
+ self.write_line(b'MAIL From:eggs@example')
+ self.write_line(b'RCPT To:spam@example')
+ self.write_line(b'DATA')
+ self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
+ self.write_line(b'and some plain ascii')
+ self.write_line(b'.')
+ self.assertEqual(
+ self.channel.received_data,
+ 'utf8 enriched text: żźć\nand some plain ascii')
+
+
+class SMTPDChannelTestWithEnableSMTPUTF8True(unittest.TestCase):
+ def setUp(self):
+ 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),
+ enable_SMTPUTF8=True)
+ conn, addr = self.server.accept()
+ self.channel = smtpd.SMTPChannel(self.server, conn, addr,
+ enable_SMTPUTF8=True)
+
+ def tearDown(self):
+ asyncore.close_all()
+ asyncore.socket = smtpd.socket = socket
+ smtpd.DEBUGSTREAM = self.old_debugstream
+
+ def write_line(self, line):
+ self.channel.socket.queue_recv(line)
+ self.channel.handle_read()
+
+ def test_MAIL_command_accepts_SMTPUTF8_when_announced(self):
+ self.write_line(b'EHLO example')
+ self.write_line(
+ 'MAIL from: <naïve@example.com> BODY=8BITMIME SMTPUTF8'.encode(
+ 'utf-8')
+ )
+ self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
+
+ def test_process_smtputf8_message(self):
+ self.write_line(b'EHLO example')
+ for mail_parameters in [b'', b'BODY=8BITMIME SMTPUTF8']:
+ self.write_line(b'MAIL from: <a@example> ' + mail_parameters)
+ self.assertEqual(self.channel.socket.last[0:3], b'250')
+ self.write_line(b'rcpt to:<b@example.com>')
+ self.assertEqual(self.channel.socket.last[0:3], b'250')
+ self.write_line(b'data')
+ self.assertEqual(self.channel.socket.last[0:3], b'354')
+ self.write_line(b'c\r\n.')
+ if mail_parameters == b'':
+ self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
+ else:
+ self.assertEqual(self.channel.socket.last,
+ b'250 SMTPUTF8 message okish\r\n')
+
+ def test_utf8_data(self):
+ self.write_line(b'EHLO example')
+ self.write_line(
+ 'MAIL From: naïve@examplé BODY=8BITMIME SMTPUTF8'.encode('utf-8'))
+ self.assertEqual(self.channel.socket.last[0:3], b'250')
+ self.write_line('RCPT To:späm@examplé'.encode('utf-8'))
+ self.assertEqual(self.channel.socket.last[0:3], b'250')
+ self.write_line(b'DATA')
+ self.assertEqual(self.channel.socket.last[0:3], b'354')
+ self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
+ self.write_line(b'.')
+ self.assertEqual(
+ self.channel.received_data,
+ b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
+
+ def test_MAIL_command_limit_extended_with_SIZE_and_SMTPUTF8(self):
+ self.write_line(b'ehlo example')
+ fill_len = (512 + 26 + 10) - len('mail from:<@example>')
+ self.write_line(b'MAIL from:<' +
+ b'a' * (fill_len + 1) +
+ b'@example>')
+ self.assertEqual(self.channel.socket.last,
+ b'500 Error: line too long\r\n')
+ self.write_line(b'MAIL from:<' +
+ b'a' * fill_len +
+ b'@example>')
+ self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
+
+ def test_multiple_emails_with_extended_command_length(self):
+ self.write_line(b'ehlo example')
+ fill_len = (512 + 26 + 10) - len('mail from:<@example>')
+ for char in [b'a', b'b', b'c']:
+ self.write_line(b'MAIL from:<' + char * fill_len + b'a@example>')
+ self.assertEqual(self.channel.socket.last[0:3], b'500')
+ self.write_line(b'MAIL from:<' + char * fill_len + b'@example>')
+ self.assertEqual(self.channel.socket.last[0:3], b'250')
+ self.write_line(b'rcpt to:<hans@example.com>')
+ self.assertEqual(self.channel.socket.last[0:3], b'250')
+ self.write_line(b'data')
+ self.assertEqual(self.channel.socket.last[0:3], b'354')
+ self.write_line(b'test\r\n.')
+ self.assertEqual(self.channel.socket.last[0:3], b'250')
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
index 95a9dbe..5f12d28 100644
--- a/Lib/test/test_smtplib.py
+++ b/Lib/test/test_smtplib.py
@@ -10,6 +10,7 @@ import sys
import time
import select
import errno
+import base64
import unittest
from test import support, mock_socket
@@ -30,7 +31,7 @@ if sys.platform == 'darwin':
def server(evt, buf, serv):
- serv.listen(5)
+ serv.listen()
evt.set()
try:
conn, addr = serv.accept()
@@ -184,7 +185,8 @@ class DebuggingServerTests(unittest.TestCase):
self.old_DEBUGSTREAM = smtpd.DEBUGSTREAM
smtpd.DEBUGSTREAM = io.StringIO()
# Pick a random unused port by passing 0 for the port number
- self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1))
+ self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1),
+ decode_data=True)
# Keep a note of what port was assigned
self.port = self.serv.socket.getsockname()[1]
serv_args = (self.serv, self.serv_evt, self.client_evt)
@@ -604,7 +606,8 @@ sim_auth_credentials = {
'cram-md5': ('TXIUQUBZB21LD2HLCMUUY29TIDG4OWQ0MJ'
'KWZGQ4ODNMNDA4NTGXMDRLZWMYZJDMODG1'),
}
-sim_auth_login_password = 'C29TZXBHC3N3B3JK'
+sim_auth_login_user = 'TXIUQUBZB21LD2HLCMUUY29T'
+sim_auth_plain = 'AE1YLKFAC29TZXDOZXJLLMNVBQBZB21LCGFZC3DVCMQ='
sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'],
'list-2':['Ms.B@xn--fo-fka.com',],
@@ -658,18 +661,16 @@ class SimSMTPChannel(smtpd.SMTPChannel):
self.push('550 No access for you!')
def smtp_AUTH(self, arg):
- if arg.strip().lower()=='cram-md5':
+ mech = arg.strip().lower()
+ if mech=='cram-md5':
self.push('334 {}'.format(sim_cram_md5_challenge))
- return
- mech, auth = arg.split()
- mech = mech.lower()
- if mech not in sim_auth_credentials:
+ elif mech not in sim_auth_credentials:
self.push('504 auth type unimplemented')
return
- if mech == 'plain' and auth==sim_auth_credentials['plain']:
- self.push('235 plain auth ok')
- elif mech=='login' and auth==sim_auth_credentials['login']:
- self.push('334 Password:')
+ elif mech=='plain':
+ self.push('334 ')
+ elif mech=='login':
+ self.push('334 ')
else:
self.push('550 No access for you!')
@@ -719,7 +720,8 @@ class SimSMTPServer(smtpd.SMTPServer):
def handle_accepted(self, conn, addr):
self._SMTPchannel = self.channel_class(
- self._extra_features, self, conn, addr)
+ self._extra_features, self, conn, addr,
+ decode_data=self._decode_data)
def process_message(self, peer, mailfrom, rcpttos, data):
pass
@@ -742,7 +744,7 @@ class SMTPSimTests(unittest.TestCase):
self.serv_evt = threading.Event()
self.client_evt = threading.Event()
# Pick a random unused port by passing 0 for the port number
- self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1))
+ self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1), decode_data=True)
# Keep a note of what port was assigned
self.port = self.serv.socket.getsockname()[1]
serv_args = (self.serv, self.serv_evt, self.client_evt)
@@ -816,28 +818,28 @@ class SMTPSimTests(unittest.TestCase):
self.assertEqual(smtp.expn(u), expected_unknown)
smtp.quit()
- def testAUTH_PLAIN(self):
- self.serv.add_feature("AUTH PLAIN")
- smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
-
- expected_auth_ok = (235, b'plain auth ok')
- self.assertEqual(smtp.login(sim_auth[0], sim_auth[1]), expected_auth_ok)
- smtp.close()
-
- # SimSMTPChannel doesn't fully support LOGIN or CRAM-MD5 auth because they
- # require a synchronous read to obtain the credentials...so instead smtpd
+ # SimSMTPChannel doesn't fully support AUTH because it requires a
+ # synchronous read to obtain the credentials...so instead smtpd
# sees the credential sent by smtplib's login method as an unknown command,
# which results in smtplib raising an auth error. Fortunately the error
# message contains the encoded credential, so we can partially check that it
# was generated correctly (partially, because the 'word' is uppercased in
# the error message).
+ def testAUTH_PLAIN(self):
+ self.serv.add_feature("AUTH PLAIN")
+ smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
+ try: smtp.login(sim_auth[0], sim_auth[1])
+ except smtplib.SMTPAuthenticationError as err:
+ self.assertIn(sim_auth_plain, str(err))
+ smtp.close()
+
def testAUTH_LOGIN(self):
self.serv.add_feature("AUTH LOGIN")
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
try: smtp.login(sim_auth[0], sim_auth[1])
except smtplib.SMTPAuthenticationError as err:
- self.assertIn(sim_auth_login_password, str(err))
+ self.assertIn(sim_auth_login_user, str(err))
smtp.close()
def testAUTH_CRAM_MD5(self):
@@ -855,7 +857,23 @@ class SMTPSimTests(unittest.TestCase):
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
try: smtp.login(sim_auth[0], sim_auth[1])
except smtplib.SMTPAuthenticationError as err:
- self.assertIn(sim_auth_login_password, str(err))
+ self.assertIn(sim_auth_login_user, str(err))
+ smtp.close()
+
+ def test_auth_function(self):
+ smtp = smtplib.SMTP(HOST, self.port,
+ local_hostname='localhost', timeout=15)
+ self.serv.add_feature("AUTH CRAM-MD5")
+ smtp.user, smtp.password = sim_auth[0], sim_auth[1]
+ supported = {'CRAM-MD5': smtp.auth_cram_md5,
+ 'PLAIN': smtp.auth_plain,
+ 'LOGIN': smtp.auth_login,
+ }
+ for mechanism, method in supported.items():
+ try: smtp.auth(mechanism, method)
+ except smtplib.SMTPAuthenticationError as err:
+ self.assertIn(sim_auth_credentials[mechanism.lower()].upper(),
+ str(err))
smtp.close()
def test_quit_resets_greeting(self):
diff --git a/Lib/test/test_sndhdr.py b/Lib/test/test_sndhdr.py
index 5e0abe0..426417c 100644
--- a/Lib/test/test_sndhdr.py
+++ b/Lib/test/test_sndhdr.py
@@ -1,4 +1,5 @@
import sndhdr
+import pickle
import unittest
from test.support import findfile
@@ -18,6 +19,19 @@ class TestFormats(unittest.TestCase):
what = sndhdr.what(filename)
self.assertNotEqual(what, None, filename)
self.assertSequenceEqual(what, expected)
+ self.assertEqual(what.filetype, expected[0])
+ self.assertEqual(what.framerate, expected[1])
+ self.assertEqual(what.nchannels, expected[2])
+ self.assertEqual(what.nframes, expected[3])
+ self.assertEqual(what.sampwidth, expected[4])
+
+ def test_pickleable(self):
+ filename = findfile('sndhdr.aifc', subdir="sndhdrdata")
+ what = sndhdr.what(filename)
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ dump = pickle.dumps(what, proto)
+ self.assertEqual(pickle.loads(dump), what)
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index b412386..cf45b73 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -20,6 +20,8 @@ import signal
import math
import pickle
import struct
+import random
+import string
try:
import multiprocessing
except ImportError:
@@ -76,7 +78,7 @@ class SocketTCPTest(unittest.TestCase):
def setUp(self):
self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.port = support.bind_port(self.serv)
- self.serv.listen(1)
+ self.serv.listen()
def tearDown(self):
self.serv.close()
@@ -445,7 +447,7 @@ class SocketListeningTestMixin(SocketTestBase):
def setUp(self):
super().setUp()
- self.serv.listen(1)
+ self.serv.listen()
class ThreadedSocketTestMixin(ThreadSafeCleanupTestCase, SocketTestBase,
@@ -716,11 +718,11 @@ class GeneralModuleTests(unittest.TestCase):
with self.assertRaises(TypeError) as cm:
s.sendto('\u2620', sockname)
self.assertEqual(str(cm.exception),
- "'str' does not support the buffer interface")
+ "a bytes-like object is required, not 'str'")
with self.assertRaises(TypeError) as cm:
s.sendto(5j, sockname)
self.assertEqual(str(cm.exception),
- "'complex' does not support the buffer interface")
+ "a bytes-like object is required, not 'complex'")
with self.assertRaises(TypeError) as cm:
s.sendto(b'foo', None)
self.assertIn('not NoneType',str(cm.exception))
@@ -728,11 +730,11 @@ class GeneralModuleTests(unittest.TestCase):
with self.assertRaises(TypeError) as cm:
s.sendto('\u2620', 0, sockname)
self.assertEqual(str(cm.exception),
- "'str' does not support the buffer interface")
+ "a bytes-like object is required, not 'str'")
with self.assertRaises(TypeError) as cm:
s.sendto(5j, 0, sockname)
self.assertEqual(str(cm.exception),
- "'complex' does not support the buffer interface")
+ "a bytes-like object is required, not 'complex'")
with self.assertRaises(TypeError) as cm:
s.sendto(b'foo', 0, None)
self.assertIn('not NoneType', str(cm.exception))
@@ -1378,10 +1380,13 @@ class GeneralModuleTests(unittest.TestCase):
def test_listen_backlog(self):
for backlog in 0, -1:
- srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
+ srv.bind((HOST, 0))
+ srv.listen(backlog)
+
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
srv.bind((HOST, 0))
- srv.listen(backlog)
- srv.close()
+ srv.listen()
@support.cpython_only
def test_listen_backlog_overflow(self):
@@ -3585,7 +3590,7 @@ class InterruptedTimeoutBase(unittest.TestCase):
def setUp(self):
super().setUp()
orig_alrm_handler = signal.signal(signal.SIGALRM,
- lambda signum, frame: None)
+ lambda signum, frame: 1 / 0)
self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler)
self.addCleanup(self.setAlarm, 0)
@@ -3622,13 +3627,11 @@ class InterruptedRecvTimeoutTest(InterruptedTimeoutBase, UDPTestBase):
self.serv.settimeout(self.timeout)
def checkInterruptedRecv(self, func, *args, **kwargs):
- # Check that func(*args, **kwargs) raises OSError with an
+ # Check that func(*args, **kwargs) raises
# errno of EINTR when interrupted by a signal.
self.setAlarm(self.alarm_time)
- with self.assertRaises(OSError) as cm:
+ with self.assertRaises(ZeroDivisionError) as cm:
func(*args, **kwargs)
- self.assertNotIsInstance(cm.exception, socket.timeout)
- self.assertEqual(cm.exception.errno, errno.EINTR)
def testInterruptedRecvTimeout(self):
self.checkInterruptedRecv(self.serv.recv, 1024)
@@ -3684,12 +3687,10 @@ class InterruptedSendTimeoutTest(InterruptedTimeoutBase,
# Check that func(*args, **kwargs), run in a loop, raises
# OSError with an errno of EINTR when interrupted by a
# signal.
- with self.assertRaises(OSError) as cm:
+ with self.assertRaises(ZeroDivisionError) as cm:
while True:
self.setAlarm(self.alarm_time)
func(*args, **kwargs)
- self.assertNotIsInstance(cm.exception, socket.timeout)
- self.assertEqual(cm.exception.errno, errno.EINTR)
# Issue #12958: The following tests have problems on OS X prior to 10.7
@support.requires_mac_ver(10, 7)
@@ -3731,8 +3732,6 @@ class TCPCloserTest(ThreadedTCPSocketTest):
self.cli.connect((HOST, self.port))
time.sleep(1.0)
-@unittest.skipUnless(hasattr(socket, 'socketpair'),
- 'test needs socket.socketpair()')
@unittest.skipUnless(thread, 'Threading required for this test.')
class BasicSocketPairTest(SocketPairTest):
@@ -3813,7 +3812,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM |
socket.SOCK_NONBLOCK)
self.port = support.bind_port(self.serv)
- self.serv.listen(1)
+ self.serv.listen()
# actual testing
start = time.time()
try:
@@ -4059,117 +4058,6 @@ class FileObjectClassTestCase(SocketConnectedTest):
pass
-class FileObjectInterruptedTestCase(unittest.TestCase):
- """Test that the file object correctly handles EINTR internally."""
-
- class MockSocket(object):
- def __init__(self, recv_funcs=()):
- # A generator that returns callables that we'll call for each
- # call to recv().
- self._recv_step = iter(recv_funcs)
-
- def recv_into(self, buffer):
- data = next(self._recv_step)()
- assert len(buffer) >= len(data)
- buffer[:len(data)] = data
- return len(data)
-
- def _decref_socketios(self):
- pass
-
- def _textiowrap_for_test(self, buffering=-1):
- raw = socket.SocketIO(self, "r")
- if buffering < 0:
- buffering = io.DEFAULT_BUFFER_SIZE
- if buffering == 0:
- return raw
- buffer = io.BufferedReader(raw, buffering)
- text = io.TextIOWrapper(buffer, None, None)
- text.mode = "rb"
- return text
-
- @staticmethod
- def _raise_eintr():
- raise OSError(errno.EINTR, "interrupted")
-
- def _textiowrap_mock_socket(self, mock, buffering=-1):
- raw = socket.SocketIO(mock, "r")
- if buffering < 0:
- buffering = io.DEFAULT_BUFFER_SIZE
- if buffering == 0:
- return raw
- buffer = io.BufferedReader(raw, buffering)
- text = io.TextIOWrapper(buffer, None, None)
- text.mode = "rb"
- return text
-
- def _test_readline(self, size=-1, buffering=-1):
- mock_sock = self.MockSocket(recv_funcs=[
- lambda : b"This is the first line\nAnd the sec",
- self._raise_eintr,
- lambda : b"ond line is here\n",
- lambda : b"",
- lambda : b"", # XXX(gps): io library does an extra EOF read
- ])
- fo = mock_sock._textiowrap_for_test(buffering=buffering)
- self.assertEqual(fo.readline(size), "This is the first line\n")
- self.assertEqual(fo.readline(size), "And the second line is here\n")
-
- def _test_read(self, size=-1, buffering=-1):
- mock_sock = self.MockSocket(recv_funcs=[
- lambda : b"This is the first line\nAnd the sec",
- self._raise_eintr,
- lambda : b"ond line is here\n",
- lambda : b"",
- lambda : b"", # XXX(gps): io library does an extra EOF read
- ])
- expecting = (b"This is the first line\n"
- b"And the second line is here\n")
- fo = mock_sock._textiowrap_for_test(buffering=buffering)
- if buffering == 0:
- data = b''
- else:
- data = ''
- expecting = expecting.decode('utf-8')
- while len(data) != len(expecting):
- part = fo.read(size)
- if not part:
- break
- data += part
- self.assertEqual(data, expecting)
-
- def test_default(self):
- self._test_readline()
- self._test_readline(size=100)
- self._test_read()
- self._test_read(size=100)
-
- def test_with_1k_buffer(self):
- self._test_readline(buffering=1024)
- self._test_readline(size=100, buffering=1024)
- self._test_read(buffering=1024)
- self._test_read(size=100, buffering=1024)
-
- def _test_readline_no_buffer(self, size=-1):
- mock_sock = self.MockSocket(recv_funcs=[
- lambda : b"a",
- lambda : b"\n",
- lambda : b"B",
- self._raise_eintr,
- lambda : b"b",
- lambda : b"",
- ])
- fo = mock_sock._textiowrap_for_test(buffering=0)
- self.assertEqual(fo.readline(size), b"a\n")
- self.assertEqual(fo.readline(size), b"Bb")
-
- def test_no_buffer(self):
- self._test_readline_no_buffer()
- self._test_readline_no_buffer(size=4)
- self._test_read(buffering=0)
- self._test_read(size=100, buffering=0)
-
-
class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase):
"""Repeat the tests from FileObjectClassTestCase with bufsize==0.
@@ -4588,7 +4476,7 @@ class TestLinuxAbstractNamespace(unittest.TestCase):
address = b"\x00python-test-hello\x00\xff"
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s1:
s1.bind(address)
- s1.listen(1)
+ s1.listen()
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s2:
s2.connect(s1.getsockname())
with s1.accept()[0] as s3:
@@ -4820,7 +4708,7 @@ class TIPCThreadableTest(unittest.TestCase, ThreadableTest):
srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
TIPC_LOWER, TIPC_UPPER)
self.srv.bind(srvaddr)
- self.srv.listen(5)
+ self.srv.listen()
self.serverExplicitReady()
self.conn, self.connaddr = self.srv.accept()
self.addCleanup(self.conn.close)
@@ -5109,6 +4997,275 @@ class TestSocketSharing(SocketTCPTest):
source.close()
+@unittest.skipUnless(thread, 'Threading required for this test.')
+class SendfileUsingSendTest(ThreadedTCPSocketTest):
+ """
+ Test the send() implementation of socket.sendfile().
+ """
+
+ FILESIZE = (10 * 1024 * 1024) # 10MB
+ BUFSIZE = 8192
+ FILEDATA = b""
+ TIMEOUT = 2
+
+ @classmethod
+ def setUpClass(cls):
+ def chunks(total, step):
+ assert total >= step
+ while total > step:
+ yield step
+ total -= step
+ if total:
+ yield total
+
+ chunk = b"".join([random.choice(string.ascii_letters).encode()
+ for i in range(cls.BUFSIZE)])
+ with open(support.TESTFN, 'wb') as f:
+ for csize in chunks(cls.FILESIZE, cls.BUFSIZE):
+ f.write(chunk)
+ with open(support.TESTFN, 'rb') as f:
+ cls.FILEDATA = f.read()
+ assert len(cls.FILEDATA) == cls.FILESIZE
+
+ @classmethod
+ def tearDownClass(cls):
+ support.unlink(support.TESTFN)
+
+ def accept_conn(self):
+ self.serv.settimeout(self.TIMEOUT)
+ conn, addr = self.serv.accept()
+ conn.settimeout(self.TIMEOUT)
+ self.addCleanup(conn.close)
+ return conn
+
+ def recv_data(self, conn):
+ received = []
+ while True:
+ chunk = conn.recv(self.BUFSIZE)
+ if not chunk:
+ break
+ received.append(chunk)
+ return b''.join(received)
+
+ def meth_from_sock(self, sock):
+ # Depending on the mixin class being run return either send()
+ # or sendfile() method implementation.
+ return getattr(sock, "_sendfile_use_send")
+
+ # regular file
+
+ def _testRegularFile(self):
+ address = self.serv.getsockname()
+ file = open(support.TESTFN, 'rb')
+ with socket.create_connection(address) as sock, file as file:
+ meth = self.meth_from_sock(sock)
+ sent = meth(file)
+ self.assertEqual(sent, self.FILESIZE)
+ self.assertEqual(file.tell(), self.FILESIZE)
+
+ def testRegularFile(self):
+ conn = self.accept_conn()
+ data = self.recv_data(conn)
+ self.assertEqual(len(data), self.FILESIZE)
+ self.assertEqual(data, self.FILEDATA)
+
+ # non regular file
+
+ def _testNonRegularFile(self):
+ address = self.serv.getsockname()
+ file = io.BytesIO(self.FILEDATA)
+ with socket.create_connection(address) as sock, file as file:
+ sent = sock.sendfile(file)
+ self.assertEqual(sent, self.FILESIZE)
+ self.assertEqual(file.tell(), self.FILESIZE)
+ self.assertRaises(socket._GiveupOnSendfile,
+ sock._sendfile_use_sendfile, file)
+
+ def testNonRegularFile(self):
+ conn = self.accept_conn()
+ data = self.recv_data(conn)
+ self.assertEqual(len(data), self.FILESIZE)
+ self.assertEqual(data, self.FILEDATA)
+
+ # empty file
+
+ def _testEmptyFileSend(self):
+ address = self.serv.getsockname()
+ filename = support.TESTFN + "2"
+ with open(filename, 'wb'):
+ self.addCleanup(support.unlink, filename)
+ file = open(filename, 'rb')
+ with socket.create_connection(address) as sock, file as file:
+ meth = self.meth_from_sock(sock)
+ sent = meth(file)
+ self.assertEqual(sent, 0)
+ self.assertEqual(file.tell(), 0)
+
+ def testEmptyFileSend(self):
+ conn = self.accept_conn()
+ data = self.recv_data(conn)
+ self.assertEqual(data, b"")
+
+ # offset
+
+ def _testOffset(self):
+ address = self.serv.getsockname()
+ file = open(support.TESTFN, 'rb')
+ with socket.create_connection(address) as sock, file as file:
+ meth = self.meth_from_sock(sock)
+ sent = meth(file, offset=5000)
+ self.assertEqual(sent, self.FILESIZE - 5000)
+ self.assertEqual(file.tell(), self.FILESIZE)
+
+ def testOffset(self):
+ conn = self.accept_conn()
+ data = self.recv_data(conn)
+ self.assertEqual(len(data), self.FILESIZE - 5000)
+ self.assertEqual(data, self.FILEDATA[5000:])
+
+ # count
+
+ def _testCount(self):
+ address = self.serv.getsockname()
+ file = open(support.TESTFN, 'rb')
+ with socket.create_connection(address, timeout=2) as sock, file as file:
+ count = 5000007
+ meth = self.meth_from_sock(sock)
+ sent = meth(file, count=count)
+ self.assertEqual(sent, count)
+ self.assertEqual(file.tell(), count)
+
+ def testCount(self):
+ count = 5000007
+ conn = self.accept_conn()
+ data = self.recv_data(conn)
+ self.assertEqual(len(data), count)
+ self.assertEqual(data, self.FILEDATA[:count])
+
+ # count small
+
+ def _testCountSmall(self):
+ address = self.serv.getsockname()
+ file = open(support.TESTFN, 'rb')
+ with socket.create_connection(address, timeout=2) as sock, file as file:
+ count = 1
+ meth = self.meth_from_sock(sock)
+ sent = meth(file, count=count)
+ self.assertEqual(sent, count)
+ self.assertEqual(file.tell(), count)
+
+ def testCountSmall(self):
+ count = 1
+ conn = self.accept_conn()
+ data = self.recv_data(conn)
+ self.assertEqual(len(data), count)
+ self.assertEqual(data, self.FILEDATA[:count])
+
+ # count + offset
+
+ def _testCountWithOffset(self):
+ address = self.serv.getsockname()
+ file = open(support.TESTFN, 'rb')
+ with socket.create_connection(address, timeout=2) as sock, file as file:
+ count = 100007
+ meth = self.meth_from_sock(sock)
+ sent = meth(file, offset=2007, count=count)
+ self.assertEqual(sent, count)
+ self.assertEqual(file.tell(), count + 2007)
+
+ def testCountWithOffset(self):
+ count = 100007
+ conn = self.accept_conn()
+ data = self.recv_data(conn)
+ self.assertEqual(len(data), count)
+ self.assertEqual(data, self.FILEDATA[2007:count+2007])
+
+ # non blocking sockets are not supposed to work
+
+ def _testNonBlocking(self):
+ address = self.serv.getsockname()
+ file = open(support.TESTFN, 'rb')
+ with socket.create_connection(address) as sock, file as file:
+ sock.setblocking(False)
+ meth = self.meth_from_sock(sock)
+ self.assertRaises(ValueError, meth, file)
+ self.assertRaises(ValueError, sock.sendfile, file)
+
+ def testNonBlocking(self):
+ conn = self.accept_conn()
+ if conn.recv(8192):
+ self.fail('was not supposed to receive any data')
+
+ # timeout (non-triggered)
+
+ def _testWithTimeout(self):
+ address = self.serv.getsockname()
+ file = open(support.TESTFN, 'rb')
+ with socket.create_connection(address, timeout=2) as sock, file as file:
+ meth = self.meth_from_sock(sock)
+ sent = meth(file)
+ self.assertEqual(sent, self.FILESIZE)
+
+ def testWithTimeout(self):
+ conn = self.accept_conn()
+ data = self.recv_data(conn)
+ self.assertEqual(len(data), self.FILESIZE)
+ self.assertEqual(data, self.FILEDATA)
+
+ # timeout (triggered)
+
+ def _testWithTimeoutTriggeredSend(self):
+ address = self.serv.getsockname()
+ file = open(support.TESTFN, 'rb')
+ with socket.create_connection(address, timeout=0.01) as sock, \
+ file as file:
+ meth = self.meth_from_sock(sock)
+ self.assertRaises(socket.timeout, meth, file)
+
+ def testWithTimeoutTriggeredSend(self):
+ conn = self.accept_conn()
+ conn.recv(88192)
+
+ # errors
+
+ def _test_errors(self):
+ pass
+
+ def test_errors(self):
+ with open(support.TESTFN, 'rb') as file:
+ with socket.socket(type=socket.SOCK_DGRAM) as s:
+ meth = self.meth_from_sock(s)
+ self.assertRaisesRegex(
+ ValueError, "SOCK_STREAM", meth, file)
+ with open(support.TESTFN, 'rt') as file:
+ with socket.socket() as s:
+ meth = self.meth_from_sock(s)
+ self.assertRaisesRegex(
+ ValueError, "binary mode", meth, file)
+ with open(support.TESTFN, 'rb') as file:
+ with socket.socket() as s:
+ meth = self.meth_from_sock(s)
+ self.assertRaisesRegex(TypeError, "positive integer",
+ meth, file, count='2')
+ self.assertRaisesRegex(TypeError, "positive integer",
+ meth, file, count=0.1)
+ self.assertRaisesRegex(ValueError, "positive integer",
+ meth, file, count=0)
+ self.assertRaisesRegex(ValueError, "positive integer",
+ meth, file, count=-1)
+
+
+@unittest.skipUnless(thread, 'Threading required for this test.')
+@unittest.skipUnless(hasattr(os, "sendfile"),
+ 'os.sendfile() required for this test.')
+class SendfileUsingSendfileTest(SendfileUsingSendTest):
+ """
+ Test the sendfile() implementation of socket.sendfile().
+ """
+ def meth_from_sock(self, sock):
+ return getattr(sock, "_sendfile_use_sendfile")
+
+
def test_main():
tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest ]
@@ -5116,7 +5273,6 @@ def test_main():
tests.extend([
NonBlockingTCPTests,
FileObjectClassTestCase,
- FileObjectInterruptedTestCase,
UnbufferedFileObjectClassTestCase,
LineBufferedFileObjectClassTestCase,
SmallBufferedFileObjectClassTestCase,
@@ -5161,6 +5317,8 @@ def test_main():
InterruptedRecvTimeoutTest,
InterruptedSendTimeoutTest,
TestSocketSharing,
+ SendfileUsingSendTest,
+ SendfileUsingSendfileTest,
])
thread_info = support.threading_setup()
diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py
index 924b9c4..31ab3b6 100644
--- a/Lib/test/test_socketserver.py
+++ b/Lib/test/test_socketserver.py
@@ -221,38 +221,6 @@ class SocketServerTest(unittest.TestCase):
socketserver.DatagramRequestHandler,
self.dgram_examine)
- @contextlib.contextmanager
- def mocked_select_module(self):
- """Mocks the select.select() call to raise EINTR for first call"""
- old_select = select.select
-
- class MockSelect:
- def __init__(self):
- self.called = 0
-
- def __call__(self, *args):
- self.called += 1
- if self.called == 1:
- # raise the exception on first call
- raise OSError(errno.EINTR, os.strerror(errno.EINTR))
- else:
- # Return real select value for consecutive calls
- return old_select(*args)
-
- select.select = MockSelect()
- try:
- yield select.select
- finally:
- select.select = old_select
-
- def test_InterruptServerSelectCall(self):
- with self.mocked_select_module() as mock_select:
- pid = self.run_server(socketserver.TCPServer,
- socketserver.StreamRequestHandler,
- self.stream_examine)
- # Make sure select was called again:
- self.assertGreater(mock_select.called, 1)
-
# Alas, on Linux (at least) recvfrom() doesn't return a meaningful
# client address so this cannot work:
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index dc82475..ce427b4 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -86,6 +86,12 @@ def have_verify_flags():
# 0.9.8 or higher
return ssl.OPENSSL_VERSION_INFO >= (0, 9, 8, 0, 15)
+def utc_offset(): #NOTE: ignore issues like #1647654
+ # local time = utc time + utc offset
+ if time.daylight and time.localtime().tm_isdst > 0:
+ return -time.altzone # seconds
+ return -time.timezone
+
def asn1time(cert_time):
# Some versions of OpenSSL ignore seconds, see #18207
# 0.9.8.i
@@ -134,6 +140,14 @@ class BasicSocketTests(unittest.TestCase):
self.assertIn(ssl.HAS_SNI, {True, False})
self.assertIn(ssl.HAS_ECDH, {True, False})
+ def test_str_for_enums(self):
+ # Make sure that the PROTOCOL_* constants have enum-like string
+ # reprs.
+ proto = ssl.PROTOCOL_SSLv23
+ self.assertEqual(str(proto), '_SSLMethod.PROTOCOL_SSLv23')
+ ctx = ssl.SSLContext(proto)
+ self.assertIs(ctx.protocol, proto)
+
def test_random(self):
v = ssl.RAND_status()
if support.verbose:
@@ -298,10 +312,10 @@ class BasicSocketTests(unittest.TestCase):
# Version string as returned by {Open,Libre}SSL, the format might change
if "LibreSSL" in s:
self.assertTrue(s.startswith("LibreSSL {:d}.{:d}".format(major, minor)),
- (s, t))
+ (s, t, hex(n)))
else:
self.assertTrue(s.startswith("OpenSSL {:d}.{:d}.{:d}".format(major, minor, fix)),
- (s, t))
+ (s, t, hex(n)))
@support.cpython_only
def test_refcycle(self):
@@ -369,6 +383,8 @@ class BasicSocketTests(unittest.TestCase):
self.assertRaises(ssl.CertificateError,
ssl.match_hostname, cert, hostname)
+ # -- Hostname matching --
+
cert = {'subject': ((('commonName', 'example.com'),),)}
ok(cert, 'example.com')
ok(cert, 'ExAmple.cOm')
@@ -454,6 +470,28 @@ class BasicSocketTests(unittest.TestCase):
# Only commonName is considered
fail(cert, 'California')
+ # -- IPv4 matching --
+ cert = {'subject': ((('commonName', 'example.com'),),),
+ 'subjectAltName': (('DNS', 'example.com'),
+ ('IP Address', '10.11.12.13'),
+ ('IP Address', '14.15.16.17'))}
+ ok(cert, '10.11.12.13')
+ ok(cert, '14.15.16.17')
+ fail(cert, '14.15.16.18')
+ fail(cert, 'example.net')
+
+ # -- IPv6 matching --
+ cert = {'subject': ((('commonName', 'example.com'),),),
+ 'subjectAltName': (('DNS', 'example.com'),
+ ('IP Address', '2001:0:0:0:0:0:0:CAFE\n'),
+ ('IP Address', '2003:0:0:0:0:0:0:BABA\n'))}
+ ok(cert, '2001::cafe')
+ ok(cert, '2003::baba')
+ fail(cert, '2003::bebe')
+ fail(cert, 'example.net')
+
+ # -- Miscellaneous --
+
# Neither commonName nor subjectAltName
cert = {'notAfter': 'Dec 18 23:59:59 2011 GMT',
'subject': ((('countryName', 'US'),),
@@ -505,9 +543,14 @@ class BasicSocketTests(unittest.TestCase):
def test_unknown_channel_binding(self):
# should raise ValueError for unknown type
s = socket.socket(socket.AF_INET)
- with ssl.wrap_socket(s) as ss:
+ s.bind(('127.0.0.1', 0))
+ 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 self.assertRaises(ValueError):
ss.get_channel_binding("unknown-type")
+ s.close()
@unittest.skipUnless("tls-unique" in ssl.CHANNEL_BINDING_TYPES,
"'tls-unique' channel binding not available")
@@ -648,6 +691,71 @@ class BasicSocketTests(unittest.TestCase):
ctx.wrap_socket(s)
self.assertEqual(str(cx.exception), "only stream sockets are supported")
+ def cert_time_ok(self, timestring, timestamp):
+ self.assertEqual(ssl.cert_time_to_seconds(timestring), timestamp)
+
+ def cert_time_fail(self, timestring):
+ with self.assertRaises(ValueError):
+ ssl.cert_time_to_seconds(timestring)
+
+ @unittest.skipUnless(utc_offset(),
+ 'local time needs to be different from UTC')
+ def test_cert_time_to_seconds_timezone(self):
+ # Issue #19940: ssl.cert_time_to_seconds() returns wrong
+ # results if local timezone is not UTC
+ self.cert_time_ok("May 9 00:00:00 2007 GMT", 1178668800.0)
+ self.cert_time_ok("Jan 5 09:34:43 2018 GMT", 1515144883.0)
+
+ def test_cert_time_to_seconds(self):
+ timestring = "Jan 5 09:34:43 2018 GMT"
+ ts = 1515144883.0
+ self.cert_time_ok(timestring, ts)
+ # accept keyword parameter, assert its name
+ self.assertEqual(ssl.cert_time_to_seconds(cert_time=timestring), ts)
+ # accept both %e and %d (space or zero generated by strftime)
+ self.cert_time_ok("Jan 05 09:34:43 2018 GMT", ts)
+ # case-insensitive
+ self.cert_time_ok("JaN 5 09:34:43 2018 GmT", ts)
+ self.cert_time_fail("Jan 5 09:34 2018 GMT") # no seconds
+ self.cert_time_fail("Jan 5 09:34:43 2018") # no GMT
+ self.cert_time_fail("Jan 5 09:34:43 2018 UTC") # not GMT timezone
+ self.cert_time_fail("Jan 35 09:34:43 2018 GMT") # invalid day
+ self.cert_time_fail("Jon 5 09:34:43 2018 GMT") # invalid month
+ self.cert_time_fail("Jan 5 24:00:00 2018 GMT") # invalid hour
+ self.cert_time_fail("Jan 5 09:60:43 2018 GMT") # invalid minute
+
+ newyear_ts = 1230768000.0
+ # leap seconds
+ self.cert_time_ok("Dec 31 23:59:60 2008 GMT", newyear_ts)
+ # same timestamp
+ self.cert_time_ok("Jan 1 00:00:00 2009 GMT", newyear_ts)
+
+ self.cert_time_ok("Jan 5 09:34:59 2018 GMT", 1515144899)
+ # allow 60th second (even if it is not a leap second)
+ self.cert_time_ok("Jan 5 09:34:60 2018 GMT", 1515144900)
+ # allow 2nd leap second for compatibility with time.strptime()
+ self.cert_time_ok("Jan 5 09:34:61 2018 GMT", 1515144901)
+ self.cert_time_fail("Jan 5 09:34:62 2018 GMT") # invalid seconds
+
+ # no special treatement for the special value:
+ # 99991231235959Z (rfc 5280)
+ self.cert_time_ok("Dec 31 23:59:59 9999 GMT", 253402300799.0)
+
+ @support.run_with_locale('LC_ALL', '')
+ def test_cert_time_to_seconds_locale(self):
+ # `cert_time_to_seconds()` should be locale independent
+
+ def local_february_name():
+ return time.strftime('%b', (1, 2, 3, 4, 5, 6, 0, 0, 0))
+
+ if local_february_name().lower() == 'feb':
+ self.skipTest("locale-specific month name needs to be "
+ "different from C locale")
+
+ # locale-independent
+ 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")
+
class ContextTests(unittest.TestCase):
@@ -1156,7 +1264,7 @@ class SSLErrorTests(unittest.TestCase):
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
with socket.socket() as s:
s.bind(("127.0.0.1", 0))
- s.listen(5)
+ s.listen()
c = socket.socket()
c.connect(s.getsockname())
c.setblocking(False)
@@ -1169,6 +1277,69 @@ class SSLErrorTests(unittest.TestCase):
self.assertEqual(cm.exception.errno, ssl.SSL_ERROR_WANT_READ)
+class MemoryBIOTests(unittest.TestCase):
+
+ def test_read_write(self):
+ bio = ssl.MemoryBIO()
+ bio.write(b'foo')
+ self.assertEqual(bio.read(), b'foo')
+ self.assertEqual(bio.read(), b'')
+ bio.write(b'foo')
+ bio.write(b'bar')
+ self.assertEqual(bio.read(), b'foobar')
+ self.assertEqual(bio.read(), b'')
+ bio.write(b'baz')
+ self.assertEqual(bio.read(2), b'ba')
+ self.assertEqual(bio.read(1), b'z')
+ self.assertEqual(bio.read(1), b'')
+
+ def test_eof(self):
+ bio = ssl.MemoryBIO()
+ self.assertFalse(bio.eof)
+ self.assertEqual(bio.read(), b'')
+ self.assertFalse(bio.eof)
+ bio.write(b'foo')
+ self.assertFalse(bio.eof)
+ bio.write_eof()
+ self.assertFalse(bio.eof)
+ self.assertEqual(bio.read(2), b'fo')
+ self.assertFalse(bio.eof)
+ self.assertEqual(bio.read(1), b'o')
+ self.assertTrue(bio.eof)
+ self.assertEqual(bio.read(), b'')
+ self.assertTrue(bio.eof)
+
+ def test_pending(self):
+ bio = ssl.MemoryBIO()
+ self.assertEqual(bio.pending, 0)
+ bio.write(b'foo')
+ self.assertEqual(bio.pending, 3)
+ for i in range(3):
+ bio.read(1)
+ self.assertEqual(bio.pending, 3-i-1)
+ for i in range(3):
+ bio.write(b'x')
+ self.assertEqual(bio.pending, i+1)
+ bio.read()
+ self.assertEqual(bio.pending, 0)
+
+ def test_buffer_types(self):
+ bio = ssl.MemoryBIO()
+ bio.write(b'foo')
+ self.assertEqual(bio.read(), b'foo')
+ bio.write(bytearray(b'bar'))
+ self.assertEqual(bio.read(), b'bar')
+ bio.write(memoryview(b'baz'))
+ self.assertEqual(bio.read(), b'baz')
+
+ def test_error_types(self):
+ bio = ssl.MemoryBIO()
+ self.assertRaises(TypeError, bio.write, 'foo')
+ self.assertRaises(TypeError, bio.write, None)
+ self.assertRaises(TypeError, bio.write, True)
+ self.assertRaises(TypeError, bio.write, 1)
+
+
class NetworkedTests(unittest.TestCase):
def test_connect(self):
@@ -1396,14 +1567,12 @@ class NetworkedTests(unittest.TestCase):
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),
- ssl.PROTOCOL_SSLv23)
+ pem = ssl.get_server_certificate((host, port))
if not pem:
self.fail("No server certificate on %s:%s!" % (host, port))
try:
pem = ssl.get_server_certificate((host, port),
- ssl.PROTOCOL_SSLv23,
ca_certs=CERTFILE)
except ssl.SSLError as x:
#should fail
@@ -1413,7 +1582,6 @@ class NetworkedTests(unittest.TestCase):
self.fail("Got server certificate %s for %s:%s!" % (pem, host, port))
pem = ssl.get_server_certificate((host, port),
- ssl.PROTOCOL_SSLv23,
ca_certs=cert)
if not pem:
self.fail("No server certificate on %s:%s!" % (host, port))
@@ -1499,6 +1667,93 @@ class NetworkedTests(unittest.TestCase):
self.assertIs(ss.context, ctx2)
self.assertIs(ss._sslobj.context, ctx2)
+
+class NetworkedBIOTests(unittest.TestCase):
+
+ def ssl_io_loop(self, sock, incoming, outgoing, func, *args, **kwargs):
+ # A simple IO loop. Call func(*args) depending on the error we get
+ # (WANT_READ or WANT_WRITE) move data between the socket and the BIOs.
+ timeout = kwargs.get('timeout', 10)
+ count = 0
+ while True:
+ errno = None
+ count += 1
+ try:
+ ret = func(*args)
+ except ssl.SSLError as e:
+ # Note that we get a spurious -1/SSL_ERROR_SYSCALL for
+ # non-blocking IO. The SSL_shutdown manpage hints at this.
+ # It *should* be safe to just ignore SYS_ERROR_SYSCALL because
+ # with a Memory BIO there's no syscalls (for IO at least).
+ if e.errno not in (ssl.SSL_ERROR_WANT_READ,
+ ssl.SSL_ERROR_WANT_WRITE,
+ ssl.SSL_ERROR_SYSCALL):
+ raise
+ errno = e.errno
+ # Get any data from the outgoing BIO irrespective of any error, and
+ # send it to the socket.
+ buf = outgoing.read()
+ sock.sendall(buf)
+ # If there's no error, we're done. For WANT_READ, we need to get
+ # data from the socket and put it in the incoming BIO.
+ if errno is None:
+ break
+ elif errno == ssl.SSL_ERROR_WANT_READ:
+ buf = sock.recv(32768)
+ if buf:
+ incoming.write(buf)
+ else:
+ incoming.write_eof()
+ if support.verbose:
+ sys.stdout.write("Needed %d calls to complete %s().\n"
+ % (count, func.__name__))
+ return ret
+
+ def test_handshake(self):
+ with support.transient_internet("svn.python.org"):
+ sock = socket.socket(socket.AF_INET)
+ sock.connect(("svn.python.org", 443))
+ incoming = ssl.MemoryBIO()
+ outgoing = ssl.MemoryBIO()
+ ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ ctx.verify_mode = ssl.CERT_REQUIRED
+ ctx.load_verify_locations(SVN_PYTHON_ORG_ROOT_CERT)
+ ctx.check_hostname = True
+ sslobj = ctx.wrap_bio(incoming, outgoing, False, 'svn.python.org')
+ self.assertIs(sslobj._sslobj.owner, sslobj)
+ self.assertIsNone(sslobj.cipher())
+ self.assertIsNone(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.assertIsNone(sslobj.shared_ciphers())
+ self.assertTrue(sslobj.getpeercert())
+ if 'tls-unique' in ssl.CHANNEL_BINDING_TYPES:
+ self.assertTrue(sslobj.get_channel_binding('tls-unique'))
+ self.ssl_io_loop(sock, incoming, outgoing, sslobj.unwrap)
+ self.assertRaises(ssl.SSLError, sslobj.write, b'foo')
+ sock.close()
+
+ def test_read_write_data(self):
+ with support.transient_internet("svn.python.org"):
+ sock = socket.socket(socket.AF_INET)
+ sock.connect(("svn.python.org", 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()
+
+
try:
import threading
except ImportError:
@@ -1530,7 +1785,8 @@ else:
try:
self.sslconn = self.server.context.wrap_socket(
self.sock, server_side=True)
- self.server.selected_protocols.append(self.sslconn.selected_npn_protocol())
+ self.server.selected_npn_protocols.append(self.sslconn.selected_npn_protocol())
+ self.server.selected_alpn_protocols.append(self.sslconn.selected_alpn_protocol())
except (ssl.SSLError, ConnectionResetError) as e:
# We treat ConnectionResetError as though it were an
# SSLError - OpenSSL on Ubuntu abruptly closes the
@@ -1547,6 +1803,7 @@ else:
self.close()
return False
else:
+ self.server.shared_ciphers.append(self.sslconn.shared_ciphers())
if self.server.context.verify_mode == ssl.CERT_REQUIRED:
cert = self.sslconn.getpeercert()
if support.verbose and self.server.chatty:
@@ -1637,7 +1894,8 @@ else:
def __init__(self, certificate=None, ssl_version=None,
certreqs=None, cacerts=None,
chatty=True, connectionchatty=False, starttls_server=False,
- npn_protocols=None, ciphers=None, context=None):
+ npn_protocols=None, alpn_protocols=None,
+ ciphers=None, context=None):
if context:
self.context = context
else:
@@ -1652,6 +1910,8 @@ else:
self.context.load_cert_chain(certificate)
if npn_protocols:
self.context.set_npn_protocols(npn_protocols)
+ if alpn_protocols:
+ self.context.set_alpn_protocols(alpn_protocols)
if ciphers:
self.context.set_ciphers(ciphers)
self.chatty = chatty
@@ -1661,7 +1921,9 @@ else:
self.port = support.bind_port(self.sock)
self.flag = None
self.active = False
- self.selected_protocols = []
+ self.selected_npn_protocols = []
+ self.selected_alpn_protocols = []
+ self.shared_ciphers = []
self.conn_errors = []
threading.Thread.__init__(self)
self.daemon = True
@@ -1681,7 +1943,7 @@ else:
def run(self):
self.sock.settimeout(0.05)
- self.sock.listen(5)
+ self.sock.listen()
self.active = True
if self.flag:
# signal an event
@@ -1887,14 +2149,25 @@ else:
'compression': s.compression(),
'cipher': s.cipher(),
'peercert': s.getpeercert(),
- 'client_npn_protocol': s.selected_npn_protocol()
+ 'client_alpn_protocol': s.selected_alpn_protocol(),
+ 'client_npn_protocol': s.selected_npn_protocol(),
+ 'version': s.version(),
})
s.close()
- stats['server_npn_protocols'] = server.selected_protocols
+ stats['server_alpn_protocols'] = server.selected_alpn_protocols
+ stats['server_npn_protocols'] = server.selected_npn_protocols
+ stats['server_shared_ciphers'] = server.shared_ciphers
return stats
def try_protocol_combo(server_protocol, client_protocol, expect_success,
certsreqs=None, server_options=0, client_options=0):
+ """
+ Try to SSL-connect using *client_protocol* to *server_protocol*.
+ If *expect_success* is true, assert that the connection succeeds,
+ if it's false, assert that the connection fails.
+ Also, if *expect_success* is a string, assert that it is the protocol
+ version actually used by the connection.
+ """
if certsreqs is None:
certsreqs = ssl.CERT_NONE
certtype = {
@@ -1924,8 +2197,8 @@ else:
ctx.load_cert_chain(CERTFILE)
ctx.load_verify_locations(CERTFILE)
try:
- server_params_test(client_context, server_context,
- chatty=False, connectionchatty=False)
+ stats = server_params_test(client_context, server_context,
+ chatty=False, connectionchatty=False)
# Protocol mismatch can result in either an SSLError, or a
# "Connection reset by peer" error.
except ssl.SSLError:
@@ -1940,6 +2213,10 @@ else:
"Client protocol %s succeeded with server protocol %s!"
% (ssl.get_protocol_name(client_protocol),
ssl.get_protocol_name(server_protocol)))
+ elif (expect_success is not True
+ and expect_success != stats['version']):
+ raise AssertionError("version mismatch: expected %r, got %r"
+ % (expect_success, stats['version']))
class ThreadedTests(unittest.TestCase):
@@ -2106,7 +2383,7 @@ else:
# and sets Event `listener_gone` to let the main thread know
# the socket is gone.
def listener():
- s.listen(5)
+ s.listen()
listener_ready.set()
newsock, addr = s.accept()
newsock.close()
@@ -2171,19 +2448,19 @@ else:
" SSL2 client to SSL23 server test unexpectedly failed:\n %s\n"
% str(x))
if hasattr(ssl, 'PROTOCOL_SSLv3'):
- try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, True)
+ try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, 'SSLv3')
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv23, True)
- try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, True)
+ try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, 'TLSv1')
if hasattr(ssl, 'PROTOCOL_SSLv3'):
- try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, True, ssl.CERT_OPTIONAL)
+ try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, 'SSLv3', ssl.CERT_OPTIONAL)
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv23, True, ssl.CERT_OPTIONAL)
- try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, True, ssl.CERT_OPTIONAL)
+ try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, 'TLSv1', ssl.CERT_OPTIONAL)
if hasattr(ssl, 'PROTOCOL_SSLv3'):
- try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, True, ssl.CERT_REQUIRED)
+ try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, 'SSLv3', ssl.CERT_REQUIRED)
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv23, True, ssl.CERT_REQUIRED)
- try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, True, ssl.CERT_REQUIRED)
+ try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, 'TLSv1', ssl.CERT_REQUIRED)
# Server with specific SSL options
if hasattr(ssl, 'PROTOCOL_SSLv3'):
@@ -2203,9 +2480,9 @@ else:
"""Connecting to an SSLv3 server with various client options"""
if support.verbose:
sys.stdout.write("\n")
- try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True)
- try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True, ssl.CERT_OPTIONAL)
- try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True, ssl.CERT_REQUIRED)
+ try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, 'SSLv3')
+ try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, 'SSLv3', ssl.CERT_OPTIONAL)
+ try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, 'SSLv3', ssl.CERT_REQUIRED)
if hasattr(ssl, 'PROTOCOL_SSLv2'):
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv2, False)
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv23, False,
@@ -2213,7 +2490,7 @@ else:
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_TLSv1, False)
if no_sslv2_implies_sslv3_hello():
# No SSLv2 => client will use an SSLv3 hello on recent OpenSSLs
- try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv23, True,
+ try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv23, 'SSLv3',
client_options=ssl.OP_NO_SSLv2)
@skip_if_broken_ubuntu_ssl
@@ -2221,9 +2498,9 @@ else:
"""Connecting to a TLSv1 server with various client options"""
if support.verbose:
sys.stdout.write("\n")
- try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True)
- try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True, ssl.CERT_OPTIONAL)
- try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True, ssl.CERT_REQUIRED)
+ try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, 'TLSv1')
+ try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, 'TLSv1', ssl.CERT_OPTIONAL)
+ try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, 'TLSv1', ssl.CERT_REQUIRED)
if hasattr(ssl, 'PROTOCOL_SSLv2'):
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv2, False)
if hasattr(ssl, 'PROTOCOL_SSLv3'):
@@ -2239,7 +2516,7 @@ else:
Testing against older TLS versions."""
if support.verbose:
sys.stdout.write("\n")
- try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_TLSv1_1, True)
+ try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_TLSv1_1, 'TLSv1.1')
if hasattr(ssl, 'PROTOCOL_SSLv2'):
try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_SSLv2, False)
if hasattr(ssl, 'PROTOCOL_SSLv3'):
@@ -2247,7 +2524,7 @@ else:
try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_SSLv23, False,
client_options=ssl.OP_NO_TLSv1_1)
- try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1_1, True)
+ try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1_1, 'TLSv1.1')
try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_TLSv1, False)
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1_1, False)
@@ -2260,7 +2537,7 @@ else:
Testing against older TLS versions."""
if support.verbose:
sys.stdout.write("\n")
- try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1_2, True,
+ try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1_2, 'TLSv1.2',
server_options=ssl.OP_NO_SSLv3|ssl.OP_NO_SSLv2,
client_options=ssl.OP_NO_SSLv3|ssl.OP_NO_SSLv2,)
if hasattr(ssl, 'PROTOCOL_SSLv2'):
@@ -2270,7 +2547,7 @@ else:
try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_SSLv23, False,
client_options=ssl.OP_NO_TLSv1_2)
- try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1_2, True)
+ try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1_2, 'TLSv1.2')
try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1, False)
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1_2, False)
try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1_1, False)
@@ -2506,6 +2783,36 @@ else:
s.write(b"over\n")
s.close()
+ def test_nonblocking_send(self):
+ server = ThreadedEchoServer(CERTFILE,
+ certreqs=ssl.CERT_NONE,
+ ssl_version=ssl.PROTOCOL_TLSv1,
+ cacerts=CERTFILE,
+ chatty=True,
+ connectionchatty=False)
+ with server:
+ s = ssl.wrap_socket(socket.socket(),
+ server_side=False,
+ certfile=CERTFILE,
+ ca_certs=CERTFILE,
+ cert_reqs=ssl.CERT_NONE,
+ ssl_version=ssl.PROTOCOL_TLSv1)
+ s.connect((HOST, server.port))
+ s.setblocking(False)
+
+ # If we keep sending data, at some point the buffers
+ # will be full and the call will block
+ buf = bytearray(8192)
+ def fill_buffer():
+ while True:
+ s.send(buf)
+ self.assertRaises((ssl.SSLWantWriteError,
+ ssl.SSLWantReadError), fill_buffer)
+
+ # Now read all the output and discard it
+ s.setblocking(True)
+ s.close()
+
def test_handshake_timeout(self):
# Issue #5103: SSL handshake must respect the socket timeout
server = socket.socket(socket.AF_INET)
@@ -2515,7 +2822,7 @@ else:
finish = False
def serve():
- server.listen(5)
+ server.listen()
started.set()
conns = []
while not finish:
@@ -2572,7 +2879,7 @@ else:
peer = None
def serve():
nonlocal remote, peer
- server.listen(5)
+ server.listen()
# Block on the accept and wait on the connection to close.
evt.set()
remote, peer = server.accept()
@@ -2622,6 +2929,21 @@ else:
s.connect((HOST, server.port))
self.assertIn("no shared cipher", str(server.conn_errors[0]))
+ def test_version_basic(self):
+ """
+ Basic tests for SSLSocket.version().
+ More tests are done in the test_protocol_*() methods.
+ """
+ context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ with ThreadedEchoServer(CERTFILE,
+ ssl_version=ssl.PROTOCOL_TLSv1,
+ chatty=False) as server:
+ with context.wrap_socket(socket.socket()) as s:
+ self.assertIs(s.version(), None)
+ s.connect((HOST, server.port))
+ self.assertEqual(s.version(), "TLSv1")
+ self.assertIs(s.version(), None)
+
@unittest.skipUnless(ssl.HAS_ECDH, "test requires ECDH-enabled OpenSSL")
def test_default_ecdh_curve(self):
# Issue #21015: elliptic curve-based Diffie Hellman key exchange
@@ -2731,6 +3053,55 @@ else:
if "ADH" not in parts and "EDH" not in parts and "DHE" not in parts:
self.fail("Non-DH cipher: " + cipher[0])
+ def test_selected_alpn_protocol(self):
+ # selected_alpn_protocol() is None unless ALPN is used.
+ context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ context.load_cert_chain(CERTFILE)
+ stats = server_params_test(context, context,
+ chatty=True, connectionchatty=True)
+ self.assertIs(stats['client_alpn_protocol'], None)
+
+ @unittest.skipUnless(ssl.HAS_ALPN, "ALPN support required")
+ def test_selected_alpn_protocol_if_server_uses_alpn(self):
+ # selected_alpn_protocol() is None unless ALPN is used by the client.
+ client_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ client_context.load_verify_locations(CERTFILE)
+ server_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ server_context.load_cert_chain(CERTFILE)
+ server_context.set_alpn_protocols(['foo', 'bar'])
+ stats = server_params_test(client_context, server_context,
+ chatty=True, connectionchatty=True)
+ self.assertIs(stats['client_alpn_protocol'], None)
+
+ @unittest.skipUnless(ssl.HAS_ALPN, "ALPN support needed for this test")
+ def test_alpn_protocols(self):
+ server_protocols = ['foo', 'bar', 'milkshake']
+ protocol_tests = [
+ (['foo', 'bar'], 'foo'),
+ (['bar', 'foo'], 'foo'),
+ (['milkshake'], 'milkshake'),
+ (['http/3.0', 'http/4.0'], None)
+ ]
+ for client_protocols, expected in protocol_tests:
+ server_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ server_context.load_cert_chain(CERTFILE)
+ server_context.set_alpn_protocols(server_protocols)
+ client_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
+ client_context.load_cert_chain(CERTFILE)
+ client_context.set_alpn_protocols(client_protocols)
+ stats = server_params_test(client_context, server_context,
+ chatty=True, connectionchatty=True)
+
+ msg = "failed trying %s (s) and %s (c).\n" \
+ "was expecting %s, but got %%s from the %%s" \
+ % (str(server_protocols), str(client_protocols),
+ str(expected))
+ client_result = stats['client_alpn_protocol']
+ self.assertEqual(client_result, expected, msg % (client_result, "client"))
+ server_result = stats['server_alpn_protocols'][-1] \
+ if len(stats['server_alpn_protocols']) else 'nothing'
+ self.assertEqual(server_result, expected, msg % (server_result, "server"))
+
def test_selected_npn_protocol(self):
# selected_npn_protocol() is None unless NPN is used
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
@@ -2871,6 +3242,20 @@ else:
self.assertEqual(cm.exception.reason, 'TLSV1_ALERT_INTERNAL_ERROR')
self.assertIn("TypeError", stderr.getvalue())
+ def test_shared_ciphers(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)
+ client_context.set_ciphers("RC4")
+ server_context.set_ciphers("AES:RC4")
+ stats = server_params_test(client_context, server_context)
+ ciphers = stats['server_shared_ciphers'][0]
+ self.assertGreater(len(ciphers), 0)
+ for name, tls_version, bits in ciphers:
+ self.assertIn("RC4", name.split("-"))
+
def test_read_write_after_close_raises_valuerror(self):
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_REQUIRED
@@ -2886,6 +3271,23 @@ else:
self.assertRaises(ValueError, s.read, 1024)
self.assertRaises(ValueError, s.write, b'hello')
+ def test_sendfile(self):
+ TEST_DATA = b"x" * 512
+ with open(support.TESTFN, 'wb') as f:
+ f.write(TEST_DATA)
+ self.addCleanup(support.unlink, support.TESTFN)
+ context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+ context.verify_mode = ssl.CERT_REQUIRED
+ context.load_verify_locations(CERTFILE)
+ context.load_cert_chain(CERTFILE)
+ server = ThreadedEchoServer(context=context, chatty=False)
+ with server:
+ with context.wrap_socket(socket.socket()) as s:
+ s.connect((HOST, server.port))
+ with open(support.TESTFN, 'rb') as file:
+ s.sendfile(file)
+ self.assertEqual(s.recv(1024), TEST_DATA)
+
def test_main(verbose=False):
if support.verbose:
@@ -2919,10 +3321,11 @@ def test_main(verbose=False):
if not os.path.exists(filename):
raise support.TestFailed("Can't read certificate file %r" % filename)
- tests = [ContextTests, BasicSocketTests, SSLErrorTests]
+ tests = [ContextTests, BasicSocketTests, SSLErrorTests, MemoryBIOTests]
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 af6ced4..f1a5938 100644
--- a/Lib/test/test_stat.py
+++ b/Lib/test/test_stat.py
@@ -1,5 +1,6 @@
import unittest
import os
+import sys
from test.support import TESTFN, import_fresh_module
c_stat = import_fresh_module('stat', fresh=['_stat'])
@@ -52,6 +53,26 @@ class TestFilemode:
'S_IWOTH': 0o002,
'S_IXOTH': 0o001}
+ # defined by the Windows API documentation
+ file_attributes = {
+ 'FILE_ATTRIBUTE_ARCHIVE': 32,
+ 'FILE_ATTRIBUTE_COMPRESSED': 2048,
+ 'FILE_ATTRIBUTE_DEVICE': 64,
+ 'FILE_ATTRIBUTE_DIRECTORY': 16,
+ 'FILE_ATTRIBUTE_ENCRYPTED': 16384,
+ 'FILE_ATTRIBUTE_HIDDEN': 2,
+ 'FILE_ATTRIBUTE_INTEGRITY_STREAM': 32768,
+ 'FILE_ATTRIBUTE_NORMAL': 128,
+ 'FILE_ATTRIBUTE_NOT_CONTENT_INDEXED': 8192,
+ 'FILE_ATTRIBUTE_NO_SCRUB_DATA': 131072,
+ 'FILE_ATTRIBUTE_OFFLINE': 4096,
+ 'FILE_ATTRIBUTE_READONLY': 1,
+ 'FILE_ATTRIBUTE_REPARSE_POINT': 1024,
+ 'FILE_ATTRIBUTE_SPARSE_FILE': 512,
+ 'FILE_ATTRIBUTE_SYSTEM': 4,
+ 'FILE_ATTRIBUTE_TEMPORARY': 256,
+ 'FILE_ATTRIBUTE_VIRTUAL': 65536}
+
def setUp(self):
try:
os.remove(TESTFN)
@@ -185,6 +206,14 @@ class TestFilemode:
self.assertTrue(callable(func))
self.assertEqual(func(0), 0)
+ @unittest.skipUnless(sys.platform == "win32",
+ "FILE_ATTRIBUTE_* constants are Win32 specific")
+ def test_file_attribute_constants(self):
+ for key, value in sorted(self.file_attributes.items()):
+ self.assertTrue(hasattr(self.statmod, key), key)
+ modvalue = getattr(self.statmod, key)
+ self.assertEqual(value, modvalue, key)
+
class TestFilemodeCStat(TestFilemode, unittest.TestCase):
statmod = c_stat
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index 08af71f..460876f 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -2421,25 +2421,6 @@ class ProcessTestCaseNoPoll(ProcessTestCase):
ProcessTestCase.tearDown(self)
-class HelperFunctionTests(unittest.TestCase):
- @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows")
- def test_eintr_retry_call(self):
- record_calls = []
- def fake_os_func(*args):
- record_calls.append(args)
- if len(record_calls) == 2:
- raise OSError(errno.EINTR, "fake interrupted system call")
- return tuple(reversed(args))
-
- self.assertEqual((999, 256),
- subprocess._eintr_retry_call(fake_os_func, 256, 999))
- self.assertEqual([(256, 999)], record_calls)
- # This time there will be an EINTR so it will loop once.
- self.assertEqual((666,),
- subprocess._eintr_retry_call(fake_os_func, 666))
- self.assertEqual([(256, 999), (666,), (666,)], record_calls)
-
-
@unittest.skipUnless(mswindows, "Windows-specific tests")
class CommandsWithSpaces (BaseTestCase):
@@ -2528,7 +2509,6 @@ def test_main():
Win32ProcessTestCase,
CommandTests,
ProcessTestCaseNoPoll,
- HelperFunctionTests,
CommandsWithSpaces,
ContextManagerTests,
)
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 03ce9d1..a02d2f4 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -85,7 +85,7 @@ class TestSupport(unittest.TestCase):
def test_bind_port(self):
s = socket.socket()
support.bind_port(s)
- s.listen(1)
+ s.listen()
s.close()
# Tests for temp_dir()
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 2f66eb7..ed6b47d 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -636,6 +636,53 @@ class SysModuleTest(unittest.TestCase):
expected = None
self.check_fsencoding(fs_encoding, expected)
+ def c_locale_get_error_handler(self, isolated=False, encoding=None):
+ # Force the POSIX locale
+ env = os.environ.copy()
+ env["LC_ALL"] = "C"
+ code = '\n'.join((
+ 'import sys',
+ 'def dump(name):',
+ ' std = getattr(sys, name)',
+ ' print("%s: %s" % (name, std.errors))',
+ 'dump("stdin")',
+ 'dump("stdout")',
+ 'dump("stderr")',
+ ))
+ args = [sys.executable, "-c", code]
+ if isolated:
+ args.append("-I")
+ elif encoding:
+ env['PYTHONIOENCODING'] = encoding
+ p = subprocess.Popen(args,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ env=env,
+ universal_newlines=True)
+ stdout, stderr = p.communicate()
+ return stdout
+
+ def test_c_locale_surrogateescape(self):
+ out = self.c_locale_get_error_handler(isolated=True)
+ self.assertEqual(out,
+ 'stdin: surrogateescape\n'
+ 'stdout: surrogateescape\n'
+ 'stderr: backslashreplace\n')
+
+ # replace the default error handler
+ out = self.c_locale_get_error_handler(encoding=':strict')
+ self.assertEqual(out,
+ 'stdin: strict\n'
+ 'stdout: strict\n'
+ 'stderr: backslashreplace\n')
+
+ # force the encoding
+ out = self.c_locale_get_error_handler(encoding='iso8859-1')
+ self.assertEqual(out,
+ 'stdin: surrogateescape\n'
+ 'stdout: surrogateescape\n'
+ 'stderr: backslashreplace\n')
+
def test_implementation(self):
# This test applies to all implementations equally.
@@ -698,6 +745,27 @@ class SysModuleTest(unittest.TestCase):
c = sys.getallocatedblocks()
self.assertIn(c, range(b - 50, b + 50))
+ def test_is_finalizing(self):
+ self.assertIs(sys.is_finalizing(), False)
+ # Don't use the atexit module because _Py_Finalizing is only set
+ # after calling atexit callbacks
+ code = """if 1:
+ import sys
+
+ class AtExit:
+ is_finalizing = sys.is_finalizing
+ print = print
+
+ def __del__(self):
+ self.print(self.is_finalizing(), flush=True)
+
+ # Keep a reference in the __main__ module namespace, so the
+ # AtExit destructor will be called at Python exit
+ ref = AtExit()
+ """
+ rc, stdout, stderr = assert_python_ok('-c', code)
+ self.assertEqual(stdout.rstrip(), b'True')
+
@test.support.cpython_only
class SizeofTest(unittest.TestCase):
@@ -770,7 +838,7 @@ class SizeofTest(unittest.TestCase):
# buffer
# XXX
# builtin_function_or_method
- check(len, size('3P')) # XXX check layout
+ check(len, size('4P')) # XXX check layout
# bytearray
samples = [b'', b'u'*100000]
for sample in samples:
@@ -871,7 +939,7 @@ class SizeofTest(unittest.TestCase):
check(bar, size('PP'))
# generator
def get_gen(): yield 1
- check(get_gen(), size('Pb2P'))
+ check(get_gen(), size('Pb2PPP'))
# iterator
check(iter('abc'), size('lP'))
# callable-iterator
@@ -898,7 +966,7 @@ class SizeofTest(unittest.TestCase):
check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit)
check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit)
# memoryview
- check(memoryview(b''), size('Pnin 2P2n2i5P 3cPn'))
+ check(memoryview(b''), size('Pnin 2P2n2i5P Pn'))
# module
check(unittest, size('PnPPP'))
# None
@@ -927,7 +995,7 @@ class SizeofTest(unittest.TestCase):
# frozenset
PySet_MINSIZE = 8
samples = [[], range(10), range(50)]
- s = size('3n2P' + PySet_MINSIZE*'nP' + 'nP')
+ s = size('3nP' + PySet_MINSIZE*'nP' + '2nP')
for sample in samples:
minused = len(sample)
if minused == 0: tmp = 1
@@ -958,7 +1026,7 @@ class SizeofTest(unittest.TestCase):
check(int, s)
# (PyTypeObject + PyNumberMethods + PyMappingMethods +
# PySequenceMethods + PyBufferProcs + 4P)
- s = vsize('P2n15Pl4Pn9Pn11PIP') + struct.calcsize('34P 3P 10P 2P 4P')
+ s = vsize('P2n17Pl4Pn9Pn11PIP') + struct.calcsize('34P 3P 10P 2P 4P')
# Separate block for PyDictKeysObject with 4 entries
s += struct.calcsize("2nPn") + 4*struct.calcsize("n2P")
# class
diff --git a/Lib/test/test_sys_setprofile.py b/Lib/test/test_sys_setprofile.py
index 9816e3e..e59320b 100644
--- a/Lib/test/test_sys_setprofile.py
+++ b/Lib/test/test_sys_setprofile.py
@@ -260,7 +260,6 @@ class ProfileHookTestCase(TestCaseBase):
def f():
for i in range(2):
yield i
- raise StopIteration
def g(p):
for i in f():
pass
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index c135304..01d1a92 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -285,6 +285,18 @@ class ListTest(ReadTest, unittest.TestCase):
self.assertIn(b'pax' + (b'/123' * 125) + b'/longlink link to pax' +
(b'/123' * 125) + b'/longname', out)
+ def test_list_members(self):
+ tio = io.TextIOWrapper(io.BytesIO(), 'ascii', newline='\n')
+ def members(tar):
+ for tarinfo in tar.getmembers():
+ if 'reg' in tarinfo.name:
+ yield tarinfo
+ with support.swap_attr(sys, 'stdout', tio):
+ self.tar.list(verbose=False, members=members(self.tar))
+ out = tio.detach().getvalue()
+ self.assertIn(b'ustar/regtype', out)
+ self.assertNotIn(b'ustar/conttype', out)
+
class GzipListTest(GzipTest, ListTest):
pass
@@ -1416,6 +1428,88 @@ class GNUWriteTest(unittest.TestCase):
("longlnk/" * 127) + "longlink_")
+class CreateTest(TarTest, unittest.TestCase):
+
+ prefix = "x:"
+
+ file_path = os.path.join(TEMPDIR, "spameggs42")
+
+ def setUp(self):
+ support.unlink(tmpname)
+
+ @classmethod
+ def setUpClass(cls):
+ with open(cls.file_path, "wb") as fobj:
+ fobj.write(b"aaa")
+
+ @classmethod
+ def tearDownClass(cls):
+ support.unlink(cls.file_path)
+
+ def test_create(self):
+ with tarfile.open(tmpname, self.mode) as tobj:
+ tobj.add(self.file_path)
+
+ with self.taropen(tmpname) as tobj:
+ names = tobj.getnames()
+ self.assertEqual(len(names), 1)
+ self.assertIn('spameggs42', names[0])
+
+ def test_create_existing(self):
+ with tarfile.open(tmpname, self.mode) as tobj:
+ tobj.add(self.file_path)
+
+ with self.assertRaises(FileExistsError):
+ tobj = tarfile.open(tmpname, self.mode)
+
+ with self.taropen(tmpname) as tobj:
+ names = tobj.getnames()
+ self.assertEqual(len(names), 1)
+ self.assertIn('spameggs42', names[0])
+
+ def test_create_taropen(self):
+ with self.taropen(tmpname, "x") as tobj:
+ tobj.add(self.file_path)
+
+ with self.taropen(tmpname) as tobj:
+ names = tobj.getnames()
+ self.assertEqual(len(names), 1)
+ self.assertIn('spameggs42', names[0])
+
+ def test_create_existing_taropen(self):
+ with self.taropen(tmpname, "x") as tobj:
+ tobj.add(self.file_path)
+
+ with self.assertRaises(FileExistsError):
+ with self.taropen(tmpname, "x"):
+ pass
+
+ with self.taropen(tmpname) as tobj:
+ names = tobj.getnames()
+ self.assertEqual(len(names), 1)
+ self.assertIn("spameggs42", names[0])
+
+
+class GzipCreateTest(GzipTest, CreateTest):
+ pass
+
+
+class Bz2CreateTest(Bz2Test, CreateTest):
+ pass
+
+
+class LzmaCreateTest(LzmaTest, CreateTest):
+ pass
+
+
+class CreateWithXModeTest(CreateTest):
+
+ prefix = "x"
+
+ test_create_taropen = None
+ test_create_existing_taropen = None
+
+
@unittest.skipUnless(hasattr(os, "link"), "Missing hardlink implementation")
class HardlinkTest(unittest.TestCase):
# Test the creation of LNKTYPE (hardlink) members in an archive.
diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py
index da8f629..d0251aa 100644
--- a/Lib/test/test_tcl.py
+++ b/Lib/test/test_tcl.py
@@ -415,6 +415,8 @@ class TclTest(unittest.TestCase):
# XXX NaN representation can be not parsable by float()
self.assertEqual(passValue((1, '2', (3.4,))),
(1, '2', (3.4,)) if self.wantobjects else '1 2 3.4')
+ self.assertEqual(passValue(['a', ['b', 'c']]),
+ ('a', ('b', 'c')) if self.wantobjects else 'a {b c}')
def test_user_command(self):
result = None
@@ -466,6 +468,7 @@ class TclTest(unittest.TestCase):
# XXX NaN representation can be not parsable by float()
check((), '')
check((1, (2,), (3, 4), '5 6', ()), '1 2 {3 4} {5 6} {}')
+ check([1, [2,], [3, 4], '5 6', []], '1 2 {3 4} {5 6} {}')
def test_splitlist(self):
splitlist = self.interp.tk.splitlist
@@ -491,12 +494,15 @@ class TclTest(unittest.TestCase):
('a 3.4', ('a', '3.4')),
(('a', 3.4), ('a', 3.4)),
((), ()),
+ ([], ()),
+ (['a', ['b', 'c']], ('a', ['b', 'c'])),
(call('list', 1, '2', (3.4,)),
(1, '2', (3.4,)) if self.wantobjects else
('1', '2', '3.4')),
]
+ tk_patchlevel = get_tk_patchlevel()
if tcl_version >= (8, 5):
- if not self.wantobjects or get_tk_patchlevel() < (8, 5, 5):
+ if not self.wantobjects or tk_patchlevel < (8, 5, 5):
# Before 8.5.5 dicts were converted to lists through string
expected = ('12', '\u20ac', '\xe2\x82\xac', '3.4')
else:
@@ -505,8 +511,11 @@ class TclTest(unittest.TestCase):
(call('dict', 'create', 12, '\u20ac', b'\xe2\x82\xac', (3.4,)),
expected),
]
+ dbg_info = ('want objects? %s, Tcl version: %s, Tk patchlevel: %s'
+ % (self.wantobjects, tcl_version, tk_patchlevel))
for arg, res in testcases:
- self.assertEqual(splitlist(arg), res, msg=arg)
+ self.assertEqual(splitlist(arg), res,
+ 'arg=%a, %s' % (arg, dbg_info))
self.assertRaises(TclError, splitlist, '{')
def test_split(self):
@@ -538,6 +547,9 @@ class TclTest(unittest.TestCase):
(('a', 3.4), ('a', 3.4)),
(('a', (2, 3.4)), ('a', (2, 3.4))),
((), ()),
+ ([], ()),
+ (['a', 'b c'], ('a', ('b', 'c'))),
+ (['a', ['b', 'c']], ('a', ('b', 'c'))),
(call('list', 1, '2', (3.4,)),
(1, '2', (3.4,)) if self.wantobjects else
('1', '2', '3.4')),
diff --git a/Lib/test/test_telnetlib.py b/Lib/test/test_telnetlib.py
index ee1c357..e1ef99a 100644
--- a/Lib/test/test_telnetlib.py
+++ b/Lib/test/test_telnetlib.py
@@ -11,7 +11,7 @@ threading = support.import_module('threading')
HOST = support.HOST
def server(evt, serv):
- serv.listen(5)
+ serv.listen()
evt.set()
try:
conn, addr = serv.accept()
diff --git a/Lib/test/test_threaded_import.py b/Lib/test/test_threaded_import.py
index 192fa08..5bf670c 100644
--- a/Lib/test/test_threaded_import.py
+++ b/Lib/test/test_threaded_import.py
@@ -115,12 +115,18 @@ class ThreadedImportTests(unittest.TestCase):
errors = []
done_tasks = []
done.clear()
+ t0 = time.monotonic()
for i in range(N):
t = threading.Thread(target=task,
args=(N, done, done_tasks, errors,))
t.start()
- self.assertTrue(done.wait(60))
- self.assertFalse(errors)
+ completed = done.wait(10 * 60)
+ dt = time.monotonic() - t0
+ if verbose:
+ print("%.1f ms" % (dt*1e3), flush=True, end=" ")
+ dbg_info = 'done: %s/%s' % (len(done_tasks), N)
+ self.assertFalse(errors, dbg_info)
+ self.assertTrue(completed, dbg_info)
if verbose:
print("OK.")
diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py
index 09e76e0..267d2c8 100644
--- a/Lib/test/test_timeit.py
+++ b/Lib/test/test_timeit.py
@@ -98,9 +98,10 @@ class TestTimeit(unittest.TestCase):
def fake_callable_stmt(self):
self.fake_timer.inc()
- def timeit(self, stmt, setup, number=None):
+ def timeit(self, stmt, setup, number=None, globals=None):
self.fake_timer = FakeTimer()
- t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)
+ t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer,
+ globals=globals)
kwargs = {}
if number is None:
number = DEFAULT_NUMBER
@@ -139,6 +140,17 @@ class TestTimeit(unittest.TestCase):
timer=FakeTimer())
self.assertEqual(delta_time, 0)
+ def test_timeit_globals_args(self):
+ global _global_timer
+ _global_timer = FakeTimer()
+ t = timeit.Timer(stmt='_global_timer.inc()', timer=_global_timer)
+ self.assertRaises(NameError, t.timeit, number=3)
+ timeit.timeit(stmt='_global_timer.inc()', timer=_global_timer,
+ globals=globals(), number=3)
+ local_timer = FakeTimer()
+ timeit.timeit(stmt='local_timer.inc()', timer=local_timer,
+ globals=locals(), number=3)
+
def repeat(self, stmt, setup, repeat=None, number=None):
self.fake_timer = FakeTimer()
t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)
diff --git a/Lib/test/test_timeout.py b/Lib/test/test_timeout.py
index 703c43a..3c75dcc 100644
--- a/Lib/test/test_timeout.py
+++ b/Lib/test/test_timeout.py
@@ -243,14 +243,14 @@ class TCPTimeoutTestCase(TimeoutTestCase):
def testAcceptTimeout(self):
# Test accept() timeout
support.bind_port(self.sock, self.localhost)
- self.sock.listen(5)
+ self.sock.listen()
self._sock_operation(1, 1.5, 'accept')
def testSend(self):
# Test send() timeout
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv:
support.bind_port(serv, self.localhost)
- serv.listen(5)
+ serv.listen()
self.sock.connect(serv.getsockname())
# Send a lot of data in order to bypass buffering in the TCP stack.
self._sock_operation(100, 1.5, 'send', b"X" * 200000)
@@ -259,7 +259,7 @@ class TCPTimeoutTestCase(TimeoutTestCase):
# Test sendto() timeout
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv:
support.bind_port(serv, self.localhost)
- serv.listen(5)
+ serv.listen()
self.sock.connect(serv.getsockname())
# The address argument is ignored since we already connected.
self._sock_operation(100, 1.5, 'sendto', b"X" * 200000,
@@ -269,7 +269,7 @@ class TCPTimeoutTestCase(TimeoutTestCase):
# Test sendall() timeout
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv:
support.bind_port(serv, self.localhost)
- serv.listen(5)
+ serv.listen()
self.sock.connect(serv.getsockname())
# Send a lot of data in order to bypass buffering in the TCP stack.
self._sock_operation(100, 1.5, 'sendall', b"X" * 200000)
diff --git a/Lib/test/test_tokenize.py b/Lib/test/test_tokenize.py
index 38611a7..8f74a06 100644
--- a/Lib/test/test_tokenize.py
+++ b/Lib/test/test_tokenize.py
@@ -464,7 +464,7 @@ Additive
Multiplicative
- >>> dump_tokens("x = 1//1*1/5*12%0x12")
+ >>> dump_tokens("x = 1//1*1/5*12%0x12@42")
ENCODING 'utf-8' (0, 0) (0, 0)
NAME 'x' (1, 0) (1, 1)
OP '=' (1, 2) (1, 3)
@@ -479,6 +479,8 @@ Multiplicative
NUMBER '12' (1, 13) (1, 15)
OP '%' (1, 15) (1, 16)
NUMBER '0x12' (1, 16) (1, 20)
+ OP '@' (1, 20) (1, 21)
+ NUMBER '42' (1, 21) (1, 23)
Unary
@@ -1154,6 +1156,7 @@ class TestTokenize(TestCase):
self.assertExactTypeEqual('//', token.DOUBLESLASH)
self.assertExactTypeEqual('//=', token.DOUBLESLASHEQUAL)
self.assertExactTypeEqual('@', token.AT)
+ self.assertExactTypeEqual('@=', token.ATEQUAL)
self.assertExactTypeEqual('a**2+b**2==c**2',
NAME, token.DOUBLESTAR, NUMBER,
diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py
index 1cec710..05bf274 100644
--- a/Lib/test/test_trace.py
+++ b/Lib/test/test_trace.py
@@ -10,7 +10,6 @@ from trace import CoverageResults, Trace
from test.tracedmodules import testmod
-
#------------------------------- Utilities -----------------------------------#
def fix_ext_py(filename):
@@ -224,6 +223,11 @@ class TestFuncs(unittest.TestCase):
self.addCleanup(sys.settrace, sys.gettrace())
self.tracer = Trace(count=0, trace=0, countfuncs=1)
self.filemod = my_file_and_modname()
+ self._saved_tracefunc = sys.gettrace()
+
+ def tearDown(self):
+ if self._saved_tracefunc is not None:
+ sys.settrace(self._saved_tracefunc)
def test_simple_caller(self):
self.tracer.runfunc(traced_func_simple_caller, 1)
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index c295563..6bd6fa6 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -6,6 +6,8 @@ import unittest
import re
from test.support import run_unittest, Error, captured_output
from test.support import TESTFN, unlink, cpython_only
+from test.script_helper import assert_python_ok
+import textwrap
import traceback
@@ -92,9 +94,9 @@ class SyntaxTracebackCases(unittest.TestCase):
self.assertEqual(len(err), 1)
str_value = '<unprintable %s object>' % X.__name__
if X.__module__ in ('__main__', 'builtins'):
- str_name = X.__name__
+ str_name = X.__qualname__
else:
- str_name = '.'.join([X.__module__, X.__name__])
+ str_name = '.'.join([X.__module__, X.__qualname__])
self.assertEqual(err[0], "%s: %s\n" % (str_name, str_value))
def test_without_exception(self):
@@ -169,6 +171,37 @@ class SyntaxTracebackCases(unittest.TestCase):
# Issue #18960: coding spec should has no effect
do_test("0\n# coding: GBK\n", "h\xe9 ho", 'utf-8', 5)
+ def test_print_traceback_at_exit(self):
+ # Issue #22599: Ensure that it is possible to use the traceback module
+ # to display an exception at Python exit
+ code = textwrap.dedent("""
+ import sys
+ import traceback
+
+ class PrintExceptionAtExit(object):
+ def __init__(self):
+ try:
+ x = 1 / 0
+ except Exception:
+ self.exc_info = sys.exc_info()
+ # self.exc_info[1] (traceback) contains frames:
+ # explicitly clear the reference to self in the current
+ # frame to break a reference cycle
+ self = None
+
+ def __del__(self):
+ traceback.print_exception(*self.exc_info)
+
+ # Keep a reference in the module namespace to call the destructor
+ # when the module is unloaded
+ obj = PrintExceptionAtExit()
+ """)
+ rc, stdout, stderr = assert_python_ok('-c', code)
+ expected = [b'Traceback (most recent call last):',
+ b' File "<string>", line 8, in __init__',
+ b'ZeroDivisionError: division by zero']
+ self.assertEqual(stderr.splitlines(), expected)
+
class TracebackFormatTests(unittest.TestCase):
diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py
index 48ccab2..19d3fd8 100644
--- a/Lib/test/test_tracemalloc.py
+++ b/Lib/test/test_tracemalloc.py
@@ -755,7 +755,7 @@ class TestCommandLine(unittest.TestCase):
stdout = stdout.rstrip()
self.assertEqual(stdout, b'False')
- @unittest.skipIf(script_helper._interpreter_requires_environment(),
+ @unittest.skipIf(script_helper.interpreter_requires_environment(),
'Cannot run -E tests when PYTHON env vars are required.')
def test_env_var_ignored_with_E(self):
"""PYTHON* environment variables must be ignored when -E is present."""
diff --git a/Lib/test/test_tuple.py b/Lib/test/test_tuple.py
index 51875a1..d711116 100644
--- a/Lib/test/test_tuple.py
+++ b/Lib/test/test_tuple.py
@@ -6,6 +6,11 @@ import pickle
class TupleTest(seq_tests.CommonTest):
type2test = tuple
+ def test_getitem_error(self):
+ msg = "tuple indices must be integers or slices"
+ with self.assertRaisesRegex(TypeError, msg):
+ ()['a']
+
def test_constructors(self):
super().test_constructors()
# calling built-in types without argument must return empty
@@ -203,6 +208,14 @@ class TupleTest(seq_tests.CommonTest):
with self.assertRaises(TypeError):
[3,] + T((1,2))
+ def test_lexicographic_ordering(self):
+ # Issue 21100
+ a = self.type2test([1, 2])
+ b = self.type2test([1, 2, 0])
+ c = self.type2test([1, 3])
+ self.assertLess(a, b)
+ self.assertLess(b, c)
+
def test_main():
support.run_unittest(TupleTest)
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index ec10752..11d9546 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -343,6 +343,8 @@ class TypesTests(unittest.TestCase):
self.assertRaises(ValueError, 3 .__format__, ",n")
# can't have ',' with 'c'
self.assertRaises(ValueError, 3 .__format__, ",c")
+ # can't have '#' with 'c'
+ self.assertRaises(ValueError, 3 .__format__, "#c")
# ensure that only int and float type specifiers work
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 7735a6b..e4cd99b 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -8,6 +8,7 @@ Written by Marc-Andre Lemburg (mal@lemburg.com).
import _string
import codecs
import itertools
+import operator
import struct
import sys
import unittest
@@ -250,6 +251,7 @@ class UnicodeTest(string_tests.CommonTest,
{ord('a'): None, ord('b'): ''})
self.checkequalnofix('xyyx', 'xzx', 'translate',
{ord('z'): 'yy'})
+
# this needs maketrans()
self.checkequalnofix('abababc', 'abababc', 'translate',
{'b': '<i>'})
@@ -259,6 +261,33 @@ class UnicodeTest(string_tests.CommonTest,
tbl = self.type2test.maketrans('abc', 'xyz', 'd')
self.checkequalnofix('xyzzy', 'abdcdcbdddd', 'translate', tbl)
+ # various tests switching from ASCII to latin1 or the opposite;
+ # same length, remove a letter, or replace with a longer string.
+ self.assertEqual("[a]".translate(str.maketrans('a', 'X')),
+ "[X]")
+ self.assertEqual("[a]".translate(str.maketrans({'a': 'X'})),
+ "[X]")
+ self.assertEqual("[a]".translate(str.maketrans({'a': None})),
+ "[]")
+ self.assertEqual("[a]".translate(str.maketrans({'a': 'XXX'})),
+ "[XXX]")
+ self.assertEqual("[a]".translate(str.maketrans({'a': '\xe9'})),
+ "[\xe9]")
+ self.assertEqual("[a]".translate(str.maketrans({'a': '<\xe9>'})),
+ "[<\xe9>]")
+ self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': 'a'})),
+ "[a]")
+ self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': None})),
+ "[]")
+
+ # invalid Unicode characters
+ invalid_char = 0x10ffff+1
+ for before in "a\xe9\u20ac\U0010ffff":
+ mapping = str.maketrans({before: invalid_char})
+ text = "[%s]" % before
+ self.assertRaises(ValueError, text.translate, mapping)
+
+ # errors
self.assertRaises(TypeError, self.type2test.maketrans)
self.assertRaises(ValueError, self.type2test.maketrans, 'abc', 'defg')
self.assertRaises(TypeError, self.type2test.maketrans, 2, 'def')
@@ -1154,20 +1183,20 @@ class UnicodeTest(string_tests.CommonTest,
self.assertEqual('%.2s' % "a\xe9\u20ac", 'a\xe9')
#issue 19995
- class PsuedoInt:
+ class PseudoInt:
def __init__(self, value):
self.value = int(value)
def __int__(self):
return self.value
def __index__(self):
return self.value
- class PsuedoFloat:
+ class PseudoFloat:
def __init__(self, value):
self.value = float(value)
def __int__(self):
return int(self.value)
- pi = PsuedoFloat(3.1415)
- letter_m = PsuedoInt(109)
+ pi = PseudoFloat(3.1415)
+ letter_m = PseudoInt(109)
self.assertEqual('%x' % 42, '2a')
self.assertEqual('%X' % 15, 'F')
self.assertEqual('%o' % 9, '11')
@@ -1176,11 +1205,11 @@ class UnicodeTest(string_tests.CommonTest,
self.assertEqual('%X' % letter_m, '6D')
self.assertEqual('%o' % letter_m, '155')
self.assertEqual('%c' % letter_m, 'm')
- self.assertWarns(DeprecationWarning, '%x'.__mod__, pi),
- self.assertWarns(DeprecationWarning, '%x'.__mod__, 3.14),
- self.assertWarns(DeprecationWarning, '%X'.__mod__, 2.11),
- self.assertWarns(DeprecationWarning, '%o'.__mod__, 1.79),
- self.assertWarns(DeprecationWarning, '%c'.__mod__, pi),
+ self.assertRaisesRegex(TypeError, '%x format: an integer is required, not float', operator.mod, '%x', 3.14),
+ self.assertRaisesRegex(TypeError, '%X format: an integer is required, not float', operator.mod, '%X', 2.11),
+ self.assertRaisesRegex(TypeError, '%o format: an integer is required, not float', operator.mod, '%o', 1.79),
+ self.assertRaisesRegex(TypeError, '%x format: an integer is required, not PseudoFloat', operator.mod, '%x', pi),
+ self.assertRaises(TypeError, operator.mod, '%c', pi),
def test_formatting_with_enum(self):
# issue18780
diff --git a/Lib/test/test_unicodedata.py b/Lib/test/test_unicodedata.py
index 707b30e..f8788a0 100644
--- a/Lib/test/test_unicodedata.py
+++ b/Lib/test/test_unicodedata.py
@@ -21,7 +21,7 @@ errors = 'surrogatepass'
class UnicodeMethodsTest(unittest.TestCase):
# update this, if the database changes
- expectedchecksum = 'e74e878de71b6e780ffac271785c3cb58f6251f3'
+ expectedchecksum = '618e2c1a22ee79d2235319709f16c50f987ee21f'
def test_method_checksum(self):
h = hashlib.sha1()
@@ -79,8 +79,9 @@ class UnicodeDatabaseTest(unittest.TestCase):
class UnicodeFunctionsTest(UnicodeDatabaseTest):
- # update this, if the database changes
- expectedchecksum = 'f0b74d26776331cc7bdc3a4698f037d73f2cee2b'
+ # Update this if the database changes. Make sure to do a full rebuild
+ # (e.g. 'make distclean && make') to get the correct checksum.
+ expectedchecksum = '585302895deead0c1c8478c51da9241d4efedca9'
def test_function_checksum(self):
data = []
h = hashlib.sha1()
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 4f38f0f..fcd2073 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -10,7 +10,10 @@ import unittest
from unittest.mock import patch
from test import support
import os
-import ssl
+try:
+ import ssl
+except ImportError:
+ ssl = None
import sys
import tempfile
from nturl2path import url2pathname, pathname2url
@@ -380,6 +383,7 @@ Content-Type: text/html; charset=iso-8859-1
with support.check_warnings(('',DeprecationWarning)):
urllib.request.URLopener()
+ @unittest.skipUnless(ssl, "ssl module required")
def test_cafile_and_context(self):
context = ssl.create_default_context()
with self.assertRaises(ValueError):
@@ -1346,7 +1350,7 @@ class URLopener_Tests(unittest.TestCase):
# serv.settimeout(3)
# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# serv.bind(("", 9093))
-# serv.listen(5)
+# serv.listen()
# try:
# conn, addr = serv.accept()
# conn.send("1 Hola mundo\n")
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index c746b9a..3182390 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -1422,6 +1422,21 @@ class HandlerTests(unittest.TestCase):
handler.do_open(conn, req)
self.assertTrue(conn.fakesock.closed, "Connection not closed")
+ def test_auth_prior_handler(self):
+ pwd_manager = MockPasswordManager()
+ pwd_manager.add_password(None, 'https://example.com',
+ 'somebody', 'verysecret')
+ auth_prior_handler = urllib.request.HTTPBasicPriorAuthHandler(
+ pwd_manager)
+ http_hand = MockHTTPSHandler()
+
+ opener = OpenerDirector()
+ opener.add_handler(http_hand)
+ opener.add_handler(auth_prior_handler)
+
+ req = Request("https://example.com")
+ opener.open(req)
+ self.assertNotIn('Authorization', http_hand.httpconn.req_headers)
class MiscTests(unittest.TestCase):
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py
index 4c8ba2d..42ebb6e 100644
--- a/Lib/test/test_urllibnet.py
+++ b/Lib/test/test_urllibnet.py
@@ -91,7 +91,8 @@ class urlopenNetworkTests(unittest.TestCase):
# test getcode() with the fancy opener to get 404 error codes
URL = "http://www.example.com/XXXinvalidXXX"
with support.transient_internet(URL):
- open_url = urllib.request.FancyURLopener().open(URL)
+ with self.assertWarns(DeprecationWarning):
+ open_url = urllib.request.FancyURLopener().open(URL)
try:
code = open_url.getcode()
finally:
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
index 3934811..cb323d3 100644
--- a/Lib/test/test_urlparse.py
+++ b/Lib/test/test_urlparse.py
@@ -211,10 +211,6 @@ class UrlParseTestCase(unittest.TestCase):
# "abnormal" cases from RFC 1808:
self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
- self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
- self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
- self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
- self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
@@ -229,6 +225,13 @@ class UrlParseTestCase(unittest.TestCase):
#self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
#self.checkJoin(RFC1808_BASE, 'http:', 'http:')
+ # XXX: The following tests are no longer compatible with RFC3986
+ # self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
+ # self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
+ # self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
+ # self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
+
+
def test_RFC2368(self):
# Issue 11467: path that starts with a number is not parsed correctly
self.assertEqual(urllib.parse.urlparse('mailto:1337@example.org'),
@@ -259,10 +262,6 @@ class UrlParseTestCase(unittest.TestCase):
self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
- self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
- self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
- self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
- self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
@@ -278,10 +277,17 @@ class UrlParseTestCase(unittest.TestCase):
self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
+ # XXX: The following tests are no longer compatible with RFC3986
+ # self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
+ # self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
+ # self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
+ # self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
+
+
def test_RFC3986(self):
# Test cases from RFC3986
self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
- self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
+ self.checkJoin(RFC3986_BASE, ';x', 'http://a/b/c/;x')
self.checkJoin(RFC3986_BASE, 'g:h','g:h')
self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g')
self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g')
@@ -305,17 +311,17 @@ class UrlParseTestCase(unittest.TestCase):
self.checkJoin(RFC3986_BASE, '../..','http://a/')
self.checkJoin(RFC3986_BASE, '../../','http://a/')
self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
+ self.checkJoin(RFC3986_BASE, '../../../g', 'http://a/g')
#Abnormal Examples
# The 'abnormal scenarios' are incompatible with RFC2986 parsing
# Tests are here for reference.
- #self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
- #self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
- #self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
- #self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
-
+ self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
+ self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
+ self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
+ self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.')
self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g')
self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..')
@@ -355,10 +361,8 @@ class UrlParseTestCase(unittest.TestCase):
self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g')
self.checkJoin(SIMPLE_BASE, '../..','http://a/')
self.checkJoin(SIMPLE_BASE, '../../g','http://a/g')
- self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g')
self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/')
- self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h')
self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h')
self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
@@ -372,6 +376,22 @@ class UrlParseTestCase(unittest.TestCase):
self.checkJoin('svn://pathtorepo/dir1', 'dir2', 'svn://pathtorepo/dir2')
self.checkJoin('svn+ssh://pathtorepo/dir1', 'dir2', 'svn+ssh://pathtorepo/dir2')
+ # XXX: The following tests are no longer compatible with RFC3986
+ # self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
+ # self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
+
+ # test for issue22118 duplicate slashes
+ self.checkJoin(SIMPLE_BASE + '/', 'foo', SIMPLE_BASE + '/foo')
+
+ # Non-RFC-defined tests, covering variations of base and trailing
+ # slashes
+ self.checkJoin('http://a/b/c/d/e/', '../../f/g/', 'http://a/b/c/f/g/')
+ self.checkJoin('http://a/b/c/d/e', '../../f/g/', 'http://a/b/f/g/')
+ self.checkJoin('http://a/b/c/d/e/', '/../../f/g/', 'http://a/f/g/')
+ self.checkJoin('http://a/b/c/d/e', '/../../f/g/', 'http://a/f/g/')
+ self.checkJoin('http://a/b/c/d/e/', '../../f/g', 'http://a/b/c/f/g')
+ self.checkJoin('http://a/b/', '../../f/g/', 'http://a/f/g/')
+
def test_RFC2732(self):
str_cases = [
('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py
index 1e8cba3..fcb8454 100644
--- a/Lib/test/test_uuid.py
+++ b/Lib/test/test_uuid.py
@@ -1,9 +1,10 @@
-import unittest
+import unittest.mock
from test import support
import builtins
import io
import os
import shutil
+import subprocess
import uuid
def importable(name):
@@ -412,28 +413,27 @@ class TestUUID(unittest.TestCase):
class TestInternals(unittest.TestCase):
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
def test_find_mac(self):
- data = '''\
-
+ data = '''
fake hwaddr
cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
'''
- def mock_popen(cmd):
- return io.StringIO(data)
-
- if shutil.which('ifconfig') is None:
- path = os.pathsep.join(('/sbin', '/usr/sbin'))
- if shutil.which('ifconfig', path=path) is None:
- self.skipTest('requires ifconfig')
-
- with support.swap_attr(os, 'popen', mock_popen):
- mac = uuid._find_mac(
- command='ifconfig',
- args='',
- hw_identifiers=['hwaddr'],
- get_index=lambda x: x + 1,
- )
- self.assertEqual(mac, 0x1234567890ab)
+
+ popen = unittest.mock.MagicMock()
+ popen.stdout = io.BytesIO(data.encode())
+
+ with unittest.mock.patch.object(shutil, 'which',
+ return_value='/sbin/ifconfig'):
+ with unittest.mock.patch.object(subprocess, 'Popen',
+ return_value=popen):
+ mac = uuid._find_mac(
+ command='ifconfig',
+ args='',
+ hw_identifiers=[b'hwaddr'],
+ get_index=lambda x: x + 1,
+ )
+
+ self.assertEqual(mac, 0x1234567890ab)
def check_node(self, node, requires=None, network=False):
if requires and node is None:
@@ -454,6 +454,11 @@ eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
self.check_node(node, 'ifconfig', True)
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
+ def test_ip_getnode(self):
+ node = uuid._ip_getnode()
+ self.check_node(node, 'ip', True)
+
+ @unittest.skipUnless(os.name == 'posix', 'requires Posix')
def test_arp_getnode(self):
node = uuid._arp_getnode()
self.check_node(node, 'arp', True)
diff --git a/Lib/test/test_wait3.py b/Lib/test/test_wait3.py
index f6a065d..bb71481 100644
--- a/Lib/test/test_wait3.py
+++ b/Lib/test/test_wait3.py
@@ -18,7 +18,8 @@ class Wait3Test(ForkWait):
# This many iterations can be required, since some previously run
# tests (e.g. test_ctypes) could have spawned a lot of children
# very quickly.
- for i in range(30):
+ deadline = time.monotonic() + 10.0
+ while time.monotonic() <= deadline:
# wait3() shouldn't hang, but some of the buildbots seem to hang
# in the forking tests. This is an attempt to fix the problem.
spid, status, rusage = os.wait3(os.WNOHANG)
diff --git a/Lib/test/test_wait4.py b/Lib/test/test_wait4.py
index 352c11a..b427a9b 100644
--- a/Lib/test/test_wait4.py
+++ b/Lib/test/test_wait4.py
@@ -19,13 +19,14 @@ class Wait4Test(ForkWait):
# Issue #11185: wait4 is broken on AIX and will always return 0
# with WNOHANG.
option = 0
- for i in range(10):
+ deadline = time.monotonic() + 10.0
+ while time.monotonic() <= deadline:
# wait4() shouldn't hang, but some of the buildbots seem to hang
# in the forking tests. This is an attempt to fix the problem.
spid, status, rusage = os.wait4(cpid, option)
if spid == cpid:
break
- time.sleep(1.0)
+ time.sleep(0.1)
self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
self.assertTrue(rusage)
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
index fb01b83..9ac2139 100644
--- a/Lib/test/test_warnings.py
+++ b/Lib/test/test_warnings.py
@@ -5,7 +5,7 @@ from io import StringIO
import sys
import unittest
from test import support
-from test.script_helper import assert_python_ok
+from test.script_helper import assert_python_ok, assert_python_failure
from test import warning_tests
@@ -432,6 +432,41 @@ class WarnTests(BaseTest):
with self.assertRaises(ValueError):
self.module.warn(BadStrWarning())
+ def test_warning_classes(self):
+ class MyWarningClass(Warning):
+ pass
+
+ class NonWarningSubclass:
+ pass
+
+ # passing a non-subclass of Warning should raise a TypeError
+ with self.assertRaises(TypeError) as cm:
+ self.module.warn('bad warning category', '')
+ self.assertIn('category must be a Warning subclass, not ',
+ str(cm.exception))
+
+ with self.assertRaises(TypeError) as cm:
+ self.module.warn('bad warning category', NonWarningSubclass)
+ self.assertIn('category must be a Warning subclass, not ',
+ str(cm.exception))
+
+ # check that warning instances also raise a TypeError
+ with self.assertRaises(TypeError) as cm:
+ self.module.warn('bad warning category', MyWarningClass())
+ self.assertIn('category must be a Warning subclass, not ',
+ str(cm.exception))
+
+ with self.assertWarns(MyWarningClass) as cm:
+ self.module.warn('good warning category', MyWarningClass)
+ self.assertEqual('good warning category', str(cm.warning))
+
+ with self.assertWarns(UserWarning) as cm:
+ self.module.warn('good warning category', None)
+ self.assertEqual('good warning category', str(cm.warning))
+
+ with self.assertWarns(MyWarningClass) as cm:
+ self.module.warn('good warning category', MyWarningClass)
+ self.assertIsInstance(cm.warning, Warning)
class CWarnTests(WarnTests, unittest.TestCase):
module = c_warnings
@@ -821,7 +856,19 @@ class EnvironmentVariableTests(BaseTest):
"import sys; sys.stdout.write(str(sys.warnoptions))",
PYTHONWARNINGS="ignore::DeprecationWarning")
self.assertEqual(stdout,
- b"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
+ b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
+
+ def test_conflicting_envvar_and_command_line(self):
+ rc, stdout, stderr = assert_python_failure("-Werror::DeprecationWarning", "-c",
+ "import sys, warnings; sys.stdout.write(str(sys.warnoptions)); "
+ "warnings.warn('Message', DeprecationWarning)",
+ PYTHONWARNINGS="default::DeprecationWarning")
+ self.assertEqual(stdout,
+ b"['default::DeprecationWarning', 'error::DeprecationWarning']")
+ self.assertEqual(stderr.splitlines(),
+ [b"Traceback (most recent call last):",
+ b" File \"<string>\", line 1, in <module>",
+ b"DeprecationWarning: Message"])
@unittest.skipUnless(sys.getfilesystemencoding() != 'ascii',
'requires non-ascii filesystemencoding')
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index 3e7347c..e735376 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -92,6 +92,18 @@ class ReferencesTestCase(TestBase):
self.check_basic_callback(create_function)
self.check_basic_callback(create_bound_method)
+ @support.cpython_only
+ def test_cfunction(self):
+ import _testcapi
+ create_cfunction = _testcapi.create_cfunction
+ f = create_cfunction()
+ wr = weakref.ref(f)
+ self.assertIs(wr(), f)
+ del f
+ self.assertIsNone(wr())
+ self.check_basic_ref(create_cfunction)
+ self.check_basic_callback(create_cfunction)
+
def test_multiple_callbacks(self):
o = C()
ref1 = weakref.ref(o, self.callback)
@@ -1574,6 +1586,14 @@ class MappingTestCase(TestBase):
self.assertEqual(len(d), 0)
self.assertEqual(count, 2)
+ def test_make_weak_valued_dict_repr(self):
+ dict = weakref.WeakValueDictionary()
+ self.assertRegex(repr(dict), '<WeakValueDictionary at 0x.*>')
+
+ def test_make_weak_keyed_dict_repr(self):
+ dict = weakref.WeakKeyDictionary()
+ self.assertRegex(repr(dict), '<WeakKeyDictionary at 0x.*>')
+
from test import mapping_tests
class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py
index e213d77..479fcbc 100644
--- a/Lib/test/test_wsgiref.py
+++ b/Lib/test/test_wsgiref.py
@@ -343,6 +343,7 @@ class HeaderTests(TestCase):
def testMappingInterface(self):
test = [('x','y')]
+ self.assertEqual(len(Headers()), 0)
self.assertEqual(len(Headers([])),0)
self.assertEqual(len(Headers(test[:])),1)
self.assertEqual(Headers(test[:]).keys(), ['x'])
@@ -350,7 +351,7 @@ class HeaderTests(TestCase):
self.assertEqual(Headers(test[:]).items(), test)
self.assertIsNot(Headers(test).items(), test) # must be copy!
- h=Headers([])
+ h = Headers()
del h['foo'] # should not raise an error
h['Foo'] = 'bar'
@@ -375,9 +376,8 @@ class HeaderTests(TestCase):
def testRequireList(self):
self.assertRaises(TypeError, Headers, "foo")
-
def testExtras(self):
- h = Headers([])
+ h = Headers()
self.assertEqual(str(h),'\r\n')
h.add_header('foo','bar',baz="spam")
diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py
index 71b590c..dc37be2 100644
--- a/Lib/test/test_xmlrpc.py
+++ b/Lib/test/test_xmlrpc.py
@@ -287,7 +287,7 @@ class DateTimeTestCase(unittest.TestCase):
def test_repr(self):
d = datetime.datetime(2007,1,2,3,4,5)
t = xmlrpclib.DateTime(d)
- val ="<DateTime '20070102T03:04:05' at %x>" % id(t)
+ val ="<DateTime '20070102T03:04:05' at %#x>" % id(t)
self.assertEqual(repr(t), val)
def test_decode(self):
@@ -713,6 +713,23 @@ class SimpleServerTestCase(BaseServerTestCase):
conn.request('POST', '/RPC2 HTTP/1.0\r\nContent-Length: 100\r\n\r\nbye')
conn.close()
+ def test_context_manager(self):
+ with xmlrpclib.ServerProxy(URL) as server:
+ server.add(2, 3)
+ self.assertNotEqual(server('transport')._connection,
+ (None, None))
+ self.assertEqual(server('transport')._connection,
+ (None, None))
+
+ def test_context_manager_method_error(self):
+ try:
+ with xmlrpclib.ServerProxy(URL) as server:
+ server.add(2, "a")
+ except xmlrpclib.Fault:
+ pass
+ self.assertEqual(server('transport')._connection,
+ (None, None))
+
class MultiPathServerTestCase(BaseServerTestCase):
threadFunc = staticmethod(http_multi_server)
@@ -891,7 +908,7 @@ class GzipUtilTestCase(unittest.TestCase):
data = b'\0' * (max_gzip_decode + 1)
encoded = xmlrpclib.gzip_encode(data)
- with self.assertRaisesRegexp(ValueError,
+ with self.assertRaisesRegex(ValueError,
"max gzipped payload length exceeded"):
xmlrpclib.gzip_decode(encoded)
@@ -919,6 +936,7 @@ class ServerProxyTestCase(unittest.TestCase):
p = xmlrpclib.ServerProxy(self.url, transport=t)
self.assertEqual(p('transport'), t)
+
# This is a contrived way to make a failure occur on the server side
# in order to test the _send_traceback_header flag on the server
class FailingMessageClass(http.client.HTTPMessage):
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 4633fe6..017c646 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -330,6 +330,37 @@ class AbstractTestsWithSourceFile:
while zipopen.read1(100):
pass
+ def test_repr(self):
+ fname = 'file.name'
+ for f in get_files(self):
+ with zipfile.ZipFile(f, 'w', self.compression) as zipfp:
+ zipfp.write(TESTFN, fname)
+ r = repr(zipfp)
+ self.assertIn("mode='w'", r)
+
+ with zipfile.ZipFile(f, 'r') as zipfp:
+ r = repr(zipfp)
+ if isinstance(f, str):
+ self.assertIn('filename=%r' % f, r)
+ else:
+ self.assertIn('file=%r' % f, r)
+ self.assertIn("mode='r'", r)
+ r = repr(zipfp.getinfo(fname))
+ self.assertIn('filename=%r' % fname, r)
+ self.assertIn('filemode=', r)
+ self.assertIn('file_size=', r)
+ if self.compression != zipfile.ZIP_STORED:
+ self.assertIn('compress_type=', r)
+ self.assertIn('compress_size=', r)
+ with zipfp.open(fname) as zipopen:
+ r = repr(zipopen)
+ self.assertIn('name=%r' % fname, r)
+ self.assertIn("mode='r'", r)
+ if self.compression != zipfile.ZIP_STORED:
+ self.assertIn('compress_type=', r)
+ self.assertIn('[closed]', repr(zipopen))
+ self.assertIn('[closed]', repr(zipfp))
+
def tearDown(self):
unlink(TESTFN)
unlink(TESTFN2)
@@ -1645,6 +1676,34 @@ class LzmaTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
compression = zipfile.ZIP_LZMA
+# Privide the tell() method but not seek()
+class Tellable:
+ def __init__(self, fp):
+ self.fp = fp
+ self.offset = 0
+
+ def write(self, data):
+ self.offset += self.fp.write(data)
+
+ def tell(self):
+ return self.offset
+
+ def flush(self):
+ pass
+
+class UnseekableTests(unittest.TestCase):
+ def test_writestr_tellable(self):
+ f = io.BytesIO()
+ with zipfile.ZipFile(Tellable(f), 'w', zipfile.ZIP_STORED) as zipfp:
+ zipfp.writestr('ones', b'111')
+ zipfp.writestr('twos', b'222')
+ with zipfile.ZipFile(f, mode='r') as zipf:
+ with zipf.open('ones') as zopen:
+ self.assertEqual(zopen.read(), b'111')
+ with zipf.open('twos') as zopen:
+ self.assertEqual(zopen.read(), b'222')
+
+
@requires_zlib
class TestsWithMultipleOpens(unittest.TestCase):
@classmethod
@@ -1661,35 +1720,52 @@ class TestsWithMultipleOpens(unittest.TestCase):
def test_same_file(self):
# Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other.
- self.make_test_archive(TESTFN2)
- with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
- with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
- data1 = zopen1.read(500)
- data2 = zopen2.read(500)
- data1 += zopen1.read()
- data2 += zopen2.read()
- self.assertEqual(data1, data2)
- self.assertEqual(data1, self.data1)
+ for f in get_files(self):
+ self.make_test_archive(f)
+ with zipfile.ZipFile(f, mode="r") as zipf:
+ with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
+ data1 = zopen1.read(500)
+ data2 = zopen2.read(500)
+ data1 += zopen1.read()
+ data2 += zopen2.read()
+ self.assertEqual(data1, data2)
+ self.assertEqual(data1, self.data1)
def test_different_file(self):
# Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other.
- self.make_test_archive(TESTFN2)
- with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
- with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
- data1 = zopen1.read(500)
- data2 = zopen2.read(500)
- data1 += zopen1.read()
- data2 += zopen2.read()
- self.assertEqual(data1, self.data1)
- self.assertEqual(data2, self.data2)
+ for f in get_files(self):
+ self.make_test_archive(f)
+ with zipfile.ZipFile(f, mode="r") as zipf:
+ with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
+ data1 = zopen1.read(500)
+ data2 = zopen2.read(500)
+ data1 += zopen1.read()
+ data2 += zopen2.read()
+ self.assertEqual(data1, self.data1)
+ self.assertEqual(data2, self.data2)
def test_interleaved(self):
# Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other.
- self.make_test_archive(TESTFN2)
- with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
- with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
+ for f in get_files(self):
+ self.make_test_archive(f)
+ with zipfile.ZipFile(f, mode="r") as zipf:
+ with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
+ data1 = zopen1.read(500)
+ data2 = zopen2.read(500)
+ data1 += zopen1.read()
+ data2 += zopen2.read()
+ self.assertEqual(data1, self.data1)
+ self.assertEqual(data2, self.data2)
+
+ def test_read_after_close(self):
+ for f in get_files(self):
+ self.make_test_archive(f)
+ with contextlib.ExitStack() as stack:
+ with zipfile.ZipFile(f, 'r') as zipf:
+ zopen1 = stack.enter_context(zipf.open('ones'))
+ zopen2 = stack.enter_context(zipf.open('twos'))
data1 = zopen1.read(500)
data2 = zopen2.read(500)
data1 += zopen1.read()
@@ -1697,43 +1773,32 @@ class TestsWithMultipleOpens(unittest.TestCase):
self.assertEqual(data1, self.data1)
self.assertEqual(data2, self.data2)
- def test_read_after_close(self):
- self.make_test_archive(TESTFN2)
- with contextlib.ExitStack() as stack:
- with zipfile.ZipFile(TESTFN2, 'r') as zipf:
- zopen1 = stack.enter_context(zipf.open('ones'))
- zopen2 = stack.enter_context(zipf.open('twos'))
- data1 = zopen1.read(500)
- data2 = zopen2.read(500)
- data1 += zopen1.read()
- data2 += zopen2.read()
- self.assertEqual(data1, self.data1)
- self.assertEqual(data2, self.data2)
-
def test_read_after_write(self):
- with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_DEFLATED) as zipf:
- zipf.writestr('ones', self.data1)
- zipf.writestr('twos', self.data2)
- with zipf.open('ones') as zopen1:
- data1 = zopen1.read(500)
- self.assertEqual(data1, self.data1[:500])
- with zipfile.ZipFile(TESTFN2, 'r') as zipf:
- data1 = zipf.read('ones')
- data2 = zipf.read('twos')
- self.assertEqual(data1, self.data1)
- self.assertEqual(data2, self.data2)
+ for f in get_files(self):
+ with zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED) as zipf:
+ zipf.writestr('ones', self.data1)
+ zipf.writestr('twos', self.data2)
+ with zipf.open('ones') as zopen1:
+ data1 = zopen1.read(500)
+ self.assertEqual(data1, self.data1[:500])
+ with zipfile.ZipFile(f, 'r') as zipf:
+ data1 = zipf.read('ones')
+ data2 = zipf.read('twos')
+ self.assertEqual(data1, self.data1)
+ self.assertEqual(data2, self.data2)
def test_write_after_read(self):
- with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipf:
- zipf.writestr('ones', self.data1)
- with zipf.open('ones') as zopen1:
- zopen1.read(500)
- zipf.writestr('twos', self.data2)
- with zipfile.ZipFile(TESTFN2, 'r') as zipf:
- data1 = zipf.read('ones')
- data2 = zipf.read('twos')
- self.assertEqual(data1, self.data1)
- self.assertEqual(data2, self.data2)
+ for f in get_files(self):
+ with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipf:
+ zipf.writestr('ones', self.data1)
+ with zipf.open('ones') as zopen1:
+ zopen1.read(500)
+ zipf.writestr('twos', self.data2)
+ with zipfile.ZipFile(f, 'r') as zipf:
+ data1 = zipf.read('ones')
+ data2 = zipf.read('twos')
+ self.assertEqual(data1, self.data1)
+ self.assertEqual(data2, self.data2)
def test_many_opens(self):
# Verify that read() and open() promptly close the file descriptor,
diff --git a/Lib/test/test_zipimport_support.py b/Lib/test/test_zipimport_support.py
index 66c3557..2e801a8 100644
--- a/Lib/test/test_zipimport_support.py
+++ b/Lib/test/test_zipimport_support.py
@@ -39,7 +39,7 @@ def _run_object_doctest(obj, module):
# Use the object's fully qualified name if it has one
# Otherwise, use the module's name
try:
- name = "%s.%s" % (obj.__module__, obj.__name__)
+ name = "%s.%s" % (obj.__module__, obj.__qualname__)
except AttributeError:
name = module.__name__
for example in finder.find(obj, name, module):
diff --git a/Lib/threading.py b/Lib/threading.py
index 37aa3b8..24cc911 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -3,10 +3,7 @@
import sys as _sys
import _thread
-try:
- from time import monotonic as _time
-except ImportError:
- from time import time as _time
+from time import monotonic as _time
from traceback import format_exc as _format_exc
from _weakrefset import WeakSet
from itertools import islice as _islice, count as _count
@@ -106,8 +103,14 @@ class _RLock:
owner = _active[owner].name
except KeyError:
pass
- return "<%s owner=%r count=%d>" % (
- self.__class__.__name__, owner, self._count)
+ return "<%s %s.%s object owner=%r count=%d at %s>" % (
+ "locked" if self._block.locked() else "unlocked",
+ self.__class__.__module__,
+ self.__class__.__qualname__,
+ owner,
+ self._count,
+ hex(id(self))
+ )
def acquire(self, blocking=True, timeout=-1):
"""Acquire a lock, blocking or non-blocking.
diff --git a/Lib/timeit.py b/Lib/timeit.py
index 9cec000..3807794 100755
--- a/Lib/timeit.py
+++ b/Lib/timeit.py
@@ -60,6 +60,8 @@ default_number = 1000000
default_repeat = 3
default_timer = time.perf_counter
+_globals = globals
+
# Don't change the indentation of the template; the reindent() calls
# in Timer.__init__() depend on setup being indented 4 spaces and stmt
# being indented 8 spaces.
@@ -94,7 +96,9 @@ class Timer:
The constructor takes a statement to be timed, an additional
statement used for setup, and a timer function. Both statements
default to 'pass'; the timer function is platform-dependent (see
- module doc string).
+ module doc string). If 'globals' is specified, the code will be
+ executed within that namespace (as opposed to inside timeit's
+ namespace).
To measure the execution time of the first statement, use the
timeit() method. The repeat() method is a convenience to call
@@ -104,10 +108,12 @@ class Timer:
multi-line string literals.
"""
- def __init__(self, stmt="pass", setup="pass", timer=default_timer):
+ def __init__(self, stmt="pass", setup="pass", timer=default_timer,
+ globals=None):
"""Constructor. See class doc string."""
self.timer = timer
- ns = {}
+ local_ns = {}
+ global_ns = _globals() if globals is None else globals
if isinstance(stmt, str):
# Check that the code can be compiled outside a function
if isinstance(setup, str):
@@ -121,19 +127,19 @@ class Timer:
src = template.format(stmt=stmt, setup=setup)
elif callable(setup):
src = template.format(stmt=stmt, setup='_setup()')
- ns['_setup'] = setup
+ local_ns['_setup'] = setup
else:
raise ValueError("setup is neither a string nor callable")
- self.src = src # Save for traceback display
+ self.src = src # Save for traceback display
code = compile(src, dummy_src_name, "exec")
- exec(code, globals(), ns)
- self.inner = ns["inner"]
+ exec(code, global_ns, local_ns)
+ self.inner = local_ns["inner"]
elif callable(stmt):
self.src = None
if isinstance(setup, str):
_setup = setup
def setup():
- exec(_setup, globals(), ns)
+ exec(_setup, global_ns, local_ns)
elif not callable(setup):
raise ValueError("setup is neither a string nor callable")
self.inner = _template_func(setup, stmt)
@@ -214,14 +220,14 @@ class Timer:
return r
def timeit(stmt="pass", setup="pass", timer=default_timer,
- number=default_number):
+ number=default_number, globals=None):
"""Convenience function to create Timer object and call timeit method."""
- return Timer(stmt, setup, timer).timeit(number)
+ return Timer(stmt, setup, timer, globals).timeit(number)
def repeat(stmt="pass", setup="pass", timer=default_timer,
- repeat=default_repeat, number=default_number):
+ repeat=default_repeat, number=default_number, globals=None):
"""Convenience function to create Timer object and call repeat method."""
- return Timer(stmt, setup, timer).repeat(repeat, number)
+ return Timer(stmt, setup, timer, globals).repeat(repeat, number)
def main(args=None, *, _wrap_timer=None):
"""Main program, used when run as a script.
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index a1ffca1..beb0a73 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -770,9 +770,6 @@ class Misc:
"""Raise this widget in the stacking order."""
self.tk.call('raise', self._w, aboveThis)
lift = tkraise
- def colormodel(self, value=None):
- """Useless. Not implemented in Tk."""
- return self.tk.call('tk', 'colormodel', self._w, value)
def winfo_atom(self, name, displayof=0):
"""Return integer which represents atom NAME."""
args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
@@ -1331,6 +1328,11 @@ class Misc:
def __str__(self):
"""Return the window path name of this widget."""
return self._w
+
+ def __repr__(self):
+ return '<%s.%s object %s>' % (
+ self.__class__.__module__, self.__class__.__qualname__, self._w)
+
# Pack methods that apply to the master
_noarg_ = ['_noarg_']
def pack_propagate(self, flag=_noarg_):
@@ -2191,21 +2193,6 @@ class Button(Widget):
"""
Widget.__init__(self, master, 'button', cnf, kw)
- def tkButtonEnter(self, *dummy):
- self.tk.call('tkButtonEnter', self._w)
-
- def tkButtonLeave(self, *dummy):
- self.tk.call('tkButtonLeave', self._w)
-
- def tkButtonDown(self, *dummy):
- self.tk.call('tkButtonDown', self._w)
-
- def tkButtonUp(self, *dummy):
- self.tk.call('tkButtonUp', self._w)
-
- def tkButtonInvoke(self, *dummy):
- self.tk.call('tkButtonInvoke', self._w)
-
def flash(self):
"""Flash the button.
@@ -2704,35 +2691,15 @@ class Menu(Widget):
disabledforeground, fg, font, foreground, postcommand, relief,
selectcolor, takefocus, tearoff, tearoffcommand, title, type."""
Widget.__init__(self, master, 'menu', cnf, kw)
+ 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 tk_mbPost(self):
- self.tk.call('tk_mbPost', self._w)
- def tk_mbUnpost(self):
- self.tk.call('tk_mbUnpost')
- def tk_traverseToMenu(self, char):
- self.tk.call('tk_traverseToMenu', self._w, char)
- def tk_traverseWithinMenu(self, char):
- self.tk.call('tk_traverseWithinMenu', self._w, char)
- def tk_getMenuButtons(self):
- return self.tk.call('tk_getMenuButtons', self._w)
- def tk_nextMenu(self, count):
- self.tk.call('tk_nextMenu', count)
- def tk_nextMenuEntry(self, count):
- self.tk.call('tk_nextMenuEntry', count)
- def tk_invokeMenu(self):
- self.tk.call('tk_invokeMenu', self._w)
- def tk_firstMenu(self):
- self.tk.call('tk_firstMenu', self._w)
- def tk_mbButtonDown(self):
- self.tk.call('tk_mbButtonDown', self._w)
- 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 activate(self, index):
"""Activate entry at INDEX."""
self.tk.call(self._w, 'activate', index)
@@ -2905,10 +2872,14 @@ class Scrollbar(Widget):
relief, repeatdelay, repeatinterval, takefocus,
troughcolor, width."""
Widget.__init__(self, master, 'scrollbar', cnf, kw)
- def activate(self, index):
- """Display the element at INDEX with activebackground and activerelief.
- INDEX can be "arrow1","slider" or "arrow2"."""
- self.tk.call(self._w, 'activate', index)
+ def activate(self, index=None):
+ """Marks the element indicated by index as active.
+ The only index values understood by this method are "arrow1",
+ "slider", or "arrow2". If any other value is specified then no
+ element of the scrollbar will be active. If index is not specified,
+ the method returns the name of the element that is currently active,
+ or None if no element is active."""
+ return self.tk.call(self._w, 'activate', index) or None
def delta(self, deltax, deltay):
"""Return the fractional change of the scrollbar setting if it
would be moved by DELTAX or DELTAY pixels."""
@@ -2926,10 +2897,10 @@ class Scrollbar(Widget):
"""Return the current fractional values (upper and lower end)
of the slider position."""
return self._getdoubles(self.tk.call(self._w, 'get'))
- def set(self, *args):
+ def set(self, first, last):
"""Set the fractional values of the slider position (upper and
lower ends as value between 0 and 1)."""
- self.tk.call((self._w, 'set') + args)
+ self.tk.call(self._w, 'set', first, last)
@@ -2964,14 +2935,6 @@ class Text(Widget, XView, YView):
box of the visible part of the character at the given index."""
return self._getints(
self.tk.call(self._w, 'bbox', index)) or None
- def tk_textSelectTo(self, index):
- self.tk.call('tk_textSelectTo', self._w, index)
- def tk_textBackspace(self):
- self.tk.call('tk_textBackspace', self._w)
- def tk_textIndexCloser(self, a, b, c):
- self.tk.call('tk_textIndexCloser', self._w, a, b, c)
- def tk_textResetAnchor(self, index):
- self.tk.call('tk_textResetAnchor', self._w, index)
def compare(self, index1, op, index2):
"""Return whether between index INDEX1 and index INDEX2 the
relation OP is satisfied. OP is one of <, <=, ==, >=, >, or !=."""
@@ -3849,35 +3812,12 @@ class PanedWindow(Widget):
"""Returns an ordered list of the child panes."""
return self.tk.splitlist(self.tk.call(self._w, 'panes'))
-######################################################################
-# Extensions:
-
-class Studbutton(Button):
- def __init__(self, master=None, cnf={}, **kw):
- Widget.__init__(self, master, 'studbutton', cnf, kw)
- self.bind('<Any-Enter>', self.tkButtonEnter)
- self.bind('<Any-Leave>', self.tkButtonLeave)
- self.bind('<1>', self.tkButtonDown)
- self.bind('<ButtonRelease-1>', self.tkButtonUp)
-
-class Tributton(Button):
- def __init__(self, master=None, cnf={}, **kw):
- Widget.__init__(self, master, 'tributton', cnf, kw)
- self.bind('<Any-Enter>', self.tkButtonEnter)
- self.bind('<Any-Leave>', self.tkButtonLeave)
- self.bind('<1>', self.tkButtonDown)
- self.bind('<ButtonRelease-1>', self.tkButtonUp)
- self['fg'] = self['bg']
- self['activebackground'] = self['bg']
-
-######################################################################
# Test:
def _test():
root = Tk()
text = "This is Tcl/Tk version %s" % TclVersion
- if TclVersion >= 8.1:
- text += "\nThis should be a cedilla: \xe7"
+ text += "\nThis should be a cedilla: \xe7"
label = Label(root, text=text)
label.pack()
test = Button(root, text="Click me!",
diff --git a/Lib/tkinter/test/test_tkinter/test_misc.py b/Lib/tkinter/test/test_tkinter/test_misc.py
index d8de949..85ee2c7 100644
--- a/Lib/tkinter/test/test_tkinter/test_misc.py
+++ b/Lib/tkinter/test/test_tkinter/test_misc.py
@@ -7,6 +7,11 @@ support.requires('gui')
class MiscTest(AbstractTkTest, unittest.TestCase):
+ def test_repr(self):
+ t = tkinter.Toplevel(self.root, name='top')
+ f = tkinter.Frame(t, name='child')
+ self.assertEqual(repr(f), '<tkinter.Frame object .top.child>')
+
def test_tk_setPalette(self):
root = self.root
root.tk_setPalette('black')
diff --git a/Lib/tkinter/test/test_tkinter/test_widgets.py b/Lib/tkinter/test/test_tkinter/test_widgets.py
index 58c8ea1..25b1a02 100644
--- a/Lib/tkinter/test/test_tkinter/test_widgets.py
+++ b/Lib/tkinter/test/test_tkinter/test_widgets.py
@@ -920,8 +920,9 @@ class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
sb = self.create()
for e in ('arrow1', 'slider', 'arrow2'):
sb.activate(e)
+ self.assertEqual(sb.activate(), e)
sb.activate('')
- self.assertRaises(TypeError, sb.activate)
+ self.assertIsNone(sb.activate())
self.assertRaises(TypeError, sb.activate, 'arrow1', 'arrow2')
def test_set(self):
@@ -931,8 +932,8 @@ class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
self.assertRaises(TclError, sb.set, 'abc', 'def')
self.assertRaises(TclError, sb.set, 0.6, 'def')
self.assertRaises(TclError, sb.set, 0.6, None)
- self.assertRaises(TclError, sb.set, 0.6)
- self.assertRaises(TclError, sb.set, 0.6, 0.7, 0.8)
+ self.assertRaises(TypeError, sb.set, 0.6)
+ self.assertRaises(TypeError, sb.set, 0.6, 0.7, 0.8)
@add_standard_options(StandardOptionsTests)
diff --git a/Lib/token.py b/Lib/token.py
index 7470c8c..bdfcec8 100644
--- a/Lib/token.py
+++ b/Lib/token.py
@@ -60,11 +60,12 @@ DOUBLESTAREQUAL = 46
DOUBLESLASH = 47
DOUBLESLASHEQUAL = 48
AT = 49
-RARROW = 50
-ELLIPSIS = 51
-OP = 52
-ERRORTOKEN = 53
-N_TOKENS = 54
+ATEQUAL = 50
+RARROW = 51
+ELLIPSIS = 52
+OP = 53
+ERRORTOKEN = 54
+N_TOKENS = 55
NT_OFFSET = 256
#--end constants--
diff --git a/Lib/tokenize.py b/Lib/tokenize.py
index 5b47ebd..0659c55 100644
--- a/Lib/tokenize.py
+++ b/Lib/tokenize.py
@@ -90,7 +90,8 @@ EXACT_TOKEN_TYPES = {
'**=': DOUBLESTAREQUAL,
'//': DOUBLESLASH,
'//=': DOUBLESLASHEQUAL,
- '@': AT
+ '@': AT,
+ '@=': ATEQUAL,
}
class TokenInfo(collections.namedtuple('TokenInfo', 'type string start end line')):
@@ -149,7 +150,7 @@ String = group(StringPrefix + r"'[^\n'\\]*(?:\\.[^\n'\\]*)*'",
# recognized as two instances of =).
Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"!=",
r"//=?", r"->",
- r"[+\-*/%&|^=<>]=?",
+ r"[+\-*/%&@|^=<>]=?",
r"~")
Bracket = '[][(){}]'
@@ -185,7 +186,6 @@ endpats = {"'": Single, '"': Double,
"rB'''": Single3, 'rB"""': Double3,
"RB'''": Single3, 'RB"""': Double3,
"u'''": Single3, 'u"""': Double3,
- "R'''": Single3, 'R"""': Double3,
"U'''": Single3, 'U"""': Double3,
'r': None, 'R': None, 'b': None, 'B': None,
'u': None, 'U': None}
diff --git a/Lib/trace.py b/Lib/trace.py
index 09fe9ee..fe84973 100755
--- a/Lib/trace.py
+++ b/Lib/trace.py
@@ -59,10 +59,7 @@ import gc
import dis
import pickle
from warnings import warn as _warn
-try:
- from time import monotonic as _time
-except ImportError:
- from time import time as _time
+from time import monotonic as _time
try:
import threading
@@ -326,16 +323,17 @@ class CoverageResults:
lnotab = _find_executable_linenos(filename)
else:
lnotab = {}
+ if lnotab:
+ source = linecache.getlines(filename)
+ coverpath = os.path.join(dir, modulename + ".cover")
+ with open(filename, 'rb') as fp:
+ encoding, _ = tokenize.detect_encoding(fp.readline)
+ n_hits, n_lines = self.write_results_file(coverpath, source,
+ lnotab, count, encoding)
+ if summary and n_lines:
+ percent = int(100 * n_hits / n_lines)
+ sums[modulename] = n_lines, percent, modulename, filename
- source = linecache.getlines(filename)
- coverpath = os.path.join(dir, modulename + ".cover")
- with open(filename, 'rb') as fp:
- encoding, _ = tokenize.detect_encoding(fp.readline)
- n_hits, n_lines = self.write_results_file(coverpath, source,
- lnotab, count, encoding)
- if summary and n_lines:
- percent = int(100 * n_hits / n_lines)
- sums[modulename] = n_lines, percent, modulename, filename
if summary and sums:
print("lines cov% module (path)")
diff --git a/Lib/traceback.py b/Lib/traceback.py
index faf593a..c1ab36e 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -205,7 +205,7 @@ def _format_exception_only_iter(etype, value):
yield _format_final_exc_line(etype, value)
return
- stype = etype.__name__
+ stype = etype.__qualname__
smod = etype.__module__
if smod not in ("__main__", "builtins"):
stype = smod + '.' + stype
diff --git a/Lib/unittest/__init__.py b/Lib/unittest/__init__.py
index a5d50af..f6d7ae2 100644
--- a/Lib/unittest/__init__.py
+++ b/Lib/unittest/__init__.py
@@ -67,3 +67,12 @@ from .signals import installHandler, registerResult, removeResult, removeHandler
# deprecated
_TextTestResult = TextTestResult
+
+# There are no tests here, so don't try to run anything discovered from
+# introspecting the symbols (e.g. FunctionTestCase). Instead, all our
+# tests come from within unittest.test.
+def load_tests(loader, tests, pattern):
+ import os.path
+ # top level directory cached on loader instance
+ this_dir = os.path.dirname(__file__)
+ return loader.discover(start_dir=this_dir, pattern=pattern)
diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py
index 808c50e..8ee6c56 100644
--- a/Lib/unittest/loader.py
+++ b/Lib/unittest/loader.py
@@ -6,6 +6,7 @@ import sys
import traceback
import types
import functools
+import warnings
from fnmatch import fnmatch
@@ -20,19 +21,22 @@ VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE)
def _make_failed_import_test(name, suiteClass):
- message = 'Failed to import test module: %s\n%s' % (name, traceback.format_exc())
+ message = 'Failed to import test module: %s\n%s' % (
+ name, traceback.format_exc())
return _make_failed_test('ModuleImportFailure', name, ImportError(message),
- suiteClass)
+ suiteClass, message)
def _make_failed_load_tests(name, exception, suiteClass):
- return _make_failed_test('LoadTestsFailure', name, exception, suiteClass)
+ message = 'Failed to call load_tests:\n%s' % (traceback.format_exc(),)
+ return _make_failed_test(
+ 'LoadTestsFailure', name, exception, suiteClass, message)
-def _make_failed_test(classname, methodname, exception, suiteClass):
+def _make_failed_test(classname, methodname, exception, suiteClass, message):
def testFailure(self):
raise exception
attrs = {methodname: testFailure}
TestClass = type(classname, (case.TestCase,), attrs)
- return suiteClass((TestClass(methodname),))
+ return suiteClass((TestClass(methodname),)), message
def _make_skipped_test(methodname, exception, suiteClass):
@case.skip(str(exception))
@@ -58,6 +62,13 @@ class TestLoader(object):
suiteClass = suite.TestSuite
_top_level_dir = None
+ def __init__(self):
+ super(TestLoader, self).__init__()
+ self.errors = []
+ # Tracks packages which we have called into via load_tests, to
+ # avoid infinite re-entrancy.
+ self._loading_packages = set()
+
def loadTestsFromTestCase(self, testCaseClass):
"""Return a suite of all tests cases contained in testCaseClass"""
if issubclass(testCaseClass, suite.TestSuite):
@@ -70,8 +81,30 @@ class TestLoader(object):
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
return loaded_suite
- def loadTestsFromModule(self, module, use_load_tests=True):
+ # XXX After Python 3.5, remove backward compatibility hacks for
+ # use_load_tests deprecation via *args and **kws. See issue 16662.
+ def loadTestsFromModule(self, module, *args, pattern=None, **kws):
"""Return a suite of all tests cases contained in the given module"""
+ # This method used to take an undocumented and unofficial
+ # use_load_tests argument. For backward compatibility, we still
+ # accept the argument (which can also be the first position) but we
+ # ignore it and issue a deprecation warning if it's present.
+ if len(args) > 0 or 'use_load_tests' in kws:
+ warnings.warn('use_load_tests is deprecated and ignored',
+ DeprecationWarning)
+ kws.pop('use_load_tests', None)
+ if len(args) > 1:
+ # Complain about the number of arguments, but don't forget the
+ # required `module` argument.
+ complaint = len(args) + 1
+ raise TypeError('loadTestsFromModule() takes 1 positional argument but {} were given'.format(complaint))
+ if len(kws) != 0:
+ # Since the keyword arguments are unsorted (see PEP 468), just
+ # pick the alphabetically sorted first argument to complain about,
+ # if multiple were given. At least the error message will be
+ # predictable.
+ complaint = sorted(kws)[0]
+ raise TypeError("loadTestsFromModule() got an unexpected keyword argument '{}'".format(complaint))
tests = []
for name in dir(module):
obj = getattr(module, name)
@@ -80,12 +113,14 @@ class TestLoader(object):
load_tests = getattr(module, 'load_tests', None)
tests = self.suiteClass(tests)
- if use_load_tests and load_tests is not None:
+ if load_tests is not None:
try:
- return load_tests(self, tests, None)
+ return load_tests(self, tests, pattern)
except Exception as e:
- return _make_failed_load_tests(module.__name__, e,
- self.suiteClass)
+ error_case, error_message = _make_failed_load_tests(
+ module.__name__, e, self.suiteClass)
+ self.errors.append(error_message)
+ return error_case
return tests
def loadTestsFromName(self, name, module=None):
@@ -98,20 +133,47 @@ class TestLoader(object):
The method optionally resolves the names relative to a given module.
"""
parts = name.split('.')
+ error_case, error_message = None, None
if module is None:
parts_copy = parts[:]
while parts_copy:
try:
- module = __import__('.'.join(parts_copy))
+ module_name = '.'.join(parts_copy)
+ module = __import__(module_name)
break
except ImportError:
- del parts_copy[-1]
+ next_attribute = parts_copy.pop()
+ # Last error so we can give it to the user if needed.
+ error_case, error_message = _make_failed_import_test(
+ next_attribute, self.suiteClass)
if not parts_copy:
- raise
+ # Even the top level import failed: report that error.
+ self.errors.append(error_message)
+ return error_case
parts = parts[1:]
obj = module
for part in parts:
- parent, obj = obj, getattr(obj, part)
+ try:
+ parent, obj = obj, getattr(obj, part)
+ except AttributeError as e:
+ # We can't traverse some part of the name.
+ if (getattr(obj, '__path__', None) is not None
+ and error_case is not None):
+ # This is a package (no __path__ per importlib docs), and we
+ # encountered an error importing something. We cannot tell
+ # the difference between package.WrongNameTestClass and
+ # package.wrong_module_name so we just report the
+ # ImportError - it is more informative.
+ self.errors.append(error_message)
+ return error_case
+ else:
+ # Otherwise, we signal that an AttributeError has occurred.
+ error_case, error_message = _make_failed_test(
+ 'AttributeError', part, e, self.suiteClass,
+ 'Failed to access attribute:\n%s' % (
+ traceback.format_exc(),))
+ self.errors.append(error_message)
+ return error_case
if isinstance(obj, types.ModuleType):
return self.loadTestsFromModule(obj)
@@ -170,9 +232,13 @@ class TestLoader(object):
If a test package name (directory with '__init__.py') matches the
pattern then the package will be checked for a 'load_tests' function. If
- this exists then it will be called with loader, tests, pattern.
+ this exists then it will be called with (loader, tests, pattern) unless
+ the package has already had load_tests called from the same discovery
+ invocation, in which case the package module object is not scanned for
+ tests - this ensures that when a package uses discover to further
+ discover child tests that infinite recursion does not happen.
- If load_tests exists then discovery does *not* recurse into the package,
+ If load_tests exists then discovery does *not* recurse into the package,
load_tests is responsible for loading all tests in the package.
The pattern is deliberately not stored as a loader attribute so that
@@ -277,6 +343,8 @@ class TestLoader(object):
return os.path.dirname(full_path)
def _get_name_from_path(self, path):
+ if path == self._top_level_dir:
+ return '.'
path = _jython_aware_splitext(os.path.normpath(path))
_relpath = os.path.relpath(path, self._top_level_dir)
@@ -296,63 +364,111 @@ class TestLoader(object):
def _find_tests(self, start_dir, pattern, namespace=False):
"""Used by discovery. Yields test suites it loads."""
+ # Handle the __init__ in this package
+ name = self._get_name_from_path(start_dir)
+ # name is '.' when start_dir == top_level_dir (and top_level_dir is by
+ # definition not a package).
+ if name != '.' and name not in self._loading_packages:
+ # name is in self._loading_packages while we have called into
+ # loadTestsFromModule with name.
+ tests, should_recurse = self._find_test_path(
+ start_dir, pattern, namespace)
+ if tests is not None:
+ yield tests
+ if not should_recurse:
+ # Either an error occured, or load_tests was used by the
+ # package.
+ return
+ # Handle the contents.
paths = sorted(os.listdir(start_dir))
-
for path in paths:
full_path = os.path.join(start_dir, path)
- if os.path.isfile(full_path):
- if not VALID_MODULE_NAME.match(path):
- # valid Python identifiers only
- continue
- if not self._match_path(path, full_path, pattern):
- continue
- # if the test file matches, load it
+ tests, should_recurse = self._find_test_path(
+ full_path, pattern, namespace)
+ if tests is not None:
+ yield tests
+ if should_recurse:
+ # we found a package that didn't use load_tests.
name = self._get_name_from_path(full_path)
+ self._loading_packages.add(name)
try:
- module = self._get_module_from_name(name)
- except case.SkipTest as e:
- yield _make_skipped_test(name, e, self.suiteClass)
- except:
- yield _make_failed_import_test(name, self.suiteClass)
- else:
- mod_file = os.path.abspath(getattr(module, '__file__', full_path))
- realpath = _jython_aware_splitext(os.path.realpath(mod_file))
- fullpath_noext = _jython_aware_splitext(os.path.realpath(full_path))
- if realpath.lower() != fullpath_noext.lower():
- module_dir = os.path.dirname(realpath)
- mod_name = _jython_aware_splitext(os.path.basename(full_path))
- expected_dir = os.path.dirname(full_path)
- msg = ("%r module incorrectly imported from %r. Expected %r. "
- "Is this module globally installed?")
- raise ImportError(msg % (mod_name, module_dir, expected_dir))
- yield self.loadTestsFromModule(module)
- elif os.path.isdir(full_path):
- if (not namespace and
- not os.path.isfile(os.path.join(full_path, '__init__.py'))):
- continue
-
- load_tests = None
- tests = None
- if fnmatch(path, pattern):
- # only check load_tests if the package directory itself matches the filter
- name = self._get_name_from_path(full_path)
- package = self._get_module_from_name(name)
- load_tests = getattr(package, 'load_tests', None)
- tests = self.loadTestsFromModule(package, use_load_tests=False)
-
- if load_tests is None:
- if tests is not None:
- # tests loaded from package file
- yield tests
- # recurse into the package
- yield from self._find_tests(full_path, pattern,
- namespace=namespace)
- else:
- try:
- yield load_tests(self, tests, pattern)
- except Exception as e:
- yield _make_failed_load_tests(package.__name__, e,
- self.suiteClass)
+ yield from self._find_tests(full_path, pattern, namespace)
+ finally:
+ self._loading_packages.discard(name)
+
+ def _find_test_path(self, full_path, pattern, namespace=False):
+ """Used by discovery.
+
+ Loads tests from a single file, or a directories' __init__.py when
+ passed the directory.
+
+ Returns a tuple (None_or_tests_from_file, should_recurse).
+ """
+ basename = os.path.basename(full_path)
+ if os.path.isfile(full_path):
+ if not VALID_MODULE_NAME.match(basename):
+ # valid Python identifiers only
+ return None, False
+ if not self._match_path(basename, full_path, pattern):
+ return None, False
+ # if the test file matches, load it
+ name = self._get_name_from_path(full_path)
+ try:
+ module = self._get_module_from_name(name)
+ except case.SkipTest as e:
+ return _make_skipped_test(name, e, self.suiteClass), False
+ except:
+ error_case, error_message = \
+ _make_failed_import_test(name, self.suiteClass)
+ self.errors.append(error_message)
+ return error_case, False
+ else:
+ mod_file = os.path.abspath(
+ getattr(module, '__file__', full_path))
+ realpath = _jython_aware_splitext(
+ os.path.realpath(mod_file))
+ fullpath_noext = _jython_aware_splitext(
+ os.path.realpath(full_path))
+ if realpath.lower() != fullpath_noext.lower():
+ module_dir = os.path.dirname(realpath)
+ mod_name = _jython_aware_splitext(
+ os.path.basename(full_path))
+ expected_dir = os.path.dirname(full_path)
+ msg = ("%r module incorrectly imported from %r. Expected "
+ "%r. Is this module globally installed?")
+ raise ImportError(
+ msg % (mod_name, module_dir, expected_dir))
+ return self.loadTestsFromModule(module, pattern=pattern), False
+ elif os.path.isdir(full_path):
+ if (not namespace and
+ not os.path.isfile(os.path.join(full_path, '__init__.py'))):
+ return None, False
+
+ load_tests = None
+ tests = None
+ name = self._get_name_from_path(full_path)
+ try:
+ package = self._get_module_from_name(name)
+ except case.SkipTest as e:
+ return _make_skipped_test(name, e, self.suiteClass), False
+ except:
+ error_case, error_message = \
+ _make_failed_import_test(name, self.suiteClass)
+ self.errors.append(error_message)
+ return error_case, False
+ else:
+ load_tests = getattr(package, 'load_tests', None)
+ # Mark this package as being in load_tests (possibly ;))
+ self._loading_packages.add(name)
+ try:
+ tests = self.loadTestsFromModule(package, pattern=pattern)
+ if load_tests is not None:
+ # loadTestsFromModule(package) has loaded tests for us.
+ return tests, False
+ return tests, True
+ finally:
+ self._loading_packages.discard(name)
+
defaultTestLoader = TestLoader()
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 5555774..3b7c157 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -27,9 +27,13 @@ __version__ = '1.0'
import inspect
import pprint
import sys
+import builtins
+from types import ModuleType
from functools import wraps, partial
+_builtins = {name for name in dir(builtins) if not name.startswith('_')}
+
BaseExceptions = (BaseException,)
if 'java' in sys.platform:
# jython
@@ -271,13 +275,11 @@ def _copy(value):
return value
-_allowed_names = set(
- [
- 'return_value', '_mock_return_value', 'side_effect',
- '_mock_side_effect', '_mock_parent', '_mock_new_parent',
- '_mock_name', '_mock_new_name'
- ]
-)
+_allowed_names = {
+ 'return_value', '_mock_return_value', 'side_effect',
+ '_mock_side_effect', '_mock_parent', '_mock_new_parent',
+ '_mock_name', '_mock_new_name'
+}
def _delegating_property(name):
@@ -375,7 +377,7 @@ class NonCallableMock(Base):
def __init__(
self, spec=None, wraps=None, name=None, spec_set=None,
parent=None, _spec_state=None, _new_name='', _new_parent=None,
- _spec_as_instance=False, _eat_self=None, **kwargs
+ _spec_as_instance=False, _eat_self=None, unsafe=False, **kwargs
):
if _new_parent is None:
_new_parent = parent
@@ -405,6 +407,7 @@ class NonCallableMock(Base):
__dict__['_mock_mock_calls'] = _CallList()
__dict__['method_calls'] = _CallList()
+ __dict__['_mock_unsafe'] = unsafe
if kwargs:
self.configure_mock(**kwargs)
@@ -561,13 +564,16 @@ class NonCallableMock(Base):
def __getattr__(self, name):
- if name == '_mock_methods':
+ if name in {'_mock_methods', '_mock_unsafe'}:
raise AttributeError(name)
elif self._mock_methods is not None:
if name not in self._mock_methods or name in _all_magics:
raise AttributeError("Mock object has no attribute %r" % name)
elif _is_magic(name):
raise AttributeError(name)
+ if not self._mock_unsafe:
+ if name.startswith(('assert', 'assret')):
+ raise AttributeError(name)
result = self._mock_children.get(name)
if result is _deleted:
@@ -750,6 +756,14 @@ class NonCallableMock(Base):
else:
return _call
+ def assert_not_called(_mock_self):
+ """assert that the mock was never called.
+ """
+ self = _mock_self
+ if self.call_count != 0:
+ msg = ("Expected '%s' to not have been called. 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.
@@ -1166,6 +1180,9 @@ class _patch(object):
else:
local = True
+ if name in _builtins and isinstance(target, ModuleType):
+ self.create = True
+
if not self.create and original is DEFAULT:
raise AttributeError(
"%s does not have the attribute %r" % (target, name)
@@ -1660,11 +1677,12 @@ right = ' '.join('r%s' % n for n in numerics.split())
# (as they are metaclass methods)
# __del__ is not supported at all as it causes problems if it exists
-_non_defaults = set('__%s__' % method for method in [
- 'get', 'set', 'delete', 'reversed', 'missing', 'reduce', 'reduce_ex',
- 'getinitargs', 'getnewargs', 'getstate', 'setstate', 'getformat',
- 'setformat', 'repr', 'dir', 'subclasses', 'format',
-])
+_non_defaults = {
+ '__get__', '__set__', '__delete__', '__reversed__', '__missing__',
+ '__reduce__', '__reduce_ex__', '__getinitargs__', '__getnewargs__',
+ '__getstate__', '__setstate__', '__getformat__', '__setformat__',
+ '__repr__', '__dir__', '__subclasses__', '__format__',
+}
def _get_method(name, func):
@@ -1675,19 +1693,19 @@ def _get_method(name, func):
return method
-_magics = set(
+_magics = {
'__%s__' % method for method in
' '.join([magic_methods, numerics, inplace, right]).split()
-)
+}
_all_magics = _magics | _non_defaults
-_unsupported_magics = set([
+_unsupported_magics = {
'__getattr__', '__setattr__',
'__init__', '__new__', '__prepare__'
'__instancecheck__', '__subclasscheck__',
'__del__'
-])
+}
_calculate_return_value = {
'__hash__': lambda self: object.__hash__(self),
@@ -1875,7 +1893,7 @@ def _format_call_signature(name, args, kwargs):
formatted_args = ''
args_string = ', '.join([repr(arg) for arg in args])
kwargs_string = ', '.join([
- '%s=%r' % (key, value) for key, value in kwargs.items()
+ '%s=%r' % (key, value) for key, value in sorted(kwargs.items())
])
if args_string:
formatted_args = args_string
@@ -1997,10 +2015,6 @@ class _Call(tuple):
return (other_args, other_kwargs) == (self_args, self_kwargs)
- def __ne__(self, other):
- return not self.__eq__(other)
-
-
def __call__(self, *args, **kwargs):
if self.name is None:
return _Call(('', args, kwargs), name='()')
@@ -2016,6 +2030,12 @@ class _Call(tuple):
return _Call(name=name, parent=self, from_kall=False)
+ def count(self, *args, **kwargs):
+ return self.__getattr__('count')(*args, **kwargs)
+
+ def index(self, *args, **kwargs):
+ return self.__getattr__('index')(*args, **kwargs)
+
def __repr__(self):
if not self.from_kall:
name = self.name or 'call'
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
index c7ff3b0..503ad2f 100644
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -1103,12 +1103,9 @@ test case
except self.failureException as e:
# need to remove the first line of the error message
error = str(e).split('\n', 1)[1]
+ self.assertEqual(sample_text_error, error)
- # no fair testing ourself with ourself, and assertEqual is used for strings
- # so can't use assertEqual either. Just use assertTrue.
- self.assertTrue(sample_text_error == error)
-
- def testAsertEqualSingleLine(self):
+ def testAssertEqualSingleLine(self):
sample_text = "laden swallows fly slowly"
revised_sample_text = "unladen swallows fly quickly"
sample_text_error = """\
@@ -1120,8 +1117,9 @@ test case
try:
self.assertEqual(sample_text, revised_sample_text)
except self.failureException as e:
+ # need to remove the first line of the error message
error = str(e).split('\n', 1)[1]
- self.assertTrue(sample_text_error == error)
+ self.assertEqual(sample_text_error, error)
def testAssertIsNone(self):
self.assertIsNone(None)
diff --git a/Lib/unittest/test/test_discovery.py b/Lib/unittest/test/test_discovery.py
index abb9066..4f61314 100644
--- a/Lib/unittest/test/test_discovery.py
+++ b/Lib/unittest/test/test_discovery.py
@@ -1,4 +1,5 @@
-import os
+import os.path
+from os.path import abspath
import re
import sys
import types
@@ -68,7 +69,13 @@ class TestDiscovery(unittest.TestCase):
self.addCleanup(restore_isfile)
loader._get_module_from_name = lambda path: path + ' module'
- loader.loadTestsFromModule = lambda module: module + ' tests'
+ orig_load_tests = loader.loadTestsFromModule
+ def loadTestsFromModule(module, pattern=None):
+ # This is where load_tests is called.
+ base = orig_load_tests(module, pattern=pattern)
+ return base + [module + ' tests']
+ loader.loadTestsFromModule = loadTestsFromModule
+ loader.suiteClass = lambda thing: thing
top_level = os.path.abspath('/foo')
loader._top_level_dir = top_level
@@ -76,9 +83,9 @@ class TestDiscovery(unittest.TestCase):
# The test suites found should be sorted alphabetically for reliable
# execution order.
- expected = [name + ' module tests' for name in
- ('test1', 'test2')]
- expected.extend([('test_dir.%s' % name) + ' module tests' for name in
+ expected = [[name + ' module tests'] for name in
+ ('test1', 'test2', 'test_dir')]
+ expected.extend([[('test_dir.%s' % name) + ' module tests'] for name in
('test3', 'test4')])
self.assertEqual(suite, expected)
@@ -116,34 +123,204 @@ class TestDiscovery(unittest.TestCase):
if os.path.basename(path) == 'test_directory':
def load_tests(loader, tests, pattern):
self.load_tests_args.append((loader, tests, pattern))
- return 'load_tests'
+ return [self.path + ' load_tests']
self.load_tests = load_tests
def __eq__(self, other):
return self.path == other.path
loader._get_module_from_name = lambda name: Module(name)
- def loadTestsFromModule(module, use_load_tests):
- if use_load_tests:
- raise self.failureException('use_load_tests should be False for packages')
- return module.path + ' module tests'
+ orig_load_tests = loader.loadTestsFromModule
+ def loadTestsFromModule(module, pattern=None):
+ # This is where load_tests is called.
+ base = orig_load_tests(module, pattern=pattern)
+ return base + [module.path + ' module tests']
loader.loadTestsFromModule = loadTestsFromModule
+ loader.suiteClass = lambda thing: thing
loader._top_level_dir = '/foo'
# this time no '.py' on the pattern so that it can match
# a test package
suite = list(loader._find_tests('/foo', 'test*'))
- # We should have loaded tests from the test_directory package by calling load_tests
- # and directly from the test_directory2 package
+ # We should have loaded tests from the a_directory and test_directory2
+ # directly and via load_tests for the test_directory package, which
+ # still calls the baseline module loader.
self.assertEqual(suite,
- ['load_tests', 'test_directory2' + ' module tests'])
+ [['a_directory module tests'],
+ ['test_directory load_tests',
+ 'test_directory module tests'],
+ ['test_directory2 module tests']])
+
+
# The test module paths should be sorted for reliable execution order
- self.assertEqual(Module.paths, ['test_directory', 'test_directory2'])
+ self.assertEqual(Module.paths,
+ ['a_directory', 'test_directory', 'test_directory2'])
# load_tests should have been called once with loader, tests and pattern
+ # (but there are no tests in our stub module itself, so thats [] at the
+ # time of call.
+ self.assertEqual(Module.load_tests_args,
+ [(loader, [], 'test*')])
+
+ def test_find_tests_default_calls_package_load_tests(self):
+ loader = unittest.TestLoader()
+
+ original_listdir = os.listdir
+ def restore_listdir():
+ os.listdir = original_listdir
+ original_isfile = os.path.isfile
+ def restore_isfile():
+ os.path.isfile = original_isfile
+ original_isdir = os.path.isdir
+ def restore_isdir():
+ os.path.isdir = original_isdir
+
+ directories = ['a_directory', 'test_directory', 'test_directory2']
+ path_lists = [directories, [], [], []]
+ os.listdir = lambda path: path_lists.pop(0)
+ self.addCleanup(restore_listdir)
+
+ os.path.isdir = lambda path: True
+ self.addCleanup(restore_isdir)
+
+ os.path.isfile = lambda path: os.path.basename(path) not in directories
+ self.addCleanup(restore_isfile)
+
+ class Module(object):
+ paths = []
+ load_tests_args = []
+
+ def __init__(self, path):
+ self.path = path
+ self.paths.append(path)
+ if os.path.basename(path) == 'test_directory':
+ def load_tests(loader, tests, pattern):
+ self.load_tests_args.append((loader, tests, pattern))
+ return [self.path + ' load_tests']
+ self.load_tests = load_tests
+
+ def __eq__(self, other):
+ return self.path == other.path
+
+ loader._get_module_from_name = lambda name: Module(name)
+ orig_load_tests = loader.loadTestsFromModule
+ def loadTestsFromModule(module, pattern=None):
+ # This is where load_tests is called.
+ base = orig_load_tests(module, pattern=pattern)
+ return base + [module.path + ' module tests']
+ loader.loadTestsFromModule = loadTestsFromModule
+ loader.suiteClass = lambda thing: thing
+
+ loader._top_level_dir = '/foo'
+ # this time no '.py' on the pattern so that it can match
+ # a test package
+ suite = list(loader._find_tests('/foo', 'test*.py'))
+
+ # We should have loaded tests from the a_directory and test_directory2
+ # directly and via load_tests for the test_directory package, which
+ # still calls the baseline module loader.
+ self.assertEqual(suite,
+ [['a_directory module tests'],
+ ['test_directory load_tests',
+ 'test_directory module tests'],
+ ['test_directory2 module tests']])
+ # The test module paths should be sorted for reliable execution order
+ self.assertEqual(Module.paths,
+ ['a_directory', 'test_directory', 'test_directory2'])
+
+
+ # load_tests should have been called once with loader, tests and pattern
+ self.assertEqual(Module.load_tests_args,
+ [(loader, [], 'test*.py')])
+
+ def test_find_tests_customise_via_package_pattern(self):
+ # This test uses the example 'do-nothing' load_tests from
+ # https://docs.python.org/3/library/unittest.html#load-tests-protocol
+ # to make sure that that actually works.
+ # Housekeeping
+ original_listdir = os.listdir
+ def restore_listdir():
+ os.listdir = original_listdir
+ self.addCleanup(restore_listdir)
+ original_isfile = os.path.isfile
+ def restore_isfile():
+ os.path.isfile = original_isfile
+ self.addCleanup(restore_isfile)
+ original_isdir = os.path.isdir
+ def restore_isdir():
+ os.path.isdir = original_isdir
+ self.addCleanup(restore_isdir)
+ self.addCleanup(sys.path.remove, abspath('/foo'))
+
+ # Test data: we expect the following:
+ # a listdir to find our package, and a isfile and isdir check on it.
+ # a module-from-name call to turn that into a module
+ # followed by load_tests.
+ # then our load_tests will call discover() which is messy
+ # but that finally chains into find_tests again for the child dir -
+ # which is why we don't have a infinite loop.
+ # We expect to see:
+ # the module load tests for both package and plain module called,
+ # and the plain module result nested by the package module load_tests
+ # indicating that it was processed and could have been mutated.
+ vfs = {abspath('/foo'): ['my_package'],
+ abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
+ def list_dir(path):
+ return list(vfs[path])
+ os.listdir = list_dir
+ os.path.isdir = lambda path: not path.endswith('.py')
+ os.path.isfile = lambda path: path.endswith('.py')
+
+ class Module(object):
+ paths = []
+ load_tests_args = []
+
+ def __init__(self, path):
+ self.path = path
+ self.paths.append(path)
+ if path.endswith('test_module'):
+ def load_tests(loader, tests, pattern):
+ self.load_tests_args.append((loader, tests, pattern))
+ return [self.path + ' load_tests']
+ else:
+ def load_tests(loader, tests, pattern):
+ self.load_tests_args.append((loader, tests, pattern))
+ # top level directory cached on loader instance
+ __file__ = '/foo/my_package/__init__.py'
+ this_dir = os.path.dirname(__file__)
+ pkg_tests = loader.discover(
+ start_dir=this_dir, pattern=pattern)
+ return [self.path + ' load_tests', tests
+ ] + pkg_tests
+ self.load_tests = load_tests
+
+ def __eq__(self, other):
+ return self.path == other.path
+
+ loader = unittest.TestLoader()
+ loader._get_module_from_name = lambda name: Module(name)
+ loader.suiteClass = lambda thing: thing
+
+ loader._top_level_dir = abspath('/foo')
+ # this time no '.py' on the pattern so that it can match
+ # a test package
+ suite = list(loader._find_tests(abspath('/foo'), 'test*.py'))
+
+ # We should have loaded tests from both my_package and
+ # my_pacakge.test_module, and also run the load_tests hook in both.
+ # (normally this would be nested TestSuites.)
+ self.assertEqual(suite,
+ [['my_package load_tests', [],
+ ['my_package.test_module load_tests']]])
+ # Parents before children.
+ self.assertEqual(Module.paths,
+ ['my_package', 'my_package.test_module'])
+
+ # load_tests should have been called twice with loader, tests and pattern
self.assertEqual(Module.load_tests_args,
- [(loader, 'test_directory' + ' module tests', 'test*')])
+ [(loader, [], 'test*.py'),
+ (loader, [], 'test*.py')])
def test_discover(self):
loader = unittest.TestLoader()
@@ -191,6 +368,51 @@ class TestDiscovery(unittest.TestCase):
self.assertEqual(_find_tests_args, [(start_dir, 'pattern')])
self.assertIn(top_level_dir, sys.path)
+ def test_discover_start_dir_is_package_calls_package_load_tests(self):
+ # This test verifies that the package load_tests in a package is indeed
+ # invoked when the start_dir is a package (and not the top level).
+ # http://bugs.python.org/issue22457
+
+ # Test data: we expect the following:
+ # an isfile to verify the package, then importing and scanning
+ # as per _find_tests' normal behaviour.
+ # We expect to see our load_tests hook called once.
+ vfs = {abspath('/toplevel'): ['startdir'],
+ abspath('/toplevel/startdir'): ['__init__.py']}
+ def list_dir(path):
+ return list(vfs[path])
+ self.addCleanup(setattr, os, 'listdir', os.listdir)
+ os.listdir = list_dir
+ self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)
+ os.path.isfile = lambda path: path.endswith('.py')
+ self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)
+ os.path.isdir = lambda path: not path.endswith('.py')
+ self.addCleanup(sys.path.remove, abspath('/toplevel'))
+
+ class Module(object):
+ paths = []
+ load_tests_args = []
+
+ def __init__(self, path):
+ self.path = path
+
+ def load_tests(self, loader, tests, pattern):
+ return ['load_tests called ' + self.path]
+
+ def __eq__(self, other):
+ return self.path == other.path
+
+ loader = unittest.TestLoader()
+ loader._get_module_from_name = lambda name: Module(name)
+ loader.suiteClass = lambda thing: thing
+
+ suite = loader.discover('/toplevel/startdir', top_level_dir='/toplevel')
+
+ # We should have loaded tests from the package __init__.
+ # (normally this would be nested TestSuites.)
+ self.assertEqual(suite,
+ [['load_tests called startdir']])
+
def setup_import_issue_tests(self, fakefile):
listdir = os.listdir
os.listdir = lambda _: [fakefile]
@@ -203,6 +425,17 @@ class TestDiscovery(unittest.TestCase):
sys.path[:] = orig_sys_path
self.addCleanup(restore)
+ def setup_import_issue_package_tests(self, vfs):
+ self.addCleanup(setattr, os, 'listdir', os.listdir)
+ self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)
+ self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)
+ self.addCleanup(sys.path.__setitem__, slice(None), list(sys.path))
+ def list_dir(path):
+ return list(vfs[path])
+ os.listdir = list_dir
+ os.path.isdir = lambda path: not path.endswith('.py')
+ os.path.isfile = lambda path: path.endswith('.py')
+
def test_discover_with_modules_that_fail_to_import(self):
loader = unittest.TestLoader()
@@ -211,11 +444,44 @@ class TestDiscovery(unittest.TestCase):
suite = loader.discover('.')
self.assertIn(os.getcwd(), sys.path)
self.assertEqual(suite.countTestCases(), 1)
+ # Errors loading the suite are also captured for introspection.
+ self.assertNotEqual([], loader.errors)
+ self.assertEqual(1, len(loader.errors))
+ error = loader.errors[0]
+ self.assertTrue(
+ 'Failed to import test module: test_this_does_not_exist' in error,
+ 'missing error string in %r' % error)
test = list(list(suite)[0])[0] # extract test from suite
with self.assertRaises(ImportError):
test.test_this_does_not_exist()
+ def test_discover_with_init_modules_that_fail_to_import(self):
+ vfs = {abspath('/foo'): ['my_package'],
+ abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
+ self.setup_import_issue_package_tests(vfs)
+ import_calls = []
+ def _get_module_from_name(name):
+ import_calls.append(name)
+ raise ImportError("Cannot import Name")
+ loader = unittest.TestLoader()
+ loader._get_module_from_name = _get_module_from_name
+ suite = loader.discover(abspath('/foo'))
+
+ self.assertIn(abspath('/foo'), sys.path)
+ self.assertEqual(suite.countTestCases(), 1)
+ # Errors loading the suite are also captured for introspection.
+ self.assertNotEqual([], loader.errors)
+ self.assertEqual(1, len(loader.errors))
+ error = loader.errors[0]
+ self.assertTrue(
+ 'Failed to import test module: my_package' in error,
+ 'missing error string in %r' % error)
+ test = list(list(suite)[0])[0] # extract test from suite
+ with self.assertRaises(ImportError):
+ test.my_package()
+ self.assertEqual(import_calls, ['my_package'])
+
def test_discover_with_module_that_raises_SkipTest_on_import(self):
loader = unittest.TestLoader()
@@ -232,6 +498,26 @@ class TestDiscovery(unittest.TestCase):
suite.run(result)
self.assertEqual(len(result.skipped), 1)
+ def test_discover_with_init_module_that_raises_SkipTest_on_import(self):
+ vfs = {abspath('/foo'): ['my_package'],
+ abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
+ self.setup_import_issue_package_tests(vfs)
+ import_calls = []
+ def _get_module_from_name(name):
+ import_calls.append(name)
+ raise unittest.SkipTest('skipperoo')
+ loader = unittest.TestLoader()
+ loader._get_module_from_name = _get_module_from_name
+ suite = loader.discover(abspath('/foo'))
+
+ self.assertIn(abspath('/foo'), sys.path)
+ self.assertEqual(suite.countTestCases(), 1)
+ result = unittest.TestResult()
+ suite.run(result)
+ self.assertEqual(len(result.skipped), 1)
+ self.assertEqual(result.testsRun, 1)
+ self.assertEqual(import_calls, ['my_package'])
+
def test_command_line_handling_parseArgs(self):
program = TestableTestProgram()
diff --git a/Lib/unittest/test/test_loader.py b/Lib/unittest/test/test_loader.py
index b62a1b5..68f1036 100644
--- a/Lib/unittest/test/test_loader.py
+++ b/Lib/unittest/test/test_loader.py
@@ -1,12 +1,36 @@
import sys
import types
-
+import warnings
import unittest
+# Decorator used in the deprecation tests to reset the warning registry for
+# test isolation and reproducibility.
+def warningregistry(func):
+ def wrapper(*args, **kws):
+ missing = object()
+ saved = getattr(warnings, '__warningregistry__', missing).copy()
+ try:
+ return func(*args, **kws)
+ finally:
+ if saved is missing:
+ try:
+ del warnings.__warningregistry__
+ except AttributeError:
+ pass
+ else:
+ warnings.__warningregistry__ = saved
+
class Test_TestLoader(unittest.TestCase):
+ ### Basic object tests
+ ################################################################
+
+ def test___init__(self):
+ loader = unittest.TestLoader()
+ self.assertEqual([], loader.errors)
+
### Tests for TestLoader.loadTestsFromTestCase
################################################################
@@ -150,6 +174,7 @@ class Test_TestLoader(unittest.TestCase):
# Check that loadTestsFromModule honors (or not) a module
# with a load_tests function.
+ @warningregistry
def test_loadTestsFromModule__load_tests(self):
m = types.ModuleType('m')
class MyTestCase(unittest.TestCase):
@@ -168,10 +193,144 @@ class Test_TestLoader(unittest.TestCase):
suite = loader.loadTestsFromModule(m)
self.assertIsInstance(suite, unittest.TestSuite)
self.assertEqual(load_tests_args, [loader, suite, None])
+ # With Python 3.5, the undocumented and unofficial use_load_tests is
+ # ignored (and deprecated).
+ load_tests_args = []
+ with warnings.catch_warnings(record=False):
+ warnings.simplefilter('never')
+ suite = loader.loadTestsFromModule(m, use_load_tests=False)
+ self.assertEqual(load_tests_args, [loader, suite, None])
+
+ @warningregistry
+ def test_loadTestsFromModule__use_load_tests_deprecated_positional(self):
+ m = types.ModuleType('m')
+ class MyTestCase(unittest.TestCase):
+ def test(self):
+ pass
+ m.testcase_1 = MyTestCase
+
+ load_tests_args = []
+ def load_tests(loader, tests, pattern):
+ self.assertIsInstance(tests, unittest.TestSuite)
+ load_tests_args.extend((loader, tests, pattern))
+ return tests
+ m.load_tests = load_tests
+ # The method still works.
+ loader = unittest.TestLoader()
+ # use_load_tests=True as a positional argument.
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ suite = loader.loadTestsFromModule(m, False)
+ self.assertIsInstance(suite, unittest.TestSuite)
+ # load_tests was still called because use_load_tests is deprecated
+ # and ignored.
+ self.assertEqual(load_tests_args, [loader, suite, None])
+ # We got a warning.
+ self.assertIs(w[-1].category, DeprecationWarning)
+ self.assertEqual(str(w[-1].message),
+ 'use_load_tests is deprecated and ignored')
+
+ @warningregistry
+ def test_loadTestsFromModule__use_load_tests_deprecated_keyword(self):
+ m = types.ModuleType('m')
+ class MyTestCase(unittest.TestCase):
+ def test(self):
+ pass
+ m.testcase_1 = MyTestCase
+
+ load_tests_args = []
+ def load_tests(loader, tests, pattern):
+ self.assertIsInstance(tests, unittest.TestSuite)
+ load_tests_args.extend((loader, tests, pattern))
+ return tests
+ m.load_tests = load_tests
+ # The method still works.
+ loader = unittest.TestLoader()
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ suite = loader.loadTestsFromModule(m, use_load_tests=False)
+ self.assertIsInstance(suite, unittest.TestSuite)
+ # load_tests was still called because use_load_tests is deprecated
+ # and ignored.
+ self.assertEqual(load_tests_args, [loader, suite, None])
+ # We got a warning.
+ self.assertIs(w[-1].category, DeprecationWarning)
+ self.assertEqual(str(w[-1].message),
+ 'use_load_tests is deprecated and ignored')
+
+ @warningregistry
+ def test_loadTestsFromModule__too_many_positional_args(self):
+ m = types.ModuleType('m')
+ class MyTestCase(unittest.TestCase):
+ def test(self):
+ pass
+ m.testcase_1 = MyTestCase
load_tests_args = []
- suite = loader.loadTestsFromModule(m, use_load_tests=False)
- self.assertEqual(load_tests_args, [])
+ def load_tests(loader, tests, pattern):
+ self.assertIsInstance(tests, unittest.TestSuite)
+ load_tests_args.extend((loader, tests, pattern))
+ return tests
+ m.load_tests = load_tests
+ loader = unittest.TestLoader()
+ with self.assertRaises(TypeError) as cm, \
+ warnings.catch_warning(record=True) as w:
+ loader.loadTestsFromModule(m, False, 'testme.*')
+ # We still got the deprecation warning.
+ self.assertIs(w[-1].category, DeprecationWarning)
+ self.assertEqual(str(w[-1].message),
+ 'use_load_tests is deprecated and ignored')
+ # We also got a TypeError for too many positional arguments.
+ self.assertEqual(type(cm.exception), TypeError)
+ self.assertEqual(
+ str(cm.exception),
+ 'loadTestsFromModule() takes 1 positional argument but 3 were given')
+
+ @warningregistry
+ def test_loadTestsFromModule__use_load_tests_other_bad_keyword(self):
+ m = types.ModuleType('m')
+ class MyTestCase(unittest.TestCase):
+ def test(self):
+ pass
+ m.testcase_1 = MyTestCase
+
+ load_tests_args = []
+ def load_tests(loader, tests, pattern):
+ self.assertIsInstance(tests, unittest.TestSuite)
+ load_tests_args.extend((loader, tests, pattern))
+ return tests
+ m.load_tests = load_tests
+ loader = unittest.TestLoader()
+ with warnings.catch_warnings():
+ warnings.simplefilter('never')
+ with self.assertRaises(TypeError) as cm:
+ loader.loadTestsFromModule(
+ m, use_load_tests=False, very_bad=True, worse=False)
+ self.assertEqual(type(cm.exception), TypeError)
+ # The error message names the first bad argument alphabetically,
+ # however use_load_tests (which sorts first) is ignored.
+ self.assertEqual(
+ str(cm.exception),
+ "loadTestsFromModule() got an unexpected keyword argument 'very_bad'")
+
+ def test_loadTestsFromModule__pattern(self):
+ m = types.ModuleType('m')
+ class MyTestCase(unittest.TestCase):
+ def test(self):
+ pass
+ m.testcase_1 = MyTestCase
+
+ load_tests_args = []
+ def load_tests(loader, tests, pattern):
+ self.assertIsInstance(tests, unittest.TestSuite)
+ load_tests_args.extend((loader, tests, pattern))
+ return tests
+ m.load_tests = load_tests
+
+ loader = unittest.TestLoader()
+ suite = loader.loadTestsFromModule(m, pattern='testme.*')
+ self.assertIsInstance(suite, unittest.TestSuite)
+ self.assertEqual(load_tests_args, [loader, suite, 'testme.*'])
def test_loadTestsFromModule__faulty_load_tests(self):
m = types.ModuleType('m')
@@ -184,6 +343,13 @@ class Test_TestLoader(unittest.TestCase):
suite = loader.loadTestsFromModule(m)
self.assertIsInstance(suite, unittest.TestSuite)
self.assertEqual(suite.countTestCases(), 1)
+ # Errors loading the suite are also captured for introspection.
+ self.assertNotEqual([], loader.errors)
+ self.assertEqual(1, len(loader.errors))
+ error = loader.errors[0]
+ self.assertTrue(
+ 'Failed to call load_tests:' in error,
+ 'missing error string in %r' % error)
test = list(suite)[0]
self.assertRaisesRegex(TypeError, "some failure", test.m)
@@ -219,15 +385,15 @@ class Test_TestLoader(unittest.TestCase):
def test_loadTestsFromName__malformed_name(self):
loader = unittest.TestLoader()
- # XXX Should this raise ValueError or ImportError?
- try:
- loader.loadTestsFromName('abc () //')
- except ValueError:
- pass
- except ImportError:
- pass
- else:
- self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
+ 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 \(\) //"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(
+ ImportError, expected_regex, getattr(test, 'abc () //'))
# "The specifier name is a ``dotted name'' that may resolve ... to a
# module"
@@ -236,28 +402,47 @@ class Test_TestLoader(unittest.TestCase):
def test_loadTestsFromName__unknown_module_name(self):
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromName('sdasfasfasdf')
- except ImportError as e:
- self.assertEqual(str(e), "No module named 'sdasfasfasdf'")
- else:
- self.fail("TestLoader.loadTestsFromName failed to raise ImportError")
+ suite = loader.loadTestsFromName('sdasfasfasdf')
+ expected = "No module named 'sdasfasfasdf'"
+ error, test = self.check_deferred_error(loader, suite)
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
- # What happens when the module is found, but the attribute can't?
- def test_loadTestsFromName__unknown_attr_name(self):
+ # What happens when the module is found, but the attribute isn't?
+ def test_loadTestsFromName__unknown_attr_name_on_module(self):
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromName('unittest.sdasfasfasdf')
- except AttributeError as e:
- self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
- else:
- self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
+ suite = loader.loadTestsFromName('unittest.loader.sdasfasfasdf')
+ expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'"
+ error, test = self.check_deferred_error(loader, suite)
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
+
+ # "The specifier name is a ``dotted name'' that may resolve either to
+ # a module, a test case class, a TestSuite instance, a test method
+ # within a test case class, or a callable object which returns a
+ # TestCase or TestSuite instance."
+ #
+ # What happens when the module is found, but the attribute isn't?
+ def test_loadTestsFromName__unknown_attr_name_on_package(self):
+ loader = unittest.TestLoader()
+
+ suite = loader.loadTestsFromName('unittest.sdasfasfasdf')
+ expected = "No module named 'unittest.sdasfasfasdf'"
+ error, test = self.check_deferred_error(loader, suite)
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
@@ -269,12 +454,13 @@ class Test_TestLoader(unittest.TestCase):
def test_loadTestsFromName__relative_unknown_name(self):
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromName('sdasfasfasdf', unittest)
- except AttributeError as e:
- self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
- else:
- self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
+ suite = loader.loadTestsFromName('sdasfasfasdf', unittest)
+ expected = "module 'unittest' has no attribute 'sdasfasfasdf'"
+ error, test = self.check_deferred_error(loader, suite)
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
@@ -290,12 +476,13 @@ class Test_TestLoader(unittest.TestCase):
def test_loadTestsFromName__relative_empty_name(self):
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromName('', unittest)
- except AttributeError as e:
- pass
- else:
- self.fail("Failed to raise AttributeError")
+ suite = loader.loadTestsFromName('', unittest)
+ error, test = self.check_deferred_error(loader, suite)
+ expected = "has no attribute ''"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(AttributeError, expected, getattr(test, ''))
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
@@ -310,14 +497,15 @@ class Test_TestLoader(unittest.TestCase):
loader = unittest.TestLoader()
# XXX Should this raise AttributeError or ValueError?
- try:
- loader.loadTestsFromName('abc () //', unittest)
- except ValueError:
- pass
- except AttributeError:
- pass
- else:
- self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
+ 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 \(\) //'"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(
+ AttributeError, expected_regex, getattr(test, 'abc () //'))
# "The method optionally resolves name relative to the given module"
#
@@ -423,12 +611,13 @@ class Test_TestLoader(unittest.TestCase):
m.testcase_1 = MyTestCase
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromName('testcase_1.testfoo', m)
- except AttributeError as e:
- self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
- else:
- self.fail("Failed to raise AttributeError")
+ suite = loader.loadTestsFromName('testcase_1.testfoo', m)
+ expected = "type object 'MyTestCase' has no attribute 'testfoo'"
+ error, test = self.check_deferred_error(loader, suite)
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(AttributeError, expected, test.testfoo)
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a ... TestSuite instance"
@@ -546,6 +735,23 @@ class Test_TestLoader(unittest.TestCase):
### Tests for TestLoader.loadTestsFromNames()
################################################################
+ def check_deferred_error(self, loader, suite):
+ """Helper function for checking that errors in loading are reported.
+
+ :param loader: A loader with some errors.
+ :param suite: A suite that should have a late bound error.
+ :return: The first error message from the loader and the test object
+ from the suite.
+ """
+ self.assertIsInstance(suite, unittest.TestSuite)
+ self.assertEqual(suite.countTestCases(), 1)
+ # Errors loading the suite are also captured for introspection.
+ self.assertNotEqual([], loader.errors)
+ self.assertEqual(1, len(loader.errors))
+ error = loader.errors[0]
+ test = list(suite)[0]
+ return error, test
+
# "Similar to loadTestsFromName(), but takes a sequence of names rather
# than a single name."
#
@@ -598,14 +804,15 @@ class Test_TestLoader(unittest.TestCase):
loader = unittest.TestLoader()
# XXX Should this raise ValueError or ImportError?
- try:
- loader.loadTestsFromNames(['abc () //'])
- except ValueError:
- pass
- except ImportError:
- pass
- else:
- self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
+ 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 \(\) //"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(
+ ImportError, expected_regex, getattr(test, 'abc () //'))
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
@@ -616,12 +823,13 @@ class Test_TestLoader(unittest.TestCase):
def test_loadTestsFromNames__unknown_module_name(self):
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromNames(['sdasfasfasdf'])
- except ImportError as e:
- self.assertEqual(str(e), "No module named 'sdasfasfasdf'")
- else:
- self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
+ suite = loader.loadTestsFromNames(['sdasfasfasdf'])
+ error, test = self.check_deferred_error(loader, list(suite)[0])
+ expected = "Failed to import test module: sdasfasfasdf"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
@@ -632,12 +840,14 @@ class Test_TestLoader(unittest.TestCase):
def test_loadTestsFromNames__unknown_attr_name(self):
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])
- except AttributeError as e:
- self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
- else:
- self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
+ suite = loader.loadTestsFromNames(
+ ['unittest.loader.sdasfasfasdf', 'unittest.test.dummy'])
+ error, test = self.check_deferred_error(loader, list(suite)[0])
+ expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
@@ -651,12 +861,13 @@ class Test_TestLoader(unittest.TestCase):
def test_loadTestsFromNames__unknown_name_relative_1(self):
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
- except AttributeError as e:
- self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
- else:
- self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
+ suite = loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
+ error, test = self.check_deferred_error(loader, list(suite)[0])
+ expected = "module 'unittest' has no attribute 'sdasfasfasdf'"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
@@ -670,12 +881,13 @@ class Test_TestLoader(unittest.TestCase):
def test_loadTestsFromNames__unknown_name_relative_2(self):
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
- except AttributeError as e:
- self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
- else:
- self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
+ suite = loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
+ error, test = self.check_deferred_error(loader, list(suite)[1])
+ expected = "module 'unittest' has no attribute 'sdasfasfasdf'"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
@@ -691,12 +903,13 @@ class Test_TestLoader(unittest.TestCase):
def test_loadTestsFromNames__relative_empty_name(self):
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromNames([''], unittest)
- except AttributeError:
- pass
- else:
- self.fail("Failed to raise ValueError")
+ suite = loader.loadTestsFromNames([''], unittest)
+ error, test = self.check_deferred_error(loader, list(suite)[0])
+ expected = "has no attribute ''"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(AttributeError, expected, getattr(test, ''))
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
@@ -710,14 +923,15 @@ class Test_TestLoader(unittest.TestCase):
loader = unittest.TestLoader()
# XXX Should this raise AttributeError or ValueError?
- try:
- loader.loadTestsFromNames(['abc () //'], unittest)
- except AttributeError:
- pass
- except ValueError:
- pass
- else:
- self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
+ 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 \(\) //'"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(
+ AttributeError, expected_regex, getattr(test, 'abc () //'))
# "The method optionally resolves name relative to the given module"
#
@@ -835,12 +1049,13 @@ class Test_TestLoader(unittest.TestCase):
m.testcase_1 = MyTestCase
loader = unittest.TestLoader()
- try:
- loader.loadTestsFromNames(['testcase_1.testfoo'], m)
- except AttributeError as e:
- self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
- else:
- self.fail("Failed to raise AttributeError")
+ suite = loader.loadTestsFromNames(['testcase_1.testfoo'], m)
+ error, test = self.check_deferred_error(loader, list(suite)[0])
+ expected = "type object 'MyTestCase' has no attribute 'testfoo'"
+ self.assertIn(
+ expected, error,
+ 'missing error string in %r' % error)
+ self.assertRaisesRegex(AttributeError, expected, test.testfoo)
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a ... TestSuite instance"
diff --git a/Lib/unittest/test/test_setups.py b/Lib/unittest/test/test_setups.py
index 392f95e..2df703e 100644
--- a/Lib/unittest/test/test_setups.py
+++ b/Lib/unittest/test/test_setups.py
@@ -111,7 +111,7 @@ class TestSetups(unittest.TestCase):
self.assertEqual(len(result.errors), 1)
error, _ = result.errors[0]
self.assertEqual(str(error),
- 'setUpClass (%s.BrokenTest)' % __name__)
+ 'setUpClass (%s.%s)' % (__name__, BrokenTest.__qualname__))
def test_error_in_teardown_class(self):
class Test(unittest.TestCase):
@@ -144,7 +144,7 @@ class TestSetups(unittest.TestCase):
error, _ = result.errors[0]
self.assertEqual(str(error),
- 'tearDownClass (%s.Test)' % __name__)
+ 'tearDownClass (%s.%s)' % (__name__, Test.__qualname__))
def test_class_not_torndown_when_setup_fails(self):
class Test(unittest.TestCase):
@@ -414,7 +414,8 @@ class TestSetups(unittest.TestCase):
self.assertEqual(len(result.errors), 0)
self.assertEqual(len(result.skipped), 1)
skipped = result.skipped[0][0]
- self.assertEqual(str(skipped), 'setUpClass (%s.Test)' % __name__)
+ self.assertEqual(str(skipped),
+ 'setUpClass (%s.%s)' % (__name__, Test.__qualname__))
def test_skiptest_in_setupmodule(self):
class Test(unittest.TestCase):
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 23675b9..3a104cb 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1187,6 +1187,42 @@ class MockTest(unittest.TestCase):
m = mock.create_autospec(object(), name='sweet_func')
self.assertIn('sweet_func', repr(m))
+ #Issue21238
+ def test_mock_unsafe(self):
+ m = Mock()
+ with self.assertRaises(AttributeError):
+ m.assert_foo_call()
+ with self.assertRaises(AttributeError):
+ m.assret_foo_call()
+ m = Mock(unsafe=True)
+ m.assert_foo_call()
+ m.assret_foo_call()
+
+ #Issue21262
+ def test_assert_not_called(self):
+ m = Mock()
+ m.hello.assert_not_called()
+ m.hello()
+ with self.assertRaises(AssertionError):
+ m.hello.assert_not_called()
+
+ #Issue21256 printout of keyword args should be in deterministic order
+ def test_sorted_call_signature(self):
+ m = Mock()
+ m.hello(name='hello', daddy='hero')
+ text = "call(daddy='hero', name='hello')"
+ self.assertEqual(repr(m.hello.call_args), text)
+
+ #Issue21270 overrides tuple methods for mock.call objects
+ def test_override_tuple_methods(self):
+ c = call.count()
+ i = call.index(132,'hello')
+ m = Mock()
+ m.count()
+ m.index(132,"hello")
+ self.assertEqual(m.method_calls[0], c)
+ self.assertEqual(m.method_calls[1], i)
+
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 b516f42..28fe86b 100644
--- a/Lib/unittest/test/testmock/testpatch.py
+++ b/Lib/unittest/test/testmock/testpatch.py
@@ -377,7 +377,7 @@ class PatchTest(unittest.TestCase):
def test_patchobject_wont_create_by_default(self):
try:
- @patch.object(SomeClass, 'frooble', sentinel.Frooble)
+ @patch.object(SomeClass, 'ord', sentinel.Frooble)
def test():
self.fail('Patching non existent attributes should fail')
@@ -386,7 +386,27 @@ class PatchTest(unittest.TestCase):
pass
else:
self.fail('Patching non existent attributes should fail')
- self.assertFalse(hasattr(SomeClass, 'frooble'))
+ self.assertFalse(hasattr(SomeClass, 'ord'))
+
+
+ def test_patch_builtins_without_create(self):
+ @patch(__name__+'.ord')
+ def test_ord(mock_ord):
+ mock_ord.return_value = 101
+ return ord('c')
+
+ @patch(__name__+'.open')
+ def test_open(mock_open):
+ m = mock_open.return_value
+ m.read.return_value = 'abcd'
+
+ fobj = open('doesnotexists.txt')
+ data = fobj.read()
+ fobj.close()
+ return data
+
+ self.assertEqual(test_ord(), 101)
+ self.assertEqual(test_open(), 'abcd')
def test_patch_with_static_methods(self):
diff --git a/Lib/unittest/util.py b/Lib/unittest/util.py
index aee498f..45485dc 100644
--- a/Lib/unittest/util.py
+++ b/Lib/unittest/util.py
@@ -52,7 +52,7 @@ def safe_repr(obj, short=False):
return result[:_MAX_LENGTH] + ' [truncated]...'
def strclass(cls):
- return "%s.%s" % (cls.__module__, cls.__name__)
+ return "%s.%s" % (cls.__module__, cls.__qualname__)
def sorted_list_difference(expected, actual):
"""Finds elements in only one or the other of two, sorted input lists.
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py
index d368331..821dae6 100644
--- a/Lib/urllib/parse.py
+++ b/Lib/urllib/parse.py
@@ -409,11 +409,13 @@ def urljoin(base, url, allow_fragments=True):
return url
if not url:
return base
+
base, url, _coerce_result = _coerce_args(base, url)
bscheme, bnetloc, bpath, bparams, bquery, bfragment = \
urlparse(base, '', allow_fragments)
scheme, netloc, path, params, query, fragment = \
urlparse(url, bscheme, allow_fragments)
+
if scheme != bscheme or scheme not in uses_relative:
return _coerce_result(url)
if scheme in uses_netloc:
@@ -421,9 +423,7 @@ def urljoin(base, url, allow_fragments=True):
return _coerce_result(urlunparse((scheme, netloc, path,
params, query, fragment)))
netloc = bnetloc
- if path[:1] == '/':
- return _coerce_result(urlunparse((scheme, netloc, path,
- params, query, fragment)))
+
if not path and not params:
path = bpath
params = bparams
@@ -431,29 +431,46 @@ def urljoin(base, url, allow_fragments=True):
query = bquery
return _coerce_result(urlunparse((scheme, netloc, path,
params, query, fragment)))
- segments = bpath.split('/')[:-1] + path.split('/')
- # XXX The stuff below is bogus in various ways...
- if segments[-1] == '.':
- segments[-1] = ''
- while '.' in segments:
- segments.remove('.')
- while 1:
- i = 1
- n = len(segments) - 1
- while i < n:
- if (segments[i] == '..'
- and segments[i-1] not in ('', '..')):
- del segments[i-1:i+1]
- break
- i = i+1
+
+ base_parts = bpath.split('/')
+ if base_parts[-1] != '':
+ # the last item is not a directory, so will not be taken into account
+ # in resolving the relative path
+ del base_parts[-1]
+
+ # for rfc3986, ignore all base path should the first character be root.
+ if path[:1] == '/':
+ segments = path.split('/')
+ else:
+ segments = base_parts + path.split('/')
+ # filter out elements that would cause redundant slashes on re-joining
+ # the resolved_path
+ segments = segments[0:1] + [
+ s for s in segments[1:-1] if len(s) > 0] + segments[-1:]
+
+ resolved_path = []
+
+ for seg in segments:
+ if seg == '..':
+ try:
+ resolved_path.pop()
+ except IndexError:
+ # ignore any .. segments that would otherwise cause an IndexError
+ # when popped from resolved_path if resolving for rfc3986
+ pass
+ elif seg == '.':
+ continue
else:
- break
- if segments == ['', '..']:
- segments[-1] = ''
- elif len(segments) >= 2 and segments[-1] == '..':
- segments[-2:] = ['']
- return _coerce_result(urlunparse((scheme, netloc, '/'.join(segments),
- params, query, fragment)))
+ resolved_path.append(seg)
+
+ if segments[-1] in ('.', '..'):
+ # do some post-processing here. if the last segment was a relative dir,
+ # then we need to append the trailing '/'
+ resolved_path.append('')
+
+ return _coerce_result(urlunparse((scheme, netloc, '/'.join(
+ resolved_path) or '/', params, query, fragment)))
+
def urldefrag(url):
"""Removes any existing fragment from URL.
@@ -641,7 +658,7 @@ class Quoter(collections.defaultdict):
def __repr__(self):
# Without this, will just display as a defaultdict
- return "<Quoter %r>" % dict(self)
+ return "<%s %r>" % (self.__class__.__name__, dict(self))
def __missing__(self, b):
# Handle a cache miss. Store quoted string in cache and return.
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index 6da9007..3ba7c01 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -916,6 +916,21 @@ class ProxyBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler):
return response
+class HTTPBasicPriorAuthHandler(HTTPBasicAuthHandler):
+ handler_order = 400
+
+ def http_request(self, req):
+ if not req.has_header('Authorization'):
+ user, passwd = self.passwd.find_user_password(None, req.host)
+ credentials = '{0}:{1}'.format(user, passwd).encode()
+ auth_str = base64.standard_b64encode(credentials).decode()
+ req.add_unredirected_header('Authorization',
+ 'Basic {}'.format(auth_str.strip()))
+ return req
+
+ https_request = http_request
+
+
# Return n random bytes.
_randombytes = os.urandom
diff --git a/Lib/urllib/robotparser.py b/Lib/urllib/robotparser.py
index 1d7b751..4fbb0cb 100644
--- a/Lib/urllib/robotparser.py
+++ b/Lib/urllib/robotparser.py
@@ -172,7 +172,7 @@ class RuleLine:
return self.path == "*" or filename.startswith(self.path)
def __str__(self):
- return (self.allowance and "Allow" or "Disallow") + ": " + self.path
+ return ("Allow" if self.allowance else "Disallow") + ": " + self.path
class Entry:
diff --git a/Lib/uuid.py b/Lib/uuid.py
index 1061bff..e627573 100644
--- a/Lib/uuid.py
+++ b/Lib/uuid.py
@@ -139,10 +139,8 @@ class UUID(object):
if bytes_le is not None:
if len(bytes_le) != 16:
raise ValueError('bytes_le is not a 16-char string')
- bytes = (bytes_(reversed(bytes_le[0:4])) +
- bytes_(reversed(bytes_le[4:6])) +
- bytes_(reversed(bytes_le[6:8])) +
- bytes_le[8:])
+ bytes = (bytes_le[4-1::-1] + bytes_le[6-1:4-1:-1] +
+ bytes_le[8-1:6-1:-1] + bytes_le[8:])
if bytes is not None:
if len(bytes) != 16:
raise ValueError('bytes is not a 16-char string')
@@ -187,11 +185,6 @@ class UUID(object):
return self.int == other.int
return NotImplemented
- def __ne__(self, other):
- if isinstance(other, UUID):
- return self.int != other.int
- return NotImplemented
-
# Q. What's the value of being able to sort UUIDs?
# A. Use them as keys in a B-Tree or similar mapping.
@@ -222,7 +215,7 @@ class UUID(object):
return self.int
def __repr__(self):
- return 'UUID(%r)' % str(self)
+ return '%s(%r)' % (self.__class__.__name__, str(self))
def __setattr__(self, name, value):
raise TypeError('UUID objects are immutable')
@@ -234,17 +227,12 @@ class UUID(object):
@property
def bytes(self):
- bytes = bytearray()
- for shift in range(0, 128, 8):
- bytes.insert(0, (self.int >> shift) & 0xff)
- return bytes_(bytes)
+ return self.int.to_bytes(16, 'big')
@property
def bytes_le(self):
bytes = self.bytes
- return (bytes_(reversed(bytes[0:4])) +
- bytes_(reversed(bytes[4:6])) +
- bytes_(reversed(bytes[6:8])) +
+ return (bytes[4-1::-1] + bytes[6-1:4-1:-1] + bytes[8-1:6-1:-1] +
bytes[8:])
@property
@@ -311,33 +299,38 @@ class UUID(object):
if self.variant == RFC_4122:
return int((self.int >> 76) & 0xf)
-def _popen(command, args):
- import os, shutil
+def _popen(command, *args):
+ import os, shutil, subprocess
executable = shutil.which(command)
if executable is None:
path = os.pathsep.join(('/sbin', '/usr/sbin'))
executable = shutil.which(command, path=path)
if executable is None:
return None
- # LC_ALL to ensure English output, 2>/dev/null to prevent output on
- # stderr (Note: we don't have an example where the words we search for
- # are actually localized, but in theory some system could do so.)
- cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args)
- return os.popen(cmd)
+ # LC_ALL=C to ensure English output, stderr=DEVNULL to prevent output
+ # on stderr (Note: we don't have an example where the words we search
+ # for are actually localized, but in theory some system could do so.)
+ env = dict(os.environ)
+ env['LC_ALL'] = 'C'
+ proc = subprocess.Popen((executable,) + args,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.DEVNULL,
+ env=env)
+ return proc
def _find_mac(command, args, hw_identifiers, get_index):
try:
- pipe = _popen(command, args)
- if not pipe:
+ proc = _popen(command, *args.split())
+ if not proc:
return
- with pipe:
- for line in pipe:
+ with proc:
+ for line in proc.stdout:
words = line.lower().rstrip().split()
for i in range(len(words)):
if words[i] in hw_identifiers:
try:
word = words[get_index(i)]
- mac = int(word.replace(':', ''), 16)
+ mac = int(word.replace(b':', b''), 16)
if mac:
return mac
except (ValueError, IndexError):
@@ -354,10 +347,17 @@ def _ifconfig_getnode():
"""Get the hardware address on Unix by running ifconfig."""
# This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
for args in ('', '-a', '-av'):
- mac = _find_mac('ifconfig', args, ['hwaddr', 'ether'], lambda i: i+1)
+ mac = _find_mac('ifconfig', args, [b'hwaddr', b'ether'], lambda i: i+1)
if mac:
return mac
+def _ip_getnode():
+ """Get the hardware address on Unix by running ip."""
+ # This works on Linux with iproute2.
+ mac = _find_mac('ip', 'link list', [b'link/ether'], lambda i: i+1)
+ if mac:
+ return mac
+
def _arp_getnode():
"""Get the hardware address on Unix by running arp."""
import os, socket
@@ -367,32 +367,32 @@ def _arp_getnode():
return None
# Try getting the MAC addr from arp based on our IP address (Solaris).
- return _find_mac('arp', '-an', [ip_addr], lambda i: -1)
+ return _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: -1)
def _lanscan_getnode():
"""Get the hardware address on Unix by running lanscan."""
# This might work on HP-UX.
- return _find_mac('lanscan', '-ai', ['lan0'], lambda i: 0)
+ return _find_mac('lanscan', '-ai', [b'lan0'], lambda i: 0)
def _netstat_getnode():
"""Get the hardware address on Unix by running netstat."""
# This might work on AIX, Tru64 UNIX and presumably on IRIX.
try:
- pipe = _popen('netstat', '-ia')
- if not pipe:
+ proc = _popen('netstat', '-ia')
+ if not proc:
return
- with pipe:
- words = pipe.readline().rstrip().split()
+ with proc:
+ words = proc.stdout.readline().rstrip().split()
try:
- i = words.index('Address')
+ i = words.index(b'Address')
except ValueError:
return
- for line in pipe:
+ for line in proc.stdout:
try:
words = line.rstrip().split()
word = words[i]
- if len(word) == 17 and word.count(':') == 5:
- mac = int(word.replace(':', ''), 16)
+ if len(word) == 17 and word.count(b':') == 5:
+ mac = int(word.replace(b':', b''), 16)
if mac:
return mac
except (ValueError, IndexError):
@@ -447,9 +447,10 @@ def _netbios_getnode():
if win32wnet.Netbios(ncb) != 0:
continue
status._unpack()
- bytes = status.adapter_address
- return ((bytes[0]<<40) + (bytes[1]<<32) + (bytes[2]<<24) +
- (bytes[3]<<16) + (bytes[4]<<8) + bytes[5])
+ bytes = status.adapter_address[:6]
+ if len(bytes) != 6:
+ continue
+ return int.from_bytes(bytes, 'big')
# Thanks to Thomas Heller for ctypes and for his help with its use here.
@@ -518,7 +519,7 @@ def _windll_getnode():
def _random_getnode():
"""Get a random node ID, with eighth bit set as suggested by RFC 4122."""
import random
- return random.randrange(0, 1<<48) | 0x010000000000
+ return random.getrandbits(48) | 0x010000000000
_node = None
@@ -539,8 +540,8 @@ def getnode():
if sys.platform == 'win32':
getters = [_windll_getnode, _netbios_getnode, _ipconfig_getnode]
else:
- getters = [_unixdll_getnode, _ifconfig_getnode, _arp_getnode,
- _lanscan_getnode, _netstat_getnode]
+ getters = [_unixdll_getnode, _ifconfig_getnode, _ip_getnode,
+ _arp_getnode, _lanscan_getnode, _netstat_getnode]
for getter in getters + [_random_getnode]:
try:
@@ -576,7 +577,7 @@ def uuid1(node=None, clock_seq=None):
_last_timestamp = timestamp
if clock_seq is None:
import random
- clock_seq = random.randrange(1<<14) # instead of stable storage
+ clock_seq = random.getrandbits(14) # instead of stable storage
time_low = timestamp & 0xffffffff
time_mid = (timestamp >> 32) & 0xffff
time_hi_version = (timestamp >> 48) & 0x0fff
@@ -608,8 +609,7 @@ def uuid4():
return UUID(bytes=os.urandom(16), version=4)
except:
import random
- bytes = bytes_(random.randrange(256) for i in range(16))
- return UUID(bytes=bytes, version=4)
+ return UUID(int=random.getrandbits(128), version=4)
def uuid5(namespace, name):
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
diff --git a/Lib/warnings.py b/Lib/warnings.py
index 70d087e..5ca4b9c 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -169,7 +169,9 @@ def warn(message, category=None, stacklevel=1):
# Check category argument
if category is None:
category = UserWarning
- assert issubclass(category, Warning)
+ if not (isinstance(category, type) and issubclass(category, Warning)):
+ raise TypeError("category must be a Warning subclass, "
+ "not '{:s}'".format(type(category).__name__))
# Get context information
try:
caller = sys._getframe(stacklevel)
diff --git a/Lib/weakref.py b/Lib/weakref.py
index 12bf975..a4ecadc 100644
--- a/Lib/weakref.py
+++ b/Lib/weakref.py
@@ -144,7 +144,7 @@ class WeakValueDictionary(collections.MutableMapping):
return o is not None
def __repr__(self):
- return "<WeakValueDictionary at %s>" % id(self)
+ return "<%s at %#x>" % (self.__class__.__name__, id(self))
def __setitem__(self, key, value):
if self._pending_removals:
@@ -359,7 +359,7 @@ class WeakKeyDictionary(collections.MutableMapping):
return len(self.data) - len(self._pending_removals)
def __repr__(self):
- return "<WeakKeyDictionary at %s>" % id(self)
+ return "<%s at %#x>" % (self.__class__.__name__, id(self))
def __setitem__(self, key, value):
self.data[ref(key, self._remove)] = value
diff --git a/Lib/wsgiref/headers.py b/Lib/wsgiref/headers.py
index d939628..7e670b3 100644
--- a/Lib/wsgiref/headers.py
+++ b/Lib/wsgiref/headers.py
@@ -26,10 +26,10 @@ def _formatparam(param, value=None, quote=1):
class Headers:
-
"""Manage a collection of HTTP response headers"""
- def __init__(self,headers):
+ def __init__(self, headers=None):
+ headers = headers if headers is not None else []
if type(headers) is not list:
raise TypeError("Headers must be a list of name/value tuples")
self._headers = headers
@@ -131,7 +131,7 @@ class Headers:
return self._headers[:]
def __repr__(self):
- return "Headers(%r)" % self._headers
+ return "%s(%r)" % (self.__class__.__name__, self._headers)
def __str__(self):
"""str() returns the formatted headers, complete with end line,
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
index c379a33..a5d813f 100644
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -545,9 +545,6 @@ class NamedNodeMap(object):
def __lt__(self, other):
return self._cmp(other) < 0
- def __ne__(self, other):
- return self._cmp(other) != 0
-
def __getitem__(self, attname_or_tuple):
if isinstance(attname_or_tuple, tuple):
return self._attrsNS[attname_or_tuple]
@@ -648,9 +645,10 @@ class TypeInfo(object):
def __repr__(self):
if self.namespace:
- return "<TypeInfo %r (from %r)>" % (self.name, self.namespace)
+ return "<%s %r (from %r)>" % (self.__class__.__name__, self.name,
+ self.namespace)
else:
- return "<TypeInfo %r>" % self.name
+ return "<%s %r>" % (self.__class__.__name__, self.name)
def _get_name(self):
return self.name
diff --git a/Lib/xml/etree/ElementPath.py b/Lib/xml/etree/ElementPath.py
index d914ddb..5de4232 100644
--- a/Lib/xml/etree/ElementPath.py
+++ b/Lib/xml/etree/ElementPath.py
@@ -114,7 +114,10 @@ def prepare_self(next, token):
return select
def prepare_descendant(next, token):
- token = next()
+ try:
+ token = next()
+ except StopIteration:
+ return
if token[0] == "*":
tag = "*"
elif not token[0]:
@@ -148,7 +151,10 @@ def prepare_predicate(next, token):
signature = []
predicate = []
while 1:
- token = next()
+ try:
+ token = next()
+ except StopIteration:
+ return
if token[0] == "]":
break
if token[0] and token[0][:1] in "'\"":
@@ -261,7 +267,10 @@ def iterfind(elem, path, namespaces=None):
if path[:1] == "/":
raise SyntaxError("cannot use absolute path on element")
next = iter(xpath_tokenizer(path, namespaces)).__next__
- token = next()
+ try:
+ token = next()
+ except StopIteration:
+ return
selector = []
while 1:
try:
@@ -286,10 +295,7 @@ def iterfind(elem, path, namespaces=None):
# Find first matching object.
def find(elem, path, namespaces=None):
- try:
- return next(iterfind(elem, path, namespaces))
- except StopIteration:
- return None
+ return next(iterfind(elem, path, namespaces), None)
##
# Find all matching objects.
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index a8585b6..4c109a2 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -174,7 +174,7 @@ class Element:
self._children = []
def __repr__(self):
- return "<Element %s at 0x%x>" % (repr(self.tag), id(self))
+ return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self))
def makeelement(self, tag, attrib):
"""Create a new element with the same type.
@@ -509,7 +509,7 @@ class QName:
def __str__(self):
return self.text
def __repr__(self):
- return '<QName %r>' % (self.text,)
+ return '<%s %r>' % (self.__class__.__name__, self.text)
def __hash__(self):
return hash(self.text)
def __le__(self, other):
@@ -532,10 +532,6 @@ class QName:
if isinstance(other, QName):
return self.text == other.text
return self.text == other
- def __ne__(self, other):
- if isinstance(other, QName):
- return self.text != other.text
- return self.text != other
# --------------------------------------------------------------------
diff --git a/Lib/xmlrpc/client.py b/Lib/xmlrpc/client.py
index e8c1944..047929a 100644
--- a/Lib/xmlrpc/client.py
+++ b/Lib/xmlrpc/client.py
@@ -208,8 +208,8 @@ class ProtocolError(Error):
self.headers = headers
def __repr__(self):
return (
- "<ProtocolError for %s: %s %s>" %
- (self.url, self.errcode, self.errmsg)
+ "<%s for %s: %s %s>" %
+ (self.__class__.__name__, self.url, self.errcode, self.errmsg)
)
##
@@ -237,7 +237,8 @@ class Fault(Error):
self.faultCode = faultCode
self.faultString = faultString
def __repr__(self):
- return "<Fault %s: %r>" % (self.faultCode, self.faultString)
+ return "<%s %s: %r>" % (self.__class__.__name__,
+ self.faultCode, self.faultString)
# --------------------------------------------------------------------
# Special values
@@ -339,10 +340,6 @@ class DateTime:
s, o = self.make_comparable(other)
return s == o
- def __ne__(self, other):
- s, o = self.make_comparable(other)
- return s != o
-
def timetuple(self):
return time.strptime(self.value, "%Y%m%dT%H:%M:%S")
@@ -355,7 +352,7 @@ class DateTime:
return self.value
def __repr__(self):
- return "<DateTime %r at %x>" % (self.value, id(self))
+ return "<%s %r at %#x>" % (self.__class__.__name__, self.value, id(self))
def decode(self, data):
self.value = str(data).strip()
@@ -406,11 +403,6 @@ class Binary:
other = other.data
return self.data == other
- def __ne__(self, other):
- if isinstance(other, Binary):
- other = other.data
- return self.data != other
-
def decode(self, data):
self.data = base64.decodebytes(data)
@@ -847,7 +839,7 @@ class MultiCall:
self.__call_list = []
def __repr__(self):
- return "<MultiCall at %x>" % id(self)
+ return "<%s at %#x>" % (self.__class__.__name__, id(self))
__str__ = __repr__
@@ -1444,8 +1436,8 @@ class ServerProxy:
def __repr__(self):
return (
- "<ServerProxy for %s%s>" %
- (self.__host, self.__handler)
+ "<%s for %s%s>" %
+ (self.__class__.__name__, self.__host, self.__handler)
)
__str__ = __repr__
@@ -1467,6 +1459,12 @@ class ServerProxy:
return self.__transport
raise AttributeError("Attribute %r not found" % (attr,))
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self.__close()
+
# compatibility
Server = ServerProxy
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index bda6134..8c2950f 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -13,6 +13,7 @@ import stat
import shutil
import struct
import binascii
+import threading
try:
@@ -355,6 +356,28 @@ class ZipInfo (object):
# compress_size Size of the compressed file
# file_size Size of the uncompressed file
+ def __repr__(self):
+ result = ['<%s filename=%r' % (self.__class__.__name__, self.filename)]
+ if self.compress_type != ZIP_STORED:
+ result.append(' compress_type=%s' %
+ compressor_names.get(self.compress_type,
+ self.compress_type))
+ hi = self.external_attr >> 16
+ lo = self.external_attr & 0xFFFF
+ if hi:
+ result.append(' filemode=%r' % stat.filemode(hi))
+ if lo:
+ result.append(' external_attr=%#x' % lo)
+ isdir = self.filename[-1:] == '/'
+ if not isdir or self.file_size:
+ result.append(' file_size=%r' % self.file_size)
+ if ((not isdir or self.compress_size) and
+ (self.compress_type != ZIP_STORED or
+ self.file_size != self.compress_size)):
+ result.append(' compress_size=%r' % self.compress_size)
+ result.append('>')
+ return ''.join(result)
+
def FileHeader(self, zip64=None):
"""Return the per-file header as a string."""
dt = self.date_time
@@ -624,6 +647,27 @@ def _get_decompressor(compress_type):
raise NotImplementedError("compression type %d" % (compress_type,))
+class _SharedFile:
+ def __init__(self, file, pos, close, lock):
+ self._file = file
+ self._pos = pos
+ self._close = close
+ self._lock = lock
+
+ def read(self, n=-1):
+ with self._lock:
+ self._file.seek(self._pos)
+ data = self._file.read(n)
+ self._pos = self._file.tell()
+ return data
+
+ def close(self):
+ if self._file is not None:
+ fileobj = self._file
+ self._file = None
+ self._close(fileobj)
+
+
class ZipExtFile(io.BufferedIOBase):
"""File-like object for reading an archive member.
Is returned by ZipFile.open().
@@ -671,6 +715,20 @@ class ZipExtFile(io.BufferedIOBase):
else:
self._expected_crc = None
+ def __repr__(self):
+ result = ['<%s.%s' % (self.__class__.__module__,
+ self.__class__.__qualname__)]
+ if not self.closed:
+ result.append(' name=%r mode=%r' % (self.name, self.mode))
+ if self._compress_type != ZIP_STORED:
+ result.append(' compress_type=%s' %
+ compressor_names.get(self._compress_type,
+ self._compress_type))
+ else:
+ result.append(' [closed]')
+ result.append('>')
+ return ''.join(result)
+
def readline(self, limit=-1):
"""Read and return a line from the stream.
@@ -909,7 +967,7 @@ class ZipFile:
self.NameToInfo = {} # Find file info given name
self.filelist = [] # List of ZipInfo instances for archive
self.compression = compression # Method of compression
- self.mode = key = mode.replace('b', '')[0]
+ self.mode = mode
self.pwd = None
self._comment = b''
@@ -918,28 +976,34 @@ class ZipFile:
# No, it's a filename
self._filePassed = 0
self.filename = file
- modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}
- try:
- self.fp = io.open(file, modeDict[mode])
- except OSError:
- if mode == 'a':
- mode = key = 'w'
- self.fp = io.open(file, modeDict[mode])
- else:
+ modeDict = {'r' : 'rb', 'w': 'w+b', 'a' : 'r+b',
+ 'r+b': 'w+b', 'w+b': 'wb'}
+ filemode = modeDict[mode]
+ while True:
+ try:
+ self.fp = io.open(file, filemode)
+ except OSError:
+ if filemode in modeDict:
+ filemode = modeDict[filemode]
+ continue
raise
+ break
else:
self._filePassed = 1
self.fp = file
self.filename = getattr(file, 'name', None)
+ self._fileRefCnt = 1
+ self._lock = threading.RLock()
try:
- if key == 'r':
+ if mode == 'r':
self._RealGetContents()
- elif key == 'w':
+ elif mode == 'w':
# set the modified flag so central directory gets written
# even if no files are added to the archive
self._didModify = True
- elif key == 'a':
+ self.start_dir = self.fp.tell()
+ elif mode == 'a':
try:
# See if file is a zip file
self._RealGetContents()
@@ -952,13 +1016,13 @@ class ZipFile:
# set the modified flag so central directory gets written
# even if no files are added to the archive
self._didModify = True
+ self.start_dir = self.fp.tell()
else:
raise RuntimeError('Mode must be "r", "w" or "a"')
except:
fp = self.fp
self.fp = None
- if not self._filePassed:
- fp.close()
+ self._fpclose(fp)
raise
def __enter__(self):
@@ -967,6 +1031,20 @@ class ZipFile:
def __exit__(self, type, value, traceback):
self.close()
+ def __repr__(self):
+ result = ['<%s.%s' % (self.__class__.__module__,
+ self.__class__.__qualname__)]
+ if self.fp is not None:
+ if self._filePassed:
+ result.append(' file=%r' % self.fp)
+ elif self.filename is not None:
+ result.append(' filename=%r' % self.filename)
+ result.append(' mode=%r' % self.mode)
+ else:
+ result.append(' [closed]')
+ result.append('>')
+ return ''.join(result)
+
def _RealGetContents(self):
"""Read in the table of contents for the ZIP file."""
fp = self.fp
@@ -1131,23 +1209,17 @@ class ZipFile:
raise RuntimeError(
"Attempt to read ZIP archive that was already closed")
- # Only open a new file for instances where we were not
- # given a file object in the constructor
- if self._filePassed:
- zef_file = self.fp
+ # Make sure we have an info object
+ if isinstance(name, ZipInfo):
+ # 'name' is already an info object
+ zinfo = name
else:
- zef_file = io.open(self.filename, 'rb')
+ # Get info object for name
+ zinfo = self.getinfo(name)
+ self._fileRefCnt += 1
+ zef_file = _SharedFile(self.fp, zinfo.header_offset, self._fpclose, self._lock)
try:
- # Make sure we have an info object
- if isinstance(name, ZipInfo):
- # 'name' is already an info object
- zinfo = name
- else:
- # Get info object for name
- zinfo = self.getinfo(name)
- zef_file.seek(zinfo.header_offset, 0)
-
# Skip the file header:
fheader = zef_file.read(sizeFileHeader)
if len(fheader) != sizeFileHeader:
@@ -1206,11 +1278,9 @@ class ZipFile:
if h[11] != check_byte:
raise RuntimeError("Bad password for file", name)
- return ZipExtFile(zef_file, mode, zinfo, zd,
- close_fileobj=not self._filePassed)
+ return ZipExtFile(zef_file, mode, zinfo, zd, True)
except:
- if not self._filePassed:
- zef_file.close()
+ zef_file.close()
raise
def extract(self, member, path=None, pwd=None):
@@ -1344,66 +1414,69 @@ class ZipFile:
zinfo.file_size = st.st_size
zinfo.flag_bits = 0x00
- 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
-
- 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
+ with self._lock:
+ self.fp.seek(self.start_dir, 0)
+ 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
+
+ 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)
+ 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) & 0xffffffff
+ 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 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, 0)
+ self.fp.write(zinfo.FileHeader(zip64))
+ self.fp.seek(self.start_dir, 0)
self.filelist.append(zinfo)
self.NameToInfo[zinfo.filename] = zinfo
- self.fp.write(zinfo.FileHeader(False))
- return
-
- cmpr = _get_compressor(zinfo.compress_type)
- 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) & 0xffffffff
- 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 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)
- position = self.fp.tell() # Preserve current position in file
- self.fp.seek(zinfo.header_offset, 0)
- self.fp.write(zinfo.FileHeader(zip64))
- self.fp.seek(position, 0)
- self.filelist.append(zinfo)
- self.NameToInfo[zinfo.filename] = zinfo
def writestr(self, zinfo_or_arcname, data, compress_type=None):
"""Write a file into the archive. The contents is 'data', which
@@ -1430,36 +1503,46 @@ class ZipFile:
"Attempt to write to ZIP archive that was already closed")
zinfo.file_size = len(data) # Uncompressed size
- 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) & 0xffffffff # 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.filelist.append(zinfo)
- self.NameToInfo[zinfo.filename] = zinfo
+ with self._lock:
+ try:
+ self.fp.seek(self.start_dir)
+ except (AttributeError, io.UnsupportedOperation):
+ # Some file-like objects can provide tell() but not seek()
+ pass
+ 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) & 0xffffffff # 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
def __del__(self):
"""Call the "close()" method in case the user forgot."""
@@ -1473,111 +1556,124 @@ class ZipFile:
try:
if self.mode in ("w", "a") and self._didModify: # write ending records
- pos1 = self.fp.tell()
- for zinfo in self.filelist: # write central directory
- dt = zinfo.date_time
- dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
- dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
- extra = []
- if zinfo.file_size > ZIP64_LIMIT \
- or zinfo.compress_size > ZIP64_LIMIT:
- extra.append(zinfo.file_size)
- extra.append(zinfo.compress_size)
- file_size = 0xffffffff
- compress_size = 0xffffffff
- else:
- file_size = zinfo.file_size
- compress_size = zinfo.compress_size
-
- if zinfo.header_offset > ZIP64_LIMIT:
- extra.append(zinfo.header_offset)
- header_offset = 0xffffffff
- else:
- header_offset = zinfo.header_offset
-
- extra_data = zinfo.extra
- min_version = 0
- if extra:
- # Append a ZIP64 field to the extra's
- extra_data = struct.pack(
- '<HH' + 'Q'*len(extra),
- 1, 8*len(extra), *extra) + extra_data
-
- min_version = ZIP64_VERSION
-
- if zinfo.compress_type == ZIP_BZIP2:
- min_version = max(BZIP2_VERSION, min_version)
- elif zinfo.compress_type == ZIP_LZMA:
- min_version = max(LZMA_VERSION, min_version)
-
- extract_version = max(min_version, zinfo.extract_version)
- create_version = max(min_version, zinfo.create_version)
+ with self._lock:
try:
- filename, flag_bits = zinfo._encodeFilenameFlags()
- centdir = struct.pack(structCentralDir,
- stringCentralDir, create_version,
- zinfo.create_system, extract_version, zinfo.reserved,
- flag_bits, zinfo.compress_type, dostime, dosdate,
- zinfo.CRC, compress_size, file_size,
- len(filename), len(extra_data), len(zinfo.comment),
- 0, zinfo.internal_attr, zinfo.external_attr,
- header_offset)
- except DeprecationWarning:
- print((structCentralDir, stringCentralDir, create_version,
- zinfo.create_system, extract_version, zinfo.reserved,
- zinfo.flag_bits, zinfo.compress_type, dostime, dosdate,
- zinfo.CRC, compress_size, file_size,
- len(zinfo.filename), len(extra_data), len(zinfo.comment),
- 0, zinfo.internal_attr, zinfo.external_attr,
- header_offset), file=sys.stderr)
- raise
- self.fp.write(centdir)
- self.fp.write(filename)
- self.fp.write(extra_data)
- self.fp.write(zinfo.comment)
-
- pos2 = self.fp.tell()
- # Write end-of-zip-archive record
- centDirCount = len(self.filelist)
- centDirSize = pos2 - pos1
- centDirOffset = pos1
- requires_zip64 = None
- if centDirCount > ZIP_FILECOUNT_LIMIT:
- requires_zip64 = "Files count"
- elif centDirOffset > ZIP64_LIMIT:
- requires_zip64 = "Central directory offset"
- elif centDirSize > ZIP64_LIMIT:
- requires_zip64 = "Central directory size"
- if requires_zip64:
- # Need to write the ZIP64 end-of-archive records
- if not self._allowZip64:
- raise LargeZipFile(requires_zip64 +
- " would require ZIP64 extensions")
- zip64endrec = struct.pack(
- structEndArchive64, stringEndArchive64,
- 44, 45, 45, 0, 0, centDirCount, centDirCount,
- centDirSize, centDirOffset)
- self.fp.write(zip64endrec)
-
- zip64locrec = struct.pack(
- structEndArchive64Locator,
- stringEndArchive64Locator, 0, pos2, 1)
- self.fp.write(zip64locrec)
- centDirCount = min(centDirCount, 0xFFFF)
- centDirSize = min(centDirSize, 0xFFFFFFFF)
- centDirOffset = min(centDirOffset, 0xFFFFFFFF)
-
- endrec = struct.pack(structEndArchive, stringEndArchive,
- 0, 0, centDirCount, centDirCount,
- centDirSize, centDirOffset, len(self._comment))
- self.fp.write(endrec)
- self.fp.write(self._comment)
- self.fp.flush()
+ self.fp.seek(self.start_dir)
+ except (AttributeError, io.UnsupportedOperation):
+ # Some file-like objects can provide tell() but not seek()
+ pass
+ self._write_end_record()
finally:
fp = self.fp
self.fp = None
- if not self._filePassed:
- fp.close()
+ self._fpclose(fp)
+
+ def _write_end_record(self):
+ for zinfo in self.filelist: # write central directory
+ dt = zinfo.date_time
+ dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
+ dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
+ extra = []
+ if zinfo.file_size > ZIP64_LIMIT \
+ or zinfo.compress_size > ZIP64_LIMIT:
+ extra.append(zinfo.file_size)
+ extra.append(zinfo.compress_size)
+ file_size = 0xffffffff
+ compress_size = 0xffffffff
+ else:
+ file_size = zinfo.file_size
+ compress_size = zinfo.compress_size
+
+ if zinfo.header_offset > ZIP64_LIMIT:
+ extra.append(zinfo.header_offset)
+ header_offset = 0xffffffff
+ else:
+ header_offset = zinfo.header_offset
+
+ extra_data = zinfo.extra
+ min_version = 0
+ if extra:
+ # Append a ZIP64 field to the extra's
+ extra_data = struct.pack(
+ '<HH' + 'Q'*len(extra),
+ 1, 8*len(extra), *extra) + extra_data
+
+ min_version = ZIP64_VERSION
+
+ if zinfo.compress_type == ZIP_BZIP2:
+ min_version = max(BZIP2_VERSION, min_version)
+ elif zinfo.compress_type == ZIP_LZMA:
+ min_version = max(LZMA_VERSION, min_version)
+
+ extract_version = max(min_version, zinfo.extract_version)
+ create_version = max(min_version, zinfo.create_version)
+ try:
+ filename, flag_bits = zinfo._encodeFilenameFlags()
+ centdir = struct.pack(structCentralDir,
+ stringCentralDir, create_version,
+ zinfo.create_system, extract_version, zinfo.reserved,
+ flag_bits, zinfo.compress_type, dostime, dosdate,
+ zinfo.CRC, compress_size, file_size,
+ len(filename), len(extra_data), len(zinfo.comment),
+ 0, zinfo.internal_attr, zinfo.external_attr,
+ header_offset)
+ except DeprecationWarning:
+ print((structCentralDir, stringCentralDir, create_version,
+ zinfo.create_system, extract_version, zinfo.reserved,
+ zinfo.flag_bits, zinfo.compress_type, dostime, dosdate,
+ zinfo.CRC, compress_size, file_size,
+ len(zinfo.filename), len(extra_data), len(zinfo.comment),
+ 0, zinfo.internal_attr, zinfo.external_attr,
+ header_offset), file=sys.stderr)
+ raise
+ self.fp.write(centdir)
+ self.fp.write(filename)
+ self.fp.write(extra_data)
+ self.fp.write(zinfo.comment)
+
+ pos2 = self.fp.tell()
+ # Write end-of-zip-archive record
+ centDirCount = len(self.filelist)
+ centDirSize = pos2 - self.start_dir
+ centDirOffset = self.start_dir
+ requires_zip64 = None
+ if centDirCount > ZIP_FILECOUNT_LIMIT:
+ requires_zip64 = "Files count"
+ elif centDirOffset > ZIP64_LIMIT:
+ requires_zip64 = "Central directory offset"
+ elif centDirSize > ZIP64_LIMIT:
+ requires_zip64 = "Central directory size"
+ if requires_zip64:
+ # Need to write the ZIP64 end-of-archive records
+ if not self._allowZip64:
+ raise LargeZipFile(requires_zip64 +
+ " would require ZIP64 extensions")
+ zip64endrec = struct.pack(
+ structEndArchive64, stringEndArchive64,
+ 44, 45, 45, 0, 0, centDirCount, centDirCount,
+ centDirSize, centDirOffset)
+ self.fp.write(zip64endrec)
+
+ zip64locrec = struct.pack(
+ structEndArchive64Locator,
+ stringEndArchive64Locator, 0, pos2, 1)
+ self.fp.write(zip64locrec)
+ centDirCount = min(centDirCount, 0xFFFF)
+ centDirSize = min(centDirSize, 0xFFFFFFFF)
+ centDirOffset = min(centDirOffset, 0xFFFFFFFF)
+
+ endrec = struct.pack(structEndArchive, stringEndArchive,
+ 0, 0, centDirCount, centDirCount,
+ centDirSize, centDirOffset, len(self._comment))
+ self.fp.write(endrec)
+ self.fp.write(self._comment)
+ self.fp.flush()
+
+ def _fpclose(self, fp):
+ assert self._fileRefCnt > 0
+ self._fileRefCnt -= 1
+ if not self._fileRefCnt and not self._filePassed:
+ fp.close()
class PyZipFile(ZipFile):
diff --git a/Mac/BuildScript/README.txt b/Mac/BuildScript/README.txt
index 8eb539c..3ced066 100644
--- a/Mac/BuildScript/README.txt
+++ b/Mac/BuildScript/README.txt
@@ -1,25 +1,15 @@
Building a Python Mac OS X distribution
=======================================
-The ``build-installer.py`` script creates Python distributions, including
+The ``build-install.py`` script creates Python distributions, including
certain third-party libraries as necessary. It builds a complete
framework-based Python out-of-tree, installs it in a funny place with
$DESTROOT, massages that installation to remove .pyc files and such, creates
an Installer package from the installation plus other files in ``resources``
and ``scripts`` and placed that on a ``.dmg`` disk image.
-This installers built by this script are legacy bundle installers that have
-been supported from the early days of OS X. In particular, they are supported
-on OS X 10.3.9, the earliest supported release for builds from this script.
-
-Beginning with Python 3.4.2, PSF practice is to build two installer variants
-using the newer flat package format, supported on 10.5+, and signed with the
-builder's Apple developer key, allowing downloaded packages to satisfy Apple's
-default Gatekeeper policy (e.g. starting with 10.8, Apple store downloads and
-Apple developer ID signed apps and installer packages). The process for
-transforming the output build artifacts into signed flat packages is not
-yet integrated into ``build-installer.py``. The steps prior to the flat
-package creation are the same as for 3.4.1 below.
+For Python 3.4.0, PSF practice is to build two installer variants
+for each release.
1. 32-bit-only, i386 and PPC universal, capable on running on all machines
supported by Mac OS X 10.5 through (at least) 10.9::
@@ -31,7 +21,6 @@ package creation are the same as for 3.4.1 below.
- builds the following third-party libraries
- * libcrypto and libssl from OpenSSL 1.0.1 (new, as of 3.4.3)
* NCurses 5.9 (http://bugs.python.org/issue15037)
* SQLite 3.8.3.1
* XZ 5.0.5
@@ -73,7 +62,6 @@ package creation are the same as for 3.4.1 below.
- uses system-supplied versions of third-party libraries
- * libcrypto and libssl from Apple OpenSSL 0.9.8
* readline module links with Apple BSD editline (libedit)
- requires ActiveState Tcl/Tk 8.5.15.1 (or later) to be installed for building
@@ -102,6 +90,47 @@ package creation are the same as for 3.4.1 below.
that the Xcode 3 gcc-4.2 compiler has had.
+* For Python 2.7.x and 3.2.x, the 32-bit-only installer was configured to
+ support Mac OS X 10.3.9 through (at least) 10.6. Because it is
+ believed that there are few systems still running OS X 10.3 or 10.4
+ and because it has become increasingly difficult to test and
+ support the differences in these earlier systems, as of Python 3.3.0 the PSF
+ 32-bit installer no longer supports them. For reference in building such
+ an installer yourself, the details are::
+
+ /usr/bin/python build-installer.py \
+ --sdk-path=/Developer/SDKs/MacOSX10.4u.sdk \
+ --universal-archs=32-bit \
+ --dep-target=10.3
+
+ - builds the following third-party libraries
+
+ * Bzip2
+ * NCurses
+ * GNU Readline (GPL)
+ * SQLite 3
+ * XZ
+ * Zlib 1.2.3
+ * Oracle Sleepycat DB 4.8 (Python 2.x only)
+
+ - requires ActiveState ``Tcl/Tk 8.4`` (currently 8.4.20) to be installed for building
+
+ - recommended build environment:
+
+ * Mac OS X 10.5.8 PPC or Intel
+ * Xcode 3.1.4 (or later)
+ * ``MacOSX10.4u`` SDK (later SDKs do not support PPC G3 processors)
+ * ``MACOSX_DEPLOYMENT_TARGET=10.3``
+ * Apple ``gcc-4.0``
+ * system Python 2.5 for documentation build with Sphinx
+
+ - alternate build environments:
+
+ * Mac OS X 10.6.8 with Xcode 3.2.6
+ - need to change ``/System/Library/Frameworks/{Tcl,Tk}.framework/Version/Current`` to ``8.4``
+
+
+
General Prerequisites
---------------------
diff --git a/Mac/BuildScript/resources/ReadMe.rtf b/Mac/BuildScript/resources/ReadMe.rtf
index 1bbdc1a..e240448 100644
--- a/Mac/BuildScript/resources/ReadMe.rtf
+++ b/Mac/BuildScript/resources/ReadMe.rtf
@@ -36,119 +36,6 @@ To use IDLE or other programs that use the Tkinter graphical user interface tool
\i0 for this version of Python and of Mac OS X.\
\b \ul \
-Installing on OS X 10.8 (Mountain Lion) or later systems\
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
-\cf0 \ulnone [CHANGED for Python 3.4.2]
-\b0 \
-\
-As of Python 3.4.2, installer packages from python.org are now compatible with the Gatekeeper security feature introduced in OS X 10.8. Downloaded packages can now be directly installed by double-clicking with the default system security settings. Python.org installer packages for OS X are signed with the Developer ID of the builder, as identified on {\field{\*\fldinst{HYPERLINK "https://www.python.org/downloads/"}}{\fldrslt the download page}} for this release. To inspect the digital signature of the package, click on the lock icon in the upper right corner of the
-\i Install Python
-\i0 installer window. Refer to Apple\'92s support pages for {\field{\*\fldinst{HYPERLINK "http://support.apple.com/kb/ht5290"}}{\fldrslt more information on Gatekeeper}}.\
-\
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
-
-\b \cf0 \ul Simplified web-based installs\
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
-\cf0 \ulnone [NEW for Python 3.4.2]
-\b0 \
-\
-With the change to the newer flat format installer package, the download file now has a
-\f1 .pkg
-\f0 extension as it is no longer necessary to embed the installer within a disk image (
-\f1 .dmg
-\f0 ) container. If you download the Python installer through a web browser, the OS X installer application may open automatically to allow you to perform the install. If your browser settings do not allow automatic open, double click on the downloaded installer file.\
-\
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
-
-\b \cf0 \ul New Installation Options and Defaults\
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
-\cf0 \ulnone [NEW for Python 3.4.0]
-\b0 \
-\
-The Python installer now includes an option to automatically install or upgrade
-\f1 pip
-\f0 , a tool for installing and managing Python packages. This option is enabled by default and no Internet access is required. If you do not want the installer to do this, select the
-\i Customize
-\i0 option at the
-\i Installation Type
-\i0 step and uncheck the
-\i Install or ugprade pip
-\i0 option.\
-\
-To make it easier to use scripts installed by third-party Python packages, with
-\f1 pip
-\f0 or by other means, the
-\i Shell profile updater
-\i0 option is now enabled by default, as has been the case with Python 2.7.x installers. You can also turn this option off by selecting
-\i Customize
-\i0 and unchecking the
-\i Shell profile updater
-\i0 option. You can also update your shell profile later by launching the
-\i Update Shell Profile
-\i0 command found in the
-\f1 /Applications/Python $VERSION
-\f0 folder. You may need to start a new terminal window for the changes to take effect.\
-\
-For other changes in this release, see the Release Notes link for this release 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 Certificate verification and OpenSSL\
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
-\cf0 \ulnone [CHANGED for Python 3.4.3]
-\b0 \
-\
-Python 3.4.3 includes a number of network security enhancements that have been approved for inclusion in Python 3.4 maintenance releases. {\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, the 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
-\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, as of 3.4.3 the
-\i 10.5+ 32-bit-only python.org variant
-\i0 is linked with a private copy of
-\i OpenSSL 1.0.1
-\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, for 3.4.3 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 .\
-\
-The bundled
-\f1 pip
-\f0 included with 3.4.3 has its own default certificate store for verifying download connections.\
-\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
-
-\b \cf0 \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 2527787..dfb75d8 100644
--- a/Mac/BuildScript/resources/Welcome.rtf
+++ b/Mac/BuildScript/resources/Welcome.rtf
@@ -1,5 +1,5 @@
{\rtf1\ansi\ansicpg1252\cocoartf1343\cocoasubrtf160
-\cocoascreenfonts1{\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\fmodern\fcharset0 CourierNewPSMT;}
+\cocoascreenfonts1{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11905\paperh16837\margl1440\margr1440\vieww12200\viewh10880\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640
@@ -17,12 +17,6 @@
\b0 .\
\
-\b NEW for Python 3.4.3:
-\b0 3.4.3 includes network security enhancements that may require changes to your Python applications. See the
-\f1 ReadMe
-\f0 file and {\field{\*\fldinst{HYPERLINK "https://docs.python.org/3/whatsnew/3.4.html#changed-in-3-4-3"}}{\fldrslt the Python documentation}} for more information.\
-\
-
\b IMPORTANT:
\b0
\b IDLE
diff --git a/Mac/PythonLauncher/Info.plist.in b/Mac/PythonLauncher/Info.plist.in
index 42846ea..b4ade1d 100644
--- a/Mac/PythonLauncher/Info.plist.in
+++ b/Mac/PythonLauncher/Info.plist.in
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
@@ -38,7 +38,7 @@
</dict>
</array>
<key>CFBundleExecutable</key>
- <string>PythonLauncher</string>
+ <string>Python Launcher</string>
<key>CFBundleGetInfoString</key>
<string>%VERSION%, © 2001-2015 Python Software Foundation</string>
<key>CFBundleIconFile</key>
diff --git a/Mac/PythonLauncher/Makefile.in b/Mac/PythonLauncher/Makefile.in
index f05efdf..4c05f26 100644
--- a/Mac/PythonLauncher/Makefile.in
+++ b/Mac/PythonLauncher/Makefile.in
@@ -15,12 +15,10 @@ BUILDPYTHON= $(builddir)/python$(BUILDEXE)
PYTHONFRAMEWORK=@PYTHONFRAMEWORK@
# Deployment target selected during configure, to be checked
-# by distutils
+# by distutils
MACOSX_DEPLOYMENT_TARGET=@CONFIGURE_MACOSX_DEPLOYMENT_TARGET@
@EXPORT_MACOSX_DEPLOYMENT_TARGET@export MACOSX_DEPLOYMENT_TARGET
-BUNDLEBULDER=$(srcdir)/../Tools/bundlebuilder.py
-
PYTHONAPPSDIR=@FRAMEWORKINSTALLAPPSPREFIX@/$(PYTHONFRAMEWORK) $(VERSION)
OBJECTS=FileSettings.o MyAppDelegate.o MyDocument.o PreferencesWindowController.o doscript.o main.o
@@ -30,10 +28,10 @@ install: Python\ Launcher.app
/bin/cp -r "Python Launcher.app" "$(DESTDIR)$(PYTHONAPPSDIR)"
touch "$(DESTDIR)$(PYTHONAPPSDIR)/Python Launcher.app"
-
clean:
rm -f *.o "Python Launcher"
rm -rf "Python Launcher.app"
+ rm -f Info.plist
Python\ Launcher.app: Info.plist \
Python\ Launcher $(srcdir)/../Icons/PythonLauncher.icns \
@@ -41,18 +39,17 @@ Python\ Launcher.app: Info.plist \
$(srcdir)/../Icons/PythonCompiled.icns \
$(srcdir)/factorySettings.plist
rm -fr "Python Launcher.app"
- $(RUNSHARED) $(BUILDPYTHON) $(BUNDLEBULDER) \
- --builddir=. \
- --name="Python Launcher" \
- --executable="Python Launcher" \
- --iconfile=$(srcdir)/../Icons/PythonLauncher.icns \
- --bundle-id=org.python.PythonLauncher \
- --resource=$(srcdir)/../Icons/PythonSource.icns \
- --resource=$(srcdir)/../Icons/PythonCompiled.icns \
- --resource=$(srcdir)/English.lproj \
- --resource=$(srcdir)/factorySettings.plist \
- --plist Info.plist \
- build
+ mkdir "Python Launcher.app"
+ mkdir "Python Launcher.app/Contents"
+ mkdir "Python Launcher.app/Contents/MacOS"
+ mkdir "Python Launcher.app/Contents/Resources"
+ cp "Python Launcher" "Python Launcher.app/Contents/MacOS"
+ cp Info.plist "Python Launcher.app/Contents"
+ cp $(srcdir)/../Icons/PythonLauncher.icns "Python Launcher.app/Contents/Resources"
+ cp $(srcdir)/../Icons/PythonSource.icns "Python Launcher.app/Contents/Resources"
+ cp $(srcdir)/../Icons/PythonCompiled.icns "Python Launcher.app/Contents/Resources"
+ cp $(srcdir)/factorySettings.plist "Python Launcher.app/Contents/Resources"
+ cp -R $(srcdir)/English.lproj "Python Launcher.app/Contents/Resources"
FileSettings.o: $(srcdir)/FileSettings.m
$(CC) $(CFLAGS) -o $@ -c $(srcdir)/FileSettings.m
diff --git a/Mac/Tools/bundlebuilder.py b/Mac/Tools/bundlebuilder.py
deleted file mode 100755
index f5679d3..0000000
--- a/Mac/Tools/bundlebuilder.py
+++ /dev/null
@@ -1,934 +0,0 @@
-#! /usr/bin/env python
-
-"""\
-bundlebuilder.py -- Tools to assemble MacOS X (application) bundles.
-
-This module contains two classes to build so called "bundles" for
-MacOS X. BundleBuilder is a general tool, AppBuilder is a subclass
-specialized in building application bundles.
-
-[Bundle|App]Builder objects are instantiated with a bunch of keyword
-arguments, and have a build() method that will do all the work. See
-the class doc strings for a description of the constructor arguments.
-
-The module contains a main program that can be used in two ways:
-
- % python bundlebuilder.py [options] build
- % python buildapp.py [options] build
-
-Where "buildapp.py" is a user-supplied setup.py-like script following
-this model:
-
- from bundlebuilder import buildapp
- buildapp(<lots-of-keyword-args>)
-
-"""
-
-
-__all__ = ["BundleBuilder", "BundleBuilderError", "AppBuilder", "buildapp"]
-
-
-import sys
-import os, errno, shutil
-import imp, marshal
-import re
-from copy import deepcopy
-import getopt
-from plistlib import Plist
-from types import FunctionType as function
-
-class BundleBuilderError(Exception): pass
-
-
-class Defaults:
-
- """Class attributes that don't start with an underscore and are
- not functions or classmethods are (deep)copied to self.__dict__.
- This allows for mutable default values.
- """
-
- def __init__(self, **kwargs):
- defaults = self._getDefaults()
- defaults.update(kwargs)
- self.__dict__.update(defaults)
-
- def _getDefaults(cls):
- defaults = {}
- for base in cls.__bases__:
- if hasattr(base, "_getDefaults"):
- defaults.update(base._getDefaults())
- for name, value in list(cls.__dict__.items()):
- if name[0] != "_" and not isinstance(value,
- (function, classmethod)):
- defaults[name] = deepcopy(value)
- return defaults
- _getDefaults = classmethod(_getDefaults)
-
-
-class BundleBuilder(Defaults):
-
- """BundleBuilder is a barebones class for assembling bundles. It
- knows nothing about executables or icons, it only copies files
- and creates the PkgInfo and Info.plist files.
- """
-
- # (Note that Defaults.__init__ (deep)copies these values to
- # instance variables. Mutable defaults are therefore safe.)
-
- # Name of the bundle, with or without extension.
- name = None
-
- # The property list ("plist")
- plist = Plist(CFBundleDevelopmentRegion = "English",
- CFBundleInfoDictionaryVersion = "6.0")
-
- # The type of the bundle.
- type = "BNDL"
- # The creator code of the bundle.
- creator = None
-
- # the CFBundleIdentifier (this is used for the preferences file name)
- bundle_id = None
-
- # List of files that have to be copied to <bundle>/Contents/Resources.
- resources = []
-
- # List of (src, dest) tuples; dest should be a path relative to the bundle
- # (eg. "Contents/Resources/MyStuff/SomeFile.ext).
- files = []
-
- # List of shared libraries (dylibs, Frameworks) to bundle with the app
- # will be placed in Contents/Frameworks
- libs = []
-
- # Directory where the bundle will be assembled.
- builddir = "build"
-
- # Make symlinks instead copying files. This is handy during debugging, but
- # makes the bundle non-distributable.
- symlink = 0
-
- # Verbosity level.
- verbosity = 1
-
- # Destination root directory
- destroot = ""
-
- def setup(self):
- # XXX rethink self.name munging, this is brittle.
- self.name, ext = os.path.splitext(self.name)
- if not ext:
- ext = ".bundle"
- bundleextension = ext
- # misc (derived) attributes
- self.bundlepath = pathjoin(self.builddir, self.name + bundleextension)
-
- plist = self.plist
- plist.CFBundleName = self.name
- plist.CFBundlePackageType = self.type
- if self.creator is None:
- if hasattr(plist, "CFBundleSignature"):
- self.creator = plist.CFBundleSignature
- else:
- self.creator = "????"
- plist.CFBundleSignature = self.creator
- if self.bundle_id:
- plist.CFBundleIdentifier = self.bundle_id
- elif not hasattr(plist, "CFBundleIdentifier"):
- plist.CFBundleIdentifier = self.name
-
- def build(self):
- """Build the bundle."""
- builddir = self.builddir
- if builddir and not os.path.exists(builddir):
- os.mkdir(builddir)
- self.message("Building %s" % repr(self.bundlepath), 1)
- if os.path.exists(self.bundlepath):
- shutil.rmtree(self.bundlepath)
- if os.path.exists(self.bundlepath + '~'):
- shutil.rmtree(self.bundlepath + '~')
- bp = self.bundlepath
-
- # Create the app bundle in a temporary location and then
- # rename the completed bundle. This way the Finder will
- # never see an incomplete bundle (where it might pick up
- # and cache the wrong meta data)
- self.bundlepath = bp + '~'
- try:
- os.mkdir(self.bundlepath)
- self.preProcess()
- self._copyFiles()
- self._addMetaFiles()
- self.postProcess()
- os.rename(self.bundlepath, bp)
- finally:
- self.bundlepath = bp
- self.message("Done.", 1)
-
- def preProcess(self):
- """Hook for subclasses."""
- pass
- def postProcess(self):
- """Hook for subclasses."""
- pass
-
- def _addMetaFiles(self):
- contents = pathjoin(self.bundlepath, "Contents")
- makedirs(contents)
- #
- # Write Contents/PkgInfo
- assert len(self.type) == len(self.creator) == 4, \
- "type and creator must be 4-byte strings."
- pkginfo = pathjoin(contents, "PkgInfo")
- f = open(pkginfo, "wb")
- f.write((self.type + self.creator).encode('latin1'))
- f.close()
- #
- # Write Contents/Info.plist
- infoplist = pathjoin(contents, "Info.plist")
- self.plist.write(infoplist)
-
- def _copyFiles(self):
- files = self.files[:]
- for path in self.resources:
- files.append((path, pathjoin("Contents", "Resources",
- os.path.basename(path))))
- for path in self.libs:
- files.append((path, pathjoin("Contents", "Frameworks",
- os.path.basename(path))))
- if self.symlink:
- self.message("Making symbolic links", 1)
- msg = "Making symlink from"
- else:
- self.message("Copying files", 1)
- msg = "Copying"
- files.sort()
- for src, dst in files:
- if os.path.isdir(src):
- self.message("%s %s/ to %s/" % (msg, src, dst), 2)
- else:
- self.message("%s %s to %s" % (msg, src, dst), 2)
- dst = pathjoin(self.bundlepath, dst)
- if self.symlink:
- symlink(src, dst, mkdirs=1)
- else:
- copy(src, dst, mkdirs=1)
-
- def message(self, msg, level=0):
- if level <= self.verbosity:
- indent = ""
- if level > 1:
- indent = (level - 1) * " "
- sys.stderr.write(indent + msg + "\n")
-
- def report(self):
- # XXX something decent
- pass
-
-
-if __debug__:
- PYC_EXT = ".pyc"
-else:
- PYC_EXT = ".pyo"
-
-MAGIC = imp.get_magic()
-USE_ZIPIMPORT = "zipimport" in sys.builtin_module_names
-
-# For standalone apps, we have our own minimal site.py. We don't need
-# all the cruft of the real site.py.
-SITE_PY = """\
-import sys
-if not %(semi_standalone)s:
- del sys.path[1:] # sys.path[0] is Contents/Resources/
-"""
-
-if USE_ZIPIMPORT:
- ZIP_ARCHIVE = "Modules.zip"
- SITE_PY += "sys.path.append(sys.path[0] + '/%s')\n" % ZIP_ARCHIVE
- def getPycData(fullname, code, ispkg):
- if ispkg:
- fullname += ".__init__"
- path = fullname.replace(".", os.sep) + PYC_EXT
- return path, MAGIC + '\0\0\0\0' + marshal.dumps(code)
-
-#
-# Extension modules can't be in the modules zip archive, so a placeholder
-# is added instead, that loads the extension from a specified location.
-#
-EXT_LOADER = """\
-def __load():
- import imp, sys, os
- for p in sys.path:
- path = os.path.join(p, "%(filename)s")
- if os.path.exists(path):
- break
- else:
- assert 0, "file not found: %(filename)s"
- mod = imp.load_dynamic("%(name)s", path)
-
-__load()
-del __load
-"""
-
-MAYMISS_MODULES = ['mac', 'nt', 'ntpath', 'dos', 'dospath',
- 'win32api', 'ce', '_winreg', 'nturl2path', 'sitecustomize',
- 'org.python.core', 'riscos', 'riscosenviron', 'riscospath'
-]
-
-STRIP_EXEC = "/usr/bin/strip"
-
-#
-# We're using a stock interpreter to run the app, yet we need
-# a way to pass the Python main program to the interpreter. The
-# bootstrapping script fires up the interpreter with the right
-# arguments. os.execve() is used as OSX doesn't like us to
-# start a real new process. Also, the executable name must match
-# the CFBundleExecutable value in the Info.plist, so we lie
-# deliberately with argv[0]. The actual Python executable is
-# passed in an environment variable so we can "repair"
-# sys.executable later.
-#
-BOOTSTRAP_SCRIPT = """\
-#!%(hashbang)s
-
-import sys, os
-execdir = os.path.dirname(sys.argv[0])
-executable = os.path.join(execdir, "%(executable)s")
-resdir = os.path.join(os.path.dirname(execdir), "Resources")
-libdir = os.path.join(os.path.dirname(execdir), "Frameworks")
-mainprogram = os.path.join(resdir, "%(mainprogram)s")
-
-sys.argv.insert(1, mainprogram)
-if %(standalone)s or %(semi_standalone)s:
- os.environ["PYTHONPATH"] = resdir
- if %(standalone)s:
- os.environ["PYTHONHOME"] = resdir
-else:
- pypath = os.getenv("PYTHONPATH", "")
- if pypath:
- pypath = ":" + pypath
- os.environ["PYTHONPATH"] = resdir + pypath
-os.environ["PYTHONEXECUTABLE"] = executable
-os.environ["DYLD_LIBRARY_PATH"] = libdir
-os.environ["DYLD_FRAMEWORK_PATH"] = libdir
-os.execve(executable, sys.argv, os.environ)
-"""
-
-
-#
-# Optional wrapper that converts "dropped files" into sys.argv values.
-#
-ARGV_EMULATOR = """\
-import argvemulator, os
-
-argvemulator.ArgvCollector().mainloop()
-execfile(os.path.join(os.path.split(__file__)[0], "%(realmainprogram)s"))
-"""
-
-#
-# When building a standalone app with Python.framework, we need to copy
-# a subset from Python.framework to the bundle. The following list
-# specifies exactly what items we'll copy.
-#
-PYTHONFRAMEWORKGOODIES = [
- "Python", # the Python core library
- "Resources/English.lproj",
- "Resources/Info.plist",
- "Resources/version.plist",
-]
-
-def isFramework():
- return sys.exec_prefix.find("Python.framework") > 0
-
-
-LIB = os.path.join(sys.prefix, "lib", "python" + sys.version[:3])
-SITE_PACKAGES = os.path.join(LIB, "site-packages")
-
-
-class AppBuilder(BundleBuilder):
-
- # Override type of the bundle.
- type = "APPL"
-
- # platform, name of the subfolder of Contents that contains the executable.
- platform = "MacOS"
-
- # A Python main program. If this argument is given, the main
- # executable in the bundle will be a small wrapper that invokes
- # the main program. (XXX Discuss why.)
- mainprogram = None
-
- # The main executable. If a Python main program is specified
- # the executable will be copied to Resources and be invoked
- # by the wrapper program mentioned above. Otherwise it will
- # simply be used as the main executable.
- executable = None
-
- # The name of the main nib, for Cocoa apps. *Must* be specified
- # when building a Cocoa app.
- nibname = None
-
- # The name of the icon file to be copied to Resources and used for
- # the Finder icon.
- iconfile = None
-
- # Symlink the executable instead of copying it.
- symlink_exec = 0
-
- # If True, build standalone app.
- standalone = 0
-
- # If True, build semi-standalone app (only includes third-party modules).
- semi_standalone = 0
-
- # If set, use this for #! lines in stead of sys.executable
- python = None
-
- # If True, add a real main program that emulates sys.argv before calling
- # mainprogram
- argv_emulation = 0
-
- # The following attributes are only used when building a standalone app.
-
- # Exclude these modules.
- excludeModules = []
-
- # Include these modules.
- includeModules = []
-
- # Include these packages.
- includePackages = []
-
- # Strip binaries from debug info.
- strip = 0
-
- # Found Python modules: [(name, codeobject, ispkg), ...]
- pymodules = []
-
- # Modules that modulefinder couldn't find:
- missingModules = []
- maybeMissingModules = []
-
- def setup(self):
- if ((self.standalone or self.semi_standalone)
- and self.mainprogram is None):
- raise BundleBuilderError("must specify 'mainprogram' when "
- "building a standalone application.")
- if self.mainprogram is None and self.executable is None:
- raise BundleBuilderError("must specify either or both of "
- "'executable' and 'mainprogram'")
-
- self.execdir = pathjoin("Contents", self.platform)
-
- if self.name is not None:
- pass
- elif self.mainprogram is not None:
- self.name = os.path.splitext(os.path.basename(self.mainprogram))[0]
- elif executable is not None:
- self.name = os.path.splitext(os.path.basename(self.executable))[0]
- if self.name[-4:] != ".app":
- self.name += ".app"
-
- if self.executable is None:
- if not self.standalone and not isFramework():
- self.symlink_exec = 1
- if self.python:
- self.executable = self.python
- else:
- self.executable = sys.executable
-
- if self.nibname:
- self.plist.NSMainNibFile = self.nibname
- if not hasattr(self.plist, "NSPrincipalClass"):
- self.plist.NSPrincipalClass = "NSApplication"
-
- if self.standalone and isFramework():
- self.addPythonFramework()
-
- BundleBuilder.setup(self)
-
- self.plist.CFBundleExecutable = self.name
-
- if self.standalone or self.semi_standalone:
- self.findDependencies()
-
- def preProcess(self):
- resdir = "Contents/Resources"
- if self.executable is not None:
- if self.mainprogram is None:
- execname = self.name
- else:
- execname = os.path.basename(self.executable)
- execpath = pathjoin(self.execdir, execname)
- if not self.symlink_exec:
- self.files.append((self.destroot + self.executable, execpath))
- self.execpath = execpath
-
- if self.mainprogram is not None:
- mainprogram = os.path.basename(self.mainprogram)
- self.files.append((self.mainprogram, pathjoin(resdir, mainprogram)))
- if self.argv_emulation:
- # Change the main program, and create the helper main program (which
- # does argv collection and then calls the real main).
- # Also update the included modules (if we're creating a standalone
- # program) and the plist
- realmainprogram = mainprogram
- mainprogram = '__argvemulator_' + mainprogram
- resdirpath = pathjoin(self.bundlepath, resdir)
- mainprogrampath = pathjoin(resdirpath, mainprogram)
- makedirs(resdirpath)
- open(mainprogrampath, "w").write(ARGV_EMULATOR % locals())
- if self.standalone or self.semi_standalone:
- self.includeModules.append("argvemulator")
- self.includeModules.append("os")
- if "CFBundleDocumentTypes" not in self.plist:
- self.plist["CFBundleDocumentTypes"] = [
- { "CFBundleTypeOSTypes" : [
- "****",
- "fold",
- "disk"],
- "CFBundleTypeRole": "Viewer"}]
- # Write bootstrap script
- executable = os.path.basename(self.executable)
- execdir = pathjoin(self.bundlepath, self.execdir)
- bootstrappath = pathjoin(execdir, self.name)
- makedirs(execdir)
- if self.standalone or self.semi_standalone:
- # XXX we're screwed when the end user has deleted
- # /usr/bin/python
- hashbang = "/usr/bin/python"
- elif self.python:
- hashbang = self.python
- else:
- hashbang = os.path.realpath(sys.executable)
- standalone = self.standalone
- semi_standalone = self.semi_standalone
- open(bootstrappath, "w").write(BOOTSTRAP_SCRIPT % locals())
- os.chmod(bootstrappath, 0o775)
-
- if self.iconfile is not None:
- iconbase = os.path.basename(self.iconfile)
- self.plist.CFBundleIconFile = iconbase
- self.files.append((self.iconfile, pathjoin(resdir, iconbase)))
-
- def postProcess(self):
- if self.standalone or self.semi_standalone:
- self.addPythonModules()
- if self.strip and not self.symlink:
- self.stripBinaries()
-
- if self.symlink_exec and self.executable:
- self.message("Symlinking executable %s to %s" % (self.executable,
- self.execpath), 2)
- dst = pathjoin(self.bundlepath, self.execpath)
- makedirs(os.path.dirname(dst))
- os.symlink(os.path.abspath(self.executable), dst)
-
- if self.missingModules or self.maybeMissingModules:
- self.reportMissing()
-
- def addPythonFramework(self):
- # If we're building a standalone app with Python.framework,
- # include a minimal subset of Python.framework, *unless*
- # Python.framework was specified manually in self.libs.
- for lib in self.libs:
- if os.path.basename(lib) == "Python.framework":
- # a Python.framework was specified as a library
- return
-
- frameworkpath = sys.exec_prefix[:sys.exec_prefix.find(
- "Python.framework") + len("Python.framework")]
-
- version = sys.version[:3]
- frameworkpath = pathjoin(frameworkpath, "Versions", version)
- destbase = pathjoin("Contents", "Frameworks", "Python.framework",
- "Versions", version)
- for item in PYTHONFRAMEWORKGOODIES:
- src = pathjoin(frameworkpath, item)
- dst = pathjoin(destbase, item)
- self.files.append((src, dst))
-
- def _getSiteCode(self):
- return compile(SITE_PY % {"semi_standalone": self.semi_standalone},
- "<-bundlebuilder.py->", "exec")
-
- def addPythonModules(self):
- self.message("Adding Python modules", 1)
-
- if USE_ZIPIMPORT:
- # Create a zip file containing all modules as pyc.
- import zipfile
- relpath = pathjoin("Contents", "Resources", ZIP_ARCHIVE)
- abspath = pathjoin(self.bundlepath, relpath)
- zf = zipfile.ZipFile(abspath, "w", zipfile.ZIP_DEFLATED)
- for name, code, ispkg in self.pymodules:
- self.message("Adding Python module %s" % name, 2)
- path, pyc = getPycData(name, code, ispkg)
- zf.writestr(path, pyc)
- zf.close()
- # add site.pyc
- sitepath = pathjoin(self.bundlepath, "Contents", "Resources",
- "site" + PYC_EXT)
- writePyc(self._getSiteCode(), sitepath)
- else:
- # Create individual .pyc files.
- for name, code, ispkg in self.pymodules:
- if ispkg:
- name += ".__init__"
- path = name.split(".")
- path = pathjoin("Contents", "Resources", *path) + PYC_EXT
-
- if ispkg:
- self.message("Adding Python package %s" % path, 2)
- else:
- self.message("Adding Python module %s" % path, 2)
-
- abspath = pathjoin(self.bundlepath, path)
- makedirs(os.path.dirname(abspath))
- writePyc(code, abspath)
-
- def stripBinaries(self):
- if not os.path.exists(STRIP_EXEC):
- self.message("Error: can't strip binaries: no strip program at "
- "%s" % STRIP_EXEC, 0)
- else:
- import stat
- self.message("Stripping binaries", 1)
- def walk(top):
- for name in os.listdir(top):
- path = pathjoin(top, name)
- if os.path.islink(path):
- continue
- if os.path.isdir(path):
- walk(path)
- else:
- mod = os.stat(path)[stat.ST_MODE]
- if not (mod & 0o100):
- continue
- relpath = path[len(self.bundlepath):]
- self.message("Stripping %s" % relpath, 2)
- inf, outf = os.popen4("%s -S \"%s\"" %
- (STRIP_EXEC, path))
- output = outf.read().strip()
- if output:
- # usually not a real problem, like when we're
- # trying to strip a script
- self.message("Problem stripping %s:" % relpath, 3)
- self.message(output, 3)
- walk(self.bundlepath)
-
- def findDependencies(self):
- self.message("Finding module dependencies", 1)
- import modulefinder
- mf = modulefinder.ModuleFinder(excludes=self.excludeModules)
- if USE_ZIPIMPORT:
- # zipimport imports zlib, must add it manually
- mf.import_hook("zlib")
- # manually add our own site.py
- site = mf.add_module("site")
- site.__code__ = self._getSiteCode()
- mf.scan_code(site.__code__, site)
-
- # warnings.py gets imported implicitly from C
- mf.import_hook("warnings")
-
- includeModules = self.includeModules[:]
- for name in self.includePackages:
- includeModules.extend(list(findPackageContents(name).keys()))
- for name in includeModules:
- try:
- mf.import_hook(name)
- except ImportError:
- self.missingModules.append(name)
-
- mf.run_script(self.mainprogram)
- modules = list(mf.modules.items())
- modules.sort()
- for name, mod in modules:
- path = mod.__file__
- if path and self.semi_standalone:
- # skip the standard library
- if path.startswith(LIB) and not path.startswith(SITE_PACKAGES):
- continue
- if path and mod.__code__ is None:
- # C extension
- filename = os.path.basename(path)
- pathitems = name.split(".")[:-1] + [filename]
- dstpath = pathjoin(*pathitems)
- if USE_ZIPIMPORT:
- if name != "zlib":
- # neatly pack all extension modules in a subdirectory,
- # except zlib, since it's necessary for bootstrapping.
- dstpath = pathjoin("ExtensionModules", dstpath)
- # Python modules are stored in a Zip archive, but put
- # extensions in Contents/Resources/. Add a tiny "loader"
- # program in the Zip archive. Due to Thomas Heller.
- source = EXT_LOADER % {"name": name, "filename": dstpath}
- code = compile(source, "<dynloader for %s>" % name, "exec")
- mod.__code__ = code
- self.files.append((path, pathjoin("Contents", "Resources", dstpath)))
- if mod.__code__ is not None:
- ispkg = mod.__path__ is not None
- if not USE_ZIPIMPORT or name != "site":
- # Our site.py is doing the bootstrapping, so we must
- # include a real .pyc file if USE_ZIPIMPORT is True.
- self.pymodules.append((name, mod.__code__, ispkg))
-
- if hasattr(mf, "any_missing_maybe"):
- missing, maybe = mf.any_missing_maybe()
- else:
- missing = mf.any_missing()
- maybe = []
- self.missingModules.extend(missing)
- self.maybeMissingModules.extend(maybe)
-
- def reportMissing(self):
- missing = [name for name in self.missingModules
- if name not in MAYMISS_MODULES]
- if self.maybeMissingModules:
- maybe = self.maybeMissingModules
- else:
- maybe = [name for name in missing if "." in name]
- missing = [name for name in missing if "." not in name]
- missing.sort()
- maybe.sort()
- if maybe:
- self.message("Warning: couldn't find the following submodules:", 1)
- self.message(" (Note that these could be false alarms -- "
- "it's not always", 1)
- self.message(" possible to distinguish between \"from package "
- "import submodule\" ", 1)
- self.message(" and \"from package import name\")", 1)
- for name in maybe:
- self.message(" ? " + name, 1)
- if missing:
- self.message("Warning: couldn't find the following modules:", 1)
- for name in missing:
- self.message(" ? " + name, 1)
-
- def report(self):
- # XXX something decent
- import pprint
- pprint.pprint(self.__dict__)
- if self.standalone or self.semi_standalone:
- self.reportMissing()
-
-#
-# Utilities.
-#
-
-SUFFIXES = [_suf for _suf, _mode, _tp in imp.get_suffixes()]
-identifierRE = re.compile(r"[_a-zA-z][_a-zA-Z0-9]*$")
-
-def findPackageContents(name, searchpath=None):
- head = name.split(".")[-1]
- if identifierRE.match(head) is None:
- return {}
- try:
- fp, path, (ext, mode, tp) = imp.find_module(head, searchpath)
- except ImportError:
- return {}
- modules = {name: None}
- if tp == imp.PKG_DIRECTORY and path:
- files = os.listdir(path)
- for sub in files:
- sub, ext = os.path.splitext(sub)
- fullname = name + "." + sub
- if sub != "__init__" and fullname not in modules:
- modules.update(findPackageContents(fullname, [path]))
- return modules
-
-def writePyc(code, path):
- f = open(path, "wb")
- f.write(MAGIC)
- f.write("\0" * 4) # don't bother about a time stamp
- marshal.dump(code, f)
- f.close()
-
-def copy(src, dst, mkdirs=0):
- """Copy a file or a directory."""
- if mkdirs:
- makedirs(os.path.dirname(dst))
- if os.path.isdir(src):
- shutil.copytree(src, dst, symlinks=1)
- else:
- shutil.copy2(src, dst)
-
-def copytodir(src, dstdir):
- """Copy a file or a directory to an existing directory."""
- dst = pathjoin(dstdir, os.path.basename(src))
- copy(src, dst)
-
-def makedirs(dir):
- """Make all directories leading up to 'dir' including the leaf
- directory. Don't moan if any path element already exists."""
- try:
- os.makedirs(dir)
- except OSError as why:
- if why.errno != errno.EEXIST:
- raise
-
-def symlink(src, dst, mkdirs=0):
- """Copy a file or a directory."""
- if not os.path.exists(src):
- raise IOError("No such file or directory: '%s'" % src)
- if mkdirs:
- makedirs(os.path.dirname(dst))
- os.symlink(os.path.abspath(src), dst)
-
-def pathjoin(*args):
- """Safe wrapper for os.path.join: asserts that all but the first
- argument are relative paths."""
- for seg in args[1:]:
- assert seg[0] != "/"
- return os.path.join(*args)
-
-
-cmdline_doc = """\
-Usage:
- python bundlebuilder.py [options] command
- python mybuildscript.py [options] command
-
-Commands:
- build build the application
- report print a report
-
-Options:
- -b, --builddir=DIR the build directory; defaults to "build"
- -n, --name=NAME application name
- -r, --resource=FILE extra file or folder to be copied to Resources
- -f, --file=SRC:DST extra file or folder to be copied into the bundle;
- DST must be a path relative to the bundle root
- -e, --executable=FILE the executable to be used
- -m, --mainprogram=FILE the Python main program
- -a, --argv add a wrapper main program to create sys.argv
- -p, --plist=FILE .plist file (default: generate one)
- --nib=NAME main nib name
- -c, --creator=CCCC 4-char creator code (default: '????')
- --iconfile=FILE filename of the icon (an .icns file) to be used
- as the Finder icon
- --bundle-id=ID the CFBundleIdentifier, in reverse-dns format
- (eg. org.python.BuildApplet; this is used for
- the preferences file name)
- -l, --link symlink files/folder instead of copying them
- --link-exec symlink the executable instead of copying it
- --standalone build a standalone application, which is fully
- independent of a Python installation
- --semi-standalone build a standalone application, which depends on
- an installed Python, yet includes all third-party
- modules.
- --python=FILE Python to use in #! line in stead of current Python
- --lib=FILE shared library or framework to be copied into
- the bundle
- -x, --exclude=MODULE exclude module (with --(semi-)standalone)
- -i, --include=MODULE include module (with --(semi-)standalone)
- --package=PACKAGE include a whole package (with --(semi-)standalone)
- --strip strip binaries (remove debug info)
- -v, --verbose increase verbosity level
- -q, --quiet decrease verbosity level
- -h, --help print this message
-"""
-
-def usage(msg=None):
- if msg:
- print(msg)
- print(cmdline_doc)
- sys.exit(1)
-
-def main(builder=None):
- if builder is None:
- builder = AppBuilder(verbosity=1)
-
- shortopts = "b:n:r:f:e:m:c:p:lx:i:hvqa"
- longopts = ("builddir=", "name=", "resource=", "file=", "executable=",
- "mainprogram=", "creator=", "nib=", "plist=", "link",
- "link-exec", "help", "verbose", "quiet", "argv", "standalone",
- "exclude=", "include=", "package=", "strip", "iconfile=",
- "lib=", "python=", "semi-standalone", "bundle-id=", "destroot=")
-
- try:
- options, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
- except getopt.error:
- usage()
-
- for opt, arg in options:
- if opt in ('-b', '--builddir'):
- builder.builddir = arg
- elif opt in ('-n', '--name'):
- builder.name = arg
- elif opt in ('-r', '--resource'):
- builder.resources.append(os.path.normpath(arg))
- elif opt in ('-f', '--file'):
- srcdst = arg.split(':')
- if len(srcdst) != 2:
- usage("-f or --file argument must be two paths, "
- "separated by a colon")
- builder.files.append(srcdst)
- elif opt in ('-e', '--executable'):
- builder.executable = arg
- elif opt in ('-m', '--mainprogram'):
- builder.mainprogram = arg
- elif opt in ('-a', '--argv'):
- builder.argv_emulation = 1
- elif opt in ('-c', '--creator'):
- builder.creator = arg
- elif opt == '--bundle-id':
- builder.bundle_id = arg
- elif opt == '--iconfile':
- builder.iconfile = arg
- elif opt == "--lib":
- builder.libs.append(os.path.normpath(arg))
- elif opt == "--nib":
- builder.nibname = arg
- elif opt in ('-p', '--plist'):
- builder.plist = Plist.fromFile(arg)
- elif opt in ('-l', '--link'):
- builder.symlink = 1
- elif opt == '--link-exec':
- builder.symlink_exec = 1
- elif opt in ('-h', '--help'):
- usage()
- elif opt in ('-v', '--verbose'):
- builder.verbosity += 1
- elif opt in ('-q', '--quiet'):
- builder.verbosity -= 1
- elif opt == '--standalone':
- builder.standalone = 1
- elif opt == '--semi-standalone':
- builder.semi_standalone = 1
- elif opt == '--python':
- builder.python = arg
- elif opt in ('-x', '--exclude'):
- builder.excludeModules.append(arg)
- elif opt in ('-i', '--include'):
- builder.includeModules.append(arg)
- elif opt == '--package':
- builder.includePackages.append(arg)
- elif opt == '--strip':
- builder.strip = 1
- elif opt == '--destroot':
- builder.destroot = arg
-
- if len(args) != 1:
- usage("Must specify one command ('build', 'report' or 'help')")
- command = args[0]
-
- if command == "build":
- builder.setup()
- builder.build()
- elif command == "report":
- builder.setup()
- builder.report()
- elif command == "help":
- usage()
- else:
- usage("Unknown command '%s'" % command)
-
-
-def buildapp(**kwargs):
- builder = AppBuilder(**kwargs)
- main(builder)
-
-
-if __name__ == "__main__":
- main()
diff --git a/Makefile.pre.in b/Makefile.pre.in
index 76aef28..d9ee777 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -328,6 +328,13 @@ PGENSRCS= $(PSRCS) $(PGSRCS)
PGENOBJS= $(POBJS) $(PGOBJS)
##########################################################################
+# opcode.h generation
+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= @OPCODEHGEN@ $(OPCODE_H_SCRIPT) $(srcdir)/Lib/opcode.py $(OPCODE_H)
+#
+##########################################################################
# AST
AST_H_DIR= Include
AST_H= $(AST_H_DIR)/Python-ast.h
@@ -382,6 +389,7 @@ PYTHON_OBJS= \
Python/pyctype.o \
Python/pyfpe.o \
Python/pyhash.o \
+ Python/pylifecycle.o \
Python/pymath.o \
Python/pystate.o \
Python/pythonrun.o \
@@ -465,7 +473,7 @@ LIBRARY_OBJS= \
# Default target
all: build_all
-build_all: $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks Modules/_testembed python-config
+build_all: $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks Programs/_testembed python-config
# Compile a binary with gcc profile guided optimization.
profile-opt:
@@ -538,8 +546,8 @@ clinic: $(BUILDPYTHON)
$(RUNSHARED) $(PYTHON_FOR_BUILD) ./Tools/clinic/clinic.py --make
# Build the interpreter
-$(BUILDPYTHON): Modules/python.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY)
- $(LINKCC) $(PY_LDFLAGS) $(LINKFORSHARED) -o $@ Modules/python.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
+$(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
@@ -674,18 +682,19 @@ Modules/Setup: $(srcdir)/Modules/Setup.dist
echo "-----------------------------------------------"; \
fi
-Modules/_testembed: Modules/_testembed.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY)
- $(LINKCC) $(PY_LDFLAGS) $(LINKFORSHARED) -o $@ Modules/_testembed.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
+Programs/_testembed: Programs/_testembed.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY)
+ $(LINKCC) $(PY_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/_testembed.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
############################################################################
# Importlib
-Modules/_freeze_importlib: Modules/_freeze_importlib.o $(LIBRARY_OBJS_OMIT_FROZEN)
- $(LINKCC) $(PY_LDFLAGS) -o $@ Modules/_freeze_importlib.o $(LIBRARY_OBJS_OMIT_FROZEN) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
+Programs/_freeze_importlib.o: Programs/_freeze_importlib.c
-Python/importlib.h: $(srcdir)/Lib/importlib/_bootstrap.py Modules/_freeze_importlib.c
- $(MAKE) Modules/_freeze_importlib
- ./Modules/_freeze_importlib \
+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.h: $(srcdir)/Lib/importlib/_bootstrap.py Programs/_freeze_importlib
+ ./Programs/_freeze_importlib \
$(srcdir)/Lib/importlib/_bootstrap.py Python/importlib.h
@@ -713,11 +722,11 @@ Modules/getpath.o: $(srcdir)/Modules/getpath.c Makefile
-DVPATH='"$(VPATH)"' \
-o $@ $(srcdir)/Modules/getpath.c
-Modules/python.o: $(srcdir)/Modules/python.c
- $(MAINCC) -c $(PY_CORE_CFLAGS) -o $@ $(srcdir)/Modules/python.c
+Programs/python.o: $(srcdir)/Programs/python.c
+ $(MAINCC) -c $(PY_CORE_CFLAGS) -o $@ $(srcdir)/Programs/python.c
-Modules/_testembed.o: $(srcdir)/Modules/_testembed.c
- $(MAINCC) -c $(PY_CORE_CFLAGS) -o $@ $(srcdir)/Modules/_testembed.c
+Programs/_testembed.o: $(srcdir)/Programs/_testembed.c
+ $(MAINCC) -c $(PY_CORE_CFLAGS) -o $@ $(srcdir)/Programs/_testembed.c
Modules/_sre.o: $(srcdir)/Modules/_sre.c $(srcdir)/Modules/sre.h $(srcdir)/Modules/sre_constants.h $(srcdir)/Modules/sre_lib.h
@@ -746,15 +755,13 @@ Python/sysmodule.o: $(srcdir)/Python/sysmodule.c Makefile
$(IO_OBJS): $(IO_H)
-$(GRAMMAR_H): $(GRAMMAR_INPUT) $(PGENSRCS)
+$(GRAMMAR_H): $(GRAMMAR_INPUT) $(PGEN)
@$(MKDIR_P) Include
- $(MAKE) $(PGEN)
$(PGEN) $(GRAMMAR_INPUT) $(GRAMMAR_H) $(GRAMMAR_C)
-$(GRAMMAR_C): $(GRAMMAR_H) $(GRAMMAR_INPUT) $(PGENSRCS)
- $(MAKE) $(GRAMMAR_H)
+$(GRAMMAR_C): $(GRAMMAR_H)
touch $(GRAMMAR_C)
-$(PGEN): $(PGENOBJS)
+$(PGEN): $(PGENOBJS)
$(CC) $(OPT) $(PY_LDFLAGS) $(PGENOBJS) $(LIBS) -o $(PGEN)
Parser/grammar.o: $(srcdir)/Parser/grammar.c \
@@ -776,6 +783,9 @@ $(AST_C): $(AST_H) $(AST_ASDL) $(ASDLGEN_FILES)
$(MKDIR_P) $(AST_C_DIR)
$(ASDLGEN) -c $(AST_C_DIR) $(AST_ASDL)
+$(OPCODE_H): $(srcdir)/Lib/opcode.py $(OPCODE_H_SCRIPT)
+ $(OPCODE_H_GEN)
+
Python/compile.o Python/symtable.o Python/ast.o: $(GRAMMAR_H) $(AST_H)
Python/getplatform.o: $(srcdir)/Python/getplatform.c
@@ -887,7 +897,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/node.h \
$(srcdir)/Include/object.h \
$(srcdir)/Include/objimpl.h \
- $(srcdir)/Include/opcode.h \
+ $(OPCODE_H) \
$(srcdir)/Include/osdefs.h \
$(srcdir)/Include/patchlevel.h \
$(srcdir)/Include/pgen.h \
@@ -900,6 +910,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/pyerrors.h \
$(srcdir)/Include/pyfpe.h \
$(srcdir)/Include/pyhash.h \
+ $(srcdir)/Include/pylifecycle.h \
$(srcdir)/Include/pymath.h \
$(srcdir)/Include/pygetopt.h \
$(srcdir)/Include/pymacro.h \
@@ -928,7 +939,7 @@ PYTHON_HEADERS= \
$(PARSER_HEADERS) \
$(AST_H)
-$(LIBRARY_OBJS) $(MODOBJS) Modules/python.o: $(PYTHON_HEADERS)
+$(LIBRARY_OBJS) $(MODOBJS) Programs/python.o: $(PYTHON_HEADERS)
######################################################################
@@ -1132,9 +1143,14 @@ LIBSUBDIRS= tkinter tkinter/test tkinter/test/test_tkinter \
test/audiodata \
test/capath test/data \
test/cjkencodings test/decimaltestdata test/xmltestdata \
+ test/eintrdata \
test/imghdrdata \
test/subprocessdata test/sndhdrdata test/support \
test/tracedmodules test/encoded_modules \
+ test/test_import \
+ test/test_import/data \
+ test/test_import/data/circular_imports \
+ test/test_import/data/circular_imports/subpkg \
test/test_importlib/namespace_pkgs \
test/test_importlib/namespace_pkgs/both_portions \
test/test_importlib/namespace_pkgs/both_portions/foo \
@@ -1338,7 +1354,7 @@ libainstall: all python-config
fi; \
fi
$(INSTALL_DATA) Modules/config.c $(DESTDIR)$(LIBPL)/config.c
- $(INSTALL_DATA) Modules/python.o $(DESTDIR)$(LIBPL)/python.o
+ $(INSTALL_DATA) Programs/python.o $(DESTDIR)$(LIBPL)/python.o
$(INSTALL_DATA) $(srcdir)/Modules/config.c.in $(DESTDIR)$(LIBPL)/config.c.in
$(INSTALL_DATA) Makefile $(DESTDIR)$(LIBPL)/Makefile
$(INSTALL_DATA) Modules/Setup $(DESTDIR)$(LIBPL)/Setup
@@ -1349,10 +1365,10 @@ libainstall: all python-config
$(INSTALL_SCRIPT) $(srcdir)/install-sh $(DESTDIR)$(LIBPL)/install-sh
$(INSTALL_SCRIPT) python-config.py $(DESTDIR)$(LIBPL)/python-config.py
$(INSTALL_SCRIPT) python-config $(DESTDIR)$(BINDIR)/python$(LDVERSION)-config
- @if [ -s Modules/python.exp -a \
+ @if [ -s Programs/python.exp -a \
"`echo $(MACHDEP) | sed 's/^\(...\).*/\1/'`" = "aix" ]; then \
echo; echo "Installing support files for building shared extension modules on AIX:"; \
- $(INSTALL_DATA) Modules/python.exp \
+ $(INSTALL_DATA) Programs/python.exp \
$(DESTDIR)$(LIBPL)/python.exp; \
echo; echo "$(LIBPL)/python.exp"; \
$(INSTALL_SCRIPT) $(srcdir)/Modules/makexp_aix \
@@ -1535,7 +1551,7 @@ clean: pycremoval
find build -name '*.py[co]' -exec rm -f {} ';' || true
-rm -f pybuilddir.txt
-rm -f Lib/lib2to3/*Grammar*.pickle
- -rm -f Modules/_testembed Modules/_freeze_importlib
+ -rm -f Programs/_testembed Programs/_freeze_importlib
profile-removal:
find . -name '*.gc??' -exec rm -f {} ';'
@@ -1559,7 +1575,7 @@ distclean: clobber
done
-rm -f core Makefile Makefile.pre config.status \
Modules/Setup Modules/Setup.local Modules/Setup.config \
- Modules/ld_so_aix Modules/python.exp Misc/python.pc
+ Modules/ld_so_aix Programs/python.exp Misc/python.pc
-rm -f python*-gdb.py
find $(srcdir)/[a-zA-Z]* '(' -name '*.fdc' -o -name '*~' \
-o -name '[@,#]*' -o -name '*.old' \
diff --git a/Misc/ACKS b/Misc/ACKS
index 65518db..c1d0dab 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -46,6 +46,7 @@ Pehr Anderson
Erik Andersén
Oliver Andrich
Ross Andrus
+Fabrice Aneche
Juancarlo Añez
Chris Angelico
Jérémy Anger
@@ -89,6 +90,7 @@ Richard Barran
Cesar Eduardo Barros
Des Barry
Ulf Bartelt
+Campbell Barton
Don Bashford
Pior Bastida
Nick Bastin
@@ -100,6 +102,7 @@ Mike Bayer
Samuel L. Bayer
Donald Beaudry
David Beazley
+Ingolf Becker
Neal Becker
Robin Becker
Torsten Becker
@@ -216,6 +219,7 @@ Pierre Carrier
Terry Carroll
Edward Catmur
Lorenzo M. Catucci
+Bruno Cauet
Donn Cave
Charles Cazabon
Jesús Cea Avión
@@ -233,6 +237,7 @@ Godefroid Chapelle
Brad Chapman
Greg Chapman
Mitch Chapman
+Matt Chaput
Yogesh Chaudhari
David Chaum
Nicolas Chauvat
@@ -469,6 +474,7 @@ Stephen M. Gava
Xavier de Gaye
Harry Henry Gebel
Marius Gedminas
+Jan-Philip Gehrcke
Thomas Gellekum
Gabriel Genellina
Christos Georgiou
@@ -495,6 +501,7 @@ Shelley Gooch
David Goodger
Hans de Graaff
Tim Graham
+Kim Gräsman
Nathaniel Gray
Eddy De Greef
Grant Griffin
@@ -567,6 +574,7 @@ Kevan Heydon
Wouter van Heyst
Kelsey Hightower
Jason Hildebrand
+Aaron Hill
Richie Hindle
Konrad Hinsen
David Hobley
@@ -601,6 +609,7 @@ Alan Hourihane
Ken Howard
Brad Howes
Mike Hoy
+Ben Hoyt
Chih-Hao Huang
Christian Hudon
Lawrence Hudson
@@ -620,6 +629,7 @@ Ali Ikinci
Aaron Iles
Lars Immisch
Bobby Impollonia
+Naoki Inada
Meador Inge
Peter Ingebretson
Tony Ingraldi
@@ -643,6 +653,7 @@ Juhana Jauhiainen
Rajagopalasarma Jayakrishnan
Zbigniew Jędrzejewski-Szmek
Julien Jehannet
+Muhammad Jehanzeb
Drew Jenkins
Flemming Kjær Jensen
Philip H. Jensen
@@ -662,6 +673,7 @@ Thomas Jollans
Nicolas Joly
Brian K. Jones
Evan Jones
+Glenn Jones
Jeremy Jones
Richard Jones
Irmen de Jong
@@ -689,6 +701,7 @@ Per Øyvind Karlsen
Anton Kasyanov
Lou Kates
Hiroaki Kawai
+Brian Kearns
Sebastien Keim
Ryan Kelly
Dan Kenigsberg
@@ -704,6 +717,7 @@ Jason Killen
Jan Kim
Taek Joo Kim
Sam Kimbrel
+James King
W. Trevor King
Paul Kippes
Steve Kirsch
@@ -811,6 +825,7 @@ Ross Light
Shawn Ligocki
Martin Ligr
Gediminas Liktaras
+Vitor de Lima
Grant Limberg
Christopher Lindblad
Ulf A. Lindgren
@@ -880,6 +895,7 @@ Madison May
Lucas Maystre
Arnaud Mazin
Matt McClure
+Jack McCracken
Rebecca McCreary
Kirk McDonald
Chris McDonough
@@ -986,6 +1002,7 @@ Nigel O'Brian
John O'Connor
Kevin O'Connor
Tim O'Malley
+Dan O'Reilly
Zooko O'Whielacronx
Aaron Oakley
James Oakley
@@ -1025,6 +1042,7 @@ Peter Parente
Alexandre Parenteau
Dan Parisien
William Park
+Claude Paroz
Heikki Partanen
Harri Pasanen
Gaël Pasgrimaud
@@ -1097,6 +1115,7 @@ Thomas Rachel
Ram Rachum
Jérôme Radix
Burton Radons
+Shorya Raj
Jeff Ramnani
Brodie Rao
Senko Rasic
@@ -1138,6 +1157,7 @@ Juan M. Bello Rivas
Davide Rizzo
Anthony Roach
Carl Robben
+Ben Roberts
Mark Roberts
Andy Robinson
Jim Robinson
@@ -1153,12 +1173,14 @@ Armin Ronacher
Case Roole
Timothy Roscoe
Erik Rose
+Josh Rosenberg
Jim Roskind
Brian Rosner
Guido van Rossum
Just van Rossum
Hugo van Rossum
Saskia van Rossum
+Clement Rouault
Donald Wallace Rouse II
Liam Routt
Todd Rovito
@@ -1182,6 +1204,7 @@ Sébastien Sablé
Suman Saha
Hajime Saitou
George Sakkis
+Victor Salgado
Rich Salz
Kevin Samborn
Adrian Sampson
@@ -1281,6 +1304,7 @@ Paul Sokolovsky
Evgeny Sologubov
Cody Somerville
Edoardo Spadolini
+Geoffrey Spear
Clay Spence
Stefan Sperling
Nicholas Spies
@@ -1338,6 +1362,7 @@ Steven Taschuk
Amy Taylor
Monty Taylor
Anatoly Techtonik
+Gustavo Temple
Mikhail Terekhov
Victor Terrón
Richard M. Tew
@@ -1370,6 +1395,7 @@ David Townshend
Nathan Trapuzzano
Laurence Tratt
Alberto Trevino
+Mayank Tripathi
Matthias Troffaes
Tom Tromey
John Tromp
@@ -1391,6 +1417,7 @@ Roger Upole
Daniel Urban
Michael Urman
Hector Urtubia
+Lukas Vacek
Ville Vainio
Andi Vajda
Case Van Horsen
@@ -1415,6 +1442,7 @@ Norman Vine
Pauli Virtanen
Frank Visser
Johannes Vogel
+Radu Voicilas
Alex Volkov
Martijn Vries
Sjoerd de Vries
@@ -1437,6 +1465,7 @@ Bob Watson
David Watson
Aaron Watters
Henrik Weber
+Leon Weber
Corran Webster
Glyn Webster
Phil Webster
@@ -1465,6 +1494,7 @@ Sue Williams
Carol Willing
Steven Willis
Frank Willison
+David Wilson
Geoff Wilson
Greg V. Wilson
J Derek Wilson
@@ -1490,6 +1520,7 @@ Gordon Worley
Darren Worrall
Thomas Wouters
Daniel Wozniak
+Wei Wu
Heiko Wundram
Doug Wyatt
Robert Xiao
diff --git a/Misc/HISTORY b/Misc/HISTORY
index 8370d8e..2b4760d 100644
--- a/Misc/HISTORY
+++ b/Misc/HISTORY
@@ -18,13 +18,13 @@ Core and Builtins
- Issue #16046: Fix loading sourceless legacy .pyo files.
-- Issue #16060: Fix refcounting bug when __trunc__ returns an object
- whose __int__ gives a non-integer. Patch by Serhiy Storchaka.
+- Issue #16060: Fix refcounting bug when `__trunc__()` returns an object whose
+ `__int__()` gives a non-integer. Patch by Serhiy Storchaka.
Extension Modules
-----------------
-- Issue #16012: Fix a regression in pyexpat. The parser's UseForeignDTD()
+- Issue #16012: Fix a regression in pyexpat. The parser's `UseForeignDTD()`
method doesn't require an argument again.
@@ -36,26 +36,26 @@ What's New in Python 3.3.0 Release Candidate 3?
Core and Builtins
-----------------
-- Issue #15900: Fix reference leak in PyUnicode_TranslateCharmap().
+- Issue #15900: Fix reference leak in `PyUnicode_TranslateCharmap()`.
- Issue #15926: Fix crash after multiple reinitializations of the interpreter.
- Issue #15895: Fix FILE pointer leak in one error branch of
- PyRun_SimpleFileExFlags() when filename points to a pyc/pyo file, closeit
- is false an and set_main_loader() fails.
+ `PyRun_SimpleFileExFlags()` when filename points to a pyc/pyo file, closeit is
+ false an and set_main_loader() fails.
- Fixes for a few crash and memory leak regressions found by Coverity.
Library
-------
-- Issue #15882: Change _decimal to accept any coefficient tuple when
- constructing infinities. This is done for backwards compatibility
- with decimal.py: Infinity coefficients are undefined in _decimal
- (in accordance with the specification).
+- Issue #15882: Change `_decimal` to accept any coefficient tuple when
+ constructing infinities. This is done for backwards compatibility with
+ decimal.py: Infinity coefficients are undefined in _decimal (in accordance
+ with the specification).
-- Issue #15925: Fix a regression in email.util where the parsedate() and
- parsedate_tz() functions did not return None anymore when the argument could
+- Issue #15925: Fix a regression in `email.util` where the `parsedate()` and
+ `parsedate_tz()` functions did not return None anymore when the argument could
not be parsed.
Extension Modules
@@ -67,7 +67,7 @@ Extension Modules
- Issue #15977: Fix memory leak in Modules/_ssl.c when the function
_set_npn_protocols() is called multiple times, thanks to Daniel Sommermann.
-- Issue #15969: faulthandler module: rename dump_tracebacks_later() to
+- Issue #15969: `faulthandler` module: rename dump_tracebacks_later() to
dump_traceback_later() and cancel_dump_tracebacks_later() to
cancel_dump_traceback_later().
@@ -83,35 +83,37 @@ Core and Builtins
-----------------
- Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
- sporadic crashes in multi-thread programs when several long deallocator
- chains ran concurrently and involved subclasses of built-in container
- types.
+ sporadic crashes in multi-thread programs when several long deallocator chains
+ ran concurrently and involved subclasses of built-in container types.
-- Issue #15784: Modify OSError.__str__() to better distinguish between
- errno error numbers and Windows error numbers.
+- Issue #15784: Modify `OSError`.__str__() to better distinguish between errno
+ error numbers and Windows error numbers.
- Issue #15781: Fix two small race conditions in import's module locking.
Library
-------
-- Issue #15847: Fix a regression in argparse, which did not accept tuples
- as argument lists anymore.
+- Issue #17158: Add 'symbols' to help() welcome message; clarify
+ 'modules spam' messages.
+
+- Issue #15847: Fix a regression in argparse, which did not accept tuples as
+ argument lists anymore.
-- Issue #15828: Restore support for C extensions in imp.load_module().
+- Issue #15828: Restore support for C extensions in `imp.load_module()`.
-- Issue #15340: Fix importing the random module when /dev/urandom cannot
- be opened. This was a regression caused by the hash randomization patch.
+- Issue #15340: Fix importing the random module when ``/dev/urandom`` cannot be
+ opened. This was a regression caused by the hash randomization patch.
-- Issue #10650: Deprecate the watchexp parameter of the Decimal.quantize()
+- Issue #10650: Deprecate the watchexp parameter of the `Decimal.quantize()`
method.
-- Issue #15785: Modify window.get_wch() API of the curses module: return
- a character for most keys, and an integer for special keys, instead of
- always returning an integer. So it is now possible to distinguish special
- keys like keypad keys.
+- Issue #15785: Modify `window.get_wch()` API of the curses module: return a
+ character for most keys, and an integer for special keys, instead of always
+ returning an integer. So it is now possible to distinguish special keys like
+ keypad keys.
-- Issue #14223: Fix window.addch() of the curses module for special characters
+- Issue #14223: Fix `window.addch()` of the curses module for special characters
like curses.ACS_HLINE: the Python function addch(int) and addch(bytes) is now
calling the C function waddch()/mvwaddch() (as it was done in Python 3.2),
instead of wadd_wch()/mvwadd_wch(). The Python function addch(str) is still
@@ -127,10 +129,10 @@ Build
Documentation
-------------
-- Issue #15814: The memoryview enhancements in 3.3.0 accidentally permitted
- the hashing of multi-dimensional memorviews and memoryviews with multi-byte
- item formats. The intended restrictions have now been documented - they
- will be correctly enforced in 3.3.1
+- Issue #15814: The memoryview enhancements in 3.3.0 accidentally permitted the
+ hashing of multi-dimensional memorviews and memoryviews with multi-byte item
+ formats. The intended restrictions have now been documented - they will be
+ correctly enforced in 3.3.1.
What's New in Python 3.3.0 Release Candidate 1?
@@ -144,131 +146,123 @@ Core and Builtins
- Issue #15573: memoryview comparisons are now performed by value with full
support for any valid struct module format definition.
-- Issue #15316: When an item in the fromlist for __import__ doesn't exist,
+- Issue #15316: When an item in the fromlist for `__import__()` doesn't exist,
don't raise an error, but if an exception is raised as part of an import do
let that propagate.
-- Issue #15778: ensure that str(ImportError(msg)) returns a str
- even when msg isn't a str.
+- Issue #15778: Ensure that ``str(ImportError(msg))`` returns a str even when
+ msg isn't a str.
-- Issue #2051: Source file permission bits are once again correctly
- copied to the cached bytecode file. (The migration to importlib
- reintroduced this problem because these was no regression test. A test
- has been added as part of this patch)
+- Issue #2051: Source file permission bits are once again correctly copied to
+ the cached bytecode file. (The migration to importlib reintroduced this
+ problem because these was no regression test. A test has been added as part of
+ this patch)
-- Issue #15761: Fix crash when PYTHONEXECUTABLE is set on Mac OS X.
+- Issue #15761: Fix crash when ``PYTHONEXECUTABLE`` is set on Mac OS X.
-- Issue #15726: Fix incorrect bounds checking in PyState_FindModule.
- Patch by Robin Schreiber.
+- Issue #15726: Fix incorrect bounds checking in PyState_FindModule. Patch by
+ Robin Schreiber.
-- Issue #15604: Update uses of PyObject_IsTrue() to check for and handle
+- Issue #15604: Update uses of `PyObject_IsTrue()` to check for and handle
errors correctly. Patch by Serhiy Storchaka.
-- Issue #14846: importlib.FileFinder now handles the case where the
- directory being searched is removed after a previous import attempt
+- Issue #14846: `importlib.FileFinder` now handles the case where the directory
+ being searched is removed after a previous import attempt.
Library
-------
-- Issue #13370: Ensure that ctypes works on Mac OS X when Python is
- compiled using the clang compiler
+- Issue #13370: Ensure that ctypes works on Mac OS X when Python is compiled
+ using the clang compiler.
-- Issue #13072: The array module's 'u' format code is now deprecated and
- will be removed in Python 4.0.
+- Issue #13072: The array module's 'u' format code is now deprecated and will be
+ removed in Python 4.0.
- Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
-- Issue #15249: BytesGenerator now correctly mangles From lines (when
+- Issue #15776: Allow pyvenv to work in existing directory with --clean.
+
+- Issue #15249: email's BytesGenerator now correctly mangles From lines (when
requested) even if the body contains undecodable bytes.
- Issue #15777: Fix a refleak in _posixsubprocess.
-- Issue ##665194: Update email.utils.localtime to use datetime.astimezone and
+- Issue ##665194: Update `email.utils.localtime` to use datetime.astimezone and
correctly handle historic changes in UTC offsets.
- Issue #15199: Fix JavaScript's default MIME type to application/javascript.
Patch by Bohuslav Kabrda.
-- Issue #12643: code.InteractiveConsole now respects sys.excepthook when
- displaying exceptions (Patch by Aaron Iles)
-
-- Issue #13579: string.Formatter now understands the 'a' conversion specifier.
+- Issue #12643: `code.InteractiveConsole` now respects `sys.excepthook` when
+ displaying exceptions. Patch by Aaron Iles.
-- Issue #15793: Stack corruption in ssl.RAND_egd().
- Patch by Serhiy Storchaka.
+- Issue #13579: `string.Formatter` now understands the 'a' conversion specifier.
-- Issue #15595: Fix subprocess.Popen(universal_newlines=True)
- for certain locales (utf-16 and utf-32 family). Patch by Chris Jerdonek.
+- Issue #15595: Fix ``subprocess.Popen(universal_newlines=True)`` for certain
+ locales (utf-16 and utf-32 family). Patch by Chris Jerdonek.
- Issue #15477: In cmath and math modules, add workaround for platforms whose
system-supplied log1p function doesn't respect signs of zeros.
-- Issue #15715: importlib.__import__() will silence an ImportError when the use
- of fromlist leads to a failed import.
+- Issue #15715: `importlib.__import__()` will silence an ImportError when the
+ use of fromlist leads to a failed import.
-- Issue #14669: Fix pickling of connections and sockets on MacOSX
- by sending/receiving an acknowledgment after file descriptor transfer.
- TestPicklingConnection has been reenabled for MacOSX.
+- Issue #14669: Fix pickling of connections and sockets on Mac OS X by
+ sending/receiving an acknowledgment after file descriptor transfer.
+ TestPicklingConnection has been reenabled for Mac OS X.
- Issue #11062: Fix adding a message from file to Babyl mailbox.
-- Issue #15646: Prevent equivalent of a fork bomb when using
- multiprocessing on Windows without the "if __name__ == '__main__'"
- idiom.
+- Issue #15646: Prevent equivalent of a fork bomb when using `multiprocessing`
+ on Windows without the ``if __name__ == '__main__'`` idiom.
-- Issue #15678: Fix IDLE menus when started from OS X command line
- (3.3.0b2 regression).
-
-C API
------
-
-Extension Modules
------------------
+IDLE
+----
-Tools/Demos
------------
+- Issue #15678: Fix IDLE menus when started from OS X command line (3.3.0b2
+ regression).
Documentation
-------------
-- Issue #14674: Add a discussion of the json module's standard compliance.
+- Touched up the Python 2 to 3 porting guide.
+
+- Issue #14674: Add a discussion of the `json` module's standard compliance.
Patch by Chris Rebert.
- Create a 'Concurrent Execution' section in the docs, and split up the
'Optional Operating System Services' section to use a more user-centric
- classification scheme (splitting them across the new CE section, IPC and
- text processing). Operating system limitatons can be reflected with
- the Sphinx ``:platform:`` tag, it doesn't make sense as part of the Table of
- Contents.
+ classification scheme (splitting them across the new CE section, IPC and text
+ processing). Operating system limitations can be reflected with the Sphinx
+ ``:platform:`` tag, it doesn't make sense as part of the Table of Contents.
-- Issue #4966: Bring the sequence docs up to date for the Py3k transition
- and the many language enhancements since they were original written
+- Issue #4966: Bring the sequence docs up to date for the Py3k transition and
+ the many language enhancements since they were original written.
- The "path importer" misnomer has been replaced with Eric Snow's
- more-awkward-but-at-least-not-wrong suggestion of "path based finder" in
- the import system reference docs
+ more-awkward-but-at-least-not-wrong suggestion of "path based finder" in the
+ import system reference docs.
-- Issue #15640: Document importlib.abc.Finder as deprecated.
+- Issue #15640: Document `importlib.abc.Finder` as deprecated.
-- Issue #15630: Add an example for "continue" stmt in the tutorial. Patch by
+- Issue #15630: Add an example for "continue" stmt in the tutorial. Patch by
Daniel Ellis.
Tests
-----
- Issue #15747: ZFS always returns EOPNOTSUPP when attempting to set the
- UF_IMMUTABLE flag (via either chflags or lchflags); refactor affected
- tests in test_posix.py to account for this.
+ UF_IMMUTABLE flag (via either chflags or lchflags); refactor affected tests in
+ test_posix.py to account for this.
-- Issue #15285: Refactor the approach for testing connect timeouts using
- two external hosts that have been configured specifically for this type
- of test.
+- Issue #15285: Refactor the approach for testing connect timeouts using two
+ external hosts that have been configured specifically for this type of test.
-- Issue #15743: Remove the deprecated method usage in urllib tests. Patch by
+- Issue #15743: Remove the deprecated method usage in `urllib` tests. Patch by
Jeff Knupp.
-- Issue #15615: Add some tests for the json module's handling of invalid
- input data. Patch by Kushal Das.
+- Issue #15615: Add some tests for the `json` module's handling of invalid input
+ data. Patch by Kushal Das.
Build
-----
@@ -277,11 +271,11 @@ Build
- Pick up 32-bit launcher from PGO directory on 64-bit PGO build.
-- Drop PC\python_nt.h as it's not used. Add input dependency on custom
+- Drop ``PC\python_nt.h`` as it's not used. Add input dependency on custom
build step.
-- Issue #15511: Drop explicit dependency on pythonxy.lib from _decimal
- amd64 configuration.
+- Issue #15511: Drop explicit dependency on pythonxy.lib from _decimal amd64
+ configuration.
- Add missing PGI/PGO configurations for pywlauncher.
@@ -296,15 +290,15 @@ What's New in Python 3.3.0 Beta 2?
Core and Builtins
-----------------
-- Issue #15568: Fix the return value of "yield from" when StopIteration is
+- Issue #15568: Fix the return value of ``yield from`` when StopIteration is
raised by a custom iterator.
-- Issue #13119: sys.stdout and sys.stderr are now using "\r\n" newline on
+- Issue #13119: `sys.stdout` and `sys.stderr` are now using "\r\n" newline on
Windows, as Python 2.
- Issue #15534: Fix the fast-search function for non-ASCII Unicode strings.
-- Issue #15508: Fix the docstring for __import__ to have the proper default
+- Issue #15508: Fix the docstring for `__import__()` to have the proper default
value of 0 for 'level' and to not mention negative levels since they are not
supported.
@@ -317,17 +311,17 @@ Core and Builtins
byte code files) equal between 32-bit and 64-bit systems.
- Issue #1692335: Move initial exception args assignment to
- "BaseException.__new__" to help pickling of naive subclasses.
+ `BaseException.__new__()` to help pickling of naive subclasses.
-- Issue #12834: Fix PyBuffer_ToContiguous() for non-contiguous arrays.
+- Issue #12834: Fix `PyBuffer_ToContiguous()` for non-contiguous arrays.
-- Issue #15456: Fix code __sizeof__ after #12399 change. Patch by Serhiy
+- Issue #15456: Fix code `__sizeof__()` after #12399 change. Patch by Serhiy
Storchaka.
- Issue #15404: Refleak in PyMethodObject repr.
-- Issue #15394: An issue in PyModule_Create that caused references to be leaked
- on some error paths has been fixed. Patch by Julia Lawall.
+- Issue #15394: An issue in `PyModule_Create()` that caused references to be
+ leaked on some error paths has been fixed. Patch by Julia Lawall.
- Issue #15368: An issue that caused bytecode generation to be non-deterministic
has been fixed.
@@ -335,7 +329,7 @@ Core and Builtins
- Issue #15202: Consistently use the name "follow_symlinks" for new parameters
in os and shutil functions.
-- Issue #15314: __main__.__loader__ is now set correctly during interpreter
+- Issue #15314: ``__main__.__loader__`` is now set correctly during interpreter
startup.
- Issue #15111: When a module imported using 'from import' has an ImportError
@@ -350,57 +344,62 @@ Core and Builtins
- Issue #15110: Fix the tracebacks generated by "import xxx" to not show the
importlib stack frames.
+- Issue #16369: Global PyTypeObjects not initialized with PyType_Ready(...).
+
- Issue #15020: The program name used to search for Python's path is now
"python3" under Unix, not "python".
-- Issue #15033: Fix the exit status bug when modules invoked using -m swith,
+- Issue #15897: zipimport.c doesn't check return value of fseek().
+ Patch by Felipe Cruz.
+
+- Issue #15033: Fix the exit status bug when modules invoked using -m switch,
return the proper failure return value (1). Patch contributed by Jeff Knupp.
-- Issue #15229: An OSError subclass whose __init__ doesn't call back
+- Issue #15229: An `OSError` subclass whose __init__ doesn't call back
OSError.__init__ could produce incomplete instances, leading to crashes when
calling str() on them.
-- Issue 15307: Virtual environments now use symlinks with framework builds on
+- Issue #15307: Virtual environments now use symlinks with framework builds on
Mac OS X, like other POSIX builds.
Library
-------
-- Issue #15424: Add a __sizeof__ implementation for array objects. Patch by
+- Issue #14590: configparser now correctly strips inline comments when delimiter
+ occurs earlier without preceding space.
+
+- Issue #15424: Add a `__sizeof__()` implementation for array objects. Patch by
Ludwig Hähne.
- Issue #15576: Allow extension modules to act as a package's __init__ module.
-- Issue #15502: Have importlib.invalidate_caches() work on sys.meta_path instead
- of sys.path_importer_cache.
+- Issue #15502: Have `importlib.invalidate_caches()` work on `sys.meta_path`
+ instead of `sys.path_importer_cache`.
- Issue #15163: Pydoc shouldn't list __loader__ as module data.
- Issue #15471: Do not use mutable objects as defaults for
- importlib.__import__().
+ `importlib.__import__()`.
- Issue #15559: To avoid a problematic failure mode when passed to the bytes
- constructor, objects in the ipaddress module no longer implement __index__
- (they still implement __int__ as appropriate)
+ constructor, objects in the ipaddress module no longer implement `__index__()`
+ (they still implement `__int__()` as appropriate).
- Issue #15546: Fix handling of pathological input data in the peek() and
read1() methods of the BZ2File, GzipFile and LZMAFile classes.
-- Issue #13052: Fix IDLE crashing when replace string in Search/Replace dialog
- ended with '\'. Patch by Roger Serwy.
-
-- Issue #12655: Instead of requiring a custom type, os.sched_getaffinity and
- os.sched_setaffinity now use regular sets of integers to represent the CPUs a
- process is restricted to.
+- Issue #12655: Instead of requiring a custom type, `os.sched_getaffinity()` and
+ `os.sched_setaffinity()` now use regular sets of integers to represent the
+ CPUs a process is restricted to.
-- Issue #15538: Fix compilation of the getnameinfo() / getaddrinfo() emulation
- code. Patch by Philipp Hagemeister.
+- Issue #15538: Fix compilation of the `socket.getnameinfo()` /
+ `socket.getaddrinfo()` emulation code. Patch by Philipp Hagemeister.
- Issue #15519: Properly expose WindowsRegistryFinder in importlib (and use the
- correct term for it). Original patch by Eric Snow.
+ correct term for it). Original patch by Eric Snow.
- Issue #15502: Bring the importlib ABCs into line with the current state of the
- import protocols given PEP 420. Original patch by Eric Snow.
+ import protocols given PEP 420. Original patch by Eric Snow.
- Issue #15499: Launching a webbrowser in Unix used to sleep for a few seconds.
Original patch by Anton Barkovsky.
@@ -408,37 +407,36 @@ Library
- Issue #15463: The faulthandler module truncates strings to 500 characters,
instead of 100, to be able to display long file paths.
-- Issue #6056: Make multiprocessing use setblocking(True) on the sockets it
+- Issue #6056: Make `multiprocessing` use setblocking(True) on the sockets it
uses. Original patch by J Derek Wilson.
- Issue #15364: Fix sysconfig.get_config_var('srcdir') to be an absolute path.
-- Issue #15041: Update "see also" list in tkinter documentation.
-
-- Issue #15413: os.times() had disappeared under Windows.
+- Issue #15413: `os.times()` had disappeared under Windows.
-- Issue #15402: An issue in the struct module that caused sys.getsizeof to
+- Issue #15402: An issue in the struct module that caused `sys.getsizeof()` to
return incorrect results for struct.Struct instances has been fixed. Initial
patch by Serhiy Storchaka.
-- Issue #15232: When mangle_from is True, email.Generator now correctly mangles
- lines that start with 'From ' that occur in a MIME preamble or epilogue.
+- Issue #15232: When mangle_from is True, `email.Generator` now correctly
+ mangles lines that start with 'From ' that occur in a MIME preamble or
+ epilogue.
- Issue #15094: Incorrectly placed #endif in _tkinter.c. Patch by Serhiy
Storchaka.
-- Issue #13922: argparse no longer incorrectly strips '--'s that appear after
+- Issue #13922: `argparse` no longer incorrectly strips '--'s that appear after
the first one.
-- Issue #12353: argparse now correctly handles null argument values.
+- Issue #12353: `argparse` now correctly handles null argument values.
- Issue #10017, issue #14998: Fix TypeError using pprint on dictionaries with
user-defined types as keys or other unorderable keys.
-- Issue #15397: inspect.getmodulename() is now based directly on importlib via a
- new importlib.machinery.all_suffixes() API.
+- Issue #15397: `inspect.getmodulename()` is now based directly on importlib via
+ a new `importlib.machinery.all_suffixes()` API.
-- Issue #14635: telnetlib will use poll() rather than select() when possible to
+- Issue #14635: `telnetlib` will use poll() rather than select() when possible to
avoid failing due to the select() file descriptor limit.
- Issue #15180: Clarify posixpath.join() error message when mixing str & bytes.
@@ -455,7 +453,7 @@ Library
- Issue #15233: Python now guarantees that callables registered with the atexit
module will be called in a deterministic order.
-- Issue #15238: shutil.copystat now copies Linux "extended attributes".
+- Issue #15238: `shutil.copystat()` now copies Linux "extended attributes".
- Issue #15230: runpy.run_path now correctly sets __package__ as described in
the documentation.
@@ -465,42 +463,42 @@ Library
- Issue #15294: Fix a regression in pkgutil.extend_path()'s handling of nested
namespace packages.
-- Issue #15056: imp.cache_from_source() and source_from_cache() raise
- NotImplementedError when sys.implementation.cache_tag is set to None.
+- Issue #15056: `imp.cache_from_source()` and `imp.source_from_cache()` raise
+ NotImplementedError when `sys.implementation.cache_tag` is set to None.
-- Issue #15256: Grammatical mistake in exception raised by imp.find_module().
+- Issue #15256: Grammatical mistake in exception raised by `imp.find_module()`.
-- Issue #5931: wsgiref environ variable SERVER_SOFTWARE will specify an
+- Issue #5931: `wsgiref` environ variable SERVER_SOFTWARE will specify an
implementation specific term like CPython, Jython instead of generic "Python".
- Issue #13248: Remove obsolete argument "max_buffer_size" of BufferedWriter and
BufferedRWPair, from the io module.
-- Issue #13248: Remove obsolete argument "version" of argparse.ArgumentParser.
+- Issue #13248: Remove obsolete argument "version" of `argparse.ArgumentParser`.
- Issue #14814: Implement more consistent ordering and sorting behaviour for
ipaddress objects.
-- Issue #14814: ipaddress network objects correctly return NotImplemented when
+- Issue #14814: `ipaddress` network objects correctly return NotImplemented when
compared to arbitrary objects instead of raising TypeError.
- Issue #14990: Correctly fail with SyntaxError on invalid encoding declaration.
-- Issue #14814: ipaddress now provides more informative error messages when
+- Issue #14814: `ipaddress` now provides more informative error messages when
constructing instances directly (changes permitted during beta due to
provisional API status).
-- Issue #15247: FileIO now raises an error when given a file descriptor pointing
- to a directory.
+- Issue #15247: `io.FileIO` now raises an error when given a file descriptor
+ pointing to a directory.
- Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
-- Issue #15166: Implement imp.get_tag() using sys.implementation.cache_tag.
+- Issue #15166: Implement `imp.get_tag()` using `sys.implementation.cache_tag`.
-- Issue #15210: Catch KeyError when importlib.__init__ can't find
+- Issue #15210: Catch KeyError when `importlib.__init__()` can't find
_frozen_importlib in sys.modules, not ImportError.
-- Issue #15030: importlib.abc.PyPycLoader now supports the new source size
+- Issue #15030: `importlib.abc.PyPycLoader` now supports the new source size
header field in .pyc files.
- Issue #5346: Preserve permissions of mbox, MMDF and Babyl mailbox files on
@@ -513,7 +511,7 @@ Library
renamed over the old file when flush() is called on an mbox, MMDF or Babyl
mailbox.
-- Issue 10924: Fixed crypt.mksalt() to use a RNG that is suitable for
+- Issue #10924: Fixed `crypt.mksalt()` to use a RNG that is suitable for
cryptographic purpose.
- Issue #15184: Ensure consistent results of OS X configuration tailoring for
@@ -524,10 +522,10 @@ Library
C API
-----
-- Issue #15610: PyImport_ImportModuleEx() now uses a 'level' of 0 instead of -1.
+- Issue #15610: `PyImport_ImportModuleEx()` now uses a 'level' of 0 instead of -1.
-- Issues #15169, #14599: Strip out the C implementation of
- imp.source_from_cache() used by PyImport_ExecCodeModuleWithPathnames() and
+- Issue #15169, issue #14599: Strip out the C implementation of
+ `imp.source_from_cache()` used by PyImport_ExecCodeModuleWithPathnames() and
used the Python code instead. Leads to PyImport_ExecCodeModuleObject() to not
try to infer the source path from the bytecode path as
PyImport_ExecCodeModuleWithPathnames() does.
@@ -535,14 +533,17 @@ C API
Extension Modules
-----------------
-- Issue #15676: Now "mmap" check for empty files before doing the
- offset check. Patch by Steven Willis.
-
-- Issue #6493: An issue in ctypes on Windows that caused structure bitfields
- of type ctypes.c_uint32 and width 32 to incorrectly be set has been fixed.
+- Issue #6493: An issue in ctypes on Windows that caused structure bitfields of
+ type `ctypes.c_uint32` and width 32 to incorrectly be set has been fixed.
- Issue #15194: Update libffi to the 3.0.11 release.
+IDLE
+----
+
+- Issue #13052: Fix IDLE crashing when replace string in Search/Replace dialog
+ ended with ``\``. Patch by Roger Serwy.
+
Tools/Demos
-----------
@@ -562,8 +563,10 @@ Tools/Demos
Documentation
-------------
-- Issue #15444: Use proper spelling for non-ASCII contributor names. Patch
- by Serhiy Storchaka.
+- Issue #15041: Update "see also" list in tkinter documentation.
+
+- Issue #15444: Use proper spelling for non-ASCII contributor names. Patch by
+ Serhiy Storchaka.
- Issue #15295: Reorganize and rewrite the documentation on the import system.
@@ -578,25 +581,29 @@ Documentation
"changed" since they will no longer work with modules directly imported by
import itself.
-- Issue #13557: Clarify effect of giving two different namespaces to exec or
- execfile().
+- Issue #13557: Clarify effect of giving two different namespaces to `exec()` or
+ `execfile()`.
-- Issue #15250: Document that filecmp.dircmp compares files shallowly. Patch
+- Issue #15250: Document that `filecmp.dircmp()` compares files shallowly. Patch
contributed by Chris Jerdonek.
+- Issue #15442: Expose the default list of directories ignored by
+ `filecmp.dircmp()` as a module attribute, and expand the list to more modern
+ values.
+
Tests
-----
-- Issue #15467: Move helpers for __sizeof__ tests into test_support. Patch by
- Serhiy Storchaka.
+- Issue #15467: Move helpers for `__sizeof__()` tests into test_support. Patch
+ by Serhiy Storchaka.
- Issue #15320: Make iterating the list of tests thread-safe when running tests
in multiprocess mode. Patch by Chris Jerdonek.
-- Issue #15168: Move importlib.test to test.test_importlib.
+- Issue #15168: Move `importlib.test` to `test.test_importlib`.
- Issue #15091: Reactivate a test on UNIX which was failing thanks to a
- forgotten importlib.invalidate_caches() call.
+ forgotten `importlib.invalidate_caches()` call.
- Issue #15230: Adopted a more systematic approach in the runpy tests.
@@ -629,6 +636,8 @@ Build
- Issue #14018: Fix OS X Tcl/Tk framework checking when using OS X SDKs.
+- Issue #16256: OS X installer now sets correct permissions for doc directory.
+
- Issue #15431: Add _freeze_importlib project to regenerate importlib.h on
Windows. Patch by Kristján Valur Jónsson.
@@ -664,14 +673,9 @@ Core and Builtins
- Issue #11626: Add _SizeT functions to stable ABI.
-- Issue #15146: Add PyType_FromSpecWithBases. Patch by Robin Schreiber.
-
- Issue #15142: Fix reference leak when deallocating instances of types
created using PyType_FromSpec().
-- Issue #15042: Add PyState_AddModule and PyState_RemoveModule. Add version
- guard for Py_LIMITED_API additions. Patch by Robin Schreiber.
-
- Issue #10053: Don't close FDs when FileIO.__init__ fails. Loosely based on
the work by Hirokazu Yamamoto.
@@ -699,9 +703,6 @@ Core and Builtins
Library
-------
-- Issue #9803: Don't close IDLE on saving if breakpoint is open.
- Patch by Roger Serwy.
-
- Issue #12288: Consider '0' and '0.0' as valid initialvalue
for tkinter SimpleDialog.
@@ -720,14 +721,8 @@ Library
- Issue #15514: Correct __sizeof__ support for cpu_set.
Patch by Serhiy Storchaka.
-- Issue #15187: Bugfix: remove temporary directories test_shutil was leaving
- behind.
-
- Issue #15177: Added dir_fd parameter to os.fwalk().
-- Issue #15176: Clarified behavior, documentation, and implementation
- of os.listdir().
-
- Issue #15061: Re-implemented hmac.compare_digest() in C to prevent further
timing analysis and to support all buffer protocol aware objects as well as
ASCII only str instances safely.
@@ -827,10 +822,6 @@ Library
- Issue #15006: Allow equality comparison between naive and aware
time or datetime objects.
-- Issue #14982: Document that pkgutil's iteration functions require the
- non-standard iter_modules() method to be defined by an importer (something
- the importlib importers do not define).
-
- Issue #15036: Mailbox no longer throws an error if a flush is done
between operations when removing or changing multiple items in mbox,
MMDF, or Babyl mailboxes.
@@ -898,9 +889,6 @@ Library
- Issue #14969: Better handling of exception chaining in contextlib.ExitStack
-- Issue #14962: Update text coloring in IDLE shell window after changing
- options. Patch by Roger Serwy.
-
- Issue #14963: Convert contextlib.ExitStack.__exit__ to use an iterative
algorithm (Patch by Alon Horev)
@@ -913,6 +901,11 @@ Library
C-API
-----
+- Issue #15146: Add PyType_FromSpecWithBases. Patch by Robin Schreiber.
+
+- Issue #15042: Add PyState_AddModule and PyState_RemoveModule. Add version
+ guard for Py_LIMITED_API additions. Patch by Robin Schreiber.
+
- Issue #13783: Inadvertent additions to the public C API in the PEP 380
implementation have either been removed or marked as private interfaces.
@@ -921,9 +914,25 @@ Extension Modules
- Issue #15000: Support the "unique" x32 architecture in _posixsubprocess.c.
+IDLE
+----
+
+- Issue #9803: Don't close IDLE on saving if breakpoint is open.
+ Patch by Roger Serwy.
+
+- Issue #14962: Update text coloring in IDLE shell window after changing
+ options. Patch by Roger Serwy.
+
Documentation
-------------
+- Issue #15176: Clarified behavior, documentation, and implementation
+ of os.listdir().
+
+- Issue #14982: Document that pkgutil's iteration functions require the
+ non-standard iter_modules() method to be defined by an importer (something
+ the importlib importers do not define).
+
- Issue #15081: Document PyState_FindModule.
Patch by Robin Schreiber.
@@ -932,6 +941,9 @@ Documentation
Tests
-----
+- Issue #15187: Bugfix: remove temporary directories test_shutil was leaving
+ behind.
+
- Issue #14769: test_capi now has SkipitemTest, which cleverly checks
for "parity" between PyArg_ParseTuple() and the Python/getargs.c static
function skipitem() for all possible "format units".
@@ -1020,34 +1032,18 @@ Core and Builtins
- Issue #14700: Fix two broken and undefined-behaviour-inducing overflow checks
in old-style string formatting.
-- Issue #14705: The PyArg_Parse() family of functions now support the 'p' format
- unit, which accepts a "boolean predicate" argument. It converts any Python
- value into an integer--0 if it is "false", and 1 otherwise.
-
Library
-------
- Issue #14690: Use monotonic clock instead of system clock in the sched,
subprocess and trace modules.
-- Issue #14958: Change IDLE systax highlighting to recognize all string and
- byte literals supported in Python 3.3.
-
-- Issue #10997: Prevent a duplicate entry in IDLE's "Recent Files" menu.
-
- Issue #14443: Tell rpmbuild to use the correct version of Python in
bdist_rpm. Initial patch by Ross Lagerwall.
-- Issue #14929: Stop Idle 3.x from closing on Unicode decode errors when
- grepping. Patch by Roger Serwy.
-
- Issue #12515: email now registers a defect if it gets to EOF while parsing
a MIME part without seeing the closing MIME boundary.
-- Issue #12510: Attempting to get invalid tooltip no longer closes Idle.
- Other tooltipss have been corrected or improved and the number of tests
- has been tripled. Original patch by Roger Serwy.
-
- Issue #1672568: email now always decodes base64 payloads, adding padding and
ignoring non-base64-alphabet characters if needed, and registering defects
for any such problems.
@@ -1081,9 +1077,6 @@ Library
- Issue #14548: Make multiprocessing finalizers check pid before
running to cope with possibility of gc running just after fork.
-- Issue #14863: Update the documentation of os.fdopen() to reflect the
- fact that it's only a thin wrapper around open() anymore.
-
- Issue #14036: Add an additional check to validate that port in urlparse does
not go in illegal range and returns None.
@@ -1210,6 +1203,21 @@ Library
- Issue #14127 and #10148: shutil.copystat now preserves exact mtime and atime
on filesystems providing nanosecond resolution.
+IDLE
+----
+
+- Issue #14958: Change IDLE systax highlighting to recognize all string and
+ byte literals supported in Python 3.3.
+
+- Issue #10997: Prevent a duplicate entry in IDLE's "Recent Files" menu.
+
+- Issue #14929: Stop IDLE 3.x from closing on Unicode decode errors when
+ grepping. Patch by Roger Serwy.
+
+- Issue #12510: Attempting to get invalid tooltip no longer closes IDLE.
+ Other tooltipss have been corrected or improved and the number of tests
+ has been tripled. Original patch by Roger Serwy.
+
Tools/Demos
-----------
@@ -1228,9 +1236,19 @@ Build
- Issue #13210: Windows build now uses VS2010, ported from VS2008.
+C-API
+-----
+
+- Issue #14705: The PyArg_Parse() family of functions now support the 'p' format
+ unit, which accepts a "boolean predicate" argument. It converts any Python
+ value into an integer--0 if it is "false", and 1 otherwise.
+
Documentation
-------------
+- Issue #14863: Update the documentation of os.fdopen() to reflect the
+ fact that it's only a thin wrapper around open() anymore.
+
- Issue #14588: The language reference now accurately documents the Python 3
class definition process. Patch by Nick Coghlan.
@@ -1279,9 +1297,6 @@ Core and Builtins
- Issue #14339: Speed improvements to bin, oct and hex functions. Patch by
Serhiy Storchaka.
-- Issue #14098: New functions PyErr_GetExcInfo and PyErr_SetExcInfo.
- Patch by Stefan Behnel.
-
- Issue #14385: It is now possible to use a custom type for the __builtins__
namespace, instead of a dict. It can be used for sandboxing for example.
Raise also a NameError instead of ImportError if __build_class__ name if not
@@ -1431,12 +1446,6 @@ Library
- Don't Py_DECREF NULL variable in io.IncrementalNewlineDecoder.
-- Issue #8515: Set __file__ when run file in IDLE.
- Initial patch by Bruce Frederiksen.
-
-- Issue #14496: Fix wrong name in idlelib/tabbedpages.py.
- Patch by Popa Claudiu.
-
- Issue #3033: Add displayof parameter to tkinter font. Patch by Guilherme Polo.
- Issue #14482: Raise a ValueError, not a NameError, when trying to create
@@ -1472,6 +1481,15 @@ Tests
- Issue #14355: Regrtest now supports the standard unittest test loading, and
will use it if a test file contains no `test_main` method.
+IDLE
+----
+
+- Issue #8515: Set __file__ when run file in IDLE.
+ Initial patch by Bruce Frederiksen.
+
+- Issue #14496: Fix wrong name in idlelib/tabbedpages.py.
+ Patch by Popa Claudiu.
+
Tools / Demos
-------------
@@ -1481,6 +1499,12 @@ Tools / Demos
- Issue #13165: stringbench is now available in the Tools/stringbench folder.
It used to live in its own SVN project.
+C-API
+-----
+
+- Issue #14098: New functions PyErr_GetExcInfo and PyErr_SetExcInfo.
+ Patch by Stefan Behnel.
+
What's New in Python 3.3.0 Alpha 2?
===================================
@@ -1532,16 +1556,9 @@ Library
- Issue #5136: deprecate old, unused functions from tkinter.
-- Issue #14409: IDLE now properly executes commands in the Shell window
- when it cannot read the normal config files on startup and
- has to use the built-in default key bindings.
- There was previously a bug in one of the defaults.
-
- Issue #14416: syslog now defines the LOG_ODELAY and LOG_AUTHPRIV constants
if they are defined in <syslog.h>.
-- IDLE can be launched as python -m idlelib
-
- Issue #14295: Add unittest.mock
- Issue #7652: Add --with-system-libmpdec option to configure for linking
@@ -1567,9 +1584,6 @@ Library
up the decimal module. Performance gains of the new C implementation are
between 10x and 100x, depending on the application.
-- Issue #3573: IDLE hangs when passing invalid command line args
- (directory(ies) instead of file(s)) (Patch by Guilherme Polo)
-
- Issue #14269: SMTPD now conforms to the RFC and requires a HELO command
before MAIL, RCPT, or DATA.
@@ -1601,8 +1615,6 @@ Library
denial of service due to hash collisions. Patch by David Malcolm with some
modifications by the expat project.
-- Issue #14200: Idle shell crash on printing non-BMP unicode character.
-
- Issue #12818: format address no longer needlessly \ escapes ()s in names when
the name ends up being quoted.
@@ -1618,8 +1630,6 @@ Library
- Issue #989712: Support using Tk without a mainloop.
-- Issue #5219: Prevent event handler cascade in IDLE.
-
- Issue #3835: Refuse to use unthreaded Tcl in threaded Python.
- Issue #2843: Add new Tk API to Tkinter.
@@ -1848,10 +1858,6 @@ Core and Builtins
on POSIX systems supporting anonymous memory mappings. Patch by
Charles-François Natali.
-- Issue #13452: PyUnicode_EncodeDecimal() doesn't support error handlers
- different than "strict" anymore. The caller was unable to compute the
- size of the output buffer: it depends on the error handler.
-
- PEP 3155 / issue #13448: Qualified name for classes and functions.
- Issue #13436: Fix a bogus error message when an AST object was passed
@@ -1942,10 +1948,6 @@ Core and Builtins
- PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
-- Issue #13560: Add PyUnicode_DecodeLocale(), PyUnicode_DecodeLocaleAndSize()
- and PyUnicode_EncodeLocale() functions to the C API to decode/encode from/to
- the current locale encoding.
-
- Add internal API for static strings (_Py_identifier et al.).
- Issue #13063: the Windows error ERROR_NO_DATA (numbered 232 and described
@@ -2028,7 +2030,7 @@ Core and Builtins
deallocator calls one of the methods on the type (e.g. when subclassing
IOBase). Diagnosis and patch by Davide Rizzo.
-- Issue #9611, #9015: FileIO.read() clamps the length to INT_MAX on Windows.
+- Issue #9611, Issue #9015: FileIO.read() clamps the length to INT_MAX on Windows.
- Issue #9642: Uniformize the tests on the availability of the mbcs codec, add
a new HAVE_MBCS define.
@@ -2191,17 +2193,11 @@ Core and Builtins
PyUnicode_AsUTF8String() and PyUnicode_AsEncodedString(unicode, "utf-8",
NULL).
-- Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats.
-
- Issue #10829: Refactor PyUnicode_FromFormat(), use the same function to parse
the format string in the 3 steps, fix crashs on invalid format strings.
- Issue #13007: whichdb should recognize gdbm 1.9 magic numbers.
-- Issue #11246: Fix PyUnicode_FromFormat("%V") to decode the byte string from
- UTF-8 (with replace error handler) instead of ISO-8859-1 (in strict mode).
- Patch written by Ray Allen.
-
- Issue #11286: Raise a ValueError from calling PyMemoryView_FromBuffer with
a buffer struct having a NULL data pointer.
@@ -2211,9 +2207,6 @@ Core and Builtins
- Issue #11828: startswith and endswith now accept None as slice index.
Patch by Torsten Becker.
-- Issue #10830: Fix PyUnicode_FromFormatV("%c") for non-BMP characters on
- narrow build.
-
- Issue #11168: Remove filename debug variable from PyEval_EvalFrameEx().
It encoded the Unicode filename to UTF-8, but the encoding fails on
undecodable filename (on surrogate characters) which raises an unexpected
@@ -2255,15 +2248,9 @@ Library
are dead or dying. Moreover, the implementation is now O(1) rather than
O(n).
-- Issue #13125: Silence spurious test_lib2to3 output when in non-verbose mode.
- Patch by Mikhail Novikov.
-
- Issue #11841: Fix comparison bug with 'rc' versions in packaging.version.
Patch by Filip Gruszczyński.
-- Issue #13447: Add a test file to host regression tests for bugs in the
- scripts found in the Tools directory.
-
- Issue #6884: Fix long-standing bugs with MANIFEST.in parsing in distutils
on Windows. Also fixed in packaging.
@@ -2319,9 +2306,6 @@ Library
authenticating (since the result may change, according to RFC 4643).
Patch by Hynek Schlawack.
-- Issue #13989: Document that GzipFile does not support text mode, and give a
- more helpful error message when opened with an invalid mode string.
-
- Issue #13590: On OS X 10.7 and 10.6 with Xcode 4.2, building
Distutils-based packages with C extension modules may fail because
Apple has removed gcc-4.2, the version used to build python.org
@@ -2338,10 +2322,6 @@ Library
- Issue #13960: HTMLParser is now able to handle broken comments when
strict=False.
-- Issue #13921: Undocument and clean up sqlite3.OptimizedUnicode,
- which is obsolete in Python 3.x. It's now aliased to str for
- backwards compatibility.
-
- When '' is a path (e.g. in sys.path), make sure __file__ uses the current
working directory instead of '' in importlib.
@@ -2363,11 +2343,6 @@ Library
- Issue #10811: Fix recursive usage of cursors. Instead of crashing,
raise a ProgrammingError now.
-- Issue #10881: Fix test_site failure with OS X framework builds.
-
-- Issue #964437: Make IDLE help window non-modal.
- Patch by Guilherme Polo and Roger Serwy.
-
- Issue #13734: Add os.fwalk(), a directory walking function yielding file
descriptors.
@@ -2377,16 +2352,8 @@ Library
- Issue #11805: package_data in setup.cfg should allow more than one value.
-- Issue #13933: IDLE auto-complete did not work with some imported
- module, like hashlib. (Patch by Roger Serwy)
-
-- Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
-
- Issue #13676: Handle strings with embedded zeros correctly in sqlite3.
-- Issue #13506: Add '' to path for IDLE Shell when started and restarted with Restart Shell.
- Original patches by Marco Scataglini and Roger Serwy.
-
- Issue #8828: Add new function os.replace(), for cross-platform renaming
with overwriting.
@@ -2407,12 +2374,6 @@ Library
OSError if localtime() failed. time.clock() now raises a RuntimeError if the
processor time used is not available or its value cannot be represented
-- Issue #13862: Fix spurious failure in test_zlib due to runtime/compile time
- minor versions not matching.
-
-- Issue #12804: Fix test_socket and test_urllib2net failures when running tests
- on a system without internet access.
-
- Issue #13772: In os.symlink() under Windows, do not try to guess the link
target's type (file or directory). The detection was buggy and made the
call non-atomic (therefore prone to race conditions).
@@ -2439,9 +2400,6 @@ Library
- Issue #13642: Unquote before b64encoding user:password during Basic
Authentication. Patch contributed by Joonas Kuorilehto.
-- Issue #13726: Fix the ambiguous -S flag in regrtest. It is -o/--slow for slow
- tests.
-
- Issue #12364: Fix a hang in concurrent.futures.ProcessPoolExecutor.
The hang would occur when retrieving the result of a scheduled future after
the executor had been shut down.
@@ -2524,10 +2482,6 @@ Library
- Issue #13591: A bug in importlib has been fixed that caused import_module
to load a module twice.
-- Issue #4625: If IDLE cannot write to its recent file or breakpoint files,
- display a message popup and continue rather than crash. Original patch by
- Roger Serwy.
-
- Issue #13449 sched.scheduler.run() method has a new "blocking" parameter which
when set to False makes run() execute the scheduled events due to expire
soonest (if any) and then return. Patch by Giampaolo Rodolà.
@@ -2544,12 +2498,9 @@ Library
'importlib.abc.PyPycLoader', 'nntplib.NNTP.xgtitle', 'nntplib.NNTP.xpath',
and private attributes of 'smtpd.SMTPChannel'.
-- Issue #5905, #13560: time.strftime() is now using the current locale
+- Issue #5905, Issue #13560: time.strftime() is now using the current locale
encoding, instead of UTF-8, if the wcsftime() function is not available.
-- Issue #8641: Update IDLE 3 syntax coloring to recognize b".." and not u"..".
- Patch by Tal Einat.
-
- Issue #13464: Add a readinto() method to http.client.HTTPResponse. Patch
by Jon Kuhn.
@@ -2661,9 +2612,6 @@ Library
- Issue #10817: Fix urlretrieve function to raise ContentTooShortError even
when reporthook is None. Patch by Jyrki Pulliainen.
-- Issue #13296: Fix IDLE to clear compile __future__ flags on shell restart.
- (Patch by Roger Serwy)
-
- Fix the xmlrpc.client user agent to return something similar to
urllib.request user agent: "Python-xmlrpc/3.3".
@@ -2766,10 +2714,6 @@ Library
- Issue #13034: When decoding some SSL certificates, the subjectAltName
extension could be unreported.
-- Issue #9871: Prevent IDLE 3 crash when given byte stings
- with invalid hex escape sequences, like b'\x0'.
- (Original patch by Claudiu Popa.)
-
- Issue #12306: Expose the runtime version of the zlib C library as a constant,
ZLIB_RUNTIME_VERSION, in the zlib module. Patch by Torsten Landschoff.
@@ -2798,8 +2742,6 @@ Library
- Issue #12878: Expose a __dict__ attribute on io.IOBase and its subclasses.
-- Issue #12636: IDLE reads the coding cookie when executing a Python script.
-
- Issue #12494: On error, call(), check_call(), check_output() and
getstatusoutput() functions of the subprocess module now kill the process,
read its status (to avoid zombis) and close pipes.
@@ -2869,9 +2811,6 @@ Library
- Issue #10087: Fix the html output format of the calendar module.
-- Issue #12540: Prevent zombie IDLE processes on Windows due to changes
- in os.kill().
-
- Issue #13121: add support for inplace math operators to collections.Counter.
- Add support for unary plus and unary minus to collections.Counter.
@@ -2903,7 +2842,7 @@ Library
Condition, etc.) used to be factory functions returning instances of hidden
classes (_Event, _Condition, etc.), because (if Guido recalls correctly) this
code pre-dates the ability to subclass extension types. It is now possible
- to inherit from these classes without having to import the private
+ to inherit from these classes, without having to import the private
underscored names like multiprocessing did.
- Issue #9723: Add shlex.quote functions, to escape filenames and command
@@ -2917,14 +2856,8 @@ Library
- Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
given as a low fd, it gets overwritten.
-- Issue #12590: IDLE editor window now always displays the first line
- when opening a long file. With Tk 8.5, the first line was hidden.
-
- Issue #12576: Fix urlopen behavior on sites which do not send (or obfuscates)
- Connection:close header.
-
-- Issue #12102: Document that buffered files must be flushed before being used
- with mmap. Patch by Steffen Daode Nurpmeso.
+ ``Connection: close`` header.
- Issue #12560: Build libpython.so on OpenBSD. Patch by Stefan Sperling.
@@ -3179,7 +3112,7 @@ Library
- Issue #12175: FileIO.readall() now raises a ValueError instead of an IOError
if the file is closed.
-- Issue #11109: New service_action method for BaseServer, used by ForkingMixIn
+- Issue #11109: New service_action method for BaseServer, used by ForkingMixin
class for cleanup. Initial Patch by Justin Warkentin.
- Issue #12045: Avoid duplicate execution of command in
@@ -3224,9 +3157,6 @@ Library
passing a ``context`` argument pointing to an ssl.SSLContext instance.
Patch by Kasun Herath.
-- Issue #11088: don't crash when using F5 to run a script in IDLE on MacOSX
- with Tk 8.5.
-
- Issue #9516: Issue #9516: avoid errors in sysconfig when MACOSX_DEPLOYMENT_TARGET
is set in shell.
@@ -3246,10 +3176,6 @@ Library
- Issue #9971: Write an optimized implementation of BufferedReader.readinto().
Patch by John O'Connor.
-- Issue #1028: Tk returns invalid Unicode null in %A: UnicodeDecodeError.
- With Tk < 8.5 _tkinter.c:PythonCmd() raised UnicodeDecodeError, caused
- IDLE to exit. Converted to valid Unicode null in PythonCmd().
-
- Issue #11799: urllib.request Authentication Handlers will raise a ValueError
when presented with an unsupported Authentication Scheme. Patch contributed
by Yuval Greenfield.
@@ -3486,12 +3412,12 @@ Library
- Issue #7639: Fix short file name generation in bdist_msi
-- Issue #11659: Fix ResourceWarning in test_subprocess introduced by #11459.
- Patch by Ben Hayden.
-
- Issue #11635: Don't use polling in worker threads and processes launched by
concurrent.futures.
+- Issue #5845: Automatically read readline configuration to enable completion
+ in interactive mode.
+
- Issue #6811: Allow importlib to change a code object's co_filename attribute
to match the path to where the source code currently is, not where the code
object originally came from.
@@ -3526,7 +3452,7 @@ Library
- Issue #11127: Raise a TypeError when trying to pickle a socket object.
-- Issue #11563: Connection:close header is sent by requests using URLOpener
+- Issue #11563: ``Connection: close`` header is sent by requests using URLOpener
class which helps in closing of sockets after connection is over. Patch
contributions by Jeff McNeil and Nadeem Vawda.
@@ -3541,8 +3467,6 @@ Library
- Issue #10979: unittest stdout buffering now works with class and module
setup and teardown.
-- Issue #11577: fix ResourceWarning triggered by improved binhex test coverage
-
- Issue #11243: fix the parameter querying methods of Message to work if
the headers contain un-encoded non-ASCII data.
@@ -3575,9 +3499,6 @@ Library
- Issue #11554: Fixed support for Japanese codecs; previously the body output
encoding was not done if euc-jp or shift-jis was specified as the charset.
-- Issue #11509: Significantly increase test coverage of fileinput.
- Patch by Denver Coneybeare at PyCon 2011 Sprints.
-
- Issue #11407: `TestCase.run` returns the result object used or created.
Contributed by Janathan Hartley.
@@ -3700,11 +3621,6 @@ Library
- Issue #9348: Raise an early error if argparse nargs and metavar don't match.
-- Issue #8982: Improve the documentation for the argparse Namespace object.
-
-- Issue #9343: Document that argparse parent parsers must be configured before
- their children.
-
- Issue #9026: Fix order of argparse sub-commands in help messages.
- Issue #9347: Fix formatting for tuples in argparse type= error messages.
@@ -3757,10 +3673,61 @@ Build
- Issue #11495: OSF support is eliminated. It was deprecated in Python 3.2.
-
IDLE
----
+- Issue #14409: IDLE now properly executes commands in the Shell window
+ when it cannot read the normal config files on startup and
+ has to use the built-in default key bindings.
+ There was previously a bug in one of the defaults.
+
+- IDLE can be launched as python -m idlelib
+
+- Issue #3573: IDLE hangs when passing invalid command line args
+ (directory(ies) instead of file(s)) (Patch by Guilherme Polo)
+
+- Issue #14200: IDLE shell crash on printing non-BMP unicode character.
+
+- Issue #5219: Prevent event handler cascade in IDLE.
+
+- Issue #964437: Make IDLE help window non-modal.
+ Patch by Guilherme Polo and Roger Serwy.
+
+- Issue #13933: IDLE auto-complete did not work with some imported
+ module, like hashlib. (Patch by Roger Serwy)
+
+- Issue #13506: Add '' to path for IDLE Shell when started and restarted with Restart Shell.
+ Original patches by Marco Scataglini and Roger Serwy.
+
+- Issue #4625: If IDLE cannot write to its recent file or breakpoint files,
+ display a message popup and continue rather than crash. Original patch by
+ Roger Serwy.
+
+- Issue #8641: Update IDLE 3 syntax coloring to recognize b".." and not u"..".
+ Patch by Tal Einat.
+
+- Issue #13296: Fix IDLE to clear compile __future__ flags on shell restart.
+ (Patch by Roger Serwy)
+
+- Issue #9871: Prevent IDLE 3 crash when given byte stings
+ with invalid hex escape sequences, like b'\x0'.
+ (Original patch by Claudiu Popa.)
+
+- Issue #12636: IDLE reads the coding cookie when executing a Python script.
+
+- Issue #12540: Prevent zombie IDLE processes on Windows due to changes
+ in os.kill().
+
+- Issue #12590: IDLE editor window now always displays the first line
+ when opening a long file. With Tk 8.5, the first line was hidden.
+
+- Issue #11088: don't crash when using F5 to run a script in IDLE on MacOSX
+ with Tk 8.5.
+
+- Issue #1028: Tk returns invalid Unicode null in %A: UnicodeDecodeError.
+ With Tk < 8.5 _tkinter.c:PythonCmd() raised UnicodeDecodeError, caused
+ IDLE to exit. Converted to valid Unicode null in PythonCmd().
+
- Issue #11718: IDLE's open module dialog couldn't find the __init__.py
file in a package.
@@ -3790,6 +3757,10 @@ Tools/Demos
Extension Modules
-----------------
+- Issue #16847: Fixed improper use of _PyUnicode_CheckConsistency() in
+ non-pydebug builds. Several extension modules now compile cleanly when
+ assert()s are enabled in standard builds (-DDEBUG flag).
+
- Issue #13840: The error message produced by ctypes.create_string_buffer
when given a Unicode string has been fixed.
@@ -3852,6 +3823,33 @@ Extension Modules
Tests
-----
+- Issue #13125: Silence spurious test_lib2to3 output when in non-verbose mode.
+ Patch by Mikhail Novikov.
+
+- Issue #13447: Add a test file to host regression tests for bugs in the
+ scripts found in the Tools directory.
+
+- Issue #10881: Fix test_site failure with OS X framework builds.
+
+- Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
+
+- Issue #13862: Fix spurious failure in test_zlib due to runtime/compile time
+ minor versions not matching.
+
+- Issue #12804: Fix test_socket and test_urllib2net failures when running tests
+ on a system without internet access.
+
+- Issue #13726: Fix the ambiguous -S flag in regrtest. It is -o/--slow for slow
+ tests.
+
+- Issue #11659: Fix ResourceWarning in test_subprocess introduced by #11459.
+ Patch by Ben Hayden.
+
+- Issue #11577: fix ResourceWarning triggered by improved binhex test coverage
+
+- Issue #11509: Significantly increase test coverage of fileinput.
+ Patch by Denver Coneybeare at PyCon 2011 Sprints.
+
- Issue #11689: Fix a variable scoping error in an sqlite3 test
- Issue #13786: Remove unimplemented 'trace' long option from regrtest.py.
@@ -4030,7 +4028,7 @@ Tests
- Issue #11505: improves test coverage of string.py. Patch by Alicia
Arlen
-- Issue #11490: test_subprocess:test_leaking_fds_on_error no longer gives a
+- Issue #11490: test_subprocess.test_leaking_fds_on_error no longer gives a
false positive if the last directory in the path is inaccessible.
- Issue #11223: Fix test_threadsignals to fail, not hang, when the
@@ -4054,6 +4052,23 @@ Tests
C-API
-----
+- Issue #13452: PyUnicode_EncodeDecimal() doesn't support error handlers
+ different than "strict" anymore. The caller was unable to compute the
+ size of the output buffer: it depends on the error handler.
+
+- Issue #13560: Add PyUnicode_DecodeLocale(), PyUnicode_DecodeLocaleAndSize()
+ and PyUnicode_EncodeLocale() functions to the C API to decode/encode from/to
+ the current locale encoding.
+
+- Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats.
+
+- Issue #11246: Fix PyUnicode_FromFormat("%V") to decode the byte string from
+ UTF-8 (with replace error handler) instead of ISO-8859-1 (in strict mode).
+ Patch written by Ray Allen.
+
+- Issue #10830: Fix PyUnicode_FromFormatV("%c") for non-BMP characters on
+ narrow build.
+
- Add PyObject_GenericGetDict and PyObject_GeneriSetDict. They are generic
implementations for the getter and setter of a ``__dict__`` descriptor of C
types.
@@ -4079,6 +4094,24 @@ C-API
Documentation
-------------
+- Issue #13989: Document that GzipFile does not support text mode, and give a
+ more helpful error message when opened with an invalid mode string.
+
+- Issue #13921: Undocument and clean up sqlite3.OptimizedUnicode,
+ which is obsolete in Python 3.x. It's now aliased to str for
+ backwards compatibility.
+
+- Issue #12102: Document that buffered files must be flushed before being used
+ with mmap. Patch by Steffen Daode Nurpmeso.
+
+- Issue #8982: Improve the documentation for the argparse Namespace object.
+
+- Issue #9343: Document that argparse parent parsers must be configured before
+ their children.
+
+- Issue #13498: Clarify docs of os.makedirs()'s exist_ok argument. Done with
+ great native-speaker help from R. David Murray.
+
- Issues #13491 and #13995: Fix many errors in sqlite3 documentation.
Initial patch for #13491 by Johannes Vogel.
@@ -9652,7 +9685,7 @@ Core and Builtins
- Issue #1210: Fixed imaplib and its documentation.
-- Issue #4233: Changed semantic of ``_fileio.FileIO``'s ``close()``
+- Issue #4233: Changed semantic of ``_fileio.FileIO``'s ``close()``
method on file objects with closefd=False. The file descriptor is still
kept open but the file object behaves like a closed file. The ``FileIO``
object also got a new readonly attribute ``closefd``.
@@ -9796,13 +9829,13 @@ Core and Builtins
cyclic garbage collection.
- Issue #3668: Fix a memory leak with the "s*" argument parser in
- PyArg_ParseTuple and friends, which occurred when the argument for "s*"
+ PyArg_ParseTuple and friends, which occurred when the argument for "s*"
was correctly parsed but parsing of subsequent arguments failed.
- Issue #3611: An exception __context__ could be cleared in a complex pattern
involving a __del__ method re-raising an exception.
-- Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
+- Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
match Python 2.5 speed despite the __instancecheck__ / __subclasscheck__
mechanism. In the process, fix a bug where isinstance() and issubclass(),
when given a tuple of classes as second argument, were looking up
@@ -9880,7 +9913,7 @@ Library
- The deprecation warnings for the camelCase threading API names were removed.
-- Issue #3110: multiprocessing fails to compiel on solaris 10 due to missing
+- Issue #3110: multiprocessing fails to compiel on solaris 10 due to missing
SEM_VALUE_MAX.
Extension Modules
@@ -21593,7 +21626,7 @@ New or improved ports
- Improved BeOS support.
-- Support dynamic loading of shared libraries on NetBSD platforms that
+- Support dynamic loading of shared libraries on NetBSD platforms that
use ELF (i.e., MIPS and Alpha systems).
Configuration/build changes
@@ -21810,7 +21843,7 @@ provide its own version of this function, while still sharing the
higher-level classes in code.py.
- turtle.py is a new module for simple turtle graphics. I'm still
-working on it; let me know if you use this to teach Python to children
+working on it; let me know if you use this to teach Python to children
or other novices without prior programming experience.
Obsoleted library modules
@@ -21942,7 +21975,7 @@ control pseudo-device, per audio(7I).
Changes to tools
----------------
-- New, improved version of Barry Warsaw's Misc/python-mode.el (editing
+- New, improved version of Barry Warsaw's Misc/python-mode.el (editing
support for Emacs).
- tabnanny.py: added a -q ('quiet') option to tabnanny, which causes
@@ -22217,7 +22250,7 @@ Windows Installer
-----------------
- Install zlib.dll in the DLLs directory instead of in the win32
-system directory, to avoid conflicts with other applications that have
+system directory, to avoid conflicts with other applications that have
their own zlib.dll.
Test Suite
@@ -22297,7 +22330,7 @@ General
so that a symlink to a symlink can work.
- Added a hack so that when you type 'quit' or 'exit' at the
-interpreter, you get a friendly explanation of how to press Ctrl-D (or
+interpreter, you get a friendly explanation of how to press Ctrl-D (or
Ctrl-Z) to exit.
- New and improved Misc/python-mode.el (Python mode for Emacs).
@@ -22470,13 +22503,13 @@ problem that randint() was accidentally defined as taking an inclusive
range. Also, randint(a, b) is now redefined as randrange(a, b+1),
adding extra range and type checking to its arguments!
-- Add some semi-thread-safety to random.gauss() (it used to be able to
+- Add some semi-thread-safety to random.gauss() (it used to be able to
crash when invoked from separate threads; now the worst it can do is
give a duplicate result occasionally).
- Some restructuring and generalization done to cmd.py.
-- Major upgrade to ConfigParser.py; converted to using 're', added new
+- Major upgrade to ConfigParser.py; converted to using 're', added new
exceptions, support underscore in section header and option name. No
longer add 'name' option to every section; instead, add '__name__'.
@@ -22673,7 +22706,7 @@ Windows Installer
-----------------
- The registry key used is now "1.5" instead of "1.5.x" -- so future
-versions of 1.5 and Mark Hammond's win32all installer don't need to be
+versions of 1.5 and Mark Hammond's win32all installer don't need to be
resynchronized.
Windows Tools
@@ -22740,7 +22773,7 @@ objects.
dynamically add one or many entries to the table of built-in modules.
- New macro Py_InitModule3(name, methods, doc) which calls
-Py_InitModule4() with appropriate arguments. (The -4 variant requires
+Py_InitModule4() with appropriate arguments. (The -4 variant requires
you to pass an obscure version number constant which is always the same.)
- New APIs PySys_WriteStdout() and PySys_WriteStderr() to write to
@@ -22812,7 +22845,7 @@ less common scenario in practice.
Syntax change
-------------
-- The raise statement can now be used without arguments, to re-raise
+- The raise statement can now be used without arguments, to re-raise
a previously set exception. This should be used after catching an
exception with an except clause only, either in the except clause or
later in the same function.
@@ -22871,7 +22904,7 @@ of a tab. The preferred module is tabnanny.py (by Tim Peters).
Demo/tkinter/guido/paint.py -- Dave Mitchell
Demo/sockets/unixserver.py -- Piet van Oostrum
-
+
- Much better freeze support. The freeze script can now freeze
hierarchical module names (with a corresponding change to import.c),
@@ -23010,7 +23043,7 @@ certain locales).
- New command supported by the ftplib module: rmd(); also fixed some
minor bugs.
-- The profile module now uses a different timer function by default --
+- The profile module now uses a different timer function by default --
time.clock() is generally better than os.times(). This makes it work
better on Windows NT, too.
@@ -23049,14 +23082,14 @@ module. Courtesy Barry Warsaw.
- In the multifile module, support the optional second parameter to
seek() when possible.
-- Several fixes to the gopherlib module by Lars Marius Garshol. Also,
+- Several fixes to the gopherlib module by Lars Marius Garshol. Also,
urlparse now correctly handles Gopher URLs with query strings.
- Fixed a tiny bug in format_exception() in the traceback module.
Also rewrite tb_lineno() to be compatible with JPython (and not
disturb the current exception!); by Jim Hugunin.
-- The httplib module is more robust when servers send a short response
+- The httplib module is more robust when servers send a short response
-- courtesy Tim O'Malley.
Tkinter and friends
@@ -23071,7 +23104,7 @@ application only).
no longer use the default root.
- The interfaces for the bind*() and unbind() widget methods have been
-redesigned; the bind*() methods now return the name of the Tcl command
+redesigned; the bind*() methods now return the name of the Tcl command
created for the callback, and this can be passed as a optional
argument to unbind() in order to delete the command (normally, such
commands are automatically unbound when the widget is destroyed, but
@@ -23107,7 +23140,7 @@ intended for storing thread-local global variables.
dictionary to allow recursive container types to detect recursion in
their repr(), str() and print implementations.
-- New function PyObject_Not(x) calculates (not x) according to Python's
+- New function PyObject_Not(x) calculates (not x) according to Python's
standard rules (basically, it negates the outcome PyObject_IsTrue(x).
- New function _PyModule_Clear(), which clears a module's dictionary
@@ -23268,7 +23301,7 @@ defined in packages correctly. The same change applies to copying
instances with copy.py. The cPickle.c changes and some pickle.py
changes are courtesy Jim Fulton.
-- Locale support in he "re" (Perl regular expressions) module. Use
+- Locale support in he "re" (Perl regular expressions) module. Use
the flag re.L (or re.LOCALE) to enable locale-specific matching
rules for \w and \b. The in-line syntax for this flag is (?L).
@@ -23334,7 +23367,7 @@ don't know how to deal with those.
- Some improvements to the _tkinter build line suggested by Case Roole.
-- A full suite of platform specific files for NetBSD 1.x, submitted by
+- A full suite of platform specific files for NetBSD 1.x, submitted by
Anders Andersen.
- New Solaris specific header STROPTS.py.
@@ -23404,7 +23437,7 @@ after studying the GNU libg++ quicksort. This should be much faster
if there are lots of duplicates, and otherwise at least as good.
- Added "uue" as an alias for "uuencode" to mimetools.py. (Hm, the
-uudecode bug where it complaints about trailing garbage is still there
+uudecode bug where it complaints about trailing garbage is still there
:-( ).
- pickle.py requires integers in text mode to be in decimal notation
@@ -24180,7 +24213,7 @@ the C and the Python variety) and for floating point numbers.
The Python/C API for frames is changed (you shouldn't be using this
anyway).
-- Significant speedup by inlining some common opcodes for common operand
+- Significant speedup by inlining some common opcodes for common operand
types (e.g. i+i, i-i, and list[i]). Fredrik Lundh.
- Small speedup by reordering the method tables of some common
@@ -24206,34 +24239,34 @@ pressure to document their own contributed modules :-). Note that
printing the documentation now kills fewer trees -- the margins have
been reduced.
-- I have started documenting the Python/C API. Unfortunately this project
-hasn't been completed yet. It will be complete before the final release of
-Python 1.5, though. At the moment, it's better to read the LaTeX source
+- I have started documenting the Python/C API. Unfortunately this project
+hasn't been completed yet. It will be complete before the final release of
+Python 1.5, though. At the moment, it's better to read the LaTeX source
than to attempt to run it through LaTeX and print the resulting dvi file.
-- The posix module (and hence os.py) now has doc strings! Thanks to Neil
-Schemenauer. I received a few other contributions of doc strings. In most
+- The posix module (and hence os.py) now has doc strings! Thanks to Neil
+Schemenauer. I received a few other contributions of doc strings. In most
other places, doc strings are still wishful thinking...
Language changes
----------------
-- Private variables with leading double underscore are now a permanent
-feature of the language. (These were experimental in release 1.4. I have
-favorable experience using them; I can't label them "experimental"
+- Private variables with leading double underscore are now a permanent
+feature of the language. (These were experimental in release 1.4. I have
+favorable experience using them; I can't label them "experimental"
forever.)
-- There's new string literal syntax for "raw strings". Prefixing a string
-literal with the letter r (or R) disables all escape processing in the
-string; for example, r'\n' is a two-character string consisting of a
-backslash followed by the letter n. This combines with all forms of string
-quotes; it is actually useful for triple quoted doc strings which might
-contain references to \n or \t. An embedded quote prefixed with a
-backslash does not terminate the string, but the backslash is still
-included in the string; for example, r'\'' is a two-character string
-consisting of a backslash and a quote. (Raw strings are also
-affectionately known as Robin strings, after their inventor, Robin
+- There's new string literal syntax for "raw strings". Prefixing a string
+literal with the letter r (or R) disables all escape processing in the
+string; for example, r'\n' is a two-character string consisting of a
+backslash followed by the letter n. This combines with all forms of string
+quotes; it is actually useful for triple quoted doc strings which might
+contain references to \n or \t. An embedded quote prefixed with a
+backslash does not terminate the string, but the backslash is still
+included in the string; for example, r'\'' is a two-character string
+consisting of a backslash and a quote. (Raw strings are also
+affectionately known as Robin strings, after their inventor, Robin
Friedrich.)
- There's a simple assert statement, and a new exception
@@ -24262,10 +24295,10 @@ patches to catch floating point exceptions, at the moment).
- The obsolete exception ConflictError (presumably used by the long
obsolete access statement) has been deleted.
-- There's a new function sys.exc_info() which returns the tuple
+- There's a new function sys.exc_info() which returns the tuple
(sys.exc_type, sys.exc_value, sys.exc_traceback) in a thread-safe way.
-- There's a new variable sys.executable, pointing to the executable file
+- There's a new variable sys.executable, pointing to the executable file
for the Python interpreter.
- The sort() methods for lists no longer uses the C library qsort(); I
@@ -24291,11 +24324,11 @@ caught an exception are kept alive until another exception is caught
returning from a function that caught an exception.
- There's a new "buffer" interface. Certain objects (e.g. strings and
-arrays) now support the "buffer" protocol. Buffer objects are acceptable
-whenever formerly a string was required for a write operation; mutable
+arrays) now support the "buffer" protocol. Buffer objects are acceptable
+whenever formerly a string was required for a write operation; mutable
buffer objects can be the target of a read operation using the call
-f.readinto(buffer). A cool feature is that regular expression matching now
-also work on array objects. Contribution by Jack Jansen. (Needs
+f.readinto(buffer). A cool feature is that regular expression matching now
+also work on array objects. Contribution by Jack Jansen. (Needs
documentation.)
- String interning: dictionary lookups are faster when the lookup
@@ -24587,7 +24620,7 @@ Wirzenius, and RFC 850 dates (Chris Lawrence).
of message sequence specifiers without invoking a subprocess. Also
added a createmessage() method by Lars Wirzenius.
-- The StringIO.StringIO class now supports readline(nbytes). (Lars
+- The StringIO.StringIO class now supports readline(nbytes). (Lars
Wirzenius.) (Of course, you should be using cStringIO for performance.)
- UserDict.py supports the new dictionary methods as well.
@@ -24635,8 +24668,8 @@ command line utilities.
- Various small fixes to the nntplib.py module that I can't bother to
document in detail.
-- Sjoerd Mullender's mimify.py module now supports base64 encoding and
-includes functions to handle the funny encoding you sometimes see in mail
+- Sjoerd Mullender's mimify.py module now supports base64 encoding and
+includes functions to handle the funny encoding you sometimes see in mail
headers. It is now documented.
- mailbox.py: Added BabylMailbox. Improved the way the mailbox is
@@ -24987,23 +25020,23 @@ Windows (NT and 95)
NT (the old VC++ 4.2 Makefile is also still supported, but will
eventually be withdrawn due to its bulkiness).
-- See the note on the new module search path in the "Miscellaneous" section
+- See the note on the new module search path in the "Miscellaneous" section
above.
- Support for Win32s (the 32-bit Windows API under Windows 3.1) is
basically withdrawn. If it still works for you, you're lucky.
-- There's a new extension module, msvcrt.c, which provides various
-low-level operations defined in the Microsoft Visual C++ Runtime Library.
-These include locking(), setmode(), get_osfhandle(), set_osfhandle(), and
+- There's a new extension module, msvcrt.c, which provides various
+low-level operations defined in the Microsoft Visual C++ Runtime Library.
+These include locking(), setmode(), get_osfhandle(), set_osfhandle(), and
console I/O functions like kbhit(), getch() and putch().
- The -u option not only sets the standard I/O streams to unbuffered
status, but also sets them in binary mode. (This can also be done
using msvcrt.setmode(), by the way.)
-- The, sys.prefix and sys.exec_prefix variables point to the directory
-where Python is installed, or to the top of the source tree, if it was run
+- The, sys.prefix and sys.exec_prefix variables point to the directory
+where Python is installed, or to the top of the source tree, if it was run
from there.
- The various os.path modules (posixpath, ntpath, macpath) now support
@@ -25011,7 +25044,7 @@ passing more than two arguments to the join() function, so
os.path.join(a, b, c) is the same as os.path.join(a, os.path.join(b,
c)).
-- The ntpath module (normally used as os.path) supports ~ to $HOME
+- The ntpath module (normally used as os.path) supports ~ to $HOME
expansion in expanduser().
- The freeze tool now works on Windows.
@@ -25309,47 +25342,47 @@ output directory.
- New module whichdb recognizes dbm, gdbm and bsddb/dbhash files.
-- The Doc/Makefile targets have been reorganized somewhat to remove the
+- The Doc/Makefile targets have been reorganized somewhat to remove the
insistence on always generating PostScript.
- The texinfo to html filter (Doc/texi2html.py) has been improved somewhat.
-- "errors.h" has been renamed to "pyerrors.h" to resolve a long-standing
+- "errors.h" has been renamed to "pyerrors.h" to resolve a long-standing
name conflict on the Mac.
-- Linking a module compiled with a different setting for Py_TRACE_REFS now
+- Linking a module compiled with a different setting for Py_TRACE_REFS now
generates a linker error rather than a core dump.
-- The cgi module has a new convenience function print_exception(), which
-formats a python exception using HTML. It also fixes a bug in the
-compatibility code and adds a dubious feature which makes it possible to
+- The cgi module has a new convenience function print_exception(), which
+formats a python exception using HTML. It also fixes a bug in the
+compatibility code and adds a dubious feature which makes it possible to
have two query strings, one in the URL and one in the POST data.
-- A subtle change in the unpickling of class instances makes it possible
-to unpickle in restricted execution mode, where the __dict__ attribute is
+- A subtle change in the unpickling of class instances makes it possible
+to unpickle in restricted execution mode, where the __dict__ attribute is
not available (but setattr() is).
-- Documentation for os.path.splitext() (== posixpath.splitext()) has been
+- Documentation for os.path.splitext() (== posixpath.splitext()) has been
cleared up. It splits at the *last* dot.
- posixfile locking is now also correctly supported on AIX.
-- The tempfile module once again honors an initial setting of tmpdir. It
+- The tempfile module once again honors an initial setting of tmpdir. It
now works on Windows, too.
-- The traceback module has some new functions to extract, format and print
+- The traceback module has some new functions to extract, format and print
the active stack.
-- Some translation functions in the urllib module have been made a little
+- Some translation functions in the urllib module have been made a little
less sluggish.
-- The addtag_* methods for Canvas widgets in Tkinter as well as in the
-separate Canvas class have been fixed so they actually do something
+- The addtag_* methods for Canvas widgets in Tkinter as well as in the
+separate Canvas class have been fixed so they actually do something
meaningful.
- A tiny _test() function has been added to Tkinter.py.
-- A generic Makefile for dynamically loaded modules is provided in the Misc
+- A generic Makefile for dynamically loaded modules is provided in the Misc
subdirectory (Misc/gMakefile).
- A new version of python-mode.el for Emacs is provided. See
@@ -25357,25 +25390,25 @@ http://www.python.org/ftp/emacs/pmdetails.html for details. The
separate file pyimenu.el is no longer needed, imenu support is folded
into python-mode.el.
-- The configure script can finally correctly find the readline library in a
-non-standard location. The LDFLAGS variable is passed on the Makefiles
+- The configure script can finally correctly find the readline library in a
+non-standard location. The LDFLAGS variable is passed on the Makefiles
from the configure script.
-- Shared libraries are now installed as programs (i.e. with executable
+- Shared libraries are now installed as programs (i.e. with executable
permission). This is required on HP-UX and won't hurt on other systems.
-- The objc.c module is no longer part of the distribution. Objective-C
+- The objc.c module is no longer part of the distribution. Objective-C
support may become available as contributed software on the ftp site.
- The sybase module is no longer part of the distribution. A much
improved sybase module is available as contributed software from the
ftp site.
-- _tkinter is now compatible with Tcl 7.5 / Tk 4.1 patch1 on Windows and
-Mac (don't use unpatched Tcl/Tk!). The default line in the Setup.in file
+- _tkinter is now compatible with Tcl 7.5 / Tk 4.1 patch1 on Windows and
+Mac (don't use unpatched Tcl/Tk!). The default line in the Setup.in file
now links with Tcl 7.5 / Tk 4.1 rather than 7.4/4.0.
-- In Setup, you can now write "*shared*" instead of "*noconfig*", and you
+- In Setup, you can now write "*shared*" instead of "*noconfig*", and you
can use *.so and *.sl as shared libraries.
- Some more fidgeting for AIX shared libraries.
@@ -25384,81 +25417,81 @@ can use *.so and *.sl as shared libraries.
(Note -- a complete replacement by Niels Mo"ller, called gpmodule, is
available from the contrib directory on the ftp site.)
-- A warning is written to sys.stderr when a __del__ method raises an
+- A warning is written to sys.stderr when a __del__ method raises an
exception (formerly, such exceptions were completely ignored).
-- The configure script now defines HAVE_OLD_CPP if the C preprocessor is
+- The configure script now defines HAVE_OLD_CPP if the C preprocessor is
incapable of ANSI style token concatenation and stringification.
-- All source files (except a few platform specific modules) are once again
+- All source files (except a few platform specific modules) are once again
compatible with K&R C compilers as well as ANSI compilers. In particular,
-ANSI-isms have been removed or made conditional in complexobject.c,
+ANSI-isms have been removed or made conditional in complexobject.c,
getargs.c and operator.c.
-- The abstract object API has three new functions, PyObject_DelItem,
+- The abstract object API has three new functions, PyObject_DelItem,
PySequence_DelItem, and PySequence_DelSlice.
-- The operator module has new functions delitem and delslice, and the
-functions "or" and "and" are renamed to "or_" and "and_" (since "or" and
+- The operator module has new functions delitem and delslice, and the
+functions "or" and "and" are renamed to "or_" and "and_" (since "or" and
"and" are reserved words). ("__or__" and "__and__" are unchanged.)
-- The environment module is no longer supported; putenv() is now a function
+- The environment module is no longer supported; putenv() is now a function
in posixmodule (also under NT).
- Error in filter(<function>, "") has been fixed.
- Unrecognized keyword arguments raise TypeError, not KeyError.
-- Better portability, fewer bugs and memory leaks, fewer compiler warnings,
+- Better portability, fewer bugs and memory leaks, fewer compiler warnings,
some more documentation.
-- Bug in float power boundary case (0.0 to the negative integer power)
+- Bug in float power boundary case (0.0 to the negative integer power)
fixed.
-- The test of negative number to the float power has been moved from the
-built-in pow() functin to floatobject.c (so complex numbers can yield the
+- The test of negative number to the float power has been moved from the
+built-in pow() functin to floatobject.c (so complex numbers can yield the
correct result).
-- The bug introduced in beta2 where shared libraries loaded (using
+- The bug introduced in beta2 where shared libraries loaded (using
dlopen()) from the current directory would fail, has been fixed.
-- Modules imported as shared libraries now also have a __file__ attribute,
-giving the filename from which they were loaded. The only modules without
+- Modules imported as shared libraries now also have a __file__ attribute,
+giving the filename from which they were loaded. The only modules without
a __file__ attribute now are built-in modules.
-- On the Mac, dynamically loaded modules can end in either ".slb" or
-".<platform>.slb" where <platform> is either "CFM68K" or "ppc". The ".slb"
+- On the Mac, dynamically loaded modules can end in either ".slb" or
+".<platform>.slb" where <platform> is either "CFM68K" or "ppc". The ".slb"
extension should only be used for "fat" binaries.
-- C API addition: marshal.c now supports
+- C API addition: marshal.c now supports
PyMarshal_WriteObjectToString(object).
- C API addition: getargs.c now supports
PyArg_ParseTupleAndKeywords(args, kwdict, format, kwnames, ...)
to parse keyword arguments.
-- The PC versioning scheme (sys.winver) has changed once again. the
-version number is now "<digit>.<digit>.<digit>.<apiversion>", where the
-first three <digit>s are the Python version (e.g. "1.4.0" for Python 1.4,
-"1.4.1" for Python 1.4.1 -- the beta level is not included) and
+- The PC versioning scheme (sys.winver) has changed once again. the
+version number is now "<digit>.<digit>.<digit>.<apiversion>", where the
+first three <digit>s are the Python version (e.g. "1.4.0" for Python 1.4,
+"1.4.1" for Python 1.4.1 -- the beta level is not included) and
<apiversion> is the four-digit PYTHON_API_VERSION (currently 1005).
- h2py.py accepts whitespace before the # in CPP directives
-- On Solaris 2.5, it should now be possible to use either Posix threads or
-Solaris threads (XXX: how do you select which is used???). (Note: the
-Python pthreads interface doesn't fully support semaphores yet -- anyone
+- On Solaris 2.5, it should now be possible to use either Posix threads or
+Solaris threads (XXX: how do you select which is used???). (Note: the
+Python pthreads interface doesn't fully support semaphores yet -- anyone
care to fix this?)
-- Thread support should now work on AIX, using either DCE threads or
+- Thread support should now work on AIX, using either DCE threads or
pthreads.
- New file Demo/sockets/unicast.py
-- Working Mac port, with CFM68K support, with Tk 4.1 support (though not
+- Working Mac port, with CFM68K support, with Tk 4.1 support (though not
both) (XXX)
-- New project setup for PC port, now compatible with PythonWin, with
+- New project setup for PC port, now compatible with PythonWin, with
_tkinter and NumPy support (XXX)
- New module site.py (XXX)
@@ -25475,7 +25508,7 @@ _tkinter and NumPy support (XXX)
- string.atoi c.s. now raise an exception for an empty input string.
-- At last, it is no longer necessary to define HAVE_CONFIG_H in order to
+- At last, it is no longer necessary to define HAVE_CONFIG_H in order to
have config.h included at various places.
- Unrecognized keyword arguments now raise TypeError rather than KeyError.
@@ -25483,25 +25516,25 @@ have config.h included at various places.
- The makesetup script recognizes files with extension .so or .sl as
(shared) libraries.
-- 'access' is no longer a reserved word, and all code related to its
-implementation is gone (or at least #ifdef'ed out). This should make
+- 'access' is no longer a reserved word, and all code related to its
+implementation is gone (or at least #ifdef'ed out). This should make
Python a little speedier too!
-- Performance enhancements suggested by Sjoerd Mullender. This includes
-the introduction of two new optional function pointers in type object,
-getattro and setattro, which are like getattr and setattr but take a
+- Performance enhancements suggested by Sjoerd Mullender. This includes
+the introduction of two new optional function pointers in type object,
+getattro and setattro, which are like getattr and setattr but take a
string object instead of a C string pointer.
-- New operations in string module: lstrip(s) and rstrip(s) strip whitespace
-only on the left or only on the right, A new optional third argument to
-split() specifies the maximum number of separators honored (so
-splitfields(s, sep, n) returns a list of at most n+1 elements). (Since
+- New operations in string module: lstrip(s) and rstrip(s) strip whitespace
+only on the left or only on the right, A new optional third argument to
+split() specifies the maximum number of separators honored (so
+splitfields(s, sep, n) returns a list of at most n+1 elements). (Since
1.3, splitfields(s, None) is totally equivalent to split(s).)
-string.capwords() has an optional second argument specifying the
+string.capwords() has an optional second argument specifying the
separator (which is passed to split()).
-- regsub.split() has the same addition as string.split(). regsub.splitx(s,
-sep, maxsep) implements the functionality that was regsub.split(s, 1) in
+- regsub.split() has the same addition as string.split(). regsub.splitx(s,
+sep, maxsep) implements the functionality that was regsub.split(s, 1) in
1.4beta2 (return a list containing the delimiters as well as the words).
- Final touch for AIX loading, rewritten Misc/AIX-NOTES.
@@ -25545,11 +25578,11 @@ What's new in 1.4beta2 (since beta1)?
meaningful value (a few things were botched in beta 1). Lib/dos_8x3
is now a standard part of the distribution (alas).
-- More improvements to the installation procedure. Typing "make install"
-now inserts the version number in the pathnames of almost everything
-installed, and creates the machine dependent modules (FCNTL.py etc.) if not
-supplied by the distribution. (XXX There's still a problem with the latter
-because the "regen" script requires that Python is installed. Some manual
+- More improvements to the installation procedure. Typing "make install"
+now inserts the version number in the pathnames of almost everything
+installed, and creates the machine dependent modules (FCNTL.py etc.) if not
+supplied by the distribution. (XXX There's still a problem with the latter
+because the "regen" script requires that Python is installed. Some manual
intervention may still be required.) (This has been fixed in 1.4beta3.)
- New modules: errno, operator (XXX).
@@ -25612,8 +25645,8 @@ What's new in 1.4beta1 (since 1.3)?
- Added sys.platform and sys.exec_platform for Bill Janssen.
-- Installation has been completely overhauled. "make install" now installs
-everything, not just the python binary. Installation uses the install-sh
+- Installation has been completely overhauled. "make install" now installs
+everything, not just the python binary. Installation uses the install-sh
script (borrowed from X11) to install each file.
- New functions in the posix module: mkfifo, plock, remove (== unlink),
@@ -25623,59 +25656,59 @@ and ftruncate. More functions are also available under NT.
- Shared library support for FreeBSD.
-- The --with-readline option can now be used without a DIRECTORY argument,
-for systems where libreadline.* is in one of the standard places. It is
+- The --with-readline option can now be used without a DIRECTORY argument,
+for systems where libreadline.* is in one of the standard places. It is
also possible for it to be a shared library.
-- The extension tkinter has been renamed to _tkinter, to avoid confusion
-with Tkinter.py oncase insensitive file systems. It now supports Tk 4.1 as
+- The extension tkinter has been renamed to _tkinter, to avoid confusion
+with Tkinter.py oncase insensitive file systems. It now supports Tk 4.1 as
well as 4.0.
-- Author's change of address from CWI in Amsterdam, The Netherlands, to
+- Author's change of address from CWI in Amsterdam, The Netherlands, to
CNRI in Reston, VA, USA.
-- The math.hypot() function is now always available (if it isn't found in
+- The math.hypot() function is now always available (if it isn't found in
the C math library, Python provides its own implementation).
-- The latex documentation is now compatible with latex2e, thanks to David
+- The latex documentation is now compatible with latex2e, thanks to David
Ascher.
- The expression x**y is now equivalent to pow(x, y).
- The indexing expression x[a, b, c] is now equivalent to x[(a, b, c)].
-- Complex numbers are now supported. Imaginary constants are written with
-a 'j' or 'J' prefix, general complex numbers can be formed by adding a real
-part to an imaginary part, like 3+4j. Complex numbers are always stored in
-floating point form, so this is equivalent to 3.0+4.0j. It is also
-possible to create complex numbers with the new built-in function
-complex(re, [im]). For the footprint-conscious, complex number support can
+- Complex numbers are now supported. Imaginary constants are written with
+a 'j' or 'J' prefix, general complex numbers can be formed by adding a real
+part to an imaginary part, like 3+4j. Complex numbers are always stored in
+floating point form, so this is equivalent to 3.0+4.0j. It is also
+possible to create complex numbers with the new built-in function
+complex(re, [im]). For the footprint-conscious, complex number support can
be disabled by defining the symbol WITHOUT_COMPLEX.
- New built-in function list() is the long-awaited counterpart of tuple().
-- There's a new "cmath" module which provides the same functions as the
-"math" library but with complex arguments and results. (There are very
-good reasons why math.sqrt(-1) still raises an exception -- you have to use
+- There's a new "cmath" module which provides the same functions as the
+"math" library but with complex arguments and results. (There are very
+good reasons why math.sqrt(-1) still raises an exception -- you have to use
cmath.sqrt(-1) to get 1j for an answer.)
-- The Python.h header file (which is really the same as allobjects.h except
-it disables support for old style names) now includes several more files,
+- The Python.h header file (which is really the same as allobjects.h except
+it disables support for old style names) now includes several more files,
so you have to have fewer #include statements in the average extension.
-- The NDEBUG symbol is no longer used. Code that used to be dependent on
-the presence of NDEBUG is now present on the absence of DEBUG. TRACE_REFS
-and REF_DEBUG have been renamed to Py_TRACE_REFS and Py_REF_DEBUG,
-respectively. At long last, the source actually compiles and links without
+- The NDEBUG symbol is no longer used. Code that used to be dependent on
+the presence of NDEBUG is now present on the absence of DEBUG. TRACE_REFS
+and REF_DEBUG have been renamed to Py_TRACE_REFS and Py_REF_DEBUG,
+respectively. At long last, the source actually compiles and links without
errors when this symbol is defined.
-- Several symbols that didn't follow the new naming scheme have been
-renamed (usually by adding to rename2.h) to use a Py or _Py prefix. There
-are no external symbols left without a Py or _Py prefix, not even those
-defined by sources that were incorporated from elsewhere (regexpr.c,
+- Several symbols that didn't follow the new naming scheme have been
+renamed (usually by adding to rename2.h) to use a Py or _Py prefix. There
+are no external symbols left without a Py or _Py prefix, not even those
+defined by sources that were incorporated from elsewhere (regexpr.c,
md5c.c). (Macros are a different story...)
-- There are now typedefs for the structures defined in config.c and
+- There are now typedefs for the structures defined in config.c and
frozen.c.
- New PYTHON_API_VERSION value and .pyc file magic number.
@@ -25689,125 +25722,125 @@ frozen.c.
- The binhex and binascii modules now actually work.
- The cgi module has been almost totally rewritten and documented.
-It now supports file upload and a new data type to handle forms more
+It now supports file upload and a new data type to handle forms more
flexibly.
- The formatter module (for use with htmllib) has been overhauled (again).
- The ftplib module now supports passive mode and has doc strings.
-- In (ideally) all places where binary files are read or written, the file
-is now correctly opened in binary mode ('rb' or 'wb') so the code will work
+- In (ideally) all places where binary files are read or written, the file
+is now correctly opened in binary mode ('rb' or 'wb') so the code will work
on Mac or PC.
-- Dummy versions of os.path.expandvars() and expanduser() are now provided
+- Dummy versions of os.path.expandvars() and expanduser() are now provided
on non-Unix platforms.
-- Module urllib now has two new functions url2pathname and pathname2url
-which turn local filenames into "file:..." URLs using the same rules as
-Netscape (why be different). it also supports urlretrieve() with a
-pathname parameter, and honors the proxy environment variables (http_proxy
+- Module urllib now has two new functions url2pathname and pathname2url
+which turn local filenames into "file:..." URLs using the same rules as
+Netscape (why be different). it also supports urlretrieve() with a
+pathname parameter, and honors the proxy environment variables (http_proxy
etc.). The URL parsing has been improved somewhat, too.
-- Micro improvements to urlparse. Added urlparse.urldefrag() which
+- Micro improvements to urlparse. Added urlparse.urldefrag() which
removes a trailing ``#fragment'' if any.
- The mailbox module now supports MH style message delimiters as well.
-- The mhlib module contains some new functionality: setcontext() to set the
-current folder and parsesequence() to parse a sequence as commonly passed
+- The mhlib module contains some new functionality: setcontext() to set the
+current folder and parsesequence() to parse a sequence as commonly passed
to MH commands (e.g. 1-10 or last:5).
-- New module mimify for conversion to and from MIME format of email
+- New module mimify for conversion to and from MIME format of email
messages.
-- Module ni now automatically installs itself when first imported -- this
-is against the normal rule that modules should define classes and functions
-but not invoke them, but appears more useful in the case that two
+- Module ni now automatically installs itself when first imported -- this
+is against the normal rule that modules should define classes and functions
+but not invoke them, but appears more useful in the case that two
different, independent modules want to use ni's features.
- Some small performance enhancements in module pickle.
-- Small interface change to the profile.run*() family of functions -- more
+- Small interface change to the profile.run*() family of functions -- more
sensible handling of return values.
-- The officially registered Mac creator for Python files is 'Pyth'. This
+- The officially registered Mac creator for Python files is 'Pyth'. This
replaces 'PYTH' which was used before but never registered.
- Added regsub.capwords(). (XXX)
-- Added string.capwords(), string.capitalize() and string.translate().
+- Added string.capwords(), string.capitalize() and string.translate().
(XXX)
-- Fixed an interface bug in the rexec module: it was impossible to pass a
-hooks instance to the RExec class. rexec now also supports the dynamic
-loading of modules from shared libraries. Some other interfaces have been
+- Fixed an interface bug in the rexec module: it was impossible to pass a
+hooks instance to the RExec class. rexec now also supports the dynamic
+loading of modules from shared libraries. Some other interfaces have been
added too.
-- Module rfc822 now caches the headers in a dictionary for more efficient
+- Module rfc822 now caches the headers in a dictionary for more efficient
lookup.
-- The sgmllib module now understands a limited number of SGML "shorthands"
+- The sgmllib module now understands a limited number of SGML "shorthands"
like <A/.../ for <A>...</A>. (It's not clear that this was a good idea...)
-- The tempfile module actually tries a number of different places to find a
-usable temporary directory. (This was prompted by certain Linux
-installations that appear to be missing a /usr/tmp directory.) [A bug in
-the implementation that would ignore a pre-existing tmpdir global has been
+- The tempfile module actually tries a number of different places to find a
+usable temporary directory. (This was prompted by certain Linux
+installations that appear to be missing a /usr/tmp directory.) [A bug in
+the implementation that would ignore a pre-existing tmpdir global has been
fixed in beta3.]
- Much improved and enhanved FileDialog module for Tkinter.
-- Many small changes to Tkinter, to bring it more in line with Tk 4.0 (as
+- Many small changes to Tkinter, to bring it more in line with Tk 4.0 (as
well as Tk 4.1).
-- New socket interfaces include ntohs(), ntohl(), htons(), htonl(), and
-s.dup(). Sockets now work correctly on Windows. On Windows, the built-in
-extension is called _socket and a wrapper module win/socket.py provides
-"makefile()" and "dup()" functionality. On Windows, the select module
+- New socket interfaces include ntohs(), ntohl(), htons(), htonl(), and
+s.dup(). Sockets now work correctly on Windows. On Windows, the built-in
+extension is called _socket and a wrapper module win/socket.py provides
+"makefile()" and "dup()" functionality. On Windows, the select module
works only with socket objects.
- Bugs in bsddb module fixed (e.g. missing default argument values).
- The curses extension now includes <ncurses.h> when available.
-- The gdbm module now supports opening databases in "fast" mode by
+- The gdbm module now supports opening databases in "fast" mode by
specifying 'f' as the second character or the mode string.
-- new variables sys.prefix and sys.exec_prefix pass corresponding
+- new variables sys.prefix and sys.exec_prefix pass corresponding
configuration options / Makefile variables to the Python programmer.
-- The ``new'' module now supports creating new user-defined classes as well
+- The ``new'' module now supports creating new user-defined classes as well
as instances thereof.
-- The soundex module now sports get_soundex() to get the soundex value for an
-arbitrary string (formerly it would only do soundex-based string
+- The soundex module now sports get_soundex() to get the soundex value for an
+arbitrary string (formerly it would only do soundex-based string
comparison) as well as doc strings.
-- New object type "cobject" to safely wrap void pointers for passing them
+- New object type "cobject" to safely wrap void pointers for passing them
between various extension modules.
- More efficient computation of float**smallint.
-- The mysterious bug whereby "x.x" (two occurrences of the same
-one-character name) typed from the commandline would sometimes fail
+- The mysterious bug whereby "x.x" (two occurrences of the same
+one-character name) typed from the commandline would sometimes fail
mysteriously.
-- The initialization of the readline function can now be invoked by a C
+- The initialization of the readline function can now be invoked by a C
extension through PyOS_ReadlineInit().
-- There's now an externally visible pointer PyImport_FrozenModules which
+- There's now an externally visible pointer PyImport_FrozenModules which
can be changed by an embedding application.
-- The argument parsing functions now support a new format character 'D' to
+- The argument parsing functions now support a new format character 'D' to
specify complex numbers.
- Various memory leaks plugged and bugs fixed.
-- Improved support for posix threads (now that real implementations are
+- Improved support for posix threads (now that real implementations are
beginning to apepar). Still no fully functioning semaphores.
-- Some various and sundry improvements and new entries in the Tools
+- Some various and sundry improvements and new entries in the Tools
directory.
@@ -27417,7 +27450,7 @@ it's now about 38).
The limit on the size of the *run-time* stack has completely been
removed -- this means that tuple or list displays can contain any
number of elements (formerly more than 50 would crash the
-interpreter).
+interpreter).
Changes to existing built-in functions and methods
diff --git a/Misc/NEWS b/Misc/NEWS
index b453fe4..f836c8d 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -2,10 +2,10 @@
Python News
+++++++++++
-What's New in Python 3.4.3?
-===========================
+What's New in Python 3.5 alpha 2?
+=================================
-Release date: 2015-02-22
+Release date: 2015-03-08
Core and Builtins
-----------------
@@ -16,28 +16,57 @@ Library
- Issue #22885: Fixed arbitrary code execution vulnerability in the dbm.dumb
module. Original patch by Claudiu Popa.
+- Issue #23239: ssl.match_hostname() now supports matching of IP addresses.
+
- Issue #23146: Fix mishandling of absolute Windows paths with forward
slashes in pathlib.
+- Issue #23096: Pickle representation of floats with protocol 0 now is the same
+ for both Python and C implementations.
+
+- Issue #19105: pprint now more efficiently uses free space at the right.
+
+- Issue #14910: Add allow_abbrev parameter to argparse.ArgumentParser. Patch by
+ Jonathan Paugh, Steven Bethard, paul j3 and Daniel Eriksson.
+
+- Issue #21717: tarfile.open() now supports 'x' (exclusive creation) mode.
+
+- Issue #23344: marshal.dumps() is now 20-25% faster on average.
+
+- Issue #20416: marshal.dumps() with protocols 3 and 4 is now 40-50% faster on
+ average.
+
- Issue #23421: Fixed compression in tarfile CLI. Patch by wdv4758h.
- Issue #23361: Fix possible overflow in Windows subprocess creation code.
+- logging.handlers.QueueListener now takes a respect_handler_level keyword
+ argument which, if set to True, will pass messages to handlers taking handler
+ levels into account.
+
Build
-----
- Issue #23445: pydebug builds now use "gcc -Og" where possible, to make
the resulting executable faster.
+Windows
+-------
+
+- Issue #23437: Make user scripts directory versioned on Windows. Patch by Paul
+ Moore.
+
-What's New in Python 3.4.3rc1?
-==============================
+What's New in Python 3.5 alpha 1?
+=================================
Release date: 2015-02-08
Core and Builtins
-----------------
+- Issue #23285: PEP 475 - EINTR handling.
+
- Issue #22735: Fix many edge cases (including crashes) involving custom mro()
implementations.
@@ -47,17 +76,46 @@ Core and Builtins
- Issue #21295: Revert some changes (issue #16795) to AST line numbers and
column offsets that constituted a regression.
+- Issue #22986: Allow changing an object's __class__ between a dynamic type and
+ static type in some cases.
+
+- Issue #15859: PyUnicode_EncodeFSDefault(), PyUnicode_EncodeMBCS() and
+ PyUnicode_EncodeCodePage() now raise an exception if the object is not an
+ Unicode object. For PyUnicode_EncodeFSDefault(), it was already the case on
+ platforms other than Windows. Patch written by Campbell Barton.
+
- Issue #21408: The default __ne__() now returns NotImplemented if __eq__()
returned NotImplemented. Original patch by Martin Panter.
- Issue #23321: Fixed a crash in str.decode() when error handler returned
replacment string longer than mailformed input data.
+- Issue #22286: The "backslashreplace" error handlers now works with
+ decoding and translating.
+
+- Issue #23253: Delay-load ShellExecute[AW] in os.startfile for reduced
+ startup overhead on Windows.
+
+- Issue #22038: pyatomic.h now uses stdatomic.h or GCC built-in functions for
+ atomic memory access if available. Patch written by Vitor de Lima and Gustavo
+ Temple.
+
+- Issue #20284: %-interpolation (aka printf) formatting added for bytes and
+ bytearray.
+
- Issue #23048: Fix jumping out of an infinite while loop in the pdb.
- Issue #20335: bytes constructor now raises TypeError when encoding or errors
is specified with non-string argument. Based on patch by Renaud Blanch.
+- Issue #22834: If the current working directory ends up being set to a
+ non-existent directory then import will no longer raise FileNotFoundError.
+
+- Issue #22869: Move the interpreter startup & shutdown code to a new
+ dedicated pylifecycle.c module
+
+- Issue #22847: Improve method cache efficiency.
+
- Issue #22335: Fix crash when trying to enlarge a bytearray to 0x7fffffff
bytes on a 32-bit platform.
@@ -67,12 +125,28 @@ Core and Builtins
- Issue #22643: Fix integer overflow in Unicode case operations (upper, lower,
title, swapcase, casefold).
+- Issue #17636: Circular imports involving relative imports are now
+ supported.
+
- Issue #22604: Fix assertion error in debug mode when dividing a complex
number by (nan+0j).
+- Issue #21052: Do not raise ImportWarning when sys.path_hooks or sys.meta_path
+ are set to None.
+
+- Issue #16518: Use 'bytes-like object required' in error messages that
+ previously used the far more cryptic "'x' does not support the buffer
+ protocol.
+
- Issue #22470: Fixed integer overflow issues in "backslashreplace",
"xmlcharrefreplace", and "surrogatepass" error handlers.
+- Issue #22540: speed up `PyObject_IsInstance` and `PyObject_IsSubclass` in the
+ common case that the second argument has metaclass `type`.
+
+- Issue #18711: Add a new `PyErr_FormatV` function, similar to `PyErr_Format`
+ but accepting a `va_list` argument.
+
- Issue #22520: Fix overflow checking when generating the repr of a unicode
object.
@@ -80,6 +154,138 @@ Core and Builtins
- Issue #22518: Fix integer overflow issues in latin-1 encoding.
+- Issue #16324: _charset parameter of MIMEText now also accepts
+ email.charset.Charset instances. Initial patch by Claude Paroz.
+
+- Issue #1764286: Fix inspect.getsource() to support decorated functions.
+ Patch by Claudiu Popa.
+
+- Issue #18554: os.__all__ includes posix functions.
+
+- Issue #21391: Use os.path.abspath in the shutil module.
+
+- Issue #11471: avoid generating a JUMP_FORWARD instruction at the end of
+ an if-block if there is no else-clause. Original patch by Eugene Toder.
+
+- Issue #22215: Now ValueError is raised instead of TypeError when str or bytes
+ argument contains not permitted null character or byte.
+
+- Issue #22258: Fix the internal function set_inheritable() on Illumos.
+ This platform exposes the function ``ioctl(FIOCLEX)``, but calling it fails
+ with errno is ENOTTY: "Inappropriate ioctl for device". set_inheritable()
+ now falls back to the slower ``fcntl()`` (``F_GETFD`` and then ``F_SETFD``).
+
+- Issue #21389: Displaying the __qualname__ of the underlying function in the
+ repr of a bound method.
+
+- Issue #22206: Using pthread, PyThread_create_key() now sets errno to ENOMEM
+ and returns -1 (error) on integer overflow.
+
+- Issue #20184: Argument Clinic based signature introspection added for
+ 30 of the builtin functions.
+
+- Issue #22116: C functions and methods (of the 'builtin_function_or_method'
+ type) can now be weakref'ed. Patch by Wei Wu.
+
+- Issue #22077: Improve index error messages for bytearrays, bytes, lists,
+ and tuples by adding 'or slices'. Added ', not <typename' for bytearrays.
+ Original patch by Claudiu Popa.
+
+- Issue #20179: Apply Argument Clinic to bytes and bytearray.
+ Patch by Tal Einat.
+
+- Issue #22082: Clear interned strings in slotdefs.
+
+- Upgrade Unicode database to Unicode 7.0.0.
+
+- Issue #21897: Fix a crash with the f_locals attribute with closure
+ variables when frame.clear() has been called.
+
+- Issue #21205: Add a new ``__qualname__`` attribute to generator, the
+ qualified name, and use it in the representation of a generator
+ (``repr(gen)``). The default name of the generator (``__name__`` attribute)
+ is now get from the function instead of the code. Use ``gen.gi_code.co_name``
+ to get the name of the code.
+
+- Issue #21669: With the aid of heuristics in SyntaxError.__init__, the
+ parser now attempts to generate more meaningful (or at least more search
+ engine friendly) error messages when "exec" and "print" are used as
+ statements.
+
+- Issue #21642: If the conditional if-else expression, allow an integer written
+ with no space between itself and the ``else`` keyword (e.g. ``True if 42else
+ False``) to be valid syntax.
+
+- Issue #21523: Fix over-pessimistic computation of the stack effect of
+ some opcodes in the compiler. This also fixes a quadratic compilation
+ time issue noticeable when compiling code with a large number of "and"
+ and "or" operators.
+
+- Issue #21418: Fix a crash in the builtin function super() when called without
+ argument and without current frame (ex: embedded Python).
+
+- Issue #21425: Fix flushing of standard streams in the interactive
+ interpreter.
+
+- Issue #21435: In rare cases, when running finalizers on objects in cyclic
+ trash a bad pointer dereference could occur due to a subtle flaw in
+ internal iteration logic.
+
+- Issue #21377: PyBytes_Concat() now tries to concatenate in-place when the
+ first argument has a reference count of 1. Patch by Nikolaus Rath.
+
+- Issue #20355: -W command line options now have higher priority than the
+ PYTHONWARNINGS environment variable. Patch by Arfrever.
+
+- Issue #21274: Define PATH_MAX for GNU/Hurd in Python/pythonrun.c.
+
+- Issue #20904: Support setting FPU precision on m68k.
+
+- Issue #21209: Fix sending tuples to custom generator objects with the yield
+ from syntax.
+
+- Issue #21193: pow(a, b, c) now raises ValueError rather than TypeError when b
+ is negative. Patch by Josh Rosenberg.
+
+- PEP 465 and Issue #21176: Add the '@' operator for matrix multiplication.
+
+- Issue #21134: Fix segfault when str is called on an uninitialized
+ UnicodeEncodeError, UnicodeDecodeError, or UnicodeTranslateError object.
+
+- Issue #19537: Fix PyUnicode_DATA() alignment under m68k. Patch by
+ Andreas Schwab.
+
+- Issue #20929: Add a type cast to avoid shifting a negative number.
+
+- Issue #20731: Properly position in source code files even if they
+ are opened in text mode. Patch by Serhiy Storchaka.
+
+- Issue #20637: Key-sharing now also works for instance dictionaries of
+ subclasses. Patch by Peter Ingebretson.
+
+- Issue #8297: Attributes missing from modules now include the module name
+ in the error text. Original patch by ysj.ray.
+
+- Issue #19995: %c, %o, %x, and %X now raise TypeError on non-integer input.
+
+- Issue #19655: The ASDL parser - used by the build process to generate code for
+ managing the Python AST in C - was rewritten. The new parser is self contained
+ and does not require to carry long the spark.py parser-generator library;
+ spark.py was removed from the source base.
+
+- Issue #12546: Allow \x00 to be used as a fill character when using str, int,
+ float, and complex __format__ methods.
+
+- Issue #20480: Add ipaddress.reverse_pointer. Patch by Leon Weber.
+
+- Issue #13598: Modify string.Formatter to support auto-numbering of
+ replacement fields. It now matches the behavior of str.format() in
+ this regard. Patches by Phil Elson and Ramchandra Apte.
+
+- Issue #8931: Make alternate formatting ('#') for type 'c' raise an
+ exception. In versions prior to 3.5, '#' with 'c' had no effect. Now
+ specifying it is an error. Patch by Torsten Landschoff.
+
- Issue #23165: Perform overflow checks before allocating memory in the
_Py_char2wchar function.
@@ -88,9 +294,24 @@ Library
- Issue #23399: pyvenv creates relative symlinks where possible.
+- Issue #20289: cgi.FieldStorage() now supports the context management
+ protocol.
+
+- Issue #13128: Print response headers for CONNECT requests when debuglevel
+ > 0. Patch by Demian Brecht.
+
+- Issue #15381: Optimized io.BytesIO to make less allocations and copyings.
+
+- Issue #22818: Splitting on a pattern that could match an empty string now
+ raises a warning. Patterns that can only match empty strings are now
+ rejected.
+
- Issue #23099: Closing io.BytesIO with exported buffer is rejected now to
prevent corrupting exported buffer.
+- Issue #23326: Removed __ne__ implementations. Since fixing default __ne__
+ implementation in issue #21408 they are redundant.
+
- Issue #23363: Fix possible overflow in itertools.permutations.
- Issue #23364: Fix possible overflow in itertools.product.
@@ -108,6 +329,14 @@ Library
is now always restored or swapped, not only if why is WHY_YIELD or
WHY_RETURN. Patch co-written with Antoine Pitrou.
+- Issue #14099: Restored support of writing ZIP files to tellable but
+ non-seekable streams.
+
+- Issue #14099: Writing to ZipFile and reading multiple ZipExtFiles is
+ threadsafe now.
+
+- Issue #19361: JSON decoder now raises JSONDecodeError instead of ValueError.
+
- Issue #18518: timeit now rejects statements which can't be compiled outside
a function or a loop (e.g. "return" or "break").
@@ -122,47 +351,78 @@ Library
- Issue #19996: :class:`email.feedparser.FeedParser` now handles (malformed)
headers with no key rather than amusing the body has started.
+- Issue #20188: Support Application-Layer Protocol Negotiation (ALPN) in the ssl
+ module.
+
+- Issue #23133: Pickling of ipaddress objects now produces more compact and
+ portable representation.
+
- Issue #23248: Update ssl error codes from latest OpenSSL git master.
+- Issue #23266: Much faster implementation of ipaddress.collapse_addresses()
+ when there are many non-consecutive addresses.
+
- Issue #23098: 64-bit dev_t is now supported in the os module.
+- Issue #21817: When an exception is raised in a task submitted to a
+ ProcessPoolExecutor, the remote traceback is now displayed in the
+ parent process. Patch by Claudiu Popa.
+
+- Issue #15955: Add an option to limit output size when decompressing LZMA
+ data. Patch by Nikolaus Rath and Martin Panter.
+
- Issue #23250: In the http.cookies module, capitalize "HttpOnly" and "Secure"
as they are written in the standard.
- Issue #23063: In the disutils' check command, fix parsing of reST with code or
code-block directives.
-- Issue #23209, #23225: selectors.BaseSelector.close() now clears its internal
- reference to the selector mapping to break a reference cycle. Initial patch
- written by Martin Richard.
+- Issue #23209, #23225: selectors.BaseSelector.get_key() now raises a
+ RuntimeError if the selector is closed. And selectors.BaseSelector.close()
+ now clears its internal reference to the selector mapping to break a
+ reference cycle. Initial patch written by Martin Richard.
-- Issue #21356: Make ssl.RAND_egd() optional to support LibreSSL. The
- availability of the function is checked during the compilation. Patch written
- by Bernard Spil.
+- Issue #19777: Provide a home() classmethod on Path objects. Contributed
+ by Victor Salgado and Mayank Tripathi.
-- Issue #20896, #22935: The :func:`ssl.get_server_certificate` function now
- uses the :data:`~ssl.PROTOCOL_SSLv23` protocol by default, not
- :data:`~ssl.PROTOCOL_SSLv3`, for maximum compatibility and support platforms
- where :data:`~ssl.PROTOCOL_SSLv3` support is disabled.
+- Issue #23206: Make ``json.dumps(..., ensure_ascii=False)`` as fast as the
+ default case of ``ensure_ascii=True``. Patch by Naoki Inada.
-- Issue #23111: In the ftplib, make ssl.PROTOCOL_SSLv23 the default protocol
- version.
+- Issue #23185: Add math.inf and math.nan constants.
-- Issue #23132: Mitigate regression in speed and clarity in functools.total_ordering.
+- Issue #23186: Add ssl.SSLObject.shared_ciphers() and
+ ssl.SSLSocket.shared_ciphers() to fetch the client's list ciphers sent at
+ handshake.
-- Issue #22585: On OpenBSD 5.6 and newer, os.urandom() now calls getentropy(),
- instead of reading /dev/urandom, to get pseudo-random bytes.
+- Issue #23143: Remove compatibility with OpenSSLs older than 0.9.8.
+
+- Issue #23132: Improve performance and introspection support of comparison
+ methods created by functool.total_ordering.
+
+- Issue #19776: Add a expanduser() method on Path objects.
- Issue #23112: Fix SimpleHTTPServer to correctly carry the query string and
fragment when it redirects to add a trailing slash.
+- Issue #21793: Added http.HTTPStatus enums (i.e. HTTPStatus.OK,
+ HTTPStatus.NOT_FOUND). Patch by Demian Brecht.
+
- Issue #23093: In the io, module allow more operations to work on detached
streams.
+- Issue #23111: In the ftplib, make ssl.PROTOCOL_SSLv23 the default protocol
+ version.
+
+- Issue #22585: On OpenBSD 5.6 and newer, os.urandom() now calls getentropy(),
+ instead of reading /dev/urandom, to get pseudo-random bytes.
+
- Issue #19104: pprint now produces evaluable output for wrapped strings.
- Issue #23071: Added missing names to codecs.__all__. Patch by Martin Panter.
+- Issue #22783: Pickling now uses the NEWOBJ opcode instead of the NEWOBJ_EX
+ opcode if possible.
+
- Issue #15513: Added a __sizeof__ implementation for pickle classes.
- Issue #19858: pickletools.optimize() now aware of the MEMOIZE opcode, can
@@ -184,12 +444,25 @@ Library
- Issue #1218234: Fix inspect.getsource() to load updated source of
reloaded module. Initial patch by Berker Peksag.
+- Issue #21740: Support wrapped callables in doctest. Patch by Claudiu Popa.
+
+- Issue #23009: Make sure selectors.EpollSelecrtor.select() works when no
+ FD is registered.
+
- Issue #22959: In the constructor of http.client.HTTPSConnection, prefer the
context's check_hostname attribute over the *check_hostname* parameter.
+- Issue #22696: Add function :func:`sys.is_finalizing` to know about
+ interpreter shutdown.
+
- Issue #16043: Add a default limit for the amount of data xmlrpclib.gzip_decode
will return. This resolves CVE-2013-1753.
+- Issue #14099: ZipFile.open() no longer reopen the underlying file. Objects
+ returned by ZipFile.open() can now operate independently of the ZipFile even
+ if the ZipFile was created by passing in a file-like object as the first
+ argument to the constructor.
+
- Issue #22966: Fix __pycache__ pyc file name clobber when pyc_compile is
asked to compile a source file containing multiple dots in the source file
name.
@@ -199,14 +472,30 @@ Library
- Issue #21032. Fixed socket leak if HTTPConnection.getresponse() fails.
Original patch by Martin Panter.
+- Issue #22407: Deprecated the use of re.LOCALE flag with str patterns or
+ re.ASCII. It was newer worked.
+
+- Issue #22902: The "ip" command is now used on Linux to determine MAC address
+ in uuid.getnode(). Pach by Bruno Cauet.
+
- Issue #22960: Add a context argument to xmlrpclib.ServerProxy constructor.
+- Issue #22389: Add contextlib.redirect_stderr().
+
+- Issue #21356: Make ssl.RAND_egd() optional to support LibreSSL. The
+ availability of the function is checked during the compilation. Patch written
+ by Bernard Spil.
+
- Issue #22915: SAX parser now supports files opened with file descriptor or
bytes path.
- Issue #22609: Constructors and update methods of mapping classes in the
collections module now accept the self keyword argument.
+- Issue #22940: Add readline.append_history_file.
+
+- Issue #19676: Added the "namereplace" error handler.
+
- Issue #22788: Add *context* parameter to logging.handlers.HTTPHandler.
- Issue #22921: Allow SSLContext to take the *hostname* parameter even if
@@ -215,14 +504,19 @@ Library
- Issue #22894: TestCase.subTest() would cause the test suite to be stopped
when in failfast mode, even in the absence of failures.
-- Issue #22638: SSLv3 is now disabled throughout the standard library.
- It can still be enabled by instantiating a SSLContext manually.
+- Issue #22796: HTTP cookie parsing is now stricter, in order to protect
+ against potential injection attacks.
- Issue #22370: Windows detection in pathlib is now more robust.
- Issue #22841: Reject coroutines in asyncio add_signal_handler().
Patch by Ludovic.Gasc.
+- Issue #19494: Added urllib.request.HTTPBasicPriorAuthHandler. Patch by
+ Matej Cepl.
+
+- Issue #22578: Added attributes to the re.error class.
+
- Issue #22849: Fix possible double free in the io.TextIOWrapper constructor.
- Issue #12728: Different Unicode characters having the same uppercase but
@@ -231,6 +525,14 @@ Library
- Issue #22821: Fixed fcntl() with integer argument on 64-bit big-endian
platforms.
+- Issue #21650: Add an `--sort-keys` option to json.tool CLI.
+
+- Issue #22824: Updated reprlib output format for sets to use set literals.
+ Patch contributed by Berker Peksag.
+
+- Issue #22824: Updated reprlib output format for arrays to display empty
+ arrays without an unnecessary empty list. Suggested by Serhiy Storchaka.
+
- Issue #22406: Fixed the uu_codec codec incorrectly ported to 3.x.
Based on patch by Martin Panter.
@@ -244,16 +546,15 @@ Library
- Issue #22775: Fixed unpickling of http.cookies.SimpleCookie with protocol 2
and above. Patch by Tim Graham.
-- Issue #22366: urllib.request.urlopen will accept a context object
- (SSLContext) as an argument which will then used be for HTTPS connection.
- Patch by Alex Gaynor.
-
- Issue #22776: Brought excluded code into the scope of a try block in
SysLogHandler.emit().
- Issue #22665: Add missing get_terminal_size and SameFileError to
shutil.__all__.
+- Issue #6623: Remove deprecated Netrc class in the ftplib module. Patch by
+ Matt Chaput.
+
- Issue #17381: Fixed handling of case-insensitive ranges in regular
expressions.
@@ -267,18 +568,53 @@ Library
doesn't work. This allows use with special filesystems such as VirtualBox
shared folders.
+- Issue #22217: Implemented reprs of classes in the zipfile module.
+
+- Issue #22457: Honour load_tests in the start_dir of discovery.
+
+- Issue #18216: gettext now raises an error when a .mo file has an
+ unsupported major version number. Patch by Aaron Hill.
+
+- Issue #13918: Provide a locale.delocalize() function which can remove
+ locale-specific number formatting from a string representing a number,
+ without then converting it to a specific type. Patch by Cédric Krier.
+
+- Issue #22676: Make the pickling of global objects which don't have a
+ __module__ attribute less slow.
+
- Issue #18853: Fixed ResourceWarning in shlex.__nain__.
- Issue #9351: Defaults set with set_defaults on an argparse subparser
are no longer ignored when also set on the parent parser.
+- Issue #7559: unittest test loading ImportErrors are reported as import errors
+ with their import exception rather than as attribute errors after the import
+ has already failed.
+
+- Issue #19746: Make it possible to examine the errors from unittest
+ discovery without executing the test suite. The new `errors` attribute
+ on TestLoader exposes these non-fatal errors encountered during discovery.
+
- Issue #21991: Make email.headerregistry's header 'params' attributes
be read-only (MappingProxyType). Previously the dictionary was modifiable
but a new one was created on each access of the attribute.
+- Issue #22638: SSLv3 is now disabled throughout the standard library.
+ It can still be enabled by instantiating a SSLContext manually.
+
- Issue #22641: In asyncio, the default SSL context for client connections
is now created using ssl.create_default_context(), for stronger security.
+- Issue #17401: Include closefd in io.FileIO repr.
+
+- Issue #21338: Add silent mode for compileall. quiet parameters of
+ compile_{dir, file, path} functions now have a multilevel value. Also,
+ -q option of the CLI now have a multilevel value. Patch by Thomas Kluyver.
+
+- Issue #20152: Convert the array and cmath modules to Argument Clinic.
+
+- Issue #18643: Add socket.socketpair() on Windows.
+
- Issue #22435: Fix a file descriptor leak when SocketServer bind fails.
- Issue #13096: Fixed segfault in CTypes POINTER handling of large
@@ -287,9 +623,19 @@ Library
- Issue #11694: Raise ConversionError in xdrlib as documented. Patch
by Filip Gruszczyński and Claudiu Popa.
+- Issue #19380: Optimized parsing of regular expressions.
+
+- Issue #1519638: Now unmatched groups are replaced with empty strings in re.sub()
+ and re.subn().
+
+- Issue #18615: sndhdr.what/whathdr now return a namedtuple.
+
- Issue #22462: Fix pyexpat's creation of a dummy frame to make it
appear in exception tracebacks.
+- Issue #21965: Add support for in-memory SSL to the ssl module. Patch
+ by Geert Jansen.
+
- Issue #21173: Fix len() on a WeakKeyDictionary when .clear() was called
with an iterator alive.
@@ -299,165 +645,65 @@ Library
- Issue #21905: Avoid RuntimeError in pickle.whichmodule() when sys.modules
is mutated while iterating. Patch by Olivier Grisel.
+- Issue #11271: concurrent.futures.Executor.map() now takes a *chunksize*
+ argument to allow batching of tasks in child processes and improve
+ performance of ProcessPoolExecutor. Patch by Dan O'Reilly.
+
+- Issue #21883: os.path.join() and os.path.relpath() now raise a TypeError with
+ more helpful error message for unsupported or mismatched types of arguments.
+
- Issue #22219: The zipfile module CLI now adds entries for directories
(including empty directories) in ZIP file.
- Issue #22449: In the ssl.SSLContext.load_default_certs, consult the
enviromental variables SSL_CERT_DIR and SSL_CERT_FILE on Windows.
+- Issue #22508: The email.__version__ variable has been removed; the email
+ code is no longer shipped separately from the stdlib, and __version__
+ hasn't been updated in several releases.
+
- Issue #20076: Added non derived UTF-8 aliases to locale aliases table.
- Issue #20079: Added locales supported in glibc 2.18 to locale alias table.
-- Issue #22396: On 32-bit AIX platform, don't expose os.posix_fadvise() nor
- os.posix_fallocate() because their prototypes in system headers are wrong.
-
-- Issue #22517: When a io.BufferedRWPair object is deallocated, clear its
- weakrefs.
-
-- Issue #22448: Improve canceled timer handles cleanup to prevent
- unbound memory usage. Patch by Joshua Moore-Oliva.
-
-- Issue #23009: Make sure selectors.EpollSelecrtor.select() works when no
- FD is registered.
-
-IDLE
-----
-
-- Issue #20577: Configuration of the max line length for the FormatParagraph
- extension has been moved from the General tab of the Idle preferences dialog
- to the FormatParagraph tab of the Config Extensions dialog.
- Patch by Tal Einat.
-
-- Issue #16893: Update Idle doc chapter to match current Idle and add new
- information.
-
-- Issue #3068: Add Idle extension configuration dialog to Options menu.
- Changes are written to HOME/.idlerc/config-extensions.cfg.
- Original patch by Tal Einat.
+- Issue #20218: Added convenience methods read_text/write_text and read_bytes/
+ write_bytes to pathlib.Path objects.
-- Issue #16233: A module browser (File : Class Browser, Alt+C) requires a
- editor window with a filename. When Class Browser is requested otherwise,
- from a shell, output window, or 'Untitled' editor, Idle no longer displays
- an error box. It now pops up an Open Module box (Alt+M). If a valid name
- is entered and a module is opened, a corresponding browser is also opened.
+- Issue #22437: Number of capturing groups in regular expression is no longer
+ limited by 100.
-- Issue #4832: Save As to type Python files automatically adds .py to the
- name you enter (even if your system does not display it). Some systems
- automatically add .txt when type is Text files.
-
-- Issue #21986: Code objects are not normally pickled by the pickle module.
- To match this, they are no longer pickled when running under Idle.
-
-- Issue #23180: Rename IDLE "Windows" menu item to "Window".
- Patch by Al Sweigart.
-
-Tests
------
+- Issue #17442: InteractiveInterpreter now displays the full chained traceback
+ in its showtraceback method, to match the built in interactive interpreter.
- Issue #23392: Added tests for marshal C API that works with FILE*.
-- Issue #18982: Add tests for CLI of the calendar module.
-
-- Issue #19548: Added some additional checks to test_codecs to ensure that
- statements in the updated documentation remain accurate. Patch by Martin
- Panter.
-
-- Issue #22838: All test_re tests now work with unittest test discovery.
-
-- Issue #22173: Update lib2to3 tests to use unittest test discovery.
-
-- Issue #16000: Convert test_curses to use unittest.
-
-- Issue #21456: Skip two tests in test_urllib2net.py if _ssl module not
- present. Patch by Remi Pointel.
-
-- Issue #22770: Prevent some Tk segfaults on OS X when running gui tests.
-
-- Issue #23211: Workaround test_logging failure on some OS X 10.6 systems.
-
-- Issue #23345: Prevent test_ssl failures with large OpenSSL patch level
- values (like 0.9.8zc).
-
-Build
------
-
-- Issue #15506: Use standard PKG_PROG_PKG_CONFIG autoconf macro in the configure
- script.
-
-- Issue #22935: Allow the ssl module to be compiled if openssl doesn't support
- SSL 3.
-
-- Issue #16537: Check whether self.extensions is empty in setup.py. Patch by
- Jonathan Hosmer.
-
-- Issue #18096: Fix library order returned by python-config.
-
-- Issue #17219: Add library build dir for Python extension cross-builds.
-
-- Issue #17128: Use private version of OpenSSL for 3.4.3 OS X 10.5+ installer.
-
-C API
------
-
-- Issue #22079: PyType_Ready() now checks that statically allocated type has
- no dynamically allocated bases.
-
-Documentation
--------------
-
-- Issue #19548: Update the codecs module documentation to better cover the
- distinction between text encodings and other codecs, together with other
- clarifications. Patch by Martin Panter.
-
-- Issue #22914: Update the Python 2/3 porting HOWTO to describe a more automated
- approach.
-
-- Issue #21514: The documentation of the json module now refers to new JSON RFC
- 7159 instead of obsoleted RFC 4627.
-
-Tools/Demos
------------
-
-- Issue #22314: pydoc now works when the LINES environment variable is set.
-
-Windows
--------
-
-- Issue #17896: The Windows build scripts now expect external library sources
- to be in ``PCbuild\..\externals`` rather than ``PCbuild\..\..``.
-
-- Issue #17717: The Windows build scripts now use a copy of NASM pulled from
- svn.python.org to build OpenSSL.
-
-- Issue #22644: The bundled version of OpenSSL has been updated to 1.0.1j.
-
-What's New in Python 3.4.2?
-===========================
-
-Release date: 2014-10-06
-
-Core and Builtins
------------------
-
-Library
--------
-
- Issue #10510: distutils register and upload methods now use HTML standards
compliant CRLF line endings.
- Issue #9850: Fixed macpath.join() for empty first component. Patch by
Oleg Oshmyan.
+- Issue #5309: distutils' build and build_ext commands now accept a ``-j``
+ option to enable parallel building of extension modules.
+
+- Issue #22448: Improve canceled timer handles cleanup to prevent
+ unbound memory usage. Patch by Joshua Moore-Oliva.
+
- Issue #22427: TemporaryDirectory no longer attempts to clean up twice when
used in the with statement in generator.
+- Issue #22362: Forbidden ambiguous octal escapes out of range 0-0o377 in
+ regular expressions.
+
- Issue #20912: Now directories added to ZIP file have correct Unix and MS-DOS
directory attributes.
- Issue #21866: ZipFile.close() no longer writes ZIP64 central directory
records if allowZip64 is false.
+- Issue #22278: Fix urljoin problem with relative urls, a regression observed
+ after changes to issue22118 were submitted.
+
- Issue #22415: Fixed debugging output of the GROUPREF_EXISTS opcode in the re
module. Removed trailing spaces in debugging output.
@@ -467,47 +713,18 @@ Library
- Issue #21332: Ensure that ``bufsize=1`` in subprocess.Popen() selects
line buffering, rather than block buffering. Patch by Akira Li.
-
-What's New in Python 3.4.2rc1?
-==============================
-
-Release date: 2014-09-22
-
-Core and Builtins
------------------
-
-- Issue #22258: Fix the the internal function set_inheritable() on Illumos.
- This platform exposes the function ``ioctl(FIOCLEX)``, but calling it fails
- with errno is ENOTTY: "Inappropriate ioctl for device". set_inheritable()
- now falls back to the slower ``fcntl()`` (``F_GETFD`` and then ``F_SETFD``).
-
-- Issue #21669: With the aid of heuristics in SyntaxError.__init__, the
- parser now attempts to generate more meaningful (or at least more search
- engine friendly) error messages when "exec" and "print" are used as
- statements.
-
-- Issue #21642: If the conditional if-else expression, allow an integer written
- with no space between itself and the ``else`` keyword (e.g. ``True if 42else
- False``) to be valid syntax.
-
-- Issue #21523: Fix over-pessimistic computation of the stack effect of
- some opcodes in the compiler. This also fixes a quadratic compilation
- time issue noticeable when compiling code with a large number of "and"
- and "or" operators.
-
-Library
--------
-
- Issue #21091: Fix API bug: email.message.EmailMessage.is_attachment is now
- a method. Since EmailMessage is provisional, we can change the API in a
- maintenance release, but we use a trick to remain backward compatible with
- 3.4.0/1.
+ a method.
- Issue #21079: Fix email.message.EmailMessage.is_attachment to return the
correct result when the header has parameters as well as a value.
- Issue #22247: Add NNTPError to nntplib.__all__.
+- Issue #22366: urllib.request.urlopen will accept a context object
+ (SSLContext) as an argument which will then used be for HTTPS connection.
+ Patch by Alex Gaynor.
+
- Issue #4180: The warnings registries are now reset when the filters
are modified.
@@ -519,6 +736,9 @@ Library
with non-standard cookie handling in some Web browsers. Reported by
Sergey Bobrov.
+- Issue #20537: logging methods now accept an exception instance as well as a
+ Boolean value or exception tuple. Thanks to Yury Selivanov for the patch.
+
- Issue #22384: An exception in Tkinter callback no longer crashes the program
when it is run with pythonw.exe.
@@ -527,6 +747,9 @@ Library
- Issue #21147: sqlite3 now raises an exception if the request contains a null
character instead of truncate it. Based on patch by Victor Stinner.
+- Issue #13968: The glob module now supports recursive search in
+ subdirectories using the "**" pattern.
+
- Issue #21951: Fixed a crash in Tkinter on AIX when called Tcl command with
empty string or tuple argument.
@@ -535,12 +758,31 @@ Library
- Issue #22338: Fix a crash in the json module on memory allocation failure.
+- Issue #12410: imaplib.IMAP4 now supports the context management protocol.
+ Original patch by Tarek Ziadé.
+
+- Issue #21270: We now override tuple methods in mock.call objects so that
+ they can be used as normal call attributes.
+
+- Issue #16662: load_tests() is now unconditionally run when it is present in
+ a package's __init__.py. TestLoader.loadTestsFromModule() still accepts
+ use_load_tests, but it is deprecated and ignored. A new keyword-only
+ attribute `pattern` is added and documented. Patch given by Robert Collins,
+ tweaked by Barry Warsaw.
+
- Issue #22226: First letter no longer is stripped from the "status" key in
the result of Treeview.heading().
- Issue #19524: Fixed resource leak in the HTTP connection when an invalid
response is received. Patch by Martin Panter.
+- Issue #20421: Add a .version() method to SSL sockets exposing the actual
+ protocol version in use.
+
+- Issue #19546: configparser exceptions no longer expose implementation details.
+ Chained KeyErrors are removed, which leads to cleaner tracebacks. Patch by
+ Claudiu Popa.
+
- Issue #22051: turtledemo no longer reloads examples to re-run them.
Initialization of variables and gui setup should be done in main(),
which is called each time a demo is run, but not on import.
@@ -557,23 +799,57 @@ Library
- Issue #18132: Turtledemo buttons no longer disappear when the window is
shrunk. Original patches by Jan Kanis and Lita Cho.
+- Issue #22043: time.monotonic() is now always available.
+ ``threading.Lock.acquire()``, ``threading.RLock.acquire()`` and socket
+ operations now use a monotonic clock, instead of the system clock, when a
+ timeout is used.
+
+- Issue #21527: Add a default number of workers to ThreadPoolExecutor equal
+ to 5 times the number of CPUs. Patch by Claudiu Popa.
+
- Issue #22216: smtplib now resets its state more completely after a quit. The
most obvious consequence of the previous behavior was a STARTTLS failure
during a connect/starttls/quit/connect/starttls sequence.
+- Issue #22098: ctypes' BigEndianStructure and LittleEndianStructure now
+ define an empty __slots__ so that subclasses don't always get an instance
+ dict. Patch by Claudiu Popa.
+
- Issue #22185: Fix an occasional RuntimeError in threading.Condition.wait()
caused by mutation of the waiters queue without holding the lock. Patch
by Doug Zongker.
+- Issue #22287: On UNIX, _PyTime_gettimeofday() now uses
+ clock_gettime(CLOCK_REALTIME) if available. As a side effect, Python now
+ depends on the librt library on Solaris and on Linux (only with glibc older
+ than 2.17).
+
- Issue #22182: Use e.args to unpack exceptions correctly in
distutils.file_util.move_file. Patch by Claudiu Popa.
- The webbrowser module now uses subprocess's start_new_session=True rather
than a potentially risky preexec_fn=os.setsid call.
+- Issue #22042: signal.set_wakeup_fd(fd) now raises an exception if the file
+ descriptor is in blocking mode.
+
+- Issue #16808: inspect.stack() now returns a named tuple instead of a tuple.
+ Patch by Daniel Shahaf.
+
- Issue #22236: Fixed Tkinter images copying operations in NoDefaultRoot mode.
-- Issue #22191: Fix warnings.__all__.
+- Issue #2527: Add a *globals* argument to timeit functions, in order to
+ override the globals namespace in which the timed code is executed.
+ Patch by Ben Roberts.
+
+- Issue #22118: Switch urllib.parse to use RFC 3986 semantics for the
+ resolution of relative URLs, rather than RFCs 1808 and 2396.
+ Patch by Demian Brecht.
+
+- Issue #21549: Added the "members" parameter to TarFile.list().
+
+- Issue #19628: Allow compileall recursion depth to be specified with a -r
+ option.
- Issue #15696: Add a __sizeof__ implementation for mmap objects on Windows.
@@ -581,6 +857,8 @@ Library
- Issue #22165: SimpleHTTPRequestHandler now supports undecodable file names.
+- Issue #15381: Optimized line reading in io.BytesIO.
+
- Issue #8797: Raise HTTPError on failed Basic Authentication immediately.
Initial patch by Sam Bull.
@@ -596,6 +874,14 @@ Library
- Issue #17923: glob() patterns ending with a slash no longer match non-dirs on
AIX. Based on patch by Delhallt.
+- Issue #21725: Added support for RFC 6531 (SMTPUTF8) in smtpd.
+
+- Issue #22176: Update the ctypes module's libffi to v3.1. This release
+ adds support for the Linux AArch64 and POWERPC ELF ABIv2 little endian
+ architectures.
+
+- Issue #5411: Added support for the "xztar" format in the shutil module.
+
- Issue #21121: Don't force 3rd party C extensions to be built with
-Werror=declaration-after-statement.
@@ -603,18 +889,57 @@ Library
when unpickling pickled sqlite3.Row). sqlite3.Row is now initialized in the
__new__() method.
+- Issue #20170: Convert posixmodule to use Argument Clinic.
+
+- Issue #21539: Add a *exists_ok* argument to `Pathlib.mkdir()` to mimic
+ `mkdir -p` and `os.makedirs()` functionality. When true, ignore
+ FileExistsErrors. Patch by Berker Peksag.
+
+- Issue #22127: Bypass IDNA for pure-ASCII host names in the socket module
+ (in particular for numeric IPs).
+
+- Issue #21047: set the default value for the *convert_charrefs* argument
+ of HTMLParser to True. Patch by Berker Peksag.
+
+- Add an __all__ to html.entities.
+
+- Issue #15114: the strict mode and argument of HTMLParser, HTMLParser.error,
+ and the HTMLParserError exception have been removed.
+
+- Issue #22085: Dropped support of Tk 8.3 in Tkinter.
+
- Issue #21580: Now Tkinter correctly handles bytes arguments passed to Tk.
In particular this allows to initialize images from binary data.
+- Issue #22003: When initialized from a bytes object, io.BytesIO() now
+ defers making a copy until it is mutated, improving performance and
+ memory use on some use cases. Patch by David Wilson.
+
+- Issue #22018: On Windows, signal.set_wakeup_fd() now also supports sockets.
+ A side effect is that Python depends to the WinSock library.
+
+- Issue #22054: Add os.get_blocking() and os.set_blocking() functions to get
+ and set the blocking mode of a file descriptor (False if the O_NONBLOCK flag
+ is set, True otherwise). These functions are not available on Windows.
+
- Issue #17172: Make turtledemo start as active on OS X even when run with
subprocess. Patch by Lita Cho.
- Issue #21704: Fix build error for _multiprocessing when semaphores
are not available. Patch by Arfrever Frehtes Taifersar Arahesis.
+- Issue #20173: Convert sha1, sha256, sha512 and md5 to ArgumentClinic.
+ Patch by Vajrasky Kok.
+
- Fix repr(_socket.socket) on Windows 64-bit: don't fail with OverflowError
on closed socket. repr(socket.socket) already works fine.
+- Issue #22033: Reprs of most Python implemened classes now contain actual
+ class name instead of hardcoded one.
+
+- Issue #21947: The dis module can now disassemble generator-iterator
+ objects based on their gi_code attribute. Patch by Clement Rouault.
+
- Issue #16133: The asynchat.async_chat.handle_read() method now ignores
BlockingIOError exceptions.
@@ -626,16 +951,42 @@ Library
sequence is used on some terminal (ex: TERM=xterm-256color") to enable
support of 8 bit characters.
+- Issue #4350: Removed a number of out-of-dated and non-working for a long time
+ Tkinter methods.
+
+- Issue #6167: Scrollbar.activate() now returns the name of active element if
+ the argument is not specified. Scrollbar.set() now always accepts only 2
+ arguments.
+
+- Issue #15275: Clean up and speed up the ntpath module.
+
- Issue #21888: plistlib's load() and loads() now work if the fmt parameter is
specified.
+- Issue #22032: __qualname__ instead of __name__ is now always used to format
+ fully qualified class names of Python implemented classes.
+
+- Issue #22031: Reprs now always use hexadecimal format with the "0x" prefix
+ when contain an id in form " at 0x...".
+
+- Issue #22018: signal.set_wakeup_fd() now raises an OSError instead of a
+ ValueError on ``fstat()`` failure.
+
- Issue #21044: tarfile.open() now handles fileobj with an integer 'name'
attribute. Based on patch by Antoine Pietri.
-- Issue #21867: Prevent turtle crash due to invalid undo buffer size.
+- Issue #21966: Respect -q command-line option when code module is ran.
- Issue #19076: Don't pass the redundant 'file' argument to self.error().
+- Issue #16382: Improve exception message of warnings.warn() for bad
+ category. Initial patch by Phil Elson.
+
+- Issue #21932: os.read() now uses a :c:func:`Py_ssize_t` type instead of
+ :c:type:`int` for the size to support reading more than 2 GB at once. On
+ Windows, the size is truncted to INT_MAX. As any call to os.read(), the OS
+ may read less bytes than the number of requested bytes.
+
- Issue #21942: Fixed source file viewing in pydoc's server mode on Windows.
- Issue #11259: asynchat.async_chat().set_terminator() now raises a ValueError
@@ -653,8 +1004,8 @@ Library
- Issue #21714: Disallow the construction of invalid paths using
Path.with_name(). Original patch by Antony Lee.
-- Issue #21897: Fix a crash with the f_locals attribute with closure
- variables when frame.clear() has been called.
+- Issue #15014: Added 'auth' method to smtplib to make implementing auth
+ mechanisms simpler, and used it internally in the login method.
- Issue #21151: Fixed a segfault in the winreg module when ``None`` is passed
as a ``REG_BINARY`` value to SetValueEx. Patch by John Ehresman.
@@ -662,8 +1013,17 @@ Library
- Issue #21090: io.FileIO.readall() does not ignore I/O errors anymore. Before,
it ignored I/O errors if at least the first C call read() succeed.
+- Issue #5800: headers parameter of wsgiref.headers.Headers is now optional.
+ Initial patch by Pablo Torres Navarrete and SilentGhost.
+
- Issue #21781: ssl.RAND_add() now supports strings longer than 2 GB.
+- Issue #21679: Prevent extraneous fstat() calls during open(). Patch by
+ Bohuslav Kabrda.
+
+- Issue #21863: cProfile now displays the module name of C extension functions,
+ in addition to their own name.
+
- Issue #11453: asyncore: emit a ResourceWarning when an unclosed file_wrapper
object is destroyed. The destructor now closes the file if needed. The
close() method can now be called twice: the second call does nothing.
@@ -673,23 +1033,17 @@ Library
- Issue #21476: Make sure the email.parser.BytesParser TextIOWrapper is
discarded after parsing, so the input file isn't unexpectedly closed.
+- Issue #20295: imghdr now recognizes OpenEXR format images.
+
- Issue #21729: Used the "with" statement in the dbm.dumb module to ensure
files closing. Patch by Claudiu Popa.
- Issue #21491: socketserver: Fix a race condition in child processes reaping.
-- Issue #21832: Require named tuple inputs to be exact strings.
-
-- Issue #19145: The times argument for itertools.repeat now handles
- negative values the same way for keyword arguments as it does for
- positional arguments.
+- Issue #21719: Added the ``st_file_attributes`` field to os.stat_result on
+ Windows.
-- Issue #21812: turtle.shapetransform did not tranform the turtle on the
- first call. (Issue identified and fixed by Lita Cho.)
-
-- Issue #21635: The difflib SequenceMatcher.get_matching_blocks() method
- cache didn't match the actual result. The former was a list of tuples
- and the latter was a list of named tuples.
+- Issue #21832: Require named tuple inputs to be exact strings.
- Issue #21722: The distutils "upload" command now exits with a non-zero
return code when uploading fails. Patch by Martin Dengler.
@@ -697,24 +1051,35 @@ Library
- Issue #21723: asyncio.Queue: support any type of number (ex: float) for the
maximum size. Patch written by Vajrasky Kok.
+- Issue #21711: support for "site-python" directories has now been removed
+ from the site module (it was deprecated in 3.4).
+
+- Issue #17552: new socket.sendfile() method allowing to send a file over a
+ socket by using high-performance os.sendfile() on UNIX.
+ Patch by Giampaolo Rodola'.
+
+- Issue #18039: dbm.dump.open() now always creates a new database when the
+ flag has the value 'n'. Patch by Claudiu Popa.
+
- Issue #21326: Add a new is_closed() method to asyncio.BaseEventLoop.
run_forever() and run_until_complete() methods of asyncio.BaseEventLoop now
raise an exception if the event loop was closed.
-- Issue #21774: Fixed NameError for an incorrect variable reference in the
- XML Minidom code for creating processing instructions.
- (Found and fixed by Claudiu Popa.)
-
- Issue #21766: Prevent a security hole in CGIHTTPServer by URL unquoting paths
before checking for a CGI script at that path.
- Issue #21310: Fixed possible resource leak in failed open().
+- Issue #21256: Printout of keyword args should be in deterministic order in
+ a mock function call. This will help to write better doctests.
+
- Issue #21677: Fixed chaining nonnormalized exceptions in io close() methods.
- Issue #11709: Fix the pydoc.help function to not fail when sys.stdin is not a
valid file.
+- Issue #21515: tempfile.TemporaryFile now uses os.O_TMPFILE flag is available.
+
- Issue #13223: Fix pydoc.writedoc so that the HTML documentation for methods
that use 'self' in the example code is generated correctly.
@@ -725,6 +1090,9 @@ Library
limits would otherwise allow. On systems with a functioning /proc/self/fd
or /dev/fd interface the max is now ignored and all fds are closed.
+- Issue #20383: Introduce importlib.util.module_from_spec() as the preferred way
+ to create a new module.
+
- Issue #21552: Fixed possible integer overflow of too long string lengths in
the tkinter module on 64-bit platforms.
@@ -733,8 +1101,14 @@ Library
error bubble up as this "bad data" appears in many real world zip files in
the wild and is ignored by other zip tools.
+- Issue #13742: Added "key" and "reverse" parameters to heapq.merge().
+ (First draft of patch contributed by Simon Sapin.)
+
- Issue #21402: tkinter.ttk now works when default root window is not set.
+- Issue #3015: _tkinter.create() now creates tkapp object with wantobject=1 by
+ default.
+
- Issue #10203: sqlite3.Row now truly supports sequence protocol. In particulr
it supports reverse() and negative indices. Original patch by Claudiu Popa.
@@ -742,97 +1116,21 @@ Library
interpreter aliases (python, python3) are now created by copying rather than
symlinking.
-- Issue #14710: pkgutil.get_loader() no longer raises an exception when None is
- found in sys.modules.
-
-- Issue #14710: pkgutil.find_loader() no longer raises an exception when a
- module doesn't exist.
+- Issue #20197: Added support for the WebP image type in the imghdr module.
+ Patch by Fabrice Aneche and Claudiu Popa.
-- Issue #21481: Argparse equality and inequality tests now return
- NotImplemented when comparing to an unknown type.
+- Issue #21513: Speedup some properties of IP addresses (IPv4Address,
+ IPv6Address) such as .is_private or .is_multicast.
-- Issue #8743: Fix interoperability between set objects and the
- collections.Set() abstract base class.
-
-- Issue #13355: random.triangular() no longer fails with a ZeroDivisionError
- when low equals high.
+- Issue #21137: Improve the repr for threading.Lock() and its variants
+ by showing the "locked" or "unlocked" status. Patch by Berker Peksag.
- Issue #21538: The plistlib module now supports loading of binary plist files
when reference or offset size is not a power of two.
-- Issue #21801: Validate that __signature__ is None or an instance of Signature.
-
-- Issue #21923: Prevent AttributeError in distutils.sysconfig.customize_compiler
- due to possible uninitialized _config_vars.
-
-- Issue #21323: Fix http.server to again handle scripts in CGI subdirectories,
- broken by the fix for security issue #19435. Patch by Zach Byrne.
-
-Extension Modules
------------------
-
-- Issue #22176: Update the ctypes module's libffi to v3.1. This release
- adds support for the Linux AArch64 and POWERPC ELF ABIv2 little endian
- architectures.
-
-Build
------
-
-- Issue #15661: python.org OS X installers are now distributed as signed
- installer packages compatible with the Gatekeeper security feature.
-
-- Issue #21958: Define HAVE_ROUND when building with Visual Studio 2013 and
- above. Patch by Zachary Turner.
-
-- Issue #15759: "make suspicious", "make linkcheck" and "make doctest" in Doc/
- now display special message when and only when there are failures.
-
-- Issue #17095: Fix Modules/Setup *shared* support.
-
-- Issue #21811: Anticipated fixes to support OS X versions > 10.9.
-
-- Issue #21166: Prevent possible segfaults and other random failures of
- python --generate-posix-vars in pybuilddir.txt build target.
-
-IDLE
-----
-
-- Issue #17390: Adjust Editor window title; remove 'Python',
- move version to end.
-
-- Issue #14105: Idle debugger breakpoints no longer disappear
- when inseting or deleting lines.
-
-- Issue #17172: Turtledemo can now be run from Idle.
- Currently, the entry is on the Help menu, but it may move to Run.
- Patch by Ramchandra Apt and Lita Cho.
-
-- Issue #21765: Add support for non-ascii identifiers to HyperParser.
-
-- Issue #21940: Add unittest for WidgetRedirector. Initial patch by Saimadhav
- Heblikar.
-
-- Issue #18592: Add unittest for SearchDialogBase. Patch by Phil Webster.
-
-- Issue #21694: Add unittest for ParenMatch. Patch by Saimadhav Heblikar.
-
-- Issue #21686: add unittest for HyperParser. Original patch by Saimadhav
- Heblikar.
-
-- Issue #12387: Add missing upper(lower)case versions of default Windows key
- bindings for Idle so Caps Lock does not disable them. Patch by Roger Serwy.
+- Issue #21455: Add a default backlog to socket.listen().
-- Issue #21695: Closing a Find-in-files output window while the search is
- still in progress no longer closes Idle.
-
-- Issue #18910: Add unittest for textView. Patch by Phil Webster.
-
-- Issue #18292: Add unittest for AutoExpand. Patch by Saihadhav Heblikar.
-
-- Issue #18409: Add unittest for AutoComplete. Patch by Phil Webster.
-
-Tests
------
+- Issue #21525: Most Tkinter methods which accepted tuples now accept lists too.
- Issue #22166: with the assistance of a new internal _codecs._forget_codec
helping function, test_codecs now clears the encoding caches to avoid the
@@ -841,100 +1139,19 @@ Tests
- Issue #22236: Tkinter tests now don't reuse default root window. New root
window is created for every test class.
-- Issue #20746: Fix test_pdb to run in refleak mode (-R). Patch by Xavier
- de Gaye.
-
-- Issue #22060: test_ctypes has been somewhat cleaned up and simplified; it
- now uses unittest test discovery to find its tests.
-
-- Issue #22104: regrtest.py no longer holds a reference to the suite of tests
- loaded from test modules that don't define test_main().
-
-- Issue #22002: Added ``load_package_tests`` function to test.support and used
- it to implement/augment test discovery in test_asyncio, test_email,
- test_importlib, test_json, and test_tools.
-
-- Issue #21976: Fix test_ssl to accept LibreSSL version strings. Thanks
- to William Orr.
-
-- Issue #21918: Converted test_tools from a module to a package containing
- separate test files for each tested script.
-
-- Issue #20155: Changed HTTP method names in failing tests in test_httpservers
- so that packet filtering software (specifically Windows Base Filtering Engine)
- does not interfere with the transaction semantics expected by the tests.
-
-- Issue #19493: Refactored the ctypes test package to skip tests explicitly
- rather than silently.
-
-- Issue #18492: All resources are now allowed when tests are not run by
- regrtest.py.
-
-- Issue #21634: Fix pystone micro-benchmark: use floor division instead of true
- division to benchmark integers instead of floating point numbers. Set pystone
- version to 1.2. Patch written by Lennart Regebro.
-
-- Issue #21605: Added tests for Tkinter images.
-
-- Issue #21493: Added test for ntpath.expanduser(). Original patch by
- Claudiu Popa.
-
-- Issue #19925: Added tests for the spwd module. Original patch by Vajrasky Kok.
-
-- Issue #21522: Added Tkinter tests for Listbox.itemconfigure(),
- PanedWindow.paneconfigure(), and Menu.entryconfigure().
-
-Documentation
--------------
-
-- Issue #21777: The binary sequence methods on bytes and bytearray are now
- documented explicitly, rather than assuming users will be able to derive
- the expected behaviour from the behaviour of the corresponding str methods.
-
-Windows
--------
-
-- Issue #21671, #22160, CVE-2014-0224: The bundled version of OpenSSL has been
- updated to 1.0.1i.
-
-- Issue #10747: Use versioned labels in the Windows start menu.
- Patch by Olive Kilburn.
-
-Tools/Demos
------------
-
-- Issue #22201: Command-line interface of the zipfile module now correctly
- extracts ZIP files with directory entries. Patch by Ryan Wilson.
-
-- Issue #21906: Make Tools/scripts/md5sum.py work in Python 3.
- Patch by Zachary Ware.
-
-- Issue #21629: Fix Argument Clinic's "--converters" feature.
-
-
-What's New in Python 3.4.1?
-===========================
-
-Release date: 2014-05-18
-
-Core and Builtins
------------------
-
-- Issue #21418: Fix a crash in the builtin function super() when called without
- argument and without current frame (ex: embedded Python).
+- Issue #10744: Fix PEP 3118 format strings on ctypes objects with a nontrivial
+ shape.
-- Issue #21425: Fix flushing of standard streams in the interactive
- interpreter.
+- Issue #20826: Optimize ipaddress.collapse_addresses().
-- Issue #21435: In rare cases, when running finalizers on objects in cyclic
- trash a bad pointer dereference could occur due to a subtle flaw in
- internal iteration logic.
+- Issue #21487: Optimize ipaddress.summarize_address_range() and
+ ipaddress.{IPv4Network,IPv6Network}.subnets().
-Library
--------
+- Issue #21486: Optimize parsing of netmasks in ipaddress.IPv4Network and
+ ipaddress.IPv6Network.
-- Issue #10744: Fix PEP 3118 format strings on ctypes objects with a nontrivial
- shape.
+- Issue #13916: Disallowed the surrogatepass error handler for non UTF-*
+ encodings.
- Issue #20998: Fixed re.fullmatch() of repeated single character pattern
with ignore case. Original patch by Matthew Barnett.
@@ -942,80 +1159,73 @@ Library
- Issue #21075: fileinput.FileInput now reads bytes from standard stream if
binary mode is specified. Patch by Sam Kimbrel.
-- Issue #21396: Fix TextIOWrapper(..., write_through=True) to not force a
- flush() on the underlying binary stream. Patch by akira.
+- Issue #19775: Add a samefile() method to pathlib Path objects. Initial
+ patch by Vajrasky Kok.
-- Issue #21470: Do a better job seeding the random number generator by
- using enough bytes to span the full state space of the Mersenne Twister.
+- Issue #21226: Set up modules properly in PyImport_ExecCodeModuleObject
+ (and friends).
- Issue #21398: Fix an unicode error in the pydoc pager when the documentation
contains characters not encodable to the stdout encoding.
-Tests
------
-
-- Issue #17756: Fix test_code test when run from the installed location.
-
-- Issue #17752: Fix distutils tests when run from the installed location.
-
-IDLE
-----
-
-- Issue #18104: Add idlelib/idle_test/htest.py with a few sample tests to begin
- consolidating and improving human-validated tests of Idle. Change other files
- as needed to work with htest. Running the module as __main__ runs all tests.
-
+- Issue #16531: ipaddress.IPv4Network and ipaddress.IPv6Network now accept
+ an (address, netmask) tuple argument, so as to easily construct network
+ objects from existing addresses.
-What's New in Python 3.4.1rc1?
-==============================
+- Issue #21156: importlib.abc.InspectLoader.source_to_code() is now a
+ staticmethod.
-Release date: 2014-05-05
+- Issue #21424: Simplified and optimized heaqp.nlargest() and nmsmallest()
+ to make fewer tuple comparisons.
-Core and Builtins
------------------
-
-- Issue #21274: Define PATH_MAX for GNU/Hurd in Python/pythonrun.c.
+- Issue #21396: Fix TextIOWrapper(..., write_through=True) to not force a
+ flush() on the underlying binary stream. Patch by akira.
-- Issue #21209: Fix sending tuples to custom generator objects with the yield
- from syntax.
+- Issue #18314: Unlink now removes junctions on Windows. Patch by Kim Gräsman
-- Issue #21134: Fix segfault when str is called on an uninitialized
- UnicodeEncodeError, UnicodeDecodeError, or UnicodeTranslateError object.
+- Issue #21088: Bugfix for curses.window.addch() regression in 3.4.0.
+ In porting to Argument Clinic, the first two arguments were reversed.
-- Issue #19537: Fix PyUnicode_DATA() alignment under m68k. Patch by
- Andreas Schwab.
+- Issue #21407: _decimal: The module now supports function signatures.
-- Issue #20929: Add a type cast to avoid shifting a negative number.
+- Issue #10650: Remove the non-standard 'watchexp' parameter from the
+ Decimal.quantize() method in the Python version. It had never been
+ present in the C version.
-- Issue #20731: Properly position in source code files even if they
- are opened in text mode. Patch by Serhiy Storchaka.
+- Issue #21469: Reduced the risk of false positives in robotparser by
+ checking to make sure that robots.txt has been read or does not exist
+ prior to returning True in can_fetch().
-- Issue #20637: Key-sharing now also works for instance dictionaries of
- subclasses. Patch by Peter Ingebretson.
+- Issue #19414: Have the OrderedDict mark deleted links as unusable.
+ This gives an early failure if the link is deleted during iteration.
-- Issue #12546: Allow \x00 to be used as a fill character when using str, int,
- float, and complex __format__ methods.
+- Issue #21421: Add __slots__ to the MappingViews ABC.
+ Patch by Josh Rosenberg.
-- Issue #13598: Modify string.Formatter to support auto-numbering of
- replacement fields. It now matches the behavior of str.format() in
- this regard. Patches by Phil Elson and Ramchandra Apte.
+- Issue #21101: Eliminate double hashing in the C speed-up code for
+ collections.Counter().
-Library
--------
+- Issue #21321: itertools.islice() now releases the reference to the source
+ iterator when the slice is exhausted. Patch by Anton Afanasyev.
-- Issue #21088: Bugfix for curses.window.addch() regression in 3.4.0.
- In porting to Argument Clinic, the first two arguments were reversed.
+- Issue #21057: TextIOWrapper now allows the underlying binary stream's
+ read() or read1() method to return an arbitrary bytes-like object
+ (such as a memoryview). Patch by Nikolaus Rath.
-- Issue #21469: Reduced the risk of false positives in robotparser by
- checking to make sure that robots.txt has been read or does not exist
- prior to returning True in can_fetch().
+- Issue #20951: SSLSocket.send() now raises either SSLWantReadError or
+ SSLWantWriteError on a non-blocking socket if the operation would block.
+ Previously, it would return 0. Patch by Nikolaus Rath.
-- Issue #21321: itertools.islice() now releases the reference to the source
- iterator when the slice is exhausted. Patch by Anton Afanasyev.
+- Issue #13248: removed previously deprecated asyncore.dispatcher __getattr__
+ cheap inheritance hack.
- Issue #9815: assertRaises now tries to clear references to local variables
in the exception's traceback.
+- Issue #19940: ssl.cert_time_to_seconds() now interprets the given time
+ string in the UTC timezone (as specified in RFC 5280), not the local
+ timezone.
+
- Issue #13204: Calling sys.flags.__new__ would crash the interpreter,
now it raises a TypeError.
@@ -1039,9 +1249,25 @@ Library
- Issue #12220: mindom now raises a custom ValueError indicating it doesn't
support spaces in URIs instead of letting a 'split' ValueError bubble up.
+- Issue #21068: The ssl.PROTOCOL* constants are now enum members.
+
+- Issue #21276: posixmodule: Don't define USE_XATTRS on KFreeBSD and the Hurd.
+
+- Issue #21262: New method assert_not_called for Mock.
+ It raises AssertionError if the mock has been called.
+
+- Issue #21238: New keyword argument `unsafe` to Mock. It raises
+ `AttributeError` incase of an attribute startswith assert or assret.
+
+- Issue #20896: ssl.get_server_certificate() now uses PROTOCOL_SSLv23, not
+ PROTOCOL_SSLv3, for maximum compatibility.
+
- Issue #21239: patch.stopall() didn't work deterministically when the same
name was patched more than once.
+- Issue #21203: Updated fileConfig and dictConfig to remove inconsistencies.
+ Thanks to Jure Koren for the patch.
+
- Issue #21222: Passing name keyword argument to mock.create_autospec now
works.
@@ -1071,17 +1297,31 @@ Library
- Issue #21171: Fixed undocumented filter API of the rot13 codec.
Patch by Berker Peksag.
+- Issue #20539: Improved math.factorial error message for large positive inputs
+ and changed exception type (OverflowError -> ValueError) for large negative
+ inputs.
+
- Issue #21172: isinstance check relaxed from dict to collections.Mapping.
- Issue #21155: asyncio.EventLoop.create_unix_server() now raises a ValueError
if path and sock are specified at the same time.
+- Issue #21136: Avoid unnecessary normalization of Fractions resulting from
+ power and other operations. Patch by Raymond Hettinger.
+
+- Issue #17621: Introduce importlib.util.LazyLoader.
+
+- Issue #21076: signal module constants were turned into enums.
+ Patch by Giampaolo Rodola'.
+
+- Issue #20636: Improved the repr of Tkinter widgets.
+
+- Issue #19505: The items, keys, and values views of OrderedDict now support
+ reverse iteration using reversed().
+
- Issue #21149: Improved thread-safety in logging cleanup during interpreter
shutdown. Thanks to Devin Jeanpierre for the patch.
-- Issue #20145: `assertRaisesRegex` and `assertWarnsRegex` now raise a
- TypeError if the second argument is not a string or compiled regex.
-
- Issue #21058: Fix a leak of file descriptor in
:func:`tempfile.NamedTemporaryFile`, close the file descriptor if
:func:`io.open` fails
@@ -1091,6 +1331,9 @@ Library
- Issue #21013: Enhance ssl.create_default_context() when used for server side
sockets to provide better security by default.
+- Issue #20145: `assertRaisesRegex` and `assertWarnsRegex` now raise a
+ TypeError if the second argument is not a string or compiled regex.
+
- Issue #20633: Replace relative import by absolute import.
- Issue #20980: Stop wrapping exception when using ThreadPool.
@@ -1104,6 +1347,8 @@ Library
curve for ECDH key exchange on OpenSSL 1.0.2 and later, and otherwise
default to "prime256v1".
+- Issue #21000: Improve the command-line interface of json.tool.
+
- Issue #20995: Enhance default ciphers used by the ssl module to enable
better security an prioritize perfect forward secrecy.
@@ -1111,6 +1356,24 @@ Library
- Issue #21499: Ignore __builtins__ in several test_importlib.test_api tests.
+- Issue #20627: xmlrpc.client.ServerProxy is now a context manager.
+
+- Issue #19165: The formatter module now raises DeprecationWarning instead of
+ PendingDeprecationWarning.
+
+- Issue #13936: Remove the ability of datetime.time instances to be considered
+ false in boolean contexts.
+
+- Issue 18931: selectors module now supports /dev/poll on Solaris.
+ Patch by Giampaolo Rodola'.
+
+- Issue #19977: When the ``LC_TYPE`` locale is the POSIX locale (``C`` locale),
+ :py:data:`sys.stdin` and :py:data:`sys.stdout` are now using the
+ ``surrogateescape`` error handler, instead of the ``strict`` error handler.
+
+- Issue #20574: Implement incremental decoder for cp65001 code (Windows code
+ page 65001, Microsoft UTF-8).
+
- Issue #20879: Delay the initialization of encoding and decoding tables for
base32, ascii85 and base85 codecs in the base64 module, and delay the
initialization of the unquote_to_bytes() table of the urllib.parse module, to
@@ -1145,6 +1408,14 @@ Library
- Issue #19748: On AIX, time.mktime() now raises an OverflowError for year
outsize range [1902; 2037].
+- Issue #19573: inspect.signature: Use enum for parameter kind constants.
+
+- Issue #20726: inspect.signature: Make Signature and Parameter picklable.
+
+- Issue #17373: Add inspect.Signature.from_callable method.
+
+- Issue #20378: Improve repr of inspect.Signature and inspect.Parameter.
+
- Issue #20816: Fix inspect.getcallargs() to raise correct TypeError for
missing keyword-only arguments. Patch by Jeremiah Lowin.
@@ -1161,6 +1432,12 @@ Library
positional-or-keyword arguments passed as keyword arguments become
keyword-only.
+- Issue #20334: inspect.Signature and inspect.Parameter are now hashable.
+ Thanks to Antony Lee for bug reports and suggestions.
+
+- Issue #15916: doctest.DocTestSuite returns an empty unittest.TestSuite instead
+ of raising ValueError if it finds no tests
+
- Issue #21209: Fix asyncio.tasks.CoroWrapper to workaround a bug
in yield-from implementation in CPythons prior to 3.4.1.
@@ -1173,16 +1450,90 @@ Library
- Issue #11571: Ensure that the turtle window becomes the topmost window
when launched on OS X.
-Extension Modules
------------------
+- Issue #21801: Validate that __signature__ is None or an instance of Signature.
-- Issue #21276: posixmodule: Don't define USE_XATTRS on KFreeBSD and the Hurd.
-- Issue #21226: Set up modules properly in PyImport_ExecCodeModuleObject
- (and friends).
+- Issue #21923: Prevent AttributeError in distutils.sysconfig.customize_compiler
+ due to possible uninitialized _config_vars.
+
+- Issue #21323: Fix http.server to again handle scripts in CGI subdirectories,
+ broken by the fix for security issue #19435. Patch by Zach Byrne.
+
+- Issue #22733: Fix ffi_prep_args not zero-extending argument values correctly
+ on 64-bit Windows.
+
+- Issue #23302: Default to TCP_NODELAY=1 upon establishing an HTTPConnection.
+ Removed use of hard-coded MSS as it's an optimization that's no longer needed
+ with Nagle disabled.
IDLE
----
+- Issue #20577: Configuration of the max line length for the FormatParagraph
+ extension has been moved from the General tab of the Idle preferences dialog
+ to the FormatParagraph tab of the Config Extensions dialog.
+ Patch by Tal Einat.
+
+- Issue #16893: Update Idle doc chapter to match current Idle and add new
+ information.
+
+- Issue #3068: Add Idle extension configuration dialog to Options menu.
+ Changes are written to HOME/.idlerc/config-extensions.cfg.
+ Original patch by Tal Einat.
+
+- Issue #16233: A module browser (File : Class Browser, Alt+C) requires a
+ editor window with a filename. When Class Browser is requested otherwise,
+ from a shell, output window, or 'Untitled' editor, Idle no longer displays
+ an error box. It now pops up an Open Module box (Alt+M). If a valid name
+ is entered and a module is opened, a corresponding browser is also opened.
+
+- Issue #4832: Save As to type Python files automatically adds .py to the
+ name you enter (even if your system does not display it). Some systems
+ automatically add .txt when type is Text files.
+
+- Issue #21986: Code objects are not normally pickled by the pickle module.
+ To match this, they are no longer pickled when running under Idle.
+
+- Issue #17390: Adjust Editor window title; remove 'Python',
+ move version to end.
+
+- Issue #14105: Idle debugger breakpoints no longer disappear
+ when inseting or deleting lines.
+
+- Issue #17172: Turtledemo can now be run from Idle.
+ Currently, the entry is on the Help menu, but it may move to Run.
+ Patch by Ramchandra Apt and Lita Cho.
+
+- Issue #21765: Add support for non-ascii identifiers to HyperParser.
+
+- Issue #21940: Add unittest for WidgetRedirector. Initial patch by Saimadhav
+ Heblikar.
+
+- Issue #18592: Add unittest for SearchDialogBase. Patch by Phil Webster.
+
+- Issue #21694: Add unittest for ParenMatch. Patch by Saimadhav Heblikar.
+
+- Issue #21686: add unittest for HyperParser. Original patch by Saimadhav
+ Heblikar.
+
+- Issue #12387: Add missing upper(lower)case versions of default Windows key
+ bindings for Idle so Caps Lock does not disable them. Patch by Roger Serwy.
+
+- Issue #21695: Closing a Find-in-files output window while the search is
+ still in progress no longer closes Idle.
+
+- Issue #18910: Add unittest for textView. Patch by Phil Webster.
+
+- Issue #18292: Add unittest for AutoExpand. Patch by Saihadhav Heblikar.
+
+- Issue #18409: Add unittest for AutoComplete. Patch by Phil Webster.
+
+- Issue #21477: htest.py - Improve framework, complete set of tests.
+ Patches by Saimadhav Heblikar
+
+- Issue #18104: Add idlelib/idle_test/htest.py with a few sample tests to begin
+ consolidating and improving human-validated tests of Idle. Change other files
+ as needed to work with htest. Running the module as __main__ runs all tests.
+
- Issue #21139: Change default paragraph width to 72, the PEP 8 recommendation.
- Issue #21284: Paragraph reformat test passes after user changes reformat width.
@@ -1190,10 +1541,52 @@ IDLE
- Issue #17654: Ensure IDLE menus are customized properly on OS X for
non-framework builds and for all variants of Tk.
+- Issue #23180: Rename IDLE "Windows" menu item to "Window".
+ Patch by Al Sweigart.
+
Build
-----
-- The Windows build now includes OpenSSL 1.0.1g
+- Issue #15506: Use standard PKG_PROG_PKG_CONFIG autoconf macro in the configure
+ script.
+
+- Issue #22935: Allow the ssl module to be compiled if openssl doesn't support
+ SSL 3.
+
+- Issue #22592: Drop support of the Borland C compiler to build Python. The
+ distutils module still supports it to build extensions.
+
+- Issue #22591: Drop support of MS-DOS, especially of the DJGPP compiler
+ (MS-DOS port of GCC).
+
+- Issue #16537: Check whether self.extensions is empty in setup.py. Patch by
+ Jonathan Hosmer.
+
+- Issue #22359: Remove incorrect uses of recursive make. Patch by Jonas
+ Wagner.
+
+- Issue #21958: Define HAVE_ROUND when building with Visual Studio 2013 and
+ above. Patch by Zachary Turner.
+
+- Issue #18093: the programs that embed the CPython runtime are now in a
+ separate "Programs" directory, rather than being kept in the Modules
+ directory.
+
+- Issue #15759: "make suspicious", "make linkcheck" and "make doctest" in Doc/
+ now display special message when and only when there are failures.
+
+- Issue #21141: The Windows build process no longer attempts to find Perl,
+ instead relying on OpenSSL source being configured and ready to build. The
+ ``PCbuild\build_ssl.py`` script has been re-written and re-named to
+ ``PCbuild\prepare_ssl.py``, and takes care of configuring OpenSSL source
+ for both 32 and 64 bit platforms. OpenSSL sources obtained from
+ svn.python.org will always be pre-configured and ready to build.
+
+- Issue #21037: Add a build option to enable AddressSanitizer support.
+
+- Issue #19962: The Windows build process now creates "python.bat" in the
+ root of the source tree, which passes all arguments through to the most
+ recently built interpreter.
- Issue #21285: Refactor and fix curses configure check to always search
in a ncursesw directory.
@@ -1202,12 +1595,60 @@ Build
include directories if they aren't already being searched. This avoids
an explicit runtime library dependency.
+- Issue #17861: Tools/scripts/generate_opcode_h.py automatically regenerates
+ Include/opcode.h from Lib/opcode.py if the later gets any change.
+
- Issue #20644: OS X installer build support for documentation build changes
in 3.4.1: assume externally supplied sphinx-build is available in /usr/bin.
+- Issue #20022: Eliminate use of deprecated bundlebuilder in OS X builds.
+
+- Issue #15968: Incorporated Tcl, Tk, and Tix builds into the Windows build
+ solution.
+
+- Issue #17095: Fix Modules/Setup *shared* support.
+
+- Issue #21811: Anticipated fixes to support OS X versions > 10.9.
+
+- Issue #21166: Prevent possible segfaults and other random failures of
+ python --generate-posix-vars in pybuilddir.txt build target.
+
+- Issue #18096: Fix library order returned by python-config.
+
+- Issue #17219: Add library build dir for Python extension cross-builds.
+
+- Issue #22919: Windows build updated to support VC 14.0 (Visual Studio 2015),
+ which will be used for the official release.
+
+- Issue #21236: Build _msi.pyd with cabinet.lib instead of fci.lib
+
+- Issue #17128: Use private version of OpenSSL for OS X 10.5+ installer.
+
C API
-----
+- Issue #14203: Remove obsolete support for view==NULL in PyBuffer_FillInfo(),
+ bytearray_getbuffer(), bytesiobuf_getbuffer() and array_buffer_getbuf().
+ All functions now raise BufferError in that case.
+
+- Issue #22445: PyBuffer_IsContiguous() now implements precise contiguity
+ tests, compatible with NumPy's NPY_RELAXED_STRIDES_CHECKING compilation
+ flag. Previously the function reported false negatives for corner cases.
+
+- Issue #22079: PyType_Ready() now checks that statically allocated type has
+ no dynamically allocated bases.
+
+- Issue #22453: Removed non-documented macro PyObject_REPR().
+
+- Issue #18395: Rename ``_Py_char2wchar()`` to :c:func:`Py_DecodeLocale`,
+ rename ``_Py_wchar2char()`` to :c:func:`Py_EncodeLocale`, and document
+ these functions.
+
+- Issue #21233: Add new C functions: PyMem_RawCalloc(), PyMem_Calloc(),
+ PyObject_Calloc(), _PyObject_GC_Calloc(). bytes(int) is now using
+ ``calloc()`` instead of ``malloc()`` for large objects which is faster and
+ use less memory.
+
- Issue #20942: PyImport_ImportFrozenModuleObject() no longer sets __file__ to
match what importlib does; this affects _frozen_importlib as well as any
module loaded using imp.init_frozen().
@@ -1215,9 +1656,29 @@ C API
Documentation
-------------
+- Issue #19548: Update the codecs module documentation to better cover the
+ distinction between text encodings and other codecs, together with other
+ clarifications. Patch by Martin Panter.
+
+- Issue #22394: Doc/Makefile now supports ``make venv PYTHON=../python`` to
+ create a venv for generating the documentation, e.g.,
+ ``make html PYTHON=venv/bin/python3``.
+
+- Issue #21514: The documentation of the json module now refers to new JSON RFC
+ 7159 instead of obsoleted RFC 4627.
+
+- Issue #21777: The binary sequence methods on bytes and bytearray are now
+ documented explicitly, rather than assuming users will be able to derive
+ the expected behaviour from the behaviour of the corresponding str methods.
+
+- Issue #6916: undocument deprecated asynchat.fifo class.
+
- Issue #17386: Expanded functionality of the ``Doc/make.bat`` script to make
it much more comparable to ``Doc/Makefile``.
+- Issue #21312: Update the thread_foobar.h template file to include newer
+ threading APIs. Patch by Jack McCracken.
+
- Issue #21043: Remove the recommendation for specific CA organizations and to
mention the ability to load the OS certificates.
@@ -1235,6 +1696,73 @@ Documentation
Tests
-----
+- Issue #18982: Add tests for CLI of the calendar module.
+
+- Issue #19548: Added some additional checks to test_codecs to ensure that
+ statements in the updated documentation remain accurate. Patch by Martin
+ Panter.
+
+- Issue #22838: All test_re tests now work with unittest test discovery.
+
+- Issue #22173: Update lib2to3 tests to use unittest test discovery.
+
+- Issue #16000: Convert test_curses to use unittest.
+
+- Issue #21456: Skip two tests in test_urllib2net.py if _ssl module not
+ present. Patch by Remi Pointel.
+
+- Issue #20746: Fix test_pdb to run in refleak mode (-R). Patch by Xavier
+ de Gaye.
+
+- Issue #22060: test_ctypes has been somewhat cleaned up and simplified; it
+ now uses unittest test discovery to find its tests.
+
+- Issue #22104: regrtest.py no longer holds a reference to the suite of tests
+ loaded from test modules that don't define test_main().
+
+- Issue #22111: Assorted cleanups in test_imaplib. Patch by Milan Oberkirch.
+
+- Issue #22002: Added ``load_package_tests`` function to test.support and used
+ it to implement/augment test discovery in test_asyncio, test_email,
+ test_importlib, test_json, and test_tools.
+
+- Issue #21976: Fix test_ssl to accept LibreSSL version strings. Thanks
+ to William Orr.
+
+- Issue #21918: Converted test_tools from a module to a package containing
+ separate test files for each tested script.
+
+- Issue #9554: Use modern unittest features in test_argparse. Initial patch by
+ Denver Coneybeare and Radu Voicilas.
+
+- Issue #20155: Changed HTTP method names in failing tests in test_httpservers
+ so that packet filtering software (specifically Windows Base Filtering Engine)
+ does not interfere with the transaction semantics expected by the tests.
+
+- Issue #19493: Refactored the ctypes test package to skip tests explicitly
+ rather than silently.
+
+- Issue #18492: All resources are now allowed when tests are not run by
+ regrtest.py.
+
+- Issue #21634: Fix pystone micro-benchmark: use floor division instead of true
+ division to benchmark integers instead of floating point numbers. Set pystone
+ version to 1.2. Patch written by Lennart Regebro.
+
+- Issue #21605: Added tests for Tkinter images.
+
+- Issue #21493: Added test for ntpath.expanduser(). Original patch by
+ Claudiu Popa.
+
+- Issue #19925: Added tests for the spwd module. Original patch by Vajrasky Kok.
+
+- Issue #21522: Added Tkinter tests for Listbox.itemconfigure(),
+ PanedWindow.paneconfigure(), and Menu.entryconfigure().
+
+- Issue #17756: Fix test_code test when run from the installed location.
+
+- Issue #17752: Fix distutils tests when run from the installed location.
+
- Issue #18604: Consolidated checks for GUI availability. All platforms now
at least check whether Tk can be instantiated when the GUI resource is
requested.
@@ -1252,6 +1780,8 @@ Tests
- Issue #21097: Move test_namespace_pkgs into test_importlib.
+- Issue #21503: Use test_both() consistently in test_importlib.
+
- Issue #20939: Avoid various network test failures due to new
redirect of http://www.python.org/ to https://www.python.org:
use http://www.example.com instead.
@@ -1262,9 +1792,41 @@ Tests
- Issue #21093: Prevent failures of ctypes test_macholib on OS X if a
copy of libz exists in $HOME/lib or /usr/local/lib.
+- Issue #22770: Prevent some Tk segfaults on OS X when running gui tests.
+
+- Issue #23211: Workaround test_logging failure on some OS X 10.6 systems.
+
+- Issue #23345: Prevent test_ssl failures with large OpenSSL patch level
+ values (like 0.9.8zc).
+
Tools/Demos
-----------
+- Issue #22314: pydoc now works when the LINES environment variable is set.
+
+- Issue #22615: Argument Clinic now supports the "type" argument for the
+ int converter. This permits using the int converter with enums and
+ typedefs.
+
+- Issue #20076: The makelocalealias.py script no longer ignores UTF-8 mapping.
+
+- Issue #20079: The makelocalealias.py script now can parse the SUPPORTED file
+ from glibc sources and supports command line options for source paths.
+
+- Issue #22201: Command-line interface of the zipfile module now correctly
+ extracts ZIP files with directory entries. Patch by Ryan Wilson.
+
+- Issue #22120: For functions using an unsigned integer return converter,
+ Argument Clinic now generates a cast to that type for the comparison
+ to -1 in the generated code. (This supresses a compilation warning.)
+
+- Issue #18974: Tools/scripts/diff.py now uses argparse instead of optparse.
+
+- Issue #21906: Make Tools/scripts/md5sum.py work in Python 3.
+ Patch by Zachary Ware.
+
+- Issue #21629: Fix Argument Clinic's "--converters" feature.
+
- Add support for ``yield from`` to 2to3.
- Add support for the PEP 465 matrix multiplication operator to 2to3.
@@ -1277,6 +1839,31 @@ Tools/Demos
- Issue #20535: PYTHONWARNING no longer affects the run_tests.py script.
Patch by Arfrever Frehtes Taifersar Arahesis.
+Windows
+-------
+
+- Issue #23260: Update Windows installer
+
+- The bundled version of Tcl/Tk has been updated to 8.6.3. The most visible
+ result of this change is the addition of new native file dialogs when
+ running on Windows Vista or newer. See Tcl/Tk's TIP 432 for more
+ information. Also, this version of Tcl/Tk includes support for Windows 10.
+
+- Issue #17896: The Windows build scripts now expect external library sources
+ to be in ``PCbuild\..\externals`` rather than ``PCbuild\..\..``.
+
+- Issue #17717: The Windows build scripts now use a copy of NASM pulled from
+ svn.python.org to build OpenSSL.
+
+- Issue #21907: Improved the batch scripts provided for building Python.
+
+- Issue #22644: The bundled version of OpenSSL has been updated to 1.0.1j.
+
+- Issue #10747: Use versioned labels in the Windows start menu.
+ Patch by Olive Kilburn.
+
+- Issue #22980: .pyd files with a version and platform tag (for example,
+ ".cp35-win32.pyd") will now be loaded in preference to those without tags.
What's New in Python 3.4.0?
===========================
@@ -5390,6 +5977,10 @@ C-API
Documentation
-------------
+- Issue #23006: Improve the documentation and indexing of dict.__missing__.
+ Add an entry in the language datamodel special methods section.
+ Revise and index its discussion in the stdtypes mapping/dict section.
+
- Issue #17701: Improving strftime documentation.
- Issue #18440: Clarify that `hash()` can truncate the value returned from an
@@ -5552,4141 +6143,4 @@ Windows
- Issue #18569: The installer now adds .py to the PATHEXT variable when extensions
are registered. Patch by Paul Moore.
-
-What's New in Python 3.3.0?
-===========================
-
-*Release date: 29-Sep-2012*
-
-Core and Builtins
------------------
-
-- Issue #16046: Fix loading sourceless legacy .pyo files.
-
-- Issue #16060: Fix refcounting bug when `__trunc__()` returns an object whose
- `__int__()` gives a non-integer. Patch by Serhiy Storchaka.
-
-Extension Modules
------------------
-
-- Issue #16012: Fix a regression in pyexpat. The parser's `UseForeignDTD()`
- method doesn't require an argument again.
-
-
-What's New in Python 3.3.0 Release Candidate 3?
-===============================================
-
-*Release date: 23-Sep-2012*
-
-Core and Builtins
------------------
-
-- Issue #15900: Fix reference leak in `PyUnicode_TranslateCharmap()`.
-
-- Issue #15926: Fix crash after multiple reinitializations of the interpreter.
-
-- Issue #15895: Fix FILE pointer leak in one error branch of
- `PyRun_SimpleFileExFlags()` when filename points to a pyc/pyo file, closeit is
- false an and set_main_loader() fails.
-
-- Fixes for a few crash and memory leak regressions found by Coverity.
-
-Library
--------
-
-- Issue #15882: Change `_decimal` to accept any coefficient tuple when
- constructing infinities. This is done for backwards compatibility with
- decimal.py: Infinity coefficients are undefined in _decimal (in accordance
- with the specification).
-
-- Issue #15925: Fix a regression in `email.util` where the `parsedate()` and
- `parsedate_tz()` functions did not return None anymore when the argument could
- not be parsed.
-
-Extension Modules
------------------
-
-- Issue #15973: Fix a segmentation fault when comparing datetime timezone
- objects.
-
-- Issue #15977: Fix memory leak in Modules/_ssl.c when the function
- _set_npn_protocols() is called multiple times, thanks to Daniel Sommermann.
-
-- Issue #15969: `faulthandler` module: rename dump_tracebacks_later() to
- dump_traceback_later() and cancel_dump_tracebacks_later() to
- cancel_dump_traceback_later().
-
-- _decimal module: use only C 89 style comments.
-
-
-What's New in Python 3.3.0 Release Candidate 2?
-===============================================
-
-*Release date: 09-Sep-2012*
-
-Core and Builtins
------------------
-
-- Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
- sporadic crashes in multi-thread programs when several long deallocator chains
- ran concurrently and involved subclasses of built-in container types.
-
-- Issue #15784: Modify `OSError`.__str__() to better distinguish between errno
- error numbers and Windows error numbers.
-
-- Issue #15781: Fix two small race conditions in import's module locking.
-
-Library
--------
-
-- Issue #17158: Add 'symbols' to help() welcome message; clarify
- 'modules spam' messages.
-
-- Issue #15847: Fix a regression in argparse, which did not accept tuples as
- argument lists anymore.
-
-- Issue #15828: Restore support for C extensions in `imp.load_module()`.
-
-- Issue #15340: Fix importing the random module when ``/dev/urandom`` cannot be
- opened. This was a regression caused by the hash randomization patch.
-
-- Issue #10650: Deprecate the watchexp parameter of the `Decimal.quantize()`
- method.
-
-- Issue #15785: Modify `window.get_wch()` API of the curses module: return a
- character for most keys, and an integer for special keys, instead of always
- returning an integer. So it is now possible to distinguish special keys like
- keypad keys.
-
-- Issue #14223: Fix `window.addch()` of the curses module for special characters
- like curses.ACS_HLINE: the Python function addch(int) and addch(bytes) is now
- calling the C function waddch()/mvwaddch() (as it was done in Python 3.2),
- instead of wadd_wch()/mvwadd_wch(). The Python function addch(str) is still
- calling the C function wadd_wch()/mvwadd_wch() if the Python curses is linked
- to libncursesw.
-
-Build
------
-
-- Issue #15822: Really ensure 2to3 grammar pickles are properly installed
- (replaces fixes for Issue #15645).
-
-Documentation
--------------
-
-- Issue #15814: The memoryview enhancements in 3.3.0 accidentally permitted the
- hashing of multi-dimensional memorviews and memoryviews with multi-byte item
- formats. The intended restrictions have now been documented - they will be
- correctly enforced in 3.3.1.
-
-
-What's New in Python 3.3.0 Release Candidate 1?
-===============================================
-
-*Release date: 25-Aug-2012*
-
-Core and Builtins
------------------
-
-- Issue #15573: memoryview comparisons are now performed by value with full
- support for any valid struct module format definition.
-
-- Issue #15316: When an item in the fromlist for `__import__()` doesn't exist,
- don't raise an error, but if an exception is raised as part of an import do
- let that propagate.
-
-- Issue #15778: Ensure that ``str(ImportError(msg))`` returns a str even when
- msg isn't a str.
-
-- Issue #2051: Source file permission bits are once again correctly copied to
- the cached bytecode file. (The migration to importlib reintroduced this
- problem because these was no regression test. A test has been added as part of
- this patch)
-
-- Issue #15761: Fix crash when ``PYTHONEXECUTABLE`` is set on Mac OS X.
-
-- Issue #15726: Fix incorrect bounds checking in PyState_FindModule. Patch by
- Robin Schreiber.
-
-- Issue #15604: Update uses of `PyObject_IsTrue()` to check for and handle
- errors correctly. Patch by Serhiy Storchaka.
-
-- Issue #14846: `importlib.FileFinder` now handles the case where the directory
- being searched is removed after a previous import attempt.
-
-Library
--------
-
-- Issue #13370: Ensure that ctypes works on Mac OS X when Python is compiled
- using the clang compiler.
-
-- Issue #13072: The array module's 'u' format code is now deprecated and will be
- removed in Python 4.0.
-
-- Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
-
-- Issue #15776: Allow pyvenv to work in existing directory with --clean.
-
-- Issue #15249: email's BytesGenerator now correctly mangles From lines (when
- requested) even if the body contains undecodable bytes.
-
-- Issue #15777: Fix a refleak in _posixsubprocess.
-
-- Issue ##665194: Update `email.utils.localtime` to use datetime.astimezone and
- correctly handle historic changes in UTC offsets.
-
-- Issue #15199: Fix JavaScript's default MIME type to application/javascript.
- Patch by Bohuslav Kabrda.
-
-- Issue #12643: `code.InteractiveConsole` now respects `sys.excepthook` when
- displaying exceptions. Patch by Aaron Iles.
-
-- Issue #13579: `string.Formatter` now understands the 'a' conversion specifier.
-
-- Issue #15595: Fix ``subprocess.Popen(universal_newlines=True)`` for certain
- locales (utf-16 and utf-32 family). Patch by Chris Jerdonek.
-
-- Issue #15477: In cmath and math modules, add workaround for platforms whose
- system-supplied log1p function doesn't respect signs of zeros.
-
-- Issue #15715: `importlib.__import__()` will silence an ImportError when the
- use of fromlist leads to a failed import.
-
-- Issue #14669: Fix pickling of connections and sockets on Mac OS X by
- sending/receiving an acknowledgment after file descriptor transfer.
- TestPicklingConnection has been reenabled for Mac OS X.
-
-- Issue #11062: Fix adding a message from file to Babyl mailbox.
-
-- Issue #15646: Prevent equivalent of a fork bomb when using `multiprocessing`
- on Windows without the ``if __name__ == '__main__'`` idiom.
-
-IDLE
-----
-
-- Issue #15678: Fix IDLE menus when started from OS X command line (3.3.0b2
- regression).
-
-Documentation
--------------
-
-- Touched up the Python 2 to 3 porting guide.
-
-- Issue #14674: Add a discussion of the `json` module's standard compliance.
- Patch by Chris Rebert.
-
-- Create a 'Concurrent Execution' section in the docs, and split up the
- 'Optional Operating System Services' section to use a more user-centric
- classification scheme (splitting them across the new CE section, IPC and text
- processing). Operating system limitations can be reflected with the Sphinx
- ``:platform:`` tag, it doesn't make sense as part of the Table of Contents.
-
-- Issue #4966: Bring the sequence docs up to date for the Py3k transition and
- the many language enhancements since they were original written.
-
-- The "path importer" misnomer has been replaced with Eric Snow's
- more-awkward-but-at-least-not-wrong suggestion of "path based finder" in the
- import system reference docs.
-
-- Issue #15640: Document `importlib.abc.Finder` as deprecated.
-
-- Issue #15630: Add an example for "continue" stmt in the tutorial. Patch by
- Daniel Ellis.
-
-Tests
------
-
-- Issue #15747: ZFS always returns EOPNOTSUPP when attempting to set the
- UF_IMMUTABLE flag (via either chflags or lchflags); refactor affected tests in
- test_posix.py to account for this.
-
-- Issue #15285: Refactor the approach for testing connect timeouts using two
- external hosts that have been configured specifically for this type of test.
-
-- Issue #15743: Remove the deprecated method usage in `urllib` tests. Patch by
- Jeff Knupp.
-
-- Issue #15615: Add some tests for the `json` module's handling of invalid input
- data. Patch by Kushal Das.
-
-Build
------
-
-- Output lib files for PGO build into PGO directory.
-
-- Pick up 32-bit launcher from PGO directory on 64-bit PGO build.
-
-- Drop ``PC\python_nt.h`` as it's not used. Add input dependency on custom
- build step.
-
-- Issue #15511: Drop explicit dependency on pythonxy.lib from _decimal amd64
- configuration.
-
-- Add missing PGI/PGO configurations for pywlauncher.
-
-- Issue #15645: Ensure 2to3 grammar pickles are properly installed.
-
-
-What's New in Python 3.3.0 Beta 2?
-==================================
-
-*Release date: 12-Aug-2012*
-
-Core and Builtins
------------------
-
-- Issue #15568: Fix the return value of ``yield from`` when StopIteration is
- raised by a custom iterator.
-
-- Issue #13119: `sys.stdout` and `sys.stderr` are now using "\r\n" newline on
- Windows, as Python 2.
-
-- Issue #15534: Fix the fast-search function for non-ASCII Unicode strings.
-
-- Issue #15508: Fix the docstring for `__import__()` to have the proper default
- value of 0 for 'level' and to not mention negative levels since they are not
- supported.
-
-- Issue #15425: Eliminated traceback noise from more situations involving
- importlib.
-
-- Issue #14578: Support modules registered in the Windows registry again.
-
-- Issue #15466: Stop using TYPE_INT64 in marshal, to make importlib.h (and other
- byte code files) equal between 32-bit and 64-bit systems.
-
-- Issue #1692335: Move initial exception args assignment to
- `BaseException.__new__()` to help pickling of naive subclasses.
-
-- Issue #12834: Fix `PyBuffer_ToContiguous()` for non-contiguous arrays.
-
-- Issue #15456: Fix code `__sizeof__()` after #12399 change. Patch by Serhiy
- Storchaka.
-
-- Issue #15404: Refleak in PyMethodObject repr.
-
-- Issue #15394: An issue in `PyModule_Create()` that caused references to be
- leaked on some error paths has been fixed. Patch by Julia Lawall.
-
-- Issue #15368: An issue that caused bytecode generation to be non-deterministic
- has been fixed.
-
-- Issue #15202: Consistently use the name "follow_symlinks" for new parameters
- in os and shutil functions.
-
-- Issue #15314: ``__main__.__loader__`` is now set correctly during interpreter
- startup.
-
-- Issue #15111: When a module imported using 'from import' has an ImportError
- inside itself, don't mask that fact behind a generic ImportError for the
- module itself.
-
-- Issue #15293: Add GC support to the AST base node type.
-
-- Issue #15291: Fix a memory leak where AST nodes where not properly
- deallocated.
-
-- Issue #15110: Fix the tracebacks generated by "import xxx" to not show the
- importlib stack frames.
-
-- Issue #16369: Global PyTypeObjects not initialized with PyType_Ready(...).
-
-- Issue #15020: The program name used to search for Python's path is now
- "python3" under Unix, not "python".
-
-- Issue #15897: zipimport.c doesn't check return value of fseek().
- Patch by Felipe Cruz.
-
-- Issue #15033: Fix the exit status bug when modules invoked using -m switch,
- return the proper failure return value (1). Patch contributed by Jeff Knupp.
-
-- Issue #15229: An `OSError` subclass whose __init__ doesn't call back
- OSError.__init__ could produce incomplete instances, leading to crashes when
- calling str() on them.
-
-- Issue #15307: Virtual environments now use symlinks with framework builds on
- Mac OS X, like other POSIX builds.
-
-Library
--------
-
-- Issue #14590: configparser now correctly strips inline comments when delimiter
- occurs earlier without preceding space.
-
-- Issue #15424: Add a `__sizeof__()` implementation for array objects. Patch by
- Ludwig Hähne.
-
-- Issue #15576: Allow extension modules to act as a package's __init__ module.
-
-- Issue #15502: Have `importlib.invalidate_caches()` work on `sys.meta_path`
- instead of `sys.path_importer_cache`.
-
-- Issue #15163: Pydoc shouldn't list __loader__ as module data.
-
-- Issue #15471: Do not use mutable objects as defaults for
- `importlib.__import__()`.
-
-- Issue #15559: To avoid a problematic failure mode when passed to the bytes
- constructor, objects in the ipaddress module no longer implement `__index__()`
- (they still implement `__int__()` as appropriate).
-
-- Issue #15546: Fix handling of pathological input data in the peek() and
- read1() methods of the BZ2File, GzipFile and LZMAFile classes.
-
-- Issue #12655: Instead of requiring a custom type, `os.sched_getaffinity()` and
- `os.sched_setaffinity()` now use regular sets of integers to represent the
- CPUs a process is restricted to.
-
-- Issue #15538: Fix compilation of the `socket.getnameinfo()` /
- `socket.getaddrinfo()` emulation code. Patch by Philipp Hagemeister.
-
-- Issue #15519: Properly expose WindowsRegistryFinder in importlib (and use the
- correct term for it). Original patch by Eric Snow.
-
-- Issue #15502: Bring the importlib ABCs into line with the current state of the
- import protocols given PEP 420. Original patch by Eric Snow.
-
-- Issue #15499: Launching a webbrowser in Unix used to sleep for a few seconds.
- Original patch by Anton Barkovsky.
-
-- Issue #15463: The faulthandler module truncates strings to 500 characters,
- instead of 100, to be able to display long file paths.
-
-- Issue #6056: Make `multiprocessing` use setblocking(True) on the sockets it
- uses. Original patch by J Derek Wilson.
-
-- Issue #15364: Fix sysconfig.get_config_var('srcdir') to be an absolute path.
-
-- Issue #15413: `os.times()` had disappeared under Windows.
-
-- Issue #15402: An issue in the struct module that caused `sys.getsizeof()` to
- return incorrect results for struct.Struct instances has been fixed. Initial
- patch by Serhiy Storchaka.
-
-- Issue #15232: When mangle_from is True, `email.Generator` now correctly
- mangles lines that start with 'From ' that occur in a MIME preamble or
- epilogue.
-
-- Issue #15094: Incorrectly placed #endif in _tkinter.c. Patch by Serhiy
- Storchaka.
-
-- Issue #13922: `argparse` no longer incorrectly strips '--'s that appear after
- the first one.
-
-- Issue #12353: `argparse` now correctly handles null argument values.
-
-- Issue #10017, issue #14998: Fix TypeError using pprint on dictionaries with
- user-defined types as keys or other unorderable keys.
-
-- Issue #15397: `inspect.getmodulename()` is now based directly on importlib via
- a new `importlib.machinery.all_suffixes()` API.
-
-- Issue #14635: `telnetlib` will use poll() rather than select() when possible to
- avoid failing due to the select() file descriptor limit.
-
-- Issue #15180: Clarify posixpath.join() error message when mixing str & bytes.
-
-- Issue #15343: pkgutil now includes an iter_importer_modules implementation for
- importlib.machinery.FileFinder (similar to the way it already handled
- zipimport.zipimporter).
-
-- Issue #15314: runpy now sets __main__.__loader__ correctly.
-
-- Issue #15357: The import emulation in pkgutil is now deprecated. pkgutil uses
- importlib internally rather than the emulation.
-
-- Issue #15233: Python now guarantees that callables registered with the atexit
- module will be called in a deterministic order.
-
-- Issue #15238: `shutil.copystat()` now copies Linux "extended attributes".
-
-- Issue #15230: runpy.run_path now correctly sets __package__ as described in
- the documentation.
-
-- Issue #15315: Support VS 2010 in distutils cygwincompiler.
-
-- Issue #15294: Fix a regression in pkgutil.extend_path()'s handling of nested
- namespace packages.
-
-- Issue #15056: `imp.cache_from_source()` and `imp.source_from_cache()` raise
- NotImplementedError when `sys.implementation.cache_tag` is set to None.
-
-- Issue #15256: Grammatical mistake in exception raised by `imp.find_module()`.
-
-- Issue #5931: `wsgiref` environ variable SERVER_SOFTWARE will specify an
- implementation specific term like CPython, Jython instead of generic "Python".
-
-- Issue #13248: Remove obsolete argument "max_buffer_size" of BufferedWriter and
- BufferedRWPair, from the io module.
-
-- Issue #13248: Remove obsolete argument "version" of `argparse.ArgumentParser`.
-
-- Issue #14814: Implement more consistent ordering and sorting behaviour for
- ipaddress objects.
-
-- Issue #14814: `ipaddress` network objects correctly return NotImplemented when
- compared to arbitrary objects instead of raising TypeError.
-
-- Issue #14990: Correctly fail with SyntaxError on invalid encoding declaration.
-
-- Issue #14814: `ipaddress` now provides more informative error messages when
- constructing instances directly (changes permitted during beta due to
- provisional API status).
-
-- Issue #15247: `io.FileIO` now raises an error when given a file descriptor
- pointing to a directory.
-
-- Issue #15261: Stop os.stat(fd) crashing on Windows when fd not open.
-
-- Issue #15166: Implement `imp.get_tag()` using `sys.implementation.cache_tag`.
-
-- Issue #15210: Catch KeyError when `importlib.__init__()` can't find
- _frozen_importlib in sys.modules, not ImportError.
-
-- Issue #15030: `importlib.abc.PyPycLoader` now supports the new source size
- header field in .pyc files.
-
-- Issue #5346: Preserve permissions of mbox, MMDF and Babyl mailbox files on
- flush().
-
-- Issue #10571: Fix the "--sign" option of distutils' upload command. Patch by
- Jakub Wilk.
-
-- Issue #9559: If messages were only added, a new file is no longer created and
- renamed over the old file when flush() is called on an mbox, MMDF or Babyl
- mailbox.
-
-- Issue #10924: Fixed `crypt.mksalt()` to use a RNG that is suitable for
- cryptographic purpose.
-
-- Issue #15184: Ensure consistent results of OS X configuration tailoring for
- universal builds by factoring out common OS X-specific customizations from
- sysconfig, distutils.sysconfig, distutils.util, and distutils.unixccompiler
- into a new module _osx_support.
-
-C API
------
-
-- Issue #15610: `PyImport_ImportModuleEx()` now uses a 'level' of 0 instead of -1.
-
-- Issue #15169, issue #14599: Strip out the C implementation of
- `imp.source_from_cache()` used by PyImport_ExecCodeModuleWithPathnames() and
- used the Python code instead. Leads to PyImport_ExecCodeModuleObject() to not
- try to infer the source path from the bytecode path as
- PyImport_ExecCodeModuleWithPathnames() does.
-
-Extension Modules
------------------
-
-- Issue #6493: An issue in ctypes on Windows that caused structure bitfields of
- type `ctypes.c_uint32` and width 32 to incorrectly be set has been fixed.
-
-- Issue #15194: Update libffi to the 3.0.11 release.
-
-IDLE
-----
-
-- Issue #13052: Fix IDLE crashing when replace string in Search/Replace dialog
- ended with ``\``. Patch by Roger Serwy.
-
-Tools/Demos
------------
-
-- Issue #15458: python-config gets a new option --configdir to print the $LIBPL
- value.
-
-- Move importlib.test.benchmark to Tools/importbench.
-
-- Issue #12605: The gdb hooks for debugging CPython (within Tools/gdb) have been
- enhanced to show information on more C frames relevant to CPython within the
- "py-bt" and "py-bt-full" commands:
-
- * C frames that are waiting on the GIL
- * C frames that are garbage-collecting
- * C frames that are due to the invocation of a PyCFunction
-
-Documentation
--------------
-
-- Issue #15041: Update "see also" list in tkinter documentation.
-
-- Issue #15444: Use proper spelling for non-ASCII contributor names. Patch by
- Serhiy Storchaka.
-
-- Issue #15295: Reorganize and rewrite the documentation on the import system.
-
-- Issue #15230: Clearly document some of the limitations of the runpy module and
- nudge readers towards importlib when appropriate.
-
-- Issue #15053: Copy Python 3.3 import lock change notice to all relevant
- functions in imp instead of just at the top of the relevant section.
-
-- Issue #15288: Link to the term "loader" in notes in pkgutil about how things
- won't work as expected in Python 3.3 and mark the requisite functions as
- "changed" since they will no longer work with modules directly imported by
- import itself.
-
-- Issue #13557: Clarify effect of giving two different namespaces to `exec()` or
- `execfile()`.
-
-- Issue #15250: Document that `filecmp.dircmp()` compares files shallowly. Patch
- contributed by Chris Jerdonek.
-
-- Issue #15442: Expose the default list of directories ignored by
- `filecmp.dircmp()` as a module attribute, and expand the list to more modern
- values.
-
-Tests
------
-
-- Issue #15467: Move helpers for `__sizeof__()` tests into test_support. Patch
- by Serhiy Storchaka.
-
-- Issue #15320: Make iterating the list of tests thread-safe when running tests
- in multiprocess mode. Patch by Chris Jerdonek.
-
-- Issue #15168: Move `importlib.test` to `test.test_importlib`.
-
-- Issue #15091: Reactivate a test on UNIX which was failing thanks to a
- forgotten `importlib.invalidate_caches()` call.
-
-- Issue #15230: Adopted a more systematic approach in the runpy tests.
-
-- Issue #15300: Ensure the temporary test working directories are in the same
- parent folder when running tests in multiprocess mode from a Python build.
- Patch by Chris Jerdonek.
-
-- Issue #15284: Skip {send,recv}msg tests in test_socket when IPv6 is not
- enabled. Patch by Brian Brazil.
-
-- Issue #15277: Fix a resource leak in support.py when IPv6 is disabled. Patch
- by Brian Brazil.
-
-Build
------
-
-- Issue #11715: Fix multiarch detection without having Debian development tools
- (dpkg-dev) installed.
-
-- Issue #15037: Build OS X installers with local copy of ncurses 5.9 libraries
- to avoid curses.unget_wch bug present in older versions of ncurses such as
- those shipped with OS X.
-
-- Issue #15560: Fix building _sqlite3 extension on OS X with an SDK. Also, for
- OS X installers, ensure consistent sqlite3 behavior and feature availability
- by building a local copy of libsqlite3 rather than depending on the wide range
- of versions supplied with various OS X releases.
-
-- Issue #8847: Disable COMDAT folding in Windows PGO builds.
-
-- Issue #14018: Fix OS X Tcl/Tk framework checking when using OS X SDKs.
-
-- Issue #16256: OS X installer now sets correct permissions for doc directory.
-
-- Issue #15431: Add _freeze_importlib project to regenerate importlib.h on
- Windows. Patch by Kristján Valur Jónsson.
-
-- Issue #14197: For OS X framework builds, ensure links to the shared library
- are created with the proper ABI suffix.
-
-- Issue #14330: For cross builds, don't use host python, use host search paths
- for host compiler.
-
-- Issue #15235: Allow Berkley DB versions up to 5.3 to build the dbm module.
-
-- Issue #15268: Search curses.h in /usr/include/ncursesw.
-
-
-What's New in Python 3.3.0 Beta 1?
-==================================
-
-*Release date: 27-Jun-2012*
-
-Core and Builtins
------------------
-
-- Fix a (most likely) very rare memory leak when calling main() and not being
- able to decode a command-line argument.
-
-- Issue #14815: Use Py_ssize_t instead of long for the object hash, to
- preserve all 64 bits of hash on Win64.
-
-- Issue #12268: File readline, readlines and read() or readall() methods
- no longer lose data when an underlying read system call is interrupted.
- IOError is no longer raised due to a read system call returning EINTR
- from within these methods.
-
-- Issue #11626: Add _SizeT functions to stable ABI.
-
-- Issue #15142: Fix reference leak when deallocating instances of types
- created using PyType_FromSpec().
-
-- Issue #10053: Don't close FDs when FileIO.__init__ fails. Loosely based on
- the work by Hirokazu Yamamoto.
-
-- Issue #15096: Removed support for ur'' as the raw notation isn't
- compatible with Python 2.x's raw unicode strings.
-
-- Issue #13783: Generator objects now use the identifier APIs internally
-
-- Issue #14874: Restore charmap decoding speed to pre-PEP 393 levels.
- Patch by Serhiy Storchaka.
-
-- Issue #15026: utf-16 encoding is now significantly faster (up to 10x).
- Patch by Serhiy Storchaka.
-
-- Issue #11022: open() and io.TextIOWrapper are now calling
- locale.getpreferredencoding(False) instead of locale.getpreferredencoding()
- in text mode if the encoding is not specified. Don't change temporary the
- locale encoding using locale.setlocale(), use the current locale encoding
- instead of the user preferred encoding.
-
-- Issue #14673: Add Eric Snow's sys.implementation implementation.
-
-- Issue #15038: Optimize python Locks on Windows.
-
-Library
--------
-
-- Issue #12288: Consider '0' and '0.0' as valid initialvalue
- for tkinter SimpleDialog.
-
-- Issue #15512: Add a __sizeof__ implementation for parser.
- Patch by Serhiy Storchaka.
-
-- Issue #15469: Add a __sizeof__ implementation for deque objects.
- Patch by Serhiy Storchaka.
-
-- Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
- Patch by Serhiy Storchaka.
-
-- Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
- Patch by Serhiy Storchaka.
-
-- Issue #15514: Correct __sizeof__ support for cpu_set.
- Patch by Serhiy Storchaka.
-
-- Issue #15177: Added dir_fd parameter to os.fwalk().
-
-- Issue #15061: Re-implemented hmac.compare_digest() in C to prevent further
- timing analysis and to support all buffer protocol aware objects as well as
- ASCII only str instances safely.
-
-- Issue #15164: Change return value of platform.uname() from a
- plain tuple to a collections.namedtuple.
-
-- Support Mageia Linux in the platform module.
-
-- Issue #11678: Support Arch linux in the platform module.
-
-- Issue #15118: Change return value of os.uname() and os.times() from
- plain tuples to immutable iterable objects with named attributes
- (structseq objects).
-
-- Speed up _decimal by another 10-15% by caching the thread local context
- that was last accessed. In the pi benchmark (64-bit platform, prec=9),
- _decimal is now only 1.5x slower than float.
-
-- Remove the packaging module, which is not ready for prime time.
-
-- Issue #15154: Add "dir_fd" parameter to os.rmdir, remove "rmdir"
- parameter from os.remove / os.unlink.
-
-- Issue #4489: Add a shutil.rmtree that isn't susceptible to symlink attacks.
- It is used automatically on platforms supporting the necessary os.openat()
- and os.unlinkat() functions. Main code by Martin von Löwis.
-
-- Issue #15156: HTMLParser now uses the new "html.entities.html5" dictionary.
-
-- Issue #11113: add a new "html5" dictionary containing the named character
- references defined by the HTML5 standard and the equivalent Unicode
- character(s) to the html.entities module.
-
-- Issue #15114: the strict mode of HTMLParser and the HTMLParseError exception
- are deprecated now that the parser is able to parse invalid markup.
-
-- Issue #3665: \u and \U escapes are now supported in unicode regular
- expressions. Patch by Serhiy Storchaka.
-
-- Issue #15153: Added inspect.getgeneratorlocals to simplify white box
- testing of generator state updates
-
-- Issue #13062: Added inspect.getclosurevars to simplify testing stateful
- closures
-
-- Issue #11024: Fixes and additional tests for Time2Internaldate.
-
-- Issue #14626: Large refactoring of functions / parameters in the os module.
- Many functions now support "dir_fd" and "follow_symlinks" parameters;
- some also support accepting an open file descriptor in place of a path
- string. Added os.support_* collections as LBYL helpers. Removed many
- functions only previously seen in 3.3 alpha releases (often starting with
- "f" or "l", or ending with "at"). Originally suggested by Serhiy Storchaka;
- implemented by Larry Hastings.
-
-- Issue #15008: Implement PEP 362 "Signature Objects".
- Patch by Yury Selivanov.
-
-- Issue: #15138: base64.urlsafe_{en,de}code() are now 3-4x faster.
-
-- Issue #444582: Add shutil.which, for finding programs on the system path.
- Original patch by Erik Demaine, with later iterations by Jan Killian
- and Brian Curtin.
-
-- Issue #14837: SSL errors now have ``library`` and ``reason`` attributes
- describing precisely what happened and in which OpenSSL submodule. The
- str() of a SSLError is also enhanced accordingly.
-
-- Issue #9527: datetime.astimezone() method will now supply a class
- timezone instance corresponding to the system local timezone when
- called with no arguments.
-
-- Issue #14653: email.utils.mktime_tz() no longer relies on system
- mktime() when timezone offest is supplied.
-
-- Issue #14684: zlib.compressobj() and zlib.decompressobj() now support the use
- of predefined compression dictionaries. Original patch by Sam Rushing.
-
-- Fix GzipFile's handling of filenames given as bytes objects.
-
-- Issue #14772: Return destination values from some shutil functions.
-
-- Issue #15064: Implement context manager protocol for multiprocessing types
-
-- Issue #15101: Make pool finalizer avoid joining current thread.
-
-- Issue #14657: The frozen instance of importlib used for bootstrap is now
- also the module imported as importlib._bootstrap.
-
-- Issue #14055: Add __sizeof__ support to _elementtree.
-
-- Issue #15054: A bug in tokenize.tokenize that caused string literals
- with 'b' prefixes to be incorrectly tokenized has been fixed.
- Patch by Serhiy Storchaka.
-
-- Issue #15006: Allow equality comparison between naive and aware
- time or datetime objects.
-
-- Issue #15036: Mailbox no longer throws an error if a flush is done
- between operations when removing or changing multiple items in mbox,
- MMDF, or Babyl mailboxes.
-
-- Issue #14059: Implement multiprocessing.Barrier.
-
-- Issue #15061: The inappropriately named hmac.secure_compare has been
- renamed to hmac.compare_digest, restricted to operating on bytes inputs
- only and had its documentation updated to more accurately reflect both its
- intent and its limitations
-
-- Issue #13841: Make child processes exit using sys.exit() on Windows.
-
-- Issue #14936: curses_panel was converted to PEP 3121 and PEP 384 API.
- Patch by Robin Schreiber.
-
-- Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
- in struct tm, time.struct_time objects returned by time.gmtime(),
- time.localtime() and time.strptime() functions now have tm_zone and
- tm_gmtoff attributes. Original patch by Paul Boddie.
-
-- Rename adjusted attribute to adjustable in time.get_clock_info() result.
-
-- Issue #3518: Remove references to non-existent BaseManager.from_address()
- method.
-
-- Issue #13857: Added textwrap.indent() function (initial patch by Ezra
- Berch)
-
-- Issue #2736: Added datetime.timestamp() method.
-
-- Issue #13854: Make multiprocessing properly handle non-integer
- non-string argument to SystemExit.
-
-- Issue #12157: Make pool.map() empty iterables correctly. Initial
- patch by mouad.
-
-- Issue #11823: disassembly now shows argument counts on calls with keyword args.
-
-- Issue #14711: os.stat_float_times() has been deprecated.
-
-- LZMAFile now accepts the modes "rb"/"wb"/"ab" as synonyms of "r"/"w"/"a".
-
-- The bz2 and lzma modules now each contain an open() function, allowing
- compressed files to readily be opened in text mode as well as binary mode.
-
-- BZ2File.__init__() and LZMAFile.__init__() now accept a file object as their
- first argument, rather than requiring a separate "fileobj" argument.
-
-- gzip.open() now accepts file objects as well as filenames.
-
-- Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
- when the path existed and had the S_ISGID mode bit set when it was
- not explicitly asked for. This is no longer an exception as mkdir
- cannot control if the OS sets that bit for it or not.
-
-- Issue #14989: Make the CGI enable option to http.server available via command
- line.
-
-- Issue #14987: Add a missing import statement to inspect.
-
-- Issue #1079: email.header.decode_header now correctly parses all the examples
- in RFC2047. There is a necessary visible behavior change: the leading and/or
- trailing whitespace on ASCII parts is now preserved.
-
-- Issue #14969: Better handling of exception chaining in contextlib.ExitStack
-
-- Issue #14963: Convert contextlib.ExitStack.__exit__ to use an iterative
- algorithm (Patch by Alon Horev)
-
-- Issue #14785: Add sys._debugmallocstats() to help debug low-level memory
- allocation issues
-
-- Issue #14443: Ensure that .py files are byte-compiled with the correct Python
- executable within bdist_rpm even on older versions of RPM
-
-C-API
------
-
-- Issue #15146: Add PyType_FromSpecWithBases. Patch by Robin Schreiber.
-
-- Issue #15042: Add PyState_AddModule and PyState_RemoveModule. Add version
- guard for Py_LIMITED_API additions. Patch by Robin Schreiber.
-
-- Issue #13783: Inadvertent additions to the public C API in the PEP 380
- implementation have either been removed or marked as private interfaces.
-
-Extension Modules
------------------
-
-- Issue #15000: Support the "unique" x32 architecture in _posixsubprocess.c.
-
-IDLE
-----
-
-- Issue #9803: Don't close IDLE on saving if breakpoint is open.
- Patch by Roger Serwy.
-
-- Issue #14962: Update text coloring in IDLE shell window after changing
- options. Patch by Roger Serwy.
-
-Documentation
--------------
-
-- Issue #15176: Clarified behavior, documentation, and implementation
- of os.listdir().
-
-- Issue #14982: Document that pkgutil's iteration functions require the
- non-standard iter_modules() method to be defined by an importer (something
- the importlib importers do not define).
-
-- Issue #15081: Document PyState_FindModule.
- Patch by Robin Schreiber.
-
-- Issue #14814: Added first draft of ipaddress module API reference
-
-Tests
------
-
-- Issue #15187: Bugfix: remove temporary directories test_shutil was leaving
- behind.
-
-- Issue #14769: test_capi now has SkipitemTest, which cleverly checks
- for "parity" between PyArg_ParseTuple() and the Python/getargs.c static
- function skipitem() for all possible "format units".
-
-- test_nntplib now tolerates being run from behind NNTP gateways that add
- "X-Antivirus" headers to articles
-
-- Issue #15043: test_gdb is now skipped entirely if gdb security settings
- block loading of the gdb hooks
-
-- Issue #14963: Add test cases for exception handling behaviour
- in contextlib.ExitStack (Initial patch by Alon Horev)
-
-Build
------
-
-- Issue #13590: Improve support for OS X Xcode 4:
- * Try to avoid building Python or extension modules with problematic
- llvm-gcc compiler.
- * Since Xcode 4 removes ppc support, extension module builds now
- check for ppc compiler support and automatically remove ppc and
- ppc64 archs when not available.
- * Since Xcode 4 no longer install SDKs in default locations,
- extension module builds now revert to using installed headers
- and libs if the SDK used to build the interpreter is not
- available.
- * Update ./configure to use better defaults for universal builds;
- in particular, --enable-universalsdk=yes uses the Xcode default
- SDK and --with-universal-archs now defaults to "intel" if ppc
- not available.
-
-- Issue #14225: Fix Unicode support for curses (#12567) on OS X
-
-- Issue #14928: Fix importlib bootstrap issues by using a custom executable
- (Modules/_freeze_importlib) to build Python/importlib.h.
-
-
-What's New in Python 3.3.0 Alpha 4?
-===================================
-
-*Release date: 31-May-2012*
-
-Core and Builtins
------------------
-
-- Issue #14835: Make plistlib output empty arrays & dicts like OS X.
- Patch by Sidney San Martín.
-
-- Issue #14744: Use the new _PyUnicodeWriter internal API to speed up
- str%args and str.format(args).
-
-- Issue #14930: Make memoryview objects weakrefable.
-
-- Issue #14775: Fix a potential quadratic dict build-up due to the garbage
- collector repeatedly trying to untrack dicts.
-
-- Issue #14857: fix regression in references to PEP 3135 implicit __class__
- closure variable (Reopens issue #12370)
-
-- Issue #14712 (PEP 405): Virtual environments. Implemented by Vinay Sajip.
-
-- Issue #14660 (PEP 420): Namespace packages. Implemented by Eric Smith.
-
-- Issue #14494: Fix __future__.py and its documentation to note that
- absolute imports are the default behavior in 3.0 instead of 2.7.
- Patch by Sven Marnach.
-
-- Issue #9260: A finer-grained import lock. Most of the import sequence
- now uses per-module locks rather than the global import lock, eliminating
- well-known issues with threads and imports.
-
-- Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs.
- Patch by Serhiy Storchaka.
-
-- asdl_seq and asdl_int_seq are now Py_ssize_t sized.
-
-- Issue #14133 (PEP 415): Implement suppression of __context__ display with an
- attribute on BaseException. This replaces the original mechanism of PEP 409.
-
-- Issue #14417: Mutating a dict during lookup now restarts the lookup instead
- of raising a RuntimeError (undoes issue #14205).
-
-- Issue #14738: Speed-up UTF-8 decoding on non-ASCII data. Patch by Serhiy
- Storchaka.
-
-- Issue #14700: Fix two broken and undefined-behaviour-inducing overflow checks
- in old-style string formatting.
-
-Library
--------
-
-- Issue #14690: Use monotonic clock instead of system clock in the sched,
- subprocess and trace modules.
-
-- Issue #14443: Tell rpmbuild to use the correct version of Python in
- bdist_rpm. Initial patch by Ross Lagerwall.
-
-- Issue #12515: email now registers a defect if it gets to EOF while parsing
- a MIME part without seeing the closing MIME boundary.
-
-- Issue #1672568: email now always decodes base64 payloads, adding padding and
- ignoring non-base64-alphabet characters if needed, and registering defects
- for any such problems.
-
-- Issue #14925: email now registers a defect when the parser decides that there
- is a missing header/body separator line. MalformedHeaderDefect, which the
- existing code would never actually generate, is deprecated.
-
-- Issue #10365: File open dialog now works instead of crashing even when
- the parent window is closed before the dialog. Patch by Roger Serwy.
-
-- Issue #8739: Updated smtpd to support RFC 5321, and added support for the
- RFC 1870 SIZE extension.
-
-- Issue #665194: Added a localtime function to email.utils to provide an
- aware local datetime for use in setting Date headers.
-
-- Issue #12586: Added new provisional policies that implement convenient
- unicode support for email headers. See What's New for details.
-
-- Issue #14731: Refactored email Policy framework to support full backward
- compatibility with Python 3.2 by default yet allow for the introduction of
- new features through new policies. Note that Policy.must_be_7bit is renamed
- to cte_type.
-
-- Issue #14876: Use user-selected font for highlight configuration.
-
-- Issue #14920: Fix the help(urllib.parse) failure on locale C on terminals.
- Have ascii characters in help.
-
-- Issue #14548: Make multiprocessing finalizers check pid before
- running to cope with possibility of gc running just after fork.
-
-- Issue #14036: Add an additional check to validate that port in urlparse does
- not go in illegal range and returns None.
-
-- Issue #14862: Add missing names to os.__all__
-
-- Issue #14875: Use float('inf') instead of float('1e66666') in the json module.
-
-- Issue #13585: Added contextlib.ExitStack
-
-- PEP 3144, Issue #14814: Added the ipaddress module
-
-- Issue #14426: Correct the Date format in Expires attribute of Set-Cookie
- Header in Cookie.py.
-
-- Issue #14588: The types module now provide new_class() and prepare_class()
- functions to support PEP 3115 compliant dynamic class creation. Patch by
- Daniel Urban and Nick Coghlan.
-
-- Issue #13152: Allow to specify a custom tabsize for expanding tabs in
- textwrap. Patch by John Feuerstein.
-
-- Issue #14721: Send the correct 'Content-length: 0' header when the body is an
- empty string ''. Initial Patch contributed by Arve Knudsen.
-
-- Issue #14072: Fix parsing of 'tel' URIs in urlparse by making the check for
- ports stricter.
-
-- Issue #9374: Generic parsing of query and fragment portions of url for any
- scheme. Supported both by RFC3986 and RFC2396.
-
-- Issue #14798: Fix the functions in pyclbr to raise an ImportError
- when the first part of a dotted name is not a package. Patch by
- Xavier de Gaye.
-
-- Issue #12098: multiprocessing on Windows now starts child processes
- using the same sys.flags as the current process. Initial patch by
- Sergey Mezentsev.
-
-- Issue #13031: Small speed-up for tarfile when unzipping tarfiles.
- Patch by Justin Peel.
-
-- Issue #14780: urllib.request.urlopen() now has a ``cadefault`` argument
- to use the default certificate store. Initial patch by James Oakley.
-
-- Issue #14829: Fix bisect and range() indexing with large indices
- (>= 2 ** 32) under 64-bit Windows.
-
-- Issue #14732: The _csv module now uses PEP 3121 module initialization.
- Patch by Robin Schreiber.
-
-- Issue #14809: Add HTTP status codes introduced by RFC 6585 to http.server
- and http.client. Patch by EungJun Yi.
-
-- Issue #14777: tkinter may return undecoded UTF-8 bytes as a string when
- accessing the Tk clipboard. Modify clipboad_get() to first request type
- UTF8_STRING when no specific type is requested in an X11 windowing
- environment, falling back to the current default type STRING if that fails.
- Original patch by Thomas Kluyver.
-
-- Issue #14773: Fix os.fwalk() failing on dangling symlinks.
-
-- Issue #12541: Be lenient with quotes around Realm field of HTTP Basic
- Authentation in urllib2.
-
-- Issue #14807: move undocumented tarfile.filemode() to stat.filemode() and add
- doc entry. Add tarfile.filemode alias with deprecation warning.
-
-- Issue #13815: TarFile.extractfile() now returns io.BufferedReader objects.
-
-- Issue #14532: Add a secure_compare() helper to the hmac module, to mitigate
- timing attacks. Patch by Jon Oberheide.
-
-- Add importlib.util.resolve_name().
-
-- Issue #14366: Support lzma compression in zip files.
- Patch by Serhiy Storchaka.
-
-- Issue #13959: Introduce importlib.find_loader() and document
- imp.find_module/load_module as deprecated.
-
-- Issue #14082: shutil.copy2() now copies extended attributes, if possible.
- Patch by Hynek Schlawack.
-
-- Issue #13959: Make importlib.abc.FileLoader.load_module()/get_filename() and
- importlib.machinery.ExtensionFileLoader.load_module() have their single
- argument be optional. Allows for the replacement (and thus deprecation) of
- imp.load_source()/load_package()/load_compiled().
-
-- Issue #13959: imp.get_suffixes() has been deprecated in favour of the new
- attributes on importlib.machinery: SOURCE_SUFFIXES, DEBUG_BYTECODE_SUFFIXES,
- OPTIMIZED_BYTECODE_SUFFIXES, BYTECODE_SUFFIXES, and EXTENSION_SUFFIXES. This
- led to an indirect deprecation of inspect.getmoduleinfo().
-
-- Issue #14662: Prevent shutil failures on OS X when destination does not
- support chflag operations. Patch by Hynek Schlawack.
-
-- Issue #14157: Fix time.strptime failing without a year on February 29th.
- Patch by Hynek Schlawack.
-
-- Issue #14753: Make multiprocessing's handling of negative timeouts
- the same as it was in Python 3.2.
-
-- Issue #14583: Fix importlib bug when a package's __init__.py would first
- import one of its modules then raise an error.
-
-- Issue #14741: Fix missing support for Ellipsis ('...') in parser module.
-
-- Issue #14697: Fix missing support for set displays and set comprehensions in
- parser module.
-
-- Issue #14701: Fix missing support for 'raise ... from' in parser module.
-
-- Add support for timeouts to the acquire() methods of
- multiprocessing's lock/semaphore/condition proxies.
-
-- Issue #13989: Add support for text mode to gzip.open().
-
-- Issue #14127: The os.stat() result object now provides three additional
- fields: st_ctime_ns, st_mtime_ns, and st_atime_ns, providing those times as an
- integer with nanosecond resolution. The functions os.utime(), os.lutimes(),
- and os.futimes() now accept a new parameter, ns, which accepts mtime and atime
- as integers with nanosecond resolution.
-
-- Issue #14127 and #10148: shutil.copystat now preserves exact mtime and atime
- on filesystems providing nanosecond resolution.
-
-IDLE
-----
-
-- Issue #14958: Change IDLE systax highlighting to recognize all string and
- byte literals supported in Python 3.3.
-
-- Issue #10997: Prevent a duplicate entry in IDLE's "Recent Files" menu.
-
-- Issue #14929: Stop IDLE 3.x from closing on Unicode decode errors when
- grepping. Patch by Roger Serwy.
-
-- Issue #12510: Attempting to get invalid tooltip no longer closes IDLE.
- Other tooltipss have been corrected or improved and the number of tests
- has been tripled. Original patch by Roger Serwy.
-
-Tools/Demos
------------
-
-- Issue #14695: Bring Tools/parser/unparse.py support up to date with
- the Python 3.3 Grammar.
-
-Build
------
-
-- Issue #14472: Update .gitignore. Patch by Matej Cepl.
-
-- Upgrade Windows library versions: bzip 1.0.6, OpenSSL 1.0.1c.
-
-- Issue #14693: Under non-Windows platforms, hashlib's fallback modules are
- always compiled, even if OpenSSL is present at build time.
-
-- Issue #13210: Windows build now uses VS2010, ported from VS2008.
-
-C-API
------
-
-- Issue #14705: The PyArg_Parse() family of functions now support the 'p' format
- unit, which accepts a "boolean predicate" argument. It converts any Python
- value into an integer--0 if it is "false", and 1 otherwise.
-
-Documentation
--------------
-
-- Issue #14863: Update the documentation of os.fdopen() to reflect the
- fact that it's only a thin wrapper around open() anymore.
-
-- Issue #14588: The language reference now accurately documents the Python 3
- class definition process. Patch by Nick Coghlan.
-
-- Issue #14943: Correct a default argument value for winreg.OpenKey
- and correctly list the argument names in the function's explanation.
-
-
-What's New in Python 3.3.0 Alpha 3?
-===================================
-
-*Release date: 01-May-2012*
-
-Core and Builtins
------------------
-
-- Issue #14699: Fix calling the classmethod descriptor directly.
-
-- Issue #14433: Prevent msvcrt crash in interactive prompt when stdin is closed.
-
-- Issue #14521: Make result of float('nan') and float('-nan') more consistent
- across platforms.
-
-- Issue #14646: __import__() sets __loader__ if the loader did not.
-
-- Issue #14605: No longer have implicit entries in sys.meta_path. If
- sys.meta_path is found to be empty, raise ImportWarning.
-
-- Issue #14605: No longer have implicit entries in sys.path_hooks. If
- sys.path_hooks is found to be empty, a warning will be raised. None is now
- inserted into sys.path_importer_cache if no finder was discovered. This also
- means imp.NullImporter is no longer implicitly used.
-
-- Issue #13903: Implement PEP 412. Individual dictionary instances can now share
- their keys with other dictionaries. Classes take advantage of this to share
- their instance dictionary keys for improved memory and performance.
-
-- Issue #11603 (again): Setting __repr__ to __str__ now raises a RuntimeError
- when repr() or str() is called on such an object.
-
-- Issue #14658: Fix binding a special method to a builtin implementation of a
- special method with a different name.
-
-- Issue #14630: Fix a memory access bug for instances of a subclass of int
- with value 0.
-
-- Issue #14339: Speed improvements to bin, oct and hex functions. Patch by
- Serhiy Storchaka.
-
-- Issue #14385: It is now possible to use a custom type for the __builtins__
- namespace, instead of a dict. It can be used for sandboxing for example.
- Raise also a NameError instead of ImportError if __build_class__ name if not
- found in __builtins__.
-
-- Issue #12599: Be more strict in accepting None compared to a false-like
- object for importlib.util.module_for_loader and
- importlib.machinery.PathFinder.
-
-- Issue #14612: Fix jumping around with blocks by setting f_lineno.
-
-- Issue #14592: Attempting a relative import w/o __package__ or __name__ set in
- globals raises a KeyError.
-
-- Issue #14607: Fix keyword-only arguments which started with ``__``.
-
-- Issue #10854: The ImportError raised when an extension module on Windows
- fails to import now uses the new path and name attributes from
- Issue #1559549.
-
-- Issue #13889: Check and (if necessary) set FPU control word before calling
- any of the dtoa.c string <-> float conversion functions, on MSVC builds of
- Python. This fixes issues when embedding Python in a Delphi app.
-
-- __import__() now matches PEP 328 and documentation by defaulting 'index' to 0
- instead of -1 and removing support for negative values.
-
-- Issue #2377: Make importlib the implementation of __import__().
-
-- Issue #1559549: ImportError now has 'name' and 'path' attributes that are set
- using keyword arguments to its constructor. They are currently not set by
- import as they are meant for use by importlib.
-
-- Issue #14474: Save and restore exception state in thread.start_new_thread()
- while writing error message if the thread leaves a unhandled exception.
-
-- Issue #13019: Fix potential reference leaks in bytearray.extend(). Patch
- by Suman Saha.
-
-Library
--------
-
-- Issue #14768: os.path.expanduser('~/a') doesn't works correctly when HOME is '/'.
-
-- Issue #14371: Support bzip2 in zipfile module. Patch by Serhiy Storchaka.
-
-- Issue #13183: Fix pdb skipping frames after hitting a breakpoint and running
- step. Patch by Xavier de Gaye.
-
-- Issue #14696: Fix parser module to understand 'nonlocal' declarations.
-
-- Issue #10941: Fix imaplib.Internaldate2tuple to produce correct result near
- the DST transition. Patch by Joe Peterson.
-
-- Issue #9154: Fix parser module to understand function annotations.
-
-- Issue #6085: In http.server.py SimpleHTTPServer.address_string returns the
- client ip address instead client hostname. Patch by Charles-François Natali.
-
-- Issue #14309: Deprecate time.clock(), use time.perf_counter() or
- time.process_time() instead.
-
-- Issue #14428: Implement the PEP 418. Add time.get_clock_info(),
- time.perf_counter() and time.process_time() functions, and rename
- time.steady() to time.monotonic().
-
-- Issue #14646: importlib.util.module_for_loader() now sets __loader__ and
- __package__ (when possible).
-
-- Issue #14664: It is now possible to use @unittest.skip{If,Unless} on a
- test class that doesn't inherit from TestCase (i.e. a mixin).
-
-- Issue #4892: multiprocessing Connections can now be transferred over
- multiprocessing Connections. Patch by Richard Oudkerk (sbt).
-
-- Issue #14160: TarFile.extractfile() failed to resolve symbolic links when
- the links were not located in an archive subdirectory.
-
-- Issue #14638: pydoc now treats non-string __name__ values as if they
- were missing, instead of raising an error.
-
-- Issue #13684: Fix httplib tunnel issue of infinite loops for certain sites
- which send EOF without trailing \r\n.
-
-- Issue #14605: Add importlib.abc.FileLoader, importlib.machinery.(FileFinder,
- SourceFileLoader, SourcelessFileLoader, ExtensionFileLoader).
-
-- Issue #13959: imp.cache_from_source()/source_from_cache() now follow
- os.path.join()/split() semantics for path manipulation instead of its prior,
- custom semantics of caring the right-most path separator forward in path
- joining.
-
-- Issue #2193: Allow ":" character in Cookie NAME values.
-
-- Issue #14629: tokenizer.detect_encoding will specify the filename in the
- SyntaxError exception if found at readline.__self__.name.
-
-- Issue #14629: Raise SyntaxError in tokenizer.detect_encoding if the
- first two lines have non-UTF-8 characters without an encoding declaration.
-
-- Issue #14308: Fix an exception when a "dummy" thread is in the threading
- module's active list after a fork().
-
-- Issue #11750: The Windows API functions scattered in the _subprocess and
- _multiprocessing.win32 modules now live in a single module "_winapi".
- Patch by sbt.
-
-- Issue #14087: multiprocessing: add Condition.wait_for(). Patch by sbt.
-
-- Issue #14538: HTMLParser can now parse correctly start tags that contain
- a bare '/'.
-
-- Issue #14452: SysLogHandler no longer inserts a UTF-8 BOM into the message.
-
-- Issue #14386: Expose the dict_proxy internal type as types.MappingProxyType.
-
-- Issue #13959: Make imp.reload() always use a module's __loader__ to perform
- the reload.
-
-- Issue #13959: Add imp.py and rename the built-in module to _imp, allowing for
- re-implementing parts of the module in pure Python.
-
-- Issue #13496: Fix potential overflow in bisect.bisect algorithm when applied
- to a collection of size > sys.maxsize / 2.
-
-- Have importlib take advantage of ImportError's new 'name' and 'path'
- attributes.
-
-- Issue #14399: zipfile now recognizes that the archive has been modified even
- if only the comment is changed. In addition, the TypeError that results from
- trying to set a non-binary value as a comment is now raised at the time
- the comment is set rather than at the time the zipfile is written.
-
-- trace.CoverageResults.is_ignored_filename() now ignores any name that starts
- with "<" and ends with ">" instead of special-casing "<string>" and
- "<doctest ".
-
-- Issue #12537: The mailbox module no longer depends on knowledge of internal
- implementation details of the email package Message object.
-
-- Issue #7978: socketserver now restarts the select() call when EINTR is
- returned. This avoids crashing the server loop when a signal is received.
- Patch by Jerzy Kozera.
-
-- Issue #14522: Avoid duplicating socket handles in multiprocessing.connection.
- Patch by sbt.
-
-- Don't Py_DECREF NULL variable in io.IncrementalNewlineDecoder.
-
-- Issue #3033: Add displayof parameter to tkinter font. Patch by Guilherme Polo.
-
-- Issue #14482: Raise a ValueError, not a NameError, when trying to create
- a multiprocessing Client or Listener with an AF_UNIX type address under
- Windows. Patch by Popa Claudiu.
-
-- Issue #802310: Generate always unique tkinter font names if not directly passed.
-
-- Issue #14151: Raise a ValueError, not a NameError, when trying to create
- a multiprocessing Client or Listener with an AF_PIPE type address under
- non-Windows platforms. Patch by Popa Claudiu.
-
-- Issue #14493: Use gvfs-open or xdg-open in webbrowser.
-
-Build
------
-
-- "make touch" will now touch generated files that are checked into Mercurial,
- after a "hg update" which failed to bring the timestamps into the right order.
-
-Tests
------
-
-- Issue #14026: In test_cmd_line_script, check that sys.argv is populated
- correctly for the various invocation approaches (Patch by Jason Yeo)
-
-- Issue #14032: Fix incorrect variable name in test_cmd_line_script debugging
- message (Patch by Jason Yeo)
-
-- Issue #14589: Update certificate chain for sha256.tbs-internet.com, fixing
- a test failure in test_ssl.
-
-- Issue #14355: Regrtest now supports the standard unittest test loading, and
- will use it if a test file contains no `test_main` method.
-
-IDLE
-----
-
-- Issue #8515: Set __file__ when run file in IDLE.
- Initial patch by Bruce Frederiksen.
-
-- Issue #14496: Fix wrong name in idlelib/tabbedpages.py.
- Patch by Popa Claudiu.
-
-Tools / Demos
--------------
-
-- Issue #3561: The Windows installer now has an option, off by default, for
- placing the Python installation into the system "Path" environment variable.
-
-- Issue #13165: stringbench is now available in the Tools/stringbench folder.
- It used to live in its own SVN project.
-
-C-API
------
-
-- Issue #14098: New functions PyErr_GetExcInfo and PyErr_SetExcInfo.
- Patch by Stefan Behnel.
-
-
-What's New in Python 3.3.0 Alpha 2?
-===================================
-
-*Release date: 01-Apr-2012*
-
-Core and Builtins
------------------
-
-- Issue #1683368: object.__new__ and object.__init__ raise a TypeError if they
- are passed arguments and their complementary method is not overridden.
-
-- Issue #14378: Fix compiling ast.ImportFrom nodes with a "__future__" string as
- the module name that was not interned.
-
-- Issue #14331: Use significantly less stack space when importing modules by
- allocating path buffers on the heap instead of the stack.
-
-- Issue #14334: Prevent in a segfault in type.__getattribute__ when it was not
- passed strings.
-
-- Issue #1469629: Allow cycles through an object's __dict__ slot to be
- collected. (For example if ``x.__dict__ is x``).
-
-- Issue #14205: dict lookup raises a RuntimeError if the dict is modified
- during a lookup.
-
-- Issue #14220: When a generator is delegating to another iterator with the
- yield from syntax, it needs to have its ``gi_running`` flag set to True.
-
-- Issue #14435: Remove dedicated block allocator from floatobject.c and rely
- on the PyObject_Malloc() api like all other objects.
-
-- Issue #14471: Fix a possible buffer overrun in the winreg module.
-
-- Issue #14288: Allow the serialization of builtin iterators
-
-Library
--------
-
-- Issue #14300: Under Windows, sockets created using socket.dup() now allow
- overlapped I/O. Patch by sbt.
-
-- Issue #13872: socket.detach() now marks the socket closed (as mirrored
- in the socket repr()). Patch by Matt Joiner.
-
-- Issue #14406: Fix a race condition when using ``concurrent.futures.wait(
- return_when=ALL_COMPLETED)``. Patch by Matt Joiner.
-
-- Issue #5136: deprecate old, unused functions from tkinter.
-
-- Issue #14416: syslog now defines the LOG_ODELAY and LOG_AUTHPRIV constants
- if they are defined in <syslog.h>.
-
-- Issue #14295: Add unittest.mock
-
-- Issue #7652: Add --with-system-libmpdec option to configure for linking
- the _decimal module against an installed libmpdec.
-
-- Issue #14380: MIMEText now defaults to utf-8 when passed non-ASCII unicode
- with no charset specified.
-
-- Issue #10340: asyncore - properly handle EINVAL in dispatcher constructor on
- OSX; avoid to call handle_connect in case of a disconnected socket which
- was not meant to connect.
-
-- Issue #14204: The ssl module now has support for the Next Protocol
- Negotiation extension, if available in the underlying OpenSSL library.
- Patch by Colin Marc.
-
-- Issue #3035: Unused functions from tkinter are marked as pending deprecated.
-
-- Issue #12757: Fix the skipping of doctests when python is run with -OO so
- that it works in unittest's verbose mode as well as non-verbose mode.
-
-- Issue #7652: Integrate the decimal floating point libmpdec library to speed
- up the decimal module. Performance gains of the new C implementation are
- between 10x and 100x, depending on the application.
-
-- Issue #14269: SMTPD now conforms to the RFC and requires a HELO command
- before MAIL, RCPT, or DATA.
-
-- Issue #13694: asynchronous connect in asyncore.dispatcher does not set addr
- attribute.
-
-- Issue #14344: fixed the repr of email.policy objects.
-
-- Issue #11686: Added missing entries to email package __all__ lists
- (mostly the new Bytes classes).
-
-- Issue #14335: multiprocessing's custom Pickler subclass now inherits from
- the C-accelerated implementation. Patch by sbt.
-
-- Issue #10484: Fix the CGIHTTPServer's PATH_INFO handling problem.
-
-- Issue #11199: Fix the with urllib which hangs on particular ftp urls.
-
-- Improve the memory utilization and speed of functools.lru_cache.
-
-- Issue #14222: Use the new time.steady() function instead of time.time() for
- timeout in queue and threading modules to not be affected of system time
- update.
-
-- Issue #13248: Remove lib2to3.pytree.Base.get_prefix/set_prefix.
-
-- Issue #14234: CVE-2012-0876: Randomize hashes of xml attributes in the hash
- table internal to the pyexpat module's copy of the expat library to avoid a
- denial of service due to hash collisions. Patch by David Malcolm with some
- modifications by the expat project.
-
-- Issue #12818: format address no longer needlessly \ escapes ()s in names when
- the name ends up being quoted.
-
-- Issue #14062: BytesGenerator now correctly folds Header objects,
- including using linesep when folding.
-
-- Issue #13839: When invoked on the command-line, the pstats module now
- accepts several filenames of profile stat files and merges them all.
- Patch by Matt Joiner.
-
-- Issue #14291: Email now defaults to utf-8 for non-ASCII unicode headers
- instead of raising an error. This fixes a regression relative to 2.7.
-
-- Issue #989712: Support using Tk without a mainloop.
-
-- Issue #3835: Refuse to use unthreaded Tcl in threaded Python.
-
-- Issue #2843: Add new Tk API to Tkinter.
-
-- Issue #14184: Increase the default stack size for secondary threads on
- Mac OS X to avoid interpreter crashes when using threads on 10.7.
-
-- Issue #14180: datetime.date.fromtimestamp(),
- datetime.datetime.fromtimestamp() and datetime.datetime.utcfromtimestamp()
- now raise an OSError instead of ValueError if localtime() or gmtime() failed.
-
-- Issue #14180: time.ctime(), gmtime(), time.localtime(),
- datetime.date.fromtimestamp(), datetime.datetime.fromtimestamp() and
- datetime.datetime.utcfromtimestamp() now raises an OverflowError, instead of
- a ValueError, if the timestamp does not fit in time_t.
-
-- Issue #14180: datetime.datetime.fromtimestamp() and
- datetime.datetime.utcfromtimestamp() now round microseconds towards zero
- instead of rounding to nearest with ties going away from zero.
-
-- Issue #10543: Fix unittest test discovery with Jython bytecode files.
-
-- Issue #1178863: Separate initialisation from setting when initializing
- Tkinter.Variables; harmonize exceptions to ValueError; only delete variables
- that have not been deleted; assert that variable names are strings.
-
-- Issue #14104: Implement time.monotonic() on Mac OS X, patch written by
- Nicholas Riley.
-
-- Issue #13394: the aifc module now uses warnings.warn() to signal warnings.
-
-- Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under
- Windows when the child process has already exited.
-
-- Issue #14223: curses.addch() is no more limited to the range 0-255 when the
- Python curses is not linked to libncursesw. It was a regression introduced
- in Python 3.3a1.
-
-- Issue #14168: Check for presence of Element._attrs in minidom before
- accessing it.
-
-- Issue #12328: Fix multiprocessing's use of overlapped I/O on Windows.
- Also, add a multiprocessing.connection.wait(rlist, timeout=None) function
- for polling multiple objects at once. Patch by sbt.
-
-- Issue #14007: Accept incomplete TreeBuilder objects (missing start, end,
- data or close method) for the Python implementation as well.
- Drop the no-op TreeBuilder().xml() method from the C implementation.
-
-- Issue #14210: pdb now has tab-completion not only for command names, but
- also for their arguments, wherever possible.
-
-- Issue #14310: Sockets can now be with other processes on Windows using
- the api socket.socket.share() and socket.fromshare().
-
-- Issue #10576: The gc module now has a 'callbacks' member that will get
- called when garbage collection takes place.
-
-Build
------
-
-- Issue #14557: Fix extensions build on HP-UX. Patch by Adi Roiban.
-
-- Issue #14387: Do not include accu.h from Python.h.
-
-- Issue #14359: Only use O_CLOEXEC in _posixmodule.c if it is defined.
- Based on patch from Hervé Coatanhay.
-
-- Issue #14321: Do not run pgen during the build if files are up to date.
-
-Documentation
--------------
-
-- Issue #14034: added the argparse tutorial.
-
-- Issue #14324: Fix configure tests for cross builds.
-
-- Issue #14327: Call AC_CANONICAL_HOST in configure.ac and check in
- config.{guess,sub}. Don't use uname calls for cross builds.
-
-Extension Modules
------------------
-
-- Issue #9041: An issue in ctypes.c_longdouble, ctypes.c_double, and
- ctypes.c_float that caused an incorrect exception to be returned in the
- case of overflow has been fixed.
-
-- Issue #14212: The re module didn't retain a reference to buffers it was
- scanning, resulting in segfaults.
-
-- Issue #14259: The finditer() method of re objects did not take any
- keyword arguments, contrary to the documentation.
-
-- Issue #10142: Support for SEEK_HOLE/SEEK_DATA (for example, under ZFS).
-
-Tests
------
-
-- Issue #14442: Add missing errno import in test_smtplib.
-
-- Issue #8315: (partial fix) python -m unittest test.test_email now works.
-
-
-What's New in Python 3.3.0 Alpha 1?
-===================================
-
-*Release date: 05-Mar-2012*
-
-Core and Builtins
------------------
-
-- Issue #14172: Fix reference leak when marshalling a buffer-like object
- (other than a bytes object).
-
-- Issue #13521: dict.setdefault() now does only one lookup for the given key,
- making it "atomic" for many purposes. Patch by Filip Gruszczyński.
-
-- PEP 409, Issue #6210: "raise X from None" is now supported as a means of
- suppressing the display of the chained exception context. The chained
- context still remains available as the __context__ attribute.
-
-- Issue #10181: New memoryview implementation fixes multiple ownership
- and lifetime issues of dynamically allocated Py_buffer members (#9990)
- as well as crashes (#8305, #7433). Many new features have been added
- (See whatsnew/3.3), and the documentation has been updated extensively.
- The ndarray test object from _testbuffer.c implements all aspects of
- PEP-3118, so further development towards the complete implementation
- of the PEP can proceed in a test-driven manner.
-
- Thanks to Nick Coghlan, Antoine Pitrou and Pauli Virtanen for review
- and many ideas.
-
-- Issue #12834: Fix incorrect results of memoryview.tobytes() for
- non-contiguous arrays.
-
-- Issue #5231: Introduce memoryview.cast() method that allows changing
- format and shape without making a copy of the underlying memory.
-
-- Issue #14084: Fix a file descriptor leak when importing a module with a
- bad encoding.
-
-- Upgrade Unicode data to Unicode 6.1.
-
-- Issue #14040: Remove rarely used file name suffixes for C extensions
- (under POSIX mainly).
-
-- Issue #14051: Allow arbitrary attributes to be set of classmethod and
- staticmethod.
-
-- Issue #13703: oCERT-2011-003: Randomize hashes of str and bytes to protect
- against denial of service attacks due to hash collisions within the dict and
- set types. Patch by David Malcolm, based on work by Victor Stinner.
-
-- Issue #13020: Fix a reference leak when allocating a structsequence object
- fails. Patch by Suman Saha.
-
-- Issue #13908: Ready types returned from PyType_FromSpec.
-
-- Issue #11235: Fix OverflowError when trying to import a source file whose
- modification time doesn't fit in a 32-bit timestamp.
-
-- Issue #12705: A SyntaxError exception is now raised when attempting to
- compile multiple statements as a single interactive statement.
-
-- Fix the builtin module initialization code to store the init function for
- future reinitialization.
-
-- Issue #8052: The posix subprocess module would take a long time closing
- all possible file descriptors in the child process rather than just open
- file descriptors. It now closes only the open fds if possible for the
- default close_fds=True behavior.
-
-- Issue #13629: Renumber the tokens in token.h so that they match the indexes
- into _PyParser_TokenNames.
-
-- Issue #13752: Add a casefold() method to str.
-
-- Issue #13761: Add a "flush" keyword argument to the print() function,
- used to ensure flushing the output stream.
-
-- Issue #13645: pyc files now contain the size of the corresponding source
- code, to avoid timestamp collisions (especially on filesystems with a low
- timestamp resolution) when checking for freshness of the bytecode.
-
-- PEP 380, Issue #11682: Add "yield from <x>" to support easy delegation to
- subgenerators (initial patch by Greg Ewing, integration into 3.3 by
- Renaud Blanch, Ryan Kelly, Zbigniew Jędrzejewski-Szmek and Nick Coghlan)
-
-- Issue #13748: Raw bytes literals can now be written with the ``rb`` prefix
- as well as ``br``.
-
-- Issue #12736: Use full unicode case mappings for upper, lower, and title case.
-
-- Issue #12760: Add a create mode to open(). Patch by David Townshend.
-
-- Issue #13738: Simplify implementation of bytes.lower() and bytes.upper().
-
-- Issue #13577: Built-in methods and functions now have a __qualname__.
- Patch by sbt.
-
-- Issue #6695: Full garbage collection runs now clear the freelist of set
- objects. Initial patch by Matthias Troffaes.
-
-- Fix OSError.__init__ and OSError.__new__ so that each of them can be
- overriden and take additional arguments (followup to issue #12555).
-
-- Fix the fix for issue #12149: it was incorrect, although it had the side
- effect of appearing to resolve the issue. Thanks to Mark Shannon for
- noticing.
-
-- Issue #13505: Pickle bytes objects in a way that is compatible with
- Python 2 when using protocols <= 2.
-
-- Issue #11147: Fix an unused argument in _Py_ANNOTATE_MEMORY_ORDER. (Fix
- given by Campbell Barton).
-
-- Issue #13503: Use a more efficient reduction format for bytearrays with
- pickle protocol >= 3. The old reduction format is kept with older protocols
- in order to allow unpickling under Python 2. Patch by Irmen de Jong.
-
-- Issue #7111: Python can now be run without a stdin, stdout or stderr
- stream. It was already the case with Python 2. However, the corresponding
- sys module entries are now set to None (instead of an unusable file object).
-
-- Issue #11849: Ensure that free()d memory arenas are really released
- on POSIX systems supporting anonymous memory mappings. Patch by
- Charles-François Natali.
-
-- PEP 3155 / issue #13448: Qualified name for classes and functions.
-
-- Issue #13436: Fix a bogus error message when an AST object was passed
- an invalid integer value.
-
-- Issue #13411: memoryview objects are now hashable when the underlying
- object is hashable.
-
-- Issue #13338: Handle all enumerations in _Py_ANNOTATE_MEMORY_ORDER
- to allow compiling extension modules with -Wswitch-enum on gcc.
- Initial patch by Floris Bruynooghe.
-
-- Issue #10227: Add an allocation cache for a single slice object. Patch by
- Stefan Behnel.
-
-- Issue #13393: BufferedReader.read1() now asks the full requested size to
- the raw stream instead of limiting itself to the buffer size.
-
-- Issue #13392: Writing a pyc file should now be atomic under Windows as well.
-
-- Issue #13333: The UTF-7 decoder now accepts lone surrogates (the encoder
- already accepts them).
-
-- Issue #13389: Full garbage collection passes now clear the freelists for
- list and dict objects. They already cleared other freelists in the
- interpreter.
-
-- Issue #13327: Remove the need for an explicit None as the second argument
- to os.utime, os.lutimes, os.futimes, os.futimens, os.futimesat, in
- order to update to the current time. Also added keyword argument
- handling to os.utimensat in order to remove the need for explicit None.
-
-- Issue #13350: Simplify some C code by replacing most usages of
- PyUnicode_Format by PyUnicode_FromFormat.
-
-- Issue #13342: input() used to ignore sys.stdin's and sys.stdout's unicode
- error handler in interactive mode (when calling into PyOS_Readline()).
-
-- Issue #9896: Add start, stop, and step attributes to range objects.
-
-- Issue #13343: Fix a SystemError when a lambda expression uses a global
- variable in the default value of a keyword-only argument: ``lambda *,
- arg=GLOBAL_NAME: None``
-
-- Issue #12797: Added custom opener parameter to builtin open() and
- FileIO.open().
-
-- Issue #10519: Avoid unnecessary recursive function calls in
- setobject.c.
-
-- Issue #10363: Deallocate global locks in Py_Finalize().
-
-- Issue #13018: Fix reference leaks in error paths in dictobject.c.
- Patch by Suman Saha.
-
-- Issue #13201: Define '==' and '!=' to compare range objects based on
- the sequence of values they define (instead of comparing based on
- object identity).
-
-- Issue #1294232: In a few cases involving metaclass inheritance, the
- interpreter would sometimes invoke the wrong metaclass when building a new
- class object. These cases now behave correctly. Patch by Daniel Urban.
-
-- Issue #12753: Add support for Unicode name aliases and named sequences.
- Both ``unicodedata.lookup()`` and '\N{...}' now resolve aliases,
- and ``unicodedata.lookup()`` resolves named sequences too.
-
-- Issue #12170: The count(), find(), rfind(), index() and rindex() methods
- of bytes and bytearray objects now accept an integer between 0 and 255
- as their first argument. Patch by Petri Lehtinen.
-
-- Issue #12604: VTRACE macro expanded to no-op in _sre.c to avoid compiler
- warnings. Patch by Josh Triplett and Petri Lehtinen.
-
-- Issue #12281: Rewrite the MBCS codec to handle correctly replace and ignore
- error handlers on all Windows versions. The MBCS codec is now supporting all
- error handlers, instead of only replace to encode and ignore to decode.
-
-- Issue #13188: When called without an explicit traceback argument,
- generator.throw() now gets the traceback from the passed exception's
- ``__traceback__`` attribute. Patch by Petri Lehtinen.
-
-- Issue #13146: Writing a pyc file is now atomic under POSIX.
-
-- Issue #7833: Extension modules built using distutils on Windows will no
- longer include a "manifest" to prevent them failing at import time in some
- embedded situations.
-
-- PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
-
-- Add internal API for static strings (_Py_identifier et al.).
-
-- Issue #13063: the Windows error ERROR_NO_DATA (numbered 232 and described
- as "The pipe is being closed") is now mapped to POSIX errno EPIPE
- (previously EINVAL).
-
-- Issue #12911: Fix memory consumption when calculating the repr() of huge
- tuples or lists.
-
-- PEP 393: flexible string representation. Thanks to Torsten Becker for the
- initial implementation, and Victor Stinner for various bug fixes.
-
-- Issue #14081: The 'sep' and 'maxsplit' parameter to str.split, bytes.split,
- and bytearray.split may now be passed as keyword arguments.
-
-- Issue #13012: The 'keepends' parameter to str.splitlines may now be passed
- as a keyword argument: "my_string.splitlines(keepends=True)". The same
- change also applies to bytes.splitlines and bytearray.splitlines.
-
-- Issue #7732: Don't open a directory as a file anymore while importing a
- module. Ignore the direcotry if its name matchs the module name (e.g.
- "__init__.py") and raise a ImportError instead.
-
-- Issue #13021: Missing decref on an error path. Thanks to Suman Saha for
- finding the bug and providing a patch.
-
-- Issue #12973: Fix overflow checks that relied on undefined behaviour in
- list_repeat (listobject.c) and islice_next (itertoolsmodule.c). These bugs
- caused test failures with recent versions of Clang.
-
-- Issue #12904: os.utime, os.futimes, os.lutimes, and os.futimesat now write
- atime and mtime with nanosecond precision on modern POSIX platforms.
-
-- Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
- mapped to POSIX errno ENOTDIR (previously EINVAL).
-
-- Issue #9200: The str.is* methods now work with strings that contain non-BMP
- characters even in narrow Unicode builds.
-
-- Issue #12791: Break reference cycles early when a generator exits with
- an exception.
-
-- Issue #12773: Make __doc__ mutable on user-defined classes.
-
-- Issue #12766: Raise a ValueError when creating a class with a class variable
- that conflicts with a name in __slots__.
-
-- Issue #12266: Fix str.capitalize() to correctly uppercase/lowercase
- titlecased and cased non-letter characters.
-
-- Issue #12732: In narrow unicode builds, allow Unicode identifiers which fall
- outside the BMP.
-
-- Issue #12575: Validate user-generated AST before it is compiled.
-
-- Make type(None), type(Ellipsis), and type(NotImplemented) callable. They
- return the respective singleton instances.
-
-- Forbid summing bytes with sum().
-
-- Verify the types of AST strings and identifiers provided by the user before
- compiling them.
-
-- Issue #12647: The None object now has a __bool__() method that returns False.
- Formerly, bool(None) returned False only because of special case logic
- in PyObject_IsTrue().
-
-- Issue #12579: str.format_map() now raises a ValueError if used on a
- format string that contains positional fields. Initial patch by
- Julian Berman.
-
-- Issue #10271: Allow warnings.showwarning() be any callable.
-
-- Issue #11627: Fix segfault when __new__ on a exception returns a
- non-exception class.
-
-- Issue #12149: Update the method cache after a type's dictionary gets
- cleared by the garbage collector. This fixes a segfault when an instance
- and its type get caught in a reference cycle, and the instance's
- deallocator calls one of the methods on the type (e.g. when subclassing
- IOBase). Diagnosis and patch by Davide Rizzo.
-
-- Issue #9611, Issue #9015: FileIO.read() clamps the length to INT_MAX on Windows.
-
-- Issue #9642: Uniformize the tests on the availability of the mbcs codec, add
- a new HAVE_MBCS define.
-
-- Issue #9642: Fix filesystem encoding initialization: use the ANSI code page
- on Windows if the mbcs codec is not available, and fail with a fatal error if
- we cannot get the locale encoding (if nl_langinfo(CODESET) is not available)
- instead of using UTF-8.
-
-- When a generator yields, do not retain the caller's exception state on the
- generator.
-
-- Issue #12475: Prevent generators from leaking their exception state into the
- caller's frame as they return for the last time.
-
-- Issue #12291: You can now load multiple marshalled objects from a stream,
- with other data interleaved between marshalled objects.
-
-- Issue #12356: When required positional or keyword-only arguments are not
- given, produce a informative error message which includes the name(s) of the
- missing arguments.
-
-- Issue #12370: Fix super with no arguments when __class__ is overriden in the
- class body.
-
-- Issue #12084: os.stat on Windows now works properly with relative symbolic
- links when called from any directory.
-
-- Loosen type restrictions on the __dir__ method. __dir__ can now return any
- sequence, which will be converted to a list and sorted by dir().
-
-- Issue #12265: Make error messages produced by passing an invalid set of
- arguments to a function more informative.
-
-- Issue #12225: Still allow Python to build if Python is not in its hg repo or
- mercurial is not installed.
-
-- Issue #1195: my_fgets() now always clears errors before calling fgets(). Fix
- the following case: sys.stdin.read() stopped with CTRL+d (end of file),
- raw_input() interrupted by CTRL+c.
-
-- Issue #12216: Allow unexpected EOF errors to happen on any line of the file.
-
-- Issue #12199: The TryExcept and TryFinally and AST nodes have been unified
- into a Try node.
-
-- Issue #9670: Increase the default stack size for secondary threads on
- Mac OS X and FreeBSD to reduce the chances of a crash instead of a
- "maximum recursion depth" RuntimeError exception.
- (patch by Ronald Oussoren)
-
-- Issue #12106: The use of the multiple-with shorthand syntax is now reflected
- in the AST.
-
-- Issue #12190: Try to use the same filename object when compiling unmarshalling
- a code objects in the same file.
-
-- Issue #12166: Move implementations of dir() specialized for various types into
- the __dir__() methods of those types.
-
-- Issue #5715: In socketserver, close the server socket in the child process.
-
-- Correct lookup of __dir__ on objects. Among other things, this causes errors
- besides AttributeError found on lookup to be propagated.
-
-- Issue #12060: Use sig_atomic_t type and volatile keyword in the signal
- module. Patch written by Charles-François Natali.
-
-- Issue #1746656: Added the if_nameindex, if_indextoname, if_nametoindex
- methods to the socket module.
-
-- Issue #12044: Fixed subprocess.Popen when used as a context manager to
- wait for the process to end when exiting the context to avoid unintentionally
- leaving zombie processes around.
-
-- Issue #1195: Fix input() if it is interrupted by CTRL+d and then CTRL+c,
- clear the end-of-file indicator after CTRL+d.
-
-- Issue #1856: Avoid crashes and lockups when daemon threads run while the
- interpreter is shutting down; instead, these threads are now killed when
- they try to take the GIL.
-
-- Issue #9756: When calling a method descriptor or a slot wrapper descriptor,
- the check of the object type doesn't read the __class__ attribute anymore.
- Fix a crash if a class override its __class__ attribute (e.g. a proxy of the
- str type). Patch written by Andreas Stührk.
-
-- Issue #10517: After fork(), reinitialize the TLS used by the PyGILState_*
- APIs, to avoid a crash with the pthread implementation in RHEL 5. Patch
- by Charles-François Natali.
-
-- Issue #10914: Initialize correctly the filesystem codec when creating a new
- subinterpreter to fix a bootstrap issue with codecs implemented in Python, as
- the ISO-8859-15 codec.
-
-- Issue #11918: OS/2 and VMS are no more supported because of the lack of
- maintainer.
-
-- Issue #6780: fix starts/endswith error message to mention that tuples are
- accepted too.
-
-- Issue #5057: fix a bug in the peepholer that led to non-portable pyc files
- between narrow and wide builds while optimizing BINARY_SUBSCR on non-BMP
- chars (e.g. "\U00012345"[0]).
-
-- Issue #11845: Fix typo in rangeobject.c that caused a crash in
- compute_slice_indices. Patch by Daniel Urban.
-
-- Issue #5673: Added a `timeout` keyword argument to subprocess.Popen.wait,
- subprocess.Popen.communicated, subprocess.call, subprocess.check_call, and
- subprocess.check_output. If the blocking operation takes more than `timeout`
- seconds, the `subprocess.TimeoutExpired` exception is raised.
-
-- Issue #11650: PyOS_StdioReadline() retries fgets() if it was interrupted
- (EINTR), for example if the program is stopped with CTRL+z on Mac OS X. Patch
- written by Charles-Francois Natali.
-
-- Issue #9319: Include the filename in "Non-UTF8 code ..." syntax error.
-
-- Issue #10785: Store the filename as Unicode in the Python parser.
-
-- Issue #11619: _PyImport_LoadDynamicModule() doesn't encode the path to bytes
- on Windows.
-
-- Issue #10998: Remove mentions of -Q, sys.flags.division_warning and
- Py_DivisionWarningFlag left over from Python 2.
-
-- Issue #11244: Remove an unnecessary peepholer check that was preventing
- negative zeros from being constant-folded properly.
-
-- Issue #11395: io.FileIO().write() clamps the data length to 32,767 bytes on
- Windows if the file is a TTY to workaround a Windows bug. The Windows console
- returns an error (12: not enough space error) on writing into stdout if
- stdout mode is binary and the length is greater than 66,000 bytes (or less,
- depending on heap usage).
-
-- Issue #11320: fix bogus memory management in Modules/getpath.c, leading to
- a possible crash when calling Py_SetPath().
-
-- Issue #11432: A bug was introduced in subprocess.Popen on posix systems with
- 3.2.0 where the stdout or stderr file descriptor being the same as the stdin
- file descriptor would raise an exception. webbrowser.open would fail. fixed.
-
-- Issue #9856: Change object.__format__ with a non-empty format string
- to be a DeprecationWarning. In 3.2 it was a PendingDeprecationWarning.
- In 3.4 it will be a TypeError.
-
-- Issue #11244: The peephole optimizer is now able to constant-fold
- arbitrarily complex expressions. This also fixes a 3.2 regression where
- operations involving negative numbers were not constant-folded.
-
-- Issue #11450: Don't truncate hg version info in Py_GetBuildInfo() when
- there are many tags (e.g. when using mq). Patch by Nadeem Vawda.
-
-- Issue #11335: Fixed a memory leak in list.sort when the key function
- throws an exception.
-
-- Issue #8923: When a string is encoded to UTF-8 in strict mode, the result is
- cached into the object. Examples: str.encode(), str.encode('utf-8'),
- PyUnicode_AsUTF8String() and PyUnicode_AsEncodedString(unicode, "utf-8",
- NULL).
-
-- Issue #10829: Refactor PyUnicode_FromFormat(), use the same function to parse
- the format string in the 3 steps, fix crashs on invalid format strings.
-
-- Issue #13007: whichdb should recognize gdbm 1.9 magic numbers.
-
-- Issue #11286: Raise a ValueError from calling PyMemoryView_FromBuffer with
- a buffer struct having a NULL data pointer.
-
-- Issue #11272: On Windows, input() strips '\r' (and not only '\n'), and
- sys.stdin uses universal newline (replace '\r\n' by '\n').
-
-- Issue #11828: startswith and endswith now accept None as slice index.
- Patch by Torsten Becker.
-
-- Issue #11168: Remove filename debug variable from PyEval_EvalFrameEx().
- It encoded the Unicode filename to UTF-8, but the encoding fails on
- undecodable filename (on surrogate characters) which raises an unexpected
- UnicodeEncodeError on recursion limit.
-
-- Issue #11187: Remove bootstrap code (use ASCII) of
- PyUnicode_AsEncodedString(), it was replaced by a better fallback (use the
- locale encoding) in PyUnicode_EncodeFSDefault().
-
-- Check for NULL result in PyType_FromSpec.
-
-- Issue #10516: New copy() and clear() methods for lists and bytearrays.
-
-- Issue #11386: bytearray.pop() now throws IndexError when the bytearray is
- empty, instead of OverflowError.
-
-- Issue #12380: The rjust, ljust and center methods of bytes and bytearray
- now accept a bytearray argument.
-
-Library
--------
-
-- Issue #14195: An issue that caused weakref.WeakSet instances to incorrectly
- return True for a WeakSet instance 'a' in both 'a < a' and 'a > a' has been
- fixed.
-
-- Issue #14166: Pickler objects now have an optional ``dispatch_table``
- attribute which allows to set custom per-pickler reduction functions.
- Patch by sbt.
-
-- Issue #14177: marshal.loads() now raises TypeError when given an unicode
- string. Patch by Guilherme Gonçalves.
-
-- Issue #13550: Remove the debug machinery from the threading module: remove
- verbose arguments from all threading classes and functions.
-
-- Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary,
- WeakValueDictionary) to return a better approximation when some objects
- are dead or dying. Moreover, the implementation is now O(1) rather than
- O(n).
-
-- Issue #11841: Fix comparison bug with 'rc' versions in packaging.version.
- Patch by Filip Gruszczyński.
-
-- Issue #6884: Fix long-standing bugs with MANIFEST.in parsing in distutils
- on Windows. Also fixed in packaging.
-
-- Issue #8033: sqlite3: Fix 64-bit integer handling in user functions
- on 32-bit architectures. Initial patch by Philippe Devalkeneer.
-
-- HTMLParser is now able to handle slashes in the start tag.
-
-- Issue #13641: Decoding functions in the base64 module now accept ASCII-only
- unicode strings. Patch by Catalin Iacob.
-
-- Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a
- new importlib.invalidate_caches() function.
-
-- Issue #14001: CVE-2012-0845: xmlrpc: Fix an endless loop in
- SimpleXMLRPCServer upon malformed POST request.
-
-- Issue #13961: Move importlib over to using os.replace() for atomic renaming.
-
-- Do away with ambiguous level values (as suggested by PEP 328) in
- importlib.__import__() by raising ValueError when level < 0.
-
-- Issue #2489: pty.spawn could consume 100% cpu when it encountered an EOF.
-
-- Issue #13014: Fix a possible reference leak in SSLSocket.getpeercert().
-
-- Issue #13777: Add PF_SYSTEM sockets on OS X.
- Patch by Michael Goderbauer.
-
-- Issue #13015: Fix a possible reference leak in defaultdict.__repr__.
- Patch by Suman Saha.
-
-- Issue #1326113: distutils' and packaging's build_ext commands option now
- correctly parses multiple values (separated by whitespace or commas) given
- to their --libraries option.
-
-- Issue #10287: nntplib now queries the server's CAPABILITIES first before
- sending MODE READER, and only sends it if not already in READER mode.
- Patch by Hynek Schlawack.
-
-- Issue #13993: HTMLParser is now able to handle broken end tags when
- strict=False.
-
-- Issue #13930: lib2to3 now supports writing converted output files to another
- directory tree as well as copying unchanged files and altering the file
- suffix.
-
-- Issue #9750: Fix sqlite3.Connection.iterdump on tables and fields
- with a name that is a keyword or contains quotes. Patch by Marko
- Kohtala.
-
-- Issue #10287: nntplib now queries the server's CAPABILITIES again after
- authenticating (since the result may change, according to RFC 4643).
- Patch by Hynek Schlawack.
-
-- Issue #13590: On OS X 10.7 and 10.6 with Xcode 4.2, building
- Distutils-based packages with C extension modules may fail because
- Apple has removed gcc-4.2, the version used to build python.org
- 64-bit/32-bit Pythons. If the user does not explicitly override
- the default C compiler by setting the CC environment variable,
- Distutils will now attempt to compile extension modules with clang
- if gcc-4.2 is required but not found. Also as a convenience, if
- the user does explicitly set CC, substitute its value as the default
- compiler in the Distutils LDSHARED configuration variable for OS X.
- (Note, the python.org 32-bit-only Pythons use gcc-4.0 and the 10.4u
- SDK, neither of which are available in Xcode 4. This change does not
- attempt to override settings to support their use with Xcode 4.)
-
-- Issue #13960: HTMLParser is now able to handle broken comments when
- strict=False.
-
-- When '' is a path (e.g. in sys.path), make sure __file__ uses the current
- working directory instead of '' in importlib.
-
-- Issue #13609: Add two functions to query the terminal size:
- os.get_terminal_size (low level) and shutil.get_terminal_size (high level).
- Patch by Zbigniew Jędrzejewski-Szmek.
-
-- Issue #13845: On Windows, time.time() now uses GetSystemTimeAsFileTime()
- instead of ftime() to have a resolution of 100 ns instead of 1 ms (the clock
- accuracy is between 0.5 ms and 15 ms).
-
-- Issue #13846: Add time.monotonic(), monotonic clock.
-
-- Issue #8184: multiprocessing: On Windows, don't set SO_REUSEADDR on
- Connection sockets, and set FILE_FLAG_FIRST_PIPE_INSTANCE on named pipes, to
- make sure two listeners can't bind to the same socket/pipe (or any existing
- socket/pipe).
-
-- Issue #10811: Fix recursive usage of cursors. Instead of crashing,
- raise a ProgrammingError now.
-
-- Issue #13734: Add os.fwalk(), a directory walking function yielding file
- descriptors.
-
-- Issue #2945: Make the distutils upload command aware of bdist_rpm products.
-
-- Issue #13712: pysetup create should not convert package_data to extra_files.
-
-- Issue #11805: package_data in setup.cfg should allow more than one value.
-
-- Issue #13676: Handle strings with embedded zeros correctly in sqlite3.
-
-- Issue #8828: Add new function os.replace(), for cross-platform renaming
- with overwriting.
-
-- Issue #13848: open() and the FileIO constructor now check for NUL
- characters in the file name. Patch by Hynek Schlawack.
-
-- Issue #13806: The size check in audioop decompression functions was too
- strict and could reject valid compressed data. Patch by Oleg Plakhotnyuk.
-
-- Issue #13812: When a multiprocessing Process child raises an exception,
- flush stderr after printing the exception traceback.
-
-- Issue #13885: CVE-2011-3389: the _ssl module would always disable the CBC
- IV attack countermeasure.
-
-- Issue #13847: time.localtime() and time.gmtime() now raise an OSError instead
- of ValueError on failure. time.ctime() and time.asctime() now raises an
- OSError if localtime() failed. time.clock() now raises a RuntimeError if the
- processor time used is not available or its value cannot be represented
-
-- Issue #13772: In os.symlink() under Windows, do not try to guess the link
- target's type (file or directory). The detection was buggy and made the
- call non-atomic (therefore prone to race conditions).
-
-- Issue #6631: Disallow relative file paths in urllib urlopen methods.
-
-- Issue #13722: Avoid silencing ImportErrors when initializing the codecs
- registry.
-
-- Issue #13781: Fix GzipFile bug that caused an exception to be raised when
- opening for writing using a fileobj returned by os.fdopen().
-
-- Issue #13803: Under Solaris, distutils doesn't include bitness
- in the directory name.
-
-- Issue #10278: Add time.wallclock() function, monotonic clock.
-
-- Issue #13809: Fix regression where bz2 module wouldn't work when threads are
- disabled. Original patch by Amaury Forgeot d'Arc.
-
-- Issue #13589: Fix some serialization primitives in the aifc module.
- Patch by Oleg Plakhotnyuk.
-
-- Issue #13642: Unquote before b64encoding user:password during Basic
- Authentication. Patch contributed by Joonas Kuorilehto.
-
-- Issue #12364: Fix a hang in concurrent.futures.ProcessPoolExecutor.
- The hang would occur when retrieving the result of a scheduled future after
- the executor had been shut down.
-
-- Issue #13502: threading: Fix a race condition in Event.wait() that made it
- return False when the event was set and cleared right after.
-
-- Issue #9993: When the source and destination are on different filesystems,
- and the source is a symlink, shutil.move() now recreates a symlink on the
- destination instead of copying the file contents. Patch by Jonathan Niehof
- and Hynek Schlawack.
-
-- Issue #12926: Fix a bug in tarfile's link extraction.
-
-- Issue #13696: Fix the 302 Relative URL Redirection problem.
-
-- Issue #13636: Weak ciphers are now disabled by default in the ssl module
- (except when SSLv2 is explicitly asked for).
-
-- Issue #12715: Add an optional symlinks argument to shutil functions
- (copyfile, copymode, copystat, copy, copy2). When that parameter is
- true, symlinks aren't dereferenced and the operation instead acts on the
- symlink itself (or creates one, if relevant). Patch by Hynek Schlawack.
-
-- Add a flags parameter to select.epoll.
-
-- Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
- SSLContext.load_dh_params() method and the ssl.OP_SINGLE_DH_USE option.
-
-- Issue #11006: Don't issue low level warning in subprocess when pipe2() fails.
-
-- Issue #13620: Support for Chrome browser in webbrowser. Patch contributed
- by Arnaud Calmettes.
-
-- Issue #11829: Fix code execution holes in inspect.getattr_static for
- metaclasses with metaclasses. Patch by Andreas Stührk.
-
-- Issue #12708: Add starmap() and starmap_async() methods (similar to
- itertools.starmap()) to multiprocessing.Pool. Patch by Hynek Schlawack.
-
-- Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
-
-- Issue #13637: "a2b" functions in the binascii module now accept ASCII-only
- unicode strings.
-
-- Issue #13634: Add support for querying and disabling SSL compression.
-
-- Issue #13627: Add support for SSL Elliptic Curve-based Diffie-Hellman
- key exchange, through the SSLContext.set_ecdh_curve() method and the
- ssl.OP_SINGLE_ECDH_USE option.
-
-- Issue #13635: Add ssl.OP_CIPHER_SERVER_PREFERENCE, so that SSL servers
- choose the cipher based on their own preferences, rather than on the
- client's.
-
-- Issue #11813: Fix inspect.getattr_static for modules. Patch by Andreas
- Stührk.
-
-- Issue #7502: Fix equality comparison for DocTestCase instances. Patch by
- Cédric Krier.
-
-- Issue #11870: threading: Properly reinitialize threads internal locks and
- condition variables to avoid deadlocks in child processes.
-
-- Issue #8035: urllib: Fix a bug where the client could remain stuck after a
- redirection or an error.
-
-- Issue #13560: os.strerror() now uses the current locale encoding instead of
- UTF-8.
-
-- Issue #8373: The filesystem path of AF_UNIX sockets now uses the filesystem
- encoding and the surrogateescape error handler, rather than UTF-8. Patch
- by David Watson.
-
-- Issue #10350: Read and save errno before calling a function which might
- overwrite it. Original patch by Hallvard B Furuseth.
-
-- Issue #11610: Introduce a more general way to declare abstract properties.
-
-- Issue #13591: A bug in importlib has been fixed that caused import_module
- to load a module twice.
-
-- Issue #13449 sched.scheduler.run() method has a new "blocking" parameter which
- when set to False makes run() execute the scheduled events due to expire
- soonest (if any) and then return. Patch by Giampaolo Rodolà.
-
-- Issue #8684 sched.scheduler class can be safely used in multi-threaded
- environments. Patch by Josiah Carlson and Giampaolo Rodolà.
-
-- Alias resource.error to OSError ala PEP 3151.
-
-- Issue #5689: Add support for lzma compression to the tarfile module.
-
-- Issue #13248: Turn 3.2's PendingDeprecationWarning into 3.3's
- DeprecationWarning. It covers 'cgi.escape', 'importlib.abc.PyLoader',
- 'importlib.abc.PyPycLoader', 'nntplib.NNTP.xgtitle', 'nntplib.NNTP.xpath',
- and private attributes of 'smtpd.SMTPChannel'.
-
-- Issue #5905, Issue #13560: time.strftime() is now using the current locale
- encoding, instead of UTF-8, if the wcsftime() function is not available.
-
-- Issue #13464: Add a readinto() method to http.client.HTTPResponse. Patch
- by Jon Kuhn.
-
-- tarfile.py: Correctly detect bzip2 compressed streams with blocksizes
- other than 900k.
-
-- Issue #13439: Fix many errors in turtle docstrings.
-
-- Issue #6715: Add a module 'lzma' for compression using the LZMA algorithm.
- Thanks to Per Øyvind Karlsen for the initial implementation.
-
-- Issue #13487: Make inspect.getmodule robust against changes done to
- sys.modules while it is iterating over it.
-
-- Issue #12618: Fix a bug that prevented py_compile from creating byte
- compiled files in the current directory. Initial patch by Sjoerd de Vries.
-
-- Issue #13444: When stdout has been closed explicitly, we should not attempt
- to flush it at shutdown and print an error.
-
-- Issue #12567: The curses module uses Unicode functions for Unicode arguments
- when it is linked to the ncurses library. It encodes also Unicode strings to
- the locale encoding instead of UTF-8.
-
-- Issue #12856: Ensure child processes do not inherit the parent's random
- seed for filename generation in the tempfile module. Patch by Brian
- Harring.
-
-- Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size
- parameter, as other file-like objects. Patch by Ryan Kelly.
-
-- Issue #13458: Fix a memory leak in the ssl module when decoding a
- certificate with a subjectAltName. Patch by Robert Xiao.
-
-- Issue #13415: os.unsetenv() doesn't ignore errors anymore.
-
-- Issue #13245: sched.scheduler class constructor's timefunc and
- delayfunct parameters are now optional.
- scheduler.enter and scheduler.enterabs methods gained a new kwargs parameter.
- Patch contributed by Chris Clark.
-
-- Issue #12328: Under Windows, refactor handling of Ctrl-C events and
- make _multiprocessing.win32.WaitForMultipleObjects interruptible when
- the wait_flag parameter is false. Patch by sbt.
-
-- Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
- raised when the wrapped raw file is non-blocking and the write would block.
- Previous code assumed that the raw write() would raise BlockingIOError, but
- RawIOBase.write() is defined to returned None when the call would block.
- Patch by sbt.
-
-- Issue #13358: HTMLParser now calls handle_data only once for each CDATA.
-
-- Issue #4147: minidom's toprettyxml no longer adds whitespace around a text
- node when it is the only child of an element. Initial patch by Dan
- Kenigsberg.
-
-- Issue #13374: The Windows bytes API has been deprecated in the os module. Use
- Unicode filenames instead of bytes filenames to not depend on the ANSI code
- page anymore and to support any filename.
-
-- Issue #13297: Use bytes type to send and receive binary data through XMLRPC.
-
-- Issue #6397: Support "/dev/poll" polling objects in select module,
- under Solaris & derivatives.
-
-- Issues #1745761, #755670, #13357, #12629, #1200313: HTMLParser now correctly
- handles non-valid attributes, including adjacent and unquoted attributes.
-
-- Issue #13193: Fix distutils.filelist.FileList and packaging.manifest.Manifest
- under Windows.
-
-- Issue #13384: Remove unnecessary __future__ import in Lib/random.py
-
-- Issue #13149: Speed up append-only StringIO objects.
-
-- Issue #13373: multiprocessing.Queue.get() could sometimes block indefinitely
- when called with a timeout. Patch by Arnaud Ysmal.
-
-- Issue #13254: Fix Maildir initialization so that maildir contents
- are read correctly.
-
-- Issue #3067: locale.setlocale() now raises TypeError if the second
- argument is an invalid iterable. Its documentation and docstring
- were also updated. Initial patch by Jyrki Pulliainen.
-
-- Issue #13140: Fix the daemon_threads attribute of ThreadingMixIn.
-
-- Issue #13339: Fix compile error in posixmodule.c due to missing semicolon.
- Thanks to Robert Xiao.
-
-- Byte compilation in packaging is now isolated from the calling Python -B or
- -O options, instead of being disallowed under -B or buggy under -O.
-
-- Issue #10570: curses.putp() and curses.tparm() are now expecting a byte
- string, instead of a Unicode string.
-
-- Issue #13295: http.server now produces valid HTML 4.01 strict.
-
-- Issue #2892: preserve iterparse events in case of SyntaxError.
-
-- Issue #13287: urllib.request and urllib.error now contains an __all__
- attribute to expose only relevant classes and functions. Patch by Florent
- Xicluna.
-
-- Issue #670664: Fix HTMLParser to correctly handle the content of
- ``<script>...</script>`` and ``<style>...</style>``.
-
-- Issue #10817: Fix urlretrieve function to raise ContentTooShortError even
- when reporthook is None. Patch by Jyrki Pulliainen.
-
-- Fix the xmlrpc.client user agent to return something similar to
- urllib.request user agent: "Python-xmlrpc/3.3".
-
-- Issue #13293: Better error message when trying to marshal bytes using
- xmlrpc.client.
-
-- Issue #13291: NameError in xmlrpc package.
-
-- Issue #13258: Use callable() built-in in the standard library.
-
-- Issue #13273: fix a bug that prevented HTMLParser to properly detect some
- tags when strict=False.
-
-- Issue #11183: Add finer-grained exceptions to the ssl module, so that
- you don't have to inspect the exception's attributes in the common case.
-
-- Issue #13216: Add cp65001 codec, the Windows UTF-8 (CP_UTF8).
-
-- Issue #13226: Add RTLD_xxx constants to the os module. These constants can be
- used with sys.setdlopenflags().
-
-- Issue #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
- the time module. time.clock_gettime(time.CLOCK_MONOTONIC) provides a
- monotonic clock
-
-- Issue #10332: multiprocessing: fix a race condition when a Pool is closed
- before all tasks have completed.
-
-- Issue #13255: wrong docstrings in array module.
-
-- Issue #8540: Remove deprecated Context._clamp attribute in Decimal module.
-
-- Issue #13235: Added DeprecationWarning to logging.warn() method and function.
-
-- Issue #9168: now smtpd is able to bind privileged port.
-
-- Issue #12529: fix cgi.parse_header issue on strings with double-quotes and
- semicolons together. Patch by Ben Darnell and Petri Lehtinen.
-
-- Issue #13227: functools.lru_cache() now has a option to distinguish
- calls with different argument types.
-
-- Issue #6090: zipfile raises a ValueError when a document with a timestamp
- earlier than 1980 is provided. Patch contributed by Petri Lehtinen.
-
-- Issue #13150: sysconfig no longer parses the Makefile and config.h files
- when imported, instead doing it at build time. This makes importing
- sysconfig faster and reduces Python startup time by 20%.
-
-- Issue #12448: smtplib now flushes stdout while running ``python -m smtplib``
- in order to display the prompt correctly.
-
-- Issue #12454: The mailbox module is now using ASCII, instead of the locale
- encoding, to read and write .mh_sequences files.
-
-- Issue #13194: zlib.compressobj().copy() and zlib.decompressobj().copy() are
- now available on Windows.
-
-- Issue #1673007: urllib.request now supports HEAD request via new method argument.
- Patch contributions by David Stanek, Patrick Westerhoff and Ezio Melotti.
-
-- Issue #12386: packaging does not fail anymore when writing the RESOURCES
- file.
-
-- Issue #13158: Fix decoding and encoding of GNU tar specific base-256 number
- fields in tarfile.
-
-- Issue #13025: mimetypes is now reading MIME types using the UTF-8 encoding,
- instead of the locale encoding.
-
-- Issue #10653: On Windows, use strftime() instead of wcsftime() because
- wcsftime() doesn't format time zone correctly.
-
-- Issue #13150: The tokenize module doesn't compile large regular expressions
- at startup anymore.
-
-- Issue #11171: Fix distutils.sysconfig.get_makefile_filename when Python was
- configured with different prefix and exec-prefix.
-
-- Issue #11254: Teach distutils and packaging to compile .pyc and .pyo files in
- PEP 3147-compliant __pycache__ directories.
-
-- Issue #7367: Fix pkgutil.walk_paths to skip directories whose
- contents cannot be read.
-
-- Issue #3163: The struct module gets new format characters 'n' and 'N'
- supporting C integer types ``ssize_t`` and ``size_t``, respectively.
-
-- Issue #13099: Fix sqlite3.Cursor.lastrowid under a Turkish locale.
- Reported and diagnosed by Thomas Kluyver.
-
-- Issue #13087: BufferedReader.seek() now always raises UnsupportedOperation
- if the underlying raw stream is unseekable, even if the seek could be
- satisfied using the internal buffer. Patch by John O'Connor.
-
-- Issue #7689: Allow pickling of dynamically created classes when their
- metaclass is registered with copyreg. Patch by Nicolas M. Thiéry and Craig
- Citro.
-
-- Issue #13034: When decoding some SSL certificates, the subjectAltName
- extension could be unreported.
-
-- Issue #12306: Expose the runtime version of the zlib C library as a constant,
- ZLIB_RUNTIME_VERSION, in the zlib module. Patch by Torsten Landschoff.
-
-- Issue #12959: Add collections.ChainMap to collections.__all__.
-
-- Issue #8933: distutils' PKG-INFO files and packaging's METADATA files will
- now correctly report Metadata-Version: 1.1 instead of 1.0 if a Classifier or
- Download-URL field is present.
-
-- Issue #12567: Add curses.unget_wch() function. Push a character so the next
- get_wch() will return it.
-
-- Issue #9561: distutils and packaging now writes egg-info files using UTF-8,
- instead of the locale encoding.
-
-- Issue #8286: The distutils command sdist will print a warning message instead
- of crashing when an invalid path is given in the manifest template.
-
-- Issue #12841: tarfile unnecessarily checked the existence of numerical user
- and group ids on extraction. If one of them did not exist the respective id
- of the current user (i.e. root) was used for the file and ownership
- information was lost.
-
-- Issue #12888: Fix a bug in HTMLParser.unescape that prevented it to escape
- more than 128 entities. Patch by Peter Otten.
-
-- Issue #12878: Expose a __dict__ attribute on io.IOBase and its subclasses.
-
-- Issue #12494: On error, call(), check_call(), check_output() and
- getstatusoutput() functions of the subprocess module now kill the process,
- read its status (to avoid zombis) and close pipes.
-
-- Issue #12720: Expose low-level Linux extended file attribute functions in os.
-
-- Issue #10946: The distutils commands bdist_dumb, bdist_wininst and bdist_msi
- now respect a --skip-build option given to bdist. The packaging commands
- were fixed too.
-
-- Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
- the C pickle implementation.
-
-- Issue #11564: Avoid crashes when trying to pickle huge objects or containers
- (more than 2**31 items). Instead, in most cases, an OverflowError is raised.
-
-- Issue #12287: Fix a stack corruption in ossaudiodev module when the FD is
- greater than FD_SETSIZE.
-
-- Issue #12839: Fix crash in zlib module due to version mismatch.
- Fix by Richard M. Tew.
-
-- Issue #9923: The mailcap module now correctly uses the platform path
- separator for the MAILCAP environment variable on non-POSIX platforms.
-
-- Issue #12835: Follow up to #6560 that unconditionally prevents use of the
- unencrypted sendmsg/recvmsg APIs on SSL wrapped sockets. Patch by David
- Watson.
-
-- Issue #12803: SSLContext.load_cert_chain() now accepts a password argument
- to be used if the private key is encrypted. Patch by Adam Simpkins.
-
-- Issue #11657: Fix sending file descriptors over 255 over a multiprocessing
- Pipe.
-
-- Issue #12811: tabnanny.check() now promptly closes checked files. Patch by
- Anthony Briggs.
-
-- Issue #6560: The sendmsg/recvmsg API is now exposed by the socket module
- when provided by the underlying platform, supporting processing of
- ancillary data in pure Python code. Patch by David Watson and Heiko Wundram.
-
-- Issue #12326: On Linux, sys.platform doesn't contain the major version
- anymore. It is now always 'linux', instead of 'linux2' or 'linux3' depending
- on the Linux version used to build Python.
-
-- Issue #12213: Fix a buffering bug with interleaved reads and writes that
- could appear on BufferedRandom streams.
-
-- Issue #12778: Reduce memory consumption when JSON-encoding a large
- container of many small objects.
-
-- Issue #12650: Fix a race condition where a subprocess.Popen could leak
- resources (FD/zombie) when killed at the wrong time.
-
-- Issue #12744: Fix inefficient representation of integers between 2**31 and
- 2**63 on systems with a 64-bit C "long".
-
-- Issue #12646: Add an 'eof' attribute to zlib.Decompress, to make it easier to
- detect truncated input streams.
-
-- Issue #11513: Fix exception handling ``tarfile.TarFile.gzopen()`` when
- the file cannot be opened.
-
-- Issue #12687: Fix a possible buffering bug when unpickling text mode
- (protocol 0, mostly) pickles.
-
-- Issue #10087: Fix the html output format of the calendar module.
-
-- Issue #13121: add support for inplace math operators to collections.Counter.
-
-- Add support for unary plus and unary minus to collections.Counter.
-
-- Issue #12683: urlparse updated to include svn as schemes that uses relative
- paths. (svn from 1.5 onwards support relative path).
-
-- Issue #12655: Expose functions from sched.h in the os module: sched_yield(),
- sched_setscheduler(), sched_getscheduler(), sched_setparam(),
- sched_get_min_priority(), sched_get_max_priority(), sched_rr_get_interval(),
- sched_getaffinity(), sched_setaffinity().
-
-- Add ThreadError to threading.__all__.
-
-- Issues #11104, #8688: Fix the behavior of distutils' sdist command with
- manually-maintained MANIFEST files.
-
-- Issue #11281: smtplib.STMP gets source_address parameter, which adds the
- ability to bind to specific source address on a machine with multiple
- interfaces. Patch by Paulo Scardine.
-
-- Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow
- symlinks: fix it. Patch by Petri Lehtinen.
-
-- Issue #8887: "pydoc somebuiltin.somemethod" (or help('somebuiltin.somemethod')
- in Python code) now finds the doc of the method.
-
-- Issue #10968: Remove indirection in threading. The public names (Event,
- Condition, etc.) used to be factory functions returning instances of hidden
- classes (_Event, _Condition, etc.), because (if Guido recalls correctly) this
- code pre-dates the ability to subclass extension types. It is now possible
- to inherit from these classes, without having to import the private
- underscored names like multiprocessing did.
-
-- Issue #9723: Add shlex.quote functions, to escape filenames and command
- lines.
-
-- Issue #12603: Fix pydoc.synopsis() on files with non-negative st_mtime.
-
-- Issue #12514: Use try/finally to assure the timeit module restores garbage
- collections when it is done.
-
-- Issue #12607: In subprocess, fix issue where if stdin, stdout or stderr is
- given as a low fd, it gets overwritten.
-
-- Issue #12576: Fix urlopen behavior on sites which do not send (or obfuscates)
- ``Connection: close`` header.
-
-- Issue #12560: Build libpython.so on OpenBSD. Patch by Stefan Sperling.
-
-- Issue #1813: Fix codec lookup under Turkish locales.
-
-- Issue #12591: Improve support of "universal newlines" in the subprocess
- module: the piped streams can now be properly read from or written to.
-
-- Issue #12591: Allow io.TextIOWrapper to work with raw IO objects (without
- a read1() method), and add a *write_through* parameter to mandate
- unbuffered writes.
-
-- Issue #10883: Fix socket leaks in urllib.request when using FTP.
-
-- Issue #12592: Make Python build on OpenBSD 5 (and future major releases).
-
-- Issue #12372: POSIX semaphores are broken on AIX: don't use them.
-
-- Issue #12551: Provide a get_channel_binding() method on SSL sockets so as
- to get channel binding data for the current SSL session (only the
- "tls-unique" channel binding is implemented). This allows the implementation
- of certain authentication mechanisms such as SCRAM-SHA-1-PLUS. Patch by
- Jacek Konieczny.
-
-- Issue #665194: email.utils now has format_datetime and parsedate_to_datetime
- functions, allowing for round tripping of RFC2822 format dates.
-
-- Issue #12571: Add a plat-linux3 directory mirroring the plat-linux2
- directory, so that "import DLFCN" and other similar imports work on
- Linux 3.0.
-
-- Issue #7484: smtplib no longer puts <> around addresses in VRFY and EXPN
- commands; they aren't required and in fact postfix doesn't support that form.
-
-- Issue #12273: Remove ast.__version__. AST changes can be accounted for by
- checking sys.version_info or sys._mercurial.
-
-- Silence spurious "broken pipe" tracebacks when shutting down a
- ProcessPoolExecutor.
-
-- Fix potential resource leaks in concurrent.futures.ProcessPoolExecutor
- by joining all queues and processes when shutdown() is called.
-
-- Issue #11603: Fix a crash when __str__ is rebound as __repr__. Patch by
- Andreas Stührk.
-
-- Issue #11321: Fix a crash with multiple imports of the _pickle module when
- embedding Python. Patch by Andreas Stührk.
-
-- Issue #6755: Add get_wch() method to curses.window class. Patch by Iñigo
- Serna.
-
-- Add cgi.closelog() function to close the log file.
-
-- Issue #12502: asyncore: fix polling loop with AF_UNIX sockets.
-
-- Issue #4376: ctypes now supports nested structures in a endian different than
- the parent structure. Patch by Vlad Riscutia.
-
-- Raise ValueError when attempting to set the _CHUNK_SIZE attribute of a
- TextIOWrapper to a huge value, not TypeError.
-
-- Issue #12504: Close file handles in a timely manner in packaging.database.
- This fixes a bug with the remove (uninstall) feature on Windows.
-
-- Issues #12169 and #10510: Factor out code used by various packaging commands
- to make HTTP POST requests, and make sure it uses CRLF.
-
-- Issue #12016: Multibyte CJK decoders now resynchronize faster. They only
- ignore the first byte of an invalid byte sequence. For example,
- b'\xff\n'.decode('gb2312', 'replace') gives '\ufffd\n' instead of '\ufffd'.
-
-- Issue #12459: time.sleep() now raises a ValueError if the sleep length is
- negative, instead of an infinite sleep on Windows or raising an IOError on
- Linux for example, to have the same behaviour on all platforms.
-
-- Issue #12451: pydoc: html_getfile() now uses tokenize.open() to support
- Python scripts using a encoding different than UTF-8 (read the coding cookie
- of the script).
-
-- Issue #12493: subprocess: Popen.communicate() now also handles EINTR errors
- if the process has only one pipe.
-
-- Issue #12467: warnings: fix a race condition if a warning is emitted at
- shutdown, if globals()['__file__'] is None.
-
-- Issue #12451: pydoc: importfile() now opens the Python script in binary mode,
- instead of text mode using the locale encoding, to avoid encoding issues.
-
-- Issue #12451: runpy: run_path() now opens the Python script in binary mode,
- instead of text mode using the locale encoding, to support other encodings
- than UTF-8 (scripts using the coding cookie).
-
-- Issue #12451: xml.dom.pulldom: parse() now opens files in binary mode instead
- of the text mode (using the locale encoding) to avoid encoding issues.
-
-- Issue #12147: Adjust the new-in-3.2 smtplib.send_message method for better
- conformance to the RFCs: correctly handle Sender and Resent- headers.
-
-- Issue #12352: Fix a deadlock in multiprocessing.Heap when a block is freed by
- the garbage collector while the Heap lock is held.
-
-- Issue #12462: time.sleep() now immediately calls the (Python) signal handler
- if it is interrupted by a signal, instead of having to wait until the next
- instruction.
-
-- Issue #12442: new shutil.disk_usage function, providing total, used and free
- disk space statistics.
-
-- Issue #12451: The XInclude default loader of xml.etree now decodes files from
- UTF-8 instead of the locale encoding if the encoding is not specified. It now
- also opens XML files for the parser in binary mode instead of the text mode
- to avoid encoding issues.
-
-- Issue #12451: doctest.debug_script() doesn't create a temporary file
- anymore to avoid encoding issues.
-
-- Issue #12451: pydoc.synopsis() now reads the encoding cookie if available,
- to read the Python script from the right encoding.
-
-- Issue #12451: distutils now opens the setup script in binary mode to read the
- encoding cookie, instead of opening it in UTF-8.
-
-- Issue #9516: On Mac OS X, change Distutils to no longer globally attempt to
- check or set the MACOSX_DEPLOYMENT_TARGET environment variable for the
- interpreter process. This could cause failures in non-Distutils subprocesses
- and was unreliable since tests or user programs could modify the interpreter
- environment after Distutils set it. Instead, have Distutils set the
- deployment target only in the environment of each build subprocess. It is
- still possible to globally override the default by setting
- MACOSX_DEPLOYMENT_TARGET before launching the interpreter; its value must be
- greater or equal to the default value, the value with which the interpreter
- was built. Also, implement the same handling in packaging.
-
-- Issue #12422: In the copy module, don't store objects that are their own copy
- in the memo dict.
-
-- Issue #12303: Add sigwaitinfo() and sigtimedwait() to the signal module.
-
-- Issue #12404: Remove C89 incompatible code from mmap module. Patch by Akira
- Kitada.
-
-- Issue #1874: email now detects and reports as a defect the presence of
- any CTE other than 7bit, 8bit, or binary on a multipart.
-
-- Issue #12383: Fix subprocess module with env={}: don't copy the environment
- variables, start with an empty environment.
-
-- Issue #11637: Fix support for importing packaging setup hooks from the
- project directory.
-
-- Issue #6771: Moved the curses.wrapper function from the single-function
- wrapper module into __init__, eliminating the module. Since __init__ was
- already importing the function to curses.wrapper, there is no API change.
-
-- Issue #11584: email.header.decode_header no longer fails if the header
- passed to it is a Header object, and Header/make_header no longer fail
- if given binary unknown-8bit input.
-
-- Issue #11700: mailbox proxy object close methods can now be called multiple
- times without error.
-
-- Issue #11767: Correct file descriptor leak in mailbox's __getitem__ method.
-
-- Issue #12133: AbstractHTTPHandler.do_open() of urllib.request closes the HTTP
- connection if its getresponse() method fails with a socket error. Patch
- written by Ezio Melotti.
-
-- Issue #12240: Allow multiple setup hooks in packaging's setup.cfg files.
- Original patch by Erik Bray.
-
-- Issue #9284: Allow inspect.findsource() to find the source of doctest
- functions.
-
-- Issue #11595: Fix assorted bugs in packaging.util.cfg_to_args, a
- compatibility helper for the distutils-packaging transition. Original patch
- by Erik Bray.
-
-- Issue #12287: In ossaudiodev, check that the device isn't closed in several
- methods.
-
-- Issue #12009: Fixed regression in netrc file comment handling.
-
-- Issue #12246: Warn and fail when trying to install a third-party project from
- an uninstalled Python (built in a source checkout). Original patch by
- Tshepang Lekhonkhobe.
-
-- Issue #10694: zipfile now ignores garbage at the end of a zipfile.
-
-- Issue #12283: Fixed regression in smtplib quoting of leading dots in DATA.
-
-- Issue #10424: Argparse now includes the names of the missing required
- arguments in the missing arguments error message.
-
-- Issue #12168: SysLogHandler now allows NUL termination to be controlled using
- a new 'append_nul' attribute on the handler.
-
-- Issue #11583: Speed up os.path.isdir on Windows by using GetFileAttributes
- instead of os.stat.
-
-- Issue #12021: Make mmap's read() method argument optional. Patch by Petri
- Lehtinen.
-
-- Issue #9205: concurrent.futures.ProcessPoolExecutor now detects killed
- children and raises BrokenProcessPool in such a situation. Previously it
- would reliably freeze/deadlock.
-
-- Issue #12040: Expose a new attribute ``sentinel`` on instances of
- ``multiprocessing.Process``. Also, fix Process.join() to not use polling
- anymore, when given a timeout.
-
-- Issue #11893: Remove obsolete internal wrapper class ``SSLFakeFile`` in the
- smtplib module. Patch by Catalin Iacob.
-
-- Issue #12080: Fix a Decimal.power() case that took an unreasonably long time
- to compute.
-
-- Issue #12221: Remove __version__ attributes from pyexpat, pickle, tarfile,
- pydoc, tkinter, and xml.parsers.expat. This were useless version constants
- left over from the Mercurial transition
-
-- Named tuples now work correctly with vars().
-
-- Issue #12085: Fix an attribute error in subprocess.Popen destructor if the
- constructor has failed, e.g. because of an undeclared keyword argument. Patch
- written by Oleg Oshmyan.
-
-- Issue #12028: Make threading._get_ident() public, rename it to
- threading.get_ident() and document it. This function was already used using
- _thread.get_ident().
-
-- Issue #12171: IncrementalEncoder.reset() of CJK codecs (multibytecodec) calls
- encreset() instead of decreset().
-
-- Issue #12218: Removed wsgiref.egg-info.
-
-- Issue #12196: Add pipe2() to the os module.
-
-- Issue #985064: Make plistlib more resilient to faulty input plists.
- Patch by Mher Movsisyan.
-
-- Issue #1625: BZ2File and bz2.decompress() now support multi-stream files.
- Initial patch by Nir Aides.
-
-- Issue #12175: BufferedReader.read(-1) now calls raw.readall() if available.
-
-- Issue #12175: FileIO.readall() now only reads the file position and size
- once.
-
-- Issue #12175: RawIOBase.readall() now returns None if read() returns None.
-
-- Issue #12175: FileIO.readall() now raises a ValueError instead of an IOError
- if the file is closed.
-
-- Issue #11109: New service_action method for BaseServer, used by ForkingMixin
- class for cleanup. Initial Patch by Justin Warkentin.
-
-- Issue #12045: Avoid duplicate execution of command in
- ctypes.util._get_soname(). Patch by Sijin Joseph.
-
-- Issue #10818: Remove the Tk GUI and the serve() function of the pydoc module,
- pydoc -g has been deprecated in Python 3.2 and it has a new enhanced web
- server.
-
-- Issue #1441530: In imaplib, read the data in one chunk to speed up large
- reads and simplify code.
-
-- Issue #12070: Fix the Makefile parser of the sysconfig module to handle
- correctly references to "bogus variable" (e.g. "prefix=$/opt/python").
-
-- Issue #12100: Don't reset incremental encoders of CJK codecs at each call to
- their encode() method anymore, but continue to call the reset() method if the
- final argument is True.
-
-- Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
- module.
-
-- Issue #6501: os.device_encoding() returns None on Windows if the application
- has no console.
-
-- Issue #12105: Add O_CLOEXEC to the os module.
-
-- Issue #12079: Decimal('Infinity').fma(Decimal('0'), (3.91224318126786e+19+0j))
- now raises TypeError (reflecting the invalid type of the 3rd argument) rather
- than Decimal.InvalidOperation.
-
-- Issue #12124: zipimport doesn't keep a reference to zlib.decompress() anymore
- to be able to unload the module.
-
-- Add the packaging module, an improved fork of distutils (also known as
- distutils2).
-
-- Issue #12065: connect_ex() on an SSL socket now returns the original errno
- when the socket's timeout expires (it used to return None).
-
-- Issue #8809: The SMTP_SSL constructor and SMTP.starttls() now support
- passing a ``context`` argument pointing to an ssl.SSLContext instance.
- Patch by Kasun Herath.
-
-- Issue #9516: Issue #9516: avoid errors in sysconfig when MACOSX_DEPLOYMENT_TARGET
- is set in shell.
-
-- Issue #8650: Make zlib module 64-bit clean. compress(), decompress() and
- their incremental counterparts now raise OverflowError if given an input
- larger than 4GB, instead of silently truncating the input and returning
- an incorrect result.
-
-- Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
- attribute when called without a max_length argument.
-
-- Issue #12062: Fix a flushing bug when doing a certain type of I/O sequence
- on a file opened in read+write mode (namely: reading, seeking a bit forward,
- writing, then seeking before the previous write but still within buffered
- data, and writing again).
-
-- Issue #9971: Write an optimized implementation of BufferedReader.readinto().
- Patch by John O'Connor.
-
-- Issue #11799: urllib.request Authentication Handlers will raise a ValueError
- when presented with an unsupported Authentication Scheme. Patch contributed
- by Yuval Greenfield.
-
-- Issue #10419, #6011: build_scripts command of distutils handles correctly
- non-ASCII path (path to the Python executable). Open and write the script in
- binary mode, but ensure that the shebang is decodable from UTF-8 and from the
- encoding of the script.
-
-- Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
- order to accept exactly one connection. Patch by Daniel Evers.
-
-- Issue #12011: signal.signal() and signal.siginterrupt() raise an OSError,
- instead of a RuntimeError: OSError has an errno attribute.
-
-- Issue #3709: add a flush_headers method to BaseHTTPRequestHandler, which
- manages the sending of headers to output stream and flushing the internal
- headers buffer. Patch contribution by Andrew Schaaf
-
-- Issue #11743: Rewrite multiprocessing connection classes in pure Python.
-
-- Issue #11164: Stop trying to use _xmlplus in the xml module.
-
-- Issue #11888: Add log2 function to math module. Patch written by Mark
- Dickinson.
-
-- Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional.
-
-- Issue #8407: The signal handler writes the signal number as a single byte
- instead of a nul byte into the wakeup file descriptor. So it is possible to
- wait more than one signal and know which signals were raised.
-
-- Issue #8407: Add pthread_kill(), sigpending() and sigwait() functions to the
- signal module.
-
-- Issue #11927: SMTP_SSL now uses port 465 by default as documented. Patch
- by Kasun Herath.
-
-- Issue #12002: ftplib's abort() method raises TypeError.
-
-- Issue #11916: Add a number of MacOSX specific definitions to the errno module.
- Patch by Pierre Carrier.
-
-- Issue #11999: fixed sporadic sync failure mailbox.Maildir due to its trying to
- detect mtime changes by comparing to the system clock instead of to the
- previous value of the mtime.
-
-- Issue #11072: added MLSD command (RFC-3659) support to ftplib.
-
-- Issue #8808: The IMAP4_SSL constructor now allows passing an SSLContext
- parameter to control parameters of the secure channel. Patch by Sijin
- Joseph.
-
-- ntpath.samefile failed to notice that "a.txt" and "A.TXT" refer to the same
- file on Windows XP. As noticed in issue #10684.
-
-- Issue #12000: When a SSL certificate has a subjectAltName without any
- dNSName entry, ssl.match_hostname() should use the subject's commonName.
- Patch by Nicolas Bareil.
-
-- Issue #10775: assertRaises, assertRaisesRegex, assertWarns, and
- assertWarnsRegex now accept a keyword argument 'msg' when used as context
- managers. Initial patch by Winston Ewert.
-
-- Issue #10684: shutil.move used to delete a folder on case insensitive
- filesystems when the source and destination name where the same except
- for the case.
-
-- Issue #11647: objects created using contextlib.contextmanager now support
- more than one call to the function when used as a decorator. Initial patch
- by Ysj Ray.
-
-- Issue #11930: Removed deprecated time.accept2dyear variable.
- Removed year >= 1000 restriction from datetime.strftime.
-
-- logging: don't define QueueListener if Python has no thread support.
-
-- functools.cmp_to_key() now works with collections.Hashable().
-
-- Issue #11277: mmap.mmap() calls fcntl(fd, F_FULLFSYNC) on Mac OS X to get
- around a mmap bug with sparse files. Patch written by Steffen Daode Nurpmeso.
-
-- Issue #8407: Add signal.pthread_sigmask() function to fetch and/or change the
- signal mask of the calling thread.
-
-- Issue #11858: configparser.ExtendedInterpolation expected lower-case section
- names.
-
-- Issue #11324: ConfigParser(interpolation=None) now works correctly.
-
-- Issue #11811: ssl.get_server_certificate() is now IPv6-compatible. Patch
- by Charles-François Natali.
-
-- Issue #11763: don't use difflib in TestCase.assertMultiLineEqual if the
- strings are too long.
-
-- Issue #11236: getpass.getpass responds to ctrl-c or ctrl-z on terminal.
-
-- Issue #11856: Speed up parsing of JSON numbers.
-
-- Issue #11005: threading.RLock()._release_save() raises a RuntimeError if the
- lock was not acquired.
-
-- Issue #11258: Speed up ctypes.util.find_library() under Linux by a factor
- of 5 to 10. Initial patch by Jonas H.
-
-- Issue #11382: Trivial system calls, such as dup() or pipe(), needn't
- release the GIL. Patch by Charles-François Natali.
-
-- Issue #11223: Add threading._info() function providing informations about
- the thread implementation.
-
-- Issue #11731: simplify/enhance email parser/generator API by introducing
- policy objects.
-
-- Issue #11768: The signal handler of the signal module only calls
- Py_AddPendingCall() for the first signal to fix a deadlock on reentrant or
- parallel calls. PyErr_SetInterrupt() writes also into the wake up file.
-
-- Issue #11492: fix several issues with header folding in the email package.
-
-- Issue #11852: Add missing imports and update tests.
-
-- Issue #11875: collections.OrderedDict's __reduce__ was temporarily
- mutating the object instead of just working on a copy.
-
-- Issue #11467: Fix urlparse behavior when handling urls which contains scheme
- specific part only digits. Patch by Santoso Wijaya.
-
-- collections.Counter().copy() now works correctly for subclasses.
-
-- Issue #11474: Fix the bug with url2pathname() handling of '/C|/' on Windows.
- Patch by Santoso Wijaya.
-
-- Issue #11684: complete email.parser bytes API by adding BytesHeaderParser.
-
-- The bz2 module now handles 4GiB+ input buffers correctly.
-
-- Issue #9233: Fix json.loads('{}') to return a dict (instead of a list), when
- _json is not available.
-
-- Issue #11830: Remove unnecessary introspection code in the decimal module.
-
-- Issue #11703: urllib2.geturl() does not return correct url when the original
- url contains #fragment.
-
-- Issue #10019: Fixed regression in json module where an indent of 0 stopped
- adding newlines and acted instead like 'None'.
-
-- Issue #11186: pydoc ignores a module if its name contains a surrogate
- character in the index of modules.
-
-- Issue #11815: Use a light-weight SimpleQueue for the result queue in
- concurrent.futures.ProcessPoolExecutor.
-
-- Issue #5162: Treat services like frozen executables to allow child spawning
- from multiprocessing.forking on Windows.
-
-- logging.basicConfig now supports an optional 'handlers' argument taking an
- iterable of handlers to be added to the root logger. Additional parameter
- checks were also added to basicConfig.
-
-- Issue #11814: Fix likely typo in multiprocessing.Pool._terminate().
-
-- Issue #11747: Fix range formatting in difflib.context_diff() and
- difflib.unified_diff().
-
-- Issue #8428: Fix a race condition in multiprocessing.Pool when terminating
- worker processes: new processes would be spawned while the pool is being
- shut down. Patch by Charles-François Natali.
-
-- Issue #2650: re.escape() no longer escapes the '_'.
-
-- Issue #11757: select.select() now raises ValueError when a negative timeout
- is passed (previously, a select.error with EINVAL would be raised). Patch
- by Charles-François Natali.
-
-- Issue #7311: fix html.parser to accept non-ASCII attribute values.
-
-- Issue #11605: email.parser.BytesFeedParser was incorrectly converting
- multipart subparts with an 8-bit CTE into unicode instead of preserving the
- bytes.
-
-- Issue #1690608: email.util.formataddr is now RFC 2047 aware: it now has a
- charset parameter that defaults to utf-8 and is used as the charset for RFC
- 2047 encoding when the realname contains non-ASCII characters.
-
-- Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
-
-- Issue #10791: Implement missing method GzipFile.read1(), allowing GzipFile
- to be wrapped in a TextIOWrapper. Patch by Nadeem Vawda.
-
-- Issue #11707: Added a fast C version of functools.cmp_to_key().
- Patch by Filip Gruszczyński.
-
-- Issue #11688: Add sqlite3.Connection.set_trace_callback(). Patch by
- Torsten Landschoff.
-
-- Issue #11746: Fix SSLContext.load_cert_chain() to accept elliptic curve
- private keys.
-
-- Issue #5863: Rewrite BZ2File in pure Python, and allow it to accept
- file-like objects using a new ``fileobj`` constructor argument. Patch by
- Nadeem Vawda.
-
-- unittest.TestCase.assertSameElements has been removed.
-
-- sys.getfilesystemencoding() raises a RuntimeError if initfsencoding() was not
- called yet: detect bootstrap (startup) issues earlier.
-
-- Issue #11393: Add the new faulthandler module.
-
-- Issue #11618: Fix the timeout logic in threading.Lock.acquire() under Windows.
-
-- Removed the 'strict' argument to email.parser.Parser, which has been
- deprecated since Python 2.4.
-
-- Issue #11256: Fix inspect.getcallargs on functions that take only keyword
- arguments.
-
-- Issue #11696: Fix ID generation in msilib.
-
-- itertools.accumulate now supports an optional *func* argument for
- a user-supplied binary function.
-
-- Issue #11692: Remove unnecessary demo functions in subprocess module.
-
-- Issue #9696: Fix exception incorrectly raised by xdrlib.Packer.pack_int when
- trying to pack a negative (in-range) integer.
-
-- Issue #11675: multiprocessing.[Raw]Array objects created from an integer size
- are now zeroed on creation. This matches the behaviour specified by the
- documentation.
-
-- Issue #7639: Fix short file name generation in bdist_msi
-
-- Issue #11635: Don't use polling in worker threads and processes launched by
- concurrent.futures.
-
-- Issue #5845: Automatically read readline configuration to enable completion
- in interactive mode.
-
-- Issue #6811: Allow importlib to change a code object's co_filename attribute
- to match the path to where the source code currently is, not where the code
- object originally came from.
-
-- Issue #8754: Have importlib use the repr of a module name in error messages.
-
-- Issue #11591: Prevent "import site" from modifying sys.path when python
- was started with -S.
-
-- collections.namedtuple() now adds a _source attribute to the generated
- class. This make the source more accessible than the outdated
- "verbose" option which prints to stdout but doesn't make the source
- string available.
-
-- Issue #11371: Mark getopt error messages as localizable. Patch by Filip
- Gruszczyński.
-
-- Issue #11333: Add __slots__ to collections ABCs.
-
-- Issue #11628: cmp_to_key generated class should use __slots__.
-
-- Issue #11666: let help() display named tuple attributes and methods
- that start with a leading underscore.
-
-- Issue #11662: Make urllib and urllib2 ignore redirections if the
- scheme is not HTTP, HTTPS or FTP (CVE-2011-1521).
-
-- Issue #5537: Fix time2isoz() and time2netscape() functions of
- httplib.cookiejar for expiration year greater than 2038 on 32-bit systems.
-
-- Issue #4391: Use proper gettext plural forms in optparse.
-
-- Issue #11127: Raise a TypeError when trying to pickle a socket object.
-
-- Issue #11563: ``Connection: close`` header is sent by requests using URLOpener
- class which helps in closing of sockets after connection is over. Patch
- contributions by Jeff McNeil and Nadeem Vawda.
-
-- Issue #11459: A ``bufsize`` value of 0 in subprocess.Popen() really creates
- unbuffered pipes, such that select() works properly on them.
-
-- Issue #5421: Fix misleading error message when one of socket.sendto()'s
- arguments has the wrong type. Patch by Nikita Vetoshkin.
-
-- Issue #10812: Add some extra posix functions to the os module.
-
-- Issue #10979: unittest stdout buffering now works with class and module
- setup and teardown.
-
-- Issue #11243: fix the parameter querying methods of Message to work if
- the headers contain un-encoded non-ASCII data.
-
-- Issue #11401: fix handling of headers with no value; this fixes a regression
- relative to Python2 and the result is now the same as it was in Python2.
-
-- Issue #9298: base64 bodies weren't being folded to line lengths less than 78,
- which was a regression relative to Python2. Unlike Python2, the last line
- of the folded body now ends with a carriage return.
-
-- Issue #11560: shutil.unpack_archive now correctly handles the format
- parameter. Patch by Evan Dandrea.
-
-- Issue #5870: Add `subprocess.DEVNULL` constant.
-
-- Issue #11133: fix two cases where inspect.getattr_static can trigger code
- execution. Patch by Andreas Stührk.
-
-- Issue #11569: use absolute path to the sysctl command in multiprocessing to
- ensure that it will be found regardless of the shell PATH. This ensures
- that multiprocessing.cpu_count works on default installs of MacOSX.
-
-- Issue #11501: disutils.archive_utils.make_zipfile no longer fails if zlib is
- not installed. Instead, the zipfile.ZIP_STORED compression is used to create
- the ZipFile. Patch by Natalia B. Bidart.
-
-- Issue #11289: `smtp.SMTP` class is now a context manager so it can be used
- in a `with` statement. Contributed by Giampaolo Rodola.
-
-- Issue #11554: Fixed support for Japanese codecs; previously the body output
- encoding was not done if euc-jp or shift-jis was specified as the charset.
-
-- Issue #11407: `TestCase.run` returns the result object used or created.
- Contributed by Janathan Hartley.
-
-- Issue #11500: Fixed a bug in the OS X proxy bypass code for fully qualified
- IP addresses in the proxy exception list.
-
-- Issue #11491: dbm.error is no longer raised when dbm.open is called with
- the "n" as the flag argument and the file exists. The behavior matches
- the documentation and general logic.
-
-- Issue #1162477: Postel Principle adjustment to email date parsing: handle the
- fact that some non-compliant MUAs use '.' instead of ':' in time specs.
-
-- Issue #11131: Fix sign of zero in decimal.Decimal plus and minus
- operations when the rounding mode is ROUND_FLOOR.
-
-- Issue #9935: Speed up pickling of instances of user-defined classes.
-
-- Issue #5622: Fix curses.wrapper to raise correct exception if curses
- initialization fails.
-
-- Issue #11408: In threading.Lock.acquire(), only call gettimeofday() when
- really necessary. Patch by Charles-François Natali.
-
-- Issue #11391: Writing to a mmap object created with
- ``mmap.PROT_READ|mmap.PROT_EXEC`` would segfault instead of raising a
- TypeError. Patch by Charles-François Natali.
-
-- Issue #9795: add context manager protocol support for nntplib.NNTP class.
-
-- Issue #11306: mailbox in certain cases adapts to an inability to open
- certain files in read-write mode. Previously it detected this by
- checking for EACCES, now it also checks for EROFS.
-
-- Issue #11265: asyncore now correctly handles EPIPE, EBADF and EAGAIN errors
- on accept(), send() and recv().
-
-- Issue #11377: Deprecate platform.popen() and reimplement it with os.popen().
-
-- Issue #8513: On UNIX, subprocess supports bytes command string.
-
-- Issue #10866: Add socket.sethostname(). Initial patch by Ross Lagerwall.
-
-- Issue #11140: Lock.release() now raises a RuntimeError when attempting
- to release an unacquired lock, as claimed in the threading documentation.
- The _thread.error exception is now an alias of RuntimeError. Patch by
- Filip Gruszczyński. Patch for _dummy_thread by Aymeric Augustin.
-
-- Issue #8594: ftplib now provides a source_address parameter to specify which
- (address, port) to bind to before connecting.
-
-- Issue #11326: Add the missing connect_ex() implementation for SSL sockets,
- and make it work for non-blocking connects.
-
-- Issue #11297: Add collections.ChainMap().
-
-- Issue #10755: Add the posix.flistdir() function. Patch by Ross Lagerwall.
-
-- Issue #4761: Add the ``*at()`` family of functions (openat(), etc.) to the
- posix module. Patch by Ross Lagerwall.
-
-- Issue #7322: Trying to read from a socket's file-like object after a timeout
- occurred now raises an error instead of silently losing data.
-
-- Issue #11291: poplib.POP no longer suppresses errors on quit().
-
-- Issue #11177: asyncore's create_socket() arguments can now be omitted.
-
-- Issue #6064: Add a ``daemon`` keyword argument to the threading.Thread
- and multiprocessing.Process constructors in order to override the
- default behaviour of inheriting the daemonic property from the current
- thread/process.
-
-- Issue #10956: Buffered I/O classes retry reading or writing after a signal
- has arrived and the handler returned successfully.
-
-- Issue #10784: New os.getpriority() and os.setpriority() functions.
-
-- Issue #11114: Fix catastrophic performance of tell() on text files (up
- to 1000x faster in some cases). It is still one to two order of magnitudes
- slower than binary tell().
-
-- Issue #10882: Add os.sendfile function.
-
-- Issue #10868: Allow usage of the register method of an ABC as a class
- decorator.
-
-- Issue #11224: Fixed a regression in tarfile that affected the file-like
- objects returned by TarFile.extractfile() regarding performance, memory
- consumption and failures with the stream interface.
-
-- Issue #10924: Adding salt and Modular Crypt Format to crypt library.
- Moved old C wrapper to _crypt, and added a Python wrapper with
- enhanced salt generation and simpler API for password generation.
-
-- Issue #11074: Make 'tokenize' so it can be reloaded.
-
-- Issue #11085: Moved collections abstract base classes into a separate
- module called collections.abc, following the pattern used by importlib.abc.
- For backwards compatibility, the names are imported into the collections
- module.
-
-- Issue #4681: Allow mmap() to work on file sizes and offsets larger than
- 4GB, even on 32-bit builds. Initial patch by Ross Lagerwall, adapted for
- 32-bit Windows.
-
-- Issue #11169: compileall module uses repr() to format filenames and paths to
- escape surrogate characters and show spaces.
-
-- Issue #11089: Fix performance issue limiting the use of ConfigParser()
- with large config files.
-
-- Issue #10276: Fix the results of zlib.crc32() and zlib.adler32() on buffers
- larger than 4GB. Patch by Nadeem Vawda.
-
-- Issue #11388: Added a clear() method to MutableSequence
-
-- Issue #11174: Add argparse.MetavarTypeHelpFormatter, which uses type names
- for the names of optional and positional arguments in help messages.
-
-- Issue #9348: Raise an early error if argparse nargs and metavar don't match.
-
-- Issue #9026: Fix order of argparse sub-commands in help messages.
-
-- Issue #9347: Fix formatting for tuples in argparse type= error messages.
-
-- Issue #12191: Added shutil.chown() to change user and/or group owner of a
- given path also specifying their names.
-
-- Issue #13988: The _elementtree accelerator is used whenever available.
- Now xml.etree.cElementTree becomes a deprecated alias to ElementTree.
-
-Build
------
-
-- Issue #6807: Run msisupport.mak earlier.
-
-- Issue #10580: Minor grammar change in Windows installer.
-
-- Issue #13326: Clean __pycache__ directories correctly on OpenBSD.
-
-- PEP 393: the configure option --with-wide-unicode is removed.
-
-- Issue #12852: Set _XOPEN_SOURCE to 700, instead of 600, to get POSIX 2008
- functions on OpenBSD (e.g. fdopendir).
-
-- Issue #11863: Remove support for legacy systems deprecated in Python 3.2
- (following PEP 11). These systems are systems using Mach C Threads,
- SunOS lightweight processes, GNU pth threads and IRIX threads.
-
-- Issue #8746: Correct faulty configure checks so that os.chflags() and
- os.lchflags() are once again built on systems that support these
- functions (BSD and OS X). Also add new stat file flags for OS X
- (UF_HIDDEN and UF_COMPRESSED).
-
-- Issue #10645: Installing Python no longer creates a
- Python-X.Y.Z-pyX.Y.egg-info file in the lib-dynload directory.
-
-- Do not accidentally include the directory containing sqlite.h twice when
- building sqlite3.
-
-- Issue #11217: For 64-bit/32-bit Mac OS X universal framework builds,
- ensure "make install" creates symlinks in --prefix bin for the "-32"
- files in the framework bin directory like the installer does.
-
-- Issue #11347: Use --no-as-needed when linking libpython3.so.
-
-- Issue #11411: Fix 'make DESTDIR=' with a relative destination.
-
-- Issue #11268: Prevent Mac OS X Installer failure if Documentation
- package had previously been installed.
-
-- Issue #11495: OSF support is eliminated. It was deprecated in Python 3.2.
-
-IDLE
-----
-
-- Issue #14409: IDLE now properly executes commands in the Shell window
- when it cannot read the normal config files on startup and
- has to use the built-in default key bindings.
- There was previously a bug in one of the defaults.
-
-- IDLE can be launched as python -m idlelib
-
-- Issue #3573: IDLE hangs when passing invalid command line args
- (directory(ies) instead of file(s)) (Patch by Guilherme Polo)
-
-- Issue #14200: IDLE shell crash on printing non-BMP unicode character.
-
-- Issue #5219: Prevent event handler cascade in IDLE.
-
-- Issue #964437: Make IDLE help window non-modal.
- Patch by Guilherme Polo and Roger Serwy.
-
-- Issue #13933: IDLE auto-complete did not work with some imported
- module, like hashlib. (Patch by Roger Serwy)
-
-- Issue #13506: Add '' to path for IDLE Shell when started and restarted with Restart Shell.
- Original patches by Marco Scataglini and Roger Serwy.
-
-- Issue #4625: If IDLE cannot write to its recent file or breakpoint files,
- display a message popup and continue rather than crash. Original patch by
- Roger Serwy.
-
-- Issue #8641: Update IDLE 3 syntax coloring to recognize b".." and not u"..".
- Patch by Tal Einat.
-
-- Issue #13296: Fix IDLE to clear compile __future__ flags on shell restart.
- (Patch by Roger Serwy)
-
-- Issue #9871: Prevent IDLE 3 crash when given byte stings
- with invalid hex escape sequences, like b'\x0'.
- (Original patch by Claudiu Popa.)
-
-- Issue #12636: IDLE reads the coding cookie when executing a Python script.
-
-- Issue #12540: Prevent zombie IDLE processes on Windows due to changes
- in os.kill().
-
-- Issue #12590: IDLE editor window now always displays the first line
- when opening a long file. With Tk 8.5, the first line was hidden.
-
-- Issue #11088: don't crash when using F5 to run a script in IDLE on MacOSX
- with Tk 8.5.
-
-- Issue #1028: Tk returns invalid Unicode null in %A: UnicodeDecodeError.
- With Tk < 8.5 _tkinter.c:PythonCmd() raised UnicodeDecodeError, caused
- IDLE to exit. Converted to valid Unicode null in PythonCmd().
-
-- Issue #11718: IDLE's open module dialog couldn't find the __init__.py
- file in a package.
-
-Tools/Demos
------------
-
-- Issue #14053: patchcheck.py ("make patchcheck") now works with MQ patches.
- Patch by Francisco Martín Brugué.
-
-- Issue #13930: 2to3 is now able to write its converted output files to another
- directory tree as well as copying unchanged files and altering the file
- suffix. See its new -o, -W and --add-suffix options. This makes it more
- useful in many automated code translation workflows.
-
-- Issue #13628: python-gdb.py is now able to retrieve more frames in the Python
- traceback if Python is optimized.
-
-- Issue #11996: libpython (gdb), replace "py-bt" command by "py-bt-full" and
- add a smarter "py-bt" command printing a classic Python traceback.
-
-- Issue #11179: Make ccbench work under Python 3.1 and 2.7 again.
-
-- Issue #10639: reindent.py no longer converts newlines and will raise
- an error if attempting to convert a file with mixed newlines.
- "--newline" option added to specify new line character.
-
-Extension Modules
------------------
-
-- Issue #16847: Fixed improper use of _PyUnicode_CheckConsistency() in
- non-pydebug builds. Several extension modules now compile cleanly when
- assert()s are enabled in standard builds (-DDEBUG flag).
-
-- Issue #13840: The error message produced by ctypes.create_string_buffer
- when given a Unicode string has been fixed.
-
-- Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
- Vilmos Nebehaj.
-
-- Issue #7777: socket: Add Reliable Datagram Sockets (PF_RDS) support.
-
-- Issue #13159: FileIO and BZ2Compressor/BZ2Decompressor now use a linear-time
- buffer growth strategy instead of a quadratic-time one.
-
-- Issue #10141: socket: Add SocketCAN (PF_CAN) support. Initial patch by
- Matthias Fuchs, updated by Tiago Gonçalves.
-
-- Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
- would be finalized after the reference to its underlying BufferedRWPair's
- writer got cleared by the GC.
-
-- Issue #12881: ctypes: Fix segfault with large structure field names.
-
-- Issue #13058: ossaudiodev: fix a file descriptor leak on error. Patch by
- Thomas Jarosch.
-
-- Issue #13013: ctypes: Fix a reference leak in PyCArrayType_from_ctype.
- Thanks to Suman Saha for finding the bug and providing a patch.
-
-- Issue #13022: Fix: _multiprocessing.recvfd() doesn't check that
- file descriptor was actually received.
-
-- Issue #1172711: Add 'long long' support to the array module.
- Initial patch by Oren Tirosh and Hirokazu Yamamoto.
-
-- Issue #12483: ctypes: Fix a crash when the destruction of a callback
- object triggers the garbage collector.
-
-- Issue #12950: Fix passing file descriptors in multiprocessing, under
- OpenIndiana/Illumos.
-
-- Issue #12764: Fix a crash in ctypes when the name of a Structure field is not
- a string.
-
-- Issue #11241: subclasses of ctypes.Array can now be subclassed.
-
-- Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to
- some functions like file.write().
-
-- Issue #10309: Define _GNU_SOURCE so that mremap() gets the proper
- signature. Without this, architectures where sizeof void* != sizeof int are
- broken. Patch given by Hallvard B Furuseth.
-
-- Issue #12051: Fix segfault in json.dumps() while encoding highly-nested
- objects using the C accelerations.
-
-- Issue #12017: Fix segfault in json.loads() while decoding highly-nested
- objects using the C accelerations.
-
-- Issue #1838: Prevent segfault in ctypes, when _as_parameter_ on a class is set
- to an instance of the class.
-
-Tests
------
-
-- Issue #13125: Silence spurious test_lib2to3 output when in non-verbose mode.
- Patch by Mikhail Novikov.
-
-- Issue #13447: Add a test file to host regression tests for bugs in the
- scripts found in the Tools directory.
-
-- Issue #10881: Fix test_site failure with OS X framework builds.
-
-- Issue #13901: Prevent test_distutils failures on OS X with --enable-shared.
-
-- Issue #13862: Fix spurious failure in test_zlib due to runtime/compile time
- minor versions not matching.
-
-- Issue #12804: Fix test_socket and test_urllib2net failures when running tests
- on a system without internet access.
-
-- Issue #13726: Fix the ambiguous -S flag in regrtest. It is -o/--slow for slow
- tests.
-
-- Issue #11659: Fix ResourceWarning in test_subprocess introduced by #11459.
- Patch by Ben Hayden.
-
-- Issue #11577: fix ResourceWarning triggered by improved binhex test coverage
-
-- Issue #11509: Significantly increase test coverage of fileinput.
- Patch by Denver Coneybeare at PyCon 2011 Sprints.
-
-- Issue #11689: Fix a variable scoping error in an sqlite3 test
-
-- Issue #13786: Remove unimplemented 'trace' long option from regrtest.py.
-
-- Issue #13725: Fix regrtest to recognize the documented -d flag.
- Patch by Erno Tukia.
-
-- Issue #13304: Skip test case if user site-packages disabled (-s or
- PYTHONNOUSERSITE). (Patch by Carl Meyer)
-
-- Issue #5661: Add a test for ECONNRESET/EPIPE handling to test_asyncore. Patch
- by Xavier de Gaye.
-
-- Issue #13218: Fix test_ssl failures on Debian/Ubuntu.
-
-- Re-enable lib2to3's test_parser.py tests, though with an expected failure
- (see issue 13125).
-
-- Issue #12656: Add tests for IPv6 and Unix sockets to test_asyncore.
-
-- Issue #6484: Add unit tests for mailcap module (patch by Gregory Nofi)
-
-- Issue #11651: Improve the Makefile test targets to run more of the test suite
- more quickly. The --multiprocess option is now enabled by default, reducing
- the amount of time needed to run the tests. "make test" and "make quicktest"
- now include some resource-intensive tests, but no longer run the test suite
- twice to check for bugs in .pyc generation. Tools/scripts/run_test.py provides
- an easy platform-independent way to run test suite with sensible defaults.
-
-- Issue #12331: The test suite for the packaging module can now run from an
- installed Python.
-
-- Issue #12331: The test suite for lib2to3 can now run from an installed
- Python.
-
-- Issue #12626: In regrtest, allow to filter tests using a glob filter
- with the ``-m`` (or ``--match``) option. This works with all test cases
- using the unittest module. This is useful with long test suites
- such as test_io or test_subprocess.
-
-- Issue #12624: It is now possible to fail after the first failure when
- running in verbose mode (``-v`` or ``-W``), by using the ``--failfast``
- (or ``-G``) option to regrtest. This is useful with long test suites
- such as test_io or test_subprocess.
-
-- Issue #12587: Correct faulty test file and reference in test_tokenize.
- (Patch by Robert Xiao)
-
-- Issue #12573: Add resource checks for dangling Thread and Process objects.
-
-- Issue #12549: Correct test_platform to not fail when OS X returns 'x86_64'
- as the processor type on some Mac systems.
-
-- Skip network tests when getaddrinfo() returns EAI_AGAIN, meaning a temporary
- failure in name resolution.
-
-- Issue #11812: Solve transient socket failure to connect to 'localhost'
- in test_telnetlib.py.
-
-- Solved a potential deadlock in test_telnetlib.py. Related to issue #11812.
-
-- Avoid failing in test_robotparser when mueblesmoraleda.com is flaky and
- an overzealous DNS service (e.g. OpenDNS) redirects to a placeholder
- Web site.
-
-- Avoid failing in test_urllibnet.test_bad_address when some overzealous
- DNS service (e.g. OpenDNS) resolves a non-existent domain name. The test
- is now skipped instead.
-
-- Issue #12440: When testing whether some bits in SSLContext.options can be
- reset, check the version of the OpenSSL headers Python was compiled against,
- rather than the runtime version of the OpenSSL library.
-
-- Issue #11512: Add a test suite for the cgitb module. Patch by Robbie Clemons.
-
-- Issue #12497: Install test/data to prevent failures of the various codecmaps
- tests.
-
-- Issue #12496: Install test/capath directory to prevent test_connect_capath
- testcase failure in test_ssl.
-
-- Issue #12469: Run wakeup and pending signal tests in a subprocess to run the
- test in a fresh process with only one thread and to not change signal
- handling of the parent process.
-
-- Issue #8716: Avoid crashes caused by Aqua Tk on OSX when attempting to run
- test_tk or test_ttk_guionly under a username that is not currently logged
- in to the console windowserver (as may be the case under buildbot or ssh).
-
-- Issue #12407: Explicitly skip test_capi.EmbeddingTest under Windows.
-
-- Issue #12400: regrtest -W doesn't rerun the tests twice anymore, but captures
- the output and displays it on failure instead. regrtest -v doesn't print the
- error twice anymore if there is only one error.
-
-- Issue #12141: Install copies of template C module file so that
- test_build_ext of test_distutils and test_command_build_ext of
- test_packaging are no longer silently skipped when
- run outside of a build directory.
-
-- Issue #8746: Add additional tests for os.chflags() and os.lchflags().
- Patch by Garrett Cooper.
-
-- Issue #10736: Fix test_ttk test_widgets failures with Cocoa Tk 8.5.9
- 2.8 + on Mac OS X. (Patch by Ronald Oussoren)
-
-- Issue #12057: Add tests for ISO 2022 codecs (iso2022_jp, iso2022_jp_2,
- iso2022_kr).
-
-- Issue #12096: Fix a race condition in test_threading.test_waitfor(). Patch
- written by Charles-François Natali.
-
-- Issue #11614: import __hello__ prints "Hello World!". Patch written by
- Andreas Stührk.
-
-- Issue #5723: Improve json tests to be executed with and without accelerations.
-
-- Issue #12041: Make test_wait3 more robust.
-
-- Issue #11873: Change regex in test_compileall to fix occasional failures when
- when the randomly generated temporary path happened to match the regex.
-
-- Issue #11958: Fix FTP tests for IPv6, bind to "::1" instead of "localhost".
- Patch written by Charles-Francois Natali.
-
-- Issue #8407, #11859: Fix tests of test_io using threads and an alarm: use
- pthread_sigmask() to ensure that the SIGALRM signal is received by the main
- thread.
-
-- Issue #11811: Factor out detection of IPv6 support on the current host
- and make it available as ``test.support.IPV6_ENABLED``. Patch by
- Charles-François Natali.
-
-- Issue #10914: Add a minimal embedding test to test_capi.
-
-- Issue #11223: Skip test_lock_acquire_interruption() and
- test_rlock_acquire_interruption() of test_threadsignals if a thread lock is
- implemented using a POSIX mutex and a POSIX condition variable. A POSIX
- condition variable cannot be interrupted by a signal (e.g. on Linux, the
- futex system call is restarted).
-
-- Issue #11790: Fix sporadic failures in test_multiprocessing.WithProcessesTestCondition.
-
-- Fix possible "file already exists" error when running the tests in parallel.
-
-- Issue #11719: Fix message about unexpected test_msilib skip on non-Windows
- platforms. Patch by Nadeem Vawda.
-
-- Issue #11727: Add a --timeout option to regrtest: if a test takes more than
- TIMEOUT seconds, dumps the traceback of all threads and exits.
-
-- Issue #11653: fix -W with -j in regrtest.
-
-- The email test suite now lives in the Lib/test/test_email package. The test
- harness code has also been modernized to allow use of new unittest features.
-
-- regrtest now discovers test packages as well as test modules.
-
-- Issue #11577: improve test coverage of binhex.py. Patch by Arkady Koplyarov.
-
-- New test_crashers added to exercise the scripts in the Lib/test/crashers
- directory and confirm they fail as expected
-
-- Issue #11578: added test for the timeit module. Patch by Michael Henry.
-
-- Issue #11503: improve test coverage of posixpath.py. Patch by Evan Dandrea.
-
-- Issue #11505: improves test coverage of string.py, increases granularity of
- string.Formatter tests. Initial patch by Alicia Arlen.
-
-- Issue #11548: Improve test coverage of the shutil module. Patch by
- Evan Dandrea.
-
-- Issue #11554: Reactivated test_email_codecs.
-
-- Issue #11505: improves test coverage of string.py. Patch by Alicia
- Arlen
-
-- Issue #11490: test_subprocess.test_leaking_fds_on_error no longer gives a
- false positive if the last directory in the path is inaccessible.
-
-- Issue #11223: Fix test_threadsignals to fail, not hang, when the
- non-semaphore implementation of locks is used under POSIX.
-
-- Issue #10911: Add tests on CGI with non-ASCII characters. Patch written by
- Pierre Quentel.
-
-- Issue #9931: Fix hangs in GUI tests under Windows in certain conditions.
- Patch by Hirokazu Yamamoto.
-
-- Issue #10512: Properly close sockets under test.test_cgi.
-
-- Issue #10992: Make tests pass under coverage.
-
-- Issue #10826: Prevent sporadic failure in test_subprocess on Solaris due
- to open door files.
-
-- Issue #10990: Prevent tests from clobbering a set trace function.
-
-C-API
------
-
-- Issue #13452: PyUnicode_EncodeDecimal() doesn't support error handlers
- different than "strict" anymore. The caller was unable to compute the
- size of the output buffer: it depends on the error handler.
-
-- Issue #13560: Add PyUnicode_DecodeLocale(), PyUnicode_DecodeLocaleAndSize()
- and PyUnicode_EncodeLocale() functions to the C API to decode/encode from/to
- the current locale encoding.
-
-- Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats.
-
-- Issue #11246: Fix PyUnicode_FromFormat("%V") to decode the byte string from
- UTF-8 (with replace error handler) instead of ISO-8859-1 (in strict mode).
- Patch written by Ray Allen.
-
-- Issue #10830: Fix PyUnicode_FromFormatV("%c") for non-BMP characters on
- narrow build.
-
-- Add PyObject_GenericGetDict and PyObject_GeneriSetDict. They are generic
- implementations for the getter and setter of a ``__dict__`` descriptor of C
- types.
-
-- Issue #13727: Add 3 macros to access PyDateTime_Delta members:
- PyDateTime_DELTA_GET_DAYS, PyDateTime_DELTA_GET_SECONDS,
- PyDateTime_DELTA_GET_MICROSECONDS.
-
-- Issue #10542: Add 4 macros to work with surrogates: Py_UNICODE_IS_SURROGATE,
- Py_UNICODE_IS_HIGH_SURROGATE, Py_UNICODE_IS_LOW_SURROGATE,
- Py_UNICODE_JOIN_SURROGATES.
-
-- Issue #12724: Add Py_RETURN_NOTIMPLEMENTED macro for returning NotImplemented.
-
-- PY_PATCHLEVEL_REVISION has been removed, since it's meaningless with
- Mercurial.
-
-- Issue #12173: The first argument of PyImport_ImportModuleLevel is now `const
- char *` instead of `char *`.
-
-- Issue #12380: PyArg_ParseTuple now accepts a bytearray for the 'c' format.
-
-Documentation
--------------
-
-- Issue #23006: Improve the documentation and indexing of dict.__missing__.
- Add an entry in the language datamodel special methods section.
- Revise and index its discussion in the stdtypes mapping/dict section.
-
-- Issue #13989: Document that GzipFile does not support text mode, and give a
- more helpful error message when opened with an invalid mode string.
-
-- Issue #13921: Undocument and clean up sqlite3.OptimizedUnicode,
- which is obsolete in Python 3.x. It's now aliased to str for
- backwards compatibility.
-
-- Issue #12102: Document that buffered files must be flushed before being used
- with mmap. Patch by Steffen Daode Nurpmeso.
-
-- Issue #8982: Improve the documentation for the argparse Namespace object.
-
-- Issue #9343: Document that argparse parent parsers must be configured before
- their children.
-
-- Issue #13498: Clarify docs of os.makedirs()'s exist_ok argument. Done with
- great native-speaker help from R. David Murray.
-
-- Issues #13491 and #13995: Fix many errors in sqlite3 documentation.
- Initial patch for #13491 by Johannes Vogel.
-
-- Issue #13402: Document absoluteness of sys.executable.
-
-- Issue #13883: PYTHONCASEOK also works on OS X.
-
-- Issue #9021: Add an introduction to the copy module documentation.
-
-- Issue #6005: Examples in the socket library documentation use sendall, where
- relevant, instead send method.
-
-- Issue #12798: Updated the mimetypes documentation.
-
-- Issue #12949: Document the kwonlyargcount argument for the PyCode_New
- C API function.
-
-- Issue #13513: Fix io.IOBase documentation to correctly link to the
- io.IOBase.readline method instead of the readline module.
-
-- Issue #13237: Reorganise subprocess documentation to emphasise convenience
- functions and the most commonly needed arguments to Popen.
-
-- Issue #13141: Demonstrate recommended style for socketserver examples.
-
-- Issue #11818: Fix tempfile examples for Python 3.
-
-
**(For information about older versions, consult the HISTORY file.)**
diff --git a/Misc/RPM/python-3.4.spec b/Misc/RPM/python-3.5.spec
index ae0938f..8efda0e 100644
--- a/Misc/RPM/python-3.4.spec
+++ b/Misc/RPM/python-3.5.spec
@@ -39,8 +39,8 @@
%define name python
#--start constants--
-%define version 3.4.3rc1
-%define libvers 3.4
+%define version 3.5.0a1
+%define libvers 3.5
#--end constants--
%define release 1pydotorg
%define __prefix /usr
diff --git a/Misc/coverity_model.c b/Misc/coverity_model.c
index 57f3aeb..421d54d 100644
--- a/Misc/coverity_model.c
+++ b/Misc/coverity_model.c
@@ -85,7 +85,7 @@ PyObject *PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
}
/* Python/fileutils.c */
-wchar_t *_Py_char2wchar(const char* arg, size_t *size)
+wchar_t *Py_DecodeLocale(const char* arg, size_t *size)
{
wchar_t *w;
__coverity_tainted_data_sink__(arg);
diff --git a/Modules/README b/Modules/README
new file mode 100644
index 0000000..9b79f53
--- /dev/null
+++ b/Modules/README
@@ -0,0 +1,2 @@
+Source files for standard library extension modules,
+and former extension modules that are now builtin modules.
diff --git a/Modules/Setup.config.in b/Modules/Setup.config.in
index 5ac2404..adac030 100644
--- a/Modules/Setup.config.in
+++ b/Modules/Setup.config.in
@@ -7,7 +7,7 @@
@USE_THREAD_MODULE@_thread _threadmodule.c
# The signal module
-@USE_SIGNAL_MODULE@signal signalmodule.c
+@USE_SIGNAL_MODULE@_signal signalmodule.c
# The rest of the modules previously listed in this file are built
# by the setup.py script in Python 2.1 and later.
diff --git a/Modules/Setup.dist b/Modules/Setup.dist
index 01fb85f..ee84df4 100644
--- a/Modules/Setup.dist
+++ b/Modules/Setup.dist
@@ -118,6 +118,7 @@ _collections _collectionsmodule.c # Container types
itertools itertoolsmodule.c # Functions creating iterators for efficient looping
atexit atexitmodule.c # Register functions to be run at interpreter-shutdown
_stat _stat.c # stat.h interface
+time timemodule.c # time module
# access to ISO C locale support
_locale _localemodule.c # -lintl
diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c
index e652f4d..4f2afda 100644
--- a/Modules/_bz2module.c
+++ b/Modules/_bz2module.c
@@ -188,7 +188,7 @@ compress(BZ2Compressor *c, char *data, size_t len, int action)
if (action == BZ_FINISH && bzerror == BZ_STREAM_END)
break;
}
- if (data_size != PyBytes_GET_SIZE(result))
+ if (data_size != (size_t)PyBytes_GET_SIZE(result))
if (_PyBytes_Resize(&result, data_size) < 0)
goto error;
return result;
@@ -457,7 +457,7 @@ decompress(BZ2Decompressor *d, char *data, size_t len)
d->bzs.avail_out = (unsigned int)Py_MIN(buffer_left, UINT_MAX);
}
}
- if (data_size != PyBytes_GET_SIZE(result))
+ if (data_size != (size_t)PyBytes_GET_SIZE(result))
if (_PyBytes_Resize(&result, data_size) < 0)
goto error;
return result;
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
index 52f3479..bf408af 100644
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -95,13 +95,15 @@ a ValueError. Other possible values are 'ignore', 'replace' and\n\
codecs.register_error that can handle ValueErrors.");
static PyObject *
-codec_encode(PyObject *self, PyObject *args)
+codec_encode(PyObject *self, PyObject *args, PyObject *kwargs)
{
+ static char *kwlist[] = {"obj", "encoding", "errors", NULL};
const char *encoding = NULL;
const char *errors = NULL;
PyObject *v;
- if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors))
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:encode", kwlist,
+ &v, &encoding, &errors))
return NULL;
if (encoding == NULL)
@@ -122,13 +124,15 @@ as well as any other name registered with codecs.register_error that is\n\
able to handle ValueErrors.");
static PyObject *
-codec_decode(PyObject *self, PyObject *args)
+codec_decode(PyObject *self, PyObject *args, PyObject *kwargs)
{
+ static char *kwlist[] = {"obj", "encoding", "errors", NULL};
const char *encoding = NULL;
const char *errors = NULL;
PyObject *v;
- if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors))
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:decode", kwlist,
+ &v, &encoding, &errors))
return NULL;
if (encoding == NULL)
@@ -750,7 +754,7 @@ unicode_internal_encode(PyObject *self,
u = PyUnicode_AsUnicodeAndSize(obj, &len);
if (u == NULL)
return NULL;
- if (len > PY_SSIZE_T_MAX / sizeof(Py_UNICODE))
+ if ((size_t)len > (size_t)PY_SSIZE_T_MAX / sizeof(Py_UNICODE))
return PyErr_NoMemory();
size = len * sizeof(Py_UNICODE);
return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size),
@@ -1179,9 +1183,9 @@ static PyMethodDef _codecs_functions[] = {
register__doc__},
{"lookup", codec_lookup, METH_VARARGS,
lookup__doc__},
- {"encode", codec_encode, METH_VARARGS,
+ {"encode", (PyCFunction)codec_encode, METH_VARARGS|METH_KEYWORDS,
encode__doc__},
- {"decode", codec_decode, METH_VARARGS,
+ {"decode", (PyCFunction)codec_decode, METH_VARARGS|METH_KEYWORDS,
decode__doc__},
{"escape_encode", escape_encode, METH_VARARGS},
{"escape_decode", escape_decode, METH_VARARGS},
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index c1aa9a3..d3b0287 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -3,7 +3,7 @@
/* collections module implementation of a deque() datatype
Written and maintained by Raymond D. Hettinger <python@rcn.com>
- Copyright (c) 2004-2013 Python Software Foundation.
+ Copyright (c) 2004-2015 Python Software Foundation.
All rights reserved.
*/
@@ -145,6 +145,12 @@ typedef struct {
static PyTypeObject deque_type;
+/* XXX Todo:
+ If aligned memory allocations become available, make the
+ deque object 64 byte aligned so that all of the fields
+ can be retrieved or updated in a single cache line.
+*/
+
static PyObject *
deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
@@ -454,6 +460,31 @@ deque_inplace_concat(dequeobject *deque, PyObject *other)
return (PyObject *)deque;
}
+/* The rotate() method is part of the public API and is used internally
+as a primitive for other methods.
+
+Rotation by 1 or -1 is a common case, so any optimizations for high
+volume rotations should take care not to penalize the common case.
+
+Conceptually, a rotate by one is equivalent to a pop on one side and an
+append on the other. However, a pop/append pair is unnecessarily slow
+because it requires a incref/decref pair for an object located randomly
+in memory. It is better to just move the object pointer from one block
+to the next without changing the reference count.
+
+When moving batches of pointers, it is tempting to use memcpy() but that
+proved to be slower than a simple loop for a variety of reasons.
+Memcpy() cannot know in advance that we're copying pointers instead of
+bytes, that the source and destination are pointer aligned and
+non-overlapping, that moving just one pointer is a common case, that we
+never need to move more than BLOCKLEN pointers, and that at least one
+pointer is always moved.
+
+For high volume rotations, newblock() and freeblock() are never called
+more than once. Previously emptied blocks are immediately reused as a
+destination block. If a block is left-over at the end, it is freed.
+*/
+
static int
_deque_rotate(dequeobject *deque, Py_ssize_t n)
{
@@ -503,13 +534,13 @@ _deque_rotate(dequeobject *deque, Py_ssize_t n)
if (m > leftindex)
m = leftindex;
assert (m > 0 && m <= len);
- src = &rightblock->data[rightindex];
- dest = &leftblock->data[leftindex - 1];
rightindex -= m;
leftindex -= m;
+ src = &rightblock->data[rightindex + 1];
+ dest = &leftblock->data[leftindex];
n -= m;
do {
- *(dest--) = *(src--);
+ *(dest++) = *(src++);
} while (--m);
}
if (rightindex == -1) {
@@ -1800,19 +1831,40 @@ _count_elements(PyObject *self, PyObject *args)
if (mapping_get != NULL && mapping_get == dict_get &&
mapping_setitem != NULL && mapping_setitem == dict_setitem) {
while (1) {
+ /* Fast path advantages:
+ 1. Eliminate double hashing
+ (by re-using the same hash for both the get and set)
+ 2. Avoid argument overhead of PyObject_CallFunctionObjArgs
+ (argument tuple creation and parsing)
+ 3. Avoid indirection through a bound method object
+ (creates another argument tuple)
+ 4. Avoid initial increment from zero
+ (reuse an existing one-object instead)
+ */
+ Py_hash_t hash;
+
key = PyIter_Next(it);
if (key == NULL)
break;
- oldval = PyDict_GetItem(mapping, key);
+
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyASCIIObject *) key)->hash) == -1)
+ {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ goto done;
+ }
+
+ oldval = _PyDict_GetItem_KnownHash(mapping, key, hash);
if (oldval == NULL) {
- if (PyDict_SetItem(mapping, key, one) == -1)
- break;
+ if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) == -1)
+ goto done;
} else {
newval = PyNumber_Add(oldval, one);
if (newval == NULL)
- break;
- if (PyDict_SetItem(mapping, key, newval) == -1)
- break;
+ goto done;
+ if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) == -1)
+ goto done;
Py_CLEAR(newval);
}
Py_DECREF(key);
diff --git a/Modules/_csv.c b/Modules/_csv.c
index c8767d1..ade35e5 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -248,7 +248,7 @@ _set_char(const char *name, Py_UCS4 *target, PyObject *src, Py_UCS4 dflt)
len = PyUnicode_GetLength(src);
if (len > 1) {
PyErr_Format(PyExc_TypeError,
- "\"%s\" must be an 1-character string",
+ "\"%s\" must be a 1-character string",
name);
return -1;
}
@@ -290,7 +290,7 @@ dialect_check_quoting(int quoting)
StyleDesc *qs;
for (qs = quote_styles; qs->name; qs++) {
- if (qs->style == quoting)
+ if ((int)qs->style == quoting)
return 0;
}
PyErr_Format(PyExc_TypeError, "bad \"quoting\" value");
@@ -432,7 +432,7 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto err;
if (self->delimiter == 0) {
PyErr_SetString(PyExc_TypeError,
- "\"delimiter\" must be an 1-character string");
+ "\"delimiter\" must be a 1-character string");
goto err;
}
if (quotechar == Py_None && quoting == NULL)
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 8daeffc..03a911f 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -1603,7 +1603,7 @@ resize(PyObject *self, PyObject *args)
"Memory cannot be resized because this object doesn't own it");
return NULL;
}
- if (size <= sizeof(obj->b_value)) {
+ if ((size_t)size <= sizeof(obj->b_value)) {
/* internal default buffer is large enough */
obj->b_size = size;
goto done;
diff --git a/Modules/_ctypes/libffi_msvc/ffi.c b/Modules/_ctypes/libffi_msvc/ffi.c
index 76cb03e..b7586c7 100644
--- a/Modules/_ctypes/libffi_msvc/ffi.c
+++ b/Modules/_ctypes/libffi_msvc/ffi.c
@@ -65,37 +65,56 @@ void ffi_prep_args(char *stack, extended_cif *ecif)
argp = (char *) ALIGN(argp, sizeof(void *));
z = (*p_arg)->size;
- if (z < sizeof(int))
+ if (z < sizeof(intptr_t))
{
- z = sizeof(int);
+ z = sizeof(intptr_t);
switch ((*p_arg)->type)
{
case FFI_TYPE_SINT8:
- *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
+ *(intptr_t *) argp = (intptr_t)*(SINT8 *)(* p_argv);
break;
case FFI_TYPE_UINT8:
- *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
+ *(uintptr_t *) argp = (uintptr_t)*(UINT8 *)(* p_argv);
break;
case FFI_TYPE_SINT16:
- *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
+ *(intptr_t *) argp = (intptr_t)*(SINT16 *)(* p_argv);
break;
case FFI_TYPE_UINT16:
- *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
+ *(uintptr_t *) argp = (uintptr_t)*(UINT16 *)(* p_argv);
break;
case FFI_TYPE_SINT32:
- *(signed int *) argp = (signed int)*(SINT32 *)(* p_argv);
+ *(intptr_t *) argp = (intptr_t)*(SINT32 *)(* p_argv);
break;
case FFI_TYPE_UINT32:
- *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
+ *(uintptr_t *) argp = (uintptr_t)*(UINT32 *)(* p_argv);
+ break;
+
+ case FFI_TYPE_FLOAT:
+ *(uintptr_t *) argp = 0;
+ *(float *) argp = *(float *)(* p_argv);
+ break;
+
+ // 64-bit value cases should never be used for x86 and AMD64 builds
+ case FFI_TYPE_SINT64:
+ *(intptr_t *) argp = (intptr_t)*(SINT64 *)(* p_argv);
+ break;
+
+ case FFI_TYPE_UINT64:
+ *(uintptr_t *) argp = (uintptr_t)*(UINT64 *)(* p_argv);
break;
case FFI_TYPE_STRUCT:
- *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
+ *(uintptr_t *) argp = (uintptr_t)*(UINT32 *)(* p_argv);
+ break;
+
+ case FFI_TYPE_DOUBLE:
+ *(uintptr_t *) argp = 0;
+ *(double *) argp = *(double *)(* p_argv);
break;
default:
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index 5ffce2f..6b77868 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -2675,7 +2675,7 @@ PyCurses_KeyName(PyObject *self, PyObject *args)
}
knp = keyname(ch);
- return PyBytes_FromString((knp == NULL) ? "" : (char *)knp);
+ return PyBytes_FromString((knp == NULL) ? "" : knp);
}
#endif
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index d8225ba..701c587 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -3805,29 +3805,6 @@ time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw)
return clone;
}
-static int
-time_bool(PyObject *self)
-{
- PyObject *offset, *tzinfo;
- int offsecs = 0;
-
- if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) {
- /* Since utcoffset is in whole minutes, nothing can
- * alter the conclusion that this is nonzero.
- */
- return 1;
- }
- tzinfo = GET_TIME_TZINFO(self);
- if (tzinfo != Py_None) {
- offset = call_utcoffset(tzinfo, Py_None);
- if (offset == NULL)
- return -1;
- offsecs = GET_TD_DAYS(offset)*86400 + GET_TD_SECONDS(offset);
- Py_DECREF(offset);
- }
- return (TIME_GET_MINUTE(self)*60 - offsecs + TIME_GET_HOUR(self)*3600) != 0;
-}
-
/* Pickle support, a simple use of __reduce__. */
/* Let basestate be the non-tzinfo data string.
@@ -3895,19 +3872,6 @@ PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time
All arguments are optional. tzinfo may be None, or an instance of\n\
a tzinfo subclass. The remaining arguments may be ints.\n");
-static PyNumberMethods time_as_number = {
- 0, /* nb_add */
- 0, /* nb_subtract */
- 0, /* nb_multiply */
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- 0, /* nb_negative */
- 0, /* nb_positive */
- 0, /* nb_absolute */
- (inquiry)time_bool, /* nb_bool */
-};
-
static PyTypeObject PyDateTime_TimeType = {
PyVarObject_HEAD_INIT(NULL, 0)
"datetime.time", /* tp_name */
@@ -3919,7 +3883,7 @@ static PyTypeObject PyDateTime_TimeType = {
0, /* tp_setattr */
0, /* tp_reserved */
(reprfunc)time_repr, /* tp_repr */
- &time_as_number, /* tp_as_number */
+ 0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)time_hash, /* tp_hash */
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index f000887..169914c 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -5640,7 +5640,7 @@ PyInit__decimal(void)
goto error; /* GCOV_NOT_REACHED */
}
- ASSIGN_PTR(cm->ex, PyErr_NewException((char *)cm->fqname, base, NULL));
+ ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL));
Py_DECREF(base);
/* add to module */
@@ -5672,7 +5672,7 @@ PyInit__decimal(void)
goto error; /* GCOV_NOT_REACHED */
}
- ASSIGN_PTR(cm->ex, PyErr_NewException((char *)cm->fqname, base, NULL));
+ ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL));
Py_DECREF(base);
Py_INCREF(cm->ex);
diff --git a/Modules/_decimal/docstrings.h b/Modules/_decimal/docstrings.h
index a6490b9..71029a9 100644
--- a/Modules/_decimal/docstrings.h
+++ b/Modules/_decimal/docstrings.h
@@ -19,26 +19,30 @@
PyDoc_STRVAR(doc__decimal,
"C decimal arithmetic module");
-PyDoc_STRVAR(doc_getcontext,"\n\
-getcontext() - Get the current default context.\n\
+PyDoc_STRVAR(doc_getcontext,
+"getcontext($module, /)\n--\n\n\
+Get the current default context.\n\
\n");
-PyDoc_STRVAR(doc_setcontext,"\n\
-setcontext(c) - Set a new default context.\n\
+PyDoc_STRVAR(doc_setcontext,
+"setcontext($module, context, /)\n--\n\n\
+Set a new default context.\n\
\n");
-PyDoc_STRVAR(doc_localcontext,"\n\
-localcontext(ctx=None) - Return a context manager that will set the default\n\
-context to a copy of ctx on entry to the with-statement and restore the\n\
-previous default context when exiting the with-statement. If no context is\n\
-specified, a copy of the current default context is used.\n\
+PyDoc_STRVAR(doc_localcontext,
+"localcontext($module, /, ctx=None)\n--\n\n\
+Return a context manager that will set the default context to a copy of ctx\n\
+on entry to the with-statement and restore the previous default context when\n\
+exiting the with-statement. If no context is specified, a copy of the current\n\
+default context is used.\n\
\n");
#ifdef EXTRA_FUNCTIONALITY
-PyDoc_STRVAR(doc_ieee_context,"\n\
-IEEEContext(bits) - Return a context object initialized to the proper values for\n\
-one of the IEEE interchange formats. The argument must be a multiple of 32 and\n\
-less than IEEE_CONTEXT_MAX_BITS. For the most common values, the constants\n\
+PyDoc_STRVAR(doc_ieee_context,
+"IEEEContext($module, bits, /)\n--\n\n\
+Return a context object initialized to the proper values for one of the\n\
+IEEE interchange formats. The argument must be a multiple of 32 and less\n\
+than IEEE_CONTEXT_MAX_BITS. For the most common values, the constants\n\
DECIMAL32, DECIMAL64 and DECIMAL128 are provided.\n\
\n");
#endif
@@ -48,32 +52,34 @@ DECIMAL32, DECIMAL64 and DECIMAL128 are provided.\n\
/* Decimal Object and Methods */
/******************************************************************************/
-PyDoc_STRVAR(doc_decimal,"\n\
-Decimal(value=\"0\", context=None): Construct a new Decimal object.\n\
-value can be an integer, string, tuple, or another Decimal object.\n\
-If no value is given, return Decimal('0'). The context does not affect\n\
-the conversion and is only passed to determine if the InvalidOperation\n\
-trap is active.\n\
+PyDoc_STRVAR(doc_decimal,
+"Decimal(value=\"0\", context=None)\n--\n\n\
+Construct a new Decimal object. 'value' can be an integer, string, tuple,\n\
+or another Decimal object. If no value is given, return Decimal('0'). The\n\
+context does not affect the conversion and is only passed to determine if\n\
+the InvalidOperation trap is active.\n\
\n");
-PyDoc_STRVAR(doc_adjusted,"\n\
-adjusted() - Return the adjusted exponent of the number.\n\
-\n\
-Defined as exp + digits - 1.\n\
+PyDoc_STRVAR(doc_adjusted,
+"adjusted($self, /)\n--\n\n\
+Return the adjusted exponent of the number. Defined as exp + digits - 1.\n\
\n");
-PyDoc_STRVAR(doc_as_tuple,"\n\
-as_tuple() - Return a tuple representation of the number.\n\
+PyDoc_STRVAR(doc_as_tuple,
+"as_tuple($self, /)\n--\n\n\
+Return a tuple representation of the number.\n\
\n");
-PyDoc_STRVAR(doc_canonical,"\n\
-canonical() - Return the canonical encoding of the argument. Currently,\n\
-the encoding of a Decimal instance is always canonical, so this operation\n\
-returns its argument unchanged.\n\
+PyDoc_STRVAR(doc_canonical,
+"canonical($self, /)\n--\n\n\
+Return the canonical encoding of the argument. Currently, the encoding\n\
+of a Decimal instance is always canonical, so this operation returns its\n\
+argument unchanged.\n\
\n");
-PyDoc_STRVAR(doc_compare,"\n\
-compare(other, context=None) - Compare self to other. Return a decimal value:\n\
+PyDoc_STRVAR(doc_compare,
+"compare($self, /, other, context=None)\n--\n\n\
+Compare self to other. Return a decimal value:\n\
\n\
a or b is a NaN ==> Decimal('NaN')\n\
a < b ==> Decimal('-1')\n\
@@ -81,17 +87,18 @@ compare(other, context=None) - Compare self to other. Return a decimal value:\n\
a > b ==> Decimal('1')\n\
\n");
-PyDoc_STRVAR(doc_compare_signal,"\n\
-compare_signal(other, context=None) - Identical to compare, except that\n\
-all NaNs signal.\n\
+PyDoc_STRVAR(doc_compare_signal,
+"compare_signal($self, /, other, context=None)\n--\n\n\
+Identical to compare, except that all NaNs signal.\n\
\n");
-PyDoc_STRVAR(doc_compare_total,"\n\
-compare_total(other, context=None) - Compare two operands using their\n\
-abstract representation rather than their numerical value. Similar to the\n\
-compare() method, but the result gives a total ordering on Decimal instances.\n\
-Two Decimal instances with the same numeric value but different representations\n\
-compare unequal in this ordering:\n\
+PyDoc_STRVAR(doc_compare_total,
+"compare_total($self, /, other, context=None)\n--\n\n\
+Compare two operands using their abstract representation rather than\n\
+their numerical value. Similar to the compare() method, but the result\n\
+gives a total ordering on Decimal instances. Two Decimal instances with\n\
+the same numeric value but different representations compare unequal\n\
+in this ordering:\n\
\n\
>>> Decimal('12.0').compare_total(Decimal('12'))\n\
Decimal('-1')\n\
@@ -107,36 +114,39 @@ and no rounding is performed. As an exception, the C version may raise\n\
InvalidOperation if the second operand cannot be converted exactly.\n\
\n");
-PyDoc_STRVAR(doc_compare_total_mag,"\n\
-compare_total_mag(other, context=None) - Compare two operands using their\n\
-abstract representation rather than their value as in compare_total(), but\n\
-ignoring the sign of each operand. x.compare_total_mag(y) is equivalent to\n\
-x.copy_abs().compare_total(y.copy_abs()).\n\
+PyDoc_STRVAR(doc_compare_total_mag,
+"compare_total_mag($self, /, other, context=None)\n--\n\n\
+Compare two operands using their abstract representation rather than their\n\
+value as in compare_total(), but ignoring the sign of each operand.\n\
+\n\
+x.compare_total_mag(y) is equivalent to x.copy_abs().compare_total(y.copy_abs()).\n\
\n\
This operation is unaffected by context and is quiet: no flags are changed\n\
and no rounding is performed. As an exception, the C version may raise\n\
InvalidOperation if the second operand cannot be converted exactly.\n\
\n");
-PyDoc_STRVAR(doc_conjugate,"\n\
-conjugate() - Return self.\n\
+PyDoc_STRVAR(doc_conjugate,
+"conjugate($self, /)\n--\n\n\
+Return self.\n\
\n");
-PyDoc_STRVAR(doc_copy_abs,"\n\
-copy_abs() - Return the absolute value of the argument. This operation\n\
-is unaffected by context and is quiet: no flags are changed and no rounding\n\
-is performed.\n\
+PyDoc_STRVAR(doc_copy_abs,
+"copy_abs($self, /)\n--\n\n\
+Return the absolute value of the argument. This operation is unaffected by\n\
+context and is quiet: no flags are changed and no rounding is performed.\n\
\n");
-PyDoc_STRVAR(doc_copy_negate,"\n\
-copy_negate() - Return the negation of the argument. This operation is\n\
-unaffected by context and is quiet: no flags are changed and no rounding\n\
-is performed.\n\
+PyDoc_STRVAR(doc_copy_negate,
+"copy_negate($self, /)\n--\n\n\
+Return the negation of the argument. This operation is unaffected by context\n\
+and is quiet: no flags are changed and no rounding is performed.\n\
\n");
-PyDoc_STRVAR(doc_copy_sign,"\n\
-copy_sign(other, context=None) - Return a copy of the first operand with\n\
-the sign set to be the same as the sign of the second operand. For example:\n\
+PyDoc_STRVAR(doc_copy_sign,
+"copy_sign($self, /, other, context=None)\n--\n\n\
+Return a copy of the first operand with the sign set to be the same as the\n\
+sign of the second operand. For example:\n\
\n\
>>> Decimal('2.3').copy_sign(Decimal('-1.5'))\n\
Decimal('-2.3')\n\
@@ -146,14 +156,16 @@ and no rounding is performed. As an exception, the C version may raise\n\
InvalidOperation if the second operand cannot be converted exactly.\n\
\n");
-PyDoc_STRVAR(doc_exp,"\n\
-exp(context=None) - Return the value of the (natural) exponential function\n\
-e**x at the given number. The function always uses the ROUND_HALF_EVEN mode\n\
-and the result is correctly rounded.\n\
+PyDoc_STRVAR(doc_exp,
+"exp($self, /, context=None)\n--\n\n\
+Return the value of the (natural) exponential function e**x at the given\n\
+number. The function always uses the ROUND_HALF_EVEN mode and the result\n\
+is correctly rounded.\n\
\n");
-PyDoc_STRVAR(doc_from_float,"\n\
-from_float(f) - Class method that converts a float to a decimal number, exactly.\n\
+PyDoc_STRVAR(doc_from_float,
+"from_float($type, f, /)\n--\n\n\
+Class method that converts a float to a decimal number, exactly.\n\
Since 0.1 is not exactly representable in binary floating point,\n\
Decimal.from_float(0.1) is not the same as Decimal('0.1').\n\
\n\
@@ -168,155 +180,176 @@ Decimal.from_float(0.1) is not the same as Decimal('0.1').\n\
\n\
\n");
-PyDoc_STRVAR(doc_fma,"\n\
-fma(other, third, context=None) - Fused multiply-add. Return self*other+third\n\
-with no rounding of the intermediate product self*other.\n\
+PyDoc_STRVAR(doc_fma,
+"fma($self, /, other, third, context=None)\n--\n\n\
+Fused multiply-add. Return self*other+third with no rounding of the\n\
+intermediate product self*other.\n\
\n\
>>> Decimal(2).fma(3, 5)\n\
Decimal('11')\n\
\n\
\n");
-PyDoc_STRVAR(doc_is_canonical,"\n\
-is_canonical() - Return True if the argument is canonical and False otherwise.\n\
-Currently, a Decimal instance is always canonical, so this operation always\n\
-returns True.\n\
+PyDoc_STRVAR(doc_is_canonical,
+"is_canonical($self, /)\n--\n\n\
+Return True if the argument is canonical and False otherwise. Currently,\n\
+a Decimal instance is always canonical, so this operation always returns\n\
+True.\n\
\n");
-PyDoc_STRVAR(doc_is_finite,"\n\
-is_finite() - Return True if the argument is a finite number, and False if the\n\
-argument is infinite or a NaN.\n\
+PyDoc_STRVAR(doc_is_finite,
+"is_finite($self, /)\n--\n\n\
+Return True if the argument is a finite number, and False if the argument\n\
+is infinite or a NaN.\n\
\n");
-PyDoc_STRVAR(doc_is_infinite,"\n\
-is_infinite() - Return True if the argument is either positive or negative\n\
-infinity and False otherwise.\n\
+PyDoc_STRVAR(doc_is_infinite,
+"is_infinite($self, /)\n--\n\n\
+Return True if the argument is either positive or negative infinity and\n\
+False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_is_nan,"\n\
-is_nan() - Return True if the argument is a (quiet or signaling) NaN and\n\
-False otherwise.\n\
+PyDoc_STRVAR(doc_is_nan,
+"is_nan($self, /)\n--\n\n\
+Return True if the argument is a (quiet or signaling) NaN and False\n\
+otherwise.\n\
\n");
-PyDoc_STRVAR(doc_is_normal,"\n\
-is_normal(context=None) - Return True if the argument is a normal finite\n\
-non-zero number with an adjusted exponent greater than or equal to Emin.\n\
-Return False if the argument is zero, subnormal, infinite or a NaN.\n\
+PyDoc_STRVAR(doc_is_normal,
+"is_normal($self, /, context=None)\n--\n\n\
+Return True if the argument is a normal finite non-zero number with an\n\
+adjusted exponent greater than or equal to Emin. Return False if the\n\
+argument is zero, subnormal, infinite or a NaN.\n\
\n");
-PyDoc_STRVAR(doc_is_qnan,"\n\
-is_qnan() - Return True if the argument is a quiet NaN, and False otherwise.\n\
+PyDoc_STRVAR(doc_is_qnan,
+"is_qnan($self, /)\n--\n\n\
+Return True if the argument is a quiet NaN, and False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_is_signed,"\n\
-is_signed() - Return True if the argument has a negative sign and\n\
-False otherwise. Note that both zeros and NaNs can carry signs.\n\
+PyDoc_STRVAR(doc_is_signed,
+"is_signed($self, /)\n--\n\n\
+Return True if the argument has a negative sign and False otherwise.\n\
+Note that both zeros and NaNs can carry signs.\n\
\n");
-PyDoc_STRVAR(doc_is_snan,"\n\
-is_snan() - Return True if the argument is a signaling NaN and False otherwise.\n\
+PyDoc_STRVAR(doc_is_snan,
+"is_snan($self, /)\n--\n\n\
+Return True if the argument is a signaling NaN and False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_is_subnormal,"\n\
-is_subnormal(context=None) - Return True if the argument is subnormal, and\n\
-False otherwise. A number is subnormal if it is non-zero, finite, and has an\n\
-adjusted exponent less than Emin.\n\
+PyDoc_STRVAR(doc_is_subnormal,
+"is_subnormal($self, /, context=None)\n--\n\n\
+Return True if the argument is subnormal, and False otherwise. A number is\n\
+subnormal if it is non-zero, finite, and has an adjusted exponent less\n\
+than Emin.\n\
\n");
-PyDoc_STRVAR(doc_is_zero,"\n\
-is_zero() - Return True if the argument is a (positive or negative) zero and\n\
-False otherwise.\n\
+PyDoc_STRVAR(doc_is_zero,
+"is_zero($self, /)\n--\n\n\
+Return True if the argument is a (positive or negative) zero and False\n\
+otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ln,"\n\
-ln(context=None) - Return the natural (base e) logarithm of the operand.\n\
-The function always uses the ROUND_HALF_EVEN mode and the result is\n\
-correctly rounded.\n\
+PyDoc_STRVAR(doc_ln,
+"ln($self, /, context=None)\n--\n\n\
+Return the natural (base e) logarithm of the operand. The function always\n\
+uses the ROUND_HALF_EVEN mode and the result is correctly rounded.\n\
\n");
-PyDoc_STRVAR(doc_log10,"\n\
-log10(context=None) - Return the base ten logarithm of the operand.\n\
-The function always uses the ROUND_HALF_EVEN mode and the result is\n\
-correctly rounded.\n\
+PyDoc_STRVAR(doc_log10,
+"log10($self, /, context=None)\n--\n\n\
+Return the base ten logarithm of the operand. The function always uses the\n\
+ROUND_HALF_EVEN mode and the result is correctly rounded.\n\
\n");
-PyDoc_STRVAR(doc_logb,"\n\
-logb(context=None) - For a non-zero number, return the adjusted exponent\n\
-of the operand as a Decimal instance. If the operand is a zero, then\n\
-Decimal('-Infinity') is returned and the DivisionByZero condition is\n\
-raised. If the operand is an infinity then Decimal('Infinity') is returned.\n\
+PyDoc_STRVAR(doc_logb,
+"logb($self, /, context=None)\n--\n\n\
+For a non-zero number, return the adjusted exponent of the operand as a\n\
+Decimal instance. If the operand is a zero, then Decimal('-Infinity') is\n\
+returned and the DivisionByZero condition is raised. If the operand is\n\
+an infinity then Decimal('Infinity') is returned.\n\
\n");
-PyDoc_STRVAR(doc_logical_and,"\n\
-logical_and(other, context=None) - Return the digit-wise and of the two\n\
-(logical) operands.\n\
+PyDoc_STRVAR(doc_logical_and,
+"logical_and($self, /, other, context=None)\n--\n\n\
+Return the digit-wise 'and' of the two (logical) operands.\n\
\n");
-PyDoc_STRVAR(doc_logical_invert,"\n\
-logical_invert(context=None) - Return the digit-wise inversion of the\n\
-(logical) operand.\n\
+PyDoc_STRVAR(doc_logical_invert,
+"logical_invert($self, /, context=None)\n--\n\n\
+Return the digit-wise inversion of the (logical) operand.\n\
\n");
-PyDoc_STRVAR(doc_logical_or,"\n\
-logical_or(other, context=None) - Return the digit-wise or of the two\n\
-(logical) operands.\n\
+PyDoc_STRVAR(doc_logical_or,
+"logical_or($self, /, other, context=None)\n--\n\n\
+Return the digit-wise 'or' of the two (logical) operands.\n\
\n");
-PyDoc_STRVAR(doc_logical_xor,"\n\
-logical_xor(other, context=None) - Return the digit-wise exclusive or of the\n\
-two (logical) operands.\n\
+PyDoc_STRVAR(doc_logical_xor,
+"logical_xor($self, /, other, context=None)\n--\n\n\
+Return the digit-wise 'exclusive or' of the two (logical) operands.\n\
\n");
-PyDoc_STRVAR(doc_max,"\n\
-max(other, context=None) - Maximum of self and other. If one operand is a\n\
-quiet NaN and the other is numeric, the numeric operand is returned.\n\
+PyDoc_STRVAR(doc_max,
+"max($self, /, other, context=None)\n--\n\n\
+Maximum of self and other. If one operand is a quiet NaN and the other is\n\
+numeric, the numeric operand is returned.\n\
\n");
-PyDoc_STRVAR(doc_max_mag,"\n\
-max_mag(other, context=None) - Similar to the max() method, but the\n\
-comparison is done using the absolute values of the operands.\n\
+PyDoc_STRVAR(doc_max_mag,
+"max_mag($self, /, other, context=None)\n--\n\n\
+Similar to the max() method, but the comparison is done using the absolute\n\
+values of the operands.\n\
\n");
-PyDoc_STRVAR(doc_min,"\n\
-min(other, context=None) - Minimum of self and other. If one operand is a\n\
-quiet NaN and the other is numeric, the numeric operand is returned.\n\
+PyDoc_STRVAR(doc_min,
+"min($self, /, other, context=None)\n--\n\n\
+Minimum of self and other. If one operand is a quiet NaN and the other is\n\
+numeric, the numeric operand is returned.\n\
\n");
-PyDoc_STRVAR(doc_min_mag,"\n\
-min_mag(other, context=None) - Similar to the min() method, but the\n\
-comparison is done using the absolute values of the operands.\n\
+PyDoc_STRVAR(doc_min_mag,
+"min_mag($self, /, other, context=None)\n--\n\n\
+Similar to the min() method, but the comparison is done using the absolute\n\
+values of the operands.\n\
\n");
-PyDoc_STRVAR(doc_next_minus,"\n\
-next_minus(context=None) - Return the largest number representable in the\n\
-given context (or in the current default context if no context is given) that\n\
-is smaller than the given operand.\n\
+PyDoc_STRVAR(doc_next_minus,
+"next_minus($self, /, context=None)\n--\n\n\
+Return the largest number representable in the given context (or in the\n\
+current default context if no context is given) that is smaller than the\n\
+given operand.\n\
\n");
-PyDoc_STRVAR(doc_next_plus,"\n\
-next_plus(context=None) - Return the smallest number representable in the\n\
-given context (or in the current default context if no context is given) that\n\
-is larger than the given operand.\n\
+PyDoc_STRVAR(doc_next_plus,
+"next_plus($self, /, context=None)\n--\n\n\
+Return the smallest number representable in the given context (or in the\n\
+current default context if no context is given) that is larger than the\n\
+given operand.\n\
\n");
-PyDoc_STRVAR(doc_next_toward,"\n\
-next_toward(other, context=None) - If the two operands are unequal, return\n\
-the number closest to the first operand in the direction of the second operand.\n\
-If both operands are numerically equal, return a copy of the first operand\n\
-with the sign set to be the same as the sign of the second operand.\n\
+PyDoc_STRVAR(doc_next_toward,
+"next_toward($self, /, other, context=None)\n--\n\n\
+If the two operands are unequal, return the number closest to the first\n\
+operand in the direction of the second operand. If both operands are\n\
+numerically equal, return a copy of the first operand with the sign set\n\
+to be the same as the sign of the second operand.\n\
\n");
-PyDoc_STRVAR(doc_normalize,"\n\
-normalize(context=None) - Normalize the number by stripping the rightmost\n\
-trailing zeros and converting any result equal to Decimal('0') to Decimal('0e0').\n\
-Used for producing canonical values for members of an equivalence class. For\n\
-example, Decimal('32.100') and Decimal('0.321000e+2') both normalize to the\n\
-equivalent value Decimal('32.1').\n\
+PyDoc_STRVAR(doc_normalize,
+"normalize($self, /, context=None)\n--\n\n\
+Normalize the number by stripping the rightmost trailing zeros and\n\
+converting any result equal to Decimal('0') to Decimal('0e0'). Used\n\
+for producing canonical values for members of an equivalence class.\n\
+For example, Decimal('32.100') and Decimal('0.321000e+2') both normalize\n\
+to the equivalent value Decimal('32.1').\n\
\n");
-PyDoc_STRVAR(doc_number_class,"\n\
-number_class(context=None) - Return a string describing the class of the\n\
-operand. The returned value is one of the following ten strings:\n\
+PyDoc_STRVAR(doc_number_class,
+"number_class($self, /, context=None)\n--\n\n\
+Return a string describing the class of the operand. The returned value\n\
+is one of the following ten strings:\n\
\n\
* '-Infinity', indicating that the operand is negative infinity.\n\
* '-Normal', indicating that the operand is a negative normal number.\n\
@@ -331,9 +364,10 @@ operand. The returned value is one of the following ten strings:\n\
\n\
\n");
-PyDoc_STRVAR(doc_quantize,"\n\
-quantize(exp, rounding=None, context=None) - Return a value equal to the\n\
-first operand after rounding and having the exponent of the second operand.\n\
+PyDoc_STRVAR(doc_quantize,
+"quantize($self, /, exp, rounding=None, context=None)\n--\n\n\
+Return a value equal to the first operand after rounding and having the\n\
+exponent of the second operand.\n\
\n\
>>> Decimal('1.41421356').quantize(Decimal('1.000'))\n\
Decimal('1.414')\n\
@@ -352,93 +386,98 @@ rounding argument if given, else by the given context argument; if neither\n\
argument is given, the rounding mode of the current thread's context is used.\n\
\n");
-PyDoc_STRVAR(doc_radix,"\n\
-radix() - Return Decimal(10), the radix (base) in which the Decimal class does\n\
+PyDoc_STRVAR(doc_radix,
+"radix($self, /)\n--\n\n\
+Return Decimal(10), the radix (base) in which the Decimal class does\n\
all its arithmetic. Included for compatibility with the specification.\n\
\n");
-PyDoc_STRVAR(doc_remainder_near,"\n\
-remainder_near(other, context=None) - Return the remainder from dividing\n\
-self by other. This differs from self % other in that the sign of the\n\
-remainder is chosen so as to minimize its absolute value. More precisely, the\n\
-return value is self - n * other where n is the integer nearest to the exact\n\
-value of self / other, and if two integers are equally near then the even one\n\
-is chosen.\n\
+PyDoc_STRVAR(doc_remainder_near,
+"remainder_near($self, /, other, context=None)\n--\n\n\
+Return the remainder from dividing self by other. This differs from\n\
+self % other in that the sign of the remainder is chosen so as to minimize\n\
+its absolute value. More precisely, the return value is self - n * other\n\
+where n is the integer nearest to the exact value of self / other, and\n\
+if two integers are equally near then the even one is chosen.\n\
\n\
If the result is zero then its sign will be the sign of self.\n\
\n");
-PyDoc_STRVAR(doc_rotate,"\n\
-rotate(other, context=None) - Return the result of rotating the digits of the\n\
-first operand by an amount specified by the second operand. The second operand\n\
-must be an integer in the range -precision through precision. The absolute\n\
-value of the second operand gives the number of places to rotate. If the second\n\
-operand is positive then rotation is to the left; otherwise rotation is to the\n\
-right. The coefficient of the first operand is padded on the left with zeros to\n\
+PyDoc_STRVAR(doc_rotate,
+"rotate($self, /, other, context=None)\n--\n\n\
+Return the result of rotating the digits of the first operand by an amount\n\
+specified by the second operand. The second operand must be an integer in\n\
+the range -precision through precision. The absolute value of the second\n\
+operand gives the number of places to rotate. If the second operand is\n\
+positive then rotation is to the left; otherwise rotation is to the right.\n\
+The coefficient of the first operand is padded on the left with zeros to\n\
length precision if necessary. The sign and exponent of the first operand are\n\
unchanged.\n\
\n");
-PyDoc_STRVAR(doc_same_quantum,"\n\
-same_quantum(other, context=None) - Test whether self and other have the\n\
-same exponent or whether both are NaN.\n\
+PyDoc_STRVAR(doc_same_quantum,
+"same_quantum($self, /, other, context=None)\n--\n\n\
+Test whether self and other have the same exponent or whether both are NaN.\n\
\n\
This operation is unaffected by context and is quiet: no flags are changed\n\
and no rounding is performed. As an exception, the C version may raise\n\
InvalidOperation if the second operand cannot be converted exactly.\n\
\n");
-PyDoc_STRVAR(doc_scaleb,"\n\
-scaleb(other, context=None) - Return the first operand with the exponent\n\
-adjusted the second. Equivalently, return the first operand multiplied by\n\
-10**other. The second operand must be an integer.\n\
+PyDoc_STRVAR(doc_scaleb,
+"scaleb($self, /, other, context=None)\n--\n\n\
+Return the first operand with the exponent adjusted the second. Equivalently,\n\
+return the first operand multiplied by 10**other. The second operand must be\n\
+an integer.\n\
\n");
-PyDoc_STRVAR(doc_shift,"\n\
-shift(other, context=None) - Return the result of shifting the digits of\n\
-the first operand by an amount specified by the second operand. The second\n\
-operand must be an integer in the range -precision through precision. The\n\
-absolute value of the second operand gives the number of places to shift.\n\
-If the second operand is positive, then the shift is to the left; otherwise\n\
-the shift is to the right. Digits shifted into the coefficient are zeros.\n\
-The sign and exponent of the first operand are unchanged.\n\
+PyDoc_STRVAR(doc_shift,
+"shift($self, /, other, context=None)\n--\n\n\
+Return the result of shifting the digits of the first operand by an amount\n\
+specified by the second operand. The second operand must be an integer in\n\
+the range -precision through precision. The absolute value of the second\n\
+operand gives the number of places to shift. If the second operand is\n\
+positive, then the shift is to the left; otherwise the shift is to the\n\
+right. Digits shifted into the coefficient are zeros. The sign and exponent\n\
+of the first operand are unchanged.\n\
\n");
-PyDoc_STRVAR(doc_sqrt,"\n\
-sqrt(context=None) - Return the square root of the argument to full precision.\n\
-The result is correctly rounded using the ROUND_HALF_EVEN rounding mode.\n\
+PyDoc_STRVAR(doc_sqrt,
+"sqrt($self, /, context=None)\n--\n\n\
+Return the square root of the argument to full precision. The result is\n\
+correctly rounded using the ROUND_HALF_EVEN rounding mode.\n\
\n");
-PyDoc_STRVAR(doc_to_eng_string,"\n\
-to_eng_string(context=None) - Convert to an engineering-type string.\n\
-Engineering notation has an exponent which is a multiple of 3, so there\n\
-are up to 3 digits left of the decimal place. For example, Decimal('123E+1')\n\
-is converted to Decimal('1.23E+3').\n\
+PyDoc_STRVAR(doc_to_eng_string,
+"to_eng_string($self, /, context=None)\n--\n\n\
+Convert to an engineering-type string. Engineering notation has an exponent\n\
+which is a multiple of 3, so there are up to 3 digits left of the decimal\n\
+place. For example, Decimal('123E+1') is converted to Decimal('1.23E+3').\n\
\n\
The value of context.capitals determines whether the exponent sign is lower\n\
or upper case. Otherwise, the context does not affect the operation.\n\
\n");
-PyDoc_STRVAR(doc_to_integral,"\n\
-to_integral(rounding=None, context=None) - Identical to the\n\
-to_integral_value() method. The to_integral() name has been kept\n\
-for compatibility with older versions.\n\
+PyDoc_STRVAR(doc_to_integral,
+"to_integral($self, /, rounding=None, context=None)\n--\n\n\
+Identical to the to_integral_value() method. The to_integral() name has been\n\
+kept for compatibility with older versions.\n\
\n");
-PyDoc_STRVAR(doc_to_integral_exact,"\n\
-to_integral_exact(rounding=None, context=None) - Round to the nearest\n\
-integer, signaling Inexact or Rounded as appropriate if rounding occurs.\n\
-The rounding mode is determined by the rounding parameter if given, else\n\
-by the given context. If neither parameter is given, then the rounding mode\n\
-of the current default context is used.\n\
+PyDoc_STRVAR(doc_to_integral_exact,
+"to_integral_exact($self, /, rounding=None, context=None)\n--\n\n\
+Round to the nearest integer, signaling Inexact or Rounded as appropriate if\n\
+rounding occurs. The rounding mode is determined by the rounding parameter\n\
+if given, else by the given context. If neither parameter is given, then the\n\
+rounding mode of the current default context is used.\n\
\n");
-PyDoc_STRVAR(doc_to_integral_value,"\n\
-to_integral_value(rounding=None, context=None) - Round to the nearest\n\
-integer without signaling Inexact or Rounded. The rounding mode is determined\n\
-by the rounding parameter if given, else by the given context. If neither\n\
-parameter is given, then the rounding mode of the current default context is\n\
-used.\n\
+PyDoc_STRVAR(doc_to_integral_value,
+"to_integral_value($self, /, rounding=None, context=None)\n--\n\n\
+Round to the nearest integer without signaling Inexact or Rounded. The\n\
+rounding mode is determined by the rounding parameter if given, else by\n\
+the given context. If neither parameter is given, then the rounding mode\n\
+of the current default context is used.\n\
\n");
@@ -446,9 +485,10 @@ used.\n\
/* Context Object and Methods */
/******************************************************************************/
-PyDoc_STRVAR(doc_context,"\n\
+PyDoc_STRVAR(doc_context,
+"Context(prec=None, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None)\n--\n\n\
The context affects almost all operations and controls rounding,\n\
-Over/Underflow, raising of exceptions and much more. A new context\n\
+Over/Underflow, raising of exceptions and much more. A new context\n\
can be constructed as follows:\n\
\n\
>>> c = Context(prec=28, Emin=-425000000, Emax=425000000,\n\
@@ -460,308 +500,372 @@ can be constructed as follows:\n\
\n");
#ifdef EXTRA_FUNCTIONALITY
-PyDoc_STRVAR(doc_ctx_apply,"\n\
-apply(x) - Apply self to Decimal x.\n\
+PyDoc_STRVAR(doc_ctx_apply,
+"apply($self, x, /)\n--\n\n\
+Apply self to Decimal x.\n\
\n");
#endif
-PyDoc_STRVAR(doc_ctx_clear_flags,"\n\
-clear_flags() - Reset all flags to False.\n\
+PyDoc_STRVAR(doc_ctx_clear_flags,
+"clear_flags($self, /)\n--\n\n\
+Reset all flags to False.\n\
\n");
-PyDoc_STRVAR(doc_ctx_clear_traps,"\n\
-clear_traps() - Set all traps to False.\n\
+PyDoc_STRVAR(doc_ctx_clear_traps,
+"clear_traps($self, /)\n--\n\n\
+Set all traps to False.\n\
\n");
-PyDoc_STRVAR(doc_ctx_copy,"\n\
-copy() - Return a duplicate of the context with all flags cleared.\n\
+PyDoc_STRVAR(doc_ctx_copy,
+"copy($self, /)\n--\n\n\
+Return a duplicate of the context with all flags cleared.\n\
\n");
-PyDoc_STRVAR(doc_ctx_copy_decimal,"\n\
-copy_decimal(x) - Return a copy of Decimal x.\n\
+PyDoc_STRVAR(doc_ctx_copy_decimal,
+"copy_decimal($self, x, /)\n--\n\n\
+Return a copy of Decimal x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_create_decimal,"\n\
-create_decimal(x) - Create a new Decimal instance from x, using self as the\n\
-context. Unlike the Decimal constructor, this function observes the context\n\
-limits.\n\
+PyDoc_STRVAR(doc_ctx_create_decimal,
+"create_decimal($self, num=\"0\", /)\n--\n\n\
+Create a new Decimal instance from num, using self as the context. Unlike the\n\
+Decimal constructor, this function observes the context limits.\n\
\n");
-PyDoc_STRVAR(doc_ctx_create_decimal_from_float,"\n\
-create_decimal_from_float(f) - Create a new Decimal instance from float f.\n\
-Unlike the Decimal.from_float() class method, this function observes the\n\
-context limits.\n\
+PyDoc_STRVAR(doc_ctx_create_decimal_from_float,
+"create_decimal_from_float($self, f, /)\n--\n\n\
+Create a new Decimal instance from float f. Unlike the Decimal.from_float()\n\
+class method, this function observes the context limits.\n\
\n");
-PyDoc_STRVAR(doc_ctx_Etiny,"\n\
-Etiny() - Return a value equal to Emin - prec + 1, which is the minimum\n\
-exponent value for subnormal results. When underflow occurs, the exponent\n\
-is set to Etiny.\n\
+PyDoc_STRVAR(doc_ctx_Etiny,
+"Etiny($self, /)\n--\n\n\
+Return a value equal to Emin - prec + 1, which is the minimum exponent value\n\
+for subnormal results. When underflow occurs, the exponent is set to Etiny.\n\
\n");
-PyDoc_STRVAR(doc_ctx_Etop,"\n\
-Etop() - Return a value equal to Emax - prec + 1. This is the maximum exponent\n\
-if the _clamp field of the context is set to 1 (IEEE clamp mode). Etop() must\n\
-not be negative.\n\
+PyDoc_STRVAR(doc_ctx_Etop,
+"Etop($self, /)\n--\n\n\
+Return a value equal to Emax - prec + 1. This is the maximum exponent\n\
+if the _clamp field of the context is set to 1 (IEEE clamp mode). Etop()\n\
+must not be negative.\n\
\n");
-PyDoc_STRVAR(doc_ctx_abs,"\n\
-abs(x) - Return the absolute value of x.\n\
+PyDoc_STRVAR(doc_ctx_abs,
+"abs($self, x, /)\n--\n\n\
+Return the absolute value of x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_add,"\n\
-add(x, y) - Return the sum of x and y.\n\
+PyDoc_STRVAR(doc_ctx_add,
+"add($self, x, y, /)\n--\n\n\
+Return the sum of x and y.\n\
\n");
-PyDoc_STRVAR(doc_ctx_canonical,"\n\
-canonical(x) - Return a new instance of x.\n\
+PyDoc_STRVAR(doc_ctx_canonical,
+"canonical($self, x, /)\n--\n\n\
+Return a new instance of x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_compare,"\n\
-compare(x, y) - Compare x and y numerically.\n\
+PyDoc_STRVAR(doc_ctx_compare,
+"compare($self, x, y, /)\n--\n\n\
+Compare x and y numerically.\n\
\n");
-PyDoc_STRVAR(doc_ctx_compare_signal,"\n\
-compare_signal(x, y) - Compare x and y numerically. All NaNs signal.\n\
+PyDoc_STRVAR(doc_ctx_compare_signal,
+"compare_signal($self, x, y, /)\n--\n\n\
+Compare x and y numerically. All NaNs signal.\n\
\n");
-PyDoc_STRVAR(doc_ctx_compare_total,"\n\
-compare_total(x, y) - Compare x and y using their abstract representation.\n\
+PyDoc_STRVAR(doc_ctx_compare_total,
+"compare_total($self, x, y, /)\n--\n\n\
+Compare x and y using their abstract representation.\n\
\n");
-PyDoc_STRVAR(doc_ctx_compare_total_mag,"\n\
-compare_total_mag(x, y) - Compare x and y using their abstract representation,\n\
-ignoring sign.\n\
+PyDoc_STRVAR(doc_ctx_compare_total_mag,
+"compare_total_mag($self, x, y, /)\n--\n\n\
+Compare x and y using their abstract representation, ignoring sign.\n\
\n");
-PyDoc_STRVAR(doc_ctx_copy_abs,"\n\
-copy_abs(x) - Return a copy of x with the sign set to 0.\n\
+PyDoc_STRVAR(doc_ctx_copy_abs,
+"copy_abs($self, x, /)\n--\n\n\
+Return a copy of x with the sign set to 0.\n\
\n");
-PyDoc_STRVAR(doc_ctx_copy_negate,"\n\
-copy_negate(x) - Return a copy of x with the sign inverted.\n\
+PyDoc_STRVAR(doc_ctx_copy_negate,
+"copy_negate($self, x, /)\n--\n\n\
+Return a copy of x with the sign inverted.\n\
\n");
-PyDoc_STRVAR(doc_ctx_copy_sign,"\n\
-copy_sign(x, y) - Copy the sign from y to x.\n\
+PyDoc_STRVAR(doc_ctx_copy_sign,
+"copy_sign($self, x, y, /)\n--\n\n\
+Copy the sign from y to x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_divide,"\n\
-divide(x, y) - Return x divided by y.\n\
+PyDoc_STRVAR(doc_ctx_divide,
+"divide($self, x, y, /)\n--\n\n\
+Return x divided by y.\n\
\n");
-PyDoc_STRVAR(doc_ctx_divide_int,"\n\
-divide_int(x, y) - Return x divided by y, truncated to an integer.\n\
+PyDoc_STRVAR(doc_ctx_divide_int,
+"divide_int($self, x, y, /)\n--\n\n\
+Return x divided by y, truncated to an integer.\n\
\n");
-PyDoc_STRVAR(doc_ctx_divmod,"\n\
-divmod(x, y) - Return quotient and remainder of the division x / y.\n\
+PyDoc_STRVAR(doc_ctx_divmod,
+"divmod($self, x, y, /)\n--\n\n\
+Return quotient and remainder of the division x / y.\n\
\n");
-PyDoc_STRVAR(doc_ctx_exp,"\n\
-exp(x) - Return e ** x.\n\
+PyDoc_STRVAR(doc_ctx_exp,
+"exp($self, x, /)\n--\n\n\
+Return e ** x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_fma,"\n\
-fma(x, y, z) - Return x multiplied by y, plus z.\n\
+PyDoc_STRVAR(doc_ctx_fma,
+"fma($self, x, y, z, /)\n--\n\n\
+Return x multiplied by y, plus z.\n\
\n");
-PyDoc_STRVAR(doc_ctx_is_canonical,"\n\
-is_canonical(x) - Return True if x is canonical, False otherwise.\n\
+PyDoc_STRVAR(doc_ctx_is_canonical,
+"is_canonical($self, x, /)\n--\n\n\
+Return True if x is canonical, False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ctx_is_finite,"\n\
-is_finite(x) - Return True if x is finite, False otherwise.\n\
+PyDoc_STRVAR(doc_ctx_is_finite,
+"is_finite($self, x, /)\n--\n\n\
+Return True if x is finite, False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ctx_is_infinite,"\n\
-is_infinite(x) - Return True if x is infinite, False otherwise.\n\
+PyDoc_STRVAR(doc_ctx_is_infinite,
+"is_infinite($self, x, /)\n--\n\n\
+Return True if x is infinite, False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ctx_is_nan,"\n\
-is_nan(x) - Return True if x is a qNaN or sNaN, False otherwise.\n\
+PyDoc_STRVAR(doc_ctx_is_nan,
+"is_nan($self, x, /)\n--\n\n\
+Return True if x is a qNaN or sNaN, False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ctx_is_normal,"\n\
-is_normal(x) - Return True if x is a normal number, False otherwise.\n\
+PyDoc_STRVAR(doc_ctx_is_normal,
+"is_normal($self, x, /)\n--\n\n\
+Return True if x is a normal number, False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ctx_is_qnan,"\n\
-is_qnan(x) - Return True if x is a quiet NaN, False otherwise.\n\
+PyDoc_STRVAR(doc_ctx_is_qnan,
+"is_qnan($self, x, /)\n--\n\n\
+Return True if x is a quiet NaN, False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ctx_is_signed,"\n\
-is_signed(x) - Return True if x is negative, False otherwise.\n\
+PyDoc_STRVAR(doc_ctx_is_signed,
+"is_signed($self, x, /)\n--\n\n\
+Return True if x is negative, False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ctx_is_snan,"\n\
-is_snan() - Return True if x is a signaling NaN, False otherwise.\n\
+PyDoc_STRVAR(doc_ctx_is_snan,
+"is_snan($self, x, /)\n--\n\n\
+Return True if x is a signaling NaN, False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ctx_is_subnormal,"\n\
-is_subnormal(x) - Return True if x is subnormal, False otherwise.\n\
+PyDoc_STRVAR(doc_ctx_is_subnormal,
+"is_subnormal($self, x, /)\n--\n\n\
+Return True if x is subnormal, False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ctx_is_zero,"\n\
-is_zero(x) - Return True if x is a zero, False otherwise.\n\
+PyDoc_STRVAR(doc_ctx_is_zero,
+"is_zero($self, x, /)\n--\n\n\
+Return True if x is a zero, False otherwise.\n\
\n");
-PyDoc_STRVAR(doc_ctx_ln,"\n\
-ln(x) - Return the natural (base e) logarithm of x.\n\
+PyDoc_STRVAR(doc_ctx_ln,
+"ln($self, x, /)\n--\n\n\
+Return the natural (base e) logarithm of x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_log10,"\n\
-log10(x) - Return the base 10 logarithm of x.\n\
+PyDoc_STRVAR(doc_ctx_log10,
+"log10($self, x, /)\n--\n\n\
+Return the base 10 logarithm of x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_logb,"\n\
-logb(x) - Return the exponent of the magnitude of the operand's MSD.\n\
+PyDoc_STRVAR(doc_ctx_logb,
+"logb($self, x, /)\n--\n\n\
+Return the exponent of the magnitude of the operand's MSD.\n\
\n");
-PyDoc_STRVAR(doc_ctx_logical_and,"\n\
-logical_and(x, y) - Digit-wise and of x and y.\n\
+PyDoc_STRVAR(doc_ctx_logical_and,
+"logical_and($self, x, y, /)\n--\n\n\
+Digit-wise and of x and y.\n\
\n");
-PyDoc_STRVAR(doc_ctx_logical_invert,"\n\
-logical_invert(x) - Invert all digits of x.\n\
+PyDoc_STRVAR(doc_ctx_logical_invert,
+"logical_invert($self, x, /)\n--\n\n\
+Invert all digits of x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_logical_or,"\n\
-logical_or(x, y) - Digit-wise or of x and y.\n\
+PyDoc_STRVAR(doc_ctx_logical_or,
+"logical_or($self, x, y, /)\n--\n\n\
+Digit-wise or of x and y.\n\
\n");
-PyDoc_STRVAR(doc_ctx_logical_xor,"\n\
-logical_xor(x, y) - Digit-wise xor of x and y.\n\
+PyDoc_STRVAR(doc_ctx_logical_xor,
+"logical_xor($self, x, y, /)\n--\n\n\
+Digit-wise xor of x and y.\n\
\n");
-PyDoc_STRVAR(doc_ctx_max,"\n\
-max(x, y) - Compare the values numerically and return the maximum.\n\
+PyDoc_STRVAR(doc_ctx_max,
+"max($self, x, y, /)\n--\n\n\
+Compare the values numerically and return the maximum.\n\
\n");
-PyDoc_STRVAR(doc_ctx_max_mag,"\n\
-max_mag(x, y) - Compare the values numerically with their sign ignored.\n\
+PyDoc_STRVAR(doc_ctx_max_mag,
+"max_mag($self, x, y, /)\n--\n\n\
+Compare the values numerically with their sign ignored.\n\
\n");
-PyDoc_STRVAR(doc_ctx_min,"\n\
-min(x, y) - Compare the values numerically and return the minimum.\n\
+PyDoc_STRVAR(doc_ctx_min,
+"min($self, x, y, /)\n--\n\n\
+Compare the values numerically and return the minimum.\n\
\n");
-PyDoc_STRVAR(doc_ctx_min_mag,"\n\
-min_mag(x, y) - Compare the values numerically with their sign ignored.\n\
+PyDoc_STRVAR(doc_ctx_min_mag,
+"min_mag($self, x, y, /)\n--\n\n\
+Compare the values numerically with their sign ignored.\n\
\n");
-PyDoc_STRVAR(doc_ctx_minus,"\n\
-minus(x) - Minus corresponds to the unary prefix minus operator in Python,\n\
-but applies the context to the result.\n\
+PyDoc_STRVAR(doc_ctx_minus,
+"minus($self, x, /)\n--\n\n\
+Minus corresponds to the unary prefix minus operator in Python, but applies\n\
+the context to the result.\n\
\n");
-PyDoc_STRVAR(doc_ctx_multiply,"\n\
-multiply(x, y) - Return the product of x and y.\n\
+PyDoc_STRVAR(doc_ctx_multiply,
+"multiply($self, x, y, /)\n--\n\n\
+Return the product of x and y.\n\
\n");
-PyDoc_STRVAR(doc_ctx_next_minus,"\n\
-next_minus(x) - Return the largest representable number smaller than x.\n\
+PyDoc_STRVAR(doc_ctx_next_minus,
+"next_minus($self, x, /)\n--\n\n\
+Return the largest representable number smaller than x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_next_plus,"\n\
-next_plus(x) - Return the smallest representable number larger than x.\n\
+PyDoc_STRVAR(doc_ctx_next_plus,
+"next_plus($self, x, /)\n--\n\n\
+Return the smallest representable number larger than x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_next_toward,"\n\
-next_toward(x) - Return the number closest to x, in the direction towards y.\n\
+PyDoc_STRVAR(doc_ctx_next_toward,
+"next_toward($self, x, y, /)\n--\n\n\
+Return the number closest to x, in the direction towards y.\n\
\n");
-PyDoc_STRVAR(doc_ctx_normalize,"\n\
-normalize(x) - Reduce x to its simplest form. Alias for reduce(x).\n\
+PyDoc_STRVAR(doc_ctx_normalize,
+"normalize($self, x, /)\n--\n\n\
+Reduce x to its simplest form. Alias for reduce(x).\n\
\n");
-PyDoc_STRVAR(doc_ctx_number_class,"\n\
-number_class(x) - Return an indication of the class of x.\n\
+PyDoc_STRVAR(doc_ctx_number_class,
+"number_class($self, x, /)\n--\n\n\
+Return an indication of the class of x.\n\
\n");
-PyDoc_STRVAR(doc_ctx_plus,"\n\
-plus(x) - Plus corresponds to the unary prefix plus operator in Python,\n\
-but applies the context to the result.\n\
+PyDoc_STRVAR(doc_ctx_plus,
+"plus($self, x, /)\n--\n\n\
+Plus corresponds to the unary prefix plus operator in Python, but applies\n\
+the context to the result.\n\
\n");
-PyDoc_STRVAR(doc_ctx_power,"\n\
-power(x, y) - Compute x**y. If x is negative, then y must be integral.\n\
-The result will be inexact unless y is integral and the result is finite\n\
-and can be expressed exactly in 'precision' digits. In the Python version\n\
-the result is always correctly rounded, in the C version the result is\n\
-almost always correctly rounded.\n\
+PyDoc_STRVAR(doc_ctx_power,
+"power($self, /, a, b, modulo=None)\n--\n\n\
+Compute a**b. If 'a' is negative, then 'b' must be integral. The result\n\
+will be inexact unless 'a' is integral and the result is finite and can\n\
+be expressed exactly in 'precision' digits. In the Python version the\n\
+result is always correctly rounded, in the C version the result is almost\n\
+always correctly rounded.\n\
\n\
-power(x, y, m) - Compute (x**y) % m. The following restrictions hold:\n\
+If modulo is given, compute (a**b) % modulo. The following restrictions\n\
+hold:\n\
\n\
* all three arguments must be integral\n\
- * y must be nonnegative\n\
- * at least one of x or y must be nonzero\n\
- * m must be nonzero and less than 10**prec in absolute value\n\
+ * 'b' must be nonnegative\n\
+ * at least one of 'a' or 'b' must be nonzero\n\
+ * modulo must be nonzero and less than 10**prec in absolute value\n\
\n\
\n");
-PyDoc_STRVAR(doc_ctx_quantize,"\n\
-quantize(x, y) - Return a value equal to x (rounded), having the exponent of y.\n\
+PyDoc_STRVAR(doc_ctx_quantize,
+"quantize($self, x, y, /)\n--\n\n\
+Return a value equal to x (rounded), having the exponent of y.\n\
\n");
-PyDoc_STRVAR(doc_ctx_radix,"\n\
-radix() - Return 10.\n\
+PyDoc_STRVAR(doc_ctx_radix,
+"radix($self, /)\n--\n\n\
+Return 10.\n\
\n");
-PyDoc_STRVAR(doc_ctx_remainder,"\n\
-remainder(x, y) - Return the remainder from integer division. The sign of\n\
-the result, if non-zero, is the same as that of the original dividend.\n\
+PyDoc_STRVAR(doc_ctx_remainder,
+"remainder($self, x, y, /)\n--\n\n\
+Return the remainder from integer division. The sign of the result,\n\
+if non-zero, is the same as that of the original dividend.\n\
\n");
-PyDoc_STRVAR(doc_ctx_remainder_near,"\n\
-remainder_near(x, y) - Return x - y * n, where n is the integer nearest the\n\
-exact value of x / y (if the result is 0 then its sign will be the sign of x).\n\
+PyDoc_STRVAR(doc_ctx_remainder_near,
+"remainder_near($self, x, y, /)\n--\n\n\
+Return x - y * n, where n is the integer nearest the exact value of x / y\n\
+(if the result is 0 then its sign will be the sign of x).\n\
\n");
-PyDoc_STRVAR(doc_ctx_rotate,"\n\
-rotate(x, y) - Return a copy of x, rotated by y places.\n\
+PyDoc_STRVAR(doc_ctx_rotate,
+"rotate($self, x, y, /)\n--\n\n\
+Return a copy of x, rotated by y places.\n\
\n");
-PyDoc_STRVAR(doc_ctx_same_quantum,"\n\
-same_quantum(x, y) - Return True if the two operands have the same exponent.\n\
+PyDoc_STRVAR(doc_ctx_same_quantum,
+"same_quantum($self, x, y, /)\n--\n\n\
+Return True if the two operands have the same exponent.\n\
\n");
-PyDoc_STRVAR(doc_ctx_scaleb,"\n\
-scaleb(x, y) - Return the first operand after adding the second value\n\
-to its exp.\n\
+PyDoc_STRVAR(doc_ctx_scaleb,
+"scaleb($self, x, y, /)\n--\n\n\
+Return the first operand after adding the second value to its exp.\n\
\n");
-PyDoc_STRVAR(doc_ctx_shift,"\n\
-shift(x, y) - Return a copy of x, shifted by y places.\n\
+PyDoc_STRVAR(doc_ctx_shift,
+"shift($self, x, y, /)\n--\n\n\
+Return a copy of x, shifted by y places.\n\
\n");
-PyDoc_STRVAR(doc_ctx_sqrt,"\n\
-sqrt(x) - Square root of a non-negative number to context precision.\n\
+PyDoc_STRVAR(doc_ctx_sqrt,
+"sqrt($self, x, /)\n--\n\n\
+Square root of a non-negative number to context precision.\n\
\n");
-PyDoc_STRVAR(doc_ctx_subtract,"\n\
-subtract(x, y) - Return the difference between x and y.\n\
+PyDoc_STRVAR(doc_ctx_subtract,
+"subtract($self, x, y, /)\n--\n\n\
+Return the difference between x and y.\n\
\n");
-PyDoc_STRVAR(doc_ctx_to_eng_string,"\n\
-to_eng_string(x) - Convert a number to a string, using engineering notation.\n\
+PyDoc_STRVAR(doc_ctx_to_eng_string,
+"to_eng_string($self, x, /)\n--\n\n\
+Convert a number to a string, using engineering notation.\n\
\n");
-PyDoc_STRVAR(doc_ctx_to_integral,"\n\
-to_integral(x) - Identical to to_integral_value(x).\n\
+PyDoc_STRVAR(doc_ctx_to_integral,
+"to_integral($self, x, /)\n--\n\n\
+Identical to to_integral_value(x).\n\
\n");
-PyDoc_STRVAR(doc_ctx_to_integral_exact,"\n\
-to_integral_exact(x) - Round to an integer. Signal if the result is\n\
-rounded or inexact.\n\
+PyDoc_STRVAR(doc_ctx_to_integral_exact,
+"to_integral_exact($self, x, /)\n--\n\n\
+Round to an integer. Signal if the result is rounded or inexact.\n\
\n");
-PyDoc_STRVAR(doc_ctx_to_integral_value,"\n\
-to_integral_value(x) - Round to an integer.\n\
+PyDoc_STRVAR(doc_ctx_to_integral_value,
+"to_integral_value($self, x, /)\n--\n\n\
+Round to an integer.\n\
\n");
-PyDoc_STRVAR(doc_ctx_to_sci_string,"\n\
-to_sci_string(x) - Convert a number to a string using scientific notation.\n\
+PyDoc_STRVAR(doc_ctx_to_sci_string,
+"to_sci_string($self, x, /)\n--\n\n\
+Convert a number to a string using scientific notation.\n\
\n");
diff --git a/Modules/_decimal/tests/deccheck.py b/Modules/_decimal/tests/deccheck.py
index c4c5a44..89433c0 100644
--- a/Modules/_decimal/tests/deccheck.py
+++ b/Modules/_decimal/tests/deccheck.py
@@ -36,6 +36,7 @@ from test.support import import_fresh_module
from randdec import randfloat, all_unary, all_binary, all_ternary
from randdec import unary_optarg, binary_optarg, ternary_optarg
from formathelper import rand_format, rand_locale
+from _pydecimal import _dec_from_triple
C = import_fresh_module('decimal', fresh=['_decimal'])
P = import_fresh_module('decimal', blocked=['_decimal'])
@@ -370,7 +371,7 @@ class SkipHandler:
return abs(a - b)
def standard_ulp(self, dec, prec):
- return P._dec_from_triple(0, '1', dec._exp+len(dec._int)-prec)
+ return _dec_from_triple(0, '1', dec._exp+len(dec._int)-prec)
def rounding_direction(self, x, mode):
"""Determine the effective direction of the rounding when
@@ -401,10 +402,10 @@ class SkipHandler:
# Convert infinities to the largest representable number + 1.
x = exact
if exact.is_infinite():
- x = P._dec_from_triple(exact._sign, '10', context.p.Emax)
+ x = _dec_from_triple(exact._sign, '10', context.p.Emax)
y = rounded
if rounded.is_infinite():
- y = P._dec_from_triple(rounded._sign, '10', context.p.Emax)
+ y = _dec_from_triple(rounded._sign, '10', context.p.Emax)
# err = (rounded - exact) / ulp(rounded)
self.maxctx.prec = p * 2
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index b3b6976..9b3e9ed 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -3741,7 +3741,7 @@ PyInit__elementtree(void)
if (expat_capi) {
/* check that it's usable */
if (strcmp(expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 ||
- expat_capi->size < sizeof(struct PyExpat_CAPI) ||
+ (size_t)expat_capi->size < sizeof(struct PyExpat_CAPI) ||
expat_capi->MAJOR_VERSION != XML_MAJOR_VERSION ||
expat_capi->MINOR_VERSION != XML_MINOR_VERSION ||
expat_capi->MICRO_VERSION != XML_MICRO_VERSION) {
diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c
index 6a2a9af..1566b18 100644
--- a/Modules/_hashopenssl.c
+++ b/Modules/_hashopenssl.c
@@ -31,10 +31,6 @@
#define HASH_OBJ_CONSTRUCTOR 0
#endif
-/* Minimum OpenSSL version needed to support sha224 and higher. */
-#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000)
-#define _OPENSSL_SUPPORTS_SHA2
-#endif
typedef struct {
PyObject_HEAD
@@ -56,12 +52,10 @@ static PyTypeObject EVPtype;
DEFINE_CONSTS_FOR_NEW(md5)
DEFINE_CONSTS_FOR_NEW(sha1)
-#ifdef _OPENSSL_SUPPORTS_SHA2
DEFINE_CONSTS_FOR_NEW(sha224)
DEFINE_CONSTS_FOR_NEW(sha256)
DEFINE_CONSTS_FOR_NEW(sha384)
DEFINE_CONSTS_FOR_NEW(sha512)
-#endif
static EVPobject *
@@ -798,12 +792,10 @@ generate_hash_name_list(void)
GEN_CONSTRUCTOR(md5)
GEN_CONSTRUCTOR(sha1)
-#ifdef _OPENSSL_SUPPORTS_SHA2
GEN_CONSTRUCTOR(sha224)
GEN_CONSTRUCTOR(sha256)
GEN_CONSTRUCTOR(sha384)
GEN_CONSTRUCTOR(sha512)
-#endif
/* List of functions exported by this module */
@@ -815,12 +807,10 @@ static struct PyMethodDef EVP_functions[] = {
#endif
CONSTRUCTOR_METH_DEF(md5),
CONSTRUCTOR_METH_DEF(sha1),
-#ifdef _OPENSSL_SUPPORTS_SHA2
CONSTRUCTOR_METH_DEF(sha224),
CONSTRUCTOR_METH_DEF(sha256),
CONSTRUCTOR_METH_DEF(sha384),
CONSTRUCTOR_METH_DEF(sha512),
-#endif
{NULL, NULL} /* Sentinel */
};
@@ -877,11 +867,9 @@ PyInit__hashlib(void)
/* these constants are used by the convenience constructors */
INIT_CONSTRUCTOR_CONSTANTS(md5);
INIT_CONSTRUCTOR_CONSTANTS(sha1);
-#ifdef _OPENSSL_SUPPORTS_SHA2
INIT_CONSTRUCTOR_CONSTANTS(sha224);
INIT_CONSTRUCTOR_CONSTANTS(sha256);
INIT_CONSTRUCTOR_CONSTANTS(sha384);
INIT_CONSTRUCTOR_CONSTANTS(sha512);
-#endif
return m;
}
diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c
index eee56a0..4372ad4 100644
--- a/Modules/_heapqmodule.c
+++ b/Modules/_heapqmodule.c
@@ -9,12 +9,11 @@ annotated by François Pinard, and converted to C by Raymond Hettinger.
#include "Python.h"
static int
-_siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
+siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
{
- PyObject *newitem, *parent, *olditem;
+ PyObject *newitem, *parent;
+ Py_ssize_t parentpos, size;
int cmp;
- Py_ssize_t parentpos;
- Py_ssize_t size;
assert(PyList_Check(heap));
size = PyList_GET_SIZE(heap);
@@ -23,60 +22,45 @@ _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
return -1;
}
- newitem = PyList_GET_ITEM(heap, pos);
- Py_INCREF(newitem);
/* Follow the path to the root, moving parents down until finding
a place newitem fits. */
- while (pos > startpos){
+ newitem = PyList_GET_ITEM(heap, pos);
+ while (pos > startpos) {
parentpos = (pos - 1) >> 1;
parent = PyList_GET_ITEM(heap, parentpos);
cmp = PyObject_RichCompareBool(newitem, parent, Py_LT);
- if (cmp == -1) {
- Py_DECREF(newitem);
+ if (cmp == -1)
return -1;
- }
if (size != PyList_GET_SIZE(heap)) {
- Py_DECREF(newitem);
PyErr_SetString(PyExc_RuntimeError,
"list changed size during iteration");
return -1;
}
if (cmp == 0)
break;
- Py_INCREF(parent);
- olditem = PyList_GET_ITEM(heap, pos);
+ parent = PyList_GET_ITEM(heap, parentpos);
+ newitem = PyList_GET_ITEM(heap, pos);
+ PyList_SET_ITEM(heap, parentpos, newitem);
PyList_SET_ITEM(heap, pos, parent);
- Py_DECREF(olditem);
pos = parentpos;
- if (size != PyList_GET_SIZE(heap)) {
- PyErr_SetString(PyExc_RuntimeError,
- "list changed size during iteration");
- return -1;
- }
}
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, newitem);
return 0;
}
static int
-_siftup(PyListObject *heap, Py_ssize_t pos)
+siftup(PyListObject *heap, Py_ssize_t pos)
{
Py_ssize_t startpos, endpos, childpos, rightpos, limit;
+ PyObject *tmp1, *tmp2;
int cmp;
- PyObject *newitem, *tmp, *olditem;
- Py_ssize_t size;
assert(PyList_Check(heap));
- size = PyList_GET_SIZE(heap);
- endpos = size;
+ endpos = PyList_GET_SIZE(heap);
startpos = pos;
if (pos >= endpos) {
PyErr_SetString(PyExc_IndexError, "index out of range");
return -1;
}
- newitem = PyList_GET_ITEM(heap, pos);
- Py_INCREF(newitem);
/* Bubble up the smaller child until hitting a leaf. */
limit = endpos / 2; /* smallest pos that has no child */
@@ -89,38 +73,25 @@ _siftup(PyListObject *heap, Py_ssize_t pos)
PyList_GET_ITEM(heap, childpos),
PyList_GET_ITEM(heap, rightpos),
Py_LT);
- if (cmp == -1) {
- Py_DECREF(newitem);
+ if (cmp == -1)
return -1;
- }
if (cmp == 0)
childpos = rightpos;
- }
- if (size != PyList_GET_SIZE(heap)) {
- Py_DECREF(newitem);
- PyErr_SetString(PyExc_RuntimeError,
- "list changed size during iteration");
- return -1;
+ if (endpos != PyList_GET_SIZE(heap)) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "list changed size during iteration");
+ return -1;
+ }
}
/* Move the smaller child up. */
- tmp = PyList_GET_ITEM(heap, childpos);
- Py_INCREF(tmp);
- olditem = PyList_GET_ITEM(heap, pos);
- PyList_SET_ITEM(heap, pos, tmp);
- Py_DECREF(olditem);
+ tmp1 = PyList_GET_ITEM(heap, childpos);
+ tmp2 = PyList_GET_ITEM(heap, pos);
+ PyList_SET_ITEM(heap, childpos, tmp2);
+ PyList_SET_ITEM(heap, pos, tmp1);
pos = childpos;
- if (size != PyList_GET_SIZE(heap)) {
- PyErr_SetString(PyExc_RuntimeError,
- "list changed size during iteration");
- return -1;
- }
}
-
- /* The leaf at pos is empty now. Put newitem there, and bubble
- it up to its final resting place (by sifting its parents down). */
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, newitem);
- return _siftdown(heap, startpos, pos);
+ /* Bubble it up to its final resting place (by sifting its parents down). */
+ return siftdown(heap, startpos, pos);
}
static PyObject *
@@ -139,17 +110,16 @@ heappush(PyObject *self, PyObject *args)
if (PyList_Append(heap, item) == -1)
return NULL;
- if (_siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1) == -1)
+ if (siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1) == -1)
return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(heappush_doc,
"heappush(heap, item) -> None. Push item onto heap, maintaining the heap invariant.");
static PyObject *
-heappop(PyObject *self, PyObject *heap)
+heappop_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
{
PyObject *lastelt, *returnitem;
Py_ssize_t n;
@@ -159,7 +129,7 @@ heappop(PyObject *self, PyObject *heap)
return NULL;
}
- /* # raises appropriate IndexError if heap is empty */
+ /* raises IndexError if the heap is empty */
n = PyList_GET_SIZE(heap);
if (n == 0) {
PyErr_SetString(PyExc_IndexError, "index out of range");
@@ -178,18 +148,24 @@ heappop(PyObject *self, PyObject *heap)
return lastelt;
returnitem = PyList_GET_ITEM(heap, 0);
PyList_SET_ITEM(heap, 0, lastelt);
- if (_siftup((PyListObject *)heap, 0) == -1) {
+ if (siftup_func((PyListObject *)heap, 0) == -1) {
Py_DECREF(returnitem);
return NULL;
}
return returnitem;
}
+static PyObject *
+heappop(PyObject *self, PyObject *heap)
+{
+ return heappop_internal(heap, siftup);
+}
+
PyDoc_STRVAR(heappop_doc,
"Pop the smallest item off the heap, maintaining the heap invariant.");
static PyObject *
-heapreplace(PyObject *self, PyObject *args)
+heapreplace_internal(PyObject *args, int siftup_func(PyListObject *, Py_ssize_t))
{
PyObject *heap, *item, *returnitem;
@@ -209,13 +185,19 @@ heapreplace(PyObject *self, PyObject *args)
returnitem = PyList_GET_ITEM(heap, 0);
Py_INCREF(item);
PyList_SET_ITEM(heap, 0, item);
- if (_siftup((PyListObject *)heap, 0) == -1) {
+ if (siftup_func((PyListObject *)heap, 0) == -1) {
Py_DECREF(returnitem);
return NULL;
}
return returnitem;
}
+static PyObject *
+heapreplace(PyObject *self, PyObject *args)
+{
+ return heapreplace_internal(args, siftup);
+}
+
PyDoc_STRVAR(heapreplace_doc,
"heapreplace(heap, item) -> value. Pop and return the current smallest value, and add the new item.\n\
\n\
@@ -256,7 +238,7 @@ heappushpop(PyObject *self, PyObject *args)
returnitem = PyList_GET_ITEM(heap, 0);
Py_INCREF(item);
PyList_SET_ITEM(heap, 0, item);
- if (_siftup((PyListObject *)heap, 0) == -1) {
+ if (siftup((PyListObject *)heap, 0) == -1) {
Py_DECREF(returnitem);
return NULL;
}
@@ -269,7 +251,7 @@ from the heap. The combined action runs more efficiently than\n\
heappush() followed by a separate call to heappop().");
static PyObject *
-heapify(PyObject *self, PyObject *heap)
+heapify_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
{
Py_ssize_t i, n;
@@ -287,141 +269,65 @@ heapify(PyObject *self, PyObject *heap)
and that's again n//2-1.
*/
for (i=n/2-1 ; i>=0 ; i--)
- if(_siftup((PyListObject *)heap, i) == -1)
+ if(siftup_func((PyListObject *)heap, i) == -1)
return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
-PyDoc_STRVAR(heapify_doc,
-"Transform list into a heap, in-place, in O(len(heap)) time.");
-
static PyObject *
-nlargest(PyObject *self, PyObject *args)
+heapify(PyObject *self, PyObject *heap)
{
- PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem;
- Py_ssize_t i, n;
- int cmp;
-
- if (!PyArg_ParseTuple(args, "nO:nlargest", &n, &iterable))
- return NULL;
-
- it = PyObject_GetIter(iterable);
- if (it == NULL)
- return NULL;
-
- heap = PyList_New(0);
- if (heap == NULL)
- goto fail;
-
- for (i=0 ; i<n ; i++ ){
- elem = PyIter_Next(it);
- if (elem == NULL) {
- if (PyErr_Occurred())
- goto fail;
- else
- goto sortit;
- }
- if (PyList_Append(heap, elem) == -1) {
- Py_DECREF(elem);
- goto fail;
- }
- Py_DECREF(elem);
- }
- if (PyList_GET_SIZE(heap) == 0)
- goto sortit;
-
- for (i=n/2-1 ; i>=0 ; i--)
- if(_siftup((PyListObject *)heap, i) == -1)
- goto fail;
-
- sol = PyList_GET_ITEM(heap, 0);
- while (1) {
- elem = PyIter_Next(it);
- if (elem == NULL) {
- if (PyErr_Occurred())
- goto fail;
- else
- goto sortit;
- }
- cmp = PyObject_RichCompareBool(sol, elem, Py_LT);
- if (cmp == -1) {
- Py_DECREF(elem);
- goto fail;
- }
- if (cmp == 0) {
- Py_DECREF(elem);
- continue;
- }
- oldelem = PyList_GET_ITEM(heap, 0);
- PyList_SET_ITEM(heap, 0, elem);
- Py_DECREF(oldelem);
- if (_siftup((PyListObject *)heap, 0) == -1)
- goto fail;
- sol = PyList_GET_ITEM(heap, 0);
- }
-sortit:
- if (PyList_Sort(heap) == -1)
- goto fail;
- if (PyList_Reverse(heap) == -1)
- goto fail;
- Py_DECREF(it);
- return heap;
-
-fail:
- Py_DECREF(it);
- Py_XDECREF(heap);
- return NULL;
+ return heapify_internal(heap, siftup);
}
-PyDoc_STRVAR(nlargest_doc,
-"Find the n largest elements in a dataset.\n\
-\n\
-Equivalent to: sorted(iterable, reverse=True)[:n]\n");
+PyDoc_STRVAR(heapify_doc,
+"Transform list into a heap, in-place, in O(len(heap)) time.");
static int
-_siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
+siftdown_max(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
{
PyObject *newitem, *parent;
+ Py_ssize_t parentpos, size;
int cmp;
- Py_ssize_t parentpos;
assert(PyList_Check(heap));
- if (pos >= PyList_GET_SIZE(heap)) {
+ size = PyList_GET_SIZE(heap);
+ if (pos >= size) {
PyErr_SetString(PyExc_IndexError, "index out of range");
return -1;
}
- newitem = PyList_GET_ITEM(heap, pos);
- Py_INCREF(newitem);
/* Follow the path to the root, moving parents down until finding
a place newitem fits. */
- while (pos > startpos){
+ newitem = PyList_GET_ITEM(heap, pos);
+ while (pos > startpos) {
parentpos = (pos - 1) >> 1;
parent = PyList_GET_ITEM(heap, parentpos);
cmp = PyObject_RichCompareBool(parent, newitem, Py_LT);
- if (cmp == -1) {
- Py_DECREF(newitem);
+ if (cmp == -1)
+ return -1;
+ if (size != PyList_GET_SIZE(heap)) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "list changed size during iteration");
return -1;
}
if (cmp == 0)
break;
- Py_INCREF(parent);
- Py_DECREF(PyList_GET_ITEM(heap, pos));
+ parent = PyList_GET_ITEM(heap, parentpos);
+ newitem = PyList_GET_ITEM(heap, pos);
+ PyList_SET_ITEM(heap, parentpos, newitem);
PyList_SET_ITEM(heap, pos, parent);
pos = parentpos;
}
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, newitem);
return 0;
}
static int
-_siftupmax(PyListObject *heap, Py_ssize_t pos)
+siftup_max(PyListObject *heap, Py_ssize_t pos)
{
Py_ssize_t startpos, endpos, childpos, rightpos, limit;
+ PyObject *tmp1, *tmp2;
int cmp;
- PyObject *newitem, *tmp;
assert(PyList_Check(heap));
endpos = PyList_GET_SIZE(heap);
@@ -430,8 +336,6 @@ _siftupmax(PyListObject *heap, Py_ssize_t pos)
PyErr_SetString(PyExc_IndexError, "index out of range");
return -1;
}
- newitem = PyList_GET_ITEM(heap, pos);
- Py_INCREF(newitem);
/* Bubble up the smaller child until hitting a leaf. */
limit = endpos / 2; /* smallest pos that has no child */
@@ -444,111 +348,50 @@ _siftupmax(PyListObject *heap, Py_ssize_t pos)
PyList_GET_ITEM(heap, rightpos),
PyList_GET_ITEM(heap, childpos),
Py_LT);
- if (cmp == -1) {
- Py_DECREF(newitem);
+ if (cmp == -1)
return -1;
- }
if (cmp == 0)
childpos = rightpos;
+ if (endpos != PyList_GET_SIZE(heap)) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "list changed size during iteration");
+ return -1;
+ }
}
/* Move the smaller child up. */
- tmp = PyList_GET_ITEM(heap, childpos);
- Py_INCREF(tmp);
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, tmp);
+ tmp1 = PyList_GET_ITEM(heap, childpos);
+ tmp2 = PyList_GET_ITEM(heap, pos);
+ PyList_SET_ITEM(heap, childpos, tmp2);
+ PyList_SET_ITEM(heap, pos, tmp1);
pos = childpos;
}
-
- /* The leaf at pos is empty now. Put newitem there, and bubble
- it up to its final resting place (by sifting its parents down). */
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, newitem);
- return _siftdownmax(heap, startpos, pos);
+ /* Bubble it up to its final resting place (by sifting its parents down). */
+ return siftdown_max(heap, startpos, pos);
}
static PyObject *
-nsmallest(PyObject *self, PyObject *args)
+heappop_max(PyObject *self, PyObject *heap)
{
- PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem;
- Py_ssize_t i, n;
- int cmp;
-
- if (!PyArg_ParseTuple(args, "nO:nsmallest", &n, &iterable))
- return NULL;
-
- it = PyObject_GetIter(iterable);
- if (it == NULL)
- return NULL;
-
- heap = PyList_New(0);
- if (heap == NULL)
- goto fail;
-
- for (i=0 ; i<n ; i++ ){
- elem = PyIter_Next(it);
- if (elem == NULL) {
- if (PyErr_Occurred())
- goto fail;
- else
- goto sortit;
- }
- if (PyList_Append(heap, elem) == -1) {
- Py_DECREF(elem);
- goto fail;
- }
- Py_DECREF(elem);
- }
- n = PyList_GET_SIZE(heap);
- if (n == 0)
- goto sortit;
+ return heappop_internal(heap, siftup_max);
+}
- for (i=n/2-1 ; i>=0 ; i--)
- if(_siftupmax((PyListObject *)heap, i) == -1)
- goto fail;
-
- los = PyList_GET_ITEM(heap, 0);
- while (1) {
- elem = PyIter_Next(it);
- if (elem == NULL) {
- if (PyErr_Occurred())
- goto fail;
- else
- goto sortit;
- }
- cmp = PyObject_RichCompareBool(elem, los, Py_LT);
- if (cmp == -1) {
- Py_DECREF(elem);
- goto fail;
- }
- if (cmp == 0) {
- Py_DECREF(elem);
- continue;
- }
+PyDoc_STRVAR(heappop_max_doc, "Maxheap variant of heappop.");
- oldelem = PyList_GET_ITEM(heap, 0);
- PyList_SET_ITEM(heap, 0, elem);
- Py_DECREF(oldelem);
- if (_siftupmax((PyListObject *)heap, 0) == -1)
- goto fail;
- los = PyList_GET_ITEM(heap, 0);
- }
+static PyObject *
+heapreplace_max(PyObject *self, PyObject *args)
+{
+ return heapreplace_internal(args, siftup_max);
+}
-sortit:
- if (PyList_Sort(heap) == -1)
- goto fail;
- Py_DECREF(it);
- return heap;
+PyDoc_STRVAR(heapreplace_max_doc, "Maxheap variant of heapreplace");
-fail:
- Py_DECREF(it);
- Py_XDECREF(heap);
- return NULL;
+static PyObject *
+heapify_max(PyObject *self, PyObject *heap)
+{
+ return heapify_internal(heap, siftup_max);
}
-PyDoc_STRVAR(nsmallest_doc,
-"Find the n smallest elements in a dataset.\n\
-\n\
-Equivalent to: sorted(iterable)[:n]\n");
+PyDoc_STRVAR(heapify_max_doc, "Maxheap variant of heapify.");
static PyMethodDef heapq_methods[] = {
{"heappush", (PyCFunction)heappush,
@@ -561,10 +404,12 @@ static PyMethodDef heapq_methods[] = {
METH_VARARGS, heapreplace_doc},
{"heapify", (PyCFunction)heapify,
METH_O, heapify_doc},
- {"nlargest", (PyCFunction)nlargest,
- METH_VARARGS, nlargest_doc},
- {"nsmallest", (PyCFunction)nsmallest,
- METH_VARARGS, nsmallest_doc},
+ {"_heappop_max", (PyCFunction)heappop_max,
+ METH_O, heappop_max_doc},
+ {"_heapreplace_max",(PyCFunction)heapreplace_max,
+ METH_VARARGS, heapreplace_max_doc},
+ {"_heapify_max", (PyCFunction)heapify_max,
+ METH_O, heapify_max_doc},
{NULL, NULL} /* sentinel */
};
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index 45c31a5..e70c4b7 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -237,8 +237,8 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL;
+ _Py_IDENTIFIER(_blksize);
_Py_IDENTIFIER(isatty);
- _Py_IDENTIFIER(fileno);
_Py_IDENTIFIER(mode);
_Py_IDENTIFIER(close);
@@ -380,24 +380,14 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
line_buffering = 0;
if (buffering < 0) {
- buffering = DEFAULT_BUFFER_SIZE;
-#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
- {
- struct stat st;
- long fileno;
- PyObject *res = _PyObject_CallMethodId(raw, &PyId_fileno, NULL);
- if (res == NULL)
- goto error;
-
- fileno = PyLong_AsLong(res);
- Py_DECREF(res);
- if (fileno == -1 && PyErr_Occurred())
- goto error;
-
- if (fstat(fileno, &st) >= 0 && st.st_blksize > 1)
- buffering = st.st_blksize;
- }
-#endif
+ PyObject *blksize_obj;
+ blksize_obj = _PyObject_GetAttrId(raw, &PyId__blksize);
+ if (blksize_obj == NULL)
+ goto error;
+ buffering = PyLong_AsLong(blksize_obj);
+ Py_DECREF(blksize_obj);
+ if (buffering == -1 && PyErr_Occurred())
+ goto error;
}
if (buffering < 0) {
PyErr_SetString(PyExc_ValueError,
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 02cf038..692ce41 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -24,6 +24,7 @@ _Py_IDENTIFIER(read);
_Py_IDENTIFIER(read1);
_Py_IDENTIFIER(readable);
_Py_IDENTIFIER(readinto);
+_Py_IDENTIFIER(readinto1);
_Py_IDENTIFIER(writable);
_Py_IDENTIFIER(write);
@@ -47,17 +48,21 @@ PyDoc_STRVAR(bufferediobase_doc,
);
static PyObject *
-bufferediobase_readinto(PyObject *self, PyObject *args)
+_bufferediobase_readinto_generic(PyObject *self, PyObject *args, char readinto1)
{
Py_buffer buf;
Py_ssize_t len;
PyObject *data;
- if (!PyArg_ParseTuple(args, "w*:readinto", &buf)) {
+ if (!PyArg_ParseTuple(args,
+ readinto1 ? "w*:readinto1" : "w*:readinto",
+ &buf)) {
return NULL;
}
- data = _PyObject_CallMethodId(self, &PyId_read, "n", buf.len);
+ data = _PyObject_CallMethodId(self,
+ readinto1 ? &PyId_read1 : &PyId_read,
+ "n", buf.len);
if (data == NULL)
goto error;
@@ -89,6 +94,18 @@ bufferediobase_readinto(PyObject *self, PyObject *args)
}
static PyObject *
+bufferediobase_readinto(PyObject *self, PyObject *args)
+{
+ return _bufferediobase_readinto_generic(self, args, 0);
+}
+
+static PyObject *
+bufferediobase_readinto1(PyObject *self, PyObject *args)
+{
+ return _bufferediobase_readinto_generic(self, args, 1);
+}
+
+static PyObject *
bufferediobase_unsupported(const char *message)
{
_PyIO_State *state = IO_STATE();
@@ -167,6 +184,7 @@ static PyMethodDef bufferediobase_methods[] = {
{"read", bufferediobase_read, METH_VARARGS, bufferediobase_read_doc},
{"read1", bufferediobase_read1, METH_VARARGS, bufferediobase_read1_doc},
{"readinto", bufferediobase_readinto, METH_VARARGS, NULL},
+ {"readinto1", bufferediobase_readinto1, METH_VARARGS, NULL},
{"write", bufferediobase_write, METH_VARARGS, bufferediobase_write_doc},
{NULL, NULL}
};
@@ -977,7 +995,7 @@ buffered_read1(buffered *self, PyObject *args)
}
static PyObject *
-buffered_readinto(buffered *self, PyObject *args)
+_buffered_readinto_generic(buffered *self, PyObject *args, char readinto1)
{
Py_buffer buf;
Py_ssize_t n, written = 0, remaining;
@@ -985,7 +1003,9 @@ buffered_readinto(buffered *self, PyObject *args)
CHECK_INITIALIZED(self)
- if (!PyArg_ParseTuple(args, "w*:readinto", &buf))
+ if (!PyArg_ParseTuple(args,
+ readinto1 ? "w*:readinto1" : "w*:readinto",
+ &buf))
return NULL;
n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
@@ -1023,7 +1043,10 @@ buffered_readinto(buffered *self, PyObject *args)
n = _bufferedreader_raw_read(self, (char *) buf.buf + written,
remaining);
}
- else {
+
+ /* In readinto1 mode, we do not want to fill the internal
+ buffer if we already have some data to return */
+ else if (!(readinto1 && written)) {
n = _bufferedreader_fill_buffer(self);
if (n > 0) {
if (n > remaining)
@@ -1034,6 +1057,9 @@ buffered_readinto(buffered *self, PyObject *args)
continue; /* short circuit */
}
}
+ else
+ n = 0;
+
if (n == 0 || (n == -2 && written > 0))
break;
if (n < 0) {
@@ -1043,6 +1069,12 @@ buffered_readinto(buffered *self, PyObject *args)
}
goto end;
}
+
+ /* At most one read in readinto1 mode */
+ if (readinto1) {
+ written += n;
+ break;
+ }
}
res = PyLong_FromSsize_t(written);
@@ -1054,6 +1086,19 @@ end_unlocked:
}
static PyObject *
+buffered_readinto(buffered *self, PyObject *args)
+{
+ return _buffered_readinto_generic(self, args, 0);
+}
+
+static PyObject *
+buffered_readinto1(buffered *self, PyObject *args)
+{
+ return _buffered_readinto_generic(self, args, 1);
+}
+
+
+static PyObject *
_buffered_readline(buffered *self, Py_ssize_t limit)
{
PyObject *res = NULL;
@@ -1738,6 +1783,7 @@ static PyMethodDef bufferedreader_methods[] = {
{"peek", (PyCFunction)buffered_peek, METH_VARARGS},
{"read1", (PyCFunction)buffered_read1, METH_VARARGS},
{"readinto", (PyCFunction)buffered_readinto, METH_VARARGS},
+ {"readinto1", (PyCFunction)buffered_readinto1, METH_VARARGS},
{"readline", (PyCFunction)buffered_readline, METH_VARARGS},
{"seek", (PyCFunction)buffered_seek, METH_VARARGS},
{"tell", (PyCFunction)buffered_tell, METH_NOARGS},
@@ -2339,6 +2385,12 @@ bufferedrwpair_readinto(rwpair *self, PyObject *args)
}
static PyObject *
+bufferedrwpair_readinto1(rwpair *self, PyObject *args)
+{
+ return _forward_call(self->reader, &PyId_readinto1, args);
+}
+
+static PyObject *
bufferedrwpair_write(rwpair *self, PyObject *args)
{
return _forward_call(self->writer, &PyId_write, args);
@@ -2403,6 +2455,7 @@ static PyMethodDef bufferedrwpair_methods[] = {
{"peek", (PyCFunction)bufferedrwpair_peek, METH_VARARGS},
{"read1", (PyCFunction)bufferedrwpair_read1, METH_VARARGS},
{"readinto", (PyCFunction)bufferedrwpair_readinto, METH_VARARGS},
+ {"readinto1", (PyCFunction)bufferedrwpair_readinto1, METH_VARARGS},
{"write", (PyCFunction)bufferedrwpair_write, METH_VARARGS},
{"flush", (PyCFunction)bufferedrwpair_flush, METH_NOARGS},
@@ -2551,6 +2604,7 @@ static PyMethodDef bufferedrandom_methods[] = {
{"read", (PyCFunction)buffered_read, METH_VARARGS},
{"read1", (PyCFunction)buffered_read1, METH_VARARGS},
{"readinto", (PyCFunction)buffered_readinto, METH_VARARGS},
+ {"readinto1", (PyCFunction)buffered_readinto1, METH_VARARGS},
{"readline", (PyCFunction)buffered_readline, METH_VARARGS},
{"peek", (PyCFunction)buffered_peek, METH_VARARGS},
{"write", (PyCFunction)bufferedwriter_write, METH_VARARGS},
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index 1537d97..fc4ea74 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -4,10 +4,9 @@
typedef struct {
PyObject_HEAD
- char *buf;
+ PyObject *buf;
Py_ssize_t pos;
Py_ssize_t string_size;
- size_t buf_size;
PyObject *dict;
PyObject *weakreflist;
Py_ssize_t exports;
@@ -18,6 +17,12 @@ typedef struct {
bytesio *source;
} bytesiobuf;
+/* The bytesio object can be in three states:
+ * Py_REFCNT(buf) == 1, exports == 0.
+ * Py_REFCNT(buf) > 1. exports == 0,
+ first modification or export causes the internal buffer copying.
+ * exports > 0. Py_REFCNT(buf) == 1, any modifications are forbidden.
+*/
#define CHECK_CLOSED(self) \
if ((self)->buf == NULL) { \
@@ -33,40 +38,60 @@ typedef struct {
return NULL; \
}
+#define SHARED_BUF(self) (Py_REFCNT((self)->buf) > 1)
+
/* Internal routine to get a line from the buffer of a BytesIO
object. Returns the length between the current position to the
next newline character. */
static Py_ssize_t
-get_line(bytesio *self, char **output)
+scan_eol(bytesio *self, Py_ssize_t len)
{
- char *n;
- const char *str_end;
- Py_ssize_t len;
+ const char *start, *n;
+ Py_ssize_t maxlen;
assert(self->buf != NULL);
/* Move to the end of the line, up to the end of the string, s. */
- str_end = self->buf + self->string_size;
- for (n = self->buf + self->pos;
- n < str_end && *n != '\n';
- n++);
-
- /* Skip the newline character */
- if (n < str_end)
- n++;
-
- /* Get the length from the current position to the end of the line. */
- len = n - (self->buf + self->pos);
- *output = self->buf + self->pos;
-
+ start = PyBytes_AS_STRING(self->buf) + self->pos;
+ maxlen = self->string_size - self->pos;
+ if (len < 0 || len > maxlen)
+ len = maxlen;
+
+ if (len) {
+ n = memchr(start, '\n', len);
+ if (n)
+ /* Get the length from the current position to the end of
+ the line. */
+ len = n - start + 1;
+ }
assert(len >= 0);
assert(self->pos < PY_SSIZE_T_MAX - len);
- self->pos += len;
return len;
}
+/* Internal routine for detaching the shared buffer of BytesIO objects.
+ The caller should ensure that the 'size' argument is non-negative and
+ not lesser than self->string_size. Returns 0 on success, -1 otherwise. */
+static int
+unshare_buffer(bytesio *self, size_t size)
+{
+ PyObject *new_buf, *old_buf;
+ assert(SHARED_BUF(self));
+ assert(self->exports == 0);
+ assert(size >= (size_t)self->string_size);
+ new_buf = PyBytes_FromStringAndSize(NULL, size);
+ if (new_buf == NULL)
+ return -1;
+ memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf),
+ self->string_size);
+ old_buf = self->buf;
+ self->buf = new_buf;
+ Py_DECREF(old_buf);
+ return 0;
+}
+
/* Internal routine for changing the size of the buffer of BytesIO objects.
The caller should ensure that the 'size' argument is non-negative. Returns
0 on success, -1 otherwise. */
@@ -75,8 +100,7 @@ resize_buffer(bytesio *self, size_t size)
{
/* Here, unsigned types are used to avoid dealing with signed integer
overflow, which is undefined in C. */
- size_t alloc = self->buf_size;
- char *new_buf = NULL;
+ size_t alloc = PyBytes_GET_SIZE(self->buf);
assert(self->buf != NULL);
@@ -104,13 +128,15 @@ resize_buffer(bytesio *self, size_t size)
if (alloc > ((size_t)-1) / sizeof(char))
goto overflow;
- new_buf = (char *)PyMem_Realloc(self->buf, alloc * sizeof(char));
- if (new_buf == NULL) {
- PyErr_NoMemory();
- return -1;
+
+ if (SHARED_BUF(self)) {
+ if (unshare_buffer(self, alloc) < 0)
+ return -1;
+ }
+ else {
+ if (_PyBytes_Resize(&self->buf, alloc) < 0)
+ return -1;
}
- self->buf_size = alloc;
- self->buf = new_buf;
return 0;
@@ -125,12 +151,18 @@ resize_buffer(bytesio *self, size_t size)
static Py_ssize_t
write_bytes(bytesio *self, const char *bytes, Py_ssize_t len)
{
+ size_t endpos;
assert(self->buf != NULL);
assert(self->pos >= 0);
assert(len >= 0);
- if ((size_t)self->pos + len > self->buf_size) {
- if (resize_buffer(self, (size_t)self->pos + len) < 0)
+ endpos = (size_t)self->pos + len;
+ if (endpos > (size_t)PyBytes_GET_SIZE(self->buf)) {
+ if (resize_buffer(self, endpos) < 0)
+ return -1;
+ }
+ else if (SHARED_BUF(self)) {
+ if (unshare_buffer(self, Py_MAX(endpos, (size_t)self->string_size)) < 0)
return -1;
}
@@ -143,18 +175,18 @@ write_bytes(bytesio *self, const char *bytes, Py_ssize_t len)
| | <--to pad-->|<---to write---> |
0 buf position
*/
- memset(self->buf + self->string_size, '\0',
+ memset(PyBytes_AS_STRING(self->buf) + self->string_size, '\0',
(self->pos - self->string_size) * sizeof(char));
}
/* Copy the data to the internal buffer, overwriting some of the existing
data if self->pos < self->string_size. */
- memcpy(self->buf + self->pos, bytes, len);
- self->pos += len;
+ memcpy(PyBytes_AS_STRING(self->buf) + self->pos, bytes, len);
+ self->pos = endpos;
/* Set the new length of the internal string if it has changed. */
- if (self->string_size < self->pos) {
- self->string_size = self->pos;
+ if ((size_t)self->string_size < endpos) {
+ self->string_size = endpos;
}
return len;
@@ -231,7 +263,22 @@ static PyObject *
bytesio_getvalue(bytesio *self)
{
CHECK_CLOSED(self);
- return PyBytes_FromStringAndSize(self->buf, self->string_size);
+ if (self->string_size <= 1 || self->exports > 0)
+ return PyBytes_FromStringAndSize(PyBytes_AS_STRING(self->buf),
+ self->string_size);
+
+ if (self->string_size != PyBytes_GET_SIZE(self->buf)) {
+ if (SHARED_BUF(self)) {
+ if (unshare_buffer(self, self->string_size) < 0)
+ return NULL;
+ }
+ else {
+ if (_PyBytes_Resize(&self->buf, self->string_size) < 0)
+ return NULL;
+ }
+ }
+ Py_INCREF(self->buf);
+ return self->buf;
}
PyDoc_STRVAR(isatty_doc,
@@ -257,6 +304,26 @@ bytesio_tell(bytesio *self)
return PyLong_FromSsize_t(self->pos);
}
+static PyObject *
+read_bytes(bytesio *self, Py_ssize_t size)
+{
+ char *output;
+
+ assert(self->buf != NULL);
+ assert(size <= self->string_size);
+ if (size > 1 &&
+ self->pos == 0 && size == PyBytes_GET_SIZE(self->buf) &&
+ self->exports == 0) {
+ self->pos += size;
+ Py_INCREF(self->buf);
+ return self->buf;
+ }
+
+ output = PyBytes_AS_STRING(self->buf) + self->pos;
+ self->pos += size;
+ return PyBytes_FromStringAndSize(output, size);
+}
+
PyDoc_STRVAR(read_doc,
"read([size]) -> read at most size bytes, returned as a string.\n"
"\n"
@@ -267,7 +334,6 @@ static PyObject *
bytesio_read(bytesio *self, PyObject *args)
{
Py_ssize_t size, n;
- char *output;
PyObject *arg = Py_None;
CHECK_CLOSED(self);
@@ -298,11 +364,7 @@ bytesio_read(bytesio *self, PyObject *args)
size = 0;
}
- assert(self->buf != NULL);
- output = self->buf + self->pos;
- self->pos += size;
-
- return PyBytes_FromStringAndSize(output, size);
+ return read_bytes(self, size);
}
@@ -336,7 +398,6 @@ static PyObject *
bytesio_readline(bytesio *self, PyObject *args)
{
Py_ssize_t size, n;
- char *output;
PyObject *arg = Py_None;
CHECK_CLOSED(self);
@@ -359,15 +420,9 @@ bytesio_readline(bytesio *self, PyObject *args)
return NULL;
}
- n = get_line(self, &output);
-
- if (size >= 0 && size < n) {
- size = n - size;
- n -= size;
- self->pos -= size;
- }
+ n = scan_eol(self, size);
- return PyBytes_FromStringAndSize(output, n);
+ return read_bytes(self, n);
}
PyDoc_STRVAR(readlines_doc,
@@ -410,7 +465,9 @@ bytesio_readlines(bytesio *self, PyObject *args)
if (!result)
return NULL;
- while ((n = get_line(self, &output)) != 0) {
+ output = PyBytes_AS_STRING(self->buf) + self->pos;
+ while ((n = scan_eol(self, -1)) != 0) {
+ self->pos += n;
line = PyBytes_FromStringAndSize(output, n);
if (!line)
goto on_error;
@@ -422,6 +479,7 @@ bytesio_readlines(bytesio *self, PyObject *args)
size += n;
if (maxsize > 0 && size >= maxsize)
break;
+ output += n;
}
return result;
@@ -456,7 +514,7 @@ bytesio_readinto(bytesio *self, PyObject *arg)
len = 0;
}
- memcpy(buffer.buf, self->buf + self->pos, len);
+ memcpy(buffer.buf, PyBytes_AS_STRING(self->buf) + self->pos, len);
assert(self->pos + len < PY_SSIZE_T_MAX);
assert(len >= 0);
self->pos += len;
@@ -516,17 +574,16 @@ bytesio_truncate(bytesio *self, PyObject *args)
static PyObject *
bytesio_iternext(bytesio *self)
{
- char *next;
Py_ssize_t n;
CHECK_CLOSED(self);
- n = get_line(self, &next);
+ n = scan_eol(self, -1);
- if (!next || n == 0)
+ if (n == 0)
return NULL;
- return PyBytes_FromStringAndSize(next, n);
+ return read_bytes(self, n);
}
PyDoc_STRVAR(seek_doc,
@@ -658,10 +715,7 @@ static PyObject *
bytesio_close(bytesio *self)
{
CHECK_EXPORTS(self);
- if (self->buf != NULL) {
- PyMem_Free(self->buf);
- self->buf = NULL;
- }
+ Py_CLEAR(self->buf);
Py_RETURN_NONE;
}
@@ -791,10 +845,7 @@ bytesio_dealloc(bytesio *self)
"deallocated BytesIO object has exported buffers");
PyErr_Print();
}
- if (self->buf != NULL) {
- PyMem_Free(self->buf);
- self->buf = NULL;
- }
+ Py_CLEAR(self->buf);
Py_CLEAR(self->dict);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
@@ -814,7 +865,7 @@ bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
/* tp_alloc initializes all the fields to zero. So we don't have to
initialize them here. */
- self->buf = (char *)PyMem_Malloc(0);
+ self->buf = PyBytes_FromStringAndSize(NULL, 0);
if (self->buf == NULL) {
Py_DECREF(self);
return PyErr_NoMemory();
@@ -837,13 +888,26 @@ bytesio_init(bytesio *self, PyObject *args, PyObject *kwds)
self->string_size = 0;
self->pos = 0;
+ if (self->exports > 0) {
+ PyErr_SetString(PyExc_BufferError,
+ "Existing exports of data: object cannot be re-sized");
+ return -1;
+ }
if (initvalue && initvalue != Py_None) {
- PyObject *res;
- res = bytesio_write(self, initvalue);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- self->pos = 0;
+ if (PyBytes_CheckExact(initvalue)) {
+ Py_INCREF(initvalue);
+ Py_XDECREF(self->buf);
+ self->buf = initvalue;
+ self->string_size = PyBytes_GET_SIZE(initvalue);
+ }
+ else {
+ PyObject *res;
+ res = bytesio_write(self, initvalue);
+ if (res == NULL)
+ return -1;
+ Py_DECREF(res);
+ self->pos = 0;
+ }
}
return 0;
@@ -855,8 +919,8 @@ bytesio_sizeof(bytesio *self, void *unused)
Py_ssize_t res;
res = sizeof(bytesio);
- if (self->buf)
- res += self->buf_size;
+ if (self->buf && !SHARED_BUF(self))
+ res += _PySys_GetSizeOf(self->buf);
return PyLong_FromSsize_t(res);
}
@@ -964,18 +1028,24 @@ PyTypeObject PyBytesIO_Type = {
static int
bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags)
{
- int ret;
bytesio *b = (bytesio *) obj->source;
+
if (view == NULL) {
- b->exports++;
- return 0;
+ PyErr_SetString(PyExc_BufferError,
+ "bytesiobuf_getbuffer: view==NULL argument is obsolete");
+ return -1;
}
- ret = PyBuffer_FillInfo(view, (PyObject*)obj, b->buf, b->string_size,
- 0, flags);
- if (ret >= 0) {
- b->exports++;
+ if (SHARED_BUF(b)) {
+ if (unshare_buffer(b, b->string_size) < 0)
+ return -1;
}
- return ret;
+
+ /* cannot fail if view != NULL and readonly == 0 */
+ (void)PyBuffer_FillInfo(view, (PyObject*)obj,
+ PyBytes_AS_STRING(b->buf), b->string_size,
+ 0, flags);
+ b->exports++;
+ return 0;
}
static void
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index a2b253b..2435513 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -53,6 +53,7 @@ typedef struct {
signed int seekable : 2; /* -1 means unknown */
unsigned int closefd : 1;
char finalizing;
+ unsigned int blksize;
PyObject *weakreflist;
PyObject *dict;
} fileio;
@@ -161,6 +162,7 @@ fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self->writable = 0;
self->appending = 0;
self->seekable = -1;
+ self->blksize = 0;
self->closefd = 1;
self->weakreflist = NULL;
}
@@ -168,26 +170,6 @@ fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return (PyObject *) self;
}
-/* On Unix, open will succeed for directories.
- In Python, there should be no file objects referring to
- directories, so we need a check. */
-
-static int
-dircheck(fileio* self, PyObject *nameobj)
-{
-#if defined(HAVE_FSTAT) && defined(S_ISDIR) && defined(EISDIR)
- struct stat buf;
- if (self->fd < 0)
- return 0;
- if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
- errno = EISDIR;
- PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
- return -1;
- }
-#endif
- return 0;
-}
-
static int
check_fd(int fd)
{
@@ -233,6 +215,10 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
#elif !defined(MS_WINDOWS)
int *atomic_flag_works = NULL;
#endif
+#ifdef HAVE_FSTAT
+ struct stat fdfstat;
+#endif
+ int async_err = 0;
assert(PyFileIO_Check(oself));
if (self->fd >= 0) {
@@ -271,7 +257,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
int rv = _PyUnicode_HasNULChars(nameobj);
if (rv) {
if (rv != -1)
- PyErr_SetString(PyExc_TypeError, "embedded NUL character");
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
return -1;
}
widename = PyUnicode_AsUnicode(nameobj);
@@ -375,15 +361,18 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
errno = 0;
if (opener == Py_None) {
- Py_BEGIN_ALLOW_THREADS
+ do {
+ Py_BEGIN_ALLOW_THREADS
#ifdef MS_WINDOWS
- if (widename != NULL)
- self->fd = _wopen(widename, flags, 0666);
- else
+ if (widename != NULL)
+ self->fd = _wopen(widename, flags, 0666);
+ else
#endif
- self->fd = open(name, flags, 0666);
+ self->fd = open(name, flags, 0666);
- Py_END_ALLOW_THREADS
+ Py_END_ALLOW_THREADS
+ } while (self->fd < 0 && errno == EINTR &&
+ !(async_err = PyErr_CheckSignals()));
}
else {
PyObject *fdobj;
@@ -412,7 +401,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
fd_is_own = 1;
if (self->fd < 0) {
- PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
+ if (!async_err)
+ PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
goto error;
}
@@ -421,8 +411,26 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
goto error;
#endif
}
- if (dircheck(self, nameobj) < 0)
+
+ self->blksize = DEFAULT_BUFFER_SIZE;
+#ifdef HAVE_FSTAT
+ if (fstat(self->fd, &fdfstat) < 0)
goto error;
+#if defined(S_ISDIR) && defined(EISDIR)
+ /* On Unix, open will succeed for directories.
+ In Python, there should be no file objects referring to
+ directories, so we need a check. */
+ if (S_ISDIR(fdfstat.st_mode)) {
+ errno = EISDIR;
+ PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, nameobj);
+ goto error;
+ }
+#endif /* defined(S_ISDIR) */
+#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
+ if (fdfstat.st_blksize > 1)
+ self->blksize = fdfstat.st_blksize;
+#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
+#endif /* HAVE_FSTAT */
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
/* don't translate newlines (\r\n <=> \n) */
@@ -547,7 +555,7 @@ fileio_readinto(fileio *self, PyObject *args)
{
Py_buffer pbuf;
Py_ssize_t n, len;
- int err;
+ int err, async_err = 0;
if (self->fd < 0)
return err_closed();
@@ -559,16 +567,19 @@ fileio_readinto(fileio *self, PyObject *args)
if (_PyVerify_fd(self->fd)) {
len = pbuf.len;
- Py_BEGIN_ALLOW_THREADS
- errno = 0;
+ do {
+ Py_BEGIN_ALLOW_THREADS
+ errno = 0;
#ifdef MS_WINDOWS
- if (len > INT_MAX)
- len = INT_MAX;
- n = read(self->fd, pbuf.buf, (int)len);
+ if (len > INT_MAX)
+ len = INT_MAX;
+ n = read(self->fd, pbuf.buf, (int)len);
#else
- n = read(self->fd, pbuf.buf, len);
+ n = read(self->fd, pbuf.buf, len);
#endif
- Py_END_ALLOW_THREADS
+ Py_END_ALLOW_THREADS
+ } while (n < 0 && errno == EINTR &&
+ !(async_err = PyErr_CheckSignals()));
} else
n = -1;
err = errno;
@@ -577,7 +588,8 @@ fileio_readinto(fileio *self, PyObject *args)
if (err == EAGAIN)
Py_RETURN_NONE;
errno = err;
- PyErr_SetFromErrno(PyExc_IOError);
+ if (!async_err)
+ PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
@@ -624,6 +636,7 @@ fileio_readall(fileio *self)
Py_ssize_t bytes_read = 0;
Py_ssize_t n;
size_t bufsize;
+ int async_err = 0;
if (self->fd < 0)
return err_closed();
@@ -670,27 +683,23 @@ fileio_readall(fileio *self)
return NULL;
}
}
- Py_BEGIN_ALLOW_THREADS
- errno = 0;
- n = bufsize - bytes_read;
+ do {
+ Py_BEGIN_ALLOW_THREADS
+ errno = 0;
+ n = bufsize - bytes_read;
#ifdef MS_WINDOWS
- if (n > INT_MAX)
- n = INT_MAX;
- n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, (int)n);
+ if (n > INT_MAX)
+ n = INT_MAX;
+ n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, (int)n);
#else
- n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, n);
+ n = read(self->fd, PyBytes_AS_STRING(result) + bytes_read, n);
#endif
- Py_END_ALLOW_THREADS
+ Py_END_ALLOW_THREADS
+ } while (n < 0 && errno == EINTR &&
+ !(async_err = PyErr_CheckSignals()));
if (n == 0)
break;
if (n < 0) {
- if (errno == EINTR) {
- if (PyErr_CheckSignals()) {
- Py_DECREF(result);
- return NULL;
- }
- continue;
- }
if (errno == EAGAIN) {
if (bytes_read > 0)
break;
@@ -698,7 +707,8 @@ fileio_readall(fileio *self)
Py_RETURN_NONE;
}
Py_DECREF(result);
- PyErr_SetFromErrno(PyExc_IOError);
+ if (!async_err)
+ PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
bytes_read += n;
@@ -720,6 +730,7 @@ fileio_read(fileio *self, PyObject *args)
char *ptr;
Py_ssize_t n;
Py_ssize_t size = -1;
+ int async_err = 0;
PyObject *bytes;
if (self->fd < 0)
@@ -744,14 +755,17 @@ fileio_read(fileio *self, PyObject *args)
ptr = PyBytes_AS_STRING(bytes);
if (_PyVerify_fd(self->fd)) {
- Py_BEGIN_ALLOW_THREADS
- errno = 0;
+ do {
+ Py_BEGIN_ALLOW_THREADS
+ errno = 0;
#ifdef MS_WINDOWS
- n = read(self->fd, ptr, (int)size);
+ n = read(self->fd, ptr, (int)size);
#else
- n = read(self->fd, ptr, size);
+ n = read(self->fd, ptr, size);
#endif
- Py_END_ALLOW_THREADS
+ Py_END_ALLOW_THREADS
+ } while (n < 0 && errno == EINTR &&
+ !(async_err = PyErr_CheckSignals()));
} else
n = -1;
@@ -761,7 +775,8 @@ fileio_read(fileio *self, PyObject *args)
if (err == EAGAIN)
Py_RETURN_NONE;
errno = err;
- PyErr_SetFromErrno(PyExc_IOError);
+ if (!async_err)
+ PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
@@ -780,7 +795,7 @@ fileio_write(fileio *self, PyObject *args)
{
Py_buffer pbuf;
Py_ssize_t n, len;
- int err;
+ int err, async_err = 0;
if (self->fd < 0)
return err_closed();
@@ -791,24 +806,26 @@ fileio_write(fileio *self, PyObject *args)
return NULL;
if (_PyVerify_fd(self->fd)) {
- Py_BEGIN_ALLOW_THREADS
- errno = 0;
- len = pbuf.len;
+ do {
+ Py_BEGIN_ALLOW_THREADS
+ errno = 0;
+ len = pbuf.len;
#ifdef MS_WINDOWS
- if (len > 32767 && isatty(self->fd)) {
- /* Issue #11395: the Windows console returns an error (12: not
- enough space error) on writing into stdout if stdout mode is
- binary and the length is greater than 66,000 bytes (or less,
- depending on heap usage). */
- len = 32767;
- }
- else if (len > INT_MAX)
- len = INT_MAX;
- n = write(self->fd, pbuf.buf, (int)len);
+ if (len > 32767 && isatty(self->fd)) {
+ /* Issue #11395: the Windows console returns an error (12: not
+ enough space error) on writing into stdout if stdout mode is
+ binary and the length is greater than 66,000 bytes (or less,
+ depending on heap usage). */
+ len = 32767;
+ } else if (len > INT_MAX)
+ len = INT_MAX;
+ n = write(self->fd, pbuf.buf, (int)len);
#else
- n = write(self->fd, pbuf.buf, len);
+ n = write(self->fd, pbuf.buf, len);
#endif
- Py_END_ALLOW_THREADS
+ Py_END_ALLOW_THREADS
+ } while (n < 0 && errno == EINTR &&
+ !(async_err = PyErr_CheckSignals()));
} else
n = -1;
err = errno;
@@ -819,7 +836,8 @@ fileio_write(fileio *self, PyObject *args)
if (err == EAGAIN)
Py_RETURN_NONE;
errno = err;
- PyErr_SetFromErrno(PyExc_IOError);
+ if (!async_err)
+ PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
@@ -1051,12 +1069,14 @@ fileio_repr(fileio *self)
PyErr_Clear();
else
return NULL;
- res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
- self->fd, mode_string(self));
+ res = PyUnicode_FromFormat(
+ "<_io.FileIO fd=%d mode='%s' closefd=%s>",
+ self->fd, mode_string(self), self->closefd ? "True" : "False");
}
else {
- res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
- nameobj, mode_string(self));
+ res = PyUnicode_FromFormat(
+ "<_io.FileIO name=%R mode='%s' closefd=%s>",
+ nameobj, mode_string(self), self->closefd ? "True" : "False");
Py_DECREF(nameobj);
}
return res;
@@ -1218,6 +1238,7 @@ static PyGetSetDef fileio_getsetlist[] = {
};
static PyMemberDef fileio_members[] = {
+ {"_blksize", T_UINT, offsetof(fileio, blksize), 0},
{"_finalizing", T_BOOL, offsetof(fileio, finalizing), 0},
{NULL}
};
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 3f01980..9fb4ef9 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -224,8 +224,8 @@ typedef struct {
PyObject_HEAD
PyObject *decoder;
PyObject *errors;
- signed int pendingcr: 1;
- signed int translate: 1;
+ unsigned int pendingcr: 1;
+ unsigned int translate: 1;
unsigned int seennl: 3;
} nldecoder_object;
@@ -546,7 +546,7 @@ incrementalnewlinedecoder_setstate(nldecoder_object *self, PyObject *state)
if (!PyArg_Parse(state, "(OK)", &buffer, &flag))
return NULL;
- self->pendingcr = (int) flag & 1;
+ self->pendingcr = (int) (flag & 1);
flag >>= 1;
if (self->decoder != Py_None)
@@ -1441,6 +1441,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
PyObject *dec_buffer = NULL;
PyObject *dec_flags = NULL;
PyObject *input_chunk = NULL;
+ Py_buffer input_chunk_buf;
PyObject *decoded_chars, *chunk_size;
Py_ssize_t nbytes, nchars;
int eof;
@@ -1472,6 +1473,15 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
Py_DECREF(state);
return -1;
}
+
+ if (!PyBytes_Check(dec_buffer)) {
+ PyErr_Format(PyExc_TypeError,
+ "decoder getstate() should have returned a bytes "
+ "object, not '%.200s'",
+ Py_TYPE(dec_buffer)->tp_name);
+ Py_DECREF(state);
+ return -1;
+ }
Py_INCREF(dec_buffer);
Py_INCREF(dec_flags);
Py_DECREF(state);
@@ -1484,23 +1494,24 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
chunk_size = PyLong_FromSsize_t(Py_MAX(self->chunk_size, size_hint));
if (chunk_size == NULL)
goto fail;
+
input_chunk = PyObject_CallMethodObjArgs(self->buffer,
(self->has_read1 ? _PyIO_str_read1: _PyIO_str_read),
chunk_size, NULL);
Py_DECREF(chunk_size);
if (input_chunk == NULL)
goto fail;
- if (!PyBytes_Check(input_chunk)) {
+
+ if (PyObject_GetBuffer(input_chunk, &input_chunk_buf, 0) != 0) {
PyErr_Format(PyExc_TypeError,
- "underlying %s() should have returned a bytes object, "
+ "underlying %s() should have returned a bytes-like object, "
"not '%.200s'", (self->has_read1 ? "read1": "read"),
Py_TYPE(input_chunk)->tp_name);
goto fail;
}
- nbytes = PyBytes_Size(input_chunk);
+ nbytes = input_chunk_buf.len;
eof = (nbytes == 0);
-
if (Py_TYPE(self->decoder) == &PyIncrementalNewlineDecoder_Type) {
decoded_chars = _PyIncrementalNewlineDecoder_decode(
self->decoder, input_chunk, eof);
@@ -1509,6 +1520,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
decoded_chars = PyObject_CallMethodObjArgs(self->decoder,
_PyIO_str_decode, input_chunk, eof ? Py_True : Py_False, NULL);
}
+ PyBuffer_Release(&input_chunk_buf);
if (check_decoded(decoded_chars) < 0)
goto fail;
@@ -1525,18 +1537,12 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
/* At the snapshot point, len(dec_buffer) bytes before the read, the
* next input to be decoded is dec_buffer + input_chunk.
*/
- PyObject *next_input = PyNumber_Add(dec_buffer, input_chunk);
- if (next_input == NULL)
- goto fail;
- if (!PyBytes_Check(next_input)) {
- PyErr_Format(PyExc_TypeError,
- "decoder getstate() should have returned a bytes "
- "object, not '%.200s'",
- Py_TYPE(next_input)->tp_name);
- Py_DECREF(next_input);
+ PyObject *next_input = dec_buffer;
+ PyBytes_Concat(&next_input, input_chunk);
+ if (next_input == NULL) {
+ dec_buffer = NULL; /* Reference lost to PyBytes_Concat */
goto fail;
}
- Py_DECREF(dec_buffer);
Py_CLEAR(self->snapshot);
self->snapshot = Py_BuildValue("NN", dec_flags, next_input);
}
@@ -1725,7 +1731,7 @@ _PyIO_find_line_ending(
else {
/* Non-universal mode. */
Py_ssize_t readnl_len = PyUnicode_GET_LENGTH(readnl);
- char *nl = PyUnicode_DATA(readnl);
+ Py_UCS1 *nl = PyUnicode_1BYTE_DATA(readnl);
/* Assume that readnl is an ASCII character. */
assert(PyUnicode_KIND(readnl) == PyUnicode_1BYTE_KIND);
if (readnl_len == 1) {
diff --git a/Modules/_json.c b/Modules/_json.c
index f523aab..031471e 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -47,7 +47,7 @@ typedef struct _PyEncoderObject {
PyObject *item_separator;
PyObject *sort_keys;
PyObject *skipkeys;
- int fast_encode;
+ PyCFunction fast_encode;
int allow_nan;
} PyEncoderObject;
@@ -225,26 +225,116 @@ ascii_escape_unicode(PyObject *pystr)
return rval;
}
+static PyObject *
+escape_unicode(PyObject *pystr)
+{
+ /* Take a PyUnicode pystr and return a new escaped PyUnicode */
+ Py_ssize_t i;
+ Py_ssize_t input_chars;
+ Py_ssize_t output_size;
+ Py_ssize_t chars;
+ PyObject *rval;
+ void *input;
+ int kind;
+ Py_UCS4 maxchar;
+
+ if (PyUnicode_READY(pystr) == -1)
+ return NULL;
+
+ maxchar = PyUnicode_MAX_CHAR_VALUE(pystr);
+ input_chars = PyUnicode_GET_LENGTH(pystr);
+ input = PyUnicode_DATA(pystr);
+ kind = PyUnicode_KIND(pystr);
+
+ /* Compute the output size */
+ for (i = 0, output_size = 2; i < input_chars; i++) {
+ Py_UCS4 c = PyUnicode_READ(kind, input, i);
+ switch (c) {
+ case '\\': case '"': case '\b': case '\f':
+ case '\n': case '\r': case '\t':
+ output_size += 2;
+ break;
+ default:
+ if (c <= 0x1f)
+ output_size += 6;
+ else
+ output_size++;
+ }
+ }
+
+ rval = PyUnicode_New(output_size, maxchar);
+ if (rval == NULL)
+ return NULL;
+
+ kind = PyUnicode_KIND(rval);
+
+#define ENCODE_OUTPUT do { \
+ chars = 0; \
+ output[chars++] = '"'; \
+ for (i = 0; i < input_chars; i++) { \
+ Py_UCS4 c = PyUnicode_READ(kind, input, i); \
+ switch (c) { \
+ case '\\': output[chars++] = '\\'; output[chars++] = c; break; \
+ case '"': output[chars++] = '\\'; output[chars++] = c; break; \
+ case '\b': output[chars++] = '\\'; output[chars++] = 'b'; break; \
+ case '\f': output[chars++] = '\\'; output[chars++] = 'f'; break; \
+ case '\n': output[chars++] = '\\'; output[chars++] = 'n'; break; \
+ case '\r': output[chars++] = '\\'; output[chars++] = 'r'; break; \
+ case '\t': output[chars++] = '\\'; output[chars++] = 't'; break; \
+ default: \
+ if (c <= 0x1f) { \
+ output[chars++] = '\\'; \
+ output[chars++] = 'u'; \
+ output[chars++] = '0'; \
+ output[chars++] = '0'; \
+ output[chars++] = Py_hexdigits[(c >> 4) & 0xf]; \
+ output[chars++] = Py_hexdigits[(c ) & 0xf]; \
+ } else { \
+ output[chars++] = c; \
+ } \
+ } \
+ } \
+ output[chars++] = '"'; \
+ } while (0)
+
+ if (kind == PyUnicode_1BYTE_KIND) {
+ Py_UCS1 *output = PyUnicode_1BYTE_DATA(rval);
+ ENCODE_OUTPUT;
+ } else if (kind == PyUnicode_2BYTE_KIND) {
+ Py_UCS2 *output = PyUnicode_2BYTE_DATA(rval);
+ ENCODE_OUTPUT;
+ } else {
+ Py_UCS4 *output = PyUnicode_4BYTE_DATA(rval);
+ assert(kind == PyUnicode_4BYTE_KIND);
+ ENCODE_OUTPUT;
+ }
+#undef ENCODE_OUTPUT
+
+#ifdef Py_DEBUG
+ assert(_PyUnicode_CheckConsistency(rval, 1));
+#endif
+ return rval;
+}
+
static void
raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
{
- /* Use the Python function json.decoder.errmsg to raise a nice
- looking ValueError exception */
- static PyObject *errmsg_fn = NULL;
- PyObject *pymsg;
- if (errmsg_fn == NULL) {
+ /* Use JSONDecodeError exception to raise a nice looking ValueError subclass */
+ static PyObject *JSONDecodeError = NULL;
+ PyObject *exc;
+ if (JSONDecodeError == NULL) {
PyObject *decoder = PyImport_ImportModule("json.decoder");
if (decoder == NULL)
return;
- errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
+ JSONDecodeError = PyObject_GetAttrString(decoder, "JSONDecodeError");
Py_DECREF(decoder);
- if (errmsg_fn == NULL)
+ if (JSONDecodeError == NULL)
return;
}
- pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end);
- if (pymsg) {
- PyErr_SetObject(PyExc_ValueError, pymsg);
- Py_DECREF(pymsg);
+ exc = PyObject_CallFunction(JSONDecodeError, "(zOn)", msg, s, end);
+ if (exc) {
+ PyErr_SetObject(JSONDecodeError, exc);
+ Py_DECREF(exc);
}
}
@@ -537,6 +627,31 @@ py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
return rval;
}
+
+PyDoc_STRVAR(pydoc_encode_basestring,
+ "encode_basestring(string) -> string\n"
+ "\n"
+ "Return a JSON representation of a Python string"
+);
+
+static PyObject *
+py_encode_basestring(PyObject* self UNUSED, PyObject *pystr)
+{
+ PyObject *rval;
+ /* Return a JSON representation of a Python string */
+ /* METH_O */
+ if (PyUnicode_Check(pystr)) {
+ rval = escape_unicode(pystr);
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "first argument must be a string, not %.80s",
+ Py_TYPE(pystr)->tp_name);
+ return NULL;
+ }
+ return rval;
+}
+
static void
scanner_dealloc(PyObject *self)
{
@@ -1230,7 +1345,14 @@ encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
s->item_separator = item_separator;
s->sort_keys = sort_keys;
s->skipkeys = skipkeys;
- s->fast_encode = (PyCFunction_Check(s->encoder) && PyCFunction_GetFunction(s->encoder) == (PyCFunction)py_encode_basestring_ascii);
+ s->fast_encode = NULL;
+ if (PyCFunction_Check(s->encoder)) {
+ PyCFunction f = PyCFunction_GetFunction(s->encoder);
+ if (f == (PyCFunction)py_encode_basestring_ascii ||
+ f == (PyCFunction)py_encode_basestring) {
+ s->fast_encode = f;
+ }
+ }
s->allow_nan = PyObject_IsTrue(allow_nan);
Py_INCREF(s->markers);
@@ -1379,7 +1501,7 @@ encoder_encode_string(PyEncoderObject *s, PyObject *obj)
{
/* Return the JSON representation of a string */
if (s->fast_encode)
- return py_encode_basestring_ascii(NULL, obj);
+ return s->fast_encode(NULL, obj);
else
return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
}
@@ -1847,6 +1969,10 @@ static PyMethodDef speedups_methods[] = {
(PyCFunction)py_encode_basestring_ascii,
METH_O,
pydoc_encode_basestring_ascii},
+ {"encode_basestring",
+ (PyCFunction)py_encode_basestring,
+ METH_O,
+ pydoc_encode_basestring},
{"scanstring",
(PyCFunction)py_scanstring,
METH_VARARGS,
diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c
index 0137d95..66e534f 100644
--- a/Modules/_lsprof.c
+++ b/Modules/_lsprof.c
@@ -202,6 +202,8 @@ normalizeUserObj(PyObject *obj)
*/
PyObject *self = fn->m_self;
PyObject *name = PyUnicode_FromString(fn->m_ml->ml_name);
+ PyObject *modname = fn->m_module;
+
if (name != NULL) {
PyObject *mo = _PyType_Lookup(Py_TYPE(self), name);
Py_XINCREF(mo);
@@ -213,9 +215,14 @@ normalizeUserObj(PyObject *obj)
return res;
}
}
+ /* Otherwise, use __module__ */
PyErr_Clear();
- return PyUnicode_FromFormat("<built-in method %s>",
- fn->m_ml->ml_name);
+ if (modname != NULL && PyUnicode_Check(modname))
+ return PyUnicode_FromFormat("<built-in method %S.%s>",
+ modname, fn->m_ml->ml_name);
+ else
+ return PyUnicode_FromFormat("<built-in method %s>",
+ fn->m_ml->ml_name);
}
}
diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c
index c43676a..fb15233 100644
--- a/Modules/_lzmamodule.c
+++ b/Modules/_lzmamodule.c
@@ -66,6 +66,9 @@ typedef struct {
int check;
char eof;
PyObject *unused_data;
+ char needs_input;
+ uint8_t *input_buffer;
+ size_t input_buffer_size;
#ifdef WITH_THREAD
PyThread_type_lock lock;
#endif
@@ -142,10 +145,15 @@ PyLzma_Free(void *opaque, void *ptr)
#endif
static int
-grow_buffer(PyObject **buf)
+grow_buffer(PyObject **buf, Py_ssize_t max_length)
{
- size_t size = PyBytes_GET_SIZE(*buf);
- return _PyBytes_Resize(buf, size + (size >> 3) + 6);
+ Py_ssize_t size = PyBytes_GET_SIZE(*buf);
+ Py_ssize_t newsize = size + (size >> 3) + 6;
+
+ if (max_length > 0 && newsize > max_length)
+ newsize = max_length;
+
+ return _PyBytes_Resize(buf, newsize);
}
@@ -504,7 +512,7 @@ class lzma_filter_converter(CConverter):
static PyObject *
compress(Compressor *c, uint8_t *data, size_t len, lzma_action action)
{
- size_t data_size = 0;
+ Py_ssize_t data_size = 0;
PyObject *result;
result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
@@ -527,7 +535,7 @@ compress(Compressor *c, uint8_t *data, size_t len, lzma_action action)
(action == LZMA_FINISH && lzret == LZMA_STREAM_END)) {
break;
} else if (c->lzs.avail_out == 0) {
- if (grow_buffer(&result) == -1)
+ if (grow_buffer(&result, -1) == -1)
goto error;
c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
@@ -888,25 +896,33 @@ static PyTypeObject Compressor_type = {
/* LZMADecompressor class. */
-static PyObject *
-decompress(Decompressor *d, uint8_t *data, size_t len)
+/* Decompress data of length d->lzs.avail_in in d->lzs.next_in. The output
+ buffer is allocated dynamically and returned. At most max_length bytes are
+ returned, so some of the input may not be consumed. d->lzs.next_in and
+ d->lzs.avail_in are updated to reflect the consumed input. */
+static PyObject*
+decompress_buf(Decompressor *d, Py_ssize_t max_length)
{
- size_t data_size = 0;
+ Py_ssize_t data_size = 0;
PyObject *result;
-
- result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
+ lzma_stream *lzs = &d->lzs;
+
+ if (max_length < 0 || max_length >= INITIAL_BUFFER_SIZE)
+ result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
+ else
+ result = PyBytes_FromStringAndSize(NULL, max_length);
if (result == NULL)
return NULL;
- d->lzs.next_in = data;
- d->lzs.avail_in = len;
- d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result);
- d->lzs.avail_out = PyBytes_GET_SIZE(result);
+
+ lzs->next_out = (uint8_t *)PyBytes_AS_STRING(result);
+ lzs->avail_out = PyBytes_GET_SIZE(result);
+
for (;;) {
lzma_ret lzret;
Py_BEGIN_ALLOW_THREADS
- lzret = lzma_code(&d->lzs, LZMA_RUN);
- data_size = (char *)d->lzs.next_out - PyBytes_AS_STRING(result);
+ lzret = lzma_code(lzs, LZMA_RUN);
+ data_size = (char *)lzs->next_out - PyBytes_AS_STRING(result);
Py_END_ALLOW_THREADS
if (catch_lzma_error(lzret))
goto error;
@@ -914,26 +930,131 @@ decompress(Decompressor *d, uint8_t *data, size_t len)
d->check = lzma_get_check(&d->lzs);
if (lzret == LZMA_STREAM_END) {
d->eof = 1;
- if (d->lzs.avail_in > 0) {
- Py_CLEAR(d->unused_data);
- d->unused_data = PyBytes_FromStringAndSize(
- (char *)d->lzs.next_in, d->lzs.avail_in);
- if (d->unused_data == NULL)
- goto error;
- }
break;
- } else if (d->lzs.avail_in == 0) {
+ } else if (lzs->avail_in == 0) {
break;
- } else if (d->lzs.avail_out == 0) {
- if (grow_buffer(&result) == -1)
+ } else if (lzs->avail_out == 0) {
+ if (data_size == max_length)
+ break;
+ if (grow_buffer(&result, max_length) == -1)
goto error;
- d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
- d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
+ lzs->next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
+ lzs->avail_out = PyBytes_GET_SIZE(result) - data_size;
}
}
if (data_size != PyBytes_GET_SIZE(result))
if (_PyBytes_Resize(&result, data_size) == -1)
goto error;
+
+ return result;
+
+error:
+ Py_XDECREF(result);
+ return NULL;
+}
+
+static PyObject *
+decompress(Decompressor *d, uint8_t *data, size_t len, Py_ssize_t max_length)
+{
+ char input_buffer_in_use;
+ PyObject *result;
+ lzma_stream *lzs = &d->lzs;
+
+ /* Prepend unconsumed input if necessary */
+ if (lzs->next_in != NULL) {
+ size_t avail_now, avail_total;
+
+ /* Number of bytes we can append to input buffer */
+ avail_now = (d->input_buffer + d->input_buffer_size)
+ - (lzs->next_in + lzs->avail_in);
+
+ /* Number of bytes we can append if we move existing
+ contents to beginning of buffer (overwriting
+ consumed input) */
+ avail_total = d->input_buffer_size - lzs->avail_in;
+
+ if (avail_total < len) {
+ size_t offset = lzs->next_in - d->input_buffer;
+ uint8_t *tmp;
+ size_t new_size = d->input_buffer_size + len - avail_now;
+
+ /* Assign to temporary variable first, so we don't
+ lose address of allocated buffer if realloc fails */
+ tmp = PyMem_Realloc(d->input_buffer, new_size);
+ if (tmp == NULL) {
+ PyErr_SetNone(PyExc_MemoryError);
+ return NULL;
+ }
+ d->input_buffer = tmp;
+ d->input_buffer_size = new_size;
+
+ lzs->next_in = d->input_buffer + offset;
+ }
+ else if (avail_now < len) {
+ memmove(d->input_buffer, lzs->next_in,
+ lzs->avail_in);
+ lzs->next_in = d->input_buffer;
+ }
+ memcpy((void*)(lzs->next_in + lzs->avail_in), data, len);
+ lzs->avail_in += len;
+ input_buffer_in_use = 1;
+ }
+ else {
+ lzs->next_in = data;
+ lzs->avail_in = len;
+ input_buffer_in_use = 0;
+ }
+
+ result = decompress_buf(d, max_length);
+ if(result == NULL)
+ return NULL;
+
+ if (d->eof) {
+ d->needs_input = 0;
+ if (lzs->avail_in > 0) {
+ Py_CLEAR(d->unused_data);
+ d->unused_data = PyBytes_FromStringAndSize(
+ (char *)lzs->next_in, lzs->avail_in);
+ if (d->unused_data == NULL)
+ goto error;
+ }
+ }
+ else if (lzs->avail_in == 0) {
+ lzs->next_in = NULL;
+ d->needs_input = 1;
+ }
+ else {
+ d->needs_input = 0;
+
+ /* If we did not use the input buffer, we now have
+ to copy the tail from the caller's buffer into the
+ input buffer */
+ if (!input_buffer_in_use) {
+
+ /* Discard buffer if it's too small
+ (resizing it may needlessly copy the current contents) */
+ if (d->input_buffer != NULL &&
+ d->input_buffer_size < lzs->avail_in) {
+ PyMem_Free(d->input_buffer);
+ d->input_buffer = NULL;
+ }
+
+ /* Allocate if necessary */
+ if (d->input_buffer == NULL) {
+ d->input_buffer = PyMem_Malloc(lzs->avail_in);
+ if (d->input_buffer == NULL) {
+ PyErr_SetNone(PyExc_MemoryError);
+ goto error;
+ }
+ d->input_buffer_size = lzs->avail_in;
+ }
+
+ /* Copy tail */
+ memcpy(d->input_buffer, lzs->next_in, lzs->avail_in);
+ lzs->next_in = d->input_buffer;
+ }
+ }
+
return result;
error:
@@ -946,20 +1067,27 @@ _lzma.LZMADecompressor.decompress
self: self(type="Decompressor *")
data: Py_buffer
- /
+ max_length: Py_ssize_t=-1
-Provide data to the decompressor object.
+Decompress *data*, returning uncompressed data as bytes.
-Returns a chunk of decompressed data if possible, or b'' otherwise.
+If *max_length* is nonnegative, returns at most *max_length* bytes of
+decompressed data. If this limit is reached and further output can be
+produced, *self.needs_input* will be set to ``False``. In this case, the next
+call to *decompress()* may provide *data* as b'' to obtain more of the output.
-Attempting to decompress data after the end of stream is reached
-raises an EOFError. Any data found after the end of the stream
-is ignored and saved in the unused_data attribute.
+If all of the input data was decompressed and returned (either because this
+was less than *max_length* bytes, or because *max_length* was negative),
+*self.needs_input* will be set to True.
+
+Attempting to decompress data after the end of stream is reached raises an
+EOFError. Any data found after the end of the stream is ignored and saved in
+the unused_data attribute.
[clinic start generated code]*/
static PyObject *
-_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data)
-/*[clinic end generated code: output=d86e78da7ff0ff21 input=50c4768b821bf0ef]*/
+_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data, Py_ssize_t max_length)
+/*[clinic end generated code: output=1532a5bb23629001 input=262e4e217f49039b]*/
{
PyObject *result = NULL;
@@ -967,7 +1095,7 @@ _lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data)
if (self->eof)
PyErr_SetString(PyExc_EOFError, "Already at end of stream");
else
- result = decompress(self, data->buf, data->len);
+ result = decompress(self, data->buf, data->len, max_length);
RELEASE_LOCK(self);
return result;
}
@@ -1055,6 +1183,7 @@ _lzma_LZMADecompressor___init___impl(Decompressor *self, int format, PyObject *m
self->alloc.alloc = PyLzma_Malloc;
self->alloc.free = PyLzma_Free;
self->lzs.allocator = &self->alloc;
+ self->lzs.next_in = NULL;
#ifdef WITH_THREAD
self->lock = PyThread_allocate_lock();
@@ -1065,6 +1194,9 @@ _lzma_LZMADecompressor___init___impl(Decompressor *self, int format, PyObject *m
#endif
self->check = LZMA_CHECK_UNKNOWN;
+ self->needs_input = 1;
+ self->input_buffer = NULL;
+ self->input_buffer_size = 0;
self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
if (self->unused_data == NULL)
goto error;
@@ -1113,6 +1245,9 @@ error:
static void
Decompressor_dealloc(Decompressor *self)
{
+ if(self->input_buffer != NULL)
+ PyMem_Free(self->input_buffer);
+
lzma_end(&self->lzs);
Py_CLEAR(self->unused_data);
#ifdef WITH_THREAD
@@ -1134,6 +1269,9 @@ PyDoc_STRVAR(Decompressor_check_doc,
PyDoc_STRVAR(Decompressor_eof_doc,
"True if the end-of-stream marker has been reached.");
+PyDoc_STRVAR(Decompressor_needs_input_doc,
+"True if more input is needed before more decompressed data can be produced.");
+
PyDoc_STRVAR(Decompressor_unused_data_doc,
"Data found after the end of the compressed stream.");
@@ -1142,6 +1280,8 @@ static PyMemberDef Decompressor_members[] = {
Decompressor_check_doc},
{"eof", T_BOOL, offsetof(Decompressor, eof), READONLY,
Decompressor_eof_doc},
+ {"needs_input", T_BOOL, offsetof(Decompressor, needs_input), READONLY,
+ Decompressor_needs_input_doc},
{"unused_data", T_OBJECT_EX, offsetof(Decompressor, unused_data), READONLY,
Decompressor_unused_data_doc},
{NULL}
diff --git a/Modules/_operator.c b/Modules/_operator.c
index adeb99e..8f524a6 100644
--- a/Modules/_operator.c
+++ b/Modules/_operator.c
@@ -69,6 +69,7 @@ spami(truth , PyObject_IsTrue)
spam2(op_add , PyNumber_Add)
spam2(op_sub , PyNumber_Subtract)
spam2(op_mul , PyNumber_Multiply)
+spam2(op_matmul , PyNumber_MatrixMultiply)
spam2(op_floordiv , PyNumber_FloorDivide)
spam2(op_truediv , PyNumber_TrueDivide)
spam2(op_mod , PyNumber_Remainder)
@@ -86,6 +87,7 @@ spam2(op_or_ , PyNumber_Or)
spam2(op_iadd , PyNumber_InPlaceAdd)
spam2(op_isub , PyNumber_InPlaceSubtract)
spam2(op_imul , PyNumber_InPlaceMultiply)
+spam2(op_imatmul , PyNumber_InPlaceMatrixMultiply)
spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide)
spam2(op_itruediv , PyNumber_InPlaceTrueDivide)
spam2(op_imod , PyNumber_InPlaceRemainder)
@@ -239,7 +241,7 @@ PyDoc_STRVAR(compare_digest__doc__,
"Return 'a == b'. This function uses an approach designed to prevent\n"
"timing analysis, making it appropriate for cryptography.\n"
"a and b must both be of the same type: either str (ASCII only),\n"
-"or any type that supports the buffer protocol (e.g. bytes).\n"
+"or any bytes-like object.\n"
"\n"
"Note: If a and b are of different lengths, or if an error occurs,\n"
"a timing attack could theoretically reveal information about the\n"
@@ -343,6 +345,7 @@ spam2o(index, "index(a) -- Same as a.__index__()")
spam2(add, "add(a, b) -- Same as a + b.")
spam2(sub, "sub(a, b) -- Same as a - b.")
spam2(mul, "mul(a, b) -- Same as a * b.")
+spam2(matmul, "matmul(a, b) -- Same as a @ b.")
spam2(floordiv, "floordiv(a, b) -- Same as a // b.")
spam2(truediv, "truediv(a, b) -- Same as a / b.")
spam2(mod, "mod(a, b) -- Same as a % b.")
@@ -360,6 +363,7 @@ spam2(or_, "or_(a, b) -- Same as a | b.")
spam2(iadd, "a = iadd(a, b) -- Same as a += b.")
spam2(isub, "a = isub(a, b) -- Same as a -= b.")
spam2(imul, "a = imul(a, b) -- Same as a *= b.")
+spam2(imatmul, "a = imatmul(a, b) -- Same as a @= b.")
spam2(ifloordiv, "a = ifloordiv(a, b) -- Same as a //= b.")
spam2(itruediv, "a = itruediv(a, b) -- Same as a /= b")
spam2(imod, "a = imod(a, b) -- Same as a %= b.")
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 10eb513..51e2f83 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -420,22 +420,22 @@ static int
Pdata_grow(Pdata *self)
{
PyObject **data = self->data;
- Py_ssize_t allocated = self->allocated;
- Py_ssize_t new_allocated;
+ size_t allocated = (size_t)self->allocated;
+ size_t new_allocated;
new_allocated = (allocated >> 3) + 6;
/* check for integer overflow */
- if (new_allocated > PY_SSIZE_T_MAX - allocated)
+ if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
goto nomemory;
new_allocated += allocated;
- if (new_allocated > (PY_SSIZE_T_MAX / sizeof(PyObject *)))
+ if (new_allocated > ((size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)))
goto nomemory;
data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *));
if (data == NULL)
goto nomemory;
self->data = data;
- self->allocated = new_allocated;
+ self->allocated = (Py_ssize_t)new_allocated;
return 0;
nomemory:
@@ -850,7 +850,7 @@ _Pickler_ClearBuffer(PicklerObject *self)
static void
_write_size64(char *out, size_t value)
{
- int i;
+ size_t i;
assert(sizeof(size_t) <= 8);
@@ -1456,7 +1456,7 @@ memo_get(PicklerObject *self, PyObject *key)
pdata[1] = (unsigned char)(*value & 0xff);
len = 2;
}
- else if (*value <= 0xffffffffL) {
+ else if ((size_t)*value <= 0xffffffffUL) {
pdata[0] = LONG_BINGET;
pdata[1] = (unsigned char)(*value & 0xff);
pdata[2] = (unsigned char)((*value >> 8) & 0xff);
@@ -1513,7 +1513,7 @@ memo_put(PicklerObject *self, PyObject *obj)
pdata[1] = (unsigned char)idx;
len = 2;
}
- else if (idx <= 0xffffffffL) {
+ else if ((size_t)idx <= 0xffffffffUL) {
pdata[0] = LONG_BINPUT;
pdata[1] = (unsigned char)(idx & 0xff);
pdata[2] = (unsigned char)((idx >> 8) & 0xff);
@@ -1535,69 +1535,109 @@ memo_put(PicklerObject *self, PyObject *obj)
}
static PyObject *
-getattribute(PyObject *obj, PyObject *name, int allow_qualname) {
- PyObject *dotted_path;
- Py_ssize_t i;
+get_dotted_path(PyObject *obj, PyObject *name, int allow_qualname) {
_Py_static_string(PyId_dot, ".");
_Py_static_string(PyId_locals, "<locals>");
+ PyObject *dotted_path;
+ Py_ssize_t i, n;
dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
- if (dotted_path == NULL) {
+ if (dotted_path == NULL)
return NULL;
- }
- assert(Py_SIZE(dotted_path) >= 1);
- if (!allow_qualname && Py_SIZE(dotted_path) > 1) {
- PyErr_Format(PyExc_AttributeError,
- "Can't get qualified attribute %R on %R;"
- "use protocols >= 4 to enable support",
- name, obj);
+ n = PyList_GET_SIZE(dotted_path);
+ assert(n >= 1);
+ if (!allow_qualname && n > 1) {
+ if (obj == NULL)
+ PyErr_Format(PyExc_AttributeError,
+ "Can't pickle qualified object %R; "
+ "use protocols >= 4 to enable support",
+ name);
+ else
+ PyErr_Format(PyExc_AttributeError,
+ "Can't pickle qualified attribute %R on %R; "
+ "use protocols >= 4 to enable support",
+ name, obj);
Py_DECREF(dotted_path);
return NULL;
}
- Py_INCREF(obj);
- for (i = 0; i < Py_SIZE(dotted_path); i++) {
+ for (i = 0; i < n; i++) {
PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
- PyObject *tmp;
PyObject *result = PyUnicode_RichCompare(
subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
int is_equal = (result == Py_True);
assert(PyBool_Check(result));
Py_DECREF(result);
if (is_equal) {
- PyErr_Format(PyExc_AttributeError,
- "Can't get local attribute %R on %R", name, obj);
+ if (obj == NULL)
+ PyErr_Format(PyExc_AttributeError,
+ "Can't pickle local object %R", name);
+ else
+ PyErr_Format(PyExc_AttributeError,
+ "Can't pickle local attribute %R on %R", name, obj);
Py_DECREF(dotted_path);
- Py_DECREF(obj);
return NULL;
}
- tmp = PyObject_GetAttr(obj, subpath);
+ }
+ return dotted_path;
+}
+
+static PyObject *
+get_deep_attribute(PyObject *obj, PyObject *names)
+{
+ Py_ssize_t i, n;
+
+ assert(PyList_CheckExact(names));
+ Py_INCREF(obj);
+ n = PyList_GET_SIZE(names);
+ for (i = 0; i < n; i++) {
+ PyObject *name = PyList_GET_ITEM(names, i);
+ PyObject *tmp;
+ tmp = PyObject_GetAttr(obj, name);
Py_DECREF(obj);
- if (tmp == NULL) {
- if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
- PyErr_Clear();
- PyErr_Format(PyExc_AttributeError,
- "Can't get attribute %R on %R", name, obj);
- }
- Py_DECREF(dotted_path);
+ if (tmp == NULL)
return NULL;
- }
obj = tmp;
}
- Py_DECREF(dotted_path);
return obj;
}
+static void
+reformat_attribute_error(PyObject *obj, PyObject *name)
+{
+ if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ PyErr_Clear();
+ PyErr_Format(PyExc_AttributeError,
+ "Can't get attribute %R on %R", name, obj);
+ }
+}
+
+
+static PyObject *
+getattribute(PyObject *obj, PyObject *name, int allow_qualname)
+{
+ PyObject *dotted_path, *attr;
+
+ dotted_path = get_dotted_path(obj, name, allow_qualname);
+ if (dotted_path == NULL)
+ return NULL;
+ attr = get_deep_attribute(obj, dotted_path);
+ Py_DECREF(dotted_path);
+ if (attr == NULL)
+ reformat_attribute_error(obj, name);
+ return attr;
+}
+
static PyObject *
whichmodule(PyObject *global, PyObject *global_name, int allow_qualname)
{
PyObject *module_name;
PyObject *modules_dict;
PyObject *module;
- PyObject *obj;
- Py_ssize_t i, j;
+ Py_ssize_t i;
_Py_IDENTIFIER(__module__);
_Py_IDENTIFIER(modules);
_Py_IDENTIFIER(__main__);
+ PyObject *dotted_path;
module_name = _PyObject_GetAttrId(global, &PyId___module__);
@@ -1616,43 +1656,49 @@ whichmodule(PyObject *global, PyObject *global_name, int allow_qualname)
}
assert(module_name == NULL);
+ /* Fallback on walking sys.modules */
modules_dict = _PySys_GetObjectId(&PyId_modules);
if (modules_dict == NULL) {
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
return NULL;
}
+ dotted_path = get_dotted_path(NULL, global_name, allow_qualname);
+ if (dotted_path == NULL)
+ return NULL;
+
i = 0;
- while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) {
- PyObject *result = PyUnicode_RichCompare(
- module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ);
- int is_equal = (result == Py_True);
- assert(PyBool_Check(result));
- Py_DECREF(result);
- if (is_equal)
+ while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
+ PyObject *candidate;
+ if (PyUnicode_Check(module_name) &&
+ !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
continue;
if (module == Py_None)
continue;
- obj = getattribute(module, global_name, allow_qualname);
- if (obj == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
+ candidate = get_deep_attribute(module, dotted_path);
+ if (candidate == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ Py_DECREF(dotted_path);
return NULL;
+ }
PyErr_Clear();
continue;
}
- if (obj == global) {
- Py_DECREF(obj);
+ if (candidate == global) {
Py_INCREF(module_name);
+ Py_DECREF(dotted_path);
+ Py_DECREF(candidate);
return module_name;
}
- Py_DECREF(obj);
+ Py_DECREF(candidate);
}
/* If no module is found, use __main__. */
module_name = _PyUnicode_FromId(&PyId___main__);
Py_INCREF(module_name);
+ Py_DECREF(dotted_path);
return module_name;
}
@@ -1933,7 +1979,7 @@ save_float(PicklerObject *self, PyObject *obj)
if (_Pickler_Write(self, &op, 1) < 0)
goto done;
- buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
+ buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
if (!buf) {
PyErr_NoMemory();
goto done;
@@ -2013,7 +2059,7 @@ save_bytes(PicklerObject *self, PyObject *obj)
header[1] = (unsigned char)size;
len = 2;
}
- else if (size <= 0xffffffffL) {
+ else if ((size_t)size <= 0xffffffffUL) {
header[0] = BINBYTES;
header[1] = (unsigned char)(size & 0xff);
header[2] = (unsigned char)((size >> 8) & 0xff);
@@ -2050,9 +2096,10 @@ save_bytes(PicklerObject *self, PyObject *obj)
static PyObject *
raw_unicode_escape(PyObject *obj)
{
- PyObject *repr, *result;
+ PyObject *repr;
char *p;
- Py_ssize_t i, size, expandsize;
+ Py_ssize_t i, size;
+ size_t expandsize;
void *data;
unsigned int kind;
@@ -2067,15 +2114,16 @@ raw_unicode_escape(PyObject *obj)
else
expandsize = 6;
- if (size > PY_SSIZE_T_MAX / expandsize)
+ if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize)
return PyErr_NoMemory();
- repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
+ repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
if (repr == NULL)
return NULL;
if (size == 0)
- goto done;
+ return repr;
+ assert(Py_REFCNT(repr) == 1);
- p = PyByteArray_AS_STRING(repr);
+ p = PyBytes_AS_STRING(repr);
for (i=0; i < size; i++) {
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
/* Map 32-bit characters to '\Uxxxxxxxx' */
@@ -2104,12 +2152,10 @@ raw_unicode_escape(PyObject *obj)
else
*p++ = (char) ch;
}
- size = p - PyByteArray_AS_STRING(repr);
-
-done:
- result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
- Py_DECREF(repr);
- return result;
+ size = p - PyBytes_AS_STRING(repr);
+ if (_PyBytes_Resize(&repr, size) < 0)
+ return NULL;
+ return repr;
}
static int
@@ -2118,12 +2164,13 @@ write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
char header[9];
Py_ssize_t len;
+ assert(size >= 0);
if (size <= 0xff && self->proto >= 4) {
header[0] = SHORT_BINUNICODE;
header[1] = (unsigned char)(size & 0xff);
len = 2;
}
- else if (size <= 0xffffffffUL) {
+ else if ((size_t)size <= 0xffffffffUL) {
header[0] = BINUNICODE;
header[1] = (unsigned char)(size & 0xff);
header[2] = (unsigned char)((size >> 8) & 0xff);
@@ -3459,20 +3506,19 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
}
PyErr_Clear();
}
- else if (self->proto >= 4) {
- _Py_IDENTIFIER(__newobj_ex__);
- use_newobj_ex = PyUnicode_Check(name) &&
- PyUnicode_Compare(
- name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
- Py_DECREF(name);
- }
- else {
- _Py_IDENTIFIER(__newobj__);
- use_newobj = PyUnicode_Check(name) &&
- PyUnicode_Compare(
- name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
- Py_DECREF(name);
+ else if (PyUnicode_Check(name)) {
+ if (self->proto >= 4) {
+ _Py_IDENTIFIER(__newobj_ex__);
+ use_newobj_ex = PyUnicode_Compare(
+ name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
+ }
+ if (!use_newobj_ex) {
+ _Py_IDENTIFIER(__newobj__);
+ use_newobj = PyUnicode_Compare(
+ name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
+ }
}
+ Py_XDECREF(name);
}
if (use_newobj_ex) {
@@ -4533,10 +4579,10 @@ static Py_ssize_t
calc_binsize(char *bytes, int nbytes)
{
unsigned char *s = (unsigned char *)bytes;
- int i;
+ Py_ssize_t i;
size_t x = 0;
- for (i = 0; i < nbytes && i < sizeof(size_t); i++) {
+ for (i = 0; i < nbytes && (size_t)i < sizeof(size_t); i++) {
x |= (size_t) s[i] << (8 * i);
}
@@ -4555,7 +4601,7 @@ static long
calc_binint(char *bytes, int nbytes)
{
unsigned char *s = (unsigned char *)bytes;
- int i;
+ Py_ssize_t i;
long x = 0;
for (i = 0; i < nbytes; i++) {
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index db96b02..7fe00e3 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -46,7 +46,7 @@ static pysqlite_StatementKind detect_statement_type(const char* statement)
dst = buf;
*dst = 0;
- while (Py_ISALPHA(*src) && dst - buf < sizeof(buf) - 2) {
+ while (Py_ISALPHA(*src) && (dst - buf) < ((Py_ssize_t)sizeof(buf) - 2)) {
*dst++ = Py_TOLOWER(*src++);
}
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 300d883..9550d97 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -97,48 +97,25 @@ static char copyright[] =
/* -------------------------------------------------------------------- */
/* search engine state */
-/* default character predicates (run sre_chars.py to regenerate tables) */
-
-#define SRE_DIGIT_MASK 1
-#define SRE_SPACE_MASK 2
-#define SRE_LINEBREAK_MASK 4
-#define SRE_ALNUM_MASK 8
-#define SRE_WORD_MASK 16
-
-/* FIXME: this assumes ASCII. create tables in init_sre() instead */
-
-static char sre_char_info[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 2,
-2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
-0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 25, 25, 25, 25, 25, 25, 25,
-25, 25, 0, 0, 0, 0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
-24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0,
-0, 0, 16, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
-24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0 };
-
-static char sre_char_lower[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
-27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
-44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
-108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
-122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
-106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
-120, 121, 122, 123, 124, 125, 126, 127 };
-
#define SRE_IS_DIGIT(ch)\
- ((ch) < 128 ? (sre_char_info[(ch)] & SRE_DIGIT_MASK) : 0)
+ ((ch) < 128 && Py_ISDIGIT(ch))
#define SRE_IS_SPACE(ch)\
- ((ch) < 128 ? (sre_char_info[(ch)] & SRE_SPACE_MASK) : 0)
+ ((ch) < 128 && Py_ISSPACE(ch))
#define SRE_IS_LINEBREAK(ch)\
- ((ch) < 128 ? (sre_char_info[(ch)] & SRE_LINEBREAK_MASK) : 0)
+ ((ch) == '\n')
#define SRE_IS_ALNUM(ch)\
- ((ch) < 128 ? (sre_char_info[(ch)] & SRE_ALNUM_MASK) : 0)
+ ((ch) < 128 && Py_ISALNUM(ch))
#define SRE_IS_WORD(ch)\
- ((ch) < 128 ? (sre_char_info[(ch)] & SRE_WORD_MASK) : 0)
+ ((ch) < 128 && (Py_ISALNUM(ch) || (ch) == '_'))
static unsigned int sre_lower(unsigned int ch)
{
- return ((ch) < 128 ? (unsigned int)sre_char_lower[ch] : ch);
+ return ((ch) < 128 ? Py_TOLOWER(ch) : ch);
+}
+
+static unsigned int sre_upper(unsigned int ch)
+{
+ return ((ch) < 128 ? Py_TOUPPER(ch) : ch);
}
/* locale-specific character predicates */
@@ -152,6 +129,11 @@ static unsigned int sre_lower_locale(unsigned int ch)
return ((ch) < 256 ? (unsigned int)tolower((ch)) : ch);
}
+static unsigned int sre_upper_locale(unsigned int ch)
+{
+ return ((ch) < 256 ? (unsigned int)toupper((ch)) : ch);
+}
+
/* unicode-specific character predicates */
#define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDECIMAL(ch)
@@ -165,6 +147,11 @@ static unsigned int sre_lower_unicode(unsigned int ch)
return (unsigned int) Py_UNICODE_TOLOWER(ch);
}
+static unsigned int sre_upper_unicode(unsigned int ch)
+{
+ return (unsigned int) Py_UNICODE_TOUPPER(ch);
+}
+
LOCAL(int)
sre_category(SRE_CODE category, unsigned int ch)
{
@@ -357,6 +344,11 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
memset(state, 0, sizeof(SRE_STATE));
+ state->mark = PyMem_New(void *, pattern->groups * 2);
+ if (!state->mark) {
+ PyErr_NoMemory();
+ goto err;
+ }
state->lastmark = -1;
state->lastindex = -1;
@@ -400,15 +392,23 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
state->pos = start;
state->endpos = end;
- if (pattern->flags & SRE_FLAG_LOCALE)
+ if (pattern->flags & SRE_FLAG_LOCALE) {
state->lower = sre_lower_locale;
- else if (pattern->flags & SRE_FLAG_UNICODE)
+ state->upper = sre_upper_locale;
+ }
+ else if (pattern->flags & SRE_FLAG_UNICODE) {
state->lower = sre_lower_unicode;
- else
+ state->upper = sre_upper_unicode;
+ }
+ else {
state->lower = sre_lower;
+ state->upper = sre_upper;
+ }
return string;
err:
+ PyMem_Del(state->mark);
+ state->mark = NULL;
if (state->buffer.buf)
PyBuffer_Release(&state->buffer);
return NULL;
@@ -421,6 +421,8 @@ state_fini(SRE_STATE* state)
PyBuffer_Release(&state->buffer);
Py_XDECREF(state->string);
data_stack_dealloc(state);
+ PyMem_Del(state->mark);
+ state->mark = NULL;
}
/* calculate offset from start of string */
@@ -560,6 +562,7 @@ pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs)
PyObject *pattern = NULL;
SRE_STATE state;
Py_ssize_t status;
+ PyObject *match;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"|Onn$O:match", _keywords,
@@ -579,12 +582,14 @@ pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs)
status = sre_match(&state, PatternObject_GetCode(self), 0);
TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
- if (PyErr_Occurred())
+ if (PyErr_Occurred()) {
+ state_fini(&state);
return NULL;
+ }
+ match = pattern_new_match(self, &state, status);
state_fini(&state);
-
- return (PyObject *)pattern_new_match(self, &state, status);
+ return match;
}
static PyObject*
@@ -592,6 +597,7 @@ pattern_fullmatch(PatternObject* self, PyObject* args, PyObject* kw)
{
SRE_STATE state;
Py_ssize_t status;
+ PyObject *match;
PyObject *string = NULL, *string2 = NULL;
Py_ssize_t start = 0;
@@ -616,12 +622,14 @@ pattern_fullmatch(PatternObject* self, PyObject* args, PyObject* kw)
status = sre_match(&state, PatternObject_GetCode(self), 1);
TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
- if (PyErr_Occurred())
+ if (PyErr_Occurred()) {
+ state_fini(&state);
return NULL;
+ }
+ match = pattern_new_match(self, &state, status);
state_fini(&state);
-
- return pattern_new_match(self, &state, status);
+ return match;
}
static PyObject*
@@ -629,6 +637,7 @@ pattern_search(PatternObject* self, PyObject* args, PyObject* kw)
{
SRE_STATE state;
Py_ssize_t status;
+ PyObject *match;
PyObject *string = NULL, *string2 = NULL;
Py_ssize_t start = 0;
@@ -652,12 +661,14 @@ pattern_search(PatternObject* self, PyObject* args, PyObject* kw)
TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
- state_fini(&state);
-
- if (PyErr_Occurred())
+ if (PyErr_Occurred()) {
+ state_fini(&state);
return NULL;
+ }
- return pattern_new_match(self, &state, status);
+ match = pattern_new_match(self, &state, status);
+ state_fini(&state);
+ return match;
}
static PyObject*
@@ -852,6 +863,19 @@ pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
if (!string)
return NULL;
+ assert(self->codesize != 0);
+ if (self->code[0] != SRE_OP_INFO || self->code[3] == 0) {
+ if (self->code[0] == SRE_OP_INFO && self->code[4] == 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "split() requires a non-empty pattern match.");
+ return NULL;
+ }
+ if (PyErr_WarnEx(PyExc_FutureWarning,
+ "split() requires a non-empty pattern match.",
+ 1) < 0)
+ return NULL;
+ }
+
string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX);
if (!string)
return NULL;
@@ -1236,7 +1260,7 @@ pattern_repr(PatternObject *obj)
};
PyObject *result = NULL;
PyObject *flag_items;
- int i;
+ size_t i;
int flags = obj->flags;
/* Omit re.UNICODE for valid string patterns. */
@@ -1417,7 +1441,7 @@ _compile(PyObject* self_, PyObject* args)
PyObject* groupindex = NULL;
PyObject* indexgroup = NULL;
- if (!PyArg_ParseTuple(args, "OiO!|nOO", &pattern, &flags,
+ if (!PyArg_ParseTuple(args, "OiO!nOO", &pattern, &flags,
&PyList_Type, &code, &groups,
&groupindex, &indexgroup))
return NULL;
@@ -1577,6 +1601,7 @@ _validate_charset(SRE_CODE *code, SRE_CODE *end)
break;
case SRE_OP_RANGE:
+ case SRE_OP_RANGE_IGNORE:
GET_ARG;
GET_ARG;
break;
@@ -1933,10 +1958,9 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
static int
_validate_outer(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
{
- if (groups < 0 || groups > 100 || code >= end || end[-1] != SRE_OP_SUCCESS)
+ if (groups < 0 || (size_t)groups > SRE_MAXGROUPS ||
+ code >= end || end[-1] != SRE_OP_SUCCESS)
FAIL;
- if (groups == 0) /* fix for simplejson */
- groups = 100; /* 100 groups should always be safe */
return _validate_inner(code, end-1, groups);
}
@@ -2747,6 +2771,12 @@ PyMODINIT_FUNC PyInit__sre(void)
Py_DECREF(x);
}
+ x = PyLong_FromUnsignedLong(SRE_MAXGROUPS);
+ if (x) {
+ PyDict_SetItemString(d, "MAXGROUPS", x);
+ Py_DECREF(x);
+ }
+
x = PyUnicode_FromString(copyright);
if (x) {
PyDict_SetItemString(d, "copyright", x);
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 914d5aa..437d2b2 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -64,6 +64,7 @@ static PySocketModule_APIObject PySocketModule;
#include "openssl/ssl.h"
#include "openssl/err.h"
#include "openssl/rand.h"
+#include "openssl/bio.h"
/* SSL error object */
static PyObject *PySSLErrorObject;
@@ -108,6 +109,11 @@ struct py_ssl_library_code {
# define HAVE_SNI 0
#endif
+/* ALPN added in OpenSSL 1.0.2 */
+#if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(OPENSSL_NO_TLSEXT)
+# define HAVE_ALPN
+#endif
+
enum py_ssl_error {
/* these mirror ssl.h */
PY_SSL_ERROR_NONE,
@@ -161,13 +167,6 @@ static unsigned int _ssl_locks_count = 0;
#define X509_NAME_MAXLEN 256
-/* RAND_* APIs got added to OpenSSL in 0.9.5 */
-#if OPENSSL_VERSION_NUMBER >= 0x0090500fL
-# define HAVE_OPENSSL_RAND 1
-#else
-# undef HAVE_OPENSSL_RAND
-#endif
-
/* SSL_CTX_clear_options() and SSL_clear_options() were first added in
* OpenSSL 0.9.8m but do not appear in some 0.9.9-dev versions such the
* 0.9.9 from "May 2008" that NetBSD 5.0 uses. */
@@ -181,36 +180,18 @@ static unsigned int _ssl_locks_count = 0;
* older SSL, but let's be safe */
#define PySSL_CB_MAXLEN 128
-/* SSL_get_finished got added to OpenSSL in 0.9.5 */
-#if OPENSSL_VERSION_NUMBER >= 0x0090500fL
-# define HAVE_OPENSSL_FINISHED 1
-#else
-# define HAVE_OPENSSL_FINISHED 0
-#endif
-
-/* ECDH support got added to OpenSSL in 0.9.8 */
-#if OPENSSL_VERSION_NUMBER < 0x0090800fL && !defined(OPENSSL_NO_ECDH)
-# define OPENSSL_NO_ECDH
-#endif
-
-/* compression support got added to OpenSSL in 0.9.8 */
-#if OPENSSL_VERSION_NUMBER < 0x0090800fL && !defined(OPENSSL_NO_COMP)
-# define OPENSSL_NO_COMP
-#endif
-
-/* X509_VERIFY_PARAM got added to OpenSSL in 0.9.8 */
-#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
-# define HAVE_OPENSSL_VERIFY_PARAM
-#endif
-
typedef struct {
PyObject_HEAD
SSL_CTX *ctx;
#ifdef OPENSSL_NPN_NEGOTIATED
- char *npn_protocols;
+ unsigned char *npn_protocols;
int npn_protocols_len;
#endif
+#ifdef HAVE_ALPN
+ unsigned char *alpn_protocols;
+ int alpn_protocols_len;
+#endif
#ifndef OPENSSL_NO_TLSEXT
PyObject *set_hostname;
#endif
@@ -226,10 +207,19 @@ typedef struct {
char shutdown_seen_zero;
char handshake_done;
enum py_ssl_server_or_client socket_type;
+ PyObject *owner; /* Python level "owner" passed to servername callback */
+ PyObject *server_hostname;
} PySSLSocket;
+typedef struct {
+ PyObject_HEAD
+ BIO *bio;
+ int eof_written;
+} PySSLMemoryBIO;
+
static PyTypeObject PySSLContext_Type;
static PyTypeObject PySSLSocket_Type;
+static PyTypeObject PySSLMemoryBIO_Type;
static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args);
static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args);
@@ -240,6 +230,7 @@ static PyObject *PySSL_cipher(PySSLSocket *self);
#define PySSLContext_Check(v) (Py_TYPE(v) == &PySSLContext_Type)
#define PySSLSocket_Check(v) (Py_TYPE(v) == &PySSLSocket_Type)
+#define PySSLMemoryBIO_Check(v) (Py_TYPE(v) == &PySSLMemoryBIO_Type)
typedef enum {
SOCKET_IS_NONBLOCKING,
@@ -251,11 +242,12 @@ typedef enum {
} timeout_state;
/* Wrap error strings with filename and line # */
-#define STRINGIFY1(x) #x
-#define STRINGIFY2(x) STRINGIFY1(x)
#define ERRSTR1(x,y,z) (x ":" y ": " z)
-#define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x)
+#define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x)
+/* Get the socket from a PySSLSocket, if it has one */
+#define GET_SOCKET(obj) ((obj)->Socket ? \
+ (PySocketSockObject *) PyWeakref_GetObject((obj)->Socket) : NULL)
/*
* SSL errors.
@@ -419,13 +411,12 @@ PySSL_SetError(PySSLSocket *obj, int ret, char *filename, int lineno)
case SSL_ERROR_SYSCALL:
{
if (e == 0) {
- PySocketSockObject *s
- = (PySocketSockObject *) PyWeakref_GetObject(obj->Socket);
+ PySocketSockObject *s = GET_SOCKET(obj);
if (ret == 0 || (((PyObject *)s) == Py_None)) {
p = PY_SSL_ERROR_EOF;
type = PySSLEOFErrorObject;
errstr = "EOF occurred in violation of protocol";
- } else if (ret == -1) {
+ } else if (s && ret == -1) {
/* underlying BIO reported an I/O error */
Py_INCREF(s);
ERR_clear_error();
@@ -479,10 +470,12 @@ _setSSLError (char *errstr, int errcode, char *filename, int lineno) {
static PySSLSocket *
newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
enum py_ssl_server_or_client socket_type,
- char *server_hostname)
+ char *server_hostname,
+ PySSLMemoryBIO *inbio, PySSLMemoryBIO *outbio)
{
PySSLSocket *self;
SSL_CTX *ctx = sslctx->ctx;
+ PyObject *hostname;
long mode;
self = PyObject_New(PySSLSocket, &PySSLSocket_Type);
@@ -495,6 +488,18 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
self->ctx = sslctx;
self->shutdown_seen_zero = 0;
self->handshake_done = 0;
+ self->owner = NULL;
+ if (server_hostname != NULL) {
+ hostname = PyUnicode_Decode(server_hostname, strlen(server_hostname),
+ "idna", "strict");
+ if (hostname == NULL) {
+ Py_DECREF(self);
+ return NULL;
+ }
+ self->server_hostname = hostname;
+ } else
+ self->server_hostname = NULL;
+
Py_INCREF(sslctx);
/* Make sure the SSL error state is initialized */
@@ -504,8 +509,17 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
PySSL_BEGIN_ALLOW_THREADS
self->ssl = SSL_new(ctx);
PySSL_END_ALLOW_THREADS
- SSL_set_app_data(self->ssl,self);
- SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int));
+ SSL_set_app_data(self->ssl, self);
+ if (sock) {
+ SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int));
+ } else {
+ /* BIOs are reference counted and SSL_set_bio borrows our reference.
+ * To prevent a double free in memory_bio_dealloc() we need to take an
+ * extra reference here. */
+ CRYPTO_add(&inbio->bio->references, 1, CRYPTO_LOCK_BIO);
+ CRYPTO_add(&outbio->bio->references, 1, CRYPTO_LOCK_BIO);
+ SSL_set_bio(self->ssl, inbio->bio, outbio->bio);
+ }
mode = SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
#ifdef SSL_MODE_AUTO_RETRY
mode |= SSL_MODE_AUTO_RETRY;
@@ -520,7 +534,7 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
/* If the socket is in non-blocking mode or timeout mode, set the BIO
* to non-blocking mode (blocking is the default)
*/
- if (sock->sock_timeout >= 0.0) {
+ if (sock && sock->sock_timeout >= 0.0) {
BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
}
@@ -533,10 +547,13 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
PySSL_END_ALLOW_THREADS
self->socket_type = socket_type;
- self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL);
- if (self->Socket == NULL) {
- Py_DECREF(self);
- return NULL;
+ if (sock != NULL) {
+ self->Socket = PyWeakref_NewRef((PyObject *) sock, NULL);
+ if (self->Socket == NULL) {
+ Py_DECREF(self);
+ Py_XDECREF(self->server_hostname);
+ return NULL;
+ }
}
return self;
}
@@ -548,20 +565,21 @@ static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self)
int ret;
int err;
int sockstate, nonblocking;
- PySocketSockObject *sock
- = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
+ PySocketSockObject *sock = GET_SOCKET(self);
- if (((PyObject*)sock) == Py_None) {
- _setSSLError("Underlying socket connection gone",
- PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
- return NULL;
- }
- Py_INCREF(sock);
+ if (sock) {
+ if (((PyObject*)sock) == Py_None) {
+ _setSSLError("Underlying socket connection gone",
+ PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
+ return NULL;
+ }
+ Py_INCREF(sock);
- /* just in case the blocking state of the socket has been changed */
- nonblocking = (sock->sock_timeout >= 0.0);
- BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
- BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
+ /* just in case the blocking state of the socket has been changed */
+ nonblocking = (sock->sock_timeout >= 0.0);
+ BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
+ BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
+ }
/* Actually negotiate SSL connection */
/* XXX If SSL_do_handshake() returns 0, it's also a failure. */
@@ -595,7 +613,7 @@ static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self)
break;
}
} while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
- Py_DECREF(sock);
+ Py_XDECREF(sock);
if (ret < 1)
return PySSL_SetError(self, ret, __FILE__, __LINE__);
@@ -610,7 +628,7 @@ static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self)
return Py_None;
error:
- Py_DECREF(sock);
+ Py_XDECREF(sock);
return NULL;
}
@@ -779,12 +797,7 @@ _get_peer_alt_names (X509 *certificate) {
char buf[2048];
char *vptr;
int len;
- /* Issue #2973: ASN1_item_d2i() API changed in OpenSSL 0.9.6m */
-#if OPENSSL_VERSION_NUMBER >= 0x009060dfL
const unsigned char *p;
-#else
- unsigned char *p;
-#endif
if (certificate == NULL)
return peer_alt_names;
@@ -1356,54 +1369,96 @@ If the optional argument is True, returns a DER-encoded copy of the\n\
peer certificate, or None if no certificate was provided. This will\n\
return the certificate even if it wasn't validated.");
-static PyObject *PySSL_cipher (PySSLSocket *self) {
-
- PyObject *retval, *v;
- const SSL_CIPHER *current;
- char *cipher_name;
- char *cipher_protocol;
-
- if (self->ssl == NULL)
- Py_RETURN_NONE;
- current = SSL_get_current_cipher(self->ssl);
- if (current == NULL)
- Py_RETURN_NONE;
-
- retval = PyTuple_New(3);
+static PyObject *
+cipher_to_tuple(const SSL_CIPHER *cipher)
+{
+ const char *cipher_name, *cipher_protocol;
+ PyObject *v, *retval = PyTuple_New(3);
if (retval == NULL)
return NULL;
- cipher_name = (char *) SSL_CIPHER_get_name(current);
+ cipher_name = SSL_CIPHER_get_name(cipher);
if (cipher_name == NULL) {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(retval, 0, Py_None);
} else {
v = PyUnicode_FromString(cipher_name);
if (v == NULL)
- goto fail0;
+ goto fail;
PyTuple_SET_ITEM(retval, 0, v);
}
- cipher_protocol = (char *) SSL_CIPHER_get_version(current);
+
+ cipher_protocol = SSL_CIPHER_get_version(cipher);
if (cipher_protocol == NULL) {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(retval, 1, Py_None);
} else {
v = PyUnicode_FromString(cipher_protocol);
if (v == NULL)
- goto fail0;
+ goto fail;
PyTuple_SET_ITEM(retval, 1, v);
}
- v = PyLong_FromLong(SSL_CIPHER_get_bits(current, NULL));
+
+ v = PyLong_FromLong(SSL_CIPHER_get_bits(cipher, NULL));
if (v == NULL)
- goto fail0;
+ goto fail;
PyTuple_SET_ITEM(retval, 2, v);
+
return retval;
- fail0:
+ fail:
Py_DECREF(retval);
return NULL;
}
+static PyObject *PySSL_shared_ciphers(PySSLSocket *self)
+{
+ SSL_SESSION *sess = SSL_get_session(self->ssl);
+ STACK_OF(SSL_CIPHER) *ciphers;
+ int i;
+ PyObject *res;
+
+ if (!sess || !sess->ciphers)
+ Py_RETURN_NONE;
+ ciphers = sess->ciphers;
+ res = PyList_New(sk_SSL_CIPHER_num(ciphers));
+ if (!res)
+ return NULL;
+ for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
+ PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i));
+ if (!tup) {
+ Py_DECREF(res);
+ return NULL;
+ }
+ PyList_SET_ITEM(res, i, tup);
+ }
+ return res;
+}
+
+static PyObject *PySSL_cipher (PySSLSocket *self)
+{
+ const SSL_CIPHER *current;
+
+ if (self->ssl == NULL)
+ Py_RETURN_NONE;
+ current = SSL_get_current_cipher(self->ssl);
+ if (current == NULL)
+ Py_RETURN_NONE;
+ return cipher_to_tuple(current);
+}
+
+static PyObject *PySSL_version(PySSLSocket *self)
+{
+ const char *version;
+
+ if (self->ssl == NULL)
+ Py_RETURN_NONE;
+ version = SSL_get_version(self->ssl);
+ if (!strcmp(version, "unknown"))
+ Py_RETURN_NONE;
+ return PyUnicode_FromString(version);
+}
+
#ifdef OPENSSL_NPN_NEGOTIATED
static PyObject *PySSL_selected_npn_protocol(PySSLSocket *self) {
const unsigned char *out;
@@ -1414,7 +1469,20 @@ static PyObject *PySSL_selected_npn_protocol(PySSLSocket *self) {
if (out == NULL)
Py_RETURN_NONE;
- return PyUnicode_FromStringAndSize((char *) out, outlen);
+ return PyUnicode_FromStringAndSize((char *)out, outlen);
+}
+#endif
+
+#ifdef HAVE_ALPN
+static PyObject *PySSL_selected_alpn_protocol(PySSLSocket *self) {
+ const unsigned char *out;
+ unsigned int outlen;
+
+ SSL_get0_alpn_selected(self->ssl, &out, &outlen);
+
+ if (out == NULL)
+ Py_RETURN_NONE;
+ return PyUnicode_FromStringAndSize((char *)out, outlen);
}
#endif
@@ -1473,6 +1541,54 @@ on the SSLContext to change the certificate information associated with the\n\
SSLSocket before the cryptographic exchange handshake messages\n");
+static PyObject *
+PySSL_get_server_side(PySSLSocket *self, void *c)
+{
+ return PyBool_FromLong(self->socket_type == PY_SSL_SERVER);
+}
+
+PyDoc_STRVAR(PySSL_get_server_side_doc,
+"Whether this is a server-side socket.");
+
+static PyObject *
+PySSL_get_server_hostname(PySSLSocket *self, void *c)
+{
+ if (self->server_hostname == NULL)
+ Py_RETURN_NONE;
+ Py_INCREF(self->server_hostname);
+ return self->server_hostname;
+}
+
+PyDoc_STRVAR(PySSL_get_server_hostname_doc,
+"The currently set server hostname (for SNI).");
+
+static PyObject *
+PySSL_get_owner(PySSLSocket *self, void *c)
+{
+ PyObject *owner;
+
+ if (self->owner == NULL)
+ Py_RETURN_NONE;
+
+ owner = PyWeakref_GetObject(self->owner);
+ Py_INCREF(owner);
+ return owner;
+}
+
+static int
+PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c)
+{
+ Py_XDECREF(self->owner);
+ self->owner = PyWeakref_NewRef(value, NULL);
+ if (self->owner == NULL)
+ return -1;
+ return 0;
+}
+
+PyDoc_STRVAR(PySSL_get_owner_doc,
+"The Python-level owner of this object.\
+Passed as \"self\" in servername callback.");
+
static void PySSL_dealloc(PySSLSocket *self)
{
@@ -1482,6 +1598,8 @@ static void PySSL_dealloc(PySSLSocket *self)
SSL_free(self->ssl);
Py_XDECREF(self->Socket);
Py_XDECREF(self->ctx);
+ Py_XDECREF(self->server_hostname);
+ Py_XDECREF(self->owner);
PyObject_Del(self);
}
@@ -1498,10 +1616,10 @@ check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
int rc;
/* Nothing to do unless we're in timeout mode (not non-blocking) */
- if (s->sock_timeout < 0.0)
- return SOCKET_IS_BLOCKING;
- else if (s->sock_timeout == 0.0)
+ if ((s == NULL) || (s->sock_timeout == 0.0))
return SOCKET_IS_NONBLOCKING;
+ else if (s->sock_timeout < 0.0)
+ return SOCKET_IS_BLOCKING;
/* Guard against closed socket */
if (s->sock_fd < 0)
@@ -1562,18 +1680,19 @@ static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args)
int sockstate;
int err;
int nonblocking;
- PySocketSockObject *sock
- = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
+ PySocketSockObject *sock = GET_SOCKET(self);
- if (((PyObject*)sock) == Py_None) {
- _setSSLError("Underlying socket connection gone",
- PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
- return NULL;
+ if (sock != NULL) {
+ if (((PyObject*)sock) == Py_None) {
+ _setSSLError("Underlying socket connection gone",
+ PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
+ return NULL;
+ }
+ Py_INCREF(sock);
}
- Py_INCREF(sock);
if (!PyArg_ParseTuple(args, "y*:write", &buf)) {
- Py_DECREF(sock);
+ Py_XDECREF(sock);
return NULL;
}
@@ -1583,10 +1702,12 @@ static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args)
goto error;
}
- /* just in case the blocking state of the socket has been changed */
- nonblocking = (sock->sock_timeout >= 0.0);
- BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
- BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
+ if (sock != NULL) {
+ /* just in case the blocking state of the socket has been changed */
+ nonblocking = (sock->sock_timeout >= 0.0);
+ BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
+ BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
+ }
sockstate = check_socket_and_wait_for_timeout(sock, 1);
if (sockstate == SOCKET_HAS_TIMED_OUT) {
@@ -1630,7 +1751,7 @@ static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args)
}
} while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE);
- Py_DECREF(sock);
+ Py_XDECREF(sock);
PyBuffer_Release(&buf);
if (len > 0)
return PyLong_FromLong(len);
@@ -1638,7 +1759,7 @@ static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args)
return PySSL_SetError(self, len, __FILE__, __LINE__);
error:
- Py_DECREF(sock);
+ Py_XDECREF(sock);
PyBuffer_Release(&buf);
return NULL;
}
@@ -1678,15 +1799,16 @@ static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args)
int sockstate;
int err;
int nonblocking;
- PySocketSockObject *sock
- = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
+ PySocketSockObject *sock = GET_SOCKET(self);
- if (((PyObject*)sock) == Py_None) {
- _setSSLError("Underlying socket connection gone",
- PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
- return NULL;
+ if (sock != NULL) {
+ if (((PyObject*)sock) == Py_None) {
+ _setSSLError("Underlying socket connection gone",
+ PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
+ return NULL;
+ }
+ Py_INCREF(sock);
}
- Py_INCREF(sock);
buf.obj = NULL;
buf.buf = NULL;
@@ -1712,10 +1834,12 @@ static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args)
}
}
- /* just in case the blocking state of the socket has been changed */
- nonblocking = (sock->sock_timeout >= 0.0);
- BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
- BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
+ if (sock != NULL) {
+ /* just in case the blocking state of the socket has been changed */
+ nonblocking = (sock->sock_timeout >= 0.0);
+ BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
+ BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
+ }
/* first check if there are bytes ready to be read */
PySSL_BEGIN_ALLOW_THREADS
@@ -1771,7 +1895,7 @@ static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args)
}
done:
- Py_DECREF(sock);
+ Py_XDECREF(sock);
if (!buf_passed) {
_PyBytes_Resize(&dest, count);
return dest;
@@ -1782,7 +1906,7 @@ done:
}
error:
- Py_DECREF(sock);
+ Py_XDECREF(sock);
if (!buf_passed)
Py_XDECREF(dest);
else
@@ -1799,21 +1923,22 @@ static PyObject *PySSL_SSLshutdown(PySSLSocket *self)
{
int err, ssl_err, sockstate, nonblocking;
int zeros = 0;
- PySocketSockObject *sock
- = (PySocketSockObject *) PyWeakref_GetObject(self->Socket);
+ PySocketSockObject *sock = GET_SOCKET(self);
- /* Guard against closed socket */
- if ((((PyObject*)sock) == Py_None) || (sock->sock_fd < 0)) {
- _setSSLError("Underlying socket connection gone",
- PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
- return NULL;
- }
- Py_INCREF(sock);
+ if (sock != NULL) {
+ /* Guard against closed socket */
+ if ((((PyObject*)sock) == Py_None) || (sock->sock_fd < 0)) {
+ _setSSLError("Underlying socket connection gone",
+ PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
+ return NULL;
+ }
+ Py_INCREF(sock);
- /* Just in case the blocking state of the socket has been changed */
- nonblocking = (sock->sock_timeout >= 0.0);
- BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
- BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
+ /* Just in case the blocking state of the socket has been changed */
+ nonblocking = (sock->sock_timeout >= 0.0);
+ BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
+ BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
+ }
while (1) {
PySSL_BEGIN_ALLOW_THREADS
@@ -1871,15 +1996,17 @@ static PyObject *PySSL_SSLshutdown(PySSLSocket *self)
}
if (err < 0) {
- Py_DECREF(sock);
+ Py_XDECREF(sock);
return PySSL_SetError(self, err, __FILE__, __LINE__);
}
- else
+ if (sock)
/* It's already INCREF'ed */
return (PyObject *) sock;
+ else
+ Py_RETURN_NONE;
error:
- Py_DECREF(sock);
+ Py_XDECREF(sock);
return NULL;
}
@@ -1889,7 +2016,6 @@ PyDoc_STRVAR(PySSL_SSLshutdown_doc,
Does the SSL shutdown handshake with the remote end, and returns\n\
the underlying socket object.");
-#if HAVE_OPENSSL_FINISHED
static PyObject *
PySSL_tls_unique_cb(PySSLSocket *self)
{
@@ -1922,11 +2048,15 @@ Returns the 'tls-unique' channel binding data, as defined by RFC 5929.\n\
\n\
If the TLS handshake is not yet complete, None is returned");
-#endif /* HAVE_OPENSSL_FINISHED */
-
static PyGetSetDef ssl_getsetlist[] = {
{"context", (getter) PySSL_get_context,
(setter) PySSL_set_context, PySSL_set_context_doc},
+ {"server_side", (getter) PySSL_get_server_side, NULL,
+ PySSL_get_server_side_doc},
+ {"server_hostname", (getter) PySSL_get_server_hostname, NULL,
+ PySSL_get_server_hostname_doc},
+ {"owner", (getter) PySSL_get_owner, (setter) PySSL_set_owner,
+ PySSL_get_owner_doc},
{NULL}, /* sentinel */
};
@@ -1941,16 +2071,19 @@ static PyMethodDef PySSLMethods[] = {
{"peer_certificate", (PyCFunction)PySSL_peercert, METH_VARARGS,
PySSL_peercert_doc},
{"cipher", (PyCFunction)PySSL_cipher, METH_NOARGS},
+ {"shared_ciphers", (PyCFunction)PySSL_shared_ciphers, METH_NOARGS},
+ {"version", (PyCFunction)PySSL_version, METH_NOARGS},
#ifdef OPENSSL_NPN_NEGOTIATED
{"selected_npn_protocol", (PyCFunction)PySSL_selected_npn_protocol, METH_NOARGS},
#endif
+#ifdef HAVE_ALPN
+ {"selected_alpn_protocol", (PyCFunction)PySSL_selected_alpn_protocol, METH_NOARGS},
+#endif
{"compression", (PyCFunction)PySSL_compression, METH_NOARGS},
{"shutdown", (PyCFunction)PySSL_SSLshutdown, METH_NOARGS,
PySSL_SSLshutdown_doc},
-#if HAVE_OPENSSL_FINISHED
{"tls_unique_cb", (PyCFunction)PySSL_tls_unique_cb, METH_NOARGS,
PySSL_tls_unique_cb_doc},
-#endif
{NULL, NULL}
};
@@ -2051,6 +2184,9 @@ context_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
#ifdef OPENSSL_NPN_NEGOTIATED
self->npn_protocols = NULL;
#endif
+#ifdef HAVE_ALPN
+ self->alpn_protocols = NULL;
+#endif
#ifndef OPENSSL_NO_TLSEXT
self->set_hostname = NULL;
#endif
@@ -2110,7 +2246,10 @@ context_dealloc(PySSLContext *self)
context_clear(self);
SSL_CTX_free(self->ctx);
#ifdef OPENSSL_NPN_NEGOTIATED
- PyMem_Free(self->npn_protocols);
+ PyMem_FREE(self->npn_protocols);
+#endif
+#ifdef HAVE_ALPN
+ PyMem_FREE(self->alpn_protocols);
#endif
Py_TYPE(self)->tp_free(self);
}
@@ -2137,6 +2276,30 @@ set_ciphers(PySSLContext *self, PyObject *args)
}
#ifdef OPENSSL_NPN_NEGOTIATED
+static int
+do_protocol_selection(int alpn, unsigned char **out, unsigned char *outlen,
+ const unsigned char *server_protocols, unsigned int server_protocols_len,
+ const unsigned char *client_protocols, unsigned int client_protocols_len)
+{
+ int ret;
+ if (client_protocols == NULL) {
+ client_protocols = (unsigned char *)"";
+ client_protocols_len = 0;
+ }
+ if (server_protocols == NULL) {
+ server_protocols = (unsigned char *)"";
+ server_protocols_len = 0;
+ }
+
+ ret = SSL_select_next_proto(out, outlen,
+ server_protocols, server_protocols_len,
+ client_protocols, client_protocols_len);
+ if (alpn && ret != OPENSSL_NPN_NEGOTIATED)
+ return SSL_TLSEXT_ERR_NOACK;
+
+ return SSL_TLSEXT_ERR_OK;
+}
+
/* this callback gets passed to SSL_CTX_set_next_protos_advertise_cb */
static int
_advertiseNPN_cb(SSL *s,
@@ -2146,10 +2309,10 @@ _advertiseNPN_cb(SSL *s,
PySSLContext *ssl_ctx = (PySSLContext *) args;
if (ssl_ctx->npn_protocols == NULL) {
- *data = (unsigned char *) "";
+ *data = (unsigned char *)"";
*len = 0;
} else {
- *data = (unsigned char *) ssl_ctx->npn_protocols;
+ *data = ssl_ctx->npn_protocols;
*len = ssl_ctx->npn_protocols_len;
}
@@ -2162,23 +2325,9 @@ _selectNPN_cb(SSL *s,
const unsigned char *server, unsigned int server_len,
void *args)
{
- PySSLContext *ssl_ctx = (PySSLContext *) args;
-
- unsigned char *client = (unsigned char *) ssl_ctx->npn_protocols;
- int client_len;
-
- if (client == NULL) {
- client = (unsigned char *) "";
- client_len = 0;
- } else {
- client_len = ssl_ctx->npn_protocols_len;
- }
-
- SSL_select_next_proto(out, outlen,
- server, server_len,
- client, client_len);
-
- return SSL_TLSEXT_ERR_OK;
+ PySSLContext *ctx = (PySSLContext *)args;
+ return do_protocol_selection(0, out, outlen, server, server_len,
+ ctx->npn_protocols, ctx->npn_protocols_len);
}
#endif
@@ -2221,6 +2370,50 @@ _set_npn_protocols(PySSLContext *self, PyObject *args)
#endif
}
+#ifdef HAVE_ALPN
+static int
+_selectALPN_cb(SSL *s,
+ const unsigned char **out, unsigned char *outlen,
+ const unsigned char *client_protocols, unsigned int client_protocols_len,
+ void *args)
+{
+ PySSLContext *ctx = (PySSLContext *)args;
+ return do_protocol_selection(1, (unsigned char **)out, outlen,
+ ctx->alpn_protocols, ctx->alpn_protocols_len,
+ client_protocols, client_protocols_len);
+}
+#endif
+
+static PyObject *
+_set_alpn_protocols(PySSLContext *self, PyObject *args)
+{
+#ifdef HAVE_ALPN
+ Py_buffer protos;
+
+ if (!PyArg_ParseTuple(args, "y*:set_npn_protocols", &protos))
+ return NULL;
+
+ PyMem_FREE(self->alpn_protocols);
+ self->alpn_protocols = PyMem_Malloc(protos.len);
+ if (!self->alpn_protocols)
+ return PyErr_NoMemory();
+ memcpy(self->alpn_protocols, protos.buf, protos.len);
+ self->alpn_protocols_len = protos.len;
+ PyBuffer_Release(&protos);
+
+ if (SSL_CTX_set_alpn_protos(self->ctx, self->alpn_protocols, self->alpn_protocols_len))
+ return PyErr_NoMemory();
+ SSL_CTX_set_alpn_select_cb(self->ctx, _selectALPN_cb, self);
+
+ PyBuffer_Release(&protos);
+ Py_RETURN_NONE;
+#else
+ PyErr_SetString(PyExc_NotImplementedError,
+ "The ALPN extension requires OpenSSL 1.0.2 or later.");
+ return NULL;
+#endif
+}
+
static PyObject *
get_verify_mode(PySSLContext *self, void *c)
{
@@ -2264,7 +2457,6 @@ set_verify_mode(PySSLContext *self, PyObject *arg, void *c)
return 0;
}
-#ifdef HAVE_OPENSSL_VERIFY_PARAM
static PyObject *
get_verify_flags(PySSLContext *self, void *c)
{
@@ -2302,7 +2494,6 @@ set_verify_flags(PySSLContext *self, PyObject *arg, void *c)
}
return 0;
}
-#endif
static PyObject *
get_options(PySSLContext *self, void *c)
@@ -2810,14 +3001,43 @@ context_wrap_socket(PySSLContext *self, PyObject *args, PyObject *kwds)
return NULL;
}
- res = (PyObject *) newPySSLSocket(self, sock, server_side,
- hostname);
+ res = (PyObject *) newPySSLSocket(self, sock, server_side, hostname,
+ NULL, NULL);
if (hostname != NULL)
PyMem_Free(hostname);
return res;
}
static PyObject *
+context_wrap_bio(PySSLContext *self, PyObject *args, PyObject *kwds)
+{
+ char *kwlist[] = {"incoming", "outgoing", "server_side",
+ "server_hostname", NULL};
+ int server_side;
+ char *hostname = NULL;
+ PyObject *hostname_obj = Py_None, *res;
+ PySSLMemoryBIO *incoming, *outgoing;
+
+ /* server_hostname is either None (or absent), or to be encoded
+ using the idna encoding. */
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!i|O:_wrap_bio", kwlist,
+ &PySSLMemoryBIO_Type, &incoming,
+ &PySSLMemoryBIO_Type, &outgoing,
+ &server_side, &hostname_obj))
+ return NULL;
+ if (hostname_obj != Py_None) {
+ if (!PyArg_Parse(hostname_obj, "et", "idna", &hostname))
+ return NULL;
+ }
+
+ res = (PyObject *) newPySSLSocket(self, NULL, server_side, hostname,
+ incoming, outgoing);
+
+ PyMem_Free(hostname);
+ return res;
+}
+
+static PyObject *
session_stats(PySSLContext *self, PyObject *unused)
{
int r;
@@ -2923,11 +3143,25 @@ _servername_callback(SSL *s, int *al, void *args)
ssl = SSL_get_app_data(s);
assert(PySSLSocket_Check(ssl));
- ssl_socket = PyWeakref_GetObject(ssl->Socket);
+
+ /* The servername callback expects a argument that represents the current
+ * SSL connection and that has a .context attribute that can be changed to
+ * identify the requested hostname. Since the official API is the Python
+ * level API we want to pass the callback a Python level object rather than
+ * a _ssl.SSLSocket instance. If there's an "owner" (typically an
+ * SSLObject) that will be passed. Otherwise if there's a socket then that
+ * will be passed. If both do not exist only then the C-level object is
+ * passed. */
+ if (ssl->owner)
+ ssl_socket = PyWeakref_GetObject(ssl->owner);
+ else if (ssl->Socket)
+ ssl_socket = PyWeakref_GetObject(ssl->Socket);
+ else
+ ssl_socket = (PyObject *) ssl;
+
Py_INCREF(ssl_socket);
- if (ssl_socket == Py_None) {
+ if (ssl_socket == Py_None)
goto error;
- }
if (servername == NULL) {
result = PyObject_CallFunctionObjArgs(ssl_ctx->set_hostname, ssl_socket,
@@ -3144,10 +3378,8 @@ static PyGetSetDef context_getsetlist[] = {
(setter) set_check_hostname, NULL},
{"options", (getter) get_options,
(setter) set_options, NULL},
-#ifdef HAVE_OPENSSL_VERIFY_PARAM
{"verify_flags", (getter) get_verify_flags,
(setter) set_verify_flags, NULL},
-#endif
{"verify_mode", (getter) get_verify_mode,
(setter) set_verify_mode, NULL},
{NULL}, /* sentinel */
@@ -3156,8 +3388,12 @@ static PyGetSetDef context_getsetlist[] = {
static struct PyMethodDef context_methods[] = {
{"_wrap_socket", (PyCFunction) context_wrap_socket,
METH_VARARGS | METH_KEYWORDS, NULL},
+ {"_wrap_bio", (PyCFunction) context_wrap_bio,
+ METH_VARARGS | METH_KEYWORDS, NULL},
{"set_ciphers", (PyCFunction) set_ciphers,
METH_VARARGS, NULL},
+ {"_set_alpn_protocols", (PyCFunction) _set_alpn_protocols,
+ METH_VARARGS, NULL},
{"_set_npn_protocols", (PyCFunction) _set_npn_protocols,
METH_VARARGS, NULL},
{"load_cert_chain", (PyCFunction) load_cert_chain,
@@ -3225,8 +3461,225 @@ static PyTypeObject PySSLContext_Type = {
};
+/*
+ * MemoryBIO objects
+ */
+
+static PyObject *
+memory_bio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ char *kwlist[] = {NULL};
+ BIO *bio;
+ PySSLMemoryBIO *self;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, ":MemoryBIO", kwlist))
+ return NULL;
+
+ bio = BIO_new(BIO_s_mem());
+ if (bio == NULL) {
+ PyErr_SetString(PySSLErrorObject,
+ "failed to allocate BIO");
+ return NULL;
+ }
+ /* Since our BIO is non-blocking an empty read() does not indicate EOF,
+ * just that no data is currently available. The SSL routines should retry
+ * the read, which we can achieve by calling BIO_set_retry_read(). */
+ BIO_set_retry_read(bio);
+ BIO_set_mem_eof_return(bio, -1);
+
+ assert(type != NULL && type->tp_alloc != NULL);
+ self = (PySSLMemoryBIO *) type->tp_alloc(type, 0);
+ if (self == NULL) {
+ BIO_free(bio);
+ return NULL;
+ }
+ self->bio = bio;
+ self->eof_written = 0;
+
+ return (PyObject *) self;
+}
+
+static void
+memory_bio_dealloc(PySSLMemoryBIO *self)
+{
+ BIO_free(self->bio);
+ Py_TYPE(self)->tp_free(self);
+}
+
+static PyObject *
+memory_bio_get_pending(PySSLMemoryBIO *self, void *c)
+{
+ return PyLong_FromLong(BIO_ctrl_pending(self->bio));
+}
+
+PyDoc_STRVAR(PySSL_memory_bio_pending_doc,
+"The number of bytes pending in the memory BIO.");
+
+static PyObject *
+memory_bio_get_eof(PySSLMemoryBIO *self, void *c)
+{
+ return PyBool_FromLong((BIO_ctrl_pending(self->bio) == 0)
+ && self->eof_written);
+}
+
+PyDoc_STRVAR(PySSL_memory_bio_eof_doc,
+"Whether the memory BIO is at EOF.");
+
+static PyObject *
+memory_bio_read(PySSLMemoryBIO *self, PyObject *args)
+{
+ int len = -1, avail, nbytes;
+ PyObject *result;
+
+ if (!PyArg_ParseTuple(args, "|i:read", &len))
+ return NULL;
+
+ avail = BIO_ctrl_pending(self->bio);
+ if ((len < 0) || (len > avail))
+ len = avail;
+
+ result = PyBytes_FromStringAndSize(NULL, len);
+ if ((result == NULL) || (len == 0))
+ return result;
+
+ nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len);
+ /* There should never be any short reads but check anyway. */
+ if ((nbytes < len) && (_PyBytes_Resize(&result, len) < 0)) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ return result;
+}
+
+PyDoc_STRVAR(PySSL_memory_bio_read_doc,
+"read([len]) -> bytes\n\
+\n\
+Read up to len bytes from the memory BIO.\n\
+\n\
+If len is not specified, read the entire buffer.\n\
+If the return value is an empty bytes instance, this means either\n\
+EOF or that no data is available. Use the \"eof\" property to\n\
+distinguish between the two.");
+
+static PyObject *
+memory_bio_write(PySSLMemoryBIO *self, PyObject *args)
+{
+ Py_buffer buf;
+ int nbytes;
+
+ if (!PyArg_ParseTuple(args, "y*:write", &buf))
+ return NULL;
+
+ if (buf.len > INT_MAX) {
+ PyErr_Format(PyExc_OverflowError,
+ "string longer than %d bytes", INT_MAX);
+ goto error;
+ }
+
+ if (self->eof_written) {
+ PyErr_SetString(PySSLErrorObject,
+ "cannot write() after write_eof()");
+ goto error;
+ }
+
+ nbytes = BIO_write(self->bio, buf.buf, buf.len);
+ if (nbytes < 0) {
+ _setSSLError(NULL, 0, __FILE__, __LINE__);
+ goto error;
+ }
+
+ PyBuffer_Release(&buf);
+ return PyLong_FromLong(nbytes);
+
+error:
+ PyBuffer_Release(&buf);
+ return NULL;
+}
+
+PyDoc_STRVAR(PySSL_memory_bio_write_doc,
+"write(b) -> len\n\
+\n\
+Writes the bytes b into the memory BIO. Returns the number\n\
+of bytes written.");
+
+static PyObject *
+memory_bio_write_eof(PySSLMemoryBIO *self, PyObject *args)
+{
+ self->eof_written = 1;
+ /* After an EOF is written, a zero return from read() should be a real EOF
+ * i.e. it should not be retried. Clear the SHOULD_RETRY flag. */
+ BIO_clear_retry_flags(self->bio);
+ BIO_set_mem_eof_return(self->bio, 0);
+
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(PySSL_memory_bio_write_eof_doc,
+"write_eof()\n\
+\n\
+Write an EOF marker to the memory BIO.\n\
+When all data has been read, the \"eof\" property will be True.");
+
+static PyGetSetDef memory_bio_getsetlist[] = {
+ {"pending", (getter) memory_bio_get_pending, NULL,
+ PySSL_memory_bio_pending_doc},
+ {"eof", (getter) memory_bio_get_eof, NULL,
+ PySSL_memory_bio_eof_doc},
+ {NULL}, /* sentinel */
+};
+
+static struct PyMethodDef memory_bio_methods[] = {
+ {"read", (PyCFunction) memory_bio_read,
+ METH_VARARGS, PySSL_memory_bio_read_doc},
+ {"write", (PyCFunction) memory_bio_write,
+ METH_VARARGS, PySSL_memory_bio_write_doc},
+ {"write_eof", (PyCFunction) memory_bio_write_eof,
+ METH_NOARGS, PySSL_memory_bio_write_eof_doc},
+ {NULL, NULL} /* sentinel */
+};
+
+static PyTypeObject PySSLMemoryBIO_Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ssl.MemoryBIO", /*tp_name*/
+ sizeof(PySSLMemoryBIO), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor)memory_bio_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ memory_bio_methods, /*tp_methods*/
+ 0, /*tp_members*/
+ memory_bio_getsetlist, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ memory_bio_new, /*tp_new*/
+};
-#ifdef HAVE_OPENSSL_RAND
/* helper routines for seeding the SSL PRNG */
static PyObject *
@@ -3365,8 +3818,6 @@ Returns number of bytes read. Raises SSLError if connection to EGD\n\
fails or if it does not provide enough data to seed PRNG.");
#endif /* HAVE_RAND_EGD */
-#endif /* HAVE_OPENSSL_RAND */
-
PyDoc_STRVAR(PySSL_get_default_verify_paths_doc,
"get_default_verify_paths() -> tuple\n\
@@ -3752,7 +4203,6 @@ PySSL_enum_crls(PyObject *self, PyObject *args, PyObject *kwds)
static PyMethodDef PySSL_methods[] = {
{"_test_decode_cert", PySSL_test_decode_certificate,
METH_VARARGS},
-#ifdef HAVE_OPENSSL_RAND
{"RAND_add", PySSL_RAND_add, METH_VARARGS,
PySSL_RAND_add_doc},
{"RAND_bytes", PySSL_RAND_bytes, METH_VARARGS,
@@ -3765,7 +4215,6 @@ static PyMethodDef PySSL_methods[] = {
#endif
{"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
PySSL_RAND_status_doc},
-#endif
{"get_default_verify_paths", (PyCFunction)PySSL_get_default_verify_paths,
METH_NOARGS, PySSL_get_default_verify_paths_doc},
#ifdef _MSC_VER
@@ -3916,6 +4365,8 @@ PyInit__ssl(void)
return NULL;
if (PyType_Ready(&PySSLSocket_Type) < 0)
return NULL;
+ if (PyType_Ready(&PySSLMemoryBIO_Type) < 0)
+ return NULL;
m = PyModule_Create(&_sslmodule);
if (m == NULL)
@@ -3979,6 +4430,9 @@ PyInit__ssl(void)
if (PyDict_SetItemString(d, "_SSLSocket",
(PyObject *)&PySSLSocket_Type) != 0)
return NULL;
+ if (PyDict_SetItemString(d, "MemoryBIO",
+ (PyObject *)&PySSLMemoryBIO_Type) != 0)
+ return NULL;
PyModule_AddIntConstant(m, "SSL_ERROR_ZERO_RETURN",
PY_SSL_ERROR_ZERO_RETURN);
PyModule_AddIntConstant(m, "SSL_ERROR_WANT_READ",
@@ -4115,11 +4569,7 @@ PyInit__ssl(void)
Py_INCREF(r);
PyModule_AddObject(m, "HAS_SNI", r);
-#if HAVE_OPENSSL_FINISHED
r = Py_True;
-#else
- r = Py_False;
-#endif
Py_INCREF(r);
PyModule_AddObject(m, "HAS_TLS_UNIQUE", r);
@@ -4139,6 +4589,14 @@ PyInit__ssl(void)
Py_INCREF(r);
PyModule_AddObject(m, "HAS_NPN", r);
+#ifdef HAVE_ALPN
+ r = Py_True;
+#else
+ r = Py_False;
+#endif
+ Py_INCREF(r);
+ PyModule_AddObject(m, "HAS_ALPN", r);
+
/* Mappings for error codes */
err_codes_to_names = PyDict_New();
err_names_to_codes = PyDict_New();
diff --git a/Modules/_stat.c b/Modules/_stat.c
index a301fa8..f6cb303 100644
--- a/Modules/_stat.c
+++ b/Modules/_stat.c
@@ -27,9 +27,21 @@ extern "C" {
#endif /* HAVE_SYS_STAT_H */
#ifdef MS_WINDOWS
+#include <windows.h>
typedef unsigned short mode_t;
+
+/* FILE_ATTRIBUTE_INTEGRITY_STREAM and FILE_ATTRIBUTE_NO_SCRUB_DATA
+ are not present in VC2010, so define them manually */
+#ifndef FILE_ATTRIBUTE_INTEGRITY_STREAM
+# define FILE_ATTRIBUTE_INTEGRITY_STREAM 0x8000
+#endif
+
+#ifndef FILE_ATTRIBUTE_NO_SCRUB_DATA
+# define FILE_ATTRIBUTE_NO_SCRUB_DATA 0x20000
#endif
+#endif /* MS_WINDOWS */
+
/* From Python's stat.py */
#ifndef S_IMODE
# define S_IMODE 07777
@@ -473,6 +485,10 @@ ST_SIZE\n\
ST_ATIME\n\
ST_MTIME\n\
ST_CTIME\n\
+\n"
+
+"FILE_ATTRIBUTE_*: Windows file attribute constants\n\
+ (only present on Windows)\n\
");
@@ -555,6 +571,26 @@ PyInit__stat(void)
if (PyModule_AddIntConstant(m, "ST_MTIME", 8)) return NULL;
if (PyModule_AddIntConstant(m, "ST_CTIME", 9)) return NULL;
+#ifdef MS_WINDOWS
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_ARCHIVE)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_COMPRESSED)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_DEVICE)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_DIRECTORY)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_ENCRYPTED)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_HIDDEN)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_INTEGRITY_STREAM)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_NORMAL)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_NO_SCRUB_DATA)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_OFFLINE)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_READONLY)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_REPARSE_POINT)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_SPARSE_FILE)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_SYSTEM)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_TEMPORARY)) return NULL;
+ if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_VIRTUAL)) return NULL;
+#endif
+
return m;
}
diff --git a/Modules/_struct.c b/Modules/_struct.c
index ad9959e..068c5d1 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -85,8 +85,6 @@ typedef struct { char c; _Bool x; } s_bool;
#define BOOL_ALIGN 0
#endif
-#define STRINGIFY(x) #x
-
#ifdef __powerc
#pragma options align=reset
#endif
@@ -546,8 +544,8 @@ np_short(char *p, PyObject *v, const formatdef *f)
return -1;
if (x < SHRT_MIN || x > SHRT_MAX){
PyErr_SetString(StructError,
- "short format requires " STRINGIFY(SHRT_MIN)
- " <= number <= " STRINGIFY(SHRT_MAX));
+ "short format requires " Py_STRINGIFY(SHRT_MIN)
+ " <= number <= " Py_STRINGIFY(SHRT_MAX));
return -1;
}
y = (short)x;
@@ -564,7 +562,8 @@ np_ushort(char *p, PyObject *v, const formatdef *f)
return -1;
if (x < 0 || x > USHRT_MAX){
PyErr_SetString(StructError,
- "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
+ "ushort format requires 0 <= number <= "
+ Py_STRINGIFY(USHRT_MAX));
return -1;
}
y = (unsigned short)x;
@@ -1264,7 +1263,8 @@ prepare_s(PyStructObject *self)
const char *s;
const char *fmt;
char c;
- Py_ssize_t size, len, ncodes, num, itemsize;
+ Py_ssize_t size, len, num, itemsize;
+ size_t ncodes;
fmt = PyBytes_AS_STRING(self->s_format);
@@ -1320,7 +1320,7 @@ prepare_s(PyStructObject *self)
}
/* check for overflow */
- if ((ncodes + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
+ if ((ncodes + 1) > ((size_t)PY_SSIZE_T_MAX / sizeof(formatcode))) {
PyErr_NoMemory();
return -1;
}
diff --git a/Modules/_testbuffer.c b/Modules/_testbuffer.c
index 0c6ef16..a563a04 100644
--- a/Modules/_testbuffer.c
+++ b/Modules/_testbuffer.c
@@ -1510,6 +1510,19 @@ ndarray_getbuf(NDArrayObject *self, Py_buffer *view, int flags)
view->shape = NULL;
}
+ /* Ascertain that the new buffer has the same contiguity as the exporter */
+ if (ND_C_CONTIGUOUS(baseflags) != PyBuffer_IsContiguous(view, 'C') ||
+ /* skip cast to 1-d */
+ (view->format != NULL && view->shape != NULL &&
+ ND_FORTRAN_CONTIGUOUS(baseflags) != PyBuffer_IsContiguous(view, 'F')) ||
+ /* cast to 1-d */
+ (view->format == NULL && view->shape == NULL &&
+ !PyBuffer_IsContiguous(view, 'F'))) {
+ PyErr_SetString(PyExc_BufferError,
+ "ndarray: contiguity mismatch in getbuf()");
+ return -1;
+ }
+
view->obj = (PyObject *)self;
Py_INCREF(view->obj);
self->head->exports++;
@@ -2206,6 +2219,8 @@ ndarray_add_suboffsets(PyObject *self, PyObject *dummy)
for (i = 0; i < base->ndim; i++)
base->suboffsets[i] = -1;
+ nd->head->flags &= ~(ND_C|ND_FORTRAN);
+
Py_RETURN_NONE;
}
@@ -2469,13 +2484,12 @@ arraycmp(const Py_ssize_t *a1, const Py_ssize_t *a2, const Py_ssize_t *shape,
{
Py_ssize_t i;
- if (ndim == 1 && shape && shape[0] == 1) {
- /* This is for comparing strides: For example, the array
- [175], shape=[1], strides=[-5] is considered contiguous. */
- return 1;
- }
for (i = 0; i < ndim; i++) {
+ if (shape && shape[i] <= 1) {
+ /* strides can differ if the dimension is less than 2 */
+ continue;
+ }
if (a1[i] != a2[i]) {
return 0;
}
@@ -2555,30 +2569,35 @@ is_contiguous(PyObject *self, PyObject *args)
PyObject *obj;
PyObject *order;
PyObject *ret = NULL;
- Py_buffer view;
+ Py_buffer view, *base;
char ord;
if (!PyArg_ParseTuple(args, "OO", &obj, &order)) {
return NULL;
}
- if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
- PyErr_SetString(PyExc_TypeError,
- "is_contiguous: object does not implement the buffer "
- "protocol");
+ ord = get_ascii_order(order);
+ if (ord == CHAR_MAX) {
return NULL;
}
- ord = get_ascii_order(order);
- if (ord == CHAR_MAX) {
- goto release;
+ if (NDArray_Check(obj)) {
+ /* Skip the buffer protocol to check simple etc. buffers directly. */
+ base = &((NDArrayObject *)obj)->head->base;
+ ret = PyBuffer_IsContiguous(base, ord) ? Py_True : Py_False;
+ }
+ else {
+ if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
+ PyErr_SetString(PyExc_TypeError,
+ "is_contiguous: object does not implement the buffer "
+ "protocol");
+ return NULL;
+ }
+ ret = PyBuffer_IsContiguous(&view, ord) ? Py_True : Py_False;
+ PyBuffer_Release(&view);
}
- ret = PyBuffer_IsContiguous(&view, ord) ? Py_True : Py_False;
Py_INCREF(ret);
-
-release:
- PyBuffer_Release(&view);
return ret;
}
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 625409e..bed6568 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -12,6 +12,7 @@
#include "structmember.h"
#include "datetime.h"
#include "marshal.h"
+#include <signal.h>
#ifdef WITH_THREAD
#include "pythread.h"
@@ -1719,7 +1720,7 @@ test_long_numbits(PyObject *self)
{-0xffffL, 16, -1},
{0xfffffffL, 28, 1},
{-0xfffffffL, 28, -1}};
- int i;
+ size_t i;
for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
size_t nbits;
@@ -2519,6 +2520,58 @@ test_from_contiguous(PyObject* self, PyObject *noargs)
Py_RETURN_NONE;
}
+#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
+extern PyTypeObject _PyBytesIOBuffer_Type;
+
+static PyObject *
+test_pep3118_obsolete_write_locks(PyObject* self, PyObject *noargs)
+{
+ PyTypeObject *type = &_PyBytesIOBuffer_Type;
+ PyObject *b;
+ char *dummy[1];
+ int ret, match;
+
+ /* PyBuffer_FillInfo() */
+ ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE);
+ match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
+ PyErr_Clear();
+ if (ret != -1 || match == 0)
+ goto error;
+
+ /* bytesiobuf_getbuffer() */
+ b = type->tp_alloc(type, 0);
+ if (b == NULL) {
+ return NULL;
+ }
+
+ ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE);
+ Py_DECREF(b);
+ match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
+ PyErr_Clear();
+ if (ret != -1 || match == 0)
+ goto error;
+
+ Py_RETURN_NONE;
+
+error:
+ PyErr_SetString(TestError,
+ "test_pep3118_obsolete_write_locks: failure");
+ return NULL;
+}
+#endif
+
+/* This tests functions that historically supported write locks. It is
+ wrong to call getbuffer() with view==NULL and a compliant getbufferproc
+ is entitled to segfault in that case. */
+static PyObject *
+getbuffer_with_null_view(PyObject* self, PyObject *obj)
+{
+ if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0)
+ return NULL;
+
+ Py_RETURN_NONE;
+}
+
/* Test that the fatal error from not having a current thread doesn't
cause an infinite loop. Run via Lib/test/test_capi.py */
static PyObject *
@@ -2703,6 +2756,21 @@ with_tp_del(PyObject *self, PyObject *args)
return obj;
}
+static PyMethodDef ml;
+
+static PyObject *
+create_cfunction(PyObject *self, PyObject *args)
+{
+ return PyCFunction_NewEx(&ml, self, NULL);
+}
+
+static PyMethodDef ml = {
+ "create_cfunction",
+ create_cfunction,
+ METH_NOARGS,
+ NULL
+};
+
static PyObject *
_test_incref(PyObject *ob)
{
@@ -2761,6 +2829,20 @@ test_pymem_alloc0(PyObject *self)
{
void *ptr;
+ ptr = PyMem_RawMalloc(0);
+ if (ptr == NULL) {
+ PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL");
+ return NULL;
+ }
+ PyMem_RawFree(ptr);
+
+ ptr = PyMem_RawCalloc(0, 0);
+ if (ptr == NULL) {
+ PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL");
+ return NULL;
+ }
+ PyMem_RawFree(ptr);
+
ptr = PyMem_Malloc(0);
if (ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL");
@@ -2768,6 +2850,13 @@ test_pymem_alloc0(PyObject *self)
}
PyMem_Free(ptr);
+ ptr = PyMem_Calloc(0, 0);
+ if (ptr == NULL) {
+ PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL");
+ return NULL;
+ }
+ PyMem_Free(ptr);
+
ptr = PyObject_Malloc(0);
if (ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL");
@@ -2775,13 +2864,22 @@ test_pymem_alloc0(PyObject *self)
}
PyObject_Free(ptr);
+ ptr = PyObject_Calloc(0, 0);
+ if (ptr == NULL) {
+ PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL");
+ return NULL;
+ }
+ PyObject_Free(ptr);
+
Py_RETURN_NONE;
}
typedef struct {
- PyMemAllocator alloc;
+ PyMemAllocatorEx alloc;
size_t malloc_size;
+ size_t calloc_nelem;
+ size_t calloc_elsize;
void *realloc_ptr;
size_t realloc_new_size;
void *free_ptr;
@@ -2794,6 +2892,14 @@ static void* hook_malloc (void* ctx, size_t size)
return hook->alloc.malloc(hook->alloc.ctx, size);
}
+static void* hook_calloc (void* ctx, size_t nelem, size_t elsize)
+{
+ alloc_hook_t *hook = (alloc_hook_t *)ctx;
+ hook->calloc_nelem = nelem;
+ hook->calloc_elsize = elsize;
+ return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize);
+}
+
static void* hook_realloc (void* ctx, void* ptr, size_t new_size)
{
alloc_hook_t *hook = (alloc_hook_t *)ctx;
@@ -2815,17 +2921,15 @@ test_setallocators(PyMemAllocatorDomain domain)
PyObject *res = NULL;
const char *error_msg;
alloc_hook_t hook;
- PyMemAllocator alloc;
- size_t size, size2;
+ PyMemAllocatorEx alloc;
+ size_t size, size2, nelem, elsize;
void *ptr, *ptr2;
- hook.malloc_size = 0;
- hook.realloc_ptr = NULL;
- hook.realloc_new_size = 0;
- hook.free_ptr = NULL;
+ memset(&hook, 0, sizeof(hook));
alloc.ctx = &hook;
alloc.malloc = &hook_malloc;
+ alloc.calloc = &hook_calloc;
alloc.realloc = &hook_realloc;
alloc.free = &hook_free;
PyMem_GetAllocator(domain, &hook.alloc);
@@ -2882,6 +2986,33 @@ test_setallocators(PyMemAllocatorDomain domain)
goto fail;
}
+ nelem = 2;
+ elsize = 5;
+ switch(domain)
+ {
+ case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break;
+ case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break;
+ case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break;
+ default: ptr = NULL; break;
+ }
+
+ if (ptr == NULL) {
+ error_msg = "calloc failed";
+ goto fail;
+ }
+
+ if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) {
+ error_msg = "calloc invalid nelem or elsize";
+ goto fail;
+ }
+
+ switch(domain)
+ {
+ case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break;
+ case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break;
+ case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break;
+ }
+
Py_INCREF(Py_None);
res = Py_None;
goto finally;
@@ -3051,6 +3182,24 @@ exit:
}
#endif /* WITH_THREAD */
+static PyObject*
+test_raise_signal(PyObject* self, PyObject *args)
+{
+ int signum, err;
+
+ if (PyArg_ParseTuple(args, "i:raise_signal", &signum) < 0)
+ return NULL;
+
+ err = raise(signum);
+ if (err)
+ return PyErr_SetFromErrno(PyExc_OSError);
+
+ if (PyErr_CheckSignals() < 0)
+ return NULL;
+
+ Py_RETURN_NONE;
+}
+
/* marshal */
static PyObject*
@@ -3236,6 +3385,10 @@ static PyMethodDef TestMethods[] = {
{"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS},
{"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
{"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
+#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
+ {"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
+#endif
+ {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
{"getargs_tuple", getargs_tuple, METH_VARARGS},
{"getargs_keywords", (PyCFunction)getargs_keywords,
METH_VARARGS|METH_KEYWORDS},
@@ -3309,6 +3462,7 @@ static PyMethodDef TestMethods[] = {
{"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS},
{"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS},
{"with_tp_del", with_tp_del, METH_VARARGS},
+ {"create_cfunction", create_cfunction, METH_NOARGS},
{"test_pymem_alloc0",
(PyCFunction)test_pymem_alloc0, METH_NOARGS},
{"test_pymem_setrawallocators",
@@ -3340,6 +3494,8 @@ static PyMethodDef TestMethods[] = {
{"docstring_with_signature_with_defaults",
(PyCFunction)test_with_docstring, METH_NOARGS,
docstring_with_signature_with_defaults},
+ {"raise_signal",
+ (PyCFunction)test_raise_signal, METH_VARARGS},
#ifdef WITH_THREAD
{"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O,
PyDoc_STR("set_error_class(error_class) -> None")},
@@ -3515,6 +3671,109 @@ static PyTypeObject test_structmembersType = {
};
+typedef struct {
+ PyObject_HEAD
+} matmulObject;
+
+static PyObject *
+matmulType_matmul(PyObject *self, PyObject *other)
+{
+ return Py_BuildValue("(sOO)", "matmul", self, other);
+}
+
+static PyObject *
+matmulType_imatmul(PyObject *self, PyObject *other)
+{
+ return Py_BuildValue("(sOO)", "imatmul", self, other);
+}
+
+static void
+matmulType_dealloc(PyObject *self)
+{
+ Py_TYPE(self)->tp_free(self);
+}
+
+static PyNumberMethods matmulType_as_number = {
+ 0, /* nb_add */
+ 0, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_remainde r*/
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* tp_positive */
+ 0, /* tp_absolute */
+ 0, /* tp_bool */
+ 0, /* nb_invert */
+ 0, /* nb_lshift */
+ 0, /* nb_rshift */
+ 0, /* nb_and */
+ 0, /* nb_xor */
+ 0, /* nb_or */
+ 0, /* nb_int */
+ 0, /* nb_reserved */
+ 0, /* nb_float */
+ 0, /* nb_inplace_add */
+ 0, /* nb_inplace_subtract */
+ 0, /* nb_inplace_multiply */
+ 0, /* nb_inplace_remainder */
+ 0, /* nb_inplace_power */
+ 0, /* nb_inplace_lshift */
+ 0, /* nb_inplace_rshift */
+ 0, /* nb_inplace_and */
+ 0, /* nb_inplace_xor */
+ 0, /* nb_inplace_or */
+ 0, /* nb_floor_divide */
+ 0, /* nb_true_divide */
+ 0, /* nb_inplace_floor_divide */
+ 0, /* nb_inplace_true_divide */
+ 0, /* nb_index */
+ matmulType_matmul, /* nb_matrix_multiply */
+ matmulType_imatmul /* nb_matrix_inplace_multiply */
+};
+
+static PyTypeObject matmulType = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "matmulType",
+ sizeof(matmulObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ matmulType_dealloc, /* destructor tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ &matmulType_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ 0, /* tp_flags */
+ "C level type with matrix operations defined",
+ 0, /* traverseproc tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ PyType_GenericNew, /* tp_new */
+ PyObject_Del, /* tp_free */
+};
+
static struct PyModuleDef _testcapimodule = {
PyModuleDef_HEAD_INIT,
@@ -3544,6 +3803,10 @@ PyInit__testcapi(void)
/* don't use a name starting with "test", since we don't want
test_capi to automatically call this */
PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
+ if (PyType_Ready(&matmulType) < 0)
+ return NULL;
+ Py_INCREF(&matmulType);
+ PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType);
PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index 9925b0e..8f59e03 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -57,7 +57,7 @@ acquire_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds)
if (microseconds > 0) {
- _PyTime_gettimeofday(&endtime);
+ _PyTime_monotonic(&endtime);
endtime.tv_sec += microseconds / (1000 * 1000);
endtime.tv_usec += microseconds % (1000 * 1000);
}
@@ -83,7 +83,7 @@ acquire_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds)
/* If we're using a timeout, recompute the timeout after processing
* signals, since those can take time. */
if (microseconds > 0) {
- _PyTime_gettimeofday(&curtime);
+ _PyTime_monotonic(&curtime);
microseconds = ((endtime.tv_sec - curtime.tv_sec) * 1000000 +
(endtime.tv_usec - curtime.tv_usec));
@@ -192,6 +192,13 @@ PyDoc_STRVAR(locked_doc,
\n\
Return whether the lock is in the locked state.");
+static PyObject *
+lock_repr(lockobject *self)
+{
+ return PyUnicode_FromFormat("<%s %s object at %p>",
+ self->locked ? "locked" : "unlocked", Py_TYPE(self)->tp_name, self);
+}
+
static PyMethodDef lock_methods[] = {
{"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
METH_VARARGS | METH_KEYWORDS, acquire_doc},
@@ -223,7 +230,7 @@ static PyTypeObject Locktype = {
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_reserved*/
- 0, /*tp_repr*/
+ (reprfunc)lock_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
@@ -475,8 +482,10 @@ rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject *
rlock_repr(rlockobject *self)
{
- return PyUnicode_FromFormat("<%s owner=%ld count=%lu>",
- Py_TYPE(self)->tp_name, self->rlock_owner, self->rlock_count);
+ return PyUnicode_FromFormat("<%s %s object owner=%ld count=%lu at %p>",
+ self->rlock_count ? "locked" : "unlocked",
+ Py_TYPE(self)->tp_name, self->rlock_owner,
+ self->rlock_count, self);
}
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index 6d777d3..d5396f6 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -9,8 +9,8 @@ Copyright (C) 1994 Steen Lumholt.
/* TCL/TK VERSION INFO:
- Only Tcl/Tk 8.3.1 and later are supported. Older versions are not
- supported. Use Python 2.6 or older if you cannot upgrade your
+ Only Tcl/Tk 8.4 and later are supported. Older versions are not
+ supported. Use Python 3.4 or older if you cannot upgrade your
Tcl/Tk libraries.
*/
@@ -36,13 +36,6 @@ Copyright (C) 1994 Steen Lumholt.
#define CHECK_SIZE(size, elemsize) \
((size_t)(size) <= Py_MAX((size_t)INT_MAX, UINT_MAX / (size_t)(elemsize)))
-/* Starting with Tcl 8.4, many APIs offer const-correctness. Unfortunately,
- making _tkinter correct for this API means to break earlier
- versions. USE_COMPAT_CONST allows to make _tkinter work with both 8.4 and
- earlier versions. Once Tcl releases before 8.4 don't need to be supported
- anymore, this should go. */
-#define USE_COMPAT_CONST
-
/* If Tcl is compiled for threads, we must also define TCL_THREAD. We define
it always; if Tcl is not threaded, the thread functions in
Tcl are empty. */
@@ -58,15 +51,8 @@ Copyright (C) 1994 Steen Lumholt.
#include "tkinter.h"
-/* For Tcl 8.2 and 8.3, CONST* is not defined (except on Cygwin). */
-#ifndef CONST84_RETURN
-#define CONST84_RETURN
-#undef CONST
-#define CONST
-#endif
-
-#if TK_VERSION_HEX < 0x08030102
-#error "Tk older than 8.3.1 not supported"
+#if TK_VERSION_HEX < 0x08040002
+#error "Tk older than 8.4 not supported"
#endif
#if !(defined(MS_WINDOWS) || defined(__CYGWIN__))
@@ -242,13 +228,13 @@ typedef struct {
int dispatching;
/* We cannot include tclInt.h, as this is internal.
So we cache interesting types here. */
- Tcl_ObjType *BooleanType;
- Tcl_ObjType *ByteArrayType;
- Tcl_ObjType *DoubleType;
- Tcl_ObjType *IntType;
- Tcl_ObjType *ListType;
- Tcl_ObjType *ProcBodyType;
- Tcl_ObjType *StringType;
+ const Tcl_ObjType *BooleanType;
+ const Tcl_ObjType *ByteArrayType;
+ const Tcl_ObjType *DoubleType;
+ const Tcl_ObjType *IntType;
+ const Tcl_ObjType *ListType;
+ const Tcl_ObjType *ProcBodyType;
+ const Tcl_ObjType *StringType;
} TkappObject;
#define Tkapp_Interp(v) (((TkappObject *) (v))->interp)
@@ -376,10 +362,10 @@ unicodeFromTclObj(Tcl_Obj *value)
static PyObject *
-Split(char *list)
+Split(const char *list)
{
int argc;
- char **argv;
+ const char **argv;
PyObject *v;
if (list == NULL) {
@@ -459,9 +445,29 @@ SplitObj(PyObject *arg)
return result;
/* Fall through, returning arg. */
}
+ else if (PyList_Check(arg)) {
+ int i, size;
+ PyObject *elem, *newelem, *result;
+
+ size = PyList_GET_SIZE(arg);
+ result = PyTuple_New(size);
+ if (!result)
+ return NULL;
+ /* Recursively invoke SplitObj for all list items. */
+ for(i = 0; i < size; i++) {
+ elem = PyList_GET_ITEM(arg, i);
+ newelem = SplitObj(elem);
+ if (!newelem) {
+ Py_XDECREF(result);
+ return NULL;
+ }
+ PyTuple_SetItem(result, i, newelem);
+ }
+ return result;
+ }
else if (PyUnicode_Check(arg)) {
int argc;
- char **argv;
+ const char **argv;
char *list = PyUnicode_AsUTF8(arg);
if (list == NULL ||
@@ -476,7 +482,7 @@ SplitObj(PyObject *arg)
}
else if (PyBytes_Check(arg)) {
int argc;
- char **argv;
+ const char **argv;
char *list = PyBytes_AsString(arg);
if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
@@ -543,8 +549,9 @@ static void EnableEventHook(void); /* Forward */
static void DisableEventHook(void); /* Forward */
static TkappObject *
-Tkapp_New(char *screenName, char *className,
- int interactive, int wantobjects, int wantTk, int sync, char *use)
+Tkapp_New(const char *screenName, const char *className,
+ int interactive, int wantobjects, int wantTk, int sync,
+ const char *use)
{
TkappObject *v;
char *argv0;
@@ -598,7 +605,7 @@ Tkapp_New(char *screenName, char *className,
Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
/* This is used to get the application class for Tk 4.1 and up */
- argv0 = (char*)attemptckalloc(strlen(className) + 1);
+ argv0 = (char*)PyMem_Malloc(strlen(className) + 1);
if (!argv0) {
PyErr_NoMemory();
Py_DECREF(v);
@@ -609,7 +616,7 @@ Tkapp_New(char *screenName, char *className,
if (Py_ISUPPER(Py_CHARMASK(argv0[0])))
argv0[0] = Py_TOLOWER(Py_CHARMASK(argv0[0]));
Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY);
- ckfree(argv0);
+ PyMem_Free(argv0);
if (! wantTk) {
Tcl_SetVar(v->interp,
@@ -632,7 +639,7 @@ Tkapp_New(char *screenName, char *className,
if (use)
len += strlen(use) + sizeof "-use ";
- args = (char*)attemptckalloc(len);
+ args = (char*)PyMem_Malloc(len);
if (!args) {
PyErr_NoMemory();
Py_DECREF(v);
@@ -650,7 +657,7 @@ Tkapp_New(char *screenName, char *className,
}
Tcl_SetVar(v->interp, "argv", args, TCL_GLOBAL_ONLY);
- ckfree(args);
+ PyMem_Free(args);
}
if (Tcl_AppInit(v->interp) != TCL_OK) {
@@ -894,24 +901,28 @@ AsObj(PyObject *value)
}
else if (PyFloat_Check(value))
return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value));
- else if (PyTuple_Check(value)) {
+ else if (PyTuple_Check(value) || PyList_Check(value)) {
Tcl_Obj **argv;
Py_ssize_t size, i;
- size = PyTuple_Size(value);
+ size = PySequence_Fast_GET_SIZE(value);
if (size == 0)
return Tcl_NewListObj(0, NULL);
if (!CHECK_SIZE(size, sizeof(Tcl_Obj *))) {
- PyErr_SetString(PyExc_OverflowError, "tuple is too long");
+ PyErr_SetString(PyExc_OverflowError,
+ PyTuple_Check(value) ? "tuple is too long" :
+ "list is too long");
return NULL;
}
- argv = (Tcl_Obj **) attemptckalloc(((size_t)size) * sizeof(Tcl_Obj *));
- if(!argv)
- return 0;
+ argv = (Tcl_Obj **) PyMem_Malloc(((size_t)size) * sizeof(Tcl_Obj *));
+ if (!argv) {
+ PyErr_NoMemory();
+ return NULL;
+ }
for (i = 0; i < size; i++)
- argv[i] = AsObj(PyTuple_GetItem(value,i));
- result = Tcl_NewListObj(PyTuple_Size(value), argv);
- ckfree(FREECAST argv);
+ argv[i] = AsObj(PySequence_Fast_GET_ITEM(value,i));
+ result = Tcl_NewListObj(size, argv);
+ PyMem_Free(argv);
return result;
}
else if (PyUnicode_Check(value)) {
@@ -937,7 +948,7 @@ AsObj(PyObject *value)
if (kind == sizeof(Tcl_UniChar))
return Tcl_NewUnicodeObj(inbuf, size);
allocsize = ((size_t)size) * sizeof(Tcl_UniChar);
- outbuf = (Tcl_UniChar*)attemptckalloc(allocsize);
+ outbuf = (Tcl_UniChar*)PyMem_Malloc(allocsize);
/* Else overflow occurred, and we take the next exit */
if (!outbuf) {
PyErr_NoMemory();
@@ -954,14 +965,14 @@ AsObj(PyObject *value)
"character U+%x is above the range "
"(U+0000-U+FFFF) allowed by Tcl",
ch);
- ckfree(FREECAST outbuf);
+ PyMem_Free(outbuf);
return NULL;
}
#endif
outbuf[i] = ch;
}
result = Tcl_NewUnicodeObj(outbuf, size);
- ckfree(FREECAST outbuf);
+ PyMem_Free(outbuf);
return result;
}
else if(PyTclObject_Check(value)) {
@@ -1073,7 +1084,7 @@ Tkapp_CallDeallocArgs(Tcl_Obj** objv, Tcl_Obj** objStore, int objc)
for (i = 0; i < objc; i++)
Tcl_DecrRefCount(objv[i]);
if (objv != objStore)
- ckfree(FREECAST objv);
+ PyMem_Free(objv);
}
/* Convert Python objects to Tcl objects. This must happen in the
@@ -1087,7 +1098,7 @@ Tkapp_CallArgs(PyObject *args, Tcl_Obj** objStore, int *pobjc)
if (args == NULL)
/* do nothing */;
- else if (!PyTuple_Check(args)) {
+ else if (!(PyTuple_Check(args) || PyList_Check(args))) {
objv[0] = AsObj(args);
if (objv[0] == 0)
goto finally;
@@ -1095,14 +1106,16 @@ Tkapp_CallArgs(PyObject *args, Tcl_Obj** objStore, int *pobjc)
Tcl_IncrRefCount(objv[0]);
}
else {
- objc = PyTuple_Size(args);
+ objc = PySequence_Fast_GET_SIZE(args);
if (objc > ARGSZ) {
if (!CHECK_SIZE(objc, sizeof(Tcl_Obj *))) {
- PyErr_SetString(PyExc_OverflowError, "tuple is too long");
+ PyErr_SetString(PyExc_OverflowError,
+ PyTuple_Check(args) ? "tuple is too long" :
+ "list is too long");
return NULL;
}
- objv = (Tcl_Obj **)attemptckalloc(((size_t)objc) * sizeof(Tcl_Obj *));
+ objv = (Tcl_Obj **)PyMem_Malloc(((size_t)objc) * sizeof(Tcl_Obj *));
if (objv == NULL) {
PyErr_NoMemory();
objc = 0;
@@ -1111,7 +1124,7 @@ Tkapp_CallArgs(PyObject *args, Tcl_Obj** objStore, int *pobjc)
}
for (i = 0; i < objc; i++) {
- PyObject *v = PyTuple_GetItem(args, i);
+ PyObject *v = PySequence_Fast_GET_ITEM(args, i);
if (v == Py_None) {
objc = i;
break;
@@ -1411,8 +1424,8 @@ varname_converter(PyObject *in, void *_out)
return 0;
}
s = PyBytes_AsString(in);
- if (strlen(s) != PyBytes_Size(in)) {
- PyErr_SetString(PyExc_ValueError, "null byte in bytes object");
+ if (strlen(s) != (size_t)PyBytes_Size(in)) {
+ PyErr_SetString(PyExc_ValueError, "embedded null byte");
return 0;
}
*out = s;
@@ -1428,8 +1441,8 @@ varname_converter(PyObject *in, void *_out)
PyErr_SetString(PyExc_OverflowError, "string is too long");
return 0;
}
- if (strlen(s) != size) {
- PyErr_SetString(PyExc_ValueError, "null character in string");
+ if (strlen(s) != (size_t)size) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
return 0;
}
*out = s;
@@ -1844,7 +1857,7 @@ Tkapp_SplitList(PyObject *self, PyObject *args)
{
char *list;
int argc;
- char **argv;
+ const char **argv;
PyObject *arg, *v;
int i;
@@ -1873,6 +1886,9 @@ Tkapp_SplitList(PyObject *self, PyObject *args)
Py_INCREF(arg);
return arg;
}
+ if (PyList_Check(arg)) {
+ return PySequence_Tuple(arg);
+ }
if (!PyArg_ParseTuple(args, "et:splitlist", "utf-8", &list))
return NULL;
@@ -1934,7 +1950,7 @@ Tkapp_Split(PyObject *self, PyObject *args)
}
return v;
}
- if (PyTuple_Check(arg))
+ if (PyTuple_Check(arg) || PyList_Check(arg))
return SplitObj(arg);
if (!PyArg_ParseTuple(args, "et:split", "utf-8", &list))
@@ -1968,7 +1984,7 @@ PythonCmd_Error(Tcl_Interp *interp)
* function or method.
*/
static int
-PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
+PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData;
PyObject *func, *arg, *res;
@@ -2736,35 +2752,15 @@ _flatten1(FlattenContext* context, PyObject* item, int depth)
PyErr_SetString(PyExc_ValueError,
"nesting too deep in _flatten");
return 0;
- } else if (PyList_Check(item)) {
- size = PyList_GET_SIZE(item);
+ } else if (PyTuple_Check(item) || PyList_Check(item)) {
+ size = PySequence_Fast_GET_SIZE(item);
/* preallocate (assume no nesting) */
if (context->size + size > context->maxsize &&
!_bump(context, size))
return 0;
/* copy items to output tuple */
for (i = 0; i < size; i++) {
- PyObject *o = PyList_GET_ITEM(item, i);
- if (PyList_Check(o) || PyTuple_Check(o)) {
- if (!_flatten1(context, o, depth + 1))
- return 0;
- } else if (o != Py_None) {
- if (context->size + 1 > context->maxsize &&
- !_bump(context, 1))
- return 0;
- Py_INCREF(o);
- PyTuple_SET_ITEM(context->tuple,
- context->size++, o);
- }
- }
- } else if (PyTuple_Check(item)) {
- /* same, for tuples */
- size = PyTuple_GET_SIZE(item);
- if (context->size + size > context->maxsize &&
- !_bump(context, size))
- return 0;
- for (i = 0; i < size; i++) {
- PyObject *o = PyTuple_GET_ITEM(item, i);
+ PyObject *o = PySequence_Fast_GET_ITEM(item, i);
if (PyList_Check(o) || PyTuple_Check(o)) {
if (!_flatten1(context, o, depth + 1))
return 0;
@@ -2822,7 +2818,7 @@ Tkinter_Create(PyObject *self, PyObject *args)
try getting rid of it. */
char *className = NULL;
int interactive = 0;
- int wantobjects = 0;
+ int wantobjects = 1;
int wantTk = 1; /* If false, then Tk_Init() doesn't get called */
int sync = 0; /* pass -sync to wish */
char *use = NULL; /* pass -use to wish */
diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c
index bbe7259..12e32eb 100644
--- a/Modules/_tracemalloc.c
+++ b/Modules/_tracemalloc.c
@@ -16,14 +16,11 @@ static void raw_free(void *ptr);
# define TRACE_DEBUG
#endif
-#define _STR(VAL) #VAL
-#define STR(VAL) _STR(VAL)
-
/* Protected by the GIL */
static struct {
- PyMemAllocator mem;
- PyMemAllocator raw;
- PyMemAllocator obj;
+ PyMemAllocatorEx mem;
+ PyMemAllocatorEx raw;
+ PyMemAllocatorEx obj;
} allocators;
static struct {
@@ -82,7 +79,7 @@ typedef struct {
(sizeof(traceback_t) + sizeof(frame_t) * (NFRAME - 1))
#define MAX_NFRAME \
- ((INT_MAX - sizeof(traceback_t)) / sizeof(frame_t) + 1)
+ ((INT_MAX - (int)sizeof(traceback_t)) / (int)sizeof(frame_t) + 1)
static PyObject *unknown_filename = NULL;
static traceback_t tracemalloc_empty_traceback;
@@ -476,17 +473,22 @@ tracemalloc_remove_trace(void *ptr)
}
static void*
-tracemalloc_malloc(void *ctx, size_t size)
+tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
{
- PyMemAllocator *alloc = (PyMemAllocator *)ctx;
+ PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
void *ptr;
- ptr = alloc->malloc(alloc->ctx, size);
+ assert(elsize == 0 || nelem <= PY_SIZE_MAX / elsize);
+
+ if (use_calloc)
+ ptr = alloc->calloc(alloc->ctx, nelem, elsize);
+ else
+ ptr = alloc->malloc(alloc->ctx, nelem * elsize);
if (ptr == NULL)
return NULL;
TABLES_LOCK();
- if (tracemalloc_add_trace(ptr, size) < 0) {
+ if (tracemalloc_add_trace(ptr, nelem * elsize) < 0) {
/* Failed to allocate a trace for the new memory block */
TABLES_UNLOCK();
alloc->free(alloc->ctx, ptr);
@@ -499,7 +501,7 @@ tracemalloc_malloc(void *ctx, size_t size)
static void*
tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
{
- PyMemAllocator *alloc = (PyMemAllocator *)ctx;
+ PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
void *ptr2;
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
@@ -544,7 +546,7 @@ tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
static void
tracemalloc_free(void *ctx, void *ptr)
{
- PyMemAllocator *alloc = (PyMemAllocator *)ctx;
+ PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
if (ptr == NULL)
return;
@@ -560,13 +562,16 @@ tracemalloc_free(void *ctx, void *ptr)
}
static void*
-tracemalloc_malloc_gil(void *ctx, size_t size)
+tracemalloc_alloc_gil(int use_calloc, void *ctx, size_t nelem, size_t elsize)
{
void *ptr;
if (get_reentrant()) {
- PyMemAllocator *alloc = (PyMemAllocator *)ctx;
- return alloc->malloc(alloc->ctx, size);
+ PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
+ if (use_calloc)
+ return alloc->calloc(alloc->ctx, nelem, elsize);
+ else
+ return alloc->malloc(alloc->ctx, nelem * elsize);
}
/* Ignore reentrant call. PyObjet_Malloc() calls PyMem_Malloc() for
@@ -574,13 +579,25 @@ tracemalloc_malloc_gil(void *ctx, size_t size)
allocation twice. */
set_reentrant(1);
- ptr = tracemalloc_malloc(ctx, size);
+ ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize);
set_reentrant(0);
return ptr;
}
static void*
+tracemalloc_malloc_gil(void *ctx, size_t size)
+{
+ return tracemalloc_alloc_gil(0, ctx, 1, size);
+}
+
+static void*
+tracemalloc_calloc_gil(void *ctx, size_t nelem, size_t elsize)
+{
+ return tracemalloc_alloc_gil(1, ctx, nelem, elsize);
+}
+
+static void*
tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
{
void *ptr2;
@@ -590,7 +607,7 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
Example: PyMem_RawRealloc() is called internally by pymalloc
(_PyObject_Malloc() and _PyObject_Realloc()) to allocate a new
arena (new_arena()). */
- PyMemAllocator *alloc = (PyMemAllocator *)ctx;
+ PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
if (ptr2 != NULL && ptr != NULL) {
@@ -614,7 +631,7 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
#ifdef TRACE_RAW_MALLOC
static void*
-tracemalloc_raw_malloc(void *ctx, size_t size)
+tracemalloc_raw_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
{
#ifdef WITH_THREAD
PyGILState_STATE gil_state;
@@ -622,8 +639,11 @@ tracemalloc_raw_malloc(void *ctx, size_t size)
void *ptr;
if (get_reentrant()) {
- PyMemAllocator *alloc = (PyMemAllocator *)ctx;
- return alloc->malloc(alloc->ctx, size);
+ PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
+ if (use_calloc)
+ return alloc->calloc(alloc->ctx, nelem, elsize);
+ else
+ return alloc->malloc(alloc->ctx, nelem * elsize);
}
/* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc()
@@ -633,10 +653,10 @@ tracemalloc_raw_malloc(void *ctx, size_t size)
#ifdef WITH_THREAD
gil_state = PyGILState_Ensure();
- ptr = tracemalloc_malloc(ctx, size);
+ ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize);
PyGILState_Release(gil_state);
#else
- ptr = tracemalloc_malloc(ctx, size);
+ ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize);
#endif
set_reentrant(0);
@@ -644,6 +664,18 @@ tracemalloc_raw_malloc(void *ctx, size_t size)
}
static void*
+tracemalloc_raw_malloc(void *ctx, size_t size)
+{
+ return tracemalloc_raw_alloc(0, ctx, 1, size);
+}
+
+static void*
+tracemalloc_raw_calloc(void *ctx, size_t nelem, size_t elsize)
+{
+ return tracemalloc_raw_alloc(1, ctx, nelem, elsize);
+}
+
+static void*
tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
{
#ifdef WITH_THREAD
@@ -653,7 +685,7 @@ tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
if (get_reentrant()) {
/* Reentrant call to PyMem_RawRealloc(). */
- PyMemAllocator *alloc = (PyMemAllocator *)ctx;
+ PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
@@ -831,7 +863,7 @@ tracemalloc_deinit(void)
static int
tracemalloc_start(int max_nframe)
{
- PyMemAllocator alloc;
+ PyMemAllocatorEx alloc;
size_t size;
if (tracemalloc_init() < 0)
@@ -856,6 +888,7 @@ tracemalloc_start(int max_nframe)
#ifdef TRACE_RAW_MALLOC
alloc.malloc = tracemalloc_raw_malloc;
+ alloc.calloc = tracemalloc_raw_calloc;
alloc.realloc = tracemalloc_raw_realloc;
alloc.free = tracemalloc_free;
@@ -865,6 +898,7 @@ tracemalloc_start(int max_nframe)
#endif
alloc.malloc = tracemalloc_malloc_gil;
+ alloc.calloc = tracemalloc_calloc_gil;
alloc.realloc = tracemalloc_realloc_gil;
alloc.free = tracemalloc_free;
@@ -1195,7 +1229,7 @@ py_tracemalloc_start(PyObject *self, PyObject *args)
if (nframe < 1 || nframe > MAX_NFRAME) {
PyErr_Format(PyExc_ValueError,
"the number of frames must be in range [1; %i]",
- (int)MAX_NFRAME);
+ MAX_NFRAME);
return NULL;
}
nframe_int = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int);
diff --git a/Modules/_winapi.c b/Modules/_winapi.c
index d472c9e..51c4d5f 100644
--- a/Modules/_winapi.c
+++ b/Modules/_winapi.c
@@ -40,6 +40,7 @@
#define WINDOWS_LEAN_AND_MEAN
#include "windows.h"
#include <crtdbg.h>
+#include "winreparse.h"
#if defined(MS_WIN32) && !defined(MS_WIN64)
#define HANDLE_TO_PYNUM(handle) \
@@ -401,6 +402,140 @@ winapi_CreateFile(PyObject *self, PyObject *args)
}
static PyObject *
+winapi_CreateJunction(PyObject *self, PyObject *args)
+{
+ /* Input arguments */
+ LPWSTR src_path = NULL;
+ LPWSTR dst_path = NULL;
+
+ /* Privilege adjustment */
+ HANDLE token = NULL;
+ TOKEN_PRIVILEGES tp;
+
+ /* Reparse data buffer */
+ const USHORT prefix_len = 4;
+ USHORT print_len = 0;
+ USHORT rdb_size = 0;
+ PREPARSE_DATA_BUFFER rdb = NULL;
+
+ /* Junction point creation */
+ HANDLE junction = NULL;
+ DWORD ret = 0;
+
+ if (!PyArg_ParseTuple(args, "uu", &src_path, &dst_path))
+ return NULL;
+
+ if (src_path == NULL || dst_path == NULL)
+ return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER);
+
+ if (wcsncmp(src_path, L"\\??\\", prefix_len) == 0)
+ return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER);
+
+ /* Adjust privileges to allow rewriting directory entry as a
+ junction point. */
+ if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
+ goto cleanup;
+
+ if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid))
+ goto cleanup;
+
+ tp.PrivilegeCount = 1;
+ tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
+ if (!AdjustTokenPrivileges(token, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
+ NULL, NULL))
+ goto cleanup;
+
+ if (GetFileAttributesW(src_path) == INVALID_FILE_ATTRIBUTES)
+ goto cleanup;
+
+ /* Store the absolute link target path length in print_len. */
+ print_len = (USHORT)GetFullPathNameW(src_path, 0, NULL, NULL);
+ if (print_len == 0)
+ goto cleanup;
+
+ /* NUL terminator should not be part of print_len. */
+ --print_len;
+
+ /* REPARSE_DATA_BUFFER usage is heavily under-documented, especially for
+ junction points. Here's what I've learned along the way:
+ - A junction point has two components: a print name and a substitute
+ name. They both describe the link target, but the substitute name is
+ the physical target and the print name is shown in directory listings.
+ - The print name must be a native name, prefixed with "\??\".
+ - Both names are stored after each other in the same buffer (the
+ PathBuffer) and both must be NUL-terminated.
+ - There are four members defining their respective offset and length
+ inside PathBuffer: SubstituteNameOffset, SubstituteNameLength,
+ PrintNameOffset and PrintNameLength.
+ - The total size we need to allocate for the REPARSE_DATA_BUFFER, thus,
+ is the sum of:
+ - the fixed header size (REPARSE_DATA_BUFFER_HEADER_SIZE)
+ - the size of the MountPointReparseBuffer member without the PathBuffer
+ - the size of the prefix ("\??\") in bytes
+ - the size of the print name in bytes
+ - the size of the substitute name in bytes
+ - the size of two NUL terminators in bytes */
+ rdb_size = REPARSE_DATA_BUFFER_HEADER_SIZE +
+ sizeof(rdb->MountPointReparseBuffer) -
+ sizeof(rdb->MountPointReparseBuffer.PathBuffer) +
+ /* Two +1's for NUL terminators. */
+ (prefix_len + print_len + 1 + print_len + 1) * sizeof(WCHAR);
+ rdb = (PREPARSE_DATA_BUFFER)PyMem_RawMalloc(rdb_size);
+ if (rdb == NULL)
+ goto cleanup;
+
+ memset(rdb, 0, rdb_size);
+ rdb->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
+ rdb->ReparseDataLength = rdb_size - REPARSE_DATA_BUFFER_HEADER_SIZE;
+ rdb->MountPointReparseBuffer.SubstituteNameOffset = 0;
+ rdb->MountPointReparseBuffer.SubstituteNameLength =
+ (prefix_len + print_len) * sizeof(WCHAR);
+ rdb->MountPointReparseBuffer.PrintNameOffset =
+ rdb->MountPointReparseBuffer.SubstituteNameLength + sizeof(WCHAR);
+ rdb->MountPointReparseBuffer.PrintNameLength = print_len * sizeof(WCHAR);
+
+ /* Store the full native path of link target at the substitute name
+ offset (0). */
+ wcscpy(rdb->MountPointReparseBuffer.PathBuffer, L"\\??\\");
+ if (GetFullPathNameW(src_path, print_len + 1,
+ rdb->MountPointReparseBuffer.PathBuffer + prefix_len,
+ NULL) == 0)
+ goto cleanup;
+
+ /* Copy everything but the native prefix to the print name offset. */
+ wcscpy(rdb->MountPointReparseBuffer.PathBuffer +
+ prefix_len + print_len + 1,
+ rdb->MountPointReparseBuffer.PathBuffer + prefix_len);
+
+ /* Create a directory for the junction point. */
+ if (!CreateDirectoryW(dst_path, NULL))
+ goto cleanup;
+
+ junction = CreateFileW(dst_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
+ OPEN_EXISTING,
+ FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
+ if (junction == INVALID_HANDLE_VALUE)
+ goto cleanup;
+
+ /* Make the directory entry a junction point. */
+ if (!DeviceIoControl(junction, FSCTL_SET_REPARSE_POINT, rdb, rdb_size,
+ NULL, 0, &ret, NULL))
+ goto cleanup;
+
+cleanup:
+ ret = GetLastError();
+
+ CloseHandle(token);
+ CloseHandle(junction);
+ PyMem_RawFree(rdb);
+
+ if (ret != 0)
+ return PyErr_SetFromWindowsErr(ret);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
winapi_CreateNamedPipe(PyObject *self, PyObject *args)
{
LPCTSTR lpName;
@@ -1235,6 +1370,8 @@ static PyMethodDef winapi_functions[] = {
METH_VARARGS | METH_KEYWORDS, ""},
{"CreateFile", winapi_CreateFile, METH_VARARGS,
""},
+ {"CreateJunction", winapi_CreateJunction, METH_VARARGS,
+ ""},
{"CreateNamedPipe", winapi_CreateNamedPipe, METH_VARARGS,
""},
{"CreatePipe", winapi_CreatePipe, METH_VARARGS,
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 5ab1ab6..6e755f4 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -15,6 +15,12 @@
#endif /* HAVE_SYS_TYPES_H */
#endif /* !STDC_HEADERS */
+/*[clinic input]
+output preset file
+module array
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=0909c1a148c69931]*/
+
struct arrayobject; /* Forward */
/* All possible arraydescr values are defined in the vector "descriptors"
@@ -42,6 +48,63 @@ typedef struct arrayobject {
static PyTypeObject Arraytype;
+typedef struct {
+ PyObject_HEAD
+ Py_ssize_t index;
+ arrayobject *ao;
+ PyObject* (*getitem)(struct arrayobject *, Py_ssize_t);
+} arrayiterobject;
+
+static PyTypeObject PyArrayIter_Type;
+
+#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
+
+enum machine_format_code {
+ UNKNOWN_FORMAT = -1,
+ /* UNKNOWN_FORMAT is used to indicate that the machine format for an
+ * array type code cannot be interpreted. When this occurs, a list of
+ * Python objects is used to represent the content of the array
+ * instead of using the memory content of the array directly. In that
+ * case, the array_reconstructor mechanism is bypassed completely, and
+ * the standard array constructor is used instead.
+ *
+ * This is will most likely occur when the machine doesn't use IEEE
+ * floating-point numbers.
+ */
+
+ UNSIGNED_INT8 = 0,
+ SIGNED_INT8 = 1,
+ UNSIGNED_INT16_LE = 2,
+ UNSIGNED_INT16_BE = 3,
+ SIGNED_INT16_LE = 4,
+ SIGNED_INT16_BE = 5,
+ UNSIGNED_INT32_LE = 6,
+ UNSIGNED_INT32_BE = 7,
+ SIGNED_INT32_LE = 8,
+ SIGNED_INT32_BE = 9,
+ UNSIGNED_INT64_LE = 10,
+ UNSIGNED_INT64_BE = 11,
+ SIGNED_INT64_LE = 12,
+ SIGNED_INT64_BE = 13,
+ IEEE_754_FLOAT_LE = 14,
+ IEEE_754_FLOAT_BE = 15,
+ IEEE_754_DOUBLE_LE = 16,
+ IEEE_754_DOUBLE_BE = 17,
+ UTF16_LE = 18,
+ UTF16_BE = 19,
+ UTF32_LE = 20,
+ UTF32_BE = 21
+};
+#define MACHINE_FORMAT_CODE_MIN 0
+#define MACHINE_FORMAT_CODE_MAX 21
+
+
+/*
+ * Must come after arrayobject, arrayiterobject,
+ * and enum machine_code_type definitions.
+ */
+#include "clinic/arraymodule.c.h"
+
#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
#define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
@@ -471,6 +534,10 @@ static struct arraydescr descriptors[] = {
/****************************************************************************
Implementations of array object methods.
****************************************************************************/
+/*[clinic input]
+class array.array "arrayobject *" "&Arraytype"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad43d37e942a8854]*/
static PyObject *
newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
@@ -684,16 +751,35 @@ array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
return (PyObject *)np;
}
+
+/*[clinic input]
+array.array.__copy__
+
+Return a copy of the array.
+[clinic start generated code]*/
+
static PyObject *
-array_copy(arrayobject *a, PyObject *unused)
+array_array___copy___impl(arrayobject *self)
+/*[clinic end generated code: output=dec7c3f925d9619e input=ad1ee5b086965f09]*/
{
- return array_slice(a, 0, Py_SIZE(a));
+ return array_slice(self, 0, Py_SIZE(self));
}
-PyDoc_STRVAR(copy_doc,
-"copy(array)\n\
-\n\
- Return a copy of the array.");
+/*[clinic input]
+array.array.__deepcopy__
+
+ unused: object
+ /
+
+Return a copy of the array.
+[clinic start generated code]*/
+
+static PyObject *
+array_array___deepcopy__(arrayobject *self, PyObject *unused)
+/*[clinic end generated code: output=1ec748d8e14a9faa input=2405ecb4933748c4]*/
+{
+ return array_array___copy___impl(self);
+}
static PyObject *
array_concat(arrayobject *a, PyObject *bb)
@@ -961,8 +1047,18 @@ ins(arrayobject *self, Py_ssize_t where, PyObject *v)
return Py_None;
}
+/*[clinic input]
+array.array.count
+
+ v: object
+ /
+
+Return number of occurrences of v in the array.
+[clinic start generated code]*/
+
static PyObject *
-array_count(arrayobject *self, PyObject *v)
+array_array_count(arrayobject *self, PyObject *v)
+/*[clinic end generated code: output=3dd3624bf7135a3a input=d9bce9d65e39d1f5]*/
{
Py_ssize_t count = 0;
Py_ssize_t i;
@@ -984,13 +1080,19 @@ array_count(arrayobject *self, PyObject *v)
return PyLong_FromSsize_t(count);
}
-PyDoc_STRVAR(count_doc,
-"count(x)\n\
-\n\
-Return number of occurrences of x in the array.");
+
+/*[clinic input]
+array.array.index
+
+ v: object
+ /
+
+Return index of first occurrence of v in the array.
+[clinic start generated code]*/
static PyObject *
-array_index(arrayobject *self, PyObject *v)
+array_array_index(arrayobject *self, PyObject *v)
+/*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/
{
Py_ssize_t i;
@@ -1013,11 +1115,6 @@ array_index(arrayobject *self, PyObject *v)
return NULL;
}
-PyDoc_STRVAR(index_doc,
-"index(x)\n\
-\n\
-Return index of first occurrence of x in the array.");
-
static int
array_contains(arrayobject *self, PyObject *v)
{
@@ -1034,8 +1131,18 @@ array_contains(arrayobject *self, PyObject *v)
return cmp;
}
+/*[clinic input]
+array.array.remove
+
+ v: object
+ /
+
+Remove the first occurrence of v in the array.
+[clinic start generated code]*/
+
static PyObject *
-array_remove(arrayobject *self, PyObject *v)
+array_array_remove(arrayobject *self, PyObject *v)
+/*[clinic end generated code: output=bef06be9fdf9dceb input=0b1e5aed25590027]*/
{
int i;
@@ -1062,18 +1169,23 @@ array_remove(arrayobject *self, PyObject *v)
return NULL;
}
-PyDoc_STRVAR(remove_doc,
-"remove(x)\n\
-\n\
-Remove the first occurrence of x in the array.");
+/*[clinic input]
+array.array.pop
+
+ i: Py_ssize_t = -1
+ /
+
+Return the i-th element and delete it from the array.
+
+i defaults to -1.
+[clinic start generated code]*/
static PyObject *
-array_pop(arrayobject *self, PyObject *args)
+array_array_pop_impl(arrayobject *self, Py_ssize_t i)
+/*[clinic end generated code: output=bc1f0c54fe5308e4 input=8e5feb4c1a11cd44]*/
{
- Py_ssize_t i = -1;
PyObject *v;
- if (!PyArg_ParseTuple(args, "|n:pop", &i))
- return NULL;
+
if (Py_SIZE(self) == 0) {
/* Special-case most common failure cause */
PyErr_SetString(PyExc_IndexError, "pop from empty array");
@@ -1095,13 +1207,18 @@ array_pop(arrayobject *self, PyObject *args)
return v;
}
-PyDoc_STRVAR(pop_doc,
-"pop([i])\n\
-\n\
-Return the i-th element and delete it from the array. i defaults to -1.");
+/*[clinic input]
+array.array.extend
+
+ bb: object
+ /
+
+Append items to the end of the array.
+[clinic start generated code]*/
static PyObject *
-array_extend(arrayobject *self, PyObject *bb)
+array_array_extend(arrayobject *self, PyObject *bb)
+/*[clinic end generated code: output=bbddbc8e8bef871d input=43be86aba5c31e44]*/
{
if (array_do_extend(self, bb) == -1)
return NULL;
@@ -1109,29 +1226,35 @@ array_extend(arrayobject *self, PyObject *bb)
return Py_None;
}
-PyDoc_STRVAR(extend_doc,
-"extend(array or iterable)\n\
-\n\
- Append items to the end of the array.");
+/*[clinic input]
+array.array.insert
+
+ i: Py_ssize_t
+ v: object
+ /
+
+Insert a new item v into the array before position i.
+[clinic start generated code]*/
static PyObject *
-array_insert(arrayobject *self, PyObject *args)
+array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v)
+/*[clinic end generated code: output=5a3648e278348564 input=5577d1b4383e9313]*/
{
- Py_ssize_t i;
- PyObject *v;
- if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
- return NULL;
return ins(self, i, v);
}
-PyDoc_STRVAR(insert_doc,
-"insert(i,x)\n\
-\n\
-Insert a new item x into the array before position i.");
+/*[clinic input]
+array.array.buffer_info
+
+Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array's contents.
+The length should be multiplied by the itemsize attribute to calculate
+the buffer length in bytes.
+[clinic start generated code]*/
static PyObject *
-array_buffer_info(arrayobject *self, PyObject *unused)
+array_array_buffer_info_impl(arrayobject *self)
+/*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=a58bae5c6e1ac6a6]*/
{
PyObject *retval = NULL, *v;
@@ -1156,29 +1279,34 @@ array_buffer_info(arrayobject *self, PyObject *unused)
return retval;
}
-PyDoc_STRVAR(buffer_info_doc,
-"buffer_info() -> (address, length)\n\
-\n\
-Return a tuple (address, length) giving the current memory address and\n\
-the length in items of the buffer used to hold array's contents\n\
-The length should be multiplied by the itemsize attribute to calculate\n\
-the buffer length in bytes.");
+/*[clinic input]
+array.array.append
+ v: object
+ /
+
+Append new value v to the end of the array.
+[clinic start generated code]*/
static PyObject *
-array_append(arrayobject *self, PyObject *v)
+array_array_append(arrayobject *self, PyObject *v)
+/*[clinic end generated code: output=745a0669bf8db0e2 input=0b98d9d78e78f0fa]*/
{
return ins(self, Py_SIZE(self), v);
}
-PyDoc_STRVAR(append_doc,
-"append(x)\n\
-\n\
-Append new value x to the end of the array.");
+/*[clinic input]
+array.array.byteswap
+
+Byteswap all items of the array.
+If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
+raised.
+[clinic start generated code]*/
static PyObject *
-array_byteswap(arrayobject *self, PyObject *unused)
+array_array_byteswap_impl(arrayobject *self)
+/*[clinic end generated code: output=5f8236cbdf0d90b5 input=6a85591b950a0186]*/
{
char *p;
Py_ssize_t i;
@@ -1228,14 +1356,15 @@ array_byteswap(arrayobject *self, PyObject *unused)
return Py_None;
}
-PyDoc_STRVAR(byteswap_doc,
-"byteswap()\n\
-\n\
-Byteswap all items of the array. If the items in the array are not 1, 2,\n\
-4, or 8 bytes in size, RuntimeError is raised.");
+/*[clinic input]
+array.array.reverse
+
+Reverse the order of the items in the array.
+[clinic start generated code]*/
static PyObject *
-array_reverse(arrayobject *self, PyObject *unused)
+array_array_reverse_impl(arrayobject *self)
+/*[clinic end generated code: output=c04868b36f6f4089 input=cd904f01b27d966a]*/
{
Py_ssize_t itemsize = self->ob_descr->itemsize;
char *p, *q;
@@ -1261,27 +1390,26 @@ array_reverse(arrayobject *self, PyObject *unused)
return Py_None;
}
-PyDoc_STRVAR(reverse_doc,
-"reverse()\n\
-\n\
-Reverse the order of the items in the array.");
+/*[clinic input]
+array.array.fromfile
+ f: object
+ n: Py_ssize_t
+ /
-/* Forward */
-static PyObject *array_frombytes(arrayobject *self, PyObject *args);
+Read n objects from the file object f and append them to the end of the array.
+[clinic start generated code]*/
static PyObject *
-array_fromfile(arrayobject *self, PyObject *args)
+array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
+/*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/
{
- PyObject *f, *b, *res;
+ PyObject *args, *b, *res;
Py_ssize_t itemsize = self->ob_descr->itemsize;
- Py_ssize_t n, nbytes;
+ Py_ssize_t nbytes;
_Py_IDENTIFIER(read);
int not_enough_bytes;
- if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
- return NULL;
-
if (n < 0) {
PyErr_SetString(PyExc_ValueError, "negative count");
return NULL;
@@ -1310,7 +1438,7 @@ array_fromfile(arrayobject *self, PyObject *args)
if (args == NULL)
return NULL;
- res = array_frombytes(self, args);
+ res = array_array_frombytes(self, args);
Py_DECREF(args);
if (res == NULL)
return NULL;
@@ -1325,15 +1453,18 @@ array_fromfile(arrayobject *self, PyObject *args)
return res;
}
-PyDoc_STRVAR(fromfile_doc,
-"fromfile(f, n)\n\
-\n\
-Read n objects from the file object f and append them to the end of the\n\
-array.");
+/*[clinic input]
+array.array.tofile
+ f: object
+ /
+
+Write all items (as machine values) to the file object f.
+[clinic start generated code]*/
static PyObject *
-array_tofile(arrayobject *self, PyObject *f)
+array_array_tofile(arrayobject *self, PyObject *f)
+/*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/
{
Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
/* Write 64K blocks at a time */
@@ -1368,14 +1499,18 @@ array_tofile(arrayobject *self, PyObject *f)
return Py_None;
}
-PyDoc_STRVAR(tofile_doc,
-"tofile(f)\n\
-\n\
-Write all items (as machine values) to the file object f.");
+/*[clinic input]
+array.array.fromlist
+ list: object
+ /
+
+Append items to array from list.
+[clinic start generated code]*/
static PyObject *
-array_fromlist(arrayobject *self, PyObject *list)
+array_array_fromlist(arrayobject *self, PyObject *list)
+/*[clinic end generated code: output=26411c2d228a3e3f input=be2605a96c49680f]*/
{
Py_ssize_t n;
@@ -1402,13 +1537,15 @@ array_fromlist(arrayobject *self, PyObject *list)
return Py_None;
}
-PyDoc_STRVAR(fromlist_doc,
-"fromlist(list)\n\
-\n\
-Append items to array from list.");
+/*[clinic input]
+array.array.tolist
+
+Convert array to an ordinary list with the same items.
+[clinic start generated code]*/
static PyObject *
-array_tolist(arrayobject *self, PyObject *unused)
+array_array_tolist_impl(arrayobject *self)
+/*[clinic end generated code: output=00b60cc9eab8ef89 input=a8d7784a94f86b53]*/
{
PyObject *list = PyList_New(Py_SIZE(self));
Py_ssize_t i;
@@ -1429,11 +1566,6 @@ error:
return NULL;
}
-PyDoc_STRVAR(tolist_doc,
-"tolist() -> list\n\
-\n\
-Convert array to an ordinary list with the same items.");
-
static PyObject *
frombytes(arrayobject *self, Py_buffer *buffer)
{
@@ -1471,47 +1603,52 @@ frombytes(arrayobject *self, Py_buffer *buffer)
return Py_None;
}
+/*[clinic input]
+array.array.fromstring
+
+ buffer: Py_buffer(types='str bytes bytearray buffer')
+ /
+
+Appends items from the string, interpreting it as an array of machine values, as if it had been read from a file using the fromfile() method).
+
+This method is deprecated. Use frombytes instead.
+[clinic start generated code]*/
+
static PyObject *
-array_fromstring(arrayobject *self, PyObject *args)
+array_array_fromstring_impl(arrayobject *self, Py_buffer *buffer)
+/*[clinic end generated code: output=31c4baa779df84ce input=1302d94c97696b84]*/
{
- Py_buffer buffer;
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"fromstring() is deprecated. Use frombytes() instead.", 2) != 0)
return NULL;
- if (!PyArg_ParseTuple(args, "s*:fromstring", &buffer))
- return NULL;
- else
- return frombytes(self, &buffer);
+ return frombytes(self, buffer);
}
-PyDoc_STRVAR(fromstring_doc,
-"fromstring(string)\n\
-\n\
-Appends items from the string, interpreting it as an array of machine\n\
-values, as if it had been read from a file using the fromfile() method).\n\
-\n\
-This method is deprecated. Use frombytes instead.");
+/*[clinic input]
+array.array.frombytes
+
+ buffer: Py_buffer
+ /
+Appends items from the string, interpreting it as an array of machine values, as if it had been read from a file using the fromfile() method).
+[clinic start generated code]*/
static PyObject *
-array_frombytes(arrayobject *self, PyObject *args)
+array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
+/*[clinic end generated code: output=d9842c8f7510a516 input=2bbf2b53ebfcc988]*/
{
- Py_buffer buffer;
- if (!PyArg_ParseTuple(args, "y*:frombytes", &buffer))
- return NULL;
- else
- return frombytes(self, &buffer);
+ return frombytes(self, buffer);
}
-PyDoc_STRVAR(frombytes_doc,
-"frombytes(bytestring)\n\
-\n\
-Appends items from the string, interpreting it as an array of machine\n\
-values, as if it had been read from a file using the fromfile() method).");
+/*[clinic input]
+array.array.tobytes
+Convert the array to an array of machine values and return the bytes representation.
+[clinic start generated code]*/
static PyObject *
-array_tobytes(arrayobject *self, PyObject *unused)
+array_array_tobytes_impl(arrayobject *self)
+/*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/
{
if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
return PyBytes_FromStringAndSize(self->ob_item,
@@ -1521,40 +1658,43 @@ array_tobytes(arrayobject *self, PyObject *unused)
}
}
-PyDoc_STRVAR(tobytes_doc,
-"tobytes() -> bytes\n\
-\n\
-Convert the array to an array of machine values and return the bytes\n\
-representation.");
+/*[clinic input]
+array.array.tostring
+
+Convert the array to an array of machine values and return the bytes representation.
+This method is deprecated. Use tobytes instead.
+[clinic start generated code]*/
static PyObject *
-array_tostring(arrayobject *self, PyObject *unused)
+array_array_tostring_impl(arrayobject *self)
+/*[clinic end generated code: output=7d6bd92745a2c8f3 input=b6c0ddee7b30457e]*/
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"tostring() is deprecated. Use tobytes() instead.", 2) != 0)
return NULL;
- return array_tobytes(self, unused);
+ return array_array_tobytes_impl(self);
}
-PyDoc_STRVAR(tostring_doc,
-"tostring() -> bytes\n\
-\n\
-Convert the array to an array of machine values and return the bytes\n\
-representation.\n\
-\n\
-This method is deprecated. Use tobytes instead.");
+/*[clinic input]
+array.array.fromunicode
+
+ ustr: Py_UNICODE(length=True)
+ /
+
+Extends this array with data from the unicode string ustr.
+The array must be a unicode type array; otherwise a ValueError is raised.
+Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
+some other type.
+[clinic start generated code]*/
static PyObject *
-array_fromunicode(arrayobject *self, PyObject *args)
+array_array_fromunicode_impl(arrayobject *self, Py_UNICODE *ustr, Py_ssize_clean_t ustr_length)
+/*[clinic end generated code: output=3b3f4f133bac725e input=56bcedb5ef70139f]*/
{
- Py_UNICODE *ustr;
- Py_ssize_t n;
char typecode;
- if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
- return NULL;
typecode = self->ob_descr->typecode;
if (typecode != 'u') {
PyErr_SetString(PyExc_ValueError,
@@ -1562,29 +1702,30 @@ array_fromunicode(arrayobject *self, PyObject *args)
"unicode type arrays");
return NULL;
}
- if (n > 0) {
+ if (ustr_length > 0) {
Py_ssize_t old_size = Py_SIZE(self);
- if (array_resize(self, old_size + n) == -1)
+ if (array_resize(self, old_size + ustr_length) == -1)
return NULL;
memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
- ustr, n * sizeof(Py_UNICODE));
+ ustr, ustr_length * sizeof(Py_UNICODE));
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
-PyDoc_STRVAR(fromunicode_doc,
-"fromunicode(ustr)\n\
-\n\
-Extends this array with data from the unicode string ustr.\n\
-The array must be a unicode type array; otherwise a ValueError\n\
-is raised. Use array.frombytes(ustr.encode(...)) to\n\
-append Unicode data to an array of some other type.");
+/*[clinic input]
+array.array.tounicode
+
+Extends this array with data from the unicode string ustr.
+Convert the array to a unicode string. The array must be a unicode type array;
+otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a
+unicode string from an array of some other type.
+[clinic start generated code]*/
static PyObject *
-array_tounicode(arrayobject *self, PyObject *unused)
+array_array_tounicode_impl(arrayobject *self)
+/*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/
{
char typecode;
typecode = self->ob_descr->typecode;
@@ -1596,70 +1737,24 @@ array_tounicode(arrayobject *self, PyObject *unused)
return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
}
-PyDoc_STRVAR(tounicode_doc,
-"tounicode() -> unicode\n\
-\n\
-Convert the array to a unicode string. The array must be\n\
-a unicode type array; otherwise a ValueError is raised. Use\n\
-array.tobytes().decode() to obtain a unicode string from\n\
-an array of some other type.");
+/*[clinic input]
+array.array.__sizeof__
+Size of the array in memory, in bytes.
+[clinic start generated code]*/
static PyObject *
-array_sizeof(arrayobject *self, PyObject *unused)
+array_array___sizeof___impl(arrayobject *self)
+/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
{
Py_ssize_t res;
res = sizeof(arrayobject) + self->allocated * self->ob_descr->itemsize;
return PyLong_FromSsize_t(res);
}
-PyDoc_STRVAR(sizeof_doc,
-"__sizeof__() -> int\n\
-\n\
-Size of the array in memory, in bytes.");
-
/*********************** Pickling support ************************/
-enum machine_format_code {
- UNKNOWN_FORMAT = -1,
- /* UNKNOWN_FORMAT is used to indicate that the machine format for an
- * array type code cannot be interpreted. When this occurs, a list of
- * Python objects is used to represent the content of the array
- * instead of using the memory content of the array directly. In that
- * case, the array_reconstructor mechanism is bypassed completely, and
- * the standard array constructor is used instead.
- *
- * This is will most likely occur when the machine doesn't use IEEE
- * floating-point numbers.
- */
-
- UNSIGNED_INT8 = 0,
- SIGNED_INT8 = 1,
- UNSIGNED_INT16_LE = 2,
- UNSIGNED_INT16_BE = 3,
- SIGNED_INT16_LE = 4,
- SIGNED_INT16_BE = 5,
- UNSIGNED_INT32_LE = 6,
- UNSIGNED_INT32_BE = 7,
- SIGNED_INT32_LE = 8,
- SIGNED_INT32_BE = 9,
- UNSIGNED_INT64_LE = 10,
- UNSIGNED_INT64_BE = 11,
- SIGNED_INT64_LE = 12,
- SIGNED_INT64_BE = 13,
- IEEE_754_FLOAT_LE = 14,
- IEEE_754_FLOAT_BE = 15,
- IEEE_754_DOUBLE_LE = 16,
- IEEE_754_DOUBLE_BE = 17,
- UTF16_LE = 18,
- UTF16_BE = 19,
- UTF32_LE = 20,
- UTF32_BE = 21
-};
-#define MACHINE_FORMAT_CODE_MIN 0
-#define MACHINE_FORMAT_CODE_MAX 21
-
static const struct mformatdescr {
size_t size;
int is_signed;
@@ -1835,21 +1930,26 @@ make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
* This functions is a special constructor used when unpickling an array. It
* provides a portable way to rebuild an array from its memory representation.
*/
+/*[clinic input]
+array._array_reconstructor
+
+ arraytype: object(type="PyTypeObject *")
+ typecode: int(types='str')
+ mformat_code: int(type="enum machine_format_code")
+ items: object
+ /
+
+Internal. Used for pickling support.
+[clinic start generated code]*/
+
static PyObject *
-array_reconstructor(PyObject *self, PyObject *args)
+array__array_reconstructor_impl(PyModuleDef *module, PyTypeObject *arraytype, int typecode, enum machine_format_code mformat_code, PyObject *items)
+/*[clinic end generated code: output=c51081ec91caf7e9 input=f72492708c0a1d50]*/
{
- PyTypeObject *arraytype;
- PyObject *items;
PyObject *converted_items;
PyObject *result;
- int typecode;
- enum machine_format_code mformat_code;
struct arraydescr *descr;
- if (!PyArg_ParseTuple(args, "OCiO:array._array_reconstructor",
- &arraytype, &typecode, &mformat_code, &items))
- return NULL;
-
if (!PyType_Check(arraytype)) {
PyErr_Format(PyExc_TypeError,
"first argument must a type object, not %.200s",
@@ -2000,7 +2100,7 @@ array_reconstructor(PyObject *self, PyObject *args)
*/
for (descr = descriptors; descr->typecode != '\0'; descr++) {
if (descr->is_integer_type &&
- descr->itemsize == mf_descr.size &&
+ (size_t)descr->itemsize == mf_descr.size &&
descr->is_signed == mf_descr.is_signed)
typecode = descr->typecode;
}
@@ -2038,13 +2138,23 @@ array_reconstructor(PyObject *self, PyObject *args)
return result;
}
+/*[clinic input]
+array.array.__reduce_ex__
+
+ value: object
+ /
+
+Return state information for pickling.
+[clinic start generated code]*/
+
static PyObject *
-array_reduce_ex(arrayobject *array, PyObject *value)
+array_array___reduce_ex__(arrayobject *self, PyObject *value)
+/*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
{
PyObject *dict;
PyObject *result;
PyObject *array_str;
- int typecode = array->ob_descr->typecode;
+ int typecode = self->ob_descr->typecode;
int mformat_code;
static PyObject *array_reconstructor = NULL;
long protocol;
@@ -2072,7 +2182,7 @@ array_reduce_ex(arrayobject *array, PyObject *value)
if (protocol == -1 && PyErr_Occurred())
return NULL;
- dict = _PyObject_GetAttrId((PyObject *)array, &PyId___dict__);
+ dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
if (dict == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
return NULL;
@@ -2095,32 +2205,30 @@ array_reduce_ex(arrayobject *array, PyObject *value)
* coercing unicode objects to bytes in array_reconstructor.
*/
PyObject *list;
- list = array_tolist(array, NULL);
+ list = array_array_tolist_impl(self);
if (list == NULL) {
Py_DECREF(dict);
return NULL;
}
result = Py_BuildValue(
- "O(CO)O", Py_TYPE(array), typecode, list, dict);
+ "O(CO)O", Py_TYPE(self), typecode, list, dict);
Py_DECREF(list);
Py_DECREF(dict);
return result;
}
- array_str = array_tobytes(array, NULL);
+ array_str = array_array_tobytes_impl(self);
if (array_str == NULL) {
Py_DECREF(dict);
return NULL;
}
result = Py_BuildValue(
- "O(OCiN)O", array_reconstructor, Py_TYPE(array), typecode,
+ "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
mformat_code, array_str, dict);
Py_DECREF(dict);
return result;
}
-PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
-
static PyObject *
array_get_typecode(arrayobject *a, void *closure)
{
@@ -2143,55 +2251,31 @@ static PyGetSetDef array_getsets [] = {
};
static PyMethodDef array_methods[] = {
- {"append", (PyCFunction)array_append, METH_O,
- append_doc},
- {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
- buffer_info_doc},
- {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
- byteswap_doc},
- {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
- copy_doc},
- {"count", (PyCFunction)array_count, METH_O,
- count_doc},
- {"__deepcopy__", (PyCFunction)array_copy, METH_O,
- copy_doc},
- {"extend", (PyCFunction)array_extend, METH_O,
- extend_doc},
- {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
- fromfile_doc},
- {"fromlist", (PyCFunction)array_fromlist, METH_O,
- fromlist_doc},
- {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
- fromstring_doc},
- {"frombytes", (PyCFunction)array_frombytes, METH_VARARGS,
- frombytes_doc},
- {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
- fromunicode_doc},
- {"index", (PyCFunction)array_index, METH_O,
- index_doc},
- {"insert", (PyCFunction)array_insert, METH_VARARGS,
- insert_doc},
- {"pop", (PyCFunction)array_pop, METH_VARARGS,
- pop_doc},
- {"__reduce_ex__", (PyCFunction)array_reduce_ex, METH_O,
- reduce_doc},
- {"remove", (PyCFunction)array_remove, METH_O,
- remove_doc},
- {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
- reverse_doc},
- {"tofile", (PyCFunction)array_tofile, METH_O,
- tofile_doc},
- {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
- tolist_doc},
- {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
- tostring_doc},
- {"tobytes", (PyCFunction)array_tobytes, METH_NOARGS,
- tobytes_doc},
- {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
- tounicode_doc},
- {"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS,
- sizeof_doc},
- {NULL, NULL} /* sentinel */
+ ARRAY_ARRAY_APPEND_METHODDEF
+ ARRAY_ARRAY_BUFFER_INFO_METHODDEF
+ ARRAY_ARRAY_BYTESWAP_METHODDEF
+ ARRAY_ARRAY___COPY___METHODDEF
+ ARRAY_ARRAY_COUNT_METHODDEF
+ ARRAY_ARRAY___DEEPCOPY___METHODDEF
+ ARRAY_ARRAY_EXTEND_METHODDEF
+ ARRAY_ARRAY_FROMFILE_METHODDEF
+ ARRAY_ARRAY_FROMLIST_METHODDEF
+ ARRAY_ARRAY_FROMSTRING_METHODDEF
+ ARRAY_ARRAY_FROMBYTES_METHODDEF
+ ARRAY_ARRAY_FROMUNICODE_METHODDEF
+ ARRAY_ARRAY_INDEX_METHODDEF
+ ARRAY_ARRAY_INSERT_METHODDEF
+ ARRAY_ARRAY_POP_METHODDEF
+ ARRAY_ARRAY___REDUCE_EX___METHODDEF
+ ARRAY_ARRAY_REMOVE_METHODDEF
+ ARRAY_ARRAY_REVERSE_METHODDEF
+ ARRAY_ARRAY_TOFILE_METHODDEF
+ ARRAY_ARRAY_TOLIST_METHODDEF
+ ARRAY_ARRAY_TOSTRING_METHODDEF
+ ARRAY_ARRAY_TOBYTES_METHODDEF
+ ARRAY_ARRAY_TOUNICODE_METHODDEF
+ ARRAY_ARRAY___SIZEOF___METHODDEF
+ {NULL, NULL} /* sentinel */
};
static PyObject *
@@ -2207,9 +2291,9 @@ array_repr(arrayobject *a)
return PyUnicode_FromFormat("array('%c')", (int)typecode);
}
if (typecode == 'u') {
- v = array_tounicode(a, NULL);
+ v = array_array_tounicode_impl(a);
} else {
- v = array_tolist(a, NULL);
+ v = array_array_tolist_impl(a);
}
if (v == NULL)
return NULL;
@@ -2446,7 +2530,11 @@ static const void *emptybuf = "";
static int
array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
{
- if (view==NULL) goto finish;
+ if (view == NULL) {
+ PyErr_SetString(PyExc_BufferError,
+ "array_buffer_getbuf: view==NULL argument is obsolete");
+ return -1;
+ }
view->buf = (void *)self->ob_item;
view->obj = (PyObject*)self;
@@ -2476,7 +2564,6 @@ array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
#endif
}
- finish:
self->ob_exports++;
return 0;
}
@@ -2592,8 +2679,8 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_DECREF(a);
return NULL;
}
- v = array_frombytes((arrayobject *)a,
- t_initial);
+ v = array_array_frombytes((arrayobject *)a,
+ t_initial);
Py_DECREF(t_initial);
if (v == NULL) {
Py_DECREF(a);
@@ -2766,16 +2853,10 @@ static PyTypeObject Arraytype = {
/*********************** Array Iterator **************************/
-typedef struct {
- PyObject_HEAD
- Py_ssize_t index;
- arrayobject *ao;
- PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
-} arrayiterobject;
-
-static PyTypeObject PyArrayIter_Type;
-
-#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
+/*[clinic input]
+class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/
static PyObject *
array_iter(arrayobject *ao)
@@ -2823,33 +2904,47 @@ arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
return 0;
}
+/*[clinic input]
+array.arrayiterator.__reduce__
+
+Return state information for pickling.
+[clinic start generated code]*/
+
static PyObject *
-arrayiter_reduce(arrayiterobject *it)
+array_arrayiterator___reduce___impl(arrayiterobject *self)
+/*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
{
return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
- it->ao, it->index);
+ self->ao, self->index);
}
+/*[clinic input]
+array.arrayiterator.__setstate__
+
+ state: object
+ /
+
+Set state information for unpickling.
+[clinic start generated code]*/
+
static PyObject *
-arrayiter_setstate(arrayiterobject *it, PyObject *state)
+array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
+/*[clinic end generated code: output=397da9904e443cbe input=f47d5ceda19e787b]*/
{
Py_ssize_t index = PyLong_AsSsize_t(state);
if (index == -1 && PyErr_Occurred())
return NULL;
if (index < 0)
index = 0;
- else if (index > Py_SIZE(it->ao))
- index = Py_SIZE(it->ao); /* iterator exhausted */
- it->index = index;
+ else if (index > Py_SIZE(self->ao))
+ index = Py_SIZE(self->ao); /* iterator exhausted */
+ self->index = index;
Py_RETURN_NONE;
}
-PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
static PyMethodDef arrayiter_methods[] = {
- {"__reduce__", (PyCFunction)arrayiter_reduce, METH_NOARGS,
- reduce_doc},
- {"__setstate__", (PyCFunction)arrayiter_setstate, METH_O,
- setstate_doc},
+ ARRAY_ARRAYITERATOR___REDUCE___METHODDEF
+ ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF
{NULL, NULL} /* sentinel */
};
@@ -2890,8 +2985,7 @@ static PyTypeObject PyArrayIter_Type = {
/* No functions in array module. */
static PyMethodDef a_methods[] = {
- {"_array_reconstructor", array_reconstructor, METH_VARARGS,
- PyDoc_STR("Internal. Used for pickling support.")},
+ ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
{NULL, NULL, 0, NULL} /* Sentinel */
};
diff --git a/Modules/atexitmodule.c b/Modules/atexitmodule.c
index 9887014..79e9962 100644
--- a/Modules/atexitmodule.c
+++ b/Modules/atexitmodule.c
@@ -60,7 +60,7 @@ atexit_cleanup(atexitmodule_state *modstate)
modstate->ncallbacks = 0;
}
-/* Installed into pythonrun.c's atexit mechanism */
+/* Installed into pylifecycle.c's atexit mechanism */
static void
atexit_callfuncs(void)
diff --git a/Modules/cjkcodecs/cjkcodecs.h b/Modules/cjkcodecs/cjkcodecs.h
index d15ccfb..a45ed12 100644
--- a/Modules/cjkcodecs/cjkcodecs.h
+++ b/Modules/cjkcodecs/cjkcodecs.h
@@ -362,7 +362,7 @@ importmap(const char *modname, const char *symbol,
if (mod == NULL)
return -1;
- o = PyObject_GetAttrString(mod, (char*)symbol);
+ o = PyObject_GetAttrString(mod, symbol);
if (o == NULL)
goto errorexit;
else if (!PyCapsule_IsValid(o, PyMultibyteCodec_CAPSULE_NAME)) {
diff --git a/Modules/cjkcodecs/clinic/multibytecodec.c.h b/Modules/cjkcodecs/clinic/multibytecodec.c.h
new file mode 100644
index 0000000..2f9cb63
--- /dev/null
+++ b/Modules/cjkcodecs/clinic/multibytecodec.c.h
@@ -0,0 +1,301 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+PyDoc_STRVAR(_multibytecodec_MultibyteCodec_encode__doc__,
+"encode($self, /, input, errors=None)\n"
+"--\n"
+"\n"
+"Return an encoded string version of `input\'.\n"
+"\n"
+"\'errors\' may be given to set a different error handling scheme. Default is\n"
+"\'strict\' meaning that encoding errors raise a UnicodeEncodeError. Other possible\n"
+"values are \'ignore\', \'replace\' and \'xmlcharrefreplace\' as well as any other name\n"
+"registered with codecs.register_error that can handle UnicodeEncodeErrors.");
+
+#define _MULTIBYTECODEC_MULTIBYTECODEC_ENCODE_METHODDEF \
+ {"encode", (PyCFunction)_multibytecodec_MultibyteCodec_encode, METH_VARARGS|METH_KEYWORDS, _multibytecodec_MultibyteCodec_encode__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteCodec_encode_impl(MultibyteCodecObject *self, PyObject *input, const char *errors);
+
+static PyObject *
+_multibytecodec_MultibyteCodec_encode(MultibyteCodecObject *self, PyObject *args, PyObject *kwargs)
+{
+ PyObject *return_value = NULL;
+ static char *_keywords[] = {"input", "errors", NULL};
+ PyObject *input;
+ const char *errors = NULL;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "O|z:encode", _keywords,
+ &input, &errors))
+ goto exit;
+ return_value = _multibytecodec_MultibyteCodec_encode_impl(self, input, errors);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_multibytecodec_MultibyteCodec_decode__doc__,
+"decode($self, /, input, errors=None)\n"
+"--\n"
+"\n"
+"Decodes \'input\'.\n"
+"\n"
+"\'errors\' may be given to set a different error handling scheme. Default is\n"
+"\'strict\' meaning that encoding errors raise a UnicodeDecodeError. Other possible\n"
+"values are \'ignore\' and \'replace\' as well as any other name registered with\n"
+"codecs.register_error that is able to handle UnicodeDecodeErrors.\"");
+
+#define _MULTIBYTECODEC_MULTIBYTECODEC_DECODE_METHODDEF \
+ {"decode", (PyCFunction)_multibytecodec_MultibyteCodec_decode, METH_VARARGS|METH_KEYWORDS, _multibytecodec_MultibyteCodec_decode__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteCodec_decode_impl(MultibyteCodecObject *self, Py_buffer *input, const char *errors);
+
+static PyObject *
+_multibytecodec_MultibyteCodec_decode(MultibyteCodecObject *self, PyObject *args, PyObject *kwargs)
+{
+ PyObject *return_value = NULL;
+ static char *_keywords[] = {"input", "errors", NULL};
+ Py_buffer input = {NULL, NULL};
+ const char *errors = NULL;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "y*|z:decode", _keywords,
+ &input, &errors))
+ goto exit;
+ return_value = _multibytecodec_MultibyteCodec_decode_impl(self, &input, errors);
+
+exit:
+ /* Cleanup for input */
+ if (input.obj)
+ PyBuffer_Release(&input);
+
+ return return_value;
+}
+
+PyDoc_STRVAR(_multibytecodec_MultibyteIncrementalEncoder_encode__doc__,
+"encode($self, /, input, final=0)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTEINCREMENTALENCODER_ENCODE_METHODDEF \
+ {"encode", (PyCFunction)_multibytecodec_MultibyteIncrementalEncoder_encode, METH_VARARGS|METH_KEYWORDS, _multibytecodec_MultibyteIncrementalEncoder_encode__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteIncrementalEncoder_encode_impl(MultibyteIncrementalEncoderObject *self, PyObject *input, int final);
+
+static PyObject *
+_multibytecodec_MultibyteIncrementalEncoder_encode(MultibyteIncrementalEncoderObject *self, PyObject *args, PyObject *kwargs)
+{
+ PyObject *return_value = NULL;
+ static char *_keywords[] = {"input", "final", NULL};
+ PyObject *input;
+ int final = 0;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "O|i:encode", _keywords,
+ &input, &final))
+ goto exit;
+ return_value = _multibytecodec_MultibyteIncrementalEncoder_encode_impl(self, input, final);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_multibytecodec_MultibyteIncrementalEncoder_reset__doc__,
+"reset($self, /)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTEINCREMENTALENCODER_RESET_METHODDEF \
+ {"reset", (PyCFunction)_multibytecodec_MultibyteIncrementalEncoder_reset, METH_NOARGS, _multibytecodec_MultibyteIncrementalEncoder_reset__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteIncrementalEncoder_reset_impl(MultibyteIncrementalEncoderObject *self);
+
+static PyObject *
+_multibytecodec_MultibyteIncrementalEncoder_reset(MultibyteIncrementalEncoderObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _multibytecodec_MultibyteIncrementalEncoder_reset_impl(self);
+}
+
+PyDoc_STRVAR(_multibytecodec_MultibyteIncrementalDecoder_decode__doc__,
+"decode($self, /, input, final=0)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTEINCREMENTALDECODER_DECODE_METHODDEF \
+ {"decode", (PyCFunction)_multibytecodec_MultibyteIncrementalDecoder_decode, METH_VARARGS|METH_KEYWORDS, _multibytecodec_MultibyteIncrementalDecoder_decode__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteIncrementalDecoder_decode_impl(MultibyteIncrementalDecoderObject *self, Py_buffer *input, int final);
+
+static PyObject *
+_multibytecodec_MultibyteIncrementalDecoder_decode(MultibyteIncrementalDecoderObject *self, PyObject *args, PyObject *kwargs)
+{
+ PyObject *return_value = NULL;
+ static char *_keywords[] = {"input", "final", NULL};
+ Py_buffer input = {NULL, NULL};
+ int final = 0;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "y*|i:decode", _keywords,
+ &input, &final))
+ goto exit;
+ return_value = _multibytecodec_MultibyteIncrementalDecoder_decode_impl(self, &input, final);
+
+exit:
+ /* Cleanup for input */
+ if (input.obj)
+ PyBuffer_Release(&input);
+
+ return return_value;
+}
+
+PyDoc_STRVAR(_multibytecodec_MultibyteIncrementalDecoder_reset__doc__,
+"reset($self, /)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTEINCREMENTALDECODER_RESET_METHODDEF \
+ {"reset", (PyCFunction)_multibytecodec_MultibyteIncrementalDecoder_reset, METH_NOARGS, _multibytecodec_MultibyteIncrementalDecoder_reset__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteIncrementalDecoder_reset_impl(MultibyteIncrementalDecoderObject *self);
+
+static PyObject *
+_multibytecodec_MultibyteIncrementalDecoder_reset(MultibyteIncrementalDecoderObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _multibytecodec_MultibyteIncrementalDecoder_reset_impl(self);
+}
+
+PyDoc_STRVAR(_multibytecodec_MultibyteStreamReader_read__doc__,
+"read($self, sizeobj=None, /)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READ_METHODDEF \
+ {"read", (PyCFunction)_multibytecodec_MultibyteStreamReader_read, METH_VARARGS, _multibytecodec_MultibyteStreamReader_read__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteStreamReader_read_impl(MultibyteStreamReaderObject *self, PyObject *sizeobj);
+
+static PyObject *
+_multibytecodec_MultibyteStreamReader_read(MultibyteStreamReaderObject *self, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ PyObject *sizeobj = Py_None;
+
+ if (!PyArg_UnpackTuple(args, "read",
+ 0, 1,
+ &sizeobj))
+ goto exit;
+ return_value = _multibytecodec_MultibyteStreamReader_read_impl(self, sizeobj);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_multibytecodec_MultibyteStreamReader_readline__doc__,
+"readline($self, sizeobj=None, /)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READLINE_METHODDEF \
+ {"readline", (PyCFunction)_multibytecodec_MultibyteStreamReader_readline, METH_VARARGS, _multibytecodec_MultibyteStreamReader_readline__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteStreamReader_readline_impl(MultibyteStreamReaderObject *self, PyObject *sizeobj);
+
+static PyObject *
+_multibytecodec_MultibyteStreamReader_readline(MultibyteStreamReaderObject *self, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ PyObject *sizeobj = Py_None;
+
+ if (!PyArg_UnpackTuple(args, "readline",
+ 0, 1,
+ &sizeobj))
+ goto exit;
+ return_value = _multibytecodec_MultibyteStreamReader_readline_impl(self, sizeobj);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_multibytecodec_MultibyteStreamReader_readlines__doc__,
+"readlines($self, sizehintobj=None, /)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READLINES_METHODDEF \
+ {"readlines", (PyCFunction)_multibytecodec_MultibyteStreamReader_readlines, METH_VARARGS, _multibytecodec_MultibyteStreamReader_readlines__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteStreamReader_readlines_impl(MultibyteStreamReaderObject *self, PyObject *sizehintobj);
+
+static PyObject *
+_multibytecodec_MultibyteStreamReader_readlines(MultibyteStreamReaderObject *self, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ PyObject *sizehintobj = Py_None;
+
+ if (!PyArg_UnpackTuple(args, "readlines",
+ 0, 1,
+ &sizehintobj))
+ goto exit;
+ return_value = _multibytecodec_MultibyteStreamReader_readlines_impl(self, sizehintobj);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_multibytecodec_MultibyteStreamReader_reset__doc__,
+"reset($self, /)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTESTREAMREADER_RESET_METHODDEF \
+ {"reset", (PyCFunction)_multibytecodec_MultibyteStreamReader_reset, METH_NOARGS, _multibytecodec_MultibyteStreamReader_reset__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteStreamReader_reset_impl(MultibyteStreamReaderObject *self);
+
+static PyObject *
+_multibytecodec_MultibyteStreamReader_reset(MultibyteStreamReaderObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _multibytecodec_MultibyteStreamReader_reset_impl(self);
+}
+
+PyDoc_STRVAR(_multibytecodec_MultibyteStreamWriter_write__doc__,
+"write($self, strobj, /)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_WRITE_METHODDEF \
+ {"write", (PyCFunction)_multibytecodec_MultibyteStreamWriter_write, METH_O, _multibytecodec_MultibyteStreamWriter_write__doc__},
+
+PyDoc_STRVAR(_multibytecodec_MultibyteStreamWriter_writelines__doc__,
+"writelines($self, lines, /)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_WRITELINES_METHODDEF \
+ {"writelines", (PyCFunction)_multibytecodec_MultibyteStreamWriter_writelines, METH_O, _multibytecodec_MultibyteStreamWriter_writelines__doc__},
+
+PyDoc_STRVAR(_multibytecodec_MultibyteStreamWriter_reset__doc__,
+"reset($self, /)\n"
+"--");
+
+#define _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_RESET_METHODDEF \
+ {"reset", (PyCFunction)_multibytecodec_MultibyteStreamWriter_reset, METH_NOARGS, _multibytecodec_MultibyteStreamWriter_reset__doc__},
+
+static PyObject *
+_multibytecodec_MultibyteStreamWriter_reset_impl(MultibyteStreamWriterObject *self);
+
+static PyObject *
+_multibytecodec_MultibyteStreamWriter_reset(MultibyteStreamWriterObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return _multibytecodec_MultibyteStreamWriter_reset_impl(self);
+}
+
+PyDoc_STRVAR(_multibytecodec___create_codec__doc__,
+"__create_codec($module, arg, /)\n"
+"--");
+
+#define _MULTIBYTECODEC___CREATE_CODEC_METHODDEF \
+ {"__create_codec", (PyCFunction)_multibytecodec___create_codec, METH_O, _multibytecodec___create_codec__doc__},
+/*[clinic end generated code: output=dff1459dec464796 input=a9049054013a1b77]*/
diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c
index 087ae9b..31bb35f 100644
--- a/Modules/cjkcodecs/multibytecodec.c
+++ b/Modules/cjkcodecs/multibytecodec.c
@@ -8,6 +8,18 @@
#include "Python.h"
#include "structmember.h"
#include "multibytecodec.h"
+#include "clinic/multibytecodec.c.h"
+
+/*[clinic input]
+output preset file
+module _multibytecodec
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e0cf1b7f3c472d17]*/
+
+/*[clinic input]
+class _multibytecodec.MultibyteCodec "MultibyteCodecObject *" "&MultibyteCodec_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d5b1fc1fec8eb003]*/
typedef struct {
PyObject *inobj;
@@ -22,27 +34,7 @@ typedef struct {
_PyUnicodeWriter writer;
} MultibyteDecodeBuffer;
-PyDoc_STRVAR(MultibyteCodec_Encode__doc__,
-"I.encode(unicode[, errors]) -> (string, length consumed)\n\
-\n\
-Return an encoded string version of `unicode'. errors may be given to\n\
-set a different error handling scheme. Default is 'strict' meaning that\n\
-encoding errors raise a UnicodeEncodeError. Other possible values are\n\
-'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name\n\
-registered with codecs.register_error that can handle UnicodeEncodeErrors.");
-
-PyDoc_STRVAR(MultibyteCodec_Decode__doc__,
-"I.decode(string[, errors]) -> (unicodeobject, length consumed)\n\
-\n\
-Decodes `string' using I, an MultibyteCodec instance. errors may be given\n\
-to set a different error handling scheme. Default is 'strict' meaning\n\
-that encoding errors raise a UnicodeDecodeError. Other possible values\n\
-are 'ignore' and 'replace' as well as any other name registered with\n\
-codecs.register_error that is able to handle UnicodeDecodeErrors.");
-
-static char *codeckwarglist[] = {"input", "errors", NULL};
static char *incnewkwarglist[] = {"errors", NULL};
-static char *incrementalkwarglist[] = {"input", "final", NULL};
static char *streamkwarglist[] = {"stream", "errors", NULL};
static PyObject *multibytecodec_encode(MultibyteCodec *,
@@ -550,26 +542,35 @@ errorexit:
return NULL;
}
+/*[clinic input]
+_multibytecodec.MultibyteCodec.encode
+
+ input: object
+ errors: str(nullable=True) = NULL
+
+Return an encoded string version of `input'.
+
+'errors' may be given to set a different error handling scheme. Default is
+'strict' meaning that encoding errors raise a UnicodeEncodeError. Other possible
+values are 'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name
+registered with codecs.register_error that can handle UnicodeEncodeErrors.
+[clinic start generated code]*/
+
static PyObject *
-MultibyteCodec_Encode(MultibyteCodecObject *self,
- PyObject *args, PyObject *kwargs)
+_multibytecodec_MultibyteCodec_encode_impl(MultibyteCodecObject *self, PyObject *input, const char *errors)
+/*[clinic end generated code: output=a36bfa08783a0d0b input=252e7ee695867b2d]*/
{
MultibyteCodec_State state;
- PyObject *errorcb, *r, *arg, *ucvt;
- const char *errors = NULL;
+ PyObject *errorcb, *r, *ucvt;
Py_ssize_t datalen;
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|z:encode",
- codeckwarglist, &arg, &errors))
- return NULL;
-
- if (PyUnicode_Check(arg))
+ if (PyUnicode_Check(input))
ucvt = NULL;
else {
- arg = ucvt = PyObject_Str(arg);
- if (arg == NULL)
+ input = ucvt = PyObject_Str(input);
+ if (input == NULL)
return NULL;
- else if (!PyUnicode_Check(arg)) {
+ else if (!PyUnicode_Check(input)) {
PyErr_SetString(PyExc_TypeError,
"couldn't convert the object to unicode.");
Py_DECREF(ucvt);
@@ -577,11 +578,11 @@ MultibyteCodec_Encode(MultibyteCodecObject *self,
}
}
- if (PyUnicode_READY(arg) < 0) {
+ if (PyUnicode_READY(input) < 0) {
Py_XDECREF(ucvt);
return NULL;
}
- datalen = PyUnicode_GET_LENGTH(arg);
+ datalen = PyUnicode_GET_LENGTH(input);
errorcb = internal_error_callback(errors);
if (errorcb == NULL) {
@@ -593,7 +594,7 @@ MultibyteCodec_Encode(MultibyteCodecObject *self,
self->codec->encinit(&state, self->codec->config) != 0)
goto errorexit;
r = multibytecodec_encode(self->codec, &state,
- arg, NULL, errorcb,
+ input, NULL, errorcb,
MBENC_FLUSH | MBENC_RESET);
if (r == NULL)
goto errorexit;
@@ -608,31 +609,39 @@ errorexit:
return NULL;
}
+/*[clinic input]
+_multibytecodec.MultibyteCodec.decode
+
+ input: Py_buffer
+ errors: str(nullable=True) = NULL
+
+Decodes 'input'.
+
+'errors' may be given to set a different error handling scheme. Default is
+'strict' meaning that encoding errors raise a UnicodeDecodeError. Other possible
+values are 'ignore' and 'replace' as well as any other name registered with
+codecs.register_error that is able to handle UnicodeDecodeErrors."
+[clinic start generated code]*/
+
static PyObject *
-MultibyteCodec_Decode(MultibyteCodecObject *self,
- PyObject *args, PyObject *kwargs)
+_multibytecodec_MultibyteCodec_decode_impl(MultibyteCodecObject *self, Py_buffer *input, const char *errors)
+/*[clinic end generated code: output=4c8ee8b2931b014e input=37e1d9236e3ce8f3]*/
{
MultibyteCodec_State state;
MultibyteDecodeBuffer buf;
PyObject *errorcb, *res;
- Py_buffer pdata;
- const char *data, *errors = NULL;
+ const char *data;
Py_ssize_t datalen;
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|z:decode",
- codeckwarglist, &pdata, &errors))
- return NULL;
- data = pdata.buf;
- datalen = pdata.len;
+ data = input->buf;
+ datalen = input->len;
errorcb = internal_error_callback(errors);
if (errorcb == NULL) {
- PyBuffer_Release(&pdata);
return NULL;
}
if (datalen == 0) {
- PyBuffer_Release(&pdata);
ERROR_DECREF(errorcb);
return make_tuple(PyUnicode_New(0, 0), 0);
}
@@ -665,13 +674,11 @@ MultibyteCodec_Decode(MultibyteCodecObject *self,
if (res == NULL)
goto errorexit;
- PyBuffer_Release(&pdata);
Py_XDECREF(buf.excobj);
ERROR_DECREF(errorcb);
return make_tuple(res, datalen);
errorexit:
- PyBuffer_Release(&pdata);
ERROR_DECREF(errorcb);
Py_XDECREF(buf.excobj);
_PyUnicodeWriter_Dealloc(&buf.writer);
@@ -680,13 +687,9 @@ errorexit:
}
static struct PyMethodDef multibytecodec_methods[] = {
- {"encode", (PyCFunction)MultibyteCodec_Encode,
- METH_VARARGS | METH_KEYWORDS,
- MultibyteCodec_Encode__doc__},
- {"decode", (PyCFunction)MultibyteCodec_Decode,
- METH_VARARGS | METH_KEYWORDS,
- MultibyteCodec_Decode__doc__},
- {NULL, NULL},
+ _MULTIBYTECODEC_MULTIBYTECODEC_ENCODE_METHODDEF
+ _MULTIBYTECODEC_MULTIBYTECODEC_DECODE_METHODDEF
+ {NULL, NULL},
};
static void
@@ -870,26 +873,32 @@ decoder_feed_buffer(MultibyteStatefulDecoderContext *ctx,
}
-/**
- * MultibyteIncrementalEncoder object
- */
+/*[clinic input]
+ class _multibytecodec.MultibyteIncrementalEncoder "MultibyteIncrementalEncoderObject *" "&MultibyteIncrementalEncoder_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3be82909cd08924d]*/
-static PyObject *
-mbiencoder_encode(MultibyteIncrementalEncoderObject *self,
- PyObject *args, PyObject *kwargs)
-{
- PyObject *data;
- int final = 0;
+/*[clinic input]
+_multibytecodec.MultibyteIncrementalEncoder.encode
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:encode",
- incrementalkwarglist, &data, &final))
- return NULL;
+ input: object
+ final: int = 0
+[clinic start generated code]*/
- return encoder_encode_stateful(STATEFUL_ECTX(self), data, final);
+static PyObject *
+_multibytecodec_MultibyteIncrementalEncoder_encode_impl(MultibyteIncrementalEncoderObject *self, PyObject *input, int final)
+/*[clinic end generated code: output=3cd8780c8a719bbf input=456b76d73e464661]*/
+{
+ return encoder_encode_stateful(STATEFUL_ECTX(self), input, final);
}
+/*[clinic input]
+_multibytecodec.MultibyteIncrementalEncoder.reset
+[clinic start generated code]*/
+
static PyObject *
-mbiencoder_reset(MultibyteIncrementalEncoderObject *self)
+_multibytecodec_MultibyteIncrementalEncoder_reset_impl(MultibyteIncrementalEncoderObject *self)
+/*[clinic end generated code: output=b4125d8f537a253f input=930f06760707b6ea]*/
{
/* Longest output: 4 bytes (b'\x0F\x1F(B') with ISO 2022 */
unsigned char buffer[4], *outbuf;
@@ -906,11 +915,9 @@ mbiencoder_reset(MultibyteIncrementalEncoderObject *self)
}
static struct PyMethodDef mbiencoder_methods[] = {
- {"encode", (PyCFunction)mbiencoder_encode,
- METH_VARARGS | METH_KEYWORDS, NULL},
- {"reset", (PyCFunction)mbiencoder_reset,
- METH_NOARGS, NULL},
- {NULL, NULL},
+ _MULTIBYTECODEC_MULTIBYTEINCREMENTALENCODER_ENCODE_METHODDEF
+ _MULTIBYTECODEC_MULTIBYTEINCREMENTALENCODER_RESET_METHODDEF
+ {NULL, NULL},
};
static PyObject *
@@ -1021,26 +1028,29 @@ static PyTypeObject MultibyteIncrementalEncoder_Type = {
};
-/**
- * MultibyteIncrementalDecoder object
- */
+/*[clinic input]
+ class _multibytecodec.MultibyteIncrementalDecoder "MultibyteIncrementalDecoderObject *" "&MultibyteIncrementalDecoder_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f6003faaf2cea692]*/
+
+/*[clinic input]
+_multibytecodec.MultibyteIncrementalDecoder.decode
+
+ input: Py_buffer
+ final: int = 0
+[clinic start generated code]*/
static PyObject *
-mbidecoder_decode(MultibyteIncrementalDecoderObject *self,
- PyObject *args, PyObject *kwargs)
+_multibytecodec_MultibyteIncrementalDecoder_decode_impl(MultibyteIncrementalDecoderObject *self, Py_buffer *input, int final)
+/*[clinic end generated code: output=a0f3f92aa7303cf7 input=eb18c2f6e83589e1]*/
{
MultibyteDecodeBuffer buf;
char *data, *wdata = NULL;
- Py_buffer pdata;
Py_ssize_t wsize, size, origpending;
- int final = 0;
PyObject *res;
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i:decode",
- incrementalkwarglist, &pdata, &final))
- return NULL;
- data = pdata.buf;
- size = pdata.len;
+ data = input->buf;
+ size = input->len;
_PyUnicodeWriter_Init(&buf.writer);
buf.excobj = NULL;
@@ -1091,14 +1101,12 @@ mbidecoder_decode(MultibyteIncrementalDecoderObject *self,
if (res == NULL)
goto errorexit;
- PyBuffer_Release(&pdata);
if (wdata != data)
PyMem_Del(wdata);
Py_XDECREF(buf.excobj);
return res;
errorexit:
- PyBuffer_Release(&pdata);
if (wdata != NULL && wdata != data)
PyMem_Del(wdata);
Py_XDECREF(buf.excobj);
@@ -1106,8 +1114,13 @@ errorexit:
return NULL;
}
+/*[clinic input]
+_multibytecodec.MultibyteIncrementalDecoder.reset
+[clinic start generated code]*/
+
static PyObject *
-mbidecoder_reset(MultibyteIncrementalDecoderObject *self)
+_multibytecodec_MultibyteIncrementalDecoder_reset_impl(MultibyteIncrementalDecoderObject *self)
+/*[clinic end generated code: output=da423b1782c23ed1 input=3b63b3be85b2fb45]*/
{
if (self->codec->decreset != NULL &&
self->codec->decreset(&self->state, self->codec->config) != 0)
@@ -1118,11 +1131,9 @@ mbidecoder_reset(MultibyteIncrementalDecoderObject *self)
}
static struct PyMethodDef mbidecoder_methods[] = {
- {"decode", (PyCFunction)mbidecoder_decode,
- METH_VARARGS | METH_KEYWORDS, NULL},
- {"reset", (PyCFunction)mbidecoder_reset,
- METH_NOARGS, NULL},
- {NULL, NULL},
+ _MULTIBYTECODEC_MULTIBYTEINCREMENTALDECODER_DECODE_METHODDEF
+ _MULTIBYTECODEC_MULTIBYTEINCREMENTALDECODER_RESET_METHODDEF
+ {NULL, NULL},
};
static PyObject *
@@ -1233,9 +1244,10 @@ static PyTypeObject MultibyteIncrementalDecoder_Type = {
};
-/**
- * MultibyteStreamReader object
- */
+/*[clinic input]
+ class _multibytecodec.MultibyteStreamReader "MultibyteStreamReaderObject *" "MultibyteStreamReader_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d323634b74976f09]*/
static PyObject *
mbstreamreader_iread(MultibyteStreamReaderObject *self,
@@ -1257,10 +1269,10 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
if (sizehint < 0)
cres = PyObject_CallMethod(self->stream,
- (char *)method, NULL);
+ method, NULL);
else
cres = PyObject_CallMethod(self->stream,
- (char *)method, "i", sizehint);
+ method, "i", sizehint);
if (cres == NULL)
goto errorexit;
@@ -1342,16 +1354,20 @@ errorexit:
return NULL;
}
+/*[clinic input]
+ _multibytecodec.MultibyteStreamReader.read
+
+ sizeobj: object = None
+ /
+[clinic start generated code]*/
+
static PyObject *
-mbstreamreader_read(MultibyteStreamReaderObject *self, PyObject *args)
+_multibytecodec_MultibyteStreamReader_read_impl(MultibyteStreamReaderObject *self, PyObject *sizeobj)
+/*[clinic end generated code: output=f298ea6e1bd2083c input=015b0d3ff2fca485]*/
{
- PyObject *sizeobj = NULL;
Py_ssize_t size;
- if (!PyArg_UnpackTuple(args, "read", 0, 1, &sizeobj))
- return NULL;
-
- if (sizeobj == Py_None || sizeobj == NULL)
+ if (sizeobj == Py_None)
size = -1;
else if (PyLong_Check(sizeobj))
size = PyLong_AsSsize_t(sizeobj);
@@ -1366,16 +1382,20 @@ mbstreamreader_read(MultibyteStreamReaderObject *self, PyObject *args)
return mbstreamreader_iread(self, "read", size);
}
+/*[clinic input]
+ _multibytecodec.MultibyteStreamReader.readline
+
+ sizeobj: object = None
+ /
+[clinic start generated code]*/
+
static PyObject *
-mbstreamreader_readline(MultibyteStreamReaderObject *self, PyObject *args)
+_multibytecodec_MultibyteStreamReader_readline_impl(MultibyteStreamReaderObject *self, PyObject *sizeobj)
+/*[clinic end generated code: output=e5ac302a6d0999de input=41ccc64f9bb0cec3]*/
{
- PyObject *sizeobj = NULL;
Py_ssize_t size;
- if (!PyArg_UnpackTuple(args, "readline", 0, 1, &sizeobj))
- return NULL;
-
- if (sizeobj == Py_None || sizeobj == NULL)
+ if (sizeobj == Py_None)
size = -1;
else if (PyLong_Check(sizeobj))
size = PyLong_AsSsize_t(sizeobj);
@@ -1390,16 +1410,21 @@ mbstreamreader_readline(MultibyteStreamReaderObject *self, PyObject *args)
return mbstreamreader_iread(self, "readline", size);
}
+/*[clinic input]
+ _multibytecodec.MultibyteStreamReader.readlines
+
+ sizehintobj: object = None
+ /
+[clinic start generated code]*/
+
static PyObject *
-mbstreamreader_readlines(MultibyteStreamReaderObject *self, PyObject *args)
+_multibytecodec_MultibyteStreamReader_readlines_impl(MultibyteStreamReaderObject *self, PyObject *sizehintobj)
+/*[clinic end generated code: output=68f024178b77cb0f input=54932f5d4d88e880]*/
{
- PyObject *sizehintobj = NULL, *r, *sr;
+ PyObject *r, *sr;
Py_ssize_t sizehint;
- if (!PyArg_UnpackTuple(args, "readlines", 0, 1, &sizehintobj))
- return NULL;
-
- if (sizehintobj == Py_None || sizehintobj == NULL)
+ if (sizehintobj == Py_None)
sizehint = -1;
else if (PyLong_Check(sizehintobj))
sizehint = PyLong_AsSsize_t(sizehintobj);
@@ -1420,8 +1445,13 @@ mbstreamreader_readlines(MultibyteStreamReaderObject *self, PyObject *args)
return sr;
}
+/*[clinic input]
+ _multibytecodec.MultibyteStreamReader.reset
+[clinic start generated code]*/
+
static PyObject *
-mbstreamreader_reset(MultibyteStreamReaderObject *self)
+_multibytecodec_MultibyteStreamReader_reset_impl(MultibyteStreamReaderObject *self)
+/*[clinic end generated code: output=138490370a680abc input=5d4140db84b5e1e2]*/
{
if (self->codec->decreset != NULL &&
self->codec->decreset(&self->state, self->codec->config) != 0)
@@ -1432,14 +1462,10 @@ mbstreamreader_reset(MultibyteStreamReaderObject *self)
}
static struct PyMethodDef mbstreamreader_methods[] = {
- {"read", (PyCFunction)mbstreamreader_read,
- METH_VARARGS, NULL},
- {"readline", (PyCFunction)mbstreamreader_readline,
- METH_VARARGS, NULL},
- {"readlines", (PyCFunction)mbstreamreader_readlines,
- METH_VARARGS, NULL},
- {"reset", (PyCFunction)mbstreamreader_reset,
- METH_NOARGS, NULL},
+ _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READ_METHODDEF
+ _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READLINE_METHODDEF
+ _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READLINES_METHODDEF
+ _MULTIBYTECODEC_MULTIBYTESTREAMREADER_RESET_METHODDEF
{NULL, NULL},
};
@@ -1562,9 +1588,10 @@ static PyTypeObject MultibyteStreamReader_Type = {
};
-/**
- * MultibyteStreamWriter object
- */
+/*[clinic input]
+ class _multibytecodec.MultibyteStreamWriter "MultibyteStreamWriterObject *" "&MultibyteStreamWriter_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=cde22780a215d6ac]*/
static int
mbstreamwriter_iwrite(MultibyteStreamWriterObject *self,
@@ -1585,8 +1612,16 @@ mbstreamwriter_iwrite(MultibyteStreamWriterObject *self,
return 0;
}
+/*[clinic input]
+ _multibytecodec.MultibyteStreamWriter.write
+
+ strobj: object
+ /
+[clinic start generated code]*/
+
static PyObject *
-mbstreamwriter_write(MultibyteStreamWriterObject *self, PyObject *strobj)
+_multibytecodec_MultibyteStreamWriter_write(MultibyteStreamWriterObject *self, PyObject *strobj)
+/*[clinic end generated code: output=44e9eb0db0374cb1 input=551dc4c018c10a2b]*/
{
if (mbstreamwriter_iwrite(self, strobj))
return NULL;
@@ -1594,8 +1629,16 @@ mbstreamwriter_write(MultibyteStreamWriterObject *self, PyObject *strobj)
Py_RETURN_NONE;
}
+/*[clinic input]
+ _multibytecodec.MultibyteStreamWriter.writelines
+
+ lines: object
+ /
+[clinic start generated code]*/
+
static PyObject *
-mbstreamwriter_writelines(MultibyteStreamWriterObject *self, PyObject *lines)
+_multibytecodec_MultibyteStreamWriter_writelines(MultibyteStreamWriterObject *self, PyObject *lines)
+/*[clinic end generated code: output=4facbb0638dde172 input=57797fe7008d4e96]*/
{
PyObject *strobj;
int i, r;
@@ -1621,8 +1664,13 @@ mbstreamwriter_writelines(MultibyteStreamWriterObject *self, PyObject *lines)
Py_RETURN_NONE;
}
+/*[clinic input]
+ _multibytecodec.MultibyteStreamWriter.reset
+[clinic start generated code]*/
+
static PyObject *
-mbstreamwriter_reset(MultibyteStreamWriterObject *self)
+_multibytecodec_MultibyteStreamWriter_reset_impl(MultibyteStreamWriterObject *self)
+/*[clinic end generated code: output=8f54a4d9b03db5ff input=b56dbcbaf35cc10c]*/
{
PyObject *pwrt;
@@ -1721,13 +1769,10 @@ mbstreamwriter_dealloc(MultibyteStreamWriterObject *self)
}
static struct PyMethodDef mbstreamwriter_methods[] = {
- {"write", (PyCFunction)mbstreamwriter_write,
- METH_O, NULL},
- {"writelines", (PyCFunction)mbstreamwriter_writelines,
- METH_O, NULL},
- {"reset", (PyCFunction)mbstreamwriter_reset,
- METH_NOARGS, NULL},
- {NULL, NULL},
+ _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_WRITE_METHODDEF
+ _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_WRITELINES_METHODDEF
+ _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_RESET_METHODDEF
+ {NULL, NULL},
};
static PyMemberDef mbstreamwriter_members[] = {
@@ -1781,12 +1826,16 @@ static PyTypeObject MultibyteStreamWriter_Type = {
};
-/**
- * Exposed factory function
- */
+/*[clinic input]
+_multibytecodec.__create_codec
+
+ arg: object
+ /
+[clinic start generated code]*/
static PyObject *
-__create_codec(PyObject *ignore, PyObject *arg)
+_multibytecodec___create_codec(PyModuleDef *module, PyObject *arg)
+/*[clinic end generated code: output=fbe74f6510640163 input=6840b2a6b183fcfa]*/
{
MultibyteCodecObject *self;
MultibyteCodec *codec;
@@ -1809,7 +1858,7 @@ __create_codec(PyObject *ignore, PyObject *arg)
}
static struct PyMethodDef __methods[] = {
- {"__create_codec", (PyCFunction)__create_codec, METH_O},
+ _MULTIBYTECODEC___CREATE_CODEC_METHODDEF
{NULL, NULL},
};
diff --git a/Modules/clinic/_lzmamodule.c.h b/Modules/clinic/_lzmamodule.c.h
index c1ad882..a46a152 100644
--- a/Modules/clinic/_lzmamodule.c.h
+++ b/Modules/clinic/_lzmamodule.c.h
@@ -62,34 +62,43 @@ _lzma_LZMACompressor_flush(Compressor *self, PyObject *Py_UNUSED(ignored))
}
PyDoc_STRVAR(_lzma_LZMADecompressor_decompress__doc__,
-"decompress($self, data, /)\n"
+"decompress($self, /, data, max_length=-1)\n"
"--\n"
"\n"
-"Provide data to the decompressor object.\n"
+"Decompresses *data*, returning uncompressed data as bytes.\n"
"\n"
-"Returns a chunk of decompressed data if possible, or b\'\' otherwise.\n"
+"If *max_length* is nonnegative, returns at most *max_length* bytes of\n"
+"decompressed data. If this limit is reached and further output can be\n"
+"produced, *self.needs_input* will be set to ``False``. In this case, the next\n"
+"call to *decompress()* may provide *data* as b\'\' to obtain more of the output.\n"
"\n"
-"Attempting to decompress data after the end of stream is reached\n"
-"raises an EOFError. Any data found after the end of the stream\n"
-"is ignored and saved in the unused_data attribute.");
+"If all of the input data was decompressed and returned (either because this\n"
+"was less than *max_length* bytes, or because *max_length* was negative),\n"
+"*self.needs_input* will be set to True.\n"
+"\n"
+"Attempting to decompress data after the end of stream is reached raises an\n"
+"EOFError. Any data found after the end of the stream is ignored and saved in\n"
+"the unused_data attribute.");
#define _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF \
- {"decompress", (PyCFunction)_lzma_LZMADecompressor_decompress, METH_VARARGS, _lzma_LZMADecompressor_decompress__doc__},
+ {"decompress", (PyCFunction)_lzma_LZMADecompressor_decompress, METH_VARARGS|METH_KEYWORDS, _lzma_LZMADecompressor_decompress__doc__},
static PyObject *
-_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data);
+_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data, Py_ssize_t max_length);
static PyObject *
-_lzma_LZMADecompressor_decompress(Decompressor *self, PyObject *args)
+_lzma_LZMADecompressor_decompress(Decompressor *self, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
+ static char *_keywords[] = {"data", "max_length", NULL};
Py_buffer data = {NULL, NULL};
+ Py_ssize_t max_length = -1;
- if (!PyArg_ParseTuple(args,
- "y*:decompress",
- &data))
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "y*|n:decompress", _keywords,
+ &data, &max_length))
goto exit;
- return_value = _lzma_LZMADecompressor_decompress_impl(self, &data);
+ return_value = _lzma_LZMADecompressor_decompress_impl(self, &data, max_length);
exit:
/* Cleanup for data */
@@ -242,4 +251,4 @@ exit:
return return_value;
}
-/*[clinic end generated code: output=808fec8216ac712b input=a9049054013a1b77]*/
+/*[clinic end generated code: output=d17fac38b09626d8 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/arraymodule.c.h b/Modules/clinic/arraymodule.c.h
new file mode 100644
index 0000000..57d7690
--- /dev/null
+++ b/Modules/clinic/arraymodule.c.h
@@ -0,0 +1,505 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+PyDoc_STRVAR(array_array___copy____doc__,
+"__copy__($self, /)\n"
+"--\n"
+"\n"
+"Return a copy of the array.");
+
+#define ARRAY_ARRAY___COPY___METHODDEF \
+ {"__copy__", (PyCFunction)array_array___copy__, METH_NOARGS, array_array___copy____doc__},
+
+static PyObject *
+array_array___copy___impl(arrayobject *self);
+
+static PyObject *
+array_array___copy__(arrayobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return array_array___copy___impl(self);
+}
+
+PyDoc_STRVAR(array_array___deepcopy____doc__,
+"__deepcopy__($self, unused, /)\n"
+"--\n"
+"\n"
+"Return a copy of the array.");
+
+#define ARRAY_ARRAY___DEEPCOPY___METHODDEF \
+ {"__deepcopy__", (PyCFunction)array_array___deepcopy__, METH_O, array_array___deepcopy____doc__},
+
+PyDoc_STRVAR(array_array_count__doc__,
+"count($self, v, /)\n"
+"--\n"
+"\n"
+"Return number of occurrences of v in the array.");
+
+#define ARRAY_ARRAY_COUNT_METHODDEF \
+ {"count", (PyCFunction)array_array_count, METH_O, array_array_count__doc__},
+
+PyDoc_STRVAR(array_array_index__doc__,
+"index($self, v, /)\n"
+"--\n"
+"\n"
+"Return index of first occurrence of v in the array.");
+
+#define ARRAY_ARRAY_INDEX_METHODDEF \
+ {"index", (PyCFunction)array_array_index, METH_O, array_array_index__doc__},
+
+PyDoc_STRVAR(array_array_remove__doc__,
+"remove($self, v, /)\n"
+"--\n"
+"\n"
+"Remove the first occurrence of v in the array.");
+
+#define ARRAY_ARRAY_REMOVE_METHODDEF \
+ {"remove", (PyCFunction)array_array_remove, METH_O, array_array_remove__doc__},
+
+PyDoc_STRVAR(array_array_pop__doc__,
+"pop($self, i=-1, /)\n"
+"--\n"
+"\n"
+"Return the i-th element and delete it from the array.\n"
+"\n"
+"i defaults to -1.");
+
+#define ARRAY_ARRAY_POP_METHODDEF \
+ {"pop", (PyCFunction)array_array_pop, METH_VARARGS, array_array_pop__doc__},
+
+static PyObject *
+array_array_pop_impl(arrayobject *self, Py_ssize_t i);
+
+static PyObject *
+array_array_pop(arrayobject *self, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t i = -1;
+
+ if (!PyArg_ParseTuple(args,
+ "|n:pop",
+ &i))
+ goto exit;
+ return_value = array_array_pop_impl(self, i);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(array_array_extend__doc__,
+"extend($self, bb, /)\n"
+"--\n"
+"\n"
+"Append items to the end of the array.");
+
+#define ARRAY_ARRAY_EXTEND_METHODDEF \
+ {"extend", (PyCFunction)array_array_extend, METH_O, array_array_extend__doc__},
+
+PyDoc_STRVAR(array_array_insert__doc__,
+"insert($self, i, v, /)\n"
+"--\n"
+"\n"
+"Insert a new item v into the array before position i.");
+
+#define ARRAY_ARRAY_INSERT_METHODDEF \
+ {"insert", (PyCFunction)array_array_insert, METH_VARARGS, array_array_insert__doc__},
+
+static PyObject *
+array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v);
+
+static PyObject *
+array_array_insert(arrayobject *self, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t i;
+ PyObject *v;
+
+ if (!PyArg_ParseTuple(args,
+ "nO:insert",
+ &i, &v))
+ goto exit;
+ return_value = array_array_insert_impl(self, i, v);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(array_array_buffer_info__doc__,
+"buffer_info($self, /)\n"
+"--\n"
+"\n"
+"Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array\'s contents.\n"
+"\n"
+"The length should be multiplied by the itemsize attribute to calculate\n"
+"the buffer length in bytes.");
+
+#define ARRAY_ARRAY_BUFFER_INFO_METHODDEF \
+ {"buffer_info", (PyCFunction)array_array_buffer_info, METH_NOARGS, array_array_buffer_info__doc__},
+
+static PyObject *
+array_array_buffer_info_impl(arrayobject *self);
+
+static PyObject *
+array_array_buffer_info(arrayobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return array_array_buffer_info_impl(self);
+}
+
+PyDoc_STRVAR(array_array_append__doc__,
+"append($self, v, /)\n"
+"--\n"
+"\n"
+"Append new value v to the end of the array.");
+
+#define ARRAY_ARRAY_APPEND_METHODDEF \
+ {"append", (PyCFunction)array_array_append, METH_O, array_array_append__doc__},
+
+PyDoc_STRVAR(array_array_byteswap__doc__,
+"byteswap($self, /)\n"
+"--\n"
+"\n"
+"Byteswap all items of the array.\n"
+"\n"
+"If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is\n"
+"raised.");
+
+#define ARRAY_ARRAY_BYTESWAP_METHODDEF \
+ {"byteswap", (PyCFunction)array_array_byteswap, METH_NOARGS, array_array_byteswap__doc__},
+
+static PyObject *
+array_array_byteswap_impl(arrayobject *self);
+
+static PyObject *
+array_array_byteswap(arrayobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return array_array_byteswap_impl(self);
+}
+
+PyDoc_STRVAR(array_array_reverse__doc__,
+"reverse($self, /)\n"
+"--\n"
+"\n"
+"Reverse the order of the items in the array.");
+
+#define ARRAY_ARRAY_REVERSE_METHODDEF \
+ {"reverse", (PyCFunction)array_array_reverse, METH_NOARGS, array_array_reverse__doc__},
+
+static PyObject *
+array_array_reverse_impl(arrayobject *self);
+
+static PyObject *
+array_array_reverse(arrayobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return array_array_reverse_impl(self);
+}
+
+PyDoc_STRVAR(array_array_fromfile__doc__,
+"fromfile($self, f, n, /)\n"
+"--\n"
+"\n"
+"Read n objects from the file object f and append them to the end of the array.");
+
+#define ARRAY_ARRAY_FROMFILE_METHODDEF \
+ {"fromfile", (PyCFunction)array_array_fromfile, METH_VARARGS, array_array_fromfile__doc__},
+
+static PyObject *
+array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n);
+
+static PyObject *
+array_array_fromfile(arrayobject *self, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ PyObject *f;
+ Py_ssize_t n;
+
+ if (!PyArg_ParseTuple(args,
+ "On:fromfile",
+ &f, &n))
+ goto exit;
+ return_value = array_array_fromfile_impl(self, f, n);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(array_array_tofile__doc__,
+"tofile($self, f, /)\n"
+"--\n"
+"\n"
+"Write all items (as machine values) to the file object f.");
+
+#define ARRAY_ARRAY_TOFILE_METHODDEF \
+ {"tofile", (PyCFunction)array_array_tofile, METH_O, array_array_tofile__doc__},
+
+PyDoc_STRVAR(array_array_fromlist__doc__,
+"fromlist($self, list, /)\n"
+"--\n"
+"\n"
+"Append items to array from list.");
+
+#define ARRAY_ARRAY_FROMLIST_METHODDEF \
+ {"fromlist", (PyCFunction)array_array_fromlist, METH_O, array_array_fromlist__doc__},
+
+PyDoc_STRVAR(array_array_tolist__doc__,
+"tolist($self, /)\n"
+"--\n"
+"\n"
+"Convert array to an ordinary list with the same items.");
+
+#define ARRAY_ARRAY_TOLIST_METHODDEF \
+ {"tolist", (PyCFunction)array_array_tolist, METH_NOARGS, array_array_tolist__doc__},
+
+static PyObject *
+array_array_tolist_impl(arrayobject *self);
+
+static PyObject *
+array_array_tolist(arrayobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return array_array_tolist_impl(self);
+}
+
+PyDoc_STRVAR(array_array_fromstring__doc__,
+"fromstring($self, buffer, /)\n"
+"--\n"
+"\n"
+"Appends items from the string, interpreting it as an array of machine values, as if it had been read from a file using the fromfile() method).\n"
+"\n"
+"This method is deprecated. Use frombytes instead.");
+
+#define ARRAY_ARRAY_FROMSTRING_METHODDEF \
+ {"fromstring", (PyCFunction)array_array_fromstring, METH_VARARGS, array_array_fromstring__doc__},
+
+static PyObject *
+array_array_fromstring_impl(arrayobject *self, Py_buffer *buffer);
+
+static PyObject *
+array_array_fromstring(arrayobject *self, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_buffer buffer = {NULL, NULL};
+
+ if (!PyArg_ParseTuple(args,
+ "s*:fromstring",
+ &buffer))
+ goto exit;
+ return_value = array_array_fromstring_impl(self, &buffer);
+
+exit:
+ /* Cleanup for buffer */
+ if (buffer.obj)
+ PyBuffer_Release(&buffer);
+
+ return return_value;
+}
+
+PyDoc_STRVAR(array_array_frombytes__doc__,
+"frombytes($self, buffer, /)\n"
+"--\n"
+"\n"
+"Appends items from the string, interpreting it as an array of machine values, as if it had been read from a file using the fromfile() method).");
+
+#define ARRAY_ARRAY_FROMBYTES_METHODDEF \
+ {"frombytes", (PyCFunction)array_array_frombytes, METH_VARARGS, array_array_frombytes__doc__},
+
+static PyObject *
+array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer);
+
+static PyObject *
+array_array_frombytes(arrayobject *self, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_buffer buffer = {NULL, NULL};
+
+ if (!PyArg_ParseTuple(args,
+ "y*:frombytes",
+ &buffer))
+ goto exit;
+ return_value = array_array_frombytes_impl(self, &buffer);
+
+exit:
+ /* Cleanup for buffer */
+ if (buffer.obj)
+ PyBuffer_Release(&buffer);
+
+ return return_value;
+}
+
+PyDoc_STRVAR(array_array_tobytes__doc__,
+"tobytes($self, /)\n"
+"--\n"
+"\n"
+"Convert the array to an array of machine values and return the bytes representation.");
+
+#define ARRAY_ARRAY_TOBYTES_METHODDEF \
+ {"tobytes", (PyCFunction)array_array_tobytes, METH_NOARGS, array_array_tobytes__doc__},
+
+static PyObject *
+array_array_tobytes_impl(arrayobject *self);
+
+static PyObject *
+array_array_tobytes(arrayobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return array_array_tobytes_impl(self);
+}
+
+PyDoc_STRVAR(array_array_tostring__doc__,
+"tostring($self, /)\n"
+"--\n"
+"\n"
+"Convert the array to an array of machine values and return the bytes representation.\n"
+"\n"
+"This method is deprecated. Use tobytes instead.");
+
+#define ARRAY_ARRAY_TOSTRING_METHODDEF \
+ {"tostring", (PyCFunction)array_array_tostring, METH_NOARGS, array_array_tostring__doc__},
+
+static PyObject *
+array_array_tostring_impl(arrayobject *self);
+
+static PyObject *
+array_array_tostring(arrayobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return array_array_tostring_impl(self);
+}
+
+PyDoc_STRVAR(array_array_fromunicode__doc__,
+"fromunicode($self, ustr, /)\n"
+"--\n"
+"\n"
+"Extends this array with data from the unicode string ustr.\n"
+"\n"
+"The array must be a unicode type array; otherwise a ValueError is raised.\n"
+"Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of\n"
+"some other type.");
+
+#define ARRAY_ARRAY_FROMUNICODE_METHODDEF \
+ {"fromunicode", (PyCFunction)array_array_fromunicode, METH_VARARGS, array_array_fromunicode__doc__},
+
+static PyObject *
+array_array_fromunicode_impl(arrayobject *self, Py_UNICODE *ustr, Py_ssize_clean_t ustr_length);
+
+static PyObject *
+array_array_fromunicode(arrayobject *self, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_UNICODE *ustr;
+ Py_ssize_clean_t ustr_length;
+
+ if (!PyArg_ParseTuple(args,
+ "u#:fromunicode",
+ &ustr, &ustr_length))
+ goto exit;
+ return_value = array_array_fromunicode_impl(self, ustr, ustr_length);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(array_array_tounicode__doc__,
+"tounicode($self, /)\n"
+"--\n"
+"\n"
+"Extends this array with data from the unicode string ustr.\n"
+"\n"
+"Convert the array to a unicode string. The array must be a unicode type array;\n"
+"otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a\n"
+"unicode string from an array of some other type.");
+
+#define ARRAY_ARRAY_TOUNICODE_METHODDEF \
+ {"tounicode", (PyCFunction)array_array_tounicode, METH_NOARGS, array_array_tounicode__doc__},
+
+static PyObject *
+array_array_tounicode_impl(arrayobject *self);
+
+static PyObject *
+array_array_tounicode(arrayobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return array_array_tounicode_impl(self);
+}
+
+PyDoc_STRVAR(array_array___sizeof____doc__,
+"__sizeof__($self, /)\n"
+"--\n"
+"\n"
+"Size of the array in memory, in bytes.");
+
+#define ARRAY_ARRAY___SIZEOF___METHODDEF \
+ {"__sizeof__", (PyCFunction)array_array___sizeof__, METH_NOARGS, array_array___sizeof____doc__},
+
+static PyObject *
+array_array___sizeof___impl(arrayobject *self);
+
+static PyObject *
+array_array___sizeof__(arrayobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return array_array___sizeof___impl(self);
+}
+
+PyDoc_STRVAR(array__array_reconstructor__doc__,
+"_array_reconstructor($module, arraytype, typecode, mformat_code, items,\n"
+" /)\n"
+"--\n"
+"\n"
+"Internal. Used for pickling support.");
+
+#define ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF \
+ {"_array_reconstructor", (PyCFunction)array__array_reconstructor, METH_VARARGS, array__array_reconstructor__doc__},
+
+static PyObject *
+array__array_reconstructor_impl(PyModuleDef *module, PyTypeObject *arraytype, int typecode, enum machine_format_code mformat_code, PyObject *items);
+
+static PyObject *
+array__array_reconstructor(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ PyTypeObject *arraytype;
+ int typecode;
+ enum machine_format_code mformat_code;
+ PyObject *items;
+
+ if (!PyArg_ParseTuple(args,
+ "OCiO:_array_reconstructor",
+ &arraytype, &typecode, &mformat_code, &items))
+ goto exit;
+ return_value = array__array_reconstructor_impl(module, arraytype, typecode, mformat_code, items);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(array_array___reduce_ex____doc__,
+"__reduce_ex__($self, value, /)\n"
+"--\n"
+"\n"
+"Return state information for pickling.");
+
+#define ARRAY_ARRAY___REDUCE_EX___METHODDEF \
+ {"__reduce_ex__", (PyCFunction)array_array___reduce_ex__, METH_O, array_array___reduce_ex____doc__},
+
+PyDoc_STRVAR(array_arrayiterator___reduce____doc__,
+"__reduce__($self, /)\n"
+"--\n"
+"\n"
+"Return state information for pickling.");
+
+#define ARRAY_ARRAYITERATOR___REDUCE___METHODDEF \
+ {"__reduce__", (PyCFunction)array_arrayiterator___reduce__, METH_NOARGS, array_arrayiterator___reduce____doc__},
+
+static PyObject *
+array_arrayiterator___reduce___impl(arrayiterobject *self);
+
+static PyObject *
+array_arrayiterator___reduce__(arrayiterobject *self, PyObject *Py_UNUSED(ignored))
+{
+ return array_arrayiterator___reduce___impl(self);
+}
+
+PyDoc_STRVAR(array_arrayiterator___setstate____doc__,
+"__setstate__($self, state, /)\n"
+"--\n"
+"\n"
+"Set state information for unpickling.");
+
+#define ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF \
+ {"__setstate__", (PyCFunction)array_arrayiterator___setstate__, METH_O, array_arrayiterator___setstate____doc__},
+/*[clinic end generated code: output=e1deb61c6a3bc8c8 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/binascii.c.h b/Modules/clinic/binascii.c.h
index f647032..b2b6e6b 100644
--- a/Modules/clinic/binascii.c.h
+++ b/Modules/clinic/binascii.c.h
@@ -320,7 +320,7 @@ binascii_crc32(PyModuleDef *module, PyObject *args)
&data, &crc))
goto exit;
_return_value = binascii_crc32_impl(module, &data, crc);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == (unsigned int)-1) && PyErr_Occurred())
goto exit;
return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
@@ -543,4 +543,4 @@ exit:
return return_value;
}
-/*[clinic end generated code: output=e46d29f8c9adae7e input=a9049054013a1b77]*/
+/*[clinic end generated code: output=771126f8f53e84e7 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/cmathmodule.c.h b/Modules/clinic/cmathmodule.c.h
new file mode 100644
index 0000000..83de518
--- /dev/null
+++ b/Modules/clinic/cmathmodule.c.h
@@ -0,0 +1,851 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+PyDoc_STRVAR(cmath_acos__doc__,
+"acos($module, z, /)\n"
+"--\n"
+"\n"
+"Return the arc cosine of z.");
+
+#define CMATH_ACOS_METHODDEF \
+ {"acos", (PyCFunction)cmath_acos, METH_VARARGS, cmath_acos__doc__},
+
+static Py_complex
+cmath_acos_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_acos(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:acos",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_acos_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_acosh__doc__,
+"acosh($module, z, /)\n"
+"--\n"
+"\n"
+"Return the inverse hyperbolic cosine of z.");
+
+#define CMATH_ACOSH_METHODDEF \
+ {"acosh", (PyCFunction)cmath_acosh, METH_VARARGS, cmath_acosh__doc__},
+
+static Py_complex
+cmath_acosh_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_acosh(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:acosh",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_acosh_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_asin__doc__,
+"asin($module, z, /)\n"
+"--\n"
+"\n"
+"Return the arc sine of z.");
+
+#define CMATH_ASIN_METHODDEF \
+ {"asin", (PyCFunction)cmath_asin, METH_VARARGS, cmath_asin__doc__},
+
+static Py_complex
+cmath_asin_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_asin(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:asin",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_asin_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_asinh__doc__,
+"asinh($module, z, /)\n"
+"--\n"
+"\n"
+"Return the inverse hyperbolic sine of z.");
+
+#define CMATH_ASINH_METHODDEF \
+ {"asinh", (PyCFunction)cmath_asinh, METH_VARARGS, cmath_asinh__doc__},
+
+static Py_complex
+cmath_asinh_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_asinh(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:asinh",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_asinh_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_atan__doc__,
+"atan($module, z, /)\n"
+"--\n"
+"\n"
+"Return the arc tangent of z.");
+
+#define CMATH_ATAN_METHODDEF \
+ {"atan", (PyCFunction)cmath_atan, METH_VARARGS, cmath_atan__doc__},
+
+static Py_complex
+cmath_atan_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_atan(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:atan",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_atan_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_atanh__doc__,
+"atanh($module, z, /)\n"
+"--\n"
+"\n"
+"Return the inverse hyperbolic tangent of z.");
+
+#define CMATH_ATANH_METHODDEF \
+ {"atanh", (PyCFunction)cmath_atanh, METH_VARARGS, cmath_atanh__doc__},
+
+static Py_complex
+cmath_atanh_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_atanh(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:atanh",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_atanh_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_cos__doc__,
+"cos($module, z, /)\n"
+"--\n"
+"\n"
+"Return the cosine of z.");
+
+#define CMATH_COS_METHODDEF \
+ {"cos", (PyCFunction)cmath_cos, METH_VARARGS, cmath_cos__doc__},
+
+static Py_complex
+cmath_cos_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_cos(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:cos",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_cos_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_cosh__doc__,
+"cosh($module, z, /)\n"
+"--\n"
+"\n"
+"Return the hyperbolic cosine of z.");
+
+#define CMATH_COSH_METHODDEF \
+ {"cosh", (PyCFunction)cmath_cosh, METH_VARARGS, cmath_cosh__doc__},
+
+static Py_complex
+cmath_cosh_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_cosh(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:cosh",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_cosh_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_exp__doc__,
+"exp($module, z, /)\n"
+"--\n"
+"\n"
+"Return the exponential value e**z.");
+
+#define CMATH_EXP_METHODDEF \
+ {"exp", (PyCFunction)cmath_exp, METH_VARARGS, cmath_exp__doc__},
+
+static Py_complex
+cmath_exp_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_exp(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:exp",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_exp_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_log10__doc__,
+"log10($module, z, /)\n"
+"--\n"
+"\n"
+"Return the base-10 logarithm of z.");
+
+#define CMATH_LOG10_METHODDEF \
+ {"log10", (PyCFunction)cmath_log10, METH_VARARGS, cmath_log10__doc__},
+
+static Py_complex
+cmath_log10_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_log10(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:log10",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_log10_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_sin__doc__,
+"sin($module, z, /)\n"
+"--\n"
+"\n"
+"Return the sine of z.");
+
+#define CMATH_SIN_METHODDEF \
+ {"sin", (PyCFunction)cmath_sin, METH_VARARGS, cmath_sin__doc__},
+
+static Py_complex
+cmath_sin_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_sin(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:sin",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_sin_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_sinh__doc__,
+"sinh($module, z, /)\n"
+"--\n"
+"\n"
+"Return the hyperbolic sine of z.");
+
+#define CMATH_SINH_METHODDEF \
+ {"sinh", (PyCFunction)cmath_sinh, METH_VARARGS, cmath_sinh__doc__},
+
+static Py_complex
+cmath_sinh_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_sinh(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:sinh",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_sinh_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_sqrt__doc__,
+"sqrt($module, z, /)\n"
+"--\n"
+"\n"
+"Return the square root of z.");
+
+#define CMATH_SQRT_METHODDEF \
+ {"sqrt", (PyCFunction)cmath_sqrt, METH_VARARGS, cmath_sqrt__doc__},
+
+static Py_complex
+cmath_sqrt_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_sqrt(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:sqrt",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_sqrt_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_tan__doc__,
+"tan($module, z, /)\n"
+"--\n"
+"\n"
+"Return the tangent of z.");
+
+#define CMATH_TAN_METHODDEF \
+ {"tan", (PyCFunction)cmath_tan, METH_VARARGS, cmath_tan__doc__},
+
+static Py_complex
+cmath_tan_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_tan(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:tan",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_tan_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_tanh__doc__,
+"tanh($module, z, /)\n"
+"--\n"
+"\n"
+"Return the hyperbolic tangent of z.");
+
+#define CMATH_TANH_METHODDEF \
+ {"tanh", (PyCFunction)cmath_tanh, METH_VARARGS, cmath_tanh__doc__},
+
+static Py_complex
+cmath_tanh_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_tanh(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+ Py_complex _return_value;
+
+ if (!PyArg_ParseTuple(args,
+ "D:tanh",
+ &z))
+ goto exit;
+ /* modifications for z */
+ errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
+ _return_value = cmath_tanh_impl(module, z);
+ PyFPE_END_PROTECT(_return_value);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_log__doc__,
+"log($module, x, y_obj=None, /)\n"
+"--\n"
+"\n"
+"The logarithm of z to the given base.\n"
+"\n"
+"If the base not specified, returns the natural logarithm (base e) of z.");
+
+#define CMATH_LOG_METHODDEF \
+ {"log", (PyCFunction)cmath_log, METH_VARARGS, cmath_log__doc__},
+
+static PyObject *
+cmath_log_impl(PyModuleDef *module, Py_complex x, PyObject *y_obj);
+
+static PyObject *
+cmath_log(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex x;
+ PyObject *y_obj = NULL;
+
+ if (!PyArg_ParseTuple(args,
+ "D|O:log",
+ &x, &y_obj))
+ goto exit;
+ return_value = cmath_log_impl(module, x, y_obj);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_phase__doc__,
+"phase($module, z, /)\n"
+"--\n"
+"\n"
+"Return argument, also known as the phase angle, of a complex.");
+
+#define CMATH_PHASE_METHODDEF \
+ {"phase", (PyCFunction)cmath_phase, METH_VARARGS, cmath_phase__doc__},
+
+static PyObject *
+cmath_phase_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_phase(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+
+ if (!PyArg_ParseTuple(args,
+ "D:phase",
+ &z))
+ goto exit;
+ return_value = cmath_phase_impl(module, z);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_polar__doc__,
+"polar($module, z, /)\n"
+"--\n"
+"\n"
+"Convert a complex from rectangular coordinates to polar coordinates.\n"
+"\n"
+"r is the distance from 0 and phi the phase angle.");
+
+#define CMATH_POLAR_METHODDEF \
+ {"polar", (PyCFunction)cmath_polar, METH_VARARGS, cmath_polar__doc__},
+
+static PyObject *
+cmath_polar_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_polar(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+
+ if (!PyArg_ParseTuple(args,
+ "D:polar",
+ &z))
+ goto exit;
+ return_value = cmath_polar_impl(module, z);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_rect__doc__,
+"rect($module, r, phi, /)\n"
+"--\n"
+"\n"
+"Convert from polar coordinates to rectangular coordinates.");
+
+#define CMATH_RECT_METHODDEF \
+ {"rect", (PyCFunction)cmath_rect, METH_VARARGS, cmath_rect__doc__},
+
+static PyObject *
+cmath_rect_impl(PyModuleDef *module, double r, double phi);
+
+static PyObject *
+cmath_rect(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ double r;
+ double phi;
+
+ if (!PyArg_ParseTuple(args,
+ "dd:rect",
+ &r, &phi))
+ goto exit;
+ return_value = cmath_rect_impl(module, r, phi);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_isfinite__doc__,
+"isfinite($module, z, /)\n"
+"--\n"
+"\n"
+"Return True if both the real and imaginary parts of z are finite, else False.");
+
+#define CMATH_ISFINITE_METHODDEF \
+ {"isfinite", (PyCFunction)cmath_isfinite, METH_VARARGS, cmath_isfinite__doc__},
+
+static PyObject *
+cmath_isfinite_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_isfinite(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+
+ if (!PyArg_ParseTuple(args,
+ "D:isfinite",
+ &z))
+ goto exit;
+ return_value = cmath_isfinite_impl(module, z);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_isnan__doc__,
+"isnan($module, z, /)\n"
+"--\n"
+"\n"
+"Checks if the real or imaginary part of z not a number (NaN).");
+
+#define CMATH_ISNAN_METHODDEF \
+ {"isnan", (PyCFunction)cmath_isnan, METH_VARARGS, cmath_isnan__doc__},
+
+static PyObject *
+cmath_isnan_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_isnan(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+
+ if (!PyArg_ParseTuple(args,
+ "D:isnan",
+ &z))
+ goto exit;
+ return_value = cmath_isnan_impl(module, z);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(cmath_isinf__doc__,
+"isinf($module, z, /)\n"
+"--\n"
+"\n"
+"Checks if the real or imaginary part of z is infinite.");
+
+#define CMATH_ISINF_METHODDEF \
+ {"isinf", (PyCFunction)cmath_isinf, METH_VARARGS, cmath_isinf__doc__},
+
+static PyObject *
+cmath_isinf_impl(PyModuleDef *module, Py_complex z);
+
+static PyObject *
+cmath_isinf(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_complex z;
+
+ if (!PyArg_ParseTuple(args,
+ "D:isinf",
+ &z))
+ goto exit;
+ return_value = cmath_isinf_impl(module, z);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=4407f898ae07c83d input=a9049054013a1b77]*/
diff --git a/Modules/clinic/fcntlmodule.c.h b/Modules/clinic/fcntlmodule.c.h
new file mode 100644
index 0000000..377e55d
--- /dev/null
+++ b/Modules/clinic/fcntlmodule.c.h
@@ -0,0 +1,188 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+PyDoc_STRVAR(fcntl_fcntl__doc__,
+"fcntl($module, fd, code, arg=None, /)\n"
+"--\n"
+"\n"
+"Perform the operation `code` on file descriptor fd.\n"
+"\n"
+"The values used for `code` are operating system dependent, and are available\n"
+"as constants in the fcntl module, using the same names as used in\n"
+"the relevant C header files. The argument arg is optional, and\n"
+"defaults to 0; it may be an int or a string. If arg is given as a string,\n"
+"the return value of fcntl is a string of that length, containing the\n"
+"resulting value put in the arg buffer by the operating system. The length\n"
+"of the arg string is not allowed to exceed 1024 bytes. If the arg given\n"
+"is an integer or if none is specified, the result value is an integer\n"
+"corresponding to the return value of the fcntl call in the C code.");
+
+#define FCNTL_FCNTL_METHODDEF \
+ {"fcntl", (PyCFunction)fcntl_fcntl, METH_VARARGS, fcntl_fcntl__doc__},
+
+static PyObject *
+fcntl_fcntl_impl(PyModuleDef *module, int fd, int code, PyObject *arg);
+
+static PyObject *
+fcntl_fcntl(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ int fd;
+ int code;
+ PyObject *arg = NULL;
+
+ if (!PyArg_ParseTuple(args,
+ "O&i|O:fcntl",
+ conv_descriptor, &fd, &code, &arg))
+ goto exit;
+ return_value = fcntl_fcntl_impl(module, fd, code, arg);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(fcntl_ioctl__doc__,
+"ioctl($module, fd, op, arg=None, mutate_flag=True, /)\n"
+"--\n"
+"\n"
+"Perform the operation op on file descriptor fd.\n"
+"\n"
+"The values used for op are operating system dependent, and are available as\n"
+"constants in the fcntl or termios library modules, using the same names as\n"
+"used in the relevant C header files.\n"
+"\n"
+"The argument `arg` is optional, and defaults to 0; it may be an int or a\n"
+"buffer containing character data (most likely a string or an array).\n"
+"\n"
+"If the argument is a mutable buffer (such as an array) and if the\n"
+"mutate_flag argument (which is only allowed in this case) is true then the\n"
+"buffer is (in effect) passed to the operating system and changes made by\n"
+"the OS will be reflected in the contents of the buffer after the call has\n"
+"returned. The return value is the integer returned by the ioctl system\n"
+"call.\n"
+"\n"
+"If the argument is a mutable buffer and the mutable_flag argument is not\n"
+"passed or is false, the behavior is as if a string had been passed. This\n"
+"behavior will change in future releases of Python.\n"
+"\n"
+"If the argument is an immutable buffer (most likely a string) then a copy\n"
+"of the buffer is passed to the operating system and the return value is a\n"
+"string of the same length containing whatever the operating system put in\n"
+"the buffer. The length of the arg buffer in this case is not allowed to\n"
+"exceed 1024 bytes.\n"
+"\n"
+"If the arg given is an integer or if none is specified, the result value is\n"
+"an integer corresponding to the return value of the ioctl call in the C\n"
+"code.");
+
+#define FCNTL_IOCTL_METHODDEF \
+ {"ioctl", (PyCFunction)fcntl_ioctl, METH_VARARGS, fcntl_ioctl__doc__},
+
+static PyObject *
+fcntl_ioctl_impl(PyModuleDef *module, int fd, unsigned int code, PyObject *ob_arg, int mutate_arg);
+
+static PyObject *
+fcntl_ioctl(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ int fd;
+ unsigned int code;
+ PyObject *ob_arg = NULL;
+ int mutate_arg = 1;
+
+ if (!PyArg_ParseTuple(args,
+ "O&I|Op:ioctl",
+ conv_descriptor, &fd, &code, &ob_arg, &mutate_arg))
+ goto exit;
+ return_value = fcntl_ioctl_impl(module, fd, code, ob_arg, mutate_arg);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(fcntl_flock__doc__,
+"flock($module, fd, code, /)\n"
+"--\n"
+"\n"
+"Perform the lock operation op on file descriptor fd.\n"
+"\n"
+"See the Unix manual page for flock(2) for details (On some systems, this\n"
+"function is emulated using fcntl()).");
+
+#define FCNTL_FLOCK_METHODDEF \
+ {"flock", (PyCFunction)fcntl_flock, METH_VARARGS, fcntl_flock__doc__},
+
+static PyObject *
+fcntl_flock_impl(PyModuleDef *module, int fd, int code);
+
+static PyObject *
+fcntl_flock(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ int fd;
+ int code;
+
+ if (!PyArg_ParseTuple(args,
+ "O&i:flock",
+ conv_descriptor, &fd, &code))
+ goto exit;
+ return_value = fcntl_flock_impl(module, fd, code);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(fcntl_lockf__doc__,
+"lockf($module, fd, code, lenobj=None, startobj=None, whence=0, /)\n"
+"--\n"
+"\n"
+"A wrapper around the fcntl() locking calls.\n"
+"\n"
+"fd is the file descriptor of the file to lock or unlock, and operation is one\n"
+"of the following values:\n"
+"\n"
+" LOCK_UN - unlock\n"
+" LOCK_SH - acquire a shared lock\n"
+" LOCK_EX - acquire an exclusive lock\n"
+"\n"
+"When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with\n"
+"LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n"
+"lock cannot be acquired, an IOError will be raised and the exception will\n"
+"have an errno attribute set to EACCES or EAGAIN (depending on the operating\n"
+"system -- for portability, check for either value).\n"
+"\n"
+"length is the number of bytes to lock, with the default meaning to lock to\n"
+"EOF. start is the byte offset, relative to whence, to that the lock\n"
+"starts. whence is as with fileobj.seek(), specifically:\n"
+"\n"
+" 0 - relative to the start of the file (SEEK_SET)\n"
+" 1 - relative to the current buffer position (SEEK_CUR)\n"
+" 2 - relative to the end of the file (SEEK_END)");
+
+#define FCNTL_LOCKF_METHODDEF \
+ {"lockf", (PyCFunction)fcntl_lockf, METH_VARARGS, fcntl_lockf__doc__},
+
+static PyObject *
+fcntl_lockf_impl(PyModuleDef *module, int fd, int code, PyObject *lenobj, PyObject *startobj, int whence);
+
+static PyObject *
+fcntl_lockf(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ int fd;
+ int code;
+ PyObject *lenobj = NULL;
+ PyObject *startobj = NULL;
+ int whence = 0;
+
+ if (!PyArg_ParseTuple(args,
+ "O&i|OOi:lockf",
+ conv_descriptor, &fd, &code, &lenobj, &startobj, &whence))
+ goto exit;
+ return_value = fcntl_lockf_impl(module, fd, code, lenobj, startobj, whence);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=84bdde73a92f7c61 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/grpmodule.c.h b/Modules/clinic/grpmodule.c.h
new file mode 100644
index 0000000..6818309
--- /dev/null
+++ b/Modules/clinic/grpmodule.c.h
@@ -0,0 +1,87 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+PyDoc_STRVAR(grp_getgrgid__doc__,
+"getgrgid($module, /, id)\n"
+"--\n"
+"\n"
+"Return the group database entry for the given numeric group ID.\n"
+"\n"
+"If id is not valid, raise KeyError.");
+
+#define GRP_GETGRGID_METHODDEF \
+ {"getgrgid", (PyCFunction)grp_getgrgid, METH_VARARGS|METH_KEYWORDS, grp_getgrgid__doc__},
+
+static PyObject *
+grp_getgrgid_impl(PyModuleDef *module, PyObject *id);
+
+static PyObject *
+grp_getgrgid(PyModuleDef *module, PyObject *args, PyObject *kwargs)
+{
+ PyObject *return_value = NULL;
+ static char *_keywords[] = {"id", NULL};
+ PyObject *id;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "O:getgrgid", _keywords,
+ &id))
+ goto exit;
+ return_value = grp_getgrgid_impl(module, id);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(grp_getgrnam__doc__,
+"getgrnam($module, /, name)\n"
+"--\n"
+"\n"
+"Return the group database entry for the given group name.\n"
+"\n"
+"If name is not valid, raise KeyError.");
+
+#define GRP_GETGRNAM_METHODDEF \
+ {"getgrnam", (PyCFunction)grp_getgrnam, METH_VARARGS|METH_KEYWORDS, grp_getgrnam__doc__},
+
+static PyObject *
+grp_getgrnam_impl(PyModuleDef *module, PyObject *name);
+
+static PyObject *
+grp_getgrnam(PyModuleDef *module, PyObject *args, PyObject *kwargs)
+{
+ PyObject *return_value = NULL;
+ static char *_keywords[] = {"name", NULL};
+ PyObject *name;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "U:getgrnam", _keywords,
+ &name))
+ goto exit;
+ return_value = grp_getgrnam_impl(module, name);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(grp_getgrall__doc__,
+"getgrall($module, /)\n"
+"--\n"
+"\n"
+"Return a list of all available group entries, in arbitrary order.\n"
+"\n"
+"An entry whose name starts with \'+\' or \'-\' represents an instruction\n"
+"to use YP/NIS and may not be accessible via getgrnam or getgrgid.");
+
+#define GRP_GETGRALL_METHODDEF \
+ {"getgrall", (PyCFunction)grp_getgrall, METH_NOARGS, grp_getgrall__doc__},
+
+static PyObject *
+grp_getgrall_impl(PyModuleDef *module);
+
+static PyObject *
+grp_getgrall(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
+{
+ return grp_getgrall_impl(module);
+}
+/*[clinic end generated code: output=4709a6ba40bb8df9 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/pwdmodule.c.h b/Modules/clinic/pwdmodule.c.h
new file mode 100644
index 0000000..6a40042
--- /dev/null
+++ b/Modules/clinic/pwdmodule.c.h
@@ -0,0 +1,73 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+PyDoc_STRVAR(pwd_getpwuid__doc__,
+"getpwuid($module, uidobj, /)\n"
+"--\n"
+"\n"
+"Return the password database entry for the given numeric user ID.\n"
+"\n"
+"See `help(pwd)` for more on password database entries.");
+
+#define PWD_GETPWUID_METHODDEF \
+ {"getpwuid", (PyCFunction)pwd_getpwuid, METH_O, pwd_getpwuid__doc__},
+
+PyDoc_STRVAR(pwd_getpwnam__doc__,
+"getpwnam($module, arg, /)\n"
+"--\n"
+"\n"
+"Return the password database entry for the given user name.\n"
+"\n"
+"See `help(pwd)` for more on password database entries.");
+
+#define PWD_GETPWNAM_METHODDEF \
+ {"getpwnam", (PyCFunction)pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
+
+static PyObject *
+pwd_getpwnam_impl(PyModuleDef *module, PyObject *arg);
+
+static PyObject *
+pwd_getpwnam(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ PyObject *arg;
+
+ if (!PyArg_ParseTuple(args,
+ "U:getpwnam",
+ &arg))
+ goto exit;
+ return_value = pwd_getpwnam_impl(module, arg);
+
+exit:
+ return return_value;
+}
+
+#if defined(HAVE_GETPWENT)
+
+PyDoc_STRVAR(pwd_getpwall__doc__,
+"getpwall($module, /)\n"
+"--\n"
+"\n"
+"Return a list of all available password database entries, in arbitrary order.\n"
+"\n"
+"See help(pwd) for more on password database entries.");
+
+#define PWD_GETPWALL_METHODDEF \
+ {"getpwall", (PyCFunction)pwd_getpwall, METH_NOARGS, pwd_getpwall__doc__},
+
+static PyObject *
+pwd_getpwall_impl(PyModuleDef *module);
+
+static PyObject *
+pwd_getpwall(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
+{
+ return pwd_getpwall_impl(module);
+}
+
+#endif /* defined(HAVE_GETPWENT) */
+
+#ifndef PWD_GETPWALL_METHODDEF
+ #define PWD_GETPWALL_METHODDEF
+#endif /* !defined(PWD_GETPWALL_METHODDEF) */
+/*[clinic end generated code: output=2e23f920020a750a input=a9049054013a1b77]*/
diff --git a/Modules/clinic/spwdmodule.c.h b/Modules/clinic/spwdmodule.c.h
new file mode 100644