Skip to content

rs_encode_msg has inconsistent behavior depending on input types and c_exp value #86

Description

@christakahashi

Version: >=2.1.x, python native.

issue

rs_encode_msg can have inconsistent behavior.

when c_exp<=8 it uses bytearray's interpretation of the data to convert it into bytes. This seems to do the "right thing" in most cases as bytes are the smallest unit data can generally be divided into. It does however fail when giving a string as there is no encoding passed.

when c_exp>8, array.array() is used and some type checks are used to decide what to do:

  • strings are converted to bytes with the "latin-1" encoding.
  • bytes objects are broken up byte by byte even if c_exp>=16 and symbols can hold multiple bytes.
  • bytearrays get the default behavior of array.array('i', obj) and are therefor packed in groups of 4 bytes per symbol, which causes an error unless c_exp=32. (note 'i' on the anaconda distribution of windows 64bit python 3.12 is 4 bytes, but may be 2 bytes on other platforms/compilers)

comments/opinions

I think the string encoding of "latin-1" is not a good choice for python 3 users. The code comments imply that UTF8 "mangles" data but I assume that's a vestigial opinion from python 2 where binary data was often held in strings. These days with python 3 i think UTF8 is the correct default behavior for encoding actual string, especially given the growth of non-english text in computing in the last two decades.

i was alarmed to find bytes and bytearrays treated differently as inputs when c_exp>8. Thankfully, use of bytearrays almost always crashes (when c_exp>8 so it doesn't cause runtime errors.

Solutions

The way I see it, there's a few options

  1. Do nothing. the api is the way it is and keeping strict compatibility with 1.x versions is a priority (I haven't confirmed 1.7 has the same behavior). Add documentation clarifying behavior.
  2. Since version 2 hasn't seen a full release yet it seems acceptable to update the API (especially since this is undocumented behavior). I'd propose only accepting bytes, and arrays/lists of integral types (including bytearray, numpy arrays) and treating each element as a symbol. IMO Users should be forced to explicitly interpret stings, which is consistent with how python works.
  3. something in between. make both strings and bytearray work, and do the same thing for for all values of c_exp.

I'm happy to implement any of these changes and make a pull request. But I want to leave API decisions to @lrq3000 as he is the maintainer.

Metadata

Metadata

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions