[Python-checkins] r60828 - in python/branches/trunk-math: Doc/c-api/method.rst Doc/c-api/tuple.rst Doc/c-api/unicode.rst Doc/library/ctypes.rst Doc/library/decimal.rst Include/classobject.h Include/frameobject.h Include/methodobject.h Include/tupleobject.h Include/unicodeobject.h Lib/SocketServer.py Lib/UserString.py Lib/test/test_complex.py Misc/NEWS Modules/gcmodule.c Objects/abstract.c Objects/classobject.c Objects/complexobject.c Objects/frameobject.c Objects/methodobject.c Objects/tupleobject.c Objects/unicodeobject.c

christian.heimes python-checkins at python.org
Fri Feb 15 07:59:42 CET 2008


Author: christian.heimes
Date: Fri Feb 15 07:59:40 2008
New Revision: 60828

Modified:
   python/branches/trunk-math/   (props changed)
   python/branches/trunk-math/Doc/c-api/method.rst
   python/branches/trunk-math/Doc/c-api/tuple.rst
   python/branches/trunk-math/Doc/c-api/unicode.rst
   python/branches/trunk-math/Doc/library/ctypes.rst
   python/branches/trunk-math/Doc/library/decimal.rst
   python/branches/trunk-math/Include/classobject.h
   python/branches/trunk-math/Include/frameobject.h
   python/branches/trunk-math/Include/methodobject.h
   python/branches/trunk-math/Include/tupleobject.h
   python/branches/trunk-math/Include/unicodeobject.h
   python/branches/trunk-math/Lib/SocketServer.py
   python/branches/trunk-math/Lib/UserString.py
   python/branches/trunk-math/Lib/test/test_complex.py
   python/branches/trunk-math/Misc/NEWS
   python/branches/trunk-math/Modules/gcmodule.c
   python/branches/trunk-math/Objects/abstract.c
   python/branches/trunk-math/Objects/classobject.c
   python/branches/trunk-math/Objects/complexobject.c
   python/branches/trunk-math/Objects/frameobject.c
   python/branches/trunk-math/Objects/methodobject.c
   python/branches/trunk-math/Objects/tupleobject.c
   python/branches/trunk-math/Objects/unicodeobject.c
Log:
Merged revisions 60788-60827 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r60790 | raymond.hettinger | 2008-02-14 10:32:45 +0100 (Thu, 14 Feb 2008) | 4 lines
  
  Add diagnostic message to help figure-out why SocketServer tests occasionally crash
  when trying to remove a pid that in not in the activechildren list.
........
  r60791 | raymond.hettinger | 2008-02-14 11:46:57 +0100 (Thu, 14 Feb 2008) | 1 line
  
  Add fixed-point examples to the decimal FAQ
........
  r60792 | raymond.hettinger | 2008-02-14 12:01:10 +0100 (Thu, 14 Feb 2008) | 1 line
  
  Improve rst markup
........
  r60794 | raymond.hettinger | 2008-02-14 12:57:25 +0100 (Thu, 14 Feb 2008) | 1 line
  
  Show how to remove exponents.
........
  r60795 | raymond.hettinger | 2008-02-14 13:05:42 +0100 (Thu, 14 Feb 2008) | 1 line
  
  Fix markup.
........
  r60797 | christian.heimes | 2008-02-14 13:47:33 +0100 (Thu, 14 Feb 2008) | 1 line
  
  Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
........
  r60798 | raymond.hettinger | 2008-02-14 13:49:37 +0100 (Thu, 14 Feb 2008) | 1 line
  
  Simplify moneyfmt() recipe.
........
  r60810 | raymond.hettinger | 2008-02-14 20:02:39 +0100 (Thu, 14 Feb 2008) | 1 line
  
  Fix markup
........
  r60811 | raymond.hettinger | 2008-02-14 20:30:30 +0100 (Thu, 14 Feb 2008) | 1 line
  
  No need to register subclass of ABCs.
........
  r60814 | thomas.heller | 2008-02-14 22:00:28 +0100 (Thu, 14 Feb 2008) | 1 line
  
  Try to correct a markup error that does hide the following paragraph.
........
  r60822 | christian.heimes | 2008-02-14 23:40:11 +0100 (Thu, 14 Feb 2008) | 1 line
  
  Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
