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.
This commit is contained in:
Levak Borok
2024-06-21 11:21:04 +02:00
parent 513e03c883
commit 2ae7f58a26
4 changed files with 7 additions and 6 deletions

View File

@@ -230,7 +230,7 @@ void print_const(std::ostream& pyc_output, PycRef<PycObject> obj, PycModule* mod
formatted_print(pyc_output, "%d", obj.cast<PycInt>()->value()); formatted_print(pyc_output, "%d", obj.cast<PycInt>()->value());
break; break;
case PycObject::TYPE_LONG: case PycObject::TYPE_LONG:
formatted_print(pyc_output, "%s", obj.cast<PycLong>()->repr().c_str()); formatted_print(pyc_output, "%s", obj.cast<PycLong>()->repr(mod).c_str());
break; break;
case PycObject::TYPE_FLOAT: case PycObject::TYPE_FLOAT:
formatted_print(pyc_output, "%s", obj.cast<PycFloat>()->value()); formatted_print(pyc_output, "%s", obj.cast<PycFloat>()->value());

View File

@@ -53,13 +53,13 @@ bool PycLong::isEqual(PycRef<PycObject> obj) const
return true; 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 // 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 // arbitrary-length integers to a power of two than an arbitrary base
if (m_size == 0) if (m_size == 0)
return "0x0L"; return (mod->verCompare(3, 0) >= 0) ? "0x0" : "0x0L";
// Realign to 32 bits, since Python uses only 15 // Realign to 32 bits, since Python uses only 15
std::vector<unsigned> bits; std::vector<unsigned> bits;
@@ -90,7 +90,8 @@ std::string PycLong::repr() const
aptr += snprintf(aptr, 9, "%X", *iter++); aptr += snprintf(aptr, 9, "%X", *iter++);
while (iter != bits.rend()) while (iter != bits.rend())
aptr += snprintf(aptr, 9, "%08X", *iter++); aptr += snprintf(aptr, 9, "%08X", *iter++);
*aptr++ = 'L'; if (mod->verCompare(3, 0) < 0)
*aptr++ = 'L';
*aptr = 0; *aptr = 0;
return accum; return accum;
} }

View File

@@ -37,7 +37,7 @@ public:
int size() const { return m_size; } int size() const { return m_size; }
const std::vector<int>& value() const { return m_value; } const std::vector<int>& value() const { return m_value; }
std::string repr() const; std::string repr(PycModule* mod) const;
private: private:
int m_size; int m_size;

View File

@@ -223,7 +223,7 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent,
iprintf(pyc_output, indent, "%d\n", obj.cast<PycInt>()->value()); iprintf(pyc_output, indent, "%d\n", obj.cast<PycInt>()->value());
break; break;
case PycObject::TYPE_LONG: case PycObject::TYPE_LONG:
iprintf(pyc_output, indent, "%s\n", obj.cast<PycLong>()->repr().c_str()); iprintf(pyc_output, indent, "%s\n", obj.cast<PycLong>()->repr(mod).c_str());
break; break;
case PycObject::TYPE_FLOAT: case PycObject::TYPE_FLOAT:
iprintf(pyc_output, indent, "%s\n", obj.cast<PycFloat>()->value()); iprintf(pyc_output, indent, "%s\n", obj.cast<PycFloat>()->value());