diff options
author | Dimitri John Ledkov <xnox@ubuntu.com> | 2019-12-03 22:59:35 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2019-12-05 15:06:44 +0100 |
commit | 12a74c66c6656bbf6a89bfae83b76e3df37d9199 (patch) | |
tree | 189bc4543ff839662d4032dbf66cd2e283f17829 | |
parent | 23d818175a81e46a899c5e8b00be54cfa0881a4e (diff) | |
download | fork-ledger-12a74c66c6656bbf6a89bfae83b76e3df37d9199.tar.gz fork-ledger-12a74c66c6656bbf6a89bfae83b76e3df37d9199.tar.bz2 fork-ledger-12a74c66c6656bbf6a89bfae83b76e3df37d9199.zip |
Port to python3
-rw-r--r-- | src/py_amount.cc | 6 | ||||
-rw-r--r-- | src/py_balance.cc | 2 | ||||
-rw-r--r-- | src/py_times.cc | 18 | ||||
-rw-r--r-- | src/py_utils.cc | 19 | ||||
-rw-r--r-- | src/py_value.cc | 2 | ||||
-rw-r--r-- | src/pyinterp.cc | 28 | ||||
-rw-r--r-- | src/pyutils.h | 4 | ||||
-rw-r--r-- | src/system.hh.in | 1 |
8 files changed, 63 insertions, 17 deletions
diff --git a/src/py_amount.cc b/src/py_amount.cc index 2d99ddec..f1096363 100644 --- a/src/py_amount.cc +++ b/src/py_amount.cc @@ -33,7 +33,9 @@ #include "pyinterp.h" #include "pyutils.h" +#if PY_MAJOR_VERSION < 3 #include "pyfstream.h" +#endif #include "commodity.h" #include "annotate.h" #include "pool.h" @@ -62,6 +64,7 @@ namespace { return amount.value(datetime_t(moment), in_terms_of); } +#if PY_MAJOR_VERSION < 3 void py_parse_2(amount_t& amount, object in, unsigned char flags) { if (PyFile_Check(in.ptr())) { pyifstream instr(reinterpret_cast<PyFileObject *>(in.ptr())); @@ -74,6 +77,7 @@ namespace { void py_parse_1(amount_t& amount, object in) { py_parse_2(amount, in, 0); } +#endif void py_parse_str_1(amount_t& amount, const string& str) { amount.parse(str); @@ -286,8 +290,10 @@ internal precision.")) .def("strip_annotations", py_strip_annotations_0) .def("strip_annotations", py_strip_annotations_1) +#if PY_MAJOR_VERSION < 3 .def("parse", py_parse_1) .def("parse", py_parse_2) +#endif .def("parse", py_parse_str_1) .def("parse", py_parse_str_2) diff --git a/src/py_balance.cc b/src/py_balance.cc index dc9975cf..3067850e 100644 --- a/src/py_balance.cc +++ b/src/py_balance.cc @@ -33,7 +33,9 @@ #include "pyinterp.h" #include "pyutils.h" +#if PY_MAJOR_VERSION < 3 #include "pyfstream.h" +#endif #include "commodity.h" #include "annotate.h" #include "balance.h" diff --git a/src/py_times.cc b/src/py_times.cc index bbd6f69a..6b2df443 100644 --- a/src/py_times.cc +++ b/src/py_times.cc @@ -30,6 +30,7 @@ */ #include <system.hh> +#include <datetime.h> #include "pyinterp.h" #include "pyutils.h" @@ -41,16 +42,11 @@ namespace ledger { using namespace boost::python; -#define MY_PyDateTime_IMPORT \ - PyDateTimeAPI = (PyDateTime_CAPI*) \ - PyCObject_Import(const_cast<char *>("datetime"), \ - const_cast<char *>("datetime_CAPI")) - struct date_to_python { static PyObject* convert(const date_t& dte) { - MY_PyDateTime_IMPORT; + PyDateTime_IMPORT; return PyDate_FromDate(dte.year(), dte.month(), dte.day()); } }; @@ -59,7 +55,7 @@ struct date_from_python { static void* convertible(PyObject* obj_ptr) { - MY_PyDateTime_IMPORT; + PyDateTime_IMPORT; if (PyDate_Check(obj_ptr)) return obj_ptr; return 0; } @@ -67,7 +63,7 @@ struct date_from_python static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data) { - MY_PyDateTime_IMPORT; + PyDateTime_IMPORT; int year = PyDateTime_GET_YEAR(obj_ptr); date::year_type y = gregorian::greg_year(static_cast<unsigned short>(year)); @@ -90,7 +86,7 @@ struct datetime_to_python { static PyObject* convert(const datetime_t& moment) { - MY_PyDateTime_IMPORT; + PyDateTime_IMPORT; date_t dte = moment.date(); datetime_t::time_duration_type tod = moment.time_of_day(); @@ -107,7 +103,7 @@ struct datetime_from_python { static void* convertible(PyObject* obj_ptr) { - MY_PyDateTime_IMPORT; + PyDateTime_IMPORT; if (PyDateTime_Check(obj_ptr)) return obj_ptr; return 0; } @@ -115,7 +111,7 @@ struct datetime_from_python static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data) { - MY_PyDateTime_IMPORT; + PyDateTime_IMPORT; int year = PyDateTime_GET_YEAR(obj_ptr); date::year_type y = gregorian::greg_year(static_cast<unsigned short>(year)); diff --git a/src/py_utils.cc b/src/py_utils.cc index c2f84f55..419af734 100644 --- a/src/py_utils.cc +++ b/src/py_utils.cc @@ -33,7 +33,9 @@ #include "pyinterp.h" #include "pyutils.h" +#if PY_MAJOR_VERSION < 3 #include "pyfstream.h" +#endif namespace ledger { @@ -88,14 +90,18 @@ struct string_from_python { static void* convertible(PyObject* obj_ptr) { - if (!PyUnicode_Check(obj_ptr) && - !PyString_Check(obj_ptr)) return 0; + if (!PyUnicode_Check(obj_ptr) +#if PY_MAJOR_VERSION < 3 + && !PyString_Check(obj_ptr) +#endif + ) return 0; return obj_ptr; } static void construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data) { +#if PY_MAJOR_VERSION < 3 if (PyString_Check(obj_ptr)) { const char* value = PyString_AsString(obj_ptr); if (value == 0) throw_error_already_set(); @@ -105,6 +111,7 @@ struct string_from_python new (storage) string(value); data->convertible = storage; } else { +#endif VERIFY(PyUnicode_Check(obj_ptr)); Py_ssize_t size = PyUnicode_GET_SIZE(obj_ptr); @@ -125,14 +132,16 @@ struct string_from_python (data)->storage.bytes; new (storage) string(str); data->convertible = storage; +#if PY_MAJOR_VERSION < 3 } +#endif } }; typedef register_python_conversion<string, string_to_python, string_from_python> string_python_conversion; - +#if PY_MAJOR_VERSION < 3 struct istream_to_python { static PyObject* convert(const std::istream&) @@ -195,7 +204,7 @@ struct ostream_from_python typedef register_python_conversion<std::ostream, ostream_to_python, ostream_from_python> ostream_python_conversion; - +#endif void export_utils() { @@ -248,8 +257,10 @@ void export_utils() bool_python_conversion(); string_python_conversion(); +#if PY_MAJOR_VERSION < 3 istream_python_conversion(); ostream_python_conversion(); +#endif } } // namespace ledger diff --git a/src/py_value.cc b/src/py_value.cc index 486228c0..b0754896 100644 --- a/src/py_value.cc +++ b/src/py_value.cc @@ -71,7 +71,7 @@ namespace { return (PyObject *)&PyBool_Type; } else if (value.is_long()) { - return (PyObject *)&PyInt_Type; + return (PyObject *)&PyLong_Type; } else if (value.is_string()) { return (PyObject *)&PyUnicode_Type; diff --git a/src/pyinterp.cc b/src/pyinterp.cc index fad0b559..9ae37687 100644 --- a/src/pyinterp.cc +++ b/src/pyinterp.cc @@ -153,7 +153,22 @@ void python_interpreter_t::initialize() main_module = import_module("__main__"); +#if PY_MAJOR_VERSION >= 3 + static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "ledger", /* m_name */ + NULL, /* m_doc */ + -1, /* m_size */ + NULL, /* m_methods */ + NULL, /* m_reload */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL, /* m_free */ + }; + python::detail::init_module(moduledef, &initialize_for_python); +#else python::detail::init_module("ledger", &initialize_for_python); +#endif is_initialized = true; } @@ -313,6 +328,18 @@ value_t python_interpreter_t::python_command(call_scope_t& args) if (! is_initialized) initialize(); +#if PY_MAJOR_VERSION >= 3 + wchar_t ** argv = new wchar_t *[args.size() + 1]; + + argv[0] = new wchar_t[std::strlen(argv0) + 1]; + mbstowcs(argv[0], argv0, std::strlen(argv0)); + + for (std::size_t i = 0; i < args.size(); i++) { + string arg = args.get<string>(i); + argv[i + 1] = new wchar_t[arg.length() + 1]; + mbstowcs(argv[0], arg.c_str(), std::strlen(arg.c_str())); + } +#else char ** argv = new char *[args.size() + 1]; argv[0] = new char[std::strlen(argv0) + 1]; @@ -323,6 +350,7 @@ value_t python_interpreter_t::python_command(call_scope_t& args) argv[i + 1] = new char[arg.length() + 1]; std::strcpy(argv[i + 1], arg.c_str()); } +#endif int status = 1; diff --git a/src/pyutils.h b/src/pyutils.h index 641dbd3b..323ffe02 100644 --- a/src/pyutils.h +++ b/src/pyutils.h @@ -130,8 +130,12 @@ template <typename T> PyObject * str_to_py_unicode(const T& str) { using namespace boost::python; +#if PY_MAJOR_VERSION >= 3 + PyObject * uni = PyUnicode_FromString(str.c_str()); +#else PyObject * pstr = PyString_FromString(str.c_str()); PyObject * uni = PyUnicode_FromEncodedObject(pstr, "UTF-8", NULL); +#endif return object(handle<>(borrowed(uni))).ptr(); } diff --git a/src/system.hh.in b/src/system.hh.in index 3ee74be2..2f0f6e79 100644 --- a/src/system.hh.in +++ b/src/system.hh.in @@ -207,7 +207,6 @@ #include <boost/python.hpp> #include <boost/python/detail/wrap_python.hpp> -#include <datetime.h> #include <boost/python/module_init.hpp> #include <boost/python/suite/indexing/vector_indexing_suite.hpp> |