Python 3.4 有什麼新功能

編輯者:

Elvis Pranskevichus <elvis@magic.io>, Yury Selivanov <yury@magic.io>

本文介紹了 Python 3.5 與 3.4 相比多了哪些新功能。Python 3.1 已於 2015 年 9 月 13 日發布。完整詳情請見changelog

也參考

PEP 478 - Python 3.5 發佈時程

發布重點摘要

新增語法特性:

  • PEP 492, coroutines with async and await syntax.

  • PEP 465, a new matrix multiplication operator:a@b.

  • PEP 448, additional unpacking generalizations.

新的函式庫模組:

新的內建功能

  • bytes%args,bytearray%args:PEP 461 --Adding% formatting to bytes and bytearray.

  • Newbytes.hex(),bytearray.hex() andmemoryview.hex()methods. (Contributed by Arnon Yaari inbpo-9951.)

  • memoryview now supports tuple indexing (including multi-dimensional).(Contributed by Antoine Pitrou inbpo-23632.)

  • Generators have a newgi_yieldfrom attribute, which returns theobject being iterated byyieldfrom expressions. (Contributedby Benno Leslie and Yury Selivanov inbpo-24450.)

  • A newRecursionError exception is now raised when maximumrecursion depth is reached. (Contributed by Georg Brandlinbpo-19235.)

CPython 實作改進:

  • When theLC_TYPE locale is the POSIX locale (C locale),sys.stdin andsys.stdout now use thesurrogateescape error handler, instead of thestrict error handler.(Contributed by Victor Stinner inbpo-19977.)

  • .pyo files are no longer used and have been replaced by a more flexiblescheme that includes the optimization level explicitly in.pyc name.(SeePEP 488 overview.)

  • Builtin and extension modules are now initialized in a multi-phase process,which is similar to how Python modules are loaded.(SeePEP 489 overview.)

標準函式庫中的顯著改進

安全性改進:

  • SSLv3 is now disabled throughout the standard library.It can still be enabled by instantiating assl.SSLContextmanually. (Seebpo-22638 for more details; this change wasbackported to CPython 3.4 and 2.7.)

  • HTTP cookie parsing is now stricter, in order to protectagainst potential injection attacks. (Contributed by Antoine Pitrouinbpo-22796.)

Windows improvements:

  • A new installer for Windows has replaced the old MSI.See在 Windows 上使用 Python for more information.

  • Windows builds now use Microsoft Visual C++ 14.0, and extension modulesshould use the same.

Please read on for a comprehensive list of user-facing changes, including manyother smaller improvements, CPython optimizations, deprecations, and potentialporting issues.

新增功能

PEP 492 - Coroutines with async and await syntax

PEP 492 greatly improves support for asynchronous programming in Pythonby addingawaitable objects,coroutine functions,asynchronous iteration,andasynchronous context managers.

Coroutine functions are declared using the newasyncdef syntax:

>>>asyncdefcoro():...return'spam'

Inside a coroutine function, the newawait expression can be usedto suspend coroutine execution until the result is available. Any objectcan beawaited, as long as it implements theawaitable protocol bydefining the__await__() method.

PEP 492 also addsasyncfor statement for convenient iterationover asynchronous iterables.

An example of a rudimentary HTTP client written using the new syntax:

importasyncioasyncdefhttp_get(domain):reader,writer=awaitasyncio.open_connection(domain,80)writer.write(b'\r\n'.join([b'GET / HTTP/1.1',b'Host: %b'%domain.encode('latin-1'),b'Connection: close',b'',b'']))asyncforlineinreader:print('>>>',line)writer.close()loop=asyncio.get_event_loop()try:loop.run_until_complete(http_get('example.com'))finally:loop.close()

Similarly to asynchronous iteration, there is a new syntax for asynchronouscontext managers. The following script:

importasyncioasyncdefcoro(name,lock):print('coro{}: waiting for lock'.format(name))asyncwithlock:print('coro{}: holding the lock'.format(name))awaitasyncio.sleep(1)print('coro{}: releasing the lock'.format(name))loop=asyncio.get_event_loop()lock=asyncio.Lock()coros=asyncio.gather(coro(1,lock),coro(2,lock))try:loop.run_until_complete(coros)finally:loop.close()

will output:

coro2:waitingforlockcoro2:holdingthelockcoro1:waitingforlockcoro2:releasingthelockcoro1:holdingthelockcoro1:releasingthelock

Note that bothasyncfor andasyncwith can onlybe used inside a coroutine function declared withasyncdef.

Coroutine functions are intended to be run inside a compatible event loop,such as theasyncio loop.

備註

在 3.5.2 版的變更:Starting with CPython 3.5.2,__aiter__ can directly returnasynchronous iterators. Returninganawaitable object will result in aPendingDeprecationWarning.

See more details in theAsynchronous Iterators documentationsection.

也參考

PEP 492 -- Coroutines with async and await syntax

由 Yury Selivanov 撰寫 PEP 與實作。

PEP 465 - A dedicated infix operator for matrix multiplication

PEP 465 adds the@ infix operator for matrix multiplication.Currently, no builtin Python types implement the new operator, however, itcan be implemented by defining__matmul__(),__rmatmul__(),and__imatmul__() for regular, reflected, and in-place matrixmultiplication. The semantics of these methods is similar to that ofmethods defining other infix arithmetic operators.

Matrix multiplication is a notably common operation in many fields ofmathematics, science, engineering, and the addition of@ allows writingcleaner code:

S=(H@beta-r).T@inv(H@V@H.T)@(H@beta-r)

instead of:

S=dot((dot(H,beta)-r).T,dot(inv(dot(dot(H,V),H.T)),dot(H,beta)-r))

NumPy 1.10 has support for the new operator:

>>>importnumpy>>>x=numpy.ones(3)>>>xarray([ 1., 1., 1.])>>>m=numpy.eye(3)>>>marray([[ 1., 0., 0.],       [ 0., 1., 0.],       [ 0., 0., 1.]])>>>x@marray([ 1., 1., 1.])

也參考

PEP 465 -- A dedicated infix operator for matrix multiplication

由 Nathaniel J. Smith 撰寫 PEP;由 Benjamin Peterson 實作。

PEP 448 - Additional Unpacking Generalizations

PEP 448 extends the allowed uses of the* iterable unpackingoperator and** dictionary unpacking operator. It is now possibleto use an arbitrary number of unpackings infunction calls:

>>>print(*[1],*[2],3,*[4,5])1 2 3 4 5>>>deffn(a,b,c,d):...print(a,b,c,d)...>>>fn(**{'a':1,'c':3},**{'b':2,'d':4})1 2 3 4

Similarly, tuple, list, set, and dictionary displays allow multipleunpackings (seeExpression lists andDictionary displays):

>>>*range(4),4(0, 1, 2, 3, 4)>>>[*range(4),4][0, 1, 2, 3, 4]>>>{*range(4),4,*(5,6,7)}{0, 1, 2, 3, 4, 5, 6, 7}>>>{'x':1,**{'y':2}}{'x': 1, 'y': 2}

也參考

PEP 448 -- Additional Unpacking Generalizations

PEP written by Joshua Landau; implemented by Neil Girdhar,Thomas Wouters, and Joshua Landau.

PEP 461 - percent formatting support for bytes and bytearray

PEP 461 adds support for the%interpolation operator tobytesandbytearray.

While interpolation is usually thought of as a string operation, there arecases where interpolation onbytes orbytearrays makes sense, and thework needed to make up for this missing functionality detracts from theoverall readability of the code. This issue is particularly important whendealing with wire format protocols, which are often a mixture of binaryand ASCII compatible text.

範例:

>>>b'Hello %b!'%b'World'b'Hello World!'>>>b'x=%i y=%f'%(1,2.5)b'x=1 y=2.500000'

Unicode is not allowed for%b, but it is accepted by%a (equivalent ofrepr(obj).encode('ascii','backslashreplace')):

>>>b'Hello %b!'%'World'Traceback (most recent call last):  File"<stdin>", line1, in<module>TypeError:%b requires bytes, or an object that implements __bytes__, not 'str'>>>b'price:%a'%'10€'b"price: '10\\u20ac'"

Note that%s and%r conversion types, although supported, shouldonly be used in codebases that need compatibility with Python 2.

也參考

PEP 461 -- Adding % formatting to bytes and bytearray

PEP written by Ethan Furman; implemented by Neil Schemenauer andEthan Furman.

PEP 484 - 型別提示

