Beispiel #1
0
static PyObject*
Sexp_rsame(PyObject *self, PyObject *other)
{
  
  if (! PyObject_IsInstance(other, 
                            (PyObject*)&Sexp_Type)) {
    PyErr_Format(PyExc_ValueError, 
                 "Can only compare Sexp objects.");
    return NULL;
  }
  
  SEXP sexp_self = RPY_SEXP(((PySexpObject*)self));
  if (! sexp_self) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;;
  }
  
  SEXP sexp_other = RPY_SEXP(((PySexpObject*)other));
  if (! sexp_other) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;;
  }
  
  long same = (sexp_self == sexp_other);
  return PyBool_FromLong(same);
}
Beispiel #2
0
static int
Sexp_init(PyObject *self, PyObject *args, PyObject *kwds)
{
#ifdef RPY_VERBOSE
  printf("Python:%p / R:%p - Sexp initializing...\n", 
         self, RPY_SEXP((PySexpObject *)self));
#endif 

  PyObject *sourceObject;

  PyObject *copy = Py_True;
  int sexptype = -1;
  SexpObject *tmpSexpObject;


  static char *kwlist[] = {"sexp", "sexptype", "copy", NULL};
  /* FIXME: handle the copy argument */

  /* the "sexptype" is as a quick hack to make calls from
   the constructor of SexpVector */
  if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iO!", 
                                    kwlist,
                                    &sourceObject,
                                    &sexptype,
                                    &PyBool_Type, &copy)) {
    return -1;
  }

  if (! PyObject_IsInstance(sourceObject, 
                            (PyObject*)&Sexp_Type)) {
    PyErr_Format(PyExc_ValueError, 
                 "Can only instanciate from Sexp objects.");
    return -1;
  }

  if (PyObject_IsTrue(copy)) {
    tmpSexpObject = ((PySexpObject *)self)->sObj;
    if (tmpSexpObject != ((PySexpObject *)sourceObject)->sObj) {
      ((PySexpObject *)self)->sObj = ((PySexpObject *)sourceObject)->sObj;
      PyMem_Free(tmpSexpObject);
    }
    RPY_INCREF((PySexpObject *)self);
#ifdef RPY_VERBOSE
    printf("Python: %p / R: %p - sexp count is now %i.\n", 
           (PySexpObject *)self, RPY_SEXP((PySexpObject *)self), RPY_COUNT((PySexpObject *)self));
#endif 

  } else {
    PyErr_Format(PyExc_ValueError, "Cast without copy is not yet implemented.");
    return -1;
  }

#ifdef RPY_VERBOSE
  printf("done.\n");
