Skip to content

Commit ae164dc

Browse files
committed
Remove calls to ensure_bytes, ensure_text, ensure_strings
1 parent 64fa354 commit ae164dc

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

docs/reference.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,3 @@ Example for a user whose language preference is set to Japanese:
10341034
},
10351035
...
10361036
}
1037-
1038-
.. note::
1039-
If needed, the encoding of the returned localized string can be ensured regardless the Python version using shotgun_api3.lib.six.ensure_text().

shotgun_api3/lib/mockgun/mockgun.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ def __init__(self,
177177
api_key=None,
178178
convert_datetimes_to_utc=True,
179179
http_proxy=None,
180-
ensure_ascii=True,
181180
connect=True,
182181
ca_certs=None,
183182
login=None,

shotgun_api3/shotgun.py

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ def __init__(
713713
auth, self.config.server = self._split_url(base_url)
714714
if auth:
715715
auth = base64encode(
716-
sgutils.ensure_binary(urllib.parse.unquote(auth))
716+
urllib.parse.unquote(auth).encode("utf-8")
717717
).decode("utf-8")
718718
self.config.authorization = "Basic " + auth.strip()
719719

@@ -2965,7 +2965,11 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No
29652965
url.find("s3.amazonaws.com") != -1
29662966
and e.headers["content-type"] == "application/xml"
29672967
):
2968-
body = [sgutils.ensure_text(line) for line in e.readlines()]
2968+
body = [
2969+
line.decode("utf-8") if isinstance(line, bytes) else line
2970+
for line in e.readlines()
2971+
]
2972+
29692973
if body:
29702974
xml = "".join(body)
29712975
# Once python 2.4 support is not needed we can think about using
@@ -3858,8 +3862,7 @@ def _encode_payload(self, payload):
38583862
be in a single byte encoding to go over the wire.
38593863
"""
38603864

3861-
wire = json.dumps(payload, ensure_ascii=False)
3862-
return sgutils.ensure_binary(wire)
3865+
return json.dumps(payload, ensure_ascii=False).encode("utf-8")
38633866

38643867
def _make_call(self, verb, path, body, headers):
38653868
"""
@@ -4165,10 +4168,6 @@ def _outbound_visitor(value):
41654168
value = _change_tz(value)
41664169
return value.strftime("%Y-%m-%dT%H:%M:%SZ")
41674170

4168-
# ensure return is six.text_type
4169-
if isinstance(value, str):
4170-
return sgutils.ensure_text(value)
4171-
41724171
return value
41734172

41744173
return self._visit_data(data, _outbound_visitor)
@@ -4656,7 +4655,10 @@ def _send_form(self, url, params):
46564655
else:
46574656
raise ShotgunError("Unanticipated error occurred %s" % (e))
46584657

4659-
return sgutils.ensure_text(result)
4658+
if isinstance(result, bytes):
4659+
result = result.decode("utf-8")
4660+
4661+
return result
46604662
else:
46614663
raise ShotgunError("Max attemps limit reached.")
46624664

@@ -4737,9 +4739,8 @@ def http_request(self, request):
47374739
else:
47384740
params.append((key, value))
47394741
if not files:
4740-
data = sgutils.ensure_binary(
4741-
urllib.parse.urlencode(params, True)
4742-
) # sequencing on
4742+
data = urllib.parse.urlencode(params, True).encode("utf-8")
4743+
# sequencing on
47434744
else:
47444745
boundary, data = self.encode(params, files)
47454746
content_type = "multipart/form-data; boundary=%s" % boundary
@@ -4762,42 +4763,48 @@ def encode(self, params, files, boundary=None, buffer=None):
47624763
if buffer is None:
47634764
buffer = BytesIO()
47644765
for key, value in params:
4765-
if not isinstance(value, str):
4766+
if isinstance(key, bytes):
4767+
key = key.decode("utf-8")
4768+
4769+
if isinstance(value, bytes):
4770+
value = value.decode("utf-8")
4771+
elif not isinstance(value, str):
47664772
# If value is not a string (e.g. int) cast to text
47674773
value = str(value)
4768-
value = sgutils.ensure_text(value)
4769-
key = sgutils.ensure_text(key)
47704774

4771-
buffer.write(sgutils.ensure_binary("--%s\r\n" % boundary))
4775+
buffer.write(f"--{boundary}\r\n".encode("utf-8"))
47724776
buffer.write(
4773-
sgutils.ensure_binary('Content-Disposition: form-data; name="%s"' % key)
4777+
f'Content-Disposition: form-data; name="{key}"'.encode("utf-8")
47744778
)
4775-
buffer.write(sgutils.ensure_binary("\r\n\r\n%s\r\n" % value))
4779+
buffer.write(f"\r\n\r\n{value}\r\n".encode("utf-8"))
47764780
for key, fd in files:
47774781
# On Windows, it's possible that we were forced to open a file
47784782
# with non-ascii characters as unicode. In that case, we need to
47794783
# encode it as a utf-8 string to remove unicode from the equation.
47804784
# If we don't, the mix of unicode and strings going into the
47814785
# buffer can cause UnicodeEncodeErrors to be raised.
4782-
filename = fd.name
4783-
filename = sgutils.ensure_text(filename)
4786+
filename = (
4787+
fd.name.decode("utf-8") if isinstance(fd.name, bytes) else fd.name
4788+
)
47844789
filename = filename.split("/")[-1]
4785-
key = sgutils.ensure_text(key)
4790+
if isinstance(key, bytes):
4791+
key = key.decode("utf-8")
4792+
47864793
content_type = mimetypes.guess_type(filename)[0]
47874794
content_type = content_type or "application/octet-stream"
47884795
file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
4789-
buffer.write(sgutils.ensure_binary("--%s\r\n" % boundary))
4796+
buffer.write(f"--{boundary}\r\n".encode("utf-8"))
47904797
c_dis = 'Content-Disposition: form-data; name="%s"; filename="%s"%s'
47914798
content_disposition = c_dis % (key, filename, "\r\n")
4792-
buffer.write(sgutils.ensure_binary(content_disposition))
4793-
buffer.write(sgutils.ensure_binary("Content-Type: %s\r\n" % content_type))
4794-
buffer.write(sgutils.ensure_binary("Content-Length: %s\r\n" % file_size))
4799+
buffer.write(content_disposition.encode("utf-8"))
4800+
buffer.write(f"Content-Type: {content_type}\r\n".encode("utf-8"))
4801+
buffer.write(f"Content-Length: {file_size}\r\n".encode("utf-8"))
47954802

4796-
buffer.write(sgutils.ensure_binary("\r\n"))
4803+
buffer.write(b"\r\n")
47974804
fd.seek(0)
47984805
shutil.copyfileobj(fd, buffer)
4799-
buffer.write(sgutils.ensure_binary("\r\n"))
4800-
buffer.write(sgutils.ensure_binary("--%s--\r\n\r\n" % boundary))
4806+
buffer.write(b"\r\n")
4807+
buffer.write("--{boundary}--\r\n\r\n".encode("utf-8"))
48014808
buffer = buffer.getvalue()
48024809
return boundary, buffer
48034810

tests/test_client.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545

4646

4747
def b64encode(val):
48-
return base64encode(sgutils.ensure_binary(val)).decode("utf-8")
48+
if isinstance(val, str):
49+
val = val.encode("utf-8")
50+
51+
return base64encode(val).decode("utf-8")
4952

5053

5154
class TestShotgunClient(base.MockTestBase):
@@ -433,8 +436,8 @@ def test_call_rpc(self):
433436
# Test unicode mixed with utf-8 as reported in Ticket #17959
434437
d = {"results": ["foo", "bar"]}
435438
a = {
436-
"utf_str": "\xe2\x88\x9a",
437-
"unicode_str": sgutils.ensure_text("\xe2\x88\x9a"),
439+
"utf_str": b"\xe2\x88\x9a",
440+
"unicode_str": "\xe2\x88\x9a",
438441
}
439442
self._mock_http(d)
440443
rv = self.sg._call_rpc("list", a)
@@ -648,9 +651,7 @@ def test_encode_payload(self):
648651
self.assertTrue(isinstance(j, bytes))
649652

650653
def test_decode_response_ascii(self):
651-
self._assert_decode_resonse(
652-
True, sgutils.ensure_str("my data \u00e0", encoding="utf8")
653-
)
654+
self._assert_decode_resonse(True, "my data \u00e0")
654655

655656
def test_decode_response_unicode(self):
656657
self._assert_decode_resonse(False, "my data \u00e0")

0 commit comments

Comments
 (0)