........
  r60827 | christian.heimes | 2008-02-15 07:57:08 +0100 (Fri, 15 Feb 2008) | 1 line
  
  Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
........


Modified: python/branches/trunk-math/Doc/c-api/method.rst
==============================================================================
--- python/branches/trunk-math/Doc/c-api/method.rst	(original)
+++ python/branches/trunk-math/Doc/c-api/method.rst	Fri Feb 15 07:59:40 2008
@@ -63,3 +63,10 @@
 .. cfunction:: PyObject* PyMethod_GET_SELF(PyObject *meth)
 
    Macro version of :cfunc:`PyMethod_Self` which avoids error checking.
+
+
+.. cfunction:: int PyMethod_ClearFreeList(void)
+
+   Clear the free list. Return the total number of freed items.
+
+   .. versionadded:: 2.6

Modified: python/branches/trunk-math/Doc/c-api/tuple.rst
==============================================================================
--- python/branches/trunk-math/Doc/c-api/tuple.rst	(original)
+++ python/branches/trunk-math/Doc/c-api/tuple.rst	Fri Feb 15 07:59:40 2008
@@ -115,3 +115,10 @@
 
    .. versionchanged:: 2.2
       Removed unused third parameter, *last_is_sticky*.
+
+
+.. cfunction:: int PyMethod_ClearFreeList(void)
+
+   Clear the free list. Return the total number of freed items.
+
+   .. versionadded:: 2.6

Modified: python/branches/trunk-math/Doc/c-api/unicode.rst
==============================================================================
--- python/branches/trunk-math/Doc/c-api/unicode.rst	(original)
+++ python/branches/trunk-math/Doc/c-api/unicode.rst	Fri Feb 15 07:59:40 2008
@@ -89,6 +89,13 @@
    Return a pointer to the internal buffer of the object. *o* has to be a
    :ctype:`PyUnicodeObject` (not checked).
 
+
+.. cfunction:: int PyUnicode_ClearFreeList(void)
+
+   Clear the free list. Return the total number of freed items.
+
+   .. versionadded:: 2.6
+
 Unicode provides many different character properties. The most often needed ones
 are available through these macros which are mapped to C functions depending on
 the Python configuration.

Modified: python/branches/trunk-math/Doc/library/ctypes.rst
==============================================================================
--- python/branches/trunk-math/Doc/library/ctypes.rst	(original)
+++ python/branches/trunk-math/Doc/library/ctypes.rst	Fri Feb 15 07:59:40 2008
@@ -2012,7 +2012,6 @@
    their methods and attributes.
 
    .. versionchanged:: 2.6
-
       ctypes data types that are not and do not contain pointers can
       now be pickled.
 

Modified: python/branches/trunk-math/Doc/library/decimal.rst
==============================================================================
--- python/branches/trunk-math/Doc/library/decimal.rst	(original)
+++ python/branches/trunk-math/Doc/library/decimal.rst	Fri Feb 15 07:59:40 2008
@@ -1404,19 +1404,15 @@
        '<.02>'
 
        """
-       q = Decimal((0, (1,), -places))    # 2 places --> '0.01'
-       sign, digits, exp = value.quantize(q).as_tuple()
-       assert exp == -places    
+       q = Decimal(10) ** -places      # 2 places --> '0.01'
+       sign, digits, exp = value.quantize(q).as_tuple()  
        result = []
        digits = map(str, digits)
        build, next = result.append, digits.pop
        if sign:
            build(trailneg)
        for i in range(places):
-           if digits:
-               build(next())
-           else:
-               build('0')
+           build(next() if digits else '0')
        build(dp)
        i = 0
        while digits:
@@ -1426,12 +1422,8 @@
                i = 0
                build(sep)
        build(curr)
-       if sign:
-           build(neg)
-       else:
-           build(pos)
-       result.reverse()
-       return ''.join(result)
+       build(neg if sign else pos)
+       return ''.join(reversed(result))
 
    def pi():
        """Compute Pi to the current precision.
@@ -1534,7 +1526,7 @@
 Q. It is cumbersome to type ``decimal.Decimal('1234.5')``.  Is there a way to
 minimize typing when using the interactive interpreter?
 