#endif 

  /* SET_NAMED(RPY_SEXP((PySexpObject *)self), (unsigned int)2); */
  return 0;
}
Beispiel #3
0
// the convert_to_py function is used to convert a PySexpObject to a SEXP and then perform the conversion.  This function was created by KRC
static PyObject *
convert_to_py(PyObject * self, PyObject * args)
{
    PyObject * obj;             // init a new py obj
    SEXP * robj;                // make a holder for the SEXP obj
    PySexpObject * rpyObj;      // make a holder for the PySexpObject
    
    // if(my_callback){
        // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "converter initialized"));
        // PyObject_CallObject(my_callback, argslist);
    // }
    
    if (!PyArg_ParseTuple(args, "O", &rpyObj)) // parse the args to set the PySexpObject to the incomming PySexpObject
        {
            // if(my_callback){
            // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "conversion to rpyobj failed"));
            // PyObject_CallObject(my_callback, argslist);
            // }
        // return Py_BuildValue("s", "conversion failed to set O ln 306");
        return NULL;
        }
    robj = RPY_SEXP(rpyObj);        // extract the SEXP from the PySexpObject    
    obj = to_Pyobj_with_mode(robj, BASIC_CONVERSION);           // perform the conversion
    return obj;                     // return the new obj
}
Beispiel #4
0
static PyObject*
Sexp___getstate__(PyObject *self)
{

  PyObject *res_string;

  SEXP sexp = RPY_SEXP((PySexpObject *)self);
  if (! sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;
  }

  SEXP sexp_ser;
  PROTECT(sexp_ser = rpy_serialize(sexp, R_GlobalEnv));
  if (TYPEOF(sexp_ser) != RAWSXP) {
    UNPROTECT(1);
    PyErr_Format(PyExc_RuntimeError, 
                 "R's serialize did not return a raw vector.");
    return NULL;
  }
  /* PyByteArray is only available with Python >= 2.6 */
          /* res = PyByteArray_FromStringAndSize(sexp_ser, len); */

  /*FIXME: is this working on 64bit archs ? */
#if (PY_VERSION_HEX < 0x03010000)  
  res_string = PyString_FromStringAndSize((void *)RAW_POINTER(sexp_ser), 
                                          (Py_ssize_t)LENGTH(sexp_ser));
#else
  res_string = PyBytes_FromStringAndSize((void *)RAW_POINTER(sexp_ser), 
					 (Py_ssize_t)LENGTH(sexp_ser));
#endif
  UNPROTECT(1);
  return res_string;
}
Beispiel #5
0
static PyObject*
Sexp_rclass_get(PyObject *self)
{
  SEXP sexp = RPY_SEXP(((PySexpObject*)self));
  if (! sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;;
  }

  SEXP res_R = GET_CLASS(sexp);
  PyObject *res = (PyObject *)newPySexpObject(res_R, 1);
  return res;
}
Beispiel #6
0
PyObject*
Sexp_list_attr(PyObject *self)
{
  SEXP sexp = RPY_SEXP(((PySexpObject*)self));
  if (! sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;
  }
  SEXP res_R;
  PROTECT(res_R = rpy_list_attr(sexp));
  PyObject *res = (PyObject *)newPySexpObject(res_R, 1);
  UNPROTECT(1);
  return res;
}
Beispiel #7
0
static PyObject*
Sexp_typeof_get(PyObject *self)
{
  PySexpObject *pso = (PySexpObject*)self;
  SEXP sexp = RPY_SEXP(pso);
  if (! sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;
  }
#if (PY_VERSION_HEX < 0x03010000)
  return PyInt_FromLong((long)TYPEOF(sexp));
#else
  return PyLong_FromLong((long)TYPEOF(sexp));
#endif
}
Beispiel #8
0
static PyObject*
Sexp_named_get(PyObject *self)
{
  SEXP sexp = RPY_SEXP(((PySexpObject*)self));
  if (! sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;;
  }
  unsigned int res = NAMED(sexp);
#if (PY_VERSION_HEX < 0x03010000)
  return PyInt_FromLong((long)res);
#else
  return PyLong_FromLong((long)res);
#endif
}
Beispiel #9
0
static PyObject*
Sexp_duplicate(PyObject *self, PyObject *kwargs)
{
  SEXP sexp_self, sexp_copy;
  PyObject *res;
  
  sexp_self = RPY_SEXP((PySexpObject*)self);
  if (! sexp_self) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;;
  }
  PROTECT(sexp_copy = Rf_duplicate(sexp_self));
  res = (PyObject *) newPySexpObject(sexp_copy, 1);
  UNPROTECT(1);
  return res;
}
Beispiel #10
0
static PyObject*
Sexp_refcount_get(PyObject *self)
{
  PySexpObject* rpyobj = (PySexpObject*)self;

  if (! RPY_SEXP(rpyobj)) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;
  }
#if (PY_VERSION_HEX < 0x03010000)  
  PyObject *res = PyInt_FromLong((long)(rpyobj->sObj->count));
#else
  PyObject *res = PyLong_FromLong((long)(rpyobj->sObj->count));
