2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-20 04:42:28 +00:00

Compare commits

...

3 Commits
tmp ... develop

Author SHA1 Message Date
Stefan Seefeld
32da86df26 Improve test coverage. 2025-12-02 08:53:09 -05:00
Neil Schemenauer
e89f86b74f Update Linux CI scripts, more Python versions.
Update scripts to use actions/setup-python to install different Python
versions.  Add run-faber.sh and get-py-env.py scripts.  Add
test-ubuntu-py-ver.yml CI script to test with different Python versions.
2025-12-02 08:19:45 -05:00
Neil Schemenauer
5013564316 Add "nogil" option for BOOST_PYTHON_MODULE_INIT.
Implement optional arguments for BOOST_PYTHON_MODULE_INIT and allow the
boost::python::mod_gil_not_used() option.  This sets the
Py_MOD_GIL_NOT_USED flag for the extension module.

To define a module that supports free-threaded Python, define it
like this:

    BOOST_PYTHON_MODULE(my_module, boost::python::mod_gil_not_used())
    {
        ...
    }
2025-12-02 08:19:45 -05:00
8 changed files with 293 additions and 22 deletions

65
.github/get-py-env.py vendored Executable file
View File

@@ -0,0 +1,65 @@
#!/usr/bin/env python3
#
# Determine info about the Python install and write shell code to stdout, to
# set env variables. This will set the variables PY_LDFLAGS, PY_CFLAGS and
# PY_INC_PATH.
#
# The python3-config tool is used as the source of this info. In theory we
# could use sysconfig as well but the setup-python action from github appears
# to patch python3-config but not patch the sysconfig info.
#
# Usage:
# eval $(python3 get-py-env.py)
import os
import re
import subprocess
def get_output(cmd):
rv = subprocess.run(
cmd,
capture_output=True, # Capture stdout and stderr
text=True, # Decode output as text (UTF-8)
check=True, # Raise an error if the command fails
)
return rv.stdout
def extract_flags(cmd, prefix):
flags = []
for part in get_output(cmd).split():
part = part.strip()
if part.startswith(prefix):
flags.append(part)
return ' '.join(flags)
def find_python_h():
"""Find the include path that has Python.h contained inside.
We could use INCLUDEPY from sysconfig but github patches
python3-config but not the sysconfig info (after moving the
install).
"""
c_flags = extract_flags(['python3-config', '--cflags'], '-I')
for part in c_flags.split():
m = re.search(r'-I(\S+)', part)
if not m:
continue
inc_path = m.group(1)
if os.path.exists(os.path.join(inc_path, 'Python.h')):
return inc_path
raise SystemExit('cannot find Python.h')
def main():
ld_flags = extract_flags(['python3-config', '--ldflags'], '-L')
c_flags = extract_flags(['python3-config', '--cflags'], '-I')
include_path = find_python_h()
print(f'PY_LDFLAGS="{ld_flags}"')
print(f'PY_CFLAGS="{c_flags}"')
print(f'PY_INC_PATH="{include_path}"')
if __name__ == '__main__':
main()

46
.github/run-faber.sh vendored Executable file
View File

@@ -0,0 +1,46 @@
#!/bin/sh
set -eu
echo "cxx version: $CXX $($CXX --version)"
echo "cxx std: $CXX_STD"
echo "python3 path: $(which python3)"
echo "python3 version: $(python3 --version)"
if ! which faber > /dev/null; then
echo "Installing faber..."
python3 -m pip install --upgrade pip
python3 -m pip install -U faber
fi
echo "faber version: $(faber -v)"
# find and set PY_LDFLAGS and PY_INC_PATH
eval $(python3 .github/get-py-env.py)
echo "PY_INC_PATH=$PY_INC_PATH"
echo "PY_LDFLAGS=$PY_LDFLAGS"
case $(python3-config --abiflags) in
*t*)
# When running with free-threaded, we always want to disable the GIL
# even for extensions without the mod_gil_not_used() flag
export PYTHON_GIL=0
;;
esac
# this could be set by LD_LIBRARY_PATH but faber overrides it
prefix=$(python3-config --prefix)
echo "${prefix}/lib" > /etc/ld.so.conf.d/boost-ci.conf && ldconfig
sed -e "s/\$PYTHON/python3/g" .ci/faber > $HOME/.faber
faber \
--with-boost-include=${BOOST_PY_DEPS} \
--builddir=build \
cxx.name="${CXX}" \
cxxflags="-std=${CXX_STD}" \
cppflags="-std=${CXX_STD}" \
include="${PY_INC_PATH}" \
ldflags="${PY_LDFLAGS}" \
-j`nproc` \
"$@"

