[pywin32-checkins] pywin32/Pythonwin/pywin/framework scriptutils.py, 1.23, 1.24
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Mark H. <mha...@us...> - 2009-01-31 05:30:20
|
Update of /cvsroot/pywin32/pywin32/Pythonwin/pywin/framework In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv30492/pywin/framework Modified Files: scriptutils.py Log Message: load scripts we run as bytes so Py3k auto-detects encoding decls Index: scriptutils.py =================================================================== RCS file: /cvsroot/pywin32/pywin32/Pythonwin/pywin/framework/scriptutils.py,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** scriptutils.py 8 Jan 2009 12:28:42 -0000 1.23 --- scriptutils.py 31 Jan 2009 05:30:16 -0000 1.24 *************** *** 27,30 **** --- 27,34 ---- Post-Mortem of unhandled exceptions""".split("\n") + byte_cr = "\r".encode("ascii") + byte_lf = "\n".encode("ascii") + byte_crlf = "\r\n".encode("ascii") + # A dialog box for the "Run Script" command. class DlgRunScript(dialog.Dialog): *************** *** 261,270 **** if not IsOnPythonPath(path): sys.path.append(path) try: ! f = open(script) except IOError, exc: win32ui.MessageBox("The file could not be opened - %s (%d)" % (exc.strerror, exc.errno)) return # Remember and hack sys.argv for the script. oldArgv = sys.argv --- 265,284 ---- if not IsOnPythonPath(path): sys.path.append(path) + # py3k fun: If we use text mode to open the file, we get \r\n + # translated so Python allows the syntax (good!), but we get back + # text already decoded from the default encoding (bad!) and Python + # ignores any encoding decls (bad!). If we use binary mode we get + # the raw bytes and Python looks at the encoding (good!) but \r\n + # chars stay in place so Python throws a syntax error (bad!). + # So: so the binary thing and manually normalize \r\n. try: ! f = open(script, 'rb') except IOError, exc: win32ui.MessageBox("The file could not be opened - %s (%d)" % (exc.strerror, exc.errno)) return + # Get the source-code - as above, normalize \r\n + code = f.read().replace(byte_crlf, byte_lf).replace(byte_cr, byte_lf) + byte_lf + # Remember and hack sys.argv for the script. oldArgv = sys.argv *************** *** 296,300 **** # at this point try: ! codeObject = compile(f.read()+"\n", script, "exec") except: # Almost certainly a syntax error! --- 310,314 ---- # at this point try: ! codeObject = compile(code, script, "exec") except: # Almost certainly a syntax error! |