#endif
  return res;
}
Beispiel #11
0
static PyObject*
Sexp_repr(PyObject *self)
{
  /* FIXME: make sure this is making any sense */
  SEXP sexp = RPY_SEXP((PySexpObject *)self);
  /* if (! sexp) {
   *  PyErr_Format(PyExc_ValueError, "NULL SEXP.");
   *  return NULL;
   *}
   */
#if (PY_VERSION_HEX < 0x03010000)
  return PyString_FromFormat("<%s - Python:\%p / R:\%p>",
                             self->ob_type->tp_name,
                             self,
                             sexp);  
#else
  return PyUnicode_FromFormat("<%s - Python:\%p / R:\%p>",
			      self->ob_type->tp_name,
			      self,
			      sexp);
#endif
}
Beispiel #12
0
static PyObject*
Sexp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{

  PySexpObject *self = NULL;
  /* unsigned short int rpy_only = 1; */

  #ifdef RPY_VERBOSE
  printf("new '%s' object @...\n", type->tp_name);
  #endif 

  /* self = (PySexpObject *)PyObject_New(PySexpObject, type); */
  self = (PySexpObject *)type->tp_alloc(type, 0);
  #ifdef RPY_VERBOSE
  printf("  Python:%p / R:%p (R_NilValue) ...\n", self, R_NilValue);
  #endif 

  if (! self)
    PyErr_NoMemory();

  self->sObj = (SexpObject *)PyMem_Malloc(1 * sizeof(SexpObject));
  if (! self->sObj) {
    Py_DECREF(self);
    PyErr_NoMemory();
  }

  RPY_COUNT(self) = 1;
  RPY_SEXP(self) = R_NilValue;
  /* RPY_RPYONLY(self) = rpy_only; */

  #ifdef RPY_VERBOSE
  printf("done.\n");
  #endif 

  return (PyObject *)self;

}
Beispiel #13
0
static PyObject*
Sexp___reduce__(PyObject* self)
{
  PyObject *dict, *result;

  if (! (rpy_has_status(RPY_R_INITIALIZED))) {
    PyErr_Format(PyExc_RuntimeError, 
                 "R cannot evaluate code before being initialized.");
    return NULL;
  }
  
  dict = PyObject_GetAttrString((PyObject *)self,
                                "__dict__");
  if (dict == NULL) {
    PyErr_Clear();
    dict = Py_None;
    Py_INCREF(dict);
  }

  if (rpy_has_status(RPY_R_BUSY)) {
    PyErr_Format(PyExc_RuntimeError, "Concurrent access to R is not allowed.");
    return NULL;
  }
  embeddedR_setlock();

  result = Py_BuildValue("O(Oi)O",
                         rinterface_unserialize, /* constructor */
                         Sexp___getstate__(self),
                         TYPEOF(RPY_SEXP((PySexpObject *)self)),
                         dict);

  embeddedR_freelock();

  Py_DECREF(dict);
  return result;

}
Beispiel #14
0
static SEXP rpy_GetEvent(SEXP rho, const char *prompt)
{
  SEXP r_res = R_NilValue;
  PyObject *result;

  pGEDevDesc dd = GEcurrentDevice();
  /* Restore the Python handler */
  /* FIXME */
  /* PyOS_setsig(SIGINT, python_sighandler); */

  PyObject *self = (PyObject *)dd->dev->deviceSpecific;
  /* FIXME optimize ? */
#ifdef RPY_DEBUG_GRDEV
  printf("FIXME: MetricInfo.\n");
#endif
#if (PY_VERSION_HEX < 0x03010000)
  PyObject *py_prompt = PyString_FromString(prompt);
#else
  PyObject *py_prompt = PyUnicode_FromString(prompt);
#endif
  /* FIXME pass gc ? */
  result = PyObject_CallMethodObjArgs(self, GrDev_getevent_name,
                                      py_prompt,
                                      NULL);

  rpy_printandclear_error();
  /* FIXME: check that the method only returns PySexp ? */
  printf("FIXME: check that only PySexp returned.\n");

  r_res = RPY_SEXP((PySexpObject *)result);
  /* FIXME: handle refcount and protection of the resulting r_res */
  printf("FIXME: handle refcount and protection of the resulting r_res");
  Py_DECREF(result);
  Py_DECREF(py_prompt);
  return r_res;
}
Beispiel #15
0
static PyObject*
Sexp_sexp_get(PyObject *self, void *closure)
{
  PySexpObject* rpyobj = (PySexpObject*)self;

  if (! RPY_SEXP(rpyobj)) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;;
  }

  
  RPY_INCREF(rpyobj);  
#if (PY_VERSION_HEX < 0x02070000)  
  /*FIXME: memory leak when INCREF ? */
  PyObject *res = PyCObject_FromVoidPtr(rpyobj->sObj, 
                                        SexpObject_CObject_destroy);
#else
  PyObject *res = PyCapsule_New((void *)(rpyobj->sObj),
				"rpy2.rinterface._C_API_",
				SexpObject_CObject_destroy);
