Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Long Numeric Integer representation for Python 3 #492

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
break;
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;
case PycObject::TYPE_FLOAT:
formatted_print(pyc_output, "%s", obj.cast<PycFloat>()->value());
Expand Down
7 changes: 4 additions & 3 deletions pyc_numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ bool PycLong::isEqual(PycRef<PycObject> 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<unsigned> bits;
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion pyc_numeric.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class PycLong : public PycObject {
int size() const { return m_size; }
const std::vector<int>& value() const { return m_value; }

std::string repr() const;
std::string repr(PycModule* mod) const;

private:
int m_size;
Expand Down
2 changes: 1 addition & 1 deletion pycdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent,
iprintf(pyc_output, indent, "%d\n", obj.cast<PycInt>()->value());
break;
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;
case PycObject::TYPE_FLOAT:
iprintf(pyc_output, indent, "%s\n", obj.cast<PycFloat>()->value());
Expand Down
Loading