# Boost Python bindings

**URL:** <https://numba.discourse.group/t/boost-python-bindings/492>\
**Category:** Community Support\
**Created:** [February 3, 2021, 8:25am UTC](https://numba.discourse.group/t/boost-python-bindings/492 "2021-02-03T08:25:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![duburcqa](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/duburcqa/32/222_2.png) [@duburcqa](https://numba.discourse.group/u/duburcqa)\
**Post date:** [February 3, 2021, 8:25am UTC](https://numba.discourse.group/t/boost-python-bindings/492/1 "2021-02-03T08:25:56Z")

</div>

Hi all!

I’m using Python packages relying on Boost Python bindings, such as [Jiminy](https://github.com/Wandercraft/jiminy), to perform some physics computations. I want to not some simple methods calling Boost Python methods and attribute getters returning `np.ndarray` or native immutable types and perform some basic linear algebra operations on it not involving `scipy` or whatsoever. It is possible to JIT such code ? Currently obviously it is not working as is, since I get an error saying number does not know how to JIT whose Boost Python methods, but I get the intuition it should be possible just to tell to the compiler those are already compiled and calling them directly should be fine. What do you guys think ? I didn’t manage to find any information about numba JIT with boost Python.

Best

---

<div class="post-metadata">

**Author:** ![stuartarchibald](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/stuartarchibald/32/10_2.png) [@stuartarchibald](https://numba.discourse.group/u/stuartarchibald)\
**Post date:** [February 3, 2021, 10:55am UTC](https://numba.discourse.group/t/boost-python-bindings/492/2 "2021-02-03T10:55:59Z")

</div>

Hi @duburcqa,

Numba can call functions in existing compiled libraries through bindings using either `ctypes` or `cffi`, or cython extensions if they are available. Docs:

- [ctypes](https://numba.readthedocs.io/en/stable/reference/pysupported.html#ctypes)
- [cffi](https://numba.readthedocs.io/en/stable/reference/pysupported.html#cffi)
- [using cython extension modules](https://numba.readthedocs.io/en/stable/extending/high-level.html#importing-cython-functions)

The basic idea in all these methods is to get hold of the address of the function in the library, declare the argument and return type(s) and then call it from Numba.

Quick example using `ctypes` binding to `libm`'s `cos` function:

```auto
In [1]: import ctypes as ct

In [2]: libm = ct.CDLL('libm.so')

In [3]: libm_cos = libm.cos

In [4]: libm_cos.argtypes = [ct.c_double,]

In [5]: libm_cos.restype = ct.c_double

In [6]: libm_cos(3.14)
Out[6]: -0.9999987317275395

In [7]: from numba import njit

In [8]: @njit
   ...: def foo(x):
   ...: return libm_cos(x)
   ...: 

In [9]: foo(3.14)
Out[9]: -0.9999987317275395

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![duburcqa](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/duburcqa/32/222_2.png) [@duburcqa](https://numba.discourse.group/u/duburcqa)\
**Post date:** [February 3, 2021, 11:49am UTC](https://numba.discourse.group/t/boost-python-bindings/492/3 "2021-02-03T11:49:23Z")

</div>

Thank you, I will have a look, but I’m afraid it is not what I’m looking for, since boost Python uses Python native types in argument rather than ctypes 😕

---

<div class="post-metadata">

**Author:** ![stuartarchibald](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/stuartarchibald/32/10_2.png) [@stuartarchibald](https://numba.discourse.group/u/stuartarchibald)\
**Post date:** [February 3, 2021, 2:27pm UTC](https://numba.discourse.group/t/boost-python-bindings/492/4 "2021-02-03T14:27:21Z")

</div>

If you cannot (or do not!) want to bind to a compiled library directly and the calls have to be made using the CPython API, then I think the only thing Numba can offer is perhaps the [object mode block](https://numba.readthedocs.io/en/stable/user/withobjmode.html#). There’s a cost of jumping back into the python interpreter from JIT compiled code, but it might be worth it if there’s a lot of work to do in the block. There’s also [numba-passthru · PyPI](https://pypi.org/project/numba-passthru/) if you need to ship objects through to the call site in an `object mode block`.
