boostorg / python

Boost.org python module
http://boostorg.github.io/python
Boost Software License 1.0
465 stars 201 forks source link

Minimal Boost Python example not working #437

Open robertapplin opened 3 months ago

robertapplin commented 3 months ago

I'm having trouble getting boost python to work with this minimal example. I've emailed the mailing list but have had no response.

I'm using C++20

#include <boost/python.hpp>

#include <iostream>
#include <string>

void printStr(const std::string &testStr) {
  std::cout << "BEFORE" <<std::endl;
  std::cout << testStr <<std::endl;
  std::cout << "AFTER" <<std::endl;
}

BOOST_PYTHON_MODULE(my_module) {
  using namespace boost::python;

  def("printStr", printStr);
}

This compiles fine, but I get a segmentation fault when I attempt to call the function from python (using 3.10):

import my_module

message = "This is a test"

my_module.printStr(message)

The output I get:

BEFORE
Segmentation fault

Can you tell me what I'm doing wrong? Thanks in advance

stefanseefeld commented 3 months ago

The code above looks fine. I suspect the problem stems from the way this is compiled. Can you share the details of your build & execution environment ? I.e., platform, compiler, build commands, etc.

robertapplin commented 3 months ago

The code above looks fine. I suspect the problem stems from the way this is compiled. Can you share the details of your build & execution environment ? I.e., platform, compiler, build commands, etc.

Thanks for your quick reply!

I'm on Windows 11, using the compiler shipped with Visual Studio 17 2022, toolset v142. Also I use Ninja as the generator. To compile from the build folder activate my conda environment and I just do

ninja

I get boost 1.84 from Conda-forge. My CMakeLists file that finds boost has:

find_package(Python REQUIRED COMPONENTS Development) 
 if(Python_FOUND) 
   include_directories(${Python_INCLUDE_DIRS}) 
 else() 
   message(FATAL_ERROR "Python not found") 
 endif() 

find_package(Boost REQUIRED COMPONENTS python) 
 if(Boost_FOUND) 
   include_directories(${Boost_INCLUDE_DIRS}) 
 else() 
   message(FATAL_ERROR "Boost not found") 
 endif()

I then link boost to the library that requires it. Perhaps I am missing a component of boost or python?

robertapplin commented 3 months ago

My conda env is created using this:

name: dev-env

channels:
  - conda-forge

dependencies:
  - cmake
  - boost
  - ninja
robertapplin commented 3 months ago

I've tried creating a minimal example here, but it seems to be struggling to build. Any ideas why? https://github.com/robertapplin/boost-example/pull/2

MasterBe commented 3 months ago

Not sure exact solution but can you try using something other than std::string e.g int, this will likely mean that your code does not find/accept converter for const std::string&. Also why not python 3.11..

robertapplin commented 2 months ago

It works for an int. The debugger seems to be able to read the std::string when I hover over the variable after setting a breakpoint. However it segfaults on the std::cout line

I've also tried with python 3.10, and 3.11 - the same problem happens

CEXT-Dan commented 2 months ago

this is what I'm using to print https://github.com/CEXT-Dan/PyRx/blob/fd5b8ad47d3fddb50453277ba63ace1d1f51fb6f/PyRxCore/PyAcEd.cpp#L13-L18 It's not to cout, and I don't remember why I locked the GIL, something for you to try