diff options
author | Alexis Hildebrandt <afh@surryhill.net> | 2022-07-04 22:30:31 +0200 |
---|---|---|
committer | Martin Michlmayr <tbm@cyrius.com> | 2023-02-01 09:31:05 +0800 |
commit | adf9d22de4d5c422dcaeca022ea53880f7264be6 (patch) | |
tree | 48c99bde533a0118e8452bdc804dc8465469d398 /src | |
parent | ba33c732de8e272576066d7a22de5ebc01a355c9 (diff) | |
download | fork-ledger-adf9d22de4d5c422dcaeca022ea53880f7264be6.tar.gz fork-ledger-adf9d22de4d5c422dcaeca022ea53880f7264be6.tar.bz2 fork-ledger-adf9d22de4d5c422dcaeca022ea53880f7264be6.zip |
Remove support for Python 2.x
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/py_amount.cc | 22 | ||||
-rw-r--r-- | src/py_balance.cc | 3 | ||||
-rw-r--r-- | src/py_utils.cc | 106 | ||||
-rw-r--r-- | src/pyfstream.h | 202 | ||||
-rw-r--r-- | src/pyinterp.cc | 21 | ||||
-rw-r--r-- | src/pyutils.h | 5 |
7 files changed, 7 insertions, 353 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 30d97cb9..d847d71a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -117,7 +117,6 @@ set(LEDGER_INCLUDES print.h pstream.h ptree.h - pyfstream.h pyinterp.h pyutils.h query.h diff --git a/src/py_amount.cc b/src/py_amount.cc index 8561c9f5..fad66bde 100644 --- a/src/py_amount.cc +++ b/src/py_amount.cc @@ -33,9 +33,6 @@ #include "pyinterp.h" #include "pyutils.h" -#if PY_MAJOR_VERSION < 3 -#include "pyfstream.h" -#endif #include "commodity.h" #include "annotate.h" #include "pool.h" @@ -64,21 +61,6 @@ 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())); - amount.parse(instr, flags); - } else { - PyErr_SetString(PyExc_IOError, - _("Argument to amount.parse(file) is not a file object")); - } - } - 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); } @@ -290,10 +272,6 @@ 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 9bc491aa..824756ef 100644 --- a/src/py_balance.cc +++ b/src/py_balance.cc @@ -33,9 +33,6 @@ #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_utils.cc b/src/py_utils.cc index 8765bb78..94f6128f 100644 --- a/src/py_utils.cc +++ b/src/py_utils.cc @@ -33,9 +33,6 @@ #include "pyinterp.h" #include "pyutils.h" -#if PY_MAJOR_VERSION < 3 -#include "pyfstream.h" -#endif namespace ledger { @@ -90,49 +87,30 @@ struct string_from_python { static void* convertible(PyObject* obj_ptr) { - if (!PyUnicode_Check(obj_ptr) -#if PY_MAJOR_VERSION < 3 - && !PyString_Check(obj_ptr) -#endif - ) return 0; + if (!PyUnicode_Check(obj_ptr)) + 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(); - void* storage = - reinterpret_cast<converter::rvalue_from_python_storage<string> *> - (data)->storage.bytes; - new (storage) string(value); - data->convertible = storage; - return; - } -#endif VERIFY(PyUnicode_Check(obj_ptr)); - string str; -#if PY_MAJOR_VERSION < 3 Py_ssize_t size = PyUnicode_GET_SIZE(obj_ptr); const Py_UNICODE* value = PyUnicode_AS_UNICODE(obj_ptr); + + string str; #if Py_UNICODE_SIZE == 2 // UTF-16 utf8::unchecked::utf16to8(value, value + size, std::back_inserter(str)); #elif Py_UNICODE_SIZE == 4 // UTF-32 utf8::unchecked::utf32to8(value, value + size, std::back_inserter(str)); #else assert("Py_UNICODE has an unexpected size" == NULL); -#endif // Py_UNICODE_SIZE -#else // PY_MAJOR_VERSION >= 3 - Py_ssize_t size = -#if PY_MINOR_VERSION >= 3 - PyUnicode_GET_LENGTH(obj_ptr); -#else - PyUnicode_GET_SIZE(obj_ptr); #endif + + VERIFY(PyUnicode_Check(obj_ptr)); + #if PY_MINOR_VERSION < 12 if (PyUnicode_READY(obj_ptr)) return; @@ -162,7 +140,6 @@ struct string_from_python default: assert("PyUnicode_KIND returned an unexpected kind" == NULL); } -#endif // PY_MAJOR_VERSION void* storage = reinterpret_cast<converter::rvalue_from_python_storage<string> *> @@ -175,71 +152,6 @@ struct string_from_python 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&) - { - 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 = - reinterpret_cast<converter::rvalue_from_python_storage<pyifstream> *> - (data)->storage.bytes; - new (storage) pyifstream(reinterpret_cast<PyFileObject *>(obj_ptr)); - data->convertible = storage; - } -}; - -typedef register_python_conversion<std::istream, - istream_to_python, istream_from_python> - istream_python_conversion; - - -struct ostream_to_python -{ - static PyObject* convert(const std::ostream&) - { - 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 = - reinterpret_cast<converter::rvalue_from_python_storage<pyofstream> *> - (data)->storage.bytes; - new (storage) pyofstream(reinterpret_cast<PyFileObject *>(obj_ptr)); - data->convertible = storage; - } -}; - -typedef register_python_conversion<std::ostream, - ostream_to_python, ostream_from_python> - ostream_python_conversion; -#endif - void export_utils() { class_< supports_flags<uint_least8_t> > ("SupportFlags8") @@ -291,10 +203,6 @@ 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/pyfstream.h b/src/pyfstream.h deleted file mode 100644 index 2ff3fa0b..00000000 --- a/src/pyfstream.h +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (c) 2003-2022, John Wiegley. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of New Artisans LLC nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef INCLUDED_PYFSTREAM_H -#define INCLUDED_PYFSTREAM_H - -// pyofstream -// - a stream that writes on a Python file object - -class pyoutbuf : public boost::noncopyable, public std::streambuf -{ - pyoutbuf(); - -protected: - PyFileObject * fo; // Python file object - -public: - // constructor - pyoutbuf(PyFileObject * _fo) : fo(_fo) { - TRACE_CTOR(pyoutbuf, "PyFileObject *"); - } - ~pyoutbuf() throw() { - TRACE_DTOR(pyoutbuf); - } - -protected: - // write one character - virtual int_type overflow (int_type c) { - if (c != EOF) { - char z[2]; - z[0] = static_cast<char>(c); - z[1] = '\0'; - if (PyFile_WriteString(z, reinterpret_cast<PyObject *>(fo)) < 0) { - return EOF; - } - } - return c; - } - - // write multiple characters - virtual std::streamsize xsputn (const char* s, std::streamsize num) { - char * buf = new char[num + 1]; - std::strncpy(buf, s, static_cast<std::size_t>(num)); - buf[num] = '\0'; - if (PyFile_WriteString(buf, reinterpret_cast<PyObject *>(fo)) < 0) - num = 0; - boost::checked_array_delete(buf); - return num; - } -}; - -class pyofstream : public boost::noncopyable, public std::ostream -{ - pyofstream(); - -protected: - pyoutbuf buf; - -public: - pyofstream (PyFileObject * fo) : std::ostream(0), buf(fo) { - rdbuf(&buf); - TRACE_CTOR(pyofstream, "PyFileObject *"); - } - ~pyofstream() throw() { - TRACE_DTOR(pyofstream); - } -}; - -// pyifstream -// - a stream that reads on a file descriptor - -class pyinbuf : public boost::noncopyable, public std::streambuf -{ - pyinbuf(); - -protected: - PyFileObject * fo; // Python file object - -protected: - /* data buffer: - * - at most, pbSize characters in putback area plus - * - at most, bufSize characters in ordinary read buffer - */ - static const size_t pbSize = 4; // size of putback area - static const size_t bufSize = 1024; // size of the data buffer - char buffer[bufSize + pbSize]; // data buffer - -public: - /* constructor - * - initialize file descriptor - * - initialize empty data buffer - * - no putback area - * => force underflow() - */ - pyinbuf (PyFileObject * _fo) : fo(_fo) { - setg (buffer+pbSize, // beginning of putback area - buffer+pbSize, // read position - buffer+pbSize); // end position - - TRACE_CTOR(pyinbuf, "PyFileObject *"); - } - ~pyinbuf() throw() { - TRACE_DTOR(pyinbuf); - } - -protected: - // insert new characters into the buffer - virtual int_type underflow () { -#ifndef _MSC_VER - using std::memmove; -#endif - - // is read position before end of buffer? - if (gptr() < egptr()) { - return traits_type::to_int_type(*gptr()); - } - - /* process size of putback area - * - use number of characters read - * - but at most size of putback area - */ - size_t numPutback; - numPutback = static_cast<size_t>(gptr() - eback()); - if (numPutback > pbSize) { - numPutback = pbSize; - } - - /* copy up to pbSize characters previously read into - * the putback area - */ - memmove (buffer+(pbSize-numPutback), gptr()-numPutback, - numPutback); - - // read at most bufSize new characters - PyObject *line = PyFile_GetLine(reinterpret_cast<PyObject *>(fo), bufSize); - if (! line || ! PyString_Check(line)) { - // ERROR or EOF - return EOF; - } - - Py_ssize_t num = PyString_Size(line); - if (num == 0) - return EOF; - - memmove(buffer+pbSize, PyString_AsString(line), static_cast<size_t>(num)); - - // reset buffer pointers - setg (buffer+(pbSize-numPutback), // beginning of putback area - buffer+pbSize, // read position - buffer+pbSize+num); // end of buffer - - // return next character - return traits_type::to_int_type(*gptr()); - } -}; - -class pyifstream : public boost::noncopyable, public std::istream -{ - pyifstream(); - -protected: - pyinbuf buf; - -public: - pyifstream (PyFileObject * fo) : std::istream(0), buf(fo) { - rdbuf(&buf); - TRACE_CTOR(pyifstream, "PyFileObject *"); - } - ~pyifstream() throw() { - TRACE_DTOR(pyifstream); - } -}; - -#endif // INCLUDED_PYFSTREAM_H diff --git a/src/pyinterp.cc b/src/pyinterp.cc index ed94de84..6508a071 100644 --- a/src/pyinterp.cc +++ b/src/pyinterp.cc @@ -61,9 +61,7 @@ void export_utils(); void export_value(); void export_xact(); -#if PY_MAJOR_VERSION >= 3 extern "C" PyObject* PyInit_ledger(); -#endif void initialize_for_python() { @@ -150,7 +148,6 @@ void python_interpreter_t::initialize() try { DEBUG("python.interp", "Initializing Python"); -#if PY_MAJOR_VERSION >= 3 // Unbuffer stdio to avoid python output getting stuck in buffer when // stdout is not a TTY. Normally buffers are flushed by Py_Finalize but // Boost has a long-standing issue preventing proper shutdown of the @@ -158,7 +155,6 @@ void python_interpreter_t::initialize() Py_UnbufferedStdioFlag = 1; // PyImport_AppendInittab docs: "This should be called before Py_Initialize()". PyImport_AppendInittab((const char*)"ledger", PyInit_ledger); -#endif Py_Initialize(); assert(Py_IsInitialized()); @@ -166,11 +162,7 @@ void python_interpreter_t::initialize() hack_system_paths(); main_module = import_module("__main__"); -#if PY_MAJOR_VERSION >= 3 PyImport_ImportModule("ledger"); -#else - python::detail::init_module("ledger", &initialize_for_python); -#endif is_initialized = true; } @@ -330,7 +322,6 @@ 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]; std::size_t len = std::strlen(argv0) + 1; @@ -343,18 +334,6 @@ value_t python_interpreter_t::python_command(call_scope_t& args) argv[i + 1] = new wchar_t[len]; mbstowcs(argv[i + 1], arg.c_str(), len); } -#else - char ** argv = new char *[args.size() + 1]; - - argv[0] = new char[std::strlen(argv0) + 1]; - std::strcpy(argv[0], argv0); - - for (std::size_t i = 0; i < args.size(); i++) { - string arg = args.get<string>(i); - 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 7bc0d0af..41a91c64 100644 --- a/src/pyutils.h +++ b/src/pyutils.h @@ -131,12 +131,7 @@ 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(); } |