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

[BUG] PythonObject doesn't handle large UInt64 correctly #3510

Open
soraros opened this issue Sep 20, 2024 · 2 comments
Open

[BUG] PythonObject doesn't handle large UInt64 correctly #3510

soraros opened this issue Sep 20, 2024 · 2 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed mojo-repo Tag all issues with this label

Comments

@soraros
Copy link
Contributor

soraros commented Sep 20, 2024

Bug description

As title.

A Mojo UInt64 value went through the following to reach Python runtime:

  • PythonObject.__init__[dt: DType](Self, SIMD[dt, 1])
    elif dt.is_integral():
    var int_val = value.cast[DType.index]().value
    self.py_object = cpython.toPython(int_val)
  • CPython.toPython(Self, Int)
    fn toPython(inout self, litInt: Int) -> PyObjectPtr:
    return self.PyLong_FromLong(litInt.value)
  • CPython.PyLong_FromLong(Self, Int)
    fn PyLong_FromLong(inout self, value: Int) -> PyObjectPtr:
    var r = self.lib.get_function[fn (Int) -> PyObjectPtr](
    "PyLong_FromLong"
    )(value)
    if self.logging_enabled:
    print(
    r._get_ptr_as_int(),
    " NEWREF PyLong_FromLong, refcnt:",
    self._Py_REFCNT(r),
    ", value:",
    value,
    )
    self._inc_total_rc()
    return r
  • PyLong_FromLong with signature PyObject *PyLong_FromLong(long v)

An unwanted bit cast happened on CPython's API boundary.

We should

  • Add a branch in PythonObject.__init__
  • Add a method calling PyLong_FromUnsignedLong on struct CPython
  • Wire them up

Steps to reproduce

from python import PythonObject

def main():
  n = UInt64(0xFEDCBA0987654321)
  print(PythonObject(n))  # prints -81986143110479071

A more peculiar repro:

from python import Python

def main():
  py = Python.import_module("builtins")
  a = UInt64(0x1234567890ABCDEF)
  b = UInt64(0xFEDCBA0987654321)
  # bb = py.int(str(b))      # doesn't crash
  a, bb = a, py.int(str(b))  # only crashes when tuple unpacking is used
  print(a + bb)

System information

Mojo 2024.9.905 (ae516e17) on Docker, Intel Mac
@soraros soraros added bug Something isn't working mojo-repo Tag all issues with this label labels Sep 20, 2024
@JoeLoser JoeLoser added help wanted Extra attention is needed good first issue Good for newcomers labels Sep 20, 2024 — with Linear
@dhruvmalik007
Copy link

hi @JoeLoser , here is the code that will act patch for this bug : dhruvmalik007@bf8d434 , let me know if you need more tests / changes required and I will do the needful in order to create PR and merge .

Thanks

@soraros
Copy link
Contributor Author

soraros commented Sep 27, 2024

Hi @dhruvmalik007, thanks for your interest! Feel free to make a PR and we can iterate there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

3 participants