-\A. Some users abbreviate the constructor to just a single letter::
+A. Some users abbreviate the constructor to just a single letter::
 
    >>> D = decimal.Decimal
    >>> D('1.23') + D('3.45')
@@ -1565,9 +1557,36 @@
 Q. Once I have valid two place inputs, how do I maintain that invariant
 throughout an application?
 
-A. Some operations like addition and subtraction automatically preserve fixed
-point.  Others, like multiplication and division, change the number of decimal
-places and need to be followed-up with a :meth:`quantize` step.
+A. Some operations like addition, subtraction, and multiplication by an integer
+will automatically preserve fixed point.  Others operations, like division and
+non-integer multiplication, will change the number of decimal places and need to
+be followed-up with a :meth:`quantize` step::
+
+    >>> a = Decimal('102.72')           # Initial fixed-point values
+    >>> b = Decimal('3.17')
+    >>> a + b                           # Addition preserves fixed-point
+    Decimal('105.89')
+    >>> a - b
+    Decimal('99.55')
+    >>> a * 42                          # So does integer multiplication
+    Decimal('4314.24')
+    >>> (a * b).quantize(TWOPLACES)     # Must quantize non-integer multiplication
+    Decimal('325.62')
+    >>> (b / a).quantize(TWOPLACES)     # And quantize division
+    Decimal('0.03')
+
+In developing fixed-point applications, it is convenient to define functions
+to handle the :meth:`quantize` step::
+
+    >>> def mul(x, y, fp=TWOPLACES):
+    ...     return (x * y).quantize(fp)
+    >>> def div(x, y, fp=TWOPLACES):
+    ...     return (x / y).quantize(fp)
+
+    >>> mul(a, b)                       # Automatically preserve fixed-point
+    Decimal('325.62')
+    >>> div(b, a)
+    Decimal('0.03')
 
 Q. There are many ways to express the same value.  The numbers :const:`200`,
 :const:`200.000`, :const:`2E2`, and :const:`.02E+4` all have the same value at
@@ -1589,6 +1608,16 @@
 :const:`5.0E+3` as :const:`5000` keeps the value constant but cannot show the
 original's two-place significance.
 
+If an application does not care about tracking significance, it is easy to
+remove the exponent and trailing zeroes, losing signficance, but keeping the
+value unchanged::
+
+    >>> def remove_exponent(d):
+    ...     return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()
+
+    >>> remove_exponent(Decimal('5E+3'))
+    Decimal('5000')
+
 Q. Is there a way to convert a regular float to a :class:`Decimal`?
 
 A. Yes, all binary floating point numbers can be exactly expressed as a

Modified: python/branches/trunk-math/Include/classobject.h
==============================================================================
--- python/branches/trunk-math/Include/classobject.h	(original)
+++ python/branches/trunk-math/Include/classobject.h	Fri Feb 15 07:59:40 2008
@@ -74,6 +74,7 @@
 
 PyAPI_FUNC(int) PyClass_IsSubclass(PyObject *, PyObject *);
 
+PyAPI_FUNC(int) PyMethod_ClearFreeList(void);
 
 #ifdef __cplusplus
 }

Modified: python/branches/trunk-math/Include/frameobject.h
==============================================================================
--- python/branches/trunk-math/Include/frameobject.h	(original)
+++ python/branches/trunk-math/Include/frameobject.h	Fri Feb 15 07:59:40 2008
@@ -75,6 +75,8 @@
 PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int);
 PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);
 
+PyAPI_FUNC(int) PyFrame_ClearFreeList(void);
+
 #ifdef __cplusplus
 }
 #endif

Modified: python/branches/trunk-math/Include/methodobject.h
==============================================================================
--- python/branches/trunk-math/Include/methodobject.h	(original)
+++ python/branches/trunk-math/Include/methodobject.h	Fri Feb 15 07:59:40 2008
@@ -85,6 +85,8 @@
     PyObject    *m_module; /* The __module__ attribute, can be anything */
 } PyCFunctionObject;
 
+PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);
+
 #ifdef __cplusplus
 }
 #endif

Modified: python/branches/trunk-math/Include/tupleobject.h
==============================================================================
--- python/branches/trunk-math/Include/tupleobject.h	(original)
+++ python/branches/trunk-math/Include/tupleobject.h	Fri Feb 15 07:59:40 2008
@@ -52,6 +52,8 @@
 /* Macro, *only* to be used to fill in brand new tuples */
 #define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = v)
 
