Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

relative seek for io.StringIO in python3

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?

like image 586
Anaphory Avatar asked Oct 19 '25 07:10

Anaphory


1 Answers

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

like image 55
Ramchandra Apte Avatar answered Oct 21 '25 22:10

Ramchandra Apte