Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix some bugs
  • Loading branch information
rane-hs committed May 8, 2018
commit cedb4b5c0ac94f907511c06db268d733a0fbf81e
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: python
python:
- "2.6"
- "2.7"
- "3.4"
- "3.5"
- "3.6"
# command to install dependencies
install:
- pip install -r tests/ci_requirements.txt
Expand Down
4 changes: 2 additions & 2 deletions shotgun_api3/lib/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def _decompressContent(response, new_content):
encoding = response.get('content-encoding', None)
if encoding in ['gzip', 'deflate']:
if encoding == 'gzip':
content = gzip.GzipFile(fileobj=io.StringIO(new_content)).read()
content = gzip.GzipFile(fileobj=io.BytesIO(new_content)).read()
if encoding == 'deflate':
content = zlib.decompress(content)
response['content-length'] = str(len(content))
Expand Down Expand Up @@ -942,7 +942,7 @@ def __init__(self, host, port=None, key_file=None, cert_file=None,
ca_certs=None, disable_ssl_certificate_validation=False):
http.client.HTTPSConnection.__init__(self, host, port=port,
key_file=key_file,
cert_file=cert_file, strict=strict)
cert_file=cert_file)
self.timeout = timeout
self.proxy_info = proxy_info
if ca_certs is None:
Expand Down
8 changes: 1 addition & 7 deletions shotgun_api3/lib/simplejson/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ def _import_c_scanstring():
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL

def _floatconstants():
_BYTES = '7FF80000000000007FF0000000000000'.decode('hex')
# The struct module in Python 2.4 would get frexp() out of range here
# when an endian is specified in the format string. Fixed in Python 2.5+
if sys.byteorder != 'big':
_BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1]
nan, inf = struct.unpack('dd', _BYTES)
return nan, inf, -inf
return float('nan'), float('inf'), float('-inf')

NaN, PosInf, NegInf = _floatconstants()

Expand Down
33 changes: 22 additions & 11 deletions shotgun_api3/shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ def __init__(self,
# and auth header
auth, self.config.server = urllib.parse.splituser(urllib.parse.urlsplit(base_url).netloc)
if auth:
auth = base64.encodestring(urllib.parse.unquote(auth))
self.config.authorization = "Basic " + auth.strip()
auth = base64.encodestring(urllib.parse.unquote(auth).encode())
self.config.authorization = b"Basic " + auth.strip()

# foo:[email protected]:3456
if http_proxy:
Expand Down Expand Up @@ -3075,6 +3075,19 @@ def entity_types(self):
# ========================================================================
# RPC Functions

def _process_str(self, _s):
if isinstance(_s, dict):
return self._process_dict(_s)
elif isinstance(_s, bytes):
return _s.decode()
return _s

def _process_dict(self, _d):
if not isinstance(_d, dict):
return _d

return {self._process_str(k): self._process_str(v) for k, v in _d.items()}

def _call_rpc(self, method, params, include_auth_params=True, first=False):
"""
Call the specified method on the Shotgun Server sending the supplied payload.
Expand Down Expand Up @@ -3109,12 +3122,12 @@ def _call_rpc(self, method, params, include_auth_params=True, first=False):
response = self._transform_inbound(response)

if not isinstance(response, dict) or "results" not in response:
return response
return self._process_dict(response)

results = response.get("results")
if first and isinstance(results, list):
return results[0]
return results
return self._process_dict(results[0])
return self._process_dict(results)

def _auth_params(self):
"""
Expand Down Expand Up @@ -3207,7 +3220,6 @@ def _encode_payload(self, payload):
requires it. The unicode string is then encoded as 'utf-8' as it must
be in a single byte encoding to go over the wire.
"""

wire = json.dumps(payload, ensure_ascii=False)
if isinstance(wire, str):
return wire.encode("utf-8")
Expand Down Expand Up @@ -3371,7 +3383,7 @@ def _decode_dict(dct):
v = _decode_list(v)
newdict[k] = v
return newdict
return json.loads(body, object_hook=_decode_dict)
return json.loads(body.decode(), object_hook=_decode_dict)


def _response_errors(self, sg_response):
Expand Down Expand Up @@ -3420,6 +3432,9 @@ def _visit_data(self, data, visitor):
for k, v in data.items()
)

# if isinstance(data, str):
# return data.encode()

return visitor(data)

def _transform_outbound(self, data):
Expand Down Expand Up @@ -3460,10 +3475,6 @@ def _outbound_visitor(value):
value = _change_tz(value)
return value.strftime("%Y-%m-%dT%H:%M:%SZ")

if isinstance(value, str):
# Convert strings to unicode
return value.decode("utf-8")

return value

return self._visit_data(data, _outbound_visitor)
Expand Down
6 changes: 3 additions & 3 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _mock_http(self, data, headers=None, status=None):
return

if not isinstance(data, str):
data = json.dumps(data, ensure_ascii=False, encoding="utf-8")
data = json.dumps(data, ensure_ascii=False)

resp_headers = { 'cache-control': 'no-cache',
'connection': 'close',
Expand All @@ -149,8 +149,8 @@ def _assert_http_method(self, method, params, check_auth=True):
"""Asserts _http_request is called with the method and params."""
args, _ = self.sg._http_request.call_args
arg_body = args[2]
assert isinstance(arg_body, str)
arg_body = json.loads(arg_body)
assert isinstance(arg_body, bytes)
arg_body = json.loads(arg_body.decode())

arg_params = arg_body.get("params")

Expand Down
8 changes: 4 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_url(self):
# login:password@domain
auth_url = "%s%s@%s" % (self.uri_prefix, login_password, self.domain)
sg = api.Shotgun(auth_url, None, None, connect=False)
expected = "Basic " + base64.encodestring(login_password).strip()
expected = b"Basic " + base64.encodestring(login_password.encode()).strip()
self.assertEqual(expected, sg.config.authorization)

def test_authorization(self):
Expand All @@ -168,7 +168,7 @@ def test_authorization(self):
verb, path, body, headers = args

expected = "Basic " + base64.encodestring(login_password).strip()
self.assertEqual(expected, headers.get("Authorization"))
self.assertEqual(expected.encode(), headers.get("Authorization"))

def test_user_agent(self):
"""User-Agent passed to server"""
Expand Down Expand Up @@ -372,7 +372,7 @@ def test_encode_payload(self):
self.assertTrue(isinstance(j, bytes))

def test_decode_response_ascii(self):
self._assert_decode_resonse(True, "my data \u00E0".encode('utf8'))
self._assert_decode_resonse(True, "my data \u00E0".encode('utf8').decode())

def test_decode_response_unicode(self):
self._assert_decode_resonse(False, "my data \u00E0")
Expand All @@ -393,7 +393,7 @@ def _assert_decode_resonse(self, ensure_ascii, data):
ensure_ascii = ensure_ascii,
connect=False)

j = json.dumps(d, ensure_ascii=ensure_ascii, encoding="utf-8")
j = json.dumps(d, ensure_ascii=ensure_ascii)
self.assertEqual(d, sg._decode_response(headers, j))

headers["content-type"] = "text/javascript"
Expand Down