Function annotation syntax has been a Python feature since version 3.0(PEP 3107), however the semantics of annotations has been left undefined.

Experience has shown that the majority of function annotationuses were to provide type hints to function parameters and return values. Itbecame evident that it would be beneficial for Python users, if thestandard library included the base definitions and tools for type annotations.

PEP 484 introduces aprovisional module toprovide these standard definitions and tools, along with some conventionsfor situations where annotations are not available.

For example, here is a simple function whose argument and return typeare declared in the annotations:

defgreeting(name:str)->str:return'Hello '+name

While these annotations are available at runtime through the usual__annotations__ attribute,no automatic type checking happensat runtime. Instead, it is assumed that a separate off-line type checker(e.g.mypy) will be used for on-demandsource code analysis.

The type system supports unions, generic types, and a special typenamedAny which is consistent with (i.e. assignable toand from) all types.

也參考

  • typing 模組文件

  • PEP 484 -- 型別提示

    PEP written by Guido van Rossum, Jukka Lehtosalo, and Łukasz Langa;implemented by Guido van Rossum.

  • PEP 483 -- 型別提示理論

    由 Guido van Rossum 撰寫 PEP

PEP 471 - os.scandir() function -- a better and faster directory iterator

PEP 471 adds a new directory iteration function,os.scandir(),to the standard library. Additionally,os.walk() is nowimplemented usingscandir, which makes it 3 to 5 times fasteron POSIX systems and 7 to 20 times faster on Windows systems. This islargely achieved by greatly reducing the number of calls toos.stat()required to walk a directory tree.

Additionally,scandir returns an iterator, as opposed to returninga list of file names, which improves memory efficiency when iteratingover very large directories.

The following example shows a simple use ofos.scandir() to display allthe files (excluding directories) in the givenpath that don't start with'.'. Theentry.is_file() call will generallynot make an additional system call:

forentryinos.scandir(path):ifnotentry.name.startswith('.')andentry.is_file():print(entry.name)

也參考

PEP 471 -- os.scandir() function -- a better and faster directory iterator

在 Victor Stinner 協助下由 Ben Hoyt 撰寫 PEP 與實作。

PEP 475: Retry system calls failing with EINTR

Anerrno.EINTR error code is returned whenever a system call, thatis waiting for I/O, is interrupted by a signal. Previously, Python wouldraiseInterruptedError in such cases. This meant that, when writing aPython application, the developer had two choices:

  1. Ignore theInterruptedError.

  2. Handle theInterruptedError and attempt to restart the interruptedsystem call at every call site.

The first option makes an application fail intermittently.The second option adds a large amount of boilerplate that makes thecode nearly unreadable. Compare:

print("Hello World")

和:

whileTrue:try:print("Hello World")breakexceptInterruptedError:continue

PEP 475 implements automatic retry of system calls onEINTR. This removes the burden of dealing withEINTRorInterruptedError in user code in most situations and makesPython programs, including the standard library, more robust. Note thatthe system call is only retried if the signal handler does not raise anexception.

Below is a list of functions which are now retried when interruptedby a signal:

也參考

PEP 475 -- Retry system calls failing with EINTR

PEP and implementation written by Charles-François Natali andVictor Stinner, with the help of Antoine Pitrou (the French connection).

PEP 479: Change StopIteration handling inside generators

The interaction of generators andStopIteration in Python 3.4 andearlier was sometimes surprising, and could conceal obscure bugs. Previously,StopIteration raised accidentally inside a generator function wasinterpreted as the end of the iteration by the loop construct driving thegenerator.

PEP 479 changes the behavior of generators: when aStopIterationexception is raised inside a generator, it is replaced with aRuntimeError before it exits the generator frame. The main goal ofthis change is to ease debugging in the situation where an unguardednext() call raisesStopIteration and causes the iteration controlledby the generator to terminate silently. This is particularly pernicious incombination with theyieldfrom construct.

This is a backwards incompatible change, so to enable the new behavior,a__future__ import is necessary:

>>>from__future__importgenerator_stop>>>defgen():...next(iter([]))...yield...>>>next(gen())Traceback (most recent call last):  File"<stdin>", line2, ingenStopIterationThe above exception was the direct cause of the following exception:Traceback (most recent call last):  File"<stdin>", line1, in<module>RuntimeError:generator raised StopIteration

Without a__future__ import, aPendingDeprecationWarning will beraised whenever aStopIteration exception is raised inside a generator.

也參考

PEP 479 -- Change StopIteration handling inside generators

PEP written by Chris Angelico and Guido van Rossum. Implemented byChris Angelico, Yury Selivanov and Nick Coghlan.

PEP 485: A function for testing approximate equality

PEP 485 adds themath.isclose() andcmath.isclose()functions which tell whether two values are approximately equal or"close" to each other. Whether or not two values are consideredclose is determined according to given absolute and relative tolerances.Relative tolerance is the maximum allowed difference betweenisclosearguments, relative to the larger absolute value:

>>>importmath>>>a=5.0>>>b=4.99998>>>math.isclose(a,b,rel_tol=1e-5)True>>>math.isclose(a,b,rel_tol=1e-6)False

It is also possible to compare two values using absolute tolerance, whichmust be a non-negative value:

>>>importmath>>>a=5.0>>>b=4.99998>>>math.isclose(a,b,abs_tol=0.00003)True>>>math.isclose(a,b,abs_tol=0.00001)False

也參考

PEP 485 -- A function for testing approximate equality

PEP written by Christopher Barker; implemented by Chris Barker andTal Einat.

PEP 486: Make the Python Launcher aware of virtual environments

PEP 486 makes the Windows launcher (seePEP 397) aware of an activevirtual environment. When the default interpreter would be used and theVIRTUAL_ENV environment variable is set, the interpreter in the virtualenvironment will be used.

也參考

PEP 486 -- Make the Python Launcher aware of virtual environments

由 Paul Moore 撰寫 PEP 與實作。

PEP 488: Elimination of PYO files

PEP 488 does away with the concept of.pyo files. This means that.pyc files represent both unoptimized and optimized bytecode. To prevent theneed to constantly regenerate bytecode files,.pyc files now have anoptionalopt- tag in their name when the bytecode is optimized. This has theside-effect of no more bytecode file name clashes when running under either-O or-OO. Consequently, bytecode files generated from-O, and-OO may now exist simultaneously.importlib.util.cache_from_source() has an updated API to help withthis change.

也參考

PEP 488 -- Elimination of PYO files

由 Brett Cannon 撰寫 PEP 與實作。

PEP 489: Multi-phase extension module initialization

PEP 489 updates extension module initialization to take advantage of thetwo step module loading mechanism introduced byPEP 451 in Python 3.4.

This change brings the import semantics of extension modules that opt-in tousing the new mechanism much closer to those of Python source and bytecodemodules, including the ability to use any valid identifier as a module name,rather than being restricted to ASCII.

也參考

PEP 489 -- Multi-phase extension module initialization

PEP written by Petr Viktorin, Stefan Behnel, and Nick Coghlan;implemented by Petr Viktorin.

其他語言更動

Some smaller changes made to the core Python language are:

  • Added the"namereplace" error handlers. The"backslashreplace"error handlers now work with decoding and translating.(Contributed by Serhiy Storchaka inbpo-19676 andbpo-22286.)

  • The-b option now affects comparisons ofbytes withint. (Contributed by Serhiy Storchaka inbpo-23681.)

  • New Kazakhkz1048 and Tajikkoi8_tcodecs.(Contributed by Serhiy Storchaka inbpo-22682 andbpo-22681.)

  • Property docstrings are now writable. This is especially useful forcollections.namedtuple() docstrings.(Contributed by Berker Peksag inbpo-24064.)

  • Circular imports involving relative imports are now supported.(Contributed by Brett Cannon and Antoine Pitrou inbpo-17636.)

新增模組

typing

The newtypingprovisional moduleprovides standard definitions and tools for function type annotations.SeeType Hints for more information.

zipapp

The newzipapp module (specified inPEP 441) provides an API andcommand line tool for creating executable Python Zip Applications, whichwere introduced in Python 2.6 inbpo-1739468, but which were not wellpublicized, either at the time or since.

With the new module, bundling your application is as simple as putting allthe files, including a__main__.py file, into a directorymyappand running:

$python-mzipappmyapp$pythonmyapp.pyz

The module implementation has been contributed by Paul Moore inbpo-23491.

也參考

PEP 441 -- Improving Python ZIP Application Support

改進的模組

argparse

