4

I am trying to refactor a python 2 package for use with python-3.x. The package uses StringIO.StringIO under python 2 and makes some use of the object's relative seek method, with statements like flob.seek(-1, 1). Unfortunately, the seek method of the corresponding io.StringIO object in python 3 does not support relative seeks, so the code raises

OSError: Can't do nonzero cur-relative seeks

when trying to execute that statement.

What is the best way to refactor modules containing these calls, given that I would like to be able to continue using the functions this appears in for file objects as well as (objects derived from) strings?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Anaphory
  • 6,045
  • 4
  • 37
  • 68

1 Answers1

11

Because strings in Python 2 are renamed bytes in Python 3, the code should use io.BytesIO in Python 3, which supports relative seeking.

Ramchandra Apte
  • 4,033
  • 2
  • 24
  • 44