PEP 3129: 类装饰器

装饰器已从函数扩展到类。 现在可以合法地编写:

  1. @foo
  2. @bar
  3. class A:
  4. pass

这相当于:

  1. class A:
  2. pass
  3.  
  4. A = foo(bar(A))

参见

PEP 3141: 数字的类型层级结构

Python 3.0 adds several abstract base classes for numeric types inspired by Scheme's numeric tower. These classes were backported to 2.6 as the numbers module.

The most general ABC is Number. It defines no operations at all, and only exists to allow checking if an object is a number by doing isinstance(obj, Number).

Complex is a subclass of Number. Complex numbers can undergo the basic operations of addition, subtraction, multiplication, division, and exponentiation, and you can retrieve the real and imaginary parts and obtain a number's conjugate. Python's builtin complex type is an implementation of Complex.

Real further derives from Complex, and adds operations that only work on real numbers: floor(), trunc(), rounding, taking the remainder mod N, floor division, and comparisons.

Rational numbers derive from Real, have numerator and denominator properties, and can be converted to floats. Python 2.6 adds a simple rational-number class, Fraction, in the fractions module. (It's called Fraction instead of Rational to avoid a name clash with numbers.Rational.)

Integral numbers derive from Rational, and can be shifted left and right with << and >>, combined using bitwise operations such as & and |, and can be used as array indexes and slice boundaries.

In Python 3.0, the PEP slightly redefines the existing builtins round(), math.floor(), math.ceil(), and adds a new one, math.trunc(), that's been backported to Python 2.6. math.trunc() rounds toward zero, returning the closest Integral that's between the function's argument and zero.

参见

Scheme's numerical tower [https://www.gnu.org/software/guile/manual/html_node/Numerical-Tower.html#Numerical-Tower], from the Guile manual.

Scheme's number datatypes [https://conservatory.scheme.org/schemers/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2] from the R5RS Scheme specification.

fractions 模块

To fill out the hierarchy of numeric types, the fractions module provides a rational-number class. Rational numbers store their values as a numerator and denominator forming a fraction, and can exactly represent numbers such as 2/3 that floating-point numbers can only approximate.

The Fraction constructor takes two Integral values that will be the numerator and denominator of the resulting fraction.

  1. >>> from fractions import Fraction
  2. >>> a = Fraction(2, 3)
  3. >>> b = Fraction(2, 5)
  4. >>> float(a), float(b)
  5. (0.66666666666666663, 0.40000000000000002)
  6. >>> a+b
  7. Fraction(16, 15)
  8. >>> a/b
  9. Fraction(5, 3)

For converting floating-point numbers to rationals, the float type now has an as_integer_ratio() method that returns the numerator and denominator for a fraction that evaluates to the same floating-point value:

  1. >>> (2.5) .as_integer_ratio()
  2. (5, 2)
  3. >>> (3.1415) .as_integer_ratio()
  4. (7074029114692207L, 2251799813685248L)
  5. >>> (1./3) .as_integer_ratio()
  6. (6004799503160661L, 18014398509481984L)

Note that values that can only be approximated by floating-point numbers, such as 1./3, are not simplified to the number being approximated; the fraction attempts to match the floating-point value exactly.

The fractions module is based upon an implementation by Sjoerd Mullender that was in Python's Demo/classes/ directory for a long time. This implementation was significantly updated by Jeffrey Yasskin.

其他语言特性修改

对Python 语言核心进行的小改动:

  • Directories and zip archives containing a __main__.py file can now be executed directly by passing their name to the interpreter. The directory or zip archive is automatically inserted as the first entry in sys.path. (Suggestion and initial patch by Andy Chu, subsequently revised by Phillip J. Eby and Nick Coghlan; bpo-1739468 [https://bugs.python.org/issue?@action=redirect&bpo=1739468].)

  • The hasattr() function was catching and ignoring all errors, under the assumption that they meant a __getattr__() method was failing somehow and the return value of hasattr() would therefore be False. This logic shouldn't be applied to KeyboardInterrupt and SystemExit, however; Python 2.6 will no longer discard such exceptions when hasattr() encounters them. (Fixed by Benjamin Peterson; bpo-2196 [https://bugs.python.org/issue?@action=redirect&bpo=2196].)

  • When calling a function using the ** syntax to provide keyword arguments, you are no longer required to use a Python dictionary; any mapping will now work:

  1. >>> def f(**kw):
  2. ... print sorted(kw)
  3. ...
  4. >>> ud=UserDict.UserDict()
  5. >>> ud['a'] = 1
  6. >>> ud['b'] = 'string'
  7. >>> f(**ud)
  8. ['a', 'b']

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

在函数调用的 *args 参数之后提供关键字参数也是合法的。

  1. >>> def f(*args, **kw):
  2. ... print args, kw
  3. ...
  4. >>> f(1,2,3, *(4,5,6), keyword=13)
  5. (1, 2, 3, 4, 5, 6) {'keyword': 13}

在之前版本中这会导致语法错误。 (由 Amaury Forgeot d'Arc 贡献;bpo-3473 [https://bugs.python.org/issue?@action=redirect&bpo=3473]。)

  • A new builtin, next(iterator, [default]) returns the next item from the specified iterator. If the default argument is supplied, it will be returned if iterator has been exhausted; otherwise, the StopIteration exception will be raised. (Backported in bpo-2719 [https://bugs.python.org/issue?@action=redirect&bpo=2719].)

  • Tuples now have index() and count() methods matching the list type's index() and count() methods:

  1. >>> t = (0,1,2,3,4,0,1,2)
  2. >>> t.index(3)
  3. 3
  4. >>> t.count(0)
  5. 2

(由 Raymond Hettinger 贡献)

  • The builtin types now have improved support for extended slicing syntax, accepting various combinations of (start, stop, step). Previously, the support was partial and certain corner cases wouldn't work. (Implemented by Thomas Wouters.)

  • Properties now have three attributes, getter, setter and deleter, that are decorators providing useful shortcuts for adding a getter, setter or deleter function to an existing property. You would use them like this:

  1. class C(object):
  2. @property
  3. def x(self):
  4. return self._x
  5.  
  6. @x.setter
  7. def x(self, value):
  8. self._x = value
  9.  
  10. @x.deleter
  11. def x(self):
  12. del self._x
  13.  
  14. class D(C):
  15. @C.x.getter
  16. def x(self):
  17. return self._x * 2
  18.  
  19. @x.setter
  20. def x(self, value):
  21. self._x = value / 2
  • Several methods of the builtin set types now accept multiple iterables: intersection(), intersection_update(), union(), update(), difference() and difference_update().
  1. >>> s=set('1234567890')
  2. >>> s.intersection('abc123', 'cdf246') # 所有输入的交集
  3. set(['2'])
  4. >>> s.difference('246', '789')
  5. set(['1', '0', '3', '5'])

(由 Raymond Hettinger 贡献。)

Other functions in the math module, isinf() and isnan(), return true if their floating-point argument is infinite or Not A Number. (bpo-1640 [https://bugs.python.org/issue?@action=redirect&bpo=1640])

Conversion functions were added to convert floating-point numbers into hexadecimal strings (bpo-3008 [https://bugs.python.org/issue?@action=redirect&bpo=3008]). These functions convert floats to and from a string representation without introducing rounding errors from the conversion between decimal and binary. Floats have a hex() method that returns a string representation, and the float.fromhex() method converts a string back into a number:

  1. >>> a = 3.75
  2. >>> a.hex()
  3. '0x1.e000000000000p+1'
  4. >>> float.fromhex('0x1.e000000000000p+1')
  5. 3.75
  6. >>> b=1./3
  7. >>> b.hex()
  8. '0x1.5555555555555p-2'
  • A numerical nicety: when creating a complex number from two floats on systems that support signed zeros (-0 and +0), the complex() constructor will now preserve the sign of the zero. (Fixed by Mark T. Dickinson; bpo-1507 [https://bugs.python.org/issue?@action=redirect&bpo=1507].)

  • Classes that inherit a __hash__() method from a parent class can set __hash__ = None to indicate that the class isn't hashable. This will make hash(obj) raise a TypeError and the class will not be indicated as implementing the Hashable ABC.

You should do this when you've defined a __cmp__() or __eq__() method that compares objects by their value rather than by identity. All objects have a default hash method that uses id(obj) as the hash value. There's no tidy way to remove the __hash__() method inherited from a parent class, so assigning None was implemented as an override. At the C level, extensions can set tp_hash to PyObject_HashNotImplemented(). (Fixed by Nick Coghlan and Amaury Forgeot d'Arc; bpo-2235 [https://bugs.python.org/issue?@action=redirect&bpo=2235].)

性能优化

  • The warnings module has been rewritten in C. This makes it possible to invoke warnings from the parser, and may also make the interpreter's startup faster. (Contributed by Neal Norwitz and Brett Cannon; bpo-1631171 [https://bugs.python.org/issue?@action=redirect&bpo=1631171].)

  • Type objects now have a cache of methods that can reduce the work required to find the correct method implementation for a particular class; once cached, the interpreter doesn't need to traverse base classes to figure out the right method to call. The cache is cleared if a base class or the class itself is modified, so the cache should remain correct even in the face of Python's dynamic nature. (Original optimization implemented by Armin Rigo, updated for Python 2.6 by Kevin Jacobs; bpo-1700288 [https://bugs.python.org/issue?@action=redirect&bpo=1700288].)

By default, this change is only applied to types that are included with the Python core. Extension modules may not necessarily be compatible with this cache, so they must explicitly add Py_TPFLAGS_HAVE_VERSION_TAG to the module's tp_flags field to enable the method cache. (To be compatible with the method cache, the extension module's code must not directly access and modify the tp_dict member of any of the types it implements. Most modules don't do this, but it's impossible for the Python interpreter to determine that. See bpo-1878 [https://bugs.python.org/issue?@action=redirect&bpo=1878] for some discussion.)

  • Function calls that use keyword arguments are significantly faster by doing a quick pointer comparison, usually saving the time of a full string comparison. (Contributed by Raymond Hettinger, after an initial implementation by Antoine Pitrou; bpo-1819 [https://bugs.python.org/issue?@action=redirect&bpo=1819].)

  • All of the functions in the struct module have been rewritten in C, thanks to work at the Need For Speed sprint. (Contributed by Raymond Hettinger.)

  • Some of the standard builtin types now set a bit in their type objects. This speeds up checking whether an object is a subclass of one of these types. (Contributed by Neal Norwitz.)

  • Unicode strings now use faster code for detecting whitespace and line breaks; this speeds up the split() method by about 25% and splitlines() by 35%. (Contributed by Antoine Pitrou.) Memory usage is reduced by using pymalloc for the Unicode string's data.

  • The with statement now stores the __exit__() method on the stack, producing a small speedup. (Implemented by Jeffrey Yasskin.)

  • To reduce memory usage, the garbage collector will now clear internal free lists when garbage-collecting the highest generation of objects. This may return memory to the operating system sooner.

解释器改动

Two command-line options have been reserved for use by other Python implementations. The -J switch has been reserved for use by Jython for Jython-specific options, such as switches that are passed to the underlying JVM. -X has been reserved for options specific to a particular implementation of Python such as CPython, Jython, or IronPython. If either option is used with Python 2.6, the interpreter will report that the option isn't currently used.

Python can now be prevented from writing .pyc or .pyo files by supplying the -B switch to the Python interpreter, or by setting the PYTHONDONTWRITEBYTECODE environment variable before running the interpreter. This setting is available to Python programs as the sys.dont_write_bytecode variable, and Python code can change the value to modify the interpreter's behaviour. (Contributed by Neal Norwitz and Georg Brandl.)

The encoding used for standard input, output, and standard error can be specified by setting the PYTHONIOENCODING environment variable before running the interpreter. The value should be a string in the form or :. The encoding part specifies the encoding's name, e.g. utf-8 or latin-1; the optional errorhandler part specifies what to do with characters that can't be handled by the encoding, and should be one of "error", "ignore", or "replace". (Contributed by Martin von Löwis.)

新增和改进的模块

As in every release, Python's standard library received a number of enhancements and bug fixes. Here's a partial list of the most notable changes, sorted alphabetically by module name. Consult the Misc/NEWS file in the source tree for a more complete list of changes, or look through the Subversion logs for all the details.

The bsddb.dbshelve module now uses the highest pickling protocol available, instead of restricting itself to protocol 1. (Contributed by W. Barnes.)

The parse_qs() and parse_qsl() functions have been relocated from the cgi module to the urlparse module. The versions still available in the cgi module will trigger PendingDeprecationWarning messages in 2.6 (bpo-600362 [https://bugs.python.org/issue?@action=redirect&bpo=600362]).

  • The cmath module underwent extensive revision, contributed by Mark Dickinson and Christian Heimes. Five new functions were added:

    • polar() converts a complex number to polar form, returning the modulus and argument of the complex number.

    • rect() does the opposite, turning a modulus, argument pair back into the corresponding complex number.

    • phase() returns the argument (also called the angle) of a complex number.

    • isnan() returns True if either the real or imaginary part of its argument is a NaN.

    • isinf() returns True if either the real or imaginary part of its argument is infinite.

The revisions also improved the numerical soundness of the cmath module. For all functions, the real and imaginary parts of the results are accurate to within a few units of least precision (ulps) whenever possible. See bpo-1381 [https://bugs.python.org/issue?@action=redirect&bpo=1381] for the details. The branch cuts for asinh(), atanh(): and atan() have also been corrected.

The tests for the module have been greatly expanded; nearly 2000 new test cases exercise the algebraic functions.

On IEEE 754 platforms, the cmath module now handles IEEE 754 special values and floating-point exceptions in a manner consistent with Annex 'G' of the C99 standard.

  • A new data type in the collections module: namedtuple(typename, fieldnames) is a factory function that creates subclasses of the standard tuple whose fields are accessible by name as well as index. For example:
  1. >>> var_type = collections.namedtuple('variable',
  2. ... 'id name type size')
  3. >>> # Names are separated by spaces or commas.
  4. >>> # 'id, name, type, size' would also work.
  5. >>> var_type._fields
  6. ('id', 'name', 'type', 'size')
  7.  
  8. >>> var = var_type(1, 'frequency', 'int', 4)
  9. >>> print var[0], var.id # Equivalent
  10. 1 1
  11. >>> print var[2], var.type # Equivalent
  12. int int
  13. >>> var._asdict()
  14. {'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'}
  15. >>> v2 = var._replace(name='amplitude')
  16. >>> v2
  17. variable(id=1, name='amplitude', type='int', size=4)

Several places in the standard library that returned tuples have been modified to return namedtuple() instances. For example, the Decimal.as_tuple() method now returns a named tuple with sign, digits, and exponent fields.

(由 Raymond Hettinger 贡献。)

  • Another change to the collections module is that the deque type now supports an optional maxlen parameter; if supplied, the deque's size will be restricted to no more than maxlen items. Adding more items to a full deque causes old items to be discarded.
  1. >>> from collections import deque
  2. >>> dq=deque(maxlen=3)
  3. >>> dq
  4. deque([], maxlen=3)
  5. >>> dq.append(1); dq.append(2); dq.append(3)
  6. >>> dq
  7. deque([1, 2, 3], maxlen=3)
  8. >>> dq.append(4)
  9. >>> dq
  10. deque([2, 3, 4], maxlen=3)

(由 Raymond Hettinger 贡献。)

  1. # Boldface text starting at y=0,x=21
  2. # and affecting the rest of the line.
  3. stdscr.chgat(0, 21, curses.A_BOLD)

The Textbox class in the curses.textpad module now supports editing in insert mode as well as overwrite mode. Insert mode is enabled by supplying a true value for the insert_mode parameter when creating the Textbox instance.

  1. >>> Decimal(1).exp()
  2. Decimal("2.718281828459045235360287471")
  3. >>> Decimal("2.7182818").ln()
  4. Decimal("0.9999999895305022877376682436")
  5. >>> Decimal(1000).log10()
  6. Decimal("3")

现在 Decimal 对象的 as_tuple() 方法将返回一个由 sign, digitsexponent 字段组成的具名元组。

(由 Facundo Batista 和 Mark Dickinson 实现。 具名元组支持由 Raymond Hettinger 添加。)

  • 现在 difflib 模块的 SequenceMatcher 类将返回代表匹配结果的具名元组,包含 a, bsize 等属性。 (由 Raymond Hettinger 贡献。)

  • An optional timeout parameter, specifying a timeout measured in seconds, was added to the ftplib.FTP class constructor as well as the connect() method. (Added by Facundo Batista.) Also, the FTP class's storbinary() and storlines() now take an optional callback parameter that will be called with each block of data after the data has been sent. (Contributed by Phil Schwartz; bpo-1221598 [https://bugs.python.org/issue?@action=redirect&bpo=1221598].)

  • The reduce() builtin function is also available in the functools module. In Python 3.0, the builtin has been dropped and reduce() is only available from functools; currently there are no plans to drop the builtin in the 2.x series. (Patched by Christian Heimes; bpo-1739906 [https://bugs.python.org/issue?@action=redirect&bpo=1739906].)

  • When possible, the getpass module will now use devtty to print a prompt message and read the password, falling back to standard error and standard input. If the password may be echoed to the terminal, a warning is printed before the prompt is displayed. (Contributed by Gregory P. Smith.)

  • The glob.glob() function can now return Unicode filenames if a Unicode path was used and Unicode filenames are matched within the directory. (bpo-1001604 [https://bugs.python.org/issue?@action=redirect&bpo=1001604])

  • A new function in the heapq module, merge(iter1, iter2, …), takes any number of iterables returning data in sorted order, and returns a new generator that returns the contents of all the iterators, also in sorted order. For example:

  1. >>> list(heapq.merge([1, 3, 5, 9], [2, 8, 16]))
  2. [1, 2, 3, 5, 8, 9, 16]

Another new function, heappushpop(heap, item), pushes item onto heap, then pops off and returns the smallest item. This is more efficient than making a call to heappush() and then heappop().

heapq is now implemented to only use less-than comparison, instead of the less-than-or-equal comparison it previously used. This makes heapq's usage of a type match the list.sort() method. (Contributed by Raymond Hettinger.)

  • An optional timeout parameter, specifying a timeout measured in seconds, was added to the httplib.HTTPConnection and HTTPSConnection class constructors. (Added by Facundo Batista.)

  • Most of the inspect module's functions, such as getmoduleinfo() and getargs(), now return named tuples. In addition to behaving like tuples, the elements of the return value can also be accessed as attributes. (Contributed by Raymond Hettinger.)

此模块中的新增函数包括 isgenerator(), isgeneratorfunction()isabstract()

izip_longest(iter1, iter2, …[, fillvalue]) makes tuples from each of the elements; if some of the iterables are shorter than others, the missing values are set to fillvalue. For example:

  1. >>> tuple(itertools.izip_longest([1,2,3], [1,2,3,4,5]))
  2. ((1, 1), (2, 2), (3, 3), (None, 4), (None, 5))

product(iter1, iter2, …, [repeat=N]) returns the Cartesian product of the supplied iterables, a set of tuples containing every possible combination of the elements returned from each iterable.

  1. >>> list(itertools.product([1,2,3], [4,5,6]))
  2. [(1, 4), (1, 5), (1, 6),
  3. (2, 4), (2, 5), (2, 6),
  4. (3, 4), (3, 5), (3, 6)]

The optional repeat keyword argument is used for taking the product of an iterable or a set of iterables with themselves, repeated N times. With a single iterable argument, N-tuples are returned:

  1. >>> list(itertools.product([1,2], repeat=3))
  2. [(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),
  3. (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]

With two iterables, 2N-tuples are returned.

  1. >>> list(itertools.product([1,2], [3,4], repeat=2))
  2. [(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4),
  3. (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4),
  4. (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4),
  5. (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)]

combinations(iterable, r) 基于 iterable 的元素返回长度为 r 的子序列。

  1. >>> list(itertools.combinations('123', 2))
  2. [('1', '2'), ('1', '3'), ('2', '3')]
  3. >>> list(itertools.combinations('123', 3))
  4. [('1', '2', '3')]
  5. >>> list(itertools.combinations('1234', 3))
  6. [('1', '2', '3'), ('1', '2', '4'),
  7. ('1', '3', '4'), ('2', '3', '4')]

permutations(iter[, r]) returns all the permutations of length r of the iterable's elements. If r is not specified, it will default to the number of elements produced by the iterable.

  1. >>> list(itertools.permutations([1,2,3,4], 2))
  2. [(1, 2), (1, 3), (1, 4),
  3. (2, 1), (2, 3), (2, 4),
  4. (3, 1), (3, 2), (3, 4),
  5. (4, 1), (4, 2), (4, 3)]

itertools.chain(*iterables) is an existing function in itertools that gained a new constructor in Python 2.6. itertools.chain.from_iterable(iterable) takes a single iterable that should return other iterables. chain() will then return all the elements of the first iterable, then all the elements of the second, and so on.

  1. >>> list(itertools.chain.from_iterable([[1,2,3], [4,5,6]]))
  2. [1, 2, 3, 4, 5, 6]

(全部由 Raymond Hettinger 贡献。)

  • The logging module's FileHandler class and its subclasses WatchedFileHandler, RotatingFileHandler, and TimedRotatingFileHandler now have an optional delay parameter to their constructors. If delay is true, opening of the log file is deferred until the first emit() call is made. (Contributed by Vinay Sajip.)

TimedRotatingFileHandler also has a utc constructor parameter. If the argument is true, UTC time will be used in determining when midnight occurs and in generating filenames; otherwise local time will be used.

  • math 模块添加了一些新函数:

  • The math module has been improved to give more consistent behaviour across platforms, especially with respect to handling of floating-point exceptions and IEEE 754 special values.

Whenever possible, the module follows the recommendations of the C99 standard about 754's special values. For example, sqrt(-1.) should now give a ValueError across almost all platforms, while sqrt(float('NaN')) should return a NaN on all IEEE 754 platforms. Where Annex 'F' of the C99 standard recommends signaling 'divide-by-zero' or 'invalid', Python will raise ValueError. Where Annex 'F' of the C99 standard recommends signaling 'overflow', Python will raise OverflowError. (See bpo-711019 [https://bugs.python.org/issue?@action=redirect&bpo=711019] and bpo-1640 [https://bugs.python.org/issue?@action=redirect&bpo=1640].)

(由 Christian Heimes 和 Mark Dickinson 贡献。)

  • mmap objects now have a rfind() method that searches for a substring beginning at the end of the string and searching backwards. The find() method also gained an end parameter giving an index at which to stop searching. (Contributed by John Lenton.)

  • The operator module gained a methodcaller() function that takes a name and an optional set of arguments, returning a callable that will call the named function on any arguments passed to it. For example:

  1. >>> # 等价于 lambda s: s.replace('old', 'new')
  2. >>> replacer = operator.methodcaller('replace', 'old', 'new')
  3. >>> replacer('old wine in old bottles')
  4. 'new wine in new bottles'

(由 Gregory Petrosyan 提供建议,之后由 Georg Brandl 贡献。)

现在 attrgetter() 函数可接受带点号的名称并执行相应的属性查找:

  1. >>> inst_name = operator.attrgetter(
  2. ... '__class__.__name__')
  3. >>> inst_name('')
  4. 'str'
  5. >>> inst_name(help)
  6. '_Helper'

(由 Barry Warsaw 提供建议,之后由 Georg Brandl 贡献。)

  • The os module now wraps several new system calls. fchmod(fd, mode) and fchown(fd, uid, gid) change the mode and ownership of an opened file, and lchmod(path, mode) changes the mode of a symlink. (Contributed by Georg Brandl and Christian Heimes.)

chflags() and lchflags() are wrappers for the corresponding system calls (where they're available), changing the flags set on a file. Constants for the flag values are defined in the stat module; some possible values include UF_IMMUTABLE to signal the file may not be changed and UF_APPEND to indicate that data can only be appended to the file. (Contributed by M. Levinson.)

os.closerange(low, high) efficiently closes all file descriptors from low to high, ignoring any errors and not including high itself. This function is now used by the subprocess module to make starting processes faster. (Contributed by Georg Brandl; bpo-1663329 [https://bugs.python.org/issue?@action=redirect&bpo=1663329].)

A new function, os.path.relpath(path, start='.'), returns a relative path from the start path, if it's supplied, or from the current working directory to the destination path. (Contributed by Richard Barran; bpo-1339796 [https://bugs.python.org/issue?@action=redirect&bpo=1339796].)

On Windows, os.path.expandvars() will now expand environment variables given in the form "%var%", and "~user" will be expanded into the user's home directory path. (Contributed by Josiah Carlson; bpo-957650 [https://bugs.python.org/issue?@action=redirect&bpo=957650].)

  1. >>> import pkgutil
  2. >>> print pkgutil.get_data('test', 'exception_hierarchy.txt')
  3. BaseException
  4. +-- SystemExit
  5. +-- KeyboardInterrupt
  6. +-- GeneratorExit
  7. +-- Exception
  8. +-- StopIteration
  9. +-- StandardError
  10. ...

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

  • The pyexpat module's Parser objects now allow setting their buffer_size attribute to change the size of the buffer used to hold character data. (Contributed by Achim Gaedke; bpo-1137 [https://bugs.python.org/issue?@action=redirect&bpo=1137].)

  • The Queue module now provides queue variants that retrieve entries in different orders. The PriorityQueue class stores queued items in a heap and retrieves them in priority order, and LifoQueue retrieves the most recently added entries first, meaning that it behaves like a stack. (Contributed by Raymond Hettinger.)

  • The random module's Random objects can now be pickled on a 32-bit system and unpickled on a 64-bit system, and vice versa. Unfortunately, this change also means that Python 2.6's Random objects can't be unpickled correctly on earlier versions of Python. (Contributed by Shawn Ligocki; bpo-1727780 [https://bugs.python.org/issue?@action=redirect&bpo=1727780].)

The new triangular(low, high, mode) function returns random numbers following a triangular distribution. The returned values are between low and high, not including high itself, and with mode as the most frequently occurring value in the distribution. (Contributed by Wladmir van der Laan and Raymond Hettinger; bpo-1681432 [https://bugs.python.org/issue?@action=redirect&bpo=1681432].)

The regular expression module is implemented by compiling bytecodes for a tiny regex-specific virtual machine. Untrusted code could create malicious strings of bytecode directly and cause crashes, so Python 2.6 includes a verifier for the regex bytecode. (Contributed by Guido van Rossum from work for Google App Engine; bpo-3487 [https://bugs.python.org/issue?@action=redirect&bpo=3487].)

  • The rlcompleter module's Completer.complete() method will now ignore exceptions triggered while evaluating a name. (Fixed by Lorenz Quack; bpo-2250 [https://bugs.python.org/issue?@action=redirect&bpo=2250].)

  • The sched module's scheduler instances now have a readonly queue attribute that returns the contents of the scheduler's queue, represented as a list of named tuples with the fields (time, priority, action, argument). (Contributed by Raymond Hettinger; bpo-1861 [https://bugs.python.org/issue?@action=redirect&bpo=1861].)

  • The select module now has wrapper functions for the Linux epoll() and BSD kqueue() system calls. modify() method was added to the existing poll objects; pollobj.modify(fd, eventmask) takes a file descriptor or file object and an event mask, modifying the recorded event mask for that file. (Contributed by Christian Heimes; bpo-1657 [https://bugs.python.org/issue?@action=redirect&bpo=1657].)

  • The shutil.copytree() function now has an optional ignore argument that takes a callable object. This callable will receive each directory path and a list of the directory's contents, and returns a list of names that will be ignored, not copied.

The shutil module also provides an ignore_patterns() function for use with this new parameter. ignore_patterns() takes an arbitrary number of glob-style patterns and returns a callable that will ignore any files and directories that match any of these patterns. The following example copies a directory tree, but skips both .svn directories and Emacs backup files, which have names ending with '~':

  1. shutil.copytree('Doc/library', 'tmplibrary',
  2. ignore=shutil.ignore_patterns('*~', '.svn'))

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

  • Integrating signal handling with GUI handling event loops like those used by Tkinter or GTk+ has long been a problem; most software ends up polling, waking up every fraction of a second to check if any GUI events have occurred. The signal module can now make this more efficient. Calling signal.set_wakeup_fd(fd) sets a file descriptor to be used; when a signal is received, a byte is written to that file descriptor. There's also a C-level function, PySignal_SetWakeupFd(), for setting the descriptor.

Event loops will use this by opening a pipe to create two descriptors, one for reading and one for writing. The writable descriptor will be passed to set_wakeup_fd(), and the readable descriptor will be added to the list of descriptors monitored by the event loop via select() or poll(). On receiving a signal, a byte will be written and the main event loop will be woken up, avoiding the need to poll.

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

The siginterrupt() function is now available from Python code, and allows changing whether signals can interrupt system calls or not. (Contributed by Ralf Schmitt.)

The setitimer() and getitimer() functions have also been added (where they're available). setitimer() allows setting interval timers that will cause a signal to be delivered to the process after a specified time, measured in wall-clock time, consumed process time, or combined process+system time. (Contributed by Guilherme Polo; bpo-2240 [https://bugs.python.org/issue?@action=redirect&bpo=2240].)

  • The smtplib module now supports SMTP over SSL thanks to the addition of the SMTP_SSL class. This class supports an interface identical to the existing SMTP class. (Contributed by Monty Taylor.) Both class constructors also have an optional timeout parameter that specifies a timeout for the initial connection attempt, measured in seconds. (Contributed by Facundo Batista.)

An implementation of the LMTP protocol ( RFC 2033 [https://datatracker.ietf.org/doc/html/rfc2033.html]) was also added to the module. LMTP is used in place of SMTP when transferring e-mail between agents that don't manage a mail queue. (LMTP implemented by Leif Hedstrom; bpo-957003 [https://bugs.python.org/issue?@action=redirect&bpo=957003].)

SMTP.starttls() now complies with RFC 3207 [https://datatracker.ietf.org/doc/html/rfc3207.html] and forgets any knowledge obtained from the server not obtained from the TLS negotiation itself. (Patch contributed by Bill Fenner; bpo-829951 [https://bugs.python.org/issue?@action=redirect&bpo=829951].)

A new function, create_connection(), takes an address and connects to it using an optional timeout value, returning the connected socket object. This function also looks up the address's type and connects to it using IPv4 or IPv6 as appropriate. Changing your code to use create_connection() instead of socket(socket.AF_INET, …) may be all that's required to make your code work with IPv6.

Another new variable, dont_write_bytecode, controls whether Python writes any .pyc or .pyo files on importing a module. If this variable is true, the compiled files are not written. The variable is initially set on startup by supplying the -B switch to the Python interpreter, or by setting the PYTHONDONTWRITEBYTECODE environment variable before running the interpreter. Python code can subsequently change the value of this variable to control whether bytecode files are written or not. (Contributed by Neal Norwitz and Georg Brandl.)

Information about the command-line arguments supplied to the Python interpreter is available by reading attributes of a named tuple available as sys.flags. For example, the verbose attribute is true if Python was executed in verbose mode, debug is true in debugging mode, etc. These attributes are all readonly. (Contributed by Christian Heimes.)

A new function, getsizeof(), takes a Python object and returns the amount of memory used by the object, measured in bytes. Builtin objects return correct results; third-party extensions may not, but can define a __sizeof__() method to return the object's size. (Contributed by Robert Schuppenies; bpo-2898 [https://bugs.python.org/issue?@action=redirect&bpo=2898].)

It's now possible to determine the current profiler and tracer functions by calling sys.getprofile() and sys.gettrace(). (Contributed by Georg Brandl; bpo-1648 [https://bugs.python.org/issue?@action=redirect&bpo=1648].)

  • The tarfile module now supports POSIX.1-2001 (pax) tarfiles in addition to the POSIX.1-1988 (ustar) and GNU tar formats that were already supported. The default format is GNU tar; specify the format parameter to open a file using a different format:
  1. tar = tarfile.open("output.tar", "w",
  2. format=tarfile.PAX_FORMAT)

The new encoding and errors parameters specify an encoding and an error handling scheme for character conversions. 'strict', 'ignore', and 'replace' are the three standard ways Python can handle errors,; 'utf-8' is a special value that replaces bad characters with their UTF-8 representation. (Character conversions occur because the PAX format supports Unicode filenames, defaulting to UTF-8 encoding.)

The TarFile.add() method now accepts an exclude argument that's a function that can be used to exclude certain filenames from an archive. The function must take a filename and return true if the file should be excluded or false if it should be archived. The function is applied to both the name initially passed to add() and to the names of files in recursively added directories.

(所有改变均由 Lars Gustäbel 贡献)。

A new class, SpooledTemporaryFile, behaves like a temporary file but stores its data in memory until a maximum size is exceeded. On reaching that limit, the contents will be written to an on-disk temporary file. (Contributed by Dustin J. Mitchell.)

The NamedTemporaryFile and SpooledTemporaryFile classes both work as context managers, so you can write with tempfile.NamedTemporaryFile() as tmp: …. (Contributed by Alexander Belopolsky; bpo-2021 [https://bugs.python.org/issue?@action=redirect&bpo=2021].)

  • The test.test_support module gained a number of context managers useful for writing tests. EnvironmentVarGuard() is a context manager that temporarily changes environment variables and automatically restores them to their old values.

Another context manager, TransientResource, can surround calls to resources that may or may not be available; it will catch and ignore a specified list of exceptions. For example, a network test may ignore certain failures when connecting to an external web site:

  1. with test_support.TransientResource(IOError,
  2. errno=errno.ETIMEDOUT):
  3. f = urllib.urlopen('https://sf.net')
  4. ...

Finally, check_warnings() resets the warning module's warning filters and returns an object that will record all warning messages triggered (bpo-3781 [https://bugs.python.org/issue?@action=redirect&bpo=3781]):

  1. with test_support.check_warnings() as wrec:
  2. warnings.simplefilter("always")
  3. # ... code that triggers a warning ...
  4. assert str(wrec.message) == "function is outdated"
  5. assert len(wrec.warnings) == 1, "Multiple warnings raised"

(由 Brett Cannon 贡献。)

  • The textwrap module can now preserve existing whitespace at the beginnings and ends of the newly created lines by specifying drop_whitespace=False as an argument:
  1. >>> S = """This sentence has a bunch of
  2. ... extra whitespace."""
  3. >>> print textwrap.fill(S, width=15)
  4. This sentence
  5. has a bunch
  6. of extra
  7. whitespace.
  8. >>> print textwrap.fill(S, drop_whitespace=False, width=15)
  9. This sentence
  10. has a bunch
  11. of extra
  12. whitespace.
  13. >>>

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

  • The threading module API is being changed to use properties such as daemon instead of setDaemon() and isDaemon() methods, and some methods have been renamed to use underscores instead of camel-case; for example, the activeCount() method is renamed to active_count(). Both the 2.6 and 3.0 versions of the module support the same properties and renamed methods, but don't remove the old methods. No date has been set for the deprecation of the old APIs in Python 3.x; the old APIs won't be removed in any 2.x version. (Carried out by several people, most notably Benjamin Peterson.)

The threading module's Thread objects gained an ident property that returns the thread's identifier, a nonzero integer. (Contributed by Gregory P. Smith; bpo-2871 [https://bugs.python.org/issue?@action=redirect&bpo=2871].)

  • The timeit module now accepts callables as well as strings for the statement being timed and for the setup code. Two convenience functions were added for creating Timer instances: repeat(stmt, setup, time, repeat, number) and timeit(stmt, setup, time, number) create an instance and call the corresponding method. (Contributed by Erik Demaine; bpo-1533909 [https://bugs.python.org/issue?@action=redirect&bpo=1533909].)

  • The Tkinter module now accepts lists and tuples for options, separating the elements by spaces before passing the resulting value to Tcl/Tk. (Contributed by Guilherme Polo; bpo-2906 [https://bugs.python.org/issue?@action=redirect&bpo=2906].)

  • The turtle module for turtle graphics was greatly enhanced by Gregor Lingl. New features in the module include:

    • Better animation of turtle movement and rotation.

    • Control over turtle movement using the new delay(), tracer(), and speed() methods.

    • The ability to set new shapes for the turtle, and to define a new coordinate system.

    • Turtles now have an undo() method that can roll back actions.

    • Simple support for reacting to input events such as mouse and keyboard activity, making it possible to write simple games.

    • turtle.cfg 文件可被用来定制海龟绘图屏幕的初始外观。

    • The module's docstrings can be replaced by new docstrings that have been translated into another language.

(bpo-1513695 [https://bugs.python.org/issue?@action=redirect&bpo=1513695])

  • An optional timeout parameter was added to the urllib.urlopen function and the urllib.ftpwrapper class constructor, as well as the urllib2.urlopen function. The parameter specifies a timeout measured in seconds. For example:
  1. >>> u = urllib2.urlopen("http://slow.example.com",
  2. timeout=3)
  3. Traceback (most recent call last): ...
  4. urllib2.URLError: <urlopen error timed out>
  5. >>>

(由 Facundo Batista 添加。)

A new function, catch_warnings(), is a context manager intended for testing purposes that lets you temporarily modify the warning filters and then restore their original values (bpo-3781 [https://bugs.python.org/issue?@action=redirect&bpo=3781]).

SimpleXMLRPCServer also has a sendtraceback_header attribute; if true, the exception and formatted traceback are returned as HTTP headers "X-Exception" and "X-Traceback". This feature is for debugging purposes only and should not be used on production servers because the tracebacks might reveal passwords or other sensitive information. (Contributed by Alan McIntyre as part of his project for Google's Summer of Code 2007.)

  1. z = zipfile.ZipFile('python-251.zip')
  2.  
  3. # Unpack a single file, writing it relative
  4. # to the /tmp directory.
  5. z.extract('Python/sysmodule.c', '/tmp')
  6.  
  7. # Unpack all the files in the archive.
  8. z.extractall()

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

The open(), read() and extract() methods can now take either a filename or a ZipInfo object. This is useful when an archive accidentally contains a duplicated filename. (Contributed by Graham Horler; bpo-1775025 [https://bugs.python.org/issue?@action=redirect&bpo=1775025].)

Finally, zipfile now supports using Unicode filenames for archived files. (Contributed by Alexey Borzenkov; bpo-1734346 [https://bugs.python.org/issue?@action=redirect&bpo=1734346].)

ast 模块

The ast module provides an Abstract Syntax Tree representation of Python code, and Armin Ronacher contributed a set of helper functions that perform a variety of common tasks. These will be useful for HTML templating packages, code analyzers, and similar tools that process Python code.

The parse() function takes an expression and returns an AST. The dump() function outputs a representation of a tree, suitable for debugging:

  1. import ast
  2.  
  3. t = ast.parse("""
  4. d = {}
  5. for i in 'abcdefghijklm':
  6. d[i + i] = ord(i) - ord('a') + 1
  7. print d
  8. """)
  9. print ast.dump(t)

输出是一棵深度嵌套的树:

  1. Module(body=[
  2. Assign(targets=[
  3. Name(id='d', ctx=Store())
  4. ], value=Dict(keys=[], values=[]))
  5. For(target=Name(id='i', ctx=Store()),
  6. iter=Str(s='abcdefghijklm'), body=[
  7. Assign(targets=[
  8. Subscript(value=
  9. Name(id='d', ctx=Load()),
  10. slice=
  11. Index(value=
  12. BinOp(left=Name(id='i', ctx=Load()), op=Add(),
  13. right=Name(id='i', ctx=Load()))), ctx=Store())
  14. ], value=
  15. BinOp(left=
  16. BinOp(left=
  17. Call(func=
  18. Name(id='ord', ctx=Load()), args=[
  19. Name(id='i', ctx=Load())
  20. ], keywords=[], starargs=None, kwargs=None),
  21. op=Sub(), right=Call(func=
  22. Name(id='ord', ctx=Load()), args=[
  23. Str(s='a')
  24. ], keywords=[], starargs=None, kwargs=None)),
  25. op=Add(), right=Num(n=1)))
  26. ], orelse=[])
  27. Print(dest=None, values=[
  28. Name(id='d', ctx=Load())
  29. ], nl=True)
  30. ])

The literal_eval() method takes a string or an AST representing a literal expression, parses and evaluates it, and returns the resulting value. A literal expression is a Python expression containing only strings, numbers, dictionaries, etc. but no statements or function calls. If you need to evaluate an expression but cannot accept the security risk of using an eval() call, literal_eval() will handle it safely:

  1. >>> literal = '("a", "b", {2:4, 3:8, 1:2})'
  2. >>> print ast.literal_eval(literal)
  3. ('a', 'b', {1: 2, 2: 4, 3: 8})
  4. >>> print ast.literal_eval('"a" + "b"')
  5. Traceback (most recent call last): ...
  6. ValueError: malformed string

The module also includes NodeVisitor and NodeTransformer classes for traversing and modifying an AST, and functions for common transformations such as changing line numbers.

future_builtins 模块

Python 3.0 makes many changes to the repertoire of builtin functions, and most of the changes can't be introduced in the Python 2.x series because they would break compatibility. The future_builtins module provides versions of these builtin functions that can be imported when writing 3.0-compatible code.

目前此模块中的函数包括:

  • ascii(obj): equivalent to repr(). In Python 3.0, repr() will return a Unicode string, while ascii() will return a pure ASCII bytestring.

  • filter(predicate, iterable), map(func, iterable1, …): the 3.0 versions return iterators, unlike the 2.x builtins which return lists.

  • hex(value), oct(value): instead of calling the __hex__() or __oct__() methods, these versions will call the __index__() method and convert the result to hexadecimal or octal. oct() will use the new 0o notation for its result.

json 模块: JavaScript Object Notation

The new json module supports the encoding and decoding of Python types in JSON (Javascript Object Notation). JSON is a lightweight interchange format often used in web applications. For more information about JSON, see http://www.json.org.

json comes with support for decoding and encoding most builtin Python types. The following example encodes and decodes a dictionary:

  1. >>> import json
  2. >>> data = {"spam": "foo", "parrot": 42}
  3. >>> in_json = json.dumps(data) # Encode the data
  4. >>> in_json
  5. '{"parrot": 42, "spam": "foo"}'
  6. >>> json.loads(in_json) # Decode into a Python object
  7. {"spam": "foo", "parrot": 42}

It's also possible to write your own decoders and encoders to support more types. Pretty-printing of the JSON strings is also supported.

json (originally called simplejson) was written by Bob Ippolito.

plistlib 模块:属性列表解析器

The .plist format is commonly used on Mac OS X to store basic data types (numbers, strings, lists, and dictionaries) by serializing them into an XML-based format. It resembles the XML-RPC serialization of data types.

Despite being primarily used on Mac OS X, the format has nothing Mac-specific about it and the Python implementation works on any platform that Python supports, so the plistlib module has been promoted to the standard library.

此模块的用法很简单:

  1. import sys
  2. import plistlib
  3. import datetime
  4.  
  5. # Create data structure
  6. data_struct = dict(lastAccessed=datetime.datetime.now(),
  7. version=1,
  8. categories=('Personal','Shared','Private'))
  9.  
  10. # Create string containing XML.
  11. plist_str = plistlib.writePlistToString(data_struct)
  12. new_struct = plistlib.readPlistFromString(plist_str)
  13. print data_struct
  14. print new_struct
  15.  
  16. # Write data structure to a file and read it back.
  17. plistlib.writePlist(data_struct, 'tmpcustomizations.plist')
  18. new_struct = plistlib.readPlist('tmpcustomizations.plist')
  19.  
  20. # read/writePlist accepts file-like objects as well as paths.
  21. plistlib.writePlist(data_struct, sys.stdout)

ctypes Enhancements

Thomas Heller continued to maintain and enhance the ctypes module.

ctypes now supports a c_bool datatype that represents the C99 bool type. (Contributed by David Remahl; bpo-1649190 [https://bugs.python.org/issue?@action=redirect&bpo=1649190].)

The ctypes string, buffer and array types have improved support for extended slicing syntax, where various combinations of (start, stop, step) are supplied. (Implemented by Thomas Wouters.)

All ctypes data types now support from_buffer() and from_buffer_copy() methods that create a ctypes instance based on a provided buffer object. from_buffer_copy() copies the contents of the object, while from_buffer() will share the same memory area.

A new calling convention tells ctypes to clear the errno or Win32 LastError variables at the outset of each wrapped call. (Implemented by Thomas Heller; bpo-1798 [https://bugs.python.org/issue?@action=redirect&bpo=1798].)

You can now retrieve the Unix errno variable after a function call. When creating a wrapped function, you can supply use_errno=True as a keyword parameter to the DLL() function and then call the module-level methods set_errno() and get_errno() to set and retrieve the error value.

The Win32 LastError variable is similarly supported by the DLL(), OleDLL(), and WinDLL() functions. You supply use_last_error=True as a keyword parameter and then call the module-level methods set_last_error() and get_last_error().

The byref() function, used to retrieve a pointer to a ctypes instance, now has an optional offset parameter that is a byte count that will be added to the returned pointer.

改进的 SSL 支持

Bill Janssen made extensive improvements to Python 2.6's support for the Secure Sockets Layer by adding a new module, ssl, that's built atop the OpenSSL [https://www.openssl.org/] library. This new module provides more control over the protocol negotiated, the X.509 certificates used, and has better support for writing SSL servers (as opposed to clients) in Python. The existing SSL support in the socket module hasn't been removed and continues to work, though it will be removed in Python 3.0.

To use the new module, you must first create a TCP connection in the usual way and then pass it to the ssl.wrap_socket() function. It's possible to specify whether a certificate is required, and to obtain certificate info by calling the getpeercert() method.

参见

ssl 模块的文档。

弃用和移除

  • String exceptions have been removed. Attempting to use them raises a TypeError.

  • Changes to the Exception interface as dictated by PEP 352 [https://peps.python.org/pep-0352/] continue to be made. For 2.6, the message attribute is being deprecated in favor of the args attribute.

  • (3.0-warning mode) Python 3.0 will feature a reorganized standard library that will drop many outdated modules and rename others. Python 2.6 running in 3.0-warning mode will warn about these modules when they are imported.

The list of deprecated modules is: audiodev, bgenlocations, buildtools, bundlebuilder, Canvas, compiler, dircache, dl, fpformat, gensuitemodule, ihooks, imageop, imgfile, linuxaudiodev, mhlib, mimetools, multifile, new, pure, statvfs, sunaudiodev, test.testall, and toaiff.

  • gopherlib 模块已被移除。

  • MimeWriter 模块和 mimify 模块已被弃用;请改用 email 包。

  • md5 模块已被弃用;请改用 hashlib 模块。

  • posixfile 模块已被弃用;fcntl.lockf() 可提供更好的锁机制。

  • popen2 模块已被弃用;请使用 subprocess 模块。

  • The rgbimg module has been removed.

  • The sets module has been deprecated; it's better to use the builtin set and frozenset types.

  • The sha module has been deprecated; use the hashlib module instead.

构建和 C API 的改变

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

  • Python now must be compiled with C89 compilers (after 19 years!). This means that the Python source tree has dropped its own implementations of memmove() and strerror(), which are in the C89 standard library.

  • Python 2.6 can be built with Microsoft Visual Studio 2008 (version 9.0), and this is the new default compiler. See the PCbuild directory for the build files. (Implemented by Christian Heimes.)

  • On Mac OS X, Python 2.6 can be compiled as a 4-way universal build. The configure script can take a --with-universal-archs=[32-bit|64-bit|all] switch, controlling whether the binaries are built for 32-bit architectures (x86, PowerPC), 64-bit (x86-64 and PPC-64), or both. (Contributed by Ronald Oussoren.)

  • A new function added in Python 2.6.6, PySys_SetArgvEx(), sets the value of sys.argv and can optionally update sys.path to include the directory containing the script named by sys.argv[0] depending on the value of an updatepath parameter.

This function was added to close a security hole for applications that embed Python. The old function, PySys_SetArgv(), would always update sys.path, and sometimes it would add the current directory. This meant that, if you ran an application embedding Python in a directory controlled by someone else, attackers could put a Trojan-horse module in the directory (say, a file named os.py) that your application would then import and run.

If you maintain a C/C++ application that embeds Python, check whether you're calling PySys_SetArgv() and carefully consider whether the application should be using PySys_SetArgvEx() with updatepath set to false. Note that using this function will break compatibility with Python versions 2.6.5 and earlier; if you have to continue working with earlier versions, you can leave the call to PySys_SetArgv() alone and call PyRun_SimpleString("sys.path.pop(0)\n") afterwards to discard the first sys.path component.

Security issue reported as CVE 2008-5983 [https://www.cve.org/CVERecord?id=CVE-2008-5983]; discussed in gh-50003 [https://github.com/python/cpython/issues/50003], and fixed by Antoine Pitrou.

  • The BerkeleyDB module now has a C API object, available as bsddb.db.api. This object can be used by other C extensions that wish to use the bsddb module for their own purposes. (Contributed by Duncan Grisby.)

  • The new buffer interface, previously described in the PEP 3118 section, adds PyObject_GetBuffer() and PyBuffer_Release(), as well as a few other functions.

  • Python's use of the C stdio library is now thread-safe, or at least as thread-safe as the underlying library is. A long-standing potential bug occurred if one thread closed a file object while another thread was reading from or writing to the object. In 2.6 file objects have a reference count, manipulated by the PyFile_IncUseCount() and PyFile_DecUseCount() functions. File objects can't be closed unless the reference count is zero. PyFile_IncUseCount() should be called while the GIL is still held, before carrying out an I/O operation using the FILE * pointer, and PyFile_DecUseCount() should be called immediately after the GIL is re-acquired. (Contributed by Antoine Pitrou and Gregory P. Smith.)

  • Importing modules simultaneously in two different threads no longer deadlocks; it will now raise an ImportError. A new API function, PyImport_ImportModuleNoBlock(), will look for a module in sys.modules first, then try to import it after acquiring an import lock. If the import lock is held by another thread, an ImportError is raised. (Contributed by Christian Heimes.)

  • Several functions return information about the platform's floating-point support. PyFloat_GetMax() returns the maximum representable floating-point value, and PyFloat_GetMin() returns the minimum positive value. PyFloat_GetInfo() returns an object containing more information from the float.h file, such as "mant_dig" (number of digits in the mantissa), "epsilon" (smallest difference between 1.0 and the next largest value representable), and several others. (Contributed by Christian Heimes; bpo-1534 [https://bugs.python.org/issue?@action=redirect&bpo=1534].)

  • C functions and methods that use PyComplex_AsCComplex() will now accept arguments that have a __complex__() method. In particular, the functions in the cmath module will now accept objects with this method. This is a backport of a Python 3.0 change. (Contributed by Mark Dickinson; bpo-1675423 [https://bugs.python.org/issue?@action=redirect&bpo=1675423].)

  • Python's C API now includes two functions for case-insensitive string comparisons, PyOS_stricmp(char*, char*) and PyOS_strnicmp(char*, char*, Py_ssize_t). (Contributed by Christian Heimes; bpo-1635 [https://bugs.python.org/issue?@action=redirect&bpo=1635].)

  • Many C extensions define their own little macro for adding integers and strings to the module's dictionary in the init* function. Python 2.6 finally defines standard macros for adding values to a module, PyModule_AddStringMacro and PyModule_AddIntMacro(). (Contributed by Christian Heimes.)

  • Some macros were renamed in both 3.0 and 2.6 to make it clearer that they are macros, not functions. Py_Size() became Py_SIZE(), Py_Type() became Py_TYPE(), and Py_Refcnt() became Py_REFCNT(). The mixed-case macros are still available in Python 2.6 for backward compatibility. (bpo-1629 [https://bugs.python.org/issue?@action=redirect&bpo=1629])

  • Distutils now places C extensions it builds in a different directory when running on a debug version of Python. (Contributed by Collin Winter; bpo-1530959 [https://bugs.python.org/issue?@action=redirect&bpo=1530959].)

  • Several basic data types, such as integers and strings, maintain internal free lists of objects that can be re-used. The data structures for these free lists now follow a naming convention: the variable is always named free_list, the counter is always named numfree, and a macro Py_MAXFREELIST is always defined.

  • A new Makefile target, "make patchcheck", prepares the Python source tree for making a patch: it fixes trailing whitespace in all modified .py files, checks whether the documentation has been changed, and reports whether the Misc/ACKS and Misc/NEWS files have been updated. (Contributed by Brett Cannon.)

Another new target, "make profile-opt", compiles a Python binary using GCC's profile-guided optimization. It compiles Python with profiling enabled, runs the test suite to obtain a set of profiling results, and then compiles using these results for optimization. (Contributed by Gregory P. Smith.)

特定于 Windows 的更改:

  • The support for Windows 95, 98, ME and NT4 has been dropped. Python 2.6 requires at least Windows 2000 SP4.

  • The new default compiler on Windows is Visual Studio 2008 (version 9.0). The build directories for Visual Studio 2003 (version 7.1) and 2005 (version 8.0) were moved into the PC/ directory. The new PCbuild directory supports cross compilation for X64, debug builds and Profile Guided Optimization (PGO). PGO builds are roughly 10% faster than normal builds. (Contributed by Christian Heimes with help from Amaury Forgeot d'Arc and Martin von Löwis.)

  • The msvcrt module now supports both the normal and wide char variants of the console I/O API. The getwch() function reads a keypress and returns a Unicode value, as does the getwche() function. The putwch() function takes a Unicode character and writes it to the console. (Contributed by Christian Heimes.)

  • os.path.expandvars() will now expand environment variables in the form "%var%", and "~user" will be expanded into the user's home directory path. (Contributed by Josiah Carlson; bpo-957650 [https://bugs.python.org/issue?@action=redirect&bpo=957650].)

  • The socket module's socket objects now have an ioctl() method that provides a limited interface to the WSAIoctl() system interface.

  • The _winreg module now has a function, ExpandEnvironmentStrings(), that expands environment variable references such as %NAME% in an input string. The handle objects provided by this module now support the context protocol, so they can be used in with statements. (Contributed by Christian Heimes.)

_winreg also has better support for x64 systems, exposing the DisableReflectionKey(), EnableReflectionKey(), and QueryReflectionKey() functions, which enable and disable registry reflection for 32-bit processes running on 64-bit systems. (bpo-1753245 [https://bugs.python.org/issue?@action=redirect&bpo=1753245])

特定于 Mac OS X 的更改:

  • 现在,在编译Python的框架版本时,可以为 configure 脚本添加 --with-framework-name= 选项来指定要使用的框架名称。

  • The macfs module has been removed. This in turn required the macostools.touched() function to be removed because it depended on the macfs module. (bpo-1490190 [https://bugs.python.org/issue?@action=redirect&bpo=1490190])

  • Many other Mac OS modules have been deprecated and will be removed in Python 3.0: _builtinSuites, aepack, aetools, aetypes, applesingle, appletrawmain, appletrunner, argvemulator, Audio_mac, autoGIL, Carbon, cfmfile, CodeWarrior, ColorPicker, EasyDialogs, Explorer, Finder, FrameWork, findertools, ic, icglue, icopen, macerrors, MacOS, macfs, macostools, macresource, MiniAEFrame, Nav, Netscape, OSATerminology, pimp, PixMapWrapper, StdSuites, SystemEvents, Terminal, and terminalcommand.

特定于 IRIX 的更改:

A number of old IRIX-specific modules were deprecated and will be removed in Python 3.0: al and AL, cd, cddb, cdplayer, CL and cl, DEVICE, ERRNO, FILE, FL and fl, flp, fm, GET, GLWS, GL and gl, IN, IOCTL, jpeg, panelparser, readcd, SV and sv, torgb, videoreader, and WAIT.

移植到Python 2.6

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

  • 预期为不可哈希的类应当在其定义中设置 __hash__ = None 来指明这一点。

  • String exceptions have been removed. Attempting to use them raises a TypeError.

  • The __init__() method of collections.deque now clears any existing contents of the deque before adding elements from the iterable. This change makes the behavior match list.__init__().

  • object.__init__() previously accepted arbitrary arguments and keyword arguments, ignoring them. In Python 2.6, this is no longer allowed and will result in a TypeError. This will affect __init__() methods that end up calling the corresponding method on object (perhaps through using super()). See bpo-1683368 [https://bugs.python.org/issue?@action=redirect&bpo=1683368] for discussion.

  • The Decimal constructor now accepts leading and trailing whitespace when passed a string. Previously it would raise an InvalidOperation exception. On the other hand, the create_decimal() method of Context objects now explicitly disallows extra whitespace, raising a ConversionSyntax exception.

  • Due to an implementation accident, if you passed a file path to the builtin __import__() function, it would actually import the specified file. This was never intended to work, however, and the implementation now explicitly checks for this case and raises an ImportError.

  • C API: the PyImport_Import() and PyImport_ImportModule() functions now default to absolute imports, not relative imports. This will affect C extensions that import other modules.

  • C API: extension data types that shouldn't be hashable should define their tp_hash slot to PyObject_HashNotImplemented().

  • The socket module exception socket.error now inherits from IOError. Previously it wasn't a subclass of StandardError but now it is, through IOError. (Implemented by Gregory P. Smith; bpo-1706815 [https://bugs.python.org/issue?@action=redirect&bpo=1706815].)

  • The xmlrpclib module no longer automatically converts datetime.date and datetime.time to the xmlrpclib.DateTime type; the conversion semantics were not necessarily correct for all applications. Code using xmlrpclib should convert date and time instances. (bpo-1330538 [https://bugs.python.org/issue?@action=redirect&bpo=1330538])

  • (3.0-warning mode) The Exception class now warns when accessed using slicing or index access; having Exception behave like a tuple is being phased out.

  • (3.0-warning mode) inequality comparisons between two dictionaries or two objects that don't implement comparison methods are reported as warnings. dict1 == dict2 still works, but dict1 < dict2 is being phased out.

Comparisons between cells, which are an implementation detail of Python's scoping rules, also cause warnings because such comparisons are forbidden entirely in 3.0.

对于嵌入Python的应用程序:

  • Python 2.6.6 中增加了 PySys_SetArgvEx() 函数,这让应用可以弥补一个在使用现有 PySys_SetArgv() 函数时会存在的安全漏洞。 请检查你是否有调用 PySys_SetArgv() 并仔细考虑应用是否应当改用 PySys_SetArgvEx() 并将 updatepath 设为假值。

致谢

作者感谢以下人员对本文各种草稿给予的建议,更正和协助: Georg Brandl, Steve Brown, Nick Coghlan, Ralph Corderoy, Jim Jewett, Kent Johnson, Chris Lambacher, Martin Michlmayr, Antoine Pitrou, Brian Warner.