#endif
  
  return res;
}
Beispiel #16
0
static PyObject*
Sexp_do_slot(PyObject *self, PyObject *name)
{
  SEXP sexp = RPY_SEXP(((PySexpObject*)self));
  if (! sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;
  }
#if (PY_VERSION_HEX < 0x03010000)
  if (! PyString_Check(name)) {
#else
    if (! PyUnicode_Check(name)) {
#endif
    PyErr_SetString(PyExc_TypeError, "The name must be a string.");
    return NULL;
  }
#if (PY_VERSION_HEX < 0x03010000)
  char *name_str = PyString_AS_STRING(name);
#else
  PyObject *pybytes = PyUnicode_AsLatin1String(name);
  char *name_str = PyBytes_AsString(pybytes);
#endif
  if (! R_has_slot(sexp, install(name_str))) {
    PyErr_SetString(PyExc_LookupError, "The object has no such attribute.");
#if (PY_VERSION_HEX >= 0x03010000)
    Py_DECREF(pybytes);
#endif
    return NULL;
  }
  SEXP res_R = GET_SLOT(sexp, install(name_str));
#if (PY_VERSION_HEX >= 0x03010000)
    Py_DECREF(pybytes);
#endif
  PyObject *res = (PyObject *)newPySexpObject(res_R, 1);
  return res;
}
PyDoc_STRVAR(Sexp_do_slot_doc,
             "Returns the attribute/slot for an R object.\n"
             " The name of the slot (a string) is the only parameter for\n"
             "the method.\n"
             ":param name: string\n"
             ":rtype: instance of type or subtype :class:`rpy2.rinterface.Sexp`");

static PyObject*
Sexp_do_slot_assign(PyObject *self, PyObject *args)
{

  SEXP sexp = RPY_SEXP(((PySexpObject*)self));
  if (! sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;;
  }

  char *name_str;
  PyObject *value;
  if (! PyArg_ParseTuple(args, "sO", 
                         &name_str,
                         &value)) {
    return NULL;
  }

  if (! PyObject_IsInstance(value, 
                          (PyObject*)&Sexp_Type)) {
      PyErr_Format(PyExc_ValueError, "Value must be an instance of Sexp.");
      return NULL;
  }

  SEXP value_sexp = RPY_SEXP((PySexpObject *)value);
  if (! value_sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return NULL;;
  }

  SET_SLOT(sexp, install(name_str), value_sexp);
  Py_INCREF(Py_None);
  return Py_None;
}
Beispiel #17
0
static int
Sexp_sexp_set(PyObject *self, PyObject *obj, void *closure)
{

#if (PY_VERSION_HEX < 0x02070000)  
  if (! PyCObject_Check(obj)) {
    PyErr_SetString(PyExc_TypeError, "The value must be a CObject.");
    return -1;
  }
#else
  if (! PyCapsule_CheckExact(obj)) {
    PyErr_SetString(PyExc_TypeError, "The value must be a Capsule");
    return -1;
  }
#endif

  SexpObject *sexpobj_orig = ((PySexpObject*)self)->sObj;

#if (PY_VERSION_HEX < 0x02070000)  
  SexpObject *sexpobj = (SexpObject *)(PyCObject_AsVoidPtr(obj));
#else
  SexpObject *sexpobj = (SexpObject *)(PyCapsule_GetPointer(obj,
							    "rpy2.rinterface._C_API_"));
#endif

  if (obj == NULL) {
    PyErr_SetString(PyExc_TypeError, 
		    "The value must be a CObject or a Capsule of name 'rpy2.rinterface._C_API_'.");
    return -1;
  }


  #ifdef RPY_DEBUG_COBJECT
  printf("Setting %p (count: %i) to %p (count: %i)\n", 
         sexpobj_orig, (int)sexpobj_orig->count,
         sexpobj, (int)sexpobj->count);
  #endif

  if ( (sexpobj_orig->sexp != R_NilValue) &
       (TYPEOF(sexpobj_orig->sexp) != TYPEOF(sexpobj->sexp))
      ) {
    PyErr_Format(PyExc_ValueError, 
                 "Mismatch in SEXP type (as returned by typeof)");
    return -1;
  }

  SEXP sexp = sexpobj->sexp;
  if (! sexp) {
    PyErr_Format(PyExc_ValueError, "NULL SEXP.");
    return -1;
  }

  /*FIXME: increment count seems needed, but is this leak free ? */
  sexpobj->count += 2;
  sexpobj_orig->count += 1;

  SexpObject_clear(sexpobj_orig);
  RPY_SEXP(((PySexpObject*)self)) = sexp;

  return 0;
}