Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1 | **************************** |
| 2 | What's New In Python 3.8 |
| 3 | **************************** |
| 4 | |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 5 | .. Rules for maintenance: |
| 6 | |
| 7 | * Anyone can add text to this document. Do not spend very much time |
| 8 | on the wording of your changes, because your text will probably |
| 9 | get rewritten to some degree. |
| 10 | |
| 11 | * The maintainer will go through Misc/NEWS periodically and add |
| 12 | changes; it's therefore more important to add your changes to |
| 13 | Misc/NEWS than to this file. |
| 14 | |
| 15 | * This is not a complete list of every single change; completeness |
| 16 | is the purpose of Misc/NEWS. Some changes I consider too small |
| 17 | or esoteric to include. If such a change is added to the text, |
| 18 | I'll just remove it. (This is another reason you shouldn't spend |
| 19 | too much time on writing your addition.) |
| 20 | |
| 21 | * If you want to draw your new text to the attention of the |
| 22 | maintainer, add 'XXX' to the beginning of the paragraph or |
| 23 | section. |
| 24 | |
| 25 | * It's OK to just add a fragmentary note about a change. For |
| 26 | example: "XXX Describe the transmogrify() function added to the |
| 27 | socket module." The maintainer will research the change and |
| 28 | write the necessary text. |
| 29 | |
| 30 | * You can comment out your additions if you like, but it's not |
| 31 | necessary (especially when a final release is some months away). |
| 32 | |
| 33 | * Credit the author of a patch or bugfix. Just the name is |
| 34 | sufficient; the e-mail address isn't necessary. |
| 35 | |
| 36 | * It's helpful to add the bug/patch number as a comment: |
| 37 | |
| 38 | XXX Describe the transmogrify() function added to the socket |
| 39 | module. |
| 40 | (Contributed by P.Y. Developer in :issue:`12345`.) |
| 41 | |
Raymond Hettinger | 66a34d3 | 2019-08-12 15:55:18 -0700 | [diff] [blame] | 42 | This saves the maintainer the effort of going through the Git log |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 43 | when researching a change. |
| 44 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 45 | :Editor: Raymond Hettinger |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 46 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 47 | This article explains the new features in Python 3.8, compared to 3.7. |
Ned Deily | 45ab51c | 2018-02-28 13:58:38 -0500 | [diff] [blame] | 48 | For full details, see the :ref:`changelog <changelog>`. |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 49 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 50 | Prerelease users should be aware that this document is currently in |
| 51 | draft form. It will be updated as Python 3.8 moves towards release, so |
| 52 | it's worth checking back even after reading earlier versions. Some |
| 53 | notable items not yet covered are: |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 54 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 55 | * :pep:`578` - Runtime audit hooks for potentially sensitive operations |
| 56 | * ``python -m asyncio`` runs a natively async REPL |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 57 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 58 | .. testsetup:: |
Nick Coghlan | b9438ce | 2019-06-09 19:07:42 +1000 | [diff] [blame] | 59 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 60 | from datetime import date |
| 61 | from math import cos, radians |
Raymond Hettinger | 66a34d3 | 2019-08-12 15:55:18 -0700 | [diff] [blame] | 62 | from unicodedata import normalize |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 63 | import re |
| 64 | import math |
Nick Coghlan | b9438ce | 2019-06-09 19:07:42 +1000 | [diff] [blame] | 65 | |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 66 | |
| 67 | Summary -- Release highlights |
| 68 | ============================= |
| 69 | |
| 70 | .. This section singles out the most important changes in Python 3.8. |
| 71 | Brevity is key. |
| 72 | |
| 73 | |
| 74 | .. PEP-sized items next. |
| 75 | |
| 76 | |
| 77 | |
| 78 | New Features |
| 79 | ============ |
| 80 | |
Guido van Rossum | 09d434c | 2019-04-24 11:30:17 -0700 | [diff] [blame] | 81 | Assignment expressions |
| 82 | ---------------------- |
| 83 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 84 | There is new syntax ``:=`` that assigns values to variables as part of a larger |
| 85 | expression. It is affectionately known as "walrus operator" due to |
| 86 | its resemblance to `the eyes and tusks of a walrus |
| 87 | <https://en.wikipedia.org/wiki/Walrus#/media/File:Pacific_Walrus_-_Bull_(8247646168).jpg>`_. |
| 88 | |
| 89 | In this example, the assignment expression helps avoid calling |
| 90 | :func:`len` twice:: |
Guido van Rossum | 09d434c | 2019-04-24 11:30:17 -0700 | [diff] [blame] | 91 | |
| 92 | if (n := len(a)) > 10: |
| 93 | print(f"List is too long ({n} elements, expected <= 10)") |
| 94 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 95 | A similar benefit arises during regular expression matching where |
| 96 | match objects are needed twice, once to test whether a match |
| 97 | occurred and another to extract a subgroup:: |
| 98 | |
| 99 | discount = 0.0 |
| 100 | if (mo := re.search(r'(\d+)% discount', advertisement)): |
| 101 | discount = float(mo.group(1)) / 100.0 |
| 102 | |
| 103 | The operator is also useful with while-loops that compute |
| 104 | a value to test loop termination and then need that same |
| 105 | value again in the body of the loop:: |
| 106 | |
| 107 | # Loop over fixed length blocks |
| 108 | while (block := f.read(256)) != '': |
| 109 | process(block) |
| 110 | |
| 111 | Another motivating use case arises in list comprehensions where |
| 112 | a value computed in a filtering condition is also needed in |
| 113 | the expression body:: |
| 114 | |
| 115 | [clean_name.title() for name in names |
| 116 | if (clean_name := normalize('NFC', name)) in allowed_names] |
| 117 | |
| 118 | Try to limit use of the walrus operator to clean cases that reduce |
| 119 | complexity and improve readability. |
| 120 | |
Guido van Rossum | 09d434c | 2019-04-24 11:30:17 -0700 | [diff] [blame] | 121 | See :pep:`572` for a full description. |
| 122 | |
| 123 | (Contributed by Emily Morehouse in :issue:`35224`.) |
| 124 | |
| 125 | .. TODO: Emily will sprint on docs at PyCon US 2019. |
| 126 | |
| 127 | |
Guido van Rossum | 843bf42 | 2019-04-29 05:49:30 -0700 | [diff] [blame] | 128 | Positional-only parameters |
| 129 | -------------------------- |
| 130 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 131 | There is a new function parameter syntax ``/`` to indicate that some |
| 132 | function parameters must be specified positionally and cannot be used as |
| 133 | keyword arguments. This is the same notation shown by ``help()`` for C |
| 134 | functions annotated with Larry Hastings' `Argument Clinic |
| 135 | <https://docs.python.org/3/howto/clinic.html>`_ tool. |
| 136 | |
| 137 | In the following example, parameters *a* and *b* are positional-only, |
| 138 | while *c* or *d* can be positional or keyword, and *e* or *f* are |
| 139 | required to be keywords:: |
| 140 | |
| 141 | def f(a, b, /, c, d, *, e, f): |
| 142 | print(a, b, c, d, e, f) |
| 143 | |
| 144 | The following is a valid call:: |
| 145 | |
| 146 | f(10, 20, 30, d=40, e=50, f=60) |
| 147 | |
| 148 | However, these are invalid calls:: |
| 149 | |
| 150 | f(10, b=20, c=30, d=40, e=50, f=60) # b cannot be a keyword argument |
| 151 | f(10, 20, 30, 40, 50, f=60) # e must be a keyword argument |
| 152 | |
| 153 | One use case for this notation is that it allows pure Python functions |
| 154 | to fully emulate behaviors of existing C coded functions. For example, |
| 155 | the built-in :func:`pow` function does not accept keyword arguments:: |
Guido van Rossum | 843bf42 | 2019-04-29 05:49:30 -0700 | [diff] [blame] | 156 | |
| 157 | def pow(x, y, z=None, /): |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 158 | "Emulate the built in pow() function" |
| 159 | r = x ** y |
| 160 | return r if z is None else r%z |
Guido van Rossum | 843bf42 | 2019-04-29 05:49:30 -0700 | [diff] [blame] | 161 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 162 | Another use case is to preclude keyword arguments when the parameter |
| 163 | name is not helpful. For example, the builtin :func:`len` function has |
| 164 | the signature ``len(obj, /)``. This precludes awkward calls such as:: |
| 165 | |
| 166 | len(obj='hello') # The "obj" keyword argument impairs readability |
| 167 | |
| 168 | A further benefit of marking a parameter as positional-only is that it |
| 169 | allows the parameter name to be changed in the future without risk of |
| 170 | breaking client code. For example, in the :mod:`statistics` module, the |
| 171 | parameter name *dist* may be changed in the future. This was made |
| 172 | possible with the following function specification:: |
| 173 | |
| 174 | def quantiles(dist, /, *, n=4, method='exclusive') |
| 175 | ... |
| 176 | |
| 177 | Since the parameters to the left of ``/`` are not exposed as possible |
| 178 | keywords, the parameters names remain available for use in ``**kwargs``:: |
| 179 | |
| 180 | >>> def f(a, b, /, **kwargs): |
| 181 | ... print(a, b, kwargs) |
| 182 | ... |
| 183 | >>> f(10, 20, a=1, b=2, c=3) # a and b are used in two ways |
| 184 | 10 20 {'a': 1, 'b': 2, 'c': 3} |
| 185 | |
| 186 | This greatly simplifies the implementation of functions and methods |
| 187 | that need to accept arbitrary keyword arguments. For example, here |
| 188 | is an except from code in the :mod:`collections` module:: |
| 189 | |
| 190 | class Counter(dict): |
| 191 | |
| 192 | def __init__(self, iterable=None, /, **kwds): |
| 193 | # Note "iterable" is a possible keyword argument |
Guido van Rossum | 843bf42 | 2019-04-29 05:49:30 -0700 | [diff] [blame] | 194 | |
| 195 | See :pep:`570` for a full description. |
| 196 | |
| 197 | (Contributed by Pablo Galindo in :issue:`36540`.) |
| 198 | |
| 199 | .. TODO: Pablo will sprint on docs at PyCon US 2019. |
| 200 | |
| 201 | |
Nick Coghlan | 16eb3bc | 2018-06-20 21:25:01 +1000 | [diff] [blame] | 202 | Parallel filesystem cache for compiled bytecode files |
| 203 | ----------------------------------------------------- |
| 204 | |
| 205 | The new :envvar:`PYTHONPYCACHEPREFIX` setting (also available as |
| 206 | :option:`-X` ``pycache_prefix``) configures the implicit bytecode |
| 207 | cache to use a separate parallel filesystem tree, rather than |
| 208 | the default ``__pycache__`` subdirectories within each source |
| 209 | directory. |
| 210 | |
| 211 | The location of the cache is reported in :data:`sys.pycache_prefix` |
| 212 | (:const:`None` indicates the default location in ``__pycache__`` |
| 213 | subdirectories). |
| 214 | |
| 215 | (Contributed by Carl Meyer in :issue:`33499`.) |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 216 | |
Victor Stinner | 4046069 | 2019-04-26 17:56:44 +0200 | [diff] [blame] | 217 | Debug build uses the same ABI as release build |
| 218 | ----------------------------------------------- |
| 219 | |
Paul Ganssle | 5c403b2 | 2019-04-27 14:14:35 -0400 | [diff] [blame] | 220 | Python now uses the same ABI whether it built in release or debug mode. On |
| 221 | Unix, when Python is built in debug mode, it is now possible to load C |
| 222 | extensions built in release mode and C extensions built using the stable ABI. |
Victor Stinner | 4046069 | 2019-04-26 17:56:44 +0200 | [diff] [blame] | 223 | |
Paul Ganssle | 5c403b2 | 2019-04-27 14:14:35 -0400 | [diff] [blame] | 224 | Release builds and debug builds are now ABI compatible: defining the |
| 225 | ``Py_DEBUG`` macro no longer implies the ``Py_TRACE_REFS`` macro, which |
| 226 | introduces the only ABI incompatibility. The ``Py_TRACE_REFS`` macro, which |
| 227 | adds the :func:`sys.getobjects` function and the :envvar:`PYTHONDUMPREFS` |
| 228 | environment variable, can be set using the new ``./configure --with-trace-refs`` |
| 229 | build option. |
Victor Stinner | 4046069 | 2019-04-26 17:56:44 +0200 | [diff] [blame] | 230 | (Contributed by Victor Stinner in :issue:`36465`.) |
| 231 | |
E. M. Bray | c994c8f | 2019-05-24 17:33:47 +0200 | [diff] [blame] | 232 | On Unix, C extensions are no longer linked to libpython except on Android |
| 233 | and Cygwin. |
Victor Stinner | 4ebcd7e | 2019-05-11 04:10:03 +0200 | [diff] [blame] | 234 | It is now possible |
Paul Ganssle | 5c403b2 | 2019-04-27 14:14:35 -0400 | [diff] [blame] | 235 | for a statically linked Python to load a C extension built using a shared |
| 236 | library Python. |
Victor Stinner | 4046069 | 2019-04-26 17:56:44 +0200 | [diff] [blame] | 237 | (Contributed by Victor Stinner in :issue:`21536`.) |
| 238 | |
| 239 | On Unix, when Python is built in debug mode, import now also looks for C |
| 240 | extensions compiled in release mode and for C extensions compiled with the |
| 241 | stable ABI. |
| 242 | (Contributed by Victor Stinner in :issue:`36722`.) |
| 243 | |
Victor Stinner | 0a8e572 | 2019-05-23 03:30:23 +0200 | [diff] [blame] | 244 | To embed Python into an application, a new ``--embed`` option must be passed to |
| 245 | ``python3-config --libs --embed`` to get ``-lpython3.8`` (link the application |
| 246 | to libpython). To support both 3.8 and older, try ``python3-config --libs |
| 247 | --embed`` first and fallback to ``python3-config --libs`` (without ``--embed``) |
| 248 | if the previous command fails. |
| 249 | |
| 250 | Add a pkg-config ``python-3.8-embed`` module to embed Python into an |
| 251 | application: ``pkg-config python-3.8-embed --libs`` includes ``-lpython3.8``. |
| 252 | To support both 3.8 and older, try ``pkg-config python-X.Y-embed --libs`` first |
| 253 | and fallback to ``pkg-config python-X.Y --libs`` (without ``--embed``) if the |
| 254 | previous command fails (replace ``X.Y`` with the Python version). |
| 255 | |
| 256 | On the other hand, ``pkg-config python3.8 --libs`` no longer contains |
| 257 | ``-lpython3.8``. C extensions must not be linked to libpython (except on |
E. M. Bray | c994c8f | 2019-05-24 17:33:47 +0200 | [diff] [blame] | 258 | Android and Cygwin, whose cases are handled by the script); |
| 259 | this change is backward incompatible on purpose. |
Victor Stinner | 0a8e572 | 2019-05-23 03:30:23 +0200 | [diff] [blame] | 260 | (Contributed by Victor Stinner in :issue:`36721`.) |
| 261 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 262 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 263 | f-strings support ``=`` for self-documenting expressions and debugging |
| 264 | ---------------------------------------------------------------------- |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 265 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 266 | Added an ``=`` specifier to :term:`f-string`\s. An f-string such as |
| 267 | ``f'{expr=}'`` will expand to the text of the expression, an equal sign, |
| 268 | then the representation of the evaluated expression. For example: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 269 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 270 | >>> user = 'eric_idle' |
| 271 | >>> member_since = date(1975, 7, 31) |
| 272 | >>> f'{user=} {member_since=}' |
| 273 | "user='eric_idle' member_since=datetime.date(1975, 7, 31)" |
| 274 | |
| 275 | The usual :ref:`f-string format specifiers <f-strings>` allow more |
| 276 | control over how the result of the expression is displayed:: |
| 277 | |
| 278 | >>> delta = date.today() - member_since |
| 279 | >>> f'{user=!s} {delta.days=:,d}' |
| 280 | 'user=eric_idle delta.days=16,075' |
| 281 | |
| 282 | The ``=`` specifier will display the whole expression so that |
| 283 | calculations can be shown:: |
| 284 | |
| 285 | >>> print(f'{theta=} {cos(radians(theta))=:.3f}') |
| 286 | theta=30 cos(radians(theta))=0.866 |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 287 | |
| 288 | (Contributed by Eric V. Smith and Larry Hastings in :issue:`36817`.) |
| 289 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 290 | PEP 587: Python Initialization Configuration |
| 291 | -------------------------------------------- |
| 292 | |
| 293 | The :pep:`587` adds a new C API to configure the Python Initialization |
| 294 | providing finer control on the whole configuration and better error reporting. |
| 295 | |
| 296 | New structures: |
| 297 | |
| 298 | * :c:type:`PyConfig` |
| 299 | * :c:type:`PyPreConfig` |
| 300 | * :c:type:`PyStatus` |
| 301 | * :c:type:`PyWideStringList` |
| 302 | |
| 303 | New functions: |
| 304 | |
| 305 | * :c:func:`PyConfig_Clear` |
| 306 | * :c:func:`PyConfig_InitIsolatedConfig` |
| 307 | * :c:func:`PyConfig_InitPythonConfig` |
| 308 | * :c:func:`PyConfig_Read` |
| 309 | * :c:func:`PyConfig_SetArgv` |
| 310 | * :c:func:`PyConfig_SetBytesArgv` |
| 311 | * :c:func:`PyConfig_SetBytesString` |
| 312 | * :c:func:`PyConfig_SetString` |
| 313 | * :c:func:`PyPreConfig_InitIsolatedConfig` |
| 314 | * :c:func:`PyPreConfig_InitPythonConfig` |
| 315 | * :c:func:`PyStatus_Error` |
| 316 | * :c:func:`PyStatus_Exception` |
| 317 | * :c:func:`PyStatus_Exit` |
| 318 | * :c:func:`PyStatus_IsError` |
| 319 | * :c:func:`PyStatus_IsExit` |
| 320 | * :c:func:`PyStatus_NoMemory` |
| 321 | * :c:func:`PyStatus_Ok` |
| 322 | * :c:func:`PyWideStringList_Append` |
| 323 | * :c:func:`PyWideStringList_Insert` |
| 324 | * :c:func:`Py_BytesMain` |
| 325 | * :c:func:`Py_ExitStatusException` |
| 326 | * :c:func:`Py_InitializeFromConfig` |
| 327 | * :c:func:`Py_PreInitialize` |
| 328 | * :c:func:`Py_PreInitializeFromArgs` |
| 329 | * :c:func:`Py_PreInitializeFromBytesArgs` |
| 330 | * :c:func:`Py_RunMain` |
| 331 | |
| 332 | This PEP also adds ``_PyRuntimeState.preconfig`` (:c:type:`PyPreConfig` type) |
| 333 | and ``PyInterpreterState.config`` (:c:type:`PyConfig` type) fields to these |
| 334 | internal structures. ``PyInterpreterState.config`` becomes the new |
| 335 | reference configuration, replacing global configuration variables and |
| 336 | other private variables. |
| 337 | |
| 338 | See :ref:`Python Initialization Configuration <init-config>` for the |
| 339 | documentation. |
| 340 | |
| 341 | See :pep:`587` for a full description. |
| 342 | |
| 343 | (Contributed by Victor Stinner in :issue:`36763`.) |
| 344 | |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 345 | |
Jeroen Demeyer | 9e3e06e | 2019-06-03 01:43:13 +0200 | [diff] [blame] | 346 | Vectorcall: a fast calling protocol for CPython |
| 347 | ----------------------------------------------- |
| 348 | |
| 349 | The "vectorcall" protocol is added to the Python/C API. |
| 350 | It is meant to formalize existing optimizations which were already done |
| 351 | for various classes. |
| 352 | Any extension type implementing a callable can use this protocol. |
| 353 | |
| 354 | This is currently provisional, |
| 355 | the aim is to make it fully public in Python 3.9. |
| 356 | |
| 357 | See :pep:`590` for a full description. |
| 358 | |
| 359 | (Contributed by Jeroen Demeyer and Mark Shannon in :issue:`36974`.) |
| 360 | |
| 361 | |
Antoine Pitrou | c879ff2 | 2019-06-09 14:47:15 +0200 | [diff] [blame] | 362 | Pickle protocol 5 with out-of-band data buffers |
| 363 | ----------------------------------------------- |
| 364 | |
| 365 | When :mod:`pickle` is used to transfer large data between Python processes |
| 366 | in order to take advantage of multi-core or multi-machine processing, |
| 367 | it is important to optimize the transfer by reducing memory copies, and |
| 368 | possibly by applying custom techniques such as data-dependent compression. |
| 369 | |
| 370 | The :mod:`pickle` protocol 5 introduces support for out-of-band buffers |
| 371 | where :pep:`3118`-compatible data can be transmitted separately from the |
| 372 | main pickle stream, at the discretion of the communication layer. |
| 373 | |
| 374 | See :pep:`574` for a full description. |
| 375 | |
| 376 | (Contributed by Antoine Pitrou in :issue:`36785`.) |
| 377 | |
| 378 | |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 379 | Other Language Changes |
| 380 | ====================== |
| 381 | |
Serhiy Storchaka | fe2bbb1 | 2018-03-18 09:56:52 +0200 | [diff] [blame] | 382 | * A :keyword:`continue` statement was illegal in the :keyword:`finally` clause |
| 383 | due to a problem with the implementation. In Python 3.8 this restriction |
| 384 | was lifted. |
| 385 | (Contributed by Serhiy Storchaka in :issue:`32489`.) |
| 386 | |
Raymond Hettinger | 66a34d3 | 2019-08-12 15:55:18 -0700 | [diff] [blame] | 387 | * The :class:`bool`, :class:`int`, and :class:`fractions.Fraction` types |
| 388 | now have an :meth:`~int.as_integer_ratio` method like that found in |
| 389 | :class:`float` and :class:`decimal.Decimal`. This minor API extension |
| 390 | makes it possible to write ``numerator, denominator = |
| 391 | x.as_integer_ratio()`` and have it work across multiple numeric types. |
| 392 | (Contributed by Lisa Roach in :issue:`33073` and Raymond Hettinger in |
| 393 | :issue:`37819`.) |
Lisa Roach | 5ac7043 | 2018-09-13 23:56:23 -0700 | [diff] [blame] | 394 | |
Serhiy Storchaka | bdbad71 | 2019-06-02 00:05:48 +0300 | [diff] [blame] | 395 | * Constructors of :class:`int`, :class:`float` and :class:`complex` will now |
| 396 | use the :meth:`~object.__index__` special method, if available and the |
| 397 | corresponding method :meth:`~object.__int__`, :meth:`~object.__float__` |
| 398 | or :meth:`~object.__complex__` is not available. |
| 399 | (Contributed by Serhiy Storchaka in :issue:`20092`.) |
| 400 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 401 | * Added support of ``\N{name}`` escapes in :mod:`regular expressions <re>`:: |
| 402 | |
| 403 | >>> notice = 'Copyright © 2019' |
| 404 | >>> copyright_year_pattern = re.compile(r'\N{copyright sign}\s*(\d{4})') |
| 405 | >>> int(copyright_year_pattern.search(notice).group(1)) |
| 406 | 2019 |
| 407 | |
Serhiy Storchaka | a445feb | 2018-02-10 00:08:17 +0200 | [diff] [blame] | 408 | (Contributed by Jonathan Eunice and Serhiy Storchaka in :issue:`30688`.) |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 409 | |
Rémi Lapeyre | 6531bf6 | 2018-11-06 01:38:54 +0100 | [diff] [blame] | 410 | * Dict and dictviews are now iterable in reversed insertion order using |
| 411 | :func:`reversed`. (Contributed by Rémi Lapeyre in :issue:`33462`.) |
| 412 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 413 | * The syntax allowed for keyword names in function calls was further |
| 414 | restricted. In particular, ``f((keyword)=arg)`` is no longer allowed. It was |
| 415 | never intended to permit more than a bare name on the left-hand side of a |
| 416 | keyword argument assignment term. See :issue:`34641`. |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 417 | |
Raymond Hettinger | 66a34d3 | 2019-08-12 15:55:18 -0700 | [diff] [blame] | 418 | * Generalized iterable unpacking in :keyword:`yield` and |
| 419 | :keyword:`return` statements no longer requires enclosing parentheses. |
| 420 | This brings the *yield* and *return* syntax into better agreement with |
| 421 | normal assignment syntax:: |
| 422 | |
| 423 | >>> def parse(family): |
| 424 | lastname, *members = family.split() |
| 425 | return lastname.upper(), *members |
| 426 | |
| 427 | >>> parse('simpsons homer marge bart lisa sally') |
| 428 | ('SIMPSONS', 'homer', 'marge', 'bart', 'lisa', 'sally') |
| 429 | |
| 430 | |
jChapman | 8fabae3 | 2018-09-22 21:13:10 -0400 | [diff] [blame] | 431 | (Contributed by David Cuthbert and Jordan Chapman in :issue:`32117`.) |
| 432 | |
Raymond Hettinger | 66a34d3 | 2019-08-12 15:55:18 -0700 | [diff] [blame] | 433 | * When a comma is missed in code such as ``[(10, 20) (30, 40)]``, the |
| 434 | compiler displays a :exc:`SyntaxWarning` with a helpful suggestion. |
| 435 | This improves on just having a :exc:`TypeError` indicating that the |
| 436 | first tuple was not callable. (Contributed by Serhiy Storchaka in |
| 437 | :issue:`15248`.) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 438 | |
Paul Ganssle | d9503c3 | 2019-02-08 11:02:00 -0500 | [diff] [blame] | 439 | * Arithmetic operations between subclasses of :class:`datetime.date` or |
| 440 | :class:`datetime.datetime` and :class:`datetime.timedelta` objects now return |
| 441 | an instance of the subclass, rather than the base class. This also affects |
| 442 | the return type of operations whose implementation (directly or indirectly) |
| 443 | uses :class:`datetime.timedelta` arithmetic, such as |
| 444 | :meth:`datetime.datetime.astimezone`. |
| 445 | (Contributed by Paul Ganssle in :issue:`32417`.) |
| 446 | |
Gregory P. Smith | 06babb2 | 2019-02-23 10:43:49 -0800 | [diff] [blame] | 447 | * When the Python interpreter is interrupted by Ctrl-C (SIGINT) and the |
| 448 | resulting :exc:`KeyboardInterrupt` exception is not caught, the Python process |
| 449 | now exits via a SIGINT signal or with the correct exit code such that the |
| 450 | calling process can detect that it died due to a Ctrl-C. Shells on POSIX |
| 451 | and Windows use this to properly terminate scripts in interactive sessions. |
| 452 | (Contributed by Google via Gregory P. Smith in :issue:`1054041`.) |
| 453 | |
Raymond Hettinger | 66a34d3 | 2019-08-12 15:55:18 -0700 | [diff] [blame] | 454 | * Some advanced styles of programming require updating the |
| 455 | :class:`types.CodeType` object for an existing function. Since code |
| 456 | objects are immutable, a new code object needs to be created, one |
| 457 | that is modeled on the existing code object. With 19 parameters, |
| 458 | this was somewhat tedious. Now, the new ``replace()`` method makes |
| 459 | it possible to create a clone with a few altered parameters. |
| 460 | |
| 461 | Here's an example that alters the :func:`statistics.mean` function to |
| 462 | prevent the *data* parameter from being used as a keyword argument:: |
| 463 | |
| 464 | >>> from statistics import mean |
| 465 | >>> mean(data=[10, 20, 90]) |
| 466 | 40 |
| 467 | >>> mean.__code__ = mean.__code__.replace(co_posonlyargcount=1) |
| 468 | >>> mean(data=[10, 20, 90]) |
| 469 | Traceback (most recent call last): |
| 470 | ... |
| 471 | TypeError: mean() got some positional-only arguments passed as keyword arguments: 'data' |
| 472 | |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 473 | (Contributed by Victor Stinner in :issue:`37032`.) |
| 474 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 475 | * For integers, the three-argument form of the :func:`pow` function now |
| 476 | permits the exponent to be negative in the case where the base is |
| 477 | relatively prime to the modulus. It then computes a modular inverse to |
| 478 | the base when the exponent is ``-1``, and a suitable power of that |
| 479 | inverse for other negative exponents. For example, to compute the |
| 480 | `modular multiplicative inverse |
| 481 | <https://en.wikipedia.org/wiki/Modular_multiplicative_inverse>`_ of 38 |
| 482 | modulo 137, write:: |
| 483 | |
| 484 | >>> pow(38, -1, 137) |
| 485 | 119 |
| 486 | >>> 119 * 38 % 137 |
| 487 | 1 |
| 488 | |
| 489 | Modular inverses arise in the solution of `linear Diophantine |
| 490 | equations <https://en.wikipedia.org/wiki/Diophantine_equation>`_. |
| 491 | For example, to find integer solutions for ``4258𝑥 + 147𝑦 = 369``, |
| 492 | first rewrite as ``4258𝑥 ≡ 369 (mod 147)`` then solve: |
| 493 | |
| 494 | >>> x = 369 * pow(4258, -1, 147) % 147 |
| 495 | >>> y = (4258 * x - 369) // -147 |
| 496 | >>> 4258 * x + 147 * y |
| 497 | 369 |
| 498 | |
Mark Dickinson | c529967 | 2019-06-02 10:24:06 +0100 | [diff] [blame] | 499 | (Contributed by Mark Dickinson in :issue:`36027`.) |
| 500 | |
Raymond Hettinger | 66a34d3 | 2019-08-12 15:55:18 -0700 | [diff] [blame] | 501 | * Dict comprehensions have been synced-up with dict literals so that the |
| 502 | key is computed first and the value second:: |
| 503 | |
| 504 | >>> # Dict comprehension |
| 505 | >>> cast = {input('role? '): input('actor? ') for i in range(2)} |
| 506 | role? King Arthur |
| 507 | actor? Chapman |
| 508 | role? Black Knight |
| 509 | actor? Cleese |
| 510 | |
| 511 | >>> # Dict literal |
| 512 | >>> cast = {input('role? '): input('actor? ')} |
| 513 | role? Sir Robin |
| 514 | actor? Eric Idle |
| 515 | |
| 516 | The guaranteed execution order is helpful with assignment expressions |
| 517 | because variables assigned in the key expression will be available in |
| 518 | the value expression:: |
| 519 | |
| 520 | >>> names = ['Martin von Löwis', 'Łukasz Langa', 'Walter Dörwald'] |
| 521 | >>> {(n := normalize('NFC', name)).casefold() : n for name in names} |
| 522 | {'martin von löwis': 'Martin von Löwis', |
| 523 | 'łukasz langa': 'Łukasz Langa', |
| 524 | 'walter dörwald': 'Walter Dörwald'} |
Pablo Galindo | b51b713 | 2019-06-25 02:41:58 +0100 | [diff] [blame] | 525 | |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 526 | |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 527 | New Modules |
| 528 | =========== |
| 529 | |
Barry Warsaw | 4867eaa | 2019-06-05 19:40:19 -0700 | [diff] [blame] | 530 | * The new :mod:`importlib.metadata` module provides (provisional) support for |
Raymond Hettinger | 66a34d3 | 2019-08-12 15:55:18 -0700 | [diff] [blame] | 531 | reading metadata from third-party packages. For example, it can extract an |
| 532 | installed package's version number, list of entry points, and more:: |
| 533 | |
| 534 | >>> # Note following example requires that the popular "requests" |
| 535 | >>> # package has been installed. |
| 536 | >>> |
| 537 | >>> from importlib.metadata import version, requires, files |
| 538 | >>> version('requests') |
| 539 | '2.22.0' |
| 540 | >>> list(requires('requests')) |
| 541 | ['chardet (<3.1.0,>=3.0.2)'] |
| 542 | >>> list(files('requests'))[:5] |
| 543 | [PackagePath('requests-2.22.0.dist-info/INSTALLER'), |
| 544 | PackagePath('requests-2.22.0.dist-info/LICENSE'), |
| 545 | PackagePath('requests-2.22.0.dist-info/METADATA'), |
| 546 | PackagePath('requests-2.22.0.dist-info/RECORD'), |
| 547 | PackagePath('requests-2.22.0.dist-info/WHEEL')] |
| 548 | |
| 549 | (Contributed in :issue:`34632` by Barry Warsaw and Jason R. Coombs.) |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 550 | |
| 551 | |
| 552 | Improved Modules |
| 553 | ================ |
| 554 | |
Raymond Hettinger | 0bb4bdf | 2019-01-31 00:59:50 -0800 | [diff] [blame] | 555 | |
Guido van Rossum | 9b33ce4 | 2019-06-11 13:42:35 -0700 | [diff] [blame] | 556 | ast |
| 557 | --- |
| 558 | |
| 559 | AST nodes now have ``end_lineno`` and ``end_col_offset`` attributes, |
| 560 | which give the precise location of the end of the node. (This only |
| 561 | applies to nodes that have ``lineno`` and ``col_offset`` attributes.) |
| 562 | |
| 563 | The :func:`ast.parse` function has some new flags: |
| 564 | |
| 565 | * ``type_comments=True`` causes it to return the text of :pep:`484` and |
| 566 | :pep:`526` type comments associated with certain AST nodes; |
| 567 | |
| 568 | * ``mode='func_type'`` can be used to parse :pep:`484` "signature type |
| 569 | comments" (returned for function definition AST nodes); |
| 570 | |
Guido van Rossum | 10b55c1 | 2019-06-11 17:23:12 -0700 | [diff] [blame] | 571 | * ``feature_version=(3, N)`` allows specifying an earlier Python 3 |
| 572 | version. (For example, ``feature_version=(3, 4)`` will treat |
| 573 | ``async`` and ``await`` as non-reserved words.) |
Guido van Rossum | 9b33ce4 | 2019-06-11 13:42:35 -0700 | [diff] [blame] | 574 | |
| 575 | New function :func:`ast.get_source_segment` returns the source code |
| 576 | for a specific AST node. |
| 577 | |
| 578 | |
Victor Stinner | 6ea29c5 | 2018-09-25 08:27:08 -0700 | [diff] [blame] | 579 | asyncio |
| 580 | ------- |
| 581 | |
| 582 | On Windows, the default event loop is now :class:`~asyncio.ProactorEventLoop`. |
Victor Stinner | 01ae897 | 2019-06-03 16:28:01 +0200 | [diff] [blame] | 583 | (Contributed by Victor Stinner in :issue:`34687`.) |
| 584 | |
| 585 | :class:`~asyncio.ProactorEventLoop` now also supports UDP. |
| 586 | (Contributed by Adam Meily and Andrew Svetlov in :issue:`29883`.) |
| 587 | |
| 588 | :class:`~asyncio.ProactorEventLoop` can now be interrupted by |
| 589 | :exc:`KeyboardInterrupt` ("CTRL+C"). |
| 590 | (Contributed by Vladimir Matveev in :issue:`23057`.) |
| 591 | |
Victor Stinner | 6ea29c5 | 2018-09-25 08:27:08 -0700 | [diff] [blame] | 592 | |
Matthias Bussonnier | 2ddbd21 | 2019-05-22 12:07:45 -0700 | [diff] [blame] | 593 | builtins |
| 594 | -------- |
| 595 | |
| 596 | The :func:`compile` built-in has been improved to accept the |
| 597 | ``ast.PyCF_ALLOW_TOP_LEVEL_AWAIT`` flag. With this new flag passed, |
| 598 | :func:`compile` will allow top-level ``await``, ``async for`` and ``async with`` |
| 599 | constructs that are usually considered invalid syntax. Asynchronous code object |
| 600 | marked with the ``CO_COROUTINE`` flag may then be returned. |
| 601 | |
| 602 | (Contributed by Matthias Bussonnier in :issue:`34616`) |
Terry Jan Reedy | fdcb5ae | 2018-09-25 12:45:27 -0400 | [diff] [blame] | 603 | |
Raymond Hettinger | 482b6b5 | 2019-05-01 17:48:13 -0700 | [diff] [blame] | 604 | collections |
| 605 | ----------- |
| 606 | |
| 607 | The :meth:`_asdict()` method for :func:`collections.namedtuple` now returns |
Daniel Porteous | 0522291 | 2019-05-02 04:20:59 -0400 | [diff] [blame] | 608 | a :class:`dict` instead of a :class:`collections.OrderedDict`. This works because |
| 609 | regular dicts have guaranteed ordering since Python 3.7. If the extra |
Raymond Hettinger | 482b6b5 | 2019-05-01 17:48:13 -0700 | [diff] [blame] | 610 | features of :class:`OrderedDict` are required, the suggested remediation is |
| 611 | to cast the result to the desired type: ``OrderedDict(nt._asdict())``. |
| 612 | (Contributed by Raymond Hettinger in :issue:`35864`.) |
| 613 | |
| 614 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 615 | ctypes |
| 616 | ------ |
| 617 | |
| 618 | On Windows, :class:`~ctypes.CDLL` and subclasses now accept a *winmode* parameter |
| 619 | to specify flags for the underlying ``LoadLibraryEx`` call. The default flags are |
| 620 | set to only load DLL dependencies from trusted locations, including the path |
| 621 | where the DLL is stored (if a full or partial path is used to load the initial |
| 622 | DLL) and paths added by :func:`~os.add_dll_directory`. |
| 623 | |
| 624 | |
Raymond Hettinger | b821868 | 2019-05-26 11:27:35 -0700 | [diff] [blame] | 625 | functools |
| 626 | --------- |
| 627 | |
| 628 | :func:`functools.lru_cache` can now be used as a straight decorator rather |
| 629 | than as a function returning a decorator. So both of these are now supported:: |
| 630 | |
| 631 | @lru_cache |
| 632 | def f(x): |
| 633 | ... |
| 634 | |
| 635 | @lru_cache(maxsize=256) |
| 636 | def f(x): |
| 637 | ... |
| 638 | |
| 639 | (Contributed by Raymond Hettinger in :issue:`36772`.) |
| 640 | |
| 641 | |
Paul Ganssle | 88c0937 | 2019-04-29 09:22:03 -0400 | [diff] [blame] | 642 | datetime |
| 643 | -------- |
| 644 | |
| 645 | Added new alternate constructors :meth:`datetime.date.fromisocalendar` and |
| 646 | :meth:`datetime.datetime.fromisocalendar`, which construct :class:`date` and |
| 647 | :class:`datetime` objects respectively from ISO year, week number and weekday; |
| 648 | these are the inverse of each class's ``isocalendar`` method. |
| 649 | (Contributed by Paul Ganssle in :issue:`36004`.) |
| 650 | |
| 651 | |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame] | 652 | gettext |
| 653 | ------- |
| 654 | |
| 655 | Added :func:`~gettext.pgettext` and its variants. |
| 656 | (Contributed by Franz Glasner, Éric Araujo, and Cheryl Sabella in :issue:`2504`.) |
| 657 | |
Terry Jan Reedy | a72ca90 | 2019-07-31 01:03:53 -0400 | [diff] [blame] | 658 | |
| 659 | idlelib and IDLE |
| 660 | ---------------- |
| 661 | |
| 662 | Output over N lines (50 by default) is squeezed down to a button. |
| 663 | N can be changed in the PyShell section of the General page of the |
| 664 | Settings dialog. Fewer, but possibly extra long, lines can be squeezed by |
| 665 | right clicking on the output. Squeezed output can be expanded in place |
| 666 | by double-clicking the button or into the clipboard or a separate window |
| 667 | by right-clicking the button. (Contributed by Tal Einat in :issue:`1529353`.) |
| 668 | |
| 669 | Add "Run Customized" to the Run menu to run a module with customized |
| 670 | settings. Any command line arguments entered are added to sys.argv. |
| 671 | They also re-appear in the box for the next customized run. One can also |
| 672 | suppress the normal Shell main module restart. (Contributed by Cheryl |
| 673 | Sabella, Terry Jan Reedy, and others in :issue:`5680` and :issue:`37627`.) |
| 674 | |
| 675 | Add optional line numbers for IDLE editor windows. Windows |
| 676 | open without line numbers unless set otherwise in the General |
| 677 | tab of the configuration dialog. Line numbers for an existing |
| 678 | window are shown and hidden in the Options menu. |
| 679 | (Contributed by Tal Einat and Saimadhav Heblikar in :issue:`17535`.) |
| 680 | |
| 681 | The changes above have been backported to 3.7 maintenance releases. |
| 682 | |
| 683 | |
Raymond Hettinger | d1e768a | 2019-03-25 13:01:13 -0700 | [diff] [blame] | 684 | inspect |
| 685 | ------- |
| 686 | |
| 687 | The :func:`inspect.getdoc` function can now find docstrings for ``__slots__`` |
| 688 | if that attribute is a :class:`dict` where the values are docstrings. |
| 689 | This provides documentation options similar to what we already have |
| 690 | for :func:`property`, :func:`classmethod`, and :func:`staticmethod`:: |
| 691 | |
| 692 | class AudioClip: |
| 693 | __slots__ = {'bit_rate': 'expressed in kilohertz to one decimal place', |
| 694 | 'duration': 'in seconds, rounded up to an integer'} |
| 695 | def __init__(self, bit_rate, duration): |
| 696 | self.bit_rate = round(bit_rate / 1000.0, 1) |
| 697 | self.duration = ceil(duration) |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 698 | |
Terry Jan Reedy | a72ca90 | 2019-07-31 01:03:53 -0400 | [diff] [blame] | 699 | |
Victor Stinner | bc2aa81 | 2019-05-23 03:45:09 +0200 | [diff] [blame] | 700 | io |
| 701 | -- |
| 702 | |
| 703 | In development mode (:option:`-X` ``env``) and in debug build, the |
| 704 | :class:`io.IOBase` finalizer now logs the exception if the ``close()`` method |
| 705 | fails. The exception is ignored silently by default in release build. |
| 706 | (Contributed by Victor Stinner in :issue:`18748`.) |
| 707 | |
| 708 | |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 709 | gc |
| 710 | -- |
| 711 | |
| 712 | :func:`~gc.get_objects` can now receive an optional *generation* parameter |
| 713 | indicating a generation to get objects from. Contributed in |
| 714 | :issue:`36016` by Pablo Galindo. |
| 715 | |
| 716 | |
guoci | 0e7497c | 2018-11-07 04:50:23 -0500 | [diff] [blame] | 717 | gzip |
| 718 | ---- |
| 719 | |
| 720 | Added the *mtime* parameter to :func:`gzip.compress` for reproducible output. |
| 721 | (Contributed by Guo Ci Teo in :issue:`34898`.) |
| 722 | |
Zackery Spytz | cf599f6 | 2019-05-13 01:50:52 -0600 | [diff] [blame] | 723 | A :exc:`~gzip.BadGzipFile` exception is now raised instead of :exc:`OSError` |
| 724 | for certain types of invalid or corrupt gzip files. |
| 725 | (Contributed by Filip Gruszczyński, Michele Orrù, and Zackery Spytz in |
| 726 | :issue:`6584`.) |
| 727 | |
guoci | 0e7497c | 2018-11-07 04:50:23 -0500 | [diff] [blame] | 728 | |
Terry Jan Reedy | fdcb5ae | 2018-09-25 12:45:27 -0400 | [diff] [blame] | 729 | idlelib and IDLE |
| 730 | ---------------- |
| 731 | |
Tal Einat | 7123ea0 | 2019-07-23 15:22:11 +0300 | [diff] [blame] | 732 | Add optional line numbers for IDLE editor windows. Windows |
| 733 | open without line numbers unless set otherwise in the General |
| 734 | tab of the configuration dialog. |
| 735 | (Contributed by Tal Einat and Saimadhav Heblikar in :issue:`17535`.) |
| 736 | |
Terry Jan Reedy | fdcb5ae | 2018-09-25 12:45:27 -0400 | [diff] [blame] | 737 | Output over N lines (50 by default) is squeezed down to a button. |
| 738 | N can be changed in the PyShell section of the General page of the |
| 739 | Settings dialog. Fewer, but possibly extra long, lines can be squeezed by |
| 740 | right clicking on the output. Squeezed output can be expanded in place |
| 741 | by double-clicking the button or into the clipboard or a separate window |
| 742 | by right-clicking the button. (Contributed by Tal Einat in :issue:`1529353`.) |
| 743 | |
| 744 | The changes above have been backported to 3.7 maintenance releases. |
| 745 | |
| 746 | |
HongWeipeng | f194479 | 2018-11-07 18:09:32 +0800 | [diff] [blame] | 747 | json.tool |
| 748 | --------- |
| 749 | |
| 750 | Add option ``--json-lines`` to parse every input line as separate JSON object. |
| 751 | (Contributed by Weipeng Hong in :issue:`31553`.) |
| 752 | |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 753 | |
| 754 | math |
| 755 | ---- |
| 756 | |
Raymond Hettinger | 3ff5962 | 2019-02-16 11:00:42 -0800 | [diff] [blame] | 757 | Added new function :func:`math.dist` for computing Euclidean distance |
| 758 | between two points. (Contributed by Raymond Hettinger in :issue:`33089`.) |
| 759 | |
| 760 | Expanded the :func:`math.hypot` function to handle multiple dimensions. |
| 761 | Formerly, it only supported the 2-D case. |
| 762 | (Contributed by Raymond Hettinger in :issue:`33089`.) |
| 763 | |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 764 | Added new function, :func:`math.prod`, as analogous function to :func:`sum` |
| 765 | that returns the product of a 'start' value (default: 1) times an iterable of |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 766 | numbers:: |
| 767 | |
| 768 | >>> prior = 0.8 |
| 769 | >>> likelihoods = [0.625, 0.84, 0.30] |
Ashwin Vishnu | 1a8de82 | 2019-09-09 14:42:27 +0200 | [diff] [blame] | 770 | >>> math.prod(likelihoods, start=prior) |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 771 | 0.126 |
| 772 | |
| 773 | (Contributed by Pablo Galindo in :issue:`35606`) |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 774 | |
Mark Dickinson | 73934b9 | 2019-05-18 12:29:50 +0100 | [diff] [blame] | 775 | Added new function :func:`math.isqrt` for computing integer square roots. |
| 776 | (Contributed by Mark Dickinson in :issue:`36887`.) |
| 777 | |
Mark Dickinson | a0adffb | 2019-06-01 12:21:53 +0100 | [diff] [blame] | 778 | The function :func:`math.factorial` no longer accepts arguments that are not |
| 779 | int-like. (Contributed by Pablo Galindo in :issue:`33083`.) |
| 780 | |
Zackery Spytz | 02db696 | 2019-05-27 10:48:17 -0600 | [diff] [blame] | 781 | |
| 782 | mmap |
| 783 | ---- |
| 784 | |
| 785 | The :class:`mmap.mmap` class now has an :meth:`~mmap.mmap.madvise` method to |
| 786 | access the ``madvise()`` system call. |
| 787 | (Contributed by Zackery Spytz in :issue:`32941`.) |
| 788 | |
| 789 | |
Victor Stinner | 17a5588 | 2019-05-28 16:02:50 +0200 | [diff] [blame] | 790 | multiprocessing |
| 791 | --------------- |
| 792 | |
| 793 | Added new :mod:`multiprocessing.shared_memory` module. |
| 794 | (Contributed Davin Potts in :issue:`35813`.) |
| 795 | |
| 796 | On macOS, the *spawn* start method is now used by default. |
| 797 | (Contributed by Victor Stinner in :issue:`33725`.) |
| 798 | |
| 799 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 800 | os |
| 801 | -- |
| 802 | |
| 803 | Added new function :func:`~os.add_dll_directory` on Windows for providing |
| 804 | additional search paths for native dependencies when importing extension |
| 805 | modules or loading DLLs using :mod:`ctypes`. |
| 806 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 807 | A new :func:`os.memfd_create` function was added to wrap the |
| 808 | ``memfd_create()`` syscall. |
| 809 | (Contributed by Zackery Spytz and Christian Heimes in :issue:`26836`.) |
| 810 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 811 | On Windows, much of the manual logic for handling reparse points (including |
| 812 | symlinks and directory junctions) has been delegated to the operating system. |
| 813 | Specifically, :func:`os.stat` will now traverse anything supported by the |
| 814 | operating system, while :func:`os.lstat` will only open reparse points that |
| 815 | identify as "name surrogates" while others are opened as for :func:`os.stat`. |
| 816 | In all cases, :attr:`stat_result.st_mode` will only have ``S_IFLNK`` set for |
| 817 | symbolic links and not other kinds of reparse points. To identify other kinds |
| 818 | of reparse point, check the new :attr:`stat_result.st_reparse_tag` attribute. |
| 819 | |
| 820 | On Windows, :func:`os.readlink` is now able to read directory junctions. Note |
| 821 | that :func:`~os.path.islink` will return ``False`` for directory junctions, |
| 822 | and so code that checks ``islink`` first will continue to treat junctions as |
| 823 | directories, while code that handles errors from :func:`os.readlink` may now |
| 824 | treat junctions as links. |
| 825 | |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 826 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 827 | os.path |
| 828 | ------- |
| 829 | |
| 830 | :mod:`os.path` functions that return a boolean result like |
| 831 | :func:`~os.path.exists`, :func:`~os.path.lexists`, :func:`~os.path.isdir`, |
| 832 | :func:`~os.path.isfile`, :func:`~os.path.islink`, and :func:`~os.path.ismount` |
| 833 | now return ``False`` instead of raising :exc:`ValueError` or its subclasses |
| 834 | :exc:`UnicodeEncodeError` and :exc:`UnicodeDecodeError` for paths that contain |
| 835 | characters or bytes unrepresentable at the OS level. |
| 836 | (Contributed by Serhiy Storchaka in :issue:`33721`.) |
| 837 | |
Steve Dower | 8ef864d | 2019-03-12 15:15:26 -0700 | [diff] [blame] | 838 | :func:`~os.path.expanduser` on Windows now prefers the :envvar:`USERPROFILE` |
| 839 | environment variable and does not use :envvar:`HOME`, which is not normally set |
| 840 | for regular user accounts. |
| 841 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 842 | :func:`~os.path.isdir` on Windows no longer returns true for a link to a |
| 843 | non-existent directory. |
| 844 | |
Steve Dower | 75e0649 | 2019-08-21 13:43:06 -0700 | [diff] [blame] | 845 | :func:`~os.path.realpath` on Windows now resolves reparse points, including |
| 846 | symlinks and directory junctions. |
| 847 | |
Serhiy Storchaka | b232df9 | 2018-10-30 13:22:42 +0200 | [diff] [blame] | 848 | |
| 849 | ncurses |
| 850 | ------- |
| 851 | |
| 852 | Added a new variable holding structured version information for the |
| 853 | underlying ncurses library: :data:`~curses.ncurses_version`. |
| 854 | (Contributed by Serhiy Storchaka in :issue:`31680`.) |
| 855 | |
| 856 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 857 | pathlib |
| 858 | ------- |
| 859 | |
| 860 | :mod:`pathlib.Path` methods that return a boolean result like |
| 861 | :meth:`~pathlib.Path.exists()`, :meth:`~pathlib.Path.is_dir()`, |
| 862 | :meth:`~pathlib.Path.is_file()`, :meth:`~pathlib.Path.is_mount()`, |
| 863 | :meth:`~pathlib.Path.is_symlink()`, :meth:`~pathlib.Path.is_block_device()`, |
| 864 | :meth:`~pathlib.Path.is_char_device()`, :meth:`~pathlib.Path.is_fifo()`, |
| 865 | :meth:`~pathlib.Path.is_socket()` now return ``False`` instead of raising |
| 866 | :exc:`ValueError` or its subclass :exc:`UnicodeEncodeError` for paths that |
| 867 | contain characters unrepresentable at the OS level. |
| 868 | (Contributed by Serhiy Storchaka in :issue:`33721`.) |
| 869 | |
Joannah Nanjekye | 6b5b013 | 2019-05-04 11:27:10 -0400 | [diff] [blame] | 870 | Added :meth:`pathlib.Path.link_to()` which creates a hard link pointing |
| 871 | to a path. |
| 872 | (Contributed by Joannah Nanjekye in :issue:`26978`) |
| 873 | |
jab | 9e00d9e | 2018-12-28 13:03:40 -0500 | [diff] [blame] | 874 | |
Pierre Glaser | ec6c1bd | 2019-07-01 15:51:57 +0200 | [diff] [blame] | 875 | pickle |
| 876 | ------ |
| 877 | |
| 878 | Reduction methods can now include a 6th item in the tuple they return. This |
| 879 | item should specify a custom state-setting method that's called instead of the |
| 880 | regular ``__setstate__`` method. |
| 881 | (Contributed by Pierre Glaser and Olivier Grisel in :issue:`35900`) |
| 882 | |
| 883 | :mod:`pickle` extensions subclassing the C-optimized :class:`~pickle.Pickler` |
| 884 | can now override the pickling logic of functions and classes by defining the |
| 885 | special :meth:`~pickle.Pickler.reducer_override` method. |
| 886 | (Contributed by Pierre Glaser and Olivier Grisel in :issue:`35900`) |
| 887 | |
| 888 | |
Jon Janzen | c981ad1 | 2019-05-15 22:14:38 +0200 | [diff] [blame] | 889 | plistlib |
| 890 | -------- |
| 891 | |
| 892 | Added new :class:`plistlib.UID` and enabled support for reading and writing |
| 893 | NSKeyedArchiver-encoded binary plists. |
| 894 | (Contributed by Jon Janzen in :issue:`26707`.) |
| 895 | |
| 896 | |
Joannah Nanjekye | 2e33ecd | 2019-05-28 13:29:04 -0300 | [diff] [blame] | 897 | py_compile |
| 898 | ---------- |
| 899 | |
| 900 | :func:`py_compile.compile` now supports silent mode. |
| 901 | (Contributed by Joannah Nanjekye in :issue:`22640`.) |
| 902 | |
| 903 | |
Giampaolo Rodola | eb7e29f | 2019-04-09 00:34:02 +0200 | [diff] [blame] | 904 | socket |
| 905 | ------ |
| 906 | |
| 907 | Added :meth:`~socket.create_server()` and :meth:`~socket.has_dualstack_ipv6()` |
| 908 | convenience functions to automate the necessary tasks usually involved when |
| 909 | creating a server socket, including accepting both IPv4 and IPv6 connections |
| 910 | on the same socket. (Contributed by Giampaolo Rodola in :issue:`17561`.) |
| 911 | |
Zackery Spytz | 8f96c9f | 2019-05-29 15:02:37 -0600 | [diff] [blame] | 912 | The :func:`socket.if_nameindex()`, :func:`socket.if_nametoindex()`, and |
| 913 | :func:`socket.if_indextoname()` functions have been implemented on Windows. |
| 914 | (Contributed by Zackery Spytz in :issue:`37007`.) |
| 915 | |
Bo Bayles | ca80495 | 2019-05-29 03:06:12 -0500 | [diff] [blame] | 916 | shlex |
| 917 | ---------- |
| 918 | |
| 919 | The new :func:`shlex.join` function acts as the inverse of :func:`shlex.split`. |
| 920 | (Contributed by Bo Bayles in :issue:`32102`.) |
Giampaolo Rodola | eb7e29f | 2019-04-09 00:34:02 +0200 | [diff] [blame] | 921 | |
jab | 9e00d9e | 2018-12-28 13:03:40 -0500 | [diff] [blame] | 922 | shutil |
| 923 | ------ |
| 924 | |
| 925 | :func:`shutil.copytree` now accepts a new ``dirs_exist_ok`` keyword argument. |
| 926 | (Contributed by Josh Bronson in :issue:`20849`.) |
| 927 | |
CAM Gerlach | 89a8944 | 2019-04-06 23:47:49 -0500 | [diff] [blame] | 928 | :func:`shutil.make_archive` now defaults to the modern pax (POSIX.1-2001) |
| 929 | format for new archives to improve portability and standards conformance, |
| 930 | inherited from the corresponding change to the :mod:`tarfile` module. |
| 931 | (Contributed by C.A.M. Gerlach in :issue:`30661`.) |
| 932 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 933 | :func:`shutil.rmtree` on Windows now removes directory junctions without |
| 934 | recursively removing their contents first. |
| 935 | |
jab | 9e00d9e | 2018-12-28 13:03:40 -0500 | [diff] [blame] | 936 | |
Christian Heimes | 9fb051f | 2018-09-23 08:32:31 +0200 | [diff] [blame] | 937 | ssl |
| 938 | --- |
| 939 | |
| 940 | Added :attr:`SSLContext.post_handshake_auth` to enable and |
| 941 | :meth:`ssl.SSLSocket.verify_client_post_handshake` to initiate TLS 1.3 |
| 942 | post-handshake authentication. |
| 943 | (Contributed by Christian Heimes in :issue:`34670`.) |
| 944 | |
Raymond Hettinger | 47d9987 | 2019-02-21 15:06:29 -0800 | [diff] [blame] | 945 | |
| 946 | statistics |
| 947 | ---------- |
| 948 | |
| 949 | Added :func:`statistics.fmean` as a faster, floating point variant of |
| 950 | :func:`statistics.mean()`. (Contributed by Raymond Hettinger and |
| 951 | Steven D'Aprano in :issue:`35904`.) |
| 952 | |
Raymond Hettinger | 6463ba3 | 2019-04-07 09:20:03 -0700 | [diff] [blame] | 953 | Added :func:`statistics.geometric_mean()` |
| 954 | (Contributed by Raymond Hettinger in :issue:`27181`.) |
| 955 | |
Raymond Hettinger | fc06a19 | 2019-03-12 00:43:27 -0700 | [diff] [blame] | 956 | Added :func:`statistics.multimode` that returns a list of the most |
| 957 | common values. (Contributed by Raymond Hettinger in :issue:`35892`.) |
| 958 | |
Raymond Hettinger | 9013ccf | 2019-04-23 00:06:35 -0700 | [diff] [blame] | 959 | Added :func:`statistics.quantiles` that divides data or a distribution |
| 960 | in to equiprobable intervals (e.g. quartiles, deciles, or percentiles). |
| 961 | (Contributed by Raymond Hettinger in :issue:`36546`.) |
| 962 | |
Raymond Hettinger | 11c7953 | 2019-02-23 14:44:07 -0800 | [diff] [blame] | 963 | Added :class:`statistics.NormalDist`, a tool for creating |
| 964 | and manipulating normal distributions of a random variable. |
| 965 | (Contributed by Raymond Hettinger in :issue:`36018`.) |
| 966 | |
| 967 | :: |
| 968 | |
| 969 | >>> temperature_feb = NormalDist.from_samples([4, 12, -3, 2, 7, 14]) |
Raymond Hettinger | 671d782 | 2019-05-01 17:49:12 -0700 | [diff] [blame] | 970 | >>> temperature_feb.mean |
| 971 | 6.0 |
| 972 | >>> temperature_feb.stdev |
| 973 | 6.356099432828281 |
Raymond Hettinger | 11c7953 | 2019-02-23 14:44:07 -0800 | [diff] [blame] | 974 | |
| 975 | >>> temperature_feb.cdf(3) # Chance of being under 3 degrees |
| 976 | 0.3184678262814532 |
| 977 | >>> # Relative chance of being 7 degrees versus 10 degrees |
| 978 | >>> temperature_feb.pdf(7) / temperature_feb.pdf(10) |
| 979 | 1.2039930378537762 |
| 980 | |
Raymond Hettinger | 671d782 | 2019-05-01 17:49:12 -0700 | [diff] [blame] | 981 | >>> el_niño = NormalDist(4, 2.5) |
| 982 | >>> temperature_feb += el_niño # Add in a climate effect |
Raymond Hettinger | 11c7953 | 2019-02-23 14:44:07 -0800 | [diff] [blame] | 983 | >>> temperature_feb |
| 984 | NormalDist(mu=10.0, sigma=6.830080526611674) |
| 985 | |
| 986 | >>> temperature_feb * (9/5) + 32 # Convert to Fahrenheit |
| 987 | NormalDist(mu=50.0, sigma=12.294144947901014) |
| 988 | >>> temperature_feb.samples(3) # Generate random samples |
| 989 | [7.672102882379219, 12.000027119750287, 4.647488369766392] |
| 990 | |
Raymond Hettinger | 47d9987 | 2019-02-21 15:06:29 -0800 | [diff] [blame] | 991 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 992 | sys |
| 993 | --- |
| 994 | |
| 995 | Add new :func:`sys.unraisablehook` function which can be overridden to control |
| 996 | how "unraisable exceptions" are handled. It is called when an exception has |
| 997 | occurred but there is no way for Python to handle it. For example, when a |
| 998 | destructor raises an exception or during garbage collection |
| 999 | (:func:`gc.collect`). |
Victor Stinner | 01ae897 | 2019-06-03 16:28:01 +0200 | [diff] [blame] | 1000 | (Contributed by Victor Stinner in :issue:`36829`.) |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1001 | |
| 1002 | |
CAM Gerlach | e680c3d | 2019-03-21 09:44:51 -0500 | [diff] [blame] | 1003 | tarfile |
| 1004 | ------- |
| 1005 | |
| 1006 | The :mod:`tarfile` module now defaults to the modern pax (POSIX.1-2001) |
| 1007 | format for new archives, instead of the previous GNU-specific one. |
| 1008 | This improves cross-platform portability with a consistent encoding (UTF-8) |
| 1009 | in a standardized and extensible format, and offers several other benefits. |
| 1010 | (Contributed by C.A.M. Gerlach in :issue:`36268`.) |
| 1011 | |
| 1012 | |
Victor Stinner | cd590a7 | 2019-05-28 00:39:52 +0200 | [diff] [blame] | 1013 | threading |
| 1014 | --------- |
| 1015 | |
Jake Tesler | 84846b0 | 2019-07-30 14:41:46 -0700 | [diff] [blame] | 1016 | * Add a new :func:`threading.excepthook` function which handles uncaught |
| 1017 | :meth:`threading.Thread.run` exception. It can be overridden to control how |
| 1018 | uncaught :meth:`threading.Thread.run` exceptions are handled. |
| 1019 | (Contributed by Victor Stinner in :issue:`1230540`.) |
| 1020 | |
| 1021 | * Add a new |
| 1022 | :func:`threading.get_native_id` function and a :data:`~threading.Thread.native_id` |
| 1023 | attribute to the :class:`threading.Thread` class. These return the native |
| 1024 | integral Thread ID of the current thread assigned by the kernel. |
| 1025 | This feature is only available on certain platforms, see |
| 1026 | :func:`get_native_id <threading.get_native_id>` for more information. |
| 1027 | (Contributed by Jake Tesler in :issue:`36084`.) |
Victor Stinner | cd590a7 | 2019-05-28 00:39:52 +0200 | [diff] [blame] | 1028 | |
| 1029 | |
Tal Einat | dfba1f6 | 2018-10-24 10:20:05 +0300 | [diff] [blame] | 1030 | tokenize |
| 1031 | -------- |
| 1032 | |
| 1033 | The :mod:`tokenize` module now implicitly emits a ``NEWLINE`` token when |
| 1034 | provided with input that does not have a trailing new line. This behavior |
| 1035 | now matches what the C tokenizer does internally. |
| 1036 | (Contributed by Ammar Askar in :issue:`33899`.) |
| 1037 | |
Juliette Monsel | af5658a | 2018-10-08 18:29:24 +0200 | [diff] [blame] | 1038 | tkinter |
| 1039 | ------- |
| 1040 | |
| 1041 | Added methods :meth:`~tkinter.Spinbox.selection_from`, |
| 1042 | :meth:`~tkinter.Spinbox.selection_present`, |
| 1043 | :meth:`~tkinter.Spinbox.selection_range` and |
| 1044 | :meth:`~tkinter.Spinbox.selection_to` |
| 1045 | in the :class:`tkinter.Spinbox` class. |
| 1046 | (Contributed by Juliette Monsel in :issue:`34829`.) |
| 1047 | |
Juliette Monsel | bf03471 | 2018-10-12 18:44:10 +0200 | [diff] [blame] | 1048 | Added method :meth:`~tkinter.Canvas.moveto` |
| 1049 | in the :class:`tkinter.Canvas` class. |
| 1050 | (Contributed by Juliette Monsel in :issue:`23831`.) |
| 1051 | |
Zackery Spytz | 50866e9 | 2019-04-05 04:17:13 -0600 | [diff] [blame] | 1052 | The :class:`tkinter.PhotoImage` class now has |
| 1053 | :meth:`~tkinter.PhotoImage.transparency_get` and |
| 1054 | :meth:`~tkinter.PhotoImage.transparency_set` methods. (Contributed by |
| 1055 | Zackery Spytz in :issue:`25451`.) |
| 1056 | |
Joannah Nanjekye | 572168a | 2019-01-10 19:56:38 +0300 | [diff] [blame] | 1057 | time |
| 1058 | ---- |
| 1059 | |
| 1060 | Added new clock :data:`~time.CLOCK_UPTIME_RAW` for macOS 10.12. |
| 1061 | (Contributed by Joannah Nanjekye in :issue:`35702`.) |
| 1062 | |
Guido van Rossum | 9b33ce4 | 2019-06-11 13:42:35 -0700 | [diff] [blame] | 1063 | |
| 1064 | typing |
| 1065 | ------ |
| 1066 | |
| 1067 | The :mod:`typing` module incorporates several new features: |
| 1068 | |
| 1069 | * Protocol definitions. See :pep:`544`, :class:`typing.Protocol` and |
| 1070 | :func:`typing.runtime_checkable`. Simple ABCs like |
| 1071 | :class:`typing.SupportsInt` are now ``Protocol`` subclasses. |
| 1072 | |
| 1073 | * A dictionary type with per-key types. See :pep:`589` and |
| 1074 | :class:`typing.TypedDict`. |
| 1075 | |
| 1076 | * Literal types. See :pep:`586` and :class:`typing.Literal`. |
| 1077 | |
| 1078 | * "Final" variables, functions, methods and classes. See :pep:`591`, |
| 1079 | :class:`typing.Final` and :func:`typing.final`. |
| 1080 | |
| 1081 | * New protocol class :class:`typing.SupportsIndex`. |
| 1082 | |
| 1083 | * New functions :func:`typing.get_origin` and :func:`typing.get_args`. |
| 1084 | |
| 1085 | |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 1086 | unicodedata |
| 1087 | ----------- |
| 1088 | |
Benjamin Peterson | 3aca40d | 2019-05-08 20:59:35 -0700 | [diff] [blame] | 1089 | * The :mod:`unicodedata` module has been upgraded to use the `Unicode 12.1.0 |
| 1090 | <http://blog.unicode.org/2019/05/unicode-12-1-en.html>`_ release. |
Raymond Hettinger | 482b6b5 | 2019-05-01 17:48:13 -0700 | [diff] [blame] | 1091 | |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 1092 | * New function :func:`~unicodedata.is_normalized` can be used to verify a string |
Greg Price | 2f09413 | 2019-09-03 19:45:44 -0700 | [diff] [blame] | 1093 | is in a specific normal form, often much faster than by actually normalizing |
| 1094 | the string. (Contributed by Max Belanger, David Euresti, and Greg Price in |
| 1095 | :issue:`32285` and :issue:`37966`). |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 1096 | |
Raymond Hettinger | 482b6b5 | 2019-05-01 17:48:13 -0700 | [diff] [blame] | 1097 | |
Lisa Roach | 0f221d0 | 2018-11-08 18:34:33 -0800 | [diff] [blame] | 1098 | unittest |
| 1099 | -------- |
| 1100 | |
Victor Stinner | 01ae897 | 2019-06-03 16:28:01 +0200 | [diff] [blame] | 1101 | * Added :class:`AsyncMock` to support an asynchronous version of :class:`Mock`. |
Lisa Roach | 77b3b77 | 2019-05-20 09:19:53 -0700 | [diff] [blame] | 1102 | Appropriate new assert functions for testing have been added as well. |
| 1103 | (Contributed by Lisa Roach in :issue:`26467`). |
| 1104 | |
Lisa Roach | 0f221d0 | 2018-11-08 18:34:33 -0800 | [diff] [blame] | 1105 | * Added :func:`~unittest.addModuleCleanup()` and |
| 1106 | :meth:`~unittest.TestCase.addClassCleanup()` to unittest to support |
| 1107 | cleanups for :func:`~unittest.setUpModule()` and |
| 1108 | :meth:`~unittest.TestCase.setUpClass()`. |
| 1109 | (Contributed by Lisa Roach in :issue:`24412`.) |
| 1110 | |
Petter Strandmark | 001d63c | 2019-06-04 21:34:49 +0200 | [diff] [blame] | 1111 | * Several mock assert functions now also print a list of actual calls upon |
| 1112 | failure. (Contributed by Petter Strandmark in :issue:`35047`.) |
| 1113 | |
Xtreak | 6a9fd66 | 2019-09-11 12:02:14 +0100 | [diff] [blame] | 1114 | * :mod:`unittest` module gained support for coroutines to be used as test cases |
| 1115 | with :class:`unittest.IsolatedAsyncioTestCase`. |
| 1116 | (Contributed by Andrew Svetlov in :issue:`32972`.) |
| 1117 | |
| 1118 | Example:: |
| 1119 | |
| 1120 | import unittest |
| 1121 | |
| 1122 | |
| 1123 | class TestRequest(unittest.IsolatedAsyncioTestCase): |
| 1124 | |
| 1125 | async def asyncSetUp(self): |
| 1126 | self.connection = await AsyncConnection() |
| 1127 | |
| 1128 | async def test_get(self): |
| 1129 | response = await self.connection.get("https://example.com") |
| 1130 | self.assertEqual(response.status_code, 200) |
| 1131 | |
| 1132 | async def asyncTearDown(self): |
| 1133 | await self.connection.close() |
| 1134 | |
| 1135 | |
| 1136 | if __name__ == "__main__": |
| 1137 | unittest.main() |
| 1138 | |
| 1139 | |
Brett Cannon | d64ee1a | 2018-09-21 15:27:26 -0700 | [diff] [blame] | 1140 | venv |
| 1141 | ---- |
| 1142 | |
| 1143 | * :mod:`venv` now includes an ``Activate.ps1`` script on all platforms for |
| 1144 | activating virtual environments under PowerShell Core 6.1. |
| 1145 | (Contributed by Brett Cannon in :issue:`32718`.) |
| 1146 | |
Mark Dickinson | 7abb6c0 | 2019-04-26 15:56:15 +0900 | [diff] [blame] | 1147 | weakref |
| 1148 | ------- |
| 1149 | |
| 1150 | * The proxy objects returned by :func:`weakref.proxy` now support the matrix |
| 1151 | multiplication operators ``@`` and ``@=`` in addition to the other |
| 1152 | numeric operators. (Contributed by Mark Dickinson in :issue:`36669`.) |
| 1153 | |
Christian Heimes | 17b1d5d | 2018-09-23 09:50:25 +0200 | [diff] [blame] | 1154 | xml |
| 1155 | --- |
| 1156 | |
| 1157 | * As mitigation against DTD and external entity retrieval, the |
Andrés Delfino | ca68261 | 2018-11-07 14:29:14 -0300 | [diff] [blame] | 1158 | :mod:`xml.dom.minidom` and :mod:`xml.sax` modules no longer process |
Christian Heimes | 17b1d5d | 2018-09-23 09:50:25 +0200 | [diff] [blame] | 1159 | external entities by default. |
| 1160 | (Contributed by Christian Heimes in :issue:`17239`.) |
| 1161 | |
Stefan Behnel | 4754168 | 2019-05-03 20:58:16 +0200 | [diff] [blame] | 1162 | * The ``.find*()`` methods in the :mod:`xml.etree.ElementTree` module |
| 1163 | support wildcard searches like ``{*}tag`` which ignores the namespace |
| 1164 | and ``{namespace}*`` which returns all tags in the given namespace. |
| 1165 | (Contributed by Stefan Behnel in :issue:`28238`.) |
| 1166 | |
Stefan Behnel | e1d5dd6 | 2019-05-01 22:34:13 +0200 | [diff] [blame] | 1167 | * The :mod:`xml.etree.ElementTree` module provides a new function |
| 1168 | :func:`–xml.etree.ElementTree.canonicalize()` that implements C14N 2.0. |
| 1169 | (Contributed by Stefan Behnel in :issue:`13611`.) |
| 1170 | |
Stefan Behnel | e9a465f | 2019-05-10 10:25:13 +0200 | [diff] [blame] | 1171 | * The target object of :class:`xml.etree.ElementTree.XMLParser` can |
| 1172 | receive namespace declaration events through the new callback methods |
| 1173 | ``start_ns()`` and ``end_ns()``. Additionally, the |
| 1174 | :class:`xml.etree.ElementTree.TreeBuilder` target can be configured |
| 1175 | to process events about comments and processing instructions to include |
| 1176 | them in the generated tree. |
| 1177 | (Contributed by Stefan Behnel in :issue:`36676` and :issue:`36673`.) |
Christian Heimes | 17b1d5d | 2018-09-23 09:50:25 +0200 | [diff] [blame] | 1178 | |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1179 | Optimizations |
| 1180 | ============= |
| 1181 | |
Victor Stinner | 9daecf3 | 2019-01-16 00:02:35 +0100 | [diff] [blame] | 1182 | * The :mod:`subprocess` module can now use the :func:`os.posix_spawn` function |
| 1183 | in some cases for better performance. Currently, it is only used on macOS |
| 1184 | and Linux (using glibc 2.24 or newer) if all these conditions are met: |
| 1185 | |
| 1186 | * *close_fds* is false; |
Victor Stinner | f6243ac | 2019-01-23 19:00:39 +0100 | [diff] [blame] | 1187 | * *preexec_fn*, *pass_fds*, *cwd* and *start_new_session* parameters |
| 1188 | are not set; |
Victor Stinner | 8c34956 | 2019-01-16 23:38:06 +0100 | [diff] [blame] | 1189 | * the *executable* path contains a directory. |
Victor Stinner | 9daecf3 | 2019-01-16 00:02:35 +0100 | [diff] [blame] | 1190 | |
Victor Stinner | 01ae897 | 2019-06-03 16:28:01 +0200 | [diff] [blame] | 1191 | (Contributed by Joannah Nanjekye and Victor Stinner in :issue:`35537`.) |
| 1192 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 1193 | * :func:`shutil.copyfile`, :func:`shutil.copy`, :func:`shutil.copy2`, |
| 1194 | :func:`shutil.copytree` and :func:`shutil.move` use platform-specific |
Giampaolo Rodola | 413d955 | 2019-05-30 14:05:41 +0800 | [diff] [blame] | 1195 | "fast-copy" syscalls on Linux and macOS in order to copy the file |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 1196 | more efficiently. |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 1197 | "fast-copy" means that the copying operation occurs within the kernel, |
| 1198 | avoiding the use of userspace buffers in Python as in |
| 1199 | "``outfd.write(infd.read())``". |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 1200 | On Windows :func:`shutil.copyfile` uses a bigger default buffer size (1 MiB |
| 1201 | instead of 16 KiB) and a :func:`memoryview`-based variant of |
| 1202 | :func:`shutil.copyfileobj` is used. |
| 1203 | The speedup for copying a 512 MiB file within the same partition is about |
| 1204 | +26% on Linux, +50% on macOS and +40% on Windows. Also, much less CPU cycles |
| 1205 | are consumed. |
| 1206 | See :ref:`shutil-platform-dependent-efficient-copy-operations` section. |
Mariatta | 16501b7 | 2018-12-06 21:59:42 -0800 | [diff] [blame] | 1207 | (Contributed by Giampaolo Rodola' in :issue:`33671`.) |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 1208 | |
Giampaolo Rodola | 19c46a4 | 2018-11-12 06:18:15 -0800 | [diff] [blame] | 1209 | * :func:`shutil.copytree` uses :func:`os.scandir` function and all copy |
| 1210 | functions depending from it use cached :func:`os.stat` values. The speedup |
| 1211 | for copying a directory with 8000 files is around +9% on Linux, +20% on |
| 1212 | Windows and +30% on a Windows SMB share. Also the number of :func:`os.stat` |
| 1213 | syscalls is reduced by 38% making :func:`shutil.copytree` especially faster |
| 1214 | on network filesystems. (Contributed by Giampaolo Rodola' in :issue:`33695`.) |
| 1215 | |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 1216 | * The default protocol in the :mod:`pickle` module is now Protocol 4, |
| 1217 | first introduced in Python 3.4. It offers better performance and smaller |
| 1218 | size compared to Protocol 3 available since Python 3.0. |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1219 | |
INADA Naoki | d5c875b | 2018-07-11 17:42:49 +0900 | [diff] [blame] | 1220 | * Removed one ``Py_ssize_t`` member from ``PyGC_Head``. All GC tracked |
| 1221 | objects (e.g. tuple, list, dict) size is reduced 4 or 8 bytes. |
| 1222 | (Contributed by Inada Naoki in :issue:`33597`) |
| 1223 | |
Tal Einat | 5475253 | 2018-09-10 16:11:04 +0300 | [diff] [blame] | 1224 | * :class:`uuid.UUID` now uses ``__slots__`` to reduce its memory footprint. |
Tal Einat | 5475253 | 2018-09-10 16:11:04 +0300 | [diff] [blame] | 1225 | |
Raymond Hettinger | 63fa1cf | 2019-02-16 12:02:22 -0800 | [diff] [blame] | 1226 | * Improved performance of :func:`operator.itemgetter` by 33%. Optimized |
| 1227 | argument handling and added a fast path for the common case of a single |
| 1228 | non-negative integer index into a tuple (which is the typical use case in |
| 1229 | the standard library). (Contributed by Raymond Hettinger in |
| 1230 | :issue:`35664`.) |
| 1231 | |
| 1232 | * Sped-up field lookups in :func:`collections.namedtuple`. They are now more |
| 1233 | than two times faster, making them the fastest form of instance variable |
| 1234 | lookup in Python. (Contributed by Raymond Hettinger, Pablo Galindo, and |
Joe Jevnik | f36f892 | 2019-02-21 16:00:40 -0500 | [diff] [blame] | 1235 | Joe Jevnik, Serhiy Storchaka in :issue:`32492`.) |
Raymond Hettinger | 63fa1cf | 2019-02-16 12:02:22 -0800 | [diff] [blame] | 1236 | |
Pablo Galindo | c61e229 | 2018-10-28 22:03:18 +0000 | [diff] [blame] | 1237 | * The :class:`list` constructor does not overallocate the internal item buffer |
| 1238 | if the input iterable has a known length (the input implements ``__len__``). |
Raymond Hettinger | e182318 | 2019-02-16 12:47:48 -0800 | [diff] [blame] | 1239 | This makes the created list 12% smaller on average. (Contributed by |
| 1240 | Raymond Hettinger and Pablo Galindo in :issue:`33234`.) |
Pablo Galindo | c61e229 | 2018-10-28 22:03:18 +0000 | [diff] [blame] | 1241 | |
Stefan Behnel | d8b9e1f | 2019-02-20 18:29:24 +0100 | [diff] [blame] | 1242 | * Doubled the speed of class variable writes. When a non-dunder attribute |
| 1243 | was updated, there was an unnecessary call to update slots. |
| 1244 | (Contributed by Stefan Behnel, Pablo Galindo Salgado, Raymond Hettinger, |
| 1245 | Neil Schemenauer, and Serhiy Storchaka in :issue:`36012`.) |
| 1246 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 1247 | * Reduced an overhead of converting arguments passed to many builtin functions |
| 1248 | and methods. This sped up calling some simple builtin functions and |
| 1249 | methods up to 20--50%. (Contributed by Serhiy Storchaka in :issue:`23867`, |
| 1250 | :issue:`35582` and :issue:`36127`.) |
| 1251 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1252 | * ``LOAD_GLOBAL`` instruction now uses new "per opcode cache" mechanism. |
| 1253 | It is about 40% faster now. (Contributed by Yury Selivanov and Inada Naoki in |
| 1254 | :issue:`26219`.) |
| 1255 | |
Serhiy Storchaka | ceeef10 | 2018-06-15 11:09:43 +0300 | [diff] [blame] | 1256 | |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1257 | Build and C API Changes |
| 1258 | ======================= |
| 1259 | |
Victor Stinner | 7efc526 | 2019-06-15 03:24:41 +0200 | [diff] [blame] | 1260 | * Default :data:`sys.abiflags` became an empty string: the ``m`` flag for |
| 1261 | pymalloc became useless (builds with and without pymalloc are ABI compatible) |
| 1262 | and so has been removed. (Contributed by Victor Stinner in :issue:`36707`.) |
| 1263 | |
| 1264 | Example of changes: |
| 1265 | |
| 1266 | * Only ``python3.8`` program is installed, ``python3.8m`` program is gone. |
| 1267 | * Only ``python3.8-config`` script is installed, ``python3.8m-config`` script |
| 1268 | is gone. |
| 1269 | * The ``m`` flag has been removed from the suffix of dynamic library |
| 1270 | filenames: extension modules in the standard library as well as those |
| 1271 | produced and installed by third-party packages, like those downloaded from |
| 1272 | PyPI. On Linux, for example, the Python 3.7 suffix |
| 1273 | ``.cpython-37m-x86_64-linux-gnu.so`` became |
| 1274 | ``.cpython-38-x86_64-linux-gnu.so`` in Python 3.8. |
| 1275 | |
Victor Stinner | bd5798f | 2019-06-14 19:43:43 +0200 | [diff] [blame] | 1276 | * The header files have been reorganized to better separate the different kinds |
| 1277 | of APIs: |
| 1278 | |
| 1279 | * ``Include/*.h`` should be the portable public stable C API. |
| 1280 | * ``Include/cpython/*.h`` should be the unstable C API specific to CPython; |
Victor Stinner | af41c56 | 2019-06-20 01:44:58 +0200 | [diff] [blame] | 1281 | public API, with some private API prefixed by ``_Py`` or ``_PY``. |
Victor Stinner | bd5798f | 2019-06-14 19:43:43 +0200 | [diff] [blame] | 1282 | * ``Include/internal/*.h`` is the private internal C API very specific to |
| 1283 | CPython. This API comes with no backward compatibility warranty and should |
| 1284 | not be used outside CPython. It is only exposed for very specific needs |
| 1285 | like debuggers and profiles which has to access to CPython internals |
| 1286 | without calling functions. This API is now installed by ``make install``. |
| 1287 | |
| 1288 | (Contributed by Victor Stinner in :issue:`35134` and :issue:`35081`, |
| 1289 | work initiated by Eric Snow in Python 3.7) |
| 1290 | |
| 1291 | * Some macros have been converted to static inline functions: parameter types |
| 1292 | and return type are well defined, they don't have issues specific to macros, |
| 1293 | variables have a local scopes. Examples: |
| 1294 | |
| 1295 | * :c:func:`Py_INCREF`, :c:func:`Py_DECREF` |
| 1296 | * :c:func:`Py_XINCREF`, :c:func:`Py_XDECREF` |
| 1297 | * :c:func:`PyObject_INIT`, :c:func:`PyObject_INIT_VAR` |
| 1298 | * Private functions: :c:func:`_PyObject_GC_TRACK`, |
| 1299 | :c:func:`_PyObject_GC_UNTRACK`, :c:func:`_Py_Dealloc` |
| 1300 | |
| 1301 | (Contributed by Victor Stinner in :issue:`35059`.) |
| 1302 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 1303 | * The :c:func:`PyByteArray_Init` and :c:func:`PyByteArray_Fini` functions have |
| 1304 | been removed. They did nothing since Python 2.7.4 and Python 3.2.0, were |
| 1305 | excluded from the limited API (stable ABI), and were not documented. |
Victor Stinner | c68e3fb | 2019-06-20 22:41:25 +0200 | [diff] [blame] | 1306 | (Contributed by Victor Stinner in :issue:`35713`.) |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 1307 | |
Serhiy Storchaka | ceeef10 | 2018-06-15 11:09:43 +0300 | [diff] [blame] | 1308 | * The result of :c:func:`PyExceptionClass_Name` is now of type |
| 1309 | ``const char *`` rather of ``char *``. |
| 1310 | (Contributed by Serhiy Storchaka in :issue:`33818`.) |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1311 | |
Antoine Pitrou | 961d54c | 2018-07-16 19:03:03 +0200 | [diff] [blame] | 1312 | * The duality of ``Modules/Setup.dist`` and ``Modules/Setup`` has been |
| 1313 | removed. Previously, when updating the CPython source tree, one had |
| 1314 | to manually copy ``Modules/Setup.dist`` (inside the source tree) to |
| 1315 | ``Modules/Setup`` (inside the build tree) in order to reflect any changes |
| 1316 | upstream. This was of a small benefit to packagers at the expense of |
| 1317 | a frequent annoyance to developers following CPython development, as |
| 1318 | forgetting to copy the file could produce build failures. |
| 1319 | |
| 1320 | Now the build system always reads from ``Modules/Setup`` inside the source |
| 1321 | tree. People who want to customize that file are encouraged to maintain |
| 1322 | their changes in a git fork of CPython or as patch files, as they would do |
| 1323 | for any other change to the source tree. |
| 1324 | |
| 1325 | (Contributed by Antoine Pitrou in :issue:`32430`.) |
| 1326 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 1327 | * Functions that convert Python number to C integer like |
| 1328 | :c:func:`PyLong_AsLong` and argument parsing functions like |
| 1329 | :c:func:`PyArg_ParseTuple` with integer converting format units like ``'i'`` |
| 1330 | will now use the :meth:`~object.__index__` special method instead of |
| 1331 | :meth:`~object.__int__`, if available. The deprecation warning will be |
| 1332 | emitted for objects with the ``__int__()`` method but without the |
| 1333 | ``__index__()`` method (like :class:`~decimal.Decimal` and |
| 1334 | :class:`~fractions.Fraction`). :c:func:`PyNumber_Check` will now return |
| 1335 | ``1`` for objects implementing ``__index__()``. |
Serhiy Storchaka | bdbad71 | 2019-06-02 00:05:48 +0300 | [diff] [blame] | 1336 | :c:func:`PyNumber_Long`, :c:func:`PyNumber_Float` and |
| 1337 | :c:func:`PyFloat_AsDouble` also now use the ``__index__()`` method if |
| 1338 | available. |
| 1339 | (Contributed by Serhiy Storchaka in :issue:`36048` and :issue:`20092`.) |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 1340 | |
Eddie Elizondo | 364f0b0 | 2019-03-27 07:52:18 -0400 | [diff] [blame] | 1341 | * Heap-allocated type objects will now increase their reference count |
| 1342 | in :c:func:`PyObject_Init` (and its parallel macro ``PyObject_INIT``) |
| 1343 | instead of in :c:func:`PyType_GenericAlloc`. Types that modify instance |
| 1344 | allocation or deallocation may need to be adjusted. |
| 1345 | (Contributed by Eddie Elizondo in :issue:`35810`.) |
| 1346 | |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 1347 | * The new function :c:func:`PyCode_NewWithPosOnlyArgs` allows to create |
| 1348 | code objects like :c:func:`PyCode_New`, but with an extra *posonlyargcount* |
| 1349 | parameter for indicating the number of positional-only arguments. |
| 1350 | (Contributed by Pablo Galindo in :issue:`37221`.) |
| 1351 | |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1352 | |
| 1353 | Deprecated |
| 1354 | ========== |
| 1355 | |
Victor Stinner | 1da4462 | 2019-07-05 10:44:12 +0200 | [diff] [blame] | 1356 | * The distutils ``bdist_wininst`` command is now deprecated, use |
| 1357 | ``bdist_wheel`` (wheel packages) instead. |
| 1358 | (Contributed by Victor Stinner in :issue:`37481`.) |
| 1359 | |
Serhiy Storchaka | 02ec92f | 2018-07-24 12:03:34 +0300 | [diff] [blame] | 1360 | * Deprecated methods ``getchildren()`` and ``getiterator()`` in |
| 1361 | the :mod:`~xml.etree.ElementTree` module emit now a |
| 1362 | :exc:`DeprecationWarning` instead of :exc:`PendingDeprecationWarning`. |
| 1363 | They will be removed in Python 3.9. |
| 1364 | (Contributed by Serhiy Storchaka in :issue:`29209`.) |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1365 | |
Elvis Pranskevichus | 22d2508 | 2018-07-30 11:42:43 +0100 | [diff] [blame] | 1366 | * Passing an object that is not an instance of |
| 1367 | :class:`concurrent.futures.ThreadPoolExecutor` to |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1368 | :meth:`asyncio.loop.set_default_executor()` is |
Elvis Pranskevichus | 22d2508 | 2018-07-30 11:42:43 +0100 | [diff] [blame] | 1369 | deprecated and will be prohibited in Python 3.9. |
| 1370 | (Contributed by Elvis Pranskevichus in :issue:`34075`.) |
| 1371 | |
Berker Peksag | ef8861c | 2018-08-21 17:58:49 +0300 | [diff] [blame] | 1372 | * The :meth:`__getitem__` methods of :class:`xml.dom.pulldom.DOMEventStream`, |
| 1373 | :class:`wsgiref.util.FileWrapper` and :class:`fileinput.FileInput` have been |
| 1374 | deprecated. |
| 1375 | |
| 1376 | Implementations of these methods have been ignoring their *index* parameter, |
| 1377 | and returning the next item instead. |
| 1378 | |
| 1379 | (Contributed by Berker Peksag in :issue:`9372`.) |
| 1380 | |
Raymond Hettinger | f7b57df | 2019-03-18 09:53:56 -0700 | [diff] [blame] | 1381 | * The :class:`typing.NamedTuple` class has deprecated the ``_field_types`` |
| 1382 | attribute in favor of the ``__annotations__`` attribute which has the same |
| 1383 | information. (Contributed by Raymond Hettinger in :issue:`36320`.) |
| 1384 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1385 | * :mod:`ast` classes ``Num``, ``Str``, ``Bytes``, ``NameConstant`` and |
| 1386 | ``Ellipsis`` are considered deprecated and will be removed in future Python |
| 1387 | versions. :class:`~ast.Constant` should be used instead. |
| 1388 | (Contributed by Serhiy Storchaka in :issue:`32892`.) |
| 1389 | |
Serhiy Storchaka | c3ea41e | 2019-08-26 10:13:19 +0300 | [diff] [blame] | 1390 | * :class:`ast.NodeVisitor` methods ``visit_Num()``, ``visit_Str()``, |
| 1391 | ``visit_Bytes()``, ``visit_NameConstant()`` and ``visit_Ellipsis()`` are |
| 1392 | deprecated now and will not be called in future Python versions. |
| 1393 | Add the :meth:`~ast.NodeVisitor.visit_Constant` method to handle all |
| 1394 | constant nodes. |
| 1395 | (Contributed by Serhiy Storchaka in :issue:`36917`.) |
| 1396 | |
Serhiy Storchaka | fec35c9 | 2018-10-27 08:00:41 +0300 | [diff] [blame] | 1397 | * The following functions and methods are deprecated in the :mod:`gettext` |
| 1398 | module: :func:`~gettext.lgettext`, :func:`~gettext.ldgettext`, |
| 1399 | :func:`~gettext.lngettext` and :func:`~gettext.ldngettext`. |
| 1400 | They return encoded bytes, and it's possible that you will get unexpected |
| 1401 | Unicode-related exceptions if there are encoding problems with the |
| 1402 | translated strings. It's much better to use alternatives which return |
| 1403 | Unicode strings in Python 3. These functions have been broken for a long time. |
| 1404 | |
| 1405 | Function :func:`~gettext.bind_textdomain_codeset`, methods |
| 1406 | :meth:`~gettext.NullTranslations.output_charset` and |
| 1407 | :meth:`~gettext.NullTranslations.set_output_charset`, and the *codeset* |
| 1408 | parameter of functions :func:`~gettext.translation` and |
| 1409 | :func:`~gettext.install` are also deprecated, since they are only used for |
| 1410 | for the ``l*gettext()`` functions. |
| 1411 | |
| 1412 | (Contributed by Serhiy Storchaka in :issue:`33710`.) |
| 1413 | |
Dong-hee Na | 89669ff | 2019-01-17 21:14:45 +0900 | [diff] [blame] | 1414 | * The :meth:`~threading.Thread.isAlive()` method of :class:`threading.Thread` has been deprecated. |
| 1415 | (Contributed by Dong-hee Na in :issue:`35283`.) |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1416 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 1417 | * Many builtin and extension functions that take integer arguments will |
| 1418 | now emit a deprecation warning for :class:`~decimal.Decimal`\ s, |
| 1419 | :class:`~fractions.Fraction`\ s and any other objects that can be converted |
| 1420 | to integers only with a loss (e.g. that have the :meth:`~object.__int__` |
| 1421 | method but do not have the :meth:`~object.__index__` method). In future |
| 1422 | version they will be errors. |
| 1423 | (Contributed by Serhiy Storchaka in :issue:`36048`.) |
| 1424 | |
Serhiy Storchaka | 42a139e | 2019-04-01 09:16:35 +0300 | [diff] [blame] | 1425 | * Deprecated passing the following arguments as keyword arguments: |
| 1426 | |
| 1427 | - *func* in :func:`functools.partialmethod`, :func:`weakref.finalize`, |
| 1428 | :meth:`profile.Profile.runcall`, :meth:`cProfile.Profile.runcall`, |
| 1429 | :meth:`bdb.Bdb.runcall`, :meth:`trace.Trace.runfunc` and |
| 1430 | :func:`curses.wrapper`. |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 1431 | - *function* in :meth:`unittest.TestCase.addCleanup`. |
Serhiy Storchaka | 42a139e | 2019-04-01 09:16:35 +0300 | [diff] [blame] | 1432 | - *fn* in the :meth:`~concurrent.futures.Executor.submit` method of |
| 1433 | :class:`concurrent.futures.ThreadPoolExecutor` and |
| 1434 | :class:`concurrent.futures.ProcessPoolExecutor`. |
| 1435 | - *callback* in :meth:`contextlib.ExitStack.callback`, |
| 1436 | :meth:`contextlib.AsyncExitStack.callback` and |
| 1437 | :meth:`contextlib.AsyncExitStack.push_async_callback`. |
| 1438 | - *c* and *typeid* in the :meth:`~multiprocessing.managers.Server.create` |
| 1439 | method of :class:`multiprocessing.managers.Server` and |
| 1440 | :class:`multiprocessing.managers.SharedMemoryServer`. |
| 1441 | - *obj* in :func:`weakref.finalize`. |
| 1442 | |
| 1443 | In future releases of Python they will be :ref:`positional-only |
| 1444 | <positional-only_parameter>`. |
| 1445 | (Contributed by Serhiy Storchaka in :issue:`36492`.) |
| 1446 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 1447 | |
Victor Stinner | 73104fa | 2018-11-29 09:58:20 +0100 | [diff] [blame] | 1448 | API and Feature Removals |
| 1449 | ======================== |
| 1450 | |
| 1451 | The following features and APIs have been removed from Python 3.8: |
| 1452 | |
Victor Stinner | d7538dd | 2018-12-14 13:37:26 +0100 | [diff] [blame] | 1453 | * The :mod:`macpath` module, deprecated in Python 3.7, has been removed. |
| 1454 | (Contributed by Victor Stinner in :issue:`35471`.) |
| 1455 | |
Victor Stinner | 73104fa | 2018-11-29 09:58:20 +0100 | [diff] [blame] | 1456 | * The function :func:`platform.popen` has been removed, it was deprecated since |
| 1457 | Python 3.3: use :func:`os.popen` instead. |
Victor Stinner | 01ae897 | 2019-06-03 16:28:01 +0200 | [diff] [blame] | 1458 | (Contributed by Victor Stinner in :issue:`35345`.) |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1459 | |
Matthias Bussonnier | b6a09ae | 2019-05-13 12:23:07 -0700 | [diff] [blame] | 1460 | * The function :func:`time.clock` has been removed, it was deprecated since Python |
| 1461 | 3.3: use :func:`time.perf_counter` or :func:`time.process_time` instead, depending |
| 1462 | on your requirements, to have a well defined behavior. |
Victor Stinner | 01ae897 | 2019-06-03 16:28:01 +0200 | [diff] [blame] | 1463 | (Contributed by Matthias Bussonnier in :issue:`36895`.) |
Matthias Bussonnier | b6a09ae | 2019-05-13 12:23:07 -0700 | [diff] [blame] | 1464 | |
Brett Cannon | a8c3424 | 2018-04-20 14:15:40 -0700 | [diff] [blame] | 1465 | * The ``pyvenv`` script has been removed in favor of ``python3.8 -m venv`` |
| 1466 | to help eliminate confusion as to what Python interpreter the ``pyvenv`` |
| 1467 | script is tied to. (Contributed by Brett Cannon in :issue:`25427`.) |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1468 | |
INADA Naoki | 698865d | 2018-06-19 17:28:50 +0900 | [diff] [blame] | 1469 | * ``parse_qs``, ``parse_qsl``, and ``escape`` are removed from :mod:`cgi` |
Simon Willison | 1abf543 | 2019-09-11 09:25:26 -0500 | [diff] [blame^] | 1470 | module. They are deprecated from Python 3.2 or older. They should be imported |
| 1471 | from the ``urllib.parse`` and ``html`` modules instead. |
INADA Naoki | 698865d | 2018-06-19 17:28:50 +0900 | [diff] [blame] | 1472 | |
INADA Naoki | 461a1c4 | 2018-06-28 17:10:36 +0900 | [diff] [blame] | 1473 | * ``filemode`` function is removed from :mod:`tarfile` module. |
| 1474 | It is not documented and deprecated since Python 3.3. |
INADA Naoki | 698865d | 2018-06-19 17:28:50 +0900 | [diff] [blame] | 1475 | |
Serhiy Storchaka | 02ec92f | 2018-07-24 12:03:34 +0300 | [diff] [blame] | 1476 | * The :class:`~xml.etree.ElementTree.XMLParser` constructor no longer accepts |
| 1477 | the *html* argument. It never had effect and was deprecated in Python 3.4. |
| 1478 | All other parameters are now :ref:`keyword-only <keyword-only_parameter>`. |
| 1479 | (Contributed by Serhiy Storchaka in :issue:`29209`.) |
| 1480 | |
| 1481 | * Removed the ``doctype()`` method of :class:`~xml.etree.ElementTree.XMLParser`. |
| 1482 | (Contributed by Serhiy Storchaka in :issue:`29209`.) |
| 1483 | |
Inada Naoki | 6a16b18 | 2019-03-18 15:44:11 +0900 | [diff] [blame] | 1484 | * "unicode_internal" codec is removed. |
| 1485 | (Contributed by Inada Naoki in :issue:`36297`.) |
| 1486 | |
Aviv Palivoda | e657624 | 2019-05-09 21:05:45 +0300 | [diff] [blame] | 1487 | * The ``Cache`` and ``Statement`` objects of the :mod:`sqlite3` module are not |
| 1488 | exposed to the user. |
| 1489 | (Contributed by Aviv Palivoda in :issue:`30262`.) |
| 1490 | |
Matthias Bussonnier | 1a3faf9 | 2019-05-20 13:44:11 -0700 | [diff] [blame] | 1491 | * The ``bufsize`` keyword argument of :func:`fileinput.input` and |
| 1492 | :func:`fileinput.FileInput` which was ignored and deprecated since Python 3.6 |
| 1493 | has been removed. :issue:`36952` (Contributed by Matthias Bussonnier) |
| 1494 | |
Matthias Bussonnier | 382034b | 2019-05-28 10:30:35 -0700 | [diff] [blame] | 1495 | * The functions :func:`sys.set_coroutine_wrapper` and |
| 1496 | :func:`sys.get_coroutine_wrapper` deprecated in Python 3.7 have been removed; |
| 1497 | :issue:`36933` (Contributed by Matthias Bussonnier) |
Matthias Bussonnier | 3880f26 | 2019-05-28 00:10:59 -0700 | [diff] [blame] | 1498 | |
Ned Deily | 07a1892 | 2018-01-31 18:12:38 -0500 | [diff] [blame] | 1499 | |
| 1500 | Porting to Python 3.8 |
| 1501 | ===================== |
| 1502 | |
| 1503 | This section lists previously described changes and other bugfixes |
| 1504 | that may require changes to your code. |
| 1505 | |
| 1506 | |
Serhiy Storchaka | 07ca9af | 2018-02-04 10:53:48 +0200 | [diff] [blame] | 1507 | Changes in Python behavior |
| 1508 | -------------------------- |
| 1509 | |
| 1510 | * Yield expressions (both ``yield`` and ``yield from`` clauses) are now disallowed |
| 1511 | in comprehensions and generator expressions (aside from the iterable expression |
Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 1512 | in the leftmost :keyword:`!for` clause). |
Serhiy Storchaka | 07ca9af | 2018-02-04 10:53:48 +0200 | [diff] [blame] | 1513 | (Contributed by Serhiy Storchaka in :issue:`10544`.) |
| 1514 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 1515 | * The compiler now produces a :exc:`SyntaxWarning` when identity checks |
| 1516 | (``is`` and ``is not``) are used with certain types of literals |
| 1517 | (e.g. strings, ints). These can often work by accident in CPython, |
| 1518 | but are not guaranteed by the language spec. The warning advises users |
| 1519 | to use equality tests (``==`` and ``!=``) instead. |
| 1520 | (Contributed by Serhiy Storchaka in :issue:`34850`.) |
| 1521 | |
Serhiy Storchaka | 7a0630c | 2019-04-08 14:34:04 +0300 | [diff] [blame] | 1522 | * The CPython interpreter can swallow exceptions in some circumstances. |
| 1523 | In Python 3.8 this happens in less cases. In particular, exceptions |
| 1524 | raised when getting the attribute from the type dictionary are no longer |
| 1525 | ignored. (Contributed by Serhiy Storchaka in :issue:`35459`.) |
| 1526 | |
Serhiy Storchaka | 96aeaec | 2019-05-06 22:29:40 +0300 | [diff] [blame] | 1527 | * Removed ``__str__`` implementations from builtin types :class:`bool`, |
| 1528 | :class:`int`, :class:`float`, :class:`complex` and few classes from |
| 1529 | the standard library. They now inherit ``__str__()`` from :class:`object`. |
| 1530 | As result, defining the ``__repr__()`` method in the subclass of these |
| 1531 | classes will affect they string representation. |
| 1532 | (Contributed by Serhiy Storchaka in :issue:`36793`.) |
| 1533 | |
Michael Felt | 9d949f7 | 2019-04-12 16:15:32 +0200 | [diff] [blame] | 1534 | * On AIX, :attr:`sys.platform` doesn't contain the major version anymore. |
| 1535 | It is always ``'aix'``, instead of ``'aix3'`` .. ``'aix7'``. Since |
| 1536 | older Python versions include the version number, it is recommended to |
| 1537 | always use the ``sys.platform.startswith('aix')``. |
| 1538 | (Contributed by M. Felt in :issue:`36588`.) |
Serhiy Storchaka | 07ca9af | 2018-02-04 10:53:48 +0200 | [diff] [blame] | 1539 | |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 1540 | * :c:func:`PyEval_AcquireLock` and :c:func:`PyEval_AcquireThread` now |
| 1541 | terminate the current thread if called while the interpreter is |
| 1542 | finalizing, making them consistent with :c:func:`PyEval_RestoreThread`, |
| 1543 | :c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`. If this |
| 1544 | behaviour is not desired, guard the call by checking :c:func:`_Py_IsFinalizing` |
| 1545 | or :c:func:`sys.is_finalizing`. |
| 1546 | |
Serhiy Storchaka | 97f1ca1 | 2018-02-01 18:49:21 +0200 | [diff] [blame] | 1547 | Changes in the Python API |
| 1548 | ------------------------- |
| 1549 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 1550 | * The :func:`os.getcwdb` function now uses the UTF-8 encoding on Windows, |
| 1551 | rather than the ANSI code page: see :pep:`529` for the rationale. The |
| 1552 | function is no longer deprecated on Windows. |
| 1553 | (Contributed by Victor Stinner in :issue:`37412`.) |
| 1554 | |
Victor Stinner | d7befad | 2019-04-25 14:30:16 +0200 | [diff] [blame] | 1555 | * :class:`subprocess.Popen` can now use :func:`os.posix_spawn` in some cases |
| 1556 | for better performance. On Windows Subsystem for Linux and QEMU User |
| 1557 | Emulation, Popen constructor using :func:`os.posix_spawn` no longer raise an |
| 1558 | exception on errors like missing program, but the child process fails with a |
| 1559 | non-zero :attr:`~Popen.returncode`. |
Victor Stinner | 01ae897 | 2019-06-03 16:28:01 +0200 | [diff] [blame] | 1560 | (Contributed by Joannah Nanjekye and Victor Stinner in :issue:`35537`.) |
Victor Stinner | d7befad | 2019-04-25 14:30:16 +0200 | [diff] [blame] | 1561 | |
Christian Heimes | 98d90f7 | 2019-08-27 23:36:56 +0200 | [diff] [blame] | 1562 | * The *preexec_fn* argument of * :class:`subprocess.Popen` is no longer |
| 1563 | compatible with subinterpreters. The use of the parameter in a |
| 1564 | subinterpreter now raises :exc:`RuntimeError`. |
| 1565 | (Contributed by Eric Snow in :issue:`34651`, modified by Christian Heimes |
| 1566 | in :issue:`37951`.) |
| 1567 | |
Victor Stinner | 74125a6 | 2019-04-15 18:23:20 +0200 | [diff] [blame] | 1568 | * The :meth:`imap.IMAP4.logout` method no longer ignores silently arbitrary |
| 1569 | exceptions. |
| 1570 | |
Victor Stinner | 73104fa | 2018-11-29 09:58:20 +0100 | [diff] [blame] | 1571 | * The function :func:`platform.popen` has been removed, it was deprecated since |
| 1572 | Python 3.3: use :func:`os.popen` instead. |
Victor Stinner | 01ae897 | 2019-06-03 16:28:01 +0200 | [diff] [blame] | 1573 | (Contributed by Victor Stinner in :issue:`35345`.) |
Victor Stinner | 73104fa | 2018-11-29 09:58:20 +0100 | [diff] [blame] | 1574 | |
Raymond Hettinger | fc06a19 | 2019-03-12 00:43:27 -0700 | [diff] [blame] | 1575 | * The :func:`statistics.mode` function no longer raises an exception |
| 1576 | when given multimodal data. Instead, it returns the first mode |
| 1577 | encountered in the input data. (Contributed by Raymond Hettinger |
| 1578 | in :issue:`35892`.) |
| 1579 | |
Serhiy Storchaka | 97f1ca1 | 2018-02-01 18:49:21 +0200 | [diff] [blame] | 1580 | * The :meth:`~tkinter.ttk.Treeview.selection` method of the |
| 1581 | :class:`tkinter.ttk.Treeview` class no longer takes arguments. Using it with |
| 1582 | arguments for changing the selection was deprecated in Python 3.6. Use |
| 1583 | specialized methods like :meth:`~tkinter.ttk.Treeview.selection_set` for |
| 1584 | changing the selection. (Contributed by Serhiy Storchaka in :issue:`31508`.) |
Serhiy Storchaka | 6c85efa5 | 2018-02-05 22:47:31 +0200 | [diff] [blame] | 1585 | |
Diego Rojas | 06e1e68 | 2019-03-16 18:44:56 -0500 | [diff] [blame] | 1586 | * The :meth:`writexml`, :meth:`toxml` and :meth:`toprettyxml` methods of the |
| 1587 | :mod:`xml.dom.minidom` module, and :mod:`xml.etree` now preserve the attribute |
| 1588 | order specified by the user. |
| 1589 | (Contributed by Diego Rojas and Raymond Hettinger in :issue:`34160`.) |
| 1590 | |
Serhiy Storchaka | 6c85efa5 | 2018-02-05 22:47:31 +0200 | [diff] [blame] | 1591 | * A :mod:`dbm.dumb` database opened with flags ``'r'`` is now read-only. |
| 1592 | :func:`dbm.dumb.open` with flags ``'r'`` and ``'w'`` no longer creates |
| 1593 | a database if it does not exist. |
| 1594 | (Contributed by Serhiy Storchaka in :issue:`32749`.) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1595 | |
Serhiy Storchaka | 02ec92f | 2018-07-24 12:03:34 +0300 | [diff] [blame] | 1596 | * The ``doctype()`` method defined in a subclass of |
| 1597 | :class:`~xml.etree.ElementTree.XMLParser` will no longer be called and will |
| 1598 | cause emitting a :exc:`RuntimeWarning` instead of a :exc:`DeprecationWarning`. |
| 1599 | Define the :meth:`doctype() <xml.etree.ElementTree.TreeBuilder.doctype>` |
| 1600 | method on a target for handling an XML doctype declaration. |
| 1601 | (Contributed by Serhiy Storchaka in :issue:`29209`.) |
| 1602 | |
Serhiy Storchaka | f5e7b19 | 2018-05-20 08:48:12 +0300 | [diff] [blame] | 1603 | * A :exc:`RuntimeError` is now raised when the custom metaclass doesn't |
| 1604 | provide the ``__classcell__`` entry in the namespace passed to |
| 1605 | ``type.__new__``. A :exc:`DeprecationWarning` was emitted in Python |
| 1606 | 3.6--3.7. (Contributed by Serhiy Storchaka in :issue:`23722`.) |
| 1607 | |
Scott Sanderson | cebe80b | 2018-06-07 05:46:42 -0400 | [diff] [blame] | 1608 | * The :class:`cProfile.Profile` class can now be used as a context |
| 1609 | manager. (Contributed by Scott Sanderson in :issue:`29235`.) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1610 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 1611 | * :func:`shutil.copyfile`, :func:`shutil.copy`, :func:`shutil.copy2`, |
| 1612 | :func:`shutil.copytree` and :func:`shutil.move` use platform-specific |
| 1613 | "fast-copy" syscalls (see |
| 1614 | :ref:`shutil-platform-dependent-efficient-copy-operations` section). |
| 1615 | |
| 1616 | * :func:`shutil.copyfile` default buffer size on Windows was changed from |
| 1617 | 16 KiB to 1 MiB. |
| 1618 | |
Raymond Hettinger | 4f9ffc9 | 2019-08-05 13:33:19 -0700 | [diff] [blame] | 1619 | * The ``PyGC_Head`` struct has changed completely. All code that touched the |
INADA Naoki | d5c875b | 2018-07-11 17:42:49 +0900 | [diff] [blame] | 1620 | struct member should be rewritten. (See :issue:`33597`) |
| 1621 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 1622 | * The ``PyInterpreterState`` struct has been moved into the "internal" |
| 1623 | header files (specifically Include/internal/pycore_pystate.h). An |
| 1624 | opaque ``PyInterpreterState`` is still available as part of the public |
| 1625 | API (and stable ABI). The docs indicate that none of the struct's |
| 1626 | fields are public, so we hope no one has been using them. However, |
| 1627 | if you do rely on one or more of those private fields and have no |
| 1628 | alternative then please open a BPO issue. We'll work on helping |
| 1629 | you adjust (possibly including adding accessor functions to the |
| 1630 | public API). (See :issue:`35886`.) |
| 1631 | |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 1632 | * Asyncio tasks can now be named, either by passing the ``name`` keyword |
| 1633 | argument to :func:`asyncio.create_task` or |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1634 | the :meth:`~asyncio.loop.create_task` event loop method, or by |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 1635 | calling the :meth:`~asyncio.Task.set_name` method on the task object. The |
| 1636 | task name is visible in the ``repr()`` output of :class:`asyncio.Task` and |
| 1637 | can also be retrieved using the :meth:`~asyncio.Task.get_name` method. |
| 1638 | |
Berker Peksag | e7d4b2f | 2018-08-22 21:21:05 +0300 | [diff] [blame] | 1639 | * The :meth:`mmap.flush() <mmap.mmap.flush>` method now returns ``None`` on |
| 1640 | success and raises an exception on error under all platforms. Previously, |
| 1641 | its behavior was platform-depended: a nonzero value was returned on success; |
| 1642 | zero was returned on error under Windows. A zero value was returned on |
| 1643 | success; an exception was raised on error under Unix. |
| 1644 | (Contributed by Berker Peksag in :issue:`2122`.) |
| 1645 | |
Andrés Delfino | ca68261 | 2018-11-07 14:29:14 -0300 | [diff] [blame] | 1646 | * :mod:`xml.dom.minidom` and :mod:`xml.sax` modules no longer process |
Christian Heimes | 17b1d5d | 2018-09-23 09:50:25 +0200 | [diff] [blame] | 1647 | external entities by default. |
| 1648 | (Contributed by Christian Heimes in :issue:`17239`.) |
INADA Naoki | d5c875b | 2018-07-11 17:42:49 +0900 | [diff] [blame] | 1649 | |
Xiang Zhang | 4fb0b8b | 2018-12-12 20:46:55 +0800 | [diff] [blame] | 1650 | * Deleting a key from a read-only :mod:`dbm` database (:mod:`dbm.dumb`, |
| 1651 | :mod:`dbm.gnu` or :mod:`dbm.ndbm`) raises :attr:`error` (:exc:`dbm.dumb.error`, |
| 1652 | :exc:`dbm.gnu.error` or :exc:`dbm.ndbm.error`) instead of :exc:`KeyError`. |
| 1653 | (Contributed by Xiang Zhang in :issue:`33106`.) |
| 1654 | |
Steve Dower | 8ef864d | 2019-03-12 15:15:26 -0700 | [diff] [blame] | 1655 | * :func:`~os.path.expanduser` on Windows now prefers the :envvar:`USERPROFILE` |
| 1656 | environment variable and does not use :envvar:`HOME`, which is not normally |
| 1657 | set for regular user accounts. |
| 1658 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1659 | .. _bpo-36085-whatsnew: |
| 1660 | |
| 1661 | * DLL dependencies for extension modules and DLLs loaded with :mod:`ctypes` on |
| 1662 | Windows are now resolved more securely. Only the system paths, the directory |
| 1663 | containing the DLL or PYD file, and directories added with |
| 1664 | :func:`~os.add_dll_directory` are searched for load-time dependencies. |
| 1665 | Specifically, :envvar:`PATH` and the current working directory are no longer |
| 1666 | used, and modifications to these will no longer have any effect on normal DLL |
| 1667 | resolution. If your application relies on these mechanisms, you should check |
| 1668 | for :func:`~os.add_dll_directory` and if it exists, use it to add your DLLs |
Steve Dower | 79da388 | 2019-03-30 20:58:17 -0700 | [diff] [blame] | 1669 | directory while loading your library. Note that Windows 7 users will need to |
| 1670 | ensure that Windows Update KB2533625 has been installed (this is also verified |
| 1671 | by the installer). |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1672 | (See :issue:`36085`.) |
| 1673 | |
Pablo Galindo | f2cf1e3 | 2019-04-13 17:05:14 +0100 | [diff] [blame] | 1674 | * The header files and functions related to pgen have been removed after its |
| 1675 | replacement by a pure Python implementation. (Contributed by Pablo Galindo |
| 1676 | in :issue:`36623`.) |
| 1677 | |
Pablo Galindo | 5d23e28 | 2019-05-12 22:45:52 +0100 | [diff] [blame] | 1678 | * :class:`types.CodeType` has a new parameter in the second position of the |
| 1679 | constructor (*posonlyargcount*) to support positional-only arguments defined |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 1680 | in :pep:`570`. The first argument (*argcount*) now represents the total |
| 1681 | number of positional arguments (including positional-only arguments). A new |
| 1682 | ``replace()`` method of :class:`types.CodeType` can be used to make the code |
| 1683 | future-proof. |
Pablo Galindo | 5d23e28 | 2019-05-12 22:45:52 +0100 | [diff] [blame] | 1684 | |
Xiang Zhang | 4fb0b8b | 2018-12-12 20:46:55 +0800 | [diff] [blame] | 1685 | |
Inada Naoki | d3c72a2 | 2019-03-23 21:04:40 +0900 | [diff] [blame] | 1686 | Changes in the C API |
| 1687 | -------------------- |
| 1688 | |
Victor Stinner | 2c9b498 | 2019-06-13 02:01:29 +0200 | [diff] [blame] | 1689 | * The :c:type:`PyCompilerFlags` structure gets a new *cf_feature_version* |
| 1690 | field. It should be initialized to ``PY_MINOR_VERSION``. The field is ignored |
| 1691 | by default, it is used if and only if ``PyCF_ONLY_AST`` flag is set in |
| 1692 | *cf_flags*. |
| 1693 | |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 1694 | * The :c:func:`PyEval_ReInitThreads` function has been removed from the C API. |
| 1695 | It should not be called explicitly: use :c:func:`PyOS_AfterFork_Child` |
| 1696 | instead. |
| 1697 | (Contributed by Victor Stinner in :issue:`36728`.) |
| 1698 | |
E. M. Bray | c994c8f | 2019-05-24 17:33:47 +0200 | [diff] [blame] | 1699 | * On Unix, C extensions are no longer linked to libpython except on Android |
| 1700 | and Cygwin. When Python is embedded, ``libpython`` must not be loaded with |
xdegaye | 254b309 | 2019-04-29 09:27:40 +0200 | [diff] [blame] | 1701 | ``RTLD_LOCAL``, but ``RTLD_GLOBAL`` instead. Previously, using |
E. M. Bray | c994c8f | 2019-05-24 17:33:47 +0200 | [diff] [blame] | 1702 | ``RTLD_LOCAL``, it was already not possible to load C extensions which |
| 1703 | were not linked to ``libpython``, like C extensions of the standard |
| 1704 | library built by the ``*shared*`` section of ``Modules/Setup``. |
Victor Stinner | 01ae897 | 2019-06-03 16:28:01 +0200 | [diff] [blame] | 1705 | (Contributed by Victor Stinner in :issue:`21536`.) |
Victor Stinner | 8c3ecc6 | 2019-04-25 20:13:10 +0200 | [diff] [blame] | 1706 | |
Inada Naoki | d3c72a2 | 2019-03-23 21:04:40 +0900 | [diff] [blame] | 1707 | * Use of ``#`` variants of formats in parsing or building value (e.g. |
| 1708 | :c:func:`PyArg_ParseTuple`, :c:func:`Py_BuildValue`, :c:func:`PyObject_CallFunction`, |
| 1709 | etc.) without ``PY_SSIZE_T_CLEAN`` defined raises ``DeprecationWarning`` now. |
| 1710 | It will be removed in 3.10 or 4.0. Read :ref:`arg-parsing` for detail. |
| 1711 | (Contributed by Inada Naoki in :issue:`36381`.) |
| 1712 | |
Eddie Elizondo | 364f0b0 | 2019-03-27 07:52:18 -0400 | [diff] [blame] | 1713 | * Instances of heap-allocated types (such as those created with |
| 1714 | :c:func:`PyType_FromSpec`) hold a reference to their type object. |
| 1715 | Increasing the reference count of these type objects has been moved from |
| 1716 | :c:func:`PyType_GenericAlloc` to the more low-level functions, |
| 1717 | :c:func:`PyObject_Init` and :c:func:`PyObject_INIT`. |
| 1718 | This makes types created through :c:func:`PyType_FromSpec` behave like |
| 1719 | other classes in managed code. |
| 1720 | |
| 1721 | Statically allocated types are not affected. |
| 1722 | |
| 1723 | For the vast majority of cases, there should be no side effect. |
| 1724 | However, types that manually increase the reference count after allocating |
| 1725 | an instance (perhaps to work around the bug) may now become immortal. |
| 1726 | To avoid this, these classes need to call Py_DECREF on the type object |
| 1727 | during instance deallocation. |
| 1728 | |
| 1729 | To correctly port these types into 3.8, please apply the following |
| 1730 | changes: |
| 1731 | |
| 1732 | * Remove :c:macro:`Py_INCREF` on the type object after allocating an |
| 1733 | instance - if any. |
| 1734 | This may happen after calling :c:func:`PyObject_New`, |
| 1735 | :c:func:`PyObject_NewVar`, :c:func:`PyObject_GC_New`, |
| 1736 | :c:func:`PyObject_GC_NewVar`, or any other custom allocator that uses |
| 1737 | :c:func:`PyObject_Init` or :c:func:`PyObject_INIT`. |
| 1738 | |
| 1739 | Example:: |
| 1740 | |
| 1741 | static foo_struct * |
| 1742 | foo_new(PyObject *type) { |
| 1743 | foo_struct *foo = PyObject_GC_New(foo_struct, (PyTypeObject *) type); |
| 1744 | if (foo == NULL) |
| 1745 | return NULL; |
| 1746 | #if PY_VERSION_HEX < 0x03080000 |
| 1747 | // Workaround for Python issue 35810; no longer necessary in Python 3.8 |
| 1748 | PY_INCREF(type) |
| 1749 | #endif |
| 1750 | return foo; |
| 1751 | } |
| 1752 | |
| 1753 | * Ensure that all custom ``tp_dealloc`` functions of heap-allocated types |
| 1754 | decrease the type's reference count. |
| 1755 | |
| 1756 | Example:: |
| 1757 | |
| 1758 | static void |
| 1759 | foo_dealloc(foo_struct *instance) { |
| 1760 | PyObject *type = Py_TYPE(instance); |
| 1761 | PyObject_GC_Del(instance); |
| 1762 | #if PY_VERSION_HEX >= 0x03080000 |
| 1763 | // This was not needed before Python 3.8 (Python issue 35810) |
| 1764 | Py_DECREF(type); |
| 1765 | #endif |
| 1766 | } |
| 1767 | |
| 1768 | (Contributed by Eddie Elizondo in :issue:`35810`.) |
| 1769 | |
Zackery Spytz | 3c8724f | 2019-05-28 09:16:33 -0600 | [diff] [blame] | 1770 | * The :c:macro:`Py_DEPRECATED()` macro has been implemented for MSVC. |
| 1771 | The macro now must be placed before the symbol name. |
| 1772 | |
| 1773 | Example:: |
| 1774 | |
| 1775 | Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void); |
| 1776 | |
| 1777 | (Contributed by Zackery Spytz in :issue:`33407`.) |
| 1778 | |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 1779 | * The interpreter does not pretend to support binary compatibility of |
Xtreak | 0d70227 | 2019-06-03 04:42:33 +0530 | [diff] [blame] | 1780 | extension types across feature releases, anymore. A :c:type:`PyTypeObject` |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 1781 | exported by a third-party extension module is supposed to have all the |
| 1782 | slots expected in the current Python version, including |
| 1783 | :c:member:`~PyTypeObject.tp_finalize` (:const:`Py_TPFLAGS_HAVE_FINALIZE` |
| 1784 | is not checked anymore before reading :c:member:`~PyTypeObject.tp_finalize`). |
| 1785 | |
| 1786 | (Contributed by Antoine Pitrou in :issue:`32388`.) |
| 1787 | |
Pablo Galindo | 545a3b8 | 2019-05-31 19:33:41 +0100 | [diff] [blame] | 1788 | * The :c:func:`PyCode_New` has a new parameter in the second position (*posonlyargcount*) |
| 1789 | to support :pep:`570`, indicating the number of positional-only arguments. |
| 1790 | |
Ivan Levkivskyi | 47c2de7 | 2019-06-19 01:17:47 +0100 | [diff] [blame] | 1791 | * The functions :c:func:`PyNode_AddChild` and :c:func:`PyParser_AddToken` now accept |
| 1792 | two additional ``int`` arguments *end_lineno* and *end_col_offset*. |
Eddie Elizondo | 364f0b0 | 2019-03-27 07:52:18 -0400 | [diff] [blame] | 1793 | |
Steve Dower | f569092 | 2019-06-21 14:28:46 -0700 | [diff] [blame] | 1794 | .. highlight:: shell |
| 1795 | |
| 1796 | * The :file:`libpython38.a` file to allow MinGW tools to link directly against |
| 1797 | :file:`python38.dll` is no longer included in the regular Windows distribution. |
| 1798 | If you require this file, it may be generated with the ``gendef`` and |
| 1799 | ``dlltool`` tools, which are part of the MinGW binutils package:: |
| 1800 | |
| 1801 | gendef python38.dll > tmp.def |
| 1802 | dlltool --dllname python38.dll --def tmp.def --output-lib libpython38.a |
| 1803 | |
| 1804 | The location of an installed :file:`pythonXY.dll` will depend on the |
| 1805 | installation options and the version and language of Windows. See |
| 1806 | :ref:`using-on-windows` for more information. The resulting library should be |
| 1807 | placed in the same directory as :file:`pythonXY.lib`, which is generally the |
| 1808 | :file:`libs` directory under your Python installation. |
| 1809 | |
| 1810 | .. highlight:: python3 |
| 1811 | |
| 1812 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1813 | CPython bytecode changes |
| 1814 | ------------------------ |
| 1815 | |
| 1816 | * The interpreter loop has been simplified by moving the logic of unrolling |
| 1817 | the stack of blocks into the compiler. The compiler emits now explicit |
Serhiy Storchaka | 3f819ca | 2018-10-31 02:26:06 +0200 | [diff] [blame] | 1818 | instructions for adjusting the stack of values and calling the |
| 1819 | cleaning-up code for :keyword:`break`, :keyword:`continue` and |
| 1820 | :keyword:`return`. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1821 | |
| 1822 | Removed opcodes :opcode:`BREAK_LOOP`, :opcode:`CONTINUE_LOOP`, |
| 1823 | :opcode:`SETUP_LOOP` and :opcode:`SETUP_EXCEPT`. Added new opcodes |
| 1824 | :opcode:`ROT_FOUR`, :opcode:`BEGIN_FINALLY`, :opcode:`CALL_FINALLY` and |
| 1825 | :opcode:`POP_FINALLY`. Changed the behavior of :opcode:`END_FINALLY` |
| 1826 | and :opcode:`WITH_CLEANUP_START`. |
| 1827 | |
| 1828 | (Contributed by Mark Shannon, Antoine Pitrou and Serhiy Storchaka in |
| 1829 | :issue:`17611`.) |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1830 | |
| 1831 | * Added new opcode :opcode:`END_ASYNC_FOR` for handling exceptions raised |
| 1832 | when awaiting a next item in an :keyword:`async for` loop. |
| 1833 | (Contributed by Serhiy Storchaka in :issue:`33041`.) |
Raymond Hettinger | f75d59e | 2019-02-02 22:54:56 -0800 | [diff] [blame] | 1834 | |
Pablo Galindo | b51b713 | 2019-06-25 02:41:58 +0100 | [diff] [blame] | 1835 | * The :opcode:`MAP_ADD` now expects the value as the first element in the |
| 1836 | stack and the key as the second element. This change was made so the key |
| 1837 | is always evaluated before the value in dictionary comprehensions, as |
Pablo Galindo | de9b606 | 2019-06-25 11:55:23 +0100 | [diff] [blame] | 1838 | proposed by :pep:`572`. (Contributed by Jörn Heissler in :issue:`35224`.) |
Pablo Galindo | b51b713 | 2019-06-25 02:41:58 +0100 | [diff] [blame] | 1839 | |
Raymond Hettinger | f75d59e | 2019-02-02 22:54:56 -0800 | [diff] [blame] | 1840 | |
| 1841 | Demos and Tools |
| 1842 | --------------- |
| 1843 | |
| 1844 | * Added a benchmark script for timing various ways to access variables: |
| 1845 | ``Tools/scripts/var_access_benchmark.py``. |
| 1846 | (Contributed by Raymond Hettinger in :issue:`35884`.) |