Show More
@@ -52,6 +52,7 b' from IPython.core import magic_arguments, page' | |||
|
52 | 52 | from IPython.core.prefilter import ESC_MAGIC |
|
53 | 53 | from IPython.lib.pylabtools import mpl_runner |
|
54 | 54 | from IPython.testing.skipdoctest import skip_doctest |
|
55 | from IPython.utils import py3compat | |
|
55 | 56 | from IPython.utils.io import file_read, nlprint |
|
56 | 57 | from IPython.utils.path import get_py_filename, unquote_filename |
|
57 | 58 | from IPython.utils.process import arg_split, abbrev_cwd |
@@ -2085,11 +2086,9 b' Currently the magic system has the following functions:\\n"""' | |||
|
2085 | 2086 | except (TypeError, ValueError) as e: |
|
2086 | 2087 | print e.args[0] |
|
2087 | 2088 | return |
|
2088 | if isinstance(cmds, unicode): | |
|
2089 | cmds = cmds.encode("utf-8") | |
|
2090 | with open(fname,'w') as f: | |
|
2091 | f.write("# coding: utf-8\n") | |
|
2092 | f.write(cmds) | |
|
2089 | with py3compat.open(fname,'w', encoding="utf-8") as f: | |
|
2090 | f.write(u"# coding: utf-8\n") | |
|
2091 | f.write(py3compat.cast_unicode(cmds)) | |
|
2093 | 2092 | print 'The following commands were written to file `%s`:' % fname |
|
2094 | 2093 | print cmds |
|
2095 | 2094 |
@@ -2,6 +2,8 b'' | |||
|
2 | 2 | """Compatibility tricks for Python 3. Mainly to do with unicode.""" |
|
3 | 3 | import sys |
|
4 | 4 | |
|
5 | orig_open = open | |
|
6 | ||
|
5 | 7 | def no_code(x, encoding=None): |
|
6 | 8 | return x |
|
7 | 9 | |
@@ -38,6 +40,8 b' if sys.version_info[0] >= 3:' | |||
|
38 | 40 | if dotted: |
|
39 | 41 | return all(isidentifier(a) for a in s.split(".")) |
|
40 | 42 | return s.isidentifier() |
|
43 | ||
|
44 | open = orig_open | |
|
41 | 45 | |
|
42 | 46 | else: |
|
43 | 47 | PY3 = False |
@@ -56,6 +60,27 b' else:' | |||
|
56 | 60 | if dotted: |
|
57 | 61 | return all(isidentifier(a) for a in s.split(".")) |
|
58 | 62 | return bool(_name_re.match(s)) |
|
63 | ||
|
64 | class open(object): | |
|
65 | """Wrapper providing key part of Python 3 open() interface.""" | |
|
66 | def __init__(self, fname, mode="r", encoding="utf-8"): | |
|
67 | self.f = orig_open(fname, mode) | |
|
68 | self.enc = encoding | |
|
69 | ||
|
70 | def write(self, s): | |
|
71 | return self.f.write(s.encode(self.enc)) | |
|
72 | ||
|
73 | def read(self, size=-1): | |
|
74 | return self.f.read(size).decode(self.enc) | |
|
75 | ||
|
76 | def close(self): | |
|
77 | return self.f.close() | |
|
78 | ||
|
79 | def __enter__(self): | |
|
80 | return self | |
|
81 | ||
|
82 | def __exit__(self, etype, value, traceback): | |
|
83 | self.f.close() | |
|
59 | 84 | |
|
60 | 85 | def execfile(fname, glob, loc=None): |
|
61 | 86 | loc = loc if (loc is not None) else glob |
General Comments 0
You need to be logged in to leave comments.
Login now