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

Issue 31723: Support for python3 in jenkins_script module #33206

Merged
merged 1 commit into from Dec 15, 2017

Conversation

Leibniz137
Copy link
Contributor

SUMMARY

Make jenkins_script module compatible with python3 by convert bytes to str when necessary
Fixes #31723

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

jenkins_script

ANSIBLE VERSION
ansible 2.4.1.0
  config file = /Users/ng186024/src/cit/td_jenkins_master/ansible/ansible.cfg
  configured module search path = ['/Users/ng186024/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/ng186024/.virtualenvs/jenkins/lib/python3.6/site-packages/ansible
  executable location = /Users/ng186024/.virtualenvs/jenkins/bin/ansible
  python version = 3.6.0 (default, Jan 21 2017, 10:40:54) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
ADDITIONAL INFORMATION

In python3, ansible's fetch_url utility function wraps urllib.request.urlopen.
For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse object slightly modified.
Calling .read() on an HTTPResponse object returns bytes (note the docstring fix).

Here, to_native is used to convert the bytestrings returned by fetch_url into unicode strings.
This is necessary because:

  1. Pre python3.6, json.loads requires passing a string, not a bytestring, as its argument
  2. In python3 generally, testing if a string is a substring of a bytestring
    using the 'in' operator will raise a TypeError

see:

before:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: a bytes-like object is required, not 'str'
fatal: [sdvl3jenk042 -> localhost]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Traceback (most recent call last):
  File \"/var/folders/g3/rt5fttrd1dx9gxpqhgh903znwg38nv/T/ansible_prohpe7i/ansible_module_jenkins_script.py\", line 196, in <module>
    main()
  File \"/var/folders/g3/rt5fttrd1dx9gxpqhgh903znwg38nv/T/ansible_prohpe7i/ansible_module_jenkins_script.py\", line 187, in main
    if 'Exception:' in result and 'at java.lang.Thread' in result:\nTypeError: a bytes-like object is required, not 'str'
", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 0}

after:

ok: [sdvl3jenk042 -> localhost]

Fixes ansible#31723
In python3, ansible's fetch_url utility function wraps urllib.request.urlopen.
For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse object slightly modified.
Calling .read() on an HTTPResponse object returns bytes (note the docstring fix).

Here, to_native is used to convert the bytestrings returned by fetch_url into unicode strings.
This is necessary because:
  1. Pre python3.6, json.loads requires passing a string, not a bytestring, as its argument
  2. In python3 generally, testing if a string is a substring of a bytestring
     using the 'in' operator will raise a TypeError

see:
- https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen
- https://docs.python.org/3/library/http.client.html#http.client.HTTPResponse.read
- https://docs.python.org/3/library/json.html#json.loads
@ansibot
Copy link
Contributor

ansibot commented Nov 22, 2017

@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 bugfix_pull_request core_review In order to be merged, this PR must follow the core review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. python3 support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Nov 22, 2017
@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Nov 22, 2017
@resmo
Copy link
Contributor

resmo commented Nov 27, 2017

code LGTM
shipit

@hogarthj
Copy link
Contributor

hogarthj commented Dec 5, 2017

shipit

@ansibot ansibot added shipit This PR is ready to be merged by Core and removed core_review In order to be merged, this PR must follow the core review workflow. labels Dec 5, 2017
@maxamillion maxamillion merged commit 441dc36 into ansible:devel Dec 15, 2017
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bugfix_pull_request labels Mar 6, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. module This issue/PR relates to a module. python3 shipit This PR is ready to be merged by Core support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jenkins_script produces Python 3 error TypeError: the JSON object must be str, not 'bytes'
6 participants