Skip to content

Commit 0ced372

Browse files
authored
Replace .decode method with str(bytes, "utf-8") (#1998)
* Replace .decode method with str(bytes, "utf-8") This is because some bytes-like objects (such as memoryview) don't have a "decode()" method. It appears that the preferred way to get a decoded string is to simply call the constructor with the bytes-like object and the desired encoding.
1 parent 22fc2c2 commit 0ced372

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

cwltool/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def content_limit_respected_read(f: IO[bytes]) -> str:
8686
:returns: the file contents
8787
:raises WorkflowException: if the file is too large
8888
"""
89-
return content_limit_respected_read_bytes(f).decode("utf-8")
89+
return str(content_limit_respected_read_bytes(f), "utf-8")
9090

9191

9292
def substitute(value: str, replace: str) -> str:

cwltool/command_line_tool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,8 +1369,8 @@ def collect_output(
13691369
else:
13701370
if binding.get("loadContents"):
13711371
with fs_access.open(cast(str, rfile["location"]), "rb") as f:
1372-
files["contents"] = content_limit_respected_read_bytes(f).decode(
1373-
"utf-8"
1372+
files["contents"] = str(
1373+
content_limit_respected_read_bytes(f), "utf-8"
13741374
)
13751375
if compute_checksum:
13761376
with fs_access.open(cast(str, rfile["location"]), "rb") as f:

cwltool/cwlrdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def printrdf(wflow: Process, ctx: ContextType, style: str) -> str:
2727
rdf = gather(wflow, ctx).serialize(format=style, encoding="utf-8")
2828
if not rdf:
2929
return ""
30-
return rdf.decode("utf-8")
30+
return str(rdf, "utf-8")
3131

3232

3333
def lastpart(uri: Any) -> str:

cwltool/docker.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ def get_image(
116116
if (docker_image_id := docker_requirement.get("dockerImageId")) is not None:
117117
try:
118118
manifest = json.loads(
119-
subprocess.check_output(
120-
[self.docker_exec, "inspect", docker_image_id]
121-
).decode( # nosec
122-
"utf-8"
119+
str(
120+
subprocess.check_output(
121+
[self.docker_exec, "inspect", docker_image_id]
122+
), # nosec
123+
"utf-8",
123124
)
124125
)
125126
found = manifest is not None

cwltool/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def bytes2str_in_dicts(
200200

201201
# if value is bytes, return decoded string,
202202
elif isinstance(inp, bytes):
203-
return inp.decode("utf-8")
203+
return str(inp, "utf-8")
204204

205205
# simply return elements itself
206206
return inp

tests/test_path_checks.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
from cwltool.main import main
1313
from cwltool.stdfsaccess import StdFsAccess
1414
from cwltool.update import INTERNAL_VERSION
15-
from cwltool.utils import CWLObjectType
15+
from cwltool.utils import CWLObjectType, CONTENT_LIMIT, bytes2str_in_dicts
16+
from cwltool.builder import content_limit_respected_read
17+
from cwltool.errors import WorkflowException
1618

1719
from .util import needs_docker
1820

@@ -214,3 +216,26 @@ def test_clt_returns_specialchar_names(tmp_path: Path) -> None:
214216
result["location"]
215217
== "keep:ae755cd1b3cff63152ff4200f4dea7e9+52/%3A%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D"
216218
)
219+
220+
221+
def test_content_limit_respected_read() -> None:
222+
b1 = b"abcd" * 100
223+
b1io = BytesIO(b1)
224+
225+
assert len(b1) < CONTENT_LIMIT
226+
assert content_limit_respected_read(b1io) == str("abcd" * 100)
227+
228+
b2 = b"abcd" * 20000
229+
b2io = BytesIO(b2)
230+
231+
assert len(b2) > CONTENT_LIMIT
232+
with pytest.raises(WorkflowException):
233+
content_limit_respected_read(b2io)
234+
235+
236+
def test_bytes2str_in_dicts() -> None:
237+
assert bytes2str_in_dicts({"foo": b"bar"}) == {"foo": "bar"}
238+
239+
assert bytes2str_in_dicts({"foo": [b"bar"]}) == {"foo": ["bar"]}
240+
241+
assert bytes2str_in_dicts({"foo": {"foo2": b"bar"}}) == {"foo": {"foo2": "bar"}}

0 commit comments

Comments
 (0)