Revision: 495 http://rpy.svn.sourceforge.net/rpy/?rev=495&view=rev Author: lgautier Date: 2008-04-18 13:56:30 -0700 (Fri, 18 Apr 2008)
Log Message: ----------- - Improved handling of multiple versions of R - Automagic setting of R_HOME at install time - SexpObject defined in a header - Added back A. Belopolsky's functions and structures for integration with numpy (not yet used) Modified Paths: -------------- branches/rpy_nextgen/rpy/rinterface/__init__.py branches/rpy_nextgen/setup.py Added Paths: ----------- branches/rpy_nextgen/rpy/rinterface/__init__.py.in branches/rpy_nextgen/rpy/rinterface/array.c branches/rpy_nextgen/rpy/rinterface/rinterface.h Modified: branches/rpy_nextgen/rpy/rinterface/__init__.py =================================================================== --- branches/rpy_nextgen/rpy/rinterface/__init__.py 2008-04-17 20:44:44 UTC (rev 494) +++ branches/rpy_nextgen/rpy/rinterface/__init__.py 2008-04-18 20:56:30 UTC (rev 495) @@ -1,2 +1,6 @@ -from rpy2.rinterface.rinterface import * +# This file is automatically generated when installing rpy2 +# For permanent changes, edit __init__py.in in the source directory. +import os + +from rinterface import * Added: branches/rpy_nextgen/rpy/rinterface/__init__.py.in =================================================================== --- branches/rpy_nextgen/rpy/rinterface/__init__.py.in (rev 0) +++ branches/rpy_nextgen/rpy/rinterface/__init__.py.in 2008-04-18 20:56:30 UTC (rev 495) @@ -0,0 +1,4 @@ +import os +os.environ["R_HOME"]="%R_HOME%" + +from rinterface import * Added: branches/rpy_nextgen/rpy/rinterface/array.c =================================================================== --- branches/rpy_nextgen/rpy/rinterface/array.c (rev 0) +++ branches/rpy_nextgen/rpy/rinterface/array.c 2008-04-18 20:56:30 UTC (rev 495) @@ -0,0 +1,125 @@ +/* A. Belopolsky's Array interface */ + + +#include <Python.h> +#include <Rdefines.h> +#include <Rinternals.h> +#include "rinterface.h" + +#define ARRAY_INTERFACE_VERSION 2 + +/* Array Interface flags */ +#define FORTRAN 0x002 +#define ALIGNED 0x100 +#define NOTSWAPPED 0x200 +#define WRITEABLE 0x400 + +typedef struct { + int version; + int nd; + char typekind; + int itemsize; + int flags; + Py_intptr_t *shape; + Py_intptr_t *strides; + void *data; +} PyArrayInterface; + +static int +sexp_typekind(SEXP sexp) +{ + switch (TYPEOF(sexp)) { + case REALSXP: return 'f'; + case INTSXP: return 'i'; + case STRSXP: return 'S'; + case CPLXSXP: return 'c'; + //FIXME: correct type ? + case LGLSXP: return 'b'; + } + return 0; +} + + +static int +sexp_itemsize(SEXP sexp) +{ + switch (TYPEOF(sexp)) { + case REALSXP: return sizeof(*REAL(sexp)); + case INTSXP: return sizeof(*INTEGER(sexp)); + case STRSXP: return sizeof(*CHAR(sexp)); + case CPLXSXP: return sizeof(*COMPLEX(sexp)); + case LGLSXP: return sizeof(*LOGICAL(sexp)); + } + return 0; +} + +static int +sexp_rank(SEXP sexp) +{ + SEXP dim = getAttrib(sexp, R_DimSymbol); + if (dim == R_NilValue) + return 1; + return LENGTH(dim); +} + +static void +sexp_shape(SEXP sexp, Py_intptr_t* shape, int nd) +{ + int i; + SEXP dim = getAttrib(sexp, R_DimSymbol); + if (dim == R_NilValue) + shape[0] = LENGTH(sexp); + else for (i = 0; i < nd; ++i) { + shape[i] = INTEGER(dim)[i]; + } +} + +static void +array_struct_free(void *ptr, void *arr) +{ + PyArrayInterface *inter = (PyArrayInterface *)ptr; + free(inter->shape); + free(inter); + Py_DECREF((PyObject *)arr); +} + + +static PyObject* +array_struct_get(SexpObject *self) +{ + SEXP sexp = self->sexp; + if (!sexp) { + PyErr_SetString(PyExc_AttributeError, "Null sexp"); + return NULL; + } + PyArrayInterface *inter; + int typekind = sexp_typekind(sexp); + if (!typekind) { + PyErr_SetString(PyExc_AttributeError, "Unsupported SEXP type"); + return NULL; + } + inter = (PyArrayInterface *)malloc(sizeof(PyArrayInterface)); + if (!inter) { + return PyErr_NoMemory(); + } + int nd = sexp_rank(sexp); + int i; + inter->version = ARRAY_INTERFACE_VERSION; + inter->nd = nd; + inter->typekind = typekind; + inter->itemsize = sexp_itemsize(sexp); + inter->flags = FORTRAN|ALIGNED|NOTSWAPPED|WRITEABLE; + inter->shape = (Py_intptr_t*)malloc(sizeof(Py_intptr_t)*nd*2); + sexp_shape(sexp, inter->shape, nd); + inter->strides = inter->shape + nd; + Py_intptr_t stride = inter->itemsize; + inter->strides[0] = stride; + for (i = 1; i < nd; ++i) { + stride *= inter->shape[i-1]; + inter->strides[i] = stride; + } + inter->data = RAW(sexp); + Py_INCREF(self); + return PyCObject_FromVoidPtrAndDesc(inter, self, array_struct_free); +} + Property changes on: branches/rpy_nextgen/rpy/rinterface/array.c ___________________________________________________________________ Name: svn:eol-style + native Added: branches/rpy_nextgen/rpy/rinterface/rinterface.h =================================================================== --- branches/rpy_nextgen/rpy/rinterface/rinterface.h (rev 0) +++ branches/rpy_nextgen/rpy/rinterface/rinterface.h 2008-04-18 20:56:30 UTC (rev 495) @@ -0,0 +1,16 @@ + +#ifndef RPY_RI_H +#define RPY_RI_H + +#include <R.h> +#include <Python.h> + +/* Representation of R objects (instances) as instances in Python. + */ +typedef struct { + PyObject_HEAD + SEXP sexp; +} SexpObject; + + +#endif /* !RPY_RI_H */ Property changes on: branches/rpy_nextgen/rpy/rinterface/rinterface.h ___________________________________________________________________ Name: svn:eol-style + native Modified: branches/rpy_nextgen/setup.py =================================================================== --- branches/rpy_nextgen/setup.py 2008-04-17 20:44:44 UTC (rev 494) +++ branches/rpy_nextgen/setup.py 2008-04-18 20:56:30 UTC (rev 495) @@ -60,15 +60,30 @@ r_libs = [os.path.join(RHOME, 'lib'), os.path.join(RHOME, 'modules')] pack_name = 'rpy2' - pack_version = '0.0.1' + pack_version = '0.0.1-dev' if r_packversion is not None: pack_name = pack_name + '_' + r_packversion pack_version = pack_version + '_' + r_packversion + f_in = open(os.path.join('rpy', 'rinterface', '__init__.py.in'), 'r') + f_out = open(os.path.join('rpy', 'rinterface', '__init__.py'), 'w') + f_out.write('# This file is automatically generated when installing rpy2\n') + f_out.write('# For permanent changes, ' +\ + 'edit __init__py.in in the source directory.\n\n') + + for row in f_in: + row = row.replace('%R_HOME%', RHOME) + f_out.write(row) + + f_in.close() + f_out.close() + rinterface = Extension( pack_name + ".rinterface.rinterface", - ["rpy/rinterface/rinterface.c", ], - include_dirs=[ os.path.join(RHOME, 'include'),], + [os.path.join('rpy', 'rinterface', 'rinterface.c'), + os.path.join('rpy', 'rinterface', 'array.c')], + include_dirs=[ os.path.join(RHOME, 'include'), + os.path.join('rpy', 'rinterface')], libraries=['R', 'Rlapack', 'Rblas'], library_dirs=r_libs, runtime_library_dirs=r_libs, 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