Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src/lib/pythongen.ml: Fix InternalError(str(exception)) for Py3
In `src/lib/pythongen.ml`, fix `return InternalError(str(exn)).failure()`: For Python3, `compat_block` defines `str = bytes`: ```ml ; Line "if sys.version_info[0] > 2:" ; Block [ Line "long = int"; Line "unicode = str"; Line "str = bytes" ] ``` The easy fix is to get a copy of `str` and use it to convert `exn` to `str`: ```py get_str = str if PY3: str = bytes ... # In the generated code: return InternalError(get_str(exn).failure()) ``` (Instead, we could also prepend `def get_str(a): return str(a)`, this is just shorter. It would be fine either way.) Without this fix, `str(Exception())`, `compat_block` causes it to be `bytes(Exception())` on Py3. This would raise a `TypeError` because `Exception` can't be converted to `bytes`: ```py $ python3 -c 'str = bytes; str(Exception("hi"))' Traceback (most recent call last): File "<string>", line 1, in <module> TypeError: cannot convert 'Exception' object to bytes ``` Setting `get_str()` to be `str()` for Py2 and Py3 fixes it and gets a `str` for `InternalError.__init__(arg)` (which expects a `str` value as argument) Co-authored-by: Edwin Török <edwin.torok@cloud.com> Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
- Loading branch information