mirror of
https://github.com/boostorg/python.git
synced 2026-01-20 16:52:15 +00:00
Compare commits
8 Commits
boost-1.46
...
boost-1.47
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b827998e23 | ||
|
|
cfa2baf8c9 | ||
|
|
faf3cd0b78 | ||
|
|
a5e564be83 | ||
|
|
dcc3590907 | ||
|
|
74daea3d88 | ||
|
|
e28f0509d8 | ||
|
|
0a211a746d |
28
class.cpp
Normal file
28
class.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
// 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>
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
struct X
|
||||
{
|
||||
int x;
|
||||
X(int n) : x(n) { }
|
||||
};
|
||||
|
||||
int x_function(X& x)
|
||||
{ return x.x;
|
||||
}
|
||||
|
||||
|
||||
BOOST_PYTHON_MODULE(class_ext)
|
||||
{
|
||||
class_<X>("X", init<int>());
|
||||
def("x_function", x_function);
|
||||
}
|
||||
|
||||
#include "module_tail.cpp"
|
||||
@@ -27,7 +27,6 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
Somerville, MA 02143
|
||||
:Contact: dave@boost-consulting.com
|
||||
:organization: `Boost Consulting`_
|
||||
:date: $Date$
|
||||
:status: This is a "work in progress"
|
||||
:version: 1
|
||||
:copyright: Copyright David Abrahams 2002. All rights reserved
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
/*
|
||||
:Author: David Goodger
|
||||
:Contact: goodger@users.sourceforge.net
|
||||
:date: $Date$
|
||||
:version: $Revision$
|
||||
:copyright: This stylesheet has been placed in the public domain.
|
||||
|
||||
boostinspect:nolicense
|
||||
|
||||
@@ -124,7 +124,7 @@
|
||||
<p>
|
||||
</p>
|
||||
<pre class="programlisting"><span class="special">>>></span> <span class="keyword">import</span> <span class="identifier">hello_ext</span>
|
||||
<span class="special">>>></span> <span class="keyword">print</span> <span class="identifier">hello</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span>
|
||||
<span class="special">>>></span> <span class="keyword">print</span> <span class="identifier">hello_ext</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span>
|
||||
<span class="identifier">hello</span><span class="special">,</span> <span class="identifier">world</span>
|
||||
</pre>
|
||||
<p>
|
||||
|
||||
@@ -62,7 +62,7 @@ resulting DLL is now visible to Python. Here's a sample Python session:
|
||||
[python]
|
||||
|
||||
>>> import hello_ext
|
||||
>>> print hello.greet()
|
||||
>>> print hello_ext.greet()
|
||||
hello, world
|
||||
|
||||
[c++]
|
||||
|
||||
@@ -77,7 +77,7 @@ namespace detail
|
||||
template <class T>
|
||||
T unchecked(type<T>* = 0)
|
||||
{
|
||||
return extract<T>(m_obj)();
|
||||
return extract<T>(m_obj.get())();
|
||||
}
|
||||
private:
|
||||
mutable handle<> m_obj;
|
||||
|
||||
@@ -366,7 +366,9 @@ namespace
|
||||
static PyTypeObject const* get_pytype() { return &PyFloat_Type;}
|
||||
};
|
||||
|
||||
#if PY_VERSION_HEX >= 0x03000000
|
||||
unaryfunc py_unicode_as_string_unaryfunc = PyUnicode_AsUTF8String;
|
||||
#endif
|
||||
|
||||
// A SlotPolicy for extracting C++ strings from Python objects.
|
||||
struct string_rvalue_from_python
|
||||
@@ -431,7 +433,10 @@ namespace
|
||||
if (!result.empty())
|
||||
{
|
||||
int err = PyUnicode_AsWideChar(
|
||||
(PyUnicodeObject *)intermediate
|
||||
#if PY_VERSION_HEX < 0x03020000
|
||||
(PyUnicodeObject *)
|
||||
#endif
|
||||
intermediate
|
||||
, &result[0]
|
||||
, result.size());
|
||||
|
||||
|
||||
@@ -44,11 +44,8 @@ BOOST_PYTHON_DECL rvalue_from_python_stage1_data rvalue_from_python_stage1(
|
||||
// First check to see if it's embedded in an extension class
|
||||
// instance, as a special case.
|
||||
data.convertible = objects::find_instance_impl(source, converters.target_type, converters.is_shared_ptr);
|
||||
if (data.convertible)
|
||||
{
|
||||
data.construct = 0;
|
||||
}
|
||||
else
|
||||
if (!data.convertible)
|
||||
{
|
||||
for (rvalue_from_python_chain const* chain = converters.rvalue_chain;
|
||||
chain != 0;
|
||||
|
||||
@@ -38,7 +38,7 @@ BOOST_PYTHON_DECL void scope_setattr_doc(char const* name, object const& x, char
|
||||
|
||||
#if PY_VERSION_HEX >= 0x03000000
|
||||
|
||||
PyObject* init_module(PyModuleDef& moduledef, void(*init_function)())
|
||||
BOOST_PYTHON_DECL PyObject* init_module(PyModuleDef& moduledef, void(*init_function)())
|
||||
{
|
||||
return init_module_in_scope(
|
||||
PyModule_Create(&moduledef),
|
||||
|
||||
@@ -76,11 +76,11 @@ extern "C"
|
||||
static int property_init(PyObject *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
|
||||
static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
|
||||
static const char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
|
||||
propertyobject *prop = (propertyobject *)self;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
|
||||
kwlist, &get, &set, &del, &doc))
|
||||
const_cast<char **>(kwlist), &get, &set, &del, &doc))
|
||||
return -1;
|
||||
|
||||
if (get == Py_None)
|
||||
@@ -303,7 +303,7 @@ static PyTypeObject class_metatype_object = {
|
||||
// object.
|
||||
void instance_holder::install(PyObject* self) throw()
|
||||
{
|
||||
assert(Py_TYPE(Py_TYPE(self)) == &class_metatype_object);
|
||||
assert(PyType_IsSubtype(Py_TYPE(Py_TYPE(self)), &class_metatype_object));
|
||||
m_next = ((objects::instance<>*)self)->objects;
|
||||
((objects::instance<>*)self)->objects = this;
|
||||
}
|
||||
@@ -482,7 +482,8 @@ namespace objects
|
||||
BOOST_PYTHON_DECL void*
|
||||
find_instance_impl(PyObject* inst, type_info type, bool null_shared_ptr_only)
|
||||
{
|
||||
if (Py_TYPE(Py_TYPE(inst)) != &class_metatype_object)
|
||||
if (!Py_TYPE(Py_TYPE(inst)) ||
|
||||
!PyType_IsSubtype(Py_TYPE(Py_TYPE(inst)), &class_metatype_object))
|
||||
return 0;
|
||||
|
||||
instance<>* self = reinterpret_cast<instance<>*>(inst);
|
||||
@@ -727,7 +728,7 @@ namespace objects
|
||||
|
||||
void* instance_holder::allocate(PyObject* self_, std::size_t holder_offset, std::size_t holder_size)
|
||||
{
|
||||
assert(Py_TYPE(Py_TYPE(self_)) == &class_metatype_object);
|
||||
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;
|
||||
@@ -752,7 +753,7 @@ void* instance_holder::allocate(PyObject* self_, std::size_t holder_offset, std:
|
||||
|
||||
void instance_holder::deallocate(PyObject* self_, void* storage) throw()
|
||||
{
|
||||
assert(Py_TYPE(Py_TYPE(self_)) == &class_metatype_object);
|
||||
assert(PyType_IsSubtype(Py_TYPE(Py_TYPE(self_)), &class_metatype_object));
|
||||
objects::instance<>* self = (objects::instance<>*)self_;
|
||||
if (storage != (char*)self + Py_SIZE(self))
|
||||
{
|
||||
|
||||
@@ -3,11 +3,15 @@
|
||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
import python ;
|
||||
import os ;
|
||||
|
||||
lib socket ;
|
||||
|
||||
use-project /boost/python : ../build ;
|
||||
project /boost/python/test
|
||||
: requirements
|
||||
<toolset>gcc:<cxxflags>-Wextra
|
||||
<target-os>qnxnto:<library>socket
|
||||
;
|
||||
|
||||
local PY = ;
|
||||
@@ -117,6 +121,7 @@ bpl-test crossmod_exception
|
||||
[ bpl-test defaults ]
|
||||
|
||||
[ bpl-test object ]
|
||||
[ bpl-test class ]
|
||||
[ bpl-test list ]
|
||||
[ bpl-test long ]
|
||||
[ bpl-test dict ]
|
||||
|
||||
28
test/class.cpp
Normal file
28
test/class.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
// 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>
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
struct X
|
||||
{
|
||||
int x;
|
||||
X(int n) : x(n) { }
|
||||
};
|
||||
|
||||
int x_function(X& x)
|
||||
{ return x.x;
|
||||
}
|
||||
|
||||
|
||||
BOOST_PYTHON_MODULE(class_ext)
|
||||
{
|
||||
class_<X>("X", init<int>());
|
||||
def("x_function", x_function);
|
||||
}
|
||||
|
||||
#include "module_tail.cpp"
|
||||
40
test/class.py
Executable file
40
test/class.py
Executable file
@@ -0,0 +1,40 @@
|
||||
# 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 class_ext import *
|
||||
|
||||
Ensure sanity:
|
||||
|
||||
>>> x = X(42)
|
||||
>>> x_function(x)
|
||||
42
|
||||
|
||||
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)
|
||||
>>> x_function(x)
|
||||
42
|
||||
|
||||
|
||||
'''
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user