Author: bugman Date: Sun Oct 7 15:40:50 2012 New Revision: 17711 URL: http://svn.gna.org/viewcvs/relax?rev=17711&view=rev Log: The relax_io.open_read_file() now supports all Python versions over 2.4. This required some really nasty hacks for Python 3.0, 3.1 and 3.2 with the Bzip2Fixed and GzipFixed classes overriding the incomplete and buggy bz2.BZ2File and gzip.GzipFile modules, and being wrapped around io.TextIOWrapper(). Modified: trunk/relax_io.py Modified: trunk/relax_io.py URL: http://svn.gna.org/viewcvs/relax/trunk/relax_io.py?rev=17711&r1=17710&r2=17711&view=diff ============================================================================== --- trunk/relax_io.py (original) +++ trunk/relax_io.py Sun Oct 7 15:40:50 2012 @@ -31,11 +31,13 @@ # Python module imports. if dep_check.bz2_module: - from bz2 import BZ2File + import bz2 if dep_check.gzip_module: - from gzip import GzipFile + import gzip if dep_check.devnull_import: from os import devnull +if dep_check.io_module: + import io from os import F_OK, X_OK, access, altsep, getenv, makedirs, pathsep, remove, sep from os.path import expanduser, basename, splitext from re import match, search @@ -355,17 +357,45 @@ # Open the file for reading. try: + # Print out. if verbosity: print(("Opening the file " + repr(file_path) + " for reading.")) + + # Uncompressed text. if compress_type == 0: file_obj = open(file_path, 'r') + + # Bzip2 compressed text. elif compress_type == 1: if dep_check.bz2_module: - file_obj = BZ2File(file_path, 'r') + # Python 3.3 text mode. + if sys.version_info[0] == 3 and sys.version_info[1] >= 3: + file_obj = bz2.open(file_path, 't') + + # Python 3.0, 3.1 and 3.2 text mode. + elif sys.version_info[0] == 3 and sys.version_info[1] < 3: + file_obj = io.TextIOWrapper(Bzip2Fixed(file_path, 'r')) + + # Python 2 text mode. + else: + file_obj = bz2.BZ2File(file_path, 'r') else: raise RelaxError("Cannot open the file " + repr(file_path) + ", try uncompressing first. " + dep_check.bz2_module_message + ".") + + # Gzipped compressed text. elif compress_type == 2: - file_obj = GzipFile(file_path, 'r') + # Python 3.3 text mode. + if sys.version_info[0] == 3 and sys.version_info[1] >= 3: + file_obj = gzip.open(file_path, 'rt') + + # Python 3.0, 3.1 and 3.2 text mode. + elif sys.version_info[0] == 3 and sys.version_info[1] < 3: + file_obj = io.TextIOWrapper(GzipFixed(file_path, 'r')) + + # Python 2 text mode. + else: + file_obj = gzip.GzipFile(file_path, 'r') + except IOError: message = sys.exc_info()[1] raise RelaxError("Cannot open the file " + repr(file_path) + ". " + message.args[1] + ".") @@ -460,9 +490,9 @@ if compress_type == 0: file_obj = open(file_path, 'w') elif compress_type == 1: - file_obj = BZ2File(file_path, 'w') + file_obj = bz2.BZ2File(file_path, 'w') elif compress_type == 2: - file_obj = GzipFile(file_path, 'w') + file_obj = gzip.GzipFile(file_path, 'w') except IOError: message = sys.exc_info()[1] raise RelaxError("Cannot open the file " + repr(file_path) + ". " + message.args[1] + ".") @@ -1131,6 +1161,45 @@ +class Bzip2Fixed(bz2.BZ2File): + """Incredibly nasty hack for bzip2 files support in Python 3.0, 3.1 and 3.2.""" + + def flush(self): + pass + + def read1(self, n): + return self.read(n) + + def readable(self): + return True + + def seekable(self): + return True + + def writable(self): + return True + + + +class GzipFixed(gzip.GzipFile): + """Incredibly nasty hack for gzipped files support in Python 3.0, 3.1 and 3.2.""" + + closed = False + + def read1(self, n): + return self.read(n) + + def readable(self): + return True + + def seekable(self): + return True + + def writable(self): + return True + + + class SplitIO: def __init__(self): """Class for splitting an IO stream to two outputs."""