sqlite3

sqlite3 模块被更新至 pysqlite 2.6.0 版。 它拥有两个新功能。

(由 R. David Murray 和 Shashwat Anand 在 bpo-8845 [https://bugs.python.org/issue?@action=redirect&bpo=8845] 中贡献。)

html

A new html module was introduced with only a single function, escape(), which is used for escaping reserved characters from HTML markup:

  1. >>> import html
  2. >>> html.escape('x > 2 && x < 7')
  3. 'x &gt; 2 &amp;&amp; x &lt; 7'

socket

socket 模块有两项新改进。

ssl

The ssl module added a number of features to satisfy common requirements for secure (encrypted, authenticated) internet connections:

  • A new class, SSLContext, serves as a container for persistent SSL data, such as protocol settings, certificates, private keys, and various other options. It includes a wrap_socket() for creating an SSL socket from an SSL context.

  • A new function, ssl.match_hostname(), supports server identity verification for higher-level protocols by implementing the rules of HTTPS (from RFC 2818 [https://datatracker.ietf.org/doc/html/rfc2818.html]) which are also suitable for other protocols.

  • The ssl.wrap_socket() constructor function now takes a ciphers argument. The ciphers string lists the allowed encryption algorithms using the format described in the OpenSSL documentation [https://docs.openssl.org/1.0.2/man1/ciphers/#cipher-list-format].

  • When linked against recent versions of OpenSSL, the ssl module now supports the Server Name Indication extension to the TLS protocol, allowing multiple "virtual hosts" using different certificates on a single IP port. This extension is only supported in client mode, and is activated by passing the server_hostname argument to ssl.SSLContext.wrap_socket().

  • Various options have been added to the ssl module, such as OP_NO_SSLv2 which disables the insecure and obsolete SSLv2 protocol.

  • The extension now loads all the OpenSSL ciphers and digest algorithms. If some SSL certificates cannot be verified, they are reported as an "unknown algorithm" error.

  • The version of OpenSSL being used is now accessible using the module attributes ssl.OPENSSL_VERSION (a string), ssl.OPENSSL_VERSION_INFO (a 5-tuple), and ssl.OPENSSL_VERSION_NUMBER (an integer).

(由 Antoine Pitrou 在 bpo-8850 [https://bugs.python.org/issue?@action=redirect&bpo=8850], bpo-1589 [https://bugs.python.org/issue?@action=redirect&bpo=1589], bpo-8322 [https://bugs.python.org/issue?@action=redirect&bpo=8322], bpo-5639 [https://bugs.python.org/issue?@action=redirect&bpo=5639], bpo-4870 [https://bugs.python.org/issue?@action=redirect&bpo=4870], bpo-8484 [https://bugs.python.org/issue?@action=redirect&bpo=8484] 和 bpo-8321 [https://bugs.python.org/issue?@action=redirect&bpo=8321] 中贡献。)

nntp

The nntplib module has a revamped implementation with better bytes and text semantics as well as more practical APIs. These improvements break compatibility with the nntplib version in Python 3.1, which was partly dysfunctional in itself.

Support for secure connections through both implicit (using nntplib.NNTP_SSL) and explicit (using nntplib.NNTP.starttls()) TLS has also been added.

(由 Antoine Pitrou 在 bpo-9360 [https://bugs.python.org/issue?@action=redirect&bpo=9360] 中贡献,由 Andrew Vant 在 bpo-1926 [https://bugs.python.org/issue?@action=redirect&bpo=1926] 中贡献。)

certificates

http.client.HTTPSConnection, urllib.request.HTTPSHandler and urllib.request.urlopen() now take optional arguments to allow for server certificate checking against a set of Certificate Authorities, as recommended in public uses of HTTPS.

(由 Antoine Pitrou 添加,bpo-9003 [https://bugs.python.org/issue?@action=redirect&bpo=9003]。)

imaplib

Support for explicit TLS on standard IMAP4 connections has been added through the new imaplib.IMAP4.starttls method.

(由 Lorenzo M. Catucci 和 Antoine Pitrou 在 bpo-4471 [https://bugs.python.org/issue?@action=redirect&bpo=4471] 中贡献。)

http.client

There were a number of small API improvements in the http.client module. The old-style HTTP 0.9 simple responses are no longer supported and the strict parameter is deprecated in all classes.

The HTTPConnection and HTTPSConnection classes now have a source_address parameter for a (host, port) tuple indicating where the HTTP connection is made from.

Support for certificate checking and HTTPS virtual hosts were added to HTTPSConnection.

The request() method on connection objects allowed an optional body argument so that a file object could be used to supply the content of the request. Conveniently, the body argument now also accepts an iterable object so long as it includes an explicit Content-Length header. This extended interface is much more flexible than before.

To establish an HTTPS connection through a proxy server, there is a new set_tunnel() method that sets the host and port for HTTP Connect tunneling.

To match the behavior of http.server, the HTTP client library now also encodes headers with ISO-8859-1 (Latin-1) encoding. It was already doing that for incoming headers, so now the behavior is consistent for both incoming and outgoing traffic. (See work by Armin Ronacher in bpo-10980 [https://bugs.python.org/issue?@action=redirect&bpo=10980].)

unittest

The unittest module has a number of improvements supporting test discovery for packages, easier experimentation at the interactive prompt, new testcase methods, improved diagnostic messages for test failures, and better method names.

  • The command-line call python -m unittest can now accept file paths instead of module names for running specific tests (bpo-10620 [https://bugs.python.org/issue?@action=redirect&bpo=10620]). The new test discovery can find tests within packages, locating any test importable from the top-level directory. The top-level directory can be specified with the -t option, a pattern for matching files with -p, and a directory to start discovery with -s:
  1. $ python -m unittest discover -s my_proj_dir -p _test.py

(由 Michael Foord 贡献)

  • Experimentation at the interactive prompt is now easier because the unittest.TestCase class can now be instantiated without arguments:
  1. >>> from unittest import TestCase
  2. >>> TestCase().assertEqual(pow(2, 3), 8)

(由 Michael Foord 贡献)

  1. with self.assertWarns(DeprecationWarning):
  2. legacy_function('XYZ')

(由 Antoine Pitrou 在 bpo-9754 [https://bugs.python.org/issue?@action=redirect&bpo=9754] 中贡献。)

Another new method, assertCountEqual() is used to compare two iterables to determine if their element counts are equal (whether the same elements are present with the same number of occurrences regardless of order):

  1. def test_anagram(self):
  2. self.assertCountEqual('algorithm', 'logarithm')

(由 Raymond Hettinger 贡献。)

  • A principal feature of the unittest module is an effort to produce meaningful diagnostics when a test fails. When possible, the failure is recorded along with a diff of the output. This is especially helpful for analyzing log files of failed test runs. However, since diffs can sometime be voluminous, there is a new maxDiff attribute that sets maximum length of diffs displayed.

  • In addition, the method names in the module have undergone a number of cleanups.

For example, assertRegex() is the new name for assertRegexpMatches() which was misnamed because the test uses re.search(), not re.match(). Other methods using regular expressions are now named using short form "Regex" in preference to "Regexp" — this matches the names used in other unittest implementations, matches Python's old name for the re module, and it has unambiguous camel-casing.

(由 Raymond Hettinger 贡献并由 Ezio Melotti 实现。)

  • To improve consistency, some long-standing method aliases are being deprecated in favor of the preferred names:

旧名称

首选名称

assert_()

assertTrue()

assertEquals()

assertEqual()

assertNotEquals()

assertNotEqual()

assertAlmostEquals()

assertAlmostEqual()

assertNotAlmostEquals()

assertNotAlmostEqual()

Likewise, the TestCase.fail* methods deprecated in Python 3.1 are expected to be removed in Python 3.3.

(由 Ezio Melotti 在 bpo-9424 [https://bugs.python.org/issue?@action=redirect&bpo=9424] 中贡献。)

  • The assertDictContainsSubset() method was deprecated because it was misimplemented with the arguments in the wrong order. This created hard-to-debug optical illusions where tests like TestCase().assertDictContainsSubset({'a':1, 'b':2}, {'a':1}) would fail.

(由 Raymond Hettinger 贡献。)

random

The integer methods in the random module now do a better job of producing uniform distributions. Previously, they computed selections with int(n*random()) which had a slight bias whenever n was not a power of two. Now, multiple selections are made from a range up to the next power of two and a selection is kept only when it falls within the range 0 <= x < n. The functions and methods affected are randrange(), randint(), choice(), shuffle() and sample().

(由 Raymond Hettinger 在 bpo-9025 [https://bugs.python.org/issue?@action=redirect&bpo=9025] 中贡献。)

poplib

POP3_SSL class now accepts a context parameter, which is a ssl.SSLContext object allowing bundling SSL configuration options, certificates and private keys into a single (potentially long-lived) structure.

(由 Giampaolo Rodolà 在 bpo-8807 [https://bugs.python.org/issue?@action=redirect&bpo=8807] 中贡献。)

asyncore

asyncore.dispatcher now provides a handle_accepted() method returning a (sock, addr) pair which is called when a connection has actually been established with a new remote endpoint. This is supposed to be used as a replacement for old handle_accept() and avoids the user to call accept() directly.

(由 Giampaolo Rodolà 在 bpo-6706 [https://bugs.python.org/issue?@action=redirect&bpo=6706] 中贡献。)

tempfile

The tempfile module has a new context manager, TemporaryDirectory which provides easy deterministic cleanup of temporary directories:

  1. with tempfile.TemporaryDirectory() as tmpdirname:
  2. print('created temporary dir:', tmpdirname)

(由 Neil Schemenauer 和 Nick Coghlan 在 bpo-5178 [https://bugs.python.org/issue?@action=redirect&bpo=5178] 中贡献。)

inspect

  1. >>> from inspect import getgeneratorstate
  2. >>> def gen():
  3. ... yield 'demo'
  4. ...
  5. >>> g = gen()
  6. >>> getgeneratorstate(g)
  7. 'GEN_CREATED'
  8. >>> next(g)
  9. 'demo'
  10. >>> getgeneratorstate(g)
  11. 'GEN_SUSPENDED'
  12. >>> next(g, None)
  13. >>> getgeneratorstate(g)
  14. 'GEN_CLOSED'

(由 Rodolpho Eckhardt 和 Nick Coghlan 在 bpo-10220 [https://bugs.python.org/issue?@action=redirect&bpo=10220] 中贡献。)

  • To support lookups without the possibility of activating a dynamic attribute, the inspect module has a new function, getattr_static(). Unlike hasattr(), this is a true read-only search, guaranteed not to change state while it is searching:
  1. >>> class A:
  2. ... @property
  3. ... def f(self):
  4. ... print('Running')
  5. ... return 10
  6. ...
  7. >>> a = A()
  8. >>> getattr(a, 'f')
  9. Running
  10. 10
  11. >>> inspect.getattr_static(a, 'f')
  12. <property object at 0x1022bd788>

(由 Michael Foord 贡献)

pydoc

The pydoc module now provides a much-improved web server interface, as well as a new command-line option -b to automatically open a browser window to display that server:

  1. $ pydoc3.2 -b

(由 Ron Adam 在 bpo-2001 [https://bugs.python.org/issue?@action=redirect&bpo=2001] 中贡献。)

dis

The dis module gained two new functions for inspecting code, code_info() and show_code(). Both provide detailed code object information for the supplied function, method, source code string or code object. The former returns a string and the latter prints it:

  1. >>> import dis, random
  2. >>> dis.show_code(random.choice)
  3. Name: choice
  4. Filename: LibraryFrameworks/Python.framework/Versions/3.2/lib/python3.2/random.py
  5. Argument count: 2
  6. Kw-only arguments: 0
  7. Number of locals: 3
  8. Stack size: 11
  9. Flags: OPTIMIZED, NEWLOCALS, NOFREE
  10. Constants:
  11. 0: 'Choose a random element from a non-empty sequence.'
  12. 1: 'Cannot choose from an empty sequence'
  13. Names:
  14. 0: _randbelow
  15. 1: len
  16. 2: ValueError
  17. 3: IndexError
  18. Variable names:
  19. 0: self
  20. 1: seq
  21. 2: i

In addition, the dis() function now accepts string arguments so that the common idiom dis(compile(s, '', 'eval')) can be shortened to dis(s):

  1. >>> dis('3*x+1 if x%2==1 else x//2')
  2. 1 0 LOAD_NAME 0 (x)
  3. 3 LOAD_CONST 0 (2)
  4. 6 BINARY_MODULO
  5. 7 LOAD_CONST 1 (1)
  6. 10 COMPARE_OP 2 (==)
  7. 13 POP_JUMP_IF_FALSE 28
  8. 16 LOAD_CONST 2 (3)
  9. 19 LOAD_NAME 0 (x)
  10. 22 BINARY_MULTIPLY
  11. 23 LOAD_CONST 1 (1)
  12. 26 BINARY_ADD
  13. 27 RETURN_VALUE
  14. >> 28 LOAD_NAME 0 (x)
  15. 31 LOAD_CONST 0 (2)
  16. 34 BINARY_FLOOR_DIVIDE
  17. 35 RETURN_VALUE

Taken together, these improvements make it easier to explore how CPython is implemented and to see for yourself what the language syntax does under-the-hood.

(由 Nick Coghlan 在 bpo-9147 [https://bugs.python.org/issue?@action=redirect&bpo=9147] 中贡献。)

dbm

All database modules now support the get() and setdefault() methods.

(由 Ray Allen 在 bpo-9523 [https://bugs.python.org/issue?@action=redirect&bpo=9523] 中建议。)

ctypes

一个新类型 ctypes.c_ssize_t 用来表示 C ssize_t 数据类型。

site

The site module has three new functions useful for reporting on the details of a given Python installation.

  1. >>> import site
  2. >>> site.getsitepackages()
  3. ['LibraryFrameworks/Python.framework/Versions/3.2/lib/python3.2/sitepackages',
  4. 'LibraryFrameworks/Python.framework/Versions/3.2/lib/site-python',
  5. 'LibraryPython/3.2/sitepackages']
  6. >>> site.getuserbase()
  7. 'UsersraymondhettingerLibraryPython/3.2'
  8. >>> site.getusersitepackages()
  9. 'UsersraymondhettingerLibraryPython/3.2/lib/python/sitepackages'

Conveniently, some of site's functionality is accessible directly from the command-line:

  1. $ python -m site --userbase
  2. Usersraymondhettinger/.local
  3. $ python -m site --usersite
  4. Usersraymondhettinger/.local/lib/python3.2/sitepackages

(由 Tarek Ziadé 在 bpo-6693 [https://bugs.python.org/issue?@action=redirect&bpo=6693] 中贡献。)

sysconfig

The new sysconfig module makes it straightforward to discover installation paths and configuration variables that vary across platforms and installations.

The module offers access simple access functions for platform and version information:

It also provides access to the paths and variables corresponding to one of seven named schemes used by distutils. Those include posix_prefix, posix_home, posix_user, nt, nt_user, os2, os2_home:

  • get_paths() makes a dictionary containing installation paths for the current installation scheme.

  • get_config_vars() returns a dictionary of platform specific variables.

还有一个方便的命令行界面:

  1. C:\Python32>python -m sysconfig
  2. Platform: "win32"
  3. Python version: "3.2"
  4. Current installation scheme: "nt"
  5.  
  6. Paths:
  7. data = "C:\Python32"
  8. include = "C:\Python32\Include"
  9. platinclude = "C:\Python32\Include"
  10. platlib = "C:\Python32\Lib\sitepackages"
  11. platstdlib = "C:\Python32\Lib"
  12. purelib = "C:\Python32\Lib\sitepackages"
  13. scripts = "C:\Python32\Scripts"
  14. stdlib = "C:\Python32\Lib"
  15.  
  16. Variables:
  17. BINDIR = "C:\Python32"
  18. BINLIBDEST = "C:\Python32\Lib"
  19. EXE = ".exe"
  20. INCLUDEPY = "C:\Python32\Include"
  21. LIBDEST = "C:\Python32\Lib"
  22. SO = ".pyd"
  23. VERSION = "32"
  24. abiflags = ""
  25. base = "C:\Python32"
  26. exec_prefix = "C:\Python32"
  27. platbase = "C:\Python32"
  28. prefix = "C:\Python32"
  29. projectbase = "C:\Python32"
  30. py_version = "3.2"
  31. py_version_nodot = "32"
  32. py_version_short = "3.2"
  33. srcdir = "C:\Python32"
  34. userbase = "C:\Documents and Settings\Raymond\Application Data\Python"

(由TarekZiadé 移出Distutils。)

pdb

The pdb debugger module gained a number of usability improvements:

  • pdb.py now has a -c option that executes commands as given in a .pdbrc script file.

  • A .pdbrc script file can contain continue and next commands that continue debugging.

  • The Pdb class constructor now accepts a nosigint argument.

  • New commands: l(list), ll(long list) and source for listing source code.

  • New commands: display and undisplay for showing or hiding the value of an expression if it has changed.

  • New command: interact for starting an interactive interpreter containing the global and local names found in the current scope.

  • Breakpoints can be cleared by breakpoint number.

(由Georg Brandl, Antonio Cuni 和 Ilya Sandler 贡献。)

configparser

The configparser module was modified to improve usability and predictability of the default parser and its supported INI syntax. The old ConfigParser class was removed in favor of SafeConfigParser which has in turn been renamed to ConfigParser. Support for inline comments is now turned off by default and section or option duplicates are not allowed in a single configuration source.

Config parsers gained a new API based on the mapping protocol:

  1. >>> parser = ConfigParser()
  2. >>> parser.read_string("""
  3. ... [DEFAULT]
  4. ... location = upper left
  5. ... visible = yes
  6. ... editable = no
  7. ... color = blue
  8. ...
  9. ... [main]
  10. ... title = Main Menu
  11. ... color = green
  12. ...
  13. ... [options]
  14. ... title = Options
  15. ... """)
  16. >>> parser['main']['color']
  17. 'green'
  18. >>> parser['main']['editable']
  19. 'no'
  20. >>> section = parser['options']
  21. >>> section['title']
  22. 'Options'
  23. >>> section['title'] = 'Options (editable: %(editable)s)'
  24. >>> section['title']
  25. 'Options (editable: no)'

The new API is implemented on top of the classical API, so custom parser subclasses should be able to use it without modifications.

The INI file structure accepted by config parsers can now be customized. Users can specify alternative option/value delimiters and comment prefixes, change the name of the DEFAULT section or switch the interpolation syntax.

There is support for pluggable interpolation including an additional interpolation handler ExtendedInterpolation:

  1. >>> parser = ConfigParser(interpolation=ExtendedInterpolation())
  2. >>> parser.read_dict({'buildout': {'directory': 'homeambv/zope9'},
  3. ... 'custom': {'prefix': 'usrlocal'}})
  4. >>> parser.read_string("""
  5. ... [buildout]
  6. ... parts =
  7. ... zope9
  8. ... instance
  9. ... find-links =
  10. ... ${buildout:directory}/downloads/dist
  11. ...
  12. ... [zope9]
  13. ... recipe = plone.recipe.zope9install
  14. ... location = optzope
  15. ...
  16. ... [instance]
  17. ... recipe = plone.recipe.zope9instance
  18. ... zope9-location = ${zope9:location}
  19. ... zope-conf = ${custom:prefix}/etc/zope.conf
  20. ... """)
  21. >>> parser['buildout']['find-links']
  22. '\nhomeambv/zope9/downloads/dist'
  23. >>> parser['instance']['zope-conf']
  24. 'usrlocal/etc/zope.conf'
  25. >>> instance = parser['instance']
  26. >>> instance['zope-conf']
  27. 'usrlocal/etc/zope.conf'
  28. >>> instance['zope9-location']
  29. 'optzope'

A number of smaller features were also introduced, like support for specifying encoding in read operations, specifying fallback values for get-functions, or reading directly from dictionaries and strings.

(所有改变均由 Łukasz Langa 贡献。)

urllib.parse

A number of usability improvements were made for the urllib.parse module.

The urlparse() function now supports IPv6 [https://en.wikipedia.org/wiki/IPv6] addresses as described in RFC 2732 [https://datatracker.ietf.org/doc/html/rfc2732.html]:

  1. >>> import urllib.parse
  2. >>> urllib.parse.urlparse('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]foo')
  3. ParseResult(scheme='http',
  4. netloc='[dead:beef:cafe:5417:affe:8FA3:deaf:feed]',
  5. path='foo',
  6. params='',
  7. query='',
  8. fragment='')

The urldefrag() function now returns a named tuple:

  1. >>> r = urllib.parse.urldefrag('http://python.orgabout#target')
  2. >>> r
  3. DefragResult(url='http://python.orgabout', fragment='target')
  4. >>> r[0]
  5. 'http://python.orgabout'
  6. >>> r.fragment
  7. 'target'

And, the urlencode() function is now much more flexible, accepting either a string or bytes type for the query argument. If it is a string, then the safe, encoding, and error parameters are sent to quote_plus() for encoding:

  1. >>> urllib.parse.urlencode([
  2. ... ('type', 'telenovela'),
  3. ... ('name', '¿Dónde Está Elisa?')],
  4. ... encoding='latin-1')
  5. 'type=telenovela&name=%BFD%F3nde+Est%E1+Elisa%3F'

As detailed in 解析ASCII编码字节, all the urllib.parse functions now accept ASCII-encoded byte strings as input, so long as they are not mixed with regular strings. If ASCII-encoded byte strings are given as parameters, the return types will also be an ASCII-encoded byte strings:

  1. >>> urllib.parse.urlparse(b'http://www.python.org:80about')
  2. ParseResultBytes(scheme=b'http', netloc=b'www.python.org:80',
  3. path=b'about', params=b'', query=b'', fragment=b'')

(Work by Nick Coghlan, Dan Mahn, and Senthil Kumaran in bpo-2987 [https://bugs.python.org/issue?@action=redirect&bpo=2987], bpo-5468 [https://bugs.python.org/issue?@action=redirect&bpo=5468], and bpo-9873 [https://bugs.python.org/issue?@action=redirect&bpo=9873].)

mailbox

Thanks to a concerted effort by R. David Murray, the mailbox module has been fixed for Python 3.2. The challenge was that mailbox had been originally designed with a text interface, but email messages are best represented with bytes because various parts of a message may have different encodings.

The solution harnessed the email package's binary support for parsing arbitrary email messages. In addition, the solution required a number of API changes.

As expected, the add() method for mailbox.Mailbox objects now accepts binary input.

StringIO and text file input are deprecated. Also, string input will fail early if non-ASCII characters are used. Previously it would fail when the email was processed in a later step.

There is also support for binary output. The get_file() method now returns a file in the binary mode (where it used to incorrectly set the file to text-mode). There is also a new get_bytes() method that returns a bytes representation of a message corresponding to a given key.

It is still possible to get non-binary output using the old API's get_string() method, but that approach is not very useful. Instead, it is best to extract messages from a Message object or to load them from binary input.

(Contributed by R. David Murray, with efforts from Steffen Daode Nurpmeso and an initial patch by Victor Stinner in bpo-9124 [https://bugs.python.org/issue?@action=redirect&bpo=9124].)

turtledemo

The demonstration code for the turtle module was moved from the Demo directory to main library. It includes over a dozen sample scripts with lively displays. Being on sys.path, it can now be run directly from the command-line:

  1. $ python -m turtledemo

(Moved from the Demo directory by Alexander Belopolsky in bpo-10199 [https://bugs.python.org/issue?@action=redirect&bpo=10199].)

多线程

  • The mechanism for serializing execution of concurrently running Python threads (generally known as the GIL or Global Interpreter Lock) has been rewritten. Among the objectives were more predictable switching intervals and reduced overhead due to lock contention and the number of ensuing system calls. The notion of a "check interval" to allow thread switches has been abandoned and replaced by an absolute duration expressed in seconds. This parameter is tunable through sys.setswitchinterval(). It currently defaults to 5 milliseconds.

Additional details about the implementation can be read from a python-dev mailing-list message [https://mail.python.org/pipermail/python-dev/2009-October/093321.html] (however, "priority requests" as exposed in this message have not been kept for inclusion).

(由 Antoine Pitrou 贡献。)

性能优化

A number of small performance enhancements have been added:

  • Python's peephole optimizer now recognizes patterns such x in {1, 2, 3} as being a test for membership in a set of constants. The optimizer recasts the set as a frozenset and stores the pre-built constant.

Now that the speed penalty is gone, it is practical to start writing membership tests using set-notation. This style is both semantically clear and operationally fast:

  1. extension = name.rpartition('.')[2]
  2. if extension in {'xml', 'html', 'xhtml', 'css'}:
  3. handle(name)

(补丁和附加测试由 Dave Malcolm 在 bpo-6690 [https://bugs.python.org/issue?@action=redirect&bpo=6690] 中贡献)。

  • Serializing and unserializing data using the pickle module is now several times faster.

(由 Alexandre Vassalotti, Antoine Pitrou 和 Unladen Swallow 团队在 bpo-9410 [https://bugs.python.org/issue?@action=redirect&bpo=9410] 和 bpo-3873 [https://bugs.python.org/issue?@action=redirect&bpo=3873] 中贡献。)

  • The Timsort algorithm [https://en.wikipedia.org/wiki/Timsort] used in list.sort() and sorted() now runs faster and uses less memory when called with a key function. Previously, every element of a list was wrapped with a temporary object that remembered the key value associated with each element. Now, two arrays of keys and values are sorted in parallel. This saves the memory consumed by the sort wrappers, and it saves time lost to delegating comparisons.

(Patch by Daniel Stutzbach in bpo-9915 [https://bugs.python.org/issue?@action=redirect&bpo=9915].)

  • JSON decoding performance is improved and memory consumption is reduced whenever the same string is repeated for multiple keys. Also, JSON encoding now uses the C speedups when the sort_keys argument is true.

(由Antoine Pitrou 在 bpo-7451 [https://bugs.python.org/issue?@action=redirect&bpo=7451] 中贡献,由 Raymond Hettinger 和 Antoine Pitrou 在 bpo-10314 [https://bugs.python.org/issue?@action=redirect&bpo=10314] 中贡献。)

  • Recursive locks (created with the threading.RLock() API) now benefit from a C implementation which makes them as fast as regular locks, and between 10x and 15x faster than their previous pure Python implementation.

(由 Antoine Pitrou 在 bpo-3001 [https://bugs.python.org/issue?@action=redirect&bpo=3001] 中贡献。)

(Patch by Florent Xicluna in bpo-7622 [https://bugs.python.org/issue?@action=redirect&bpo=7622] and bpo-7462 [https://bugs.python.org/issue?@action=redirect&bpo=7462].)

  • Integer to string conversions now work two "digits" at a time, reducing the number of division and modulo operations.

(bpo-6713 [https://bugs.python.org/issue?@action=redirect&bpo=6713] by Gawain Bolton, Mark Dickinson, and Victor Stinner.)

There were several other minor optimizations. Set differencing now runs faster when one operand is much larger than the other (patch by Andress Bennetts in bpo-8685 [https://bugs.python.org/issue?@action=redirect&bpo=8685]). The array.repeat() method has a faster implementation (bpo-1569291 [https://bugs.python.org/issue?@action=redirect&bpo=1569291] by Alexander Belopolsky). The BaseHTTPRequestHandler has more efficient buffering (bpo-3709 [https://bugs.python.org/issue?@action=redirect&bpo=3709] by Andrew Schaaf). The operator.attrgetter() function has been sped-up (bpo-10160 [https://bugs.python.org/issue?@action=redirect&bpo=10160] by Christos Georgiou). And ConfigParser loads multiline arguments a bit faster (bpo-7113 [https://bugs.python.org/issue?@action=redirect&bpo=7113] by Łukasz Langa).

Unicode

Python has been updated to Unicode 6.0.0 [https://unicode.org/versions/Unicode6.0.0/]. The update to the standard adds over 2,000 new characters including emoji [https://en.wikipedia.org/wiki/Emoji] symbols which are important for mobile phones.

In addition, the updated standard has altered the character properties for two Kannada characters (U+0CF1, U+0CF2) and one New Tai Lue numeric character (U+19DA), making the former eligible for use in identifiers while disqualifying the latter. For more information, see Unicode Character Database Changes [https://www.unicode.org/versions/Unicode6.0.0/#Database_Changes].

编解码器

Support was added for cp720 Arabic DOS encoding (bpo-1616979 [https://bugs.python.org/issue?@action=redirect&bpo=1616979]).

MBCS encoding no longer ignores the error handler argument. In the default strict mode, it raises an UnicodeDecodeError when it encounters an undecodable byte sequence and an UnicodeEncodeError for an unencodable character.

The MBCS codec supports 'strict' and 'ignore' error handlers for decoding, and 'strict' and 'replace' for encoding.

To emulate Python3.1 MBCS encoding, select the 'ignore' handler for decoding and the 'replace' handler for encoding.

On Mac OS X, Python decodes command line arguments with 'utf-8' rather than the locale encoding.

By default, tarfile uses 'utf-8' encoding on Windows (instead of 'mbcs') and the 'surrogateescape' error handler on all operating systems.

文档

文档继续得到改进。

  • A table of quick links has been added to the top of lengthy sections such as 内置函数. In the case of itertools, the links are accompanied by tables of cheatsheet-style summaries to provide an overview and memory jog without having to read all of the docs.

  • In some cases, the pure Python source code can be a helpful adjunct to the documentation, so now many modules now feature quick links to the latest version of the source code. For example, the functools module documentation has a quick link at the top labeled:

(由 Raymond Hettinger 贡献,参见 rationale [https://rhettinger.wordpress.com/2011/01/28/open-your-source-more/]。)

  • The docs now contain more examples and recipes. In particular, re module has an extensive section, 正则表达式例子. Likewise, the itertools module continues to be updated with new itertools 配方.

  • The datetime module now has an auxiliary implementation in pure Python. No functionality was changed. This just provides an easier-to-read alternate implementation.

(由 Alexander Belopolsky 在 bpo-9528 [https://bugs.python.org/issue?@action=redirect&bpo=9528] 中贡献。)

  • The unmaintained Demo directory has been removed. Some demos were integrated into the documentation, some were moved to the Tools/demo directory, and others were removed altogether.

(由 Georg Brandl 在 bpo-7962 [https://bugs.python.org/issue?@action=redirect&bpo=7962] 中贡献)

IDLE

  • The format menu now has an option to clean source files by stripping trailing whitespace.

(由 Raymond Hettinger 在 bpo-5150 [https://bugs.python.org/issue?@action=redirect&bpo=5150] 中贡献。)

  • IDLE on Mac OS X now works with both Carbon AquaTk and Cocoa AquaTk.

(由 Kevin Walzer, Ned Deily 和 Ronald Oussoren 在 bpo-6075 [https://bugs.python.org/issue?@action=redirect&bpo=6075] 中贡献。)

代码库

In addition to the existing Subversion code repository at https://svn.python.org there is now a Mercurial [https://www.mercurial-scm.org/] repository at https://hg.python.org/.

After the 3.2 release, there are plans to switch to Mercurial as the primary repository. This distributed version control system should make it easier for members of the community to create and share external changesets. See PEP 385 [https://peps.python.org/pep-0385/] for details.

To learn to use the new version control system, see the Quick Start [https://www.mercurial-scm.org/wiki/QuickStart] or the Guide to Mercurial Workflows [https://www.mercurial-scm.org/guide].

构建和 C API 的改变

针对 Python 构建过程和 C API 的改变包括:

  • The idle, pydoc and 2to3 scripts are now installed with a version-specific suffix on make altinstall (bpo-10679 [https://bugs.python.org/issue?@action=redirect&bpo=10679]).

  • The C functions that access the Unicode Database now accept and return characters from the full Unicode range, even on narrow unicode builds (Py_UNICODE_TOLOWER, Py_UNICODE_ISDECIMAL, and others). A visible difference in Python is that unicodedata.numeric() now returns the correct value for large code points, and repr() may consider more characters as printable.

(Reported by Bupjoe Lee and fixed by Amaury Forgeot D'Arc; bpo-5127 [https://bugs.python.org/issue?@action=redirect&bpo=5127].)

  • Computed gotos are now enabled by default on supported compilers (which are detected by the configure script). They can still be disabled selectively by specifying --without-computed-gotos.

(由 Antoine Pitrou 在 bpo-9203 [https://bugs.python.org/issue?@action=redirect&bpo=9203] 中贡献。)

  • The option --with-wctype-functions was removed. The builtin unicode database is now used for all functions.

(由 Amaury Forgeot d'Arc 在 bpo-9210 [https://bugs.python.org/issue?@action=redirect&bpo=9210] 中贡献。)

  • Hash values are now values of a new type, Py_hash_t, which is defined to be the same size as a pointer. Previously they were of type long, which on some 64-bit operating systems is still only 32 bits long. As a result of this fix, set and dict can now hold more than 2**32 entries on builds with 64-bit pointers (previously, they could grow to that size but their performance degraded catastrophically).

(Suggested by Raymond Hettinger and implemented by Benjamin Peterson; bpo-9778 [https://bugs.python.org/issue?@action=redirect&bpo=9778].)

There were a number of other small changes to the C-API. See the Misc/NEWS [https://github.com/python/cpython/blob/v3.2.6/Misc/NEWS] file for a complete list.

Also, there were a number of updates to the Mac OS X build, see Mac/BuildScript/README.txt [https://github.com/python/cpython/blob/v3.2.6/Mac/BuildScript/README.txt] for details. For users running a 32/64-bit build, there is a known problem with the default Tcl/Tk on Mac OS X 10.6. Accordingly, we recommend installing an updated alternative such as ActiveState Tcl/Tk 8.5.9 [https://web.archive.org/web/20101208191259/https://www.activestate.com/activetcl/downloads]. See https://www.python.org/download/mac/tcltk/ for additional details.

移植到 Python 3.2

本节列出了先前描述的改变以及可能需要修改你的代码的其他问题修正:

  • The configparser module has a number of cleanups. The major change is to replace the old ConfigParser class with long-standing preferred alternative SafeConfigParser. In addition there are a number of smaller incompatibilities:

    • The interpolation syntax is now validated on get() and set() operations. In the default interpolation scheme, only two tokens with percent signs are valid: %(name)s and %%, the latter being an escaped percent sign.

    • The set() and add_section() methods now verify that values are actual strings. Formerly, unsupported types could be introduced unintentionally.

    • Duplicate sections or options from a single source now raise either DuplicateSectionError or DuplicateOptionError. Formerly, duplicates would silently overwrite a previous entry.

    • Inline comments are now disabled by default so now the ; character can be safely used in values.

    • Comments now can be indented. Consequently, for ; or # to appear at the start of a line in multiline values, it has to be interpolated. This keeps comment prefix characters in values from being mistaken as comments.

    • "" is now a valid value and is no longer automatically converted to an empty string. For empty strings, use "option =" in a line.

  • The nntplib module was reworked extensively, meaning that its APIs are often incompatible with the 3.1 APIs.

  • bytearray objects can no longer be used as filenames; instead, they should be converted to bytes.

  • The array.tostring() and array.fromstring() have been renamed to array.tobytes() and array.frombytes() for clarity. The old names have been deprecated. (See bpo-8990 [https://bugs.python.org/issue?@action=redirect&bpo=8990].)

  • PyArg_Parse*() 函数:

    • "t#" 格式已被移除:改用 "s#" 或 "s*"

    • "w" 和 "w#" 格式已被移除:改用 "w*"

  • The PyCObject type, deprecated in 3.1, has been removed. To wrap opaque C pointers in Python objects, the PyCapsule API should be used instead; the new type has a well-defined interface for passing typing safety information and a less complicated signature for calling a destructor.

  • The sys.setfilesystemencoding() function was removed because it had a flawed design.

  • The random.seed() function and method now salt string seeds with an sha512 hash function. To access the previous version of seed in order to reproduce Python 3.1 sequences, set the version argument to 1, random.seed(s, version=1).

  • The previously deprecated string.maketrans() function has been removed in favor of the static methods bytes.maketrans() and bytearray.maketrans(). This change solves the confusion around which types were supported by the string module. Now, str, bytes, and bytearray each have their own maketrans and translate methods with intermediate translation tables of the appropriate type.

(由Georg Brandl在 bpo-5675 [https://bugs.python.org/issue?@action=redirect&bpo=5675] 中贡献)

  • The previously deprecated contextlib.nested() function has been removed in favor of a plain with statement which can accept multiple context managers. The latter technique is faster (because it is builtin), and it does a better job finalizing multiple context managers when one of them raises an exception:
  1. with open('mylog.txt') as infile, open('a.out', 'w') as outfile:
  2. for line in infile:
  3. if '<critical>' in line:
  4. outfile.write(line)

(由 Georg Brandl 和 Mattias Brändström 贡献; appspot issue 53094 [https://codereview.appspot.com/53094]。)

  • struct.pack() now only allows bytes for the s string pack code. Formerly, it would accept text arguments and implicitly encode them to bytes using UTF-8. This was problematic because it made assumptions about the correct encoding and because a variable-length encoding can fail when writing to fixed length segment of a structure.

Code such as struct.pack('<6sHHBBB', 'GIF87a', x, y) should be rewritten with to use bytes instead of text, struct.pack('<6sHHBBB', b'GIF87a', x, y).

(Discovered by David Beazley and fixed by Victor Stinner; bpo-10783 [https://bugs.python.org/issue?@action=redirect&bpo=10783].)

  • The xml.etree.ElementTree class now raises an xml.etree.ElementTree.ParseError when a parse fails. Previously it raised an xml.parsers.expat.ExpatError.

  • The new, longer str() value on floats may break doctests which rely on the old output format.

  • In subprocess.Popen, the default value for close_fds is now True under Unix; under Windows, it is True if the three standard streams are set to None, False otherwise. Previously, close_fds was always False by default, which produced difficult to solve bugs or race conditions when open file descriptors would leak into the child process.

  • Support for legacy HTTP 0.9 has been removed from urllib.request and http.client. Such support is still present on the server side (in http.server).

(由 Antoine Pitrou 在 bpo-10711 [https://bugs.python.org/issue?@action=redirect&bpo=10711] 中贡献。)

(由 Antoine Pitrou 在 bpo-10272 [https://bugs.python.org/issue?@action=redirect&bpo=10272] 中贡献。)

  • The misleading functions PyEval_AcquireLock() and PyEval_ReleaseLock() have been officially deprecated. The thread-state aware APIs (such as PyEval_SaveThread() and PyEval_RestoreThread()) should be used instead.

  • Due to security risks, asyncore.handle_accept() has been deprecated, and a new function, asyncore.handle_accepted(), was added to replace it.

(由 Giampaolo Rodola 在 bpo-6706 [https://bugs.python.org/issue?@action=redirect&bpo=6706] 中贡献。)

  • Due to the new GIL implementation, PyEval_InitThreads() cannot be called before Py_Initialize() anymore.