Commits

Anonymous committed e3656f7

add objects file

  • Participants
  • Parent commits 5872eaf

Comments (0)

Files changed (4)

File src/convert.c

+#include <EXTERN.h>
+#include <perl.h>
+
+#include "Python.h"
+#include "_interp.h"
+#include "convert.h"
+#include "object.h"
+
+PyObject *
+sv2py(Perl_Object *self, SV *sv)
+{
+    PyObject *o;
+    dP;
+
+    if(SvPOK(sv)){
+        //string
+    	STRLEN len;
+	    char *s = SvPV(sv, len);
+	    o = Py_BuildValue("s#", s, len);
+    
+    }else if(SvNOK(sv)){
+        //double
+    	o = Py_BuildValue("d", SvNV(sv));
+    
+    }else if(SvIOK(sv)){
+        //long
+    	o = Py_BuildValue("l", SvIV(sv));
+
+    }else if(SvROK(sv)){
+        // ref
+	    o = newSvRV_Object(my_perl, sv);
+
+    }else{
+        //other
+	    o = Py_BuildValue("");
+
+    }
+
+    return o;
+}
+
+SV*
+py2sv(Perl_Object *self, PyObject *o)
+{
+    dP;
+
+    if (o == Py_None) {
+	    return newSV(0);
+    }else if (PyString_Check(o)) {
+	    return newSVpvn(PyString_AS_STRING(o), PyString_GET_SIZE(o));
+    }else if (PyInt_Check(o)) {
+	    return newSViv(PyInt_AsLong(o));
+    }else if (PyLong_Check(o)) {
+	    unsigned long tmp = PyLong_AsUnsignedLong(o);
+	    return newSVuv(tmp);
+    }else if (PyFloat_Check(o)) {
+	    return newSVnv(PyFloat_AsDouble(o));
+    }else if (PySvRV_Check(o)){
+	    return PySvRV_AsSV(o);
+    }else {
+	    return newSV(0);
+    }
+    return NULL;
+
+}

File src/convert.h

+PyObject* sv2py(Perl_Object *self, SV *sv);
+
+SV* py2sv(Perl_Object *self, PyObject *o);
+
+
+#define PySvRV_AsSV(v)     (((SvRV_Object*)(v))->sv)
+

File src/object.c

+#include <EXTERN.h>
+#include <perl.h>
+
+#include "Python.h"
+#include "object.h"
+
+PyObject * 
+newSvRV_Object(PerlInterpreter *my_perl, SV *sv)
+{
+    PERL_SET_CONTEXT(my_perl);
+    SvRV_Object *self;
+    self = PyObject_NEW(SvRV_Object, &SvRV_Type);
+    if (self == NULL)
+	    return NULL;
+    self->sv = SvREFCNT_inc(sv);
+    self->perl = my_perl;
+    return (PyObject *)self;
+}
+
+static void
+svrv_dealloc(SvRV_Object *self)
+{
+    PerlInterpreter *my_perl;
+    my_perl = self->perl;
+    PERL_SET_CONTEXT(my_perl);
+    SvREFCNT_dec(self->sv);
+	Py_TYPE(self)->tp_free(self);
+}
+
+
+PyTypeObject SvRV_Type = {
+	PyVarObject_HEAD_INIT(NULL, 0)
+	"PerlRef",					/* tp_name */
+	sizeof(SvRV_Object),				/* tp_basicsize */
+	0,						/* tp_itemsize */
+	(destructor)svrv_dealloc,			/* tp_dealloc */
+	0,						/* tp_print */
+	0,						/* tp_getattr */
+	0,						/* tp_setattr */
+	0,						/* tp_compare */
+	0,						/* tp_repr */
+	0,						/* tp_as_number */
+	0,						/* tp_as_sequence */
+	0,						/* tp_as_mapping */
+	0,						/* tp_hash */
+	0,              				/* tp_call */
+	0,						/* tp_str */
+	PyObject_GenericGetAttr,			/* tp_getattro */
+	0,						/* tp_setattro */
+	0,						/* tp_as_buffer */
+	Py_TPFLAGS_DEFAULT,				/* tp_flags */
+	0,					/* tp_doc */
+	0,						/* tp_traverse */
+	0,						/* tp_clear */
+	0,						/* tp_richcompare */
+	0,						/* tp_weaklistoffset */
+	0,						/* tp_iter */
+	0,						/* tp_iternext */
+    /*svrv_method*/0,  				/* tp_methods */
+	0,						/* tp_members */
+	0,				/* tp_getset */
+	0,						/* tp_base */
+	0,						/* tp_dict */
+	0,						/* tp_descr_get */
+	0,						/* tp_descr_set */
+	0,						/* tp_dictoffset */
+	0,						/* tp_init */
+	0,						/* tp_alloc */
+	0,					/* tp_new */
+	0,						/* tp_free */
+};
+

File src/object.h

+
+typedef struct {
+    PyObject_HEAD
+    SV *sv;
+    PerlInterpreter *perl;
+} SvRV_Object;
+
+
+extern PyTypeObject SvRV_Type;
+
+#define PySvRV_Check(v)  ((v)->ob_type == &SvRV_Type)
+//#define SVRV_RV(v)     (((PySVRV*)(v))->rv)
+
+PyObject * newSvRV_Object(PerlInterpreter *my_perl, SV *sv);
+
+