Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: specify encoding when calling bytes [APE-1186] #1539

Merged
merged 5 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 16 additions & 4 deletions src/ape/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,23 @@ def __repr__(self) -> str:
def __str__(self) -> str:
data = self.dict()
if len(data["data"]) > 9:
data["data"] = (
"0x" + bytes(data["data"][:3]).hex() + "..." + bytes(data["data"][-3:]).hex()
)
# only want to specify encoding if data["data"] is a string
if isinstance(data["data"], str):
data["data"] = (
"0x"
+ bytes(data["data"][:3], encoding="utf8").hex()
+ "..."
+ bytes(data["data"][-3:], encoding="utf8").hex()
)
else:
data["data"] = (
"0x" + bytes(data["data"][:3]).hex() + "..." + bytes(data["data"][-3:]).hex()
)
else:
data["data"] = "0x" + bytes(data["data"]).hex()
if isinstance(data["data"], str):
data["data"] = "0x" + bytes(data["data"], encoding="utf8").hex()
else:
data["data"] = "0x" + bytes(data["data"]).hex()
params = "\n ".join(f"{k}: {v}" for k, v in data.items())
return f"{self.__class__.__name__}:\n {params}"

Expand Down
10 changes: 10 additions & 0 deletions tests/functional/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ def test_transaction_dict_excludes_none_values():
txn.value = None # type: ignore
actual = txn.dict()
assert "value" not in actual


def test_txn_str_when_data_is_bytes(ethereum):
"""
Tests against a condition that would cause transactions to
fail with string-encoding errors.
"""
txn = ethereum.create_transaction(data=HexBytes("0x123"))
actual = str(txn)
assert isinstance(actual, str)
Loading