TheArgumentParser class now allows disablingabbreviated usage of long options by settingallow_abbrev toFalse. (Contributed by Jonathan Paugh,Steven Bethard, paul j3 and Daniel Eriksson inbpo-14910.)

asyncio

Since theasyncio module isprovisional,all changes introduced in Python 3.5 have also been backported to Python 3.4.x.

Notable changes in theasyncio module since Python 3.4.0:

Updates in 3.5.1:

Updates in 3.5.2:

bz2

TheBZ2Decompressor.decompressmethod now accepts an optionalmax_length argument to limit the maximumsize of decompressed data. (Contributed by Nikolaus Rath inbpo-15955.)

cgi

TheFieldStorage class now supports thecontext managerprotocol. (Contributed by Berker Peksag inbpo-20289.)

cmath

A new functionisclose() provides a way to test for approximateequality. (Contributed by Chris Barker and Tal Einat inbpo-24270.)

code

TheInteractiveInterpreter.showtraceback()method now prints the full chained traceback, just like the interactiveinterpreter. (Contributed by Claudiu Popa inbpo-17442.)

collections

TheOrderedDict class is now implemented in C, whichmakes it 4 to 100 times faster. (Contributed by Eric Snow inbpo-16991.)

OrderedDict.items(),OrderedDict.keys(),OrderedDict.values() views now supportreversed() iteration.(Contributed by Serhiy Storchaka inbpo-19505.)

Thedeque class now definesindex(),insert(), andcopy(), and supports the+ and* operators.This allows deques to be recognized as aMutableSequenceand improves their substitutability for lists.(Contributed by Raymond Hettinger inbpo-23704.)

Docstrings produced bynamedtuple() can now be updated:

Point=namedtuple('Point',['x','y'])Point.__doc__+=': Cartesian coordinate'Point.x.__doc__='abscissa'Point.y.__doc__='ordinate'

(由 Berker Peksag 在bpo-24064 中貢獻。)

TheUserString class now implements the__getnewargs__(),__rmod__(),casefold(),format_map(),isprintable(), andmaketrans()methods to match the corresponding methods ofstr.(Contributed by Joe Jevnik inbpo-22189.)

collections.abc

TheSequence.index() method nowacceptsstart andstop arguments to match the corresponding methodsoftuple,list, etc.(Contributed by Devin Jeanpierre inbpo-23086.)

A newGenerator abstract base class. (Contributedby Stefan Behnel inbpo-24018.)

NewAwaitable,Coroutine,AsyncIterator, andAsyncIterable abstract base classes.(Contributed by Yury Selivanov inbpo-24184.)

For earlier Python versions, a backport of the new ABCs is available in anexternalPyPI package.

compileall

A newcompileall option,-jN, allows runningN workerssimultaneously to perform parallel bytecode compilation.Thecompile_dir() function has a correspondingworkersparameter. (Contributed by Claudiu Popa inbpo-16104.)

Another new option,-r, allows controlling the maximum recursionlevel for subdirectories. (Contributed by Claudiu Popa inbpo-19628.)

The-q command line option can now be specified more than once, inwhich case all output, including errors, will be suppressed. The correspondingquiet parameter incompile_dir(),compile_file(), andcompile_path() can nowaccept an integer value indicating the level of output suppression.(Contributed by Thomas Kluyver inbpo-21338.)

concurrent.futures

