ice icon indicating copy to clipboard operation
ice copied to clipboard

`__repr__()` function returns utf-8 encoded bytes rather than a Python string

Open ReeceHumphreys opened this issue 1 year ago • 1 comments

The __repr__ method is always assigned the output of the __str__ method which itself calls into IcePy via IcePy.stringify. This is currently not working as expected as IcePy.stringify returns utf-8 encoded bytes rather than a Python string. As such when you try to use python tooling like help() or pydoc with Ice you get the following error:

python3 -m pydoc -w Ice                                                     
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/reece/.pyenv/versions/3.11.8/lib/python3.11/pydoc.py", line 2851, in <module>
    cli()
  File "/Users/reece/.pyenv/versions/3.11.8/lib/python3.11/pydoc.py", line 2810, in cli
    writedoc(arg)
  File "/Users/reece/.pyenv/versions/3.11.8/lib/python3.11/pydoc.py", line 1800, in writedoc
    page = html.page(describe(object), html.document(object, name))
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/reece/.pyenv/versions/3.11.8/lib/python3.11/pydoc.py", line 479, in document
    if inspect.ismodule(object): return self.docmodule(*args)
                                        ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/reece/.pyenv/versions/3.11.8/lib/python3.11/pydoc.py", line 846, in docmodule
    contents.append(self.document(value, key))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/reece/.pyenv/versions/3.11.8/lib/python3.11/pydoc.py", line 485, in document
    return self.docother(*args)
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/reece/.pyenv/versions/3.11.8/lib/python3.11/pydoc.py", line 1108, in docother
    return lhs + self.repr(object)
                 ^^^^^^^^^^^^^^^^^
  File "/Users/reece/.pyenv/versions/3.11.8/lib/python3.11/pydoc.py", line 535, in repr
    return Repr.repr(self, object)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/reece/.pyenv/versions/3.11.8/lib/python3.11/reprlib.py", line 53, in repr
    return self.repr1(x, self.maxlevel)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/reece/.pyenv/versions/3.11.8/lib/python3.11/pydoc.py", line 542, in repr1
    return self.escape(cram(stripid(repr(x)), self.maxother))
                                    ^^^^^^^
TypeError: __repr__ returned non-string (type bytes) 

One potential fix would be to always call decode() on the result from IcePy.stringify but there may be a more elegant solution by actually updating IcePy.

ReeceHumphreys avatar Feb 22 '24 18:02 ReeceHumphreys

We should probably fix this on 3.7 as well.

externl avatar Feb 23 '24 20:02 externl

stringify itself is fine, it uses createString. See https://github.com/zeroc-ice/ice/blob/3a0a05af7a795e172e839cb808f687fcdab459aa/python/modules/IcePy/Types.cpp#L4607-L4628

The problem is the repr implementation for Ice.Unset that returns a bytes object https://github.com/zeroc-ice/ice/blob/3a0a05af7a795e172e839cb808f687fcdab459aa/python/modules/IcePy/Types.cpp#L155-L160

pepone avatar Jul 15 '24 09:07 pepone