Revision: 512 http://rpy.svn.sourceforge.net/rpy/?rev=512&view=rev Author: lgautier Date: 2008-05-10 06:30:40 -0700 (Sat, 10 May 2008)
Log Message: ----------- - work on the tests (some of the tests are not passing, but are here what needs to be done is not forgotten) rinterface: - R output to the R console can be customized by a python callback function (in theory - this is not appearing to completely work for the moment) Modified Paths: -------------- branches/rpy_nextgen/rpy/rinterface/rinterface.c branches/rpy_nextgen/rpy/rinterface/tests/__init__.py branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py branches/rpy_nextgen/rpy/robjects/__init__.py branches/rpy_nextgen/rpy/robjects/tests/testRVector.py Modified: branches/rpy_nextgen/rpy/rinterface/rinterface.c =================================================================== --- branches/rpy_nextgen/rpy/rinterface/rinterface.c 2008-05-09 21:17:54 UTC (rev 511) +++ branches/rpy_nextgen/rpy/rinterface/rinterface.c 2008-05-10 13:30:40 UTC (rev 512) @@ -124,14 +124,87 @@ "); -static PySexpObject* globalEnv; -static PySexpObject* baseNameSpaceEnv; +static PySexpObject *globalEnv; +static PySexpObject *baseNameSpaceEnv; +static PySexpObject *na_string; /* early definition of functions */ static PySexpObject* newPySexpObject(const SEXP sexp); static SEXP newSEXP(PyObject *object, const int rType); +/* --- set output from the R console ---*/ + + +static PyObject* writeConsoleCallback = NULL; + +static PyObject* EmbeddedR_setWriteConsole(PyObject *self, + PyObject *args) +{ + + PyObject *result = NULL; + PyObject *function; + + if ( PyArg_ParseTuple(args, "O:console", + &function)) { + + if (!PyCallable_Check(function)) { + PyErr_SetString(PyExc_TypeError, "parameter must be callable"); + return NULL; + } + + Py_XDECREF(writeConsoleCallback); + Py_XINCREF(function); + writeConsoleCallback = function; + Py_INCREF(Py_None); + result = Py_None; + } else { + PyErr_SetString(PyExc_TypeError, "The only parameter should be a callable."); + } + return result; + +} + +PyDoc_STRVAR(EmbeddedR_setWriteConsole_doc, + "setWriteConsoleEmbeddedR()\n\ + \n\ + Set the R console output."); + + + +static void +EmbeddedR_WriteConsole(char *buf, int len) +{ + PyOS_sighandler_t old_int; + PyObject *arglist; + PyObject *result; + + /* It is necessary to restore the Python handler when using a Python + function for I/O. */ + old_int = PyOS_getsig(SIGINT); + PyOS_setsig(SIGINT, python_sigint); + arglist = Py_BuildValue("(s)", buf); + if (! arglist) { + PyErr_NoMemory(); + signal(SIGINT, old_int); + //return NULL; + } + + result = PyEval_CallObject(writeConsoleCallback, arglist); + + Py_DECREF(arglist); + signal(SIGINT, old_int); + + if (result == NULL) { + return; + //return NULL; + } + + Py_DECREF(result); + +} + + /* --- Initialize and terminate an embedded R --- */ /* Should having multiple threads of R become possible, * useful routines could appear here... @@ -154,13 +227,28 @@ options[ii] = PyString_AsString(opt_string); } + int status = Rf_initEmbeddedR(n_args, options); + /* FIXME: setup_Rmainloop causes ipython to crash + *(bug in ipython ?) + */ +/* int status = 1; */ +/* Rf_initialize_R(n_args, options); */ +/* R_Interactive = TRUE; Rf_initialize_R set this based on isatty */ +/* setup_Rmainloop(); */ embeddedR_isInitialized = PyBool_FromLong((long)1); + /* Redirect R console output */ + R_Outputfile = NULL; + + extern void (*ptr_R_WriteConsole)(char *, int); + ptr_R_WriteConsole = EmbeddedR_WriteConsole; + + RPY_SEXP(globalEnv) = R_GlobalEnv; - RPY_SEXP(baseNameSpaceEnv) = R_BaseNamespace; + RPY_SEXP(na_string) = NA_STRING; PyObject *res = PyInt_FromLong(status); @@ -209,39 +297,8 @@ Terminate an embedded R."); -/* --- set output from the R console ---*/ -static void -EmbeddedR_WriteConsole(char *buf, int len) -{ - PyOS_sighandler_t old_int; - /* It is necessary to restore the Python handler when using a Python - function for I/O. */ - old_int = PyOS_getsig(SIGINT); - PyOS_setsig(SIGINT, python_sigint); - PySys_WriteStdout(buf); - signal(SIGINT, old_int); -} - - -/* Redirect R console output */ -// R_Outputfile = NULL; - - -/* FIXME: implement possibility to specify arbitrary callback functions */ -extern void (*ptr_R_WriteConsole)(char *, int); -static PyObject* EmbeddedR_setWriteConsole(PyObject *self) -{ - ptr_R_WriteConsole = EmbeddedR_WriteConsole; - Py_RETURN_NONE; -} -PyDoc_STRVAR(EmbeddedR_setWriteConsole_doc, - "setWriteConsoleEmbeddedR()\n\ - \n\ - Set the R console output to the Python console."); - - static PyObject* EmbeddedR_exception_from_errmessage(void) { @@ -879,11 +936,15 @@ vs = translateChar(STRING_ELT(*sexp, i_R)); res = PyString_FromString(vs); break; +/* case CHARSXP: */ +/* //FIXME: implement handling of single char (if possible ?) */ +/* vs = (CHAR(*sexp)[i_R]); */ +/* res = PyString_FromStringAndSize(vs, 1); */ case VECSXP: res = (PyObject *)newPySexpObject(VECTOR_ELT(*sexp, i_R)); break; default: - PyErr_Format(PyExc_ValueError, "cannot handle type %d", + PyErr_Format(PyExc_ValueError, "Cannot handle type %d", TYPEOF(*sexp)); res = NULL; break; @@ -1649,7 +1710,7 @@ EmbeddedR_init_doc}, {"endEmbeddedR", (PyCFunction)EmbeddedR_end, METH_O, EmbeddedR_end_doc}, - {"setWriteConsole", (PyCFunction)EmbeddedR_setWriteConsole, METH_NOARGS, + {"setWriteConsole", (PyCFunction)EmbeddedR_setWriteConsole, METH_VARARGS, EmbeddedR_setWriteConsole_doc}, {"findVarEmbeddedR", (PyCFunction)EmbeddedR_findVar, METH_VARARGS, EmbeddedR_findVar_doc}, @@ -1926,8 +1987,8 @@ /* /\* Rinternals.h *\/ */ - PySexpObject *na_string = (PySexpObject *)Sexp_new(&VectorSexp_Type, - Py_None, Py_None); + na_string = (PySexpObject *)Sexp_new(&VectorSexp_Type, + Py_None, Py_None); RPY_SEXP(na_string) = NA_STRING; if (PyDict_SetItemString(d, "NA_STRING", (PyObject *)na_string) < 0) Modified: branches/rpy_nextgen/rpy/rinterface/tests/__init__.py =================================================================== --- branches/rpy_nextgen/rpy/rinterface/tests/__init__.py 2008-05-09 21:17:54 UTC (rev 511) +++ branches/rpy_nextgen/rpy/rinterface/tests/__init__.py 2008-05-10 13:30:40 UTC (rev 512) @@ -5,6 +5,7 @@ import test_Sexp import test_SexpClosure import test_SexpVectorNumeric +import test_EmbeddedR def suite(): @@ -13,11 +14,13 @@ suite_Sexp = test_Sexp.suite() suite_SexpClosure = test_SexpClosure.suite() suite_SexpVectorNumeric = test_SexpVectorNumeric.suite() + suite_EmbeddedR = test_EmbeddedR.suite() alltests = unittest.TestSuite([suite_SexpVector, suite_SexpEnvironment, suite_Sexp, suite_SexpClosure, - suite_SexpVectorNumeric]) + suite_SexpVectorNumeric, + suite_EmbeddedR]) return alltests def main(): Modified: branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py =================================================================== --- branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py 2008-05-09 21:17:54 UTC (rev 511) +++ branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVector.py 2008-05-10 13:30:40 UTC (rev 512) @@ -73,6 +73,8 @@ ok = isCharacter(sexp)[0] self.assertTrue(ok) + ri.NA_STRING[0] + def testNewVector(self): sexp_char = ri.SexpVector(["abc", ], ri.STRSXP) Modified: branches/rpy_nextgen/rpy/robjects/__init__.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/__init__.py 2008-05-09 21:17:54 UTC (rev 511) +++ branches/rpy_nextgen/rpy/robjects/__init__.py 2008-05-10 13:30:40 UTC (rev 512) @@ -183,7 +183,6 @@ def getNames(self): res = r.names(self.getSexp()) - import pdb; pdb.set_trace() return res class RArray(Rvector): Modified: branches/rpy_nextgen/rpy/robjects/tests/testRVector.py =================================================================== --- branches/rpy_nextgen/rpy/robjects/tests/testRVector.py 2008-05-09 21:17:54 UTC (rev 511) +++ branches/rpy_nextgen/rpy/robjects/tests/testRVector.py 2008-05-10 13:30:40 UTC (rev 512) @@ -6,6 +6,7 @@ rlist = robjects.baseNameSpaceEnv["list"] class RvectorTestCase(unittest.TestCase): + def testNew(self): identical = ri.baseNameSpaceEnv["identical"] py_a = array.array('i', [1,2,3]) @@ -17,8 +18,7 @@ self.assertTrue(identical(ro_v._sexp, ri_v)[0]) - #FIXME: why isn't this working ? - #del(ri_v) + del(ri_v) self.assertEquals(ri.INTSXP, ro_v.typeof()) def testOperators(self): @@ -72,8 +72,7 @@ v_names = [robjects.baseNameSpaceEnv["letters"][x] for x in (0,1,2)] #FIXME: simplify this r_names = robjects.baseNameSpaceEnv["c"](*v_names) - robjects.r["names<-"](vec, r_names) - + vec = robjects.r["names<-"](vec, r_names) for i in xrange(len(vec)): self.assertEquals(v_names[i], vec.getNames()[i]) 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 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list