[Python-checkins] r60854 - in python/branches/trunk-math: Doc/whatsnew/2.6.rst Lib/idlelib/EditorWindow.py Lib/idlelib/NEWS.txt Lib/idlelib/ScriptBinding.py Lib/idlelib/configDialog.py Lib/logging/__init__.py Lib/test/test_descr.py Lib/test/test_gc.py Lib/test/test_mmap.py Misc/build.sh Objects/abstract.c Objects/complexobject.c Objects/descrobject.c Objects/floatobject.c

christian.heimes python-checkins at python.org
Sat Feb 16 09:50:48 CET 2008


Author: christian.heimes
Date: Sat Feb 16 09:50:47 2008
New Revision: 60854

Modified:
   python/branches/trunk-math/   (props changed)
   python/branches/trunk-math/Doc/whatsnew/2.6.rst
   python/branches/trunk-math/Lib/idlelib/EditorWindow.py
   python/branches/trunk-math/Lib/idlelib/NEWS.txt
   python/branches/trunk-math/Lib/idlelib/ScriptBinding.py
   python/branches/trunk-math/Lib/idlelib/configDialog.py
   python/branches/trunk-math/Lib/logging/__init__.py
   python/branches/trunk-math/Lib/test/test_descr.py
   python/branches/trunk-math/Lib/test/test_gc.py
   python/branches/trunk-math/Lib/test/test_mmap.py
   python/branches/trunk-math/Misc/build.sh
   python/branches/trunk-math/Objects/abstract.c
   python/branches/trunk-math/Objects/complexobject.c
   python/branches/trunk-math/Objects/descrobject.c
   python/branches/trunk-math/Objects/floatobject.c
Log:
Merged revisions 60833-60853 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r60835 | eric.smith | 2008-02-15 13:14:32 +0100 (Fri, 15 Feb 2008) | 1 line
  
  In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long.  It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
........
  r60837 | skip.montanaro | 2008-02-15 20:03:59 +0100 (Fri, 15 Feb 2008) | 8 lines
  
  Two new functions:
  
    * place_summary_first copies the regrtest summary to the front of the file
      making it easier to scan quickly for problems.
  
    * count_failures gets the actual count of the number of failing tests, not
      just a 1 (some failures) or 0 (no failures).
........
  r60840 | raymond.hettinger | 2008-02-15 22:21:25 +0100 (Fri, 15 Feb 2008) | 1 line
  
  Update example to match the current syntax.
........
  r60841 | amaury.forgeotdarc | 2008-02-15 22:22:45 +0100 (Fri, 15 Feb 2008) | 8 lines
  
  Issue #2115: __slot__ attributes setting was 10x slower.
  Also correct a possible crash using ABCs.
  
  This change is exactly the same as an optimisation
  done 5 years ago, but on slot *access*:
  http://svn.python.org/view?view=rev&rev=28297
........
  r60842 | amaury.forgeotdarc | 2008-02-15 22:27:44 +0100 (Fri, 15 Feb 2008) | 2 lines
  
  Temporarily let these tests pass
........
  r60843 | kurt.kaiser | 2008-02-15 22:56:36 +0100 (Fri, 15 Feb 2008) | 2 lines
  
  ScriptBinding event handlers weren't returning 'break'. Patch 2050, Tal Einat.
........
  r60844 | kurt.kaiser | 2008-02-15 23:25:09 +0100 (Fri, 15 Feb 2008) | 4 lines
  
  Configured selection highlighting colors were ignored; updating highlighting
  in the config dialog would cause non-Python files to be colored as if they
  were Python source; improve use of ColorDelagator.  Patch 1334. Tal Einat.
........
  r60845 | amaury.forgeotdarc | 2008-02-15 23:44:20 +0100 (Fri, 15 Feb 2008) | 9 lines
  
  Re-enable tests, they were failing since gc.collect() clears the various freelists.
  They still remain fragile.
  
  For example, a call to assertEqual currently does not make any allocation
  (which surprised me at first).
  But this can change when gc.collect also deletes the numerous "zombie frames" 
  attached to each function.
........
  r60850 | amaury.forgeotdarc | 2008-02-16 01:16:50 +0100 (Sat, 16 Feb 2008) | 3 lines
  
  mmap.PROT_READ does not exists on win32;
  Skip this test created by r60830.
