Commits

Anonymous committed feb1a80

first import

  • Participants

Comments (0)

Files changed (8)

+#include "Python.h"
+#include "lib.h"
+#include "cyfwk_api.h"
+
+#include <iostream>
+
+void print_pyerr()
+{
+  // fetch error
+  PyObject* pytype = 0, *pyvalue = 0, *pytrace = 0;
+  PyErr_Fetch (&pytype, &pyvalue, &pytrace);
+  Py_XINCREF  (pytype);
+  Py_XINCREF  (pyvalue);
+  Py_XINCREF  (pytrace);
+  // restore...
+  PyErr_Restore (pytype, pyvalue, pytrace);
+  // and print
+  PyErr_Print();
+}
+
+int main(int argc, char **argv) 
+{
+  std::cout << "=== app ===\n";
+  Py_Initialize();
+  if (!Py_IsInitialized()) {
+    std::cerr << ":: could not initialize python interpreter !\n";
+    print_pyerr();
+    return 1;
+  } else {
+    std::cout << ":: python initialized\n";
+  }
+  PyEval_InitThreads();
+  if (!PyEval_ThreadsInitialized()) {
+    std::cerr << ":: could not init GIL !\n";
+    print_pyerr();
+    return 1;
+  }
+
+  {
+    if (import_cyfwk()) {
+      std::cerr << ":: error in import_cyfwk!\n";
+      print_pyerr();
+      return 1;
+    } else {
+      std::cout << ":: import_cyfwk ok\n";
+    }
+  }
+  std::cout << "===> testing a pure c++ alg...\n";
+  {
+    IAlg *alg = create_alg(0);
+    alg->run();
+  }
+  std::cout << "===> testing a cy-alg...\n";
+  {
+    IAlg *alg = cy_create_alg();
+    if (alg) {
+      alg->run();
+    }
+    else {
+      std::cerr << "::: nullptr!\n";
+    }
+  }
+
+  std::cout << "=== bye.\n";
+  return 0;
+}
+from cpython.ref cimport PyObject
+
+cdef extern from "lib.h":
+    cdef cppclass IAlg:
+        void run()
+
+    cdef IAlg* create_cy_alg()
+
+    ctypedef void (*RunFct)(void *obj)
+
+    cdef cppclass CyAlgBase:
+        #PyObject *obj
+        #RunFct fct
+        
+        CyAlgBase(PyObject *obj, RunFct fct)
+        void set_cy_state(void* obj, RunFct fct)
+        void run()
+        
+cdef public api IAlg* cy_create_alg()
+
+
+cimport cyfwk
+cimport cpython.ref as cpy_ref
+#cimport cpython.cobject as cpy_cobj
+
+cdef class AlgBase:
+    cdef void run(self):
+        pass
+
+cdef void cy_run_fct(void *ptr):
+    cdef AlgBase alg = <AlgBase>(ptr)
+    print("--> cy_run_fct...")
+    alg.run()
+    print("--> cy_run_fct...[done]")
+
+cdef class myalg(AlgBase):
+    cdef int i
+    def __init__(self, i=-1):
+        self.i = i
+        print("--- self.i: %i" % self.i)
+    cdef void run(self):
+        print("--- myalg.run => %i" % self.i)
+
+cdef public api IAlg* cy_create_alg():
+    cdef AlgBase cyalg = myalg(42)
+    cdef CyAlgBase* alg = new CyAlgBase(
+        <cpy_ref.PyObject*>cyalg,
+        <RunFct>cy_run_fct)
+    return <IAlg*>(alg)
+
+cdef void myalg_run(void* ptr):
+    print("+++ myalg_run")
+    cdef myalg obj = <myalg>(ptr)
+    print("+++ myalg_run")
+    obj.run()
+    print("+++ myalg_run")
+
+def create_alg():
+    print "==="
+    cy_create_alg()
+    print "==="
+
+print("==> cyfwk module imported")
+
+
+
+    
+##
+## stdlib imports
+import re
+
+## waflib imports
+import waflib
+import waflib.Logs as _msg
+from waflib.Task import Task
+from waflib.TaskGen import extension, feature, before, after
+
+re_cyt = re.compile('import\\s(\\w+)\\s*$', re.M)
+def cy_scan(self):
+
+    txt = self.inputs[0].read()
+    mods = []
+    for m in re_cyt.finditer(txt):
+        mods.append(m.group(1))
+
+    _msg.debug("modules: %r" % mods)
+    incs = getattr(self.generator, 'cython_includes', [])
+    incs = [self.generator.path.find_dir(x) for x in incs]
+    incs.append(self.inputs[0].parent)
+
+    found = []
+    missing = []
+    for x in mods:
+        for y in incs:
+            k = y.find_resource(x + '.pxd')
+            if k:
+                found.append(k)
+                break
+        else:
+            missing.append(x)
+    _msg.debug("==> cython defs: %r" % found)
+    return (found, missing)
+
+@extension('.pyx')
+def decide_ext(self, node):
+    if 'cxx' in self.features:
+        self.env.append_unique('CYTHONFLAGS', '--cplus')
+        return ['.cc']
+    return ['.c']
+    
+waflib.TaskGen.declare_chain(
+    name      = 'cython',
+    rule      = '${CYTHON} ${CYTHONFLAGS} -o ${TGT} ${SRC}',
+    color     = 'GREEN',
+    ext_in    = '.pyx',
+    ext_out   = ['.h'],
+    scan      = cy_scan,
+    reentrant = True,
+    decider   = decide_ext,
+)
+    
+def options(ctx):
+    ctx.load('compiler_c')
+    ctx.load('compiler_cxx')
+    ctx.load('python')
+
+    ctx.add_option('--cython-flags',
+                   action='store',
+                   default='',
+                   help='space separated list of flags to pass to cython')
+    
+def configure(ctx):
+    ctx.find_program('cython', var='CYTHON')
+    
+    if ctx.options.cython_flags:
+        ctx.env.CYTHONFLAGS = ctx.options.cython_flags
+
+    ctx.load('compiler_c')
+    ctx.load('compiler_cxx')
+
+    ctx.load('python')
+    ctx.check_python_headers()
+
+@feature('cython')
+def feature_cython(self):
+    return 
+    
+#include "Python.h"
+#include "lib.h"
+
+#include <iostream>
+
+IAlg::~IAlg()
+{}
+
+IAlg::IAlg()
+{}
+
+class Alg : public IAlg
+{
+public:
+  Alg();
+  ~Alg();
+
+  void run();
+};
+
+Alg::Alg()
+{
+  std::cout << ":: Alg::Alg()...\n";
+}
+
+Alg::~Alg()
+{
+  std::cout << ":: Alg::~Alg()...\n";
+}
+
+void
+Alg::run()
+{
+  std::cout << ":: Alg::run()...\n";
+}
+
+IAlg *create_alg(int algid)
+{
+  switch (algid) {
+  case 0:
+    return new Alg;
+  case 1:
+    return 0;
+  default:
+    return 0;
+  }
+
+  return 0;
+}
+
+CyAlgBase::CyAlgBase(PyObject *obj, RunFct fct) :
+  obj(obj),
+  fct(fct)
+{
+  Py_XINCREF(this->obj);
+}
+
+CyAlgBase::~CyAlgBase()
+{
+  Py_XDECREF(this->obj);
+}
+
+void
+CyAlgBase::run()
+{
+  std::cout << "::CyAlgBase::run()...\n";
+  if (this->obj && this->fct) {
+    return this->fct(this->obj);
+  }
+  std::cerr << "** invalid cy-state: obj [" << this->obj << "] fct: ["
+            << this->fct << "]\n";
+  return;
+}
+// -*- c++ -*-
+#ifndef FWK_LIB_H
+#define FWK_LIB_H 1
+
+struct _object;
+typedef _object PyObject;
+
+class IAlg 
+{
+ public:
+  IAlg();
+  virtual ~IAlg();
+
+  virtual void run() =0;
+
+};
+
+IAlg *create_alg(int algid);
+
+typedef void (*RunFct)(void *instance);
+
+struct IAlgWrapper
+{
+  void *obj;
+  RunFct    run;
+};
+
+class CyAlgBase : public IAlg
+{
+public:
+  PyObject *obj;
+  RunFct fct;
+
+  CyAlgBase(PyObject *obj, RunFct fct);
+  virtual ~CyAlgBase();
+  virtual void run();
+};
+
+#endif // !FWK_LIB_H
Binary file added.
+#
+
+top = '.'
+out = '__build__'
+
+def options(ctx):
+    ctx.load('compiler_c')
+    ctx.load('compiler_cxx')
+    #ctx.load('python')
+    ctx.load('cython', tooldir='.')
+
+def configure(ctx):
+
+    ctx.find_program('python2', var='PYTHON')
+    ctx.load('compiler_c')
+    ctx.load('compiler_cxx')
+
+    ctx.find_program('python2', var='PYTHON')
+    ctx.load('python')
+    ctx.check_python_headers()
+
+    ctx.load('cython', tooldir='.')
+    #ctx.env['CYTHONFLAGS'] = '--cplus'
+    ctx.env.append_unique('CFLAGS', ['-fPIC', '-g'])
+    ctx.env.append_unique('CXXFLAGS', ['-fPIC', '-g'])
+    
+def build(ctx):
+
+    ctx(features='cxx cshlib pyembed',
+        source  ='lib/lib.cxx',
+        name    = 'fwk',
+        target  ='fwk')
+
+    ctx(features='cython cxx cshlib pyext',
+        name    ='cyfwk',
+        source  ='cy/cyfwk.pyx',
+        target  = 'cyfwk',
+        use ='fwk',
+        includes='lib',
+        )
+    
+    ctx(features='cxx cxxprogram pyembed',
+        source  ='app/app.cxx',
+        target  ='test-app',
+        includes='lib cy',
+        use ='fwk')
+