blob: 55d9a2b93e5ff07b8b68d03d5ba4e747d63213c8 [file] [log] [blame]
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001****************************
2 What's New in Python 2.7
3****************************
4
5:Author: A.M. Kuchling (amk at amk.ca)
6:Release: |release|
7:Date: |today|
8
Benjamin Petersond23f8222009-04-05 19:13:16 +00009.. Fix accents on Kristjan Valur Jonsson, Fuerstenau, Tarek Ziade.
Benjamin Peterson1010bf32009-01-30 04:00:29 +000010
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000011.. $Id$
12 Rules for maintenance:
13
14 * Anyone can add text to this document. Do not spend very much time
15 on the wording of your changes, because your text will probably
16 get rewritten to some degree.
17
18 * The maintainer will go through Misc/NEWS periodically and add
19 changes; it's therefore more important to add your changes to
20 Misc/NEWS than to this file.
21
22 * This is not a complete list of every single change; completeness
23 is the purpose of Misc/NEWS. Some changes I consider too small
24 or esoteric to include. If such a change is added to the text,
25 I'll just remove it. (This is another reason you shouldn't spend
26 too much time on writing your addition.)
27
28 * If you want to draw your new text to the attention of the
29 maintainer, add 'XXX' to the beginning of the paragraph or
30 section.
31
32 * It's OK to just add a fragmentary note about a change. For
33 example: "XXX Describe the transmogrify() function added to the
34 socket module." The maintainer will research the change and
35 write the necessary text.
36
37 * You can comment out your additions if you like, but it's not
38 necessary (especially when a final release is some months away).
39
40 * Credit the author of a patch or bugfix. Just the name is
41 sufficient; the e-mail address isn't necessary.
42
43 * It's helpful to add the bug/patch number in a parenthetical comment.
44
45 XXX Describe the transmogrify() function added to the socket
46 module.
47 (Contributed by P.Y. Developer; :issue:`12345`.)
48
49 This saves the maintainer some effort going through the SVN logs
50 when researching a change.
51
52This article explains the new features in Python 2.7.
53No release schedule has been decided yet for 2.7.
54
55.. Compare with previous release in 2 - 3 sentences here.
56 add hyperlink when the documentation becomes available online.
57
Benjamin Petersond23f8222009-04-05 19:13:16 +000058Python 3.1
59================
60
61Much as Python 2.6 incorporated features from Python 3.0,
62version 2.7 is influenced by features from 3.1.
63
64XXX mention importlib; anything else?
65
66One porting change: the :option:`-3` switch now automatically
67enables the :option:`-Qwarn` switch that causes warnings
68about using classic division with integers and long integers.
69
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000070.. ========================================================================
71.. Large, PEP-level features and changes should be described here.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000072.. ========================================================================
73
Benjamin Petersond23f8222009-04-05 19:13:16 +000074PEP 372: Adding an ordered dictionary to collections
75====================================================
76
77XXX write this
78
79Several modules will now use :class:`OrderedDict` by default. The
80:mod:`ConfigParser` module uses :class:`OrderedDict` for the list
81of sections and the options within a section.
82The :meth:`namedtuple._asdict` method returns an :class:`OrderedDict`
83as well.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000084
85
86Other Language Changes
87======================
88
89Some smaller changes made to the core Python language are:
90
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +000091* The string :method:`format` method now supports automatic numbering
92 of the replacement fields. This makes using :meth:`format`
93 more closely resemble using ``%s`` formatting::
94
95 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
96 '2009:4:Sunday'
97 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
98 '2009:4:Sunday'
99
100 The auto-numbering takes the fields from left to right, so the first
101 ``{...}`` specifier will use the first argument to :meth:`format`,
102 the next specifier will use the next argument, and so on. You can't
103 mix auto-numbering and explicit numbering -- either number all of
104 your specifier fields or none of them -- but you can mix
105 auto-numbering and named fields, as in the second example above.
106 (Contributed by XXX; :issue`5237`.)
107
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000108* The :func:`int` and :func:`long` types gained a ``bit_length``
109 method that returns the number of bits necessary to represent
110 its argument in binary::
111
112 >>> n = 37
113 >>> bin(37)
114 '0b100101'
115 >>> n.bit_length()
116 6
117 >>> n = 2**123-1
118 >>> n.bit_length()
119 123
120 >>> (n+1).bit_length()
121 124
122
123 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
124
Benjamin Petersond23f8222009-04-05 19:13:16 +0000125* The :class:`bytearray` type's :meth:`translate` method will
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000126 now accept ``None`` as its first argument. (Fixed by Georg Brandl;
Benjamin Petersond23f8222009-04-05 19:13:16 +0000127 :issue:`4759`.)
Mark Dickinsond72c7b62009-03-20 16:00:49 +0000128
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000129.. ======================================================================
130
131
132Optimizations
133-------------
134
Benjamin Petersond23f8222009-04-05 19:13:16 +0000135Several performance enhancements have been added:
136
137.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
138 compiles the main bytecode interpreter loop using a new dispatch
139 mechanism that gives speedups of up to 20%, depending on the system
140 and benchmark. The new mechanism is only supported on certain
141 compilers, such as gcc, SunPro, and icc.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000142
143* The garbage collector now performs better when many objects are
144 being allocated without deallocating any. A full garbage collection
145 pass is only performed when the middle generation has been collected
146 10 times and when the number of survivor objects from the middle
147 generation exceeds 10% of the number of objects in the oldest
148 generation. The second condition was added to reduce the number
149 of full garbage collections as the number of objects on the heap grows,
150 avoiding quadratic performance when allocating very many objects.
151 (Suggested by Martin von Loewis and implemented by Antoine Pitrou;
152 :issue:`4074`.)
153
Benjamin Petersond23f8222009-04-05 19:13:16 +0000154* The garbage collector tries to avoid tracking simple containers
155 which can't be part of a cycle. In Python 2.7, this is now true for
156 tuples and dicts containing atomic types (such as ints, strings,
157 etc.). Transitively, a dict containing tuples of atomic types won't
158 be tracked either. This helps reduce the cost of each
159 garbage collection by decreasing the number of objects to be
160 considered and traversed by the collector.
Antoine Pitrou9d81def2009-03-28 19:20:09 +0000161 (Contributed by Antoine Pitrou; :issue:`4688`.)
162
Benjamin Petersond23f8222009-04-05 19:13:16 +0000163* Integers are now stored internally either in base 2**15 or in base
164 2**30, the base being determined at build time. Previously, they
165 were always stored in base 2**15. Using base 2**30 gives
166 significant performance improvements on 64-bit machines, but
167 benchmark results on 32-bit machines have been mixed. Therefore,
168 the default is to use base 2**30 on 64-bit machines and base 2**15
169 on 32-bit machines; on Unix, there's a new configure option
170 :option:`--enable-big-digits` that can be used to override this default.
171
172 Apart from the performance improvements this change should be
173 invisible to end users, with one exception: for testing and
174 debugging purposes there's a new structseq ``sys.long_info`` that
175 provides information about the internal format, giving the number of
176 bits per digit and the size in bytes of the C type used to store
177 each digit::
178
179 >>> import sys
180 >>> sys.long_info
181 sys.long_info(bits_per_digit=30, sizeof_digit=4)
182
183 (Contributed by Mark Dickinson; :issue:`4258`.)
184
185 Another set of changes made long objects a few bytes smaller: 2 bytes
186 smaller on 32-bit systems and 6 bytes on 64-bit.
187 (Contributed by Mark Dickinson; :issue:`5260`.)
188
189* The division algorithm for long integers has been made faster
190 by tightening the inner loop, doing shifts instead of multiplications,
191 and fixing an unnecessary extra iteration.
192 Various benchmarks show speedups of between 50% and 150% for long
193 integer divisions and modulo operations.
194 (Contributed by Mark Dickinson; :issue:`5512`.)
195
196* The implementation of ``%`` checks for the left-side operand being
197 a Python string and special-cases it; this results in a 1-3%
198 performance increase for applications that frequently use ``%``
199 with strings, such as templating libraries.
200 (Implemented by Collin Winter; :issue:`5176`.)
201
202* List comprehensions with an ``if`` condition are compiled into
203 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
204 by Jeffrey Yasskin; :issue:`4715`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000205
206.. ======================================================================
207
208New, Improved, and Deprecated Modules
209=====================================
210
211As in every release, Python's standard library received a number of
212enhancements and bug fixes. Here's a partial list of the most notable
213changes, sorted alphabetically by module name. Consult the
214:file:`Misc/NEWS` file in the source tree for a more complete list of
215changes, or look through the Subversion logs for all the details.
216
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000217* The :mod:`bz2` module's :class:`BZ2File` now supports the context
218 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
219 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
220
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000221* New class: the :class:`Counter` class in the :mod:`collections` module is
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000222 useful for tallying data. :class:`Counter` instances behave mostly
223 like dictionaries but return zero for missing keys instead of
224 raising a :exc:`KeyError`::
225
226 >>> from collections import Counter
227 >>> c=Counter()
228 >>> for letter in 'here is a sample of english text':
229 ... c[letter] += 1
230 ...
231 >>> c
232 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
233 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
234 'p': 1, 'r': 1, 'x': 1})
235 >>> c['e']
236 5
237 >>> c['z']
238 0
239
240 There are two additional :class:`Counter` methods: :meth:`most_common`
241 returns the N most common elements and their counts, and :meth:`elements`
242 returns an iterator over the contained element, repeating each element
243 as many times as its count::
244
245 >>> c.most_common(5)
246 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
247 >>> c.elements() ->
248 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
249 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
250 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
251 's', 's', 'r', 't', 't', 'x']
252
253 Contributed by Raymond Hettinger; :issue:`1696199`.
254
Benjamin Petersond23f8222009-04-05 19:13:16 +0000255 The :class:`namedtuple` class now has an optional *rename* parameter.
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000256 If *rename* is true, field names that are invalid because they've
Benjamin Petersond23f8222009-04-05 19:13:16 +0000257 been repeated or that aren't legal Python identifiers will be
258 renamed to legal names that are derived from the field's
259 position within the list of fields:
260
261 >>> T=namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
262 >>> T._fields
263 ('field1', '_1', '_2', 'field2')
264
265 (Added by Raymond Hettinger; :issue:`1818`.)
266
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000267 The :class:`deque` data type now exposes its maximum length as the
268 read-only :attr:`maxlen` attribute. (Added by Raymond Hettinger.)
269
Benjamin Petersond23f8222009-04-05 19:13:16 +0000270* In Distutils, :func:`distutils.sdist.add_defaults` now uses
271 *package_dir* and *data_files* to create the MANIFEST file.
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000272 :mod:`distutils.sysconfig` will now read the :envvar:`AR`
273 environment variable.
Benjamin Petersond23f8222009-04-05 19:13:16 +0000274
275 It is no longer mandatory to store clear-text passwords in the
276 :file:`.pypirc` file when registering and uploading packages to PyPI. As long
277 as the username is present in that file, the :mod:`distutils` package will
278 prompt for the password if not present. (Added by Tarek Ziade,
279 based on an initial contribution by Nathan Van Gheem; :issue:`4394`.)
280
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000281 A Distutils setup can now specify that a C extension is optional by
282 setting the *optional* option setting to true. If this optional is
283 supplied, failure to build the extension will not abort the build
284 process, but instead simply not install the failing extension.
285 (Contributed by Georg Brandl; :issue:`5583`.)
286
Benjamin Petersond23f8222009-04-05 19:13:16 +0000287* New method: the :class:`Decimal` class gained a
288 :meth:`from_float` class method that performs an exact conversion
289 of a floating-point number to a :class:`Decimal`.
290 Note that this is an **exact** conversion that strives for the
291 closest decimal approximation to the floating-point representation's value;
292 the resulting decimal value will therefore still include the inaccuracy,
293 if any.
294 For example, ``Decimal.from_float(0.1)`` returns
295 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
296 (Implemented by Raymond Hettinger; :issue:`4796`.)
297
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000298* New function: the :mod:`gc` module's :func:`is_tracked` returns
299 true if a given instance is tracked by the garbage collector, false
Benjamin Petersond23f8222009-04-05 19:13:16 +0000300 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
301
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000302* The :mod:`gzip` module's :class:`GzipFile` now supports the context
303 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``.
304 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000305 It's now possible to override the modification time
306 recorded in a gzipped file by providing an optional timestamp to
307 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000308
309* The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
310 an invalid file descriptor. (Implemented by Benjamin Peterson;
311 :issue:`4991`.)
312
Benjamin Petersond23f8222009-04-05 19:13:16 +0000313* New function: ``itertools.compress(*data*, *selectors*)`` takes two
314 iterators. Elements of *data* are returned if the corresponding
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000315 value in *selectors* is true::
Benjamin Petersond23f8222009-04-05 19:13:16 +0000316
317 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
318 A, C, E, F
319
320 New function: ``itertools.combinations_with_replacement(*iter*, *r*)``
321 returns all the possible *r*-length combinations of elements from the
322 iterable *iter*. Unlike :func:`combinations`, individual elements
323 can be repeated in the generated combinations::
324
325 itertools.combinations_with_replacement('abc', 2) =>
326 ('a', 'a'), ('a', 'b'), ('a', 'c'),
327 ('b', 'b'), ('b', 'c'), ('c', 'c')
328
329 Note that elements are treated as unique depending on their position
330 in the input, not their actual values.
331
332 The :class:`itertools.count` function now has a *step* argument that
333 allows incrementing by values other than 1. :func:`count` also
334 now allows keyword arguments, and using non-integer values such as
335 floats or :class:`Decimal` instances. (Implemented by Raymond
336 Hettinger; :issue:`5032`.)
337
338 :func:`itertools.combinations` and :func:`itertools.product` were
339 previously raising :exc:`ValueError` for values of *r* larger than
340 the input iterable. This was deemed a specification error, so they
341 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
342
343* The :mod:`json` module was upgraded to version 2.0.9 of the
344 simplejson package, which includes a C extension that makes
345 encoding and decoding faster.
346 (Contributed by Bob Ippolito; :issue:`4136`.)
347
348 To support the new :class:`OrderedDict` type, :func:`json.load`
349 now has an optional *object_pairs_hook* parameter that will be called
350 with any object literal that decodes to a list of pairs.
351 (Contributed by Raymond Hettinger; :issue:`5381`.)
352
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000353* The :mod:`multiprocessing` module's :class:`Manager*` classes
354 can now be passed a callable that will be called whenever
355 a subprocess is started, along with a set of arguments that will be
356 passed to the callable.
357 (Contributed by lekma; :issue:`5585`.)
358
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000359* The :mod:`pydoc` module now has help for the various symbols that Python
360 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
361 (Contributed by David Laban; :issue:`4739`.)
362
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000363* The :mod:`re` module's :func:`split`, :func:`sub`, and :func:`subn`
364 now accept an optional *flags* argument, for consistency with the
365 other functions in the module. (Added by Gregory P. Smith.)
366
367* New function: the :mod:`subprocess` module's
368 :func:`check_output` runs a command with a specified set of arguments
Benjamin Petersond23f8222009-04-05 19:13:16 +0000369 and returns the command's output as a string when the command runs without
Georg Brandl1f01deb2009-01-03 22:47:39 +0000370 error, or raises a :exc:`CalledProcessError` exception otherwise.
371
372 ::
373
374 >>> subprocess.check_output(['df', '-h', '.'])
375 'Filesystem Size Used Avail Capacity Mounted on\n
376 /dev/disk0s2 52G 49G 3.0G 94% /\n'
377
378 >>> subprocess.check_output(['df', '-h', '/bogus'])
379 ...
380 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
381
382 (Contributed by Gregory P. Smith.)
383
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000384* New function: :func:`is_declared_global` in the :mod:`symtable` module
385 returns true for variables that are explicitly declared to be global,
386 false for ones that are implicitly global.
387 (Contributed by Jeremy Hylton.)
388
Benjamin Petersond23f8222009-04-05 19:13:16 +0000389* The ``sys.version_info`` value is now a named tuple, with attributes
390 named ``major``, ``minor``, ``micro``, ``releaselevel``, and ``serial``.
391 (Contributed by Ross Light; :issue:`4285`.)
392
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000393* The :mod:`threading` module's :meth:`Event.wait` method now returns
394 the internal flag on exit. This means the method will usually
395 return true because :meth:`wait` is supposed to block until the
396 internal flag becomes true. The return value will only be false if
397 a timeout was provided and the operation timed out.
398 (Contributed by XXX; :issue:`1674032`.)
399
Benjamin Petersond23f8222009-04-05 19:13:16 +0000400* The :mod:`unittest` module was enhanced in several ways.
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000401 The progress messages will now show 'x' for expected failures
402 and 'u' for unexpected successes when run in verbose mode.
403 (Contributed by Benjamin Peterson.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000404 Test cases can raise the :exc:`SkipTest` exception to skip a test.
405 (:issue:`1034053`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000406
407 The error messages for :meth:`assertEqual`,
408 :meth:`assertTrue`, and :meth:`assertFalse`
409 failures now provide more information. If you set the
410 :attr:`longMessage` attribute of your :class:`TestCase` classes to
411 true, both the standard error message and any additional message you
412 provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000413
414 The :meth:`assertRaises` and :meth:`failUnlessRaises` methods now
415 return a context handler when called without providing a callable
416 object to run. For example, you can write this::
417
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000418 with self.assertRaises(KeyError):
419 raise ValueError
Benjamin Petersond23f8222009-04-05 19:13:16 +0000420
421 (Implemented by Antoine Pitrou; :issue:`4444`.)
422
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000423 A number of new methods were added that provide more specialized
424 tests. Many of these methods were written by Google engineers
425 for use in their test suites; Gregory P. Smith, Michael Foord, and
426 GvR worked on merging them into Python's version of :mod:`unittest`.
427
428 * :meth:`assertIsNone` and :meth:`assertIsNotNone` take one
429 expression and verify that the result is or is not ``None``.
430
431 * :meth:`assertIs` and :meth:`assertIsNot` take two values and check
432 whether the two values evaluate to the same object or not.
433 (Added by Michael Foord; :issue:`2578`.)
434
435 * :meth:`assertGreater`, :meth:`assertGreaterEqual`,
436 :meth:`assertLess`, and :meth:`assertLessEqual` compare
437 two quantities.
438
439 * :meth:`assertMultiLineEqual` compares two strings, and if they're
440 not equal, displays a helpful comparison that highlights the
441 differences in the two strings.
442
443 * :meth:`assertRegexpMatches` checks whether its first argument is a
444 string matching a regular expression provided as its second argument.
445
446 * :meth:`assertRaisesRegexp` checks whether a particular exception
447 is raised, and then also checks that the string representation of
448 the exception matches the provided regular expression.
449
450 * :meth:`assertIn` and :meth:`assertNotIn` tests whether
451 *first* is or is not in *second*.
452
453 * :meth:`assertSameElements` tests whether two provided sequences
454 contain the same elements.
455
456 * :meth:`assertSetEqual` compares whether two sets are equal, and
457 only reports the differences between the sets in case of error.
458
459 * Similarly, :meth:`assertListEqual` and :meth:`assertTupleEqual`
460 compare the specified types and explain the differences.
461 More generally, :meth:`assertSequenceEqual` compares two sequences
462 and can optionally check whether both sequences are of a
463 particular type.
464
465 * :meth:`assertDictEqual` compares two dictionaries and reports the
466 differences. :meth:`assertDictContainsSubset` checks whether
467 all of the key/value pairs in *first* are found in *second*.
468
469 * A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
470 function. The :meth:`assertEqual` method will use the function
471 when both of the objects being compared are of the specified type.
472 This function should compare the two objects and raise an
473 exception if they don't match; it's a good idea for the function
474 to provide additional information about why the two objects are
475 matching, much as the new sequence comparison methods do.
476
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000477* The :func:`is_zipfile` function in the :mod:`zipfile` module will now
478 accept a file object, in addition to the path names accepted in earlier
479 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000480
Benjamin Petersond23f8222009-04-05 19:13:16 +0000481 :mod:`zipfile` now supports archiving empty directories and
482 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
483
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000484.. ======================================================================
485.. whole new modules get described in subsections here
486
Benjamin Petersond23f8222009-04-05 19:13:16 +0000487importlib: Importing Modules
488------------------------------
489
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000490Python 3.1 includes the :mod:`importlib` package, a re-implementation
491of the logic underlying Python's :keyword:`import` statement.
492:mod:`importlib` is useful for implementors of Python interpreters and
493to user who wish to write new importers that can participate in the
494import process. Python 2.7 doesn't contain the complete
495:mod:`importlib` package, but instead has a tiny subset that contains
496a single function, :func:`import_module`.
497
498``import_module(*name*, *package*=None)`` imports a module. *name* is
499a string containing the module or package's name. It's possible to do
500relative imports by providing a string that begins with a ``.``
501character, such as ``..utils.errors``. For relative imports, the
502*package* argument must be provided and is the name of the package that
503will be used as the anchor for
504the relative import. :func:`import_module` both inserts the imported
505module into ``sys.modules`` and returns the module object.
506
507Here are some examples::
508
509 >>> from importlib import import_module
510 >>> anydbm = import_module('anydbm') # Standard absolute import
511 >>> anydbm
512 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
513 >>> # Relative import
514 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
515 >>> sysconfig
516 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
517
518:mod:`importlib` was implemented by Brett Cannon and introduced in
519Python 3.1.
520
Benjamin Petersond23f8222009-04-05 19:13:16 +0000521
Benjamin Peterson5c6d7872009-02-06 02:40:07 +0000522ttk: Themed Widgets for Tk
523--------------------------
524
525Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
526widgets but have a more customizable appearance and can therefore more
527closely resemble the native platform's widgets. This widget
528set was originally called Tile, but was renamed to Ttk (for "themed Tk")
529on being added to Tcl/Tck release 8.5.
530
531XXX write a brief discussion and an example here.
532
533The :mod:`ttk` module was written by Guilherme Polo and added in
534:issue:`2983`. An alternate version called ``Tile.py``, written by
535Martin Franklin and maintained by Kevin Walzer, was proposed for
536inclusion in :issue:`2618`, but the authors argued that Guilherme
537Polo's work was more comprehensive.
538
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000539.. ======================================================================
540
541
542Build and C API Changes
543=======================
544
545Changes to Python's build process and to the C API include:
546
Georg Brandl1f01deb2009-01-03 22:47:39 +0000547* If you use the :file:`.gdbinit` file provided with Python,
548 the "pyo" macro in the 2.7 version will now work when the thread being
549 debugged doesn't hold the GIL; the macro will now acquire it before printing.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000550 (Contributed by Victor Stinner; :issue:`3632`.)
551
Benjamin Petersond23f8222009-04-05 19:13:16 +0000552* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000553 worker thread submit notifications to the main Python thread. This
554 is particularly useful for asynchronous IO operations.
555 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
556
Benjamin Petersond23f8222009-04-05 19:13:16 +0000557* The :program:`configure` script now checks for floating-point rounding bugs
558 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
559 preprocessor definition. No code currently uses this definition,
560 but it's available if anyone wishes to use it.
561 (Added by Mark Dickinson; :issue:`2937`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000562
563.. ======================================================================
564
565Port-Specific Changes: Windows
566-----------------------------------
567
Georg Brandl1f01deb2009-01-03 22:47:39 +0000568* The :mod:`msvcrt` module now contains some constants from
569 the :file:`crtassem.h` header file:
570 :data:`CRT_ASSEMBLY_VERSION`,
571 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
572 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000573 (Contributed by David Cournapeau; :issue:`4365`.)
574
575* The new :cfunc:`_beginthreadex` API is used to start threads, and
576 the native thread-local storage functions are now used.
577 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000578
579.. ======================================================================
580
581Port-Specific Changes: Mac OS X
582-----------------------------------
583
Benjamin Petersond23f8222009-04-05 19:13:16 +0000584* The ``/Library/Python/2.7/site-packages`` is now appended to
585 ``sys.path``, in order to share added packages between the system
586 installation and a user-installed copy of the same version.
587 (Changed by Ronald Oussoren; :issue:`4865`.)
588
589
590Other Changes and Fixes
591=======================
592
593* When importing a module from a :file:`.pyc` or :file:`.pyo` file
594 with an existing :file:`.py` counterpart, the :attr:`co_filename`
595 attributes of all code objects if the original filename is obsolete,
596 which can happen if the file has been renamed, moved, or is accessed
597 through different paths. (Patch by Ziga Seilnacht and Jean-Paul
598 Calderone; :issue:`1180193`.)
599
600* The :file:`regrtest.py` script now takes a :option:`--randseed=`
601 switch that takes an integer that will be used as the random seed
602 for the :option:`-r` option that executes tests in random order.
603 The :option:`-r` option also now reports the seed that was used
604 (Added by Collin Winter.)
605
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000606
607.. ======================================================================
608
609Porting to Python 2.7
610=====================
611
612This section lists previously described changes and other bugfixes
613that may require changes to your code:
614
615To be written.
616
617.. ======================================================================
618
619
620.. _acks27:
621
622Acknowledgements
623================
624
625The author would like to thank the following people for offering
626suggestions, corrections and assistance with various drafts of this
627article: no one yet.
628