All those little spots where you use strings...

x.split( '.' )
Is an idiom that shows up all over the place, particularly in list comprehensions. Run your code through 2to3 and it fails when x is a str (bytes) object, as 3.x thinks the '.' is unicode. It fails with a rather cryptic TypeError: Type str doesn't support the buffer API error as it's apparently trying to convert '.' using the Py_Buffer protocol rather than up-converting x to unicode. The number of places this needs to be tracked down (I constantly sling strings around and there are lots of string literals around in the codebase) to continue porting PyOpenGL and OpenGLContext has put me into "meh" on the project for now.

Comments

  1. Brian Harring

    Brian Harring on 08/22/2010 5:24 a.m. #

    The annoying thing about supporting both py2k and py3k is that you wind up having to get rather explicit about your types passed in- mostly deciding that function xyz is going to take bytes, function abc taking strings.

    Either way, if you're doing >=python2.6, you could use b'' syntax in those cases- 2to3 obviously leaves it alone, in py2k it's a str, in py3k it's bytes.

    If you're trying to support <py2.6, a force_bytes function comes to mind- it's not great, but roughly:

    if sys.version_info >= (3,0):
    force_bytes = lambda x:x.encode("utf8")
    else:
    force_bytes = str

    Using something similar to that provides a way to maintain a fairly large range of compatibility, although the hardcoded encoding there can be problematic.

  2. Brian Harring

    Brian Harring on 08/22/2010 5:28 a.m. #

    Your blog aparently likes doesn't handle the less than char (&lt;) conversion all that well.. everything following is automatically stripped from the comment. Finishing my original comment, if you're trying to support python2.4/2.5, use something similar-

    if sys.version_info >= (3,0:
    force_bytes = lambda x:x.encode("utf8")
    else:
    force_bytes = str

    x.split(force_bytes("."))

  3. Mike C. Fletcher

    Mike C. Fletcher on 08/22/2010 7:40 a.m. #

    Yeah, that's what I started doing (the function to convert to bytes), as I support down to 2.4. Thing is this is one of those "subtle corner case fails 2 years later" changes where you have to track down every possible use and uglify it... that made me meh.

  4. Brian Harring

    Brian Harring on 08/23/2010 7:57 p.m. #

    Yeah, in a similar boat with the code I write... upshot, I've got some pretty good compat code in snakeoil, just was a pain building it out over the years.

Comments are closed.

Pingbacks

Pingbacks are closed.