question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unified approach to string formatting.

See original GitHub issue
  • Version: 3.15.0
  • Python: 2.7/3.4/3.5

What was wrong?

We need a well though through string handling solution for bytesXX, bytes and string types being passed into functions as arguments.

Currently, users expect to be able to provide hex encoded strings to some of these methods which either fails due to these methods expecting bytestrings, or it appears to succeed when in fact it has sent the provided hex-string as-is.

How can it be fixed?

For web3.sha3 and web3.eth.sign we have a workable solution. See https://github.com/pipermerriam/web3.py/issues/289

For bytesXX we should be able to be smart and detect hex encoded values.

  • If the value is a byte string, validate len(v) == XX where XX is the bytesXX length.
  • If the value is a text string, assume hex encoding. validate len(v_without_0x_prefix) == 2 * XX where XX is the bytesXX length.

For bytes types.

  • If the value is a byte string, pass it through unmodified.
  • If the value is a text string, validate that it is a valid hexidecimal encoded value.

For string types.

  • If the value is a byte string, pass it through unmodified.
  • If the value is a text string, decode it as utf8 into it’s bytes representation.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
pipermerriamcommented, Sep 29, 2017

I’ve updated the initial issue description to reflect this change.

2reactions
pipermerriamcommented, Sep 29, 2017

@veox I believe what we are trying to support is both of the following:

  • demo.transact().setString("0xtentacle"); # that must be a unicode string (so in python2 u"0xtent...")
  • demo.transact().setString(b"\x30\x78\x74\x65\x6e\x74\x61\x63\x6c\x65");
Read more comments on GitHub >

github_iconTop Results From Across the Web

String Formatting – Programming Fundamentals - Rebus Press
String formatting uses a process of string interpolation (variable substitution) to evaluate a string literal containing one or more placeholders, ...
Read more >
Unified method to parse string in any ISO-8601 compliant format
I need a unified method to parse any ISO-8601 compliant string. Unfortunately, neither Joda nor fastxml StdDateFormat is fully qualified to ...
Read more >
Standard numeric format strings | Microsoft Learn
Format method, and the StringBuilder.AppendFormat method. The composite format feature allows you to include the string representation of ...
Read more >
String Formatting in Python - Scaler Topics
Another way to format strings in Python is to use the format() method. In this method, first, we put placeholders in the string...
Read more >
Python string formatting - Medium
The basic way. The simplest method for concatenating strings and variables is the following: >>> print("hello" + " " + "world ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found