@@ -713,7 +713,7 @@ def __init__(
713
713
auth , self .config .server = self ._split_url (base_url )
714
714
if auth :
715
715
auth = base64encode (
716
- sgutils . ensure_binary ( urllib .parse .unquote (auth ))
716
+ urllib .parse .unquote (auth ). encode ( "utf-8" )
717
717
).decode ("utf-8" )
718
718
self .config .authorization = "Basic " + auth .strip ()
719
719
@@ -2965,7 +2965,11 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No
2965
2965
url .find ("s3.amazonaws.com" ) != - 1
2966
2966
and e .headers ["content-type" ] == "application/xml"
2967
2967
):
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
+
2969
2973
if body :
2970
2974
xml = "" .join (body )
2971
2975
# Once python 2.4 support is not needed we can think about using
@@ -3858,8 +3862,7 @@ def _encode_payload(self, payload):
3858
3862
be in a single byte encoding to go over the wire.
3859
3863
"""
3860
3864
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" )
3863
3866
3864
3867
def _make_call (self , verb , path , body , headers ):
3865
3868
"""
@@ -4165,10 +4168,6 @@ def _outbound_visitor(value):
4165
4168
value = _change_tz (value )
4166
4169
return value .strftime ("%Y-%m-%dT%H:%M:%SZ" )
4167
4170
4168
- # ensure return is six.text_type
4169
- if isinstance (value , str ):
4170
- return sgutils .ensure_text (value )
4171
-
4172
4171
return value
4173
4172
4174
4173
return self ._visit_data (data , _outbound_visitor )
@@ -4656,7 +4655,10 @@ def _send_form(self, url, params):
4656
4655
else :
4657
4656
raise ShotgunError ("Unanticipated error occurred %s" % (e ))
4658
4657
4659
- return sgutils .ensure_text (result )
4658
+ if isinstance (result , bytes ):
4659
+ result = result .decode ("utf-8" )
4660
+
4661
+ return result
4660
4662
else :
4661
4663
raise ShotgunError ("Max attemps limit reached." )
4662
4664
@@ -4737,9 +4739,8 @@ def http_request(self, request):
4737
4739
else :
4738
4740
params .append ((key , value ))
4739
4741
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
4743
4744
else :
4744
4745
boundary , data = self .encode (params , files )
4745
4746
content_type = "multipart/form-data; boundary=%s" % boundary
@@ -4762,42 +4763,48 @@ def encode(self, params, files, boundary=None, buffer=None):
4762
4763
if buffer is None :
4763
4764
buffer = BytesIO ()
4764
4765
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 ):
4766
4772
# If value is not a string (e.g. int) cast to text
4767
4773
value = str (value )
4768
- value = sgutils .ensure_text (value )
4769
- key = sgutils .ensure_text (key )
4770
4774
4771
- buffer .write (sgutils . ensure_binary ( "--%s \r \n " % boundary ))
4775
+ buffer .write (f "--{ boundary } \r \n ". encode ( "utf-8" ))
4772
4776
buffer .write (
4773
- sgutils . ensure_binary ( 'Content-Disposition: form-data; name="%s"' % key )
4777
+ f 'Content-Disposition: form-data; name="{ key } "' . encode ( "utf-8" )
4774
4778
)
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" ))
4776
4780
for key , fd in files :
4777
4781
# On Windows, it's possible that we were forced to open a file
4778
4782
# with non-ascii characters as unicode. In that case, we need to
4779
4783
# encode it as a utf-8 string to remove unicode from the equation.
4780
4784
# If we don't, the mix of unicode and strings going into the
4781
4785
# 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
+ )
4784
4789
filename = filename .split ("/" )[- 1 ]
4785
- key = sgutils .ensure_text (key )
4790
+ if isinstance (key , bytes ):
4791
+ key = key .decode ("utf-8" )
4792
+
4786
4793
content_type = mimetypes .guess_type (filename )[0 ]
4787
4794
content_type = content_type or "application/octet-stream"
4788
4795
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" ))
4790
4797
c_dis = 'Content-Disposition: form-data; name="%s"; filename="%s"%s'
4791
4798
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" ))
4795
4802
4796
- buffer .write (sgutils . ensure_binary ( "\r \n " ) )
4803
+ buffer .write (b "\r \n " )
4797
4804
fd .seek (0 )
4798
4805
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" ))
4801
4808
buffer = buffer .getvalue ()
4802
4809
return boundary , buffer
4803
4810
0 commit comments