Extending C with Python
![](https://secure.gravatar.com/avatar/4cf609d50d5cb8b522b4b0ae5eb481ef.jpg?s=120&d=mm&r=g)
Hello, I'm extending C with python (which is opposite way of what people usually do, extending python with C), I'm currently stuck in passing a C array to python layer, could anyone plz advise? I have a C buffer in my C code and want to pass it to a python function. In the C code, I have: npy_intp dims [2];
In the Python code, I want to just print that array: def f(self, dims): print ("np array:%d,%d"%(dims[0],dims[1])) But it only prints the first number correctly, i.e., dims[0]. The second number is always 0. Best, Jialin LBNL/NERSC
![](https://secure.gravatar.com/avatar/5dde29b54a3f1b76b2541d0a4a9b232c.jpg?s=120&d=mm&r=g)
I'm guessing you could use Cython to make this easier. It's usually used for calling C from Python, but can do the sandwich in both directions... Just a thought -- it will help with some of that boilerplate code... -CHB On Tue, Jan 30, 2018 at 10:57 PM, Jialin Liu <jalnliu@lbl.gov> wrote:
-- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
![](https://secure.gravatar.com/avatar/8d3840e88e3d0be366655068a0ac4720.jpg?s=120&d=mm&r=g)
On 31.01.2018 17:58, Chris Barker wrote:
I'm guessing you could use Cython to make this easier.
... or Boost.Python (http://boostorg.github.io/python), which has built-in support for NumPy (http://boostorg.github.io/python/doc/html/numpy/index.html), and supports both directions: extending Python with C++, as well as embedding Python into C++ applications. Stefan -- ...ich hab' noch einen Koffer in Berlin...
![](https://secure.gravatar.com/avatar/971399cd3d255dfffb7f1924f4595c05.jpg?s=120&d=mm&r=g)
You can also check out pybind11, xtensor, and xtensor-python The latter enables a high-level numpy-like API on the C++ side. You can check out the numpy to xtensor cheat sheet: http://xtensor.readthedocs.io/en/latest/numpy.html Best, Sylvain On Thu, Feb 1, 2018 at 12:11 AM, Stefan Seefeld <stefan@seefeld.name> wrote:
![](https://secure.gravatar.com/avatar/5dde29b54a3f1b76b2541d0a4a9b232c.jpg?s=120&d=mm&r=g)
I'm guessing you could use Cython to make this easier. It's usually used for calling C from Python, but can do the sandwich in both directions... Just a thought -- it will help with some of that boilerplate code... -CHB On Tue, Jan 30, 2018 at 10:57 PM, Jialin Liu <jalnliu@lbl.gov> wrote:
-- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
![](https://secure.gravatar.com/avatar/8d3840e88e3d0be366655068a0ac4720.jpg?s=120&d=mm&r=g)
On 31.01.2018 17:58, Chris Barker wrote:
I'm guessing you could use Cython to make this easier.
... or Boost.Python (http://boostorg.github.io/python), which has built-in support for NumPy (http://boostorg.github.io/python/doc/html/numpy/index.html), and supports both directions: extending Python with C++, as well as embedding Python into C++ applications. Stefan -- ...ich hab' noch einen Koffer in Berlin...
![](https://secure.gravatar.com/avatar/971399cd3d255dfffb7f1924f4595c05.jpg?s=120&d=mm&r=g)
You can also check out pybind11, xtensor, and xtensor-python The latter enables a high-level numpy-like API on the C++ side. You can check out the numpy to xtensor cheat sheet: http://xtensor.readthedocs.io/en/latest/numpy.html Best, Sylvain On Thu, Feb 1, 2018 at 12:11 AM, Stefan Seefeld <stefan@seefeld.name> wrote:
participants (5)
-
Chris Barker
-
Jialin Liu
-
Robert Kern
-
Stefan Seefeld
-
Sylvain Corlay