From 2ae7f58a26106a0714727f9f78ef59dbb94235f4 Mon Sep 17 00:00:00 2001 From: Levak Borok Date: Fri, 21 Jun 2024 11:21:04 +0200 Subject: [PATCH] Fix Long Numeric Integer representation for Python 3 Before Python 3, long integers were input with an L suffix. Since Python 3, all integers are 64-bit and do not need the L suffix. --- bytecode.cpp | 2 +- pyc_numeric.cpp | 7 ++++--- pyc_numeric.h | 2 +- pycdas.cpp | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bytecode.cpp b/bytecode.cpp index 0d780fb76..1797175fa 100644 --- a/bytecode.cpp +++ b/bytecode.cpp @@ -230,7 +230,7 @@ void print_const(std::ostream& pyc_output, PycRef obj, PycModule* mod formatted_print(pyc_output, "%d", obj.cast()->value()); break; case PycObject::TYPE_LONG: - formatted_print(pyc_output, "%s", obj.cast()->repr().c_str()); + formatted_print(pyc_output, "%s", obj.cast()->repr(mod).c_str()); break; case PycObject::TYPE_FLOAT: formatted_print(pyc_output, "%s", obj.cast()->value()); diff --git a/pyc_numeric.cpp b/pyc_numeric.cpp index 4f6d3c448..d70ffaf4b 100644 --- a/pyc_numeric.cpp +++ b/pyc_numeric.cpp @@ -53,13 +53,13 @@ bool PycLong::isEqual(PycRef obj) const return true; } -std::string PycLong::repr() const +std::string PycLong::repr(PycModule* mod) const { // Longs are printed as hex, since it's easier (and faster) to convert // arbitrary-length integers to a power of two than an arbitrary base if (m_size == 0) - return "0x0L"; + return (mod->verCompare(3, 0) >= 0) ? "0x0" : "0x0L"; // Realign to 32 bits, since Python uses only 15 std::vector bits; @@ -90,7 +90,8 @@ std::string PycLong::repr() const aptr += snprintf(aptr, 9, "%X", *iter++); while (iter != bits.rend()) aptr += snprintf(aptr, 9, "%08X", *iter++); - *aptr++ = 'L'; + if (mod->verCompare(3, 0) < 0) + *aptr++ = 'L'; *aptr = 0; return accum; } diff --git a/pyc_numeric.h b/pyc_numeric.h index 46ddb98be..548afcd0e 100644 --- a/pyc_numeric.h +++ b/pyc_numeric.h @@ -37,7 +37,7 @@ class PycLong : public PycObject { int size() const { return m_size; } const std::vector& value() const { return m_value; } - std::string repr() const; + std::string repr(PycModule* mod) const; private: int m_size; diff --git a/pycdas.cpp b/pycdas.cpp index ddc210b6a..3f079ab92 100644 --- a/pycdas.cpp +++ b/pycdas.cpp @@ -223,7 +223,7 @@ void output_object(PycRef obj, PycModule* mod, int indent, iprintf(pyc_output, indent, "%d\n", obj.cast()->value()); break; case PycObject::TYPE_LONG: - iprintf(pyc_output, indent, "%s\n", obj.cast()->repr().c_str()); + iprintf(pyc_output, indent, "%s\n", obj.cast()->repr(mod).c_str()); break; case PycObject::TYPE_FLOAT: iprintf(pyc_output, indent, "%s\n", obj.cast()->value());