blob: 89912a9cf71e1412c14c2e34d20859d636db7fc4 [file] [log] [blame]
Georg Brandlb80f5112012-09-30 09:11:58 +02001****************************
2 What's New In Python 3.4
3****************************
4
Georg Brandl3b80d342012-10-28 13:37:54 +01005.. :Author: Someone <email>
6 (uncomment if there is a principal author)
Georg Brandlb80f5112012-09-30 09:11:58 +02007
8.. Rules for maintenance:
9
Nick Coghlan03074fd2012-09-30 18:51:53 +053010 * Anyone can add text to this document, but the maintainer reserves the
11 right to rewrite any additions. In particular, for obscure or esoteric
12 features, the maintainer may reduce any addition to a simple reference to
13 the new documentation rather than explaining the feature inline.
Georg Brandlb80f5112012-09-30 09:11:58 +020014
Nick Coghlan03074fd2012-09-30 18:51:53 +053015 * While the maintainer will periodically go through Misc/NEWS
16 and add changes, it's best not to rely on this. We know from experience
17 that any changes that aren't in the What's New documentation around the
18 time of the original release will remain largely unknown to the community
19 for years, even if they're added later. We also know from experience that
20 other priorities can arise, and the maintainer will run out of time to do
21 updates - in such cases, end users will be much better served by partial
22 notifications that at least give a hint about new features to
23 investigate.
Georg Brandlb80f5112012-09-30 09:11:58 +020024
25 * This is not a complete list of every single change; completeness
Nick Coghlan03074fd2012-09-30 18:51:53 +053026 is the purpose of Misc/NEWS. The What's New should focus on changes that
27 are visible to Python *users* and that *require* a feature release (i.e.
28 most bug fixes should only be recorded in Misc/NEWS)
29
30 * PEPs should not be marked Final until they have an entry in What's New.
31 A placeholder entry that is just a section header and a link to the PEP
32 (e.g ":pep:`397` has been implemented") is acceptable. If a PEP has been
33 implemented and noted in What's New, don't forget to mark it as Final!
Georg Brandlb80f5112012-09-30 09:11:58 +020034
35 * If you want to draw your new text to the attention of the
36 maintainer, add 'XXX' to the beginning of the paragraph or
37 section.
38
Nick Coghlan03074fd2012-09-30 18:51:53 +053039 * It's OK to add just a very brief note about a change. For
40 example: "The :ref:`~socket.transmogrify()` function was added to the
41 :mod:`socket` module." The maintainer will research the change and
42 write the necessary text (if appropriate). The advantage of doing this
43 is that even if no more descriptive text is ever added, readers will at
44 least have a notification that the new feature exists and a link to the
45 relevant documentation.
Georg Brandlb80f5112012-09-30 09:11:58 +020046
47 * You can comment out your additions if you like, but it's not
48 necessary (especially when a final release is some months away).
49
50 * Credit the author of a patch or bugfix. Just the name is
51 sufficient; the e-mail address isn't necessary.
52
53 * It's helpful to add the bug/patch number as a comment:
54
Nick Coghlan03074fd2012-09-30 18:51:53 +053055 The :ref:`~socket.transmogrify()` function was added to the
56 :mod:`socket` module. (Contributed by P.Y. Developer in :issue:`12345`.)
Georg Brandlb80f5112012-09-30 09:11:58 +020057
58 This saves the maintainer the effort of going through the Mercurial log
59 when researching a change.
60
Nick Coghlan03074fd2012-09-30 18:51:53 +053061 * Cross referencing tip: :ref:`mod.attr` will display as ``mod.attr``,
62 while :ref:`~mod.attr` will display as ``attr``.
63
Georg Brandlb80f5112012-09-30 09:11:58 +020064This article explains the new features in Python 3.4, compared to 3.3.
65
Nick Coghlan03074fd2012-09-30 18:51:53 +053066.. Python 3.4 was released on TBD.
Georg Brandlb80f5112012-09-30 09:11:58 +020067
Ezio Melotti25bbe5e2012-11-17 19:30:48 +020068For full details, see the
69`changelog <http://docs.python.org/3.4/whatsnew/changelog.html>`_.
Georg Brandlb80f5112012-09-30 09:11:58 +020070
71.. note:: Prerelease users should be aware that this document is currently in
72 draft form. It will be updated substantially as Python 3.4 moves towards
73 release, so it's worth checking back even after reading earlier versions.
74
75
76.. seealso::
77
Nick Coghlan03074fd2012-09-30 18:51:53 +053078 .. :pep:`4XX` - Python 3.4 Release Schedule
Georg Brandlb80f5112012-09-30 09:11:58 +020079
80
81Summary -- Release highlights
82=============================
83
Kristjan Valur Jonssona1e82442013-03-26 13:56:14 +000084.. This section singles out the most important changes in Python 3.4.
Georg Brandlb80f5112012-09-30 09:11:58 +020085 Brevity is key.
86
87New syntax features:
88
89* None yet.
90
91New library modules:
92
Victor Stinner84e33c82013-06-21 00:31:55 +020093* :mod:`enum`: Implementation of the :pep:`435`.
Victor Stinner4aea4a02013-09-04 20:30:34 +020094* :mod:`selectors`: High-level and efficient I/O multiplexing, built upon the
95 :mod:`select` module primitives.
Georg Brandlb80f5112012-09-30 09:11:58 +020096
97New built-in features:
98
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +020099* :ref:`PEP 442: Safe object finalization <pep-442>`.
100* :ref:`PEP 445: Configurable memory allocators <pep-445>`.
Victor Stinnerdaf45552013-08-28 00:53:59 +0200101* :ref:`PEP 446: Make newly created file descriptors non-inheritable <pep-446>`.
Georg Brandlb80f5112012-09-30 09:11:58 +0200102
103Implementation improvements:
104
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200105* A more efficient :mod:`marshal` format (:issue:`16475`).
106* Improve finalization of Python modules to avoid setting their globals
107 to None, in most cases (:issue:`18214`).
Georg Brandlb80f5112012-09-30 09:11:58 +0200108
109Significantly Improved Library Modules:
110
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200111* Single-dispatch generic functions (:pep:`443`)
112* SHA-3 (Keccak) support for :mod:`hashlib`.
113* TLSv1.1 and TLSv1.2 support for :mod:`ssl`.
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100114* :mod:`multiprocessing` now has option to avoid using :func:`os.fork`
115 on Unix (:issue:`8713`).
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200116
Georg Brandlb80f5112012-09-30 09:11:58 +0200117Security improvements:
118
Christian Heimesad73a9c2013-08-10 16:36:18 +0200119* command line option for :ref:`isolated mode <using-on-misc-options>`,
120 :issue:`16499`.
Georg Brandlb80f5112012-09-30 09:11:58 +0200121
122Please read on for a comprehensive list of user-facing changes.
123
Victor Stinnerdaf45552013-08-28 00:53:59 +0200124.. _pep-446:
125
126PEP 446: Make newly created file descriptors non-inheritable
127============================================================
128
Georg Brandl5642ff92013-09-15 10:37:57 +0200129The :pep:`446` makes newly created file descriptors :ref:`non-inheritable
130<fd_inheritance>`. New functions and methods:
Victor Stinnerdaf45552013-08-28 00:53:59 +0200131
132* :func:`os.get_inheritable`, :func:`os.set_inheritable`
133* :func:`os.get_handle_inheritable`, :func:`os.set_handle_inheritable`
134* :meth:`socket.socket.get_inheritable`, :meth:`socket.socket.set_inheritable`
135
136
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200137.. _pep-445:
138
139PEP 445: Add new APIs to customize Python memory allocators
140===========================================================
Georg Brandlb80f5112012-09-30 09:11:58 +0200141
Victor Stinner0507bf52013-07-07 02:05:46 +0200142The :pep:`445` adds new Application Programming Interfaces (API) to customize
143Python memory allocators.
Georg Brandlb80f5112012-09-30 09:11:58 +0200144
145
Antoine Pitrou796564c2013-07-30 19:59:21 +0200146.. _pep-442:
147
148PEP 442: Safe object finalization
149=================================
150
151This PEP removes the current limitations and quirks of object finalization.
152With it, objects with :meth:`__del__` methods, as well as generators
153with :keyword:`finally` clauses, can be finalized when they are part of a
154reference cycle.
155
156.. seealso::
157
158 :pep:`442` - Safe object finalization
159 PEP written and implemented by Antoine Pitrou
160
Georg Brandlb80f5112012-09-30 09:11:58 +0200161
162Other Language Changes
163======================
164
165Some smaller changes made to the core Python language are:
166
Georg Brandl50de8502012-09-30 14:39:18 +0200167* Unicode database updated to UCD version 6.2.
Georg Brandlb80f5112012-09-30 09:11:58 +0200168
R David Murray9a2f1392013-06-28 13:31:19 -0400169* :func:`min` and :func:`max` now accept a *default* argument that can be used
170 to specify the value they return if the iterable they are evaluating has no
171 elements. Contributed by Julian Berman in :issue:`18111`.
Georg Brandlb80f5112012-09-30 09:11:58 +0200172
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200173* Module objects are now :mod:`weakref`'able.
174
Georg Brandlb80f5112012-09-30 09:11:58 +0200175
176New Modules
177===========
178
Charles-François Natali243d8d82013-09-04 19:02:49 +0200179selectors
180---------
Georg Brandlb80f5112012-09-30 09:11:58 +0200181
Charles-François Natali243d8d82013-09-04 19:02:49 +0200182The new :mod:`selectors` module allows high-level and efficient I/O
183multiplexing, built upon the :mod:`select` module primitives.
Georg Brandlb80f5112012-09-30 09:11:58 +0200184
185
186Improved Modules
187================
188
Antoine Pitroud6cbd342013-08-12 20:48:15 +0200189aifc
190----
191
192The :meth:`~aifc.getparams` method now returns a namedtuple rather than a
193plain tuple. (Contributed by Claudiu Popa in :issue:`17818`.)
194
195
196colorsys
197--------
198
199The number of digits in the coefficients for the RGB --- YIQ conversions have
200been expanded so that they match the FCC NTSC versions. The change in
201results should be less than 1% and may better match results found elsewhere.
202
R David Murray8e37d5d2013-04-13 14:49:48 -0400203
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000204dis
205---
206
207The :mod:`dis` module is now built around an :class:`Instruction` class that
208provides details of individual bytecode operations and a
209:func:`get_instructions` iterator that emits the Instruction stream for a
210given piece of Python code. The various display tools in the :mod:`dis`
211module have been updated to be based on these new components.
212
213The new :class:`dis.Bytecode` class provides an object-oriented API for
214inspecting bytecode, both in human-readable form and for iterating over
215instructions.
216
217(Contributed by Nick Coghlan, Ryan Kelly and Thomas Kluyver in :issue:`11816`)
218
Antoine Pitroud6cbd342013-08-12 20:48:15 +0200219
R David Murray5a9d7062012-11-21 15:09:21 -0500220doctest
221-------
222
R David Murray5707d502013-06-23 14:24:13 -0400223Added :data:`~doctest.FAIL_FAST` flag to halt test running as soon as the first
224failure is detected. (Contributed by R. David Murray and Daniel Urban in
225:issue:`16522`.)
226
227Updated the doctest command line interface to use :mod:`argparse`, and added
228``-o`` and ``-f`` options to the interface. ``-o`` allows doctest options to
229be specified on the command line, and ``-f`` is a shorthand for ``-o
230FAIL_FAST`` (to parallel the similar option supported by the :mod:`unittest`
231CLI). (Contributed by R. David Murray in :issue:`11390`.)
Georg Brandlb80f5112012-09-30 09:11:58 +0200232
R David Murray8e37d5d2013-04-13 14:49:48 -0400233
R David Murraybb17d2b2013-08-09 16:15:28 -0400234email
235-----
236
237:meth:`~email.message.Message.as_string` now accepts a *policy* argument to
238override the default policy of the message when generating a string
239representation of it. This means that ``as_string`` can now be used in more
240circumstances, instead of having to create and use a :mod:`~email.generator` in
241order to pass formatting parameters to its ``flatten`` method.
242
243New method :meth:`~email.message.Message.as_bytes` added to produce a bytes
244representation of the message in a fashion similar to how ``as_string``
245produces a string representation. It does not accept the *maxheaderlen*
246argument, but does accept the *unixfrom* and *policy* arguments. The
247:class:`~email.message.Message` :meth:`~email.message.Message.__bytes__` method
248calls it, meaning that ``bytes(mymsg)`` will now produce the intuitive
249result: a bytes object containing the fully formatted message.
250
251(Contributed by R. David Murray in :issue:`18600`.)
252
253
Victor Stinner854ffcb2013-06-21 00:36:30 +0200254functools
255---------
256
257New :func:`functools.singledispatch` decorator: see the :pep:`443`.
258
Nick Coghlane8c45d62013-07-28 20:00:01 +1000259
Antoine Pitroud6cbd342013-08-12 20:48:15 +0200260inspect
261-------
262
263:func:`~inspect.unwrap` makes it easy to unravel wrapper function chains
264created by :func:`functools.wraps` (and any other API that sets the
265``__wrapped__`` attribute on a wrapper function).
266
267
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200268mmap
269----
270
271mmap objects can now be weakref'ed.
272
273(Contributed by Valerie Lambert in :issue:`4885`.)
274
275
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100276multiprocessing
277---------------
278
279On Unix two new *start methods* have been added for starting processes
280using :mod:`multiprocessing`. These make the mixing of processes with
281threads more robust. See :issue:`8713`.
282
283Also, except when using the old *fork* start method, child processes
284will no longer inherit unneeded handles/file descriptors from their parents.
285
286
Victor Stinnerdaf45552013-08-28 00:53:59 +0200287os
288--
289
290New functions to get and set the `inheritable flag <fd_inheritance>`_ of a file
291descriptors or a Windows handle:
292
293* :func:`os.get_inheritable`, :func:`os.set_inheritable`
294* :func:`os.get_handle_inheritable`, :func:`os.set_handle_inheritable`
295
296
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200297poplib
298------
299
300New :meth:`~poplib.POP3.stls` method to switch a clear-text POP3 session into
301an encrypted POP3 session.
302
303New :meth:`~poplib.POP3.capa` method to query the capabilities advertised by the
304POP3 server.
305
306(Contributed by Lorenzo Catucci in :issue:`4473`.)
307
308
R David Murray8e37d5d2013-04-13 14:49:48 -0400309smtplib
310-------
311
R David Murray8a345962013-04-14 06:46:35 -0400312:exc:`~smtplib.SMTPException` is now a subclass of :exc:`OSError`, which allows
R David Murray8e37d5d2013-04-13 14:49:48 -0400313both socket level errors and SMTP protocol level errors to be caught in one
314try/except statement by code that only cares whether or not an error occurred.
315(:issue:`2118`).
316
Antoine Pitroud6cbd342013-08-12 20:48:15 +0200317
Victor Stinnerdaf45552013-08-28 00:53:59 +0200318socket
319------
320
Georg Brandl5642ff92013-09-15 10:37:57 +0200321Socket objects have new methods to get or set their :ref:`inheritable flag
322<fd_inheritance>`:
Victor Stinnerdaf45552013-08-28 00:53:59 +0200323
324* :meth:`socket.socket.get_inheritable`, :meth:`socket.socket.set_inheritable`
325
Eli Bendersky34567ec2013-08-31 15:18:48 -0700326The ``socket.AF_*`` and ``socket.SOCK_*`` constants are enumeration values,
327using the new :mod:`enum` module. This allows descriptive reporting during
328debugging, instead of seeing integer "magic numbers".
Victor Stinnerdaf45552013-08-28 00:53:59 +0200329
Christian Heimes24cd4cf2013-06-22 19:31:58 +0200330ssl
331---
332
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200333TLSv1.1 and TLSv1.2 support.
Christian Heimes70833a82013-06-22 19:34:17 +0200334
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200335(Contributed by Michele Orrù and Antoine Pitrou in :issue:`16692`)
Christian Heimes24cd4cf2013-06-22 19:31:58 +0200336
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200337* New diagnostic functions :func:`~ssl.get_default_verify_paths`,
338 :meth:`~ssl.SSLContext.cert_store_stats` and
339 :meth:`~ssl.SSLContext.get_ca_certs`
340
341* Add :func:`ssl.enum_cert_store` to retrieve certificates and CRL from Windows'
342 cert store.
Christian Heimes24cd4cf2013-06-22 19:31:58 +0200343
344(Contributed by Christian Heimes in :issue:`18143`, :issue:`18147` and
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200345 :issue:`17134`.)
346
347Support for server-side SNI using the new
348:meth:`ssl.SSLContext.set_servername_callback` method.
349
350(Contributed by Daniel Black in :issue:`8109`.)
351
352
Antoine Pitroud6cbd342013-08-12 20:48:15 +0200353stat
354----
355
356The :mod:`stat` module is now backed by a C implementation in :mod:`_stat`. A C
357implementation is required as most of the values aren't standardized and
358platform-dependent. (Contributed by Christian Heimes in :issue:`11016`.)
359
360The module supports new file types: door, event port and whiteout.
361
362
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200363struct
364------
365
366Streaming struct unpacking using :func:`struct.iter_unpack`.
367
368(Contributed by Antoine Pitrou in :issue:`17804`.)
369
370
Serhiy Storchakae06a8962013-09-04 00:43:03 +0300371sunau
372-----
373
374The :meth:`~sunau.getparams` method now returns a namedtuple rather than a
375plain tuple. (Contributed by Claudiu Popa in :issue:`18901`.)
376
Serhiy Storchaka34d20132013-09-05 17:01:53 +0300377:meth:`sunau.open` now supports the context manager protocol (:issue:`18878`).
378
Serhiy Storchakae06a8962013-09-04 00:43:03 +0300379
Andrew Kuchling173a1572013-09-15 18:15:56 -0400380traceback
381---------
382
383A new :func:`traceback.clear_frames` function takes a traceback object
384and clears the local variables in all of the frames it references,
385reducing the amount of memory consumed (:issue:`1565525`).
386
387
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200388urllib
389------
390
391Add support.for ``data:`` URLs in :mod:`urllib.request`.
392
393(Contributed by Mathias Panzenböck in :issue:`16423`.)
394
395
396unittest
397--------
398
399Support for easy dynamically-generated subtests using the
400:meth:`~unittest.TestCase.subTest` context manager.
401
402(Contributed by Antoine Pitrou in :issue:`16997`.)
403
R David Murray8e37d5d2013-04-13 14:49:48 -0400404
R David Murray671cd322013-04-10 12:31:43 -0400405wave
406----
407
408The :meth:`~wave.getparams` method now returns a namedtuple rather than a
409plain tuple. (Contributed by Claudiu Popa in :issue:`17487`.)
410
R David Murrayc91d5ee2013-07-31 13:46:08 -0400411:meth:`wave.open` now supports the context manager protocol. (Contributed
412by Claudiu Popa in :issue:`17616`.)
413
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200414
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200415weakref
416-------
417
418New :class:`~weakref.WeakMethod` class simulates weak references to bound
419methods.
420
421(Contributed by Antoine Pitrou in :issue:`14631`.)
422
423
424xml.etree
425---------
426
427Add an event-driven parser for non-blocking applications,
Eli Benderskyb5869342013-08-30 05:51:20 -0700428:class:`~xml.etree.ElementTree.XMLPullParser`.
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200429
Eli Benderskyb5869342013-08-30 05:51:20 -0700430(Contributed by Antoine Pitrou in :issue:`17741`.)
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200431
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200432Other improvements
433==================
434
435Tab-completion is now enabled by default in the interactive interpreter.
436
437(Contributed by Antoine Pitrou and Éric Araujo in :issue:`5845`.)
438
Eli Bendersky96d848a2013-09-06 06:55:58 -0700439Python invocation changes
440=========================
441
442Invoking the Python interpreter with ``--version`` now outputs the version to
443standard output instead of standard error (:issue:`18338`). Similar changes
444were made to :mod:`argparse` (:issue:`18920`) and other modules that have
445script-like invocation capabilities (:issue:`18922`).
Antoine Pitrouf89aa9a2013-08-12 20:46:47 +0200446
Georg Brandlb80f5112012-09-30 09:11:58 +0200447Optimizations
448=============
449
450Major performance enhancements have been added:
451
Victor Stinnere64322e2012-10-30 23:12:47 +0100452* The UTF-32 decoder is now 3x to 4x faster.
Georg Brandlb80f5112012-09-30 09:11:58 +0200453
Raymond Hettingerc301b552013-08-19 09:12:20 -0700454* The cost of hash collisions for sets is now reduced. Each hash table
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700455 probe now checks a series of consecutive, adjacent key/hash pairs before
456 continuing to make random probes through the hash table. This exploits
457 cache locality to make collision resolution less expensive.
458
459 The collision resolution scheme can be described as a hybrid of linear
460 probing and open addressing. The number of additional linear probes
461 defaults to nine. This can be changed at compile-time by defining
462 LINEAR_PROBES to be any value. Set LINEAR_PROBES=0 to turn-off
463 linear probing entirely.
Raymond Hettingerc301b552013-08-19 09:12:20 -0700464
Andrew Kuchling13aa7812013-08-19 13:07:18 -0400465 (Contributed by Raymond Hettinger in :issue"`18771`.)
Raymond Hettingerc301b552013-08-19 09:12:20 -0700466
Georg Brandlb80f5112012-09-30 09:11:58 +0200467
468Build and C API Changes
469=======================
470
471Changes to Python's build process and to the C API include:
472
473* None yet.
474
475
476Deprecated
477==========
478
479Unsupported Operating Systems
480-----------------------------
481
Victor Stinnerf3fd13b2013-08-04 10:30:57 +0200482* OS/2
Victor Stinnerf3fd13b2013-08-04 10:30:57 +0200483* Windows 2000
Georg Brandlb80f5112012-09-30 09:11:58 +0200484
485
486Deprecated Python modules, functions and methods
487------------------------------------------------
488
Terry Jan Reedy2b6c26e2013-03-21 19:36:26 -0400489* :meth:`difflib.SequenceMatcher.isbjunk` and
Andrew Kuchling0d0813a2013-06-15 13:29:09 -0400490 :meth:`difflib.SequenceMatcher.isbpopular` were removed: use ``x in sm.bjunk`` and
491 ``x in sm.bpopular``, where *sm* is a :class:`~difflib.SequenceMatcher` object.
Georg Brandlb80f5112012-09-30 09:11:58 +0200492
Brett Cannon82b3d6a2013-06-14 22:37:11 -0400493* :func:`importlib.util.module_for_loader` is pending deprecation. Using
494 :func:`importlib.util.module_to_load` and
495 :meth:`importlib.abc.Loader.init_module_attrs` allows subclasses of a loader
496 to more easily customize module loading.
497
Brett Cannone4f41de2013-06-16 13:13:40 -0400498* The :mod:`imp` module is pending deprecation. To keep compatibility with
499 Python 2/3 code bases, the module's removal is currently not scheduled.
500
Georg Brandlb80f5112012-09-30 09:11:58 +0200501
502Deprecated functions and types of the C API
503-------------------------------------------
504
505* None yet.
506
507
508Deprecated features
509-------------------
510
Brett Cannon82b3d6a2013-06-14 22:37:11 -0400511* None yet.
Georg Brandlb80f5112012-09-30 09:11:58 +0200512
513
Benjamin Peterson88f3b232012-10-04 12:45:10 -0400514Porting to Python 3.4
Georg Brandlb80f5112012-09-30 09:11:58 +0200515=====================
516
517This section lists previously described changes and other bugfixes
518that may require changes to your code.
519
Brett Cannon777622b2013-04-09 17:03:10 -0400520* The ABCs defined in :mod:`importlib.abc` now either raise the appropriate
521 exception or return a default value instead of raising
522 :exc:`NotImplementedError` blindly. This will only affect code calling
523 :func:`super` and falling through all the way to the ABCs. For compatibility,
524 catch both :exc:`NotImplementedError` or the appropriate exception as needed.
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400525
526* The module type now initializes the :attr:`__package__` and :attr:`__loader__`
527 attributes to ``None`` by default. To determine if these attributes were set
528 in a backwards-compatible fashion, use e.g.
Brett Cannon3dc48d62013-05-28 18:35:54 -0400529 ``getattr(module, '__loader__', None) is not None``.
530
531* :meth:`importlib.util.module_for_loader` now sets ``__loader__`` and
532 ``__package__`` unconditionally to properly support reloading. If this is not
533 desired then you will need to set these attributes manually. You can use
Brett Cannon028d5122013-05-31 18:02:11 -0400534 :func:`importlib.util.module_to_load` for module management.
Brett Cannon3e0651b2013-05-31 23:18:39 -0400535
536* Import now resets relevant attributes (e.g. ``__name__``, ``__loader__``,
537 ``__package__``, ``__file__``, ``__cached__``) unconditionally when reloading.
538
539* Frozen packages no longer set ``__path__`` to a list containg the package name
540 but an empty list instead. Determing if a module is a package should be done
541 using ``hasattr(module, '__path__')``.
Brett Cannon8f5ac512013-06-12 23:29:18 -0400542
543* :c:func:`PyErr_SetImportError` now sets :exc:`TypeError` when its **msg**
Brett Cannon82da8882013-07-04 17:48:16 -0400544 argument is not set. Previously only ``NULL`` was returned with no exception
545 set.
Brett Cannon33915eb2013-06-14 18:33:00 -0400546
547* :func:`py_compile.compile` now raises :exc:`FileExistsError` if the file path
548 it would write to is a symlink or a non-regular file. This is to act as a
549 warning that import will overwrite those files with a regular file regardless
550 of what type of file path they were originally.
Brett Cannonf4375ef2013-06-16 18:05:54 -0400551
552* :meth:`importlib.abc.SourceLoader.get_source` no longer raises
553 :exc:`ImportError` when the source code being loaded triggers a
554 :exc:`SyntaxError` or :exc:`UnicodeDecodeError`. As :exc:`ImportError` is
555 meant to be raised only when source code cannot be found but it should, it was
556 felt to be over-reaching/overloading of that meaning when the source code is
557 found but improperly structured. If you were catching ImportError before and
558 wish to continue to ignore syntax or decoding issues, catch all three
Victor Stinner84e33c82013-06-21 00:31:55 +0200559 exceptions now.
Nick Coghlan24c05bc2013-07-15 21:13:08 +1000560
561* :func:`functools.update_wrapper` and :func:`functools.wraps` now correctly
562 set the ``__wrapped__`` attribute even if the wrapped function had a
563 wrapped attribute set. This means ``__wrapped__`` attributes now correctly
564 link a stack of decorated functions rather than every ``__wrapped__``
565 attribute in the chain referring to the innermost function. Introspection
Nick Coghlane8c45d62013-07-28 20:00:01 +1000566 libraries that assumed the previous behaviour was intentional can use
567 :func:`inspect.unwrap` to gain equivalent behaviour.