TheExecutor.map() method now accepts achunksize argument to allow batching of tasks to improve performance whenProcessPoolExecutor() is used.(Contributed by Dan O'Reilly inbpo-11271.)

The number of workers in theThreadPoolExecutorconstructor is optional now. The default value is 5 times the number of CPUs.(Contributed by Claudiu Popa inbpo-21527.)

configparser

configparser now provides a way to customize the conversionof values by specifying a dictionary of converters in theConfigParser constructor, or by defining themas methods inConfigParser subclasses. Converters defined ina parser instance are inherited by its section proxies.

範例:

>>>importconfigparser>>>conv={}>>>conv['list']=lambdav:[e.strip()foreinv.split()ife.strip()]>>>cfg=configparser.ConfigParser(converters=conv)>>>cfg.read_string("""...[s]...list = a b c d e f g...""")>>>cfg.get('s','list')'a b c d e f g'>>>cfg.getlist('s','list')['a', 'b', 'c', 'd', 'e', 'f', 'g']>>>section=cfg['s']>>>section.getlist('list')['a', 'b', 'c', 'd', 'e', 'f', 'g']

(由 Łukasz Langa 在bpo-18159 中貢獻。)

contextlib

The newredirect_stderr()context manager (similar toredirect_stdout()) makes it easier for utility scripts tohandle inflexible APIs that write their output tosys.stderr anddon't provide any options to redirect it:

>>>importcontextlib,io,logging>>>f=io.StringIO()>>>withcontextlib.redirect_stderr(f):...logging.warning('warning')...>>>f.getvalue()'WARNING:root:warning\n'

(由 Berker Peksag 在bpo-22389 中貢獻。)

csv

Thewriterow() method now supports arbitrary iterables,not just sequences. (Contributed by Serhiy Storchaka inbpo-23171.)

curses

The newupdate_lines_cols() function updates theLINESandCOLS module variables. This is useful for detectingmanual screen resizing. (Contributed by Arnon Yaari inbpo-4254.)

dbm

dumb.open always creates a new database when the flaghas the value"n". (Contributed by Claudiu Popa inbpo-18039.)

difflib

The charset of HTML documents generated byHtmlDiff.make_file()can now be customized by using a newcharset keyword-only argument.The default charset of HTML document changed from"ISO-8859-1"to"utf-8".(Contributed by Berker Peksag inbpo-2052.)

Thediff_bytes() function can now compare lists of bytestrings. This fixes a regression from Python 2.(Contributed by Terry J. Reedy and Greg Ward inbpo-17445.)

distutils

Both thebuild andbuild_ext commands now accept a-j option toenable parallel building of extension modules.(Contributed by Antoine Pitrou inbpo-5309.)

Thedistutils module now supportsxz compression, and can beenabled by passingxztar as an argument tobdist--format.(Contributed by Serhiy Storchaka inbpo-16314.)

doctest

TheDocTestSuite() function returns an emptyunittest.TestSuite ifmodule contains no docstrings, instead ofraisingValueError. (Contributed by Glenn Jones inbpo-15916.)

email

A new policy optionPolicy.mangle_from_controls whether or not lines that start with"From" in email bodies areprefixed with a">" character by generators. The default isTrue forcompat32 andFalse for all other policies.(Contributed by Milan Oberkirch inbpo-20098.)

A newMessage.get_content_disposition()method provides easy access to a canonical value for theContent-Disposition header.(Contributed by Abhilash Raj inbpo-21083.)

A new policy optionEmailPolicy.utf8can be set toTrue to encode email headers using the UTF-8 charset insteadof using encoded words. This allowsMessages to be formatted according toRFC 6532 and used with an SMTP server that supports theRFC 6531SMTPUTF8 extension. (Contributed by R. David Murray inbpo-24211.)

Themime.text.MIMEText constructor nowaccepts acharset.Charset instance.(Contributed by Claude Paroz and Berker Peksag inbpo-16324.)

enum

TheEnum callable has a new parameterstart tospecify the initial number of enum values if onlynames are provided:

>>>Animal=enum.Enum('Animal','cat dog',start=10)>>>Animal.cat<Animal.cat: 10>>>>Animal.dog<Animal.dog: 11>

(由 Ethan Furman 在bpo-21706 中貢獻。)

faulthandler

Theenable(),register(),dump_traceback() anddump_traceback_later() functions now accept filedescriptors in addition to file-like objects.(Contributed by Wei Wu inbpo-23566.)

functools

Most of thelru_cache() machinery is now implemented in C, makingit significantly faster. (Contributed by Matt Joiner, Alexey Kachayev, andSerhiy Storchaka inbpo-14373.)

glob

Theiglob() andglob() functions now support recursivesearch in subdirectories, using the"**" pattern.(Contributed by Serhiy Storchaka inbpo-13968.)

gzip

Themode argument of theGzipFile constructor nowaccepts"x" to request exclusive creation.(Contributed by Tim Heaney inbpo-19222.)

heapq

Element comparison inmerge() can now be customized bypassing akey function in a new optionalkey keyword argument,and a new optionalreverse keyword argument can be used to reverse elementcomparison:

>>>importheapq>>>a=['9','777','55555']>>>b=['88','6666']>>>list(heapq.merge(a,b,key=len))['9', '88', '777', '6666', '55555']>>>list(heapq.merge(reversed(a),reversed(b),key=len,reverse=True))['55555', '6666', '777', '88', '9']

(由 Raymond Hettinger 在bpo-13742 中貢獻。)

http

A newHTTPStatus enum that defines a set ofHTTP status codes, reason phrases and long descriptions written in English.(Contributed by Demian Brecht inbpo-21793.)

http.client

HTTPConnection.getresponse()now raises aRemoteDisconnected exception when aremote server connection is closed unexpectedly. Additionally, if aConnectionError (of whichRemoteDisconnectedis a subclass) is raised, the client socket is now closed automatically,and will reconnect on the next request:

importhttp.clientconn=http.client.HTTPConnection('www.python.org')forretriesinrange(3):try:conn.request('GET','/')resp=conn.getresponse()excepthttp.client.RemoteDisconnected:pass

(由 Martin Panter 在bpo-3566 中貢獻。)

idlelib and IDLE

Since idlelib implements the IDLE shell and editor and is not intended forimport by other programs, it gets improvements with every release. SeeLib/idlelib/NEWS.txt for a cumulative list of changes since 3.4.0,as well as changes made in future 3.5.x releases. This file is also availablefrom the IDLEHelp ‣ About IDLE dialog.

imaplib

TheIMAP4 class now supports thecontext manager protocol.When used in awith statement, the IMAP4LOGOUTcommand will be called automatically at the end of the block.(Contributed by Tarek Ziadé and Serhiy Storchaka inbpo-4972.)

Theimaplib module now supportsRFC 5161 (ENABLE Extension)andRFC 6855 (UTF-8 Support) via theIMAP4.enable()method. A newIMAP4.utf8_enabledattribute tracks whether or notRFC 6855 support is enabled.(Contributed by Milan Oberkirch, R. David Murray, and Maciej Szulik inbpo-21800.)

Theimaplib module now automatically encodes non-ASCII string usernamesand passwords using UTF-8, as recommended by the RFCs. (Contributed by MilanOberkirch inbpo-21800.)

imghdr

Thewhat() function now recognizes theOpenEXR format(contributed by Martin Vignali and Claudiu Popa inbpo-20295),and theWebP format(contributed by Fabrice Aneche and Claudiu Popa inbpo-20197.)

importlib

Theutil.LazyLoader class allows forlazy loading of modules in applications where startup time is important.(Contributed by Brett Cannon inbpo-17621.)

Theabc.InspectLoader.source_to_code()method is now a static method. This makes it easier to initialize a moduleobject with code compiled from a string by runningexec(code,module.__dict__).(Contributed by Brett Cannon inbpo-21156.)

The newutil.module_from_spec()function is now the preferred way to create a new module. As opposed tocreating atypes.ModuleType instance directly, this new functionwill set the various import-controlled attributes based on the passed-inspec object. (Contributed by Brett Cannon inbpo-20383.)

inspect

Both theSignature andParameter classes arenow picklable and hashable. (Contributed by Yury Selivanov inbpo-20726andbpo-20334.)

A newBoundArguments.apply_defaults()method provides a way to set default values for missing arguments:

>>>deffoo(a,b='ham',*args):pass>>>ba=inspect.signature(foo).bind('spam')>>>ba.apply_defaults()>>>ba.argumentsOrderedDict([('a', 'spam'), ('b', 'ham'), ('args', ())])

(由 Yury Selivanov 在bpo-24190 中貢獻。)

A new class methodSignature.from_callable() makessubclassing ofSignature easier. (Contributedby Yury Selivanov and Eric Snow inbpo-17373.)

Thesignature() function now accepts afollow_wrappedoptional keyword argument, which, when set toFalse, disables automaticfollowing of__wrapped__ links.(Contributed by Yury Selivanov inbpo-20691.)

A set of new functions to inspectcoroutine functions andcoroutine objects has been added:iscoroutine(),iscoroutinefunction(),isawaitable(),getcoroutinelocals(),andgetcoroutinestate().(Contributed by Yury Selivanov inbpo-24017 andbpo-24400.)

Thestack(),trace(),getouterframes(), andgetinnerframes()functions now return a list of named tuples.(Contributed by Daniel Shahaf inbpo-16808.)

io

A newBufferedIOBase.readinto1()method, that uses at most one call to the underlying raw stream'sRawIOBase.read() orRawIOBase.readinto() methods.(Contributed by Nikolaus Rath inbpo-20578.)

ipaddress

Both theIPv4Network andIPv6Network classesnow accept an(address,netmask) tuple argument, so as to easily constructnetwork objects from existing addresses:

>>>importipaddress>>>ipaddress.IPv4Network(('127.0.0.0',8))IPv4Network('127.0.0.0/8')>>>ipaddress.IPv4Network(('127.0.0.0','255.0.0.0'))IPv4Network('127.0.0.0/8')

(由 Peter Moody 和 Antoine Pitrou 在bpo-16531 中貢獻。)

A newreverse_pointer attribute for theIPv4Network andIPv6Network classesreturns the name of the reverse DNS PTR record:

>>>importipaddress>>>addr=ipaddress.IPv4Address('127.0.0.1')>>>addr.reverse_pointer'1.0.0.127.in-addr.arpa'>>>addr6=ipaddress.IPv6Address('::1')>>>addr6.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.0.0.0.0.0.0.0.0.ip6.arpa'

(由 Leon Weber 在bpo-20480 中貢獻。)

json

Thejson.tool command line interface now preserves the order of keys inJSON objects passed in input. The new--sort-keys option can be usedto sort the keys alphabetically. (Contributed by Berker Peksaginbpo-21650.)

JSON decoder now raisesJSONDecodeError instead ofValueError to provide better context information about the error.(Contributed by Serhiy Storchaka inbpo-19361.)

linecache

A newlazycache() function can be used to capture informationabout a non-file-based module to permit getting its lines later viagetline(). This avoids doing I/O until a line is actuallyneeded, without having to carry the module globals around indefinitely.(Contributed by Robert Collins inbpo-17911.)

locale

A newdelocalize() function can be used to convert a string intoa normalized number string, taking theLC_NUMERIC settings into account:

>>>importlocale>>>locale.setlocale(locale.LC_NUMERIC,'de_DE.UTF-8')'de_DE.UTF-8'>>>locale.delocalize('1.234,56')'1234.56'>>>locale.setlocale(locale.LC_NUMERIC,'en_US.UTF-8')'en_US.UTF-8'>>>locale.delocalize('1,234.56')'1234.56'

(由 Cédric Krier 在bpo-13918 中貢獻。)

logging

All logging methods (Loggerlog(),exception(),critical(),debug(), etc.), now accept exception instancesas anexc_info argument, in addition to boolean values and exceptiontuples:

>>>importlogging>>>try:...1/0...exceptZeroDivisionErrorasex:...logging.error('exception',exc_info=ex)ERROR:root:exception

(由 Yury Selivanov 在bpo-20537 中貢獻。)

Thehandlers.HTTPHandler class nowaccepts an optionalssl.SSLContext instance to configure SSLsettings used in an HTTP connection.(Contributed by Alex Gaynor inbpo-22788.)

Thehandlers.QueueListener class nowtakes arespect_handler_level keyword argument which, if set toTrue,will pass messages to handlers taking handler levels into account.(Contributed by Vinay Sajip.)

lzma

TheLZMADecompressor.decompress()method now accepts an optionalmax_length argument to limit the maximumsize of decompressed data.(Contributed by Martin Panter inbpo-15955.)

math

Two new constants have been added to themath module:infandnan. (Contributed by Mark Dickinson inbpo-23185.)

A new functionisclose() provides a way to test for approximateequality. (Contributed by Chris Barker and Tal Einat inbpo-24270.)

A newgcd() function has been added. Thefractions.gcd()function is now deprecated. (Contributed by Mark Dickinson and SerhiyStorchaka inbpo-22486.)

multiprocessing

sharedctypes.synchronized()objects now support thecontext manager protocol.(Contributed by Charles-François Natali inbpo-21565.)

operator

attrgetter(),itemgetter(),andmethodcaller() objects now support pickling.(Contributed by Josh Rosenberg and Serhiy Storchaka inbpo-22955.)

Newmatmul() andimatmul() functionsto perform matrix multiplication.(Contributed by Benjamin Peterson inbpo-21176.)

os

The newscandir() function returning an iterator ofDirEntry objects has been added. If possible,scandir()extracts file attributes while scanning a directory, removing the need toperform subsequent system calls to determine file type or attributes, which maysignificantly improve performance. (Contributed by Ben Hoyt with the helpof Victor Stinner inbpo-22524.)

On Windows, a newstat_result.st_file_attributesattribute is now available. It corresponds to thedwFileAttributes memberof theBY_HANDLE_FILE_INFORMATION structure returned byGetFileInformationByHandle(). (Contributed by Ben Hoyt inbpo-21719.)

Theurandom() function now uses thegetrandom() syscall on Linux 3.17or newer, andgetentropy() on OpenBSD 5.6 and newer, removing the need touse/dev/urandom and avoiding failures due to potential file descriptorexhaustion. (Contributed by Victor Stinner inbpo-22181.)

Newget_blocking() andset_blocking() functions allowgetting and setting a file descriptor's blocking mode (O_NONBLOCK.)(Contributed by Victor Stinner inbpo-22054.)

Thetruncate() andftruncate() functions are now supportedon Windows. (Contributed by Steve Dower inbpo-23668.)

There is a newos.path.commonpath() function returning the longestcommon sub-path of each passed pathname. Unlike theos.path.commonprefix() function, it always returns a validpath:

>>>os.path.commonprefix(['/usr/lib','/usr/local/lib'])'/usr/l'>>>os.path.commonpath(['/usr/lib','/usr/local/lib'])'/usr'

(由 Rafik Draoui 和 Serhiy Storchaka 在bpo-10395 中貢獻。)

pathlib

The newPath.samefile() method can be usedto check whether the path points to the same file as another path, which canbe either anotherPath object, or a string:

>>>importpathlib>>>p1=pathlib.Path('/etc/hosts')>>>p2=pathlib.Path('/etc/../etc/hosts')>>>p1.samefile(p2)True

(由 Vajrasky Kok 和 Antoine Pitrou 在bpo-19775 中貢獻。)

ThePath.mkdir() method now accepts a new optionalexist_ok argument to matchmkdir-p andos.makedirs()functionality. (Contributed by Berker Peksag inbpo-21539.)

There is a newPath.expanduser() method toexpand~ and~user prefixes. (Contributed by Serhiy Storchaka andClaudiu Popa inbpo-19776.)

A newPath.home() class method can be used to getaPath instance representing the user’s homedirectory.(Contributed by Victor Salgado and Mayank Tripathi inbpo-19777.)

NewPath.write_text(),Path.read_text(),Path.write_bytes(),Path.read_bytes() methods to simplifyread/write operations on files.

The following code snippet will create or rewrite existing file~/spam42:

>>>importpathlib>>>p=pathlib.Path('~/spam42')>>>p.expanduser().write_text('ham')3

(由 Christopher Welborn 在bpo-20218 中貢獻。)

pickle

Nested objects, such as unbound methods or nested classes, can now be pickledusingpickle protocols older than protocol version 4.Protocol version 4 already supports these cases. (Contributed by SerhiyStorchaka inbpo-23611.)

poplib

A newPOP3.utf8() command enablesRFC 6856(Internationalized Email) support, if a POP server supports it.(Contributed by Milan OberKirch inbpo-21804.)

re

References and conditional references to groups with fixed length are nowallowed in lookbehind assertions:

>>>importre>>>pat=re.compile(r'(a|b).(?<=\1)c')>>>pat.match('aac')<_sre.SRE_Match object; span=(0, 3), match='aac'>>>>pat.match('bbc')<_sre.SRE_Match object; span=(0, 3), match='bbc'>

(由 Serhiy Storchaka 在bpo-9179 中貢獻。)

The number of capturing groups in regular expressions is no longer limited to100. (Contributed by Serhiy Storchaka inbpo-22437.)

Thesub() andsubn() functions now replace unmatchedgroups with empty strings instead of raising an exception.(Contributed by Serhiy Storchaka inbpo-1519638.)

There.error exceptions have new attributes,msg,pattern,pos,lineno,andcolno, that provide better contextinformation about the error:

>>>re.compile("""...    (?x)...    .++...""")Traceback (most recent call last):...sre_constants.error:multiple repeat at position 16 (line 3, column 7)

(由 Serhiy Storchaka 在bpo-22578 中貢獻。)

readline

A newappend_history_file() function can be used to appendthe specified number of trailing elements in history to the given file.(Contributed by Bruno Cauet inbpo-22940.)

selectors

The newDevpollSelector supports efficient/dev/poll polling on Solaris.(Contributed by Giampaolo Rodola' inbpo-18931.)

shutil

Themove() function now accepts acopy_function argument,allowing, for example, thecopy() function to be used instead ofthe defaultcopy2() if there is a need to ignore file metadatawhen moving.(Contributed by Claudiu Popa inbpo-19840.)

Themake_archive() function now supports thexztar format.(Contributed by Serhiy Storchaka inbpo-5411.)

signal

On Windows, theset_wakeup_fd() function now also supportssocket handles. (Contributed by Victor Stinner inbpo-22018.)

VariousSIG* constants in thesignal module have been converted intoEnums. This allows meaningful names to be printedduring debugging, instead of integer "magic numbers".(Contributed by Giampaolo Rodola' inbpo-21076.)

smtpd

Both theSMTPServer andSMTPChannel classes nowaccept adecode_data keyword argument to determine if theDATA portion ofthe SMTP transaction is decoded using the"utf-8" codec or is insteadprovided to theSMTPServer.process_message()method as a byte string. The default isTrue for backward compatibilityreasons, but will change toFalse in Python 3.6. Ifdecode_data is settoFalse, theprocess_message method must be prepared to accept keywordarguments.(Contributed by Maciej Szulik inbpo-19662.)

TheSMTPServer class now advertises the8BITMIME extension(RFC 6152) ifdecode_data has been setTrue. If the clientspecifiesBODY=8BITMIME on theMAIL command, it is passed toSMTPServer.process_message()via themail_options keyword.(Contributed by Milan Oberkirch and R. David Murray inbpo-21795.)

TheSMTPServer class now also supports theSMTPUTF8extension (RFC 6531: Internationalized Email). If the client specifiedSMTPUTF8BODY=8BITMIME on theMAIL command, they are passed toSMTPServer.process_message()via themail_options keyword. It is the responsibility of theprocess_message method to correctly handle theSMTPUTF8 data.(Contributed by Milan Oberkirch inbpo-21725.)

It is now possible to provide, directly or via name resolution, IPv6addresses in theSMTPServer constructor, and have itsuccessfully connect. (Contributed by Milan Oberkirch inbpo-14758.)

smtplib

A newSMTP.auth() method provides a convenient way toimplement custom authentication mechanisms. (Contributed by MilanOberkirch inbpo-15014.)

TheSMTP.set_debuglevel() method nowaccepts an additional debuglevel (2), which enables timestamps in debugmessages. (Contributed by Gavin Chappell and Maciej Szulik inbpo-16914.)

Both theSMTP.sendmail() andSMTP.send_message() methods nowsupportRFC 6531 (SMTPUTF8).(Contributed by Milan Oberkirch and R. David Murray inbpo-22027.)

sndhdr

Thewhat() andwhathdr() functions now returnanamedtuple(). (Contributed by Claudiu Popa inbpo-18615.)

socket

Functions with timeouts now use a monotonic clock, instead of a system clock.(Contributed by Victor Stinner inbpo-22043.)

A newsocket.sendfile() method allowssending a file over a socket by using the high-performanceos.sendfile()function on UNIX, resulting in uploads being from 2 to 3 times faster than whenusing plainsocket.send().(Contributed by Giampaolo Rodola' inbpo-17552.)

Thesocket.sendall() method no longer resets thesocket timeout every time bytes are received or sent. The socket timeout isnow the maximum total duration to send all data.(Contributed by Victor Stinner inbpo-23853.)

Thebacklog argument of thesocket.listen()method is now optional. By default it is set toSOMAXCONN or to128, whichever is less.(Contributed by Charles-François Natali inbpo-21455.)

ssl

Memory BIO Support

(由 Geert Jansen 在bpo-21965 中貢獻。)

The newSSLObject class has been added to provide SSL protocolsupport for cases when the network I/O capabilities ofSSLSocketare not necessary or are suboptimal.SSLObject representsan SSL protocol instance, but does not implement any network I/O methods, andinstead provides a memory buffer interface. The newMemoryBIOclass can be used to pass data between Python and an SSL protocol instance.

The memory BIO SSL support is primarily intended to be used in frameworksimplementing asynchronous I/O for whichSSLSocket's readinessmodel ("select/poll") is inefficient.

A newSSLContext.wrap_bio() method can be usedto create a newSSLObject instance.

Application-Layer Protocol Negotiation Support

(由 Benjamin Peterson 在bpo-20188 中貢獻。)

Where OpenSSL support is present, thessl module now implementstheApplication-Layer Protocol Negotiation TLS extension as describedinRFC 7301.

The newSSLContext.set_alpn_protocols()can be used to specify which protocols a socket should advertise duringthe TLS handshake.

The newSSLSocket.selected_alpn_protocol()returns the protocol that was selected during the TLS handshake.TheHAS_ALPN flag indicates whether ALPN support is present.

Other Changes

There is a newSSLSocket.version() method toquery the actual protocol version in use.(Contributed by Antoine Pitrou inbpo-20421.)

TheSSLSocket class now implementsaSSLSocket.sendfile() method.(Contributed by Giampaolo Rodola' inbpo-17552.)

TheSSLSocket.send() method now raises eitherthessl.SSLWantReadError orssl.SSLWantWriteError exception on anon-blocking socket if the operation would block. Previously, it would return0. (Contributed by Nikolaus Rath inbpo-20951.)

Thecert_time_to_seconds() function now interprets the input timeas UTC and not as local time, perRFC 5280. Additionally, the returnvalue is always anint. (Contributed by Akira Li inbpo-19940.)

NewSSLObject.shared_ciphers() andSSLSocket.shared_ciphers() methods returnthe list of ciphers sent by the client during the handshake.(Contributed by Benjamin Peterson inbpo-23186.)

TheSSLSocket.do_handshake(),SSLSocket.read(),SSLSocket.shutdown(), andSSLSocket.write() methods of theSSLSocketclass no longer reset the socket timeout every time bytes are received or sent.The socket timeout is now the maximum total duration of the method.(Contributed by Victor Stinner inbpo-23853.)

Thematch_hostname() function now supports matching of IP addresses.(Contributed by Antoine Pitrou inbpo-23239.)

sqlite3

TheRow class now fully supports the sequence protocol,in particularreversed() iteration and slice indexing.(Contributed by Claudiu Popa inbpo-10203; by Lucas Sinclair,Jessica McKellar, and Serhiy Storchaka inbpo-13583.)

subprocess

The newrun() function has been added.It runs the specified command and returns aCompletedProcess object, which describes a finishedprocess. The new API is more consistent and is the recommended approachto invoking subprocesses in Python code that does not need to maintaincompatibility with earlier Python versions.(Contributed by Thomas Kluyver inbpo-23342.)

範例:

>>>subprocess.run(["ls","-l"])# 沒有捕捉到輸出CompletedProcess(args=['ls', '-l'], returncode=0)>>>subprocess.run("exit 1",shell=True,check=True)Traceback (most recent call last):...subprocess.CalledProcessError:Command 'exit 1' returned non-zero exit status 1>>>subprocess.run(["ls","-l","/dev/null"],stdout=subprocess.PIPE)CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')

sys

A newset_coroutine_wrapper() function allows setting a globalhook that will be called whenever acoroutine objectis created by anasyncdef function. A correspondingget_coroutine_wrapper() can be used to obtain a currently setwrapper. Both functions areprovisional,and are intended for debugging purposes only. (Contributed by Yury Selivanovinbpo-24017.)

A newis_finalizing() function can be used to check if the Pythoninterpreter isshutting down.(Contributed by Antoine Pitrou inbpo-22696.)

sysconfig

The name of the user scripts directory on Windows now includes the firsttwo components of the Python version. (Contributed by Paul Mooreinbpo-23437.)

tarfile

Themode argument of theopen() function now accepts"x"to request exclusive creation. (Contributed by Berker Peksag inbpo-21717.)

TheTarFile.extractall() andTarFile.extract() methods now take a keywordargumentnumeric_owner. If set toTrue, the extracted files anddirectories will be owned by the numericuid andgid from the tarfile.If set toFalse (the default, and the behavior in versions prior to 3.5),they will be owned by the named user and group in the tarfile.(Contributed by Michael Vogt and Eric Smith inbpo-23193.)

TheTarFile.list() now accepts an optionalmembers keyword argument that can be set to a subset of the list returnedbyTarFile.getmembers().(Contributed by Serhiy Storchaka inbpo-21549.)

threading

Both theLock.acquire() andRLock.acquire() methodsnow use a monotonic clock for timeout management.(Contributed by Victor Stinner inbpo-22043.)

time

Themonotonic() function is now always available.(Contributed by Victor Stinner inbpo-22043.)

timeit

A new command line option-u or--unit=U can be used to specify the timeunit for the timer output. Supported options areusec,msec,orsec. (Contributed by Julian Gindi inbpo-18983.)

Thetimeit() function has a newglobals parameter forspecifying the namespace in which the code will be running.(Contributed by Ben Roberts inbpo-2527.)

tkinter

Thetkinter._fix module used for setting up the Tcl/Tk environmenton Windows has been replaced by a private function in the_tkintermodule which makes no permanent changes to environment variables.(Contributed by Zachary Ware inbpo-20035.)

traceback

Newwalk_stack() andwalk_tb()functions to conveniently traverse frame andtraceback objects.(Contributed by Robert Collins inbpo-17911.)

New lightweight classes:TracebackException,StackSummary, andFrameSummary.(Contributed by Robert Collins inbpo-17911.)

Both theprint_tb() andprint_stack() functionsnow support negative values for thelimit argument.(Contributed by Dmitry Kazakov inbpo-22619.)

types

A newcoroutine() function to transformgenerator andgenerator-like objects intoawaitables.(Contributed by Yury Selivanov inbpo-24017.)

A new type calledCoroutineType, which is used forcoroutine objects created byasyncdef functions.(Contributed by Yury Selivanov inbpo-24400.)

unicodedata

Theunicodedata module now uses data fromUnicode 8.0.0.

unittest

TheTestLoader.loadTestsFromModule()method now accepts a keyword-only argumentpattern which is passed toload_tests as the third argument. Found packages are now checked forload_tests regardless of whether their path matchespattern, because itis impossible for a package name to match the default pattern.(Contributed by Robert Collins and Barry A. Warsaw inbpo-16662.)

Unittest discovery errors now are exposed in theTestLoader.errors attribute of theTestLoader instance.(Contributed by Robert Collins inbpo-19746.)

A new command line option--locals to show local variables intracebacks. (Contributed by Robert Collins inbpo-22936.)

unittest.mock

TheMock class has the following improvements:

  • The class constructor has a newunsafe parameter, which causes mockobjects to raiseAttributeError on attribute names startingwith"assert".(Contributed by Kushal Das inbpo-21238.)

  • A newMock.assert_not_called()method to check if the mock object was called.(Contributed by Kushal Das inbpo-21262.)

TheMagicMock class now supports__truediv__(),__divmod__() and__matmul__() operators.(Contributed by Johannes Baiter inbpo-20968, and Håkan Lövdahlinbpo-23581 andbpo-23568.)

It is no longer necessary to explicitly passcreate=True to thepatch() function when patching builtin names.(Contributed by Kushal Das inbpo-17660.)

urllib

A newrequest.HTTPPasswordMgrWithPriorAuthclass allows HTTP Basic Authentication credentials to be managed so as toeliminate unnecessary401 response handling, or to unconditionally sendcredentials on the first request in order to communicate with servers thatreturn a404 response instead of a401 if theAuthorization headeris not sent. (Contributed by Matej Cepl inbpo-19494 and Akshit Khurana inbpo-7159.)

A newquote_via argument for theparse.urlencode()function provides a way to control the encoding of query parts if needed.(Contributed by Samwyse and Arnon Yaari inbpo-13866.)

Therequest.urlopen() function accepts anssl.SSLContext object as acontext argument, which will be used forthe HTTPS connection. (Contributed by Alex Gaynor inbpo-22366.)

Theparse.urljoin() was updated to use theRFC 3986 semantics for the resolution of relative URLs, rather thanRFC 1808 andRFC 2396.(Contributed by Demian Brecht and Senthil Kumaran inbpo-22118.)

wsgiref

Theheaders argument of theheaders.Headersclass constructor is now optional.(Contributed by Pablo Torres Navarrete and SilentGhost inbpo-5800.)

xmlrpc

Theclient.ServerProxy class now supportsthecontext manager protocol.(Contributed by Claudiu Popa inbpo-20627.)

Theclient.ServerProxy constructor now acceptsan optionalssl.SSLContext instance.(Contributed by Alex Gaynor inbpo-22960.)

xml.sax

SAX parsers now support a character stream of thexmlreader.InputSource object.(Contributed by Serhiy Storchaka inbpo-2175.)

parseString() now accepts astr instance.(Contributed by Serhiy Storchaka inbpo-10590.)

zipfile

ZIP output can now be written to unseekable streams.(Contributed by Serhiy Storchaka inbpo-23252.)

Themode argument ofZipFile.open() method nowaccepts"x" to request exclusive creation.(Contributed by Serhiy Storchaka inbpo-21717.)

Other module-level changes

Many functions in themmap,ossaudiodev,socket,ssl, andcodecs modules now accept writablebytes-like objects.(Contributed by Serhiy Storchaka inbpo-23001.)

最佳化

Theos.walk() function has been sped up by 3 to 5 times on POSIX systems,and by 7 to 20 times on Windows. This was done using the newos.scandir()function, which exposes file information from the underlyingreaddir orFindFirstFile/FindNextFile system calls. (Contributed byBen Hoyt with help from Victor Stinner inbpo-23605.)

Construction ofbytes(int) (filled by zero bytes) is faster and uses lessmemory for large objects.calloc() is used instead ofmalloc() toallocate memory for these objects.(Contributed by Victor Stinner inbpo-21233.)

Some operations onipaddressIPv4Network andIPv6Network have been massively sped up, such assubnets(),supernet(),summarize_address_range(),collapse_addresses().The speed up can range from 3 to 15 times.(Contributed by Antoine Pitrou, Michel Albert, and Markus inbpo-21486,bpo-21487,bpo-20826,bpo-23266.)

Pickling ofipaddress objects was optimized to produce significantlysmaller output. (Contributed by Serhiy Storchaka inbpo-23133.)

Many operations onio.BytesIO are now 50% to 100% faster.(Contributed by Serhiy Storchaka inbpo-15381 and David Wilson inbpo-22003.)

Themarshal.dumps() function is now faster: 65--85% with versions 3and 4, 20--25% with versions 0 to 2 on typical data, and up to 5 times inbest cases.(Contributed by Serhiy Storchaka inbpo-20416 andbpo-23344.)

The UTF-32 encoder is now 3 to 7 times faster.(Contributed by Serhiy Storchaka inbpo-15027.)

Regular expressions are now parsed up to 10% faster.(Contributed by Serhiy Storchaka inbpo-19380.)

Thejson.dumps() function was optimized to run withensure_ascii=False as fast as withensure_ascii=True.(Contributed by Naoki Inada inbpo-23206.)

ThePyObject_IsInstance() andPyObject_IsSubclass()functions have been sped up in the common case that the second argumenthastype as its metaclass.(Contributed Georg Brandl by inbpo-22540.)

Method caching was slightly improved, yielding up to 5% performanceimprovement in some benchmarks.(Contributed by Antoine Pitrou inbpo-22847.)

Objects from therandom module now use 50% less memory on 64-bitbuilds. (Contributed by Serhiy Storchaka inbpo-23488.)

Theproperty() getter calls are up to 25% faster.(Contributed by Joe Jevnik inbpo-23910.)

Instantiation offractions.Fraction is now up to 30% faster.(Contributed by Stefan Behnel inbpo-22464.)

String methodsfind(),rfind(),split(),partition() and thein string operator are now significantlyfaster for searching 1-character substrings.(Contributed by Serhiy Storchaka inbpo-23573.)

建置和 C API 變更

Newcalloc functions were added:

(由 Victor Stinner 在bpo-21233 中貢獻。)

New encoding/decoding helper functions:

(由 Victor Stinner 在bpo-18395 中貢獻。)

A newPyCodec_NameReplaceErrors() function to replace the unicodeencode error with\N{...} escapes.(Contributed by Serhiy Storchaka inbpo-19676.)

A newPyErr_FormatV() function similar toPyErr_Format(),but accepts ava_list argument.(Contributed by Antoine Pitrou inbpo-18711.)

A newPyExc_RecursionError exception.(Contributed by Georg Brandl inbpo-19235.)

NewPyModule_FromDefAndSpec(),PyModule_FromDefAndSpec2(),andPyModule_ExecDef() functions introduced byPEP 489 --multi-phase extension module initialization.(Contributed by Petr Viktorin inbpo-24268.)

NewPyNumber_MatrixMultiply() andPyNumber_InPlaceMatrixMultiply() functions to perform matrixmultiplication.(Contributed by Benjamin Peterson inbpo-21176. See alsoPEP 465for details.)

ThePyTypeObject.tp_finalize slot is now part of the stable ABI.

Windows builds now require Microsoft Visual C++ 14.0, whichis available as part ofVisual Studio 2015.

Extension modules now include a platform information tag in their filename onsome platforms (the tag is optional, and CPython will import extensions withoutit, although if the tag is present and mismatched, the extension won't beloaded):

  • On Linux, extension module filenames end with.cpython-<major><minor>m-<architecture>-<os>.pyd:

    • <major> is the major number of the Python version;for Python 3.5 this is3.

    • <minor> is the minor number of the Python version;for Python 3.5 this is5.

    • <architecture> is the hardware architecture the extension modulewas built to run on. It's most commonly eitheri386 for 32-bit Intelplatforms orx86_64 for 64-bit Intel (and AMD) platforms.

    • <os> is alwayslinux-gnu, except for extensions built totalk to the 32-bit ABI on 64-bit platforms, in which case it islinux-gnu32 (and<architecture> will bex86_64).

  • On Windows, extension module filenames end with<debug>.cp<major><minor>-<platform>.pyd:

    • <major> is the major number of the Python version;for Python 3.5 this is3.

    • <minor> is the minor number of the Python version;for Python 3.5 this is5.

    • <platform> is the platform the extension module was built for,eitherwin32 for Win32,win_amd64 for Win64,win_ia64 forWindows Itanium 64, andwin_arm for Windows on ARM.

    • If built in debug mode,<debug> will be_d,otherwise it will be blank.

  • On OS X platforms, extension module filenames now end with-darwin.so.

  • On all other platforms, extension module filenames are the same as they werewith Python 3.4.

已棄用

New Keywords

async andawait are not recommended to be used as variable, class,function or module names. Introduced byPEP 492 in Python 3.5, they willbecome proper keywords in Python 3.7.

Deprecated Python Behavior

Raising theStopIteration exception inside a generator will now generate a silentPendingDeprecationWarning, which will become a non-silent deprecationwarning in Python 3.6 and will trigger aRuntimeError in Python 3.7.SeePEP 479: Change StopIteration handling inside generatorsfor details.

Unsupported Operating Systems

Windows XP is no longer supported by Microsoft, thus, perPEP 11, CPython3.5 is no longer officially supported on this OS.

Deprecated Python modules, functions and methods

Theformatter module has now graduated to full deprecation and is stillslated for removal in Python 3.6.

Theasyncio.async() function is deprecated in favor ofensure_future().

Thesmtpd module has in the past always decoded the DATA portion ofemail messages using theutf-8 codec. This can now be controlled by thenewdecode_data keyword toSMTPServer. The default value isTrue, but this default is deprecated. Specify thedecode_data keywordwith an appropriate value to avoid the deprecation warning.

Directly assigning values to thekey,value andcoded_value ofhttp.cookies.Morselobjects is deprecated. Use theset() methodinstead. In addition, the undocumentedLegalChars parameter ofset() is deprecated, and is now ignored.

Passing a format string as keyword argumentformat_string to theformat() method of thestring.Formatterclass has been deprecated.(Contributed by Serhiy Storchaka inbpo-23671.)

Theplatform.dist() andplatform.linux_distribution() functionsare now deprecated. Linux distributions use too many different ways ofdescribing themselves, so the functionality is left to a package.(Contributed by Vajrasky Kok and Berker Peksag inbpo-1322.)

The previously undocumentedfrom_function andfrom_builtin methods ofinspect.Signature are deprecated. Use the newSignature.from_callable()method instead. (Contributed by Yury Selivanov inbpo-24248.)

Theinspect.getargspec() function is deprecated and scheduled to beremoved in Python 3.6. (Seebpo-20438 for details.)

Theinspectgetfullargspec(),getcallargs(), andformatargspec() functions aredeprecated in favor of theinspect.signature() API. (Contributed by YurySelivanov inbpo-20438.)

getargvalues() andformatargvalues() functionswere inadvertently marked as deprecated with the release of Python 3.5.0.

Use ofre.LOCALE flag with str patterns orre.ASCII is nowdeprecated. (Contributed by Serhiy Storchaka inbpo-22407.)

Use of unrecognized special sequences consisting of'\' and an ASCII letterin regular expression patterns and replacement patterns now raises adeprecation warning and will be forbidden in Python 3.6.(Contributed by Serhiy Storchaka inbpo-23622.)

The undocumented and unofficialuse_load_tests default argument of theunittest.TestLoader.loadTestsFromModule() method now isdeprecated and ignored.(Contributed by Robert Collins and Barry A. Warsaw inbpo-16662.)

已移除

API 與功能的移除

The following obsolete and previously deprecated APIs and features have beenremoved:

  • The__version__ attribute has been dropped from the email package. Theemail 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 internalNetrc class in theftplib module was deprecated in3.4, and has now been removed.(Contributed by Matt Chaput inbpo-6623.)

  • The concept of.pyo files has been removed.

  • The JoinableQueue class in the provisionalasyncio module wasdeprecated in 3.4.4 and is now removed.(Contributed by A. Jesse Jiryu Davis inbpo-23464.)

移植至 Python 3.5

This section lists previously described changes and other bugfixesthat may require changes to your code.

Python 行為的改變

  • Due to an oversight, earlier Python versions erroneously accepted thefollowing syntax:

    f(1forxin[1],*args)f(1forxin[1],**kwargs)

    Python 3.5 now correctly raises aSyntaxError, as generatorexpressions must be put in parentheses if not a sole argument to a function.

Python API 的變更

  • PEP 475: System calls are now retried when interrupted by a signal insteadof raisingInterruptedError if the Python signal handler does notraise an exception.

  • Before Python 3.5, adatetime.time object was considered to be falseif it represented midnight in UTC. This behavior was considered obscure anderror-prone and has been removed in Python 3.5. Seebpo-13936 for fulldetails.

  • Thessl.SSLSocket.send() method now raises eitherssl.SSLWantReadError orssl.SSLWantWriteErroron a non-blocking socket if the operation would block. Previously,it would return0. (Contributed by Nikolaus Rath inbpo-20951.)

  • The__name__ attribute of generators is now set from the function name,instead of being set from the code name. Usegen.gi_code.co_name toretrieve the code name. Generators also have a new__qualname__attribute, the qualified name, which is now used for the representationof a generator (repr(gen)).(Contributed by Victor Stinner inbpo-21205.)

  • The deprecated "strict" mode and argument ofHTMLParser,HTMLParser.error(), and theHTMLParserError exception have beenremoved. (Contributed by Ezio Melotti inbpo-15114.)Theconvert_charrefs argument ofHTMLParser isnowTrue by default. (Contributed by Berker Peksag inbpo-21047.)

  • Although it is not formally part of the API, it is worth noting for portingpurposes (ie: fixing tests) that error messages that were previously of theform "'sometype' does not support the buffer protocol" are now of the form "abytes-like object is required, not 'sometype'".(Contributed by Ezio Melotti inbpo-16518.)

  • If the current directory is set to a directory that no longer exists thenFileNotFoundError will no longer be raised and insteadfind_spec() will returnNonewithout cachingNone insys.path_importer_cache, which isdifferent than the typical case (bpo-22834).

  • HTTP status code and messages fromhttp.client andhttp.serverwere refactored into a commonHTTPStatus enum. The values inhttp.client andhttp.server remain available for backwardscompatibility. (Contributed by Demian Brecht inbpo-21793.)

  • When an import loader definesimportlib.machinery.Loader.exec_module()it is now expected to also definecreate_module() (raises aDeprecationWarning now, will be an error in Python 3.6). If the loaderinherits fromimportlib.abc.Loader then there is nothing to do, elsesimply definecreate_module() to returnNone. (Contributed by Brett Cannon inbpo-23014.)

  • There.split() function always ignored empty pattern matches, so the"x*" pattern worked the same as"x+", and the"\b" pattern neverworked. Nowre.split() raises a warning if the pattern could matchan empty string. For compatibility, use patterns that never match an emptystring (e.g."x+" instead of"x*"). Patterns that could only matchan empty string (such as"\b") now raise an error.(Contributed by Serhiy Storchaka inbpo-22818.)

  • Thehttp.cookies.Morsel dict-like interface has been made selfconsistent: morsel comparison now takes thekeyandvalue into account,copy() now results in aMorsel instance rather than adict, andupdate() will now raise an exception if any of thekeys in the update dictionary are invalid. In addition, the undocumentedLegalChars parameter ofset() is deprecated andis now ignored. (Contributed by Demian Brecht inbpo-2211.)

  • PEP 488 has removed.pyo files from Python and introduced the optionalopt- tag in.pyc file names. Theimportlib.util.cache_from_source() has gained anoptimizationparameter to help control theopt- tag. Because of this, thedebug_override parameter of the function is now deprecated..pyo filesare also no longer supported as a file argument to the Python interpreter andthus serve no purpose when distributed on their own (i.e. sourceless codedistribution). Due to the fact that the magic number for bytecode has changedin Python 3.5, all old.pyo files from previous versions of Python areinvalid regardless of this PEP.

  • Thesocket module now exports theCAN_RAW_FD_FRAMESconstant on linux 3.6 and greater.

  • Thessl.cert_time_to_seconds() function now interprets the input timeas UTC and not as local time, perRFC 5280. Additionally, the returnvalue is always anint. (Contributed by Akira Li inbpo-19940.)

  • Thepygettext.py Tool now uses the standard +NNNN format for timezones inthe POT-Creation-Date header.

  • Thesmtplib module now usessys.stderr instead of the previousmodule-levelstderr variable for debug output. If your (test)program depends on patching the module-level variable to capture the debugoutput, you will need to update it to capture sys.stderr instead.

  • Thestr.startswith() andstr.endswith() methods no longer returnTrue when finding the empty string and the indexes are completely out ofrange. (Contributed by Serhiy Storchaka inbpo-24284.)

  • Theinspect.getdoc() function now returns documentation stringsinherited from base classes. Documentation strings no longer need to beduplicated if the inherited documentation is appropriate. To suppress aninherited string, an empty string must be specified (or the documentationmay be filled in). This change affects the output of thepydocmodule and thehelp() function.(Contributed by Serhiy Storchaka inbpo-15582.)

  • Nestedfunctools.partial() calls are now flattened. If you wererelying on the previous behavior, you can now either add an attribute to afunctools.partial() object or you can create a subclass offunctools.partial().(Contributed by Alexander Belopolsky inbpo-7830.)

C API 中的改動

  • The undocumentedformat member of the(non-public)PyMemoryViewObject structure has been removed.All extensions relying on the relevant parts inmemoryobject.hmust be rebuilt.

  • ThePyMemAllocator structure was renamed toPyMemAllocatorEx and a newcalloc field was added.

  • Removed non-documented macroPyObject_REPR() which leaked references.Use format character%R inPyUnicode_FromFormat()-like functionsto format therepr() of the object.(Contributed by Serhiy Storchaka inbpo-22453.)

  • Because the lack of the__module__ attribute breaks pickling andintrospection, a deprecation warning is now raised for builtin types withoutthe__module__ attribute. This will be anAttributeError inthe future.(Contributed by Serhiy Storchaka inbpo-20204.)

  • As part of thePEP 492 implementation, thetp_reserved slot ofPyTypeObject was replaced with atp_as_async slot. Refer toCoroutine(協程)物件 fornew types, structures and functions.

Python 3.5.4 中顯著的變更

Newmakeregen-all build target

To simplify cross-compilation, and to ensure that CPython can reliably becompiled without requiring an existing version of Python to already beavailable, the autotools-based build system no longer attempts to implicitlyrecompile generated files based on file modification times.

Instead, a newmakeregen-all command has been added to force regenerationof these files when desired (e.g. after an initial version of Python hasalready been built based on the pregenerated versions).

More selective regeneration targets are also defined - seeMakefile.pre.in for details.

(由 Victor Stinner 在bpo-23404 中貢獻。)

在 3.5.4 版被加入.

Removal ofmaketouch build target

Themaketouch build target previously used to request implicit regenerationof generated files by updating their modification times has been removed.

It has been replaced by the newmakeregen-all target.

(由 Victor Stinner 在bpo-23404 中貢獻。)

在 3.5.4 版的變更.