From e73277e156c0df2347139c53348fd6cc7f8fe7fc Mon Sep 17 00:00:00 2001 From: Haoyu Bai Date: Tue, 26 May 2009 17:05:53 +0000 Subject: [PATCH] more fixes to get testcases work, includes module init and other minor fixes [SVN r53285] --- include/boost/python/enum.hpp | 4 ++ include/boost/python/module_init.hpp | 37 ++++++++++++------- include/boost/python/object/make_instance.hpp | 2 +- src/exec.cpp | 10 ++++- src/object_operators.cpp | 8 ++++ src/object_protocol.cpp | 10 ++++- 6 files changed, 53 insertions(+), 18 deletions(-) diff --git a/include/boost/python/enum.hpp b/include/boost/python/enum.hpp index 57684530..050643a4 100644 --- a/include/boost/python/enum.hpp +++ b/include/boost/python/enum.hpp @@ -79,7 +79,11 @@ void* enum_::convertible_from_python(PyObject* obj) template void enum_::construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data) { +#if PY_VERSION_HEX >= 0x03000000 + T x = static_cast(PyLong_AS_LONG(obj)); +#else T x = static_cast(PyInt_AS_LONG(obj)); +#endif void* const storage = ((converter::rvalue_from_python_storage*)data)->storage.bytes; new (storage) T(x); data->convertible = storage; diff --git a/include/boost/python/module_init.hpp b/include/boost/python/module_init.hpp index 0d53b5e1..99dddec6 100644 --- a/include/boost/python/module_init.hpp +++ b/include/boost/python/module_init.hpp @@ -18,36 +18,45 @@ BOOST_PYTHON_DECL PyObject* init_module(char const* name, void(*)()); // TODO(bhy) Take care of this later. // But any reseaon we don't use BOOST_PYTHON_DECL here? -# if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(BOOST_PYTHON_STATIC_MODULE) +# if PY_VERSION_HEX >= 0x03000000 -# define BOOST_PYTHON_MODULE_INIT(name) \ -void init_module_##name(); \ -extern "C" __declspec(dllexport) void init##name() \ +# define _BOOST_PYTHON_MODULE_INIT(name) \ +PyObject* PyInit_##name() \ +{ \ + return boost::python::detail::init_module( \ + #name,&init_module_##name); \ +} \ +void init_module_##name() + +# else + +# define _BOOST_PYTHON_MODULE_INIT(name) \ +void init##name() \ { \ boost::python::detail::init_module( \ #name,&init_module_##name); \ } \ void init_module_##name() +# endif + +# if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(BOOST_PYTHON_STATIC_MODULE) + +# define BOOST_PYTHON_MODULE_INIT(name) \ +void init_module_##name(); \ +extern "C" __declspec(dllexport) _BOOST_PYTHON_MODULE_INIT(name) + # elif BOOST_PYTHON_USE_GCC_SYMBOL_VISIBILITY # define BOOST_PYTHON_MODULE_INIT(name) \ void init_module_##name(); \ -extern "C" __attribute__ ((visibility("default"))) void init##name() \ -{ \ - boost::python::detail::init_module(#name, &init_module_##name); \ -} \ -void init_module_##name() +extern "C" __attribute__ ((visibility("default"))) _BOOST_PYTHON_MODULE_INIT(name) # else # define BOOST_PYTHON_MODULE_INIT(name) \ void init_module_##name(); \ -extern "C" void init##name() \ -{ \ - boost::python::detail::init_module(#name, &init_module_##name); \ -} \ -void init_module_##name() +extern "C" _BOOST_PYTHON_MODULE_INIT(name) # endif diff --git a/include/boost/python/object/make_instance.hpp b/include/boost/python/object/make_instance.hpp index 55771eca..8f355fe2 100644 --- a/include/boost/python/object/make_instance.hpp +++ b/include/boost/python/object/make_instance.hpp @@ -43,7 +43,7 @@ struct make_instance_impl // Note the position of the internally-stored Holder, // for the sake of destruction - instance->ob_size = offsetof(instance_t, storage); + Py_SIZE(instance) = offsetof(instance_t, storage); // Release ownership of the python object protect.cancel(); diff --git a/src/exec.cpp b/src/exec.cpp index 6c30797a..8e269543 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -38,11 +38,19 @@ object BOOST_PYTHON_DECL exec_file(str filename, object global, object local) { // should be 'char const *' but older python versions don't use 'const' yet. char *f = python::extract(filename); +#if PY_VERSION_HEX >= 0x03000000 + // TODO(bhy) temporary workaround for Python 3. + // should figure out a way to avoid binary incompatibilities as the Python 2 + // version did. + FILE *fs = fopen(f, "r"); +#else // Let python open the file to avoid potential binary incompatibilities. PyObject *pyfile = PyFile_FromString(f, const_cast("r")); if (!pyfile) throw std::invalid_argument(std::string(f) + " : no such file"); python::handle<> file(pyfile); - PyObject* result = PyRun_File(PyFile_AsFile(file.get()), + FILE *fs = PyFile_AsFile(file.get()); +#endif + PyObject* result = PyRun_File(fs, f, Py_file_input, global.ptr(), local.ptr()); diff --git a/src/object_operators.cpp b/src/object_operators.cpp index b6f1c5fb..9828de70 100644 --- a/src/object_operators.cpp +++ b/src/object_operators.cpp @@ -38,7 +38,11 @@ BOOST_PYTHON_DECL object operator op(object const& l, object const& r) \ BOOST_PYTHON_BINARY_OPERATOR(+, Add) BOOST_PYTHON_BINARY_OPERATOR(-, Subtract) BOOST_PYTHON_BINARY_OPERATOR(*, Multiply) +#if PY_VERSION_HEX >= 0x03000000 +BOOST_PYTHON_BINARY_OPERATOR(/, TrueDivide) +#else BOOST_PYTHON_BINARY_OPERATOR(/, Divide) +#endif BOOST_PYTHON_BINARY_OPERATOR(%, Remainder) BOOST_PYTHON_BINARY_OPERATOR(<<, Lshift) BOOST_PYTHON_BINARY_OPERATOR(>>, Rshift) @@ -58,7 +62,11 @@ BOOST_PYTHON_DECL object& operator op##=(object& l, object const& r) \ BOOST_PYTHON_INPLACE_OPERATOR(+, Add) BOOST_PYTHON_INPLACE_OPERATOR(-, Subtract) BOOST_PYTHON_INPLACE_OPERATOR(*, Multiply) +#if PY_VERSION_HEX >= 0x03000000 +BOOST_PYTHON_INPLACE_OPERATOR(/, TrueDivide) +#else BOOST_PYTHON_INPLACE_OPERATOR(/, Divide) +#endif BOOST_PYTHON_INPLACE_OPERATOR(%, Remainder) BOOST_PYTHON_INPLACE_OPERATOR(<<, Lshift) BOOST_PYTHON_INPLACE_OPERATOR(>>, Rshift) diff --git a/src/object_protocol.cpp b/src/object_protocol.cpp index 8606fa2b..95c8c73e 100644 --- a/src/object_protocol.cpp +++ b/src/object_protocol.cpp @@ -103,6 +103,7 @@ namespace // slicing code copied directly out of the Python implementation static PyObject * apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */ { +#if PY_VERSION_HEX < 0x03000000 PyTypeObject *tp = u->ob_type; PySequenceMethods *sq = tp->tp_as_sequence; @@ -114,7 +115,9 @@ namespace // slicing code copied directly out of the Python implementation return NULL; return PySequence_GetSlice(u, ilow, ihigh); } - else { + else +#endif + { PyObject *slice = PySlice_New(v, w, NULL); if (slice != NULL) { PyObject *res = PyObject_GetItem(u, slice); @@ -130,6 +133,7 @@ namespace // slicing code copied directly out of the Python implementation assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) /* u[v:w] = x */ { +#if PY_VERSION_HEX < 0x03000000 PyTypeObject *tp = u->ob_type; PySequenceMethods *sq = tp->tp_as_sequence; @@ -144,7 +148,9 @@ namespace // slicing code copied directly out of the Python implementation else return PySequence_SetSlice(u, ilow, ihigh, x); } - else { + else +#endif + { PyObject *slice = PySlice_New(v, w, NULL); if (slice != NULL) { int res;