1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
|
****************************
What's New in Python 2.6
****************************
.. XXX add trademark info for Apple, Microsoft, SourceForge.
:Author: A.M. Kuchling
:Release: |release|
:Date: |today|
.. $Id: whatsnew26.tex 55746 2007-06-02 18:33:53Z neal.norwitz $
Rules for maintenance:
* Anyone can add text to this document. Do not spend very much time
on the wording of your changes, because your text will probably
get rewritten to some degree.
* The maintainer will go through Misc/NEWS periodically and add
changes; it's therefore more important to add your changes to
Misc/NEWS than to this file.
* This is not a complete list of every single change; completeness
is the purpose of Misc/NEWS. Some changes I consider too small
or esoteric to include. If such a change is added to the text,
I'll just remove it. (This is another reason you shouldn't spend
too much time on writing your addition.)
* If you want to draw your new text to the attention of the
maintainer, add 'XXX' to the beginning of the paragraph or
section.
* It's OK to just add a fragmentary note about a change. For
example: "XXX Describe the transmogrify() function added to the
socket module." The maintainer will research the change and
write the necessary text.
* You can comment out your additions if you like, but it's not
necessary (especially when a final release is some months away).
* Credit the author of a patch or bugfix. Just the name is
sufficient; the e-mail address isn't necessary.
* It's helpful to add the bug/patch number in an parenthetical
XXX Describe the transmogrify() function added to the socket
module.
(Contributed by P.Y. Developer; :issue:`12345`.)
This saves the maintainer the effort of going through the SVN logs
when researching a change.
This article explains the new features in Python 2.6. No release date for
Python 2.6 has been set; it will probably be released in mid 2008.
This article doesn't attempt to provide a complete specification of
the new features, but instead provides a convenient overview. For
full details, you should refer to the documentation for Python 2.6. If
you want to understand the complete implementation and design
rationale, refer to the PEP for a particular new feature. For smaller
changes, this edition of "What's New in Python" links to the bug/patch
item for each change whenever possible.
.. Compare with previous release in 2 - 3 sentences here.
add hyperlink when the documentation becomes available online.
.. ========================================================================
.. Large, PEP-level features and changes should be described here.
.. Should there be a new section here for 3k migration?
.. Or perhaps a more general section describing module changes/deprecation?
.. ========================================================================
Python 3.0
================
The development cycle for Python 2.6 also saw the release of the first
alphas of Python 3.0, and the development of 3.0 has influenced
a number of features in 2.6.
Python 3.0 is a far-ranging redesign of Python that breaks
compatibility with the 2.x series. This means that existing Python
code will need a certain amount of conversion in order to run on
Python 3.0. However, not all the changes in 3.0 necessarily break
compatibility. In cases where new features won't cause existing code
to break, they've been backported to 2.6 and are described in this
document in the appropriate place. Some of the 3.0-derived features
are:
* A :meth:`__complex__` method for converting objects to a complex number.
* Alternate syntax for catching exceptions: ``except TypeError as exc``.
* The addition of :func:`functools.reduce` as a synonym for the built-in
:func:`reduce` function.
A new command-line switch, :option:`-3`, enables warnings
about features that will be removed in Python 3.0. You can run code
with this switch to see how much work will be necessary to port
code to 3.0. The value of this switch is available
to Python code as the boolean variable :data:`sys.py3kwarning`,
and to C extension code as :cdata:`Py_Py3kWarningFlag`.
Python 3.0 adds several new built-in functions and change the
semantics of some existing built-ins. Entirely new functions such as
:func:`bin` have simply been added to Python 2.6, but existing
built-ins haven't been changed; instead, the :mod:`future_builtins`
module has versions with the new 3.0 semantics. Code written to be
compatible with 3.0 can do ``from future_builtins import hex, map``
as necessary.
.. seealso::
The 3xxx series of PEPs, which describes the development process for
Python 3.0 and various features that have been accepted, rejected,
or are still under consideration.
Development Changes
==================================================
While 2.6 was being developed, the Python development process
underwent two significant changes: the developer group
switched from SourceForge's issue tracker to a customized
Roundup installation, and the documentation was converted from
LaTeX to reStructuredText.
New Issue Tracker: Roundup
--------------------------------------------------
For a long time, the Python developers have been growing increasingly
annoyed by SourceForge's bug tracker. SourceForge's hosted solution
doesn't permit much customization; for example, it wasn't possible to
customize the life cycle of issues.
The infrastructure committee of the Python Software Foundation
therefore posted a call for issue trackers, asking volunteers to set
up different products and import some of the bugs and patches from
SourceForge. Four different trackers were examined: Atlassian's `Jira
<http://www.atlassian.com/software/jira/>`__,
`Launchpad <http://www.launchpad.net>`__,
`Roundup <http://roundup.sourceforge.net/>`__, and
`Trac <http://trac.edgewall.org/>`__.
The committee eventually settled on Jira
and Roundup as the two candidates. Jira is a commercial product that
offers a no-cost hosted instance to free-software projects; Roundup
is an open-source project that requires volunteers
to administer it and a server to host it.
After posting a call for volunteers, a new Roundup installation was
set up at http://bugs.python.org. One installation of Roundup can
host multiple trackers, and this server now also hosts issue trackers
for Jython and for the Python web site. It will surely find
other uses in the future. Where possible,
this edition of "What's New in Python" links to the bug/patch
item for each change.
Hosting is kindly provided by
`Upfront Systems <http://www.upfrontsystems.co.za/>`__
of Stellenbosch, South Africa. Martin von Loewis put a
lot of effort into importing existing bugs and patches from
SourceForge; his scripts for this import operation are at
http://svn.python.org/view/tracker/importer/.
.. seealso::
http://bugs.python.org
The Python bug tracker.
http://bugs.jython.org:
The Jython bug tracker.
http://roundup.sourceforge.net/
Roundup downloads and documentation.
New Documentation Format: reStructuredText Using Sphinx
-----------------------------------------------------------
Since the Python project's inception around 1989, the documentation
had been written using LaTeX. At that time, most documentation was
printed out for later study, not viewed online. LaTeX was widely used
because it provided attractive printed output while remaining
straightforward to write, once the basic rules of the markup have been
learned.
LaTeX is still used today for writing technical publications destined
for printing, but the landscape for programming tools has shifted. We
no longer print out reams of documentation; instead, we browse through
it online and HTML has become the most important format to support.
Unfortunately, converting LaTeX to HTML is fairly complicated, and
Fred L. Drake Jr., the Python documentation editor for many years,
spent a lot of time wrestling the conversion process into shape.
Occasionally people would suggest converting the documentation into
SGML or, later, XML, but performing a good conversion is a major task
and no one pursued the task to completion.
During the 2.6 development cycle, Georg Brandl put a substantial
effort into building a new toolchain for processing the documentation.
The resulting package is called Sphinx, and is available from
http://sphinx.pocoo.org/. The input format is reStructuredText, a
markup commonly used in the Python community that supports custom
extensions and directives. Sphinx concentrates on HTML output,
producing attractively styled and modern HTML, though printed output
is still supported through conversion to LaTeX. Sphinx is a
standalone package that can be used in documenting other projects.
.. seealso::
:ref:`documenting-index`
Describes how to write for Python's documentation.
`Sphinx <http://sphinx.pocoo.org/>`__
Documentation and code for the Sphinx toolchain.
`Docutils <http://docutils.sf.net>`__
The underlying reStructuredText parser and toolset.
PEP 343: The 'with' statement
=============================
The previous version, Python 2.5, added the ':keyword:`with`'
statement an optional feature, to be enabled by a ``from __future__
import with_statement`` directive. In 2.6 the statement no longer needs to
be specially enabled; this means that :keyword:`with` is now always a
keyword. The rest of this section is a copy of the corresponding
section from "What's New in Python 2.5" document; if you read
it back when Python 2.5 came out, you can skip the rest of this
section.
The ':keyword:`with`' statement clarifies code that previously would use
``try...finally`` blocks to ensure that clean-up code is executed. In this
section, I'll discuss the statement as it will commonly be used. In the next
section, I'll examine the implementation details and show how to write objects
for use with this statement.
The ':keyword:`with`' statement is a new control-flow structure whose basic
structure is::
with expression [as variable]:
with-block
The expression is evaluated, and it should result in an object that supports the
context management protocol (that is, has :meth:`__enter__` and :meth:`__exit__`
methods.
The object's :meth:`__enter__` is called before *with-block* is executed and
therefore can run set-up code. It also may return a value that is bound to the
name *variable*, if given. (Note carefully that *variable* is *not* assigned
the result of *expression*.)
After execution of the *with-block* is finished, the object's :meth:`__exit__`
method is called, even if the block raised an exception, and can therefore run
clean-up code.
Some standard Python objects now support the context management protocol and can
be used with the ':keyword:`with`' statement. File objects are one example::
with open('/etc/passwd', 'r') as f:
for line in f:
print line
... more processing code ...
After this statement has executed, the file object in *f* will have been
automatically closed, even if the :keyword:`for` loop raised an exception part-
way through the block.
.. note::
In this case, *f* is the same object created by :func:`open`, because
:meth:`file.__enter__` returns *self*.
The :mod:`threading` module's locks and condition variables also support the
':keyword:`with`' statement::
lock = threading.Lock()
with lock:
# Critical section of code
...
The lock is acquired before the block is executed and always released once the
block is complete.
The new :func:`localcontext` function in the :mod:`decimal` module makes it easy
to save and restore the current decimal context, which encapsulates the desired
precision and rounding characteristics for computations::
from decimal import Decimal, Context, localcontext
# Displays with default precision of 28 digits
v = Decimal('578')
print v.sqrt()
with localcontext(Context(prec=16)):
# All code in this block uses a precision of 16 digits.
# The original context is restored on exiting the block.
print v.sqrt()
.. _new-26-context-managers:
Writing Context Managers
------------------------
Under the hood, the ':keyword:`with`' statement is fairly complicated. Most
people will only use ':keyword:`with`' in company with existing objects and
don't need to know these details, so you can skip the rest of this section if
you like. Authors of new objects will need to understand the details of the
underlying implementation and should keep reading.
A high-level explanation of the context management protocol is:
* The expression is evaluated and should result in an object called a "context
manager". The context manager must have :meth:`__enter__` and :meth:`__exit__`
methods.
* The context manager's :meth:`__enter__` method is called. The value returned
is assigned to *VAR*. If no ``as VAR`` clause is present, the value is simply
discarded.
* The code in *BLOCK* is executed.
* If *BLOCK* raises an exception, the :meth:`__exit__(type, value, traceback)`
is called with the exception details, the same values returned by
:func:`sys.exc_info`. The method's return value controls whether the exception
is re-raised: any false value re-raises the exception, and ``True`` will result
in suppressing it. You'll only rarely want to suppress the exception, because
if you do the author of the code containing the ':keyword:`with`' statement will
never realize anything went wrong.
* If *BLOCK* didn't raise an exception, the :meth:`__exit__` method is still
called, but *type*, *value*, and *traceback* are all ``None``.
Let's think through an example. I won't present detailed code but will only
sketch the methods necessary for a database that supports transactions.
(For people unfamiliar with database terminology: a set of changes to the
database are grouped into a transaction. Transactions can be either committed,
meaning that all the changes are written into the database, or rolled back,
meaning that the changes are all discarded and the database is unchanged. See
any database textbook for more information.)
Let's assume there's an object representing a database connection. Our goal will
be to let the user write code like this::
db_connection = DatabaseConnection()
with db_connection as cursor:
cursor.execute('insert into ...')
cursor.execute('delete from ...')
# ... more operations ...
The transaction should be committed if the code in the block runs flawlessly or
rolled back if there's an exception. Here's the basic interface for
:class:`DatabaseConnection` that I'll assume::
class DatabaseConnection:
# Database interface
def cursor(self):
"Returns a cursor object and starts a new transaction"
def commit(self):
"Commits current transaction"
def rollback(self):
"Rolls back current transaction"
The :meth:`__enter__` method is pretty easy, having only to start a new
transaction. For this application the resulting cursor object would be a useful
result, so the method will return it. The user can then add ``as cursor`` to
their ':keyword:`with`' statement to bind the cursor to a variable name. ::
class DatabaseConnection:
...
def __enter__(self):
# Code to start a new transaction
cursor = self.cursor()
return cursor
The :meth:`__exit__` method is the most complicated because it's where most of
the work has to be done. The method has to check if an exception occurred. If
there was no exception, the transaction is committed. The transaction is rolled
back if there was an exception.
In the code below, execution will just fall off the end of the function,
returning the default value of ``None``. ``None`` is false, so the exception
will be re-raised automatically. If you wished, you could be more explicit and
add a :keyword:`return` statement at the marked location. ::
class DatabaseConnection:
...
def __exit__(self, type, value, tb):
if tb is None:
# No exception, so commit
self.commit()
else:
# Exception occurred, so rollback.
self.rollback()
# return False
.. _module-contextlib:
The contextlib module
---------------------
The new :mod:`contextlib` module provides some functions and a decorator that
are useful for writing objects for use with the ':keyword:`with`' statement.
The decorator is called :func:`contextmanager`, and lets you write a single
generator function instead of defining a new class. The generator should yield
exactly one value. The code up to the :keyword:`yield` will be executed as the
:meth:`__enter__` method, and the value yielded will be the method's return
value that will get bound to the variable in the ':keyword:`with`' statement's
:keyword:`as` clause, if any. The code after the :keyword:`yield` will be
executed in the :meth:`__exit__` method. Any exception raised in the block will
be raised by the :keyword:`yield` statement.
Our database example from the previous section could be written using this
decorator as::
from contextlib import contextmanager
@contextmanager
def db_transaction(connection):
cursor = connection.cursor()
try:
yield cursor
except:
connection.rollback()
raise
else:
connection.commit()
db = DatabaseConnection()
with db_transaction(db) as cursor:
...
The :mod:`contextlib` module also has a :func:`nested(mgr1, mgr2, ...)` function
that combines a number of context managers so you don't need to write nested
':keyword:`with`' statements. In this example, the single ':keyword:`with`'
statement both starts a database transaction and acquires a thread lock::
lock = threading.Lock()
with nested (db_transaction(db), lock) as (cursor, locked):
...
Finally, the :func:`closing(object)` function returns *object* so that it can be
bound to a variable, and calls ``object.close`` at the end of the block. ::
import urllib, sys
from contextlib import closing
with closing(urllib.urlopen('http://www.yahoo.com')) as f:
for line in f:
sys.stdout.write(line)
.. seealso::
:pep:`343` - The "with" statement
PEP written by Guido van Rossum and Nick Coghlan; implemented by Mike Bland,
Guido van Rossum, and Neal Norwitz. The PEP shows the code generated for a
':keyword:`with`' statement, which can be helpful in learning how the statement
works.
The documentation for the :mod:`contextlib` module.
.. ======================================================================
.. _pep-0366:
PEP 366: Explicit Relative Imports From a Main Module
============================================================
Python's :option:`-m` switch allows running a module as a script.
When you ran a module that was located inside a package, relative
imports didn't work correctly.
The fix in Python 2.6 adds a :attr:`__package__` attribute to modules.
When present, relative imports will be relative to the value of this
attribute instead of the :attr:`__name__` attribute. PEP 302-style
importers can then set :attr:`__package__`. The :mod:`runpy` module
that implements the :option:`-m` switch now does this, so relative imports
can now be used in scripts running from inside a package.
.. ======================================================================
.. ::
.. _pep-0370:
PEP 370: XXX
=====================================================
When you run Python, the module search page ``sys.modules`` usually
includes a directory whose path ends in ``"site-packages"``. This
directory is intended to hold locally-installed packages available to
all users on a machine or using a particular site installation.
Python 2.6 introduces a convention for user-specific site directories.
.. seealso::
:pep:`370` - XXX
PEP written by XXX; implemented by Christian Heimes.
.. ======================================================================
.. _pep-3101:
PEP 3101: Advanced String Formatting
=====================================================
In Python 3.0, the `%` operator is supplemented by a more powerful
string formatting method, :meth:`format`. Support for the
:meth:`format` method has been backported to Python 2.6.
In 2.6, both 8-bit and Unicode strings have a `.format()` method that
treats the string as a template and takes the arguments to be formatted.
The formatting template uses curly brackets (`{`, `}`) as special characters::
# Substitute positional argument 0 into the string.
"User ID: {0}".format("root") -> "User ID: root"
# Use the named keyword arguments
uid = 'root'
'User ID: {uid} Last seen: {last_login}'.format(uid='root',
last_login = '5 Mar 2008 07:20') ->
'User ID: root Last seen: 5 Mar 2008 07:20'
Curly brackets can be escaped by doubling them::
format("Empty dict: {{}}") -> "Empty dict: {}"
Field names can be integers indicating positional arguments, such as
``{0}``, ``{1}``, etc. or names of keyword arguments. You can also
supply compound field names that read attributes or access dictionary keys::
import sys
'Platform: {0.platform}\nPython version: {0.version}'.format(sys) ->
'Platform: darwin\n
Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41) \n
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
import mimetypes
'Content-type: {0[.mp4]}'.format(mimetypes.types_map) ->
'Content-type: video/mp4'
Note that when using dictionary-style notation such as ``[.mp4]``, you
don't need to put any quotation marks around the string; it will look
up the value using ``.mp4`` as the key. Strings beginning with a
number will be converted to an integer. You can't write more
complicated expressions inside a format string.
So far we've shown how to specify which field to substitute into the
resulting string. The precise formatting used is also controllable by
adding a colon followed by a format specifier. For example::
# Field 0: left justify, pad to 15 characters
# Field 1: right justify, pad to 6 characters
fmt = '{0:15} ${1:>6}'
fmt.format('Registration', 35) ->
'Registration $ 35'
fmt.format('Tutorial', 50) ->
'Tutorial $ 50'
fmt.format('Banquet', 125) ->
'Banquet $ 125'
Format specifiers can reference other fields through nesting::
fmt = '{0:{1}}'
fmt.format('Invoice #1234', 15) ->
'Invoice #1234 '
width = 35
fmt.format('Invoice #1234', width) ->
'Invoice #1234 '
The alignment of a field within the desired width can be specified:
================ ============================================
Character Effect
================ ============================================
< (default) Left-align
> Right-align
^ Center
= (For numeric types only) Pad after the sign.
================ ============================================
Format specifiers can also include a presentation type, which
controls how the value is formatted. For example, floating-point numbers
can be formatted as a general number or in exponential notation:
>>> '{0:g}'.format(3.75)
'3.75'
>>> '{0:e}'.format(3.75)
'3.750000e+00'
A variety of presentation types are available. Consult the 2.6
documentation for a complete list (XXX add link, once it's in the 2.6
docs), but here's a sample::
'b' - Binary. Outputs the number in base 2.
'c' - Character. Converts the integer to the corresponding
Unicode character before printing.
'd' - Decimal Integer. Outputs the number in base 10.
'o' - Octal format. Outputs the number in base 8.
'x' - Hex format. Outputs the number in base 16, using lower-
case letters for the digits above 9.
'e' - Exponent notation. Prints the number in scientific
notation using the letter 'e' to indicate the exponent.
'g' - General format. This prints the number as a fixed-point
number, unless the number is too large, in which case
it switches to 'e' exponent notation.
'n' - Number. This is the same as 'g', except that it uses the
current locale setting to insert the appropriate
number separator characters.
'%' - Percentage. Multiplies the number by 100 and displays
in fixed ('f') format, followed by a percent sign.
Classes and types can define a __format__ method to control how they're
formatted. It receives a single argument, the format specifier::
def __format__(self, format_spec):
if isinstance(format_spec, unicode):
return unicode(str(self))
else:
return str(self)
There's also a format() built-in that will format a single value. It calls
the type's :meth:`__format__` method with the provided specifier::
>>> format(75.6564, '.2f')
'75.66'
.. seealso::
:pep:`3101` - Advanced String Formatting
PEP written by Talin.
.. ======================================================================
.. _pep-3105:
PEP 3105: ``print`` As a Function
=====================================================
The ``print`` statement becomes the :func:`print` function in Python 3.0.
Making :func:`print` a function makes it easier to change
by doing 'def print(...)' or importing a new function from somewhere else.
Python 2.6 has a ``__future__`` import that removes ``print`` as language
syntax, letting you use the functional form instead. For example::
from __future__ import print_function
print('# of entries', len(dictionary), file=sys.stderr)
The signature of the new function is::
def print(*args, sep=' ', end='\n', file=None)
The parameters are:
* **args**: positional arguments whose values will be printed out.
* **sep**: the separator, which will be printed between arguments.
* **end**: the ending text, which will be printed after all of the
arguments have been output.
* **file**: the file object to which the output will be sent.
.. seealso::
:pep:`3105` - Make print a function
PEP written by Georg Brandl.
.. ======================================================================
.. _pep-3110:
PEP 3110: Exception-Handling Changes
=====================================================
One error that Python programmers occasionally make
is the following::
try:
...
except TypeError, ValueError:
...
The author is probably trying to catch both
:exc:`TypeError` and :exc:`ValueError` exceptions, but this code
actually does something different: it will catch
:exc:`TypeError` and bind the resulting exception object
to the local name ``"ValueError"``. The correct code
would have specified a tuple::
try:
...
except (TypeError, ValueError):
...
This error is possible because the use of the comma here is ambiguous:
does it indicate two different nodes in the parse tree, or a single
node that's a tuple.
Python 3.0 changes the syntax to make this unambiguous by replacing
the comma with the word "as". To catch an exception and store the
exception object in the variable ``exc``, you must write::
try:
...
except TypeError as exc:
...
Python 3.0 will only support the use of "as", and therefore interprets
the first example as catching two different exceptions. Python 2.6
supports both the comma and "as", so existing code will continue to
work.
.. seealso::
:pep:`3110` - Catching Exceptions in Python 3000
PEP written and implemented by Collin Winter.
.. ======================================================================
.. _pep-3112:
PEP 3112: Byte Literals
=====================================================
Python 3.0 adopts Unicode as the language's fundamental string type, and
denotes 8-bit literals differently, either as ``b'string'``
or using a :class:`bytes` constructor. For future compatibility,
Python 2.6 adds :class:`bytes` as a synonym for the :class:`str` type,
and it also supports the ``b''`` notation.
There's also a ``__future__`` import that causes all string literals
to become Unicode strings. This means that ``\u`` escape sequences
can be used to include Unicode characters::
from __future__ import unicode_literals
s = ('\u751f\u3080\u304e\u3000\u751f\u3054'
'\u3081\u3000\u751f\u305f\u307e\u3054')
print len(s) # 12 Unicode characters
.. seealso::
:pep:`3112` - Bytes literals in Python 3000
PEP written by Jason Orendorff; backported to 2.6 by Christian Heimes.
.. ======================================================================
.. _pep-3116:
PEP 3116: New I/O Library
=====================================================
Python's built-in file objects support a number of methods, but
file-like objects don't necessarily support all of them. Objects that
imitate files usually support :meth:`read` and :meth:`write`, but they
may not support :meth:`readline`. Python 3.0 introduces a layered I/O
library in the :mod:`io` module that separates buffering and
text-handling features from the fundamental read and write operations.
There are three levels of abstract base classes provided by
the :mod:`io` module:
* :class:`RawIOBase`: defines raw I/O operations: :meth:`read`,
:meth:`readinto`,
:meth:`write`, :meth:`seek`, :meth:`tell`, :meth:`truncate`,
and :meth:`close`.
Most of the methods of this class will often map to a single system call.
There are also :meth:`readable`, :meth:`writable`, and :meth:`seekable`
methods for determining what operations a given object will allow.
Python 3.0 has concrete implementations of this class for files and
sockets, but Python 2.6 hasn't restructured its file and socket objects
in this way.
.. XXX should 2.6 register them in io.py?
* :class:`BufferedIOBase`: is an abstract base class that
buffers data in memory to reduce the number of
system calls used, making I/O processing more efficient.
It supports all of the methods of :class:`RawIOBase`,
and adds a :attr:`raw` attribute holding the underlying raw object.
There are four concrete classes implementing this ABC:
:class:`BufferedWriter` and
:class:`BufferedReader` for objects that only support
writing or reading and don't support random access,
:class:`BufferedRandom` for objects that support the :meth:`seek` method
for random access,
and :class:`BufferedRWPair` for objects such as TTYs that have
both read and write operations that act upon unconnected streams of data.
* :class:`TextIOBase`: Provides functions for reading and writing
strings (remember, strings will be Unicode in Python 3.0),
and supporting universal newlines. :class:`TextIOBase` defines
the :meth:`readline` method and supports iteration upon
objects.
There are two concrete implementations. :class:`TextIOWrapper`
wraps a buffered I/O object, supporting all of the methods for
text I/O and adding a :attr:`buffer` attribute for access
to the underlying object. :class:`StringIO` simply buffers
everything in memory without ever writing anything to disk.
(In current 2.6 alpha releases, :class:`io.StringIO` is implemented in
pure Python, so it's pretty slow. You should therefore stick with the
existing :mod:`StringIO` module or :mod:`cStringIO` for now. At some
point Python 3.0's :mod:`io` module will be rewritten into C for speed,
and perhaps the C implementation will be backported to the 2.x releases.)
.. XXX check before final release: is io.py still written in Python?
In Python 2.6, the underlying implementations haven't been
restructured to build on top of the :mod:`io` module's classes. The
module is being provided to make it easier to write code that's
forward-compatible with 3.0, and to save developers the effort of writing
their own implementations of buffering and text I/O.
.. seealso::
:pep:`3116` - New I/O
PEP written by Daniel Stutzbach, Mike Verdone, and Guido van Rossum.
Code by Guido van Rossum, Georg Brandl, Walter Doerwald,
Jeremy Hylton, Martin von Loewis, Tony Lownds, and others.
.. ======================================================================
.. _pep-3118:
PEP 3118: Revised Buffer Protocol
=====================================================
The buffer protocol is a C-level API that lets Python types
exchange pointers into their internal representations. A
memory-mapped file can be viewed as a buffer of characters, for
example, and this lets another module such as :mod:`re`
treat memory-mapped files as a string of characters to be searched.
The primary users of the buffer protocol are numeric-processing
packages such as NumPy, which can expose the internal representation
of arrays so that callers can write data directly into an array instead
of going through a slower API. This PEP updates the buffer protocol in light of experience
from NumPy development, adding a number of new features
such as indicating the shape of an array,
locking memory .
The most important new C API function is
``PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)``, which
takes an object and a set of flags, and fills in the
``Py_buffer`` structure with information
about the object's memory representation. Objects
can use this operation to lock memory in place
while an external caller could be modifying the contents,
so there's a corresponding
``PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view)`` to
indicate that the external caller is done.
The **flags** argument to :cfunc:`PyObject_GetBuffer` specifies
constraints upon the memory returned. Some examples are:
* :const:`PyBUF_WRITABLE` indicates that the memory must be writable.
* :const:`PyBUF_LOCK` requests a read-only or exclusive lock on the memory.
* :const:`PyBUF_C_CONTIGUOUS` and :const:`PyBUF_F_CONTIGUOUS`
requests a C-contiguous (last dimension varies the fastest) or
Fortran-contiguous (first dimension varies the fastest) layout.
.. XXX this feature is not in 2.6 docs yet
.. seealso::
:pep:`3118` - Revising the buffer protocol
PEP written by Travis Oliphant and Carl Banks; implemented by
Travis Oliphant.
.. ======================================================================
.. _pep-3119:
PEP 3119: Abstract Base Classes
=====================================================
Some object-oriented languages such as Java support interfaces: declarations
that a class has a given set of methods or supports a given access protocol.
Abstract Base Classes (or ABCs) are an equivalent feature for Python. The ABC
support consists of an :mod:`abc` module containing a metaclass called
:class:`ABCMeta`, special handling
of this metaclass by the :func:`isinstance` and :func:`issubclass` built-ins,
and a collection of basic ABCs that the Python developers think will be widely
useful.
Let's say you have a particular class and wish to know whether it supports
dictionary-style access. The phrase "dictionary-style" is vague, however.
It probably means that accessing items with ``obj[1]`` works.
Does it imply that setting items with ``obj[2] = value`` works?
Or that the object will have :meth:`keys`, :meth:`values`, and :meth:`items`
methods? What about the iterative variants such as :meth:`iterkeys`? :meth:`copy`
and :meth:`update`? Iterating over the object with :func:`iter`?
Python 2.6 includes a number of different ABCs in the :mod:`collections`
module. :class:`Iterable` indicates that a class defines :meth:`__iter__`,
and :class:`Container` means the class supports ``x in y`` expressions
by defining a :meth:`__contains__` method. The basic dictionary interface of
getting items, setting items, and
:meth:`keys`, :meth:`values`, and :meth:`items`, is defined by the
:class:`MutableMapping` ABC.
You can derive your own classes from a particular ABC
to indicate they support that ABC's interface::
import collections
class Storage(collections.MutableMapping):
...
Alternatively, you could write the class without deriving from
the desired ABC and instead register the class by
calling the ABC's :meth:`register` method::
import collections
class Storage:
...
collections.MutableMapping.register(Storage)
For classes that you write, deriving from the ABC is probably clearer.
The :meth:`register` method is useful when you've written a new
ABC that can describe an existing type or class, or if you want
to declare that some third-party class implements an ABC.
For example, if you defined a :class:`PrintableType` ABC,
it's legal to do:
# Register Python's types
PrintableType.register(int)
PrintableType.register(float)
PrintableType.register(str)
Classes should obey the semantics specified by an ABC, but
Python can't check this; it's up to the class author to
understand the ABC's requirements and to implement the code accordingly.
To check whether an object supports a particular interface, you can
now write::
def func(d):
if not isinstance(d, collections.MutableMapping):
raise ValueError("Mapping object expected, not %r" % d)
(Don't feel that you must now begin writing lots of checks as in the
above example. Python has a strong tradition of duck-typing, where
explicit type-checking isn't done and code simply calls methods on
an object, trusting that those methods will be there and raising an
exception if they aren't. Be judicious in checking for ABCs
and only do it where it helps.)
You can write your own ABCs by using ``abc.ABCMeta`` as the
metaclass in a class definition::
from abc import ABCMeta
class Drawable():
__metaclass__ = ABCMeta
def draw(self, x, y, scale=1.0):
pass
def draw_doubled(self, x, y):
self.draw(x, y, scale=2.0)
class Square(Drawable):
def draw(self, x, y, scale):
...
In the :class:`Drawable` ABC above, the :meth:`draw_doubled` method
renders the object at twice its size and can be implemented in terms
of other methods described in :class:`Drawable`. Classes implementing
this ABC therefore don't need to provide their own implementation
of :meth:`draw_doubled`, though they can do so. An implementation
of :meth:`draw` is necessary, though; the ABC can't provide
a useful generic implementation. You
can apply the ``@abstractmethod`` decorator to methods such as
:meth:`draw` that must be implemented; Python will
then raise an exception for classes that
don't define the method::
class Drawable():
__metaclass__ = ABCMeta
@abstractmethod
def draw(self, x, y, scale):
pass
Note that the exception is only raised when you actually
try to create an instance of a subclass without the method::
>>> s=Square()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Square with abstract methods draw
>>>
Abstract data attributes can be declared using the ``@abstractproperty`` decorator::
@abstractproperty
def readonly(self):
return self._x
Subclasses must then define a :meth:`readonly` property
.. seealso::
:pep:`3119` - Introducing Abstract Base Classes
PEP written by Guido van Rossum and Talin.
Implemented by Guido van Rossum.
Backported to 2.6 by Benjamin Aranguren, with Alex Martelli.
.. ======================================================================
.. _pep-3127:
PEP 3127: Integer Literal Support and Syntax
=====================================================
Python 3.0 changes the syntax for octal (base-8) integer literals,
which are now prefixed by "0o" or "0O" instead of a leading zero, and
adds support for binary (base-2) integer literals, signalled by a "0b"
or "0B" prefix.
Python 2.6 doesn't drop support for a leading 0 signalling
an octal number, but it does add support for "0o" and "0b"::
>>> 0o21, 2*8 + 1
(17, 17)
>>> 0b101111
47
The :func:`oct` built-in still returns numbers
prefixed with a leading zero, and a new :func:`bin`
built-in returns the binary representation for a number::
>>> oct(42)
'052'
>>> bin(173)
'0b10101101'
The :func:`int` and :func:`long` built-ins will now accept the "0o"
and "0b" prefixes when base-8 or base-2 are requested, or when the
**base** argument is zero (meaning the base used is determined from
the string):
>>> int ('0o52', 0)
42
>>> int('1101', 2)
13
>>> int('0b1101', 2)
13
>>> int('0b1101', 0)
13
.. seealso::
:pep:`3127` - Integer Literal Support and Syntax
PEP written by Patrick Maupin; backported to 2.6 by
Eric Smith.
.. ======================================================================
.. _pep-3129:
PEP 3129: Class Decorators
=====================================================
Decorators have been extended from functions to classes. It's now legal to
write::
@foo
@bar
class A:
pass
This is equivalent to::
class A:
pass
A = foo(bar(A))
.. seealso::
:pep:`3129` - Class Decorators
PEP written by Collin Winter.
.. ======================================================================
.. _pep-3141:
PEP 3141: A Type Hierarchy for Numbers
=====================================================
In Python 3.0, several abstract base classes for numeric types,
inspired by Scheme's numeric tower, are being added.
This change was backported to 2.6 as the :mod:`numbers` module.
The most general ABC is :class:`Number`. It defines no operations at
all, and only exists to allow checking if an object is a number by
doing ``isinstance(obj, Number)``.
Numbers are further divided into :class:`Exact` and :class:`Inexact`.
Exact numbers can represent values precisely and operations never
round off the results or introduce tiny errors that may break the
commutativity and associativity properties; inexact numbers may
perform such rounding or introduce small errors. Integers, long
integers, and rational numbers are exact, while floating-point
and complex numbers are inexact.
:class:`Complex` is a subclass of :class:`Number`. Complex numbers
can undergo the basic operations of addition, subtraction,
multiplication, division, and exponentiation, and you can retrieve the
real and imaginary parts and obtain a number's conjugate. Python's built-in
complex type is an implementation of :class:`Complex`.
:class:`Real` further derives from :class:`Complex`, and adds
operations that only work on real numbers: :func:`floor`, :func:`trunc`,
rounding, taking the remainder mod N, floor division,
and comparisons.
:class:`Rational` numbers derive from :class:`Real`, have
:attr:`numerator` and :attr:`denominator` properties, and can be
converted to floats. Python 2.6 adds a simple rational-number class,
:class:`Fraction`, in the :mod:`fractions` module. (It's called
:class:`Fraction` instead of :class:`Rational` to avoid
a name clash with :class:`numbers.Rational`.)
:class:`Integral` numbers derive from :class:`Rational`, and
can be shifted left and right with ``<<`` and ``>>``,
combined using bitwise operations such as ``&`` and ``|``,
and can be used as array indexes and slice boundaries.
In Python 3.0, the PEP slightly redefines the existing built-ins
:func:`round`, :func:`math.floor`, :func:`math.ceil`, and adds a new
one, :func:`math.trunc`, that's been backported to Python 2.6.
:func:`math.trunc` rounds toward zero, returning the closest
:class:`Integral` that's between the function's argument and zero.
.. seealso::
:pep:`3141` - A Type Hierarchy for Numbers
PEP written by Jeffrey Yasskin.
`Scheme's numerical tower <http://www.gnu.org/software/guile/manual/html_node/Numerical-Tower.html#Numerical-Tower>`__, from the Guile manual.
`Scheme's number datatypes <http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2>`__ from the R5RS Scheme specification.
The :mod:`fractions` Module
--------------------------------------------------
To fill out the hierarchy of numeric types, a rational-number class
has been added as the :mod:`fractions` module. Rational numbers are
represented as a fraction, and can exactly represent
numbers such as two-thirds that floating-point numbers can only
approximate.
The :class:`Fraction` constructor takes two :class:`Integral` values
that will be the numerator and denominator of the resulting fraction. ::
>>> from fractions import Fraction
>>> a = Fraction(2, 3)
>>> b = Fraction(2, 5)
>>> float(a), float(b)
(0.66666666666666663, 0.40000000000000002)
>>> a+b
Fraction(16, 15)
>>> a/b
Fraction(5, 3)
To help in converting floating-point numbers to rationals,
the float type now has a :meth:`as_integer_ratio()` method that returns
the numerator and denominator for a fraction that evaluates to the same
floating-point value::
>>> (2.5) .as_integer_ratio()
(5, 2)
>>> (3.1415) .as_integer_ratio()
(7074029114692207L, 2251799813685248L)
>>> (1./3) .as_integer_ratio()
(6004799503160661L, 18014398509481984L)
Note that values that can only be approximated by floating-point
numbers, such as 1./3, are not simplified to the number being
approximated; the fraction attempts to match the floating-point value
**exactly**.
The :mod:`fractions` module is based upon an implementation by Sjoerd
Mullender that was in Python's :file:`Demo/classes/` directory for a
long time. This implementation was significantly updated by Jeffrey
Yasskin.
Other Language Changes
======================
Here are all of the changes that Python 2.6 makes to the core Python language.
* When calling a function using the ``**`` syntax to provide keyword
arguments, you are no longer required to use a Python dictionary;
any mapping will now work::
>>> def f(**kw):
... print sorted(kw)
...
>>> ud=UserDict.UserDict()
>>> ud['a'] = 1
>>> ud['b'] = 'string'
>>> f(**ud)
['a', 'b']
(Contributed by Alexander Belopolsky; :issue:`1686487`.)
* Tuples now have an :meth:`index` method matching the list type's
:meth:`index` method::
>>> t = (0,1,2,3,4)
>>> t.index(3)
3
* The built-in types now have improved support for extended slicing syntax,
where various combinations of ``(start, stop, step)`` are supplied.
Previously, the support was partial and certain corner cases wouldn't work.
(Implemented by Thomas Wouters.)
.. Revision 57619
* Properties now have three attributes, :attr:`getter`,
:attr:`setter` and :attr:`deleter`, that are useful shortcuts for
adding or modifying a getter, setter or deleter function to an
existing property. You would use them like this::
class C(object):
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
class D(C):
@C.x.getter
def x(self):
return self._x * 2
@x.setter
def x(self, value):
self._x = value / 2
* C functions and methods that use
:cfunc:`PyComplex_AsCComplex` will now accept arguments that
have a :meth:`__complex__` method. In particular, the functions in the
:mod:`cmath` module will now accept objects with this method.
This is a backport of a Python 3.0 change.
(Contributed by Mark Dickinson; :issue:`1675423`.)
A numerical nicety: when creating a complex number from two floats
on systems that support signed zeros (-0 and +0), the
:func:`complex` constructor will now preserve the sign
of the zero. (:issue:`1507`)
* More floating-point features were also added. The :func:`float` function
will now turn the strings ``+nan`` and ``-nan`` into the corresponding
IEEE 754 Not A Number values, and ``+inf`` and ``-inf`` into
positive or negative infinity. This works on any platform with
IEEE 754 semantics. (Contributed by Christian Heimes; :issue:`1635`.)
Other functions in the :mod:`math` module, :func:`isinf` and
:func:`isnan`, return true if their floating-point argument is
infinite or Not A Number. (:issue:`1640`)
* The :mod:`math` module has seven new functions, and the existing
functions have been improved to give more consistent behaviour
across platforms, especially with respect to handling of
floating-point exceptions and IEEE 754 special values.
The new functions are:
* :func:`isinf` and :func:`isnan` determine whether a given float is
a (positive or negative) infinity or a NaN (Not a Number),
respectively.
* ``copysign(x, y)`` copies the sign bit of an IEEE 754 number,
returning the absolute value of *x* combined with the sign bit of
*y*. For example, ``math.copysign(1, -0.0)`` returns -1.0.
(Contributed by Christian Heimes.)
* The inverse hyperbolic functions :func:`acosh`, :func:`asinh` and
:func:`atanh`.
* The function :func:`log1p`, returning the natural logarithm of
*1+x* (base *e*).
There's also a new :func:`trunc` function as a result of the
backport of `PEP 3141's type hierarchy for numbers <#pep-3141>`__.
The existing math functions have been modified to follow the
recommendations of the C99 standard with respect to special values
whenever possible. For example, ``sqrt(-1.)`` should now give a
:exc:`ValueError` across (nearly) all platforms, while
``sqrt(float('NaN'))`` should return a NaN on all IEEE 754
platforms. Where Annex 'F' of the C99 standard recommends signaling
'divide-by-zero' or 'invalid', Python will raise :exc:`ValueError`.
Where Annex 'F' of the C99 standard recommends signaling 'overflow',
Python will raise :exc:`OverflowError`. (See :issue:`711019`,
:issue:`1640`.)
(Contributed by Christian Heimes and Mark Dickinson.)
* Changes to the :class:`Exception` interface
as dictated by :pep:`352` continue to be made. For 2.6,
the :attr:`message` attribute is being deprecated in favor of the
:attr:`args` attribute.
* The :exc:`GeneratorExit` exception now subclasses
:exc:`BaseException` instead of :exc:`Exception`. This means
that an exception handler that does ``except Exception:``
will not inadvertently catch :exc:`GeneratorExit`.
(Contributed by Chad Austin; :issue:`1537`.)
* Generator objects now have a :attr:`gi_code` attribute that refers to
the original code object backing the generator.
(Contributed by Collin Winter; :issue:`1473257`.)
* The :func:`compile` built-in function now accepts keyword arguments
as well as positional parameters. (Contributed by Thomas Wouters;
:issue:`1444529`.)
* The :func:`complex` constructor now accepts strings containing
parenthesized complex numbers, letting ``complex(repr(cmplx))``
will now round-trip values. For example, ``complex('(3+4j)')``
now returns the value (3+4j). (:issue:`1491866`)
* The string :meth:`translate` method now accepts ``None`` as the
translation table parameter, which is treated as the identity
transformation. This makes it easier to carry out operations
that only delete characters. (Contributed by Bengt Richter;
:issue:`1193128`.)
* The built-in :func:`dir` function now checks for a :meth:`__dir__`
method on the objects it receives. This method must return a list
of strings containing the names of valid attributes for the object,
and lets the object control the value that :func:`dir` produces.
Objects that have :meth:`__getattr__` or :meth:`__getattribute__`
methods can use this to advertise pseudo-attributes they will honor.
(:issue:`1591665`)
* Instance method objects have new attributes for the object and function
comprising the method; the new synonym for :attr:`im_self` is
:attr:`__self__`, and :attr:`im_func` is also available as :attr:`__func__`.
The old names are still supported in Python 2.6; they're gone in 3.0.
* An obscure change: when you use the the :func:`locals` function inside a
:keyword:`class` statement, the resulting dictionary no longer returns free
variables. (Free variables, in this case, are variables referred to in the
:keyword:`class` statement that aren't attributes of the class.)
.. ======================================================================
Optimizations
-------------
* The :mod:`warnings` module has been rewritten in C. This makes
it possible to invoke warnings from the parser, and may also
make the interpreter's startup faster.
(Contributed by Neal Norwitz and Brett Cannon; :issue:`1631171`.)
* Type objects now have a cache of methods that can reduce
the amount of work required to find the correct method implementation
for a particular class; once cached, the interpreter doesn't need to
traverse base classes to figure out the right method to call.
The cache is cleared if a base class or the class itself is modified,
so the cache should remain correct even in the face of Python's dynamic
nature.
(Original optimization implemented by Armin Rigo, updated for
Python 2.6 by Kevin Jacobs; :issue:`1700288`.)
* All of the functions in the :mod:`struct` module have been rewritten in
C, thanks to work at the Need For Speed sprint.
(Contributed by Raymond Hettinger.)
* Internally, a bit is now set in type objects to indicate some of the standard
built-in types. This speeds up checking if an object is a subclass of one of
these types. (Contributed by Neal Norwitz.)
* Unicode strings now use faster code for detecting
whitespace and line breaks; this speeds up the :meth:`split` method
by about 25% and :meth:`splitlines` by 35%.
(Contributed by Antoine Pitrou.) Memory usage is reduced
by using pymalloc for the Unicode string's data.
* The ``with`` statement now stores the :meth:`__exit__` method on the stack,
producing a small speedup. (Implemented by Jeffrey Yasskin.)
* To reduce memory usage, the garbage collector will now clear internal
free lists when garbage-collecting the highest generation of objects.
This may return memory to the OS sooner.
The net result of the 2.6 optimizations is that Python 2.6 runs the pystone
benchmark around XX% faster than Python 2.5.
.. ======================================================================
.. _new-26-interactive:
Interactive Interpreter Changes
-------------------------------
Two command-line options have been reserved for use by other Python
implementations. The :option:`-J` switch has been reserved for use by
Jython for Jython-specific options, such as ones that are passed to
the underlying JVM. :option:`-X` has been reserved for options
specific to a particular implementation of Python such as CPython,
Jython, or IronPython. If either option is used with Python 2.6, the
interpreter will report that the option isn't currently used.
.. ======================================================================
New, Improved, and Deprecated Modules
=====================================
As usual, Python's standard library received a number of enhancements and bug
fixes. Here's a partial list of the most notable changes, sorted alphabetically
by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more
complete list of changes, or look through the CVS logs for all the details.
* The :mod:`bsddb.dbshelve` module now uses the highest pickling protocol
available, instead of restricting itself to protocol 1.
(Contributed by W. Barnes; :issue:`1551443`.)
* The :mod:`cmath` module underwent an extensive set of revisions,
thanks to Mark Dickinson and Christian Heimes, that added some new
features and greatly improved the accuracy of the computations.
Five new functions were added:
* :func:`polar` converts a complex number to polar form, returning
the modulus and argument of that complex number.
* :func:`rect` does the opposite, turning a (modulus, argument) pair
back into the corresponding complex number.
* :func:`phase` returns the phase or argument of a complex number.
* :func:`isnan` returns True if either
the real or imaginary part of its argument is a NaN.
* :func:`isinf` returns True if either the real or imaginary part of
its argument is infinite.
The revisions also improved the numerical soundness of the
:mod:`cmath` module. For all functions, the real and imaginary
parts of the results are accurate to within a few units of least
precision (ulps) whenever possible. See :issue:`1381` for the
details. The branch cuts for :func:`asinh`, :func:`atanh`: and
:func:`atan` have also been corrected.
The tests for the module have been greatly expanded; nearly 2000 new
test cases exercise the algebraic functions.
On IEEE 754 platforms, the :mod:`cmath` module now handles IEEE 754
special values and floating-point exceptions in a manner consistent
with Annex 'G' of the C99 standard.
* A new data type in the :mod:`collections` module: :class:`namedtuple(typename,
fieldnames)` is a factory function that creates subclasses of the standard tuple
whose fields are accessible by name as well as index. For example::
>>> var_type = collections.namedtuple('variable',
... 'id name type size')
# Names are separated by spaces or commas.
# 'id, name, type, size' would also work.
>>> var_type._fields
('id', 'name', 'type', 'size')
>>> var = var_type(1, 'frequency', 'int', 4)
>>> print var[0], var.id # Equivalent
1 1
>>> print var[2], var.type # Equivalent
int int
>>> var._asdict()
{'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'}
>>> v2 = var._replace(name='amplitude')
>>> v2
variable(id=1, name='amplitude', type='int', size=4)
Where the new :class:`namedtuple` type proved suitable, the standard
library has been modified to return them. For example,
the :meth:`Decimal.as_tuple` method now returns a named tuple with
:attr:`sign`, :attr:`digits`, and :attr:`exponent` fields.
(Contributed by Raymond Hettinger.)
* Another change to the :mod:`collections` module is that the
:class:`deque` type now supports an optional *maxlen* parameter;
if supplied, the deque's size will be restricted to no more
than *maxlen* items. Adding more items to a full deque causes
old items to be discarded.
::
>>> from collections import deque
>>> dq=deque(maxlen=3)
>>> dq
deque([], maxlen=3)
>>> dq.append(1) ; dq.append(2) ; dq.append(3)
>>> dq
deque([1, 2, 3], maxlen=3)
>>> dq.append(4)
>>> dq
deque([2, 3, 4], maxlen=3)
(Contributed by Raymond Hettinger.)
* The :mod:`ctypes` module now supports a :class:`c_bool` datatype
that represents the C99 ``bool`` type. (Contributed by David Remahl;
:issue:`1649190`.)
The :mod:`ctypes` string, buffer and array types also have improved
support for extended slicing syntax,
where various combinations of ``(start, stop, step)`` are supplied.
(Implemented by Thomas Wouters.)
.. Revision 57769
* A new method in the :mod:`curses` module: for a window, :meth:`chgat` changes
the display characters for a certain number of characters on a single line.
(Contributed by Fabian Kreutz.)
::
# Boldface text starting at y=0,x=21
# and affecting the rest of the line.
stdscr.chgat(0,21, curses.A_BOLD)
The :class:`Textbox` class in the :mod:`curses.textpad` module
now supports editing in insert mode as well as overwrite mode.
Insert mode is enabled by supplying a true value for the *insert_mode*
parameter when creating the :class:`Textbox` instance.
* The :mod:`datetime` module's :meth:`strftime` methods now support a
``%f`` format code that expands to the number of microseconds in the
object, zero-padded on
the left to six places. (Contributed by Skip Montanaro; :issue:`1158`.)
* The :mod:`decimal` module was updated to version 1.66 of
`the General Decimal Specification <http://www2.hursley.ibm.com/decimal/decarith.html>`__. New features
include some methods for some basic mathematical functions such as
:meth:`exp` and :meth:`log10`::
>>> Decimal(1).exp()
Decimal("2.718281828459045235360287471")
>>> Decimal("2.7182818").ln()
Decimal("0.9999999895305022877376682436")
>>> Decimal(1000).log10()
Decimal("3")
The :meth:`as_tuple` method of :class:`Decimal` objects now returns a
named tuple with :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields.
(Implemented by Facundo Batista and Mark Dickinson. Named tuple
support added by Raymond Hettinger.)
* The :mod:`difflib` module's :class:`SequenceMatcher` class
now returns named tuples representing matches.
In addition to behaving like tuples, the returned values
also have :attr:`a`, :attr:`b`, and :attr:`size` attributes.
(Contributed by Raymond Hettinger.)
* An optional ``timeout`` parameter was added to the
:class:`ftplib.FTP` class constructor as well as the :meth:`connect`
method, specifying a timeout measured in seconds. (Added by Facundo
Batista.) Also, the :class:`FTP` class's
:meth:`storbinary` and :meth:`storlines`
now take an optional *callback* parameter that will be called with
each block of data after the data has been sent.
(Contributed by Phil Schwartz; :issue:`1221598`.)
* The :func:`reduce` built-in function is also available in the
:mod:`functools` module. In Python 3.0, the built-in is dropped and it's
only available from :mod:`functools`; currently there are no plans
to drop the built-in in the 2.x series. (Patched by
Christian Heimes; :issue:`1739906`.)
* The :func:`glob.glob` function can now return Unicode filenames if
a Unicode path was used and Unicode filenames are matched within the
directory. (:issue:`1001604`)
* The :mod:`gopherlib` module has been removed.
* A new function in the :mod:`heapq` module: ``merge(iter1, iter2, ...)``
takes any number of iterables that return data *in sorted
order*, and returns a new iterator that returns the contents of all
the iterators, also in sorted order. For example::
heapq.merge([1, 3, 5, 9], [2, 8, 16]) ->
[1, 2, 3, 5, 8, 9, 16]
Another new function, ``heappushpop(heap, item)``,
pushes *item* onto *heap*, then pops off and returns the smallest item.
This is more efficient than making a call to :func:`heappush` and then
:func:`heappop`.
(Contributed by Raymond Hettinger.)
* An optional ``timeout`` parameter was added to the
:class:`httplib.HTTPConnection` and :class:`HTTPSConnection`
class constructors, specifying a timeout measured in seconds.
(Added by Facundo Batista.)
* Most of the :mod:`inspect` module's functions, such as
:func:`getmoduleinfo` and :func:`getargs`, now return named tuples.
In addition to behaving like tuples, the elements of the return value
can also be accessed as attributes.
(Contributed by Raymond Hettinger.)
Some new functions in the module include
:func:`isgenerator`, :func:`isgeneratorfunction`,
and :func:`isabstract`.
* The :mod:`itertools` module gained several new functions.
``izip_longest(iter1, iter2, ...[, fillvalue])`` makes tuples from
each of the elements; if some of the iterables are shorter than
others, the missing values are set to *fillvalue*. For example::
itertools.izip_longest([1,2,3], [1,2,3,4,5]) ->
[(1, 1), (2, 2), (3, 3), (None, 4), (None, 5)]
``product(iter1, iter2, ..., [repeat=N])`` returns the Cartesian product
of the supplied iterables, a set of tuples containing
every possible combination of the elements returned from each iterable. ::
itertools.product([1,2,3], [4,5,6]) ->
[(1, 4), (1, 5), (1, 6),
(2, 4), (2, 5), (2, 6),
(3, 4), (3, 5), (3, 6)]
The optional *repeat* keyword argument is used for taking the
product of an iterable or a set of iterables with themselves,
repeated *N* times. With a single iterable argument, *N*-tuples
are returned::
itertools.product([1,2], repeat=3)) ->
[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),
(2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]
With two iterables, *2N*-tuples are returned. ::
itertools(product([1,2], [3,4], repeat=2) ->
[(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4),
(1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4),
(2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4),
(2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)]
``combinations(iterable, r)`` returns sub-sequences of length *r* from
the elements of *iterable*. ::
itertools.combinations('123', 2) ->
[('1', '2'), ('1', '3'), ('2', '3')]
itertools.combinations('123', 3) ->
[('1', '2', '3')]
itertools.combinations('1234', 3) ->
[('1', '2', '3'), ('1', '2', '4'), ('1', '3', '4'),
('2', '3', '4')]
``permutations(iter[, r])`` returns all the permutations of length *r* of
the iterable's elements. If *r* is not specified, it will default to the
number of elements produced by the iterable. ::
itertools.permutations([1,2,3,4], 2) ->
[(1, 2), (1, 3), (1, 4),
(2, 1), (2, 3), (2, 4),
(3, 1), (3, 2), (3, 4),
(4, 1), (4, 2), (4, 3)]
``itertools.chain(*iterables)`` is an existing function in
:mod:`itertools` that gained a new constructor in Python 2.6.
``itertools.chain.from_iterable(iterable)`` takes a single
iterable that should return other iterables. :func:`chain` will
then return all the elements of the first iterable, then
all the elements of the second, and so on. ::
chain.from_iterable([[1,2,3], [4,5,6]]) ->
[1, 2, 3, 4, 5, 6]
(All contributed by Raymond Hettinger.)
* The :mod:`logging` module's :class:`FileHandler` class
and its subclasses :class:`WatchedFileHandler`, :class:`RotatingFileHandler`,
and :class:`TimedRotatingFileHandler` now
have an optional *delay* parameter to its constructor. If *delay*
is true, opening of the log file is deferred until the first
:meth:`emit` call is made. (Contributed by Vinay Sajip.)
* The :mod:`macfs` module has been removed. This in turn required the
:func:`macostools.touched` function to be removed because it depended on the
:mod:`macfs` module. (:issue:`1490190`)
* :class:`mmap` objects now have a :meth:`rfind` method that finds
a substring, beginning at the end of the string and searching
backwards. The :meth:`find` method
also gained an *end* parameter containing the index at which to stop
the forward search.
(Contributed by John Lenton.)
* (3.0-warning mode) The :mod:`new` module has been removed from
Python 3.0. Importing it therefore triggers a warning message.
* The :mod:`operator` module gained a
:func:`methodcaller` function that takes a name and an optional
set of arguments, returning a callable that will call
the named function on any arguments passed to it. For example::
>>> # Equivalent to lambda s: s.replace('old', 'new')
>>> replacer = operator.methodcaller('replace', 'old', 'new')
>>> replacer('old wine in old bottles')
'new wine in new bottles'
(Contributed by Georg Brandl, after a suggestion by Gregory Petrosyan.)
The :func:`attrgetter` function now accepts dotted names and performs
the corresponding attribute lookups::
>>> inst_name = operator.attrgetter('__class__.__name__')
>>> inst_name('')
'str'
>>> inst_name(help)
'_Helper'
(Contributed by Georg Brandl, after a suggestion by Barry Warsaw.)
* New functions in the :mod:`os` module include
``fchmod(fd, mode)``, ``fchown(fd, uid, gid)``,
and ``lchmod(path, mode)``, on operating systems that support these
functions. :func:`fchmod` and :func:`fchown` let you change the mode
and ownership of an opened file, and :func:`lchmod` changes the mode
of a symlink.
(Contributed by Georg Brandl and Christian Heimes.)
* The :func:`os.walk` function now has a ``followlinks`` parameter. If
set to True, it will follow symlinks pointing to directories and
visit the directory's contents. For backward compatibility, the
parameter's default value is false. Note that the function can fall
into an infinite recursion if there's a symlink that points to a
parent directory. (:issue:`1273829`)
* The ``os.environ`` object's :meth:`clear` method will now unset the
environment variables using :func:`os.unsetenv` in addition to clearing
the object's keys. (Contributed by Martin Horcicka; :issue:`1181`.)
* In the :mod:`os.path` module, the :func:`splitext` function
has been changed to not split on leading period characters.
This produces better results when operating on Unix's dot-files.
For example, ``os.path.splitext('.ipython')``
now returns ``('.ipython', '')`` instead of ``('', '.ipython')``.
(:issue:`115886`)
A new function, :func:`relpath(path, start)` returns a relative path
from the ``start`` path, if it's supplied, or from the current
working directory to the destination ``path``. (Contributed by
Richard Barran; :issue:`1339796`.)
On Windows, :func:`os.path.expandvars` will now expand environment variables
in the form "%var%", and "~user" will be expanded into the
user's home directory path. (Contributed by Josiah Carlson;
:issue:`957650`.)
* The Python debugger provided by the :mod:`pdb` module
gained a new command: "run" restarts the Python program being debugged,
and can optionally take new command-line arguments for the program.
(Contributed by Rocky Bernstein; :issue:`1393667`.)
The :func:`post_mortem` function, used to enter debugging of a
traceback, will now use the traceback returned by :func:`sys.exc_info`
if no traceback is supplied. (Contributed by Facundo Batista;
:issue:`1106316`.)
* The :mod:`pickletools` module now has an :func:`optimize` function
that takes a string containing a pickle and removes some unused
opcodes, returning a shorter pickle that contains the same data structure.
(Contributed by Raymond Hettinger.)
* A :func:`get_data` function was added to the :mod:`pkgutil`
module that returns the contents of resource files included
with an installed Python package. For example::
>>> import pkgutil
>>> pkgutil.get_data('test', 'exception_hierarchy.txt')
'BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StandardError
...'
>>>
(Contributed by Paul Moore; :issue:`2439`.)
* New functions in the :mod:`posix` module: :func:`chflags` and :func:`lchflags`
are wrappers for the corresponding system calls (where they're available).
Constants for the flag values are defined in the :mod:`stat` module; some
possible values include :const:`UF_IMMUTABLE` to signal the file may not be
changed and :const:`UF_APPEND` to indicate that data can only be appended to the
file. (Contributed by M. Levinson.)
``os.closerange(*low*, *high*)`` efficiently closes all file descriptors
from *low* to *high*, ignoring any errors and not including *high* itself.
This function is now used by the :mod:`subprocess` module to make starting
processes faster. (Contributed by Georg Brandl; :issue:`1663329`.)
* The :mod:`pyexpat` module's :class:`Parser` objects now allow setting
their :attr:`buffer_size` attribute to change the size of the buffer
used to hold character data.
(Contributed by Achim Gaedke; :issue:`1137`.)
* The :mod:`Queue` module now provides queue classes that retrieve entries
in different orders. The :class:`PriorityQueue` class stores
queued items in a heap and retrieves them in priority order,
and :class:`LifoQueue` retrieves the most recently added entries first,
meaning that it behaves like a stack.
(Contributed by Raymond Hettinger.)
* The :mod:`random` module's :class:`Random` objects can
now be pickled on a 32-bit system and unpickled on a 64-bit
system, and vice versa. Unfortunately, this change also means
that Python 2.6's :class:`Random` objects can't be unpickled correctly
on earlier versions of Python.
(Contributed by Shawn Ligocki; :issue:`1727780`.)
The new ``triangular(low, high, mode)`` function returns random
numbers following a triangular distribution. The returned values
are between *low* and *high*, not including *high* itself, and
with *mode* as the mode, the most frequently occurring value
in the distribution. (Contributed by Wladmir van der Laan and
Raymond Hettinger; :issue:`1681432`.)
* Long regular expression searches carried out by the :mod:`re`
module will now check for signals being delivered, so especially
long searches can now be interrupted.
(Contributed by Josh Hoyt and Ralf Schmitt; :issue:`846388`.)
* The :mod:`rgbimg` module has been removed.
* The :mod:`sched` module's :class:`scheduler` instances now
have a read-only :attr:`queue` attribute that returns the
contents of the scheduler's queue, represented as a list of
named tuples with the fields ``(time, priority, action, argument)``.
(Contributed by Raymond Hettinger; :issue:`1861`.)
* The :mod:`select` module now has wrapper functions
for the Linux :cfunc:`epoll` and BSD :cfunc:`kqueue` system calls.
Also, a :meth:`modify` method was added to the existing :class:`poll`
objects; ``pollobj.modify(fd, eventmask)`` takes a file descriptor
or file object and an event mask,
(Contributed by Christian Heimes; :issue:`1657`.)
* The :mod:`sets` module has been deprecated; it's better to
use the built-in :class:`set` and :class:`frozenset` types.
* Integrating signal handling with GUI handling event loops
like those used by Tkinter or GTk+ has long been a problem; most
software ends up polling, waking up every fraction of a second.
The :mod:`signal` module can now make this more efficient.
Calling ``signal.set_wakeup_fd(fd)`` sets a file descriptor
to be used; when a signal is received, a byte is written to that
file descriptor. There's also a C-level function,
:cfunc:`PySignal_SetWakeupFd`, for setting the descriptor.
Event loops will use this by opening a pipe to create two descriptors,
one for reading and one for writing. The writable descriptor
will be passed to :func:`set_wakeup_fd`, and the readable descriptor
will be added to the list of descriptors monitored by the event loop via
:cfunc:`select` or :cfunc:`poll`.
On receiving a signal, a byte will be written and the main event loop
will be woken up, without the need to poll.
(Contributed by Adam Olsen; :issue:`1583`.)
The :func:`siginterrupt` function is now available from Python code,
and allows changing whether signals can interrupt system calls or not.
(Contributed by Ralf Schmitt.)
The :func:`setitimer` and :func:`getitimer` functions have also been
added on systems that support these system calls. :func:`setitimer`
allows setting interval timers that will cause a signal to be
delivered to the process after a specified time, measured in
wall-clock time, consumed process time, or combined process+system
time. (Contributed by Guilherme Polo; :issue:`2240`.)
* The :mod:`smtplib` module now supports SMTP over SSL thanks to the
addition of the :class:`SMTP_SSL` class. This class supports an
interface identical to the existing :class:`SMTP` class. Both
class constructors also have an optional ``timeout`` parameter
that specifies a timeout for the initial connection attempt, measured in
seconds.
An implementation of the LMTP protocol (:rfc:`2033`) was also added to
the module. LMTP is used in place of SMTP when transferring e-mail
between agents that don't manage a mail queue.
(SMTP over SSL contributed by Monty Taylor; timeout parameter
added by Facundo Batista; LMTP implemented by Leif
Hedstrom; :issue:`957003`.)
* In the :mod:`smtplib` module, SMTP.starttls() now complies with :rfc:`3207`
and forgets any knowledge obtained from the server not obtained from
the TLS negotiation itself. (Patch contributed by Bill Fenner;
:issue:`829951`.)
* The :mod:`socket` module now supports TIPC (http://tipc.sf.net),
a high-performance non-IP-based protocol designed for use in clustered
environments. TIPC addresses are 4- or 5-tuples.
(Contributed by Alberto Bertogli; :issue:`1646`.)
A new function, :func:`create_connection`, takes an address
and connects to it using an optional timeout value, returning
the connected socket object.
* The base classes in the :mod:`SocketServer` module now support
calling a :meth:`handle_timeout` method after a span of inactivity
specified by the server's :attr:`timeout` attribute. (Contributed
by Michael Pomraning.) The :meth:`serve_forever` method
now takes an optional poll interval measured in seconds,
controlling how often the server will check for a shutdown request.
(Contributed by Pedro Werneck and Jeffrey Yasskin;
:issue:`742598`, :issue:`1193577`.)
* The :mod:`struct` module now supports the C99 :ctype:`_Bool` type,
using the format character ``'?'``.
(Contributed by David Remahl.)
* The :class:`Popen` objects provided by the :mod:`subprocess` module
now have :meth:`terminate`, :meth:`kill`, and :meth:`send_signal` methods.
On Windows, :meth:`send_signal` only supports the :const:`SIGTERM`
signal, and all these methods are aliases for the Win32 API function
:cfunc:`TerminateProcess`.
(Contributed by Christian Heimes.)
* A new variable in the :mod:`sys` module,
:attr:`float_info`, is an object
containing information about the platform's floating-point support
derived from the :file:`float.h` file. Attributes of this object
include
:attr:`mant_dig` (number of digits in the mantissa), :attr:`epsilon`
(smallest difference between 1.0 and the next largest value
representable), and several others. (Contributed by Christian Heimes;
:issue:`1534`.)
Another new variable, :attr:`dont_write_bytecode`, controls whether Python
writes any :file:`.pyc` or :file:`.pyo` files on importing a module.
If this variable is true, the compiled files are not written. The
variable is initially set on start-up by supplying the :option:`-B`
switch to the Python interpreter, or by setting the
:envvar:`PYTHONDONTWRITEBYTECODE` environment variable before
running the interpreter. Python code can subsequently
change the value of this variable to control whether bytecode files
are written or not.
(Contributed by Neal Norwitz and Georg Brandl.)
Information about the command-line arguments supplied to the Python
interpreter are available as attributes of a ``sys.flags`` named
tuple. For example, the :attr:`verbose` attribute is true if Python
was executed in verbose mode, :attr:`debug` is true in debugging mode, etc.
These attributes are all read-only.
(Contributed by Christian Heimes.)
It's now possible to determine the current profiler and tracer functions
by calling :func:`sys.getprofile` and :func:`sys.gettrace`.
(Contributed by Georg Brandl; :issue:`1648`.)
* The :mod:`tarfile` module now supports POSIX.1-2001 (pax) and
POSIX.1-1988 (ustar) format tarfiles, in addition to the GNU tar
format that was already supported. The default format
is GNU tar; specify the ``format`` parameter to open a file
using a different format::
tar = tarfile.open("output.tar", "w", format=tarfile.PAX_FORMAT)
The new ``errors`` parameter lets you specify an error handling
scheme for character conversions: the three standard ways Python can
handle errors ``'strict'``, ``'ignore'``, ``'replace'`` , or the
special value ``'utf-8'``, which replaces bad characters with their
UTF-8 representation. Character conversions occur because the PAX
format supports Unicode filenames, defaulting to UTF-8 encoding.
The :meth:`TarFile.add` method now accepts a ``exclude`` argument that's
a function that can be used to exclude certain filenames from
an archive.
The function must take a filename and return true if the file
should be excluded or false if it should be archived.
The function is applied to both the name initially passed to :meth:`add`
and to the names of files in recursively-added directories.
(All changes contributed by Lars Gustäbel).
* An optional ``timeout`` parameter was added to the
:class:`telnetlib.Telnet` class constructor, specifying a timeout
measured in seconds. (Added by Facundo Batista.)
* The :class:`tempfile.NamedTemporaryFile` class usually deletes
the temporary file it created when the file is closed. This
behaviour can now be changed by passing ``delete=False`` to the
constructor. (Contributed by Damien Miller; :issue:`1537850`.)
A new class, :class:`SpooledTemporaryFile`, behaves like
a temporary file but stores its data in memory until a maximum size is
exceeded. On reaching that limit, the contents will be written to
an on-disk temporary file. (Contributed by Dustin J. Mitchell.)
The :class:`NamedTemporaryFile` and :class:`SpooledTemporaryFile` classes
both work as context managers, so you can write
``with tempfile.NamedTemporaryFile() as tmp: ...``.
(Contributed by Alexander Belopolsky; :issue:`2021`.)
* The :mod:`test.test_support` module now contains a
:func:`EnvironmentVarGuard`
context manager that supports temporarily changing environment variables and
automatically restores them to their old values.
Another context manager, :class:`TransientResource`, can surround calls
to resources that may or may not be available; it will catch and
ignore a specified list of exceptions. For example,
a network test may ignore certain failures when connecting to an
external web site::
with test_support.TransientResource(IOError, errno=errno.ETIMEDOUT):
f = urllib.urlopen('https://sf.net')
...
(Contributed by Brett Cannon.)
* The :mod:`textwrap` module can now preserve existing whitespace
at the beginnings and ends of the newly-created lines
by specifying ``drop_whitespace=False``
as an argument::
>>> S = """This sentence has a bunch of extra whitespace."""
>>> print textwrap.fill(S, width=15)
This sentence
has a bunch
of extra
whitespace.
>>> print textwrap.fill(S, drop_whitespace=False, width=15)
This sentence
has a bunch
of extra
whitespace.
>>>
(Contributed by Dwayne Bailey; :issue:`1581073`.)
* The :mod:`timeit` module now accepts callables as well as strings
for the statement being timed and for the setup code.
Two convenience functions were added for creating
:class:`Timer` instances:
``repeat(stmt, setup, time, repeat, number)`` and
``timeit(stmt, setup, time, number)`` create an instance and call
the corresponding method. (Contributed by Erik Demaine;
:issue:`1533909`.)
* An optional ``timeout`` parameter was added to the
:func:`urllib.urlopen` function and the
:class:`urllib.ftpwrapper` class constructor, as well as the
:func:`urllib2.urlopen` function. The parameter specifies a timeout
measured in seconds. For example::
>>> u = urllib2.urlopen("http://slow.example.com", timeout=3)
Traceback (most recent call last):
...
urllib2.URLError: <urlopen error timed out>
>>>
(Added by Facundo Batista.)
* The XML-RPC classes :class:`SimpleXMLRPCServer` and :class:`DocXMLRPCServer`
classes can now be prevented from immediately opening and binding to
their socket by passing True as the ``bind_and_activate``
constructor parameter. This can be used to modify the instance's
:attr:`allow_reuse_address` attribute before calling the
:meth:`server_bind` and :meth:`server_activate` methods to
open the socket and begin listening for connections.
(Contributed by Peter Parente; :issue:`1599845`.)
:class:`SimpleXMLRPCServer` also has a :attr:`_send_traceback_header`
attribute; if true, the exception and formatted traceback are returned
as HTTP headers "X-Exception" and "X-Traceback". This feature is
for debugging purposes only and should not be used on production servers
because the tracebacks could possibly reveal passwords or other sensitive
information. (Contributed by Alan McIntyre as part of his
project for Google's Summer of Code 2007.)
* The :mod:`xmlrpclib` module no longer automatically converts
:class:`datetime.date` and :class:`datetime.time` to the
:class:`xmlrpclib.DateTime` type; the conversion semantics were
not necessarily correct for all applications. Code using
:mod:`xmlrpclib` should convert :class:`date` and :class:`time`
instances. (:issue:`1330538`) The code can also handle
dates before 1900. (Contributed by Ralf Schmitt; :issue:`2014`.)
* The :mod:`zipfile` module's :class:`ZipFile` class now has
:meth:`extract` and :meth:`extractall` methods that will unpack
a single file or all the files in the archive to the current directory, or
to a specified directory::
z = zipfile.ZipFile('python-251.zip')
# Unpack a single file, writing it relative to the /tmp directory.
z.extract('Python/sysmodule.c', '/tmp')
# Unpack all the files in the archive.
z.extractall()
(Contributed by Alan McIntyre; :issue:`467924`.)
.. ======================================================================
.. whole new modules get described in subsections here
The :mod:`json` module
----------------------
The new :mod:`json` module supports the encoding and decoding of Python types in
JSON (Javascript Object Notation). JSON is a lightweight interchange format
often used in web applications. For more information about JSON, see
http://www.json.org.
:mod:`json` comes with support for decoding and encoding most builtin Python
types. The following example encodes and decodes a dictionary::
>>> import json
>>> data = {"spam" : "foo", "parrot" : 42}
>>> in_json = json.dumps(data) # Encode the data
>>> in_json
'{"parrot": 42, "spam": "foo"}'
>>> json.loads(in_json) # Decode into a Python object
{"spam" : "foo", "parrot" : 42}
It is also possible to write your own decoders and encoders to support more
types. Pretty-printing of the JSON strings is also supported.
:mod:`json` (originally called simplejson) was written by Bob Ippolito.
Improved SSL Support
--------------------------------------------------
Bill Janssen made extensive improvements to Python 2.6's support for
the Secure Sockets Layer by adding a new module, :mod:`ssl`, on top of
the `OpenSSL <http://www.openssl.org/>`__ library. This new module
provides more control over the protocol negotiated, the X.509
certificates used, and has better support for writing SSL servers (as
opposed to clients) in Python. The existing SSL support in the
:mod:`socket` module hasn't been removed and continues to work,
though it will be removed in Python 3.0.
To use the new module, first you must create a TCP connection in the
usual way and then pass it to the :func:`ssl.wrap_socket` function.
It's possible to specify whether a certificate is required, and to
obtain certificate info by calling the :meth:`getpeercert` method.
.. seealso::
The documentation for the :mod:`ssl` module.
.. ======================================================================
plistlib: A Property-List Parser
--------------------------------------------------
A commonly-used format on MacOS X is the ``.plist`` format,
which stores basic data types (numbers, strings, lists,
and dictionaries) and serializes them into an XML-based format.
(It's a lot like the XML-RPC serialization of data types.)
Despite being primarily used on MacOS X, the format
has nothing Mac-specific about it and the Python implementation works
on any platform that Python supports, so the :mod:`plistlib` module
has been promoted to the standard library.
Using the module is simple::
import sys
import plistlib
import datetime
# Create data structure
data_struct = dict(lastAccessed=datetime.datetime.now(),
version=1,
categories=('Personal', 'Shared', 'Private'))
# Create string containing XML.
plist_str = plistlib.writePlistToString(data_struct)
new_struct = plistlib.readPlistFromString(plist_str)
print data_struct
print new_struct
# Write data structure to a file and read it back.
plistlib.writePlist(data_struct, '/tmp/customizations.plist')
new_struct = plistlib.readPlist('/tmp/customizations.plist')
# read/writePlist accepts file-like objects as well as paths.
plistlib.writePlist(data_struct, sys.stdout)
.. ======================================================================
Build and C API Changes
=======================
Changes to Python's build process and to the C API include:
* Python 2.6 can be built with Microsoft Visual Studio 2008.
See the :file:`PCbuild9` directory for the build files.
(Implemented by Christian Heimes.)
* Python now can only be compiled with C89 compilers (after 19
years!). This means that the Python source tree can now drop its
own implementations of :cfunc:`memmove` and :cfunc:`strerror`, which
are in the C89 standard library.
* The BerkeleyDB module now has a C API object, available as
``bsddb.db.api``. This object can be used by other C extensions
that wish to use the :mod:`bsddb` module for their own purposes.
(Contributed by Duncan Grisby; :issue:`1551895`.)
* The new buffer interface, previously described in
`the PEP 3118 section <#pep-3118-revised-buffer-protocol>`__,
adds :cfunc:`PyObject_GetBuffer` and :cfunc:`PyObject_ReleaseBuffer`,
as well as a few other functions.
* Python's use of the C stdio library is now thread-safe, or at least
as thread-safe as the underlying library is. A long-standing potential
bug occurred if one thread closed a file object while another thread
was reading from or writing to the object. In 2.6 file objects
have a reference count, manipulated by the
:cfunc:`PyFile_IncUseCount` and :cfunc:`PyFile_DecUseCount`
functions. File objects can't be closed unless the reference count
is zero. :cfunc:`PyFile_IncUseCount` should be called while the GIL
is still held, before carrying out an I/O operation using the
``FILE *`` pointer, and :cfunc:`PyFile_DecUseCount` should be called
immediately after the GIL is re-acquired.
(Contributed by Antoine Pitrou and Gregory P. Smith.)
* Importing modules simultaneously in two different threads no longer
deadlocks; it will now raise an :exc:`ImportError`. A new API
function, :cfunc:`PyImport_ImportModuleNoBlock`, will look for a
module in ``sys.modules`` first, then try to import it after
acquiring an import lock. If the import lock is held by another
thread, the :exc:`ImportError` is raised.
(Contributed by Christian Heimes.)
* Several functions return information about the platform's
floating-point support. :cfunc:`PyFloat_GetMax` returns
the maximum representable floating point value,
and :cfunc:`PyFloat_GetMin` returns the minimum
positive value. :cfunc:`PyFloat_GetInfo` returns a dictionary
containing more information from the :file:`float.h` file, such as
``"mant_dig"`` (number of digits in the mantissa), ``"epsilon"``
(smallest difference between 1.0 and the next largest value
representable), and several others.
(Contributed by Christian Heimes; :issue:`1534`.)
* Python's C API now includes two functions for case-insensitive string
comparisons, ``PyOS_stricmp(char*, char*)``
and ``PyOS_strnicmp(char*, char*, Py_ssize_t)``.
(Contributed by Christian Heimes; :issue:`1635`.)
* Many C extensions define their own little macro for adding
integers and strings to the module's dictionary in the
``init*`` function. Python 2.6 finally defines standard macros
for adding values to a module, :cmacro:`PyModule_AddStringMacro`
and :cmacro:`PyModule_AddIntMacro()`. (Contributed by
Christian Heimes.)
* Some macros were renamed in both 3.0 and 2.6 to make it clearer that
they are macros,
not functions. :cmacro:`Py_Size()` became :cmacro:`Py_SIZE()`,
:cmacro:`Py_Type()` became :cmacro:`Py_TYPE()`, and
:cmacro:`Py_Refcnt()` became :cmacro:`Py_REFCNT()`.
The mixed-case macros are still available
in Python 2.6 for backward compatibility.
(:issue:`1629`)
* Distutils now places C extensions it builds in a
different directory when running on a debug version of Python.
(Contributed by Collin Winter; :issue:`1530959`.)
* Several basic data types, such as integers and strings, maintain
internal free lists of objects that can be re-used. The data
structures for these free lists now follow a naming convention: the
variable is always named ``free_list``, the counter is always named
``numfree``, and a macro :cmacro:`Py<typename>_MAXFREELIST` is
always defined.
* A new Makefile target, "make check", prepares the Python source tree
for making a patch: it fixes trailing whitespace in all modified
``.py`` files, checks whether the documentation has been changed,
and reports whether the :file:`Misc/ACKS` and :file:`Misc/NEWS` files
have been updated.
(Contributed by Brett Cannon.)
Another new target, "make profile-opt", compiles a Python binary
using GCC's profile-guided optimization. It compiles Python with
profiling enabled, runs the test suite to obtain a set of profiling
results, and then compiles using these results for optimization.
(Contributed by Gregory P. Smith.)
.. ======================================================================
Port-Specific Changes: Windows
-----------------------------------
* The support for Windows 95, 98, ME and NT4 has been dropped.
Python 2.6 requires at least Windows 2000 SP4.
* The :mod:`msvcrt` module now supports
both the normal and wide char variants of the console I/O
API. The :func:`getwch` function reads a keypress and returns a Unicode
value, as does the :func:`getwche` function. The :func:`putwch` function
takes a Unicode character and writes it to the console.
(Contributed by Christian Heimes.)
* :func:`os.path.expandvars` will now expand environment variables
in the form "%var%", and "~user" will be expanded into the
user's home directory path. (Contributed by Josiah Carlson.)
* The :mod:`socket` module's socket objects now have an
:meth:`ioctl` method that provides a limited interface to the
:cfunc:`WSAIoctl` system interface.
* The :mod:`_winreg` module now has a function,
:func:`ExpandEnvironmentStrings`,
that expands environment variable references such as ``%NAME%``
in an input string. The handle objects provided by this
module now support the context protocol, so they can be used
in :keyword:`with` statements. (Contributed by Christian Heimes.)
:mod:`_winreg` also has better support for x64 systems,
exposing the :func:`DisableReflectionKey`, :func:`EnableReflectionKey`,
and :func:`QueryReflectionKey` functions, which enable and disable
registry reflection for 32-bit processes running on 64-bit systems.
(:issue:`1753245`)
* The new default compiler on Windows is Visual Studio 2008 (VS 9.0). The
build directories for Visual Studio 2003 (VS7.1) and 2005 (VS8.0)
were moved into the PC/ directory. The new PCbuild directory supports
cross compilation for X64, debug builds and Profile Guided Optimization
(PGO). PGO builds are roughly 10% faster than normal builds.
(Contributed by Christian Heimes with help from Amaury Forgeot d'Arc and
Martin von Loewis.)
.. ======================================================================
.. _section-other:
Other Changes and Fixes
=======================
As usual, there were a bunch of other improvements and bugfixes
scattered throughout the source tree. A search through the change
logs finds there were XXX patches applied and YYY bugs fixed between
Python 2.5 and 2.6. Both figures are likely to be underestimates.
Some of the more notable changes are:
* It's now possible to prevent Python from writing any :file:`.pyc`
or :file:`.pyo` files by either supplying the :option:`-B` switch
or setting the :envvar:`PYTHONDONTWRITEBYTECODE` environment variable
to any non-empty string when running the Python interpreter. These
are also used to set the :data:`sys.dont_write_bytecode` attribute;
Python code can change this variable to control whether bytecode
files are subsequently written.
(Contributed by Neal Norwitz and Georg Brandl.)
.. ======================================================================
Porting to Python 2.6
=====================
This section lists previously described changes and other bugfixes
that may require changes to your code:
* The :meth:`__init__` method of :class:`collections.deque`
now clears any existing contents of the deque
before adding elements from the iterable. This change makes the
behavior match that of ``list.__init__()``.
* The :class:`Decimal` constructor now accepts leading and trailing
whitespace when passed a string. Previously it would raise an
:exc:`InvalidOperation` exception. On the other hand, the
:meth:`create_decimal` method of :class:`Context` objects now
explicitly disallows extra whitespace, raising a
:exc:`ConversionSyntax` exception.
* Due to an implementation accident, if you passed a file path to
the built-in :func:`__import__` function, it would actually import
the specified file. This was never intended to work, however, and
the implementation now explicitly checks for this case and raises
an :exc:`ImportError`.
* C API: the :cfunc:`PyImport_Import` and :cfunc:`PyImport_ImportModule`
functions now default to absolute imports, not relative imports.
This will affect C extensions that import other modules.
* The :mod:`socket` module exception :exc:`socket.error` now inherits
from :exc:`IOError`. Previously it wasn't a subclass of
:exc:`StandardError` but now it is, through :exc:`IOError`.
(Implemented by Gregory P. Smith; :issue:`1706815`.)
* The :mod:`xmlrpclib` module no longer automatically converts
:class:`datetime.date` and :class:`datetime.time` to the
:class:`xmlrpclib.DateTime` type; the conversion semantics were
not necessarily correct for all applications. Code using
:mod:`xmlrpclib` should convert :class:`date` and :class:`time`
instances. (:issue:`1330538`)
* (3.0-warning mode) The :class:`Exception` class now warns
when accessed using slicing or index access; having
:class:`Exception` behave like a tuple is being phased out.
* (3.0-warning mode) inequality comparisons between two dictionaries
or two objects that don't implement comparison methods are reported
as warnings. ``dict1 == dict2`` still works, but ``dict1 < dict2``
is being phased out.
Comparisons between cells, which are an implementation detail of Python's
scoping rules, also cause warnings because such comparisons are forbidden
entirely in 3.0.
.. ======================================================================
.. _acks:
Acknowledgements
================
The author would like to thank the following people for offering suggestions,
corrections and assistance with various drafts of this article:
Georg Brandl, Jim Jewett.
|