Commits

Maciej Fijalkowski committed fa3119d

(arigo, fijal) add a hack to pass a python function pointer to C

Comments (0)

Files changed (3)

bench/numeric/arr.py

     from numpypy import zeros, fromstring
     from _numpypy.pypy import remove_invalidates
 except:
-    from numpy import zeros
+    from numpy import zeros, fromstring
     remove_invalidates = lambda arg: None
 
 def hook(jitdriver_name, loop_type, greenkey, operations):
+
+import traceback
+import ctypes, sys
+
+def f(a):
+    print 3 + a
+    return 3
+
+cfunc = ctypes.cast(ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)(f), ctypes.c_void_p).value
+
+#include <Python.h>
+#include <stdio.h>
+
+int main()
+{
+  PyObject *code, *py_globals, *py_res, *py_func;
+  FILE *f;
+  char p[1024];
+  int lgt;
+  long addr;
+
+  Py_Initialize();
+  f = fopen("a.py", "r");
+  lgt = fread(p, 1, 1024, f);
+  p[lgt] = 0;
+  fclose(f);
+  code = Py_CompileStringFlags(p, "exec", Py_file_input, NULL);
+  py_globals = PyDict_New();
+  PyDict_SetItemString(py_globals, "__builtins__", PyEval_GetBuiltins());
+  py_res = PyEval_EvalCode(code, py_globals, py_globals);
+  py_func = PyDict_GetItemString(py_globals, "cfunc");
+  addr = PyInt_AsLong(py_func);
+  ((int(*)(int))addr)(3);
+  Py_Finalize();
+}