mirror of
https://github.com/boostorg/python.git
synced 2026-01-19 16:32:16 +00:00
Compare commits
16 Commits
boost-1.76
...
boost-1.85
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
47d5bc76f6 | ||
|
|
508da1d198 | ||
|
|
271bcea8bf | ||
|
|
fdd3e8b2c1 | ||
|
|
a218babc8d | ||
|
|
41e208ecb5 | ||
|
|
f028aa4076 | ||
|
|
a060d43bf2 | ||
|
|
8dd1511773 | ||
|
|
909a4d1530 | ||
|
|
aee2667407 | ||
|
|
209179fa09 | ||
|
|
5e77eabb63 | ||
|
|
ecda18f01e | ||
|
|
2a82afdf6d | ||
|
|
aca3c80c4f |
2
.github/workflows/deploy-documentation.yml
vendored
2
.github/workflows/deploy-documentation.yml
vendored
@@ -1,6 +1,6 @@
|
||||
name: deploy documentation
|
||||
|
||||
on: [push, pull_request]
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
|
||||
4
.github/workflows/test-ubuntu.yml
vendored
4
.github/workflows/test-ubuntu.yml
vendored
@@ -18,9 +18,9 @@ jobs:
|
||||
# pre-reqs installed, see:
|
||||
# https://github.com/teeks99/boost-python-test-docker
|
||||
- cxx: clang++
|
||||
docker-img: teeks99/boost-python-test:clang-11_1.66.0
|
||||
docker-img: teeks99/boost-python-test:clang-12_1.76.0
|
||||
- cxx: g++
|
||||
docker-img: teeks99/boost-python-test:gcc-9_1.66.0
|
||||
docker-img: teeks99/boost-python-test:gcc-10_1.76.0
|
||||
|
||||
container:
|
||||
image: ${{ matrix.docker-img }}
|
||||
|
||||
18
.github/workflows/test-windows.yml
vendored
18
.github/workflows/test-windows.yml
vendored
@@ -15,16 +15,26 @@ jobs:
|
||||
- uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
- uses: microsoft/setup-msbuild@v1.1
|
||||
- name: setup boost prerequisites
|
||||
uses: lukka/run-vcpkg@v6
|
||||
with:
|
||||
vcpkgGitCommitId: '8a9a97315aefb3f8bc5d81bf66ca0025938b9c91'
|
||||
vcpkgGitCommitId: '88b1071e39f13b632644d9d953738d345a4ac055'
|
||||
vcpkgDirectory: '${{ runner.workspace }}/vcpkg'
|
||||
vcpkgTriplet: x64-windows
|
||||
vcpkgArguments: boost-config boost-core boost-function boost-graph boost-iterator boost-lexical-cast boost-mpl boost-preprocessor boost-smart-ptr boost-static-assert
|
||||
|
||||
vcpkgArguments: >
|
||||
boost-config
|
||||
boost-core
|
||||
boost-function
|
||||
boost-graph
|
||||
boost-iterator
|
||||
boost-lexical-cast
|
||||
boost-mpl
|
||||
boost-preprocessor
|
||||
boost-smart-ptr
|
||||
boost-static-assert
|
||||
boost-align
|
||||
- name: setup faber
|
||||
#shell: 'bash'
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install setuptools faber numpy
|
||||
|
||||
176
CMakeLists.txt
Normal file
176
CMakeLists.txt
Normal file
@@ -0,0 +1,176 @@
|
||||
# Copyright 2020, 2021 Peter Dimov
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
cmake_minimum_required(VERSION 3.14...3.20)
|
||||
|
||||
project(boost_python VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX)
|
||||
|
||||
find_package(Python REQUIRED COMPONENTS Development OPTIONAL_COMPONENTS NumPy)
|
||||
|
||||
if(Python_NumPy_FOUND)
|
||||
message(STATUS "Boost.Python: using Python ${Python_VERSION} with NumPy at ${Python_NumPy_INCLUDE_DIRS}")
|
||||
else()
|
||||
message(STATUS "Boost.Python: using Python ${Python_VERSION} without NumPy")
|
||||
endif()
|
||||
|
||||
# boost_pythonXY
|
||||
|
||||
set(_pyver ${Python_VERSION_MAJOR}${Python_VERSION_MINOR})
|
||||
set(_boost_python boost_python${_pyver})
|
||||
|
||||
add_library(${_boost_python}
|
||||
src/dict.cpp
|
||||
src/errors.cpp
|
||||
src/exec.cpp
|
||||
src/import.cpp
|
||||
src/list.cpp
|
||||
src/long.cpp
|
||||
src/module.cpp
|
||||
src/object_operators.cpp
|
||||
src/object_protocol.cpp
|
||||
src/slice.cpp
|
||||
src/str.cpp
|
||||
src/tuple.cpp
|
||||
src/wrapper.cpp
|
||||
src/converter/from_python.cpp
|
||||
src/converter/registry.cpp
|
||||
src/converter/type_id.cpp
|
||||
src/converter/builtin_converters.cpp
|
||||
src/converter/arg_to_python_base.cpp
|
||||
src/object/enum.cpp
|
||||
src/object/class.cpp
|
||||
src/object/function.cpp
|
||||
src/object/inheritance.cpp
|
||||
src/object/life_support.cpp
|
||||
src/object/pickle_support.cpp
|
||||
src/object/iterator.cpp
|
||||
src/object/stl_iterator.cpp
|
||||
src/object_protocol.cpp
|
||||
src/object_operators.cpp
|
||||
src/object/function_doc_signature.cpp
|
||||
)
|
||||
|
||||
add_library(Boost::python${_pyver} ALIAS ${_boost_python})
|
||||
|
||||
target_include_directories(${_boost_python} PUBLIC include)
|
||||
|
||||
target_link_libraries(${_boost_python}
|
||||
PUBLIC
|
||||
Boost::align
|
||||
Boost::bind
|
||||
Boost::config
|
||||
Boost::conversion
|
||||
Boost::core
|
||||
Boost::detail
|
||||
Boost::foreach
|
||||
Boost::function
|
||||
Boost::iterator
|
||||
Boost::lexical_cast
|
||||
Boost::mpl
|
||||
Boost::numeric_conversion
|
||||
Boost::preprocessor
|
||||
Boost::smart_ptr
|
||||
Boost::static_assert
|
||||
Boost::tuple
|
||||
Boost::type_traits
|
||||
Boost::utility
|
||||
|
||||
Python::Module
|
||||
|
||||
PRIVATE
|
||||
Boost::graph
|
||||
Boost::integer
|
||||
Boost::property_map
|
||||
)
|
||||
|
||||
target_compile_definitions(${_boost_python}
|
||||
PUBLIC BOOST_PYTHON_NO_LIB
|
||||
PRIVATE BOOST_PYTHON_SOURCE
|
||||
)
|
||||
|
||||
if(BUILD_SHARED_LIBS)
|
||||
target_compile_definitions(${_boost_python} PUBLIC BOOST_PYTHON_DYN_LINK)
|
||||
else()
|
||||
target_compile_definitions(${_boost_python} PUBLIC BOOST_PYTHON_STATIC_LINK BOOST_PYTHON_STATIC_LIB)
|
||||
endif()
|
||||
|
||||
# Boost::python alias
|
||||
|
||||
add_library(boost_python INTERFACE)
|
||||
add_library(Boost::python ALIAS boost_python)
|
||||
target_link_libraries(boost_python INTERFACE Boost::python${_pyver})
|
||||
|
||||
# Installation
|
||||
|
||||
if(BOOST_SUPERPROJECT_VERSION AND NOT CMAKE_VERSION VERSION_LESS 3.13)
|
||||
boost_install(TARGETS ${_boost_python} boost_python VERSION ${BOOST_SUPERPROJECT_VERSION} HEADER_DIRECTORY include)
|
||||
endif()
|
||||
|
||||
if(Python_NumPy_FOUND)
|
||||
|
||||
# boost_numpyXY
|
||||
|
||||
set(_boost_numpy boost_numpy${_pyver})
|
||||
|
||||
add_library(${_boost_numpy}
|
||||
src/numpy/dtype.cpp
|
||||
src/numpy/matrix.cpp
|
||||
src/numpy/ndarray.cpp
|
||||
src/numpy/numpy.cpp
|
||||
src/numpy/scalars.cpp
|
||||
src/numpy/ufunc.cpp
|
||||
)
|
||||
|
||||
add_library(Boost::numpy${_pyver} ALIAS ${_boost_numpy})
|
||||
|
||||
target_include_directories(${_boost_numpy} PUBLIC include)
|
||||
|
||||
target_link_libraries(${_boost_numpy}
|
||||
PUBLIC
|
||||
Boost::config
|
||||
Boost::core
|
||||
Boost::detail
|
||||
Boost::mpl
|
||||
Boost::python
|
||||
Boost::smart_ptr
|
||||
|
||||
Python::NumPy
|
||||
)
|
||||
|
||||
target_compile_definitions(${_boost_numpy}
|
||||
PUBLIC BOOST_NUMPY_NO_LIB
|
||||
PRIVATE BOOST_NUMPY_SOURCE
|
||||
)
|
||||
|
||||
if(BUILD_SHARED_LIBS)
|
||||
target_compile_definitions(${_boost_numpy} PUBLIC BOOST_NUMPY_DYN_LINK)
|
||||
else()
|
||||
target_compile_definitions(${_boost_numpy} PUBLIC BOOST_NUMPY_STATIC_LINK BOOST_NUMPY_STATIC_LIB)
|
||||
endif()
|
||||
|
||||
# Boost::numpy alias
|
||||
|
||||
add_library(boost_numpy INTERFACE)
|
||||
add_library(Boost::numpy ALIAS boost_numpy)
|
||||
target_link_libraries(boost_numpy INTERFACE Boost::numpy${_pyver})
|
||||
|
||||
# Installation
|
||||
|
||||
if(BOOST_SUPERPROJECT_VERSION AND NOT CMAKE_VERSION VERSION_LESS 3.13)
|
||||
boost_install(TARGETS ${_boost_numpy} boost_numpy VERSION ${BOOST_SUPERPROJECT_VERSION})
|
||||
endif()
|
||||
|
||||
endif()
|
||||
|
||||
unset(_pyver)
|
||||
unset(_boost_python)
|
||||
unset(_boost_numpy)
|
||||
|
||||
# Testing
|
||||
|
||||
if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt")
|
||||
|
||||
add_subdirectory(test)
|
||||
|
||||
endif()
|
||||
@@ -117,7 +117,7 @@ lib boost_numpy
|
||||
<define>BOOST_NUMPY_SOURCE
|
||||
[ cond [ python.numpy ] : <library>/python//python_for_extensions ]
|
||||
[ unless [ python.numpy ] : <build>no ]
|
||||
<include>$(numpy-include)
|
||||
<library>/python//numpy
|
||||
<library>boost_python
|
||||
<python-debugging>on:<define>BOOST_DEBUG_PYTHON
|
||||
-<tag>@$(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).tag
|
||||
|
||||
@@ -59,7 +59,7 @@ call_method(PyObject* self, char const* name
|
||||
)
|
||||
{
|
||||
PyObject* const result =
|
||||
PyEval_CallMethod(
|
||||
PyObject_CallMethod(
|
||||
self
|
||||
, const_cast<char*>(name)
|
||||
, const_cast<char*>("(" BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_FIXED, "O") ")")
|
||||
|
||||
@@ -81,9 +81,9 @@ inline object_manager_ref_arg_from_python<Ref>::object_manager_ref_arg_from_pyth
|
||||
# if defined(__EDG_VERSION__) && __EDG_VERSION__ <= 243
|
||||
// needed for warning suppression
|
||||
python::detail::borrowed_reference x_ = python::detail::borrowed_reference(x);
|
||||
python::detail::construct_referent<Ref>(&m_result.bytes, x_);
|
||||
python::detail::construct_referent<Ref>(m_result.bytes, x_);
|
||||
# else
|
||||
python::detail::construct_referent<Ref>(&m_result.bytes, (python::detail::borrowed_reference)x);
|
||||
python::detail::construct_referent<Ref>(m_result.bytes, (python::detail::borrowed_reference)x);
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
# include <boost/python/detail/referent_storage.hpp>
|
||||
# include <boost/python/detail/destroy.hpp>
|
||||
# include <boost/python/detail/type_traits.hpp>
|
||||
# include <boost/align/align.hpp>
|
||||
# include <boost/static_assert.hpp>
|
||||
# include <cstddef>
|
||||
|
||||
@@ -132,7 +133,13 @@ template <class T>
|
||||
inline rvalue_from_python_data<T>::~rvalue_from_python_data()
|
||||
{
|
||||
if (this->stage1.convertible == this->storage.bytes)
|
||||
python::detail::destroy_referent<ref_type>(this->storage.bytes);
|
||||
{
|
||||
size_t allocated = sizeof(this->storage);
|
||||
void *ptr = this->storage.bytes;
|
||||
void *aligned_storage =
|
||||
::boost::alignment::align(boost::python::detail::alignment_of<T>::value, 0, ptr, allocated);
|
||||
python::detail::destroy_referent<ref_type>(aligned_storage);
|
||||
}
|
||||
}
|
||||
|
||||
}}} // namespace boost::python::converter
|
||||
|
||||
@@ -49,13 +49,17 @@ struct shared_ptr_from_python
|
||||
new (storage) SP<T>();
|
||||
else
|
||||
{
|
||||
SP<void> hold_convertible_ref_count(
|
||||
(void*)0, shared_ptr_deleter(handle<>(borrowed(source))) );
|
||||
// use aliasing constructor
|
||||
new (storage) SP<T>(hold_convertible_ref_count,
|
||||
static_cast<T*>(data->convertible));
|
||||
void *const storage = ((converter::rvalue_from_python_storage<SP<T> >*)data)->storage.bytes;
|
||||
// Deal with the "None" case.
|
||||
if (data->convertible == source)
|
||||
new (storage) SP<T>();
|
||||
else
|
||||
{
|
||||
SP<void> hold_convertible_ref_count((void*)0, shared_ptr_deleter(handle<>(borrowed(source))) );
|
||||
// use aliasing constructor
|
||||
new (storage) SP<T>(hold_convertible_ref_count, static_cast<T*>(data->convertible));
|
||||
}
|
||||
}
|
||||
|
||||
data->convertible = storage;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -5,39 +5,21 @@
|
||||
#ifndef REFERENT_STORAGE_DWA200278_HPP
|
||||
# define REFERENT_STORAGE_DWA200278_HPP
|
||||
# include <boost/mpl/if.hpp>
|
||||
# include <boost/type_traits/aligned_storage.hpp>
|
||||
# include <cstddef>
|
||||
|
||||
namespace boost { namespace python { namespace detail {
|
||||
|
||||
struct alignment_dummy;
|
||||
typedef void (*function_ptr)();
|
||||
typedef int (alignment_dummy::*member_ptr);
|
||||
typedef int (alignment_dummy::*member_function_ptr)();
|
||||
|
||||
# define BOOST_PYTHON_ALIGNER(T, n) \
|
||||
typename mpl::if_c< \
|
||||
sizeof(T) <= size, T, char>::type t##n
|
||||
|
||||
// Storage for size bytes, aligned to all fundamental types no larger than size
|
||||
template <std::size_t size>
|
||||
union aligned_storage
|
||||
template <std::size_t size, std::size_t alignment = std::size_t(-1)>
|
||||
struct aligned_storage
|
||||
{
|
||||
BOOST_PYTHON_ALIGNER(char, 0);
|
||||
BOOST_PYTHON_ALIGNER(short, 1);
|
||||
BOOST_PYTHON_ALIGNER(int, 2);
|
||||
BOOST_PYTHON_ALIGNER(long, 3);
|
||||
BOOST_PYTHON_ALIGNER(float, 4);
|
||||
BOOST_PYTHON_ALIGNER(double, 5);
|
||||
BOOST_PYTHON_ALIGNER(long double, 6);
|
||||
BOOST_PYTHON_ALIGNER(void*, 7);
|
||||
BOOST_PYTHON_ALIGNER(function_ptr, 8);
|
||||
BOOST_PYTHON_ALIGNER(member_ptr, 9);
|
||||
BOOST_PYTHON_ALIGNER(member_function_ptr, 10);
|
||||
union type
|
||||
{
|
||||
typename ::boost::aligned_storage<size, alignment>::type data;
|
||||
char bytes[size];
|
||||
};
|
||||
};
|
||||
|
||||
# undef BOOST_PYTHON_ALIGNER
|
||||
|
||||
|
||||
// Compute the size of T's referent. We wouldn't need this at all,
|
||||
// but sizeof() is broken in CodeWarriors <= 8.0
|
||||
template <class T> struct referent_size;
|
||||
@@ -50,15 +32,12 @@ union aligned_storage
|
||||
std::size_t, value = sizeof(T));
|
||||
};
|
||||
|
||||
|
||||
// A metafunction returning a POD type which can store U, where T ==
|
||||
// U&. If T is not a reference type, returns a POD which can store T.
|
||||
template <class T>
|
||||
struct referent_storage
|
||||
{
|
||||
typedef aligned_storage<
|
||||
::boost::python::detail::referent_size<T>::value
|
||||
> type;
|
||||
typedef typename aligned_storage<referent_size<T>::value, alignment_of<T>::value>::type type;
|
||||
};
|
||||
|
||||
}}} // namespace boost::python::detail
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
# include <boost/python/detail/prefix.hpp>
|
||||
|
||||
# include <boost/bind.hpp>
|
||||
# include <boost/bind/bind.hpp>
|
||||
# include <boost/bind/placeholders.hpp>
|
||||
# include <boost/type.hpp>
|
||||
# include <boost/python/detail/translate_exception.hpp>
|
||||
@@ -18,6 +18,7 @@ namespace boost { namespace python {
|
||||
template <class ExceptionType, class Translate>
|
||||
void register_exception_translator(Translate translate, boost::type<ExceptionType>* = 0)
|
||||
{
|
||||
using namespace boost::placeholders;
|
||||
detail::register_exception_handler(
|
||||
boost::bind<bool>(detail::translate_exception<ExceptionType,Translate>(), _1, _2, translate)
|
||||
);
|
||||
|
||||
@@ -38,7 +38,7 @@ struct BOOST_PYTHON_DECL instance_holder : private noncopyable
|
||||
// Allocate storage for an object of the given size at the given
|
||||
// offset in the Python instance<> object if bytes are available
|
||||
// there. Otherwise allocate size bytes of heap memory.
|
||||
static void* allocate(PyObject*, std::size_t offset, std::size_t size);
|
||||
static void* allocate(PyObject*, std::size_t offset, std::size_t size, std::size_t alignment = 1);
|
||||
|
||||
// Deallocate storage from the heap if it was not carved out of
|
||||
// the given Python object by allocate(), above.
|
||||
|
||||
@@ -22,7 +22,7 @@ works correctly. */
|
||||
# pragma warning(disable: 4180)
|
||||
# endif
|
||||
|
||||
# include <boost/bind.hpp>
|
||||
# include <boost/bind/bind.hpp>
|
||||
# include <boost/bind/protect.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
@@ -40,6 +40,7 @@ namespace detail
|
||||
, Target&(*)()
|
||||
)
|
||||
{
|
||||
using namespace boost::placeholders;
|
||||
return objects::make_iterator_function<Target>(
|
||||
boost::protect(boost::bind(get_start, _1))
|
||||
, boost::protect(boost::bind(get_finish, _1))
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
# define INSTANCE_DWA200295_HPP
|
||||
|
||||
# include <boost/python/detail/prefix.hpp>
|
||||
|
||||
# include <boost/python/detail/type_traits.hpp>
|
||||
# include <cstddef>
|
||||
|
||||
namespace boost { namespace python
|
||||
@@ -28,7 +28,7 @@ struct instance
|
||||
typedef typename boost::python::detail::type_with_alignment<
|
||||
boost::python::detail::alignment_of<Data>::value
|
||||
>::type align_t;
|
||||
|
||||
|
||||
union
|
||||
{
|
||||
align_t align;
|
||||
@@ -41,9 +41,10 @@ struct additional_instance_size
|
||||
{
|
||||
typedef instance<Data> instance_data;
|
||||
typedef instance<char> instance_char;
|
||||
BOOST_STATIC_CONSTANT(
|
||||
std::size_t, value = sizeof(instance_data)
|
||||
- BOOST_PYTHON_OFFSETOF(instance_char,storage));
|
||||
BOOST_STATIC_CONSTANT(std::size_t,
|
||||
value = sizeof(instance_data) -
|
||||
BOOST_PYTHON_OFFSETOF(instance_char,storage) +
|
||||
boost::python::detail::alignment_of<Data>::value);
|
||||
};
|
||||
|
||||
}}} // namespace boost::python::object
|
||||
|
||||
@@ -89,8 +89,9 @@ struct make_holder<N>
|
||||
BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(1, N, t, a))
|
||||
{
|
||||
typedef instance<Holder> instance_t;
|
||||
|
||||
void* memory = Holder::allocate(p, offsetof(instance_t, storage), sizeof(Holder));
|
||||
|
||||
void* memory = Holder::allocate(p, offsetof(instance_t, storage), sizeof(Holder),
|
||||
boost::python::detail::alignment_of<Holder>::value);
|
||||
try {
|
||||
(new (memory) Holder(
|
||||
p BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_DO_FORWARD_ARG, nil)))->install(p);
|
||||
|
||||
@@ -43,11 +43,14 @@ struct make_instance_impl
|
||||
|
||||
// construct the new C++ object and install the pointer
|
||||
// in the Python object.
|
||||
Derived::construct(&instance->storage, (PyObject*)instance, x)->install(raw_result);
|
||||
Holder *holder =Derived::construct(instance->storage.bytes, (PyObject*)instance, x);
|
||||
holder->install(raw_result);
|
||||
|
||||
// Note the position of the internally-stored Holder,
|
||||
// for the sake of destruction
|
||||
Py_SET_SIZE(instance, offsetof(instance_t, storage));
|
||||
const size_t offset = reinterpret_cast<size_t>(holder) -
|
||||
reinterpret_cast<size_t>(instance->storage.bytes) + offsetof(instance_t, storage);
|
||||
Py_SET_SIZE(instance, offset);
|
||||
|
||||
// Release ownership of the python object
|
||||
protect.cancel();
|
||||
@@ -69,7 +72,10 @@ struct make_instance
|
||||
|
||||
static inline Holder* construct(void* storage, PyObject* instance, reference_wrapper<T const> x)
|
||||
{
|
||||
return new (storage) Holder(instance, x);
|
||||
size_t allocated = objects::additional_instance_size<Holder>::value;
|
||||
void* aligned_storage = ::boost::alignment::align(boost::python::detail::alignment_of<Holder>::value,
|
||||
sizeof(Holder), storage, allocated);
|
||||
return new (aligned_storage) Holder(instance, x);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -109,8 +109,8 @@ object BOOST_PYTHON_DECL exec_file(char const *filename, object global, object l
|
||||
PyObject *fo = Py_BuildValue("s", f);
|
||||
PyObject *fb = Py_None;
|
||||
PyUnicode_FSConverter(fo, &fb);
|
||||
f = PyBytes_AsString(fb);
|
||||
FILE *fs = fopen(f, "r");
|
||||
char *f_as_uft = PyBytes_AsString(fb);
|
||||
FILE *fs = fopen(f_as_uft, "r");
|
||||
Py_DECREF(fo);
|
||||
Py_DECREF(fb);
|
||||
#elif PY_VERSION_HEX >= 0x03000000
|
||||
@@ -129,6 +129,7 @@ object BOOST_PYTHON_DECL exec_file(char const *filename, object global, object l
|
||||
f,
|
||||
Py_file_input,
|
||||
global.ptr(), local.ptr());
|
||||
fclose(fs);
|
||||
if (!result) throw_error_already_set();
|
||||
return object(detail::new_reference(result));
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <boost/python/detail/prefix.hpp>
|
||||
#include <boost/mpl/lambda.hpp> // #including this first is an intel6 workaround
|
||||
#include <boost/cstdint.hpp>
|
||||
|
||||
#include <boost/python/object/class.hpp>
|
||||
#include <boost/python/object/instance.hpp>
|
||||
@@ -721,28 +722,47 @@ namespace objects
|
||||
} // namespace objects
|
||||
|
||||
|
||||
void* instance_holder::allocate(PyObject* self_, std::size_t holder_offset, std::size_t holder_size)
|
||||
typedef unsigned int alignment_marker_t;
|
||||
|
||||
void* instance_holder::allocate(PyObject* self_, std::size_t holder_offset, std::size_t holder_size, std::size_t alignment)
|
||||
{
|
||||
assert(PyType_IsSubtype(Py_TYPE(Py_TYPE(self_)), &class_metatype_object));
|
||||
objects::instance<>* self = (objects::instance<>*)self_;
|
||||
|
||||
int total_size_needed = holder_offset + holder_size;
|
||||
int total_size_needed = holder_offset + holder_size + alignment - 1;
|
||||
|
||||
if (-Py_SIZE(self) >= total_size_needed)
|
||||
{
|
||||
// holder_offset should at least point into the variable-sized part
|
||||
assert(holder_offset >= offsetof(objects::instance<>,storage));
|
||||
|
||||
size_t allocated = holder_size + alignment;
|
||||
void* storage = (char*)self + holder_offset;
|
||||
void* aligned_storage = ::boost::alignment::align(alignment, holder_size, storage, allocated);
|
||||
|
||||
// Record the fact that the storage is occupied, noting where it starts
|
||||
Py_SET_SIZE(self, holder_offset);
|
||||
return (char*)self + holder_offset;
|
||||
const size_t offset = reinterpret_cast<uintptr_t>(aligned_storage) - reinterpret_cast<uintptr_t>(storage) + holder_offset;
|
||||
Py_SET_SIZE(self, offset);
|
||||
return (char*)self + offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
void* const result = PyMem_Malloc(holder_size);
|
||||
if (result == 0)
|
||||
const size_t base_allocation = sizeof(alignment_marker_t) + holder_size + alignment - 1;
|
||||
void* const base_storage = PyMem_Malloc(base_allocation);
|
||||
if (base_storage == 0)
|
||||
throw std::bad_alloc();
|
||||
return result;
|
||||
|
||||
const uintptr_t x = reinterpret_cast<uintptr_t>(base_storage) + sizeof(alignment_marker_t);
|
||||
//this has problems for x -> max(void *)
|
||||
//const size_t padding = alignment - ((x + sizeof(alignment_marker_t)) % alignment);
|
||||
//only works for alignments with alignments of powers of 2, but no edge conditions
|
||||
const uintptr_t padding = alignment == 1 ? 0 : ( alignment - (x & (alignment - 1)) );
|
||||
const size_t aligned_offset = sizeof(alignment_marker_t) + padding;
|
||||
void* const aligned_storage = (char *)base_storage + aligned_offset;
|
||||
BOOST_ASSERT((char *) aligned_storage + holder_size <= (char *)base_storage + base_allocation);
|
||||
alignment_marker_t* const marker_storage = reinterpret_cast<alignment_marker_t *>((char *)aligned_storage - sizeof(alignment_marker_t));
|
||||
*marker_storage = static_cast<alignment_marker_t>(padding);
|
||||
return aligned_storage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -752,7 +772,9 @@ void instance_holder::deallocate(PyObject* self_, void* storage) throw()
|
||||
objects::instance<>* self = (objects::instance<>*)self_;
|
||||
if (storage != (char*)self + Py_SIZE(self))
|
||||
{
|
||||
PyMem_Free(storage);
|
||||
alignment_marker_t* marker_storage = reinterpret_cast<alignment_marker_t *>((char *)storage - sizeof(alignment_marker_t));
|
||||
void *malloced_storage = (char *) storage - sizeof(alignment_marker_t) - (*marker_storage);
|
||||
PyMem_Free(malloced_storage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -113,7 +113,6 @@ static PyTypeObject enum_type_object = {
|
||||
#if PY_VERSION_HEX < 0x03000000
|
||||
| Py_TPFLAGS_CHECKTYPES
|
||||
#endif
|
||||
| Py_TPFLAGS_HAVE_GC
|
||||
| Py_TPFLAGS_BASETYPE, /* tp_flags */
|
||||
0, /* tp_doc */
|
||||
0, /* tp_traverse */
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include <boost/python/detail/none.hpp>
|
||||
#include <boost/mpl/vector/vector10.hpp>
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <boost/graph/adjacency_list.hpp>
|
||||
#include <boost/graph/reverse_graph.hpp>
|
||||
#include <boost/property_map/property_map.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/integer_traits.hpp>
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/tuple/tuple_comparison.hpp>
|
||||
@@ -184,6 +184,7 @@ namespace
|
||||
// map a type to a position in the index
|
||||
inline type_index_t::iterator type_position(class_id type)
|
||||
{
|
||||
using namespace boost::placeholders;
|
||||
typedef index_entry entry;
|
||||
|
||||
return std::lower_bound(
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
#include <boost/python/object/iterator_core.hpp>
|
||||
#include <boost/python/object/function_object.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/mpl/vector/vector10.hpp>
|
||||
|
||||
namespace boost { namespace python { namespace objects {
|
||||
|
||||
@@ -132,6 +132,7 @@ bpl-test crossmod_exception
|
||||
|
||||
[ bpl-test object ]
|
||||
[ bpl-test class ]
|
||||
[ bpl-test aligned_class ]
|
||||
[ bpl-test list ]
|
||||
[ bpl-test long ]
|
||||
[ bpl-test dict ]
|
||||
|
||||
33
test/aligned_class.cpp
Normal file
33
test/aligned_class.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/def.hpp>
|
||||
#include <boost/python/object.hpp>
|
||||
#include <boost/python/class.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
struct BOOST_ALIGNMENT(32) X
|
||||
{
|
||||
int x;
|
||||
BOOST_ALIGNMENT(32) float f;
|
||||
X(int n, float _f) : x(n), f(_f)
|
||||
{
|
||||
BOOST_ASSERT((reinterpret_cast<uintptr_t>(&f) % 32) == 0);
|
||||
}
|
||||
};
|
||||
|
||||
int x_function(X& x) { return x.x;}
|
||||
float f_function(X& x) { return x.f;}
|
||||
|
||||
BOOST_PYTHON_MODULE(aligned_class_ext)
|
||||
{
|
||||
class_<X>("X", init<int, float>());
|
||||
def("x_function", x_function);
|
||||
def("f_function", f_function);
|
||||
}
|
||||
|
||||
#include "module_tail.cpp"
|
||||
44
test/aligned_class.py
Executable file
44
test/aligned_class.py
Executable file
@@ -0,0 +1,44 @@
|
||||
# Distributed under the Boost
|
||||
# Software License, Version 1.0. (See accompanying
|
||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
'''
|
||||
>>> from aligned_class_ext import *
|
||||
|
||||
Ensure sanity:
|
||||
|
||||
>>> x = X(42, 16)
|
||||
>>> x_function(x)
|
||||
42
|
||||
>>> f_function(x)
|
||||
16.0
|
||||
|
||||
Demonstrate extraction in the presence of metaclass changes:
|
||||
|
||||
>>> class MetaX(X.__class__):
|
||||
... def __new__(cls, *args):
|
||||
... return super(MetaX, cls).__new__(cls, *args)
|
||||
>>> class XPlusMetatype(X):
|
||||
... __metaclass__ = MetaX
|
||||
>>> x = XPlusMetatype(42, 16)
|
||||
>>> x_function(x)
|
||||
42
|
||||
>>> f_function(x)
|
||||
16.0
|
||||
|
||||
|
||||
'''
|
||||
|
||||
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)
|
||||
@@ -79,6 +79,8 @@ for t in [('injected',),
|
||||
('callbacks',),
|
||||
('defaults',),
|
||||
('object',),
|
||||
('class',),
|
||||
('aligned_class',),
|
||||
('list',),
|
||||
('long',),
|
||||
('dict',),
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <boost/python.hpp>
|
||||
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user