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

Do not return bytes, but str #485

Merged
merged 5 commits into from
Apr 3, 2018
Merged

Do not return bytes, but str #485

merged 5 commits into from
Apr 3, 2018

Conversation

jhutar
Copy link
Member

@jhutar jhutar commented Mar 29, 2018

Fixes #484

@jhutar
Copy link
Member Author

jhutar commented Mar 29, 2018

What does this error mean please?

FAIL: test_generic (tests.test_entities.GenericTestCase) [(<bound method Organization.download_debug_certificate of nailgun.entities.Organization(id=1)>, 'get')]
Check that a variety of helper methods are sane.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/SatelliteQE/nailgun/tests/test_entities.py", line 1772, in test_generic
    self.assertEqual(handlr.return_value, response)
AssertionError: <MagicMock name='_handle_response()' id='22771032522088'> != "<MagicMock name='_handle_response()' id='22771032522088'>"

@jhutar jhutar requested a review from sghai March 29, 2018 08:52
@@ -4383,7 +4383,8 @@ def download_debug_certificate(self, synchronous=True, **kwargs):
kwargs.update(self._server_config.get_client_kwargs())
response = client.get(
self.path('download_debug_certificate'), **kwargs)
return _handle_response(response, self._server_config, synchronous)
return str(_handle_response(
response, self._server_config, synchronous))
Copy link
Contributor

Choose a reason for hiding this comment

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

This will not work in py3 this will return something like b'***'.
Please use decode('utf-8') that will work in py2 and py3.

@ldjebran
Copy link
Contributor

@jhutar seems not that simple as it appear, and it seems by changing that return will affect unittests, we have to decide is nailgun default content return should be bytes or string under py3 (in case of string we have to change the unitests) if we cannot come to a consensus on this, we have to change the tests to verify the type of the returned values from nailgun response content.

Copy link
Contributor

@lpramuk lpramuk left a comment

Choose a reason for hiding this comment

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

NACK
str representation of bytes is
b'string' (including qoutes and b)

This has to be resolved by using .decode('utf-8')

FYI I had hard times figuring this out for SatelliteQE/automation-tools#671

@lpramuk
Copy link
Contributor

lpramuk commented Mar 29, 2018

Python 3.6.4

>>> str(b'bytes')
"b'bytes'"

Oops, while decode is OK:

>>> b'bytes'.decode('utf-8')
'bytes'

So please use .decode('utf-8') instead of str()

@lpramuk lpramuk closed this Mar 29, 2018
@lpramuk lpramuk removed the review label Mar 29, 2018
@lpramuk lpramuk reopened this Mar 29, 2018
@lpramuk lpramuk added the review label Mar 29, 2018
@ldjebran
Copy link
Contributor

we have two alternatives
1- Change the default nailgun behavior to return always string content by putting decode('utf-8') in _handle_response.
2- Aways decode('utf-8') the returned content of nailgun

For me I prefer (and recommend) the first one as nailgun is higher lib over basic network api lib and it's logical to receive and return user ready data. Of course this need more effort as nailgun unit-tests needs to be fixed. but this will allow us to not fix all the tests that we know (and the ones not yet found) to be broken on py3.

@ldjebran
Copy link
Contributor

@lpramuk @jhutar Let discuss about the alternatives tomorrow in the meeting.

@ldjebran
Copy link
Contributor

But an other issue if we go with the first variant, if some function are already decoding that content they have to be fixed to not do so (as exception will be raised on py3 AttributeError , on py2 UnicodeEncodeError).

@jhutar
Copy link
Member Author

jhutar commented Mar 29, 2018

Are we positive that Nailgun needs to support both Python 3 and 2? Is that a strong decision?

@jhutar
Copy link
Member Author

jhutar commented Mar 29, 2018

Wont be in the mtg. There are vacations at Fry and Mon in the CZ.

@coveralls
Copy link

coveralls commented Mar 29, 2018

Coverage Status

Coverage decreased (-0.04%) to 97.869% when pulling ebce060 on jhutar:robottelo_py36_TypeError_write_argument_must_be_str_not_bytes into 4878142 on SatelliteQE:master.

@jhutar
Copy link
Member Author

jhutar commented Mar 29, 2018

So, I do not insist on what I have committed. If you want it in a different way, please let me know how.

@jhutar jhutar dismissed lpramuk’s stale review March 29, 2018 21:06

Thank you!. Fixed.

Copy link
Contributor

@lpramuk lpramuk left a comment

Choose a reason for hiding this comment

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

Why don't you change _handler_reponse() ?
https://github.com/SatelliteQE/nailgun/blob/master/nailgun/entities.py#L125-L126

with something like:

if isinstance(response.content, bytes):
    return response.content.decode('utf-8')
else:
    return response.content

WYT?

@jhutar
Copy link
Member Author

jhutar commented Apr 3, 2018

Done, please re-review.

@jhutar jhutar closed this Apr 3, 2018
@rochacbruno rochacbruno removed the review label Apr 3, 2018
@jhutar jhutar reopened this Apr 3, 2018
@jhutar
Copy link
Member Author

jhutar commented Apr 3, 2018

Oh my, it so easy to close...

Copy link
Contributor

@lpramuk lpramuk left a comment

Choose a reason for hiding this comment

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

Brilliant solution :-)

@lpramuk
Copy link
Contributor

lpramuk commented Apr 3, 2018

to other reviewers:

Please "Squash and merge" this PR.

Copy link
Contributor

@ldjebran ldjebran left a comment

Choose a reason for hiding this comment

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

ACK, think we need a third reviewer for this as this is making default content return as string on py3 @rochacbruno @renzon @abalakh please review

@ldjebran
Copy link
Contributor

ldjebran commented Apr 3, 2018

@lpramuk @rochacbruno @renzon @abalakh anyway we have string return in py2 perhaps this is not a concern but that was the default request content return.

@rplevka rplevka self-requested a review April 3, 2018 13:53
Copy link
Member

@rplevka rplevka left a comment

Choose a reason for hiding this comment

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

ACK
LGTM

@rochacbruno rochacbruno merged commit 8d0d3f6 into SatelliteQE:master Apr 3, 2018
@rochacbruno rochacbruno removed the review label Apr 3, 2018
lpramuk pushed a commit to lpramuk/nailgun that referenced this pull request Sep 26, 2023
* Do not return bytes, but str

* Fix issue reviewers found. Thanks!

* Alter tests to pass for download_debug_certificate

* Fix too long line

* This makes it easier - thank you lpramuk
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.

6 participants