[Python-bugs-list] [ python-Bugs-825676 ] code.InteractiveConsole
interprets escape chars incorrectly
SourceForge.net
noreply at sourceforge.net
Sat Oct 18 05:59:28 EDT 2003
Bugs item #825676, was opened at 2003-10-17 20:50
Message generated for change (Comment added) made by johahn
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=825676&group_id=5470
Category: Python Library
Group: Python 2.2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Mark Richardson (markrichardson)
Assigned to: Nobody/Anonymous (nobody)
Summary: code.InteractiveConsole interprets escape chars incorrectly
Initial Comment:
code.InteractiveConsole interprets escape characters
incorrectly. For example, it interprets "\t" the same
as "\t", so it prints a tab instead of a backslash t.
to reproduce:
import sys
import code
class MyConsole(code.InteractiveConsole):
def __init__(self):
code.InteractiveConsole.__init__(self)
# I tried it with runsource too. Same result.
def run_code(self, cmdString):
self.runcode(cmdString)
def write(self, data):
sys.__stdout__.write(data)
instance = MyConsole()
instance.run_code('print "\thello\tworld"')
print "\thello\tworld"
----------------------------------------------------------------------
Comment By: Johan M. Hahn (johahn)
Date: 2003-10-18 11:59
Message:
Logged In: YES
user_id=887415
You should use the r prefix when passing strings to
InteractiveConsole to prevent the string from beeing parsed
twice. This works fine:
>>> instance.run_code(r'print "\thello\tworld"')
The strange behaviour you are experiencing lies with the
exec statement... it boiles down to:
>>> exec 'print"\thello\tworld"'
hello world
>>> print "\thello\tworld"
\thello\tworld
The cause is that the inner string in the first call is parsed
twice... first double backslash '\' is reduced to '\' prior to the
exec statement is executed... then before print is called the
string is parsed again and '\t' becomes a tab. I wouldn't
classify this as a bug, rather a gotcha. Think of what this line
means:
>>> exec 'print "hello\nworld"'
...johahn
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=825676&group_id=5470
More information about the Python-bugs-list
mailing list