Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | |
| 3 | \title{What's New in Python 1.6} |
| 4 | \release{0.01} |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 5 | \author{A.M. Kuchling and Moshe Zadka} |
| 6 | \authoraddress{\email{amk1@bigfoot.com}, \email{moshez@math.huji.ac.il} } |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 7 | \begin{document} |
| 8 | \maketitle\tableofcontents |
| 9 | |
| 10 | \section{Introduction} |
| 11 | |
| 12 | A new release of Python, version 1.6, will be released some time this |
| 13 | summer. Alpha versions are already available from |
| 14 | \url{http://www.python.org/1.6/}. This article talks about the |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 15 | exciting new features in 1.6, highlights some other useful changes, |
| 16 | and points out a few incompatible changes that may require rewriting |
| 17 | code. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 18 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 19 | Python's development never completely stops between releases, and a |
| 20 | steady flow of bug fixes and improvements are always being submitted. |
| 21 | A host of minor fixes, a few optimizations, additional docstrings, and |
| 22 | better error messages went into 1.6; to list them all would be |
| 23 | impossible, but they're certainly significant. Consult the |
| 24 | publicly-available CVS logs if you want to see the full list. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 25 | |
| 26 | % ====================================================================== |
| 27 | \section{Unicode} |
| 28 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 29 | The largest new feature in Python 1.6 is a new fundamental data type: |
| 30 | Unicode strings. Unicode uses 16-bit numbers to represent characters |
| 31 | instead of the 8-bit number used by ASCII, meaning that 65,536 |
| 32 | distinct characters can be supported. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 33 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 34 | The final interface for Unicode support was arrived at through |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 35 | countless often-stormy discussions on the python-dev mailing list, and |
| 36 | mostly implemented by Marc-Andr\'e Lemburg. A detailed explanation of |
| 37 | the interface is in the file |
| 38 | \file{Misc/unicode.txt} in the Python source distribution; it's also |
| 39 | available on the Web at |
| 40 | \url{http://starship.python.net/crew/lemburg/unicode-proposal.txt}. |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 41 | This article will simply cover the most significant points from the |
| 42 | full interface. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 43 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 44 | In Python source code, Unicode strings are written as |
| 45 | \code{u"string"}. Arbitrary Unicode characters can be written using a |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 46 | new escape sequence, \code{\e u\var{HHHH}}, where \var{HHHH} is a |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 47 | 4-digit hexadecimal number from 0000 to FFFF. The existing |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 48 | \code{\e x\var{HHHH}} escape sequence can also be used, and octal |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 49 | escapes can be used for characters up to U+01FF, which is represented |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 50 | by \code{\e 777}. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 51 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 52 | Unicode strings, just like regular strings, are an immutable sequence |
| 53 | type, so they can be indexed and sliced. They also have an |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 54 | \method{encode( \optional{\var{encoding}} )} method that returns an 8-bit |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 55 | string in the desired encoding. Encodings are named by strings, such |
| 56 | as \code{'ascii'}, \code{'utf-8'}, \code{'iso-8859-1'}, or whatever. |
| 57 | A codec API is defined for implementing and registering new encodings |
| 58 | that are then available throughout a Python program. If an encoding |
| 59 | isn't specified, the default encoding is always 7-bit ASCII. (XXX is |
| 60 | that the current default encoding?) |
| 61 | |
| 62 | Combining 8-bit and Unicode strings always coerces to Unicode, using |
| 63 | the default ASCII encoding; the result of \code{'a' + u'bc'} is |
| 64 | \code{'abc'}. |
| 65 | |
| 66 | New built-in functions have been added, and existing built-ins |
| 67 | modified to support Unicode: |
| 68 | |
| 69 | \begin{itemize} |
| 70 | \item \code{unichr(\var{ch})} returns a Unicode string 1 character |
| 71 | long, containing the character \var{ch}. |
| 72 | |
| 73 | \item \code{ord(\var{u})}, where \var{u} is a 1-character regular or Unicode string, returns the number of the character as an integer. |
| 74 | |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 75 | \item \code{unicode(\var{string}, \optional{\var{encoding},} |
| 76 | \optional{\var{errors}} ) } creates a Unicode string from an 8-bit |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 77 | string. \code{encoding} is a string naming the encoding to use. |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 78 | The \code{errors} parameter specifies the treatment of characters that |
| 79 | are invalid for the current encoding; passing \code{'strict'} as the |
| 80 | value causes an exception to be raised on any encoding error, while |
| 81 | \code{'ignore'} causes errors to be silently ignored and |
| 82 | \code{'replace'} uses U+FFFD, the official replacement character, in |
| 83 | case of any problems. |
| 84 | |
| 85 | \end{itemize} |
| 86 | |
| 87 | A new module, \module{unicodedata}, provides an interface to Unicode |
| 88 | character properties. For example, \code{unicodedata.category(u'A')} |
| 89 | returns the 2-character string 'Lu', the 'L' denoting it's a letter, |
| 90 | and 'u' meaning that it's uppercase. |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 91 | \code{u.bidirectional(u'\e x0660')} returns 'AN', meaning that U+0660 is |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 92 | an Arabic number. |
| 93 | |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 94 | The \module{codecs} module contains functions to look up existing encodings |
| 95 | and register new ones. Unless you want to implement a |
| 96 | new encoding, you'll most often use the |
| 97 | \function{codecs.lookup(\var{encoding})} function, which returns a |
| 98 | 4-element tuple: \code{(\var{encode_func}, |
| 99 | \var{decode_func}, \var{stream_reader}, \var{stream_writer})}. |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 100 | |
| 101 | \begin{itemize} |
| 102 | \item \var{encode_func} is a function that takes a Unicode string, and |
| 103 | returns a 2-tuple \code{(\var{string}, \var{length})}. \var{string} |
| 104 | is an 8-bit string containing a portion (perhaps all) of the Unicode |
| 105 | string converted into the given encoding, and \var{length} tells you how much of the Unicode string was converted. |
| 106 | |
| 107 | \item \var{decode_func} is the mirror of \var{encode_func}, |
| 108 | taking a Unicode string and |
| 109 | returns a 2-tuple \code{(\var{ustring}, \var{length})} containing a Unicode string |
| 110 | and \var{length} telling you how much of the string was consumed. |
| 111 | |
| 112 | \item \var{stream_reader} is a class that supports decoding input from |
| 113 | a stream. \var{stream_reader(\var{file_obj})} returns an object that |
| 114 | supports the \method{read()}, \method{readline()}, and |
| 115 | \method{readlines()} methods. These methods will all translate from |
| 116 | the given encoding and return Unicode strings. |
| 117 | |
| 118 | \item \var{stream_writer}, similarly, is a class that supports |
| 119 | encoding output to a stream. \var{stream_writer(\var{file_obj})} |
| 120 | returns an object that supports the \method{write()} and |
| 121 | \method{writelines()} methods. These methods expect Unicode strings, translating them to the given encoding on output. |
| 122 | \end{itemize} |
| 123 | |
| 124 | For example, the following code writes a Unicode string into a file, |
| 125 | encoding it as UTF-8: |
| 126 | |
| 127 | \begin{verbatim} |
| 128 | import codecs |
| 129 | |
| 130 | unistr = u'\u0660\u2000ab ...' |
| 131 | |
| 132 | (UTF8_encode, UTF8_decode, |
| 133 | UTF8_streamreader, UTF8_streamwriter) = codecs.lookup('UTF-8') |
| 134 | |
| 135 | output = UTF8_streamwriter( open( '/tmp/output', 'wb') ) |
| 136 | output.write( unistr ) |
| 137 | output.close() |
| 138 | \end{verbatim} |
| 139 | |
| 140 | The following code would then read UTF-8 input from the file: |
| 141 | |
| 142 | \begin{verbatim} |
| 143 | input = UTF8_streamread( open( '/tmp/output', 'rb') ) |
| 144 | print repr(input.read()) |
| 145 | input.close() |
| 146 | \end{verbatim} |
| 147 | |
| 148 | Unicode-aware regular expressions are available through the |
| 149 | \module{re} module, which has a new underlying implementation called |
| 150 | SRE written by Fredrik Lundh of Secret Labs AB. |
| 151 | |
| 152 | % Added -U command line option. With the option enabled the Python |
| 153 | % compiler interprets all "..." strings as u"..." (same with r"..." and |
| 154 | % ur"..."). (XXX Is this just for experimenting?) |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 155 | |
| 156 | % ====================================================================== |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 157 | \section{Distutils: Making Modules Easy to Install} |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 158 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 159 | Before Python 1.6, installing modules was a tedious affair -- there |
| 160 | was no way to figure out automatically where Python is installed, or |
| 161 | what compiler options to use for extension modules. Software authors |
| 162 | had to go through an ardous ritual of editing Makefiles and |
| 163 | configuration files, which only really work on Unix and leave Windows |
| 164 | and MacOS unsupported. Software users faced wildly differing |
| 165 | installation instructions |
| 166 | |
| 167 | The SIG for distribution utilities, shepherded by Greg Ward, has |
| 168 | created the Distutils, a system to make package installation much |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 169 | easier. They form the \module{distutils} package, a new part of |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 170 | Python's standard library. In the best case, installing a Python |
| 171 | module from source will require the same steps: first you simply mean |
| 172 | unpack the tarball or zip archive, and the run ``\code{python setup.py |
| 173 | install}''. The platform will be automatically detected, the compiler |
| 174 | will be recognized, C extension modules will be compiled, and the |
| 175 | distribution installed into the proper directory. Optional |
| 176 | command-line arguments provide more control over the installation |
| 177 | process, the distutils package offers many places to override defaults |
| 178 | -- separating the build from the install, building or installing in |
| 179 | non-default directories, and more. |
| 180 | |
| 181 | In order to use the Distutils, you need to write a \file{setup.py} |
| 182 | script. For the simple case, when the software contains only .py |
| 183 | files, a minimal \file{setup.py} can be just a few lines long: |
| 184 | |
| 185 | \begin{verbatim} |
| 186 | from distutils.core import setup |
| 187 | setup (name = "foo", version = "1.0", |
| 188 | py_modules = ["module1", "module2"]) |
| 189 | \end{verbatim} |
| 190 | |
| 191 | The \file{setup.py} file isn't much more complicated if the software |
| 192 | consists of a few packages: |
| 193 | |
| 194 | \begin{verbatim} |
| 195 | from distutils.core import setup |
| 196 | setup (name = "foo", version = "1.0", |
| 197 | packages = ["package", "package.subpackage"]) |
| 198 | \end{verbatim} |
| 199 | |
| 200 | A C extension can be the most complicated case; here's an example taken from |
| 201 | the PyXML package: |
| 202 | |
| 203 | |
| 204 | \begin{verbatim} |
| 205 | from distutils.core import setup, Extension |
| 206 | |
| 207 | expat_extension = Extension('xml.parsers.pyexpat', |
| 208 | define_macros = [('XML_NS', None)], |
| 209 | include_dirs = [ 'extensions/expat/xmltok', |
| 210 | 'extensions/expat/xmlparse' ], |
| 211 | sources = [ 'extensions/pyexpat.c', |
| 212 | 'extensions/expat/xmltok/xmltok.c', |
| 213 | 'extensions/expat/xmltok/xmlrole.c', |
| 214 | ] |
| 215 | ) |
| 216 | setup (name = "PyXML", version = "0.5.4", |
| 217 | ext_modules =[ expat_extension ] ) |
| 218 | |
| 219 | \end{verbatim} |
| 220 | |
| 221 | The Distutils can also take care of creating source and binary |
| 222 | distributions. The ``sdist'' command, run by ``\code{python setup.py |
| 223 | sdist}', builds a source distribution such as \file{foo-1.0.tar.gz}. |
| 224 | Adding new commands isn't difficult, and a ``bdist_rpm'' command has |
| 225 | already been contributed to create an RPM distribution for the |
| 226 | software. Commands to create Windows installer programs, Debian |
| 227 | packages, and Solaris .pkg files have been discussed and are in |
| 228 | various stages of development. |
| 229 | |
| 230 | All this is documented in a new manual, \textit{Distributing Python |
| 231 | Modules}. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 232 | |
| 233 | % ====================================================================== |
| 234 | \section{String Methods} |
| 235 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 236 | Until now string-manipulation functionality was in the \module{string} |
| 237 | Python module, which was usually a front-end for the \module{strop} |
| 238 | module written in C. The addition of Unicode posed a difficulty for |
| 239 | the \module{strop} module, because the functions would all need to be |
| 240 | rewritten in order to accept either 8-bit or Unicode strings. For |
| 241 | functions such as \function{string.replace()}, which takes 3 string |
| 242 | arguments, that means eight possible permutations, and correspondingly |
| 243 | complicated code. |
| 244 | |
| 245 | Instead, Python 1.6 pushes the problem onto the string type, making |
| 246 | string manipulation functionality available through methods on both |
| 247 | 8-bit strings and Unicode strings. |
| 248 | |
| 249 | \begin{verbatim} |
| 250 | >>> 'andrew'.capitalize() |
| 251 | 'Andrew' |
| 252 | >>> 'hostname'.replace('os', 'linux') |
| 253 | 'hlinuxtname' |
| 254 | >>> 'moshe'.find('sh') |
| 255 | 2 |
| 256 | \end{verbatim} |
| 257 | |
| 258 | One thing that hasn't changed, April Fools' jokes notwithstanding, is |
| 259 | that Python strings are immutable. Thus, the string methods return new |
| 260 | strings, and do not modify the string on which they operate. |
| 261 | |
| 262 | The old \module{string} module is still around for backwards |
| 263 | compatibility, but it mostly acts as a front-end to the new string |
| 264 | methods. |
| 265 | |
| 266 | Two methods which have no parallel in pre-1.6 versions, although they |
| 267 | did exist in JPython for quite some time, are \method{startswith()} |
| 268 | and \method{endswith}. \code{s.startswith(t)} is equivalent to \code{s[:len(t)] |
| 269 | == t}, while \code{s.endswith(t)} is equivalent to \code{s[-len(t):] == t}. |
| 270 | |
| 271 | (XXX what'll happen to join?) One other method which deserves special |
| 272 | mention is \method{join}. The \method{join} method of a list receives |
| 273 | one parameter, a sequence of strings, and is equivalent to the |
| 274 | \function{string.join} function from the old \module{string} module, |
| 275 | with the arguments reversed. In other words, \code{s.join(seq)} is |
| 276 | equivalent to the old \code{string.join(seq, s)}. |
| 277 | |
| 278 | Some list methods, such as \method{find}, \method{index}, |
| 279 | \method{count}, \method{rindex}, and \method{rfind} are now available |
| 280 | on strings, allowing some nice polymorphic code which can deal with |
| 281 | either lists or strings without changes. |
| 282 | |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 283 | % ====================================================================== |
| 284 | \section{Porting to 1.6} |
| 285 | |
| 286 | New Python releases try hard to be compatible with previous releases, |
| 287 | and the record has been pretty good. However, some changes are |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 288 | considered useful enough, often fixing initial design decisions that |
| 289 | turned to be actively mistaken, that breaking backward compatibility |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 290 | can't always be avoided. This section lists the changes in Python 1.6 |
| 291 | that may cause old Python code to break. |
| 292 | |
| 293 | The change which will probably break the most code is tightening up |
| 294 | the arguments accepted by some methods. Some methods would take |
| 295 | multiple arguments and treat them as a tuple, particularly various |
| 296 | list methods such as \method{.append()}, \method{.insert()}, |
| 297 | \method{remove()}, and \method{.count()}. |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 298 | (XXX did anyone ever call the last 2 methods with multiple args?) |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 299 | In earlier versions of Python, if \code{L} is a list, \code{L.append( |
| 300 | 1,2 )} appends the tuple \code{(1,2)} to the list. In Python 1.6 this |
| 301 | causes a \exception{TypeError} exception to be raised, with the |
| 302 | message: 'append requires exactly 1 argument; 2 given'. The fix is to |
| 303 | simply add an extra set of parentheses to pass both values as a tuple: |
| 304 | \code{L.append( (1,2) )}. |
| 305 | |
| 306 | The earlier versions of these methods were more forgiving because they |
| 307 | used an old function in Python's C interface to parse their arguments; |
| 308 | 1.6 modernizes them to use \function{PyArg_ParseTuple}, the current |
| 309 | argument parsing function, which provides more helpful error messages |
| 310 | and treats multi-argument calls as errors. If you absolutely must use |
| 311 | 1.6 but can't fix your code, you can edit \file{Objects/listobject.c} |
| 312 | and define the preprocessor symbol \code{NO_STRICT_LIST_APPEND} to |
| 313 | preserve the old behaviour; this isn't recommended. |
| 314 | |
| 315 | Some of the functions in the \module{socket} module are still |
| 316 | forgiving in this way. For example, \function{socket.connect( |
| 317 | ('hostname', 25) )} is the correct form, passing a tuple representing |
| 318 | an IP address, but |
| 319 | \function{socket.connect( 'hostname', 25 )} also |
| 320 | works. \function{socket.connect_ex()} and \function{socket.bind()} are |
| 321 | similarly easy-going. 1.6alpha1 tightened these functions up, but |
| 322 | because the documentation actually used the erroneous multiple |
| 323 | argument form, many people wrote code which will break. So for |
| 324 | the\module{socket} module, the documentation was fixed and the |
| 325 | multiple argument form is simply marked as deprecated; it'll be |
| 326 | removed in a future Python version. |
| 327 | |
| 328 | Some work has been done to make integers and long integers a bit more |
| 329 | interchangeable. In 1.5.2, large-file support was added for Solaris, |
| 330 | to allow reading files larger than 2Gb; this made the \method{tell()} |
| 331 | method of file objects return a long integer instead of a regular |
| 332 | integer. Some code would subtract two file offsets and attempt to use |
| 333 | the result to multiply a sequence or slice a string, but this raised a |
| 334 | \exception{TypeError}. In 1.6, long integers can be used to multiply |
| 335 | or slice a sequence, and it'll behave as you'd intuitively expect it to; |
| 336 | \code{3L * 'abc'} produces 'abcabcabc', and |
| 337 | \code{ (0,1,2,3)[2L:4L]} produces (2,3). Long integers can also be |
| 338 | used in various new places where previously only integers were |
| 339 | accepted, such as in the \method{seek()} method of file objects. |
| 340 | |
| 341 | The subtlest long integer change of all is that the \function{str()} |
| 342 | of a long integer no longer has a trailing 'L' character, though |
| 343 | \function{repr()} still includes it. The 'L' annoyed many people who |
| 344 | wanted to print long integers that looked just like regular integers, |
| 345 | since they had to go out of their way to chop off the character. This |
| 346 | is no longer a problem in 1.6, but code which assumes the 'L' is |
| 347 | there, and does \code{str(longval)[:-1]} will now lose the final |
| 348 | digit. |
| 349 | |
| 350 | Taking the \function{repr()} of a float now uses a different |
| 351 | formatting precision than \function{str()}. \function{repr()} uses |
| 352 | ``%.17g'' format string for C's \function{sprintf()}, while |
| 353 | \function{str()} uses ``%.12g'' as before. The effect is that |
| 354 | \function{repr()} may occasionally show more decimal places than |
| 355 | \function{str()}, for numbers |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 356 | XXX need example value here to demonstrate problem. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 357 | |
| 358 | |
| 359 | % ====================================================================== |
| 360 | \section{Core Changes} |
| 361 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 362 | Various minor changes have been made to Python's syntax and built-in |
| 363 | functions. None of the changes are very far-reaching, but they're |
| 364 | handy conveniences. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 365 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 366 | A change to syntax makes it more convenient to call a given function |
| 367 | with a tuple of arguments and/or a dictionary of keyword arguments. |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 368 | In Python 1.5 and earlier, you do this with the \function{apply()} |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 369 | built-in function: \code{apply(f, \var{args}, \var{kw})} calls the |
| 370 | function \function{f()} with the argument tuple \var{args} and the |
| 371 | keyword arguments in the dictionary \var{kw}. Thanks to a patch from |
| 372 | Greg Ewing, 1.6 adds \code{f(*\var{args}, **\var{kw})} as a shorter |
| 373 | and clearer way to achieve the same effect. This syntax is |
| 374 | symmetrical with the syntax for defining functions: |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 375 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 376 | \begin{verbatim} |
| 377 | def f(*args, **kw): |
| 378 | # args is a tuple of positional args, |
| 379 | # kw is a dictionary of keyword args |
| 380 | ... |
| 381 | \end{verbatim} |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 382 | |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 383 | A new format style is available when using the \code{\%} operator. |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 384 | '\%r' will insert the \function{repr()} of its argument. This was |
| 385 | also added from symmetry considerations, this time for symmetry with |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 386 | the existing '\%s' format style, which inserts the \function{str()} of |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 387 | its argument. For example, \code{'\%r \%s' \% ('abc', 'abc')} returns a |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 388 | string containing \verb|'abc' abc|. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 389 | |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 390 | The \function{int()} and \function{long()} functions now accept an |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 391 | optional ``base'' parameter when the first argument is a string. |
| 392 | \code{int('123', 10)} returns 123, while \code{int('123', 16)} returns |
| 393 | 291. \code{int(123, 16)} raises a \exception{TypeError} exception |
| 394 | with the message ``can't convert non-string with explicit base''. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 395 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 396 | Previously there was no way to implement a class that overrode |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 397 | Python's built-in \keyword{in} operator and implemented a custom |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 398 | version. \code{\var{obj} in \var{seq}} returns true if \var{obj} is |
| 399 | present in the sequence \var{seq}; Python computes this by simply |
| 400 | trying every index of the sequence until either \var{obj} is found or |
| 401 | an \exception{IndexError} is encountered. Moshe Zadka contributed a |
| 402 | patch which adds a \method{__contains__} magic method for providing a |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 403 | custom implementation for \keyword{in}. Additionally, new built-in |
| 404 | objects written in C can define what \keyword{in} means for them via a |
| 405 | new slot in the sequence protocol. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 406 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 407 | Earlier versions of Python used a recursive algorithm for deleting |
| 408 | objects. Deeply nested data structures could cause the interpreter to |
| 409 | fill up the C stack and crash; Christian Tismer rewrote the deletion |
| 410 | logic to fix this problem. On a related note, comparing recursive |
| 411 | objects recursed infinitely and crashed; Jeremy Hylton rewrote the |
| 412 | code to no longer crash, producing a useful result instead. For |
| 413 | example, after this code: |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 414 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 415 | \begin{verbatim} |
| 416 | a = [] |
| 417 | b = [] |
| 418 | a.append(a) |
| 419 | b.append(b) |
| 420 | \end{verbatim} |
| 421 | |
| 422 | The comparison \code{a==b} returns true, because the two recursive |
| 423 | data structures are isomorphic. |
| 424 | \footnote{See the thread ``trashcan and PR\#7'' in the April 2000 archives of the python-dev mailing list for the discussion leading up to this implementation, and some useful relevant links. |
| 425 | %http://www.python.org/pipermail/python-dev/2000-April/004834.html |
| 426 | } |
| 427 | |
| 428 | Work has been done on porting Python to 64-bit Windows on the Itanium |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 429 | processor, mostly by Trent Mick of ActiveState. (Confusingly, \code{sys.platform} is still \code{'win32'} on |
| 430 | Win64 because it seems that for ease of porting, MS Visual C++ treats code |
| 431 | as 32 bit. |
| 432 | ) PythonWin also supports Windows CE; see the Python CE page at |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 433 | \url{http://www.python.net/crew/mhammond/ce/} for more information. |
| 434 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 435 | An attempt has been made to alleviate one of Python's warts, the |
| 436 | often-confusing \exception{NameError} exception when code refers to a |
| 437 | local variable before the variable has been assigned a value. For |
| 438 | example, the following code raises an exception on the \keyword{print} |
| 439 | statement in both 1.5.2 and 1.6; in 1.5.2 a \exception{NameError} |
| 440 | exception is raised, while 1.6 raises \exception{UnboundLocalError}. |
| 441 | |
| 442 | \begin{verbatim} |
| 443 | def f(): |
| 444 | print "i=",i |
| 445 | i = i + 1 |
| 446 | f() |
| 447 | \end{verbatim} |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 448 | |
| 449 | A new variable holding more detailed version information has been |
| 450 | added to the \module{sys} module. \code{sys.version_info} is a tuple |
| 451 | \code{(\var{major}, \var{minor}, \var{micro}, \var{level}, |
| 452 | \var{serial})} For example, in 1.6a2 \code{sys.version_info} is |
| 453 | \code{(1, 6, 0, 'alpha', 2)}. \var{level} is a string such as |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 454 | \code{"alpha"}, \code{"beta"}, or \code{""} for a final release. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 455 | |
| 456 | % ====================================================================== |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 457 | \section{Extending/Embedding Changes} |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 458 | |
| 459 | Some of the changes are under the covers, and will only be apparent to |
| 460 | people writing C extension modules, or embedding a Python interpreter |
| 461 | in a larger application. If you aren't dealing with Python's C API, |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 462 | you can safely skip this section. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 463 | |
| 464 | Users of Jim Fulton's ExtensionClass module will be pleased to find |
| 465 | out that hooks have been added so that ExtensionClasses are now |
| 466 | supported by \function{isinstance()} and \function{issubclass()}. |
| 467 | This means you no longer have to remember to write code such as |
| 468 | \code{if type(obj) == myExtensionClass}, but can use the more natural |
| 469 | \code{if isinstance(obj, myExtensionClass)}. |
| 470 | |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame^] | 471 | The \file{Python/importdl.c} file, which was a mass of \#ifdefs to |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 472 | support dynamic loading on many different platforms, was cleaned up |
| 473 | are reorganized by Greg Stein. \file{importdl.c} is now quite small, |
| 474 | and platform-specific code has been moved into a bunch of |
| 475 | \file{Python/dynload_*.c} files. |
| 476 | |
| 477 | Vladimir Marangozov's long-awaited malloc restructuring was completed, |
| 478 | to make it easy to have the Python interpreter use a custom allocator |
| 479 | instead of C's standard \function{malloc()}. For documentation, read |
| 480 | the comments in \file{Include/mymalloc.h} and |
| 481 | \file{Include/objimpl.h}. For the lengthy discussions during which |
| 482 | the interface was hammered out, see the Web archives of the 'patches' |
| 483 | and 'python-dev' lists at python.org. |
| 484 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 485 | Recent versions of the GUSI (XXX what is GUSI?) |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 486 | development environment for MacOS support POSIX threads. Therefore, |
| 487 | POSIX threads are now supported on the Macintosh too. Threading |
| 488 | support using the user-space GNU pth library was also contributed. |
| 489 | |
| 490 | Threading support on Windows was enhanced, too. Windows supports |
| 491 | thread locks that use kernel objects only in case of contention; in |
| 492 | the common case when there's no contention, they use simpler functions |
| 493 | which are an order of magnitude faster. A threaded version of Python |
| 494 | 1.5.2 on NT is twice as slow as an unthreaded version; with the 1.6 |
| 495 | changes, the difference is only 10\%. These improvements were |
| 496 | contributed by Yakov Markovitch. |
| 497 | |
| 498 | % ====================================================================== |
| 499 | \section{Module changes} |
| 500 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 501 | Lots of improvements and bugfixes were made to Python's extensive |
| 502 | standard library; some of the affected modules include |
| 503 | \module{readline}, \module{ConfigParser}, \module{cgi}, |
| 504 | \module{calendar}, \module{posix}, \module{readline}, \module{xmllib}, |
| 505 | \module{aifc}, \module{chunk, wave}, \module{random}, \module{shelve}, |
| 506 | and \module{nntplib}. Consult the CVS logs for the exact |
| 507 | patch-by-patch details. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 508 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 509 | Brian Gallew contributed OpenSSL support for the \module{socket} |
| 510 | module. When compiling Python, you can edit \file{Modules/Setup} to |
| 511 | include SSL support. When enabled, an additional function |
| 512 | \function{socket.ssl(\var{socket}, \var{keyfile}, \var{certfile})}, |
| 513 | which takes a socket object and returns an SSL socket. When SSL |
| 514 | support is available, the \module{httplib} and \module{urllib} modules |
| 515 | will support ``https://'' URLs. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 516 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 517 | The \module{Tkinter} module now supports Tcl/Tk version 8.1, 8.2, or |
| 518 | 8.3, and support for the older 7.x versions has been dropped. The |
| 519 | Tkinter module also supports displaying Unicode strings in Tk |
| 520 | widgets. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 521 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 522 | The \module{curses} module has been greatly extended, starting from |
| 523 | Oliver Andrich's enhanced version, to provide many additional |
| 524 | functions from ncurses and SYSV curses, such as colour, alternative |
| 525 | character set support, pads, and other new features. This means the |
| 526 | module is no longer compatible with operating systems that only have |
| 527 | BSD curses, but there don't seem to be any currently maintained OSes |
| 528 | that fall into this category. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 529 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 530 | XXX re - changed to be a frontend to sre |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 531 | |
| 532 | % ====================================================================== |
| 533 | \section{New modules} |
| 534 | |
| 535 | winreg - Windows registry interface. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 536 | PyExpat - interface to Expat XML parser |
| 537 | robotparser - parse a robots.txt file (for writing web spiders) |
| 538 | linuxaudio - audio for Linux |
| 539 | mmap - treat a file as a memory buffer |
| 540 | filecmp - supersedes the old cmp.py and dircmp.py modules |
| 541 | tabnanny - check Python sources for tab-width dependance |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 542 | |
| 543 | % ====================================================================== |
| 544 | \section{IDLE Improvements} |
| 545 | |
| 546 | XXX IDLE -- complete overhaul; what are the changes? |
| 547 | |
| 548 | % ====================================================================== |
| 549 | \section{Deleted and Deprecated Modules} |
| 550 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 551 | XXX stdwin, others? |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 552 | |
| 553 | \end{document} |
| 554 | |