Python 3.4 有什麼新功能¶
- 作者:
R. David Murray <rdmurray@bitdance.com> (編輯者)
本文介紹了 Python 3.4 與 3.3 相比多了哪些新功能。Python 3.1 已於 2014 年 3 月 16 日發布。完整詳情請見changelog。
也參考
PEP 429 - Python 3.4 發佈時程
發布重點摘要¶
新增語法特性:
Python 3.4 沒有新增任何新的語法特性。
其他新特性:
Newly created file descriptors are non-inheritable(PEP 446).
command line option forisolated mode(bpo-16499).
improvements in the handling of codecsthat are not text encodings (multiple issues).
A ModuleSpec Type for the Import System(PEP 451). (Affects importer authors.)
The
marshal
format has been mademore compact and efficient (bpo-16475).
新的函式庫模組:
selectors
:High-level and efficient I/O multiplexing, built upon theselect
module primitives (partofPEP 3156).statistics
: A basicnumerically stable statistics library (PEP 450).
顯著改進的函式庫模組:
email
has a new submodule,contentmanager
, anda newMessage
subclass(EmailMessage
) thatsimplify MIMEhandling (bpo-18891).The
inspect
andpydoc
modules are now capable ofcorrect introspection of a much wider variety of callable objects,which improves the output of the Pythonhelp()
system.ipaddress
模組 API 已宣告為穩定版本
安全性改進:
Make newly created file descriptors non-inheritable(PEP 446) to avoid leaking file descriptors to child processes.
multiprocessing
now hasan option to avoid using os.forkon Unix.spawn andforkserver aremore secure because they avoid sharing data with child processes.multiprocessing
child processes on Windows no longer inheritall of the parent's inheritable handles, only the necessary ones.Retrieving certificates from the Windows system cert store support for
ssl
.The
ssl.SSLContext
class has alot of improvements.All modules in the standard library that support SSL now support servercertificate verification, including hostname matching(
ssl.match_hostname()
) and CRLs (Certificate Revocation lists, seessl.SSLContext.load_verify_locations()
).
CPython 實作改進:
LeveragingPEP 442, in most casesmodule globals are no longer setto None during finalization (bpo-18214).
Please read on for a comprehensive list of user-facing changes, including manyother smaller improvements, CPython optimizations, deprecations, and potentialporting issues.
新增功能¶
PEP 453: Explicit Bootstrapping of PIP in Python Installations¶
Bootstrapping pip By Default¶
The newensurepip
module (defined inPEP 453) provides a standardcross-platform mechanism to bootstrap the pip installer into Pythoninstallations and virtual environments. The version ofpip
includedwith Python 3.4.0 ispip
1.5.4, and future 3.4.x maintenance releaseswill update the bundled version to the latest version ofpip
that isavailable at the time of creating the release candidate.
By default, the commandspipX
andpipX.Y
will be installed on allplatforms (where X.Y stands for the version of the Python installation),along with thepip
Python package and its dependencies. On Windows andin virtual environments on all platforms, the unversionedpip
commandwill also be installed. On other platforms, the system wide unversionedpip
command typically refers to the separately installed Python 2version.
Thepyvenv
command line utility and thevenv
module make use of theensurepip
module to makepip
readilyavailable in virtual environments. When using the command line utility,pip
is installed by default, while when using thevenv
moduleAPI installation ofpip
must be requested explicitly.
For CPythonsource builds on POSIX systems,themakeinstall
andmakealtinstall
commands bootstrappip
bydefault. This behaviour can be controlled through configure options, andoverridden through Makefile options.
On Windows and Mac OS X, the CPython installers now default to installingpip
along with CPython itself (users may opt out of installing itduring the installation process). Window users will need to opt in to theautomaticPATH
modifications to havepip
available from the commandline by default, otherwise it can still be accessed through the Pythonlauncher for Windows aspy-mpip
.
Asdiscussed in the PEPplatform packagers may choose not to installthese commands by default, as long as, when invoked, they provide clear andsimple directions on how to install them on that platform (usually usingthe system package manager).
備註
To avoid conflicts between parallel Python 2 and Python 3 installations,only the versionedpip3
andpip3.4
commands are bootstrapped bydefault whenensurepip
is invoked directly - the--default-pip
option is needed to also request the unversionedpip
command.pyvenv
and the Windows installer ensure that the unqualifiedpip
command is made available in those environments, andpip
can always beinvoked via the-m
switch rather than directly to avoid ambiguity onsystems with multiple Python installations.
文件更動¶
As part of this change, the安裝 Python 模組 and發布 Python 模組 sections of the documentation have beencompletely redesigned as short getting started and FAQ documents. Mostpackaging documentation has now been moved out to the Python PackagingAuthority maintainedPython Packaging User Guide and the documentation of the individualprojects.
However, as this migration is currently still incomplete, the legacyversions of those guides remaining available as用 setuptools 建置 C 與 C++ 擴充套件and用 setuptools 建置 C 與 C++ 擴充套件.
也參考
- PEP 453 -- Explicit bootstrapping of pip in Python installations
PEP written by Donald Stufft and Nick Coghlan, implemented byDonald Stufft, Nick Coghlan, Martin von Löwis and Ned Deily.
PEP 446: Newly Created File Descriptors Are Non-Inheritable¶
PEP 446 makes newly created file descriptorsnon-inheritable. In general, this is the behavior an application willwant: when launching a new process, having currently open files alsoopen in the new process can lead to all sorts of hard to find bugs,and potentially to security issues.
However, there are occasions when inheritance is desired. To supportthese cases, the following new functions and methods are available:
也參考
- PEP 446 - 使新建立的檔案描述器不可繼承
由 Victor Stinner 撰寫 PEP 與實作。
編解碼器處理的改進¶
Since it was first introduced, thecodecs
module has always beenintended to operate as a type-neutral dynamic encoding and decodingsystem. However, its close coupling with the Python text model, especiallythe type restricted convenience methods on the builtinstr
,bytes
andbytearray
types, has historically obscured thatfact.
As a key step in clarifying the situation, thecodecs.encode()
andcodecs.decode()
convenience functions are now properly documented inPython 2.7, 3.3 and 3.4. These functions have existed in thecodecs
module (and have been covered by the regression test suite) since Python 2.4,but were previously only discoverable through runtime introspection.
Unlike the convenience methods onstr
,bytes
andbytearray
, thecodecs
convenience functions support arbitrarycodecs in both Python 2 and Python 3, rather than being limited to Unicode textencodings (in Python 3) orbasestring
<->basestring
conversions (inPython 2).
In Python 3.4, the interpreter is able to identify the known non-textencodings provided in the standard library and direct users towards thesegeneral purpose convenience functions when appropriate:
>>>b"abcdef".decode("hex")Traceback (most recent call last): File"<stdin>", line1, in<module>LookupError:'hex' is not a text encoding; use codecs.decode() to handle arbitrary codecs>>>"hello".encode("rot13")Traceback (most recent call last): File"<stdin>", line1, in<module>LookupError:'rot13' is not a text encoding; use codecs.encode() to handle arbitrary codecs>>>open("foo.txt",encoding="hex")Traceback (most recent call last): File"<stdin>", line1, in<module>LookupError:'hex' is not a text encoding; use codecs.open() to handle arbitrary codecs
In a related change, whenever it is feasible without breaking backwardscompatibility, exceptions raised during encoding and decoding operationsare wrapped in a chained exception of the same type that mentions thename of the codec responsible for producing the error:
>>>importcodecs>>>codecs.decode(b"abcdefgh","hex")Traceback (most recent call last): File"/usr/lib/python3.4/encodings/hex_codec.py", line20, inhex_decodereturn(binascii.a2b_hex(input),len(input))binascii.Error:Non-hexadecimal digit foundThe above exception was the direct cause of the following exception:Traceback (most recent call last): File"<stdin>", line1, in<module>binascii.Error:decoding with 'hex' codec failed (Error: Non-hexadecimal digit found)>>>codecs.encode("hello","bz2")Traceback (most recent call last): File"/usr/lib/python3.4/encodings/bz2_codec.py", line17, inbz2_encodereturn(bz2.compress(input),len(input)) File"/usr/lib/python3.4/bz2.py", line498, incompressreturncomp.compress(data)+comp.flush()TypeError:'str' does not support the buffer interfaceThe above exception was the direct cause of the following exception:Traceback (most recent call last): File"<stdin>", line1, in<module>TypeError:encoding with 'bz2' codec failed (TypeError: 'str' does not support the buffer interface)
Finally, as the examples above show, these improvements have permittedthe restoration of the convenience aliases for the non-Unicode codecs thatwere themselves restored in Python 3.2. This means that encoding binary datato and from its hexadecimal representation (for example) can now be writtenas:
>>>fromcodecsimportencode,decode>>>encode(b"hello","hex")b'68656c6c6f'>>>decode(b"68656c6c6f","hex")b'hello'
The binary and text transforms provided in the standard library are detailedinBinary Transforms andText Transforms.
(由 Nick Coghlan 在bpo-7475、bpo-17827、bpo-17828 和bpo-19619 中貢獻。)
PEP 451:用於引入系統的 ModuleSpec 型別¶
PEP 451 provides an encapsulation of the information about a module that theimport machinery will use to load it (that is, a module specification). Thishelps simplify both the import implementation and several import-related APIs.The change is also a stepping stone forseveral future import-relatedimprovements.
The public-facing changes from the PEP are entirely backward-compatible.Furthermore, they should be transparent to everyone but importer authors. Keyfinder and loader methods have been deprecated, but they will continue working.New importers should use the new methods described in the PEP. Existingimporters should be updated to implement the new methods. See the已棄用 section for a list of methods that should be replaced andtheir replacements.
其他語言更動¶
Some smaller changes made to the core Python language are:
Unicode database updated to UCD version 6.3.
min()
andmax()
now accept adefault keyword-only argument thatcan be used to specify the value they return if the iterable they areevaluating has no elements. (Contributed by Julian Berman inbpo-18111.)Module objects are nowweakly referenceable.
Module
__file__
attributes (and related values) should now alwayscontain absolute paths by default, with the sole exception of__main__.__file__
when a script has been executed directly usinga relative path. (Contributed by Brett Cannon inbpo-18416.)All the UTF-* codecs (except UTF-7) now reject surrogates during bothencoding and decoding unless the
surrogatepass
error handler is used,with the exception of the UTF-16 decoder (which accepts valid surrogate pairs)and the UTF-16 encoder (which produces them while encoding non-BMP characters).(Contributed by Victor Stinner, Kang-Hao (Kenny) Lu and Serhiy Storchaka inbpo-12892.)New German EBCDICcodec
cp273
. (Contributedby Michael Bierenfeld and Andrew Kuchling inbpo-1097797.)New Ukrainiancodec
cp1125
. (Contributed bySerhiy Storchaka inbpo-19668.)bytes
.join() andbytearray
.join() now accept arbitrarybuffer objects as arguments. (Contributed by Antoine Pitrou inbpo-15958.)The
int
constructor now accepts any object that has an__index__
method for itsbase argument. (Contributed by Mark Dickinson inbpo-16772.)Frame objects now have a
clear()
method that clears allreferences to local variables from the frame. (Contributed by Antoine Pitrouinbpo-17934.)memoryview
is now registered as aSequence
,and supports thereversed()
builtin. (Contributed by Nick Coghlanand Claudiu Popa inbpo-18690 andbpo-19078.)Signatures reported by
help()
have been modified and improved inseveral cases as a result of the introduction of Argument Clinic and otherchanges to theinspect
andpydoc
modules.__length_hint__()
is now part of the formal languagespecification (seePEP 424). (Contributed by Armin Ronacher inbpo-16148.)
新模組¶
asyncio¶
The newasyncio
module (defined inPEP 3156) provides a standardpluggable event loop model for Python, providing solid asynchronous IOsupport in the standard library, and making it easier for other event loopimplementations to interoperate with the standard library and each other.
For Python 3.4, this module is considered aprovisional API.
也參考
- PEP 3156 -- Asynchronous IO Support Rebooted: the "asyncio" Module
由 Guido van Rossum 撰寫 PEP 與帶領實作。
ensurepip¶
The newensurepip
module is the primary infrastructure for thePEP 453 implementation. In the normal course of events end users will notneed to interact with this module, but it can be used to manually bootstrappip
if the automated bootstrapping into an installation or virtualenvironment was declined.
ensurepip
includes a bundled copy ofpip
, up-to-date as of the firstrelease candidate of the release of CPython with which it ships (this appliesto both maintenance releases and feature releases).ensurepip
does notaccess the internet. If the installation has internet access, afterensurepip
is run the bundledpip
can be used to upgradepip
to amore recent release than the bundled one. (Note that such an upgraded versionofpip
is considered to be a separately installed package and will not beremoved if Python is uninstalled.)
The module is namedensurepip because if called whenpip
is alreadyinstalled, it does nothing. It also has an--upgrade
option that willcause it to install the bundled copy ofpip
if the existing installedversion ofpip
is older than the bundled copy.
enum¶
The newenum
module (defined inPEP 435) provides a standardimplementation of enumeration types, allowing other modules (such assocket
) to provide more informative error messages and betterdebugging support by replacing opaque integer constants with backwardscompatible enumeration values.
也參考
- PEP 435 -- Adding an Enum type to the Python standard library
PEP written by Barry Warsaw, Eli Bendersky and Ethan Furman,implemented by Ethan Furman.
pathlib¶
The newpathlib
module offers classes representing filesystem pathswith semantics appropriate for different operating systems. Path classes aredivided betweenpure paths, which provide purely computational operationswithout I/O, andconcrete paths, which inherit from pure paths but alsoprovide I/O operations.
For Python 3.4, this module is considered aprovisional API.
也參考
- PEP 428 -- The pathlib module -- object-oriented filesystem paths
由 Antoine Pitrou 撰寫 PEP 與實作。
selectors¶
The newselectors
module (created as part of implementingPEP 3156)allows high-level and efficient I/O multiplexing, built upon theselect
module primitives.
statistics¶
The newstatistics
module (defined inPEP 450) offers some corestatistics functionality directly in the standard library. This modulesupports calculation of the mean, median, mode, variance and standarddeviation of a data series.
也參考
- PEP 450 -- Adding A Statistics Module To The Standard Library
由 Steven D'Aprano 撰寫 PEP 與實作
tracemalloc¶
The newtracemalloc
module (defined inPEP 454) is a debug tool totrace memory blocks allocated by Python. It provides the following information:
Trace where an object was allocated
Statistics on allocated memory blocks per filename and per line number:total size, number and average size of allocated memory blocks
Compute the differences between two snapshots to detect memory leaks
也參考
- PEP 454 -- Add a new tracemalloc module to trace Python memory allocations
由 Victor Stinner 撰寫 PEP 與實作
改進的模組¶
abc¶
New functionabc.get_cache_token()
can be used to know when to invalidatecaches that are affected by changes in the object graph. (Contributedby Łukasz Langa inbpo-16832.)
New classABC
hasABCMeta
as its meta class.UsingABC
as a base class has essentially the same effect as specifyingmetaclass=abc.ABCMeta
, but is simpler to type and easier to read.(Contributed by Bruno Dupuis inbpo-16049.)
aifc¶
Thegetparams()
method now returns a namedtuple rather than aplain tuple. (Contributed by Claudiu Popa inbpo-17818.)
aifc.open()
now supports the context management protocol: when used in awith
block, theclose()
method of the returnedobject will be called automatically at the end of the block. (Contributed bySerhiy Storchacha inbpo-16486.)
Thewriteframesraw()
andwriteframes()
methods now accept anybytes-like object. (Contributed by SerhiyStorchaka inbpo-8311.)
argparse¶
TheFileType
class now acceptsencoding anderrors arguments, which are passed through toopen()
. (Contributedby Lucas Maystre inbpo-11175.)
audioop¶
audioop
now supports 24-bit samples. (Contributed by Serhiy Storchakainbpo-12866.)
Newbyteswap()
function converts big-endian samples tolittle-endian and vice versa. (Contributed by Serhiy Storchaka inbpo-19641.)
Allaudioop
functions now accept anybytes-like object. Stringsare not accepted: they didn't work before, now they raise an error right away.(Contributed by Serhiy Storchaka inbpo-16685.)
base64¶
The encoding and decoding functions inbase64
now accept anybytes-like object in cases where it previously required abytes
orbytearray
instance. (Contributed by Nick Coghlan inbpo-17839.)
New functionsa85encode()
,a85decode()
,b85encode()
, andb85decode()
provide the ability toencode and decode binary data from and toAscii85
and the git/mercurialBase85
formats, respectively. Thea85
functions have options that canbe used to make them compatible with the variants of theAscii85
encoding,including the Adobe variant. (Contributed by Martin Morrison, the Mercurialproject, Serhiy Storchaka, and Antoine Pitrou inbpo-17618.)
collections¶
TheChainMap.new_child()
method now accepts anm argument specifyingthe child map to add to the chain. This allows an existing mapping and/or acustom mapping type to be used for the child. (Contributed by Vinay Sajip inbpo-16613.)
colorsys¶
The number of digits in the coefficients for the RGB --- YIQ conversions havebeen expanded so that they match the FCC NTSC versions. The change inresults should be less than 1% and may better match results found elsewhere.(Contributed by Brian Landers and Serhiy Storchaka inbpo-14323.)
contextlib¶
The newcontextlib.suppress
context manager helps to clarify theintent of code that deliberately suppresses exceptions from a singlestatement. (Contributed by Raymond Hettinger inbpo-15806 andZero Piraeus inbpo-19266.)
The newcontextlib.redirect_stdout()
context manager makes it easierfor utility scripts to handle inflexible APIs that write their output tosys.stdout
and don't provide any options to redirect it. Using thecontext manager, thesys.stdout
output can be redirected to anyother stream or, in conjunction withio.StringIO
, to a string.The latter can be especially useful, for example, to capture outputfrom a function that was written to implement a command line interface.It is recommended only for utility scripts because it affects theglobal state ofsys.stdout
. (Contributed by Raymond Hettingerinbpo-15805.)
Thecontextlib
documentation has also been updated to include adiscussion of thedifferences between single use, reusable and reentrant context managers.
dbm¶
dbm.open()
objects now support the context management protocol. Whenused in awith
statement, theclose
method of the databaseobject will be called automatically at the end of the block. (Contributed byClaudiu Popa and Nick Coghlan inbpo-19282.)
dis¶
Functionsshow_code()
,dis()
,distb()
, anddisassemble()
now accept a keyword-onlyfile argument thatcontrols where they write their output.
Thedis
module is now built around anInstruction
classthat provides object oriented access to the details of each individual bytecodeoperation.
A new method,get_instructions()
, provides an iterator that emitsthe Instruction stream for a given piece of Python code. Thus it is nowpossible to write a program that inspects and manipulates a bytecodeobject in ways different from those provided by thedis
moduleitself. For example:
>>>importdis>>>forinstrindis.get_instructions(lambdax:x+1):...print(instr.opname)LOAD_FASTLOAD_CONSTBINARY_ADDRETURN_VALUE
The various display tools in thedis
module have been rewritten to usethese new components.
In addition, a new application-friendly classBytecode
providesan object-oriented API for inspecting bytecode in both in human-readable formand for iterating over instructions. TheBytecode
constructortakes the same arguments thatget_instruction()
does (plus anoptionalcurrent_offset), and the resulting object can be iterated to produceInstruction
objects. But it also has adis
method, equivalent to callingdis
on the constructor argument, butreturned as a multi-line string:
>>>bytecode=dis.Bytecode(lambdax:x+1,current_offset=3)>>>forinstrinbytecode:...print('{} ({})'.format(instr.opname,instr.opcode))LOAD_FAST (124)LOAD_CONST (100)BINARY_ADD (23)RETURN_VALUE (83)>>>bytecode.dis().splitlines()[' 1 0 LOAD_FAST 0 (x)', ' --> 3 LOAD_CONST 1 (1)', ' 6 BINARY_ADD', ' 7 RETURN_VALUE']
Bytecode
also has a class method,from_traceback()
, that provides the ability to manipulate atraceback (that is,print(Bytecode.from_traceback(tb).dis())
is equivalenttodistb(tb)
).
(由 Nick Coghlan、Ryan Kelly 和 Thomas Kluyver 在bpo-11816 中以及 Claudiu Popa 在bpo-17916 中貢獻。)
New functionstack_effect()
computes the effect on the Python stackof a given opcode and argument, information that is not otherwise available.(Contributed by Larry Hastings inbpo-19722.)
doctest¶
A newoption flag,FAIL_FAST
, haltstest running as soon as the first failure is detected. (Contributed by R.David Murray and Daniel Urban inbpo-16522.)
Thedoctest
command line interface now usesargparse
, and has twonew options,-o
and-f
.-o
allowsdoctest options to be specified on the command line, and-f
is ashorthand for-oFAIL_FAST
(to parallel the similar option supported by theunittest
CLI). (Contributed by R. David Murray inbpo-11390.)
doctest
will now find doctests in extension module__doc__
strings.(Contributed by Zachary Ware inbpo-3158.)
email¶
as_string()
now accepts apolicy argument tooverride the default policy of the message when generating a stringrepresentation of it. This means thatas_string
can now be used in morecircumstances, instead of having to create and use agenerator
inorder to pass formatting parameters to itsflatten
method. (Contributed byR. David Murray inbpo-18600.)
New methodas_bytes()
added to produce a bytesrepresentation of the message in a fashion similar to howas_string
produces a string representation. It does not accept themaxheaderlenargument, but does accept theunixfrom andpolicy arguments. TheMessage
__bytes__()
methodcalls it, meaning thatbytes(mymsg)
will now produce the intuitiveresult: a bytes object containing the fully formatted message. (Contributedby R. David Murray inbpo-18600.)
TheMessage.set_param()
message now accepts areplace keyword argument.When specified, the associated header will be updated without changingits location in the list of headers. For backward compatibility, the defaultisFalse
. (Contributed by R. David Murray inbpo-18891.)
A pair of new subclasses ofMessage
have been added(EmailMessage
andMIMEPart
), along with a new sub-module,contentmanager
and a newpolicy
attributecontent_manager
. All documentation iscurrently in the new module, which is being added as part of email's newprovisional API. These classes provide a number of new methods thatmake extracting content from and inserting content into email messages mucheasier. For details, see thecontentmanager
documentation andtheemail:範例. These API additions complete thebulk of the work that was planned as part of the email6 project. The currentlyprovisional API is scheduled to become final in Python 3.5 (possibly with a fewminor additions in the area of error handling). (Contributed by R. DavidMurray inbpo-18891.)
filecmp¶
A newclear_cache()
function provides the ability to clear thefilecmp
comparison cache, which usesos.stat()
information todetermine if the file has changed since the last compare. This can be used,for example, if the file might have been changed and re-checked in less timethan the resolution of a particular filesystem's file modification time field.(Contributed by Mark Levitt inbpo-18149.)
New module attributeDEFAULT_IGNORES
provides the list ofdirectories that are used as the default value for theignore parameter ofthedircmp()
function. (Contributed by Eli Bendersky inbpo-15442.)
functools¶
The newpartialmethod()
descriptor brings partial argumentapplication to descriptors, just aspartial()
providesfor normal callables. The new descriptor also makes it easier to getarbitrary callables (includingpartial()
instances)to behave like normal instance methods when included in a class definition.(Contributed by Alon Horev and Nick Coghlan inbpo-4331.)
The newsingledispatch()
decorator brings support forsingle-dispatch generic functions to the Python standard library. Whereobject oriented programming focuses on grouping multiple operations on acommon set of data into a class, a generic function focuses on groupingmultiple implementations of an operation that allows it to work withdifferent kinds of data.
也參考
- PEP 443 -- Single-dispatch generic functions
由 Łukasz Langa 撰寫 PEP 與實作。
total_ordering()
now supports a return value ofNotImplemented
from the underlying comparison function. (Contributedby Katie Miller inbpo-10042.)
A pure-python version of thepartial()
function is now in thestdlib; in CPython it is overridden by the C accelerated version, but it isavailable for other implementations to use. (Contributed by Brian Thorne inbpo-12428.)
gc¶
New functionget_stats()
returns a list of three per-generationdictionaries containing the collections statistics since interpreter startup.(Contributed by Antoine Pitrou inbpo-16351.)
glob¶
A new functionescape()
provides a way to escape special charactersin a filename so that they do not become part of the globbing expansion but areinstead matched literally. (Contributed by Serhiy Storchaka inbpo-8402.)
hashlib¶
新的hashlib.pbkdf2_hmac()
函式提供了基於密碼的 PKCS#5 密鑰生成函式 2。(由 Christian Heimes 在bpo-18582 中貢獻。)
Thename
attribute ofhashlib
hash objects is nowa formally supported interface. It has always existed in CPython'shashlib
(although it did not return lower case names for all supportedhashes), but it was not a public interface and so some other Pythonimplementations have not previously supported it. (Contributed by Jason R.Coombs inbpo-18532.)
hmac¶
hmac
now acceptsbytearray
as well asbytes
for thekeyargument to thenew()
function, and themsg parameter to both thenew()
function and theupdate()
method nowaccepts any type supported by thehashlib
module. (Contributedby Jonas Borgström inbpo-18240.)
Thedigestmod argument to thehmac.new()
function may now be any hashdigest name recognized byhashlib
. In addition, the current behavior inwhich the value ofdigestmod defaults toMD5
is deprecated: in afuture version of Python there will be no default value. (Contributed byChristian Heimes inbpo-17276.)
With the addition ofblock_size
andname
attributes (and the formal documentation of thedigest_size
attribute), thehmac
module now conforms fully to thePEP 247 API.(Contributed by Christian Heimes inbpo-18775.)
html¶
New functionunescape()
function converts HTML5 character references tothe corresponding Unicode characters. (Contributed by Ezio Melotti inbpo-2927.)
HTMLParser
accepts a new keyword argumentconvert_charrefs that, whenTrue
, automatically converts all characterreferences. For backward-compatibility, its value defaults toFalse
, butit will change toTrue
in a future version of Python, so you are invited toset it explicitly and update your code to use this new feature. (Contributedby Ezio Melotti inbpo-13633.)
Thestrict argument ofHTMLParser
is now deprecated.(Contributed by Ezio Melotti inbpo-15114.)
http¶
send_error()
now accepts anoptional additionalexplain parameter which can be used to provide anextended error description, overriding the hardcoded default if there is one.This extended error description will be formatted using theerror_message_format
attribute and sent as the bodyof the error response. (Contributed by Karl Cow inbpo-12921.)
Thehttp.server
command line interface now hasa-b/--bind
option that causes the server to listen on a specific address.(Contributed by Malte Swart inbpo-17764.)
idlelib 與 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.3.0,as well as changes made in future 3.4.x releases. This file is also availablefrom the IDLEHelp ‣ About IDLE dialog.
importlib¶
TheInspectLoader
ABC defines a new method,source_to_code()
that accepts sourcedata and a path and returns a code object. The default implementationis equivalent tocompile(data,path,'exec',dont_inherit=True)
.(Contributed by Eric Snow and Brett Cannon inbpo-15627.)
InspectLoader
also now has a default implementationfor theget_code()
method. However,it will normally be desirable to override the default implementationfor performance reasons. (Contributed by Brett Cannon inbpo-18072.)
Thereload()
function has been moved fromimp
toimportlib
as part of theimp
module deprecation. (Contributed byBerker Peksag inbpo-18193.)
importlib.util
now has aMAGIC_NUMBER
attributeproviding access to the bytecode version number. This replaces theget_magic()
function in the deprecatedimp
module.(Contributed by Brett Cannon inbpo-18192.)
Newimportlib.util
functionscache_from_source()
andsource_from_cache()
replace the same-named functionsin the deprecatedimp
module. (Contributed by Brett Cannon inbpo-18194.)
Theimportlib
bootstrapNamespaceLoader
now conforms totheInspectLoader
ABC, which means thatrunpy
andpython-m
can now be used with namespace packages. (Contributedby Brett Cannon inbpo-18058.)
importlib.util
has a new functiondecode_source()
that decodes source from bytes using universal newline processing. This isuseful for implementingInspectLoader.get_source()
methods.
importlib.machinery.ExtensionFileLoader
now has aget_filename()
method. This wasinadvertently omitted in the original implementation. (Contributed by EricSnow inbpo-19152.)
inspect¶
Theinspect
module now offers a basiccommand line interface to quickly display source code and otherinformation for modules, classes and functions. (Contributed by Claudiu Popaand Nick Coghlan inbpo-18626.)
unwrap()
makes it easy to unravel wrapper function chainscreated byfunctools.wraps()
(and any other API that sets the__wrapped__
attribute on a wrapper function). (Contributed byDaniel Urban, Aaron Iles and Nick Coghlan inbpo-13266.)
As part of the implementation of the newenum
module, theinspect
module now has substantially better support for custom__dir__
methods and dynamic class attributes provided throughmetaclasses. (Contributed by Ethan Furman inbpo-18929 andbpo-19030.)
getfullargspec()
andgetargspec()
now use thesignature()
API. This allows them tosupport a much broader range of callables, including those with__signature__
attributes, those with metadata provided by argumentclinic,functools.partial()
objects and more. Note that, unlikesignature()
, these functions still ignore__wrapped__
attributes, and report the already bound first argument for bound methods,so it is still necessary to update your code to usesignature()
directly if those features are desired.(Contributed by Yury Selivanov inbpo-17481.)
signature()
now supports duck types of CPython functions,which adds support for functions compiled with Cython. (Contributedby Stefan Behnel and Yury Selivanov inbpo-17159.)
ipaddress¶
ipaddress
was added to the standard library in Python 3.3 as aprovisional API. With the release of Python 3.4, this qualificationhas been removed:ipaddress
is now considered a stable API, coveredby the normal standard library requirements to maintain backwardscompatibility.
A newis_global
property isTrue
ifan address is globally routeable. (Contributed by Peter Moody inbpo-17400.)
logging¶
TheTimedRotatingFileHandler
has a newatTimeparameter that can be used to specify the time of day when rollover shouldhappen. (Contributed by Ronald Oussoren inbpo-9556.)
SocketHandler
andDatagramHandler
now support Unix domain sockets (bysettingport toNone
). (Contributed by Vinay Sajip in commitce46195b56a9.)
fileConfig()
now accepts aconfigparser.RawConfigParser
subclass instance for thefnameparameter. This facilitates using a configuration file when loggingconfiguration is just a part of the overall application configuration, or wherethe application modifies the configuration before passing it tofileConfig()
. (Contributed by Vinay Sajip inbpo-16110.)
Logging configuration data received from a socket via thelogging.config.listen()
function can now be validated before beingprocessed by supplying a verification function as the argument to the newverify keyword argument. (Contributed by Vinay Sajip inbpo-15452.)
marshal¶
The defaultmarshal
version has been bumped to 3. The code implementingthe new version restores the Python2 behavior of recording only one copy ofinterned strings and preserving the interning on deserialization, and extendsthis "one copy" ability to any object type (including handling recursivereferences). This reduces both the size of.pyc
files and the amount ofmemory a module occupies in memory when it is loaded from a.pyc
(or.pyo
) file. (Contributed by Kristján Valur Jónsson inbpo-16475,with additional speedups by Antoine Pitrou inbpo-19219.)
mmap¶
mmap objects are nowweakly referenceable.(Contributed by Valerie Lambert inbpo-4885.)
multiprocessing¶
On Unix two newstart methods,spawn
andforkserver
, have been added for starting processes usingmultiprocessing
. These make the mixing of processes with threads morerobust, and thespawn
method matches the semantics that multiprocessing hasalways used on Windows. New functionget_all_start_methods()
reports all start methodsavailable on the platform,get_start_method()
reportsthe current start method, andset_start_method()
setsthe start method. (Contributed by Richard Oudkerk inbpo-8713.)
multiprocessing
also now has the concept of acontext
, whichdetermines how child processes are created. New functionget_context()
returns a context that uses a specifiedstart method. It has the same API as themultiprocessing
module itself,so you can use it to createPool
s and otherobjects that will operate within that context. This allows a framework and anapplication or different parts of the same application to use multiprocessingwithout interfering with each other. (Contributed by Richard Oudkerk inbpo-18999.)
Except when using the oldfork start method, child processes no longerinherit unneeded handles/file descriptors from their parents (part ofbpo-8713).
multiprocessing
now relies onrunpy
(which implements the-m
switch) to initialise__main__
appropriately in child processeswhen using thespawn
orforkserver
start methods. This resolves someedge cases where combining multiprocessing, the-m
command line switch,and explicit relative imports could cause obscure failures in childprocesses. (Contributed by Nick Coghlan inbpo-19946.)
operator¶
New functionlength_hint()
provides an implementation of thespecification for how the__length_hint__()
special method shouldbe used, as part of thePEP 424 formal specification of this languagefeature. (Contributed by Armin Ronacher inbpo-16148.)
There is now a pure-python version of theoperator
module available forreference and for use by alternate implementations of Python. (Contributed byZachary Ware inbpo-16694.)
os¶
There are new functions to get and set theinheritable flag of a file descriptor (os.get_inheritable()
,os.set_inheritable()
) or a Windows handle(os.get_handle_inheritable()
,os.set_handle_inheritable()
).
New functioncpu_count()
reports the number of CPUs available on theplatform on which Python is running (orNone
if the count can't bedetermined). Themultiprocessing.cpu_count()
function is now implementedin terms of this function). (Contributed by Trent Nelson, Yogesh Chaudhari,Victor Stinner, and Charles-François Natali inbpo-17914.)
os.path.samestat()
is now available on the Windows platform (and theos.path.samefile()
implementation is now shared between Unix andWindows). (Contributed by Brian Curtin inbpo-11939.)
os.path.ismount()
now recognizes volumes mounted below a driveroot on Windows. (Contributed by Tim Golden inbpo-9035.)
os.open()
supports two new flags on platforms that provide them,O_PATH
(un-opened file descriptor), andO_TMPFILE
(unnamed temporary file; as of 3.4.0 release available only on Linux systemswith a kernel version of 3.11 or newer that have uapi headers). (Contributedby Christian Heimes inbpo-18673 and Benjamin Peterson, respectively.)
pdb¶
pdb
has been enhanced to handle generators,yield
, andyieldfrom
in a more useful fashion. This is especially helpful whendebuggingasyncio
based programs. (Contributed by Andrew Svetlov andXavier de Gaye inbpo-16596.)
Theprint
command has been removed frompdb
, restoring access to thePythonprint()
function from the pdb command line. Python2'spdb
didnot have aprint
command; instead, enteringprint
executed theprint
statement. In Python3print
was mistakenly made an alias for thepdbp
command.p
, however, prints therepr
of its argument,not thestr
like the Python2print
command did. Worse, the Python3pdbprint
command shadowed the Python3print
function, making itinaccessible at thepdb
prompt. (Contributed by Connor Osborn inbpo-18764.)
pickle¶
pickle
now supports (but does not use by default) a new pickle protocol,protocol 4. This new protocol addresses a number of issues that were presentin previous protocols, such as the serialization of nested classes, very largestrings and containers, and classes whose__new__()
method takeskeyword-only arguments. It also provides some efficiency improvements.
也參考
- PEP 3154 -- Pickle 協定 4
由 Antoine Pitrou 撰寫 PEP、Alexandre Vassalotti 實作。
plistlib¶
plistlib
now has an API that is similar to the standard pattern forstdlib serialization protocols, with newload()
,dump()
,loads()
, anddumps()
functions. (The older API is now deprecated.) In addition to the alreadysupported XML plist format (FMT_XML
), it also now supportsthe binary plist format (FMT_BINARY
). (Contributed by RonaldOussoren and others inbpo-14455.)
poplib¶
Two new methods have been added topoplib
:capa()
,which returns the list of capabilities advertised by the POP server, andstls()
, which switches a clear-text POP3 session into anencrypted POP3 session if the POP server supports it. (Contributed by LorenzoCatucci inbpo-4473.)
pprint¶
Thepprint
module'sPrettyPrinter
class and itspformat()
, andpprint()
functions have a newoption,compact, that controls how the output is formatted. Currentlysettingcompact toTrue
means that sequences will be printed with as manysequence elements as will fit withinwidth on each (indented) line.(Contributed by Serhiy Storchaka inbpo-19132.)
Long strings are now wrapped using Python's normal line continuationsyntax. (Contributed by Antoine Pitrou inbpo-17150.)
pty¶
pty.spawn()
now returns the status value fromos.waitpid()
onthe child process, instead ofNone
. (Contributed by Gregory P. Smith.)
pydoc¶
Thepydoc
module is now based directly on theinspect.signature()
introspection API, allowing it to provide signature information for a widervariety of callable objects. This change also means that__wrapped__
attributes are now taken into account when displaying help information.(Contributed by Larry Hastings inbpo-19674.)
Thepydoc
module no longer displays theself
parameter foralready bound methods. Instead, it aims to always display the exact currentsignature of the supplied callable. (Contributed by Larry Hastings inbpo-20710.)
In addition to the changes that have been made topydoc
directly,its handling of custom__dir__
methods and various descriptorbehaviours has also been improved substantially by the underlying changes intheinspect
module.
As thehelp()
builtin is based onpydoc
, the above changes alsoaffect the behaviour ofhelp()
.
re¶
Newfullmatch()
function andregex.fullmatch()
method anchorthe pattern at both ends of the string to match. This provides a way to beexplicit about the goal of the match, which avoids a class of subtle bugs where$
characters get lost during code changes or the addition of alternativesto an existing regular expression. (Contributed by Matthew Barnett inbpo-16203.)
The repr ofregex objects now includes the patternand the flags; the repr ofmatch objects nowincludes the start, end, and the part of the string that matched. (Contributedby Hugo Lopes Tavares and Serhiy Storchaka inbpo-13592 andbpo-17087.)
resource¶
Newprlimit()
function, available on Linux platforms with akernel version of 2.6.36 or later and glibc of 2.13 or later, provides theability to query or set the resource limits for processes other than the onemaking the call. (Contributed by Christian Heimes inbpo-16595.)
On Linux kernel version 2.6.36 or later, there are also some newLinux specific constants:RLIMIT_MSGQUEUE
,RLIMIT_NICE
,RLIMIT_RTPRIO
,RLIMIT_RTTIME
, andRLIMIT_SIGPENDING
.(Contributed by Christian Heimes inbpo-19324.)
On FreeBSD version 9 and later, there some new FreeBSD specific constants:RLIMIT_SBSIZE
,RLIMIT_SWAP
, andRLIMIT_NPTS
. (Contributed by Claudiu Popa inbpo-19343.)
select¶
epoll
objects now support the context management protocol.When used in awith
statement, theclose()
method will be called automatically at the end of the block. (Contributedby Serhiy Storchaka inbpo-16488.)
devpoll
objects now havefileno()
andclose()
methods, as well as a new attributeclosed
. (Contributed by Victor Stinner inbpo-18794.)
shelve¶
Shelf
instances may now be used inwith
statements,and will be automatically closed at the end of thewith
block.(Contributed by Filip Gruszczyński inbpo-13896.)
shutil¶
copyfile()
now raises a specificError
subclass,SameFileError
, when the source and destination are the samefile, which allows an application to take appropriate action on this specificerror. (Contributed by Atsuo Ishimoto and Hynek Schlawack inbpo-1492704.)
smtpd¶
TheSMTPServer
andSMTPChannel
classes nowaccept amap keyword argument which, if specified, is passed in toasynchat.async_chat
as itsmap argument. This allows an applicationto avoid affecting the global socket map. (Contributed by Vinay Sajip inbpo-11959.)
smtplib¶
SMTPException
is now a subclass ofOSError
, which allowsboth socket level errors and SMTP protocol level errors to be caught in onetry/except statement by code that only cares whether or not an error occurred.(Contributed by Ned Jackson Lovely inbpo-2118.)
socket¶
The socket module now supports theCAN_BCM
protocol onplatforms that support it. (Contributed by Brian Thorne inbpo-15359.)
Socket objects have new methods to get or set theirinheritable flag,get_inheritable()
andset_inheritable()
.
Thesocket.AF_*
andsocket.SOCK_*
constants are now enumeration valuesusing the newenum
module. This allows meaningful names to be printedduring debugging, instead of integer "magic numbers".
TheAF_LINK
constant is now available on BSD and OSX.
inet_pton()
andinet_ntop()
are now supportedon Windows. (Contributed by Atsuo Ishimoto inbpo-7171.)
sqlite3¶
A new boolean parameter to theconnect()
function,uri, can beused to indicate that thedatabase parameter is auri
(see theSQLiteURI documentation). (Contributed by poq inbpo-13773.)
ssl¶
PROTOCOL_TLSv1_1
andPROTOCOL_TLSv1_2
(TLSv1.1 andTLSv1.2 support) have been added; support for these protocols is only available ifPython is linked with OpenSSL 1.0.1 or later. (Contributed by Michele Orrù andAntoine Pitrou inbpo-16692.)
New functioncreate_default_context()
provides a standard way toobtain anSSLContext
whose settings are intended to be areasonable balance between compatibility and security. These settings aremore stringent than the defaults provided by theSSLContext
constructor, and may be adjusted in the future, without prior deprecation, ifbest-practice security requirements change. The new recommended bestpractice for using stdlib libraries that support SSL is to usecreate_default_context()
to obtain anSSLContext
object, modify it if needed, and then pass it as thecontext argumentof the appropriate stdlib API. (Contributed by Christian Heimesinbpo-19689.)
SSLContext
methodload_verify_locations()
accepts a new optional argumentcadata, which can be used to provide PEM orDER encoded certificates directly via strings or bytes, respectively.(Contributed by Christian Heimes inbpo-18138.)
New functionget_default_verify_paths()
returnsa named tuple of the paths and environment variables that theset_default_verify_paths()
method uses to setOpenSSL's defaultcafile
andcapath
. This can be an aid indebugging default verification issues. (Contributed by Christian Heimesinbpo-18143.)
SSLContext
has a new method,cert_store_stats()
, that reports the number of loadedX.509
certs,X.509CA
certs, and certificate revocation lists(crl
s), as well as aget_ca_certs()
method thatreturns a list of the loadedCA
certificates. (Contributed by ChristianHeimes inbpo-18147.)
If OpenSSL 0.9.8 or later is available,SSLContext
has a newattributeverify_flags
that can be used to control thecertificate verification process by setting it to some combination of the newconstantsVERIFY_DEFAULT
,VERIFY_CRL_CHECK_LEAF
,VERIFY_CRL_CHECK_CHAIN
, orVERIFY_X509_STRICT
.OpenSSL does not do any CRL verification by default. (Contributed byChristien Heimes inbpo-8813.)
NewSSLContext
methodload_default_certs()
loads a set of default "certificate authority" (CA) certificates from defaultlocations, which vary according to the platform. It can be used to load bothTLS web server authentication certificates(purpose=
SERVER_AUTH
) for a client to use to verify aserver, and certificates for a server to use in verifying client certificates(purpose=
CLIENT_AUTH
). (Contributed by ChristianHeimes inbpo-19292.)
Two new windows-only functions,enum_certificates()
andenum_crls()
provide the ability to retrieve certificates,certificate information, and CRLs from the Windows cert store. (Contributedby Christian Heimes inbpo-17134.)
Support for server-side SNI (Server Name Indication) using the newssl.SSLContext.set_servername_callback()
method.(Contributed by Daniel Black inbpo-8109.)
The dictionary returned bySSLSocket.getpeercert()
contains additionalX509v3
extension items:crlDistributionPoints
,calIssuers
, andOCSP
URIs. (Contributed by Christian Heimes inbpo-18379.)
stat¶
Thestat
module is now backed by a C implementation in_stat
. A Cimplementation is required as most of the values aren't standardized andare platform-dependent. (Contributed by Christian Heimes inbpo-11016.)
The module supports newST_MODE
flags,S_IFDOOR
,S_IFPORT
, andS_IFWHT
. (Contributed byChristian Hiemes inbpo-11016.)
struct¶
New functioniter_unpack
and a newstruct.Struct.iter_unpack()
method on compiled formats provide streamedunpacking of a buffer containing repeated instances of a given format of data.(Contributed by Antoine Pitrou inbpo-17804.)
subprocess¶
check_output()
now accepts aninput argument that canbe used to provide the contents ofstdin
for the command that is run.(Contributed by Zack Weinberg inbpo-16624.)
getstatus()
andgetstatusoutput()
nowwork on Windows. This change was actually inadvertently made in 3.3.4.(Contributed by Tim Golden inbpo-10197.)
sunau¶
Thegetparams()
method now returns a namedtuple rather than aplain tuple. (Contributed by Claudiu Popa inbpo-18901.)
sunau.open()
now supports the context management protocol: when used in awith
block, theclose
method of the returned object will becalled automatically at the end of the block. (Contributed by Serhiy Storchakainbpo-18878.)
AU_write.setsampwidth()
now supports 24 bit samples, thus addingsupport for writing 24 sample using the module. (Contributed bySerhiy Storchaka inbpo-19261.)
Thewriteframesraw()
andwriteframes()
methods now accept anybytes-likeobject. (Contributed by Serhiy Storchaka inbpo-8311.)
sys¶
New functionsys.getallocatedblocks()
returns the current number ofblocks allocated by the interpreter. (In CPython with the default--with-pymalloc
setting, this is allocations made through thePyObject_Malloc()
API.) This can be useful for tracking memory leaks,especially if automated via a test suite. (Contributed by Antoine Pitrouinbpo-13390.)
When the Python interpreter starts ininteractive mode, it checks for an__interactivehook__
attributeon thesys
module. If the attribute exists, its value is called with noarguments just before interactive mode is started. The check is made after thePYTHONSTARTUP
file is read, so it can be set there. Thesite
modulesets it to a function that enables tabcompletion and history saving (in~/.python-history
) if the platformsupportsreadline
. If you do not want this (new) behavior, you canoverride it inPYTHONSTARTUP
,sitecustomize
, orusercustomize
by deleting this attribute fromsys
(or setting itto some other callable). (Contributed by Éric Araujo and Antoine Pitrou inbpo-5845.)
tarfile¶
Thetarfile
module now supports a simpleCommand-Line Interface whencalled as a script directly or via-m
. This can be used to create andextract tarfile archives. (Contributed by Berker Peksag inbpo-13477.)
textwrap¶
TheTextWrapper
class has two new attributes/constructorarguments:max_lines
, which limits the number oflines in the output, andplaceholder
, which is astring that will appear at the end of the output if it has been truncatedbecause ofmax_lines. Building on these capabilities, a new conveniencefunctionshorten()
collapses all of the whitespace in the inputto single spaces and produces a single line of a givenwidth that ends withtheplaceholder (by default,[...]
). (Contributed by Antoine Pitrou andSerhiy Storchaka inbpo-18585 andbpo-18725.)
threading¶
TheThread
object representing the main thread can beobtained from the newmain_thread()
function. In normalconditions this will be the thread from which the Python interpreter wasstarted. (Contributed by Andrew Svetlov inbpo-18882.)
traceback¶
A newtraceback.clear_frames()
function takes a traceback objectand clears the local variables in all of the frames it references,reducing the amount of memory consumed. (Contributed by Andrew Kuchling inbpo-1565525.)
types¶
A newDynamicClassAttribute()
descriptor provides a way to definean attribute that acts normally when looked up through an instance object, butwhich is routed to theclass__getattr__
when looked up through theclass. This allows one to have properties active on a class, and have virtualattributes on the class with the same name (seeEnum
for an example).(Contributed by Ethan Furman inbpo-19030.)
urllib¶
urllib.request
now supportsdata:
URLs via theDataHandler
class. (Contributed by Mathias Panzenböckinbpo-16423.)
The http method that will be used by aRequest
classcan now be specified by setting amethod
class attribute on the subclass. (Contributed by Jason R Coombs inbpo-18978.)
Request
objects are now reusable: if thefull_url
ordata
attributes are modified, all relevant internal properties are updated. Thismeans, for example, that it is now possible to use the sameRequest
object in more than oneOpenerDirector.open()
call with differentdata arguments, or tomodify aRequest
'surl
rather than recomputing itfrom scratch. There is also a newremove_header()
method that can be used to removeheaders from aRequest
. (Contributed by AlexeyKachayev inbpo-16464, Daniel Wozniak inbpo-17485, and Damien Brechtand Senthil Kumaran inbpo-17272.)
HTTPError
objects now have aheaders
attribute that provides access to theHTTP response headers associated with the error. (Contributed byBerker Peksag inbpo-15701.)
unittest¶
TheTestCase
class has a new method,subTest()
, that produces a context manager whosewith
block becomes a "sub-test". This context manager allows a testmethod to dynamically generate subtests by, say, calling thesubTest
context manager inside a loop. A single test method can thereby produce anindefinite number of separately identified and separately counted tests, all ofwhich will run even if one or more of them fail. For example:
classNumbersTest(unittest.TestCase):deftest_even(self):foriinrange(6):withself.subTest(i=i):self.assertEqual(i%2,0)
will result in six subtests, each identified in the unittest verbose outputwith a label consisting of the variable namei
and a particular value forthat variable (i=0
,i=1
, etc). SeeDistinguishing test iterations using subtests for the fullversion of this example. (Contributed by Antoine Pitrou inbpo-16997.)
unittest.main()
now accepts an iterable of test names fordefaultTest, where previously it only accepted a single test name as astring. (Contributed by Jyrki Pulliainen inbpo-15132.)
IfSkipTest
is raised during test discovery (that is, at themodule level in the test file), it is now reported as a skip instead of anerror. (Contributed by Zach Ware inbpo-16935.)
discover()
now sorts the discovered files to provideconsistent test ordering. (Contributed by Martin Melin and Jeff Ramnani inbpo-16709.)
TestSuite
now drops references to tests as soon as the testhas been run, if the test is successful. On Python interpreters that dogarbage collection, this allows the tests to be garbage collected if nothingelse is holding a reference to the test. It is possible to override thisbehavior by creating aTestSuite
subclass that defines acustom_removeTestAtIndex
method. (Contributed by Tom Wardill, MattMcClure, and Andrew Svetlov inbpo-11798.)
A new test assertion context-manager,assertLogs()
,will ensure that a given block of code emits a log message using thelogging
module. By default the message can come from any logger andhave a priority ofINFO
or higher, but both the logger name and analternative minimum logging level may be specified. The object returned by thecontext manager can be queried for theLogRecord
s and/orformatted messages that were logged. (Contributed by Antoine Pitrou inbpo-18937.)
Test discovery now works with namespace packages (Contributed by Claudiu Popainbpo-17457.)
unittest.mock
objects now inspect their specification signatures whenmatching calls, which means an argument can now be matched by either positionor name, instead of only by position. (Contributed by Antoine Pitrou inbpo-17015.)
mock_open()
objects now havereadline
andreadlines
methods. (Contributed by Toshio Kuratomi inbpo-17467.)
venv¶
venv
now includes activation scripts for thecsh
andfish
shells. (Contributed by Andrew Svetlov inbpo-15417.)
EnvBuilder
and thecreate()
convenience functiontake a new keyword argumentwith_pip, which defaults toFalse
, thatcontrols whether or notEnvBuilder
ensures thatpip
isinstalled in the virtual environment. (Contributed by Nick Coghlan inbpo-19552 as part of thePEP 453 implementation.)
wave¶
Thegetparams()
method now returns a namedtuple rather than aplain tuple. (Contributed by Claudiu Popa inbpo-17487.)
wave.open()
now supports the context management protocol. (Contributedby Claudiu Popa inbpo-17616.)
wave
can nowwrite output to unseekable files. (Contributed by David Jones, Guilherme Polo, and SerhiyStorchaka inbpo-5202.)
Thewriteframesraw()
andwriteframes()
methods now accept anybytes-likeobject. (Contributed by Serhiy Storchaka inbpo-8311.)
weakref¶
NewWeakMethod
class simulates weak references to boundmethods. (Contributed by Antoine Pitrou inbpo-14631.)
Newfinalize
class makes it possible to register a callbackto be invoked when an object is garbage collected, without needing tocarefully manage the lifecycle of the weak reference itself. (Contributed byRichard Oudkerk inbpo-15528.)
The callback, if any, associated with aref
is nowexposed via the__callback__
attribute. (Contributedby Mark Dickinson inbpo-17643.)
xml.etree¶
A new parser,XMLPullParser
, allows anon-blocking applications to parse XML documents. An example can beseen atPull API for non-blocking parsing. (Contributed by AntoinePitrou inbpo-17741.)
Thexml.etree.ElementTree
tostring()
andtostringlist()
functions, and theElementTree
write()
method, now have ashort_empty_elementskeyword-only parameterproviding control over whether elements with no content are written inabbreviated (<tag/>
) or expanded (<tag></tag>
) form. (Contributed byAriel Poliak and Serhiy Storchaka inbpo-14377.)
zipfile¶
Thewritepy()
method of thePyZipFile
class has a newfilterfunc option that can beused to control which directories and files are added to the archive. Forexample, this could be used to exclude test files from the archive.(Contributed by Christian Tismer inbpo-19274.)
TheallowZip64 parameter toZipFile
andPyZipfile
is nowTrue
by default. (Contributed byWilliam Mallard inbpo-17201.)
CPython 實作變更¶
PEP 445: Customization of CPython Memory Allocators¶
PEP 445 adds new C level interfaces to customize memory allocation inthe CPython interpreter.
也參考
- PEP 445 -- Add new APIs to customize Python memory allocators
由 Victor Stinner 撰寫 PEP 與實作。
PEP 442: Safe Object Finalization¶
PEP 442 removes the current limitations and quirks of object finalizationin CPython. With it, objects with__del__()
methods, as well asgenerators withfinally
clauses, can be finalized when they arepart of a reference cycle.
As part of this change, module globals are no longer forcibly set toNone
during interpreter shutdown in most cases, instead relyingon the normal operation of the cyclic garbage collector. This avoids awhole class of interpreter-shutdown-time errors, usually involving__del__
methods, that have plagued Python since the cyclic GCwas first introduced.
也參考
- PEP 442 -- 安全的物件最終化
由 Antoine Pitrou 撰寫 PEP 與實作。
PEP 456: Secure and Interchangeable Hash Algorithm¶
PEP 456 follows up on earlier security fix work done on Python's hashalgorithm to address certain DOS attacks to which public facing APIs backed bydictionary lookups may be subject. (Seebpo-14621 for the start of thecurrent round of improvements.) The PEP unifies CPython's hash code to make iteasier for a packager to substitute a different hash algorithm, and switchesPython's default implementation to a SipHash implementation on platforms thathave a 64 bit data type. Any performance differences in comparison with theolder FNV algorithm are trivial.
The PEP adds additional fields to thesys.hash_info
named tuple todescribe the hash algorithm in use by the currently executing binary. Otherwise,the PEP does not alter any existing CPython APIs.
PEP 436: Argument Clinic¶
"Argument Clinic" (PEP 436) is now part of the CPython build processand can be used to simplify the process of defining and maintainingaccurate signatures for builtins and standard library extension modulesimplemented in C.
Some standard library extension modules have been converted to use ArgumentClinic in Python 3.4, andpydoc
andinspect
have been updatedaccordingly.
It is expected that signature metadata for programmatic introspection willbe added to additional callables implemented in C as part of Python 3.4maintenance releases.
備註
The Argument Clinic PEP is not fully up to date with the state of theimplementation. This has been deemed acceptable by the release managerand core development team in this case, as Argument Clinic will notbe made available as a public API for third party use in Python 3.4.
也參考
- PEP 436 -- The Argument Clinic DSL
由 Larry Hastings 撰寫 PEP 與實作。
其他建置和 C API 變更¶
The new
PyType_GetSlot()
function has been added to the stable ABI,allowing retrieval of function pointers from named type slots when usingthe limited API. (Contributed by Martin von Löwis inbpo-17162.)The new
Py_SetStandardStreamEncoding()
pre-initialization APIallows applications embedding the CPython interpreter to reliably forcea particular encoding and error handler for the standard streams.(Contributed by Bastien Montagne and Nick Coghlan inbpo-16129.)Most Python C APIs that don't mutate string arguments are now correctlymarked as accepting
constchar*
rather thanchar*
. (Contributedby Serhiy Storchaka inbpo-1772673.)A new shell version of
python-config
can be used even when a pythoninterpreter is not available (for example, in cross compilation scenarios).PyUnicode_FromFormat()
now supports width and precisionspecifications for%s
,%A
,%U
,%V
,%S
, and%R
.(Contributed by Ysj Ray and Victor Stinner inbpo-7330.)New function
PyStructSequence_InitType2()
supplements theexistingPyStructSequence_InitType()
function. The differenceis that it returns0
on success and-1
on failure.The CPython source can now be compiled using the address sanity checkingfeatures of recent versions of GCC and clang: the false alarms in the smallobject allocator have been silenced. (Contributed by Dhiru Kholia inbpo-18596.)
The Windows build now usesAddress Space Layout Randomization andData Execution Prevention. (Contributed byChristian Heimes inbpo-16632.)
New function
PyObject_LengthHint()
is the C API equivalentofoperator.length_hint()
. (Contributed by Armin Ronacher inbpo-16148.)
其他改進¶
Thepython command has a newoption,
-I
, which causes it to run in "isolated mode",which means thatsys.path
contains neither the script's directory northe user'ssite-packages
directory, and allPYTHON*
environmentvariables are ignored (it implies both-s
and-E
). Otherrestrictions may also be applied in the future, with the goal being toisolate the execution of a script from the user's environment. This isappropriate, for example, when Python is used to run a system script. Onmost POSIX systems it can and should be used in the#!
line of systemscripts. (Contributed by Christian Heimes inbpo-16499.)Tab-completion is now enabled by default in the interactive interpreteron systems that support
readline
. History is also enabled by default,and is written to (and read from) the file~/.python-history
.(Contributed by Antoine Pitrou and Éric Araujo inbpo-5845.)Invoking the Python interpreter with
--version
now outputs the version tostandard output instead of standard error (bpo-18338). Similar changeswere made toargparse
(bpo-18920) and other modules that havescript-like invocation capabilities (bpo-18922).The CPython Windows installer now adds
.py
to thePATHEXT
variable when extensions are registered, allowing users to run a pythonscript at the windows command prompt by just typing its name without the.py
extension. (Contributed by Paul Moore inbpo-18569.)A new
make
targetcoverage-reportwill build python, run the test suite, and generate an HTML coverage reportfor the C codebase usinggcov
andlcov.python 迴歸測試套裝 的
-R
選項現在也可以透過使用sys.getallocatedblocks()
來檢查記憶體配置洩漏。(由 Antoine Pitrou 在bpo-13390 貢獻。)python-m
現在可以使用於命名空間套件。The
stat
module is now implemented in C, which means it gets thevalues for its constants from the C header files, instead of having thevalues hard-coded in the python module as was previously the case.Loading multiple python modules from a single OS module (
.so
,.dll
)now works correctly (previously it silently returned the first pythonmodule in the file). (Contributed by Václav Šmilauer inbpo-16421.)A new opcode,
LOAD_CLASSDEREF
, has been added to fix a bug in theloading of free variables in class bodies that could be triggered by certainuses of__prepare__. (Contributed by Benjamin Peterson inbpo-17853.)A number of MemoryError-related crashes were identified and fixed by VictorStinner using hisPEP 445-based
pyfailmalloc
tool (bpo-18408,bpo-18520).The
pyvenv
command now accepts a--copies
optionto use copies rather than symlinks even on systems where symlinks are thedefault. (Contributed by Vinay Sajip inbpo-18807.)The
pyvenv
command also accepts a--without-pip
option to suppress the otherwise-automatic bootstrapping of pip intothe virtual environment. (Contributed by Nick Coghlan inbpo-19552as part of thePEP 453 implementation.)The encoding name is now optional in the value set for the
PYTHONIOENCODING
environment variable. This makes it possible toset just the error handler, without changing the default encoding.(Contributed by Serhiy Storchaka inbpo-18818.)The
bz2
,lzma
, andgzip
moduleopen
functions nowsupportx
(exclusive creation) mode. (Contributed by Tim Heaney andVajrasky Kok inbpo-19201,bpo-19222, andbpo-19223.)
顯著最佳化¶
The UTF-32 decoder is now 3x to 4x faster. (Contributed by Serhiy Storchakainbpo-14625.)
The cost of hash collisions for sets is now reduced. Each hash tableprobe now checks a series of consecutive, adjacent key/hash pairs beforecontinuing to make random probes through the hash table. This exploitscache locality to make collision resolution less expensive.The collision resolution scheme can be described as a hybrid of linearprobing and open addressing. The number of additional linear probesdefaults to nine. This can be changed at compile-time by definingLINEAR_PROBES to be any value. Set LINEAR_PROBES=0 to turn-offlinear probing entirely. (Contributed by Raymond Hettinger inbpo-18771.)
The interpreter starts about 30% faster. A couple of measures lead to thespeedup. The interpreter loads fewer modules on startup, e.g. the
re
,collections
andlocale
modules and their dependencies are nolonger imported by default. The marshal module has been improved to loadcompiled Python code faster. (Contributed by Antoine Pitrou, ChristianHeimes and Victor Stinner inbpo-19219,bpo-19218,bpo-19209,bpo-19205 andbpo-9548.)bz2.BZ2File
is now as fast or faster than the Python2 version formost cases.lzma.LZMAFile
has also been optimized. (Contributed bySerhiy Storchaka and Nadeem Vawda inbpo-16034.)random.getrandbits()
is 20%-40% faster for small integers (the mostcommon use case). (Contributed by Serhiy Storchaka inbpo-16674.)By taking advantage of the new storage format for strings, pickling ofstrings is now significantly faster. (Contributed by Victor Stinner andAntoine Pitrou inbpo-15596.)
A performance issue in
io.FileIO.readall()
has been solved. Thisparticularly affects Windows, and significantly speeds up the case of pipingsignificant amounts of data throughsubprocess
. (Contributedby Richard Oudkerk inbpo-15758.)html.escape()
is now 10x faster. (Contributed by Matt Bryant inbpo-18020.)On Windows, the native
VirtualAlloc
is now used instead of the CRTmalloc
inobmalloc
. Artificial benchmarks show about a 3% memorysavings.os.urandom()
now uses a lazily opened persistent file descriptorso as to avoid using many file descriptors when run in parallel frommultiple threads. (Contributed by Antoine Pitrou inbpo-18756.)
已棄用¶
This section covers various APIs and other features that have been deprecatedin Python 3.4, and will be removed in Python 3.5 or later. In most (but notall) cases, using the deprecated APIs will produce aDeprecationWarning
when the interpreter is run with deprecation warnings enabled (for example, byusing-Wd
).
已棄用的 Python API¶
As mentioned inPEP 451:用於引入系統的 ModuleSpec 型別, a number of
importlib
methods and functions are deprecated:importlib.find_loader()
isreplaced byimportlib.util.find_spec()
;importlib.machinery.PathFinder.find_module()
is replaced byimportlib.machinery.PathFinder.find_spec()
;importlib.abc.MetaPathFinder.find_module()
is replaced byimportlib.abc.MetaPathFinder.find_spec()
;importlib.abc.PathEntryFinder.find_loader()
andfind_module()
are replaced byimportlib.abc.PathEntryFinder.find_spec()
; all of thexxxLoader
ABCload_module
methods (importlib.abc.Loader.load_module()
,importlib.abc.InspectLoader.load_module()
,importlib.abc.FileLoader.load_module()
,importlib.abc.SourceLoader.load_module()
) should no longer beimplemented, instead loaders should implement anexec_module
method(importlib.abc.Loader.exec_module()
,importlib.abc.InspectLoader.exec_module()
importlib.abc.SourceLoader.exec_module()
) and let the import systemtake care of the rest; andimportlib.abc.Loader.module_repr()
,importlib.util.module_for_loader()
,importlib.util.set_loader()
,andimportlib.util.set_package()
are no longer needed because theirfunctions are now handled automatically by the import system.The
imp
module is pending deprecation. To keep compatibility withPython 2/3 code bases, the module's removal is currently not scheduled.The
formatter
module is pending deprecation and is slated for removalin Python 3.6.MD5
as the defaultdigestmod for thehmac.new()
function isdeprecated. Python 3.6 will require an explicit digest name or constructor asdigestmod argument.The internal
Netrc
class in theftplib
module has been documentedas deprecated in its docstring for quite some time. It now emits aDeprecationWarning
and will be removed completely in Python 3.5.The undocumentedendtime argument to
subprocess.Popen.wait()
shouldnot have been exposed and is hopefully not in use; it is deprecated andwill mostly likely be removed in Python 3.5.Thestrict argument of
HTMLParser
is deprecated.The
plistlib
readPlist()
,writePlist()
,readPlistFromBytes()
, andwritePlistToBytes()
functions are deprecated in favor of thecorresponding new functionsload()
,dump()
,loads()
, anddumps()
.Data()
is deprecated in favor of just using thebytes
constructor.The
sysconfig
keySO
is deprecated, it has been replaced byEXT_SUFFIX
.The
U
mode accepted by variousopen
functions is deprecated.In Python3 it does not do anything useful, and should be replaced byappropriate uses ofio.TextIOWrapper
(if needed) and itsnewlineargument.Theparser argument of
xml.etree.ElementTree.iterparse()
hasbeen deprecated, as has thehtml argument ofXMLParser()
. To prepare for the removal of thelatter, all arguments toXMLParser
should be passed by keyword.
已棄用功能¶
RunningIDLE --- Python 編輯器與 shell with the
-n
flag (no subprocess) is deprecated.However, the feature will not be removed untilbpo-18823 is resolved.The site module adding a "site-python" directory to sys.path, if itexists, is deprecated (bpo-19375).
已移除¶
不再支援的作業系統¶
Support for the following operating systems has been removed from the sourceand build tools:
API 與功能的移除¶
The following obsolete and previously deprecated APIs and features have beenremoved:
The unmaintained
Misc/TextMate
andMisc/vim
directories have beenremoved (see thedevguidefor suggestions on what to use instead).The
SO
makefile macro is removed (it was replaced by theSHLIB_SUFFIX
andEXT_SUFFIX
macros) (bpo-16754).The
PyThreadState.tick_counter
field has been removed; its value hasbeen meaningless since Python 3.2, when the "new GIL" was introduced(bpo-19199).PyLoader
andPyPycLoader
have been removed fromimportlib
.(Contributed by Taras Lyapun inbpo-15641.)Thestrict argument to
HTTPConnection
andHTTPSConnection
has been removed. HTTP 0.9-style"Simple Responses" are no longer supported.The deprecated
urllib.request.Request
getter and setter methodsadd_data
,has_data
,get_data
,get_type
,get_host
,get_selector
,set_proxy
,get_origin_req_host
, andis_unverifiable
have been removed (use direct attribute access instead).Support for loading the deprecated
TYPE_INT64
has been removed frommarshal
. (Contributed by Dan Riti inbpo-15480.)inspect.Signature
: positional-only parameters are now requiredto have a valid name.object.__format__()
no longer accepts non-empty format strings, it nowraises aTypeError
instead. Using a non-empty string has beendeprecated since Python 3.2. This change has been made to prevent asituation where previously working (but incorrect) code would start failingif an object gained a __format__ method, which means that your code may nowraise aTypeError
if you are using an's'
format code with objectsthat do not have a __format__ method that handles it. Seebpo-7994 forbackground.difflib.SequenceMatcher.isbjunk()
anddifflib.SequenceMatcher.isbpopular()
were deprecated in 3.2, and havenow been removed: usexinsm.bjunk
andxinsm.bpopular
, wheresm is aSequenceMatcher
object(bpo-13248).
程式碼的清除¶
The unused and undocumented internal
Scanner
class has been removed fromthepydoc
module.The private and effectively unused
_gestalt
module has been removed,along with the privateplatform
functions_mac_ver_lookup
,_mac_ver_gstalt
, and_bcd2str
, which would only have ever been calledon badly broken OSX systems (seebpo-18393).The hardcoded copies of certain
stat
constants that were included inthetarfile
module namespace have been removed.
移植至 Python 3.4¶
This section lists previously described changes and other bugfixesthat may require changes to your code.
Changes in 'python' Command Behavior¶
In a posix shell, setting the
PATH
environment variable toan empty value is equivalent to not setting it at all. However, settingPYTHONPATH
to an empty value wasnot equivalent to not setting itat all: settingPYTHONPATH
to an empty value was equivalent tosetting it to.
, which leads to confusion when reasoning by analogy tohowPATH
works. The behavior now conforms to the posix conventionforPATH
.The [X refs, Y blocks] output of a debug (
--with-pydebug
) build of theCPython interpreter is now off by default. It can be re-enabled using the-Xshowrefcount
option. (Contributed by Ezio Melotti inbpo-17323.)The python command and most stdlib scripts (as well as
argparse
) nowoutput--version
information tostdout
instead ofstderr
(forissue list see其他改進 above).
Python API 的變更¶
The ABCs defined in
importlib.abc
now either raise the appropriateexception or return a default value instead of raisingNotImplementedError
blindly. This will only affect code callingsuper()
and falling through all the way to the ABCs. For compatibility,catch bothNotImplementedError
or the appropriate exception as needed.The module type now initializes the
__package__
and__loader__
attributes toNone
by default. To determine if these attributes were setin a backwards-compatible fashion, use e.g.getattr(module,'__loader__',None)isnotNone
. (bpo-17115.)importlib.util.module_for_loader()
now sets__loader__
and__package__
unconditionally to properly support reloading. If this is notdesired then you will need to set these attributes manually. You can useimportlib.util.module_to_load()
for module management.Import now resets relevant attributes (e.g.
__name__
,__loader__
,__package__
,__file__
,__cached__
) unconditionally when reloading.Note that this restores a pre-3.3 behavior in that it means a module isre-found when re-loaded (bpo-19413).Frozen packages no longer set
__path__
to a list containing the packagename, they now set it to an empty list. The previous behavior could causethe import system to do the wrong thing on submodule imports if there wasalso a directory with the same name as the frozen package. The correct wayto determine if a module is a package or not is to usehasattr(module,'__path__')
(bpo-18065).Frozen modules no longer define a
__file__
attribute. It's semanticallyincorrect for frozen modules to set the attribute as they are not loaded fromany explicit location. If you must know that a module comes from frozen codethen you can see if the module's__spec__.location
is set to'frozen'
,check if the loader is a subclass ofimportlib.machinery.FrozenImporter
,or if Python 2 compatibility is necessary you can useimp.is_frozen()
.py_compile.compile()
now raisesFileExistsError
if the file pathit would write to is a symlink or a non-regular file. This is to act as awarning that import will overwrite those files with a regular file regardlessof what type of file path they were originally.importlib.abc.SourceLoader.get_source()
no longer raisesImportError
when the source code being loaded triggers aSyntaxError
orUnicodeDecodeError
. AsImportError
ismeant to be raised only when source code cannot be found but it should, it wasfelt to be over-reaching/overloading of that meaning when the source code isfound but improperly structured. If you were catching ImportError before andwish to continue to ignore syntax or decoding issues, catch all threeexceptions now.functools.update_wrapper()
andfunctools.wraps()
now correctlyset the__wrapped__
attribute to the function being wrapped, even ifthat function also had its__wrapped__
attribute set. This means__wrapped__
attributes now correctly link a stack of decoratedfunctions rather than every__wrapped__
attribute in the chainreferring to the innermost function. Introspection libraries thatassumed the previous behaviour was intentional can useinspect.unwrap()
to access the first function in the chain that hasno__wrapped__
attribute.inspect.getfullargspec()
has been reimplemented on top ofinspect.signature()
and hence handles a much wider variety of callableobjects than it did in the past. It is expected that additional builtin andextension module callables will gain signature metadata over the course ofthe Python 3.4 series. Code that assumes thatinspect.getfullargspec()
will fail on non-Python callables may needto be adjusted accordingly.importlib.machinery.PathFinder
now passes on the current workingdirectory to objects insys.path_hooks
for the empty string. Thisresults insys.path_importer_cache
never containing''
, thusiterating throughsys.path_importer_cache
based onsys.path
will not find all keys. A module's__file__
when imported in the currentworking directory will also now have an absolute path, including when using-m
with the interpreter (except for__main__.__file__
when a scripthas been executed directly using a relative path) (Contributed by BrettCannon inbpo-18416). is specified on the command-line)(bpo-18416).The removal of thestrict argument to
HTTPConnection
andHTTPSConnection
changes the meaning of theremaining arguments if you are specifying them positionally rather than bykeyword. If you've been paying attention to deprecation warnings your codeshould already be specifying any additional arguments via keywords.Strings between
from__future__import...
statements nowalways raiseaSyntaxError
. Previously if there was no leading docstring, aninterstitial string would sometimes be ignored. This brings CPython intocompliance with the language spec; Jython and PyPy already were.(bpo-17434).ssl.SSLSocket.getpeercert()
andssl.SSLSocket.do_handshake()
now raise anOSError
withENOTCONN
when theSSLSocket
is notconnected, instead of the previous behavior of raising anAttributeError
. In addition,getpeercert()
will raise aValueError
if the handshake has not yet been done.base64.b32decode()
now raises abinascii.Error
when theinput string contains non-b32-alphabet characters, instead of aTypeError
. This particularTypeError
was missed when the otherTypeError
s were converted. (Contributed by Serhiy Storchaka inbpo-18011.) Note: this change was also inadvertently applied in Python3.3.3.The
file
attribute is now automatically closed whenthe creatingcgi.FieldStorage
instance is garbage collected. If youwere pulling the file object out separately from thecgi.FieldStorage
instance and not keeping the instance alive, then you should either store theentirecgi.FieldStorage
instance or read the contents of the filebefore thecgi.FieldStorage
instance is garbage collected.Calling
read
orwrite
on a closed SSL socket now raises aninformativeValueError
rather than the previous more mysteriousAttributeError
(bpo-9177).slice.indices()
no longer produces anOverflowError
for hugevalues. As a consequence of this fix,slice.indices()
now raises aValueError
if given a negative length; previously it returned nonsensevalues (bpo-14794).The
complex
constructor, unlike thecmath
functions, wasincorrectly acceptingfloat
values if an object's__complex__
special method returned one. This now raises aTypeError
.(bpo-16290.)The
int
constructor in 3.2 and 3.3 erroneously acceptsfloat
values for thebase parameter. It is unlikely anyone was doing this, butif so, it will now raise aTypeError
(bpo-16772).Defaults for keyword-only arguments are now evaluatedafter defaults forregular keyword arguments, instead of before. Hopefully no one wrote anycode that depends on the previous buggy behavior (bpo-16967).
Stale thread states are now cleared after
fork()
. This may causesome system resources to be released that previously were incorrectly keptperpetually alive (for example, database connections kept in thread-localstorage). (bpo-17094.)Parameter names in
__annotations__
dicts are now mangled properly,similarly to__kwdefaults__
.(Contributed by Yury Selivanov inbpo-20625.)hashlib.hash.name
now always returns the identifier in lower case.Previously some builtin hashes had uppercase names, but now that it is aformal public interface the naming has been made consistent (bpo-18532).Because
unittest.TestSuite
now drops references to tests after theyare run, test harnesses that reuse aTestSuite
to re-runa set of tests may fail. Test suites should not be re-used in this fashionsince it means state is retained between test runs, breaking the testisolation thatunittest
is designed to provide. However, if the lackof isolation is considered acceptable, the old behavior can be restored bycreating aTestSuite
subclass that defines a_removeTestAtIndex
method that does nothing (seeTestSuite.__iter__()
) (bpo-11798).unittest
now usesargparse
for command line parsing. There arecertain invalid command forms that used to work that are no longer allowed;in theory this should not cause backward compatibility issues since thedisallowed command forms didn't make any sense and are unlikely to be in use.The
re.split()
,re.findall()
, andre.sub()
functions, andthegroup()
andgroups()
methods ofmatch
objects now always return abytes object when the stringto be matched is abytes-like object. Previously the return typematched the input type, so if your code was depending on the return valuebeing, say, abytearray
, you will need to change your code.audioop
functions now raise an error immediately if passed stringinput, instead of failing randomly later on (bpo-16685).The newconvert_charrefs argument to
HTMLParser
currently defaults toFalse
for backward compatibility, but willeventually be changed to default toTrue
. It is recommended that you addthis keyword, with the appropriate value, to anyHTMLParser
calls in your code (bpo-13633).Since thedigestmod argument to the
hmac.new()
function will in thefuture have no default, all calls tohmac.new()
should be changed toexplicitly specify adigestmod (bpo-17276).Calling
sysconfig.get_config_var()
with theSO
key, or lookingSO
up in the results of a call tosysconfig.get_config_vars()
is deprecated. This key should be replaced byEXT_SUFFIX
orSHLIB_SUFFIX
, depending on the context (bpo-19555).Any calls to
open
functions that specifyU
should be modified.U
is ineffective in Python3 and will eventually raise an error if used.Depending on the function, the equivalent of its old Python2 behavior can beachieved using either anewline argument, or if necessary by wrapping thestream inTextIOWrapper
to use itsnewline argument(bpo-15204).If you use
pyvenv
in a script and desire that pipnot be installed, you must add--without-pip
to your commandinvocation.The default behavior of
json.dump()
andjson.dumps()
whenan indent is specified has changed: it no longer produces trailingspaces after the item separating commas at the ends of lines. Thiswill matter only if you have tests that are doing white-space-sensitivecomparisons of such output (bpo-16333).doctest
now looks for doctests in extension module__doc__
strings, so if your doctest test discovery includes extension modules thathave things that look like doctests in them you may see test failures you'venever seen before when running your tests (bpo-3158).The
collections.abc
module has been slightly refactored aspart of the Python startup improvements. As a consequence of this, it is nolonger the case that importingcollections
automatically importscollections.abc
. If your program depended on the (undocumented)implicit import, you will need to add an explicitimportcollections.abc
(bpo-20784).
C API 中的改動¶
PyEval_EvalFrameEx()
,PyObject_Repr()
, andPyObject_Str()
, along with some other internal C APIs, now includea debugging assertion that ensures they are not used in situations wherethey may silently discard a currently active exception. In cases wherediscarding the active exception is expected and desired (for example,because it has already been saved locally withPyErr_Fetch()
oris being deliberately replaced with a different exception), an explicitPyErr_Clear()
call will be needed to avoid triggering theassertion when invoking these operations (directly or indirectly) andrunning against a version of Python that is compiled with assertionsenabled.PyErr_SetImportError()
now setsTypeError
when itsmsgargument is not set. Previously onlyNULL
was returned with no exceptionset.The result of the
PyOS_ReadlineFunctionPointer
callback mustnow be a string allocated byPyMem_RawMalloc()
orPyMem_RawRealloc()
, orNULL
if an error occurred, instead of astring allocated byPyMem_Malloc()
orPyMem_Realloc()
(bpo-16742)PyThread_set_key_value()
now always set the value. In Python3.3, the function did nothing if the key already exists (if the currentvalue is a non-NULL
pointer).The
f_tstate
(thread state) field of thePyFrameObject
structure has been removed to fix a bug: seebpo-14432 for therationale.
3.4.3 中的變更¶
PEP 476: Enabling certificate verification by default for stdlib http clients¶
http.client
and modules which use it, such asurllib.request
andxmlrpc.client
, will now verify that the server presents a certificatewhich is signed by a CA in the platform trust store and whose hostname matchesthe hostname being requested by default, significantly improving security formany applications.
For applications which require the old previous behavior, they can pass analternate context:
importurllib.requestimportssl# This disables all verificationcontext=ssl._create_unverified_context()# This allows using a specific certificate for the host, which doesn't need# to be in the trust storecontext=ssl.create_default_context(cafile="/path/to/file.crt")urllib.request.urlopen("https://invalid-cert",context=context)