+PyAPI_FUNC(int) PyTuple_ClearFreeList(void);
+
 #ifdef __cplusplus
 }
 #endif

Modified: python/branches/trunk-math/Include/unicodeobject.h
==============================================================================
--- python/branches/trunk-math/Include/unicodeobject.h	(original)
+++ python/branches/trunk-math/Include/unicodeobject.h	Fri Feb 15 07:59:40 2008
@@ -209,6 +209,7 @@
 # define _PyUnicode_AsDefaultEncodedString _PyUnicodeUCS2_AsDefaultEncodedString
 # define _PyUnicode_Fini _PyUnicodeUCS2_Fini
 # define _PyUnicode_Init _PyUnicodeUCS2_Init
+# define PyUnicode_ClearFreeList PyUnicodeUCS2_ClearFreelist
 # define _PyUnicode_IsAlpha _PyUnicodeUCS2_IsAlpha
 # define _PyUnicode_IsDecimalDigit _PyUnicodeUCS2_IsDecimalDigit
 # define _PyUnicode_IsDigit _PyUnicodeUCS2_IsDigit
@@ -295,6 +296,7 @@
 # define _PyUnicode_AsDefaultEncodedString _PyUnicodeUCS4_AsDefaultEncodedString
 # define _PyUnicode_Fini _PyUnicodeUCS4_Fini
 # define _PyUnicode_Init _PyUnicodeUCS4_Init
+# define PyUnicode_ClearFreeList PyUnicodeUCS2_ClearFreelist
 # define _PyUnicode_IsAlpha _PyUnicodeUCS4_IsAlpha
 # define _PyUnicode_IsDecimalDigit _PyUnicodeUCS4_IsDecimalDigit
 # define _PyUnicode_IsDigit _PyUnicodeUCS4_IsDigit
@@ -403,6 +405,8 @@
 extern "C" {
 #endif
 
+PyAPI_FUNC(int) PyUnicode_ClearFreeList(void);
+
 /* --- Unicode Type ------------------------------------------------------- */
 
 typedef struct {

Modified: python/branches/trunk-math/Lib/SocketServer.py
==============================================================================
--- python/branches/trunk-math/Lib/SocketServer.py	(original)
+++ python/branches/trunk-math/Lib/SocketServer.py	Fri Feb 15 07:59:40 2008
@@ -452,7 +452,11 @@
             except os.error:
                 pid = None
             if not pid: break
-            self.active_children.remove(pid)
+            try:
+                self.active_children.remove(pid)
+            except ValueError, e:
+                raise ValueError('%s. x=%d and list=%r' % (e.message, pid,
+                                                           self.active_children))
 
     def handle_timeout(self):
         """Wait for zombies after self.timeout seconds of inactivity.

Modified: python/branches/trunk-math/Lib/UserString.py
==============================================================================
--- python/branches/trunk-math/Lib/UserString.py	(original)
+++ python/branches/trunk-math/Lib/UserString.py	Fri Feb 15 07:59:40 2008
@@ -130,8 +130,6 @@
     def upper(self): return self.__class__(self.data.upper())
     def zfill(self, width): return self.__class__(self.data.zfill(width))
 
-collections.Sequence.register(UserString)
-
 class MutableString(UserString, collections.MutableSequence):
     """mutable string objects
 

Modified: python/branches/trunk-math/Lib/test/test_complex.py
==============================================================================
--- python/branches/trunk-math/Lib/test/test_complex.py	(original)
+++ python/branches/trunk-math/Lib/test/test_complex.py	Fri Feb 15 07:59:40 2008
@@ -11,6 +11,8 @@
 from random import random
 from math import atan2, pi
 
+INF = float("inf")
+NAN = float("nan")
 # These tests ensure that complex math does the right thing
 
 class ComplexTest(unittest.TestCase):
@@ -337,6 +339,18 @@
         self.assertEqual(-6j,complex(repr(-6j)))
         self.assertEqual(6j,complex(repr(6j)))
 
+        self.assertEqual(repr(complex(1., INF)), "(1+inf*j)")
+        self.assertEqual(repr(complex(1., -INF)), "(1-inf*j)")
+        self.assertEqual(repr(complex(INF, 1)), "(inf+1j)")
+        self.assertEqual(repr(complex(-INF, INF)), "(-inf+inf*j)")
+        self.assertEqual(repr(complex(NAN, 1)), "(nan+1j)")
+        self.assertEqual(repr(complex(1, NAN)), "(1+nan*j)")
+        self.assertEqual(repr(complex(NAN, NAN)), "(nan+nan*j)")
+
+        self.assertEqual(repr(complex(0, INF)), "inf*j")
+        self.assertEqual(repr(complex(0, -INF)), "-inf*j")
+        self.assertEqual(repr(complex(0, NAN)), "nan*j")
+
     def test_neg(self):
         self.assertEqual(-(1+6j), -1-6j)
 

Modified: python/branches/trunk-math/Misc/NEWS
==============================================================================
--- python/branches/trunk-math/Misc/NEWS	(original)
+++ python/branches/trunk-math/Misc/NEWS	Fri Feb 15 07:59:40 2008
@@ -20,6 +20,13 @@
   without the four functions and copysign in libm were added to a new file
   Python/pymath.c.
 
+- Fixed repr() and str() of complex numbers with infinity or nan as real or
+  imaginary part.
+
+- Clear all free list during a gc.collect() of the highest generation in order
+  to allow pymalloc to free more arenas. Python may give back memory to the
+  OS earlier.
+
 - Issue #2045: Fix an infinite recursion triggered when printing a subclass of
   collections.defaultdict, if its default_factory is set to a bound method.
 

Modified: python/branches/trunk-math/Modules/gcmodule.c
==============================================================================
--- python/branches/trunk-math/Modules/gcmodule.c	(original)
+++ python/branches/trunk-math/Modules/gcmodule.c	Fri Feb 15 07:59:40 2008
@@ -19,6 +19,7 @@
 */
 
 #include "Python.h"
+#include "frameobject.h"	/* for PyFrame_ClearFreeList */
 
 /* Get an object's GC head */
 #define AS_GC(o) ((PyGC_Head *)(o)-1)
@@ -722,6 +723,21 @@
 	}
 }
 
