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

byte literals not supported #14

Open
jayvdb opened this issue Jul 6, 2021 · 4 comments
Open

byte literals not supported #14

jayvdb opened this issue Jul 6, 2021 · 4 comments

Comments

@jayvdb
Copy link

jayvdb commented Jul 6, 2021

b'a' are emitted as is, causes a syntax error in the ruby

@nanobowers
Copy link

@jayvdb any thought on what Ruby construct this would map to?

@jayvdb
Copy link
Author

jayvdb commented Jul 7, 2021

https://stackoverflow.com/a/15843685/5037965 has three options.
IMO Array.pack should be the default approach used here, with String approaches only used if the literal looks like usable ascii text.

@nanobowers
Copy link

There's probably a lot to more to this than just literal support - it's just the tip of the iceberg, so to speak.
python byte objects seem to look like either an array of integers or a string depending on the context:

>>> a = b'abcd@\xef\xdd'
>>> a
b'abcd@\xef\xdd'
>>> print(a)
b'abcd@\xef\xdd'
>>> a[0]
97
>>> a[1]
98
>>> a[1:3]
b'bc'
>>> a[1] + a[2]
197

Using pack/unpack in ruby would either store the data as an array of integers or an encoded string, but if stored as a ruby String or Array, then getting the above functionality may be impractical. Depending on how well supported this needs to be, one could write PyBytes and PyByteArray classes that store the data internally as either a string or Array (optionally frozen in the case of bytes()) and emulate the methods here: https://docs.python.org/3.8/library/stdtypes.html#bytes-and-bytearray-operations

@jayvdb
Copy link
Author

jayvdb commented Jul 10, 2021

Sounds like bytes is likely to be a lot of work. I personally am not using bytes in the projects I transpile. I just noticed it and raised the issue so it is easier for others to evaluate whether py2rb will meet their needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants