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"
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