From 5ab0e75f657ddd5beb5a0ca527874930981a47bc Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Fri, 20 Mar 2015 16:22:06 -0700 Subject: [PATCH] Try to detect if an object is a file type on python python 2 & 3. The base type for files changed between the two releases. --- htsworkflow/util/opener.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/htsworkflow/util/opener.py b/htsworkflow/util/opener.py index 6d5e2c0..c599975 100644 --- a/htsworkflow/util/opener.py +++ b/htsworkflow/util/opener.py @@ -4,9 +4,16 @@ Helpful utilities for turning random names/objects into streams. import os import gzip import bz2 -import types +import six from six.moves import urllib +if six.PY2: + import types + FILE_CLASS = types.FileType +else: + import io + FILE_CLASS = io.IOBase + def isfilelike(file_ref, mode): """Does file_ref have the core file operations? """ @@ -41,7 +48,7 @@ def autoopen(file_ref, mode='r'): Attempt to intelligently turn file_ref into a readable stream """ # catch being passed a file - if type(file_ref) is types.FileType: + if isinstance(file_ref, FILE_CLASS): return file_ref # does it look like a file? elif isfilelike(file_ref, mode): -- 2.30.2