blob: 8a3a01fa10444ed21a6383f3978e027c53efc516 [file] [log] [blame]
Georg Brandlc47408a2008-12-04 18:44:53 +00001****************************
Georg Brandl48310cd2009-01-03 21:18:54 +00002 What's New In Python 3.1
Georg Brandlc47408a2008-12-04 18:44:53 +00003****************************
4
Raymond Hettinger1f251a02009-04-04 10:47:35 +00005:Author: Raymond Hettinger
Benjamin Peterson34328292008-12-05 03:05:29 +00006:Release: |release|
7:Date: |today|
Georg Brandlc47408a2008-12-04 18:44:53 +00008
9.. $Id$
10 Rules for maintenance:
Georg Brandl48310cd2009-01-03 21:18:54 +000011
Georg Brandlc47408a2008-12-04 18:44:53 +000012 * Anyone can add text to this document. Do not spend very much time
13 on the wording of your changes, because your text will probably
14 get rewritten to some degree.
Georg Brandl48310cd2009-01-03 21:18:54 +000015
Georg Brandlc47408a2008-12-04 18:44:53 +000016 * The maintainer will go through Misc/NEWS periodically and add
17 changes; it's therefore more important to add your changes to
Raymond Hettingerb9b33192009-04-08 09:38:32 +000018 Misc/NEWS than to this file.
Georg Brandl48310cd2009-01-03 21:18:54 +000019
Georg Brandlc47408a2008-12-04 18:44:53 +000020 * This is not a complete list of every single change; completeness
21 is the purpose of Misc/NEWS. Some changes I consider too small
22 or esoteric to include. If such a change is added to the text,
23 I'll just remove it. (This is another reason you shouldn't spend
24 too much time on writing your addition.)
Georg Brandl48310cd2009-01-03 21:18:54 +000025
Georg Brandlc47408a2008-12-04 18:44:53 +000026 * If you want to draw your new text to the attention of the
27 maintainer, add 'XXX' to the beginning of the paragraph or
28 section.
Georg Brandl48310cd2009-01-03 21:18:54 +000029
Georg Brandlc47408a2008-12-04 18:44:53 +000030 * It's OK to just add a fragmentary note about a change. For
31 example: "XXX Describe the transmogrify() function added to the
32 socket module." The maintainer will research the change and
33 write the necessary text.
Georg Brandl48310cd2009-01-03 21:18:54 +000034
Georg Brandlc47408a2008-12-04 18:44:53 +000035 * You can comment out your additions if you like, but it's not
36 necessary (especially when a final release is some months away).
Georg Brandl48310cd2009-01-03 21:18:54 +000037
Georg Brandlc47408a2008-12-04 18:44:53 +000038 * Credit the author of a patch or bugfix. Just the name is
Raymond Hettingerb9b33192009-04-08 09:38:32 +000039 sufficient; the e-mail address isn't necessary.
Georg Brandl48310cd2009-01-03 21:18:54 +000040
Georg Brandlc47408a2008-12-04 18:44:53 +000041 * It's helpful to add the bug/patch number as a comment:
Georg Brandl48310cd2009-01-03 21:18:54 +000042
Georg Brandlc47408a2008-12-04 18:44:53 +000043 % Patch 12345
44 XXX Describe the transmogrify() function added to the socket
45 module.
46 (Contributed by P.Y. Developer.)
Georg Brandl48310cd2009-01-03 21:18:54 +000047
Georg Brandlc47408a2008-12-04 18:44:53 +000048 This saves the maintainer the effort of going through the SVN log
Raymond Hettingerb9b33192009-04-08 09:38:32 +000049 when researching a change.
Georg Brandlc47408a2008-12-04 18:44:53 +000050
51This article explains the new features in Python 3.1, compared to 3.0.
52
Georg Brandlc47408a2008-12-04 18:44:53 +000053
Raymond Hettinger1f251a02009-04-04 10:47:35 +000054PEP 372: Ordered Dictionaries
55=============================
56
57Regular Python dictionaries iterate over key/value pairs in arbitrary order.
58Over the years, a number of authors have written alternative implementations
59that remember the order that the keys were originally inserted. Based on
Raymond Hettingerd621dd72009-04-14 08:16:50 +000060the experiences from those implementations, a new
61:class:`collections.OrderedDict` class has been introduced.
Raymond Hettinger1f251a02009-04-04 10:47:35 +000062
63The OrderedDict API is substantially the same as regular dictionaries
64but will iterate over keys and values in a guaranteed order depending on
65when a key was first inserted. If a new entry overwrites an existing entry,
66the original insertion position is left unchanged. Deleting an entry and
67reinserting it will move it to the end.
68
69The standard library now supports use of ordered dictionaries in several
Raymond Hettinger7f5d7462009-04-14 08:05:31 +000070modules. The :mod:`configparser` module uses them by default. This lets
Raymond Hettinger1f251a02009-04-04 10:47:35 +000071configuration files be read, modified, and then written back in their original
Raymond Hettingerd621dd72009-04-14 08:16:50 +000072order. The *_asdict()* method for :func:`collections.namedtuple` now
Raymond Hettingerc4f6d292009-04-04 12:35:58 +000073returns an ordered dictionary with the values appearing in the same order as
74the underlying tuple indicies. The :mod:`json` module is being built-out with
75an *object_pairs_hook* to allow OrderedDicts to be built by the decoder.
Raymond Hettinger347396a2009-04-07 23:10:59 +000076Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_.
Raymond Hettinger1f251a02009-04-04 10:47:35 +000077
78.. seealso::
79
80 :pep:`372` - Ordered Dictionaries
Raymond Hettingerf84dfe52009-04-04 13:13:56 +000081 PEP written by Armin Ronacher and Raymond Hettinger. Implementation
82 written by Raymond Hettinger.
Raymond Hettinger1f251a02009-04-04 10:47:35 +000083
Raymond Hettingerb9b33192009-04-08 09:38:32 +000084
Raymond Hettinger1f251a02009-04-04 10:47:35 +000085PEP 378: Format Specifier for Thousands Separator
86=================================================
87
88The builtin :func:`format` function and the :meth:`str.format` method use
89a mini-language that now includes a simple, non-locale aware way to format
90a number with a thousands separator. That provides a way to humanize a
91program's output, improving its professional appearance and readability::
92
Raymond Hettinger0422e142009-04-17 18:58:06 +000093 >>> format(1234567, ',d')
94 '1,234,567'
95 >>> format(1234567.89, ',.2f')
96 '1,234,567.89'
Raymond Hettingerd48ed2e2009-05-14 22:48:19 +000097 >>> format(12345.6 + 8901234.12j, ',f')
98 '12,345.600000+8,901,234.120000j'
Raymond Hettinger1f251a02009-04-04 10:47:35 +000099 >>> format(Decimal('1234567.89'), ',f')
100 '1,234,567.89'
101
Raymond Hettingerd48ed2e2009-05-14 22:48:19 +0000102The supported types are :class:`int`, :class:`float`, :class:`complex`
103and :class:`decimal.Decimal`.
Raymond Hettingerc548b6e2009-04-17 10:09:27 +0000104
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000105Discussions are underway about how to specify alternative separators
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000106like dots, spaces, apostrophes, or underscores. Locale-aware applications
107should use the existing *n* format specifier which already has some support
108for thousands separators.
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000109
110.. seealso::
111
112 :pep:`378` - Format Specifier for Thousands Separator
Raymond Hettinger55fc9ce2009-04-14 20:45:17 +0000113 PEP written by Raymond Hettinger and implemented by Eric Smith and
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000114 Mark Dickinson.
115
116
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000117Other Language Changes
118======================
119
120Some smaller changes made to the core Python language are:
121
122* The :func:`int` type gained a ``bit_length`` method that returns the
123 number of bits necessary to represent its argument in binary::
124
125 >>> n = 37
126 >>> bin(37)
127 '0b100101'
128 >>> n.bit_length()
129 6
130 >>> n = 2**123-1
131 >>> n.bit_length()
132 123
133 >>> (n+1).bit_length()
134 124
135
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000136 (Contributed by Fredrik Johansson, Victor Stinner, Raymond Hettinger,
137 and Mark Dickinson; :issue:`3439`.)
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000138
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000139* The fields in :func:`format` strings can now be automatically
140 numbered::
141
142 >>> 'Sir {} of {}'.format('Gallahad', 'Camelot')
143 'Sir Gallahad of Camelot'
144
145 Formerly, the string would have required numbered fields such as:
146 ``'Sir {0} of {1}'``.
147
148 (Contributed by Eric Smith; :issue:`5237`.)
149
Raymond Hettingerbd3da6b2009-05-15 16:16:12 +0000150* The :func:`string.maketrans` function is deprecated and is replaced by new
151 static methods, :meth:`bytes.maketrans` and :meth:`bytearray.maketrans`.
152 This change solves the confusion around which types were supported by the
153 :mod:`string` module. Now, :class:`str`, :class:`bytes`, and
154 :class:`bytearray` each have their own **maketrans** and **translate**
155 methods with intermediate translation tables of the appropriate type.
156
157 (Contributed by Georg Brandl; :issue:`5675`.)
158
Raymond Hettinger81c0dce2009-05-27 09:12:18 +0000159* The syntax of the :keyword:`with` statement now allows multiple context
160 managers in a single statement::
161
162 >>> with open('mylog.txt') as infile, open('a.out', 'w') as outfile:
163 ... for line in infile:
164 ... if '<critical>' in line:
165 ... outfile.write(line)
166
Georg Brandled23ab62009-05-27 19:46:38 +0000167 (Contributed by Georg Brandl and Mattias Brändström;
Raymond Hettinger81c0dce2009-05-27 09:12:18 +0000168 `appspot issue 53094 <http://codereview.appspot.com/53094>`_.)
169
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000170* ``round(x, n)`` now returns an integer if *x* is an integer.
171 Previously it returned a float::
172
173 >>> round(1123, -2)
174 1100
175
176 (Contributed by Mark Dickinson; :issue:`4707`.)
177
Raymond Hettinger8648e502009-04-17 00:11:54 +0000178* Python now uses David Gay's algorithm for finding the shortest floating
179 point representation that doesn't change its value. This should help
Eric Smith86c06bc2009-04-17 12:33:24 +0000180 mitigate some of the confusion surrounding binary floating point
Raymond Hettinger8648e502009-04-17 00:11:54 +0000181 numbers.
182
183 The significance is easily seen with a number like ``1.1`` which does not
184 have an exact equivalent in binary floating point. Since there is no exact
Raymond Hettingerfee346b2009-04-17 09:45:19 +0000185 equivalent, an expression like ``float('1.1')`` evaluates to the nearest
Raymond Hettinger8648e502009-04-17 00:11:54 +0000186 representable value which is ``0x1.199999999999ap+0`` in hex or
187 ``1.100000000000000088817841970012523233890533447265625`` in decimal. That
188 nearest value was and still is used in subsequent floating point
189 calculations.
190
191 What is new is how the number gets displayed. Formerly, Python used a
192 simple approach. The value of ``repr(1.1)`` was computed as ``format(1.1,
Raymond Hettingerfee346b2009-04-17 09:45:19 +0000193 '.17g')`` which evaluated to ``'1.1000000000000001'``. The advantage of
Raymond Hettinger8648e502009-04-17 00:11:54 +0000194 using 17 digits was that it relied on IEEE-754 guarantees to assure that
195 ``eval(repr(1.1))`` would round-trip exactly to its original value. The
196 disadvantage is that many people found the output to be confusing (mistaking
197 intrinsic limitations of binary floating point representation as being a
198 problem with Python itself).
199
Raymond Hettingerfee346b2009-04-17 09:45:19 +0000200 The new algorithm for ``repr(1.1)`` is smarter and returns ``'1.1'``.
Raymond Hettinger8648e502009-04-17 00:11:54 +0000201 Effectively, it searches all equivalent string representations (ones that
Raymond Hettingerfee346b2009-04-17 09:45:19 +0000202 get stored with the same underlying float value) and returns the shortest
Raymond Hettinger8648e502009-04-17 00:11:54 +0000203 representation.
204
205 The new algorithm tends to emit cleaner representations when possible, but
206 it does not change the underlying values. So, it is still the case that
207 ``1.1 + 2.2 != 3.3`` even though the representations may suggest otherwise.
208
209 The new algorithm depends on certain features in the underlying floating
210 point implementation. If the required features are not found, the old
211 algorithm will continue to be used. Also, the text pickle protocols
212 assure cross-platform portability by using the old algorithm.
213
214 (Contributed by Eric Smith and Mark Dickinson; :issue:`1580`)
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000215
216New, Improved, and Deprecated Modules
217=====================================
218
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000219* Added a :class:`collections.Counter` class to support convenient
220 counting of unique items in a sequence or iterable::
221
222 >>> Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])
223 Counter({'blue': 3, 'red': 2, 'green': 1})
224
225 (Contributed by Raymond Hettinger; :issue:`1696199`.)
226
Raymond Hettingerc1bd49a2009-04-06 23:11:08 +0000227* Added a new module, :mod:`tkinter.ttk` for access to the Tk themed widget set.
Raymond Hettingerdbd51b52009-04-06 22:45:52 +0000228 The basic idea of ttk is to separate, to the extent possible, the code
Raymond Hettingerf84dfe52009-04-04 13:13:56 +0000229 implementing a widget's behavior from the code implementing its appearance.
230
Raymond Hettingerd1b3de32009-04-08 00:09:26 +0000231 (Contributed by Guilherme Polo; :issue:`2983`.)
Raymond Hettingerf84dfe52009-04-04 13:13:56 +0000232
Raymond Hettingerf4cc2c42009-04-06 22:39:03 +0000233* The :class:`gzip.GzipFile` and :class:`bz2.BZ2File` classes now support
234 the context manager protocol::
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000235
Raymond Hettingerf4cc2c42009-04-06 22:39:03 +0000236 >>> # Automatically close file after writing
237 >>> with gzip.GzipFile(filename, "wb") as f:
238 ... f.write(b"xxx")
239
240 (Contributed by Antoine Pitrou.)
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000241
Raymond Hettingerbf1537c2009-04-09 22:34:46 +0000242* The :mod:`decimal` module now supports methods for creating a
Raymond Hettinger76efa2b2009-04-06 23:11:47 +0000243 decimal object from a binary :class:`float`. The conversion is
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000244 exact but can sometimes be surprising::
245
246 >>> Decimal.from_float(1.1)
247 Decimal('1.100000000000000088817841970012523233890533447265625')
248
249 The long decimal result shows the actual binary fraction being
250 stored for *1.1*. The fraction has many digits because *1.1* cannot
251 be exactly represented in binary.
252
253 (Contributed by Raymond Hettinger and Mark Dickinson.)
254
Raymond Hettingere7ec57d2009-04-04 11:08:48 +0000255* The :mod:`itertools` module grew two new functions. The
256 :func:`itertools.combinations_with_replacement` function is one of
257 four for generating combinatorics including permutations and Cartesian
258 products. The :func:`itertools.compress` function mimics its namesake
259 from APL. Also, the existing :func:`itertools.count` function now has
260 an optional *step* argument and can accept any type of counting
261 sequence including :class:`fractions.Fraction` and
Raymond Hettinger8d97ccb2009-04-06 17:55:05 +0000262 :class:`decimal.Decimal`::
263
264 >>> [p+q for p,q in combinations_with_replacement('LOVE', 2)]
265 ['LL', 'LO', 'LV', 'LE', 'OO', 'OV', 'OE', 'VV', 'VE', 'EE']
266
267 >>> list(compress(data=range(10), selectors=[0,0,1,1,0,1,0,1,0,0]))
268 [2, 3, 5, 7]
269
270 >>> c = count(start=Fraction(1,2), step=Fraction(1,6))
Raymond Hettingerbd3da6b2009-05-15 16:16:12 +0000271 >>> [next(c), next(c), next(c), next(c)]
272 [Fraction(1, 2), Fraction(2, 3), Fraction(5, 6), Fraction(1, 1)]
Raymond Hettingere7ec57d2009-04-04 11:08:48 +0000273
274 (Contributed by Raymond Hettinger.)
275
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000276* :func:`collections.namedtuple` now supports a keyword argument
277 *rename* which lets invalid fieldnames be automatically converted to
278 positional names in the form _0, _1, etc. This is useful when
279 the field names are being created by an external source such as a
Raymond Hettinger4c0b1e42009-04-08 07:49:03 +0000280 CSV header, SQL field list, or user input::
281
Raymond Hettinger2a39e0f2009-04-08 22:50:09 +0000282 >>> query = input()
283 SELECT region, dept, count(*) FROM main GROUPBY region, dept
Raymond Hettinger4c0b1e42009-04-08 07:49:03 +0000284
285 >>> cursor.execute(query)
286 >>> query_fields = [desc[0] for desc in cursor.description]
287 >>> UserQuery = namedtuple('UserQuery', query_fields, rename=True)
288 >>> pprint.pprint([UserQuery(*row) for row in cursor])
289 [UserQuery(region='South', dept='Shipping', _2=185),
290 UserQuery(region='North', dept='Accounting', _2=37),
291 UserQuery(region='West', dept='Sales', _2=419)]
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000292
293 (Contributed by Raymond Hettinger; :issue:`1818`.)
294
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000295* The :func:`re.sub`, :func:`re.subn` and :func:`re.split` functions now
296 accept a flags parameter.
297
298 (Contributed by Gregory Smith.)
299
Raymond Hettingerd621dd72009-04-14 08:16:50 +0000300* The :mod:`logging` module now implements a simple :class:`logging.NullHandler`
Raymond Hettinger35a88362009-04-09 00:08:24 +0000301 class for applications that are not using logging but are calling
302 library code that does. Setting-up a null handler will suppress
Raymond Hettingerd621dd72009-04-14 08:16:50 +0000303 spurious warnings such as "No handlers could be found for logger foo"::
Raymond Hettinger35a88362009-04-09 00:08:24 +0000304
305 >>> h = logging.NullHandler()
306 >>> logging.getLogger("foo").addHandler(h)
307
Raymond Hettingerd621dd72009-04-14 08:16:50 +0000308 (Contributed by Vinay Sajip; :issue:`4384`).
Raymond Hettinger35a88362009-04-09 00:08:24 +0000309
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000310* The :mod:`runpy` module which supports the ``-m`` command line switch
311 now supports the execution of packages by looking for and executing
312 a ``__main__`` submodule when a package name is supplied.
313
314 (Contributed by Andi Vajda; :issue:`4195`.)
315
316* The :mod:`pdb` module can now access and display source code loaded via
317 :mod:`zipimport` (or any other conformant :pep:`302` loader).
318
319 (Contributed by Alexander Belopolsky; :issue:`4201`.)
320
321* :class:`functools.partial` objects can now be pickled.
322
323 (Suggested by Antoine Pitrou and Jesse Noller. Implemented by
324 Jack Diedrich; :issue:`5228`.)
325
Raymond Hettinger8e330512009-04-04 13:20:55 +0000326* Add :mod:`pydoc` help topics for symbols so that ``help('@')``
327 works as expected in the interactive environment.
328
329 (Contributed by David Laban; :issue:`4739`.)
330
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000331* The :mod:`unittest` module now supports skipping individual tests or classes
332 of tests. And it supports marking a test as a expected failure, a test that
Raymond Hettinger8daab402009-04-04 13:01:19 +0000333 is known to be broken, but shouldn't be counted as a failure on a
Raymond Hettinger8d97ccb2009-04-06 17:55:05 +0000334 TestResult::
335
336 class TestGizmo(unittest.TestCase):
337
338 @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
339 def test_gizmo_on_windows(self):
340 ...
341
342 @unittest.expectedFailure
343 def test_gimzo_without_required_library(self):
344 ...
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000345
Raymond Hettinger55fc9ce2009-04-14 20:45:17 +0000346 Also, tests for exceptions have been builtout to work with context managers
347 using the :keyword:`with` statement::
Raymond Hettinger35a88362009-04-09 00:08:24 +0000348
349 def test_division_by_zero(self):
350 with self.assertRaises(ZeroDivisionError):
351 x / 0
352
353 In addition, several new assertion methods were added including
354 :func:`assertSetEqual`, :func:`assertDictEqual`,
355 :func:`assertDictContainsSubset`, :func:`assertListEqual`,
356 :func:`assertTupleEqual`, :func:`assertSequenceEqual`,
357 :func:`assertRaisesRegexp`, :func:`assertIsNone`,
Michael Foord5859b862009-04-25 20:47:43 +0000358 and :func:`assertIsNotNone`.
Raymond Hettinger35a88362009-04-09 00:08:24 +0000359
360 (Contributed by Benjamin Peterson and Antoine Pitrou.)
361
Raymond Hettingerbe3a8212009-04-09 00:18:29 +0000362* The :mod:`io` module has three new constants for the :meth:`seek`
Raymond Hettinger35a88362009-04-09 00:08:24 +0000363 method :data:`SEEK_SET`, :data:`SEEK_CUR`, and :data:`SEEK_END`.
364
365* The :attr:`sys.version_info` tuple is now a named tuple::
366
367 >>> sys.version_info
368 sys.version_info(major=3, minor=1, micro=0, releaselevel='alpha', serial=2)
369
370 (Contributed by Ross Light; :issue:`4285`.)
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000371
Raymond Hettinger9dd74762009-05-15 14:57:35 +0000372* A new module, :mod:`ipaddr` has been added to the standard library.
373 It provides classes to represent, verify and manipulate IPv4 and IPv6
374 host and network addresses.
375
Raymond Hettingerdaafea32009-05-15 15:21:33 +0000376 The :func:`ipaddr.IP` factory function creates an address object from
377 a string or integer representing the IP or the IP and prefix/netmask.
378 The objects provide a number of attributes for direct access to
379 components of the full address::
380
381 >>> addr = IP('2001:658:22A:CAFE:200::1/64')
382 >>> for attr in ['ip', 'ip_ext', 'ip_ext_full', 'network', 'network_ext',
383 ... 'hostmask', 'hostmask_ext', 'broadcast', 'broadcast_ext',
384 ... 'netmask', 'netmask_ext', 'prefixlen']:
385 ... print(attr, '=', getattr(addr, attr))
386 ...
387 ip = 42540616829182469433547762482097946625
388 ip_ext = 2001:658:22a:cafe:200::1
389 ip_ext_full = 2001:0658:022a:cafe:0200:0000:0000:0001
390 network = 42540616829182469433403647294022090752
391 network_ext = 2001:658:22a:cafe::
392 hostmask = 18446744073709551615
393 hostmask_ext = ::ffff:ffff:ffff:ffff
394 broadcast = 42540616829182469451850391367731642367
395 broadcast_ext = 2001:658:22a:cafe:ffff:ffff:ffff:ffff
396 netmask = 340282366920938463444927863358058659840
397 netmask_ext = 64
398 prefixlen = 64
399
400 Each address object supports a number of simple properties including:
401 ``is_private``, ``is_multicast``, ``is_loopback``, and ``is_link_local``.
402
403 Additionally, the address objects provide a sort order for IP addresses,
404 support for address ranges (stored as lists of addresses), and subnet
405 computations.
406
Raymond Hettinger9dd74762009-05-15 14:57:35 +0000407 (Contributed by Google, :issue:`3959`.)
408
Raymond Hettingerdaafea32009-05-15 15:21:33 +0000409* The :mod:`nntplib` and :mod:`imaplib` modules now support IPv6.
Raymond Hettinger9dd74762009-05-15 14:57:35 +0000410
411 (Contributed by Derek Morr; :issue:`1655` and :issue:`1664`.)
412
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000413* A new module, :mod:`importlib` was added. It provides a complete, portable,
Raymond Hettinger55fc9ce2009-04-14 20:45:17 +0000414 pure Python reference implementation of the :keyword:`import` statement and its
Benjamin Peterson3fa0fb42009-04-04 12:42:53 +0000415 counterpart, the :func:`__import__` function. It represents a substantial
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000416 step forward in documenting and defining the actions that take place during
417 imports.
418
419 (Contributed by Brett Cannon.)
Raymond Hettinger1f251a02009-04-04 10:47:35 +0000420
Antoine Pitroub5564522009-03-28 19:45:26 +0000421Optimizations
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000422=============
Antoine Pitroub5564522009-03-28 19:45:26 +0000423
424Major performance enhancements have been added:
425
426* The new I/O library (as defined in :pep:`3116`) was mostly written in
427 Python and quickly proved to be a problematic bottleneck in Python 3.0.
428 In Python 3.1, the I/O library has been entirely rewritten in C and is
429 2 to 20 times faster depending on the task at hand. The pure Python
430 version is still available for experimentation purposes through
431 the ``_pyio`` module.
432
433 (Contributed by Amaury Forgeot d'Arc and Antoine Pitrou.)
434
Raymond Hettinger8daab402009-04-04 13:01:19 +0000435* Added a heuristic so that tuples and dicts containing only untrackable objects
Raymond Hettingere7ec57d2009-04-04 11:08:48 +0000436 are not tracked by the garbage collector. This can reduce the size of
437 collections and therefore the garbage collection overhead on long-running
438 programs, depending on their particular use of datatypes.
439
440 (Contributed by Antoine Pitrou, :issue:`4688`.)
441
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000442* Enabling a configure option named ``--with-computed-gotos``
443 on compilers that support it (notably: gcc, SunPro, icc), the bytecode
444 evaluation loop is compiled with a new dispatch mechanism which gives
Raymond Hettinger2a027772009-04-04 12:46:57 +0000445 speedups of up to 20%, depending on the system, the compiler, and
446 the benchmark.
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000447
Raymond Hettinger2a027772009-04-04 12:46:57 +0000448 (Contributed by Antoine Pitrou along with a number of other participants,
449 :issue:`4753`).
Raymond Hettingerf41857e2009-04-04 11:59:00 +0000450
451* The decoding of UTF-8, UTF-16 and LATIN-1 is now two to four times
452 faster.
453
454 (Contributed by Antoine Pitrou and Amaury Forgeot d'Arc, :issue:`4868`.)
455
Raymond Hettingerbd3da6b2009-05-15 16:16:12 +0000456* The :mod:`json` module now has a C extension to substantially improve
Raymond Hettingerd48ed2e2009-05-14 22:48:19 +0000457 its performance. In addition, the API was modified so that json works
458 only with :class:`str`, not with :class:`bytes`. That change makes the
Raymond Hettingerbd3da6b2009-05-15 16:16:12 +0000459 module closely match the `JSON specification <http://json.org/>`_
Raymond Hettingerd48ed2e2009-05-14 22:48:19 +0000460 which is defined in terms of Unicode.
Raymond Hettinger9dd74762009-05-15 14:57:35 +0000461
Raymond Hettingerd48ed2e2009-05-14 22:48:19 +0000462 (Contributed by Bob Ippolito and converted to Py3.1 by Antoine Pitrou
463 and Benjamin Peterson; :issue:`4136`.)
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000464
Raymond Hettingerd48ed2e2009-05-14 22:48:19 +0000465* Unpickling now interns the attribute names of pickled objects. This saves
466 memory and allows pickles to be smaller.
Raymond Hettinger9dd74762009-05-15 14:57:35 +0000467
Raymond Hettingerd48ed2e2009-05-14 22:48:19 +0000468 (Contributed by Jake McGuire and Antoine Pitrou; :issue:`5084`.)
Raymond Hettinger35a88362009-04-09 00:08:24 +0000469
470Build and C API Changes
471=======================
472
473Changes to Python's build process and to the C API include:
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000474
475* Integers are now stored internally either in base 2**15 or in base
476 2**30, the base being determined at build time. Previously, they
477 were always stored in base 2**15. Using base 2**30 gives
478 significant performance improvements on 64-bit machines, but
479 benchmark results on 32-bit machines have been mixed. Therefore,
480 the default is to use base 2**30 on 64-bit machines and base 2**15
481 on 32-bit machines; on Unix, there's a new configure option
482 ``--enable-big-digits`` that can be used to override this default.
483
484 Apart from the performance improvements this change should be invisible to
485 end users, with one exception: for testing and debugging purposes there's a
Raymond Hettinger79d0b0e2009-04-07 07:11:00 +0000486 new :attr:`sys.int_info` that provides information about the
Raymond Hettingerc4f6d292009-04-04 12:35:58 +0000487 internal format, giving the number of bits per digit and the size in bytes
488 of the C type used to store each digit::
489
490 >>> import sys
491 >>> sys.int_info
492 sys.int_info(bits_per_digit=30, sizeof_digit=4)
493
494 (Contributed by Mark Dickinson; :issue:`4258`.)
Antoine Pitroub5564522009-03-28 19:45:26 +0000495
Raymond Hettinger35a88362009-04-09 00:08:24 +0000496* The :cfunc:`PyLong_AsUnsignedLongLong()` function now handles a negative
497 *pylong* by raising :exc:`OverflowError` instead of :exc:`TypeError`.
498
499 (Contributed by Mark Dickinson and Lisandro Dalcrin; :issue:`5175`.)
500
501* Deprecated :cfunc:`PyNumber_Int`. Use :cfunc:`PyNumber_Long` instead.
502
Raymond Hettingerbe3a8212009-04-09 00:18:29 +0000503 (Contributed by Mark Dickinson; :issue:`4910`.)
Raymond Hettinger1a6b73d2009-04-17 20:55:52 +0000504
Raymond Hettingerbd3da6b2009-05-15 16:16:12 +0000505* Added a new :cfunc:`PyOS_string_to_double` function to replace the
506 deprecated functions :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof`.
507
508 (Contributed by Mark Dickinson; :issue:`5914`.)
509
Raymond Hettinger4f20a742009-05-14 23:17:38 +0000510* Added :ctype:`PyCapsule` as a replacement for the :ctype:`PyCObject` API.
511 The principal difference is that the new type has a well defined interface
512 for passing typing safety information and a less complicated signature
Raymond Hettinger9dd74762009-05-15 14:57:35 +0000513 for calling a destructor. The old type had a problematic API and is now
514 deprecated.
Raymond Hettinger4f20a742009-05-14 23:17:38 +0000515
516 (Contributed by Larry Hastings; :issue:`5630`.)
517
Raymond Hettinger1a6b73d2009-04-17 20:55:52 +0000518Porting to Python 3.1
519=====================
520
521This section lists previously described changes and other bugfixes
522that may require changes to your code:
523
524* The new floating point string representations can break existing doctests.
525 For example::
526
527 def e():
528 '''Compute the base of natural logarithms.
529
530 >>> e()
531 2.7182818284590451
532
533 '''
534 return sum(1/math.factorial(x) for x in reversed(range(30)))
535
536 doctest.testmod()
537
538 **********************************************************************
539 Failed example:
540 e()
541 Expected:
542 2.7182818284590451
543 Got:
544 2.718281828459045
545 **********************************************************************