........
  r60851 | raymond.hettinger | 2008-02-16 02:22:54 +0100 (Sat, 16 Feb 2008) | 1 line
  
  Add __all__ to logging module.
........


Modified: python/branches/trunk-math/Doc/whatsnew/2.6.rst
==============================================================================
--- python/branches/trunk-math/Doc/whatsnew/2.6.rst	(original)
+++ python/branches/trunk-math/Doc/whatsnew/2.6.rst	Sat Feb 16 09:50:47 2008
@@ -825,7 +825,7 @@
      int int
      >>> var._asdict()
      {'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'}
-     >>> v2 = var._replace('name', 'amplitude')
+     >>> v2 = var._replace(name='amplitude')
      >>> v2
      variable(id=1, name='amplitude', type='int', size=4)
 

Modified: python/branches/trunk-math/Lib/idlelib/EditorWindow.py
==============================================================================
--- python/branches/trunk-math/Lib/idlelib/EditorWindow.py	(original)
+++ python/branches/trunk-math/Lib/idlelib/EditorWindow.py	Sat Feb 16 09:50:47 2008
@@ -107,16 +107,6 @@
         self.width = idleConf.GetOption('main','EditorWindow','width')
         self.text = text = MultiCallCreator(Text)(
                 text_frame, name='text', padx=5, wrap='none',
-                foreground=idleConf.GetHighlight(currentTheme,
-                        'normal',fgBg='fg'),
-                background=idleConf.GetHighlight(currentTheme,
-                        'normal',fgBg='bg'),
-                highlightcolor=idleConf.GetHighlight(currentTheme,
-                        'hilite',fgBg='fg'),
-                highlightbackground=idleConf.GetHighlight(currentTheme,
-                        'hilite',fgBg='bg'),
-                insertbackground=idleConf.GetHighlight(currentTheme,
-                        'cursor',fgBg='fg'),
                 width=self.width,
                 height=idleConf.GetOption('main','EditorWindow','height') )
         self.top.focused_widget = self.text
@@ -224,11 +214,6 @@
         self.num_context_lines = 50, 500, 5000000
 
         self.per = per = self.Percolator(text)
-        if self.ispythonsource(filename):
-            self.color = color = self.ColorDelegator()
-            per.insertfilter(color)
-        else:
-            self.color = None
 
         self.undo = undo = self.UndoDelegator()
         per.insertfilter(undo)
@@ -247,11 +232,13 @@
                                              menu=self.recent_files_menu)
         self.update_recent_files_list()
 
+        self.color = None # initialized below in self.ResetColorizer
         if filename:
             if os.path.exists(filename) and not os.path.isdir(filename):
                 io.loadfile(filename)
             else:
                 io.set_filename(filename)
+        self.ResetColorizer()
         self.saved_change_hook()
 
         self.set_indentation_params(self.ispythonsource(filename))
@@ -572,36 +559,42 @@
             self.flist.filename_changed_edit(self)
         self.saved_change_hook()
         self.top.update_windowlist_registry(self)
-        if self.ispythonsource(self.io.filename):
-            self.addcolorizer()
-        else:
-            self.rmcolorizer()
+        self.ResetColorizer()
 
-    def addcolorizer(self):
+    def _addcolorizer(self):
         if self.color:
             return
-        self.per.removefilter(self.undo)
-        self.color = self.ColorDelegator()
-        self.per.insertfilter(self.color)
-        self.per.insertfilter(self.undo)
+        if self.ispythonsource(self.io.filename):
+            self.color = self.ColorDelegator()
+        # can add more colorizers here...
+        if self.color:
+            self.per.removefilter(self.undo)
+            self.per.insertfilter(self.color)
+            self.per.insertfilter(self.undo)
 
-    def rmcolorizer(self):
+    def _rmcolorizer(self):
         if not self.color:
             return
         self.color.removecolors()
-        self.per.removefilter(self.undo)
         self.per.removefilter(self.color)
         self.color = None
-        self.per.insertfilter(self.undo)
 
     def ResetColorizer(self):