+/* Clear all free lists
+ * All free lists are cleared during the collection of the highest generation.
+ * Allocated items in the free list may keep a pymalloc arena occupied.
+ * Clearing the free lists may give back memory to the OS earlier.
+ */
+static void
+clear_freelists(void)
+{
+	(void)PyMethod_ClearFreeList();
+	(void)PyFrame_ClearFreeList();
+	(void)PyCFunction_ClearFreeList();
+	(void)PyTuple_ClearFreeList();
+	(void)PyUnicode_ClearFreeList();
+}
+
 /* This is the main function.  Read this to understand how the
  * collection process works. */
 static Py_ssize_t
@@ -874,6 +890,12 @@
 	 */
 	(void)handle_finalizers(&finalizers, old);
 
+	/* Clear free list only during the collection of the higest
+	 * generation */
+	if (generation == NUM_GENERATIONS-1) {
+		clear_freelists();
+	}
+
 	if (PyErr_Occurred()) {
 		if (gc_str == NULL)
 			gc_str = PyString_FromString("garbage collection");

Modified: python/branches/trunk-math/Objects/abstract.c
==============================================================================
--- python/branches/trunk-math/Objects/abstract.c	(original)
+++ python/branches/trunk-math/Objects/abstract.c	Fri Feb 15 07:59:40 2008
@@ -2401,10 +2401,17 @@
 int
 PyObject_IsInstance(PyObject *inst, PyObject *cls)
 {
+	static PyObject *name = NULL;
 	PyObject *t, *v, *tb;
 	PyObject *checker;
 	PyErr_Fetch(&t, &v, &tb);
-	checker = PyObject_GetAttrString(cls, "__instancecheck__");
+
+	if (name == NULL) {
+		name = PyString_InternFromString("__instancecheck__");
+		if (name == NULL)
+			return -1;
+	}
+	checker = PyObject_GetAttr(cls, name);
 	PyErr_Restore(t, v, tb);
 	if (checker != NULL) {
 		PyObject *res;
@@ -2477,10 +2484,17 @@
 int
 PyObject_IsSubclass(PyObject *derived, PyObject *cls)
 {
+	static PyObject *name = NULL;
 	PyObject *t, *v, *tb;
 	PyObject *checker;
 	PyErr_Fetch(&t, &v, &tb);
-	checker = PyObject_GetAttrString(cls, "__subclasscheck__");
+	
+	if (name == NULL) {
+		name = PyString_InternFromString("__subclasscheck__");
+		if (name == NULL)
+			return -1;
+	}
+	checker = PyObject_GetAttr(cls, name);
 	PyErr_Restore(t, v, tb);
 	if (checker != NULL) {
 		PyObject *res;

Modified: python/branches/trunk-math/Objects/classobject.c
==============================================================================
--- python/branches/trunk-math/Objects/classobject.c	(original)
+++ python/branches/trunk-math/Objects/classobject.c	Fri Feb 15 07:59:40 2008
@@ -2626,9 +2626,11 @@
 
 /* Clear out the free list */
 
-void
-PyMethod_Fini(void)
+int
+PyMethod_ClearFreeList(void)
 {
+	int freelist_size = numfree;
+	
 	while (free_list) {
 		PyMethodObject *im = free_list;
 		free_list = (PyMethodObject *)(im->im_self);
@@ -2636,4 +2638,11 @@
 		numfree--;
 	}
 	assert(numfree == 0);
+	return freelist_size;
+}
+
+void
+PyMethod_Fini(void)
+{
+	(void)PyMethod_ClearFreeList();
 }

Modified: python/branches/trunk-math/Objects/complexobject.c
==============================================================================
--- python/branches/trunk-math/Objects/complexobject.c	(original)
+++ python/branches/trunk-math/Objects/complexobject.c	Fri Feb 15 07:59:40 2008
@@ -322,16 +322,49 @@
 {
 	char format[32];
 	if (v->cval.real == 0.) {
-		PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
-		PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
-		strncat(buf, "j", 1);
+		if (!Py_IS_FINITE(v->cval.imag)) {
+			if (Py_IS_NAN(v->cval.imag))
+				strncpy(buf, "nan*j", 6);
+			/* else if (copysign(1, v->cval.imag) == 1) */
+			else if (v->cval.imag > 0)
+				strncpy(buf, "inf*j", 6);
+			else
+				strncpy(buf, "-inf*j", 7);
+		}
+		else {
+			PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
+			PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
+			strncat(buf, "j", 1);
+		}
 	} else {
 		char re[64], im[64];
 		/* Format imaginary part with sign, real part without */
-		PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
-		PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
-		PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
-		PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
+		if (!Py_IS_FINITE(v->cval.real)) {
+			if (Py_IS_NAN(v->cval.real))
+				strncpy(re, "nan", 4);
+			/* else if (copysign(1, v->cval.real) == 1) */
+			else if (v->cval.real > 0)
+				strncpy(re, "inf", 4);
+			else
+				strncpy(re, "-inf", 5);
+		}
+		else {
+			PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
+			PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
+		}
+		if (!Py_IS_FINITE(v->cval.imag)) {
+			if (Py_IS_NAN(v->cval.imag))
+				strncpy(im, "+nan*", 6);
+			/* else if (copysign(1, v->cval.imag) == 1) */
+			else if (v->cval.imag > 0)
+				strncpy(im, "+inf*", 6);
+			else
+				strncpy(im, "-inf*", 6);
+		}
+		else {
+			PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
+			PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
+		}
 		PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im);
 	}
 }

Modified: python/branches/trunk-math/Objects/frameobject.c
==============================================================================
--- python/branches/trunk-math/Objects/frameobject.c	(original)
+++ python/branches/trunk-math/Objects/frameobject.c	Fri Feb 15 07:59:40 2008
@@ -889,10 +889,11 @@
 }
 
 /* Clear out the free list */
-
-void
-PyFrame_Fini(void)
+int
+PyFrame_ClearFreeList(void)
 {
+	int freelist_size = numfree;
+	
 	while (free_list != NULL) {
 		PyFrameObject *f = free_list;
 		free_list = free_list->f_back;
@@ -900,6 +901,13 @@
 		--numfree;
 	}
 	assert(numfree == 0);
+	return freelist_size;
+}
+
+void
+PyFrame_Fini(void)
+{
+	(void)PyFrame_ClearFreeList();
 	Py_XDECREF(builtin_object);
 	builtin_object = NULL;
 }

Modified: python/branches/trunk-math/Objects/methodobject.c
==============================================================================
--- python/branches/trunk-math/Objects/methodobject.c	(original)
+++ python/branches/trunk-math/Objects/methodobject.c	Fri Feb 15 07:59:40 2008
@@ -353,9 +353,11 @@
 
 /* Clear out the free list */
 
-void
-PyCFunction_Fini(void)
+int
+PyCFunction_ClearFreeList(void)
 {
+	int freelist_size = numfree;
+	
 	while (free_list) {
 		PyCFunctionObject *v = free_list;
 		free_list = (PyCFunctionObject *)(v->m_self);
@@ -363,6 +365,13 @@
 		numfree--;
 	}
 	assert(numfree == 0);
+	return freelist_size;
+}
+
+void
+PyCFunction_Fini(void)
+{
+	(void)PyCFunction_ClearFreeList();
 }
 
 /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(),

Modified: python/branches/trunk-math/Objects/tupleobject.c
==============================================================================
--- python/branches/trunk-math/Objects/tupleobject.c	(original)
+++ python/branches/trunk-math/Objects/tupleobject.c	Fri Feb 15 07:59:40 2008
@@ -832,19 +832,18 @@
 	return 0;
 }
 
-void
-PyTuple_Fini(void)
+int
+PyTuple_ClearFreeList(void)
 {
+	int freelist_size = 0;
 #if PyTuple_MAXSAVESIZE > 0
 	int i;
-
-	Py_XDECREF(free_list[0]);
-	free_list[0] = NULL;
-
 	for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
 		PyTupleObject *p, *q;
 		p = free_list[i];
+		freelist_size += numfree[i];
 		free_list[i] = NULL;
+		numfree[i] = 0;
 		while (p) {
 			q = p;
 			p = (PyTupleObject *)(p->ob_item[0]);
@@ -852,6 +851,20 @@
 		}
 	}
 #endif
+	return freelist_size;
+}
+	
+void
+PyTuple_Fini(void)
+{
+#if PyTuple_MAXSAVESIZE > 0
+	/* empty tuples are used all over the place and applications may
+	 * rely on the fact that an empty tuple is a singleton. */
+	Py_XDECREF(free_list[0]);
+	free_list[0] = NULL;
+
+	(void)PyTuple_ClearFreeList();
+#endif
 }
 
 /*********************** Tuple Iterator **************************/

Modified: python/branches/trunk-math/Objects/unicodeobject.c
==============================================================================
--- python/branches/trunk-math/Objects/unicodeobject.c	(original)
+++ python/branches/trunk-math/Objects/unicodeobject.c	Fri Feb 15 07:59:40 2008
@@ -8853,10 +8853,29 @@
 
 /* Finalize the Unicode implementation */
 
+int
+PyUnicode_ClearFreeList(void)
+{
+    int freelist_size = numfree;
+    PyUnicodeObject *u;
+
+    for (u = free_list; u != NULL;) {
+	PyUnicodeObject *v = u;
+	u = *(PyUnicodeObject **)u;
+	if (v->str)
+	    PyMem_DEL(v->str);
+	Py_XDECREF(v->defenc);
+	PyObject_Del(v);
+	numfree--;
+    }
+    free_list = NULL;
+    assert(numfree == 0);
+    return freelist_size;
+}
+
 void
 _PyUnicode_Fini(void)
 {
-    PyUnicodeObject *u;
     int i;
 
     Py_XDECREF(unicode_empty);
@@ -8868,17 +8887,7 @@
 	    unicode_latin1[i] = NULL;
 	}
     }
-
-    for (u = free_list; u != NULL;) {
-	PyUnicodeObject *v = u;
-	u = *(PyUnicodeObject **)u;
-	if (v->str)
-	    PyMem_DEL(v->str);
-	Py_XDECREF(v->defenc);
-	PyObject_Del(v);
-    }
-    free_list = NULL;
-    numfree = 0;
+    (void)PyUnicode_ClearFreeList();
 }
 
 #ifdef __cplusplus


More information about the Python-checkins mailing list