Skip to content

Decode bytes to strings in Python Object API #8551

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sethraymond
Copy link

Type-hints for the Python Object API are incorrect for strings - the _UnPack() method does not convert bytes to strings. This PR introduces an optional flag (false by default) that enables decoding the bytes into string objects.

Resolves #5997

@github-actions github-actions bot added c++ codegen Involving generating code from schema python labels Mar 5, 2025
@sethraymond
Copy link
Author

Bump

@sethraymond
Copy link
Author

@aardappel bump, can we get the CI run on this PR?

@aardappel
Copy link
Collaborator

You can get the CI to run by rebasing, it is out of date.. I guess I'll do that for you.

@@ -132,6 +132,8 @@ def _UnPack(self, nestedUnionTest):
if nestedUnionTest is None:
return
self.name = nestedUnionTest.Name()
if self.name is not None:
self.name = self.name.decode('utf-8')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to be self.name = nestedUnionTest.name.decode('utf-8') ?

Copy link
Author

@sethraymond sethraymond Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nestedUnionTest is not an object API type, so the only way to access the name from that object is via nestedUnionTest.Name().

This change is intentional, but if you're asking for a modification to the auto-generated code, I could change the autogeneration here to be

code += GetIndents(3) + "self." + field_field + " = " + struct_var + "." + field_method + "().decode('utf-8')";

Then this would read

self.name = nestedUnionTest.Name()
if self.name is not None:
    self.name = nestedUnionTest.Name().decode('utf-8')

It's functionally the same, though, since self.name is set to nestedUnionTest.name() a couple lines beforehand. I personally think the current implementation is a bit more readable, but I'm open to suggestions.

@aardappel
Copy link
Collaborator

Looks ok, I guess? @dbaileychess

@sethraymond
Copy link
Author

Bump @aardappel and @dbaileychess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ codegen Involving generating code from schema python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Object api strings return bytes instead of str [Python]
2 participants