-        "Update the colour theme if it is changed"
-        # Called from configDialog.py
-        if self.color:
-            self.color = self.ColorDelegator()
-            self.per.insertfilter(self.color)
+        "Update the colour theme"
+        # Called from self.filename_change_hook and from configDialog.py
+        self._rmcolorizer()
+        self._addcolorizer()
         theme = idleConf.GetOption('main','Theme','name')
-        self.text.config(idleConf.GetHighlight(theme, "normal"))
+        normal_colors = idleConf.GetHighlight(theme, 'normal')
+        cursor_color = idleConf.GetHighlight(theme, 'cursor', fgBg='fg')
+        select_colors = idleConf.GetHighlight(theme, 'hilite')
+        self.text.config(
+            foreground=normal_colors['foreground'],
+            background=normal_colors['background'],
+            insertbackground=cursor_color,
+            selectforeground=select_colors['foreground'],
+            selectbackground=select_colors['background'],
+            )
 
     def ResetFont(self):
         "Update the text widgets' font if it is changed"

Modified: python/branches/trunk-math/Lib/idlelib/NEWS.txt
==============================================================================
--- python/branches/trunk-math/Lib/idlelib/NEWS.txt	(original)
+++ python/branches/trunk-math/Lib/idlelib/NEWS.txt	Sat Feb 16 09:50:47 2008
@@ -3,6 +3,12 @@
 
 *Release date: XX-XXX-2008*
 
+- Configured selection highlighting colors were ignored; updating highlighting
+  in the config dialog would cause non-Python files to be colored as if they
+  were Python source; improve use of ColorDelagator.  Patch 1334. Tal Einat.
+
+- ScriptBinding event handlers weren't returning 'break'. Patch 2050, Tal Einat.
+
 - There was an error on exit if no sys.exitfunc was defined. Issue 1647.
 
 - Could not open files in .idlerc directory if latter was hidden on Windows.

Modified: python/branches/trunk-math/Lib/idlelib/ScriptBinding.py
==============================================================================
--- python/branches/trunk-math/Lib/idlelib/ScriptBinding.py	(original)
+++ python/branches/trunk-math/Lib/idlelib/ScriptBinding.py	Sat Feb 16 09:50:47 2008
@@ -56,11 +56,11 @@
     def check_module_event(self, event):
         filename = self.getfilename()
         if not filename:
-            return
+            return 'break'
         if not self.checksyntax(filename):
-            return
+            return 'break'
         if not self.tabnanny(filename):
-            return
+            return 'break'
 
     def tabnanny(self, filename):
         f = open(filename, 'r')
@@ -136,12 +136,12 @@
         """
         filename = self.getfilename()
         if not filename:
-            return
+            return 'break'
         code = self.checksyntax(filename)
         if not code:
-            return
+            return 'break'
         if not self.tabnanny(filename):
-            return
+            return 'break'
         shell = self.shell
         interp = shell.interp
         if PyShell.use_subprocess:
@@ -164,6 +164,7 @@
         #         go to __stderr__.  With subprocess, they go to the shell.
         #         Need to change streams in PyShell.ModifiedInterpreter.
         interp.runcode(code)
+        return 'break'
 
     def getfilename(self):
         """Get source filename.  If not saved, offer to save (or create) file

Modified: python/branches/trunk-math/Lib/idlelib/configDialog.py
==============================================================================
--- python/branches/trunk-math/Lib/idlelib/configDialog.py	(original)
+++ python/branches/trunk-math/Lib/idlelib/configDialog.py	Sat Feb 16 09:50:47 2008
@@ -1119,15 +1119,12 @@
     def ActivateConfigChanges(self):
         "Dynamically apply configuration changes"
         winInstances=self.parent.instance_dict.keys()
-        theme = idleConf.CurrentTheme()
-        cursor_color = idleConf.GetHighlight(theme, 'cursor', fgBg='fg')
         for instance in winInstances:
             instance.ResetColorizer()
             instance.ResetFont()
             instance.set_notabs_indentwidth()
             instance.ApplyKeybindings()
             instance.reset_help_menu_entries()
-            instance.text.configure(insertbackground=cursor_color)
 
     def Cancel(self):
         self.destroy()

Modified: python/branches/trunk-math/Lib/logging/__init__.py
==============================================================================
--- python/branches/trunk-math/Lib/logging/__init__.py	(original)
+++ python/branches/trunk-math/Lib/logging/__init__.py	Sat Feb 16 09:50:47 2008
@@ -26,6 +26,11 @@
 To use, simply 'import logging' and log away!
 """
 
+__all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR',
+           'FATAL', 'FileHandler', 'Filter', 'Filterer', 'Formatter', 'Handler',
+           'INFO', 'LogRecord', 'Logger', 'Manager', 'NOTSET', 'PlaceHolder',
+           'RootLogger', 'StreamHandler', 'WARN', 'WARNING']
+
 import sys, os, types, time, string, cStringIO, traceback
 
 try:

Modified: python/branches/trunk-math/Lib/test/test_descr.py
==============================================================================
--- python/branches/trunk-math/Lib/test/test_descr.py	(original)
+++ python/branches/trunk-math/Lib/test/test_descr.py	Sat Feb 16 09:50:47 2008
@@ -1182,6 +1182,24 @@
         a.foo = 42
         self.assertEqual(a.__dict__, {"foo": 42})
 
+    def test_slots_descriptor(self):
+        # Issue2115: slot descriptors did not correctly check
+        # the type of the given object
+        import abc
+        class MyABC:
+            __metaclass__ = abc.ABCMeta
+            __slots__ = "a"
+
+        class Unrelated(object):
+            pass
+        MyABC.register(Unrelated)
+
+        u = Unrelated()
+        self.assert_(isinstance(u, MyABC))
+
+        # This used to crash
+        self.assertRaises(TypeError, MyABC.a.__set__, u, 3)
+
     def test_dynamics(self):
         # Testing class attribute propagation...
         class D(object):

Modified: python/branches/trunk-math/Lib/test/test_gc.py
==============================================================================
--- python/branches/trunk-math/Lib/test/test_gc.py	(original)
+++ python/branches/trunk-math/Lib/test/test_gc.py	Sat Feb 16 09:50:47 2008
@@ -236,21 +236,33 @@
         gc.disable()
         gc.set_threshold(*thresholds)
 
+    # The following two tests are fragile:
+    # They precisely count the number of allocations,
+    # which is highly implementation-dependent.
+    # For example:
+    # - disposed tuples are not freed, but reused
+    # - the call to assertEqual somehow avoids building its args tuple
     def test_get_count(self):
+        # Avoid future allocation of method object
+        assertEqual = self.assertEqual
         gc.collect()
-        self.assertEqual(gc.get_count(), (0, 0, 0))
+        assertEqual(gc.get_count(), (0, 0, 0))
         a = dict()
-        self.assertEqual(gc.get_count(), (1, 0, 0))
+        # since gc.collect(), we created two objects:
+        # the dict, and the tuple returned by get_count()
+        assertEqual(gc.get_count(), (2, 0, 0))
 
     def test_collect_generations(self):
+        # Avoid future allocation of method object
+        assertEqual = self.assertEqual
         gc.collect()
         a = dict()
         gc.collect(0)
-        self.assertEqual(gc.get_count(), (0, 1, 0))
+        assertEqual(gc.get_count(), (0, 1, 0))
         gc.collect(1)
-        self.assertEqual(gc.get_count(), (0, 0, 1))
+        assertEqual(gc.get_count(), (0, 0, 1))
         gc.collect(2)
-        self.assertEqual(gc.get_count(), (0, 0, 0))
+        assertEqual(gc.get_count(), (0, 0, 0))
 
     def test_trashcan(self):
         class Ouch:

Modified: python/branches/trunk-math/Lib/test/test_mmap.py
==============================================================================
--- python/branches/trunk-math/Lib/test/test_mmap.py	(original)
+++ python/branches/trunk-math/Lib/test/test_mmap.py	Sat Feb 16 09:50:47 2008
@@ -427,6 +427,8 @@
         anon_mmap(PAGESIZE)
 
     def test_prot_readonly(self):
+        if not hasattr(mmap, 'PROT_READ'):
+            return
         mapsize = 10
         open(TESTFN, "wb").write("a"*mapsize)
         f = open(TESTFN, "rb")

Modified: python/branches/trunk-math/Misc/build.sh
==============================================================================
--- python/branches/trunk-math/Misc/build.sh	(original)
+++ python/branches/trunk-math/Misc/build.sh	Sat Feb 16 09:50:47 2008
@@ -90,6 +90,24 @@
     echo "<li><a href=\"$2\">$1</a> <font size=\"-1\">($time seconds)</font></li>" >> $RESULT_FILE
 }
 
+place_summary_first() {
+    testf=$1
+    sed -n '/^[0-9][0-9]* tests OK\./,$p' < $testf \
+	| egrep -v '\[[0-9]+ refs\]' > $testf.tmp
+    echo "" >> $testf.tmp
+    cat $testf >> $testf.tmp
+    mv $testf.tmp $testf
+}
+
+count_failures () {
+    testf=$1
+    n=`grep -ic " failed:" $testf`
+    if [ $n -eq 1 ] ; then
+	n=`grep " failed:" $testf | sed -e 's/ .*//'`
+    fi
+    echo $n
+}
+
 mail_on_failure() {
     if [ "$NUM_FAILURES" != "0" ]; then
         dest=$FAILURE_MAILTO
@@ -185,14 +203,16 @@
             F=make-test.out
             start=`current_time`
             $PYTHON $REGRTEST_ARGS >& build/$F
-            NUM_FAILURES=`grep -ic " failed:" build/$F`
+            NUM_FAILURES=`count_failures build/$F`
+            place_summary_first build/$F
             update_status "Testing basics ($NUM_FAILURES failures)" "$F" $start
             mail_on_failure "basics" build/$F
 
             F=make-test-opt.out
             start=`current_time`
             $PYTHON -O $REGRTEST_ARGS >& build/$F
-            NUM_FAILURES=`grep -ic " failed:" build/$F`
+            NUM_FAILURES=`count_failures build/$F`
+            place_summary_first build/$F
             update_status "Testing opt ($NUM_FAILURES failures)" "$F" $start
             mail_on_failure "opt" build/$F
 
@@ -204,6 +224,7 @@
             $PYTHON $REGRTEST_ARGS -R 4:3:$REFLOG -u network $LEAKY_SKIPS >& build/$F
 	    LEAK_PAT="($LEAKY_TESTS|sum=0)"
             NUM_FAILURES=`egrep -vc "$LEAK_PAT" $REFLOG`
+            place_summary_first build/$F
             update_status "Testing refleaks ($NUM_FAILURES failures)" "$F" $start
             mail_on_failure "refleak" $REFLOG "$LEAK_PAT"
 
@@ -213,7 +234,8 @@
             ## skip curses when running from cron since there's no terminal
             ## skip sound since it's not setup on the PSF box (/dev/dsp)
             $PYTHON $REGRTEST_ARGS -uall -x test_curses test_linuxaudiodev test_ossaudiodev >& build/$F
-            NUM_FAILURES=`grep -ic " failed:" build/$F`
+            NUM_FAILURES=`count_failures build/$F`
+            place_summary_first build/$F
             update_status "Testing all except curses and sound ($NUM_FAILURES failures)" "$F" $start
             mail_on_failure "all" build/$F
         fi

Modified: python/branches/trunk-math/Objects/abstract.c
==============================================================================
--- python/branches/trunk-math/Objects/abstract.c	(original)
+++ python/branches/trunk-math/Objects/abstract.c	Sat Feb 16 09:50:47 2008
@@ -1275,7 +1275,11 @@
 	else if (PyInt_Check(index))
 	  	res = _PyInt_Format((PyIntObject*)index, base, 1);
 	else
-		assert("PyNumber_ToBase: not long or int");
+		/* It should not be possible to get here, as
+		   PyNumber_Index already has a check for the same
+		   condition */
+		PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not "
+				"int or long");
 	Py_DECREF(index);
 	return res;
 }

Modified: python/branches/trunk-math/Objects/complexobject.c
==============================================================================
--- python/branches/trunk-math/Objects/complexobject.c	(original)
+++ python/branches/trunk-math/Objects/complexobject.c	Sat Feb 16 09:50:47 2008
@@ -121,6 +121,33 @@
 }
 
 Py_complex
+c_quot_ieee754(Py_complex a, Py_complex b)
+{
+	/* IEEE 754 style division */
+	if (b.real == 0. && b.imag == 0.) {
+		Py_complex quot;
+		if (a.real == 0. && a.imag == 0.) {
+			/* 0./0. */
+			quot.real = Py_NAN;
+			quot.imag = Py_NAN;
+		}
+		else {
+			float re, im;
+			/* the outcome is inexact but we care only about the
+			 * sign bits
+			 * (a+ib)/(c+id) = (ac+bd)/(c2+d2) + i(bc-ad)/(c2+d2)
+			 */
+			re = a.real * b.real + a.imag * b.imag;
+			im = a.imag * b.real - a.real * b.imag;
+			quot.real = copysign(Py_HUGE_VAL, re);
+			quot.imag = copysign(Py_HUGE_VAL, im);
+		}
+		return quot;
+	}
+	return c_quot(a, b);
+}
+
+Py_complex
 c_pow(Py_complex a, Py_complex b)
 {
 	Py_complex r;
@@ -517,9 +544,13 @@
 complex_div(PyComplexObject *v, PyComplexObject *w)
 {
 	Py_complex quot;
+
 	PyFPE_START_PROTECT("complex_div", return 0)
 	errno = 0;
-	quot = c_quot(v->cval,w->cval);
+	if (PyFloat_GetIEEE754())
+		quot = c_quot_ieee754(v->cval,w->cval);
+	else
+		quot = c_quot(v->cval,w->cval);
 	PyFPE_END_PROTECT(quot)
 	if (errno == EDOM) {
 		PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
@@ -540,7 +571,10 @@
 
 	PyFPE_START_PROTECT("complex_classic_div", return 0)
 	errno = 0;
-	quot = c_quot(v->cval,w->cval);
+	if (PyFloat_GetIEEE754())
+		quot = c_quot_ieee754(v->cval,w->cval);
+	else
+		quot = c_quot(v->cval,w->cval);
 	PyFPE_END_PROTECT(quot)
 	if (errno == EDOM) {
 		PyErr_SetString(PyExc_ZeroDivisionError, "complex division");

Modified: python/branches/trunk-math/Objects/descrobject.c
==============================================================================
--- python/branches/trunk-math/Objects/descrobject.c	(original)
+++ python/branches/trunk-math/Objects/descrobject.c	Sat Feb 16 09:50:47 2008
@@ -166,7 +166,7 @@
 	       int *pres)
 {
 	assert(obj != NULL);
-	if (!PyObject_IsInstance(obj, (PyObject *)(descr->d_type))) {
+	if (!PyObject_TypeCheck(obj, descr->d_type)) {
 		PyErr_Format(PyExc_TypeError,
 			     "descriptor '%.200s' for '%.100s' objects "
 			     "doesn't apply to '%.100s' object",

Modified: python/branches/trunk-math/Objects/floatobject.c
==============================================================================
--- python/branches/trunk-math/Objects/floatobject.c	(original)
+++ python/branches/trunk-math/Objects/floatobject.c	Sat Feb 16 09:50:47 2008
@@ -10,6 +10,19 @@
 #include <ctype.h>
 #include <float.h>
 
+/* global NAN and INF objects */
+#ifdef Py_NAN
+static PyObject *PyFloat_NAN = NULL;
+#define Py_RETURN_NAN return Py_INCREF(PyFloat_NAN), PyFloat_NAN
+#endif
+static PyObject *PyFloat_PINF = NULL;
+static PyObject *PyFloat_NINF = NULL;
+#define Py_RETURN_INF(sign) 					\
+	if (copysign(1., sign) == 1.)				\
+		return Py_INCREF(PyFloat_PINF), PyFloat_PINF;	\
+	else							\
+		return Py_INCREF(PyFloat_NINF), PyFloat_NINF
+
 /* Special free list -- see comments for same code in intobject.c. */
 #define BLOCK_SIZE	1000	/* 1K less typical malloc overhead */
 #define BHEAD_SIZE	8	/* Enough for a 64-bit pointer */
@@ -235,11 +248,11 @@
 			p++;
 		}
 		if (PyOS_strnicmp(p, "inf", 4) == 0) {
-			return PyFloat_FromDouble(sign * Py_HUGE_VAL);
+			Py_RETURN_INF(sign);
 		}
 #ifdef Py_NAN
 		if(PyOS_strnicmp(p, "nan", 4) == 0) {
-			return PyFloat_FromDouble(Py_NAN);
+			Py_RETURN_NAN;
 		}
 #endif
 		PyOS_snprintf(buffer, sizeof(buffer),
@@ -873,6 +886,7 @@
 	double a,b;
 	CONVERT_TO_DOUBLE(v, a);
 	CONVERT_TO_DOUBLE(w, b);
+#ifdef Py_NAN
 	if (b == 0.0) {
 		if (!PyFloat_GetIEEE754()) {
 			PyErr_SetString(PyExc_ZeroDivisionError,
@@ -880,10 +894,11 @@
 			return NULL;
 		}
 		else if (a == 0.)
-			return PyFloat_FromDouble(Py_NAN);
+			Py_RETURN_NAN;
 		else
-			return PyFloat_FromDouble(copysign(Py_HUGE_VAL, a));
+			Py_RETURN_INF(a);
 	}
+#endif
 	PyFPE_START_PROTECT("divide", return 0)
 	a = a / b;
 	PyFPE_END_PROTECT(a)
@@ -899,6 +914,7 @@
 	if (Py_DivisionWarningFlag >= 2 &&
 	    PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
 		return NULL;
+#ifdef Py_NAN
 	if (b == 0.0) {
 		if (!PyFloat_GetIEEE754()) {
 			PyErr_SetString(PyExc_ZeroDivisionError,
@@ -906,10 +922,11 @@
 			return NULL;
 		}
 		else if (a == 0.)
-			return PyFloat_FromDouble(Py_NAN);
+			Py_RETURN_NAN;
 		else
-			return PyFloat_FromDouble(copysign(Py_HUGE_VAL, a));
+			Py_RETURN_INF(a);
 	}
+#endif
 	PyFPE_START_PROTECT("divide", return 0)
 	a = a / b;
 	PyFPE_END_PROTECT(a)
@@ -921,8 +938,9 @@
 {
 	double vx, wx;
 	double mod;
- 	CONVERT_TO_DOUBLE(v, vx);
- 	CONVERT_TO_DOUBLE(w, wx);
+	CONVERT_TO_DOUBLE(v, vx);
+	CONVERT_TO_DOUBLE(w, wx);
+#ifdef Py_NAN
 	if (wx == 0.0) {
 		if (!PyFloat_GetIEEE754()) {
 			PyErr_SetString(PyExc_ZeroDivisionError,
@@ -930,10 +948,11 @@
 			return NULL;
 		}
 		else if (vx == 0.)
-			return PyFloat_FromDouble(Py_NAN);
+			Py_RETURN_NAN;
 		else
-			return PyFloat_FromDouble(copysign(Py_HUGE_VAL, vx));
+			Py_RETURN_INF(vx);
 	}
+#endif
 	PyFPE_START_PROTECT("modulo", return 0)
 	mod = fmod(vx, wx);
 	/* note: checking mod*wx < 0 is incorrect -- underflows to
@@ -1676,6 +1695,20 @@
 	/* Init float info */
 	if (FloatInfoType.tp_name == 0)
 		PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc);
+
+	/* static floats */
+#define static_float(var, value)				\
+	if (var == NULL)					\
+		var = PyFloat_FromDouble(value);		\
+	else {							\
+		assert(PyFloat_CheckExact(var));		\
+		_Py_NewReference(var);				\
+	}
+#ifdef Py_NAN
+	static_float(PyFloat_NAN, Py_NAN);
+#endif
+	static_float(PyFloat_PINF, Py_HUGE_VAL);
+	static_float(PyFloat_NINF, -Py_HUGE_VAL);
 }
 
 void
@@ -1736,6 +1769,12 @@
 	size_t bc, bf;	/* block count, number of freed blocks */
 	size_t fsum;	/* total unfreed floats per block */
 
+#ifdef Py_NAN
+	Py_XDECREF(PyFloat_NAN);
+#endif
+	Py_XDECREF(PyFloat_PINF);
+	Py_XDECREF(PyFloat_NINF);
+
 	PyFloat_CompactFreeList(&bc, &bf, &fsum);
 
 	if (!Py_VerboseFlag)


More information about the Python-checkins mailing list