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} |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 4 | \release{0.02} |
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 | |
Andrew M. Kuchling | a5bbb00 | 2000-06-10 02:41:46 +0000 | [diff] [blame^] | 12 | {\large This is a draft document; please report inaccuracies and |
| 13 | omissions to the authors. \\ |
| 14 | XXX marks locations where fact-checking or rewriting is still needed. |
| 15 | } |
| 16 | |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 17 | A new release of Python, version 1.6, will be released some time this |
| 18 | summer. Alpha versions are already available from |
| 19 | \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] | 20 | exciting new features in 1.6, highlights some other useful changes, |
| 21 | and points out a few incompatible changes that may require rewriting |
| 22 | code. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 23 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 24 | Python's development never completely stops between releases, and a |
| 25 | steady flow of bug fixes and improvements are always being submitted. |
| 26 | A host of minor fixes, a few optimizations, additional docstrings, and |
| 27 | better error messages went into 1.6; to list them all would be |
| 28 | impossible, but they're certainly significant. Consult the |
| 29 | 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] | 30 | |
| 31 | % ====================================================================== |
| 32 | \section{Unicode} |
| 33 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 34 | The largest new feature in Python 1.6 is a new fundamental data type: |
| 35 | Unicode strings. Unicode uses 16-bit numbers to represent characters |
| 36 | instead of the 8-bit number used by ASCII, meaning that 65,536 |
| 37 | distinct characters can be supported. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 38 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 39 | The final interface for Unicode support was arrived at through |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 40 | countless often-stormy discussions on the python-dev mailing list, and |
| 41 | mostly implemented by Marc-Andr\'e Lemburg. A detailed explanation of |
| 42 | the interface is in the file |
| 43 | \file{Misc/unicode.txt} in the Python source distribution; it's also |
| 44 | available on the Web at |
| 45 | \url{http://starship.python.net/crew/lemburg/unicode-proposal.txt}. |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 46 | This article will simply cover the most significant points from the |
| 47 | full interface. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 48 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 49 | In Python source code, Unicode strings are written as |
| 50 | \code{u"string"}. Arbitrary Unicode characters can be written using a |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 51 | 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] | 52 | 4-digit hexadecimal number from 0000 to FFFF. The existing |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 53 | \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] | 54 | 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] | 55 | by \code{\e 777}. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 56 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 57 | Unicode strings, just like regular strings, are an immutable sequence |
| 58 | 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] | 59 | \method{encode( \optional{\var{encoding}} )} method that returns an 8-bit |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 60 | string in the desired encoding. Encodings are named by strings, such |
| 61 | as \code{'ascii'}, \code{'utf-8'}, \code{'iso-8859-1'}, or whatever. |
| 62 | A codec API is defined for implementing and registering new encodings |
| 63 | that are then available throughout a Python program. If an encoding |
| 64 | isn't specified, the default encoding is always 7-bit ASCII. (XXX is |
| 65 | that the current default encoding?) |
| 66 | |
| 67 | Combining 8-bit and Unicode strings always coerces to Unicode, using |
| 68 | the default ASCII encoding; the result of \code{'a' + u'bc'} is |
Andrew M. Kuchling | 7f6270d | 2000-06-09 02:48:18 +0000 | [diff] [blame] | 69 | \code{u'abc'}. |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 70 | |
| 71 | New built-in functions have been added, and existing built-ins |
| 72 | modified to support Unicode: |
| 73 | |
| 74 | \begin{itemize} |
| 75 | \item \code{unichr(\var{ch})} returns a Unicode string 1 character |
| 76 | long, containing the character \var{ch}. |
| 77 | |
| 78 | \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. |
| 79 | |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 80 | \item \code{unicode(\var{string}, \optional{\var{encoding},} |
| 81 | \optional{\var{errors}} ) } creates a Unicode string from an 8-bit |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 82 | string. \code{encoding} is a string naming the encoding to use. |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 83 | The \code{errors} parameter specifies the treatment of characters that |
| 84 | are invalid for the current encoding; passing \code{'strict'} as the |
| 85 | value causes an exception to be raised on any encoding error, while |
| 86 | \code{'ignore'} causes errors to be silently ignored and |
| 87 | \code{'replace'} uses U+FFFD, the official replacement character, in |
| 88 | case of any problems. |
| 89 | |
| 90 | \end{itemize} |
| 91 | |
| 92 | A new module, \module{unicodedata}, provides an interface to Unicode |
| 93 | character properties. For example, \code{unicodedata.category(u'A')} |
| 94 | returns the 2-character string 'Lu', the 'L' denoting it's a letter, |
| 95 | and 'u' meaning that it's uppercase. |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 96 | \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] | 97 | an Arabic number. |
| 98 | |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 99 | The \module{codecs} module contains functions to look up existing encodings |
| 100 | and register new ones. Unless you want to implement a |
| 101 | new encoding, you'll most often use the |
| 102 | \function{codecs.lookup(\var{encoding})} function, which returns a |
| 103 | 4-element tuple: \code{(\var{encode_func}, |
| 104 | \var{decode_func}, \var{stream_reader}, \var{stream_writer})}. |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 105 | |
| 106 | \begin{itemize} |
| 107 | \item \var{encode_func} is a function that takes a Unicode string, and |
| 108 | returns a 2-tuple \code{(\var{string}, \var{length})}. \var{string} |
| 109 | is an 8-bit string containing a portion (perhaps all) of the Unicode |
| 110 | string converted into the given encoding, and \var{length} tells you how much of the Unicode string was converted. |
| 111 | |
| 112 | \item \var{decode_func} is the mirror of \var{encode_func}, |
| 113 | taking a Unicode string and |
| 114 | returns a 2-tuple \code{(\var{ustring}, \var{length})} containing a Unicode string |
| 115 | and \var{length} telling you how much of the string was consumed. |
| 116 | |
| 117 | \item \var{stream_reader} is a class that supports decoding input from |
| 118 | a stream. \var{stream_reader(\var{file_obj})} returns an object that |
| 119 | supports the \method{read()}, \method{readline()}, and |
| 120 | \method{readlines()} methods. These methods will all translate from |
| 121 | the given encoding and return Unicode strings. |
| 122 | |
| 123 | \item \var{stream_writer}, similarly, is a class that supports |
| 124 | encoding output to a stream. \var{stream_writer(\var{file_obj})} |
| 125 | returns an object that supports the \method{write()} and |
| 126 | \method{writelines()} methods. These methods expect Unicode strings, translating them to the given encoding on output. |
| 127 | \end{itemize} |
| 128 | |
| 129 | For example, the following code writes a Unicode string into a file, |
| 130 | encoding it as UTF-8: |
| 131 | |
| 132 | \begin{verbatim} |
| 133 | import codecs |
| 134 | |
| 135 | unistr = u'\u0660\u2000ab ...' |
| 136 | |
| 137 | (UTF8_encode, UTF8_decode, |
| 138 | UTF8_streamreader, UTF8_streamwriter) = codecs.lookup('UTF-8') |
| 139 | |
| 140 | output = UTF8_streamwriter( open( '/tmp/output', 'wb') ) |
| 141 | output.write( unistr ) |
| 142 | output.close() |
| 143 | \end{verbatim} |
| 144 | |
| 145 | The following code would then read UTF-8 input from the file: |
| 146 | |
| 147 | \begin{verbatim} |
| 148 | input = UTF8_streamread( open( '/tmp/output', 'rb') ) |
| 149 | print repr(input.read()) |
| 150 | input.close() |
| 151 | \end{verbatim} |
| 152 | |
| 153 | Unicode-aware regular expressions are available through the |
| 154 | \module{re} module, which has a new underlying implementation called |
| 155 | SRE written by Fredrik Lundh of Secret Labs AB. |
| 156 | |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 157 | (XXX M.A. Lemburg added a -U command line option, which causes the |
| 158 | Python compiler to interpret all "..." strings as u"..." (same with |
| 159 | r"..." and ur"..."). Is this just for experimenting/testing, or is it |
| 160 | actually a new feature?) |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 161 | |
| 162 | % ====================================================================== |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 163 | \section{Distutils: Making Modules Easy to Install} |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 164 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 165 | Before Python 1.6, installing modules was a tedious affair -- there |
| 166 | was no way to figure out automatically where Python is installed, or |
| 167 | what compiler options to use for extension modules. Software authors |
| 168 | had to go through an ardous ritual of editing Makefiles and |
| 169 | configuration files, which only really work on Unix and leave Windows |
| 170 | and MacOS unsupported. Software users faced wildly differing |
| 171 | installation instructions |
| 172 | |
| 173 | The SIG for distribution utilities, shepherded by Greg Ward, has |
| 174 | created the Distutils, a system to make package installation much |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 175 | easier. They form the \module{distutils} package, a new part of |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 176 | Python's standard library. In the best case, installing a Python |
| 177 | module from source will require the same steps: first you simply mean |
| 178 | unpack the tarball or zip archive, and the run ``\code{python setup.py |
| 179 | install}''. The platform will be automatically detected, the compiler |
| 180 | will be recognized, C extension modules will be compiled, and the |
| 181 | distribution installed into the proper directory. Optional |
| 182 | command-line arguments provide more control over the installation |
| 183 | process, the distutils package offers many places to override defaults |
| 184 | -- separating the build from the install, building or installing in |
| 185 | non-default directories, and more. |
| 186 | |
| 187 | In order to use the Distutils, you need to write a \file{setup.py} |
| 188 | script. For the simple case, when the software contains only .py |
| 189 | files, a minimal \file{setup.py} can be just a few lines long: |
| 190 | |
| 191 | \begin{verbatim} |
| 192 | from distutils.core import setup |
| 193 | setup (name = "foo", version = "1.0", |
| 194 | py_modules = ["module1", "module2"]) |
| 195 | \end{verbatim} |
| 196 | |
| 197 | The \file{setup.py} file isn't much more complicated if the software |
| 198 | consists of a few packages: |
| 199 | |
| 200 | \begin{verbatim} |
| 201 | from distutils.core import setup |
| 202 | setup (name = "foo", version = "1.0", |
| 203 | packages = ["package", "package.subpackage"]) |
| 204 | \end{verbatim} |
| 205 | |
| 206 | A C extension can be the most complicated case; here's an example taken from |
| 207 | the PyXML package: |
| 208 | |
| 209 | |
| 210 | \begin{verbatim} |
| 211 | from distutils.core import setup, Extension |
| 212 | |
| 213 | expat_extension = Extension('xml.parsers.pyexpat', |
| 214 | define_macros = [('XML_NS', None)], |
| 215 | include_dirs = [ 'extensions/expat/xmltok', |
| 216 | 'extensions/expat/xmlparse' ], |
| 217 | sources = [ 'extensions/pyexpat.c', |
| 218 | 'extensions/expat/xmltok/xmltok.c', |
| 219 | 'extensions/expat/xmltok/xmlrole.c', |
| 220 | ] |
| 221 | ) |
| 222 | setup (name = "PyXML", version = "0.5.4", |
| 223 | ext_modules =[ expat_extension ] ) |
| 224 | |
| 225 | \end{verbatim} |
| 226 | |
| 227 | The Distutils can also take care of creating source and binary |
| 228 | distributions. The ``sdist'' command, run by ``\code{python setup.py |
| 229 | sdist}', builds a source distribution such as \file{foo-1.0.tar.gz}. |
| 230 | Adding new commands isn't difficult, and a ``bdist_rpm'' command has |
| 231 | already been contributed to create an RPM distribution for the |
| 232 | software. Commands to create Windows installer programs, Debian |
| 233 | packages, and Solaris .pkg files have been discussed and are in |
| 234 | various stages of development. |
| 235 | |
| 236 | All this is documented in a new manual, \textit{Distributing Python |
Andrew M. Kuchling | a5bbb00 | 2000-06-10 02:41:46 +0000 | [diff] [blame^] | 237 | Modules}, that joins the basic set of Python documentation. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 238 | |
| 239 | % ====================================================================== |
| 240 | \section{String Methods} |
| 241 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 242 | Until now string-manipulation functionality was in the \module{string} |
| 243 | Python module, which was usually a front-end for the \module{strop} |
| 244 | module written in C. The addition of Unicode posed a difficulty for |
| 245 | the \module{strop} module, because the functions would all need to be |
| 246 | rewritten in order to accept either 8-bit or Unicode strings. For |
| 247 | functions such as \function{string.replace()}, which takes 3 string |
| 248 | arguments, that means eight possible permutations, and correspondingly |
| 249 | complicated code. |
| 250 | |
| 251 | Instead, Python 1.6 pushes the problem onto the string type, making |
| 252 | string manipulation functionality available through methods on both |
| 253 | 8-bit strings and Unicode strings. |
| 254 | |
| 255 | \begin{verbatim} |
| 256 | >>> 'andrew'.capitalize() |
| 257 | 'Andrew' |
| 258 | >>> 'hostname'.replace('os', 'linux') |
| 259 | 'hlinuxtname' |
| 260 | >>> 'moshe'.find('sh') |
| 261 | 2 |
| 262 | \end{verbatim} |
| 263 | |
| 264 | One thing that hasn't changed, April Fools' jokes notwithstanding, is |
| 265 | that Python strings are immutable. Thus, the string methods return new |
| 266 | strings, and do not modify the string on which they operate. |
| 267 | |
| 268 | The old \module{string} module is still around for backwards |
| 269 | compatibility, but it mostly acts as a front-end to the new string |
| 270 | methods. |
| 271 | |
| 272 | Two methods which have no parallel in pre-1.6 versions, although they |
| 273 | did exist in JPython for quite some time, are \method{startswith()} |
| 274 | and \method{endswith}. \code{s.startswith(t)} is equivalent to \code{s[:len(t)] |
| 275 | == t}, while \code{s.endswith(t)} is equivalent to \code{s[-len(t):] == t}. |
| 276 | |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 277 | (XXX what'll happen to join? is this even worth mentioning?) One |
| 278 | other method which deserves special mention is \method{join}. The |
| 279 | \method{join} method of a string receives one parameter, a sequence of |
| 280 | strings, and is equivalent to the \function{string.join} function from |
| 281 | the old \module{string} module, with the arguments reversed. In other |
| 282 | words, \code{s.join(seq)} is equivalent to the old |
| 283 | \code{string.join(seq, s)}. |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 284 | |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 285 | % ====================================================================== |
| 286 | \section{Porting to 1.6} |
| 287 | |
| 288 | New Python releases try hard to be compatible with previous releases, |
| 289 | and the record has been pretty good. However, some changes are |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 290 | considered useful enough, often fixing initial design decisions that |
| 291 | turned to be actively mistaken, that breaking backward compatibility |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 292 | can't always be avoided. This section lists the changes in Python 1.6 |
| 293 | that may cause old Python code to break. |
| 294 | |
| 295 | The change which will probably break the most code is tightening up |
| 296 | the arguments accepted by some methods. Some methods would take |
| 297 | multiple arguments and treat them as a tuple, particularly various |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 298 | list methods such as \method{.append()} and \method{.insert()}. |
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 |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 318 | an IP address, but \function{socket.connect( 'hostname', 25 )} also |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 319 | works. \function{socket.connect_ex()} and \function{socket.bind()} are |
| 320 | similarly easy-going. 1.6alpha1 tightened these functions up, but |
| 321 | because the documentation actually used the erroneous multiple |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 322 | argument form, many people wrote code which would break with the |
| 323 | stricter checking. GvR backed out the changes in the face of public |
| 324 | reaction, so for the\module{socket} module, the documentation was |
| 325 | fixed and the multiple argument form is simply marked as deprecated; |
| 326 | it \emph{will} be tightened up again in a future Python version. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 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 |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 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 |
| 336 | to; \code{3L * 'abc'} produces 'abcabcabc', and \code{ |
| 337 | (0,1,2,3)[2L:4L]} produces (2,3). Long integers can also be used in |
| 338 | various new places where previously only integers were accepted, such |
| 339 | as in the \method{seek()} method of file objects. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 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 | a5bbb00 | 2000-06-10 02:41:46 +0000 | [diff] [blame^] | 356 | For example, the number 8.1 can't be represented exactly in binary, so |
| 357 | \code{repr(8.1)} is \code{'8.0999999999999996'}, while str(8.1) is |
| 358 | \code{'8.1'}. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 359 | |
Andrew M. Kuchling | a5bbb00 | 2000-06-10 02:41:46 +0000 | [diff] [blame^] | 360 | %The \code{-X} command-line option, which turns all standard exceptions |
| 361 | %into strings instead of classes, has been removed. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 362 | |
| 363 | % ====================================================================== |
| 364 | \section{Core Changes} |
| 365 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 366 | Various minor changes have been made to Python's syntax and built-in |
| 367 | functions. None of the changes are very far-reaching, but they're |
| 368 | handy conveniences. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 369 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 370 | A change to syntax makes it more convenient to call a given function |
| 371 | 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] | 372 | 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] | 373 | built-in function: \code{apply(f, \var{args}, \var{kw})} calls the |
| 374 | function \function{f()} with the argument tuple \var{args} and the |
| 375 | keyword arguments in the dictionary \var{kw}. Thanks to a patch from |
| 376 | Greg Ewing, 1.6 adds \code{f(*\var{args}, **\var{kw})} as a shorter |
| 377 | and clearer way to achieve the same effect. This syntax is |
| 378 | symmetrical with the syntax for defining functions: |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 379 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 380 | \begin{verbatim} |
| 381 | def f(*args, **kw): |
| 382 | # args is a tuple of positional args, |
| 383 | # kw is a dictionary of keyword args |
| 384 | ... |
| 385 | \end{verbatim} |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 386 | |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 387 | A new format style is available when using the \code{\%} operator. |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 388 | '\%r' will insert the \function{repr()} of its argument. This was |
| 389 | also added from symmetry considerations, this time for symmetry with |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 390 | the existing '\%s' format style, which inserts the \function{str()} of |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 391 | its argument. For example, \code{'\%r \%s' \% ('abc', 'abc')} returns a |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 392 | string containing \verb|'abc' abc|. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 393 | |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 394 | The \function{int()} and \function{long()} functions now accept an |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 395 | optional ``base'' parameter when the first argument is a string. |
| 396 | \code{int('123', 10)} returns 123, while \code{int('123', 16)} returns |
| 397 | 291. \code{int(123, 16)} raises a \exception{TypeError} exception |
| 398 | with the message ``can't convert non-string with explicit base''. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 399 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 400 | Previously there was no way to implement a class that overrode |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 401 | Python's built-in \keyword{in} operator and implemented a custom |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 402 | version. \code{\var{obj} in \var{seq}} returns true if \var{obj} is |
| 403 | present in the sequence \var{seq}; Python computes this by simply |
| 404 | trying every index of the sequence until either \var{obj} is found or |
| 405 | an \exception{IndexError} is encountered. Moshe Zadka contributed a |
| 406 | patch which adds a \method{__contains__} magic method for providing a |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 407 | custom implementation for \keyword{in}. Additionally, new built-in |
| 408 | objects written in C can define what \keyword{in} means for them via a |
| 409 | new slot in the sequence protocol. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 410 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 411 | Earlier versions of Python used a recursive algorithm for deleting |
| 412 | objects. Deeply nested data structures could cause the interpreter to |
| 413 | fill up the C stack and crash; Christian Tismer rewrote the deletion |
| 414 | logic to fix this problem. On a related note, comparing recursive |
| 415 | objects recursed infinitely and crashed; Jeremy Hylton rewrote the |
| 416 | code to no longer crash, producing a useful result instead. For |
| 417 | example, after this code: |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 418 | |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 419 | \begin{verbatim} |
| 420 | a = [] |
| 421 | b = [] |
| 422 | a.append(a) |
| 423 | b.append(b) |
| 424 | \end{verbatim} |
| 425 | |
| 426 | The comparison \code{a==b} returns true, because the two recursive |
| 427 | data structures are isomorphic. |
| 428 | \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. |
| 429 | %http://www.python.org/pipermail/python-dev/2000-April/004834.html |
| 430 | } |
| 431 | |
| 432 | 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] | 433 | processor, mostly by Trent Mick of ActiveState. (Confusingly, \code{sys.platform} is still \code{'win32'} on |
| 434 | Win64 because it seems that for ease of porting, MS Visual C++ treats code |
| 435 | as 32 bit. |
| 436 | ) PythonWin also supports Windows CE; see the Python CE page at |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 437 | \url{http://www.python.net/crew/mhammond/ce/} for more information. |
| 438 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 439 | An attempt has been made to alleviate one of Python's warts, the |
| 440 | often-confusing \exception{NameError} exception when code refers to a |
| 441 | local variable before the variable has been assigned a value. For |
| 442 | example, the following code raises an exception on the \keyword{print} |
| 443 | statement in both 1.5.2 and 1.6; in 1.5.2 a \exception{NameError} |
| 444 | exception is raised, while 1.6 raises \exception{UnboundLocalError}. |
| 445 | |
| 446 | \begin{verbatim} |
| 447 | def f(): |
| 448 | print "i=",i |
| 449 | i = i + 1 |
| 450 | f() |
| 451 | \end{verbatim} |
Andrew M. Kuchling | 5b8311e | 2000-05-31 03:28:42 +0000 | [diff] [blame] | 452 | |
| 453 | A new variable holding more detailed version information has been |
| 454 | added to the \module{sys} module. \code{sys.version_info} is a tuple |
| 455 | \code{(\var{major}, \var{minor}, \var{micro}, \var{level}, |
| 456 | \var{serial})} For example, in 1.6a2 \code{sys.version_info} is |
| 457 | \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] | 458 | \code{"alpha"}, \code{"beta"}, or \code{""} for a final release. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 459 | |
| 460 | % ====================================================================== |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 461 | \section{Extending/Embedding Changes} |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 462 | |
| 463 | Some of the changes are under the covers, and will only be apparent to |
| 464 | people writing C extension modules, or embedding a Python interpreter |
| 465 | 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] | 466 | you can safely skip this section. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 467 | |
Andrew M. Kuchling | a5bbb00 | 2000-06-10 02:41:46 +0000 | [diff] [blame^] | 468 | The version number of the Python C API was incremented, so C |
| 469 | extensions compiled for 1.5.2 must be recompiled in order to work with |
| 470 | 1.6. On Windows, attempting to import a third party extension built |
| 471 | for Python 1.5.x usually results in an immediate crash; there's not |
| 472 | much we can do about this. (XXX can anyone tell me why it crashes?) |
| 473 | |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 474 | Users of Jim Fulton's ExtensionClass module will be pleased to find |
| 475 | out that hooks have been added so that ExtensionClasses are now |
| 476 | supported by \function{isinstance()} and \function{issubclass()}. |
| 477 | This means you no longer have to remember to write code such as |
| 478 | \code{if type(obj) == myExtensionClass}, but can use the more natural |
| 479 | \code{if isinstance(obj, myExtensionClass)}. |
| 480 | |
Andrew M. Kuchling | b853ea0 | 2000-06-03 03:06:58 +0000 | [diff] [blame] | 481 | 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] | 482 | support dynamic loading on many different platforms, was cleaned up |
| 483 | are reorganized by Greg Stein. \file{importdl.c} is now quite small, |
| 484 | and platform-specific code has been moved into a bunch of |
| 485 | \file{Python/dynload_*.c} files. |
| 486 | |
| 487 | Vladimir Marangozov's long-awaited malloc restructuring was completed, |
| 488 | to make it easy to have the Python interpreter use a custom allocator |
| 489 | instead of C's standard \function{malloc()}. For documentation, read |
| 490 | the comments in \file{Include/mymalloc.h} and |
| 491 | \file{Include/objimpl.h}. For the lengthy discussions during which |
| 492 | the interface was hammered out, see the Web archives of the 'patches' |
| 493 | and 'python-dev' lists at python.org. |
| 494 | |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 495 | Recent versions of the GUSI development environment for MacOS support |
| 496 | POSIX threads. Therefore, Python's POSIX threading support now works |
| 497 | on the Macintosh. Threading support using the user-space GNU \texttt{pth} |
| 498 | library was also contributed. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 499 | |
| 500 | Threading support on Windows was enhanced, too. Windows supports |
| 501 | thread locks that use kernel objects only in case of contention; in |
| 502 | the common case when there's no contention, they use simpler functions |
| 503 | which are an order of magnitude faster. A threaded version of Python |
| 504 | 1.5.2 on NT is twice as slow as an unthreaded version; with the 1.6 |
| 505 | changes, the difference is only 10\%. These improvements were |
| 506 | contributed by Yakov Markovitch. |
| 507 | |
| 508 | % ====================================================================== |
| 509 | \section{Module changes} |
| 510 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 511 | Lots of improvements and bugfixes were made to Python's extensive |
| 512 | standard library; some of the affected modules include |
| 513 | \module{readline}, \module{ConfigParser}, \module{cgi}, |
| 514 | \module{calendar}, \module{posix}, \module{readline}, \module{xmllib}, |
| 515 | \module{aifc}, \module{chunk, wave}, \module{random}, \module{shelve}, |
| 516 | and \module{nntplib}. Consult the CVS logs for the exact |
| 517 | patch-by-patch details. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 518 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 519 | Brian Gallew contributed OpenSSL support for the \module{socket} |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 520 | module. OpenSSL is an implementation of the Secure Socket Layer, |
| 521 | which encrypts the data being sent over a socket. When compiling |
| 522 | Python, you can edit \file{Modules/Setup} to include SSL support, |
| 523 | which adds an additional function to the \module{socket} module: |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 524 | \function{socket.ssl(\var{socket}, \var{keyfile}, \var{certfile})}, |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 525 | which takes a socket object and returns an SSL socket. The |
| 526 | \module{httplib} and \module{urllib} modules were also changed to |
| 527 | support ``https://'' URLs, though no one has implemented FTP or SMTP |
| 528 | over SSL. |
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 | The \module{Tkinter} module now supports Tcl/Tk version 8.1, 8.2, or |
| 531 | 8.3, and support for the older 7.x versions has been dropped. The |
| 532 | Tkinter module also supports displaying Unicode strings in Tk |
| 533 | widgets. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 534 | |
Andrew M. Kuchling | fa33a4e | 2000-06-03 02:52:40 +0000 | [diff] [blame] | 535 | The \module{curses} module has been greatly extended, starting from |
| 536 | Oliver Andrich's enhanced version, to provide many additional |
| 537 | functions from ncurses and SYSV curses, such as colour, alternative |
| 538 | character set support, pads, and other new features. This means the |
| 539 | module is no longer compatible with operating systems that only have |
| 540 | BSD curses, but there don't seem to be any currently maintained OSes |
| 541 | that fall into this category. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 542 | |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 543 | As mentioned in the earlier discussion of 1.6's Unicode support, the |
| 544 | underlying implementation of the regular expressions provided by the |
| 545 | \module{re} module has been changed. SRE, a new regular expression |
| 546 | engine written by Fredrik Lundh and partially funded by Hewlett |
| 547 | Packard, supports matching against both 8-bit strings and Unicode |
| 548 | strings. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 549 | |
| 550 | % ====================================================================== |
| 551 | \section{New modules} |
| 552 | |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 553 | A number of new modules were added. We'll simply list them with brief |
| 554 | descriptions; consult the 1.6 documentation for the details of a |
| 555 | particular module. |
| 556 | |
| 557 | \begin{itemize} |
| 558 | |
| 559 | \item{\module{codecs}, \module{encodings}, \module{unicodedata}:} Added as part of the new Unicode support. |
| 560 | |
| 561 | \item{\module{filecmp}:} Supersedes the old \module{cmp} and |
| 562 | \module{dircmp} modules, which have now become deprecated. |
| 563 | (Contributed by Moshe Zadka.) |
| 564 | |
| 565 | \item{\module{linuxaudio}:} Support for the \file{/dev/audio} device on Linux, |
| 566 | a twin to the existing \module{sunaudiodev} module. |
| 567 | (Contributed by Peter Bosch.) |
| 568 | |
| 569 | \item{\module{mmap}:} An interface to memory-mapped files on both |
| 570 | Windows and Unix. A file's contents can be mapped directly into |
| 571 | memory, at which point it behaves like a mutable string, so its |
| 572 | contents can be read and modified. They can even be passed to |
| 573 | functions that expect ordinary strings, such as the \module{re} |
| 574 | module. (Contributed by Sam Rushing, with some extensions by |
| 575 | A.M. Kuchling.) |
| 576 | |
| 577 | \item{\module{PyExpat}:} An interface to the Expat XML parser. |
| 578 | (Contributed by Paul Prescod.) |
| 579 | |
| 580 | \item{\module{robotparser}:} Parse a \file{robots.txt} file, which is |
| 581 | used for writing Web spiders that politely avoid certain areas of a |
| 582 | Web site. The parser accepts the contents of a \file{robots.txt} file |
| 583 | builds a set of rules from it, and can then answer questions about |
| 584 | the fetchability of a given URL. (Contributed by Skip Montanaro.) |
| 585 | |
| 586 | \item{\module{tabnanny}:} A module/script to |
| 587 | checks Python source code for ambiguous indentation. |
| 588 | (Contributed by Tim Peters.) |
| 589 | |
Andrew M. Kuchling | a5bbb00 | 2000-06-10 02:41:46 +0000 | [diff] [blame^] | 590 | \item{\module{UserString}:} A base class useful for deriving objects that behave like strings. |
| 591 | |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 592 | \item{\module{winreg}:} An interface to the Windows registry. |
| 593 | \module{winreg} has been part of PythonWin since 1995, but now has |
| 594 | been added to the core distribution, and enhanced to support Unicode. |
| 595 | (Contributed by Bill Tutt and Mark Hammond.) |
| 596 | |
| 597 | \item{\module{zipfile}:} A module for reading and writing ZIP-format |
| 598 | archives. These are archives produced by \program{PKZIP} on |
| 599 | DOS/Windows or \program{zip} on Unix, not to be confused with |
| 600 | \program{gzip}-format files (which are supported by the \module{gzip} |
| 601 | module) |
| 602 | |
| 603 | (Contributed by James C. Ahlstrom.) |
| 604 | |
| 605 | \end{itemize} |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 606 | |
| 607 | % ====================================================================== |
| 608 | \section{IDLE Improvements} |
| 609 | |
Andrew M. Kuchling | a5bbb00 | 2000-06-10 02:41:46 +0000 | [diff] [blame^] | 610 | XXX IDLE -- complete overhaul. I don't use IDLE; can anyone tell me |
| 611 | what the changes are? |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 612 | |
| 613 | % ====================================================================== |
| 614 | \section{Deleted and Deprecated Modules} |
| 615 | |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 616 | A few modules have been dropped because they're obsolete, or because |
| 617 | there are now better ways to do the same thing. The \module{stdwin} |
| 618 | module is gone; it was for a platform-independent windowing toolkit |
| 619 | that's no longer developed. |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 620 | |
Andrew M. Kuchling | a5bbb00 | 2000-06-10 02:41:46 +0000 | [diff] [blame^] | 621 | A number of modules have been moved to the |
| 622 | \file{lib-old} subdirectory: |
| 623 | \module{cmp}, \module{cmpcache}, \module{dircmp}, \module{dump}, |
| 624 | \module{find}, \module{grep}, \module{packmail}, |
| 625 | \module{poly}, \module{util}, \module{whatsound}, \module{zmod}. |
| 626 | If you have code which relies on a module that's been moved to |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 627 | \file{lib-old}, you can simply add that directory to \code{sys.path} |
Andrew M. Kuchling | a5bbb00 | 2000-06-10 02:41:46 +0000 | [diff] [blame^] | 628 | to get them back, but you're encouraged to update any code that uses |
| 629 | these modules. |
Andrew M. Kuchling | 6c3cd8d | 2000-06-10 02:24:31 +0000 | [diff] [blame] | 630 | |
| 631 | XXX any others deleted? |
| 632 | |
| 633 | XXX Other candidates for deletion in 1.6: sgimodule.c, glmodule.c (and hence |
| 634 | cgenmodule.c), imgfile.c, svmodule.c, flmodule.c, fmmodule.c, almodule.c, clmodule.c, |
| 635 | knee.py. |
Andrew M. Kuchling | 25bfd0e | 2000-05-27 11:28:26 +0000 | [diff] [blame] | 636 | |
| 637 | \end{document} |
| 638 | |