From f6275cdbf45ceb2f75d4aa1c9a9473199d7a3f02 Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Wed, 16 Jul 2025 14:05:50 +0200 Subject: [PATCH 1/7] Remove deprecated ensure_ascii parameter from SG object --- shotgun_api3/lib/mockgun/mockgun.py | 1 - shotgun_api3/shotgun.py | 33 ----------------------- tests/test_api.py | 41 +++++++++-------------------- tests/test_client.py | 1 - 4 files changed, 13 insertions(+), 63 deletions(-) diff --git a/shotgun_api3/lib/mockgun/mockgun.py b/shotgun_api3/lib/mockgun/mockgun.py index 18e4a142..45d0b2aa 100644 --- a/shotgun_api3/lib/mockgun/mockgun.py +++ b/shotgun_api3/lib/mockgun/mockgun.py @@ -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, diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 49c4d120..652bd234 100644 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -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, @@ -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() @@ -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. diff --git a/tests/test_api.py b/tests/test_api.py index e7e046d2..0c754efc 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -29,7 +29,6 @@ import uuid import warnings -from shotgun_api3.lib import six from shotgun_api3.lib.httplib2 import Http import shotgun_api3 @@ -828,28 +827,23 @@ 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, connect=False, **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 so we can assert_called_with + sg._http_request(return_value=(200, {}, "")) - 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 - ) - result = sg_unicode.find_one( - "Note", [["id", "is", self.note["id"]]], fields=["content"] + sg.find_one("Note", [["id", "is", "Noëlご"]]) # Force a non-ascii character + + sg._http_request.assert_called_once_with( + "POST", # verb + "api3/json", # path + "", # body + {}, # headers ) - self.assertTrue(_has_unicode(result)) def test_work_schedule(self): """test_work_schedule tests WorkDayRules api""" @@ -3443,15 +3437,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. diff --git a/tests/test_client.py b/tests/test_client.py index d9c66ae1..249203db 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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, ) From 5c4dc5b9600ac97ede214e0f1d3d92589cc68d81 Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Thu, 17 Jul 2025 19:44:50 +0200 Subject: [PATCH 2/7] fixup! Remove deprecated ensure_ascii parameter from SG object --- tests/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index 0c754efc..bb7eee53 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -834,7 +834,7 @@ def test_json_dumps_default_ensure_ascii_disabled(self): ) # Mock the _http_request method so we can assert_called_with - sg._http_request(return_value=(200, {}, "")) + sg._http_request = unittest.mock.MagicMock(return_value=(200, {}, "")) sg.find_one("Note", [["id", "is", "Noëlご"]]) # Force a non-ascii character From c321734da05ba392951afb8542f9be7f1f0e3f1d Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Thu, 17 Jul 2025 19:56:09 +0200 Subject: [PATCH 3/7] Test CI --- tests/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index bb7eee53..19737c1a 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -834,7 +834,7 @@ def test_json_dumps_default_ensure_ascii_disabled(self): ) # Mock the _http_request method so we can assert_called_with - sg._http_request = unittest.mock.MagicMock(return_value=(200, {}, "")) + sg._http_request = unittest.mock.MagicMock(return_value=((200, "OK"), {}, "")) sg.find_one("Note", [["id", "is", "Noëlご"]]) # Force a non-ascii character From 1f370fe1b9fb9b79154fd5e92270578f7a8814ee Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Thu, 17 Jul 2025 20:27:55 +0200 Subject: [PATCH 4/7] fixup! Test CI --- tests/test_api.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 19737c1a..c1143c82 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -829,12 +829,10 @@ def test_summary_values(self): 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, connect=False, **self.auth_args - ) + sg = shotgun_api3.Shotgun(self.config.server_url, **self.auth_args) # Mock the _http_request method so we can assert_called_with - sg._http_request = unittest.mock.MagicMock(return_value=((200, "OK"), {}, "")) + sg._http_request = unittest.mock.MagicMock(return_value=((200, "OK"), {}, None)) sg.find_one("Note", [["id", "is", "Noëlご"]]) # Force a non-ascii character From 2eb8306510728745e015639bbc87154b9533ca86 Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Thu, 17 Jul 2025 20:43:16 +0200 Subject: [PATCH 5/7] fixup! fixup! Test CI --- tests/test_api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index c1143c82..0e374aa8 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -832,7 +832,8 @@ def test_json_dumps_default_ensure_ascii_disabled(self): sg = shotgun_api3.Shotgun(self.config.server_url, **self.auth_args) # Mock the _http_request method so we can assert_called_with - sg._http_request = unittest.mock.MagicMock(return_value=((200, "OK"), {}, None)) + sg._orig_http_request = sg._http_request + sg._http_request = unittest.mock.Mock(wraps=sg._orig_http_request) sg.find_one("Note", [["id", "is", "Noëlご"]]) # Force a non-ascii character From 1463852518c035c1b7da8bb548054ec392dda3d4 Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Thu, 17 Jul 2025 20:55:05 +0200 Subject: [PATCH 6/7] fixup! fixup! fixup! Test CI --- tests/test_api.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index 0e374aa8..b2b1792c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -835,7 +835,10 @@ def test_json_dumps_default_ensure_ascii_disabled(self): sg._orig_http_request = sg._http_request sg._http_request = unittest.mock.Mock(wraps=sg._orig_http_request) - sg.find_one("Note", [["id", "is", "Noëlご"]]) # Force a non-ascii character + sg.find_one( + "Note", + [["content", "is", "Noëlご"]], # Force a non-ascii character + ) sg._http_request.assert_called_once_with( "POST", # verb From da922f969fa5dcba08b1dc302420523262c28532 Mon Sep 17 00:00:00 2001 From: Julien Langlois Date: Thu, 17 Jul 2025 21:14:25 +0200 Subject: [PATCH 7/7] fixup! fixup! fixup! fixup! Test CI --- tests/test_api.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index b2b1792c..0302dfe0 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -831,7 +831,7 @@ 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) - # Mock the _http_request method so we can assert_called_with + # Mock the _http_request method sg._orig_http_request = sg._http_request sg._http_request = unittest.mock.Mock(wraps=sg._orig_http_request) @@ -840,11 +840,10 @@ def test_json_dumps_default_ensure_ascii_disabled(self): [["content", "is", "Noëlご"]], # Force a non-ascii character ) - sg._http_request.assert_called_once_with( - "POST", # verb - "api3/json", # path - "", # body - {}, # headers + 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 ) def test_work_schedule(self):