- http
- html
- imaplib
- inspect
- io
- itertools
- logging
- math
- mmap
- multiprocessing
- nntplib
- os
- pdb
- pickle
- pydoc
- re
- sched
- select
- shlex
- shutil
- signal
- smtpd
- smtplib
- socket
- socketserver
- sqlite3
- ssl
- stat
- struct
- subprocess
- sys
- tarfile
- tempfile
- textwrap
- threading
- time
- types
- unittest
- urllib
- webbrowser
- xml.etree.ElementTree
- zlib
- 性能优化
- 构建和 C API 的改变
- 弃用
- 移植到 Python 3.3
http
http.server.BaseHTTPRequestHandler
now buffers the headers and writes them all at once when end_headers()
is called. A new method flush_headers()
can be used to directly manage when the accumulated headers are sent. (Contributed by Andrew Schaaf in bpo-3709 [https://bugs.python.org/issue?@action=redirect&bpo=3709].) http.server
now produces valid HTML 4.01 strict
output. (Contributed by Ezio Melotti in bpo-13295 [https://bugs.python.org/issue?@action=redirect&bpo=13295].) http.client.HTTPResponse
now has a readinto()
method, which means it can be used as an io.RawIOBase
class. (Contributed by John Kuhn in bpo-13464 [https://bugs.python.org/issue?@action=redirect&bpo=13464].)
html
html.parser.HTMLParser
is now able to parse broken markup without raising errors, therefore the strict argument of the constructor and the HTMLParseError
exception are now deprecated. The ability to parse broken markup is the result of a number of bug fixes that are also available on the latest bug fix releases of Python 2.7/3.2. (Contributed by Ezio Melotti in bpo-15114 [https://bugs.python.org/issue?@action=redirect&bpo=15114], and bpo-14538 [https://bugs.python.org/issue?@action=redirect&bpo=14538], bpo-13993 [https://bugs.python.org/issue?@action=redirect&bpo=13993], bpo-13960 [https://bugs.python.org/issue?@action=redirect&bpo=13960], bpo-13358 [https://bugs.python.org/issue?@action=redirect&bpo=13358], bpo-1745761 [https://bugs.python.org/issue?@action=redirect&bpo=1745761], bpo-755670 [https://bugs.python.org/issue?@action=redirect&bpo=755670], bpo-13357 [https://bugs.python.org/issue?@action=redirect&bpo=13357], bpo-12629 [https://bugs.python.org/issue?@action=redirect&bpo=12629], bpo-1200313 [https://bugs.python.org/issue?@action=redirect&bpo=1200313], bpo-670664 [https://bugs.python.org/issue?@action=redirect&bpo=670664], bpo-13273 [https://bugs.python.org/issue?@action=redirect&bpo=13273], bpo-12888 [https://bugs.python.org/issue?@action=redirect&bpo=12888], bpo-7311 [https://bugs.python.org/issue?@action=redirect&bpo=7311].) A new html5
dictionary that maps HTML5 named character references to the equivalent Unicode character(s) (e.g. html5['gt;'] ==
'>'
) has been added to the html.entities
module. The dictionary is now also used by HTMLParser
. (Contributed by Ezio Melotti in bpo-11113 [https://bugs.python.org/issue?@action=redirect&bpo=11113] and bpo-15156 [https://bugs.python.org/issue?@action=redirect&bpo=15156].)
imaplib
The IMAP4_SSL
constructor now accepts an SSLContext parameter to control parameters of the secure channel.
(由 Sijin Joseph 在 bpo-8808 [https://bugs.python.org/issue?@action=redirect&bpo=8808] 中贡献。)
inspect
A new getclosurevars()
function has been added. This function reports the current binding of all names referenced from the function body and where those names were resolved, making it easier to verify correct internal state when testing code that relies on stateful closures.
(由 Meador Inge 和 Nick Coghlan 在 bpo-13062 [https://bugs.python.org/issue?@action=redirect&bpo=13062] 中贡献。)
A new getgeneratorlocals()
function has been added. This function reports the current binding of local variables in the generator's stack frame, making it easier to verify correct internal state when testing generators.
(由 Meador Inge 在 bpo-15153 [https://bugs.python.org/issue?@action=redirect&bpo=15153] 中贡献。)
io
The open()
function has a new 'x'
mode that can be used to exclusively create a new file, and raise a FileExistsError
if the file already exists. It is based on the C11 'x' mode to fopen().
(由 David Townshend 在 bpo-12760 [https://bugs.python.org/issue?@action=redirect&bpo=12760] 中贡献。)
The constructor of the TextIOWrapper
class has a new write_through optional argument. If write_through is True
, calls to write()
are guaranteed not to be buffered: any data written on the TextIOWrapper
object is immediately handled to its underlying binary buffer.
itertools
accumulate()
now takes an optional func
argument for providing a user-supplied binary function.
logging
The basicConfig()
function now supports an optional handlers
argument taking an iterable of handlers to be added to the root logger.
A class level attribute append_nul
has been added to SysLogHandler
to allow control of the appending of the NUL
(\000
) byte to syslog records, since for some daemons it is required while for others it is passed through to the log.
math
The math
module has a new function, log2()
, which returns the base-2 logarithm of x.
(Written by Mark Dickinson in bpo-11888 [https://bugs.python.org/issue?@action=redirect&bpo=11888].)
mmap
The read()
method is now more compatible with other file-like objects: if the argument is omitted or specified as None
, it returns the bytes from the current file position to the end of the mapping. (Contributed by Petri Lehtinen in bpo-12021 [https://bugs.python.org/issue?@action=redirect&bpo=12021].)
multiprocessing
The new multiprocessing.connection.wait()
function allows polling multiple objects (such as connections, sockets and pipes) with a timeout. (Contributed by Richard Oudkerk in bpo-12328 [https://bugs.python.org/issue?@action=redirect&bpo=12328].) multiprocessing.Connection
objects can now be transferred over multiprocessing connections. (Contributed by Richard Oudkerk in bpo-4892 [https://bugs.python.org/issue?@action=redirect&bpo=4892].) multiprocessing.Process
now accepts a daemon
keyword argument to override the default behavior of inheriting the daemon
flag from the parent process (bpo-6064 [https://bugs.python.org/issue?@action=redirect&bpo=6064]).
New attribute multiprocessing.Process.sentinel
allows a program to wait on multiple Process
objects at one time using the appropriate OS primitives (for example, select
on posix systems).
New methods multiprocessing.pool.Pool.starmap()
and starmap_async()
provide itertools.starmap()
equivalents to the existing multiprocessing.pool.Pool.map()
and map_async()
functions. (Contributed by Hynek Schlawack in bpo-12708 [https://bugs.python.org/issue?@action=redirect&bpo=12708].)
nntplib
The nntplib.NNTP
class now supports the context management protocol to unconditionally consume socket.error
exceptions and to close the NNTP connection when done:
- >>> from nntplib import NNTP
- >>> with NNTP('news.gmane.org') as n:
- ... n.group('gmane.comp.python.committers')
- ...
- ('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers')
- >>>
(由 Giampaolo Rodolà 在 bpo-9795 [https://bugs.python.org/issue?@action=redirect&bpo=9795] 中贡献。)
os
The
os
module has a newpipe2()
function that makes it possible to create a pipe withO_CLOEXEC
orO_NONBLOCK
flags set atomically. This is especially useful to avoid race conditions in multi-threaded programs.The
os
module has a newsendfile()
function which provides an efficient "zero-copy" way for copying data from one file (or socket) descriptor to another. The phrase "zero-copy" refers to the fact that all of the copying of data between the two descriptors is done entirely by the kernel, with no copying of data into userspace buffers.sendfile()
can be used to efficiently copy data from a file on disk to a network socket, e.g. for downloading a file.
(Patch submitted by Ross Lagerwall and Giampaolo Rodolà in bpo-10882 [https://bugs.python.org/issue?@action=redirect&bpo=10882].)
To avoid race conditions like symlink attacks and issues with temporary files and directories, it is more reliable (and also faster) to manipulate file descriptors instead of file names. Python 3.3 enhances existing functions and introduces new functions to work on file descriptors (bpo-4761 [https://bugs.python.org/issue?@action=redirect&bpo=4761], bpo-10755 [https://bugs.python.org/issue?@action=redirect&bpo=10755] and bpo-14626 [https://bugs.python.org/issue?@action=redirect&bpo=14626]).
The
os
module has a newfwalk()
function similar towalk()
except that it also yields file descriptors referring to the directories visited. This is especially useful to avoid symlink races.The following functions get new optional dir_fd (paths relative to directory descriptors) and/or follow_symlinks (not following symlinks):
access()
,chflags()
,chmod()
,chown()
,link()
,lstat()
,mkdir()
,mkfifo()
,mknod()
,open()
,readlink()
,remove()
,rename()
,replace()
,rmdir()
,stat()
,symlink()
,unlink()
,utime()
. Platform support for using these parameters can be checked via the setsos.supports_dir_fd
andos.supports_follows_symlinks
.The following functions now support a file descriptor for their path argument:
chdir()
,chmod()
,chown()
,execve()
,listdir()
,pathconf()
,exists()
,stat()
,statvfs()
,utime()
. Platform support for this can be checked via theos.supports_fd
set.
access()
accepts aneffective_ids
keyword argument to turn on using the effective uid/gid rather than the real uid/gid in the access check. Platform support for this can be checked via thesupports_effective_ids
set.The
os
module has two new functions:getpriority()
andsetpriority()
. They can be used to get or set process niceness/priority in a fashion similar toos.nice()
but extended to all processes instead of just the current one.
(Patch submitted by Giampaolo Rodolà in bpo-10784 [https://bugs.python.org/issue?@action=redirect&bpo=10784].)
The new
os.replace()
function allows cross-platform renaming of a file with overwriting the destination. Withos.rename()
, an existing destination file is overwritten under POSIX, but raises an error under Windows. (Contributed by Antoine Pitrou in bpo-8828 [https://bugs.python.org/issue?@action=redirect&bpo=8828].)The stat family of functions (
stat()
,fstat()
, andlstat()
) now support reading a file's timestamps with nanosecond precision. Symmetrically,utime()
can now write file timestamps with nanosecond precision. (Contributed by Larry Hastings in bpo-14127 [https://bugs.python.org/issue?@action=redirect&bpo=14127].)The new
os.get_terminal_size()
function queries the size of the terminal attached to a file descriptor. See alsoshutil.get_terminal_size()
. (Contributed by Zbigniew Jędrzejewski-Szmek in bpo-13609 [https://bugs.python.org/issue?@action=redirect&bpo=13609].)
New functions to support Linux extended attributes (bpo-12720 [https://bugs.python.org/issue?@action=redirect&bpo=12720]):
getxattr()
,listxattr()
,removexattr()
,setxattr()
.New interface to the scheduler. These functions control how a process is allocated CPU time by the operating system. New functions:
sched_get_priority_max()
,sched_get_priority_min()
,sched_getaffinity()
,sched_getparam()
,sched_getscheduler()
,sched_rr_get_interval()
,sched_setaffinity()
,sched_setparam()
,sched_setscheduler()
,sched_yield()
,New functions to control the file system:
posix_fadvise()
: Announces an intention to access data in a specific pattern thus allowing the kernel to make optimizations.posix_fallocate()
: Ensures that enough disk space is allocated for a file.sync()
: Force write of everything to disk.
Additional new posix functions:
lockf()
: Apply, test or remove a POSIX lock on an open file descriptor.pread()
: Read from a file descriptor at an offset, the file offset remains unchanged.pwrite()
: Write to a file descriptor from an offset, leaving the file offset unchanged.readv()
: Read from a file descriptor into a number of writable buffers.truncate()
: Truncate the file corresponding to path, so that it is at most length bytes in size.waitid()
: Wait for the completion of one or more child processes.writev()
: Write the contents of buffers to a file descriptor, where buffers is an arbitrary sequence of buffers.getgrouplist()
(bpo-9344 [https://bugs.python.org/issue?@action=redirect&bpo=9344]): Return list of group ids that specified user belongs to.
times()
anduname()
: Return type changed from a tuple to a tuple-like object with named attributes.Some platforms now support additional constants for the
lseek()
function, such asos.SEEK_HOLE
andos.SEEK_DATA
.New constants
RTLD_LAZY
,RTLD_NOW
,RTLD_GLOBAL
,RTLD_LOCAL
,RTLD_NODELETE
,RTLD_NOLOAD
, andRTLD_DEEPBIND
are available on platforms that support them. These are for use with thesys.setdlopenflags()
function, and supersede the similar constants defined inctypes
andDLFCN
. (Contributed by Victor Stinner in bpo-13226 [https://bugs.python.org/issue?@action=redirect&bpo=13226].)os.symlink()
now accepts (and ignores) thetarget_is_directory
keyword argument on non-Windows platforms, to ease cross-platform support.
pdb
Tab-completion is now available not only for command names, but also their arguments. For example, for the break
command, function and file names are completed.
(由 Georg Brandl 在 bpo-14210 [https://bugs.python.org/issue?@action=redirect&bpo=14210] 中贡献)
pickle
pickle.Pickler
objects now have an optional dispatch_table
attribute allowing per-pickler reduction functions to be set.
(由 Richard Oudkerk 在 bpo-14166 [https://bugs.python.org/issue?@action=redirect&bpo=14166] 中贡献。)
pydoc
The Tk GUI and the serve()
function have been removed from the pydoc
module: pydoc -g
and serve()
have been deprecated in Python 3.2.
re
str
regular expressions now support \u
and \U
escapes.
(由 Serhiy Storchaka 在 bpo-3665 [https://bugs.python.org/issue?@action=redirect&bpo=3665] 中贡献。)
sched
run()
now accepts a blocking parameter which when set to false makes the method execute the scheduled events due to expire soonest (if any) and then return immediately. This is useful in case you want to use thescheduler
in non-blocking applications. (Contributed by Giampaolo Rodolà in bpo-13449 [https://bugs.python.org/issue?@action=redirect&bpo=13449].)scheduler
class can now be safely used in multi-threaded environments. (Contributed by Josiah Carlson and Giampaolo Rodolà in bpo-8684 [https://bugs.python.org/issue?@action=redirect&bpo=8684].)timefunc and delayfunct parameters of
scheduler
class constructor are now optional and defaults totime.time()
andtime.sleep()
respectively. (Contributed by Chris Clark in bpo-13245 [https://bugs.python.org/issue?@action=redirect&bpo=13245].)enter()
andenterabs()
argument parameter is now optional. (Contributed by Chris Clark in bpo-13245 [https://bugs.python.org/issue?@action=redirect&bpo=13245].)enter()
andenterabs()
now accept a kwargs parameter. (Contributed by Chris Clark in bpo-13245 [https://bugs.python.org/issue?@action=redirect&bpo=13245].)
select
Solaris and derivative platforms have a new class select.devpoll
for high performance asynchronous sockets via devpoll
. (Contributed by Jesús Cea Avión in bpo-6397 [https://bugs.python.org/issue?@action=redirect&bpo=6397].)
shlex
The previously undocumented helper function quote
from the pipes
modules has been moved to the shlex
module and documented. quote()
properly escapes all characters in a string that might be otherwise given special meaning by the shell.
shutil
新的函数:
disk_usage()
: provides total, used and free disk space statistics. (Contributed by Giampaolo Rodolà in bpo-12442 [https://bugs.python.org/issue?@action=redirect&bpo=12442].)chown()
: allows one to change user and/or group of the given path also specifying the user/group names and not only their numeric ids. (Contributed by Sandro Tosi in bpo-12191 [https://bugs.python.org/issue?@action=redirect&bpo=12191].)shutil.get_terminal_size()
: returns the size of the terminal window to which the interpreter is attached. (Contributed by Zbigniew Jędrzejewski-Szmek in bpo-13609 [https://bugs.python.org/issue?@action=redirect&bpo=13609].)
copy2()
andcopystat()
now preserve file timestamps with nanosecond precision on platforms that support it. They also preserve file "extended attributes" on Linux. (Contributed by Larry Hastings in bpo-14127 [https://bugs.python.org/issue?@action=redirect&bpo=14127] and bpo-15238 [https://bugs.python.org/issue?@action=redirect&bpo=15238].)Several functions now take an optional
symlinks
argument: when that parameter is true, symlinks aren't dereferenced and the operation instead acts on the symlink itself (or creates one, if relevant). (Contributed by Hynek Schlawack in bpo-12715 [https://bugs.python.org/issue?@action=redirect&bpo=12715].)When copying files to a different file system,
move()
now handles symlinks the way the posixmv
command does, recreating the symlink rather than copying the target file contents. (Contributed by Jonathan Niehof in bpo-9993 [https://bugs.python.org/issue?@action=redirect&bpo=9993].)move()
now also returns thedst
argument as its result.rmtree()
is now resistant to symlink attacks on platforms which support the newdir_fd
parameter inos.open()
andos.unlink()
. (Contributed by Martin von Löwis and Hynek Schlawack in bpo-4489 [https://bugs.python.org/issue?@action=redirect&bpo=4489].)
signal
signal
模块新增的函数:pthread_sigmask()
: 获取和/或改变调用方线程的信号掩码(由 Jean-Paul Calderone 在 bpo-8407 [https://bugs.python.org/issue?@action=redirect&bpo=8407] 中贡献);pthread_kill()
: 向指定线程发送信号;sigpending()
: 检查挂起的函数;sigwait()
: 等待一个信号;sigwaitinfo()
: 等待信号,返回相关的详细信息;sigtimedwait()
: likesigwaitinfo()
but with a timeout.
The signal handler writes the signal number as a single byte instead of a nul byte into the wakeup file descriptor. So it is possible to wait more than one signal and know which signals were raised.
signal.signal()
andsignal.siginterrupt()
raise an OSError, instead of a RuntimeError: OSError has an errno attribute.
smtpd
The smtpd
module now supports RFC 5321 [https://datatracker.ietf.org/doc/html/rfc5321.html] (extended SMTP) and RFC 1870 [https://datatracker.ietf.org/doc/html/rfc1870.html] (size extension). Per the standard, these extensions are enabled if and only if the client initiates the session with an EHLO
command.
(Initial ELHO
support by Alberto Trevino. Size extension by Juhana Jauhiainen. Substantial additional work on the patch contributed by Michele Orrù and Dan Boswell. bpo-8739 [https://bugs.python.org/issue?@action=redirect&bpo=8739])
smtplib
The SMTP
, SMTP_SSL
, and LMTP
classes now accept a source_address
keyword argument to specify the (host, port)
to use as the source address in the bind call when creating the outgoing socket. (Contributed by Paulo Scardine in bpo-11281 [https://bugs.python.org/issue?@action=redirect&bpo=11281].) SMTP
now supports the context management protocol, allowing an SMTP
instance to be used in a with
statement. (Contributed by Giampaolo Rodolà in bpo-11289 [https://bugs.python.org/issue?@action=redirect&bpo=11289].) The SMTP_SSL
constructor and the starttls()
method now accept an SSLContext parameter to control parameters of the secure channel. (Contributed by Kasun Herath in bpo-8809 [https://bugs.python.org/issue?@action=redirect&bpo=8809].)
socket
The
socket
class now exposes additional methods to process ancillary data when supported by the underlying platform:
(Contributed by David Watson in bpo-6560 [https://bugs.python.org/issue?@action=redirect&bpo=6560], based on an earlier patch by Heiko Wundram)
- The
socket
class now supports the PF_CAN protocol family (https://en.wikipedia.org/wiki/Socketcan), on Linux (https://lwn.net/Articles/253425).
(Contributed by Matthias Fuchs, updated by Tiago Gonçalves in bpo-10141 [https://bugs.python.org/issue?@action=redirect&bpo=10141].)
The
socket
class now supports the PF_RDS protocol family (https://en.wikipedia.org/wiki/Reliable_Datagram_Sockets and https://oss.oracle.com/projects/rds [https://web.archive.org/web/20130115155505/https://oss.oracle.com/projects/rds/]).The
socket
class now supports thePF_SYSTEM
protocol family on OS X. (Contributed by Michael Goderbauer in bpo-13777 [https://bugs.python.org/issue?@action=redirect&bpo=13777].)New function
sethostname()
allows the hostname to be set on Unix systems if the calling process has sufficient privileges. (Contributed by Ross Lagerwall in bpo-10866 [https://bugs.python.org/issue?@action=redirect&bpo=10866].)
socketserver
BaseServer
now has an overridable method service_actions()
that is called by the serve_forever()
method in the service loop. ForkingMixIn
now uses this to clean up zombie child processes. (Contributed by Justin Warkentin in bpo-11109 [https://bugs.python.org/issue?@action=redirect&bpo=11109].)
sqlite3
New sqlite3.Connection
method set_trace_callback()
can be used to capture a trace of all sql commands processed by sqlite. (Contributed by Torsten Landschoff in bpo-11688 [https://bugs.python.org/issue?@action=redirect&bpo=11688].)
ssl
The
ssl
module has two new random generation functions:RAND_bytes()
: generate cryptographically strong pseudo-random bytes.RAND_pseudo_bytes()
: 生成伪随机字节。
(由 Victor Stinner 在 bpo-12049 [https://bugs.python.org/issue?@action=redirect&bpo=12049] 中贡献。)
The
ssl
module now exposes a finer-grained exception hierarchy in order to make it easier to inspect the various kinds of errors. (Contributed by Antoine Pitrou in bpo-11183 [https://bugs.python.org/issue?@action=redirect&bpo=11183].)load_cert_chain()
now accepts a password argument to be used if the private key is encrypted. (Contributed by Adam Simpkins in bpo-12803 [https://bugs.python.org/issue?@action=redirect&bpo=12803].)Diffie-Hellman key exchange, both regular and Elliptic Curve-based, is now supported through the
load_dh_params()
andset_ecdh_curve()
methods. (Contributed by Antoine Pitrou in bpo-13626 [https://bugs.python.org/issue?@action=redirect&bpo=13626] and bpo-13627 [https://bugs.python.org/issue?@action=redirect&bpo=13627].)SSL sockets have a new
get_channel_binding()
method allowing the implementation of certain authentication mechanisms such as SCRAM-SHA-1-PLUS. (Contributed by Jacek Konieczny in bpo-12551 [https://bugs.python.org/issue?@action=redirect&bpo=12551].)You can query the SSL compression algorithm used by an SSL socket, thanks to its new
compression()
method. The new attributeOP_NO_COMPRESSION
can be used to disable compression. (Contributed by Antoine Pitrou in bpo-13634 [https://bugs.python.org/issue?@action=redirect&bpo=13634].)Support has been added for the Next Protocol Negotiation extension using the
ssl.SSLContext.set_npn_protocols()
method. (Contributed by Colin Marc in bpo-14204 [https://bugs.python.org/issue?@action=redirect&bpo=14204].)SSL errors can now be introspected more easily thanks to
library
andreason
attributes. (Contributed by Antoine Pitrou in bpo-14837 [https://bugs.python.org/issue?@action=redirect&bpo=14837].)The
get_server_certificate()
function now supports IPv6. (Contributed by Charles-François Natali in bpo-11811 [https://bugs.python.org/issue?@action=redirect&bpo=11811].)New attribute
OP_CIPHER_SERVER_PREFERENCE
allows setting SSLv3 server sockets to use the server's cipher ordering preference rather than the client's (bpo-13635 [https://bugs.python.org/issue?@action=redirect&bpo=13635]).
stat
The undocumented tarfile.filemode function has been moved to stat.filemode()
. It can be used to convert a file's mode to a string of the form '-rwxrwxrwx'.
(由 Giampaolo Rodolà 在 bpo-14807 [https://bugs.python.org/issue?@action=redirect&bpo=14807] 中贡献。)
struct
The struct
module now supports ssize_t
and size_t
via the new codes n
and N
, respectively. (Contributed by Antoine Pitrou in bpo-3163 [https://bugs.python.org/issue?@action=redirect&bpo=3163].)
subprocess
Command strings can now be bytes objects on posix platforms. (Contributed by Victor Stinner in bpo-8513 [https://bugs.python.org/issue?@action=redirect&bpo=8513].) A new constant DEVNULL
allows suppressing output in a platform-independent fashion. (Contributed by Ross Lagerwall in bpo-5870 [https://bugs.python.org/issue?@action=redirect&bpo=5870].)
sys
The sys
module has a new thread_info
named tuple holding information about the thread implementation (bpo-11223 [https://bugs.python.org/issue?@action=redirect&bpo=11223]).
tarfile
tarfile
now supports lzma
encoding via the lzma
module. (Contributed by Lars Gustäbel in bpo-5689 [https://bugs.python.org/issue?@action=redirect&bpo=5689].)
tempfile
tempfile.SpooledTemporaryFile
's truncate()
method now accepts a size
parameter. (Contributed by Ryan Kelly in bpo-9957 [https://bugs.python.org/issue?@action=redirect&bpo=9957].)
textwrap
The textwrap
module has a new indent()
that makes it straightforward to add a common prefix to selected lines in a block of text (bpo-13857 [https://bugs.python.org/issue?@action=redirect&bpo=13857]).
threading
threading.Condition
, threading.Semaphore
, threading.BoundedSemaphore
, threading.Event
, and threading.Timer
, all of which used to be factory functions returning a class instance, are now classes and may be subclassed. (Contributed by Éric Araujo in bpo-10968 [https://bugs.python.org/issue?@action=redirect&bpo=10968].) The threading.Thread
constructor now accepts a daemon
keyword argument to override the default behavior of inheriting the daemon
flag value from the parent thread (bpo-6064 [https://bugs.python.org/issue?@action=redirect&bpo=6064]).
The formerly private function thread.getident
is now available as the public function threading.get_ident()
. This eliminates several cases of direct access to the _thread
module in the stdlib. Third party code that used thread.getident
should likewise be changed to use the new public interface.
time
The PEP 418 [https://peps.python.org/pep-0418/] added new functions to the time
module:
get_clock_info()
: Get information on a clock.monotonic()
: Monotonic clock (cannot go backward), not affected by system clock updates.perf_counter()
: Performance counter with the highest available resolution to measure a short duration.process_time()
: Sum of the system and user CPU time of the current process.
Other new functions:
clock_getres()
,clock_gettime()
andclock_settime()
functions withCLOCK_xxx
constants. (Contributed by Victor Stinner in bpo-10278 [https://bugs.python.org/issue?@action=redirect&bpo=10278].)
To improve cross platform consistency, sleep()
now raises a ValueError
when passed a negative sleep value. Previously this was an error on posix, but produced an infinite sleep on Windows.
types
Add a new types.MappingProxyType
class: Read-only proxy of a mapping. (bpo-14386 [https://bugs.python.org/issue?@action=redirect&bpo=14386]) The new functions types.new_class()
and types.prepare_class()
provide support for PEP 3115 [https://peps.python.org/pep-3115/] compliant dynamic type creation. (bpo-14588 [https://bugs.python.org/issue?@action=redirect&bpo=14588])
unittest
assertRaises()
, assertRaisesRegex()
, assertWarns()
, and assertWarnsRegex()
now accept a keyword argument msg when used as context managers. (Contributed by Ezio Melotti and Winston Ewert in bpo-10775 [https://bugs.python.org/issue?@action=redirect&bpo=10775].) unittest.TestCase.run()
now returns the TestResult
object.
urllib
The Request
class, now accepts a method argument used by get_method()
to determine what HTTP method should be used. For example, this will send a 'HEAD'
request:
- >>> urlopen(Request('https://www.python.org', method='HEAD'))
(bpo-1673007 [https://bugs.python.org/issue?@action=redirect&bpo=1673007])
webbrowser
The webbrowser
module supports more "browsers": Google Chrome (named chrome, chromium, chrome-browser or chromium-browser depending on the version and operating system), and the generic launchers xdg-open, from the FreeDesktop.org project, and gvfs-open, which is the default URI handler for GNOME 3. (The former contributed by Arnaud Calmettes in bpo-13620 [https://bugs.python.org/issue?@action=redirect&bpo=13620], the latter by Matthias Klose in bpo-14493 [https://bugs.python.org/issue?@action=redirect&bpo=14493].)
xml.etree.ElementTree
The xml.etree.ElementTree
module now imports its C accelerator by default; there is no longer a need to explicitly import xml.etree.cElementTree
(this module stays for backwards compatibility, but is now deprecated). In addition, the iter
family of methods of Element
has been optimized (rewritten in C). The module's documentation has also been greatly improved with added examples and a more detailed reference.
zlib
New attribute zlib.Decompress.eof
makes it possible to distinguish between a properly formed compressed stream and an incomplete or truncated one. (Contributed by Nadeem Vawda in bpo-12646 [https://bugs.python.org/issue?@action=redirect&bpo=12646].) New attribute zlib.ZLIB_RUNTIME_VERSION
reports the version string of the underlying zlib
library that is loaded at runtime. (Contributed by Torsten Landschoff in bpo-12306 [https://bugs.python.org/issue?@action=redirect&bpo=12306].)
性能优化
已增加的主要性能改善:
得益于:pep:393 ,Unicode 字符串的某些操作已得到优化:
the memory footprint is divided by 2 to 4 depending on the text
将 ASCII 字符串编码为 UTF-8 不再需要对字符进行编码,UTF-8 的表示法与 ASCII 的表示法是共享的
the UTF-8 encoder has been optimized
repeating a single ASCII letter and getting a substring of an ASCII string is 4 times faster
UTF-8 编码现在快 2 到 4 倍。 UTF-16 编码的速度现在提高了 10 倍。
(由 Serhiy Storchaka 在 bpo-14624 [https://bugs.python.org/issue?@action=redirect&bpo=14624], bpo-14738 [https://bugs.python.org/issue?@action=redirect&bpo=14738] 和 bpo-15026 [https://bugs.python.org/issue?@action=redirect&bpo=15026] 中贡献)。
构建和 C API 的改变
针对 Python 构建过程和 C API 的改变包括:
新的 PEP 3118 [https://peps.python.org/pep-3118/] 相关功能:
PEP 393 [https://peps.python.org/pep-0393/] 添加了新的 Unicode 类型,宏和函数
高阶 API
低阶 API:
PyUnicode_DATA
,PyUnicode_1BYTE_DATA
,PyUnicode_2BYTE_DATA
,PyUnicode_4BYTE_DATA
PyUnicode_KIND
withPyUnicode_Kind
enum:PyUnicode_WCHAR_KIND
,PyUnicode_1BYTE_KIND
,PyUnicode_2BYTE_KIND
,PyUnicode_4BYTE_KIND
PyArg_ParseTuple
现在接受c
格式的bytearray
(bpo-12380 [https://bugs.python.org/issue?@action=redirect&bpo=12380])。
弃用
不支持的操作系统
由于缺少维护人员,不再支持 OS/2 和 VMS 系统 。
由于维护负担,将 COMSPEC
设置为 command.com
的 Windows平台(含Windows 2000)不再受支持。
OSF支持在3.2中被弃用,现在已经被完全删除。
已弃用的 Python 模块、函数和方法
向
object.__format__()
传递非空字符串的做法已被弃用,在 Python 3.4 中会产生一个TypeError
(bpo-9856 [https://bugs.python.org/issue?@action=redirect&bpo=9856])。由于:pep:393,
unicode_internal
编解码器已被弃用 。请使用 UTF-8、UTF-16 (utf-16-le
或utf-16-be
) 或 UTF-32 (utf-32-le
或utf-32-be
)ftplib.FTP.nlst()
andftplib.FTP.dir()
: useftplib.FTP.mlsd()
platform.popen()
: use thesubprocess
module. Check especially the 使用 subprocess 模块替换旧函数 section (bpo-11377 [https://bugs.python.org/issue?@action=redirect&bpo=11377]).bpo-13374 [https://bugs.python.org/issue?@action=redirect&bpo=13374]: The Windows bytes API has been deprecated in the
os
module. Use Unicode filenames, instead of bytes filenames, to not depend on the ANSI code page anymore and to support any filename.bpo-13988 [https://bugs.python.org/issue?@action=redirect&bpo=13988]: The
xml.etree.cElementTree
module is deprecated. The accelerator is used automatically whenever available.The behaviour of
time.clock()
depends on the platform: use the newtime.perf_counter()
ortime.process_time()
function instead, depending on your requirements, to have a well defined behaviour.The
os.stat_float_times()
function is deprecated.abc
module:abc.abstractproperty
has been deprecated, useproperty
withabc.abstractmethod()
instead.abc.abstractclassmethod
has been deprecated, useclassmethod
withabc.abstractmethod()
instead.abc.abstractstaticmethod
has been deprecated, usestaticmethod
withabc.abstractmethod()
instead.
importlib
package:importlib.abc.SourceLoader.path_mtime()
is now deprecated in favour ofimportlib.abc.SourceLoader.path_stats()
as bytecode files now store both the modification time and size of the source file the bytecode file was compiled from.
已弃用的 C API 函数和类型
Py_UNICODE
已经在 PEP 393 [https://peps.python.org/pep-0393/] 弃用,并将于 Python 4 中移除。所有使用此类型的函数都已弃用:
Unicode functions and methods using Py_UNICODE
and Py_UNICODE* types:
PyUnicode_FromUnicode
: usePyUnicode_FromWideChar()
orPyUnicode_FromKindAndData()
PyUnicode_AS_UNICODE
,PyUnicode_AsUnicode()
,PyUnicode_AsUnicodeAndSize()
: usePyUnicode_AsWideCharString()
PyUnicode_AS_DATA
: usePyUnicode_DATA
withPyUnicode_READ
andPyUnicode_WRITE
PyUnicode_GET_SIZE
,PyUnicode_GetSize()
: usePyUnicode_GET_LENGTH
orPyUnicode_GetLength()
PyUnicode_GET_DATA_SIZE
: usePyUnicode_GET_LENGTH(str) * PyUnicode_KIND(str)
(only work on ready strings)PyUnicode_AsUnicodeCopy()
: usePyUnicode_AsUCS4Copy()
orPyUnicode_AsWideCharString()
PyUnicode_GetMax()
Functions and macros manipulating Py_UNICODE* strings:
Py_UNICODE_strlen()
: usePyUnicode_GetLength()
orPyUnicode_GET_LENGTH
Py_UNICODE_strcat()
: usePyUnicode_CopyCharacters()
orPyUnicode_FromFormat()
Py_UNICODE_strcpy()
,Py_UNICODE_strncpy()
,Py_UNICODE_COPY()
: usePyUnicode_CopyCharacters()
orPyUnicode_Substring()
Py_UNICODE_strcmp()
: usePyUnicode_Compare()
Py_UNICODE_strncmp()
: usePyUnicode_Tailmatch()
Py_UNICODE_strchr()
,Py_UNICODE_strrchr()
: usePyUnicode_FindChar()
Py_UNICODE_FILL()
: usePyUnicode_Fill()
Py_UNICODE_MATCH
编码器:
PyUnicode_Encode()
: usePyUnicode_AsEncodedObject()
PyUnicode_EncodeUTF7()
PyUnicode_EncodeUTF8()
: usePyUnicode_AsUTF8()
orPyUnicode_AsUTF8String()
PyUnicode_EncodeUTF32()
PyUnicode_EncodeUTF16()
PyUnicode_EncodeUnicodeEscape()
usePyUnicode_AsUnicodeEscapeString()
PyUnicode_EncodeRawUnicodeEscape()
usePyUnicode_AsRawUnicodeEscapeString()
PyUnicode_EncodeLatin1()
: usePyUnicode_AsLatin1String()
PyUnicode_EncodeASCII()
: usePyUnicode_AsASCIIString()
PyUnicode_EncodeCharmap()
PyUnicode_TranslateCharmap()
PyUnicode_EncodeMBCS()
: usePyUnicode_AsMBCSString()
orPyUnicode_EncodeCodePage()
(withCP_ACP
code_page)PyUnicode_EncodeDecimal()
,PyUnicode_TransformDecimalToASCII()
弃用的功能
array
模块的'u'
格式代码现已弃用,将在 Python 4 中与 (Py_UNICODE
) API 的其他部分一起删除。
移植到 Python 3.3
本节列出了先前描述的更改以及可能需要更改代码的其他错误修正.
移植 Python 代码
默认启用哈希随机化。 将
PYTHONHASHSEED
环境变量设为0
可禁用哈希随机化。 另请参阅object.__hash__()
方法。bpo-12326 [https://bugs.python.org/issue?@action=redirect&bpo=12326]: 在 Linux 上,sys.platform 不再包含主要版本。现在它始终是 "linux",而不是 "linux2" 或 "linux3",这取决于用于构建 Python 的 Linux 版本。请用 sys.platform.startswith('linux') 替换 sys.platform == 'linux2',如果不需要支持较旧的 Python 版本,则可直接替换成 sys.platform == 'linux'。
bpo-13847 [https://bugs.python.org/issue?@action=redirect&bpo=13847], bpo-14180 [https://bugs.python.org/issue?@action=redirect&bpo=14180]:
time
和datetime
: 现在如果时间戳超出范围将会引发OverflowError
而不是ValueError
。 现在如果 C 函数gmtime()
或localtime()
失败 将会引发OSError
。The default finders used by import now utilize a cache of what is contained within a specific directory. If you create a Python source file or sourceless bytecode file, make sure to call
importlib.invalidate_caches()
to clear out the cache for the finders to notice the new file.ImportError
now uses the full name of the module that was attempted to be imported. Doctests that check ImportErrors' message will need to be updated to use the full name of the module instead of just the tail of the name.The index argument to
__import__()
now defaults to 0 instead of -1 and no longer support negative values. It was an oversight when PEP 328 [https://peps.python.org/pep-0328/] was implemented that the default value remained -1. If you need to continue to perform a relative import followed by an absolute import, then perform the relative import using an index of 1, followed by another import using an index of 0. It is preferred, though, that you useimportlib.import_module()
rather than call__import__()
directly.__import__()
no longer allows one to use an index value other than 0 for top-level modules. E.g.__import__('sys', level=1)
is now an error.Because
sys.meta_path
andsys.path_hooks
now have finders on them by default, you will most likely want to uselist.insert()
instead oflist.append()
to add to those lists.Because
None
is now inserted intosys.path_importer_cache
, if you are clearing out entries in the dictionary of paths that do not have a finder, you will need to remove keys paired with values ofNone
andimp.NullImporter
to be backwards-compatible. This will lead to extra overhead on older versions of Python that re-insertNone
intosys.path_importer_cache
where it represents the use of implicit finders, but semantically it should not change anything.importlib.abc.Finder
no longer specifies afind_module()
abstract method that must be implemented. If you were relying on subclasses to implement that method, make sure to check for the method's existence first. You will probably want to check forfind_loader()
first, though, in the case of working with path entry finders.pkgutil
has been converted to useimportlib
internally. This eliminates many edge cases where the old behaviour of the PEP 302 [https://peps.python.org/pep-0302/] import emulation failed to match the behaviour of the real import system. The import emulation itself is still present, but is now deprecated. Thepkgutil.iter_importers()
andpkgutil.walk_packages()
functions special case the standard import hooks so they are still supported even though they do not provide the non-standarditer_modules()
method.A longstanding RFC-compliance bug (bpo-1079 [https://bugs.python.org/issue?@action=redirect&bpo=1079]) in the parsing done by
email.header.decode_header()
has been fixed. Code that uses the standard idiom to convert encoded headers into unicode (str(make_header(decode_header(h))
) will see no change, but code that looks at the individual tuples returned by decode_header will see that whitespace that precedes or followsASCII
sections is now included in theASCII
section. Code that builds headers usingmake_header
should also continue to work without change, sincemake_header
continues to add whitespace betweenASCII
and non-ASCII
sections if it is not already present in the input strings.email.utils.formataddr()
now does the correct content transfer encoding when passed non-ASCII
display names. Any code that depended on the previous buggy behavior that preserved the non-ASCII
unicode in the formatted output string will need to be changed (bpo-1690608 [https://bugs.python.org/issue?@action=redirect&bpo=1690608]).poplib.POP3.quit()
may now raise protocol errors like all otherpoplib
methods. Code that assumesquit
does not raisepoplib.error_proto
errors may need to be changed if errors onquit
are encountered by a particular application (bpo-11291 [https://bugs.python.org/issue?@action=redirect&bpo=11291]).The
strict
argument toemail.parser.Parser
, deprecated since Python 2.4, has finally been removed.The deprecated method
unittest.TestCase.assertSameElements
has been removed.The deprecated variable
time.accept2dyear
has been removed.被弃用的
Context._clamp
属性已从decimal
模块中移除。 在此之前它已被公有属性clamp
取代。 (参见 bpo-8540 [https://bugs.python.org/issue?@action=redirect&bpo=8540]。)The undocumented internal helper class
SSLFakeFile
has been removed fromsmtplib
, since its functionality has long been provided directly bysocket.socket.makefile()
.Passing a negative value to
time.sleep()
on Windows now raises an error instead of sleeping forever. It has always raised an error on posix.The
ast.__version__
constant has been removed. If you need to make decisions affected by the AST version, usesys.version_info
to make the decision.Code that used to work around the fact that the
threading
module used factory functions by subclassing the private classes will need to change to subclass the now-public classes.The undocumented debugging machinery in the threading module has been removed, simplifying the code. This should have no effect on production code, but is mentioned here in case any application debug frameworks were interacting with it (bpo-13550 [https://bugs.python.org/issue?@action=redirect&bpo=13550]).
移植 C 代码
- In the course of changes to the buffer API the undocumented
smalltable
member of thePy_buffer
structure has been removed and the layout of thePyMemoryViewObject
has changed.
All extensions relying on the relevant parts in memoryobject.h
or object.h
must be rebuilt.
- Due to PEP 393, the
Py_UNICODE
type and all functions using this type are deprecated (but will stay available for at least five years). If you were using low-level Unicode APIs to construct and access unicode objects and you want to benefit of the memory footprint reduction provided by PEP 393 [https://peps.python.org/pep-0393/], you have to convert your code to the new Unicode API.
However, if you only have been using high-level functions such as PyUnicode_Concat()
, PyUnicode_Join()
or PyUnicode_FromFormat()
, your code will automatically take advantage of the new unicode representations.
PyImport_GetMagicNumber()
now returns-1
upon failure.As a negative value for the level argument to
__import__()
is no longer valid, the same now holds forPyImport_ImportModuleLevel()
. This also means that the value of level used byPyImport_ImportModuleEx()
is now0
instead of-1
.
Building C extensions
- The range of possible file names for C extensions has been narrowed. Very rarely used spellings have been suppressed: under POSIX, files named
xxxmodule.so
,xxxmodule.abi3.so
andxxxmodule.cpython-*.so
are no longer recognized as implementing thexxx
module. If you had been generating such files, you have to switch to the other spellings (i.e., remove themodule
string from the file names).
(在 bpo-14040 [https://bugs.python.org/issue?@action=redirect&bpo=14040] 中实现。)
命令行开关的变化
- 删除了 -Q命令-line旗标 和相关工具。 检查 sys.flags.division_warning 的代码需要更新。
(bpo-10998 [https://bugs.python.org/issue?@action=redirect&bpo=10998],由 Éric Araujo 贡献。)
- 当 python 附带
-S
启动时,import site
将不再向模块搜索路径添加 site 专属路径。 在之前版本中则会这样做。
(由 Carl Meyer 在 bpo-11591 [https://bugs.python.org/issue?@action=redirect&bpo=11591] 中贡献并由 Éric Araujo 进行修改。)