Skip to content

SG-38306 Python2 Removal - Part 8 - Remove deprecated ensure_ascii parameter from SG object #405

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

Open
wants to merge 7 commits into
base: ticket/SG-38306-python2-various
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion shotgun_api3/lib/mockgun/mockgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def __init__(self,
api_key=None,
convert_datetimes_to_utc=True,
http_proxy=None,
ensure_ascii=True,
connect=True,
ca_certs=None,
login=None,
Expand Down
33 changes: 0 additions & 33 deletions shotgun_api3/shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ def __init__(
api_key=None,
convert_datetimes_to_utc=True,
http_proxy=None,
ensure_ascii=True,
connect=True,
ca_certs=None,
login=None,
Expand Down Expand Up @@ -709,9 +708,6 @@ def __init__(
{self.config.scheme: proxy_addr}
)

if ensure_ascii:
self._json_loads = self._json_loads_ascii

self.client_caps = ClientCapabilities()
# this relies on self.client_caps being set first
self.reset_user_agent()
Expand Down Expand Up @@ -3982,35 +3978,6 @@ def _decode_response(self, headers, body):
def _json_loads(self, body):
return json.loads(body)

def _json_loads_ascii(self, body):
"""
See http://stackoverflow.com/questions/956867
"""

def _decode_list(lst):
newlist = []
for i in lst:
if isinstance(i, str):
i = sgutils.ensure_str(i)
elif isinstance(i, list):
i = _decode_list(i)
newlist.append(i)
return newlist

def _decode_dict(dct):
newdict = {}
for k, v in dct.items():
if isinstance(k, str):
k = sgutils.ensure_str(k)
if isinstance(v, str):
v = sgutils.ensure_str(v)
elif isinstance(v, list):
v = _decode_list(v)
newdict[k] = v
return newdict

return json.loads(body, object_hook=_decode_dict)

def _response_errors(self, sg_response):
"""
Raise any API errors specified in the response.
Expand Down
42 changes: 14 additions & 28 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import uuid
import warnings

from shotgun_api3.lib import six
from shotgun_api3.lib.httplib2 import Http

import shotgun_api3
Expand Down Expand Up @@ -828,28 +827,24 @@ def test_summary_values(self):
sorted(result["groups"], key=lambda x: x["group_name"]), groups
)

def test_ensure_ascii(self):
"""test_ensure_ascii tests ensure_unicode flag."""
sg_ascii = shotgun_api3.Shotgun(
self.config.server_url, ensure_ascii=True, **self.auth_args
)
def test_json_dumps_default_ensure_ascii_disabled(self):
"""Make sure SG'payload is using ensure_ascii for json dumps"""
sg = shotgun_api3.Shotgun(self.config.server_url, **self.auth_args)

result = sg_ascii.find_one(
"Note", [["id", "is", self.note["id"]]], fields=["content"]
)
if six.PY2:
# In Python3 there isn't a separate unicode type.
self.assertFalse(_has_unicode(result))
# Mock the _http_request method
sg._orig_http_request = sg._http_request
sg._http_request = unittest.mock.Mock(wraps=sg._orig_http_request)

def test_ensure_unicode(self):
"""test_ensure_unicode tests ensure_unicode flag."""
sg_unicode = shotgun_api3.Shotgun(
self.config.server_url, ensure_ascii=False, **self.auth_args
sg.find_one(
"Note",
[["content", "is", "Noëlご"]], # Force a non-ascii character
)
result = sg_unicode.find_one(
"Note", [["id", "is", self.note["id"]]], fields=["content"]

sg._http_request.assert_called_once()
self.assertIn(
b"No\xc3\xabl\xe3\x81\x94", # utf-8 encoded version of Noëlご
sg._http_request.call_args.args[2], # Get the body of the request
)
self.assertTrue(_has_unicode(result))

def test_work_schedule(self):
"""test_work_schedule tests WorkDayRules api"""
Expand Down Expand Up @@ -3443,15 +3438,6 @@ def test_import_httplib(self):
self.assertTrue(hasattr(socks, "HTTPError"))


def _has_unicode(data):
for k, v in data.items():
if isinstance(k, str):
return True
if isinstance(v, str):
return True
return False


def _get_path(url):
"""Returns path component of a url without the sheme, host, query, anchor, or any other
additional elements.
Expand Down
1 change: 0 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,6 @@ def _assert_decode_resonse(self, ensure_ascii, data):
self.config.script_name,
self.config.api_key,
http_proxy=self.config.http_proxy,
ensure_ascii=ensure_ascii,
connect=False,
)

Expand Down