Show More
@@ -52,6 +52,7 b' from IPython.core import magic_arguments, page' | |||||
52 | from IPython.core.prefilter import ESC_MAGIC |
|
52 | from IPython.core.prefilter import ESC_MAGIC | |
53 | from IPython.lib.pylabtools import mpl_runner |
|
53 | from IPython.lib.pylabtools import mpl_runner | |
54 | from IPython.testing.skipdoctest import skip_doctest |
|
54 | from IPython.testing.skipdoctest import skip_doctest | |
|
55 | from IPython.utils import py3compat | |||
55 | from IPython.utils.io import file_read, nlprint |
|
56 | from IPython.utils.io import file_read, nlprint | |
56 | from IPython.utils.path import get_py_filename, unquote_filename |
|
57 | from IPython.utils.path import get_py_filename, unquote_filename | |
57 | from IPython.utils.process import arg_split, abbrev_cwd |
|
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 | except (TypeError, ValueError) as e: |
|
2086 | except (TypeError, ValueError) as e: | |
2086 | print e.args[0] |
|
2087 | print e.args[0] | |
2087 | return |
|
2088 | return | |
2088 | if isinstance(cmds, unicode): |
|
2089 | with py3compat.open(fname,'w', encoding="utf-8") as f: | |
2089 | cmds = cmds.encode("utf-8") |
|
2090 | f.write(u"# coding: utf-8\n") | |
2090 | with open(fname,'w') as f: |
|
2091 | f.write(py3compat.cast_unicode(cmds)) | |
2091 | f.write("# coding: utf-8\n") |
|
|||
2092 | f.write(cmds) |
|
|||
2093 | print 'The following commands were written to file `%s`:' % fname |
|
2092 | print 'The following commands were written to file `%s`:' % fname | |
2094 | print cmds |
|
2093 | print cmds | |
2095 |
|
2094 |
@@ -2,6 +2,8 b'' | |||||
2 | """Compatibility tricks for Python 3. Mainly to do with unicode.""" |
|
2 | """Compatibility tricks for Python 3. Mainly to do with unicode.""" | |
3 | import sys |
|
3 | import sys | |
4 |
|
4 | |||
|
5 | orig_open = open | |||
|
6 | ||||
5 | def no_code(x, encoding=None): |
|
7 | def no_code(x, encoding=None): | |
6 | return x |
|
8 | return x | |
7 |
|
9 | |||
@@ -39,6 +41,8 b' if sys.version_info[0] >= 3:' | |||||
39 | return all(isidentifier(a) for a in s.split(".")) |
|
41 | return all(isidentifier(a) for a in s.split(".")) | |
40 | return s.isidentifier() |
|
42 | return s.isidentifier() | |
41 |
|
43 | |||
|
44 | open = orig_open | |||
|
45 | ||||
42 | else: |
|
46 | else: | |
43 | PY3 = False |
|
47 | PY3 = False | |
44 |
|
48 | |||
@@ -57,6 +61,27 b' else:' | |||||
57 | return all(isidentifier(a) for a in s.split(".")) |
|
61 | return all(isidentifier(a) for a in s.split(".")) | |
58 | return bool(_name_re.match(s)) |
|
62 | return bool(_name_re.match(s)) | |
59 |
|
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() | |||
|
84 | ||||
60 | def execfile(fname, glob, loc=None): |
|
85 | def execfile(fname, glob, loc=None): | |
61 | loc = loc if (loc is not None) else glob |
|
86 | loc = loc if (loc is not None) else glob | |
62 | exec compile(open(fname).read(), fname, 'exec') in glob, loc |
|
87 | exec compile(open(fname).read(), fname, 'exec') in glob, loc |
General Comments 0
You need to be logged in to leave comments.
Login now