diff --git a/Doc/api/abstract.tex b/Doc/api/abstract.tex index f35fb638e1f..f2929722214 100644 --- a/Doc/api/abstract.tex +++ b/Doc/api/abstract.tex @@ -5,6 +5,10 @@ of their type, or with wide classes of object types (e.g. all numerical types, or all sequence types). When used on object types for which they do not apply, they will raise a Python exception. +It is not possible to use these functions on objects that are not properly +initialized, such as a list object that has been created by +\cfunction{PyList_New()}, but whose items have not been set to some +non-\code{NULL} value yet. \section{Object Protocol \label{object}} @@ -673,12 +677,31 @@ determination. \samp{float(\var{o})}.\bifuncindex{float} \end{cfuncdesc} -\begin{cfuncdesc}{Py_ssize_t}{PyNumber_Index}{PyObject *o} - Returns the \var{o} converted to a Py_ssize_t integer on success, or - -1 with an exception raised on failure. +\begin{cfuncdesc}{PyObject*}{PyNumber_Index}{PyObject *o} + Returns the \var{o} converted to a Python int or long on success or \NULL{} + with a TypeError exception raised on failure. \versionadded{2.5} \end{cfuncdesc} +\begin{cfuncdesc}{Py_ssize_t}{PyNumber_AsSsize_t}{PyObject *o, PyObject *exc} + Returns \var{o} converted to a Py_ssize_t value if \var{o} + can be interpreted as an integer. If \var{o} can be converted to a Python + int or long but the attempt to convert to a Py_ssize_t value + would raise an \exception{OverflowError}, then the \var{exc} argument + is the type of exception that will be raised (usually \exception{IndexError} + or \exception{OverflowError}). If \var{exc} is \NULL{}, then the exception + is cleared and the value is clipped to \var{PY_SSIZE_T_MIN} + for a negative integer or \var{PY_SSIZE_T_MAX} for a positive integer. + \versionadded{2.5} +\end{cfuncdesc} + +\begin{cfuncdesc}{int}{PyIndex_Check}{PyObject *o} + Returns True if \var{o} is an index integer (has the nb_index slot of + the tp_as_number structure filled in). + \versionadded{2.5} +\end{cfuncdesc} + + \section{Sequence Protocol \label{sequence}} \begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o} diff --git a/Doc/api/concrete.tex b/Doc/api/concrete.tex index 4c7487c4ffc..34221ad3af8 100644 --- a/Doc/api/concrete.tex +++ b/Doc/api/concrete.tex @@ -1560,6 +1560,26 @@ They all return \NULL{} or \code{-1} if an exception occurs. greater than, respectively. \end{cfuncdesc} +\begin{cfuncdesc}{int}{PyUnicode_RichCompare}{PyObject *left, + PyObject *right, + int op} + + Rich compare two unicode strings and return one of the following: + \begin{itemize} + \item \code{NULL} in case an exception was raised + \item \constant{Py_True} or \constant{Py_False} for successful comparisons + \item \constant{Py_NotImplemented} in case the type combination is unknown + \end{itemize} + + Note that \constant{Py_EQ} and \constant{Py_NE} comparisons can cause a + \exception{UnicodeWarning} in case the conversion of the arguments to + Unicode fails with a \exception{UnicodeDecodeError}. + + Possible values for \var{op} are + \constant{Py_GT}, \constant{Py_GE}, \constant{Py_EQ}, + \constant{Py_NE}, \constant{Py_LT}, and \constant{Py_LE}. +\end{cfuncdesc} + \begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format, PyObject *args} Return a new string object from \var{format} and \var{args}; this @@ -1820,6 +1840,11 @@ format. \begin{cfuncdesc}{PyObject*}{PyList_New}{Py_ssize_t len} Return a new list of length \var{len} on success, or \NULL{} on failure. + \note{If \var{length} is greater than zero, the returned list object's + items are set to \code{NULL}. Thus you cannot use abstract + API functions such as \cfunction{PySequence_SetItem()} + or expose the object to Python code before setting all items to a + real object with \cfunction{PyList_SetItem()}.} \end{cfuncdesc} \begin{cfuncdesc}{Py_ssize_t}{PyList_Size}{PyObject *list} diff --git a/Doc/api/exceptions.tex b/Doc/api/exceptions.tex index 2a9db540aa8..d7a490e52b2 100644 --- a/Doc/api/exceptions.tex +++ b/Doc/api/exceptions.tex @@ -285,10 +285,11 @@ error indicator for each thread. names are \samp{PyExc_} followed by the Python exception name. These have the type \ctype{PyObject*}; they are all class objects. Their names are \cdata{PyExc_Warning}, \cdata{PyExc_UserWarning}, - \cdata{PyExc_DeprecationWarning}, \cdata{PyExc_SyntaxWarning}, - \cdata{PyExc_RuntimeWarning}, and \cdata{PyExc_FutureWarning}. - \cdata{PyExc_Warning} is a subclass of \cdata{PyExc_Exception}; the - other warning categories are subclasses of \cdata{PyExc_Warning}. + \cdata{PyExc_UnicodeWarning}, \cdata{PyExc_DeprecationWarning}, + \cdata{PyExc_SyntaxWarning}, \cdata{PyExc_RuntimeWarning}, and + \cdata{PyExc_FutureWarning}. \cdata{PyExc_Warning} is a subclass of + \cdata{PyExc_Exception}; the other warning categories are subclasses + of \cdata{PyExc_Warning}. For information about warning control, see the documentation for the \module{warnings} module and the \programopt{-W} option in the diff --git a/Doc/api/init.tex b/Doc/api/init.tex index 9225f699862..e380bdb250c 100644 --- a/Doc/api/init.tex +++ b/Doc/api/init.tex @@ -696,15 +696,15 @@ interpreter lock has been created. \end{cfuncdesc} \begin{cfuncdesc}{int}{PyThreadState_SetAsyncExc}{long id, PyObject *exc} - Asynchronously raise an exception in a thread. + Asynchronously raise an exception in a thread. The \var{id} argument is the thread id of the target thread; \var{exc} is the exception object to be raised. This function does not steal any references to \var{exc}. - To prevent naive misuse, you must write your own C extension - to call this. Must be called with the GIL held. - Returns the number of thread states modified; if it returns a number - greater than one, you're in trouble, and you should call it again - with \var{exc} set to \constant{NULL} to revert the effect. + To prevent naive misuse, you must write your own C extension + to call this. Must be called with the GIL held. + Returns the number of thread states modified; this is normally one, but + will be zero if the thread id isn't found. If \var{exc} is + \constant{NULL}, the pending exception (if any) for the thread is cleared. This raises no exceptions. \versionadded{2.3} \end{cfuncdesc} diff --git a/Doc/api/intro.tex b/Doc/api/intro.tex index c2c3fecf839..a94518061d1 100644 --- a/Doc/api/intro.tex +++ b/Doc/api/intro.tex @@ -225,25 +225,10 @@ immutable data type. You should only use \cfunction{PyTuple_SetItem()} for tuples that you are creating yourself. -Equivalent code for populating a list can be written using -\cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. Such code -can also use \cfunction{PySequence_SetItem()}; this illustrates the -difference between the two (the extra \cfunction{Py_DECREF()} calls): +Equivalent code for populating a list can be written using +\cfunction{PyList_New()} and \cfunction{PyList_SetItem()}. -\begin{verbatim} -PyObject *l, *x; - -l = PyList_New(3); -x = PyInt_FromLong(1L); -PySequence_SetItem(l, 0, x); Py_DECREF(x); -x = PyInt_FromLong(2L); -PySequence_SetItem(l, 1, x); Py_DECREF(x); -x = PyString_FromString("three"); -PySequence_SetItem(l, 2, x); Py_DECREF(x); -\end{verbatim} - -You might find it strange that the ``recommended'' approach takes more -code. However, in practice, you will rarely use these ways of +However, in practice, you will rarely use these ways of creating and populating a tuple or list. There's a generic function, \cfunction{Py_BuildValue()}, that can create most common objects from C values, directed by a \dfn{format string}. For example, the @@ -251,10 +236,10 @@ above two blocks of code could be replaced by the following (which also takes care of the error checking): \begin{verbatim} -PyObject *t, *l; +PyObject *tuple, *list; -t = Py_BuildValue("(iis)", 1, 2, "three"); -l = Py_BuildValue("[iis]", 1, 2, "three"); +tuple = Py_BuildValue("(iis)", 1, 2, "three"); +list = Py_BuildValue("[iis]", 1, 2, "three"); \end{verbatim} It is much more common to use \cfunction{PyObject_SetItem()} and @@ -276,8 +261,12 @@ set_all(PyObject *target, PyObject *item) if (n < 0) return -1; for (i = 0; i < n; i++) { - if (PyObject_SetItem(target, i, item) < 0) + PyObject *index = PyInt_FromLong(i); + if (!index) return -1; + if (PyObject_SetItem(target, index, item) < 0) + return -1; + Py_DECREF(index); } return 0; } diff --git a/Doc/api/newtypes.tex b/Doc/api/newtypes.tex index 04f67953fad..43d2f8839ab 100644 --- a/Doc/api/newtypes.tex +++ b/Doc/api/newtypes.tex @@ -979,7 +979,7 @@ The following three fields only exist if the More information about Python's garbage collection scheme can be found in section \ref{supporting-cycle-detection}. - This field is inherited by subtypes together with \member{tp_clear} + This field is inherited by subtypes together with \member{tp_traverse} and the \constant{Py_TPFLAGS_HAVE_GC} flag bit: the flag bit, \member{tp_traverse}, and \member{tp_clear} are all inherited from the base type if they are all zero in the subtype \emph{and} the diff --git a/Doc/commontex/boilerplate.tex b/Doc/commontex/boilerplate.tex index 974943245e9..b4c9f480da3 100644 --- a/Doc/commontex/boilerplate.tex +++ b/Doc/commontex/boilerplate.tex @@ -5,5 +5,5 @@ Email: \email{docs@python.org} } -\date{3rd August, 2006} % XXX update before final release! +\date{\today} % XXX update before final release! \input{patchlevel} % include Python version information diff --git a/Doc/dist/dist.tex b/Doc/dist/dist.tex index c1b72ad5d81..e8ae96f393e 100644 --- a/Doc/dist/dist.tex +++ b/Doc/dist/dist.tex @@ -1992,6 +1992,7 @@ If the \module{foo} extension belongs in the root package, the setup script for this could be \begin{verbatim} from distutils.core import setup +from distutils.extension import Extension setup(name='foobar', version='1.0', ext_modules=[Extension('foo', ['foo.c'])], @@ -2006,6 +2007,7 @@ the \module{foopkg} package simply by changing the name of the extension: \begin{verbatim} from distutils.core import setup +from distutils.extension import Extension setup(name='foobar', version='1.0', ext_modules=[Extension('foopkg.foo', ['foo.c'])], diff --git a/Doc/lib/libctypes.tex b/Doc/lib/libctypes.tex index 6206b8c1707..b2e488a807e 100755 --- a/Doc/lib/libctypes.tex +++ b/Doc/lib/libctypes.tex @@ -199,8 +199,13 @@ WindowsError: exception: access violation reading 0x00000020 There are, however, enough ways to crash Python with \code{ctypes}, so you should be careful anyway. -Python integers, strings and unicode strings are the only objects that -can directly be used as parameters in these function calls. +\code{None}, integers, longs, byte strings and unicode strings are the +only native Python objects that can directly be used as parameters in +these function calls. \code{None} is passed as a C \code{NULL} pointer, +byte strings and unicode strings are passed as pointer to the memory +block that contains their data (\code{char *} or \code{wchar{\_}t *}). Python +integers and Python longs are passed as the platforms default C +\code{int} type, their value is masked to fit into the C type. Before we move on calling functions with other parameter types, we have to learn more about \code{ctypes} data types. @@ -227,7 +232,18 @@ Python type \code{char} } { -character +1-character +string +} +\lineiii{ +\class{c{\_}wchar} +} +{ +\code{wchar{\_}t} +} +{ +1-character +unicode string } \lineiii{ \class{c{\_}byte} @@ -236,7 +252,7 @@ character \code{char} } { -integer +int/long } \lineiii{ \class{c{\_}ubyte} @@ -245,7 +261,7 @@ integer \code{unsigned char} } { -integer +int/long } \lineiii{ \class{c{\_}short} @@ -254,7 +270,7 @@ integer \code{short} } { -integer +int/long } \lineiii{ \class{c{\_}ushort} @@ -263,7 +279,7 @@ integer \code{unsigned short} } { -integer +int/long } \lineiii{ \class{c{\_}int} @@ -272,7 +288,7 @@ integer \code{int} } { -integer +int/long } \lineiii{ \class{c{\_}uint} @@ -281,7 +297,7 @@ integer \code{unsigned int} } { -integer +int/long } \lineiii{ \class{c{\_}long} @@ -290,7 +306,7 @@ integer \code{long} } { -integer +int/long } \lineiii{ \class{c{\_}ulong} @@ -299,7 +315,7 @@ integer \code{unsigned long} } { -long +int/long } \lineiii{ \class{c{\_}longlong} @@ -309,7 +325,7 @@ long \code{long long} } { -long +int/long } \lineiii{ \class{c{\_}ulonglong} @@ -319,7 +335,7 @@ long \code{unsigned long long} } { -long +int/long } \lineiii{ \class{c{\_}float} @@ -368,8 +384,8 @@ unicode or \code{void *} } { -integer or -\code{None} +int/long +or \code{None} } \end{tableiii} \end{quote} @@ -554,11 +570,11 @@ the \member{{\_}as{\_}parameter{\_}} attribute. \subsubsection{Return types\label{ctypes-return-types}} -By default functions are assumed to return integers. Other return -types can be specified by setting the \member{restype} attribute of the -function object. +By default functions are assumed to return the C \code{int} type. Other +return types can be specified by setting the \member{restype} attribute of +the function object. -Here is a more advanced example, it uses the strchr function, which +Here is a more advanced example, it uses the \code{strchr} function, which expects a string pointer and a char, and returns a pointer to a string: \begin{verbatim} @@ -1611,8 +1627,8 @@ library object is available: \begin{datadescni}{pythonapi} An instance of \class{PyDLL} that exposes Python C api functions as -attributes. Note that all these functions are assumed to return -integers, which is of course not always the truth, so you have to +attributes. Note that all these functions are assumed to return C +\code{int}, which is of course not always the truth, so you have to assign the correct \member{restype} attribute to use these functions. \end{datadescni} @@ -1642,8 +1658,8 @@ function. Use \code{None} for \code{void} a function not returning anything. It is possible to assign a callable Python object that is not a -ctypes type, in this case the function is assumed to return an -integer, and the callable will be called with this integer, +ctypes type, in this case the function is assumed to return a +C \code{int}, and the callable will be called with this integer, allowing to do further processing or error checking. Using this is deprecated, for more flexible postprocessing or error checking use a ctypes data type as \member{restype} and assign a callable to the @@ -2283,9 +2299,12 @@ Windows only: Represents a \class{HRESULT} value, which contains success or error information for a function or method call. \end{classdesc*} -\begin{classdesc*}{py_object} -Represents the C \code{PyObject *} datatype. -\end{classdesc*} +\code{py{\_}object} : classdesc* +\begin{quote} + +Represents the C \code{PyObject *} datatype. Calling this with an +without an argument creates a \code{NULL} \code{PyObject *} pointer. +\end{quote} The \code{ctypes.wintypes} module provides quite some other Windows specific data types, for example \code{HWND}, \code{WPARAM}, or \code{DWORD}. @@ -2324,9 +2343,9 @@ A sequence defining the structure fields. The items must be the second item specifies the type of the field; it can be any ctypes data type. -For integer type fields, a third optional item can be given. It -must be a small positive integer defining the bit width of the -field. +For integer type fields like \class{c{\_}int}, a third optional item can +be given. It must be a small positive integer defining the bit +width of the field. Field names must be unique within one structure or union. This is not checked, only one field can be accessed when names are diff --git a/Doc/lib/libexcs.tex b/Doc/lib/libexcs.tex index 30fe831d74a..b64d57d4c62 100644 --- a/Doc/lib/libexcs.tex +++ b/Doc/lib/libexcs.tex @@ -451,6 +451,11 @@ Base class for warnings about probable mistakes in module imports. \versionadded{2.5} \end{excdesc} +\begin{excdesc}{UnicodeWarning} +Base class for warnings related to Unicode. +\versionadded{2.5} +\end{excdesc} + The class hierarchy for built-in exceptions is: \verbatiminput{../../Lib/test/exception_hierarchy.txt} diff --git a/Doc/lib/libwarnings.tex b/Doc/lib/libwarnings.tex index 08c0340241a..a37a9f53348 100644 --- a/Doc/lib/libwarnings.tex +++ b/Doc/lib/libwarnings.tex @@ -76,6 +76,9 @@ features that will be deprecated in the future (ignored by default).} \lineii{ImportWarning}{Base category for warnings triggered during the process of importing a module (ignored by default).} + +\lineii{UnicodeWarning}{Base category for warnings related to Unicode.} + \end{tableii} While these are technically built-in exceptions, they are documented diff --git a/Doc/whatsnew/whatsnew25.tex b/Doc/whatsnew/whatsnew25.tex index dcb6ab146b0..bf939c0bedc 100644 --- a/Doc/whatsnew/whatsnew25.tex +++ b/Doc/whatsnew/whatsnew25.tex @@ -5,7 +5,7 @@ % Fix XXX comments \title{What's New in Python 2.5} -\release{0.9} +\release{1.0} \author{A.M. Kuchling} \authoraddress{\email{amk@amk.ca}} @@ -40,15 +40,14 @@ Python's semantics. As well as the language and library additions, other improvements and bugfixes were made throughout the source tree. A search through the -SVN change logs finds there were 334 patches applied and 443 bugs +SVN change logs finds there were 353 patches applied and 458 bugs fixed between Python 2.4 and 2.5. (Both figures are likely to be underestimates.) This article doesn't try to be a complete specification of the new features; instead changes are briefly introduced using helpful examples. For full details, you should always refer to the -documentation for Python 2.5. -% XXX add hyperlink when the documentation becomes available online. +documentation for Python 2.5 at \url{http://docs.python.org}. If you want to understand the complete implementation and design rationale, refer to the PEP for a particular new feature. @@ -751,7 +750,6 @@ 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.) -% XXX find a shorter reference? Let's assume there's an object representing a database connection. Our goal will be to let the user write code like this: @@ -1184,6 +1182,35 @@ a line like this near the top of the source file: # -*- coding: latin1 -*- \end{verbatim} +\item A new warning, \class{UnicodeWarning}, is triggered when +you attempt to compare a Unicode string and an 8-bit string +that can't be converted to Unicode using the default ASCII encoding. +The result of the comparison is false: + +\begin{verbatim} +>>> chr(128) == unichr(128) # Can't convert chr(128) to Unicode +__main__:1: UnicodeWarning: Unicode equal comparison failed + to convert both arguments to Unicode - interpreting them + as being unequal +False +>>> chr(127) == unichr(127) # chr(127) can be converted +True +\end{verbatim} + +Previously this would raise a \class{UnicodeDecodeError} exception, +but in 2.5 this could result in puzzling problems when accessing a +dictionary. If you looked up \code{unichr(128)} and \code{chr(128)} +was being used as a key, you'd get a \class{UnicodeDecodeError} +exception. Other changes in 2.5 resulted in this exception being +raised instead of suppressed by the code in \file{dictobject.c} that +implements dictionaries. + +Raising an exception for such a comparison is strictly correct, but +the change might have broken code, so instead +\class{UnicodeWarning} was introduced. + +(Implemented by Marc-Andr\'e Lemburg.) + \item One error that Python programmers sometimes make is forgetting to include an \file{__init__.py} module in a package directory. Debugging this mistake can be confusing, and usually requires running @@ -1305,9 +1332,6 @@ they exist or not so that the interpreter makes fewer \end{itemize} -The net result of the 2.5 optimizations is that Python 2.5 runs the -pystone benchmark around XXX\% faster than Python 2.4. - %====================================================================== \section{New, Improved, and Removed Modules\label{modules}} @@ -2423,6 +2447,11 @@ was always a frame object. Because of the \pep{342} changes described in section~\ref{pep-342}, it's now possible for \member{gi_frame} to be \code{None}. +\item A new warning, \class{UnicodeWarning}, is triggered when +you attempt to compare a Unicode string and an 8-bit string that can't +be converted to Unicode using the default ASCII encoding. Previously +such comparisons would raise a \class{UnicodeDecodeError} exception. + \item Library: the \module{csv} module is now stricter about multi-line quoted fields. If your files contain newlines embedded within fields, the input should be split into lines in a manner which preserves the diff --git a/Include/abstract.h b/Include/abstract.h index eaac2787e42..357afe1a338 100644 --- a/Include/abstract.h +++ b/Include/abstract.h @@ -694,13 +694,26 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ expression: o1|o2. */ - PyAPI_FUNC(Py_ssize_t) PyNumber_Index(PyObject *); +#define PyIndex_Check(obj) \ + ((obj)->ob_type->tp_as_number != NULL && \ + (obj)->ob_type->tp_as_number->nb_index != NULL) + + PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o); /* - Returns the object converted to Py_ssize_t on success - or -1 with an error raised on failure. + Returns the object converted to a Python long or int + or NULL with an error raised on failure. */ + PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc); + + /* + Returns the object converted to Py_ssize_t by going through + PyNumber_Index first. If an overflow error occurs while + converting the int-or-long to Py_ssize_t, then the second argument + is the error-type to return. If it is NULL, then the overflow error + is cleared and the value is clipped. + */ PyAPI_FUNC(PyObject *) PyNumber_Int(PyObject *o); diff --git a/Include/code.h b/Include/code.h index 432ec8e0337..3de77b63e91 100644 --- a/Include/code.h +++ b/Include/code.h @@ -88,6 +88,9 @@ typedef struct _addr_pair { PyAPI_FUNC(int) PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds); +PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts, + PyObject *names, PyObject *lineno_obj); + #ifdef __cplusplus } #endif diff --git a/Include/import.h b/Include/import.h index a4e5c0e69f4..414e059a3ec 100644 --- a/Include/import.h +++ b/Include/import.h @@ -22,7 +22,7 @@ PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevel(char *name, PyAPI_FUNC(PyObject *) PyImport_ImportModuleEx( char *name, PyObject *globals, PyObject *locals, PyObject *fromlist); #define PyImport_ImportModuleEx(n, g, l, f) \ - PyImport_ImportModuleLevel(n, g, l, f, -1); + PyImport_ImportModuleLevel(n, g, l, f, -1) PyAPI_FUNC(PyObject *) PyImport_Import(PyObject *name); PyAPI_FUNC(PyObject *) PyImport_ReloadModule(PyObject *m); diff --git a/Include/object.h b/Include/object.h index b69ee319312..1f1aeaaa9fe 100644 --- a/Include/object.h +++ b/Include/object.h @@ -190,7 +190,7 @@ typedef struct { binaryfunc nb_inplace_floor_divide; binaryfunc nb_inplace_true_divide; - lenfunc nb_index; + unaryfunc nb_index; } PyNumberMethods; typedef struct { diff --git a/Include/pyerrors.h b/Include/pyerrors.h index 5df334b6520..67f1909d9ee 100644 --- a/Include/pyerrors.h +++ b/Include/pyerrors.h @@ -166,6 +166,7 @@ PyAPI_DATA(PyObject *) PyExc_SyntaxWarning; PyAPI_DATA(PyObject *) PyExc_RuntimeWarning; PyAPI_DATA(PyObject *) PyExc_FutureWarning; PyAPI_DATA(PyObject *) PyExc_ImportWarning; +PyAPI_DATA(PyObject *) PyExc_UnicodeWarning; /* Convenience functions */ diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h index c7e07a86ef4..33aa185814a 100644 --- a/Include/unicodeobject.h +++ b/Include/unicodeobject.h @@ -189,6 +189,7 @@ typedef PY_UNICODE_TYPE Py_UNICODE; # define PyUnicode_RSplit PyUnicodeUCS2_RSplit # define PyUnicode_Replace PyUnicodeUCS2_Replace # define PyUnicode_Resize PyUnicodeUCS2_Resize +# define PyUnicode_RichCompare PyUnicodeUCS2_RichCompare # define PyUnicode_SetDefaultEncoding PyUnicodeUCS2_SetDefaultEncoding # define PyUnicode_Split PyUnicodeUCS2_Split # define PyUnicode_Splitlines PyUnicodeUCS2_Splitlines @@ -266,6 +267,7 @@ typedef PY_UNICODE_TYPE Py_UNICODE; # define PyUnicode_RSplit PyUnicodeUCS4_RSplit # define PyUnicode_Replace PyUnicodeUCS4_Replace # define PyUnicode_Resize PyUnicodeUCS4_Resize +# define PyUnicode_RichCompare PyUnicodeUCS4_RichCompare # define PyUnicode_SetDefaultEncoding PyUnicodeUCS4_SetDefaultEncoding # define PyUnicode_Split PyUnicodeUCS4_Split # define PyUnicode_Splitlines PyUnicodeUCS4_Splitlines @@ -1139,6 +1141,28 @@ PyAPI_FUNC(int) PyUnicode_Compare( PyObject *right /* Right string */ ); +/* Rich compare two strings and return one of the following: + + - NULL in case an exception was raised + - Py_True or Py_False for successfuly comparisons + - Py_NotImplemented in case the type combination is unknown + + Note that Py_EQ and Py_NE comparisons can cause a UnicodeWarning in + case the conversion of the arguments to Unicode fails with a + UnicodeDecodeError. + + Possible values for op: + + Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE + +*/ + +PyAPI_FUNC(PyObject *) PyUnicode_RichCompare( + PyObject *left, /* Left string */ + PyObject *right, /* Right string */ + int op /* Operation: Py_EQ, Py_NE, Py_GT, etc. */ + ); + /* Apply a argument tuple or dictionary to a format string and return the resulting Unicode string. */ diff --git a/Lib/Cookie.py b/Lib/Cookie.py index 1b84e9f7fab..46ef3ea61c7 100644 --- a/Lib/Cookie.py +++ b/Lib/Cookie.py @@ -305,8 +305,10 @@ _Translator = { '\375' : '\\375', '\376' : '\\376', '\377' : '\\377' } +_idmap = ''.join(chr(x) for x in xrange(256)) + def _quote(str, LegalChars=_LegalChars, - idmap=string._idmap, translate=string.translate): + idmap=_idmap, translate=string.translate): # # If the string does not need to be double-quoted, # then just return the string. Otherwise, surround @@ -440,7 +442,7 @@ class Morsel(dict): def set(self, key, val, coded_val, LegalChars=_LegalChars, - idmap=string._idmap, translate=string.translate ): + idmap=_idmap, translate=string.translate): # First we verify that the key isn't a reserved word # Second we make sure it only contains legal characters if key.lower() in self._reserved: diff --git a/Lib/cgi.py b/Lib/cgi.py index 4c617a6e13c..47c02795737 100755 --- a/Lib/cgi.py +++ b/Lib/cgi.py @@ -251,6 +251,10 @@ def parse_multipart(fp, pdict): XXX This should really be subsumed by FieldStorage altogether -- no point in having two implementations of the same parsing algorithm. + Also, FieldStorage protects itself better against certain DoS attacks + by limiting the size of the data read in one chunk. The API here + does not support that kind of protection. This also affects parse() + since it can call parse_multipart(). """ boundary = "" @@ -691,7 +695,7 @@ class FieldStorage: def read_lines_to_eof(self): """Internal: read lines until EOF.""" while 1: - line = self.fp.readline() + line = self.fp.readline(1<<16) if not line: self.done = -1 break @@ -702,12 +706,13 @@ class FieldStorage: next = "--" + self.outerboundary last = next + "--" delim = "" + last_line_lfend = True while 1: - line = self.fp.readline() + line = self.fp.readline(1<<16) if not line: self.done = -1 break - if line[:2] == "--": + if line[:2] == "--" and last_line_lfend: strippedline = line.strip() if strippedline == next: break @@ -718,11 +723,14 @@ class FieldStorage: if line[-2:] == "\r\n": delim = "\r\n" line = line[:-2] + last_line_lfend = True elif line[-1] == "\n": delim = "\n" line = line[:-1] + last_line_lfend = True else: delim = "" + last_line_lfend = False self.__write(odelim + line) def skip_lines(self): @@ -731,18 +739,20 @@ class FieldStorage: return next = "--" + self.outerboundary last = next + "--" + last_line_lfend = True while 1: - line = self.fp.readline() + line = self.fp.readline(1<<16) if not line: self.done = -1 break - if line[:2] == "--": + if line[:2] == "--" and last_line_lfend: strippedline = line.strip() if strippedline == next: break if strippedline == last: self.done = 1 break + last_line_lfend = line.endswith('\n') def make_file(self, binary=None): """Overridable: return a readable & writable file. diff --git a/Lib/compiler/ast.py b/Lib/compiler/ast.py index 8dcdf68ada3..93437d6ceef 100644 --- a/Lib/compiler/ast.py +++ b/Lib/compiler/ast.py @@ -583,11 +583,9 @@ class GenExpr(Node): def __init__(self, code, lineno=None): self.code = code self.lineno = lineno - self.argnames = ['[outmost-iterable]'] + self.argnames = ['.0'] self.varargs = self.kwargs = None - - def getChildren(self): return self.code, diff --git a/Lib/compiler/pycodegen.py b/Lib/compiler/pycodegen.py index c8a9779180e..db2251e6f87 100644 --- a/Lib/compiler/pycodegen.py +++ b/Lib/compiler/pycodegen.py @@ -656,18 +656,19 @@ class CodeGenerator: stack = [] for i, for_ in zip(range(len(node.quals)), node.quals): - start, anchor = self.visit(for_) + start, anchor, end = self.visit(for_) cont = None for if_ in for_.ifs: if cont is None: cont = self.newBlock() self.visit(if_, cont) - stack.insert(0, (start, cont, anchor)) + stack.insert(0, (start, cont, anchor, end)) self.visit(node.expr) self.emit('YIELD_VALUE') + self.emit('POP_TOP') - for start, cont, anchor in stack: + for start, cont, anchor, end in stack: if cont: skip_one = self.newBlock() self.emit('JUMP_FORWARD', skip_one) @@ -676,14 +677,22 @@ class CodeGenerator: self.nextBlock(skip_one) self.emit('JUMP_ABSOLUTE', start) self.startBlock(anchor) + self.emit('POP_BLOCK') + self.setups.pop() + self.startBlock(end) + self.emit('LOAD_CONST', None) def visitGenExprFor(self, node): start = self.newBlock() anchor = self.newBlock() + end = self.newBlock() + + self.setups.push((LOOP, start)) + self.emit('SETUP_LOOP', end) if node.is_outmost: - self.loadName('[outmost-iterable]') + self.loadName('.0') else: self.visit(node.iter) self.emit('GET_ITER') @@ -693,7 +702,7 @@ class CodeGenerator: self.emit('FOR_ITER', anchor) self.nextBlock() self.visit(node.assign) - return start, anchor + return start, anchor, end def visitGenExprIf(self, node, branch): self.set_lineno(node, force=True) diff --git a/Lib/compiler/symbols.py b/Lib/compiler/symbols.py index af8e1c9a9ee..7ddf42c6c9e 100644 --- a/Lib/compiler/symbols.py +++ b/Lib/compiler/symbols.py @@ -188,7 +188,7 @@ class GenExprScope(Scope): i = self.__counter self.__counter += 1 self.__super_init("generator expression<%d>"%i, module, klass) - self.add_param('[outmost-iterable]') + self.add_param('.0') def get_names(self): keys = Scope.get_names(self) diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 9e91ac63817..78a2ab820fc 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -5,7 +5,7 @@ import os as _os, sys as _sys -__version__ = "1.0.0" +__version__ = "1.0.1" from _ctypes import Union, Structure, Array from _ctypes import _Pointer @@ -135,6 +135,11 @@ from _ctypes import _SimpleCData class py_object(_SimpleCData): _type_ = "O" + def __repr__(self): + try: + return super(py_object, self).__repr__() + except ValueError: + return "%s()" % type(self).__name__ class c_short(_SimpleCData): _type_ = "h" diff --git a/Lib/ctypes/test/test_as_parameter.py b/Lib/ctypes/test/test_as_parameter.py new file mode 100644 index 00000000000..05810593356 --- /dev/null +++ b/Lib/ctypes/test/test_as_parameter.py @@ -0,0 +1,215 @@ +import unittest +from ctypes import * +import _ctypes_test + +dll = CDLL(_ctypes_test.__file__) + +try: + CALLBACK_FUNCTYPE = WINFUNCTYPE +except NameError: + # fake to enable this test on Linux + CALLBACK_FUNCTYPE = CFUNCTYPE + +class POINT(Structure): + _fields_ = [("x", c_int), ("y", c_int)] + +class BasicWrapTestCase(unittest.TestCase): + def wrap(self, param): + return param + + def test_wchar_parm(self): + try: + c_wchar + except NameError: + return + f = dll._testfunc_i_bhilfd + f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double] + result = f(self.wrap(1), self.wrap(u"x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0)) + self.failUnlessEqual(result, 139) + self.failUnless(type(result), int) + + def test_pointers(self): + f = dll._testfunc_p_p + f.restype = POINTER(c_int) + f.argtypes = [POINTER(c_int)] + + # This only works if the value c_int(42) passed to the + # function is still alive while the pointer (the result) is + # used. + + v = c_int(42) + + self.failUnlessEqual(pointer(v).contents.value, 42) + result = f(self.wrap(pointer(v))) + self.failUnlessEqual(type(result), POINTER(c_int)) + self.failUnlessEqual(result.contents.value, 42) + + # This on works... + result = f(self.wrap(pointer(v))) + self.failUnlessEqual(result.contents.value, v.value) + + p = pointer(c_int(99)) + result = f(self.wrap(p)) + self.failUnlessEqual(result.contents.value, 99) + + def test_shorts(self): + f = dll._testfunc_callback_i_if + + args = [] + expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, + 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1] + + def callback(v): + args.append(v) + return v + + CallBack = CFUNCTYPE(c_int, c_int) + + cb = CallBack(callback) + f(self.wrap(2**18), self.wrap(cb)) + self.failUnlessEqual(args, expected) + + ################################################################ + + def test_callbacks(self): + f = dll._testfunc_callback_i_if + f.restype = c_int + + MyCallback = CFUNCTYPE(c_int, c_int) + + def callback(value): + #print "called back with", value + return value + + cb = MyCallback(callback) + + result = f(self.wrap(-10), self.wrap(cb)) + self.failUnlessEqual(result, -18) + + # test with prototype + f.argtypes = [c_int, MyCallback] + cb = MyCallback(callback) + + result = f(self.wrap(-10), self.wrap(cb)) + self.failUnlessEqual(result, -18) + + result = f(self.wrap(-10), self.wrap(cb)) + self.failUnlessEqual(result, -18) + + AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int) + + # check that the prototype works: we call f with wrong + # argument types + cb = AnotherCallback(callback) + self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb)) + + def test_callbacks_2(self): + # Can also use simple datatypes as argument type specifiers + # for the callback function. + # In this case the call receives an instance of that type + f = dll._testfunc_callback_i_if + f.restype = c_int + + MyCallback = CFUNCTYPE(c_int, c_int) + + f.argtypes = [c_int, MyCallback] + + def callback(value): + #print "called back with", value + self.failUnlessEqual(type(value), int) + return value + + cb = MyCallback(callback) + result = f(self.wrap(-10), self.wrap(cb)) + self.failUnlessEqual(result, -18) + + def test_longlong_callbacks(self): + + f = dll._testfunc_callback_q_qf + f.restype = c_longlong + + MyCallback = CFUNCTYPE(c_longlong, c_longlong) + + f.argtypes = [c_longlong, MyCallback] + + def callback(value): + self.failUnless(isinstance(value, (int, long))) + return value & 0x7FFFFFFF + + cb = MyCallback(callback) + + self.failUnlessEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb)))) + + def test_byval(self): + # without prototype + ptin = POINT(1, 2) + ptout = POINT() + # EXPORT int _testfunc_byval(point in, point *pout) + result = dll._testfunc_byval(ptin, byref(ptout)) + got = result, ptout.x, ptout.y + expected = 3, 1, 2 + self.failUnlessEqual(got, expected) + + # with prototype + ptin = POINT(101, 102) + ptout = POINT() + dll._testfunc_byval.argtypes = (POINT, POINTER(POINT)) + dll._testfunc_byval.restype = c_int + result = dll._testfunc_byval(self.wrap(ptin), byref(ptout)) + got = result, ptout.x, ptout.y + expected = 203, 101, 102 + self.failUnlessEqual(got, expected) + + def test_struct_return_2H(self): + class S2H(Structure): + _fields_ = [("x", c_short), + ("y", c_short)] + dll.ret_2h_func.restype = S2H + dll.ret_2h_func.argtypes = [S2H] + inp = S2H(99, 88) + s2h = dll.ret_2h_func(self.wrap(inp)) + self.failUnlessEqual((s2h.x, s2h.y), (99*2, 88*3)) + + def test_struct_return_8H(self): + class S8I(Structure): + _fields_ = [("a", c_int), + ("b", c_int), + ("c", c_int), + ("d", c_int), + ("e", c_int), + ("f", c_int), + ("g", c_int), + ("h", c_int)] + dll.ret_8i_func.restype = S8I + dll.ret_8i_func.argtypes = [S8I] + inp = S8I(9, 8, 7, 6, 5, 4, 3, 2) + s8i = dll.ret_8i_func(self.wrap(inp)) + self.failUnlessEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h), + (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9)) + +#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +class AsParamWrapper(object): + def __init__(self, param): + self._as_parameter_ = param + +class AsParamWrapperTestCase(BasicWrapTestCase): + wrap = AsParamWrapper + +#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +class AsParamPropertyWrapper(object): + def __init__(self, param): + self._param = param + + def getParameter(self): + return self._param + _as_parameter_ = property(getParameter) + +class AsParamPropertyWrapperTestCase(BasicWrapTestCase): + wrap = AsParamPropertyWrapper + +#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +if __name__ == '__main__': + unittest.main() diff --git a/Lib/ctypes/test/test_functions.py b/Lib/ctypes/test/test_functions.py index bfa0cad911b..759aea7eeee 100644 --- a/Lib/ctypes/test/test_functions.py +++ b/Lib/ctypes/test/test_functions.py @@ -222,6 +222,7 @@ class FunctionTestCase(unittest.TestCase): def callback(v): args.append(v) + return v CallBack = CFUNCTYPE(c_int, c_int) diff --git a/Lib/ctypes/test/test_numbers.py b/Lib/ctypes/test/test_numbers.py index 83003db8f84..c22688dba9b 100644 --- a/Lib/ctypes/test/test_numbers.py +++ b/Lib/ctypes/test/test_numbers.py @@ -19,7 +19,7 @@ def valid_ranges(*types): result.append((min(a, b, c, d), max(a, b, c, d))) return result -ArgType = type(c_int(0)._as_parameter_) +ArgType = type(byref(c_int(0))) unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong] signed_types = [c_byte, c_short, c_int, c_long, c_longlong] @@ -80,19 +80,6 @@ class NumberTestCase(unittest.TestCase): for t in signed_types + unsigned_types + float_types: self.failUnlessEqual(ArgType, type(t.from_param(0))) - def test_as_parameter(self): - # The _as_parameter_ property must also - # be a PyCArgObject instance - for t in signed_types + unsigned_types + float_types: - parm = t()._as_parameter_ - self.failUnlessEqual(ArgType, type(parm)) - - # _as_parameter_ is readonly! - # - # Python 2.3 and 2.4 raise a TypeError when trying to set - # a readonly attribute, 2.5 raises an AttributeError. - self.assertRaises((AttributeError, TypeError), setattr, t(), "_as_parameter_", None) - def test_byref(self): # calling byref returns also a PyCArgObject instance for t in signed_types + unsigned_types + float_types: diff --git a/Lib/ctypes/test/test_prototypes.py b/Lib/ctypes/test/test_prototypes.py index aaaa47a34c4..9f020866f4a 100644 --- a/Lib/ctypes/test/test_prototypes.py +++ b/Lib/ctypes/test/test_prototypes.py @@ -125,13 +125,18 @@ class CharPointersTestCase(unittest.TestCase): self.failUnlessEqual(None, func(c_wchar_p(None))) self.failUnlessEqual(u"123", func(c_wchar_p(u"123"))) -## def test_instance(self): -## func = testdll._testfunc_p_p + def test_instance(self): + func = testdll._testfunc_p_p + func.restype = c_void_p -## class X: -## _as_parameter_ = 0 + class X: + _as_parameter_ = None -## self.failUnlessEqual(0, func(X())) + func.argtypes = c_void_p, + self.failUnlessEqual(None, func(X())) + + func.argtypes = None + self.failUnlessEqual(None, func(X())) try: c_wchar diff --git a/Lib/ctypes/test/test_python_api.py b/Lib/ctypes/test/test_python_api.py index 78e02317cf0..9d134740f5a 100644 --- a/Lib/ctypes/test/test_python_api.py +++ b/Lib/ctypes/test/test_python_api.py @@ -78,5 +78,10 @@ class PythonAPITestCase(unittest.TestCase): # not enough arguments self.failUnlessRaises(TypeError, PyOS_snprintf, buf) + def test_pyobject_repr(self): + self.failUnlessEqual(repr(py_object()), "py_object()") + self.failUnlessEqual(repr(py_object(42)), "py_object(42)") + self.failUnlessEqual(repr(py_object(object)), "py_object(%r)" % object) + if __name__ == "__main__": unittest.main() diff --git a/Lib/distutils/__init__.py b/Lib/distutils/__init__.py index 9c60e54690e..21d34c767bc 100644 --- a/Lib/distutils/__init__.py +++ b/Lib/distutils/__init__.py @@ -12,6 +12,12 @@ used from a setup script as __revision__ = "$Id$" -import sys -__version__ = "%d.%d.%d" % sys.version_info[:3] -del sys +# Distutils version +# +# Please coordinate with Marc-Andre Lemburg when adding +# new features to distutils that would warrant bumping the version number. +# +# In general, major and minor version should loosely follow the Python +# version number the distutils code was shipped with. +# +__version__ = "2.5.0" diff --git a/Lib/idlelib/Bindings.py b/Lib/idlelib/Bindings.py index d24be3f1e23..a3c9fc45f82 100644 --- a/Lib/idlelib/Bindings.py +++ b/Lib/idlelib/Bindings.py @@ -22,9 +22,9 @@ menudefs = [ None, ('_Save', '<>'), ('Save _As...', '<>'), - ('Save Co_py As...', '<>'), + ('Save Cop_y As...', '<>'), None, - ('_Print Window', '<>'), + ('Prin_t Window', '<>'), None, ('_Close', '<>'), ('E_xit', '<>'), diff --git a/Lib/idlelib/CREDITS.txt b/Lib/idlelib/CREDITS.txt index e838c03987c..30561a9ea9f 100644 --- a/Lib/idlelib/CREDITS.txt +++ b/Lib/idlelib/CREDITS.txt @@ -24,8 +24,8 @@ Noam Raphael (Code Context, Call Tips, many other patches), and Chui Tey (RPC integration, debugger integration and persistent breakpoints). Scott David Daniels, Tal Einat, Hernan Foffani, Christos Georgiou, -Martin v. Löwis, Jason Orendorff, Josh Robb, Nigel Rowe, Bruce Sherwood, -and Jeff Shute have submitted useful patches. Thanks, guys! +Jim Jewett, Martin v. Löwis, Jason Orendorff, Josh Robb, Nigel Rowe, +Bruce Sherwood, and Jeff Shute have submitted useful patches. Thanks, guys! For additional details refer to NEWS.txt and Changelog. diff --git a/Lib/idlelib/CodeContext.py b/Lib/idlelib/CodeContext.py index 63cc82cd512..74d5b70240d 100644 --- a/Lib/idlelib/CodeContext.py +++ b/Lib/idlelib/CodeContext.py @@ -15,7 +15,7 @@ import re from sys import maxint as INFINITY BLOCKOPENERS = set(["class", "def", "elif", "else", "except", "finally", "for", - "if", "try", "while"]) + "if", "try", "while", "with"]) UPDATEINTERVAL = 100 # millisec FONTUPDATEINTERVAL = 1000 # millisec diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt index 235963e93f5..3b3d79af33c 100644 --- a/Lib/idlelib/NEWS.txt +++ b/Lib/idlelib/NEWS.txt @@ -1,7 +1,29 @@ +What's New in IDLE 2.6a1? +========================= + +*Release date: XX-XXX-200X* + +- IDLE's version number takes a big jump to match the version number of + the Python release of which it's a part. + + What's New in IDLE 1.2c1? ========================= -*Release date: XX-AUG-2006* +*Release date: 17-AUG-2006* + +- File menu hotkeys: there were three 'p' assignments. Reassign the + 'Save Copy As' and 'Print' hotkeys to 'y' and 't'. Change the + Shell hotkey from 's' to 'l'. + +- IDLE honors new quit() and exit() commands from site.py Quitter() object. + Patch 1540892, Jim Jewett + +- The 'with' statement is now a Code Context block opener. + Patch 1540851, Jim Jewett + +- Retrieval of previous shell command was not always preserving indentation + (since 1.2a1) Patch 1528468 Tal Einat. - Changing tokenize (39046) to detect dedent broke tabnanny check (since 1.2a1) diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/PyShell.py index 25eb446360b..d8befffe889 100644 --- a/Lib/idlelib/PyShell.py +++ b/Lib/idlelib/PyShell.py @@ -478,9 +478,6 @@ class ModifiedInterpreter(InteractiveInterpreter): import sys as _sys _sys.path = %r del _sys - _msg = 'Use File/Exit or your end-of-file key to quit IDLE' - __builtins__.quit = __builtins__.exit = _msg - del _msg \n""" % (sys.path,)) active_seq = None @@ -514,7 +511,10 @@ class ModifiedInterpreter(InteractiveInterpreter): print >>sys.__stderr__, errmsg, what print >>console, errmsg, what # we received a response to the currently active seq number: - self.tkconsole.endexecuting() + try: + self.tkconsole.endexecuting() + except AttributeError: # shell may have closed + pass # Reschedule myself if not self.tkconsole.closing: self.tkconsole.text.after(self.tkconsole.pollinterval, @@ -593,7 +593,7 @@ class ModifiedInterpreter(InteractiveInterpreter): source = source.encode(IOBinding.encoding) except UnicodeError: self.tkconsole.resetoutput() - self.write("Unsupported characters in input") + self.write("Unsupported characters in input\n") return try: # InteractiveInterpreter.runsource() calls its runcode() method, @@ -713,14 +713,17 @@ class ModifiedInterpreter(InteractiveInterpreter): else: exec code in self.locals except SystemExit: - if tkMessageBox.askyesno( - "Exit?", - "Do you want to exit altogether?", - default="yes", - master=self.tkconsole.text): - raise + if not self.tkconsole.closing: + if tkMessageBox.askyesno( + "Exit?", + "Do you want to exit altogether?", + default="yes", + master=self.tkconsole.text): + raise + else: + self.showtraceback() else: - self.showtraceback() + raise except: if use_subprocess: print >> self.tkconsole.stderr, \ @@ -730,7 +733,10 @@ class ModifiedInterpreter(InteractiveInterpreter): self.tkconsole.endexecuting() finally: if not use_subprocess: - self.tkconsole.endexecuting() + try: + self.tkconsole.endexecuting() + except AttributeError: # shell may have closed + pass def write(self, s): "Override base class method" @@ -794,7 +800,7 @@ class PyShell(OutputWindow): if use_subprocess: ms = self.menu_specs if ms[2][0] != "shell": - ms.insert(2, ("shell", "_Shell")) + ms.insert(2, ("shell", "She_ll")) self.interp = ModifiedInterpreter(self) if flist is None: root = Tk() @@ -804,9 +810,6 @@ class PyShell(OutputWindow): # OutputWindow.__init__(self, flist, None, None) # - import __builtin__ - __builtin__.quit = __builtin__.exit = "To exit, type Ctrl-D." - # ## self.config(usetabs=1, indentwidth=8, context_use_ps1=1) self.usetabs = True # indentwidth must be 8 when using tabs. See note in EditorWindow: @@ -1138,21 +1141,27 @@ class PyShell(OutputWindow): return "break" def recall(self, s, event): + # remove leading and trailing empty or whitespace lines + s = re.sub(r'^\s*\n', '' , s) + s = re.sub(r'\n\s*$', '', s) + lines = s.split('\n') self.text.undo_block_start() try: self.text.tag_remove("sel", "1.0", "end") self.text.mark_set("insert", "end-1c") - s = s.strip() - lines = s.split('\n') - prefix = self.text.get("insert linestart","insert").rstrip() - if prefix and prefix[-1]==':': + prefix = self.text.get("insert linestart", "insert") + if prefix.rstrip().endswith(':'): self.newline_and_indent_event(event) - self.text.insert("insert",lines[0].strip()) + prefix = self.text.get("insert linestart", "insert") + self.text.insert("insert", lines[0].strip()) if len(lines) > 1: - self.newline_and_indent_event(event) + orig_base_indent = re.search(r'^([ \t]*)', lines[0]).group(0) + new_base_indent = re.search(r'^([ \t]*)', prefix).group(0) for line in lines[1:]: - self.text.insert("insert", line.strip()) - self.newline_and_indent_event(event) + if line.startswith(orig_base_indent): + # replace orig base indentation with new indentation + line = new_base_indent + line[len(orig_base_indent):] + self.text.insert('insert', '\n'+line.rstrip()) finally: self.text.see("insert") self.text.undo_block_stop() diff --git a/Lib/idlelib/idlever.py b/Lib/idlelib/idlever.py index 07d3d827c9a..f56b4d4404b 100644 --- a/Lib/idlelib/idlever.py +++ b/Lib/idlelib/idlever.py @@ -1 +1 @@ -IDLE_VERSION = "1.2b3" +IDLE_VERSION = "2.6a0" diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 1ef8f4405b6..d5681c8a630 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1337,14 +1337,14 @@ def disable(level): """ root.manager.disable = level -def shutdown(): +def shutdown(handlerList=_handlerList): """ Perform any cleanup actions in the logging system (e.g. flushing buffers). Should be called at application exit. """ - for h in _handlerList[:]: # was _handlers.keys(): + for h in handlerList[:]: #errors might occur, for example, if files are locked #we just ignore them if raiseExceptions is not set try: diff --git a/Lib/logging/config.py b/Lib/logging/config.py index 1d5f8c4a647..a31e8d0eca6 100644 --- a/Lib/logging/config.py +++ b/Lib/logging/config.py @@ -79,7 +79,7 @@ def fileConfig(fname, defaults=None): logging._acquireLock() try: logging._handlers.clear() - logging._handlerList = [] + del logging._handlerList[:] # Handlers add themselves to logging._handlers handlers = _install_handlers(cp, formatters) _install_loggers(cp, handlers) diff --git a/Lib/site.py b/Lib/site.py index da7bfaab21f..92798ca3c5a 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -27,7 +27,7 @@ sys.path more than once. Blank lines and lines beginning with '#' are skipped. Lines starting with 'import' are executed. For example, suppose sys.prefix and sys.exec_prefix are set to -/usr/local and there is a directory /usr/local/lib/python1.5/site-packages +/usr/local and there is a directory /usr/local/lib/python2.5/site-packages with three subdirectories, foo, bar and spam, and two path configuration files, foo.pth and bar.pth. Assume foo.pth contains the following: @@ -44,8 +44,8 @@ and bar.pth contains: Then the following directories are added to sys.path, in this order: - /usr/local/lib/python1.5/site-packages/bar - /usr/local/lib/python1.5/site-packages/foo + /usr/local/lib/python2.5/site-packages/bar + /usr/local/lib/python2.5/site-packages/foo Note that bletch is omitted because it doesn't exist; bar precedes foo because bar.pth comes alphabetically before foo.pth; and spam is @@ -242,6 +242,12 @@ def setquit(): def __repr__(self): return 'Use %s() or %s to exit' % (self.name, eof) def __call__(self, code=None): + # Shells like IDLE catch the SystemExit, but listen when their + # stdin wrapper is closed. + try: + sys.stdin.close() + except: + pass raise SystemExit(code) __builtin__.quit = Quitter('quit') __builtin__.exit = Quitter('exit') diff --git a/Lib/string.py b/Lib/string.py index a5837e94b82..921bd8b1d01 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -35,7 +35,6 @@ printable = digits + letters + punctuation + whitespace # Case conversion helpers # Use str to convert Unicode literal in case of -U -# Note that Cookie.py bogusly uses _idmap :( l = map(chr, xrange(256)) _idmap = str('').join(l) del l diff --git a/Lib/tabnanny.py b/Lib/tabnanny.py index f38a79f8a54..76665ac91a0 100755 --- a/Lib/tabnanny.py +++ b/Lib/tabnanny.py @@ -109,6 +109,10 @@ def check(file): errprint("%r: Token Error: %s" % (file, msg)) return + except IndentationError, msg: + errprint("%r: Indentation Error: %s" % (file, msg)) + return + except NannyNag, nag: badline = nag.get_lineno() line = nag.get_line() diff --git a/Lib/tarfile.py b/Lib/tarfile.py index c185fbd49eb..38cccae1d5e 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -411,9 +411,6 @@ class _Stream: self.buf += self.cmp.flush() if self.mode == "w" and self.buf: - blocks, remainder = divmod(len(self.buf), self.bufsize) - if remainder > 0: - self.buf += NUL * (self.bufsize - remainder) self.fileobj.write(self.buf) self.buf = "" if self.comptype == "gz": diff --git a/Lib/test/exception_hierarchy.txt b/Lib/test/exception_hierarchy.txt index 58131d7aaf7..a03f7bbd718 100644 --- a/Lib/test/exception_hierarchy.txt +++ b/Lib/test/exception_hierarchy.txt @@ -45,3 +45,4 @@ BaseException +-- UserWarning +-- FutureWarning +-- ImportWarning + +-- UnicodeWarning diff --git a/Lib/test/inspect_fodder2.py b/Lib/test/inspect_fodder2.py index f150ec6af5f..3d978cffe3d 100644 --- a/Lib/test/inspect_fodder2.py +++ b/Lib/test/inspect_fodder2.py @@ -88,3 +88,12 @@ extra85 = 'stop' def func88(): # comment return 90 + +# line 92 +def f(): + class X: + def g(): + "doc" + return 42 + return X +method_in_dynamic_class = f().g.im_func diff --git a/Lib/test/output/test_cgi b/Lib/test/output/test_cgi index d5d6f75c376..26eddfa9158 100644 --- a/Lib/test/output/test_cgi +++ b/Lib/test/output/test_cgi @@ -38,3 +38,5 @@ test_cgi Testing log Testing initlog 1 Testing log 2 +Test FieldStorage methods that use readline +Test basic FieldStorage multipart parsing diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index d45ff648510..06beb1d0077 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -60,13 +60,13 @@ class BytesTest(unittest.TestCase): self.assertRaises(ValueError, bytes, [-1]) self.assertRaises(ValueError, bytes, [-sys.maxint]) self.assertRaises(ValueError, bytes, [-sys.maxint-1]) - self.assertRaises(ValueError, bytes, [-sys.maxint-2]) - self.assertRaises(ValueError, bytes, [-10**100]) + self.assertRaises(TypeError, bytes, [-sys.maxint-2]) + self.assertRaises(TypeError, bytes, [-10**100]) self.assertRaises(ValueError, bytes, [256]) self.assertRaises(ValueError, bytes, [257]) self.assertRaises(ValueError, bytes, [sys.maxint]) - self.assertRaises(ValueError, bytes, [sys.maxint+1]) - self.assertRaises(ValueError, bytes, [10**100]) + self.assertRaises(TypeError, bytes, [sys.maxint+1]) + self.assertRaises(TypeError, bytes, [10**100]) def test_repr(self): self.assertEqual(repr(bytes()), "bytes()") diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index f1981161745..85617235d11 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -158,10 +158,21 @@ class BZ2FileTest(BaseTest): sio = StringIO(self.TEXT) bz2f.writelines(sio.readlines()) bz2f.close() + # patch #1535500 + self.assertRaises(ValueError, bz2f.writelines, ["a"]) f = open(self.filename, 'rb') self.assertEqual(self.decompress(f.read()), self.TEXT) f.close() + def testWriteMethodsOnReadOnlyFile(self): + bz2f = BZ2File(self.filename, "w") + bz2f.write("abc") + bz2f.close() + + bz2f = BZ2File(self.filename, "r") + self.assertRaises(IOError, bz2f.write, "a") + self.assertRaises(IOError, bz2f.writelines, ["a"]) + def testSeekForward(self): # "Test BZ2File.seek(150, 0)" self.createTempFile() diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py index 8b0b482b0c1..8fe487bb91d 100644 --- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -2,6 +2,8 @@ from test.test_support import verify, verbose import cgi import os import sys +import tempfile +from StringIO import StringIO class HackedSysModule: # The regression test will have real values in sys.argv, which @@ -203,4 +205,71 @@ def main(): cgi.initlog("%s", "Testing log 3") cgi.log("Testing log 4") + print "Test FieldStorage methods that use readline" + # FieldStorage uses readline, which has the capacity to read all + # contents of the input file into memory; we use readline's size argument + # to prevent that for files that do not contain any newlines in + # non-GET/HEAD requests + class TestReadlineFile: + def __init__(self, file): + self.file = file + self.numcalls = 0 + + def readline(self, size=None): + self.numcalls += 1 + if size: + return self.file.readline(size) + else: + return self.file.readline() + + def __getattr__(self, name): + file = self.__dict__['file'] + a = getattr(file, name) + if not isinstance(a, int): + setattr(self, name, a) + return a + + f = TestReadlineFile(tempfile.TemporaryFile()) + f.write('x' * 256 * 1024) + f.seek(0) + env = {'REQUEST_METHOD':'PUT'} + fs = cgi.FieldStorage(fp=f, environ=env) + # if we're not chunking properly, readline is only called twice + # (by read_binary); if we are chunking properly, it will be called 5 times + # as long as the chunksize is 1 << 16. + verify(f.numcalls > 2) + + print "Test basic FieldStorage multipart parsing" + env = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE':'multipart/form-data; boundary=---------------------------721837373350705526688164684', 'CONTENT_LENGTH':'558'} + postdata = """-----------------------------721837373350705526688164684 +Content-Disposition: form-data; name="id" + +1234 +-----------------------------721837373350705526688164684 +Content-Disposition: form-data; name="title" + + +-----------------------------721837373350705526688164684 +Content-Disposition: form-data; name="file"; filename="test.txt" +Content-Type: text/plain + +Testing 123. + +-----------------------------721837373350705526688164684 +Content-Disposition: form-data; name="submit" + + Add\x20 +-----------------------------721837373350705526688164684-- +""" + fs = cgi.FieldStorage(fp=StringIO(postdata), environ=env) + verify(len(fs.list) == 4) + expect = [{'name':'id', 'filename':None, 'value':'1234'}, + {'name':'title', 'filename':None, 'value':''}, + {'name':'file', 'filename':'test.txt','value':'Testing 123.\n'}, + {'name':'submit', 'filename':None, 'value':' Add '}] + for x in range(len(fs.list)): + for k, exp in expect[x].items(): + got = getattr(fs.list[x], k) + verify(got == exp) + main() diff --git a/Lib/test/test_compiler.py b/Lib/test/test_compiler.py index 1efb6a620fd..81f2ea89250 100644 --- a/Lib/test/test_compiler.py +++ b/Lib/test/test_compiler.py @@ -116,6 +116,13 @@ class CompilerTest(unittest.TestCase): exec c in dct self.assertEquals(dct.get('result'), 3) + def testGenExp(self): + c = compiler.compile('list((i,j) for i in range(3) if i < 3' + ' for j in range(4) if j > 2)', + '', + 'eval') + self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)]) + NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py index 29594475c25..a9b31707a24 100644 --- a/Lib/test/test_format.py +++ b/Lib/test/test_format.py @@ -230,6 +230,14 @@ test_exc(u'no format', '1', TypeError, test_exc(u'no format', u'1', TypeError, "not all arguments converted during string formatting") +class Foobar(long): + def __oct__(self): + # Returning a non-string should not blow up. + return self + 1 + +test_exc('%o', Foobar(), TypeError, + "expected string or Unicode object, long found") + if sys.maxint == 2**31-1: # crashes 2.2.1 and earlier: try: diff --git a/Lib/test/test_index.py b/Lib/test/test_index.py index 45b3b2b6cde..ecb566d5d58 100644 --- a/Lib/test/test_index.py +++ b/Lib/test/test_index.py @@ -1,6 +1,7 @@ import unittest from test import test_support import operator +from sys import maxint class oldstyle: def __index__(self): @@ -10,68 +11,116 @@ class newstyle(object): def __index__(self): return self.ind +class TrapInt(int): + def __index__(self): + return self + +class TrapLong(long): + def __index__(self): + return self + class BaseTestCase(unittest.TestCase): + def setUp(self): + self.o = oldstyle() + self.n = newstyle() + + def test_basic(self): + self.o.ind = -2 + self.n.ind = 2 + self.assertEqual(operator.index(self.o), -2) + self.assertEqual(operator.index(self.n), 2) + + def test_slice(self): + self.o.ind = 1 + self.n.ind = 2 + slc = slice(self.o, self.o, self.o) + check_slc = slice(1, 1, 1) + self.assertEqual(slc.indices(self.o), check_slc.indices(1)) + slc = slice(self.n, self.n, self.n) + check_slc = slice(2, 2, 2) + self.assertEqual(slc.indices(self.n), check_slc.indices(2)) + + def test_wrappers(self): + self.o.ind = 4 + self.n.ind = 5 + self.assertEqual(6 .__index__(), 6) + self.assertEqual(-7L.__index__(), -7) + self.assertEqual(self.o.__index__(), 4) + self.assertEqual(self.n.__index__(), 5) + + def test_subclasses(self): + r = range(10) + self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10]) + self.assertEqual(r[TrapLong(5):TrapLong(10)], r[5:10]) + self.assertEqual(slice(TrapInt()).indices(0), (0,0,1)) + self.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1)) + + def test_error(self): + self.o.ind = 'dumb' + self.n.ind = 'bad' + self.failUnlessRaises(TypeError, operator.index, self.o) + self.failUnlessRaises(TypeError, operator.index, self.n) + self.failUnlessRaises(TypeError, slice(self.o).indices, 0) + self.failUnlessRaises(TypeError, slice(self.n).indices, 0) + + +class SeqTestCase(unittest.TestCase): + # This test case isn't run directly. It just defines common tests + # to the different sequence types below def setUp(self): self.o = oldstyle() self.n = newstyle() self.o2 = oldstyle() self.n2 = newstyle() - def test_basic(self): + def test_index(self): self.o.ind = -2 self.n.ind = 2 - assert(self.seq[self.n] == self.seq[2]) - assert(self.seq[self.o] == self.seq[-2]) - assert(operator.index(self.o) == -2) - assert(operator.index(self.n) == 2) - - def test_error(self): - self.o.ind = 'dumb' - self.n.ind = 'bad' - myfunc = lambda x, obj: obj.seq[x] - self.failUnlessRaises(TypeError, operator.index, self.o) - self.failUnlessRaises(TypeError, operator.index, self.n) - self.failUnlessRaises(TypeError, myfunc, self.o, self) - self.failUnlessRaises(TypeError, myfunc, self.n, self) + self.assertEqual(self.seq[self.n], self.seq[2]) + self.assertEqual(self.seq[self.o], self.seq[-2]) def test_slice(self): self.o.ind = 1 self.o2.ind = 3 self.n.ind = 2 self.n2.ind = 4 - assert(self.seq[self.o:self.o2] == self.seq[1:3]) - assert(self.seq[self.n:self.n2] == self.seq[2:4]) + self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3]) + self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4]) def test_repeat(self): self.o.ind = 3 self.n.ind = 2 - assert(self.seq * self.o == self.seq * 3) - assert(self.seq * self.n == self.seq * 2) - assert(self.o * self.seq == self.seq * 3) - assert(self.n * self.seq == self.seq * 2) + self.assertEqual(self.seq * self.o, self.seq * 3) + self.assertEqual(self.seq * self.n, self.seq * 2) + self.assertEqual(self.o * self.seq, self.seq * 3) + self.assertEqual(self.n * self.seq, self.seq * 2) def test_wrappers(self): - n = self.n - n.ind = 5 - assert n.__index__() == 5 - assert 6 .__index__() == 6 - assert -7L.__index__() == -7 - assert self.seq.__getitem__(n) == self.seq[5] - assert self.seq.__mul__(n) == self.seq * 5 - assert self.seq.__rmul__(n) == self.seq * 5 + self.o.ind = 4 + self.n.ind = 5 + self.assertEqual(self.seq.__getitem__(self.o), self.seq[4]) + self.assertEqual(self.seq.__mul__(self.o), self.seq * 4) + self.assertEqual(self.seq.__rmul__(self.o), self.seq * 4) + self.assertEqual(self.seq.__getitem__(self.n), self.seq[5]) + self.assertEqual(self.seq.__mul__(self.n), self.seq * 5) + self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5) - def test_infinite_recusion(self): - class Trap1(int): - def __index__(self): - return self - class Trap2(long): - def __index__(self): - return self - self.failUnlessRaises(TypeError, operator.getitem, self.seq, Trap1()) - self.failUnlessRaises(TypeError, operator.getitem, self.seq, Trap2()) + def test_subclasses(self): + self.assertEqual(self.seq[TrapInt()], self.seq[0]) + self.assertEqual(self.seq[TrapLong()], self.seq[0]) + + def test_error(self): + self.o.ind = 'dumb' + self.n.ind = 'bad' + indexobj = lambda x, obj: obj.seq[x] + self.failUnlessRaises(TypeError, indexobj, self.o, self) + self.failUnlessRaises(TypeError, indexobj, self.n, self) + sliceobj = lambda x, obj: obj.seq[x:] + self.failUnlessRaises(TypeError, sliceobj, self.o, self) + self.failUnlessRaises(TypeError, sliceobj, self.n, self) -class ListTestCase(BaseTestCase): +class ListTestCase(SeqTestCase): seq = [0,10,20,30,40,50] def test_setdelitem(self): @@ -82,36 +131,36 @@ class ListTestCase(BaseTestCase): del lst[self.n] lst[self.o] = 'X' lst[self.n] = 'Y' - assert lst == list('abYdefghXj') + self.assertEqual(lst, list('abYdefghXj')) lst = [5, 6, 7, 8, 9, 10, 11] lst.__setitem__(self.n, "here") - assert lst == [5, 6, "here", 8, 9, 10, 11] + self.assertEqual(lst, [5, 6, "here", 8, 9, 10, 11]) lst.__delitem__(self.n) - assert lst == [5, 6, 8, 9, 10, 11] + self.assertEqual(lst, [5, 6, 8, 9, 10, 11]) def test_inplace_repeat(self): self.o.ind = 2 self.n.ind = 3 lst = [6, 4] lst *= self.o - assert lst == [6, 4, 6, 4] + self.assertEqual(lst, [6, 4, 6, 4]) lst *= self.n - assert lst == [6, 4, 6, 4] * 3 + self.assertEqual(lst, [6, 4, 6, 4] * 3) lst = [5, 6, 7, 8, 9, 11] l2 = lst.__imul__(self.n) - assert l2 is lst - assert lst == [5, 6, 7, 8, 9, 11] * 3 + self.assert_(l2 is lst) + self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3) -class TupleTestCase(BaseTestCase): +class TupleTestCase(SeqTestCase): seq = (0,10,20,30,40,50) -class StringTestCase(BaseTestCase): +class StringTestCase(SeqTestCase): seq = "this is a test" -class UnicodeTestCase(BaseTestCase): +class UnicodeTestCase(SeqTestCase): seq = u"this is a test" @@ -120,17 +169,54 @@ class XRangeTestCase(unittest.TestCase): def test_xrange(self): n = newstyle() n.ind = 5 - assert xrange(1, 20)[n] == 6 - assert xrange(1, 20).__getitem__(n) == 6 + self.assertEqual(xrange(1, 20)[n], 6) + self.assertEqual(xrange(1, 20).__getitem__(n), 6) + +class OverflowTestCase(unittest.TestCase): + + def setUp(self): + self.pos = 2**100 + self.neg = -self.pos + + def test_large_longs(self): + self.assertEqual(self.pos.__index__(), self.pos) + self.assertEqual(self.neg.__index__(), self.neg) + + def _getitem_helper(self, base): + class GetItem(base): + def __len__(self): + return maxint + def __getitem__(self, key): + return key + def __getslice__(self, i, j): + return i, j + x = GetItem() + self.assertEqual(x[self.pos], self.pos) + self.assertEqual(x[self.neg], self.neg) + self.assertEqual(x[self.neg:self.pos], (-1, maxint)) + self.assertEqual(x[self.neg:self.pos:1].indices(maxint), (0, maxint, 1)) + + def test_getitem(self): + self._getitem_helper(object) + + def test_getitem_classic(self): + class Empty: pass + self._getitem_helper(Empty) + + def test_sequence_repeat(self): + self.failUnlessRaises(OverflowError, lambda: "a" * self.pos) + self.failUnlessRaises(OverflowError, lambda: "a" * self.neg) def test_main(): test_support.run_unittest( + BaseTestCase, ListTestCase, TupleTestCase, StringTestCase, UnicodeTestCase, XRangeTestCase, + OverflowTestCase, ) if __name__ == "__main__": diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 99140d2faaa..e9f9ef1ebbf 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -276,6 +276,9 @@ class TestBuggyCases(GetSourceBase): def test_with_comment_instead_of_docstring(self): self.assertSourceEqual(mod2.func88, 88, 90) + def test_method_in_dynamic_class(self): + self.assertSourceEqual(mod2.method_in_dynamic_class, 95, 97) + # Helper for testing classify_class_attrs. def attrs_wo_objs(cls): return [t[:3] for t in inspect.classify_class_attrs(cls)] diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index fe0512399e7..d6dcc3c0f15 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -493,7 +493,7 @@ def test4(): try: logging._handlers.clear() logging._handlers.update(saved_handlers) - logging._handlerList = saved_handler_list + logging._handlerList[:] = saved_handler_list loggerDict = logging.getLogger().manager.loggerDict loggerDict.clear() loggerDict.update(saved_loggers) @@ -560,7 +560,7 @@ def test5(): try: logging._handlers.clear() logging._handlers.update(saved_handlers) - logging._handlerList = saved_handler_list + logging._handlerList[:] = saved_handler_list loggerDict = logging.getLogger().manager.loggerDict loggerDict.clear() loggerDict.update(saved_loggers) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 04e856fcfe7..18373066628 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -1,4 +1,5 @@ import os +import sys import time import stat import socket @@ -454,7 +455,7 @@ class TestMaildir(TestMailbox): def setUp(self): TestMailbox.setUp(self) - if os.name in ('nt', 'os2'): + if os.name in ('nt', 'os2') or sys.platform == 'cygwin': self._box.colon = '!' def test_add_MM(self): @@ -729,11 +730,13 @@ class _TestMboxMMDF(TestMailbox): # In the parent, sleep a bit to give the child time to acquire # the lock. time.sleep(0.5) - self.assertRaises(mailbox.ExternalClashError, - self._box.lock) + try: + self.assertRaises(mailbox.ExternalClashError, + self._box.lock) + finally: + # Wait for child to exit. Locking should now succeed. + exited_pid, status = os.waitpid(pid, 0) - # Wait for child to exit. Locking should now succeed. - exited_pid, status = os.waitpid(pid, 0) self._box.lock() self._box.unlock() diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index a6267d27f7f..13d02bbc265 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -6,11 +6,16 @@ import os, sys, time if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos': raise TestSkipped, "Can't test signal on %s" % sys.platform +MAX_DURATION = 20 # Entire test should last at most 20 sec. + if verbose: x = '-x' else: x = '+x' + pid = os.getpid() +if verbose: + print "test runner's pid is", pid # Shell script that will send us asynchronous signals script = """ @@ -31,7 +36,7 @@ def handlerA(*args): global a_called a_called = True if verbose: - print "handlerA", args + print "handlerA invoked", args class HandlerBCalled(Exception): pass @@ -40,96 +45,102 @@ def handlerB(*args): global b_called b_called = True if verbose: - print "handlerB", args + print "handlerB invoked", args raise HandlerBCalled, args -MAX_DURATION = 20 -signal.alarm(MAX_DURATION) # Entire test should last at most 20 sec. +# Set up a child to send signals to us (the parent) after waiting long +# enough to receive the alarm. It seems we miss the alarm for some +# reason. This will hopefully stop the hangs on Tru64/Alpha. +# Alas, it doesn't. Tru64 appears to miss all the signals at times, or +# seemingly random subsets of them, and nothing done in force_test_exit +# so far has actually helped. +def force_test_exit(): + # Sigh, both imports seem necessary to avoid errors. + import os + fork_pid = os.fork() + if fork_pid: + # In parent. + return fork_pid + + # In child. + import os, time + try: + # Wait 5 seconds longer than the expected alarm to give enough + # time for the normal sequence of events to occur. This is + # just a stop-gap to try to prevent the test from hanging. + time.sleep(MAX_DURATION + 5) + print >> sys.__stdout__, ' child should not have to kill parent' + for signame in "SIGHUP", "SIGUSR1", "SIGUSR2", "SIGALRM": + os.kill(pid, getattr(signal, signame)) + print >> sys.__stdout__, " child sent", signame, "to", pid + time.sleep(1) + finally: + os._exit(0) + +# Install handlers. hup = signal.signal(signal.SIGHUP, handlerA) usr1 = signal.signal(signal.SIGUSR1, handlerB) usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN) alrm = signal.signal(signal.SIGALRM, signal.default_int_handler) -vereq(signal.getsignal(signal.SIGHUP), handlerA) -vereq(signal.getsignal(signal.SIGUSR1), handlerB) -vereq(signal.getsignal(signal.SIGUSR2), signal.SIG_IGN) - try: - signal.signal(4242, handlerB) - raise TestFailed, 'expected ValueError for invalid signal # to signal()' -except ValueError: - pass -try: - signal.getsignal(4242) - raise TestFailed, 'expected ValueError for invalid signal # to getsignal()' -except ValueError: - pass - -try: - signal.signal(signal.SIGUSR1, None) - raise TestFailed, 'expected TypeError for non-callable' -except TypeError: - pass - -# Set up a child to send an alarm signal to us (the parent) after waiting -# long enough to receive the alarm. It seems we miss the alarm for some -# reason. This will hopefully stop the hangs on Tru64/Alpha. -def force_test_exit(): - # Sigh, both imports seem necessary to avoid errors. - import os - fork_pid = os.fork() - if fork_pid == 0: - # In child - import os, time - try: - # Wait 5 seconds longer than the expected alarm to give enough - # time for the normal sequence of events to occur. This is - # just a stop-gap to prevent the test from hanging. - time.sleep(MAX_DURATION + 5) - print >> sys.__stdout__, ' child should not have to kill parent' - for i in range(3): - os.kill(pid, signal.SIGALARM) - finally: - os._exit(0) - # In parent (or error) - return fork_pid - -try: - os.system(script) + signal.alarm(MAX_DURATION) + vereq(signal.getsignal(signal.SIGHUP), handlerA) + vereq(signal.getsignal(signal.SIGUSR1), handlerB) + vereq(signal.getsignal(signal.SIGUSR2), signal.SIG_IGN) + vereq(signal.getsignal(signal.SIGALRM), signal.default_int_handler) # Try to ensure this test exits even if there is some problem with alarm. - # Tru64/Alpha sometimes hangs and is ultimately killed by the buildbot. + # Tru64/Alpha often hangs and is ultimately killed by the buildbot. fork_pid = force_test_exit() - print "starting pause() loop..." try: + signal.getsignal(4242) + raise TestFailed('expected ValueError for invalid signal # to ' + 'getsignal()') + except ValueError: + pass + + try: + signal.signal(4242, handlerB) + raise TestFailed('expected ValueError for invalid signal # to ' + 'signal()') + except ValueError: + pass + + try: + signal.signal(signal.SIGUSR1, None) + raise TestFailed('expected TypeError for non-callable') + except TypeError: + pass + + # Launch an external script to send us signals. + # We expect the external script to: + # send HUP, which invokes handlerA to set a_called + # send USR1, which invokes handlerB to set b_called and raise + # HandlerBCalled + # send USR2, which is ignored + # + # Then we expect the alarm to go off, and its handler raises + # KeyboardInterrupt, finally getting us out of the loop. + os.system(script) + try: + print "starting pause() loop..." while 1: - if verbose: - print "call pause()..." try: + if verbose: + print "call pause()..." signal.pause() if verbose: print "pause() returned" except HandlerBCalled: if verbose: print "HandlerBCalled exception caught" - else: - pass except KeyboardInterrupt: if verbose: - print "KeyboardInterrupt (assume the alarm() went off)" - - # Forcibly kill the child we created to ping us if there was a test error. - try: - # Make sure we don't kill ourself if there was a fork error. - if fork_pid > 0: - os.kill(fork_pid, signal.SIGKILL) - except: - # If the child killed us, it has probably exited. Killing a - # non-existant process will raise an error which we don't care about. - pass + print "KeyboardInterrupt (the alarm() went off)" if not a_called: print 'HandlerA not called' @@ -138,6 +149,18 @@ try: print 'HandlerB not called' finally: + # Forcibly kill the child we created to ping us if there was a test error. + try: + # Make sure we don't kill ourself if there was a fork error. + if fork_pid > 0: + os.kill(fork_pid, signal.SIGKILL) + except: + # If the child killed us, it has probably exited. Killing a + # non-existent process will raise an error which we don't care about. + pass + + # Restore handlers. + signal.alarm(0) # cancel alarm in case we died early signal.signal(signal.SIGHUP, hup) signal.signal(signal.SIGUSR1, usr1) signal.signal(signal.SIGUSR2, usr2) diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py index dd4532fa4ea..e4cbb2b5888 100644 --- a/Lib/test/test_socketserver.py +++ b/Lib/test/test_socketserver.py @@ -181,10 +181,19 @@ else: if hasattr(os, 'fork') and os.name not in ('os2',): dgramservers.append(ForkingUnixDatagramServer) +def sloppy_cleanup(): + # See http://python.org/sf/1540386 + # We need to reap children here otherwise a child from one server + # can be left running for the next server and cause a test failure. + time.sleep(DELAY) + reap_children() + def testall(): testloop(socket.AF_INET, tcpservers, MyStreamHandler, teststream) + sloppy_cleanup() testloop(socket.AF_INET, udpservers, MyDatagramHandler, testdgram) if hasattr(socket, 'AF_UNIX'): + sloppy_cleanup() testloop(socket.AF_UNIX, streamservers, MyStreamHandler, teststream) # Alas, on Linux (at least) recvfrom() doesn't return a meaningful # client address so this cannot work: diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 8ee0f41ea8b..ebcb8c59f5d 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -324,6 +324,27 @@ class WriteSize0Test(BaseTest): class WriteStreamTest(WriteTest): sep = '|' + def test_padding(self): + self.dst.close() + + if self.comp == "gz": + f = gzip.GzipFile(self.dstname) + s = f.read() + f.close() + elif self.comp == "bz2": + f = bz2.BZ2Decompressor() + s = file(self.dstname).read() + s = f.decompress(s) + self.assertEqual(len(f.unused_data), 0, "trailing data") + else: + f = file(self.dstname) + s = f.read() + f.close() + + self.assertEqual(s.count("\0"), tarfile.RECORDSIZE, + "incorrect zero padding") + + class WriteGNULongTest(unittest.TestCase): """This testcase checks for correct creation of GNU Longname and Longlink extensions. diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 79335eaca06..8614ecb49c9 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -131,6 +131,76 @@ class ThreadTests(unittest.TestCase): threading._DummyThread)) del threading._active[tid] + # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently) + # exposed at the Python level. This test relies on ctypes to get at it. + def test_PyThreadState_SetAsyncExc(self): + try: + import ctypes + except ImportError: + if verbose: + print "test_PyThreadState_SetAsyncExc can't import ctypes" + return # can't do anything + + set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc + + class AsyncExc(Exception): + pass + + exception = ctypes.py_object(AsyncExc) + + # `worker_started` is set by the thread when it's inside a try/except + # block waiting to catch the asynchronously set AsyncExc exception. + # `worker_saw_exception` is set by the thread upon catching that + # exception. + worker_started = threading.Event() + worker_saw_exception = threading.Event() + + class Worker(threading.Thread): + def run(self): + self.id = thread.get_ident() + self.finished = False + + try: + while True: + worker_started.set() + time.sleep(0.1) + except AsyncExc: + self.finished = True + worker_saw_exception.set() + + t = Worker() + t.setDaemon(True) # so if this fails, we don't hang Python at shutdown + t.start() + if verbose: + print " started worker thread" + + # Try a thread id that doesn't make sense. + if verbose: + print " trying nonsensical thread id" + result = set_async_exc(ctypes.c_long(-1), exception) + self.assertEqual(result, 0) # no thread states modified + + # Now raise an exception in the worker thread. + if verbose: + print " waiting for worker thread to get started" + worker_started.wait() + if verbose: + print " verifying worker hasn't exited" + self.assert_(not t.finished) + if verbose: + print " attempting to raise asynch exception in worker" + result = set_async_exc(ctypes.c_long(t.id), exception) + self.assertEqual(result, 1) # one thread state modified + if verbose: + print " waiting for worker to say it caught the exception" + worker_saw_exception.wait(timeout=10) + self.assert_(t.finished) + if verbose: + print " all OK -- joining worker" + if t.finished: + t.join() + # else the thread is still running, and we have no way to kill it + def test_main(): test.test_support.run_unittest(ThreadTests) diff --git a/Lib/test/test_threadsignals.py b/Lib/test/test_threadsignals.py index 51e3d97f495..1f10fe722f5 100644 --- a/Lib/test/test_threadsignals.py +++ b/Lib/test/test_threadsignals.py @@ -49,7 +49,7 @@ class ThreadSignals(unittest.TestCase): # and might be out of order.) If we haven't seen # the signals yet, send yet another signal and # wait for it return. - if signal_blackboard[signal.SIGUSR2]['tripped'] == 0 \ + if signal_blackboard[signal.SIGUSR1]['tripped'] == 0 \ or signal_blackboard[signal.SIGUSR2]['tripped'] == 0: signal.alarm(1) signal.pause() diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 4df854e3599..be7d0c87dcd 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -46,6 +46,69 @@ class TrivialTests(unittest.TestCase): self.assertEquals(urllib2.parse_http_list(string), list) +def test_request_headers_dict(): + """ + The Request.headers dictionary is not a documented interface. It should + stay that way, because the complete set of headers are only accessible + through the .get_header(), .has_header(), .header_items() interface. + However, .headers pre-dates those methods, and so real code will be using + the dictionary. + + The introduction in 2.4 of those methods was a mistake for the same reason: + code that previously saw all (urllib2 user)-provided headers in .headers + now sees only a subset (and the function interface is ugly and incomplete). + A better change would have been to replace .headers dict with a dict + subclass (or UserDict.DictMixin instance?) that preserved the .headers + interface and also provided access to the "unredirected" headers. It's + probably too late to fix that, though. + + + Check .capitalize() case normalization: + + >>> url = "http://example.com" + >>> Request(url, headers={"Spam-eggs": "blah"}).headers["Spam-eggs"] + 'blah' + >>> Request(url, headers={"spam-EggS": "blah"}).headers["Spam-eggs"] + 'blah' + + Currently, Request(url, "Spam-eggs").headers["Spam-Eggs"] raises KeyError, + but that could be changed in future. + + """ + +def test_request_headers_methods(): + """ + Note the case normalization of header names here, to .capitalize()-case. + This should be preserved for backwards-compatibility. (In the HTTP case, + normalization to .title()-case is done by urllib2 before sending headers to + httplib). + + >>> url = "http://example.com" + >>> r = Request(url, headers={"Spam-eggs": "blah"}) + >>> r.has_header("Spam-eggs") + True + >>> r.header_items() + [('Spam-eggs', 'blah')] + >>> r.add_header("Foo-Bar", "baz") + >>> items = r.header_items() + >>> items.sort() + >>> items + [('Foo-bar', 'baz'), ('Spam-eggs', 'blah')] + + Note that e.g. r.has_header("spam-EggS") is currently False, and + r.get_header("spam-EggS") returns None, but that could be changed in + future. + + >>> r.has_header("Not-there") + False + >>> print r.get_header("Not-there") + None + >>> r.get_header("Not-there", "default") + 'default' + + """ + + def test_password_manager(self): """ >>> mgr = urllib2.HTTPPasswordMgr() @@ -676,11 +739,11 @@ class HandlerTests(unittest.TestCase): r = MockResponse(200, "OK", {}, "") newreq = h.do_request_(req) if data is None: # GET - self.assert_("Content-Length" not in req.unredirected_hdrs) - self.assert_("Content-Type" not in req.unredirected_hdrs) + self.assert_("Content-length" not in req.unredirected_hdrs) + self.assert_("Content-type" not in req.unredirected_hdrs) else: # POST - self.assertEqual(req.unredirected_hdrs["Content-Length"], "0") - self.assertEqual(req.unredirected_hdrs["Content-Type"], + self.assertEqual(req.unredirected_hdrs["Content-length"], "0") + self.assertEqual(req.unredirected_hdrs["Content-type"], "application/x-www-form-urlencoded") # XXX the details of Host could be better tested self.assertEqual(req.unredirected_hdrs["Host"], "example.com") @@ -692,8 +755,8 @@ class HandlerTests(unittest.TestCase): req.add_unredirected_header("Host", "baz") req.add_unredirected_header("Spam", "foo") newreq = h.do_request_(req) - self.assertEqual(req.unredirected_hdrs["Content-Length"], "foo") - self.assertEqual(req.unredirected_hdrs["Content-Type"], "bar") + self.assertEqual(req.unredirected_hdrs["Content-length"], "foo") + self.assertEqual(req.unredirected_hdrs["Content-type"], "bar") self.assertEqual(req.unredirected_hdrs["Host"], "baz") self.assertEqual(req.unredirected_hdrs["Spam"], "foo") @@ -847,7 +910,7 @@ class HandlerTests(unittest.TestCase): 407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm) opener.add_handler(auth_handler) opener.add_handler(http_handler) - self._test_basic_auth(opener, auth_handler, "Proxy-Authorization", + self._test_basic_auth(opener, auth_handler, "Proxy-authorization", realm, http_handler, password_manager, "http://acme.example.com:3128/protected", "proxy.example.com:3128", diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index 0586cfddb77..90671be05d4 100644 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -16,12 +16,13 @@ class TestUUID(TestCase): def test_UUID(self): equal = self.assertEqual ascending = [] - for (string, curly, hex, bytes, fields, integer, urn, + for (string, curly, hex, bytes, bytes_le, fields, integer, urn, time, clock_seq, variant, version) in [ ('00000000-0000-0000-0000-000000000000', '{00000000-0000-0000-0000-000000000000}', '00000000000000000000000000000000', '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', + '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', (0, 0, 0, 0, 0, 0), 0, 'urn:uuid:00000000-0000-0000-0000-000000000000', @@ -30,6 +31,7 @@ class TestUUID(TestCase): '{00010203-0405-0607-0809-0a0b0c0d0e0f}', '000102030405060708090a0b0c0d0e0f', '\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f', + '\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f', (0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL), 0x000102030405060708090a0b0c0d0e0fL, 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', @@ -38,6 +40,7 @@ class TestUUID(TestCase): '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}', '02d9e6d59467382e8f9b9300a64ac3cd', '\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', + '\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', (0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL), 0x02d9e6d59467382e8f9b9300a64ac3cdL, 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd', @@ -46,6 +49,7 @@ class TestUUID(TestCase): '{12345678-1234-5678-1234-567812345678}', '12345678123456781234567812345678', '\x12\x34\x56\x78'*4, + '\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78', (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678), 0x12345678123456781234567812345678, 'urn:uuid:12345678-1234-5678-1234-567812345678', @@ -54,6 +58,7 @@ class TestUUID(TestCase): '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}', '6ba7b8109dad11d180b400c04fd430c8', '\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', + '\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', (0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), 0x6ba7b8109dad11d180b400c04fd430c8L, 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8', @@ -62,6 +67,7 @@ class TestUUID(TestCase): '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}', '6ba7b8119dad11d180b400c04fd430c8', '\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', + '\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', (0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), 0x6ba7b8119dad11d180b400c04fd430c8L, 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8', @@ -70,6 +76,7 @@ class TestUUID(TestCase): '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}', '6ba7b8129dad11d180b400c04fd430c8', '\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', + '\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', (0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), 0x6ba7b8129dad11d180b400c04fd430c8L, 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8', @@ -78,6 +85,7 @@ class TestUUID(TestCase): '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}', '6ba7b8149dad11d180b400c04fd430c8', '\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', + '\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', (0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), 0x6ba7b8149dad11d180b400c04fd430c8L, 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8', @@ -86,6 +94,7 @@ class TestUUID(TestCase): '{7d444840-9dc0-11d1-b245-5ffdce74fad2}', '7d4448409dc011d1b2455ffdce74fad2', '\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', + '\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', (0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L), 0x7d4448409dc011d1b2455ffdce74fad2L, 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2', @@ -94,6 +103,7 @@ class TestUUID(TestCase): '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}', 'e902893a9d223c7ea7b8d6e313b71d9f', '\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', + '\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', (0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL), 0xe902893a9d223c7ea7b8d6e313b71d9fL, 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f', @@ -102,6 +112,7 @@ class TestUUID(TestCase): '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}', 'eb4240266f544ef8a4d0bb658a1fc6cf', '\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', + '\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', (0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL), 0xeb4240266f544ef8a4d0bb658a1fc6cfL, 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', @@ -110,6 +121,7 @@ class TestUUID(TestCase): '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}', 'f81d4fae7dec11d0a76500a0c91e6bf6', '\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', + '\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', (0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L), 0xf81d4fae7dec11d0a76500a0c91e6bf6L, 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', @@ -118,6 +130,7 @@ class TestUUID(TestCase): '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}', 'fffefdfcfffefffefffefffefdfcfbfa', '\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', + '\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', (0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL), 0xfffefdfcfffefffefffefffefdfcfbfaL, 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', @@ -126,6 +139,7 @@ class TestUUID(TestCase): '{ffffffff-ffff-ffff-ffff-ffffffffffff}', 'ffffffffffffffffffffffffffffffff', '\xff'*16, + '\xff'*16, (0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL), 0xffffffffffffffffffffffffffffffffL, 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff', @@ -134,12 +148,14 @@ class TestUUID(TestCase): equivalents = [] # Construct each UUID in several different ways. for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex), - uuid.UUID(bytes=bytes), uuid.UUID(fields=fields), - uuid.UUID(int=integer), uuid.UUID(urn)]: + uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le), + uuid.UUID(fields=fields), uuid.UUID(int=integer), + uuid.UUID(urn)]: # Test all conversions and properties of the UUID object. equal(str(u), string) equal(int(u), integer) equal(u.bytes, bytes) + equal(u.bytes_le, bytes_le) equal(u.fields, fields) equal(u.time_low, fields[0]) equal(u.time_mid, fields[1]) @@ -189,6 +205,11 @@ class TestUUID(TestCase): badvalue(lambda: uuid.UUID(bytes='\0'*15)) badvalue(lambda: uuid.UUID(bytes='\0'*17)) + # Badly formed bytes_le. + badvalue(lambda: uuid.UUID(bytes_le='abc')) + badvalue(lambda: uuid.UUID(bytes_le='\0'*15)) + badvalue(lambda: uuid.UUID(bytes_le='\0'*17)) + # Badly formed fields. badvalue(lambda: uuid.UUID(fields=(1,))) badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5))) @@ -221,51 +242,43 @@ class TestUUID(TestCase): uuid.UUID(h) uuid.UUID(hex=h) uuid.UUID(bytes=b) + uuid.UUID(bytes_le=b) uuid.UUID(fields=f) uuid.UUID(int=i) # Wrong number of arguments (positional). badtype(lambda: uuid.UUID()) badtype(lambda: uuid.UUID(h, b)) - badtype(lambda: uuid.UUID(h, b, f)) - badtype(lambda: uuid.UUID(h, b, f, i)) + badtype(lambda: uuid.UUID(h, b, b)) + badtype(lambda: uuid.UUID(h, b, b, f)) + badtype(lambda: uuid.UUID(h, b, b, f, i)) - # Duplicate arguments (named). - badtype(lambda: uuid.UUID(hex=h, bytes=b)) - badtype(lambda: uuid.UUID(hex=h, fields=f)) - badtype(lambda: uuid.UUID(hex=h, int=i)) - badtype(lambda: uuid.UUID(bytes=b, fields=f)) - badtype(lambda: uuid.UUID(bytes=b, int=i)) - badtype(lambda: uuid.UUID(fields=f, int=i)) - badtype(lambda: uuid.UUID(hex=h, bytes=b, fields=f)) - badtype(lambda: uuid.UUID(hex=h, bytes=b, int=i)) - badtype(lambda: uuid.UUID(hex=h, fields=f, int=i)) - badtype(lambda: uuid.UUID(bytes=b, int=i, fields=f)) - badtype(lambda: uuid.UUID(hex=h, bytes=b, int=i, fields=f)) - - # Duplicate arguments (positional and named). - badtype(lambda: uuid.UUID(h, hex=h)) - badtype(lambda: uuid.UUID(h, bytes=b)) - badtype(lambda: uuid.UUID(h, fields=f)) - badtype(lambda: uuid.UUID(h, int=i)) - badtype(lambda: uuid.UUID(h, hex=h, bytes=b)) - badtype(lambda: uuid.UUID(h, hex=h, fields=f)) - badtype(lambda: uuid.UUID(h, hex=h, int=i)) - badtype(lambda: uuid.UUID(h, bytes=b, fields=f)) - badtype(lambda: uuid.UUID(h, bytes=b, int=i)) - badtype(lambda: uuid.UUID(h, fields=f, int=i)) - badtype(lambda: uuid.UUID(h, hex=h, bytes=b, fields=f)) - badtype(lambda: uuid.UUID(h, hex=h, bytes=b, int=i)) - badtype(lambda: uuid.UUID(h, hex=h, fields=f, int=i)) - badtype(lambda: uuid.UUID(h, bytes=b, int=i, fields=f)) - badtype(lambda: uuid.UUID(h, hex=h, bytes=b, int=i, fields=f)) + # Duplicate arguments. + for hh in [[], [('hex', h)]]: + for bb in [[], [('bytes', b)]]: + for bble in [[], [('bytes_le', b)]]: + for ii in [[], [('int', i)]]: + for ff in [[], [('fields', f)]]: + args = dict(hh + bb + bble + ii + ff) + if len(args) != 0: + badtype(lambda: uuid.UUID(h, **args)) + if len(args) != 1: + badtype(lambda: uuid.UUID(**args)) # Immutability. u = uuid.UUID(h) badtype(lambda: setattr(u, 'hex', h)) badtype(lambda: setattr(u, 'bytes', b)) + badtype(lambda: setattr(u, 'bytes_le', b)) badtype(lambda: setattr(u, 'fields', f)) badtype(lambda: setattr(u, 'int', i)) + badtype(lambda: setattr(u, 'time_low', 0)) + badtype(lambda: setattr(u, 'time_mid', 0)) + badtype(lambda: setattr(u, 'time_hi_version', 0)) + badtype(lambda: setattr(u, 'time_hi_version', 0)) + badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0)) + badtype(lambda: setattr(u, 'clock_seq_low', 0)) + badtype(lambda: setattr(u, 'node', 0)) def check_node(self, node, source): individual_group_bit = (node >> 40L) & 1 @@ -356,11 +369,17 @@ class TestUUID(TestCase): def test_uuid1(self): equal = self.assertEqual - # Make sure uuid4() generates UUIDs that are actually version 1. + # Make sure uuid1() generates UUIDs that are actually version 1. for u in [uuid.uuid1() for i in range(10)]: equal(u.variant, uuid.RFC_4122) equal(u.version, 1) + # Make sure the generated UUIDs are actually unique. + uuids = {} + for u in [uuid.uuid1() for i in range(1000)]: + uuids[u] = 1 + equal(len(uuids.keys()), 1000) + # Make sure the supplied node ID appears in the UUID. u = uuid.uuid1(0) equal(u.node, 0) @@ -408,6 +427,12 @@ class TestUUID(TestCase): equal(u.variant, uuid.RFC_4122) equal(u.version, 4) + # Make sure the generated UUIDs are actually unique. + uuids = {} + for u in [uuid.uuid4() for i in range(1000)]: + uuids[u] = 1 + equal(len(uuids.keys()), 1000) + def test_uuid5(self): equal = self.assertEqual diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py index 56e7fedfdc9..250f7910738 100644 --- a/Lib/test/test_xml_etree_c.py +++ b/Lib/test/test_xml_etree_c.py @@ -204,6 +204,17 @@ def check_encoding(encoding): "" % encoding ) +def bug_1534630(): + """ + >>> bob = ET.TreeBuilder() + >>> e = bob.data("data") + >>> e = bob.start("tag", {}) + >>> e = bob.end("tag") + >>> e = bob.close() + >>> serialize(ET, e) + '' + """ + def test_main(): from test import test_xml_etree_c test_support.run_doctest(test_xml_etree_c, verbosity=True) diff --git a/Lib/tokenize.py b/Lib/tokenize.py index a30791c2cdd..a9be4cfe03e 100644 --- a/Lib/tokenize.py +++ b/Lib/tokenize.py @@ -273,7 +273,8 @@ def generate_tokens(readline): while column < indents[-1]: if column not in indents: raise IndentationError( - "unindent does not match any outer indentation level") + "unindent does not match any outer indentation level", + ("", lnum, pos, line)) indents = indents[:-1] yield (DEDENT, '', (lnum, pos), (lnum, pos), line) diff --git a/Lib/trace.py b/Lib/trace.py index e70869c2fc0..7ebed71e5d9 100644 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -179,9 +179,11 @@ def fullmodname(path): # looking in sys.path for the longest matching prefix. We'll # assume that the rest is the package name. + comparepath = os.path.normcase(path) longest = "" for dir in sys.path: - if path.startswith(dir) and path[len(dir)] == os.path.sep: + dir = os.path.normcase(dir) + if comparepath.startswith(dir) and comparepath[len(dir)] == os.sep: if len(dir) > len(longest): longest = dir diff --git a/Lib/urllib2.py b/Lib/urllib2.py index 93cadd7d872..1e19f33d12f 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -263,11 +263,11 @@ class Request: def add_header(self, key, val): # useful for something like authentication - self.headers[key.title()] = val + self.headers[key.capitalize()] = val def add_unredirected_header(self, key, val): # will not be added to a redirected request - self.unredirected_hdrs[key.title()] = val + self.unredirected_hdrs[key.capitalize()] = val def has_header(self, header_name): return (header_name in self.headers or @@ -286,7 +286,7 @@ class Request: class OpenerDirector: def __init__(self): client_version = "Python-urllib/%s" % __version__ - self.addheaders = [('User-Agent', client_version)] + self.addheaders = [('User-agent', client_version)] # manage the individual handlers self.handlers = [] self.handle_open = {} @@ -675,7 +675,7 @@ class ProxyHandler(BaseHandler): if user and password: user_pass = '%s:%s' % (unquote(user), unquote(password)) creds = base64.encodestring(user_pass).strip() - req.add_header('Proxy-Authorization', 'Basic ' + creds) + req.add_header('Proxy-authorization', 'Basic ' + creds) hostport = unquote(hostport) req.set_proxy(hostport, proxy_type) if orig_type == proxy_type: @@ -819,7 +819,7 @@ class HTTPBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler): class ProxyBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler): - auth_header = 'Proxy-Authorization' + auth_header = 'Proxy-authorization' def http_error_407(self, req, fp, code, msg, headers): # http_error_auth_reqed requires that there is no userinfo component in @@ -1022,20 +1022,20 @@ class AbstractHTTPHandler(BaseHandler): if request.has_data(): # POST data = request.get_data() - if not request.has_header('Content-Type'): + if not request.has_header('Content-type'): request.add_unredirected_header( - 'Content-Type', + 'Content-type', 'application/x-www-form-urlencoded') - if not request.has_header('Content-Length'): + if not request.has_header('Content-length'): request.add_unredirected_header( - 'Content-Length', '%d' % len(data)) + 'Content-length', '%d' % len(data)) scheme, sel = splittype(request.get_selector()) sel_host, sel_path = splithost(sel) if not request.has_header('Host'): request.add_unredirected_header('Host', sel_host or host) for name, value in self.parent.addheaders: - name = name.title() + name = name.capitalize() if not request.has_header(name): request.add_unredirected_header(name, value) @@ -1067,6 +1067,8 @@ class AbstractHTTPHandler(BaseHandler): # So make sure the connection gets closed after the (only) # request. headers["Connection"] = "close" + headers = dict( + (name.title(), val) for name, val in headers.items()) try: h.request(req.get_method(), req.get_selector(), req.data, headers) r = h.getresponse() @@ -1217,7 +1219,7 @@ class FileHandler(BaseHandler): modified = email.Utils.formatdate(stats.st_mtime, usegmt=True) mtype = mimetypes.guess_type(file)[0] headers = mimetools.Message(StringIO( - 'Content-Type: %s\nContent-Length: %d\nLast-Modified: %s\n' % + 'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' % (mtype or 'text/plain', size, modified))) if host: host, port = splitport(host) @@ -1272,9 +1274,9 @@ class FTPHandler(BaseHandler): headers = "" mtype = mimetypes.guess_type(req.get_full_url())[0] if mtype: - headers += "Content-Type: %s\n" % mtype + headers += "Content-type: %s\n" % mtype if retrlen is not None and retrlen >= 0: - headers += "Content-Length: %d\n" % retrlen + headers += "Content-length: %d\n" % retrlen sf = StringIO(headers) headers = mimetools.Message(sf) return addinfourl(fp, headers, req.get_full_url()) diff --git a/Lib/uuid.py b/Lib/uuid.py index a6446a16f9f..ae3da25ca55 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -45,8 +45,6 @@ Typical usage: """ __author__ = 'Ka-Ping Yee ' -__date__ = '$Date: 2006/06/12 23:15:40 $'.split()[1].replace('/', '-') -__version__ = '$Revision: 1.30 $'.split()[1] RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [ 'reserved for NCS compatibility', 'specified in RFC 4122', @@ -57,15 +55,21 @@ class UUID(object): UUID objects are immutable, hashable, and usable as dictionary keys. Converting a UUID to a string with str() yields something in the form '12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts - four possible forms: a similar string of hexadecimal digits, or a - string of 16 raw bytes as an argument named 'bytes', or a tuple of - six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and - 48-bit values respectively) as an argument named 'fields', or a single - 128-bit integer as an argument named 'int'. + five possible forms: a similar string of hexadecimal digits, or a tuple + of six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and + 48-bit values respectively) as an argument named 'fields', or a string + of 16 bytes (with all the integer fields in big-endian order) as an + argument named 'bytes', or a string of 16 bytes (with the first three + fields in little-endian order) as an argument named 'bytes_le', or a + single 128-bit integer as an argument named 'int'. UUIDs have these read-only attributes: - bytes the UUID as a 16-byte string + bytes the UUID as a 16-byte string (containing the six + integer fields in big-endian byte order) + + bytes_le the UUID as a 16-byte string (with time_low, time_mid, + and time_hi_version in little-endian byte order) fields a tuple of the six integer fields of the UUID, which are also available as six individual attributes @@ -94,10 +98,11 @@ class UUID(object): when the variant is RFC_4122) """ - def __init__(self, hex=None, bytes=None, fields=None, int=None, - version=None): + def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None, + int=None, version=None): r"""Create a UUID from either a string of 32 hexadecimal digits, - a string of 16 bytes as the 'bytes' argument, a tuple of six + a string of 16 bytes as the 'bytes' argument, a string of 16 bytes + in little-endian order as the 'bytes_le' argument, a tuple of six integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version, 8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as the 'fields' argument, or a single 128-bit integer as the 'int' @@ -109,23 +114,31 @@ class UUID(object): UUID('12345678123456781234567812345678') UUID('urn:uuid:12345678-1234-5678-1234-567812345678') UUID(bytes='\x12\x34\x56\x78'*4) + UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' + + '\x12\x34\x56\x78\x12\x34\x56\x78') UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)) UUID(int=0x12345678123456781234567812345678) - Exactly one of 'hex', 'bytes', 'fields', or 'int' must be given. - The 'version' argument is optional; if given, the resulting UUID - will have its variant and version number set according to RFC 4122, - overriding bits in the given 'hex', 'bytes', 'fields', or 'int'. + Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must + be given. The 'version' argument is optional; if given, the resulting + UUID will have its variant and version set according to RFC 4122, + overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'. """ - if [hex, bytes, fields, int].count(None) != 3: - raise TypeError('need just one of hex, bytes, fields, or int') + if [hex, bytes, bytes_le, fields, int].count(None) != 4: + raise TypeError('need one of hex, bytes, bytes_le, fields, or int') if hex is not None: hex = hex.replace('urn:', '').replace('uuid:', '') hex = hex.strip('{}').replace('-', '') if len(hex) != 32: raise ValueError('badly formed hexadecimal UUID string') int = long(hex, 16) + if bytes_le is not None: + if len(bytes_le) != 16: + raise ValueError('bytes_le is not a 16-char string') + bytes = (bytes_le[3] + bytes_le[2] + bytes_le[1] + bytes_le[0] + + bytes_le[5] + bytes_le[4] + bytes_le[7] + bytes_le[6] + + bytes_le[8:]) if bytes is not None: if len(bytes) != 16: raise ValueError('bytes is not a 16-char string') @@ -194,6 +207,13 @@ class UUID(object): bytes = property(get_bytes) + def get_bytes_le(self): + bytes = self.bytes + return (bytes[3] + bytes[2] + bytes[1] + bytes[0] + + bytes[5] + bytes[4] + bytes[7] + bytes[6] + bytes[8:]) + + bytes_le = property(get_bytes_le) + def get_fields(self): return (self.time_low, self.time_mid, self.time_hi_version, self.clock_seq_hi_variant, self.clock_seq_low, self.node) @@ -448,6 +468,8 @@ def getnode(): if _node is not None: return _node +_last_timestamp = None + def uuid1(node=None, clock_seq=None): """Generate a UUID from a host ID, sequence number, and the current time. If 'node' is not given, getnode() is used to obtain the hardware @@ -460,11 +482,15 @@ def uuid1(node=None, clock_seq=None): _uuid_generate_time(_buffer) return UUID(bytes=_buffer.raw) + global _last_timestamp import time nanoseconds = int(time.time() * 1e9) # 0x01b21dd213814000 is the number of 100-ns intervals between the # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. timestamp = int(nanoseconds/100) + 0x01b21dd213814000L + if timestamp <= _last_timestamp: + timestamp = _last_timestamp + 1 + _last_timestamp = timestamp if clock_seq is None: import random clock_seq = random.randrange(1<<14L) # instead of stable storage diff --git a/Makefile.pre.in b/Makefile.pre.in index 74ce8a815b0..9c8d7baa9d6 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -260,6 +260,7 @@ PYTHON_OBJS= \ Python/modsupport.o \ Python/mystrtoul.o \ Python/mysnprintf.o \ + Python/peephole.o \ Python/pyarena.o \ Python/pyfpe.o \ Python/pystate.o \ diff --git a/Misc/ACKS b/Misc/ACKS index e13b5942e3b..11e95510552 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -412,6 +412,7 @@ Greg McFarlane Michael McLay Gordon McMillan Jay T. Miller +Chris McDonough Andrew McNamara Caolan McNamara Craig McPheeters diff --git a/Misc/PURIFY.README b/Misc/PURIFY.README index a71433206d4..1e5d2ac1921 100644 --- a/Misc/PURIFY.README +++ b/Misc/PURIFY.README @@ -1,6 +1,6 @@ Purify (tm) and Quantify (tm) are commercial software quality -assurance tools available from Rational Software Corporation -. Purify is essentially a memory access +assurance tools available from IBM . +Purify is essentially a memory access verifier and leak detector; Quantify is a C level profiler. The rest of this file assumes you generally know how to use Purify and Quantify, and that you have installed valid licenses for these @@ -20,6 +20,17 @@ Quantify'd interpreter, do this: make PURIFY=quantify +Starting with Python 2.3, pymalloc is enabled by default. This +will cause many supurious warnings. Modify Objects/obmalloc.c +and enable Py_USING_MEMORY_DEBUGGER by uncommenting it. +README.valgrind has more details about why this is necessary. +See below about setting up suppressions. Some tests may not +run well with Purify due to heavy memory or CPU usage. These +tests may include: test_largefile, test_import, and test_long. + +Please report any findings (problems or no warnings) to python-dev@python.org. +It may be useful to submit a bug report for any problems. + When running the regression test (make test), I have found it useful to set my PURIFYOPTIONS environment variable using the following (bash) shell function. Check out the Purify documentation for @@ -52,6 +63,11 @@ following in your .purify file: suppress umr ...; "nismodule.c" suppress umr ...; "pwdmodule.c" +Note: this list is very old and may not be accurate any longer. +It's possible some of these no longer need to be suppressed. +You will also need to suppress warnings (at least umr) +from Py_ADDRESS_IN_RANGE. + This will still leave you with just a few UMR, mostly in the readline library, which you can safely ignore. A lot of work has gone into Python 1.5 to plug as many leaks as possible. diff --git a/Misc/README.klocwork b/Misc/README.klocwork index a22715ed029..6d2f57fc73a 100644 --- a/Misc/README.klocwork +++ b/Misc/README.klocwork @@ -23,4 +23,8 @@ in addition to any analysis. False positives were also annotated so that the comments can be reviewed and reversed if the analysis was incorrect. +A second run was performed on 10-Aug-2006. The tool was tuned to remove +some false positives and perform some additional checks. ~150 new +warnings were produced, primarily related to dereferencing NULL pointers. + Contact python-dev@python.org for more information. diff --git a/Misc/RPM/python-2.5.spec b/Misc/RPM/python-2.6.spec similarity index 99% rename from Misc/RPM/python-2.5.spec rename to Misc/RPM/python-2.6.spec index afbb09e5c27..73eba566a37 100644 --- a/Misc/RPM/python-2.5.spec +++ b/Misc/RPM/python-2.6.spec @@ -6,7 +6,7 @@ # "python2"? #WARNING: Commenting out doesn't work. Last line is what's used. %define config_binsuffix none -%define config_binsuffix 2.5 +%define config_binsuffix 2.6 # Build tkinter? "auto" enables it if /usr/bin/wish exists. #WARNING: Commenting out doesn't work. Last line is what's used. @@ -33,8 +33,8 @@ ################################# %define name python -%define version 2.5b3 -%define libvers 2.5 +%define version 2.6a1 +%define libvers 2.6 %define release 1pydotorg %define __prefix /usr diff --git a/Misc/build.sh b/Misc/build.sh index ff46bbab0f7..d5079ca582c 100755 --- a/Misc/build.sh +++ b/Misc/build.sh @@ -58,7 +58,7 @@ RSYNC_OPTS="-aC -e ssh" PYTHON=$INSTALL_DIR/bin/python # Python options and regression test program that should always be run. -REGRTEST_ARGS="-E -tt $INSTALL_DIR/lib/python2.5/test/regrtest.py" +REGRTEST_ARGS="-E -tt $INSTALL_DIR/lib/python2.6/test/regrtest.py" REFLOG="build/reflog.txt.out" # These tests are not stable and falsely report leaks sometimes. diff --git a/Modules/_bsddb.c b/Modules/_bsddb.c index 2a3ac5c9483..ad459365e80 100644 --- a/Modules/_bsddb.c +++ b/Modules/_bsddb.c @@ -1797,7 +1797,6 @@ DB_join(DBObject* self, PyObject* args) DBC** cursors; DBC* dbc; - if (!PyArg_ParseTuple(args,"O|i:join", &cursorsObj, &flags)) return NULL; @@ -1811,6 +1810,11 @@ DB_join(DBObject* self, PyObject* args) length = PyObject_Length(cursorsObj); cursors = malloc((length+1) * sizeof(DBC*)); + if (!cursors) { + PyErr_NoMemory(); + return NULL; + } + cursors[length] = NULL; for (x=0; xdb->verify(self->db, fileName, dbName, outFile, flags); MYDB_END_ALLOW_THREADS; - if (outFileName) + if (outFile) fclose(outFile); /* DB.verify acts as a DB handle destructor (like close); this was diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c index 405fd7a29ac..4dbceb78be6 100644 --- a/Modules/_codecsmodule.c +++ b/Modules/_codecsmodule.c @@ -192,7 +192,8 @@ escape_encode(PyObject *self, buf = PyString_AS_STRING (str); len = PyString_GET_SIZE (str); memmove(buf, buf+1, len-2); - _PyString_Resize(&str, len-2); + if (_PyString_Resize(&str, len-2) < 0) + return NULL; return codec_tuple(str, PyString_Size(str)); } diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 76d88de0a16..e4aae2d10fc 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -140,6 +140,31 @@ char *conversion_mode_errors = NULL; accessible fields somehow. */ +static PyCArgObject * +StructUnionType_paramfunc(CDataObject *self) +{ + PyCArgObject *parg; + StgDictObject *stgdict; + + parg = new_CArgObject(); + if (parg == NULL) + return NULL; + + parg->tag = 'V'; + stgdict = PyObject_stgdict((PyObject *)self); + assert(stgdict); /* Cannot be NULL for structure/union instances */ + parg->pffi_type = &stgdict->ffi_type_pointer; + /* For structure parameters (by value), parg->value doesn't contain the structure + data itself, instead parg->value.p *points* to the structure's data + See also _ctypes.c, function _call_function_pointer(). + */ + parg->value.p = self->b_ptr; + parg->size = self->b_size; + Py_INCREF(self); + parg->obj = (PyObject *)self; + return parg; +} + static PyObject * StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isStruct) { @@ -172,6 +197,8 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt Py_DECREF(result->tp_dict); result->tp_dict = (PyObject *)dict; + dict->paramfunc = StructUnionType_paramfunc; + fields = PyDict_GetItemString((PyObject *)dict, "_fields_"); if (!fields) { StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base); @@ -287,6 +314,7 @@ static char from_param_doc[] = static PyObject * CDataType_from_param(PyObject *type, PyObject *value) { + PyObject *as_parameter; if (1 == PyObject_IsInstance(value, type)) { Py_INCREF(value); return value; @@ -300,7 +328,6 @@ CDataType_from_param(PyObject *type, PyObject *value) /* If we got a PyCArgObject, we must check if the object packed in it is an instance of the type's dict->proto */ -// if(dict && ob && dict->proto == (PyObject *)ob->ob_type){ if(dict && ob && PyObject_IsInstance(ob, dict->proto)) { Py_INCREF(value); @@ -330,6 +357,13 @@ CDataType_from_param(PyObject *type, PyObject *value) } /* ... and leave the rest */ #endif + + as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); + if (as_parameter) { + value = CDataType_from_param(type, as_parameter); + Py_DECREF(as_parameter); + return value; + } PyErr_Format(PyExc_TypeError, "expected %s instance instead of %s", ((PyTypeObject *)type)->tp_name, @@ -540,6 +574,23 @@ PointerType_SetProto(StgDictObject *stgdict, PyObject *proto) return 0; } +static PyCArgObject * +PointerType_paramfunc(CDataObject *self) +{ + PyCArgObject *parg; + + parg = new_CArgObject(); + if (parg == NULL) + return NULL; + + parg->tag = 'P'; + parg->pffi_type = &ffi_type_pointer; + Py_INCREF(self); + parg->obj = (PyObject *)self; + parg->value.p = *(void **)self->b_ptr; + return parg; +} + static PyObject * PointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { @@ -563,6 +614,7 @@ PointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) stgdict->align = getentry("P")->pffi_type->alignment; stgdict->length = 1; stgdict->ffi_type_pointer = ffi_type_pointer; + stgdict->paramfunc = PointerType_paramfunc; proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */ if (proto && -1 == PointerType_SetProto(stgdict, proto)) { @@ -620,6 +672,7 @@ PointerType_from_param(PyObject *type, PyObject *value) return PyInt_FromLong(0); /* NULL pointer */ typedict = PyType_stgdict(type); + assert(typedict); /* Cannot be NULL for pointer types */ /* If we expect POINTER(), but receive a instance, accept it by calling byref(). @@ -640,6 +693,7 @@ PointerType_from_param(PyObject *type, PyObject *value) the item types are the same. */ StgDictObject *v = PyObject_stgdict(value); + assert(v); /* Cannot be NULL for pointer or array objects */ if (PyObject_IsSubclass(v->proto, typedict->proto)) { Py_INCREF(value); return value; @@ -906,6 +960,19 @@ add_getset(PyTypeObject *type, PyGetSetDef *gsp) return 0; } +static PyCArgObject * +ArrayType_paramfunc(CDataObject *self) +{ + PyCArgObject *p = new_CArgObject(); + if (p == NULL) + return NULL; + p->tag = 'P'; + p->pffi_type = &ffi_type_pointer; + p->value.p = (char *)self->b_ptr; + Py_INCREF(self); + p->obj = (PyObject *)self; + return p; +} static PyObject * ArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) @@ -961,6 +1028,8 @@ ArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_INCREF(proto); stgdict->proto = proto; + stgdict->paramfunc = &ArrayType_paramfunc; + /* Arrays are passed as pointers to function calls. */ stgdict->ffi_type_pointer = ffi_type_pointer; @@ -1055,6 +1124,7 @@ static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv"; static PyObject * c_wchar_p_from_param(PyObject *type, PyObject *value) { + PyObject *as_parameter; #if (PYTHON_API_VERSION < 1012) # error not supported #endif @@ -1067,6 +1137,8 @@ c_wchar_p_from_param(PyObject *type, PyObject *value) struct fielddesc *fd = getentry("Z"); parg = new_CArgObject(); + if (parg == NULL) + return NULL; parg->pffi_type = &ffi_type_pointer; parg->tag = 'Z'; parg->obj = fd->setfunc(&parg->value, value, 0); @@ -1083,7 +1155,9 @@ c_wchar_p_from_param(PyObject *type, PyObject *value) if (ArrayObject_Check(value) || PointerObject_Check(value)) { /* c_wchar array instance or pointer(c_wchar(...)) */ StgDictObject *dt = PyObject_stgdict(value); - StgDictObject *dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL; + StgDictObject *dict; + assert(dt); /* Cannot be NULL for pointer or array objects */ + dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL; if (dict && (dict->setfunc == getentry("u")->setfunc)) { Py_INCREF(value); return value; @@ -1098,6 +1172,13 @@ c_wchar_p_from_param(PyObject *type, PyObject *value) return value; } } + + as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); + if (as_parameter) { + value = c_wchar_p_from_param(type, as_parameter); + Py_DECREF(as_parameter); + return value; + } /* XXX better message */ PyErr_SetString(PyExc_TypeError, "wrong type"); @@ -1107,6 +1188,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value) static PyObject * c_char_p_from_param(PyObject *type, PyObject *value) { + PyObject *as_parameter; #if (PYTHON_API_VERSION < 1012) # error not supported #endif @@ -1119,6 +1201,8 @@ c_char_p_from_param(PyObject *type, PyObject *value) struct fielddesc *fd = getentry("z"); parg = new_CArgObject(); + if (parg == NULL) + return NULL; parg->pffi_type = &ffi_type_pointer; parg->tag = 'z'; parg->obj = fd->setfunc(&parg->value, value, 0); @@ -1135,7 +1219,9 @@ c_char_p_from_param(PyObject *type, PyObject *value) if (ArrayObject_Check(value) || PointerObject_Check(value)) { /* c_char array instance or pointer(c_char(...)) */ StgDictObject *dt = PyObject_stgdict(value); - StgDictObject *dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL; + StgDictObject *dict; + assert(dt); /* Cannot be NULL for pointer or array objects */ + dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL; if (dict && (dict->setfunc == getentry("c")->setfunc)) { Py_INCREF(value); return value; @@ -1150,6 +1236,13 @@ c_char_p_from_param(PyObject *type, PyObject *value) return value; } } + + as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); + if (as_parameter) { + value = c_char_p_from_param(type, as_parameter); + Py_DECREF(as_parameter); + return value; + } /* XXX better message */ PyErr_SetString(PyExc_TypeError, "wrong type"); @@ -1160,6 +1253,7 @@ static PyObject * c_void_p_from_param(PyObject *type, PyObject *value) { StgDictObject *stgd; + PyObject *as_parameter; #if (PYTHON_API_VERSION < 1012) # error not supported #endif @@ -1176,6 +1270,8 @@ c_void_p_from_param(PyObject *type, PyObject *value) struct fielddesc *fd = getentry("P"); parg = new_CArgObject(); + if (parg == NULL) + return NULL; parg->pffi_type = &ffi_type_pointer; parg->tag = 'P'; parg->obj = fd->setfunc(&parg->value, value, 0); @@ -1191,6 +1287,8 @@ c_void_p_from_param(PyObject *type, PyObject *value) struct fielddesc *fd = getentry("z"); parg = new_CArgObject(); + if (parg == NULL) + return NULL; parg->pffi_type = &ffi_type_pointer; parg->tag = 'z'; parg->obj = fd->setfunc(&parg->value, value, 0); @@ -1206,6 +1304,8 @@ c_void_p_from_param(PyObject *type, PyObject *value) struct fielddesc *fd = getentry("Z"); parg = new_CArgObject(); + if (parg == NULL) + return NULL; parg->pffi_type = &ffi_type_pointer; parg->tag = 'Z'; parg->obj = fd->setfunc(&parg->value, value, 0); @@ -1242,6 +1342,8 @@ c_void_p_from_param(PyObject *type, PyObject *value) CFuncPtrObject *func; func = (CFuncPtrObject *)value; parg = new_CArgObject(); + if (parg == NULL) + return NULL; parg->pffi_type = &ffi_type_pointer; parg->tag = 'P'; Py_INCREF(value); @@ -1269,6 +1371,13 @@ c_void_p_from_param(PyObject *type, PyObject *value) return (PyObject *)parg; } } + + as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); + if (as_parameter) { + value = c_void_p_from_param(type, as_parameter); + Py_DECREF(as_parameter); + return value; + } /* XXX better message */ PyErr_SetString(PyExc_TypeError, "wrong type"); @@ -1355,6 +1464,33 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject return (PyObject *)result; } +static PyCArgObject * +SimpleType_paramfunc(CDataObject *self) +{ + StgDictObject *dict; + char *fmt; + PyCArgObject *parg; + struct fielddesc *fd; + + dict = PyObject_stgdict((PyObject *)self); + assert(dict); /* Cannot be NULL for CDataObject instances */ + fmt = PyString_AsString(dict->proto); + assert(fmt); + + fd = getentry(fmt); + assert(fd); + + parg = new_CArgObject(); + if (parg == NULL) + return NULL; + + parg->tag = fmt[0]; + parg->pffi_type = fd->pffi_type; + Py_INCREF(self); + parg->obj = (PyObject *)self; + memcpy(&parg->value, self->b_ptr, self->b_size); + return parg; +} static PyObject * SimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) @@ -1404,6 +1540,8 @@ SimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) stgdict->size = fmt->pffi_type->size; stgdict->setfunc = fmt->setfunc; stgdict->getfunc = fmt->getfunc; + + stgdict->paramfunc = SimpleType_paramfunc; /* if (result->tp_base != &Simple_Type) { stgdict->setfunc = NULL; @@ -1502,23 +1640,6 @@ SimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) /* * This is a *class method*. * Convert a parameter into something that ConvParam can handle. - * - * This is either an instance of the requested type, a Python integer, or a - * 'magic' 3-tuple. - * - * (These are somewhat related to Martin v. Loewis 'Enhanced Argument Tuples', - * described in PEP 286.) - * - * The tuple must contain - * - * - a format character, currently 'ifdqc' are understood - * which will inform ConvParam about how to push the argument on the stack. - * - * - a corresponding Python object: i - integer, f - float, d - float, - * q - longlong, c - integer - * - * - any object which can be used to keep the original parameter alive - * as long as the tuple lives. */ static PyObject * SimpleType_from_param(PyObject *type, PyObject *value) @@ -1527,6 +1648,7 @@ SimpleType_from_param(PyObject *type, PyObject *value) char *fmt; PyCArgObject *parg; struct fielddesc *fd; + PyObject *as_parameter; /* If the value is already an instance of the requested type, we can use it as is */ @@ -1552,11 +1674,20 @@ SimpleType_from_param(PyObject *type, PyObject *value) parg->tag = fmt[0]; parg->pffi_type = fd->pffi_type; parg->obj = fd->setfunc(&parg->value, value, 0); - if (parg->obj == NULL) { - Py_DECREF(parg); - return NULL; + if (parg->obj) + return (PyObject *)parg; + PyErr_Clear(); + Py_DECREF(parg); + + as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); + if (as_parameter) { + value = SimpleType_from_param(type, as_parameter); + Py_DECREF(as_parameter); + return value; } - return (PyObject *)parg; + PyErr_SetString(PyExc_TypeError, + "wrong type"); + return NULL; } static PyMethodDef SimpleType_methods[] = { @@ -1721,6 +1852,23 @@ make_funcptrtype_dict(StgDictObject *stgdict) } +static PyCArgObject * +CFuncPtrType_paramfunc(CDataObject *self) +{ + PyCArgObject *parg; + + parg = new_CArgObject(); + if (parg == NULL) + return NULL; + + parg->tag = 'P'; + parg->pffi_type = &ffi_type_pointer; + Py_INCREF(self); + parg->obj = (PyObject *)self; + parg->value.p = *(void **)self->b_ptr; + return parg; +} + static PyObject * CFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { @@ -1732,6 +1880,8 @@ CFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (!stgdict) return NULL; + stgdict->paramfunc = CFuncPtrType_paramfunc; + /* create the new instance (which is a class, since we are a metatype!) */ result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); @@ -1921,6 +2071,7 @@ static int CData_clear(CDataObject *self) { StgDictObject *dict = PyObject_stgdict((PyObject *)self); + assert(dict); /* Cannot be NULL for CDataObject instances */ Py_CLEAR(self->b_objects); if ((self->b_needsfree) && ((size_t)dict->size > sizeof(self->b_value))) @@ -2218,7 +2369,9 @@ _CData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value, StgDictObject *p1, *p2; PyObject *keep; p1 = PyObject_stgdict(value); + assert(p1); /* Cannot be NULL for array instances */ p2 = PyType_stgdict(type); + assert(p2); /* Cannot be NULL for pointer types */ if (p1->proto != p2->proto) { PyErr_Format(PyExc_TypeError, @@ -2308,23 +2461,6 @@ GenericCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds) CFuncPtr_Type */ -static PyObject * -CFuncPtr_as_parameter(CDataObject *self) -{ - PyCArgObject *parg; - - parg = new_CArgObject(); - if (parg == NULL) - return NULL; - - parg->tag = 'P'; - parg->pffi_type = &ffi_type_pointer; - Py_INCREF(self); - parg->obj = (PyObject *)self; - parg->value.p = *(void **)self->b_ptr; - return (PyObject *)parg; -} - static int CFuncPtr_set_errcheck(CFuncPtrObject *self, PyObject *ob) { @@ -2384,7 +2520,7 @@ CFuncPtr_get_restype(CFuncPtrObject *self) return self->restype; } dict = PyObject_stgdict((PyObject *)self); - assert(dict); + assert(dict); /* Cannot be NULL for CFuncPtrObject instances */ if (dict->restype) { Py_INCREF(dict->restype); return dict->restype; @@ -2426,7 +2562,7 @@ CFuncPtr_get_argtypes(CFuncPtrObject *self) return self->argtypes; } dict = PyObject_stgdict((PyObject *)self); - assert(dict); + assert(dict); /* Cannot be NULL for CFuncPtrObject instances */ if (dict->argtypes) { Py_INCREF(dict->argtypes); return dict->argtypes; @@ -2444,9 +2580,6 @@ static PyGetSetDef CFuncPtr_getsets[] = { { "argtypes", (getter)CFuncPtr_get_argtypes, (setter)CFuncPtr_set_argtypes, "specify the argument types", NULL }, - { "_as_parameter_", (getter)CFuncPtr_as_parameter, NULL, - "return a magic value so that this can be converted to a C parameter (readonly)", - NULL }, { NULL, NULL } }; @@ -2475,6 +2608,8 @@ static PPROC FindAddress(void *handle, char *name, PyObject *type) where n is 0, 4, 8, 12, ..., 128 */ mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */ + if (!mangled_name) + return NULL; for (i = 0; i < 32; ++i) { sprintf(mangled_name, "_%s@%d", name, i*4); address = (PPROC)GetProcAddress(handle, mangled_name); @@ -2520,8 +2655,12 @@ static int _validate_paramflags(PyTypeObject *type, PyObject *paramflags) { int i, len; - StgDictObject *dict = PyType_stgdict((PyObject *)type); - PyObject *argtypes = dict->argtypes; + StgDictObject *dict; + PyObject *argtypes; + + dict = PyType_stgdict((PyObject *)type); + assert(dict); /* Cannot be NULL. 'type' is a CFuncPtr type. */ + argtypes = dict->argtypes; if (paramflags == NULL || dict->argtypes == NULL) return 1; @@ -2744,6 +2883,8 @@ CFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (ptr == NULL) return NULL; ob = (CDataObject *)GenericCData_new(type, args, kwds); + if (ob == NULL) + return NULL; *(void **)ob->b_ptr = ptr; return (PyObject *)ob; } @@ -2791,6 +2932,8 @@ CFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return NULL; self = (CFuncPtrObject *)GenericCData_new(type, args, kwds); + if (self == NULL) + return NULL; Py_INCREF(callable); self->callable = callable; @@ -2987,6 +3130,13 @@ _build_callargs(CFuncPtrObject *self, PyObject *argtypes, } ob = PyTuple_GET_ITEM(argtypes, i); dict = PyType_stgdict(ob); + if (dict == NULL) { + /* Cannot happen: _validate_paramflags() + would not accept such an object */ + PyErr_Format(PyExc_RuntimeError, + "NULL stgdict unexpected"); + goto error; + } if (PyString_Check(dict->proto)) { PyErr_Format( PyExc_TypeError, @@ -3129,7 +3279,7 @@ CFuncPtr_call(CFuncPtrObject *self, PyObject *inargs, PyObject *kwds) int outmask; unsigned int numretvals; - assert(dict); /* if not, it's a bug */ + assert(dict); /* Cannot be NULL for CFuncPtrObject instances */ restype = self->restype ? self->restype : dict->restype; converters = self->converters ? self->converters : dict->converters; checker = self->checker ? self->checker : dict->checker; @@ -3349,30 +3499,6 @@ IBUG(char *msg) return -1; } -static PyObject * -Struct_as_parameter(CDataObject *self) -{ - PyCArgObject *parg; - StgDictObject *stgdict; - - parg = new_CArgObject(); - if (parg == NULL) - return NULL; - - parg->tag = 'V'; - stgdict = PyObject_stgdict((PyObject *)self); - parg->pffi_type = &stgdict->ffi_type_pointer; - /* For structure parameters (by value), parg->value doesn't contain the structure - data itself, instead parg->value.p *points* to the structure's data - See also _ctypes.c, function _call_function_pointer(). - */ - parg->value.p = self->b_ptr; - parg->size = self->b_size; - Py_INCREF(self); - parg->obj = (PyObject *)self; - return (PyObject *)parg; -} - static int Struct_init(PyObject *self, PyObject *args, PyObject *kwds) { @@ -3447,13 +3573,6 @@ Struct_init(PyObject *self, PyObject *args, PyObject *kwds) return 0; } -static PyGetSetDef Struct_getsets[] = { - { "_as_parameter_", (getter)Struct_as_parameter, NULL, - "return a magic value so that this can be converted to a C parameter (readonly)", - NULL }, - { NULL, NULL } -}; - static PyTypeObject Struct_Type = { PyObject_HEAD_INIT(NULL) 0, @@ -3485,7 +3604,7 @@ static PyTypeObject Struct_Type = { 0, /* tp_iternext */ 0, /* tp_methods */ 0, /* tp_members */ - Struct_getsets, /* tp_getset */ + 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ @@ -3528,7 +3647,7 @@ static PyTypeObject Union_Type = { 0, /* tp_iternext */ 0, /* tp_methods */ 0, /* tp_members */ - Struct_getsets, /* tp_getset */ + 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ @@ -3581,7 +3700,7 @@ Array_item(PyObject *_self, Py_ssize_t index) } stgdict = PyObject_stgdict((PyObject *)self); - assert(stgdict); + assert(stgdict); /* Cannot be NULL for array instances */ /* Would it be clearer if we got the item size from stgdict->proto's stgdict? */ @@ -3612,8 +3731,11 @@ Array_slice(PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh) len = ihigh - ilow; stgdict = PyObject_stgdict((PyObject *)self); + assert(stgdict); /* Cannot be NULL for array object instances */ proto = stgdict->proto; itemdict = PyType_stgdict(proto); + assert(itemdict); /* proto is the item type of the array, a ctypes + type, so this cannot be NULL */ if (itemdict->getfunc == getentry("c")->getfunc) { char *ptr = (char *)self->b_ptr; return PyString_FromStringAndSize(ptr + ilow, len); @@ -3650,6 +3772,7 @@ Array_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value) } stgdict = PyObject_stgdict((PyObject *)self); + assert(stgdict); /* Cannot be NULL for array object instances */ if (index < 0 || index >= stgdict->length) { PyErr_SetString(PyExc_IndexError, "invalid index"); @@ -3726,26 +3849,6 @@ static PySequenceMethods Array_as_sequence = { 0, /* sq_inplace_repeat; */ }; -static PyObject * -Array_as_parameter(CDataObject *self) -{ - PyCArgObject *p = new_CArgObject(); - if (p == NULL) - return NULL; - p->tag = 'P'; - p->pffi_type = &ffi_type_pointer; - p->value.p = (char *)self->b_ptr; - Py_INCREF(self); - p->obj = (PyObject *)self; - return (PyObject *)p; -} - -static PyGetSetDef Array_getsets[] = { - { "_as_parameter_", (getter)Array_as_parameter, - (setter)NULL, "convert to a parameter", NULL }, - { NULL }, -}; - PyTypeObject Array_Type = { PyObject_HEAD_INIT(NULL) 0, @@ -3777,7 +3880,7 @@ PyTypeObject Array_Type = { 0, /* tp_iternext */ 0, /* tp_methods */ 0, /* tp_members */ - Array_getsets, /* tp_getset */ + 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ @@ -3861,6 +3964,7 @@ Simple_set_value(CDataObject *self, PyObject *value) PyObject *result; StgDictObject *dict = PyObject_stgdict((PyObject *)self); + assert(dict); /* Cannot be NULL for CDataObject instances */ assert(dict->setfunc); result = dict->setfunc(self->b_ptr, value, dict->size); if (!result) @@ -3886,40 +3990,14 @@ Simple_get_value(CDataObject *self) { StgDictObject *dict; dict = PyObject_stgdict((PyObject *)self); + assert(dict); /* Cannot be NULL for CDataObject instances */ assert(dict->getfunc); - dict = PyObject_stgdict((PyObject *)self); return dict->getfunc(self->b_ptr, self->b_size); } -static PyObject * -Simple_as_parameter(CDataObject *self) -{ - StgDictObject *dict = PyObject_stgdict((PyObject *)self); - char *fmt = PyString_AsString(dict->proto); - PyCArgObject *parg; - struct fielddesc *fd; - - fd = getentry(fmt); - assert(fd); - - parg = new_CArgObject(); - if (parg == NULL) - return NULL; - - parg->tag = fmt[0]; - parg->pffi_type = fd->pffi_type; - Py_INCREF(self); - parg->obj = (PyObject *)self; - memcpy(&parg->value, self->b_ptr, self->b_size); - return (PyObject *)parg; -} - static PyGetSetDef Simple_getsets[] = { { "value", (getter)Simple_get_value, (setter)Simple_set_value, "current value", NULL }, - { "_as_parameter_", (getter)Simple_as_parameter, NULL, - "return a magic value so that this can be converted to a C parameter (readonly)", - NULL }, { NULL, NULL } }; @@ -4085,12 +4163,14 @@ Pointer_item(PyObject *_self, Py_ssize_t index) } stgdict = PyObject_stgdict((PyObject *)self); - assert(stgdict); - assert(stgdict->proto); + assert(stgdict); /* Cannot be NULL for pointer object instances */ proto = stgdict->proto; - /* XXXXXX MAKE SURE PROTO IS NOT NULL! */ + assert(proto); itemdict = PyType_stgdict(proto); + assert(itemdict); /* proto is the item type of the pointer, a ctypes + type, so this cannot be NULL */ + size = itemdict->size; offset = index * itemdict->size; @@ -4120,12 +4200,15 @@ Pointer_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value) } stgdict = PyObject_stgdict((PyObject *)self); - assert(stgdict); - assert(stgdict->proto); + assert(stgdict); /* Cannot be NULL fr pointer instances */ proto = stgdict->proto; - /* XXXXXX MAKE SURE PROTO IS NOT NULL! */ + assert(proto); + itemdict = PyType_stgdict(proto); + assert(itemdict); /* Cannot be NULL because the itemtype of a pointer + is always a ctypes type */ + size = itemdict->size; offset = index * itemdict->size; @@ -4145,7 +4228,7 @@ Pointer_get_contents(CDataObject *self, void *closure) } stgdict = PyObject_stgdict((PyObject *)self); - assert(stgdict); + assert(stgdict); /* Cannot be NULL fr pointer instances */ return CData_FromBaseObj(stgdict->proto, (PyObject *)self, 0, *(void **)self->b_ptr); @@ -4164,7 +4247,7 @@ Pointer_set_contents(CDataObject *self, PyObject *value, void *closure) return -1; } stgdict = PyObject_stgdict((PyObject *)self); - /* should have been catched in Pointer_new() */ + assert(stgdict); /* Cannot be NULL fr pointer instances */ assert(stgdict->proto); if (!CDataObject_Check(value) || 0 == PyObject_IsInstance(value, stgdict->proto)) { @@ -4193,30 +4276,10 @@ Pointer_set_contents(CDataObject *self, PyObject *value, void *closure) return KeepRef(self, 0, keep); } -static PyObject * -Pointer_as_parameter(CDataObject *self) -{ - PyCArgObject *parg; - - parg = new_CArgObject(); - if (parg == NULL) - return NULL; - - parg->tag = 'P'; - parg->pffi_type = &ffi_type_pointer; - Py_INCREF(self); - parg->obj = (PyObject *)self; - parg->value.p = *(void **)self->b_ptr; - return (PyObject *)parg; -} - static PyGetSetDef Pointer_getsets[] = { { "contents", (getter)Pointer_get_contents, (setter)Pointer_set_contents, "the object this pointer points to (read-write)", NULL }, - { "_as_parameter_", (getter)Pointer_as_parameter, NULL, - "return a magic value so that this can be converted to a C parameter (readonly)", - NULL }, { NULL, NULL } }; @@ -4260,8 +4323,11 @@ Pointer_slice(PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh) len = ihigh - ilow; stgdict = PyObject_stgdict((PyObject *)self); + assert(stgdict); /* Cannot be NULL fr pointer instances */ proto = stgdict->proto; + assert(proto); itemdict = PyType_stgdict(proto); + assert(itemdict); if (itemdict->getfunc == getentry("c")->getfunc) { char *ptr = *(char **)self->b_ptr; return PyString_FromStringAndSize(ptr + ilow, len); @@ -4676,7 +4742,7 @@ init_ctypes(void) #endif PyModule_AddObject(m, "FUNCFLAG_CDECL", PyInt_FromLong(FUNCFLAG_CDECL)); PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyInt_FromLong(FUNCFLAG_PYTHONAPI)); - PyModule_AddStringConstant(m, "__version__", "1.0.0"); + PyModule_AddStringConstant(m, "__version__", "1.0.1"); PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove)); PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset)); diff --git a/Modules/_ctypes/_ctypes_test.c b/Modules/_ctypes/_ctypes_test.c index 99cc7a96348..7331d01dcd5 100644 --- a/Modules/_ctypes/_ctypes_test.c +++ b/Modules/_ctypes/_ctypes_test.c @@ -197,18 +197,6 @@ EXPORT(PY_LONG_LONG) _testfunc_callback_q_qf(PY_LONG_LONG value, #endif -EXPORT(int) _testfunc_ppp(char ***p) -{ - static char message[] = "Hello, World"; - if (p) { - *p = (char **)malloc(sizeof(char *)); - printf("malloc returned %p\n", *p); - **p = message; - return 1; - } - return 0; -} - typedef struct { char *name; char *value; diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index c091d88364a..c8e669a86e6 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -205,7 +205,7 @@ if (x == NULL) _AddTraceback(what, __FILE__, __LINE__ - 1), PyErr_Print() result = PyObject_CallObject(callable, arglist); CHECK("'calling callback function'", result); - if ((restype != &ffi_type_void) && result && result != Py_None) { + if ((restype != &ffi_type_void) && result) { PyObject *keep; assert(setfunc); #ifdef WORDS_BIGENDIAN @@ -225,13 +225,13 @@ if (x == NULL) _AddTraceback(what, __FILE__, __LINE__ - 1), PyErr_Print() itself knows how to manage the refcount of these objects. */ if (keep == NULL) /* Could not convert callback result. */ - PyErr_WriteUnraisable(Py_None); + PyErr_WriteUnraisable(callable); else if (keep == Py_None) /* Nothing to keep */ Py_DECREF(keep); else if (setfunc != getentry("O")->setfunc) { if (-1 == PyErr_Warn(PyExc_RuntimeWarning, "memory leak in callback function.")) - PyErr_WriteUnraisable(Py_None); + PyErr_WriteUnraisable(callable); } } Py_XDECREF(result); diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index d6f875b9460..e0765e917cb 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -465,7 +465,21 @@ struct argument { */ static int ConvParam(PyObject *obj, int index, struct argument *pa) { + StgDictObject *dict; pa->keep = NULL; /* so we cannot forget it later */ + + dict = PyObject_stgdict(obj); + if (dict) { + PyCArgObject *carg; + assert(dict->paramfunc); + /* If it has an stgdict, it is a CDataObject */ + carg = dict->paramfunc((CDataObject *)obj); + pa->ffi_type = carg->pffi_type; + memcpy(&pa->value, &carg->value, sizeof(pa->value)); + pa->keep = (PyObject *)carg; + return 0; + } + if (PyCArg_CheckExact(obj)) { PyCArgObject *carg = (PyCArgObject *)obj; pa->ffi_type = carg->pffi_type; @@ -548,25 +562,12 @@ static int ConvParam(PyObject *obj, int index, struct argument *pa) as parameters (they have to expose the '_as_parameter_' attribute) */ - if (arg == 0) { - PyErr_Format(PyExc_TypeError, - "Don't know how to convert parameter %d", index); - return -1; + if (arg) { + int result; + result = ConvParam(arg, index, pa); + Py_DECREF(arg); + return result; } - if (PyCArg_CheckExact(arg)) { - PyCArgObject *carg = (PyCArgObject *)arg; - pa->ffi_type = carg->pffi_type; - memcpy(&pa->value, &carg->value, sizeof(pa->value)); - pa->keep = arg; - return 0; - } - if (PyInt_Check(arg)) { - pa->ffi_type = &ffi_type_sint; - pa->value.i = PyInt_AS_LONG(arg); - pa->keep = arg; - return 0; - } - Py_DECREF(arg); PyErr_Format(PyExc_TypeError, "Don't know how to convert parameter %d", index); return -1; @@ -915,6 +916,10 @@ PyObject *_CallProc(PPROC pProc, #endif args = (struct argument *)alloca(sizeof(struct argument) * argcount); + if (!args) { + PyErr_NoMemory(); + return NULL; + } memset(args, 0, sizeof(struct argument) * argcount); argtype_count = argtypes ? PyTuple_GET_SIZE(argtypes) : 0; #ifdef MS_WIN32 @@ -968,6 +973,10 @@ PyObject *_CallProc(PPROC pProc, avalues = (void **)alloca(sizeof(void *) * argcount); atypes = (ffi_type **)alloca(sizeof(ffi_type *) * argcount); + if (!resbuf || !avalues || !atypes) { + PyErr_NoMemory(); + goto cleanup; + } for (i = 0; i < argcount; ++i) { atypes[i] = args[i].ffi_type; if (atypes[i]->type == FFI_TYPE_STRUCT) @@ -1068,6 +1077,11 @@ static PyObject *load_library(PyObject *self, PyObject *args) return NULL; #ifdef _UNICODE name = alloca((PyString_Size(nameobj) + 1) * sizeof(WCHAR)); + if (!name) { + PyErr_NoMemory(); + return NULL; + } + { int r; char *aname = PyString_AsString(nameobj); diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index 3595b052a99..c16a387464f 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -105,6 +105,12 @@ CField_FromDesc(PyObject *desc, int index, StgDictObject *idict; if (adict && adict->proto) { idict = PyType_stgdict(adict->proto); + if (!idict) { + PyErr_SetString(PyExc_TypeError, + "has no _stginfo_"); + Py_DECREF(self); + return NULL; + } if (idict->getfunc == getentry("c")->getfunc) { struct fielddesc *fd = getentry("s"); getfunc = fd->getfunc; @@ -1094,7 +1100,7 @@ O_get(void *ptr, unsigned size) if (!PyErr_Occurred()) /* Set an error if not yet set */ PyErr_SetString(PyExc_ValueError, - "PyObject is NULL?"); + "PyObject is NULL"); return NULL; } Py_INCREF(ob); diff --git a/Modules/_ctypes/ctypes.h b/Modules/_ctypes/ctypes.h index 303eda31ec9..0af78510208 100644 --- a/Modules/_ctypes/ctypes.h +++ b/Modules/_ctypes/ctypes.h @@ -23,9 +23,11 @@ typedef int Py_ssize_t; #define PY_LONG_LONG LONG_LONG #endif +typedef struct tagPyCArgObject PyCArgObject; typedef struct tagCDataObject CDataObject; typedef PyObject *(* GETFUNC)(void *, unsigned size); typedef PyObject *(* SETFUNC)(void *, PyObject *value, unsigned size); +typedef PyCArgObject *(* PARAMFUNC)(CDataObject *obj); /* A default buffer in CDataObject, which can be used for small C types. If this buffer is too small, PyMem_Malloc will be called to create a larger one, @@ -205,6 +207,7 @@ typedef struct { PyObject *proto; /* Only for Pointer/ArrayObject */ SETFUNC setfunc; /* Only for simple objects */ GETFUNC getfunc; /* Only for simple objects */ + PARAMFUNC paramfunc; /* Following fields only used by CFuncPtrType_Type instances */ PyObject *argtypes; /* tuple of CDataObjects */ @@ -283,7 +286,7 @@ PyObject *_CallProc(PPROC pProc, #define DICTFLAG_FINAL 0x1000 -typedef struct { +struct tagPyCArgObject { PyObject_HEAD ffi_type *pffi_type; char tag; @@ -302,7 +305,7 @@ typedef struct { } value; PyObject *obj; int size; /* for the 'V' tag */ -} PyCArgObject; +}; extern PyTypeObject PyCArg_Type; extern PyCArgObject *new_CArgObject(void); diff --git a/Modules/_ctypes/libffi/configure b/Modules/_ctypes/libffi/configure index 9eded5a5bef..9808384ce5a 100755 --- a/Modules/_ctypes/libffi/configure +++ b/Modules/_ctypes/libffi/configure @@ -3470,6 +3470,7 @@ fi TARGETDIR="unknown" case "$host" in +x86_64-*-openbsd*) TARGET=X86_64; TARGETDIR=x86;; mips*-*-openbsd*) TARGET=MIPS; TARGETDIR=mips;; sparc-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;; sparc64-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;; diff --git a/Modules/_ctypes/libffi/configure.ac b/Modules/_ctypes/libffi/configure.ac index 384959efbf4..1308034ac19 100644 --- a/Modules/_ctypes/libffi/configure.ac +++ b/Modules/_ctypes/libffi/configure.ac @@ -21,6 +21,7 @@ AC_FUNC_MMAP_BLACKLIST TARGETDIR="unknown" case "$host" in +x86_64-*-openbsd*) TARGET=X86_64; TARGETDIR=x86;; mips*-*-openbsd*) TARGET=MIPS; TARGETDIR=mips;; sparc-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;; sparc64-*-openbsd*) TARGET=SPARC; TARGETDIR=sparc;; diff --git a/Modules/_ctypes/libffi/fficonfig.py.in b/Modules/_ctypes/libffi/fficonfig.py.in index 3364049aeb0..7a9821691c3 100644 --- a/Modules/_ctypes/libffi/fficonfig.py.in +++ b/Modules/_ctypes/libffi/fficonfig.py.in @@ -43,6 +43,3 @@ ffi_sources += ffi_platforms['@MKTARGET@'] ffi_sources = [os.path.join('@srcdir@', f) for f in ffi_sources] ffi_cflags = '@CFLAGS@' -# I think this may no longer be needed: -if sys.platform == "openbsd3": - ffi_cflags += " -fno-stack-protector" diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c index b4d8196c0af..0942b48d11b 100644 --- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -204,12 +204,12 @@ MakeFields(PyObject *type, CFieldObject *descr, continue; } new_descr = (CFieldObject *)PyObject_CallObject((PyObject *)&CField_Type, NULL); - assert(new_descr->ob_type == &CField_Type); if (new_descr == NULL) { Py_DECREF(fdescr); Py_DECREF(fieldlist); return -1; } + assert(new_descr->ob_type == &CField_Type); new_descr->size = fdescr->size; new_descr->offset = fdescr->offset + offset; new_descr->index = fdescr->index + index; diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index ec55c8ed231..2921d53a8f5 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1784,7 +1784,6 @@ static PyObject * PyCurses_InitScr(PyObject *self) { WINDOW *win; - PyObject *nlines, *cols; if (initialised == TRUE) { wrefresh(stdscr); @@ -1803,7 +1802,12 @@ PyCurses_InitScr(PyObject *self) /* This was moved from initcurses() because it core dumped on SGI, where they're not defined until you've called initscr() */ #define SetDictInt(string,ch) \ - PyDict_SetItemString(ModDict,string,PyInt_FromLong((long) (ch))); + do { \ + PyObject *o = PyInt_FromLong((long) (ch)); \ + if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \ + Py_DECREF(o); \ + } \ + } while (0) /* Here are some graphic symbols you can use */ SetDictInt("ACS_ULCORNER", (ACS_ULCORNER)); @@ -1872,12 +1876,8 @@ PyCurses_InitScr(PyObject *self) SetDictInt("ACS_STERLING", (ACS_STERLING)); #endif - nlines = PyInt_FromLong((long) LINES); - PyDict_SetItemString(ModDict, "LINES", nlines); - Py_DECREF(nlines); - cols = PyInt_FromLong((long) COLS); - PyDict_SetItemString(ModDict, "COLS", cols); - Py_DECREF(cols); + SetDictInt("LINES", LINES); + SetDictInt("COLS", COLS); return (PyObject *)PyCursesWindow_New(win); } @@ -2554,6 +2554,8 @@ init_curses(void) /* Add some symbolic constants to the module */ d = PyModule_GetDict(m); + if (d == NULL) + return; ModDict = d; /* For PyCurses_InitScr to use later */ /* Add a CObject for the C API */ @@ -2667,6 +2669,10 @@ init_curses(void) if (strncmp(key_n,"KEY_F(",6)==0) { char *p1, *p2; key_n2 = malloc(strlen(key_n)+1); + if (!key_n2) { + PyErr_NoMemory(); + break; + } p1 = key_n; p2 = key_n2; while (*p1) { @@ -2679,7 +2685,7 @@ init_curses(void) *p2 = (char)0; } else key_n2 = key_n; - PyDict_SetItemString(d,key_n2,PyInt_FromLong((long) key)); + SetDictInt(key_n2,key); if (key_n2 != key_n) free(key_n2); } diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index fe30f3047af..e5d61c67ff4 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -48,7 +48,7 @@ #include "Python.h" -#define VERSION "1.0.6-snapshot" +#define VERSION "1.0.6" /* -------------------------------------------------------------------- */ /* configuration */ @@ -809,7 +809,7 @@ element_findtext(ElementObject* self, PyObject* args) PyObject* text = element_get_text(item); if (text == Py_None) return PyString_FromString(""); - Py_INCREF(text); + Py_XINCREF(text); return text; } } @@ -1599,6 +1599,10 @@ LOCAL(PyObject*) treebuilder_handle_data(TreeBuilderObject* self, PyObject* data) { if (!self->data) { + if (self->last == (ElementObject*) Py_None) { + /* ignore calls to data before the first call to start */ + Py_RETURN_NONE; + } /* store the first item as is */ Py_INCREF(data); self->data = data; } else { diff --git a/Modules/_hotshot.c b/Modules/_hotshot.c index 6d9776f1e65..21bd3839026 100644 --- a/Modules/_hotshot.c +++ b/Modules/_hotshot.c @@ -313,6 +313,11 @@ unpack_string(LogReaderObject *self, PyObject **pvalue) return err; buf = (char *)malloc(len); + if (!buf) { + PyErr_NoMemory(); + return ERR_EXCEPTION; + } + for (i=0; i < len; i++) { ch = fgetc(self->logfp); buf[i] = ch; diff --git a/Modules/_sre.c b/Modules/_sre.c index 6f1e2b62f7b..d30a3508c07 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -1166,6 +1166,9 @@ entrance: /* install new repeat context */ ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC(sizeof(*ctx->u.rep)); + /* XXX(nnorwitz): anything else we need to do on error? */ + if (!ctx->u.rep) + RETURN_FAILURE; ctx->u.rep->count = -1; ctx->u.rep->pattern = ctx->pattern; ctx->u.rep->prev = state->repeat; diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 088fbb95b18..4347383810f 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -2493,8 +2493,10 @@ Tkapp_CreateTimerHandler(PyObject *self, PyObject *args) } v = Tktt_New(func); - v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler, - (ClientData)v); + if (v) { + v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler, + (ClientData)v); + } return (PyObject *) v; } diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index e44b8e1f8ab..6ce6b07f9f2 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -702,6 +702,8 @@ array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) /* Special case "a[i:j] = a" -- copy b first */ int ret; v = array_slice(b, 0, n); + if (!v) + return -1; ret = array_ass_slice(a, ilow, ihigh, v); Py_DECREF(v); return ret; @@ -1573,9 +1575,8 @@ array_repr(arrayobject *a) static PyObject* array_subscr(arrayobject* self, PyObject* item) { - PyNumberMethods *nb = item->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(item); + if (PyIndex_Check(item)) { + Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i==-1 && PyErr_Occurred()) { return NULL; } @@ -1623,9 +1624,8 @@ array_subscr(arrayobject* self, PyObject* item) static int array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) { - PyNumberMethods *nb = item->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(item); + if (PyIndex_Check(item)) { + Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i==-1 && PyErr_Occurred()) return -1; if (i < 0) @@ -1706,6 +1706,8 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) if (self == av) { value = array_slice(av, 0, av->ob_size); av = (arrayobject*)value; + if (!av) + return -1; } else { Py_INCREF(value); diff --git a/Modules/bz2module.c b/Modules/bz2module.c index b0075a07f98..146e2ee8ff6 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -805,12 +805,12 @@ BZ2File_write(BZ2FileObject *self, PyObject *args) case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); - goto cleanup;; + goto cleanup; default: PyErr_SetString(PyExc_IOError, "file is not ready for writing"); - goto cleanup;; + goto cleanup; } self->f_softspace = 0; @@ -854,6 +854,21 @@ BZ2File_writelines(BZ2FileObject *self, PyObject *seq) int bzerror; ACQUIRE_LOCK(self); + switch (self->mode) { + case MODE_WRITE: + break; + + case MODE_CLOSED: + PyErr_SetString(PyExc_ValueError, + "I/O operation on closed file"); + goto error; + + default: + PyErr_SetString(PyExc_IOError, + "file is not ready for writing"); + goto error; + } + islist = PyList_Check(seq); if (!islist) { iter = PyObject_GetIter(seq); diff --git a/Modules/collectionsmodule.c b/Modules/collectionsmodule.c index d761dceaa7b..cb12b3a6a70 100644 --- a/Modules/collectionsmodule.c +++ b/Modules/collectionsmodule.c @@ -211,6 +211,7 @@ deque_popleft(dequeobject *deque, PyObject *unused) PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); return NULL; } + assert(deque->leftblock != NULL); item = deque->leftblock->data[deque->leftindex]; deque->leftindex++; deque->len--; @@ -226,7 +227,6 @@ deque_popleft(dequeobject *deque, PyObject *unused) } else { assert(deque->leftblock != deque->rightblock); prevblock = deque->leftblock->rightlink; - assert(deque->leftblock != NULL); PyMem_Free(deque->leftblock); assert(prevblock != NULL); prevblock->leftlink = NULL; diff --git a/Modules/expat/xmlparse.c b/Modules/expat/xmlparse.c index 882470dee3c..e04426d0cc1 100644 --- a/Modules/expat/xmlparse.c +++ b/Modules/expat/xmlparse.c @@ -2795,6 +2795,8 @@ storeAtts(XML_Parser parser, const ENCODING *enc, unsigned long uriHash = 0; ((XML_Char *)s)[-1] = 0; /* clear flag */ id = (ATTRIBUTE_ID *)lookup(&dtd->attributeIds, s, 0); + if (!id) + return XML_ERROR_NO_MEMORY; b = id->prefix->binding; if (!b) return XML_ERROR_UNBOUND_PREFIX; @@ -5340,6 +5342,8 @@ getAttributeId(XML_Parser parser, const ENCODING *enc, return NULL; id->prefix = (PREFIX *)lookup(&dtd->prefixes, poolStart(&dtd->pool), sizeof(PREFIX)); + if (!id->prefix) + return NULL; if (id->prefix->name == poolStart(&dtd->pool)) poolFinish(&dtd->pool); else diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index afe9916f4bd..a728d3dfa76 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -815,31 +815,19 @@ static PyTypeObject mmap_object_type = { static Py_ssize_t _GetMapSize(PyObject *o) { - PyNumberMethods *nb = o->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(o); + if (PyIndex_Check(o)) { + Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError); if (i==-1 && PyErr_Occurred()) return -1; - if (i < 0) - goto onnegoverflow; - if (i==PY_SSIZE_T_MAX) - goto onposoverflow; + if (i < 0) { + PyErr_SetString(PyExc_OverflowError, + "memory mapped size must be positive"); + return -1; + } return i; } - else { - PyErr_SetString(PyExc_TypeError, - "map size must be an integral value"); - return -1; - } - onnegoverflow: - PyErr_SetString(PyExc_OverflowError, - "memory mapped size must be positive"); - return -1; - - onposoverflow: - PyErr_SetString(PyExc_OverflowError, - "memory mapped size is too large (limited by C int)"); + PyErr_SetString(PyExc_TypeError, "map size must be an integral value"); return -1; } @@ -1138,6 +1126,15 @@ static struct PyMethodDef mmap_functions[] = { {NULL, NULL} /* Sentinel */ }; +static void +setint(PyObject *d, const char *name, long value) +{ + PyObject *o = PyInt_FromLong(value); + if (o && PyDict_SetItemString(d, name, o) == 0) { + Py_DECREF(o); + } +} + PyMODINIT_FUNC initmmap(void) { @@ -1150,47 +1147,40 @@ PyMODINIT_FUNC if (module == NULL) return; dict = PyModule_GetDict(module); + if (!dict) + return; mmap_module_error = PyExc_EnvironmentError; - Py_INCREF(mmap_module_error); PyDict_SetItemString(dict, "error", mmap_module_error); #ifdef PROT_EXEC - PyDict_SetItemString(dict, "PROT_EXEC", PyInt_FromLong(PROT_EXEC) ); + setint(dict, "PROT_EXEC", PROT_EXEC); #endif #ifdef PROT_READ - PyDict_SetItemString(dict, "PROT_READ", PyInt_FromLong(PROT_READ) ); + setint(dict, "PROT_READ", PROT_READ); #endif #ifdef PROT_WRITE - PyDict_SetItemString(dict, "PROT_WRITE", PyInt_FromLong(PROT_WRITE) ); + setint(dict, "PROT_WRITE", PROT_WRITE); #endif #ifdef MAP_SHARED - PyDict_SetItemString(dict, "MAP_SHARED", PyInt_FromLong(MAP_SHARED) ); + setint(dict, "MAP_SHARED", MAP_SHARED); #endif #ifdef MAP_PRIVATE - PyDict_SetItemString(dict, "MAP_PRIVATE", - PyInt_FromLong(MAP_PRIVATE) ); + setint(dict, "MAP_PRIVATE", MAP_PRIVATE); #endif #ifdef MAP_DENYWRITE - PyDict_SetItemString(dict, "MAP_DENYWRITE", - PyInt_FromLong(MAP_DENYWRITE) ); + setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE); #endif #ifdef MAP_EXECUTABLE - PyDict_SetItemString(dict, "MAP_EXECUTABLE", - PyInt_FromLong(MAP_EXECUTABLE) ); + setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE); #endif #ifdef MAP_ANONYMOUS - PyDict_SetItemString(dict, "MAP_ANON", PyInt_FromLong(MAP_ANONYMOUS) ); - PyDict_SetItemString(dict, "MAP_ANONYMOUS", - PyInt_FromLong(MAP_ANONYMOUS) ); + setint(dict, "MAP_ANON", MAP_ANONYMOUS); + setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS); #endif - PyDict_SetItemString(dict, "PAGESIZE", - PyInt_FromLong((long)my_getpagesize())); + setint(dict, "PAGESIZE", (long)my_getpagesize()); - PyDict_SetItemString(dict, "ACCESS_READ", - PyInt_FromLong(ACCESS_READ)); - PyDict_SetItemString(dict, "ACCESS_WRITE", - PyInt_FromLong(ACCESS_WRITE)); - PyDict_SetItemString(dict, "ACCESS_COPY", - PyInt_FromLong(ACCESS_COPY)); + setint(dict, "ACCESS_READ", ACCESS_READ); + setint(dict, "ACCESS_WRITE", ACCESS_WRITE); + setint(dict, "ACCESS_COPY", ACCESS_COPY); } diff --git a/Modules/operator.c b/Modules/operator.c index bf3def401c6..fa40da496ed 100644 --- a/Modules/operator.c +++ b/Modules/operator.c @@ -137,15 +137,7 @@ op_ipow(PyObject *s, PyObject *a) static PyObject * op_index(PyObject *s, PyObject *a) { - Py_ssize_t i; - PyObject *a1; - if (!PyArg_UnpackTuple(a,"index", 1, 1, &a1)) - return NULL; - i = PyNumber_Index(a1); - if (i == -1 && PyErr_Occurred()) - return NULL; - else - return PyInt_FromSsize_t(i); + return PyNumber_Index(a); } static PyObject* @@ -247,7 +239,7 @@ spam1o(isMappingType, spam1(is_, "is_(a, b) -- Same as a is b.") spam1(is_not, "is_not(a, b) -- Same as a is not b.") -spam2(index, __index__, "index(a) -- Same as a.__index__()") +spam2o(index, __index__, "index(a) -- Same as a.__index__()") spam2(add,__add__, "add(a, b) -- Same as a + b.") spam2(sub,__sub__, "sub(a, b) -- Same as a - b.") spam2(mul,__mul__, "mul(a, b) -- Same as a * b.") diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index d968b6c4cd4..5c67be6dc08 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -1121,6 +1121,8 @@ fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) #else ival = PyInt_FromLong((long)sec); #endif + if (!ival) + return; if (_stat_float_times) { fval = PyFloat_FromDouble(sec + 1e-9*nsec); } else { diff --git a/Modules/rgbimgmodule.c b/Modules/rgbimgmodule.c index a2b91c4b2a3..0f9ee71f241 100644 --- a/Modules/rgbimgmodule.c +++ b/Modules/rgbimgmodule.c @@ -410,6 +410,11 @@ longimagedata(PyObject *self, PyObject *args) addlongimgtag(base, xsize, ysize); #endif verdat = (unsigned char *)malloc(xsize); + if (!verdat) { + Py_CLEAR(rv); + goto finally; + } + fseek(inf, 512, SEEK_SET); for (z = 0; z < zsize; z++) { lptr = base; @@ -431,10 +436,14 @@ longimagedata(PyObject *self, PyObject *args) copybw((Py_Int32 *) base, xsize * ysize); } finally: - free(starttab); - free(lengthtab); - free(rledat); - free(verdat); + if (starttab) + free(starttab); + if (lengthtab) + free(lengthtab); + if (rledat) + free(rledat); + if (verdat) + free(verdat); fclose(inf); return rv; } diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index bb99bdeb9de..f03b34c057f 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -3041,17 +3041,20 @@ gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af) if ((addr_list = PyList_New(0)) == NULL) goto err; - for (pch = h->h_aliases; *pch != NULL; pch++) { - int status; - tmp = PyString_FromString(*pch); - if (tmp == NULL) - goto err; + /* SF #1511317: h_aliases can be NULL */ + if (h->h_aliases) { + for (pch = h->h_aliases; *pch != NULL; pch++) { + int status; + tmp = PyString_FromString(*pch); + if (tmp == NULL) + goto err; - status = PyList_Append(name_list, tmp); - Py_DECREF(tmp); + status = PyList_Append(name_list, tmp); + Py_DECREF(tmp); - if (status) - goto err; + if (status) + goto err; + } } for (pch = h->h_addr_list; *pch != NULL; pch++) { diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c index 1b0be28fd2d..a11a0b70b7c 100644 --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -1,8 +1,8 @@ /* ------------------------------------------------------------------------ - unicodedata -- Provides access to the Unicode 3.2 data base. + unicodedata -- Provides access to the Unicode 4.1 data base. - Data was extracted from the Unicode 3.2 UnicodeData.txt file. + Data was extracted from the Unicode 4.1 UnicodeData.txt file. Written by Marc-Andre Lemburg (mal@lemburg.com). Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com) @@ -1078,6 +1078,7 @@ unicodedata_lookup(PyObject* self, PyObject* args) { Py_UCS4 code; Py_UNICODE str[1]; + char errbuf[256]; char* name; int namelen; @@ -1085,11 +1086,19 @@ unicodedata_lookup(PyObject* self, PyObject* args) return NULL; if (!_getcode(self, name, namelen, &code)) { + /* XXX(nnorwitz): why are we allocating for the error msg? + Why not always use snprintf? */ char fmt[] = "undefined character name '%s'"; char *buf = PyMem_MALLOC(sizeof(fmt) + namelen); - sprintf(buf, fmt, name); + if (buf) + sprintf(buf, fmt, name); + else { + buf = errbuf; + PyOS_snprintf(buf, sizeof(errbuf), fmt, name); + } PyErr_SetString(PyExc_KeyError, buf); - PyMem_FREE(buf); + if (buf != errbuf) + PyMem_FREE(buf); return NULL; } @@ -1173,11 +1182,11 @@ PyDoc_STRVAR(unicodedata_docstring, "This module provides access to the Unicode Character Database which\n\ defines character properties for all Unicode characters. The data in\n\ this database is based on the UnicodeData.txt file version\n\ -3.2.0 which is publically available from ftp://ftp.unicode.org/.\n\ +4.1.0 which is publically available from ftp://ftp.unicode.org/.\n\ \n\ The module uses the same names and symbols as defined by the\n\ -UnicodeData File Format 3.2.0 (see\n\ -http://www.unicode.org/Public/3.2-Update/UnicodeData-3.2.0.html)."); +UnicodeData File Format 4.1.0 (see\n\ +http://www.unicode.org/Public/4.1.0/ucd/UCD.html)."); PyMODINIT_FUNC initunicodedata(void) diff --git a/Modules/zipimport.c b/Modules/zipimport.c index d59ebd804a8..69b28813c1d 100644 --- a/Modules/zipimport.c +++ b/Modules/zipimport.c @@ -950,6 +950,9 @@ normalize_line_endings(PyObject *source) char *buf, *q, *p = PyString_AsString(source); PyObject *fixed_source; + if (!p) + return NULL; + /* one char extra for trailing \n and one for terminating \0 */ buf = (char *)PyMem_Malloc(PyString_Size(source) + 2); if (buf == NULL) { diff --git a/Objects/abstract.c b/Objects/abstract.c index 79dfe404191..881076246ef 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -6,6 +6,7 @@ #include "longintrepr.h" + /* Shorthands to return certain errors */ static PyObject * @@ -117,9 +118,9 @@ PyObject_GetItem(PyObject *o, PyObject *key) return m->mp_subscript(o, key); if (o->ob_type->tp_as_sequence) { - PyNumberMethods *nb = key->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t key_value = nb->nb_index(key); + if (PyIndex_Check(key)) { + Py_ssize_t key_value; + key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); if (key_value == -1 && PyErr_Occurred()) return NULL; return PySequence_GetItem(o, key_value); @@ -146,9 +147,9 @@ PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value) return m->mp_ass_subscript(o, key, value); if (o->ob_type->tp_as_sequence) { - PyNumberMethods *nb = key->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t key_value = nb->nb_index(key); + if (PyIndex_Check(key)) { + Py_ssize_t key_value; + key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); if (key_value == -1 && PyErr_Occurred()) return -1; return PySequence_SetItem(o, key_value, value); @@ -178,9 +179,9 @@ PyObject_DelItem(PyObject *o, PyObject *key) return m->mp_ass_subscript(o, key, (PyObject*)NULL); if (o->ob_type->tp_as_sequence) { - PyNumberMethods *nb = key->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t key_value = nb->nb_index(key); + if (PyIndex_Check(key)) { + Py_ssize_t key_value; + key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); if (key_value == -1 && PyErr_Occurred()) return -1; return PySequence_DelItem(o, key_value); @@ -530,9 +531,8 @@ static PyObject * sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n) { Py_ssize_t count; - PyNumberMethods *nb = n->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - count = nb->nb_index(n); + if (PyIndex_Check(n)) { + count = PyNumber_AsSsize_t(n, PyExc_OverflowError); if (count == -1 && PyErr_Occurred()) return NULL; } @@ -809,23 +809,87 @@ int_from_string(const char *s, Py_ssize_t len) return x; } -/* Return a Py_ssize_t integer from the object item */ -Py_ssize_t +/* Return a Python Int or Long from the object item + Raise TypeError if the result is not an int-or-long + or if the object cannot be interpreted as an index. +*/ +PyObject * PyNumber_Index(PyObject *item) { - Py_ssize_t value = -1; - PyNumberMethods *nb = item->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - value = nb->nb_index(item); + PyObject *result = NULL; + if (item == NULL) + return null_error(); + if (PyInt_Check(item) || PyLong_Check(item)) { + Py_INCREF(item); + return item; + } + if (PyIndex_Check(item)) { + result = item->ob_type->tp_as_number->nb_index(item); + if (result && + !PyInt_Check(result) && !PyLong_Check(result)) { + PyErr_Format(PyExc_TypeError, + "__index__ returned non-(int,long) " \ + "(type %.200s)", + result->ob_type->tp_name); + Py_DECREF(result); + return NULL; + } } else { PyErr_Format(PyExc_TypeError, "'%.200s' object cannot be interpreted " "as an index", item->ob_type->tp_name); } - return value; + return result; } +/* Return an error on Overflow only if err is not NULL*/ + +Py_ssize_t +PyNumber_AsSsize_t(PyObject *item, PyObject *err) +{ + Py_ssize_t result; + PyObject *runerr; + PyObject *value = PyNumber_Index(item); + if (value == NULL) + return -1; + + /* We're done if PyInt_AsSsize_t() returns without error. */ + result = PyInt_AsSsize_t(value); + if (result != -1 || !(runerr = PyErr_Occurred())) + goto finish; + + /* Error handling code -- only manage OverflowError differently */ + if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) + goto finish; + + PyErr_Clear(); + /* If no error-handling desired then the default clipping + is sufficient. + */ + if (!err) { + assert(PyLong_Check(value)); + /* Whether or not it is less than or equal to + zero is determined by the sign of ob_size + */ + if (_PyLong_Sign(value) < 0) + result = PY_SSIZE_T_MIN; + else + result = PY_SSIZE_T_MAX; + } + else { + /* Otherwise replace the error with caller's error object. */ + PyErr_Format(err, + "cannot fit '%.200s' into an index-sized integer", + item->ob_type->tp_name); + } + + finish: + Py_DECREF(value); + return result; +} + + PyObject * PyNumber_Int(PyObject *o) { diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 2ee71bbafbb..241281e257e 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -245,10 +245,9 @@ bytes_contains(PyBytesObject *self, PyObject *value) if (PyBytes_Check(value)) return bytes_substring(self, (PyBytesObject *)value); - ival = PyNumber_Index(value); + ival = PyNumber_AsSsize_t(value, PyExc_TypeError); if (ival == -1 && PyErr_Occurred()) return -1; - if (ival < 0 || ival >= 256) { PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); return -1; @@ -366,7 +365,7 @@ bytes_setitem(PyBytesObject *self, Py_ssize_t i, PyObject *value) if (value == NULL) return bytes_setslice(self, i, i+1, NULL); - ival = PyNumber_Index(value); + ival = PyNumber_AsSsize_t(value, PyExc_TypeError); if (ival == -1 && PyErr_Occurred()) return -1; @@ -449,7 +448,7 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds) } /* Is it an int? */ - count = PyNumber_Index(arg); + count = PyNumber_AsSsize_t(arg, PyExc_TypeError); if (count == -1 && PyErr_Occurred()) PyErr_Clear(); else { @@ -501,7 +500,7 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds) } /* Interpret it as an int (__index__) */ - value = PyNumber_Index(item); + value = PyNumber_AsSsize_t(item, PyExc_TypeError); Py_DECREF(item); if (value == -1 && PyErr_Occurred()) goto error; diff --git a/Objects/exceptions.c b/Objects/exceptions.c index be9627c4a60..c3ead698e81 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1948,6 +1948,14 @@ SimpleExtendsException(PyExc_Warning, ImportWarning, "Base class for warnings about probable mistakes in module imports"); +/* + * UnicodeWarning extends Warning + */ +SimpleExtendsException(PyExc_Warning, UnicodeWarning, + "Base class for warnings about Unicode related problems, mostly\n" + "related to conversion problems."); + + /* Pre-computed MemoryError instance. Best to create this as early as * possible and not wait until a MemoryError is actually raised! */ @@ -2048,6 +2056,7 @@ _PyExc_Init(void) PRE_INIT(RuntimeWarning) PRE_INIT(FutureWarning) PRE_INIT(ImportWarning) + PRE_INIT(UnicodeWarning) m = Py_InitModule4("exceptions", functions, exceptions_doc, (PyObject *)NULL, PYTHON_API_VERSION); @@ -2113,6 +2122,7 @@ _PyExc_Init(void) POST_INIT(RuntimeWarning) POST_INIT(FutureWarning) POST_INIT(ImportWarning) + POST_INIT(UnicodeWarning) PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL); if (!PyExc_MemoryErrorInst) diff --git a/Objects/intobject.c b/Objects/intobject.c index f07040445ad..0ff232112c9 100644 --- a/Objects/intobject.c +++ b/Objects/intobject.c @@ -193,16 +193,21 @@ PyInt_AsSsize_t(register PyObject *op) PyIntObject *io; Py_ssize_t val; #endif - if (op && !PyInt_CheckExact(op) && PyLong_Check(op)) + + if (op == NULL) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return -1; + } + + if (PyInt_Check(op)) + return PyInt_AS_LONG((PyIntObject*) op); + if (PyLong_Check(op)) return _PyLong_AsSsize_t(op); #if SIZEOF_SIZE_T == SIZEOF_LONG return PyInt_AsLong(op); #else - if (op && PyInt_Check(op)) - return PyInt_AS_LONG((PyIntObject*) op); - - if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || + if ((nb = op->ob_type->tp_as_number) == NULL || (nb->nb_int == NULL && nb->nb_long == 0)) { PyErr_SetString(PyExc_TypeError, "an integer is required"); return -1; @@ -1045,7 +1050,7 @@ static PyNumberMethods int_as_number = { int_true_divide, /* nb_true_divide */ 0, /* nb_inplace_floor_divide */ 0, /* nb_inplace_true_divide */ - PyInt_AsSsize_t, /* nb_index */ + (unaryfunc)int_int, /* nb_index */ }; PyTypeObject PyInt_Type = { diff --git a/Objects/listobject.c b/Objects/listobject.c index b84998f54a7..ab408e92b53 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1398,7 +1398,7 @@ merge_lo(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject *compare; PyObject **dest; int result = -1; /* guilty until proved innocent */ - Py_ssize_t min_gallop = ms->min_gallop; + Py_ssize_t min_gallop; assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb); if (MERGE_GETMEM(ms, na) < 0) @@ -1414,6 +1414,7 @@ merge_lo(MergeState *ms, PyObject **pa, Py_ssize_t na, if (na == 1) goto CopyB; + min_gallop = ms->min_gallop; compare = ms->compare; for (;;) { Py_ssize_t acount = 0; /* # of times A won in a row */ @@ -1531,7 +1532,7 @@ merge_hi(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject **pb, Py_ssize_t int result = -1; /* guilty until proved innocent */ PyObject **basea; PyObject **baseb; - Py_ssize_t min_gallop = ms->min_gallop; + Py_ssize_t min_gallop; assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb); if (MERGE_GETMEM(ms, nb) < 0) @@ -1550,6 +1551,7 @@ merge_hi(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject **pb, Py_ssize_t if (nb == 1) goto CopyA; + min_gallop = ms->min_gallop; compare = ms->compare; for (;;) { Py_ssize_t acount = 0; /* # of times A won in a row */ @@ -2445,9 +2447,9 @@ PyDoc_STRVAR(list_doc, static PyObject * list_subscript(PyListObject* self, PyObject* item) { - PyNumberMethods *nb = item->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(item); + if (PyIndex_Check(item)) { + Py_ssize_t i; + i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i == -1 && PyErr_Occurred()) return NULL; if (i < 0) @@ -2494,9 +2496,8 @@ list_subscript(PyListObject* self, PyObject* item) static int list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) { - PyNumberMethods *nb = item->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(item); + if (PyIndex_Check(item)) { + Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i == -1 && PyErr_Occurred()) return -1; if (i < 0) @@ -2531,6 +2532,10 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) garbage = (PyObject**) PyMem_MALLOC(slicelength*sizeof(PyObject*)); + if (!garbage) { + PyErr_NoMemory(); + return -1; + } /* drawing pictures might help understand these for loops */ @@ -2579,9 +2584,9 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) else { seq = PySequence_Fast(value, "must assign iterable to extended slice"); - if (!seq) - return -1; } + if (!seq) + return -1; if (PySequence_Fast_GET_SIZE(seq) != slicelength) { PyErr_Format(PyExc_ValueError, diff --git a/Objects/longobject.c b/Objects/longobject.c index a2690a26d19..16c70435a16 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -240,8 +240,11 @@ PyLong_AsLong(PyObject *vv) return -1; } -static Py_ssize_t -_long_as_ssize_t(PyObject *vv) { +/* Get a Py_ssize_t from a long int object. + Returns -1 and sets an error condition if overflow occurs. */ + +Py_ssize_t +_PyLong_AsSsize_t(PyObject *vv) { register PyLongObject *v; size_t x, prev; Py_ssize_t i; @@ -277,45 +280,7 @@ _long_as_ssize_t(PyObject *vv) { overflow: PyErr_SetString(PyExc_OverflowError, "long int too large to convert to int"); - if (sign > 0) - return PY_SSIZE_T_MAX; - else - return PY_SSIZE_T_MIN; -} - -/* Get a Py_ssize_t from a long int object. - Returns -1 and sets an error condition if overflow occurs. */ - -Py_ssize_t -_PyLong_AsSsize_t(PyObject *vv) -{ - Py_ssize_t x; - - x = _long_as_ssize_t(vv); - if (PyErr_Occurred()) return -1; - return x; -} - - -/* Get a Py_ssize_t from a long int object. - Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, - and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1. - On error, return -1 with an exception set. -*/ - -static Py_ssize_t -long_index(PyObject *vv) -{ - Py_ssize_t x; - - x = _long_as_ssize_t(vv); - if (PyErr_Occurred()) { - /* If overflow error, ignore the error */ - if (x != -1) { - PyErr_Clear(); - } - } - return x; + return -1; } /* Get a C unsigned long int from a long int object. @@ -3379,7 +3344,7 @@ static PyNumberMethods long_as_number = { long_true_divide, /* nb_true_divide */ 0, /* nb_inplace_floor_divide */ 0, /* nb_inplace_true_divide */ - long_index, /* nb_index */ + long_long, /* nb_index */ }; PyTypeObject PyLong_Type = { diff --git a/Objects/object.c b/Objects/object.c index 44b3f7db7e2..cb603201b5d 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -725,23 +725,6 @@ default_3way_compare(PyObject *v, PyObject *w) return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; } -#ifdef Py_USING_UNICODE - /* Special case for Unicode */ - if (PyUnicode_Check(v) || PyUnicode_Check(w)) { - c = PyUnicode_Compare(v, w); - if (!PyErr_Occurred()) - return c; - /* TypeErrors are ignored: if Unicode coercion fails due - to one of the arguments not having the right type, we - continue as defined by the coercion protocol (see - above). Luckily, decoding errors are reported as - ValueErrors and are not masked by this technique. */ - if (!PyErr_ExceptionMatches(PyExc_TypeError)) - return -2; - PyErr_Clear(); - } -#endif - /* None is smaller than anything */ if (v == Py_None) return -1; diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 271a9ad62d2..d8a24653a7d 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -252,7 +252,7 @@ slice_indices(PySliceObject* self, PyObject* len) { Py_ssize_t ilen, start, stop, step, slicelength; - ilen = PyInt_AsSsize_t(len); + ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError); if (ilen == -1 && PyErr_Occurred()) { return NULL; diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 7ba15a00f86..028b375b49f 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -1187,9 +1187,8 @@ string_hash(PyStringObject *a) static PyObject* string_subscript(PyStringObject* self, PyObject* item) { - PyNumberMethods *nb = item->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(item); + if (PyIndex_Check(item)) { + Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i == -1 && PyErr_Occurred()) return NULL; if (i < 0) @@ -4226,12 +4225,17 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type, if (!result) return NULL; + buf = PyString_AsString(result); + if (!buf) { + Py_DECREF(result); + return NULL; + } + /* To modify the string in-place, there can only be one reference. */ if (result->ob_refcnt != 1) { PyErr_BadInternalCall(); return NULL; } - buf = PyString_AsString(result); llen = PyString_Size(result); if (llen > PY_SSIZE_T_MAX) { PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong"); diff --git a/Objects/structseq.c b/Objects/structseq.c index e074810df3a..7ac2a1f61e3 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -215,6 +215,8 @@ structseq_contains(PyStructSequence *obj, PyObject *o) PyObject *tup; int result; tup = make_tuple(obj); + if (!tup) + return -1; result = PySequence_Contains(tup, o); Py_DECREF(tup); return result; @@ -226,6 +228,8 @@ structseq_hash(PyObject *obj) PyObject *tup; long result; tup = make_tuple((PyStructSequence*) obj); + if (!tup) + return -1; result = PyObject_Hash(tup); Py_DECREF(tup); return result; diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 53afab5d2dd..6f3711f1f45 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -580,9 +580,8 @@ static PySequenceMethods tuple_as_sequence = { static PyObject* tuplesubscript(PyTupleObject* self, PyObject* item) { - PyNumberMethods *nb = item->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(item); + if (PyIndex_Check(item)) { + Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i == -1 && PyErr_Occurred()) return NULL; if (i < 0) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index e9ffa8744cc..1578801a5ff 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -3373,7 +3373,7 @@ wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) return NULL; - i = PyNumber_Index(o); + i = PyNumber_AsSsize_t(o, PyExc_OverflowError); if (i == -1 && PyErr_Occurred()) return NULL; return (*func)(self, i); @@ -3384,7 +3384,7 @@ getindex(PyObject *self, PyObject *arg) { Py_ssize_t i; - i = PyNumber_Index(arg); + i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); if (i == -1 && PyErr_Occurred()) return -1; if (i < 0) { @@ -3956,19 +3956,17 @@ slot_sq_length(PyObject *self) { static PyObject *len_str; PyObject *res = call_method(self, "__len__", &len_str, "()"); - Py_ssize_t temp; Py_ssize_t len; if (res == NULL) return -1; - temp = PyInt_AsSsize_t(res); - len = (int)temp; + len = PyInt_AsSsize_t(res); Py_DECREF(res); if (len == -1 && PyErr_Occurred()) return -1; -#if SIZEOF_SIZE_T < SIZEOF_LONG +#if SIZEOF_SIZE_T < SIZEOF_INT /* Overflow check -- range of PyInt is more than C ssize_t */ - if (len != temp) { + if (len != (int)len) { PyErr_SetString(PyExc_OverflowError, "__len__() should return 0 <= outcome < 2**31"); return -1; @@ -4189,26 +4187,11 @@ slot_nb_nonzero(PyObject *self) } -static Py_ssize_t +static PyObject * slot_nb_index(PyObject *self) { static PyObject *index_str; - PyObject *temp = call_method(self, "__index__", &index_str, "()"); - Py_ssize_t result; - - if (temp == NULL) - return -1; - if (PyInt_CheckExact(temp) || PyLong_CheckExact(temp)) { - result = temp->ob_type->tp_as_number->nb_index(temp); - } - else { - PyErr_Format(PyExc_TypeError, - "__index__ must return an int or a long, " - "not '%.200s'", temp->ob_type->tp_name); - result = -1; - } - Py_DECREF(temp); - return result; + return call_method(self, "__index__", &index_str, "()"); } @@ -4882,7 +4865,7 @@ static slotdef slotdefs[] = { "oct(x)"), UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, "hex(x)"), - NBSLOT("__index__", nb_index, slot_nb_index, wrap_lenfunc, + NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, "x[y:z] <==> x[y.__index__():z.__index__()]"), IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, wrap_binaryfunc, "+"), diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index e63e629309f..92301c00123 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -5405,6 +5405,82 @@ onError: return -1; } +PyObject *PyUnicode_RichCompare(PyObject *left, + PyObject *right, + int op) +{ + int result; + + result = PyUnicode_Compare(left, right); + if (result == -1 && PyErr_Occurred()) + goto onError; + + /* Convert the return value to a Boolean */ + switch (op) { + case Py_EQ: + result = (result == 0); + break; + case Py_NE: + result = (result != 0); + break; + case Py_LE: + result = (result <= 0); + break; + case Py_GE: + result = (result >= 0); + break; + case Py_LT: + result = (result == -1); + break; + case Py_GT: + result = (result == 1); + break; + } + return PyBool_FromLong(result); + + onError: + + /* Standard case + + Type errors mean that PyUnicode_FromObject() could not convert + one of the arguments (usually the right hand side) to Unicode, + ie. we can't handle the comparison request. However, it is + possible that the other object knows a comparison method, which + is why we return Py_NotImplemented to give the other object a + chance. + + */ + if (PyErr_ExceptionMatches(PyExc_TypeError)) { + PyErr_Clear(); + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } + if (op != Py_EQ && op != Py_NE) + return NULL; + + /* Equality comparison. + + This is a special case: we silence any PyExc_UnicodeDecodeError + and instead turn it into a PyErr_UnicodeWarning. + + */ + if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) + return NULL; + PyErr_Clear(); + if (PyErr_Warn(PyExc_UnicodeWarning, + (op == Py_EQ) ? + "Unicode equal comparison " + "failed to convert both arguments to Unicode - " + "interpreting them as being unequal" : + "Unicode unequal comparison " + "failed to convert both arguments to Unicode - " + "interpreting them as being unequal" + ) < 0) + return NULL; + result = (op == Py_NE); + return PyBool_FromLong(result); +} + int PyUnicode_Contains(PyObject *container, PyObject *element) { @@ -6987,9 +7063,8 @@ static PySequenceMethods unicode_as_sequence = { static PyObject* unicode_subscript(PyUnicodeObject* self, PyObject* item) { - PyNumberMethods *nb = item->ob_type->tp_as_number; - if (nb != NULL && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(item); + if (PyIndex_Check(item)) { + Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i == -1 && PyErr_Occurred()) return NULL; if (i < 0) @@ -7861,7 +7936,7 @@ PyTypeObject PyUnicode_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ - (cmpfunc) unicode_compare, /* tp_compare */ + 0, /* tp_compare */ unicode_repr, /* tp_repr */ &unicode_as_number, /* tp_as_number */ &unicode_as_sequence, /* tp_as_sequence */ @@ -7876,7 +7951,7 @@ PyTypeObject PyUnicode_Type = { unicode_doc, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ - 0, /* tp_richcompare */ + PyUnicode_RichCompare, /* tp_richcompare */ 0, /* tp_weaklistoffset */ unicode_iter, /* tp_iter */ 0, /* tp_iternext */ diff --git a/PC/pyconfig.h b/PC/pyconfig.h index e0df67383bd..4aedce73ac4 100644 --- a/PC/pyconfig.h +++ b/PC/pyconfig.h @@ -280,9 +280,9 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */ their Makefile (other compilers are generally taken care of by distutils.) */ # ifdef _DEBUG -# pragma comment(lib,"python25_d.lib") +# pragma comment(lib,"python26_d.lib") # else -# pragma comment(lib,"python25.lib") +# pragma comment(lib,"python26.lib") # endif /* _DEBUG */ # endif /* _MSC_VER */ # endif /* Py_BUILD_CORE */ diff --git a/PCbuild/_ssl.mak b/PCbuild/_ssl.mak index 2c47c6b43c6..2623d270cb8 100644 --- a/PCbuild/_ssl.mak +++ b/PCbuild/_ssl.mak @@ -1,21 +1,37 @@ !IFDEF DEBUG -MODULE=_ssl_d.pyd -TEMP_DIR=x86-temp-debug/_ssl +SUFFIX=_d.pyd +TEMP=x86-temp-debug/ CFLAGS=/Od /Zi /MDd /LDd /DDEBUG /D_DEBUG /DWIN32 SSL_LIB_DIR=$(SSL_DIR)/out32.dbg !ELSE -MODULE=_ssl.pyd -TEMP_DIR=x86-temp-release/_ssl +SUFFIX=.pyd +TEMP=x86-temp-release/ CFLAGS=/Ox /MD /LD /DWIN32 SSL_LIB_DIR=$(SSL_DIR)/out32 !ENDIF INCLUDES=-I ../Include -I ../PC -I $(SSL_DIR)/inc32 -LIBS=gdi32.lib wsock32.lib user32.lib advapi32.lib /libpath:$(SSL_LIB_DIR) libeay32.lib ssleay32.lib -SOURCE=../Modules/_ssl.c $(SSL_LIB_DIR)/libeay32.lib $(SSL_LIB_DIR)/ssleay32.lib +SSL_LIBS=gdi32.lib wsock32.lib user32.lib advapi32.lib /LIBPATH:$(SSL_LIB_DIR) libeay32.lib ssleay32.lib +SSL_SOURCE=../Modules/_ssl.c -$(MODULE): $(SOURCE) ../PC/*.h ../Include/*.h - @if not exist "$(TEMP_DIR)/." mkdir "$(TEMP_DIR)" - cl /nologo $(SOURCE) $(CFLAGS) /Fo$(TEMP_DIR)\$*.obj $(INCLUDES) /link /out:$(MODULE) $(LIBS) +HASH_LIBS=gdi32.lib user32.lib advapi32.lib /libpath:$(SSL_LIB_DIR) libeay32.lib +HASH_SOURCE=../Modules/_hashopenssl.c + +all: _ssl$(SUFFIX) _hashlib$(SUFFIX) + +# Split compile/link into two steps to better support VSExtComp +_ssl$(SUFFIX): $(SSL_SOURCE) $(SSL_LIB_DIR)/libeay32.lib $(SSL_LIB_DIR)/ssleay32.lib ../PC/*.h ../Include/*.h + @if not exist "$(TEMP)/_ssl/." mkdir "$(TEMP)/_ssl" + cl /nologo /c $(SSL_SOURCE) $(CFLAGS) /Fo$(TEMP)\_ssl\$*.obj $(INCLUDES) + link /nologo @<< + /dll /out:_ssl$(SUFFIX) $(TEMP)\_ssl\$*.obj $(SSL_LIBS) +<< + +_hashlib$(SUFFIX): $(HASH_SOURCE) $(SSL_LIB_DIR)/libeay32.lib ../PC/*.h ../Include/*.h + @if not exist "$(TEMP)/_hashlib/." mkdir "$(TEMP)/_hashlib" + cl /nologo /c $(HASH_SOURCE) $(CFLAGS) /Fo$(TEMP)\_hashlib\$*.obj $(INCLUDES) + link /nologo @<< + /dll /out:_hashlib$(SUFFIX) $(HASH_LIBS) $(TEMP)\_hashlib\$*.obj +<< diff --git a/PCbuild/_ssl.vcproj b/PCbuild/_ssl.vcproj index bc69ee8862b..c1abd2486b2 100644 --- a/PCbuild/_ssl.vcproj +++ b/PCbuild/_ssl.vcproj @@ -75,6 +75,9 @@ + + diff --git a/PCbuild/build_ssl.py b/PCbuild/build_ssl.py index d4beee54169..5ab34498a84 100644 --- a/PCbuild/build_ssl.py +++ b/PCbuild/build_ssl.py @@ -1,7 +1,7 @@ -# Script for building the _ssl module for Windows. +# Script for building the _ssl and _hashlib modules for Windows. # Uses Perl to setup the OpenSSL environment correctly # and build OpenSSL, then invokes a simple nmake session -# for _ssl.pyd itself. +# for the actual _ssl.pyd and _hashlib.pyd DLLs. # THEORETICALLY, you can: # * Unpack the latest SSL release one level above your main Python source @@ -10,8 +10,8 @@ # * Install ActivePerl and ensure it is somewhere on your path. # * Run this script from the PCBuild directory. # -# it should configure and build SSL, then build the ssl Python extension -# without intervention. +# it should configure and build SSL, then build the _ssl and _hashlib +# Python extensions without intervention. import os, sys, re @@ -59,7 +59,8 @@ def find_best_ssl_dir(sources): candidates = [] for s in sources: try: - s = os.path.abspath(s) + # note: do not abspath s; the build will fail if any + # higher up directory name has spaces in it. fnames = os.listdir(s) except os.error: fnames = [] @@ -82,31 +83,9 @@ def find_best_ssl_dir(sources): print "Found an SSL directory at '%s'" % (best_name,) else: print "Could not find an SSL directory in '%s'" % (sources,) + sys.stdout.flush() return best_name -def run_32all_py(): - # ms\32all.bat will reconfigure OpenSSL and then try to build - # all outputs (debug/nondebug/dll/lib). So we filter the file - # to exclude any "nmake" commands and then execute. - tempname = "ms\\32all_py.bat" - - in_bat = open("ms\\32all.bat") - temp_bat = open(tempname,"w") - while 1: - cmd = in_bat.readline() - print 'cmd', repr(cmd) - if not cmd: break - if cmd.strip()[:5].lower() == "nmake": - continue - temp_bat.write(cmd) - in_bat.close() - temp_bat.close() - os.system(tempname) - try: - os.remove(tempname) - except: - pass - def run_configure(configure, do_script): os.system("perl Configure "+configure) os.system(do_script) @@ -117,12 +96,14 @@ def main(): arch = "x86" debug = False configure = "VC-WIN32" - makefile = "32.mak" + do_script = "ms\\do_masm" + makefile = "ms\\nt.mak" elif sys.argv[1] == "Debug": arch = "x86" debug = True configure = "VC-WIN32" - makefile="d32.mak" + do_script = "ms\\do_masm" + makefile="ms\\d32.mak" elif sys.argv[1] == "ReleaseItanium": arch = "ia64" debug = False @@ -148,8 +129,9 @@ def main(): sys.exit(1) print "Found a working perl at '%s'" % (perl,) + sys.stdout.flush() # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live. - ssl_dir = find_best_ssl_dir(("../..",)) + ssl_dir = find_best_ssl_dir(("..\\..",)) if ssl_dir is None: sys.exit(1) @@ -157,31 +139,40 @@ def main(): try: os.chdir(ssl_dir) # If the ssl makefiles do not exist, we invoke Perl to generate them. - if not os.path.isfile(makefile): + # Due to a bug in this script, the makefile sometimes ended up empty + # Force a regeneration if it is. + if not os.path.isfile(makefile) or os.path.getsize(makefile)==0: print "Creating the makefiles..." + sys.stdout.flush() # Put our working Perl at the front of our path - os.environ["PATH"] = os.path.split(perl)[0] + \ + os.environ["PATH"] = os.path.dirname(perl) + \ os.pathsep + \ os.environ["PATH"] - if arch=="x86": - run_32all_py() - else: - run_configure(configure, do_script) + run_configure(configure, do_script) + if arch=="x86" and debug: + # the do_masm script in openssl doesn't generate a debug + # build makefile so we generate it here: + os.system("perl util\mk1mf.pl debug "+configure+" >"+makefile) # Now run make. - print "Executing nmake over the ssl makefiles..." - rc = os.system("nmake /nologo -f "+makefile) + makeCommand = "nmake /nologo PERL=\"%s\" -f \"%s\"" %(perl, makefile) + print "Executing ssl makefiles:", makeCommand + sys.stdout.flush() + rc = os.system(makeCommand) if rc: - print "Executing d32.mak failed" + print "Executing "+makefile+" failed" print rc sys.exit(rc) finally: os.chdir(old_cd) # And finally, we can build the _ssl module itself for Python. - defs = "SSL_DIR=%s" % (ssl_dir,) + defs = "SSL_DIR=\"%s\"" % (ssl_dir,) if debug: defs = defs + " " + "DEBUG=1" - rc = os.system('nmake /nologo -f _ssl.mak ' + defs + " " + make_flags) + makeCommand = 'nmake /nologo -f _ssl.mak ' + defs + " " + make_flags + print "Executing:", makeCommand + sys.stdout.flush() + rc = os.system(makeCommand) sys.exit(rc) if __name__=='__main__': diff --git a/PCbuild/pythoncore.vcproj b/PCbuild/pythoncore.vcproj index ef200c488a4..f0f71b8ab16 100644 --- a/PCbuild/pythoncore.vcproj +++ b/PCbuild/pythoncore.vcproj @@ -39,15 +39,15 @@ @@ -99,15 +99,15 @@ @@ -166,15 +166,15 @@ Name="VCLinkerTool" AdditionalOptions=" /MACHINE:IA64 /USELINK:MS_SDK" AdditionalDependencies="getbuildinfo.o" - OutputFile="./python25.dll" + OutputFile="./python26.dll" LinkIncremental="1" SuppressStartupBanner="FALSE" IgnoreDefaultLibraryNames="libc" GenerateDebugInformation="TRUE" - ProgramDatabaseFile=".\./python25.pdb" + ProgramDatabaseFile=".\./python26.pdb" SubSystem="2" BaseAddress="0x1e000000" - ImportLibrary=".\./python25.lib" + ImportLibrary=".\./python26.lib" TargetMachine="0"/> @@ -233,15 +233,15 @@ Name="VCLinkerTool" AdditionalOptions=" /MACHINE:AMD64 /USELINK:MS_SDK" AdditionalDependencies="getbuildinfo.o" - OutputFile="./python25.dll" + OutputFile="./python26.dll" LinkIncremental="1" SuppressStartupBanner="TRUE" IgnoreDefaultLibraryNames="libc" GenerateDebugInformation="TRUE" - ProgramDatabaseFile=".\./python25.pdb" + ProgramDatabaseFile=".\./python26.pdb" SubSystem="2" BaseAddress="0x1e000000" - ImportLibrary=".\./python25.lib" + ImportLibrary=".\./python26.lib" TargetMachine="0"/> diff --git a/PCbuild/readme.txt b/PCbuild/readme.txt index 45363684c48..79a391acb91 100644 --- a/PCbuild/readme.txt +++ b/PCbuild/readme.txt @@ -12,7 +12,7 @@ the "Standard" toolbar"), and build the projects. The proper order to build subprojects: 1) pythoncore (this builds the main Python DLL and library files, - python25.{dll, lib} in Release mode) + python26.{dll, lib} in Release mode) NOTE: in previous releases, this subproject was named after the release number, e.g. python20. @@ -26,7 +26,7 @@ The proper order to build subprojects: test slave; see SUBPROJECTS below) When using the Debug setting, the output files have a _d added to -their name: python25_d.dll, python_d.exe, parser_d.pyd, and so on. +their name: python26_d.dll, python_d.exe, parser_d.pyd, and so on. SUBPROJECTS ----------- diff --git a/Parser/grammar.c b/Parser/grammar.c index b0dafe7a6f7..9e7c49aab65 100644 --- a/Parser/grammar.c +++ b/Parser/grammar.c @@ -199,6 +199,10 @@ translabel(grammar *g, label *lb) else name_len = strlen(src); dest = (char *)malloc(name_len + 1); + if (!dest) { + printf("Can't alloc dest '%s'\n", src); + return; + } strncpy(dest, src, name_len); dest[name_len] = '\0'; free(lb->lb_str); diff --git a/Parser/parsetok.c b/Parser/parsetok.c index a27575c1864..01a3051518c 100644 --- a/Parser/parsetok.c +++ b/Parser/parsetok.c @@ -225,6 +225,11 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, } } else if (tok->encoding != NULL) { node* r = PyNode_New(encoding_decl); + if (!r) { + err_ret->error = E_NOMEM; + n = NULL; + goto done; + } r->n_str = tok->encoding; r->n_nchildren = 1; r->n_child = n; @@ -232,6 +237,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, n = r; } +done: PyTokenizer_Free(tok); return n; diff --git a/Python/ast.c b/Python/ast.c index ca832aa5e15..b356192d892 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -2197,6 +2197,8 @@ alias_for_import_name(struct compiling *c, const node *n) } else { alias_ty a = alias_for_import_name(c, CHILD(n, 0)); + if (!a) + return NULL; if (strcmp(STR(CHILD(n, 1)), "as") != 0) { ast_error(n, "must use 'as' in import"); return NULL; diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 8b92c872ad8..9616394c3fe 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -691,7 +691,7 @@ builtin_globals(PyObject *self) PyObject *d; d = PyEval_GetGlobals(); - Py_INCREF(d); + Py_XINCREF(d); return d; } @@ -1084,7 +1084,7 @@ builtin_locals(PyObject *self) PyObject *d; d = PyEval_GetLocals(); - Py_INCREF(d); + Py_XINCREF(d); return d; } diff --git a/Python/ceval.c b/Python/ceval.c index 07dfdc20a6c..b33c16bd901 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2597,6 +2597,7 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, return NULL; } + assert(tstate != NULL); assert(globals != NULL); f = PyFrame_New(tstate, co, globals, locals); if (f == NULL) @@ -3589,6 +3590,7 @@ fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk) PyFrame_New() that doesn't take locals, but does take builtins without sanity checking them. */ + assert(tstate != NULL); f = PyFrame_New(tstate, co, globals, NULL); if (f == NULL) return NULL; @@ -3601,7 +3603,6 @@ fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk) fastlocals[i] = *stack++; } retval = PyEval_EvalFrameEx(f,0); - assert(tstate != NULL); ++tstate->recursion_depth; Py_DECREF(f); --tstate->recursion_depth; @@ -3819,11 +3820,14 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) if (v != NULL) { Py_ssize_t x; if (PyInt_Check(v)) { - x = PyInt_AsSsize_t(v); + /* XXX(nnorwitz): I think PyInt_AS_LONG is correct, + however, it looks like it should be AsSsize_t. + There should be a comment here explaining why. + */ + x = PyInt_AS_LONG(v); } - else if (v->ob_type->tp_as_number && - v->ob_type->tp_as_number->nb_index) { - x = v->ob_type->tp_as_number->nb_index(v); + else if (PyIndex_Check(v)) { + x = PyNumber_AsSsize_t(v, NULL); if (x == -1 && PyErr_Occurred()) return 0; } @@ -3839,9 +3843,8 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) } #undef ISINDEX -#define ISINDEX(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x) || \ - ((x)->ob_type->tp_as_number && \ - (x)->ob_type->tp_as_number->nb_index)) +#define ISINDEX(x) ((x) == NULL || \ + PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x)) static PyObject * apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */ @@ -4081,6 +4084,11 @@ exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, locals = PyEval_GetLocals(); plain = 1; } + if (!globals || !locals) { + PyErr_SetString(PyExc_SystemError, + "globals and locals cannot be NULL"); + return -1; + } } else if (locals == Py_None) locals = globals; diff --git a/Python/compile.c b/Python/compile.c index 4464882edf6..678a4deee96 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -204,8 +204,8 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident) const char *p, *name = PyString_AsString(ident); char *buffer; size_t nlen, plen; - if (privateobj == NULL || name == NULL || name[0] != '_' || - name[1] != '_') { + if (privateobj == NULL || !PyString_Check(privateobj) || + name == NULL || name[0] != '_' || name[1] != '_') { Py_INCREF(ident); return ident; } @@ -394,608 +394,6 @@ dictbytype(PyObject *src, int scope_type, int flag, int offset) return dest; } -/* Begin: Peephole optimizations ----------------------------------------- */ - -#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) -#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) -#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP) -#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) -#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 -#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) -#define ISBASICBLOCK(blocks, start, bytes) \ - (blocks[start]==blocks[start+bytes-1]) - -/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n - with LOAD_CONST (c1, c2, ... cn). - The consts table must still be in list form so that the - new constant (c1, c2, ... cn) can be appended. - Called with codestr pointing to the first LOAD_CONST. - Bails out with no change if one or more of the LOAD_CONSTs is missing. - Also works for BUILD_LIST when followed by an "in" or "not in" test. -*/ -static int -tuple_of_constants(unsigned char *codestr, int n, PyObject *consts) -{ - PyObject *newconst, *constant; - Py_ssize_t i, arg, len_consts; - - /* Pre-conditions */ - assert(PyList_CheckExact(consts)); - assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST); - assert(GETARG(codestr, (n*3)) == n); - for (i=0 ; i 20) { - Py_DECREF(newconst); - return 0; - } - - /* Append folded constant into consts table */ - len_consts = PyList_GET_SIZE(consts); - if (PyList_Append(consts, newconst)) { - Py_DECREF(newconst); - return 0; - } - Py_DECREF(newconst); - - /* Write NOP NOP NOP NOP LOAD_CONST newconst */ - memset(codestr, NOP, 4); - codestr[4] = LOAD_CONST; - SETARG(codestr, 4, len_consts); - return 1; -} - -static int -fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts) -{ - PyObject *newconst=NULL, *v; - Py_ssize_t len_consts; - int opcode; - - /* Pre-conditions */ - assert(PyList_CheckExact(consts)); - assert(codestr[0] == LOAD_CONST); - - /* Create new constant */ - v = PyList_GET_ITEM(consts, GETARG(codestr, 0)); - opcode = codestr[3]; - switch (opcode) { - case UNARY_NEGATIVE: - /* Preserve the sign of -0.0 */ - if (PyObject_IsTrue(v) == 1) - newconst = PyNumber_Negative(v); - break; - case UNARY_CONVERT: - newconst = PyObject_Repr(v); - break; - case UNARY_INVERT: - newconst = PyNumber_Invert(v); - break; - default: - /* Called with an unknown opcode */ - PyErr_Format(PyExc_SystemError, - "unexpected unary operation %d on a constant", - opcode); - return 0; - } - if (newconst == NULL) { - PyErr_Clear(); - return 0; - } - - /* Append folded constant into consts table */ - len_consts = PyList_GET_SIZE(consts); - if (PyList_Append(consts, newconst)) { - Py_DECREF(newconst); - return 0; - } - Py_DECREF(newconst); - - /* Write NOP LOAD_CONST newconst */ - codestr[0] = NOP; - codestr[1] = LOAD_CONST; - SETARG(codestr, 1, len_consts); - return 1; -} - -static unsigned int * -markblocks(unsigned char *code, int len) -{ - unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int)); - int i,j, opcode, blockcnt = 0; - - if (blocks == NULL) { - PyErr_NoMemory(); - return NULL; - } - memset(blocks, 0, len*sizeof(int)); - - /* Mark labels in the first pass */ - for (i=0 ; i= 255. - - Optimizations are restricted to simple transformations occuring within a - single basic block. All transformations keep the code size the same or - smaller. For those that reduce size, the gaps are initially filled with - NOPs. Later those NOPs are removed and the jump addresses retargeted in - a single pass. Line numbering is adjusted accordingly. */ - -static PyObject * -optimize_code(PyObject *code, PyObject* consts, PyObject *names, - PyObject *lineno_obj) -{ - Py_ssize_t i, j, codelen; - int nops, h, adj; - int tgt, tgttgt, opcode; - unsigned char *codestr = NULL; - unsigned char *lineno; - int *addrmap = NULL; - int new_line, cum_orig_line, last_line, tabsiz; - int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONSTs */ - unsigned int *blocks = NULL; - char *name; - - /* Bail out if an exception is set */ - if (PyErr_Occurred()) - goto exitUnchanged; - - /* Bypass optimization when the lineno table is too complex */ - assert(PyString_Check(lineno_obj)); - lineno = (unsigned char*)PyString_AS_STRING(lineno_obj); - tabsiz = PyString_GET_SIZE(lineno_obj); - if (memchr(lineno, 255, tabsiz) != NULL) - goto exitUnchanged; - - /* Avoid situations where jump retargeting could overflow */ - assert(PyString_Check(code)); - codelen = PyString_Size(code); - if (codelen > 32700) - goto exitUnchanged; - - /* Make a modifiable copy of the code string */ - codestr = (unsigned char *)PyMem_Malloc(codelen); - if (codestr == NULL) - goto exitUnchanged; - codestr = (unsigned char *)memcpy(codestr, - PyString_AS_STRING(code), codelen); - - /* Verify that RETURN_VALUE terminates the codestring. This allows - the various transformation patterns to look ahead several - instructions without additional checks to make sure they are not - looking beyond the end of the code string. - */ - if (codestr[codelen-1] != RETURN_VALUE) - goto exitUnchanged; - - /* Mapping to new jump targets after NOPs are removed */ - addrmap = (int *)PyMem_Malloc(codelen * sizeof(int)); - if (addrmap == NULL) - goto exitUnchanged; - - blocks = markblocks(codestr, codelen); - if (blocks == NULL) - goto exitUnchanged; - assert(PyList_Check(consts)); - - for (i=0 ; i a is not b - not a in b --> a not in b - not a is not b --> a is b - not a not in b --> a in b - */ - case COMPARE_OP: - j = GETARG(codestr, i); - if (j < 6 || j > 9 || - codestr[i+3] != UNARY_NOT || - !ISBASICBLOCK(blocks,i,4)) - continue; - SETARG(codestr, i, (j^1)); - codestr[i+3] = NOP; - break; - - /* Replace LOAD_GLOBAL/LOAD_NAME None - with LOAD_CONST None */ - case LOAD_NAME: - case LOAD_GLOBAL: - j = GETARG(codestr, i); - name = PyString_AsString(PyTuple_GET_ITEM(names, j)); - if (name == NULL || strcmp(name, "None") != 0) - continue; - for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) { - if (PyList_GET_ITEM(consts, j) == Py_None) { - codestr[i] = LOAD_CONST; - SETARG(codestr, i, j); - cumlc = lastlc + 1; - break; - } - } - break; - - /* Skip over LOAD_CONST trueconst - JUMP_IF_FALSE xx POP_TOP */ - case LOAD_CONST: - cumlc = lastlc + 1; - j = GETARG(codestr, i); - if (codestr[i+3] != JUMP_IF_FALSE || - codestr[i+6] != POP_TOP || - !ISBASICBLOCK(blocks,i,7) || - !PyObject_IsTrue(PyList_GET_ITEM(consts, j))) - continue; - memset(codestr+i, NOP, 7); - cumlc = 0; - break; - - /* Try to fold tuples of constants (includes a case for lists - which are only used for "in" and "not in" tests). - Skip over BUILD_SEQN 1 UNPACK_SEQN 1. - Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. - Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ - case BUILD_TUPLE: - case BUILD_LIST: - j = GETARG(codestr, i); - h = i - 3 * j; - if (h >= 0 && - j <= lastlc && - ((opcode == BUILD_TUPLE && - ISBASICBLOCK(blocks, h, 3*(j+1))) || - (opcode == BUILD_LIST && - codestr[i+3]==COMPARE_OP && - ISBASICBLOCK(blocks, h, 3*(j+2)) && - (GETARG(codestr,i+3)==6 || - GETARG(codestr,i+3)==7))) && - tuple_of_constants(&codestr[h], j, consts)) { - assert(codestr[i] == LOAD_CONST); - cumlc = 1; - break; - } - if (codestr[i+3] != UNPACK_SEQUENCE || - !ISBASICBLOCK(blocks,i,6) || - j != GETARG(codestr, i+3)) - continue; - if (j == 1) { - memset(codestr+i, NOP, 6); - } else if (j == 2) { - codestr[i] = ROT_TWO; - memset(codestr+i+1, NOP, 5); - } else if (j == 3) { - codestr[i] = ROT_THREE; - codestr[i+1] = ROT_TWO; - memset(codestr+i+2, NOP, 4); - } - break; - - /* Fold binary ops on constants. - LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */ - case BINARY_POWER: - case BINARY_MULTIPLY: - case BINARY_TRUE_DIVIDE: - case BINARY_FLOOR_DIVIDE: - case BINARY_MODULO: - case BINARY_ADD: - case BINARY_SUBTRACT: - case BINARY_SUBSCR: - case BINARY_LSHIFT: - case BINARY_RSHIFT: - case BINARY_AND: - case BINARY_XOR: - case BINARY_OR: - if (lastlc >= 2 && - ISBASICBLOCK(blocks, i-6, 7) && - fold_binops_on_constants(&codestr[i-6], consts)) { - i -= 2; - assert(codestr[i] == LOAD_CONST); - cumlc = 1; - } - break; - - /* Fold unary ops on constants. - LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */ - case UNARY_NEGATIVE: - case UNARY_CONVERT: - case UNARY_INVERT: - if (lastlc >= 1 && - ISBASICBLOCK(blocks, i-3, 4) && - fold_unaryops_on_constants(&codestr[i-3], consts)) { - i -= 2; - assert(codestr[i] == LOAD_CONST); - cumlc = 1; - } - break; - - /* Simplify conditional jump to conditional jump where the - result of the first test implies the success of a similar - test or the failure of the opposite test. - Arises in code like: - "if a and b:" - "if a or b:" - "a and b or c" - "(a and b) and c" - x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z - x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3 - where y+3 is the instruction following the second test. - */ - case JUMP_IF_FALSE: - case JUMP_IF_TRUE: - tgt = GETJUMPTGT(codestr, i); - j = codestr[tgt]; - if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) { - if (j == opcode) { - tgttgt = GETJUMPTGT(codestr, tgt) - i - 3; - SETARG(codestr, i, tgttgt); - } else { - tgt -= i; - SETARG(codestr, i, tgt); - } - break; - } - /* Intentional fallthrough */ - - /* Replace jumps to unconditional jumps */ - case FOR_ITER: - case JUMP_FORWARD: - case JUMP_ABSOLUTE: - case CONTINUE_LOOP: - case SETUP_LOOP: - case SETUP_EXCEPT: - case SETUP_FINALLY: - tgt = GETJUMPTGT(codestr, i); - if (!UNCONDITIONAL_JUMP(codestr[tgt])) - continue; - tgttgt = GETJUMPTGT(codestr, tgt); - if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */ - opcode = JUMP_ABSOLUTE; - if (!ABSOLUTE_JUMP(opcode)) - tgttgt -= i + 3; /* Calc relative jump addr */ - if (tgttgt < 0) /* No backward relative jumps */ - continue; - codestr[i] = opcode; - SETARG(codestr, i, tgttgt); - break; - - case EXTENDED_ARG: - goto exitUnchanged; - - /* Replace RETURN LOAD_CONST None RETURN with just RETURN */ - case RETURN_VALUE: - if (i+4 >= codelen || - codestr[i+4] != RETURN_VALUE || - !ISBASICBLOCK(blocks,i,5)) - continue; - memset(codestr+i+1, NOP, 4); - break; - } - } - - /* Fixup linenotab */ - for (i=0, nops=0 ; i= 0) { wrapper = PyList_GET_ITEM(c->c_stack, n); c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper); + assert(c->u); /* we are deleting from a list so this really shouldn't fail */ if (PySequence_DelItem(c->c_stack, n) < 0) Py_FatalError("compiler_exit_scope()"); @@ -4400,7 +3799,7 @@ makecode(struct compiler *c, struct assembler *a) if (flags < 0) goto error; - bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab); + bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); if (!bytecode) goto error; diff --git a/Python/import.c b/Python/import.c index ef37e8b5b5a..a05078e2b72 100644 --- a/Python/import.c +++ b/Python/import.c @@ -934,7 +934,8 @@ load_source_module(char *name, char *pathname, FILE *fp) if (Py_VerboseFlag) PySys_WriteStderr("import %s # from %s\n", name, pathname); - write_compiled_module(co, cpathname, mtime); + if (cpathname) + write_compiled_module(co, cpathname, mtime); } m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname); Py_DECREF(co); @@ -1233,6 +1234,8 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf, for (i = 0; i < npath; i++) { PyObject *copy = NULL; PyObject *v = PyList_GetItem(path, i); + if (!v) + return NULL; #ifdef Py_USING_UNICODE if (PyUnicode_Check(v)) { copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v), @@ -3045,6 +3048,8 @@ initimp(void) if (m == NULL) goto failure; d = PyModule_GetDict(m); + if (d == NULL) + goto failure; if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure; if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure; diff --git a/Python/marshal.c b/Python/marshal.c index 10a6c0c1d7a..c3bc87f21f0 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -213,6 +213,10 @@ w_object(PyObject *v, WFILE *p) w_byte(TYPE_COMPLEX, p); temp = (PyFloatObject*)PyFloat_FromDouble( PyComplex_RealAsDouble(v)); + if (!temp) { + p->error = 1; + return; + } PyFloat_AsReprString(buf, temp); Py_DECREF(temp); n = strlen(buf); @@ -220,6 +224,10 @@ w_object(PyObject *v, WFILE *p) w_string(buf, (int)n, p); temp = (PyFloatObject*)PyFloat_FromDouble( PyComplex_ImagAsDouble(v)); + if (!temp) { + p->error = 1; + return; + } PyFloat_AsReprString(buf, temp); Py_DECREF(temp); n = strlen(buf); diff --git a/Python/peephole.c b/Python/peephole.c new file mode 100644 index 00000000000..836d3983c99 --- /dev/null +++ b/Python/peephole.c @@ -0,0 +1,610 @@ +/* Peehole optimizations for bytecode compiler. */ + +#include "Python.h" + +#include "Python-ast.h" +#include "node.h" +#include "pyarena.h" +#include "ast.h" +#include "code.h" +#include "compile.h" +#include "symtable.h" +#include "opcode.h" + +#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) +#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) +#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP) +#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) +#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 +#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) +#define ISBASICBLOCK(blocks, start, bytes) \ + (blocks[start]==blocks[start+bytes-1]) + +/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n + with LOAD_CONST (c1, c2, ... cn). + The consts table must still be in list form so that the + new constant (c1, c2, ... cn) can be appended. + Called with codestr pointing to the first LOAD_CONST. + Bails out with no change if one or more of the LOAD_CONSTs is missing. + Also works for BUILD_LIST when followed by an "in" or "not in" test. +*/ +static int +tuple_of_constants(unsigned char *codestr, int n, PyObject *consts) +{ + PyObject *newconst, *constant; + Py_ssize_t i, arg, len_consts; + + /* Pre-conditions */ + assert(PyList_CheckExact(consts)); + assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST); + assert(GETARG(codestr, (n*3)) == n); + for (i=0 ; i 20) { + Py_DECREF(newconst); + return 0; + } + + /* Append folded constant into consts table */ + len_consts = PyList_GET_SIZE(consts); + if (PyList_Append(consts, newconst)) { + Py_DECREF(newconst); + return 0; + } + Py_DECREF(newconst); + + /* Write NOP NOP NOP NOP LOAD_CONST newconst */ + memset(codestr, NOP, 4); + codestr[4] = LOAD_CONST; + SETARG(codestr, 4, len_consts); + return 1; +} + +static int +fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts) +{ + PyObject *newconst=NULL, *v; + Py_ssize_t len_consts; + int opcode; + + /* Pre-conditions */ + assert(PyList_CheckExact(consts)); + assert(codestr[0] == LOAD_CONST); + + /* Create new constant */ + v = PyList_GET_ITEM(consts, GETARG(codestr, 0)); + opcode = codestr[3]; + switch (opcode) { + case UNARY_NEGATIVE: + /* Preserve the sign of -0.0 */ + if (PyObject_IsTrue(v) == 1) + newconst = PyNumber_Negative(v); + break; + case UNARY_CONVERT: + newconst = PyObject_Repr(v); + break; + case UNARY_INVERT: + newconst = PyNumber_Invert(v); + break; + default: + /* Called with an unknown opcode */ + PyErr_Format(PyExc_SystemError, + "unexpected unary operation %d on a constant", + opcode); + return 0; + } + if (newconst == NULL) { + PyErr_Clear(); + return 0; + } + + /* Append folded constant into consts table */ + len_consts = PyList_GET_SIZE(consts); + if (PyList_Append(consts, newconst)) { + Py_DECREF(newconst); + return 0; + } + Py_DECREF(newconst); + + /* Write NOP LOAD_CONST newconst */ + codestr[0] = NOP; + codestr[1] = LOAD_CONST; + SETARG(codestr, 1, len_consts); + return 1; +} + +static unsigned int * +markblocks(unsigned char *code, int len) +{ + unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int)); + int i,j, opcode, blockcnt = 0; + + if (blocks == NULL) { + PyErr_NoMemory(); + return NULL; + } + memset(blocks, 0, len*sizeof(int)); + + /* Mark labels in the first pass */ + for (i=0 ; i= 255. + + Optimizations are restricted to simple transformations occuring within a + single basic block. All transformations keep the code size the same or + smaller. For those that reduce size, the gaps are initially filled with + NOPs. Later those NOPs are removed and the jump addresses retargeted in + a single pass. Line numbering is adjusted accordingly. */ + +PyObject * +PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, + PyObject *lineno_obj) +{ + Py_ssize_t i, j, codelen; + int nops, h, adj; + int tgt, tgttgt, opcode; + unsigned char *codestr = NULL; + unsigned char *lineno; + int *addrmap = NULL; + int new_line, cum_orig_line, last_line, tabsiz; + int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONSTs */ + unsigned int *blocks = NULL; + char *name; + + /* Bail out if an exception is set */ + if (PyErr_Occurred()) + goto exitUnchanged; + + /* Bypass optimization when the lineno table is too complex */ + assert(PyString_Check(lineno_obj)); + lineno = (unsigned char*)PyString_AS_STRING(lineno_obj); + tabsiz = PyString_GET_SIZE(lineno_obj); + if (memchr(lineno, 255, tabsiz) != NULL) + goto exitUnchanged; + + /* Avoid situations where jump retargeting could overflow */ + assert(PyString_Check(code)); + codelen = PyString_Size(code); + if (codelen > 32700) + goto exitUnchanged; + + /* Make a modifiable copy of the code string */ + codestr = (unsigned char *)PyMem_Malloc(codelen); + if (codestr == NULL) + goto exitUnchanged; + codestr = (unsigned char *)memcpy(codestr, + PyString_AS_STRING(code), codelen); + + /* Verify that RETURN_VALUE terminates the codestring. This allows + the various transformation patterns to look ahead several + instructions without additional checks to make sure they are not + looking beyond the end of the code string. + */ + if (codestr[codelen-1] != RETURN_VALUE) + goto exitUnchanged; + + /* Mapping to new jump targets after NOPs are removed */ + addrmap = (int *)PyMem_Malloc(codelen * sizeof(int)); + if (addrmap == NULL) + goto exitUnchanged; + + blocks = markblocks(codestr, codelen); + if (blocks == NULL) + goto exitUnchanged; + assert(PyList_Check(consts)); + + for (i=0 ; i a is not b + not a in b --> a not in b + not a is not b --> a is b + not a not in b --> a in b + */ + case COMPARE_OP: + j = GETARG(codestr, i); + if (j < 6 || j > 9 || + codestr[i+3] != UNARY_NOT || + !ISBASICBLOCK(blocks,i,4)) + continue; + SETARG(codestr, i, (j^1)); + codestr[i+3] = NOP; + break; + + /* Replace LOAD_GLOBAL/LOAD_NAME None + with LOAD_CONST None */ + case LOAD_NAME: + case LOAD_GLOBAL: + j = GETARG(codestr, i); + name = PyString_AsString(PyTuple_GET_ITEM(names, j)); + if (name == NULL || strcmp(name, "None") != 0) + continue; + for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) { + if (PyList_GET_ITEM(consts, j) == Py_None) { + codestr[i] = LOAD_CONST; + SETARG(codestr, i, j); + cumlc = lastlc + 1; + break; + } + } + break; + + /* Skip over LOAD_CONST trueconst + JUMP_IF_FALSE xx POP_TOP */ + case LOAD_CONST: + cumlc = lastlc + 1; + j = GETARG(codestr, i); + if (codestr[i+3] != JUMP_IF_FALSE || + codestr[i+6] != POP_TOP || + !ISBASICBLOCK(blocks,i,7) || + !PyObject_IsTrue(PyList_GET_ITEM(consts, j))) + continue; + memset(codestr+i, NOP, 7); + cumlc = 0; + break; + + /* Try to fold tuples of constants (includes a case for lists + which are only used for "in" and "not in" tests). + Skip over BUILD_SEQN 1 UNPACK_SEQN 1. + Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. + Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ + case BUILD_TUPLE: + case BUILD_LIST: + j = GETARG(codestr, i); + h = i - 3 * j; + if (h >= 0 && + j <= lastlc && + ((opcode == BUILD_TUPLE && + ISBASICBLOCK(blocks, h, 3*(j+1))) || + (opcode == BUILD_LIST && + codestr[i+3]==COMPARE_OP && + ISBASICBLOCK(blocks, h, 3*(j+2)) && + (GETARG(codestr,i+3)==6 || + GETARG(codestr,i+3)==7))) && + tuple_of_constants(&codestr[h], j, consts)) { + assert(codestr[i] == LOAD_CONST); + cumlc = 1; + break; + } + if (codestr[i+3] != UNPACK_SEQUENCE || + !ISBASICBLOCK(blocks,i,6) || + j != GETARG(codestr, i+3)) + continue; + if (j == 1) { + memset(codestr+i, NOP, 6); + } else if (j == 2) { + codestr[i] = ROT_TWO; + memset(codestr+i+1, NOP, 5); + } else if (j == 3) { + codestr[i] = ROT_THREE; + codestr[i+1] = ROT_TWO; + memset(codestr+i+2, NOP, 4); + } + break; + + /* Fold binary ops on constants. + LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */ + case BINARY_POWER: + case BINARY_MULTIPLY: + case BINARY_TRUE_DIVIDE: + case BINARY_FLOOR_DIVIDE: + case BINARY_MODULO: + case BINARY_ADD: + case BINARY_SUBTRACT: + case BINARY_SUBSCR: + case BINARY_LSHIFT: + case BINARY_RSHIFT: + case BINARY_AND: + case BINARY_XOR: + case BINARY_OR: + if (lastlc >= 2 && + ISBASICBLOCK(blocks, i-6, 7) && + fold_binops_on_constants(&codestr[i-6], consts)) { + i -= 2; + assert(codestr[i] == LOAD_CONST); + cumlc = 1; + } + break; + + /* Fold unary ops on constants. + LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */ + case UNARY_NEGATIVE: + case UNARY_CONVERT: + case UNARY_INVERT: + if (lastlc >= 1 && + ISBASICBLOCK(blocks, i-3, 4) && + fold_unaryops_on_constants(&codestr[i-3], consts)) { + i -= 2; + assert(codestr[i] == LOAD_CONST); + cumlc = 1; + } + break; + + /* Simplify conditional jump to conditional jump where the + result of the first test implies the success of a similar + test or the failure of the opposite test. + Arises in code like: + "if a and b:" + "if a or b:" + "a and b or c" + "(a and b) and c" + x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z + x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3 + where y+3 is the instruction following the second test. + */ + case JUMP_IF_FALSE: + case JUMP_IF_TRUE: + tgt = GETJUMPTGT(codestr, i); + j = codestr[tgt]; + if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) { + if (j == opcode) { + tgttgt = GETJUMPTGT(codestr, tgt) - i - 3; + SETARG(codestr, i, tgttgt); + } else { + tgt -= i; + SETARG(codestr, i, tgt); + } + break; + } + /* Intentional fallthrough */ + + /* Replace jumps to unconditional jumps */ + case FOR_ITER: + case JUMP_FORWARD: + case JUMP_ABSOLUTE: + case CONTINUE_LOOP: + case SETUP_LOOP: + case SETUP_EXCEPT: + case SETUP_FINALLY: + tgt = GETJUMPTGT(codestr, i); + if (!UNCONDITIONAL_JUMP(codestr[tgt])) + continue; + tgttgt = GETJUMPTGT(codestr, tgt); + if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */ + opcode = JUMP_ABSOLUTE; + if (!ABSOLUTE_JUMP(opcode)) + tgttgt -= i + 3; /* Calc relative jump addr */ + if (tgttgt < 0) /* No backward relative jumps */ + continue; + codestr[i] = opcode; + SETARG(codestr, i, tgttgt); + break; + + case EXTENDED_ARG: + goto exitUnchanged; + + /* Replace RETURN LOAD_CONST None RETURN with just RETURN */ + case RETURN_VALUE: + if (i+4 >= codelen || + codestr[i+4] != RETURN_VALUE || + !ISBASICBLOCK(blocks,i,5)) + continue; + memset(codestr+i+1, NOP, 4); + break; + } + } + + /* Fixup linenotab */ + for (i=0, nops=0 ; iinterp; PyThreadState *p; - int count = 0; + + /* Although the GIL is held, a few C API functions can be called + * without the GIL held, and in particular some that create and + * destroy thread and interpreter states. Those can mutate the + * list of thread states we're traversing, so to prevent that we lock + * head_mutex for the duration. + */ HEAD_LOCK(); for (p = interp->tstate_head; p != NULL; p = p->next) { - if (p->thread_id != id) - continue; - Py_CLEAR(p->async_exc); - Py_XINCREF(exc); - p->async_exc = exc; - count += 1; + if (p->thread_id == id) { + /* Tricky: we need to decref the current value + * (if any) in p->async_exc, but that can in turn + * allow arbitrary Python code to run, including + * perhaps calls to this function. To prevent + * deadlock, we need to release head_mutex before + * the decref. + */ + PyObject *old_exc = p->async_exc; + Py_XINCREF(exc); + p->async_exc = exc; + HEAD_UNLOCK(); + Py_XDECREF(old_exc); + return 1; + } } HEAD_UNLOCK(); - return count; + return 0; } diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 555c39d5337..e170090a82e 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -199,12 +199,16 @@ Py_InitializeEx(int install_sigs) if (bimod == NULL) Py_FatalError("Py_Initialize: can't initialize __builtin__"); interp->builtins = PyModule_GetDict(bimod); + if (interp->builtins == NULL) + Py_FatalError("Py_Initialize: can't initialize builtins dict"); Py_INCREF(interp->builtins); sysmod = _PySys_Init(); if (sysmod == NULL) Py_FatalError("Py_Initialize: can't initialize sys"); interp->sysdict = PyModule_GetDict(sysmod); + if (interp->sysdict == NULL) + Py_FatalError("Py_Initialize: can't initialize sys dict"); Py_INCREF(interp->sysdict); _PyImport_FixupExtension("sys", "sys"); PySys_SetPath(Py_GetPath()); diff --git a/Python/symtable.c b/Python/symtable.c index 439a24349df..05d504c2cb6 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -221,8 +221,8 @@ PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future) return st; st->st_filename = filename; st->st_future = future; - if (!symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock, - (void *)mod, 0)) { + if (!GET_IDENTIFIER(top) || + !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) { PySymtable_Free(st); return NULL; } @@ -915,6 +915,8 @@ symtable_new_tmpname(struct symtable *st) PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++st->st_cur->ste_tmpname); tmp = PyString_InternFromString(tmpname); + if (!tmp) + return 0; if (!symtable_add_def(st, tmp, DEF_LOCAL)) return 0; Py_DECREF(tmp); @@ -1121,12 +1123,13 @@ symtable_visit_expr(struct symtable *st, expr_ty e) VISIT(st, expr, e->v.UnaryOp.operand); break; case Lambda_kind: { - if (!symtable_add_def(st, GET_IDENTIFIER(lambda), DEF_LOCAL)) + if (!GET_IDENTIFIER(lambda) || + !symtable_add_def(st, lambda, DEF_LOCAL)) return 0; if (e->v.Lambda.args->defaults) VISIT_SEQ(st, expr, e->v.Lambda.args->defaults); /* XXX how to get line numbers for expressions */ - if (!symtable_enter_block(st, GET_IDENTIFIER(lambda), + if (!symtable_enter_block(st, lambda, FunctionBlock, (void *)e, 0)) return 0; VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e); @@ -1323,8 +1326,11 @@ symtable_visit_alias(struct symtable *st, alias_ty a) PyObject *name = (a->asname == NULL) ? a->name : a->asname; const char *base = PyString_AS_STRING(name); char *dot = strchr(base, '.'); - if (dot) + if (dot) { store_name = PyString_FromStringAndSize(base, dot - base); + if (!store_name) + return 0; + } else { store_name = name; Py_INCREF(store_name); @@ -1399,8 +1405,8 @@ symtable_visit_genexp(struct symtable *st, expr_ty e) /* Outermost iterator is evaluated in current scope */ VISIT(st, expr, outermost->iter); /* Create generator scope for the rest */ - if (!symtable_enter_block(st, GET_IDENTIFIER(genexpr), - FunctionBlock, (void *)e, 0)) { + if (!GET_IDENTIFIER(genexpr) || + !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) { return 0; } st->st_cur->ste_generator = 1; @@ -1414,7 +1420,5 @@ symtable_visit_genexp(struct symtable *st, expr_ty e) VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension, e->v.GeneratorExp.generators, 1, (void*)e); VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e); - if (!symtable_exit_block(st, (void *)e)) - return 0; - return 1; + return symtable_exit_block(st, (void *)e); } diff --git a/Tools/buildbot/external.bat b/Tools/buildbot/external.bat index c6d252ddb9e..463285fa498 100644 --- a/Tools/buildbot/external.bat +++ b/Tools/buildbot/external.bat @@ -28,6 +28,7 @@ if not exist tcl8.4.12 ( cd tk8.4.12\win nmake -f makefile.vc TCLDIR=..\..\tcl8.4.12 nmake -f makefile.vc TCLDIR=..\..\tcl8.4.12 INSTALLDIR=..\..\tcltk install + cd ..\.. ) @rem sqlite diff --git a/Tools/msi/msi.py b/Tools/msi/msi.py index aebab9836a3..8a72eb93d06 100644 --- a/Tools/msi/msi.py +++ b/Tools/msi/msi.py @@ -89,7 +89,8 @@ extensions = [ '_msi.pyd', '_ctypes.pyd', '_ctypes_test.pyd', - '_sqlite3.pyd' + '_sqlite3.pyd', + '_hashlib.pyd' ] # Well-known component UUIDs