View File

@@ -1,6 +1,10 @@
# Test on Ubuntu with various compiler and language standard versions.
name: Test Ubuntu
on: [push, pull_request]
on:
push:
pull_request:
workflow_dispatch:
jobs:
build:
@@ -9,35 +13,59 @@ jobs:
strategy:
fail-fast: false
matrix:
python: [python, python3]
python-version: ['3.14']
cxx: [g++, clang++]
std: [c++11, c++14, c++17]
include:
# Add the appropriate docker image for each compiler.
# The images from teeks99/boost-python-test already have boost::python
# pre-reqs installed, see:
# https://github.com/teeks99/boost-python-test-docker
- cxx: clang++
docker-img: teeks99/boost-python-test:clang-21_1.89.0
- cxx: g++
docker-img: teeks99/boost-python-test:gcc-15_1.89.0
- python-version: '2.7'
cxx: g++
std: c++11
- python-version: '3.10'
cxx: g++
std: c++17
- python-version: '3.11'
cxx: g++
std: c++17
- python-version: '3.12'
cxx: g++
std: c++17
- python-version: '3.13'
cxx: g++
std: c++17
# Also test with free-threaded build of Python
- python-version: '3.14t'
cxx: clang++
std: c++17
container:
image: ${{ matrix.docker-img }}
# Add the appropriate docker image for the compiler.
# The images from teeks99/boost-python-test already have boost::python
# pre-reqs installed, see:
# https://github.com/teeks99/boost-python-test-docker
image: ${{ matrix.cxx == 'g++' &&
'teeks99/boost-python-test:gcc-15_1.89.0' ||
'teeks99/boost-python-test:clang-21_1.89.0' }}
steps:
- uses: actions/checkout@v5
- name: setup python
if: "${{ matrix.python-version != '2.7' }}"
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: setup prerequisites
run: |
# Warning: this is not necessarily the same Python version as the one configured above !
python3 -m pip install -U faber --break-system-packages
- name: build
echo "CXX=${{ matrix.cxx }}" >> "$GITHUB_ENV"
echo "CXX_STD=${{ matrix.std }}" >> "$GITHUB_ENV"
- name: build-py2
if: "${{ matrix.python-version == '2.7' }}"
run: |
${{ matrix.python }} --version
python --version
${{ matrix.cxx }} --version
faber -v
sed -e "s/\$PYTHON/${{ matrix.python }}/g" .ci/faber > ~/.faber
sed -e "s/\$PYTHON/python/g" .ci/faber > ~/.faber
faber \
--with-boost-include=${BOOST_PY_DEPS} \
--builddir=build \
@@ -45,7 +73,12 @@ jobs:
cxxflags=-std=${{ matrix.std }} \
cppflags=-std=${{ matrix.std }} \
-j`nproc`
- name: test
- name: build-py3
if: "${{ matrix.python-version != '2.7' }}"
run: |
.github/run-faber.sh
- name: test-py2
if: "${{ matrix.python-version == '2.7' }}"
run: |
faber \
--with-boost-include=${BOOST_PY_DEPS} \
@@ -55,3 +88,7 @@ jobs:
cppflags=-std=${{ matrix.std }} \
-j`nproc` \
test.report
- name: test-py3
if: "${{ matrix.python-version != '2.7' }}"
run: |
.github/run-faber.sh test.report

View File

