naitoh / py2rb

A code translator using AST from Python to Ruby.
MIT License
134 stars 17 forks source link

byte literals not supported #14

Open jayvdb opened 3 years ago

jayvdb commented 3 years ago

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

nanobowers commented 3 years ago

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

jayvdb commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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.