diff options
author | John Wiegley <johnw@newartisans.com> | 2009-11-10 18:44:08 -0500 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-11-10 18:44:08 -0500 |
commit | ac885a907525589aa56266d9a2527cdc7127c9cb (patch) | |
tree | d95c1cd20cb7df81b643f35c840c379fcae3f547 /src/pyutils.h | |
parent | 0ac2dc28104253faf82a3ed3d12bb16a1a150067 (diff) | |
download | fork-ledger-ac885a907525589aa56266d9a2527cdc7127c9cb.tar.gz fork-ledger-ac885a907525589aa56266d9a2527cdc7127c9cb.tar.bz2 fork-ledger-ac885a907525589aa56266d9a2527cdc7127c9cb.zip |
All strings passed to Python are now Unicode objects
Diffstat (limited to 'src/pyutils.h')
-rw-r--r-- | src/pyutils.h | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/src/pyutils.h b/src/pyutils.h index 5709eb35..a9e968e0 100644 --- a/src/pyutils.h +++ b/src/pyutils.h @@ -36,7 +36,7 @@ template <typename T, typename TfromPy> struct object_from_python { object_from_python() { - boost::python::converter::registry::push_back + boost::python::converter::registry::insert (&TfromPy::convertible, &TfromPy::construct, boost::python::type_id<T>()); } @@ -106,6 +106,55 @@ struct register_optional_to_python : public boost::noncopyable } }; +namespace boost { namespace python { + +// Use expr to create the PyObject corresponding to x +# define BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T, expr, pytype)\ + template <> struct to_python_value<T&> \ + : detail::builtin_to_python \ + { \ + inline PyObject* operator()(T const& x) const \ + { \ + return (expr); \ + } \ + inline PyTypeObject const* get_pytype() const \ + { \ + return (pytype); \ + } \ + }; \ + template <> struct to_python_value<T const&> \ + : detail::builtin_to_python \ + { \ + inline PyObject* operator()(T const& x) const \ + { \ + return (expr); \ + } \ + inline PyTypeObject const* get_pytype() const \ + { \ + return (pytype); \ + } \ + }; + +# define BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T, expr) \ + namespace converter \ + { \ + template <> struct arg_to_python< T > \ + : handle<> \ + { \ + arg_to_python(T const& x) \ + : python::handle<>(expr) {} \ + }; \ + } + +// Specialize argument and return value converters for T using expr +# define BOOST_PYTHON_TO_PYTHON_BY_VALUE(T, expr, pytype) \ + BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T,expr, pytype) \ + BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T,expr) + +BOOST_PYTHON_TO_PYTHON_BY_VALUE(ledger::string, ::PyUnicode_FromEncodedObject(::PyString_FromString(x.c_str()), "UTF-8", NULL), &PyUnicode_Type) + +} } // namespace boost::python + //boost::python::register_ptr_to_python< boost::shared_ptr<Base> >(); #endif // _PY_UTILS_H |