Opened 8 years ago
Closed 8 years ago
#972 closed defect (nonchimerax)
open() opens text as ascii rather than unicode when Mac build is run from the GUI
| Reported by: | Tristan Croll | Owned by: | Tom Goddard |
|---|---|---|---|
| Priority: | major | Milestone: | |
| Component: | Core | Version: | |
| Keywords: | Cc: | ||
| Blocked By: | Blocking: | ||
| Notify when closed: | Platform: | all | |
| Project: | ChimeraX |
Description
With a fresh installation of today's ChimeraX 0.5 on the Mac:
If I run ChimeraX by double-clicking on the icon in Finder, install ISOLDE via the ToolShed then run ISOLDE, I get the traceback copied below. The offending lines are:
infile = open(file_prefix+'.data', 'r')
lines = [line.rstrip('\n ').split(' ') for line in infile]
where infile contains a number of unicode characters (Greek letters, degree symbol). If I instead run ChimeraX from the terminal (/Applications/ChimeraX.app/Contents/bin/ChimeraX) and run ISOLDE, it works without issue. Note that this text file should only ever be read once - I distribute all my data files as plain text for robustness, and pickle the necessary objects on first run using the logic below.
try:
infile = open(file_prefix+'.pickle', 'r+b')
rotamers = pickle.load(infile)
infile.close()
infile = None
except:
# Load in and process the data from a text file, and pickle the result for fast loading next time
Traceback:
Traceback (most recent call last):
File "/Users/tic20/Library/Application Support/ChimeraX/0.5/site-packages/chimerax/isolde/rotamers.py", line 77, in load_rotamers
rotamers = pickle.load(infile)
ModuleNotFoundError: No module named 'src'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Applications/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chimerax/core/toolshed/info.py", line 481, in start_tool
ti = api._api_caller.start_tool(api, session, self, tool_info)
File "/Applications/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chimerax/core/toolshed/__init__.py", line 953, in start_tool
return cls._get_func(api, "start_tool")(session, ti.name)
File "/Users/tic20/Library/Application Support/ChimeraX/0.5/site-packages/chimerax/isolde/__init__.py", line 29, in start_tool
return tools.get_singleton(session, ISOLDE_ToolUI, 'ISOLDE', create=True)
File "/Applications/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chimerax/core/tools.py", line 214, in get_singleton
tinst = tool_class(session, tool_name, **kw)
File "/Users/tic20/Library/Application Support/ChimeraX/0.5/site-packages/chimerax/isolde/tool.py", line 167, in __init__
from . import isolde
File "/Users/tic20/Library/Application Support/ChimeraX/0.5/site-packages/chimerax/isolde/isolde.py", line 45, in <module>
from . import rotamers, dihedrals
File "/Users/tic20/Library/Application Support/ChimeraX/0.5/site-packages/chimerax/isolde/rotamers.py", line 630, in <module>
_rotamer_info = load_rotamers(_rot_file_prefix)
File "/Users/tic20/Library/Application Support/ChimeraX/0.5/site-packages/chimerax/isolde/rotamers.py", line 84, in load_rotamers
lines = [line.rstrip('\n ').split(' ') for line in infile]
File "/Users/tic20/Library/Application Support/ChimeraX/0.5/site-packages/chimerax/isolde/rotamers.py", line 84, in <listcomp>
lines = [line.rstrip('\n ').split(' ') for line in infile]
File "/Applications/ChimeraX.app/Contents/MacOS/../Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcf in position 30: ordinal not in range(128)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Applications/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chimerax/core/ui/gui.py", line 781, in <lambda>
run(ses, "toolshed show %s" % quote_if_necessary(tool_name)))
File "/Applications/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chimerax/core/commands/run.py", line 31, in run
results = command.run(text, log=log)
File "/Applications/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chimerax/core/commands/cli.py", line 2569, in run
result = ci.function(session, **kw_args)
File "/Applications/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chimerax/core/commands/toolshed.py", line 334, in toolshed_show
bi.start_tool(session, tool_name)
File "/Applications/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chimerax/core/toolshed/info.py", line 487, in start_tool
"start_tool() failed for tool %s in bundle %s:\n%s" % (tool_name, self.name, str(e)))
chimerax.core.toolshed.ToolshedError: start_tool() failed for tool ISOLDE in bundle ChimeraX-ISOLDE:
'ascii' codec can't decode byte 0xcf in position 30: ordinal not in range(128)
Take a look at the Python 3 open() documentation.
It says that the encoding for a file open in "r" mode is platform dependent, obtained by
A user can change this by setting their locale. So your code cannot assume a certain text encoding used by open() and has to specify the open() encoding option if you want it to work reliably.