3

Let's assume you have the following Python2 Code:

#!/usr/bin/env python
import struct
struct.pack('s', 'hello')

This works fine under Python2, but it won't run under Python3, as the implementation of struct.pack changed. struct.pack now expects a bytes object for a string identifier and not a string any more. The fix for running this code under Python3 would be something like: struct.pack('s', bytes('hello', 'utf-8')). But again, this code will not run under Python2 :-)

I'm trying to code version independent as I don't want to force anybody to install python2 or 3.

What would be the best approach for getting this piece of code version independent?

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Nov 13, 2015 at 19:38
7
  • Python 2 str is a bytestring. It is not struct that changed, it is the Python string object. Python 2 unicode became str in Python 3, Python 2 str became bytes in Python 3. Commented Nov 13, 2015 at 19:41
  • So your problem is a generic how do I produce a bytestring in both Python 2 and 3 issue. b'hello' will work in both Python 2 and 3 if you are using literals. Commented Nov 13, 2015 at 19:41
  • Well in fact I use a variable rather than a literal. But wait, look at the documentation of struct. It states that the python types changed from string to bytes. Commented Nov 13, 2015 at 19:51
  • Yes, because the type was renamed, basically. Commented Nov 13, 2015 at 19:52
  • 1
    Yes, and the Python 3 str type is the same thing as unicode in Python 2. You'd encode unicode values in Python 2 too. Commented Nov 13, 2015 at 19:58

1 Answer 1

3

The Python 2 str type was basically renamed to bytes in Python 3, while the unicode type became str. You are in essence sending bytestrings in Python 2 and Unicode strings in Python 3 to struct.pack().

It could be the correct fix is to use Unicode everywhere in both Python 2 and 3 (and thus always encode when using struct.pack(), regardless of the Python version).

The alternative is to use an isinstance test:

value = 'hello'
if not isinstance(value, bytes):
 vaule = value.encode('utf8')

This works in both Python 2 and 3, because in Python 2 bytes is an alias for str.

answered Nov 13, 2015 at 19:56
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.