@@ -11,11 +11,41 @@
# ifndef BOOST_PYTHON_MODULE_INIT
namespace boost { namespace python { namespace detail {
namespace boost { namespace python {
#ifdef HAS_CXX11
// Use to activate the Py_MOD_GIL_NOT_USED flag.
class mod_gil_not_used {
public:
explicit mod_gil_not_used(bool flag = true) : flag_(flag) {}
bool flag() const { return flag_; }
private:
bool flag_;
};
namespace detail {
inline bool gil_not_used_option() { return false; }
template <typename F, typename... O>
bool gil_not_used_option(F &&, O &&...o);
template <typename... O>
inline bool gil_not_used_option(mod_gil_not_used f, O &&...o) {
return f.flag() || gil_not_used_option(o...);
}
template <typename F, typename... O>
inline bool gil_not_used_option(F &&, O &&...o) {
return gil_not_used_option(o...);
}
}
#endif // HAS_CXX11
namespace detail {
# if PY_VERSION_HEX >= 0x03000000
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef&, void(*)());
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef&, void(*)(), bool gil_not_used = false);
#else
@@ -27,7 +57,37 @@ BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
# if PY_VERSION_HEX >= 0x03000000
# define _BOOST_PYTHON_MODULE_INIT(name) \
# ifdef HAS_CXX11
# define _BOOST_PYTHON_MODULE_INIT(name, ...) \
PyObject* BOOST_PP_CAT(PyInit_, name)() \
{ \
static PyModuleDef_Base initial_m_base = { \
PyObject_HEAD_INIT(NULL) \
0, /* m_init */ \
0, /* m_index */ \
0 /* m_copy */ }; \
static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; \
\
static struct PyModuleDef moduledef = { \
initial_m_base, \
BOOST_PP_STRINGIZE(name), \
0, /* m_doc */ \
-1, /* m_size */ \
initial_methods, \
0, /* m_reload */ \
0, /* m_traverse */ \
0, /* m_clear */ \
0, /* m_free */ \
}; \
\
return boost::python::detail::init_module( \
moduledef, BOOST_PP_CAT(init_module_, name), \
boost::python::detail::gil_not_used_option(__VA_ARGS__) ); \
} \
void BOOST_PP_CAT(init_module_, name)()
# else // !HAS_CXX11
# define _BOOST_PYTHON_MODULE_INIT(name) \
PyObject* BOOST_PP_CAT(PyInit_, name)() \
{ \
static PyModuleDef_Base initial_m_base = { \
@@ -53,6 +113,7 @@ BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
moduledef, BOOST_PP_CAT(init_module_, name) ); \
} \
void BOOST_PP_CAT(init_module_, name)()
# endif // HAS_CXX11
# else
@@ -66,9 +127,15 @@ BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)());
# endif
# define BOOST_PYTHON_MODULE_INIT(name) \
# if defined(HAS_CXX11) && (PY_VERSION_HEX >= 0x03000000)
# define BOOST_PYTHON_MODULE_INIT(name, ...) \
void BOOST_PP_CAT(init_module_,name)(); \
extern "C" BOOST_SYMBOL_EXPORT _BOOST_PYTHON_MODULE_INIT(name, __VA_ARGS__)
# else
# define BOOST_PYTHON_MODULE_INIT(name) \
void BOOST_PP_CAT(init_module_,name)(); \
extern "C" BOOST_SYMBOL_EXPORT _BOOST_PYTHON_MODULE_INIT(name)
# endif // HAS_CXX11 && Python 3
# endif

View File

@@ -38,11 +38,12 @@ BOOST_PYTHON_DECL void scope_setattr_doc(char const* name, object const& x, char
#if PY_VERSION_HEX >= 0x03000000
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef& moduledef, void(*init_function)())
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef& moduledef,
void(*init_function)(), bool gil_not_used)
{
PyObject *mod = PyModule_Create(&moduledef);
#ifdef Py_GIL_DISABLED
if (mod != NULL) {
if (mod != NULL && gil_not_used) {
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
}
#endif

View File

@@ -68,6 +68,7 @@ for t in [('injected',),
('raw_ctor',),
('exception_translator',),
('module_init_exception',),
('module_nogil',),
('test_enum', ['enum_ext']),
('test_cltree', ['cltree']),
('newtest', ['m1', 'm2']),

25
test/module_nogil.cpp Normal file
View File

@@ -0,0 +1,25 @@
// Test for BOOST_PYTHON_MODULE with optional mod_gil_not_used argument
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
// Simple function to export
int get_value() {
return 1234;
}
#if defined(HAS_CXX11) && (PY_VERSION_HEX >= 0x03000000)
// C++11 build with Python 3: test with mod_gil_not_used option
BOOST_PYTHON_MODULE(module_nogil_ext, boost::python::mod_gil_not_used())
{
using namespace boost::python;
def("get_value", get_value);
}
#else
// C++98 build or Python 2: test without optional arguments
BOOST_PYTHON_MODULE(module_nogil_ext)
{
using namespace boost::python;
def("get_value", get_value);
}
#endif

29
test/module_nogil.py Normal file
View File

@@ -0,0 +1,29 @@
"""
>>> from module_nogil_ext import *
>>> get_value()
1234
>>> import sys, sysconfig
>>> Py_GIL_DISABLED = bool(sysconfig.get_config_var('Py_GIL_DISABLED'))
>>> if Py_GIL_DISABLED and sys._is_gil_enabled():
... print('GIL is enabled and should not be')
... else:
... print('okay')
okay
"""
from __future__ import print_function
def run(args = None):
import sys
import doctest
if args is not None:
sys.argv = args
return doctest.testmod(sys.modules.get(__name__))
if __name__ == '__main__':
print("running...")
import sys
status = run()[0]
if (status == 0): print("Done.")
sys.exit(status)