Revision: 545 http://rpy.svn.sourceforge.net/rpy/?rev=545&view=rev Author: lgautier Date: 2008-06-04 11:31:44 -0700 (Wed, 04 Jun 2008)
Log Message: ----------- - Split C code into files - Fix in propagated exceptions - Fix in reference counting for R-borne objects Modified Paths: -------------- branches/rpy_nextgen/rpy/rinterface/rinterface.c branches/rpy_nextgen/rpy/rinterface/rinterface.h branches/rpy_nextgen/rpy/rinterface/tests/test_Sexp.py branches/rpy_nextgen/rpy/rinterface/tests/test_SexpEnvironment.py branches/rpy_nextgen/setup.py Added Paths: ----------- branches/rpy_nextgen/rpy/rinterface/r_utils.c branches/rpy_nextgen/rpy/rinterface/r_utils.h Added: branches/rpy_nextgen/rpy/rinterface/r_utils.c =================================================================== --- branches/rpy_nextgen/rpy/rinterface/r_utils.c (rev 0) +++ branches/rpy_nextgen/rpy/rinterface/r_utils.c 2008-06-04 18:31:44 UTC (rev 545) @@ -0,0 +1,30 @@ +#include <Rdefines.h> + + +SEXP rpy_findFun(SEXP symbol, SEXP rho) +{ + SEXP vl; + while (rho != R_EmptyEnv) { + /* This is not really right. Any variable can mask a function */ + vl = findVarInFrame3(rho, symbol, TRUE); + + if (vl != R_UnboundValue) { + if (TYPEOF(vl) == PROMSXP) { + PROTECT(vl); + vl = eval(vl, rho); + UNPROTECT(1); + } + if (TYPEOF(vl) == CLOSXP || TYPEOF(vl) == BUILTINSXP || + TYPEOF(vl) == SPECIALSXP) + return (vl); + + if (vl == R_MissingArg) { + printf("R_MissingArg in rpy_FindFun.\n"); + return R_UnboundValue; + } + } + rho = ENCLOS(rho); + } + return R_UnboundValue; +} + Property changes on: branches/rpy_nextgen/rpy/rinterface/r_utils.c ___________________________________________________________________ Name: svn:eol-style + native Added: branches/rpy_nextgen/rpy/rinterface/r_utils.h =================================================================== --- branches/rpy_nextgen/rpy/rinterface/r_utils.h (rev 0) +++ branches/rpy_nextgen/rpy/rinterface/r_utils.h 2008-06-04 18:31:44 UTC (rev 545) @@ -0,0 +1,9 @@ +#ifndef RPY_RU_H +#define RPY_RU_H + +#include <Rdefines.h> + +SEXP rpy_findFun(SEXP symbol, SEXP rho); + + +#endif Property changes on: branches/rpy_nextgen/rpy/rinterface/r_utils.h ___________________________________________________________________ Name: svn:eol-style + native Modified: branches/rpy_nextgen/rpy/rinterface/rinterface.c =================================================================== --- branches/rpy_nextgen/rpy/rinterface/rinterface.c 2008-06-01 15:07:21 UTC (rev 544) +++ branches/rpy_nextgen/rpy/rinterface/rinterface.c 2008-06-04 18:31:44 UTC (rev 545) @@ -69,7 +69,9 @@ #include "rinterface.h" #include "array.h" +#include "r_utils.h" +#define RPY_VERBOSE /* Back-compatibility with Python 2.4 */ #if (PY_VERSION_HEX < 0x02050000) @@ -331,9 +333,11 @@ RPY_DECREF(self); #ifdef RPY_VERBOSE - printf("%p: sexp count is %i\n", self, RPY_COUNT(self)); + printf("Python:%p / R:%p -- sexp count is %i\n", + self, RPY_SEXP(self), RPY_COUNT(self)); #endif + printf("dealloc-count--->\n"); if ((RPY_COUNT(self) == 0) && RPY_SEXP(self)) { #ifdef RPY_VERBOSE printf("freeing SEXP resources...\n"); @@ -348,7 +352,9 @@ printf("done.\n"); #endif } + printf("dealloc-del--->\n"); PyObject_Del(self); + printf("dealloc-ok--->\n"); } @@ -500,6 +506,8 @@ { PySexpObject *self; + //unsigned short int rpy_only = 1; + #ifdef RPY_VERBOSE printf("new object @...\n"); #endif @@ -507,7 +515,7 @@ //self = (PySexpObject *)_PyObject_New(&type); self = (PySexpObject *)type->tp_alloc(type, 0); #ifdef RPY_VERBOSE - printf(" %p...\n", self); + printf(" Python:%p / R:%p ...\n", self, R_NilValue); #endif if (! self) @@ -519,6 +527,7 @@ RPY_COUNT(self) = 1; RPY_SEXP(self) = R_NilValue; + //RPY_RPYONLY(self) = rpy_only; #ifdef RPY_VERBOSE printf("done.\n"); @@ -530,10 +539,11 @@ static int -Sexp_init(PySexpObject *self, PyObject *args, PyObject *kwds) +Sexp_init(PyObject *self, PyObject *args, PyObject *kwds) { #ifdef RPY_VERBOSE - printf("%p: Sexp initializing...\n", self); + printf("Python:%p / R:%p - Sexp initializing...\n", + self, RPY_SEXP((PySexpObject *)self)); #endif PyObject *sourceObject; @@ -559,12 +569,15 @@ } if (PyObject_IsTrue(copy)) { - SEXP oldSexp; - oldSexp = RPY_SEXP((PySexpObject *)sourceObject); - RPY_SEXP(self) = oldSexp; + //SEXP oldSexp; + //oldSexp = RPY_SEXP((PySexpObject *)sourceObject); + //RPY_SEXP(self) = oldSexp; + free(((PySexpObject *)self)->sObj); + self->sObj = ((PySexpObject *)sourceObject)->sObj; RPY_INCREF(self); #ifdef RPY_VERBOSE - printf("%p: sexp count is increased to %i.\n", self, RPY_COUNT(self)); + printf("%p: sexp count is increased to %i.\n", + (PySexpObject *)self, RPY_COUNT((PySexpObject *)self)); #endif } else { @@ -1142,6 +1155,7 @@ PyObject *copy; static char *kwlist[] = {"sexpvector", "sexptype", "copy", NULL}; + //FIXME: handle the copy argument if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iO!", kwlist, @@ -1160,8 +1174,8 @@ (PyObject*)&VectorSexp_Type)) { //call parent's constructor - if (Sexp_init(self, args, kwds) == -1) { - PyErr_Format(PyExc_RuntimeError, "Error initializing instance."); + if (Sexp_init(self, args, NULL) == -1) { + //PyErr_Format(PyExc_RuntimeError, "Error initializing instance."); return -1; } } else { @@ -1181,34 +1195,7 @@ return 0; } -static SEXP rpy_findFun(SEXP symbol, SEXP rho) -{ - SEXP vl; - while (rho != R_EmptyEnv) { - /* This is not really right. Any variable can mask a function */ - vl = findVarInFrame3(rho, symbol, TRUE); - if (vl != R_UnboundValue) { - if (TYPEOF(vl) == PROMSXP) { - PROTECT(vl); - vl = eval(vl, rho); - UNPROTECT(1); - } - if (TYPEOF(vl) == CLOSXP || TYPEOF(vl) == BUILTINSXP || - TYPEOF(vl) == SPECIALSXP) - return (vl); - - if (vl == R_MissingArg) - printf("R_MissingArg in rpy_FindFun.\n"); - return R_UnboundValue; - } - rho = ENCLOS(rho); - } - return R_UnboundValue; -} - - - /* --- */ static PyObject* EnvironmentSexp_findVar(PyObject *self, PyObject *args, PyObject *kwds) @@ -1246,6 +1233,7 @@ } if (res_R != R_UnboundValue) { + //FIXME rpy_only res = newPySexpObject(res_R); } else { PyErr_Format(PyExc_LookupError, "'%s' not found", name); Modified: branches/rpy_nextgen/rpy/rinterface/rinterface.h =================================================================== --- branches/rpy_nextgen/rpy/rinterface/rinterface.h 2008-06-01 15:07:21 UTC (rev 544) +++ branches/rpy_nextgen/rpy/rinterface/rinterface.h 2008-06-04 18:31:44 UTC (rev 545) @@ -13,6 +13,7 @@ typedef struct { int count; + //unsigned short int rpy_only; SEXP sexp; } SexpObject; @@ -27,6 +28,7 @@ #define RPY_COUNT(obj) (((obj)->sObj)->count) #define RPY_SEXP(obj) (((obj)->sObj)->sexp) //#define RPY_SEXP(obj) ((obj)->sexp) +//#define RPY_RPYONLY(obj) (((obj)->sObj)->rpy_only) #define RPY_INCREF(obj) ((obj->sObj)->count++) #define RPY_DECREF(obj) ((obj->sObj)->count--) Modified: branches/rpy_nextgen/rpy/rinterface/tests/test_Sexp.py =================================================================== --- branches/rpy_nextgen/rpy/rinterface/tests/test_Sexp.py 2008-06-01 15:07:21 UTC (rev 544) +++ branches/rpy_nextgen/rpy/rinterface/tests/test_Sexp.py 2008-06-04 18:31:44 UTC (rev 545) @@ -15,11 +15,12 @@ #def tearDown(self): # rinterface.endEmbeddedR(1); - def testNew(self): + def testNew_invalid(self): x = "a" self.assertRaises(ValueError, rinterface.Sexp, x) - + + def testNew(self): sexp = rinterface.globalEnv.get("letters") sexp_new = rinterface.Sexp(sexp) Modified: branches/rpy_nextgen/rpy/rinterface/tests/test_SexpEnvironment.py =================================================================== --- branches/rpy_nextgen/rpy/rinterface/tests/test_SexpEnvironment.py 2008-06-01 15:07:21 UTC (rev 544) +++ branches/rpy_nextgen/rpy/rinterface/tests/test_SexpEnvironment.py 2008-06-04 18:31:44 UTC (rev 545) @@ -52,14 +52,23 @@ ok = isinstance(sfit_R, rinterface.SexpClosure) self.assertTrue(ok) + + def testGet_functionOnly_lookupError(self): # now with the function-only option - plot = rinterface.globalEnv.get("plot", wantFun = False) - self.assertEquals(rinterface.CLOSXP, plot.typeof()) - rinterface.globalEnv["plot"] = rinterface.SexpVector(["foo", ], - rinterface.CHARSXP) - plot = rinterface.globalEnv.get("plot", wantFun = True) - self.assertEquals(rinterface.CLOSXP, plot.typeof()) + self.assertRaises(LookupError, + rinterface.globalEnv.get, "pi", wantFun = True) + + def testGet_functionOnly(self): + hist = rinterface.globalEnv.get("hist", wantFun = False) + self.assertEquals(rinterface.CLOSXP, hist.typeof()) + rinterface.globalEnv["hist"] = rinterface.SexpVector(["foo", ], + rinterface.STRSXP) + + hist = rinterface.globalEnv.get("hist", wantFun = True) + self.assertEquals(rinterface.CLOSXP, hist.typeof()) + + def testSubscript(self): ge = rinterface.globalEnv obj = rinterface.globalEnv.get("letters") Modified: branches/rpy_nextgen/setup.py =================================================================== --- branches/rpy_nextgen/setup.py 2008-06-01 15:07:21 UTC (rev 544) +++ branches/rpy_nextgen/setup.py 2008-06-04 18:31:44 UTC (rev 545) @@ -99,6 +99,7 @@ rinterface_ext = Extension( pack_name + '.rinterface.rinterface', [os.path.join('rpy', 'rinterface', 'array.c'), + os.path.join('rpy', 'rinterface', 'r_utils.c'), os.path.join('rpy', 'rinterface', 'rinterface.c')], include_dirs = [ os.path.join(RHOME, 'include'), os.path.join('rpy', 'rinterface')], This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list