Differences between revisions 2 and 3
Revision 2 as of 2018-03-07 22:41:05
Size: 1768
Editor: WimLavrijsen
Comment:
Revision 3 as of 2019-01-14 17:21:09
Size: 2346
Editor: WimLavrijsen
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
Line 11: Line 12:
...  MyClass(int i) : m_data(i) {}
... int m_data;
... MyClass(int i) : m_data(i) {}
... virtual ~MyClass() {}
... virtual int add_int(int i) { return m_data + i; }
...
int m_data;
Line 16: Line 19:
>>> v += [MyClass(i) for i in range(3)] >>> v += [MyClass(i) for i in range(2)]
Line 18: Line 21:
3 2
Line 24: Line 27:
2
Line 30: Line 32:
...  ...
Line 33: Line 35:
# cross inheritence (CPython only for now)
>>> class PyMyClass(MyClass):
... def add_int(self, i):
... return self.m_data + 2*i
...
# helper on C++ side to show inheritence
>>> cppyy.cppdef("int callback(MyClass* m, int i) { return m->add_int(i); }")
>>> cppyy.gbl.callback(m, 2) # calls C++ add_int
Line 34: Line 44:
>>> >>> cppyy.gbl.callback(PyMyClass(1), 2) # calls Python-side override
5
Line 36: Line 47:
Source and wheels available on PyPy. To install, run:
Line 37: Line 49:
Full details in the cppyy documentation: [[http://cppyy.readthedocs.io]] {{{
$ python -m pip install cppyy
}}}
Full details are in the cppyy documentation: http://cppyy.readthedocs.io

The cppyy package integrates the Clang/LLVM-based Cling C++ interpreter into Python, providing interactive access to C/C++ from Python. Using precompiled modules, a class loader, and an everything-lazy implementation, cppyy is designed for automatic generation of Python bindings for large scale C++ programs. PyPy supports cppyy natively for high performance, as described in this PyHPC'16 paper.

Thanks to LLVM's JIT, cppyy supports embedded C++ code, automatic template instantiations, auto-downcasting, etc., etc. Where possible, C++ idioms are automatically recognized and pythonized. If necessary, a pythonization API provides further fine tuning for memory ownership, threading, and application-specific conversions.

An example session follows:

   1 >>> import cppyy
   2 >>> cppyy.cppdef("""
   3 ... class MyClass {
   4 ... public:
   5 ...    MyClass(int i) : m_data(i) {}
   6 ...    virtual ~MyClass() {}
   7 ...    virtual int add_int(int i) { return m_data + i; }
   8 ...    int m_data;
   9 ... };""")                               # defines a new C++ class
  10 >>> from cppyy.gbl import MyClass        # bound on-the-fly
  11 >>> v = cppyy.gbl.std.vector[MyClass]()  # template generated
  12 >>> v += [MyClass(i) for i in range(2)]
  13 >>> len(v)
  14 2
  15 >>> for m in v:                          # idiomatically mapped
  16 ...    print(m.m_data)
  17 ...
  18 0
  19 1
  20 # create a C++ function on the fly and attach on the Python side
  21 >>> cppyy.cppdef("auto add_int = [](MyClass* m, int a) { return m->m_data + a; };")
  22 >>> MyClass.add_int = lambda self, i: cppyy.gbl.add_int(self, i)
  23 >>> for m in v:
  24 ...    print(m.add_int(1))
  25 ...
  26 1
  27 2
  28 # cross inheritence (CPython only for now)
  29 >>> class PyMyClass(MyClass):
  30 ...    def add_int(self, i):
  31 ...       return self.m_data + 2*i
  32 ...
  33 # helper on C++ side to show inheritence
  34 >>> cppyy.cppdef("int callback(MyClass* m, int i) { return m->add_int(i); }")
  35 >>> cppyy.gbl.callback(m, 2)             # calls C++ add_int
  36 3
  37 >>> cppyy.gbl.callback(PyMyClass(1), 2)  # calls Python-side override
  38 5

Source and wheels available on PyPy. To install, run:

$ python -m pip install cppyy

Full details are in the cppyy documentation: http://cppyy.readthedocs.io

cppyy (last edited 2019-11-08 18:17:43 by WimLavrijsen)

Unable to edit the page? See the FrontPage for instructions.