Minimal Boost Python example not working

Open
#437 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
32/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp, python
Domain
api, backend

Research direction

Run the shown C++20 Boost.Python module with Python 3.10 and the my_module.printStr call, then inspect the boundary around def("printStr", printStr) and the std::string conversion. Capture the build and runtime configuration needed to reproduce the fault, and confirm completion when the call prints BEFORE, the message, and AFTER without a segmentation fault.

Written by the indexing model from the issue text.

Description

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

Dominant language
C++
Stars
537
Forks
223
Avg merge
11h 22m
Merged PRs (30d)
2

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/python

All issues in boostorg/python

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.