Repository navigation
fix(python): Avoid exceptions when decoding non-UTF-8 data from string attrs - #5485
Conversation
| #if defined(OIIO_PY_BACKEND_NANOBIND) | ||
| if (!py_str) { | ||
| py::raise_python_error(); | ||
| } | ||
| return py::steal<py::str>(py_str); | ||
| #else | ||
| if (!py_str) { | ||
| throw py::error_already_set(); | ||
| } | ||
| return py::reinterpret_steal<py::str>(py_str); | ||
| #endif |
There was a problem hiding this comment.
I went with a single function definition containing implementation details for both backends, but it would be easy enough to just have two definitions in the oiio_py namespace with the Python API call duplicated if you prefer. Or we could split the difference and alias the exception type and handle stealing functions in py_backend.h in order to collapse this into one code path.
| .OIIO_PY_PROP_RO("name", | ||
| [](const ParamValue& self) { | ||
| return oiio_py::str(self.name().string()); | ||
| return py_str_escaped(self.name().string()); |
There was a problem hiding this comment.
Flagging that I've also wrapped the ParamValue.name accessor, because there's nothing that normalizes/sanitizes attribute names in the C++ API.
|
Those wheel failures are unrelated. I just merged a fix that should take care of them. If you rebase on the current main, they should go away. |
Signed-off-by: Nathan Rusch <nrusch@users.noreply.github.com>
4c4f80b to
7137488
Compare
Signed-off-by: Larry Gritz <lg@larrygritz.com>
lgritz
left a comment
There was a problem hiding this comment.
LGTM!
I added a couple lines to a comment, but otherwise, all looks great and I am merging.
|
Oh OK cool. I can come back and address the spec serialization later, but this should cover most of the potential user-facing issues. |
|
Oh, sorry, did I get ahead of you? If there are more changes you want to make before merging this PR, I can certainly wait. |
|
Well, the outstanding tripping hazards here are just around I'd appreciate your thoughts on how to proceed here, since you have the context on how you'd like to dovetail this into the impending 3.2 cut (or whether that matters in practice for the Python bindings):
|
|
So the problem is that is we turn the xml serialization into valid UTF-8 for Python's sake, that XML won't correctly encode the actual string of char 255's? So there is a discrepancy between what the string contains in the file, what the python str can contain, and how the xml needs to be expressed to properly round-trip, right? Can't we just express it as hex/oct strings, like "\xff\xff"? I think that is your suggestion 2. I'd be fine with that. If we re-consume the resulting XML, will we end up where we started, with a string full of 0xff's? Totally your choice whether to do that as part of this PR, or whether to commit what we have and do that in isolation next. |
Signed-off-by: Nathan Rusch <nrusch@users.noreply.github.com>
|
OK. I'll push up the change to wrap those methods with this new helper as-is, and then look at a better solution for the XML data as a follow-up. |
|
Done |
lgritz
left a comment
There was a problem hiding this comment.
LGTM
Follow-up to address the xml issue is expected separately.
…g attrs (AcademySoftwareFoundation#5485) Add a new `py_str_escaped` helper function to handle conversion of string-like C++ types to `py:str` without raising a `UnicodeDecodeError` for non-UTF-8 input data. This function is a fairly simple wrapper that calls the `PyUnicode_DecodeUTF8` with the `'surrogateescape'` error handling scheme (described at https://docs.python.org/3/library/codecs.html#error-handlers) and transfers ownership to the binding-specific object type. Fixes AcademySoftwareFoundation#3856 Only manual tests so far, but I plan to add some test cases to the harness. --------- Signed-off-by: Nathan Rusch <nrusch@users.noreply.github.com>
Add a new
py_str_escapedhelper function to handle conversion of string-like C++ types topy:strwithout raising aUnicodeDecodeErrorfor non-UTF-8 input data.This function is a fairly simple wrapper that calls the
PyUnicode_DecodeUTF8with the'surrogateescape'error handling scheme (described at https://docs.python.org/3/library/codecs.html#error-handlers) and transfers ownership to the binding-specific object type.Fixes #3856
Tests
Only manual tests so far, but I plan to add some test cases to the harness.
Checklist:
and if I used AI coding assistants, I have an
Assisted-by: TOOL / MODELline in the pull request description above.
behavior.
PR, by pushing the changes to my fork and seeing that the automated CI
passed there. (Exceptions: If most tests pass and you can't figure out why
the remaining ones fail, it's ok to submit the PR and ask for help. Or if
any failures seem entirely unrelated to your change; sometimes things break
on the GitHub runners.)
fixed any problems reported by the clang-format CI test.
corresponding Python bindings. If altering ImageBufAlgo functions, I also
exposed the new functionality as oiiotool options.