Skip to content

Commit 0a211a7

Browse files
author
Ralf W. Grosse-Kunstleve
committed
merging current boost/python and libs/python from trunk into release branch
[SVN r70448]
1 parent ba21366 commit 0a211a7

5 files changed

Lines changed: 102 additions & 4 deletions

File tree

class.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Distributed under the Boost Software License, Version 1.0. (See
2+
// accompanying file LICENSE_1_0.txt or copy at
3+
// http://www.boost.org/LICENSE_1_0.txt)
4+
#include <boost/python/module.hpp>
5+
#include <boost/python/def.hpp>
6+
#include <boost/python/object.hpp>
7+
#include <boost/python/class.hpp>
8+
9+
using namespace boost::python;
10+
11+
struct X
12+
{
13+
int x;
14+
X(int n) : x(n) { }
15+
};
16+
17+
int x_function(X& x)
18+
{ return x.x;
19+
}
20+
21+
22+
BOOST_PYTHON_MODULE(class_ext)
23+
{
24+
class_<X>("X", init<int>());
25+
def("x_function", x_function);
26+
}
27+
28+
#include "module_tail.cpp"

src/object/class.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static PyTypeObject class_metatype_object = {
303303
// object.
304304
void instance_holder::install(PyObject* self) throw()
305305
{
306-
assert(Py_TYPE(Py_TYPE(self)) == &class_metatype_object);
306+
assert(PyType_IsSubtype(Py_TYPE(Py_TYPE(self)), &class_metatype_object));
307307
m_next = ((objects::instance<>*)self)->objects;
308308
((objects::instance<>*)self)->objects = this;
309309
}
@@ -482,7 +482,8 @@ namespace objects
482482
BOOST_PYTHON_DECL void*
483483
find_instance_impl(PyObject* inst, type_info type, bool null_shared_ptr_only)
484484
{
485-
if (Py_TYPE(Py_TYPE(inst)) != &class_metatype_object)
485+
if (!Py_TYPE(Py_TYPE(inst)) ||
486+
!PyType_IsSubtype(Py_TYPE(Py_TYPE(inst)), &class_metatype_object))
486487
return 0;
487488

488489
instance<>* self = reinterpret_cast<instance<>*>(inst);
@@ -727,7 +728,7 @@ namespace objects
727728

728729
void* instance_holder::allocate(PyObject* self_, std::size_t holder_offset, std::size_t holder_size)
729730
{
730-
assert(Py_TYPE(Py_TYPE(self_)) == &class_metatype_object);
731+
assert(PyType_IsSubtype(Py_TYPE(Py_TYPE(self_)), &class_metatype_object));
731732
objects::instance<>* self = (objects::instance<>*)self_;
732733

733734
int total_size_needed = holder_offset + holder_size;
@@ -752,7 +753,7 @@ void* instance_holder::allocate(PyObject* self_, std::size_t holder_offset, std:
752753

753754
void instance_holder::deallocate(PyObject* self_, void* storage) throw()
754755
{
755-
assert(Py_TYPE(Py_TYPE(self_)) == &class_metatype_object);
756+
assert(PyType_IsSubtype(Py_TYPE(Py_TYPE(self_)), &class_metatype_object));
756757
objects::instance<>* self = (objects::instance<>*)self_;
757758
if (storage != (char*)self + Py_SIZE(self))
758759
{

test/Jamfile.v2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ bpl-test crossmod_exception
117117
[ bpl-test defaults ]
118118

119119
[ bpl-test object ]
120+
[ bpl-test class ]
120121
[ bpl-test list ]
121122
[ bpl-test long ]
122123
[ bpl-test dict ]

test/class.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Distributed under the Boost Software License, Version 1.0. (See
2+
// accompanying file LICENSE_1_0.txt or copy at
3+
// http://www.boost.org/LICENSE_1_0.txt)
4+
#include <boost/python/module.hpp>
5+
#include <boost/python/def.hpp>
6+
#include <boost/python/object.hpp>
7+
#include <boost/python/class.hpp>
8+
9+
using namespace boost::python;
10+
11+
struct X
12+
{
13+
int x;
14+
X(int n) : x(n) { }
15+
};
16+
17+
int x_function(X& x)
18+
{ return x.x;
19+
}
20+
21+
22+
BOOST_PYTHON_MODULE(class_ext)
23+
{
24+
class_<X>("X", init<int>());
25+
def("x_function", x_function);
26+
}
27+
28+
#include "module_tail.cpp"

test/class.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Distributed under the Boost
2+
# Software License, Version 1.0. (See accompanying
3+
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4+
'''
5+
>>> from class_ext import *
6+
7+
Ensure sanity:
8+
9+
>>> x = X(42)
10+
>>> x_function(x)
11+
42
12+
13+
Demonstrate extraction in the presence of metaclass changes:
14+
15+
>>> class MetaX(X.__class__):
16+
... def __new__(cls, *args):
17+
... return super(MetaX, cls).__new__(cls, *args)
18+
>>> class XPlusMetatype(X):
19+
... __metaclass__ = MetaX
20+
>>> x = XPlusMetatype(42)
21+
>>> x_function(x)
22+
42
23+
24+
25+
'''
26+
27+
def run(args = None):
28+
import sys
29+
import doctest
30+
31+
if args is not None:
32+
sys.argv = args
33+
return doctest.testmod(sys.modules.get(__name__))
34+
35+
if __name__ == '__main__':
36+
print "running..."
37+
import sys
38+
status = run()[0]
39+
if (status == 0): print "Done."
40+
sys.exit(status)

0 commit comments

Comments
 (0)