2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00

Respect alignment of by-value storage.

This commit is contained in:
Stefan Seefeld
2021-04-24 14:19:31 -04:00
parent f5d14ef15e
commit aca3c80c4f
13 changed files with 158 additions and 58 deletions

View File

@@ -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
View 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
View 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)

View File

@@ -79,6 +79,8 @@ for t in [('injected',),
('callbacks',),
('defaults',),
('object',),
('class',),
('aligned_class',),
('list',),
('long',),
('dict',),