Author: bugman Date: Sun Oct 7 18:12:33 2012 New Revision: 17718 URL: http://svn.gna.org/viewcvs/relax?rev=17718&view=rev Log: Python 3 fix for the relax_io.open_write_file() function. This now matches the behaviour of open_read_file() in that there are three different behaviours for opening bz2 and gz files for writing to for the different Python versions (one for Python 2, one for Python 3.0 to 3.2, and one for Python 3.3+). All byte streams have been eliminated as open_write_file() is for creating text files. Modified: trunk/relax_io.py Modified: trunk/relax_io.py URL: http://svn.gna.org/viewcvs/relax/trunk/relax_io.py?rev=17718&r1=17717&r2=17718&view=diff ============================================================================== --- trunk/relax_io.py (original) +++ trunk/relax_io.py Sun Oct 7 18:12:33 2012 @@ -395,6 +395,7 @@ else: file_obj = gzip.GzipFile(file_path, 'r') + # Cannot open. except IOError: message = sys.exc_info()[1] raise RelaxError("Cannot open the file " + repr(file_path) + ". " + message.args[1] + ".") @@ -484,14 +485,43 @@ # Open the file for writing. try: + # Print out. if verbosity: print(("Opening the file " + repr(file_path) + " for writing.")) + + # Uncompressed text. if compress_type == 0: file_obj = open(file_path, 'w') + + # Bzip2 compressed text. elif compress_type == 1: - file_obj = bz2.BZ2File(file_path, 'w') + # Python 3.3 text mode. + if sys.version_info[0] == 3 and sys.version_info[1] >= 3: + file_obj = bz2.open(file_path, 'wt') + + # 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, 'w')) + + # Python 2 text mode. + else: + file_obj = bz2.BZ2File(file_path, 'w') + + # Gzipped compressed text. elif compress_type == 2: - file_obj = gzip.GzipFile(file_path, 'w') + # Python 3.3 text mode. + if sys.version_info[0] == 3 and sys.version_info[1] >= 3: + file_obj = gzip.open(file_path, 'wt') + + # 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, 'w')) + + # Python 2 text mode. + else: + file_obj = gzip.GzipFile(file_path, 'w') + + # Cannot open. except IOError: message = sys.exc_info()[1] raise RelaxError("Cannot open the file " + repr(file_path) + ". " + message.args[1] + ".")