Revision: 645
          http://rpy.svn.sourceforge.net/rpy/?rev=645&view=rev
Author:   lgautier
Date:     2008-08-28 07:14:29 +0000 (Thu, 28 Aug 2008)

Log Message:
-----------
- fixed crash when calling enclos() on SexpEnvironment with R not 
initialized (bug report #2078176)
- fixed possible crash when calling frame() on SexpEnvironment with R 
not initialized
- fixed CObject destructor 

Modified Paths:
--------------
    branches/rpy_nextgen/rpy/rinterface/rinterface.c

Modified: branches/rpy_nextgen/rpy/rinterface/rinterface.c
===================================================================
--- branches/rpy_nextgen/rpy/rinterface/rinterface.c    2008-08-25 09:52:23 UTC 
(rev 644)
+++ branches/rpy_nextgen/rpy/rinterface/rinterface.c    2008-08-28 07:14:29 UTC 
(rev 645)
@@ -448,33 +448,40 @@
 
 staticforward PyTypeObject Sexp_Type;
 
-static void
-Sexp_clear(PySexpObject *self)
+void SexpObject_clear(SexpObject *sexpobj)
 {
-  RPY_DECREF(self);
+
+  (*sexpobj).count--;
+
 #ifdef RPY_VERBOSE
   printf("Python:%p / R:%p -- sexp count is %i...", 
-        self, RPY_SEXP(self), RPY_COUNT(self));
+        self, sexpobj->sexp, sexpobj->count);
 #endif
-
-  if ((RPY_COUNT(self) == 0) && RPY_SEXP(self)) {
+  if (((*sexpobj).count == 0) && (*sexpobj).sexp) {
 #ifdef RPY_VERBOSE
     printf("freeing SEXP resources...");
 #endif 
 
-    if (RPY_SEXP(self) != R_NilValue) {
+    if (sexpobj->sexp != R_NilValue) {
 #ifdef RPY_DEBUG_PRESERVE
-      printf("  Sexp_clear: R_ReleaseObject( %p )\n", RPY_SEXP(self));
+      printf("  Sexp_clear: R_ReleaseObject( %p )\n", sexpobj->sexp);
 #endif 
-    R_ReleaseObject(RPY_SEXP(self));
+    R_ReleaseObject(sexpobj->sexp);
     }
-    PyMem_Free(self->sObj);
+    PyMem_Free(sexpobj);
     ////self->ob_type->tp_free((PyObject*)self);
 #ifdef RPY_VERBOSE
     printf("done.\n");
 #endif 
-  }
+  }  
+}
 
+static void
+Sexp_clear(PySexpObject *self)
+{
+  
+  SexpObject_clear(self->sObj);
+
 }
 
 
@@ -609,17 +616,24 @@
 This method corresponds to the macro NAMED.\n\
 See the R-extensions manual for further details.");
 
+void SexpObject_CObject_destroy(void *cobj)
+{
+  SexpObject* sexpobj_ptr = (SexpObject *)cobj;
+  SexpObject_clear(sexpobj_ptr);
+}
+
 static PyObject*
 Sexp_sexp_get(PyObject *self, void *closure)
 {
-  SEXP sexp = RPY_SEXP(((PySexpObject*)self));
+  PySexpObject* rpyobj = (PySexpObject*)self;
 
-  if (! sexp) {
+  if (! RPY_SEXP(rpyobj)) {
     PyErr_Format(PyExc_ValueError, "NULL SEXP.");
     return NULL;;
   }
-
-  PyObject *res = PyCObject_FromVoidPtr(sexp, NULL);
+  
+  RPY_INCREF(rpyobj);
+  PyObject *res = PyCObject_FromVoidPtr(&rpyobj, SexpObject_CObject_destroy);
   return res;
 }
 
@@ -873,6 +887,7 @@
     //env_R = R_GlobalContext;
   }
 
+  /* From Alexander's original code: */
   /* Enable our handler for SIGINT inside the R
      interpreter. Otherwise, we cannot stop R calculations, since
      SIGINT is only processed between Python bytecodes. Also, save the
@@ -1733,6 +1748,11 @@
 static PyObject*
 EnvironmentSexp_frame(PyObject *self)
 {
+  if (! (embeddedR_status & RPY_R_INITIALIZED)) {
+    PyErr_Format(PyExc_RuntimeError, 
+                "R must be initialized before environments can be accessed.");
+    return NULL;
+  }
   SEXP res_R = NULL;
   PySexpObject *res;
   res_R = FRAME(RPY_SEXP((PySexpObject *)self));
@@ -1745,6 +1765,11 @@
 static PyObject*
 EnvironmentSexp_enclos(PyObject *self)
 {
+  if (! (embeddedR_status & RPY_R_INITIALIZED)) {
+    PyErr_Format(PyExc_RuntimeError, 
+                "R must be initialized before environments can be accessed.");
+    return NULL;
+  }
   SEXP res_R = NULL;
   PySexpObject *res;
   res_R = ENCLOS(RPY_SEXP((PySexpObject *)self));


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to