Skip to content

Commit 80a30d2

Browse files
SG-39698 Remove try/except sgutil blocks and ensure_ methods (#297)
1 parent 0748b0a commit 80a30d2

5 files changed

Lines changed: 34 additions & 54 deletions

File tree

python/tk_framework_desktopserver/command.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919

2020
import sgtk.util
2121

22-
try:
23-
from tank_vendor import sgutils
24-
except ImportError:
25-
from tank_vendor import six as sgutils
26-
2722
logger = get_logger(__name__)
2823

2924

@@ -49,7 +44,9 @@ def run(self):
4944
is closed.
5045
"""
5146
while True:
52-
line = sgutils.ensure_str(self.pipe.readline()) # blocking read
47+
line = self.pipe.readline() # blocking read
48+
if isinstance(line, bytes):
49+
line = line.decode("utf-8")
5350
if line == "":
5451
break
5552
self.target_queue.put(line)

python/tk_framework_desktopserver/server.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727

2828
from sgtk.platform.qt import QtCore
2929

30-
try:
31-
from tank_vendor import sgutils
32-
except ImportError:
33-
from tank_vendor import six as sgutils
34-
3530
logger = get_logger(__name__)
3631

3732

@@ -107,8 +102,8 @@ def __init__(
107102
if encrypt:
108103
# urandom is considered cryptographically secure as it calls the OS's CSRNG, so we can
109104
# use that to generate our own server id.
110-
self._ws_server_id = sgutils.ensure_str(
111-
base64.urlsafe_b64encode(os.urandom(16))
105+
self._ws_server_id = base64.urlsafe_b64encode(os.urandom(16)).decode(
106+
"utf-8"
112107
)
113108
else:
114109
self._ws_server_id = None

python/tk_framework_desktopserver/server_protocol.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
from .message_host import MessageHost
2525
from .process_manager import ProcessManager
2626

27-
try:
28-
from tank_vendor import sgutils
29-
except ImportError:
30-
from tank_vendor import six as sgutils
31-
3227
logger = get_logger(__name__)
3328

3429

@@ -166,7 +161,9 @@ def _on_message(self, payload, is_binary):
166161
logger.exception("Unexpected error while decrypting:")
167162
return
168163

169-
decoded_payload = sgutils.ensure_str(payload)
164+
decoded_payload = (
165+
payload.decode("utf-8") if isinstance(payload, bytes) else str(payload)
166+
)
170167

171168
# Special message to get protocol version for this protocol. This message doesn't follow the
172169
# standard message format as it doesn't require a protocol version to be retrieved and is
@@ -360,7 +357,12 @@ def _retrieve_server_secret(self):
360357
response = shotgun._call_rpc(
361358
"retrieve_ws_server_secret", {"ws_server_id": self.factory.ws_server_id}
362359
)
363-
ws_server_secret = sgutils.ensure_str(response["ws_server_secret"])
360+
ws_server_secret = response["ws_server_secret"]
361+
ws_server_secret = (
362+
response["ws_server_secret"].decode("utf-8")
363+
if isinstance(ws_server_secret, bytes)
364+
else ws_server_secret
365+
)
364366
# FIXME: Server doesn't seem to provide a properly padded string. The Javascript side
365367
# doesn't seem to complain however, so I'm not sure whose implementation is broken.
366368
if ws_server_secret[-1] != "=":
@@ -447,13 +449,11 @@ def json_reply(self, data):
447449
:param data: Object Data that will be converted to JSON and sent to client.
448450
"""
449451
# ensure_ascii allows unicode strings.
450-
payload = sgutils.ensure_binary(
451-
json.dumps(
452-
data,
453-
ensure_ascii=True,
454-
default=self._json_date_handler,
455-
)
456-
)
452+
payload = json.dumps(
453+
data,
454+
ensure_ascii=True,
455+
default=self._json_date_handler,
456+
).encode("utf-8")
457457

458458
if self._fernet:
459459
payload = self._fernet.encrypt(payload)

python/tk_framework_desktopserver/shotgun/api_v2.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@
3434
from . import constants
3535
from .. import command
3636

37-
try:
38-
from tank_vendor import sgutils
39-
except ImportError:
40-
from tank_vendor import six as sgutils
41-
4237
logger = sgtk.platform.get_logger(__name__)
4338

4439

@@ -316,9 +311,8 @@ def _execute_action(self, data):
316311
# message that wasn't on the first line of text before any newlines.
317312
for line in stdout.split("\n") + stderr.split("\n"):
318313
if line.startswith(tag):
319-
filtered_output.append(
320-
sgutils.ensure_str(base64.b64decode(line[tag_length:]))
321-
)
314+
decoded = base64.b64decode(line[tag_length:]).decode("utf-8")
315+
filtered_output.append(decoded)
322316

323317
filtered_output_string = "\n".join(filtered_output)
324318

@@ -632,8 +626,9 @@ def _get_actions(self, data):
632626
if cached_data:
633627
# The value will be bytes
634628
# ensure_str doesn't accept a buffer as input
635-
string_data = sgutils.ensure_str(cached_data[0])
636-
629+
string_data = cached_data[0]
630+
if isinstance(string_data, bytes):
631+
string_data = string_data.decode("utf-8")
637632
try:
638633
decoded_data = sgtk.util.json.loads(string_data)
639634
except Exception:
@@ -1030,7 +1025,7 @@ def _write_commands_to_db(self, commands, config_data, contents_hash):
10301025

10311026
connection.commit()
10321027

1033-
commands_blob = sqlite3.Binary(sgutils.ensure_binary(json.dumps(commands)))
1028+
commands_blob = sqlite3.Binary(json.dumps(commands).encode("utf-8"))
10341029

10351030
# Since we're likely to be updating out-of-date cached data more
10361031
# often than we're going to be inserting new rows into the cache,
@@ -1265,10 +1260,10 @@ def _get_contents_hash(self, config_descriptor, entities):
12651260
logger.debug("Contents data to be used in hash generation: %s", json_data)
12661261

12671262
hash_data = hashlib.md5()
1268-
hash_data.update(sgutils.ensure_binary(json_data))
1263+
hash_data.update(json_data.encode("utf-8"))
12691264
# Base64 encode the digest, will is a binary string
12701265
# in Python 3. This ensures we can always encode it to a str.
1271-
return sgutils.ensure_str(base64.b64encode(hash_data.digest()))
1266+
return base64.b64encode(hash_data.digest()).decode("utf-8")
12721267

12731268
def _get_entities_from_payload(self, data):
12741269
"""
@@ -1541,7 +1536,7 @@ def _get_exception_message(self):
15411536
)
15421537

15431538
if self._global_debug:
1544-
message = sgutils.ensure_binary(html.escape(traceback.format_exc()))
1539+
message = html.escape(traceback.format_exc()).encode("utf-8")
15451540

15461541
return message
15471542

@@ -2178,7 +2173,7 @@ def _legacy_sanitize_output(self, out):
21782173
line = re.sub(bold_match, "*", line)
21792174
sanitized.append(line)
21802175

2181-
return sgutils.ensure_binary(html.escape("\n".join(sanitized)))
2176+
return html.escape("\n".join(sanitized)).encode("utf-8")
21822177

21832178
@sgtk.LogManager.log_timing
21842179
def _process_commands(self, commands, project, entities):

python/tk_framework_desktopserver/shotgun/scripts/execute_command.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def format(self, *args, **kwargs):
3939
result = super().format(*args, **kwargs)
4040
return "%s%s" % (
4141
LOGGING_PREFIX,
42-
sgutils.ensure_str(base64.b64encode(sgutils.ensure_binary(result))),
42+
base64.b64encode(result.encode("utf-8")).decode("utf-8"),
4343
)
4444

4545

@@ -322,7 +322,6 @@ def execute(
322322
# We need to make sure that we're not introducing unicode into the
323323
# environment. This cropped up with some studio-team apps that ended
324324
# up causing some hangs on launch.
325-
core_root = sgutils.ensure_str(core_root)
326325

327326
sgtk.util.prepend_path_to_env_var(
328327
"PYTHONPATH",
@@ -357,11 +356,10 @@ def execute(
357356
# match the project's config path instead.
358357
config_path = engine.sgtk.pipeline_configuration.get_path()
359358

360-
# We need to make sure that we don't introduce unicode into the
361-
# environment. This appears to happen at times, likely due to some
362-
# component of the path built by pipeline_configuration "infecting"
363-
# the resulting aggregate path.
364-
config_path = sgutils.ensure_str(config_path)
359+
# Ensure that no unicode characters are introduced into the environment.
360+
# This sometimes happens when part of the path from pipeline_configuration
361+
# "contaminates" the final aggregated path.
362+
config_path = str(config_path)
365363

366364
os.environ["TANK_CURRENT_PC"] = config_path
367365

@@ -389,11 +387,6 @@ def execute(
389387
try:
390388
sys.path = [arg_data["sys_path"]] + sys.path
391389
import sgtk
392-
393-
try:
394-
from tank_vendor import sgutils
395-
except ImportError:
396-
from tank_vendor import six as sgutils
397390
finally:
398391
sys.path = original_sys_path
399392

0 commit comments

Comments
 (0)