Skip to content

Commit 9d2cb77

Browse files
committed
{AH} fix py3 StringIO issues
1 parent 11976c2 commit 9d2cb77

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

pysam/calignmentfile.pyx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ import collections
5757
import re
5858
import warnings
5959
import array
60-
import StringIO
6160

6261
from cpython cimport array as c_array
6362
from cpython.version cimport PY_MAJOR_VERSION
@@ -67,6 +66,11 @@ from pysam.cutils cimport encode_filename, from_string_and_size
6766
from pysam.calignedsegment cimport makeAlignedSegment, makePileupColumn
6867
from pysam.chtslib cimport hisremote
6968

69+
if PY_MAJOR_VERSION >= 3:
70+
from io import StringIO
71+
else:
72+
from StringIO import StringIO
73+
7074
cimport cython
7175

7276
########################################################
@@ -408,17 +412,18 @@ cdef class AlignmentFile:
408412
if self.htsfile != NULL:
409413
self.close()
410414

411-
# check if we are working with a File object
412-
if hasattr(filepath_or_object, "fileno"):
413-
filename = filepath_or_object.name
414-
if filepath_or_object.closed:
415-
raise ValueError('I/O operation on closed file')
416-
elif isinstance(filepath_or_object, StringIO.StringIO):
415+
# StringIO not supported
416+
if isinstance(filepath_or_object, StringIO):
417417
filename = "stringio"
418418
raise NotImplementedError(
419419
"access from StringIO objects not supported")
420420
if filepath_or_object.closed:
421421
raise ValueError('I/O operation on closed StringIO object')
422+
# check if we are working with a File object
423+
elif hasattr(filepath_or_object, "fileno"):
424+
filename = filepath_or_object.name
425+
if filepath_or_object.closed:
426+
raise ValueError('I/O operation on closed file')
422427
else:
423428
filename = filepath_or_object
424429

tests/AlignmentFile_test.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
import subprocess
1414
import logging
1515
import array
16-
import StringIO
16+
if sys.version_info.major >= 3:
17+
from io import StringIO
18+
else:
19+
from StringIO import StringIO
20+
1721
from functools import partial
1822

1923
import pysam
@@ -375,8 +379,11 @@ def testRaises(self):
375379
statement = "samtools view -h {}".format(
376380
os.path.join(DATADIR, "ex3.bam"))
377381
stdout = subprocess.check_output(statement.split(" "))
378-
bam = StringIO.StringIO()
379-
bam.write(stdout)
382+
bam = StringIO()
383+
if sys.version_info.major >= 3:
384+
bam.write(stdout.decode('ascii'))
385+
else:
386+
bam.write(stdout)
380387
bam.seek(0)
381388
self.assertRaises(NotImplementedError,
382389
pysam.AlignmentFile, bam)

0 commit comments

Comments
 (0)