Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Writing unicode characters to a log file. (IPython 0.10.2.git) #249

Merged
2 commits merged into from

4 participants

@TvrtkoM

Example:

When trying to log a statements like this during logging:

In [2]: logstart -o logtest.py over
In [3]: t = 'žćčšđ'
...
UnicodeEncodeError: 'ascii' codec can't encode characters in position 5-9: ordinal not in range(128)

The supplied patch fixes the problem. At least temporary.

Also, changed the log header to contain the encoding definition for the source file.
Useful for rerunning the log file outside IPython.

@takluyver
Owner

Looking at the code, output data is written separately. It's not easy to get unicode output in Python 2.x (assuming we're limited to reprs produced by the interpreter, not print statements), but it is possible: define a class, replace its __name__ with a unicode character, and ask for it. So we should probably encode odata too, just to be on the safe side.

@fperez
Owner

Adding encoding also to output and merging for 0.10.2 release. Many thanks!

@damianavila damianavila referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@minrk minrk added this to the old milestone
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 21, 2011
  1. @TvrtkoM

    Encode data with system default encoding before writing to log file.

    TvrtkoM authored
    This enables writing non-ascii characters to be written in log file during
    logging session.
  2. @TvrtkoM
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 3 deletions.
  1. +2 −1  IPython/Logger.py
  2. +1 −1  IPython/Magic.py
  3. +1 −1  IPython/iplib.py
View
3  IPython/Logger.py
@@ -18,6 +18,7 @@
import glob
import os
import time
+import sys
#****************************************************************************
# FIXME: This class isn't a mixin anymore, but it still needs attributes from
@@ -241,7 +242,7 @@ def log_write(self,data,kind='input'):
if self.timestamp:
write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
time.localtime()))
- write('%s\n' % data)
+ write('%s\n' % data.encode(sys.stdin.encoding))
elif kind=='output' and self.log_output:
odata = '\n'.join(['#[Out]# %s' % s
for s in data.split('\n')])
View
2  IPython/Magic.py
@@ -1164,7 +1164,7 @@ def magic_logstart(self,parameter_s=''):
if logfname:
logfname = os.path.expanduser(logfname)
rc.opts.logfile = logfname
- loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
+ loghead = self.shell.loghead_tpl % (sys.stdin.encoding, rc.opts, rc.args)
try:
started = logger.logstart(logfname,loghead,logmode,
log_output,timestamp,log_raw_input)
View
2  IPython/iplib.py
@@ -670,7 +670,7 @@ def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
# logstart method.
self.loghead_tpl = \
"""#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
-#log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
+#log# DO NOT CHANGE THIS LINE OR THE TWO BELOW -* coding: %s *-
#log# opts = %s
#log# args = %s
#log# It is safe to make manual edits below here.
Something went wrong with that request. Please try again.