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

Python3 compatibility [ESD-1193] #31

Merged
merged 3 commits into from
Mar 25, 2019

Conversation

pmiettinen
Copy link
Contributor

@pmiettinen pmiettinen commented Mar 22, 2019

TODO

  • Recreate wheels

bytes(section),
bytes(name),
bytes(str(value)))
bytearray(section, 'utf8'),

Choose a reason for hiding this comment

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

When I did the python3 update for the command line settings application, I asked somebody (probably @silverjam ) what the settings key/value encoding should be, and he said ascii. Back then I put it into 2 constants (one for keys and one for values) in the cli application. Maybe getting the encoding (whatever it is) should be part of the public API of libsettings so it can be easily changed if needed and user applications (console and in the future also settings.py) can get it?

Also coordinate this with console's settings PR (at the moment there is one encoding/decoding command that uses hard-coded ascii).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ascii it is then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need to think about the encoding from API. I am not going to mix it in here anymore though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, all these are inputs for the libsettings so the client needs to provide the encoding of the argument string. I need to add the argument for that.

@pmiettinen pmiettinen force-pushed the pmiettinen/esd-1193-python3-compatibility branch 4 times, most recently from f32b941 to ceecf78 Compare March 22, 2019 13:45
Copy link
Contributor

@benjaminaltieri benjaminaltieri left a comment

Choose a reason for hiding this comment

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

I'm not super familiar with the requirements for 2 to 3 porting but these seem like straightforward changes. IGTM

@@ -13,6 +13,8 @@

from __future__ import absolute_import, print_function

from six.moves import input
Copy link
Contributor

Choose a reason for hiding this comment

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

should probably add 'client' requirements somewhere. tried running this in a fresh venv and had to install sbp, six and piksi_tools. Not a big deal but would be helpful

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for testing, I'll keep that in mind.

@pmiettinen
Copy link
Contributor Author

Tested using Swift Console on 2.7.12 and 3.5.0. Settings read/write sanity checks ok.

bytes(str(value)))
bytearray(section, encoding),
bytearray(name, encoding),
bytearray(value, encoding))

Choose a reason for hiding this comment

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

isn't there a risk of double encoding with this line and value = str(value).encode(encoding) above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, reworked

value,
sizeof(value))
if (ret == 0):
return str(value)
return value
Copy link

@tn-exafore tn-exafore Mar 25, 2019

Choose a reason for hiding this comment

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

I'm not 100% sure what would be the neatest method but now it seems that all inputs of read/read_all/write are strings but outputs are bytes/bytearrays. Wouldn't it be more symmetric if the outputs were strings, too? I.e.,
return value.decode(encoding) (and the same for read_all)? This way the application wouldn't need to do any decoding in the default uses but an extra encoding parameter (with a default value) would be needed in read_all.

Edit: some values are numeric too, complicating the logic. But I guess they could be returned as strings and the application could convert them to numeric values if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added decoding

@pmiettinen pmiettinen force-pushed the pmiettinen/esd-1193-python3-compatibility branch from f04c073 to f6f6328 Compare March 25, 2019 09:55
value = bytearray(str(value), 'ascii')
elif isinstance(value, unicode):
# python2 can have 'str' or 'unicode'
value = bytearray(str(value), 'utf8')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

copy paste mistake here, need to fix

@pmiettinen pmiettinen force-pushed the pmiettinen/esd-1193-python3-compatibility branch from f6f6328 to 4eec11b Compare March 25, 2019 10:01
@pmiettinen
Copy link
Contributor Author

Additional bench test; writing an int value using settings_client.py OK now.

@@ -148,25 +148,38 @@ cdef class Settings:
def destroy(self):
settings_destroy(&self.ctx)

def write(self, section, name, value):
def write(self, section, name, value, encoding='ascii'):
Copy link

@tn-exafore tn-exafore Mar 25, 2019

Choose a reason for hiding this comment

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

Not very important at this point, and thus this comment can be ignored, but I have a feeling that the encoding could be a server-side setting from application perspective and this library could be the middle-man storing it. The settings change the behavior of the device after all, so the device would know best how it wants to be controlled and what are valid values.

Following this logic, this library could have an additional function/constant that tells the expected encoding, and read/read_all/write wouldn't have an encoding parameter but try to use the expected encoding for string values, throwing an exception if it cannot be done.

However, if there are string settings that accept arbitrary bytes, then this logic wouldn't work well. Then it would be better to take input as bytes/bytearrays. But I am not aware of string settings needing arbitrary bytes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's some merit to this but currently I'd say not relevant since at least on Piksi side everything is (presumed) to be in ascii domain AFAIK.

@pmiettinen pmiettinen merged commit 8029bfd into master Mar 25, 2019
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

Successfully merging this pull request may close these issues.

3 participants