[Python-checkins] r46092 - sandbox/trunk/decimal-c/_decimal.c

georg.brandl python-checkins at python.org
Tue May 23 12:01:04 CEST 2006


Author: georg.brandl
Date: Tue May 23 12:01:03 2006
New Revision: 46092

Modified:
   sandbox/trunk/decimal-c/_decimal.c
Log:
Make getcontext() return a borrowed ref.



Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c	(original)
+++ sandbox/trunk/decimal-c/_decimal.c	Tue May 23 12:01:03 2006
@@ -6,8 +6,6 @@
 #include "structmember.h"
 #include "decimal.h"
 
-/* XXX: getcontext() perhaps shouldn't INCREF the ctx */
-
 /* helpful macros ************************************************************/
 
 #define MODULE_NAME   "_decimal"
@@ -397,7 +395,8 @@
 no_way:
     /* Now rounding starts. */
     rnd_func = round_funcs[rounding];
-
+    if (prec != ctx->prec) {
+        ctx = 
 
 }
 
@@ -1476,6 +1475,7 @@
 
 #ifdef WITH_THREAD
 
+/* Get the context for the current thread. This returns a borrowed reference. */
 static contextobject *
 getcontext(void)
 {
@@ -1503,11 +1503,11 @@
             Py_DECREF(ctx);
             return NULL;
         }
-        /* clear the KeyError */
-        PyErr_Clear();
+        Py_DECREF(ctx); /* the dict now has a reference, see comment below */
         return ctx;
     }
-    Py_INCREF(ctx);
+    /* ctx is NOT incref'd since that simplifies argument parsing
+     * in functions that either are passed a context or use the default */
     return ctx;        
 }
 
@@ -1532,6 +1532,7 @@
 
 static PyObject *current_context;
 
+/* Get the global current context object. Returns a borrowed reference. */
 static contextobject *
 getcontext()
 {
@@ -1545,6 +1546,7 @@
         if (!current_context)
             return NULL;
     }
+    /* no Py_INCREF here */
     return (contextobject *)current_context;
 }
 
@@ -2029,7 +2031,10 @@
 static PyObject *
 module_getcontext(PyObject *self)
 {
-    return (PyObject *)getcontext();
+    PyObject *ctx = (PyObject *)getcontext();
+    /* getcontext() returned a borrowed reference */
+    Py_XINCREF(ctx);
+    return ctx;
 }
 
 static PyObject *


More information about the Python-checkins mailing list