Revision: 432 http://rpy.svn.sourceforge.net/rpy/?rev=432&view=rev Author: lgautier Date: 2008-03-15 01:35:53 -0700 (Sat, 15 Mar 2008)
Log Message: ----------- - Added unit tests for Sexp objects - Added method do_slot for Sexp_Type (crashes when incorrect slot name. R_has_slot announced for R-1.7.0-dev is likely needed to prevent this from happening) - moved the definition of newSexpObject and newSEXP to the top Modified Paths: -------------- trunk/sandbox/rpy_nextgen/rpy/rinterface/rinterface.c Added Paths: ----------- trunk/sandbox/rpy_nextgen/rpy/rinterface/tests/test_Sexp.py Modified: trunk/sandbox/rpy_nextgen/rpy/rinterface/rinterface.c =================================================================== --- trunk/sandbox/rpy_nextgen/rpy/rinterface/rinterface.c 2008-03-14 22:10:33 UTC (rev 431) +++ trunk/sandbox/rpy_nextgen/rpy/rinterface/rinterface.c 2008-03-15 08:35:53 UTC (rev 432) @@ -124,6 +124,11 @@ static SexpObject* globalEnv; static SexpObject* baseNameSpaceEnv; +/* early definition of functions */ +static SexpObject* newSexpObject(const SEXP sexp); +static SEXP newSEXP(PyObject *object, const int rType); + + /* --- Initialize and terminate an embedded R --- */ /* Should having multiple threads of R become possible, * Useful routines deal with can could appear here... @@ -286,9 +291,40 @@ "\n\ Returns the R internal SEXPREC type."); +static PyObject* +Sexp_do_slot(PyObject *self, PyObject *name) +{ + SEXP sexp =((SexpObject*)self)->sexp; + if (! sexp) { + PyErr_Format(PyExc_ValueError, "NULL SEXP."); + return NULL;; + } + if (! PyString_Check(name)) { + PyErr_SetString(PyExc_TypeError, "The name must be a string."); + return NULL; + } + char *name_str = PyString_AS_STRING(name); + SEXP res_R = GET_SLOT(sexp, install(name_str)); + if (!res_R) { + PyErr_Format(PyExc_ValueError, "R Error."); + return NULL;; + } + + SexpObject *res = newSexpObject(res_R); + return res; +} +PyDoc_STRVAR(Sexp_do_slot_doc, +"\n\ +Returns the attribute/slot for an R object.\n\ +The name of the slot as a string is the only parameter for\ + the method.\n"); + + static PyMethodDef Sexp_methods[] = { {"typeof", (PyCFunction)Sexp_typeof, METH_NOARGS, Sexp_typeof_doc}, + {"do_slot", (PyCFunction)Sexp_do_slot, METH_O, + Sexp_do_slot_doc}, {NULL, NULL} /* sentinel */ }; @@ -348,8 +384,6 @@ * Closure-type Sexp. */ -static SexpObject* newSexpObject(const SEXP sexp); -static SEXP newSEXP(PyObject *object, const int rType); /* Evaluate a SEXP. It must be constructed by hand. It raises a Python exception if an error ocurred in the evaluation */ Added: trunk/sandbox/rpy_nextgen/rpy/rinterface/tests/test_Sexp.py =================================================================== --- trunk/sandbox/rpy_nextgen/rpy/rinterface/tests/test_Sexp.py (rev 0) +++ trunk/sandbox/rpy_nextgen/rpy/rinterface/tests/test_Sexp.py 2008-03-15 08:35:53 UTC (rev 432) @@ -0,0 +1,39 @@ +import unittest +import rpy.rinterface as rinterface + +#FIXME: can starting and stopping an embedded R be done several times ? +rinterface.initEmbeddedR("foo", "--vanilla", "--no-save", "--quiet") + +class SexpTestCase(unittest.TestCase): + #def setUpt(self): + # rinterface.initEmbeddedR("foo", "--no-save") + + #def tearDown(self): + # rinterface.endEmbeddedR(1); + + def testTypeof(self): + sexp = rinterface.globalEnv.get("letters") + self.assertEquals(sexp.typeof(), rinterface.STRSXP) + + sexp = rinterface.globalEnv.get("pi") + self.assertEquals(sexp.typeof(), rinterface.REALSXP) + + sexp = rinterface.globalEnv.get("plot") + self.assertEquals(sexp.typeof(), rinterface.CLOSXP) + + def testDo_slot(self): + data_func = rinterface.globalEnv.get("data") + data_func(rinterface.SexpVector(["iris", ], rinterface.STRSXP)) + sexp = rinterface.globalEnv.get("iris") + names = sexp.do_slot("names") + iris_names = ("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species") + + self.assertEquals(len(iris_names), len(names)) + + for i, n in enumerate(iris_names): + self.assertEquals(iris_names[i], names[i]) + + missing = sexp.do_slot("foo") + +if __name__ == '__main__': + unittest.main() 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: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list