Commits

Anonymous committed 24f073d

initial commit

  • Participants

Comments (0)

Files changed (5)

+from distutils.core import setup, Extension
+from subprocess import Popen, PIPE
+import os
+
+cmd = "perl -MConfig -e 'print $Config{archlib}' 2>&1"
+p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE)
+perl_path = p.stdout.read()
+p.wait();
+perl_path = os.path.join(perl_path, 'CORE')
+
+
+setup(name='perlpy',
+    version='0.0',
+    description='',
+    url='',
+    license='MIT License',
+    ext_modules=[
+        Extension(
+            name='_perlpy', 
+            sources = ['src/_interp.c', 'src/_object.c'],
+            include_dirs = [perl_path],
+            #library_dirs = [perl_path],
+            libraries = ['perl', 'm'],
+
+            )
+    ]
+)
+
+#include <EXTERN.h>
+#include <perl.h>
+
+#include "Python.h"
+#include "_interp.h"
+#include "_object.h"
+
+typedef struct {
+    PyObject_HEAD
+    PerlInterpreter *perl;
+} Perl_Object;
+
+static PyTypeObject Perl_Type;
+
+static PyObject *
+newPerl_Object(PyTypeObject *type, char *file)
+{
+    Perl_Object *self;
+	PerlInterpreter *my_perl;
+
+    //char *argv[] = { "", "-e", "0" };
+    //char *argv2[] = { "", file};
+
+    self = (Perl_Object *) type->tp_alloc(type, 0);
+	
+    if (self == NULL)
+		return NULL;
+    my_perl = perl_alloc();
+    perl_construct(my_perl);
+    if (file == NULL){
+        char *argv[] = { "", "-e", "0" };
+        perl_parse(my_perl, NULL, 3, argv, NULL);
+        PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
+        perl_run(my_perl);
+    }else{
+        char *argv[] = { "", file };
+        perl_parse(my_perl, NULL, 2, argv, NULL);
+        PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
+    }
+    //PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
+    //perl_run(my_perl);
+    self->perl = my_perl;
+    return (PyObject *)self;
+
+}
+
+static PyObject *
+perl_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+    char *file;
+	static char *kwlist[] = {"file", NULL};
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:Perl", kwlist,
+					 &file))
+		return NULL;
+
+	return newPerl_Object(type, file);
+}
+
+static void
+perl_dealloc(Perl_Object *self)
+{
+    PerlInterpreter *my_perl = self->perl; 
+    perl_destruct(my_perl);
+    perl_free(my_perl);
+	Py_TYPE(self)->tp_free(self);
+}
+
+static PyObject *
+perl_eval(Perl_Object *self, PyObject *args)
+{
+    char *code;
+    PyObject *res;
+    SV *res_sv;
+    if (!PyArg_ParseTuple(args, "s:perl.eval", &code))
+	    return NULL;
+    dPCTX;
+    //PerlInterpreter *my_perl = self->perl; 
+    //PERL_SET_CONTEXT(my_perl);
+    res_sv = eval_pv(code, TRUE);
+    res = sv2py(my_perl, res_sv);
+    return res; 
+}
+
+
+static PyObject *
+perl_callmethod(Perl_Object *self, PyObject *args)
+{
+    PyObject *res;
+
+    dPCTX;
+
+    dSP;
+    ENTER;
+    SAVETMPS;
+    PUSHMARK(SP);
+    
+    //XPUSHs(sv_2mortal(newSViv(a)));
+    //XPUSHs(sv_2mortal(newSViv(b)));
+    //PUTBACK; 
+    //call_pv("expo", G_SCALAR);
+    //SPAGAIN;
+    
+    //printf ("%d to the %dth power is %d.\n", a, b, POPi);
+    //PUTBACK;
+    //FREETMPS;
+    //LEAVE;
+
+    return res;
+}
+
+PyDoc_STRVAR(perl_eval_doc, "");
+
+static PyMethodDef perl_methods[] = {
+    {"eval", (PyCFunction)perl_eval, METH_VARARGS, perl_eval_doc},
+    {NULL, NULL},
+
+};
+
+static PyTypeObject Perl_Type = {
+	PyVarObject_HEAD_INIT(NULL, 0)
+	"Perl",					/* tp_name */
+	sizeof(Perl_Object),				/* tp_basicsize */
+	0,						/* tp_itemsize */
+	(destructor)perl_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 */
+    perl_methods,  				/* 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 */
+	perl_new,					/* tp_new */
+	0,						/* tp_free */
+};
+
+PyDoc_STRVAR(module_doc,
+"test doc.");
+
+static PyMethodDef perlpy_methods[] = {
+	{0,  	0},	/* sentinel */
+};
+
+PyMODINIT_FUNC
+init_perlpy(void)
+{
+	PyObject *m;
+	m = Py_InitModule3("_perlpy", perlpy_methods, module_doc);
+	if (m == NULL)
+		return;
+    
+	Py_TYPE(&Perl_Type) = &PyType_Type;
+	if (PyType_Ready(&Perl_Type) < 0)
+		return;
+
+	Py_INCREF(&Perl_Type);
+	PyModule_AddObject(m, "Perl", (PyObject *) &Perl_Type);
+	
+}
+
+#define dP     PerlInterpreter *my_perl = self->perl;
+#define dPCTX    dP; PERL_SET_CONTEXT(my_perl);
+
+#include <EXTERN.h>
+#include <perl.h>
+
+#include "Python.h"
+
+typedef struct {
+    PyObject_HEAD
+    SV *sv;
+} SvRV_Object;
+
+PyObject *
+sv2py(PerlInterpreter *my_perl, SV *sv)
+{
+    PyObject *o;
+
+    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 = Py_BuildValue("");
+
+    }else{
+        //other
+	    o = Py_BuildValue("");
+
+    }
+
+    return o;
+}
+
+SV*
+py2sv(PerlInterpreter *my_perl, PyObject *o)
+{
+    return NULL;
+}
+
+PyObject* sv2py(PerlInterpreter *my_perl, SV *sv);
+
+SV* py2sv(PerlInterpreter *my_perl, PyObject *o);
+