From 1d065bf0dc7822a32632b474e14aa7c36146b427 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 11 May 2007 07:22:41 +0000 Subject: Copied over all of the C++ tests to Python again. --- src/py_utils.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'src/py_utils.cc') diff --git a/src/py_utils.cc b/src/py_utils.cc index ea439297..09fb9740 100644 --- a/src/py_utils.cc +++ b/src/py_utils.cc @@ -31,6 +31,7 @@ #include "pyinterp.h" #include "pyutils.h" +#include "pyfstream.h" #include #include @@ -74,6 +75,7 @@ struct bool_from_python typedef register_python_conversion bool_python_conversion; + struct string_to_python { static PyObject* convert(const string& str) @@ -103,10 +105,69 @@ struct string_from_python typedef register_python_conversion string_python_conversion; + +struct istream_to_python +{ + static PyObject* convert(const std::istream& str) + { + return incref(boost::python::detail::none()); + } +}; + +struct istream_from_python +{ + static void* convertible(PyObject* obj_ptr) + { + if (!PyFile_Check(obj_ptr)) return 0; + return obj_ptr; + } + + static void construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data) + { + void* storage = ((converter::rvalue_from_python_storage*) data)->storage.bytes; + new (storage) pyifstream(reinterpret_cast(obj_ptr)); + data->convertible = storage; + } +}; + +typedef register_python_conversion + istream_python_conversion; + + +struct ostream_to_python +{ + static PyObject* convert(const std::ostream& str) + { + return incref(boost::python::detail::none()); + } +}; + +struct ostream_from_python +{ + static void* convertible(PyObject* obj_ptr) + { + if (!PyFile_Check(obj_ptr)) return 0; + return obj_ptr; + } + + static void construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data) + { + void* storage = ((converter::rvalue_from_python_storage*) data)->storage.bytes; + new (storage) pyofstream(reinterpret_cast(obj_ptr)); + data->convertible = storage; + } +}; + +typedef register_python_conversion + ostream_python_conversion; + + void export_utils() { bool_python_conversion(); string_python_conversion(); + istream_python_conversion(); + ostream_python_conversion(); } } // namespace ledger -- cgit v1.2.3