ethereum / eth-abi

Ethereum ABI utilities for python
MIT License
248 stars 269 forks source link

fix: decode malformed strings #187

Closed banteg closed 1 year ago

banteg commented 2 years ago

What was wrong?

decoding was failing for some malformed strings.

you can see an example of this if you try to decode logs from mainnet transaction 0x505870232ebd6cefd2a59c760924664212f72759e58fd2df82d61b67ffe0dd75.

it should be possible to read the useful data even from a malformed input.

How was it fixed?

added errors="surrogateescape" to decode as suggested here https://peps.python.org/pep-0383/

i deliberately skipped adding the same to encode because passing an unencodable string usually signifies a user error.

To-Do

Cute Animal Picture

out-3-2

pacrob commented 1 year ago

I've updated the class as described and adjusted testing, let me know if you have any thoughts. I'll work on an example for the docs tomorrow.

pacrob commented 1 year ago

Not too complex, got it added. User can now call the existing string decoder like you wrote, or add multiple StringDecoders with different behaviors to the registry. The default behavior has been returned to the default strict.

Not sure about the docs location - it could be appropriate to put it in the Decoding section too. Thoughts?

pacrob commented 1 year ago

You are correct, the defaults were not updated, I must not have saved properly. I've added a test to make sure of it now.

I've removed the errors argument from the top-level decode function as I don't see a way forward for it, but we can discuss further.

pacrob commented 1 year ago

Thanks @banteg !