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 decode response data in Python2 #225

Merged
merged 1 commit into from
Oct 31, 2018
Merged

Conversation

wallecan
Copy link
Contributor

@wallecan wallecan commented Oct 9, 2018

response data is already str in Python2 so it doesn't need to be decoded.
Furthermore, as decode() uses ascii by default in PY2, it breaks in case of a non-ascii character the response.
Just mimic what kubernetes do in https://github.com/kubernetes-client/python/blob/master/kubernetes/client/rest.py#L216
Fix #224

response data is already str in Python2 so it doesn't need to be decoded.
Furthermore, as decode() uses ascii by default in PY2, it breaks in case of a non-ascii character the response.
Just mimic what kubernetes do in https://github.com/kubernetes-client/python/blob/master/kubernetes/client/rest.py#L216
Fix openshift#224
@openshift-ci-robot openshift-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 9, 2018
@openshift-ci-robot
Copy link

Hi @wallecan. Thanks for your PR.

I'm waiting for a openshift member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@fabianvf
Copy link
Member

@wallecan sorry for the delay, was out on PTO!

@fabianvf fabianvf merged commit 8e3a2e9 into openshift:master Oct 31, 2018
fabianvf pushed a commit to fabianvf/openshift-restclient-python that referenced this pull request Nov 5, 2018
response data is already str in Python2 so it doesn't need to be decoded.
Furthermore, as decode() uses ascii by default in PY2, it breaks in case of a non-ascii character the response.
Just mimic what kubernetes do in https://github.com/kubernetes-client/python/blob/master/kubernetes/client/rest.py#L216
Fix openshift#224

(cherry picked from commit 8e3a2e9)
@nyanloutre
Copy link

Is there a workaround for the 0.6.2 version in RHEL ?

@fabianvf
Copy link
Member

fabianvf commented Nov 9, 2018

Sort of, but it's a little messy. If you're hitting this I think the only way around it is to make your request with the serialize=False keyword, which will just return the raw response object. You could then access the response.data object directly. For example:

import six
import json
from kubernetes.config import new_client_from_config
from openshift.dynamic import DynamicClient, ResourceInstance

# These two functions are the up to date versions from openshift/dynamic/client.py
def serialize(resource, response):
    try:
        return ResourceInstance(resource, load_json(response))
    except ValueError:
        if six.PY2:
            return response.data
        return response.data.decode('utf8')

def load_json(response):
    if six.PY2:
        return json.loads(response.data)
    return json.loads(response.data.decode('utf8'))

client = DynamicClient(new_client_from_config())
v1_pods = client.resources.get(api_version='v1', kind='Pod')

# assume the `test` pod in the `test` namespace triggers this bug
my_pod = serialize(v1_pods, v1_pods.get(name='test', namespace='test', serialize=False))

@nyanloutre
Copy link

nyanloutre commented Nov 10, 2018

Thanks for the help ! I will try that

Edit: seems to be working

willthames pushed a commit to willthames/openshift-restclient-python that referenced this pull request Nov 22, 2018
response data is already str in Python2 so it doesn't need to be decoded.
Furthermore, as decode() uses ascii by default in PY2, it breaks in case of a non-ascii character the response.
Just mimic what kubernetes do in https://github.com/kubernetes-client/python/blob/master/kubernetes/client/rest.py#L216
Fix openshift#224
@fabianvf
Copy link
Member

/cherrypick release-0.6

@openshift-cherrypick-robot

@fabianvf: new pull request created: #329

In response to this:

/cherrypick release-0.6

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

willthames pushed a commit to willthames/openshift-restclient-python that referenced this pull request Nov 22, 2019
response data is already str in Python2 so it doesn't need to be decoded.
Furthermore, as decode() uses ascii by default in PY2, it breaks in case of a non-ascii character the response.
Just mimic what kubernetes do in https://github.com/kubernetes-client/python/blob/master/kubernetes/client/rest.py#L216
Fix openshift#224
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UnicodeDecodeError with Python2
5 participants