blob: 928d7486ad646e0984669510feea5ee9b6c1b69e [file] [log] [blame]
Yury Selivanovd1da5072015-05-27 22:09:10 -04001****************************
2 What's New In Python 3.6
3****************************
4
5:Release: |release|
6:Date: |today|
7
8.. Rules for maintenance:
9
10 * Anyone can add text to this document. Do not spend very much time
11 on the wording of your changes, because your text will probably
12 get rewritten to some degree.
13
14 * The maintainer will go through Misc/NEWS periodically and add
15 changes; it's therefore more important to add your changes to
16 Misc/NEWS than to this file.
17
18 * This is not a complete list of every single change; completeness
19 is the purpose of Misc/NEWS. Some changes I consider too small
20 or esoteric to include. If such a change is added to the text,
21 I'll just remove it. (This is another reason you shouldn't spend
22 too much time on writing your addition.)
23
24 * If you want to draw your new text to the attention of the
25 maintainer, add 'XXX' to the beginning of the paragraph or
26 section.
27
28 * It's OK to just add a fragmentary note about a change. For
29 example: "XXX Describe the transmogrify() function added to the
30 socket module." The maintainer will research the change and
31 write the necessary text.
32
33 * You can comment out your additions if you like, but it's not
34 necessary (especially when a final release is some months away).
35
36 * Credit the author of a patch or bugfix. Just the name is
37 sufficient; the e-mail address isn't necessary.
38
39 * It's helpful to add the bug/patch number as a comment:
40
41 XXX Describe the transmogrify() function added to the socket
42 module.
43 (Contributed by P.Y. Developer in :issue:`12345`.)
44
45 This saves the maintainer the effort of going through the Mercurial log
46 when researching a change.
47
48This article explains the new features in Python 3.6, compared to 3.5.
49
50For full details, see the :source:`Misc/NEWS` file.
51
52.. note::
53
54 Prerelease users should be aware that this document is currently in draft
55 form. It will be updated substantially as Python 3.6 moves towards release,
56 so it's worth checking back even after reading earlier versions.
57
58
59Summary -- Release highlights
60=============================
61
62.. This section singles out the most important changes in Python 3.6.
63 Brevity is key.
64
Martin Panterbc1ee462016-02-13 00:41:37 +000065* PEP 498: :ref:`Formatted string literals <whatsnew-fstrings>`
Yury Selivanovd1da5072015-05-27 22:09:10 -040066
67.. PEP-sized items next.
68
69.. _pep-4XX:
70
71.. PEP 4XX: Virtual Environments
72.. =============================
73
74
75.. (Implemented by Foo Bar.)
76
77.. .. seealso::
78
79 :pep:`4XX` - Python Virtual Environments
80 PEP written by Carl Meyer
81
82
Victor Stinner34be8072016-03-14 12:04:26 +010083New Features
84============
85
Martin Panterbc1ee462016-02-13 00:41:37 +000086.. _whatsnew-fstrings:
87
88PEP 498: Formatted string literals
89----------------------------------
90
91Formatted string literals are a new kind of string literal, prefixed
92with ``'f'``. They are similar to the format strings accepted by
93:meth:`str.format`. They contain replacement fields surrounded by
94curly braces. The replacement fields are expressions, which are
95evaluated at run time, and then formatted using the :func:`format` protocol.
96
97 >>> name = "Fred"
98 >>> f"He said his name is {name}."
99 'He said his name is Fred.'
100
101See :pep:`498` and the main documentation at :ref:`f-strings`.
102
103
Victor Stinner34be8072016-03-14 12:04:26 +0100104PYTHONMALLOC environment variable
105---------------------------------
106
107The new :envvar:`PYTHONMALLOC` environment variable allows to set the Python
108memory allocators and/or install debug hooks.
109
110It is now possible to install debug hooks on Python memory allocators on Python
111compiled in release mode using ``PYTHONMALLOC=debug``. Effects of debug hooks:
112
113* Newly allocated memory is filled with the byte ``0xCB``
114* Freed memory is filled with the byte ``0xDB``
115* Detect violations of Python memory allocator API. For example,
116 :c:func:`PyObject_Free` called on a memory block allocated by
117 :c:func:`PyMem_Malloc`.
118* Detect write before the start of the buffer (buffer underflow)
119* Detect write after the end of the buffer (buffer overflow)
Victor Stinnerc4aec362016-03-14 22:26:53 +0100120* Check that the :term:`GIL <global interpreter lock>` is held when allocator
Victor Stinnerc2fc5682016-03-18 11:04:31 +0100121 functions of :c:data:`PYMEM_DOMAIN_OBJ` (ex: :c:func:`PyObject_Malloc`) and
122 :c:data:`PYMEM_DOMAIN_MEM` (ex: :c:func:`PyMem_Malloc`) domains are called.
123
Victor Stinner9b46a572016-03-18 15:10:43 +0100124Checking if the GIL is held is also a new feature of Python 3.6.
Victor Stinner34be8072016-03-14 12:04:26 +0100125
126See the :c:func:`PyMem_SetupDebugHooks` function for debug hooks on Python
127memory allocators.
128
129It is now also possible to force the usage of the :c:func:`malloc` allocator of
130the C library for all Python memory allocations using ``PYTHONMALLOC=malloc``.
131It helps to use external memory debuggers like Valgrind on a Python compiled in
132release mode.
133
Victor Stinner0611c262016-03-15 22:22:13 +0100134On error, the debug hooks on Python memory allocators now use the
135:mod:`tracemalloc` module to get the traceback where a memory block was
136allocated.
137
138Example of fatal error on buffer overflow using
139``python3.6 -X tracemalloc=5`` (store 5 frames in traces)::
140
141 Debug memory block at address p=0x7fbcd41666f8: API 'o'
142 4 bytes originally requested
143 The 7 pad bytes at p-7 are FORBIDDENBYTE, as expected.
144 The 8 pad bytes at tail=0x7fbcd41666fc are not all FORBIDDENBYTE (0xfb):
145 at tail+0: 0x02 *** OUCH
146 at tail+1: 0xfb
147 at tail+2: 0xfb
148 at tail+3: 0xfb
149 at tail+4: 0xfb
150 at tail+5: 0xfb
151 at tail+6: 0xfb
152 at tail+7: 0xfb
153 The block was made by call #1233329 to debug malloc/realloc.
154 Data at p: 1a 2b 30 00
155
156 Memory block allocated at (most recent call first):
157 File "test/test_bytes.py", line 323
158 File "unittest/case.py", line 600
159 File "unittest/case.py", line 648
160 File "unittest/suite.py", line 122
161 File "unittest/suite.py", line 84
162
163 Fatal Python error: bad trailing pad byte
164
165 Current thread 0x00007fbcdbd32700 (most recent call first):
166 File "test/test_bytes.py", line 323 in test_hex
167 File "unittest/case.py", line 600 in run
168 File "unittest/case.py", line 648 in __call__
169 File "unittest/suite.py", line 122 in run
170 File "unittest/suite.py", line 84 in __call__
171 File "unittest/suite.py", line 122 in run
172 File "unittest/suite.py", line 84 in __call__
173 ...
174
175(Contributed by Victor Stinner in :issue:`26516` and :issue:`26564`.)
Victor Stinner34be8072016-03-14 12:04:26 +0100176
177
Yury Selivanovd1da5072015-05-27 22:09:10 -0400178Other Language Changes
179======================
180
181* None yet.
182
183
184New Modules
185===========
186
187* None yet.
188
189
190Improved Modules
191================
192
Berker Peksagb6c95722015-10-08 13:58:49 +0300193datetime
194--------
195
Martin Panterfad4b602015-11-14 01:29:17 +0000196The :meth:`datetime.strftime() <datetime.datetime.strftime>` and
197:meth:`date.strftime() <datetime.date.strftime>` methods now support ISO 8601 date
Berker Peksagb6c95722015-10-08 13:58:49 +0300198directives ``%G``, ``%u`` and ``%V``.
199(Contributed by Ashley Anderson in :issue:`12006`.)
200
201
Victor Stinner404cdc52016-03-23 10:39:17 +0100202faulthandler
203------------
204
205On Windows, the :mod:`faulthandler` module now installs an handler for Windows
206exceptions: see :func:`faulthandler.enable`. (Contributed by Victor Stinner in
207:issue:`23848`.)
208
209
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +0200210os
211--
212
213A new :meth:`~os.scandir.close` method allows explicitly closing a
214:func:`~os.scandir` iterator. The :func:`~os.scandir` iterator now
215supports the :term:`context manager` protocol. If a :func:`scandir`
216iterator is neither exhausted nor explicitly closed a :exc:`ResourceWarning`
217will be emitted in its destructor.
218(Contributed by Serhiy Storchaka in :issue:`25994`.)
219
220
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300221pickle
222------
223
Martin Panterfad4b602015-11-14 01:29:17 +0000224Objects that need calling ``__new__`` with keyword arguments can now be pickled
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300225using :ref:`pickle protocols <pickle-protocols>` older than protocol version 4.
226Protocol version 4 already supports this case. (Contributed by Serhiy
227Storchaka in :issue:`24164`.)
228
229
Martin Panterfad4b602015-11-14 01:29:17 +0000230rlcompleter
231-----------
Serhiy Storchakaab824222015-09-27 13:43:50 +0300232
233Private and special attribute names now are omitted unless the prefix starts
Martin Panterfad4b602015-11-14 01:29:17 +0000234with underscores. A space or a colon is added after some completed keywords.
Serhiy Storchakaab824222015-09-27 13:43:50 +0300235(Contributed by Serhiy Storchaka in :issue:`25011` and :issue:`25209`.)
236
Martin Panter6fe39262015-11-13 23:54:02 +0000237Names of most attributes listed by :func:`dir` are now completed.
238Previously, names of properties and slots which were not yet created on
239an instance were excluded. (Contributed by Martin Panter in :issue:`25590`.)
240
Serhiy Storchakaab824222015-09-27 13:43:50 +0300241
R David Murray4f098062015-11-28 12:24:52 -0500242telnetlib
243---------
244
245:class:`~telnetlib.Telnet` is now a context manager (contributed by
246Stéphane Wirtel in :issue:`25485`).
247
248
Victor Stinner2c2a4e62016-03-11 22:17:48 +0100249unittest.mock
250-------------
251
252The :class:`~unittest.mock.Mock` class has the following improvements:
253
254* Two new methods, :meth:`Mock.assert_called()
255 <unittest.mock.Mock.assert_called>` and :meth:`Mock.assert_called_once()
256 <unittest.mock.Mock.assert_called_once>` to check if the mock object
257 was called.
258 (Contributed by Amit Saha in :issue:`26323`.)
259
260
Berker Peksag960e8482015-10-08 12:27:06 +0300261urllib.robotparser
262------------------
263
Martin Panterfad4b602015-11-14 01:29:17 +0000264:class:`~urllib.robotparser.RobotFileParser` now supports the ``Crawl-delay`` and
Berker Peksag960e8482015-10-08 12:27:06 +0300265``Request-rate`` extensions.
266(Contributed by Nikolay Bogoychev in :issue:`16099`.)
267
268
Victor Stinner914cde82016-03-19 01:03:51 +0100269warnings
270--------
271
272A new optional *source* parameter has been added to the
273:func:`warnings.warn_explicit` function: the destroyed object which emitted a
274:exc:`ResourceWarning`. A *source* attribute has also been added to
275:class:`warnings.WarningMessage` (contributed by Victor Stinner in
276:issue:`26568` and :issue:`26567`).
277
278When a :exc:`ResourceWarning` warning is logged, the :mod:`tracemalloc` is now
279used to try to retrieve the traceback where the detroyed object was allocated.
280
281Example with the script ``example.py``::
282
Victor Stinneree803a82016-03-19 10:33:25 +0100283 import warnings
Victor Stinner914cde82016-03-19 01:03:51 +0100284
Victor Stinneree803a82016-03-19 10:33:25 +0100285 def func():
286 return open(__file__)
287
288 f = func()
289 f = None
Victor Stinner914cde82016-03-19 01:03:51 +0100290
291Output of the command ``python3.6 -Wd -X tracemalloc=5 example.py``::
292
Victor Stinneree803a82016-03-19 10:33:25 +0100293 example.py:7: ResourceWarning: unclosed file <_io.TextIOWrapper name='example.py' mode='r' encoding='UTF-8'>
Victor Stinner914cde82016-03-19 01:03:51 +0100294 f = None
295 Object allocated at (most recent call first):
Victor Stinneree803a82016-03-19 10:33:25 +0100296 File "example.py", lineno 4
297 return open(__file__)
298 File "example.py", lineno 6
299 f = func()
Victor Stinner914cde82016-03-19 01:03:51 +0100300
301The "Object allocated at" traceback is new and only displayed if
Victor Stinneree803a82016-03-19 10:33:25 +0100302:mod:`tracemalloc` is tracing Python memory allocations and if the
303:mod:`warnings` was already imported.
Victor Stinner914cde82016-03-19 01:03:51 +0100304
305
Serhiy Storchaka503f9082016-02-08 00:02:25 +0200306zipfile
307-------
308
309A new :meth:`ZipInfo.from_file() <zipfile.ZipInfo.from_file>` class method
Martin Panter288ed032016-02-10 05:45:55 +0000310allows making a :class:`~zipfile.ZipInfo` instance from a filesystem file.
Serhiy Storchaka503f9082016-02-08 00:02:25 +0200311A new :meth:`ZipInfo.is_dir() <zipfile.ZipInfo.is_dir>` method can be used
312to check if the :class:`~zipfile.ZipInfo` instance represents a directory.
313(Contributed by Thomas Kluyver in :issue:`26039`.)
314
315
Martin Panter1fe0d132016-02-10 10:06:36 +0000316zlib
317----
318
319The :func:`~zlib.compress` function now accepts keyword arguments.
320(Contributed by Aviv Palivoda in :issue:`26243`.)
321
322
Yury Selivanovd1da5072015-05-27 22:09:10 -0400323Optimizations
324=============
325
Martin Panterfad4b602015-11-14 01:29:17 +0000326* The ASCII decoder is now up to 60 times as fast for error handlers
Victor Stinnerebcf9ed2015-10-14 10:10:00 +0200327 ``surrogateescape``, ``ignore`` and ``replace`` (Contributed
328 by Victor Stinner in :issue:`24870`).
Yury Selivanovd1da5072015-05-27 22:09:10 -0400329
Terry Jan Reedy6dc9ce12015-10-20 01:07:53 -0400330* The ASCII and the Latin1 encoders are now up to 3 times as fast for the
Martin Panterfad4b602015-11-14 01:29:17 +0000331 error handler ``surrogateescape`` (Contributed by Victor Stinner in :issue:`25227`).
Victor Stinnerc3713e92015-09-29 12:32:13 +0200332
Martin Panterfad4b602015-11-14 01:29:17 +0000333* The UTF-8 encoder is now up to 75 times as fast for error handlers
Victor Stinnerebcf9ed2015-10-14 10:10:00 +0200334 ``ignore``, ``replace``, ``surrogateescape``, ``surrogatepass`` (Contributed
335 by Victor Stinner in :issue:`25267`).
Victor Stinner01ada392015-10-01 21:54:51 +0200336
Martin Panterfad4b602015-11-14 01:29:17 +0000337* The UTF-8 decoder is now up to 15 times as fast for error handlers
Victor Stinnerebcf9ed2015-10-14 10:10:00 +0200338 ``ignore``, ``replace`` and ``surrogateescape`` (Contributed
339 by Victor Stinner in :issue:`25301`).
340
341* ``bytes % args`` is now up to 2 times faster. (Contributed by Victor Stinner
342 in :issue:`25349`).
343
344* ``bytearray % args`` is now between 2.5 and 5 times faster. (Contributed by
345 Victor Stinner in :issue:`25399`).
Victor Stinner1d65d912015-10-05 13:43:50 +0200346
Victor Stinner2bf89932015-10-14 11:25:33 +0200347* Optimize :meth:`bytes.fromhex` and :meth:`bytearray.fromhex`: they are now
348 between 2x and 3.5x faster. (Contributed by Victor Stinner in :issue:`25401`).
349
Victor Stinnerfac39562016-03-21 10:38:58 +0100350* Optimize ``bytes.replace(b'', b'.')`` and ``bytearray.replace(b'', b'.')``:
351 up to 80% faster. (Contributed by Josh Snider in :issue:`26574`).
352
Yury Selivanovd1da5072015-05-27 22:09:10 -0400353
354Build and C API Changes
355=======================
356
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000357* New :c:func:`Py_FinalizeEx` API which indicates if flushing buffered data
358 failed (:issue:`5319`).
Yury Selivanovd1da5072015-05-27 22:09:10 -0400359
360
361Deprecated
362==========
363
Yury Selivanov7a219112015-05-28 17:10:29 -0400364New Keywords
365------------
366
Yury Selivanov62f27b52015-08-03 14:57:21 -0400367``async`` and ``await`` are not recommended to be used as variable, class,
368function or module names. Introduced by :pep:`492` in Python 3.5, they will
369become proper keywords in Python 3.7.
Yury Selivanov7a219112015-05-28 17:10:29 -0400370
371
Yury Selivanovd1da5072015-05-27 22:09:10 -0400372Deprecated Python modules, functions and methods
373------------------------------------------------
374
Brett Cannoneae30792015-12-28 17:55:27 -0800375* :meth:`importlib.machinery.SourceFileLoader` and
376 :meth:`importlib.machinery.SourcelessFileLoader` are now deprecated. They
377 were the only remaining implementations of
378 :meth:`importlib.abc.Loader.load_module` in :mod:`importlib` that had not
379 been deprecated in previous versions of Python in favour of
380 :meth:`importlib.abc.Loader.exec_module`.
Yury Selivanovd1da5072015-05-27 22:09:10 -0400381
382
383Deprecated functions and types of the C API
384-------------------------------------------
385
386* None yet.
387
388
389Deprecated features
390-------------------
391
Brett Cannon9b638682015-10-16 15:14:27 -0700392* The ``pyvenv`` script has been deprecated in favour of ``python3 -m venv``.
393 This prevents confusion as to what Python interpreter ``pyvenv`` is
394 connected to and thus what Python interpreter will be used by the virtual
Brett Cannon63b85052016-01-15 13:33:03 -0800395 environment. (Contributed by Brett Cannon in :issue:`25154`.)
396
397* When performing a relative import, falling back on ``__name__`` and
398 ``__path__`` from the calling module when ``__spec__`` or
399 ``__package__`` are not defined now raises an :exc:`ImportWarning`.
400 (Contributed by Rose Ames in :issue:`25791`.)
Yury Selivanovd1da5072015-05-27 22:09:10 -0400401
402
Martin Panter7e3a91a2016-02-10 04:40:48 +0000403Deprecated Python behavior
404--------------------------
405
406* Raising the :exc:`StopIteration` exception inside a generator will now generate a
407 :exc:`DeprecationWarning`, and will trigger a :exc:`RuntimeError` in Python 3.7.
408 See :ref:`whatsnew-pep-479` for details.
409
410
Yury Selivanovd1da5072015-05-27 22:09:10 -0400411Removed
412=======
413
414API and Feature Removals
415------------------------
416
Yury Selivanov56613162015-07-23 17:51:34 +0300417* ``inspect.getmoduleinfo()`` was removed (was deprecated since CPython 3.3).
Yury Selivanov6dfbc5d2015-07-23 17:49:00 +0300418 :func:`inspect.getmodulename` should be used for obtaining the module
419 name for a given path.
420
Senthil Kumaran613065b2016-01-17 20:12:16 -0800421* ``traceback.Ignore`` class and ``traceback.usage``, ``traceback.modname``,
422 ``traceback.fullmodname``, ``traceback.find_lines_from_code``,
423 ``traceback.find_lines``, ``traceback.find_strings``,
424 ``traceback.find_executable_lines`` methods were removed from the
425 :mod:`traceback` module. They were undocumented methods deprecated since
426 Python 3.2 and equivalent functionality is available from private methods.
427
Yury Selivanovd1da5072015-05-27 22:09:10 -0400428
429Porting to Python 3.6
430=====================
431
432This section lists previously described changes and other bugfixes
433that may require changes to your code.
434
435Changes in the Python API
436-------------------------
437
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100438* The format of the ``co_lnotab`` attribute of code objects changed to support
439 negative line number delta. By default, Python does not emit bytecode with
440 negative line number delta. Functions using ``frame.f_lineno``,
441 ``PyFrame_GetLineNumber()`` or ``PyCode_Addr2Line()`` are not affected.
442 Functions decoding directly ``co_lnotab`` should be updated to use a signed
443 8-bit integer type for the line number delta, but it's only required to
444 support applications using negative line number delta. See
445 ``Objects/lnotab_notes.txt`` for the ``co_lnotab`` format and how to decode
446 it, and see the :pep:`511` for the rationale.
447
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800448* The functions in the :mod:`compileall` module now return booleans instead
449 of ``1`` or ``0`` to represent success or failure, respectively. Thanks to
450 booleans being a subclass of integers, this should only be an issue if you
451 were doing identity checks for ``1`` or ``0``. See :issue:`25768`.
452
Robert Collinsdfa95c92015-08-10 09:53:30 +1200453* Reading the :attr:`~urllib.parse.SplitResult.port` attribute of
454 :func:`urllib.parse.urlsplit` and :func:`~urllib.parse.urlparse` results
455 now raises :exc:`ValueError` for out-of-range values, rather than
456 returning :const:`None`. See :issue:`20059`.
Yury Selivanovd1da5072015-05-27 22:09:10 -0400457
Brett Cannonc0d91af2015-10-16 12:21:37 -0700458* The :mod:`imp` module now raises a :exc:`DeprecationWarning` instead of
459 :exc:`PendingDeprecationWarning`.
460
Martin Panter28a465c2015-11-14 12:52:08 +0000461* The following modules have had missing APIs added to their :attr:`__all__`
Martin Pantere8afd012016-01-16 07:01:46 +0000462 attributes to match the documented APIs: :mod:`calendar`, :mod:`csv`,
463 :mod:`enum`, :mod:`fileinput`, :mod:`ftplib`, :mod:`logging`,
464 :mod:`optparse`, :mod:`tarfile`, :mod:`threading` and
Martin Panter28a465c2015-11-14 12:52:08 +0000465 :mod:`wave`. This means they will export new symbols when ``import *``
466 is used. See :issue:`23883`.
467
Brett Cannon849113a2016-01-22 15:25:50 -0800468* When performing a relative import, if ``__package__`` does not compare equal
469 to ``__spec__.parent`` then :exc:`ImportWarning` is raised.
470 (Contributed by Brett Cannon in :issue:`25791`.)
Brett Cannon63b85052016-01-15 13:33:03 -0800471
Brett Cannon9fa81262016-01-22 16:39:02 -0800472* When a relative import is performed and no parent package is known, then
473 :exc:`ImportError` will be raised. Previously, :exc:`SystemError` could be
Martin Panterd9108d12016-02-21 08:49:56 +0000474 raised. (Contributed by Brett Cannon in :issue:`18018`.)
475
476* Servers based on the :mod:`socketserver` module, including those
477 defined in :mod:`http.server`, :mod:`xmlrpc.server` and
478 :mod:`wsgiref.simple_server`, now only catch exceptions derived
479 from :exc:`Exception`. Therefore if a request handler raises
480 an exception like :exc:`SystemExit` or :exc:`KeyboardInterrupt`,
481 :meth:`~socketserver.BaseServer.handle_error` is no longer called, and
482 the exception will stop a single-threaded server. (Contributed by
483 Martin Panter in :issue:`23430`.)
Brett Cannon9fa81262016-01-22 16:39:02 -0800484
Berker Peksag3c3d7f42016-03-19 11:44:17 +0200485* :func:`spwd.getspnam` now raises a :exc:`PermissionError` instead of
486 :exc:`KeyError` if the user doesn't have privileges.
Yury Selivanovd1da5072015-05-27 22:09:10 -0400487
488Changes in the C API
489--------------------
490
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000491* :c:func:`Py_Exit` (and the main interpreter) now override the exit status
492 with 120 if flushing buffered data failed. See :issue:`5319`.