Show More
@@ -55,6 +55,7 b' from IPython.core.prefilter import ESC_MAGIC' | |||||
55 | from IPython.core.pylabtools import mpl_runner |
|
55 | from IPython.core.pylabtools import mpl_runner | |
56 | from IPython.testing.skipdoctest import skip_doctest |
|
56 | from IPython.testing.skipdoctest import skip_doctest | |
57 | from IPython.utils import py3compat |
|
57 | from IPython.utils import py3compat | |
|
58 | from IPython.utils import openpy | |||
58 | from IPython.utils.io import file_read, nlprint |
|
59 | from IPython.utils.io import file_read, nlprint | |
59 | from IPython.utils.module_paths import find_mod |
|
60 | from IPython.utils.module_paths import find_mod | |
60 | from IPython.utils.path import get_py_filename, unquote_filename |
|
61 | from IPython.utils.path import get_py_filename, unquote_filename | |
@@ -2261,28 +2262,15 b' Currently the magic system has the following functions:\\n"""' | |||||
2261 | # Local files must be .py; for remote URLs it's possible that the |
|
2262 | # Local files must be .py; for remote URLs it's possible that the | |
2262 | # fetch URL doesn't have a .py in it (many servers have an opaque |
|
2263 | # fetch URL doesn't have a .py in it (many servers have an opaque | |
2263 | # URL, such as scipy-central.org). |
|
2264 | # URL, such as scipy-central.org). | |
2264 | raise ValueError('%%load only works with .py files: %s' % arg_s) |
|
2265 | raise ValueError('%%loadpy only works with .py files: %s' % arg_s) | |
|
2266 | ||||
|
2267 | # openpy takes care of finding the source encoding (per PEP 263) | |||
2265 | if remote_url: |
|
2268 | if remote_url: | |
2266 | import urllib2 |
|
2269 | contents = openpy.read_py_url(arg_s, skip_encoding_cookie=True) | |
2267 | fileobj = urllib2.urlopen(arg_s) |
|
|||
2268 | # While responses have a .info().getencoding() way of asking for |
|
|||
2269 | # their encoding, in *many* cases the return value is bogus. In |
|
|||
2270 | # the wild, servers serving utf-8 but declaring latin-1 are |
|
|||
2271 | # extremely common, as the old HTTP standards specify latin-1 as |
|
|||
2272 | # the default but many modern filesystems use utf-8. So we can NOT |
|
|||
2273 | # rely on the headers. Short of building complex encoding-guessing |
|
|||
2274 | # logic, going with utf-8 is a simple solution likely to be right |
|
|||
2275 | # in most real-world cases. |
|
|||
2276 | linesource = fileobj.read().decode('utf-8', 'replace').splitlines() |
|
|||
2277 | fileobj.close() |
|
|||
2278 | else: |
|
2270 | else: | |
2279 | with open(arg_s) as fileobj: |
|
2271 | contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True) | |
2280 | linesource = fileobj.read().splitlines() |
|
|||
2281 |
|
||||
2282 | # Strip out encoding declarations |
|
|||
2283 | lines = [l for l in linesource if not _encoding_declaration_re.match(l)] |
|
|||
2284 |
|
2272 | |||
2285 |
self.set_next_input( |
|
2273 | self.set_next_input(contents) | |
2286 |
|
2274 | |||
2287 | def _find_edit_target(self, args, opts, last_call): |
|
2275 | def _find_edit_target(self, args, opts, last_call): | |
2288 | """Utility method used by magic_edit to find what to edit.""" |
|
2276 | """Utility method used by magic_edit to find what to edit.""" |
@@ -4,6 +4,7 b' as per PEP 263.' | |||||
4 |
|
4 | |||
5 | Much of the code is taken from the tokenize module in Python 3.2. |
|
5 | Much of the code is taken from the tokenize module in Python 3.2. | |
6 | """ |
|
6 | """ | |
|
7 | from __future__ import absolute_import | |||
7 |
|
8 | |||
8 | import __builtin__ |
|
9 | import __builtin__ | |
9 | import io |
|
10 | import io | |
@@ -107,7 +108,7 b' except ImportError:' | |||||
107 | try: |
|
108 | try: | |
108 | # Available in Python 3.2 and above. |
|
109 | # Available in Python 3.2 and above. | |
109 | from tokenize import open |
|
110 | from tokenize import open | |
110 | except: |
|
111 | except ImportError: | |
111 | # Copied from Python 3.2 tokenize |
|
112 | # Copied from Python 3.2 tokenize | |
112 | def open(filename): |
|
113 | def open(filename): | |
113 | """Open a file in read only mode using the encoding detected by |
|
114 | """Open a file in read only mode using the encoding detected by | |
@@ -118,19 +119,7 b' except:' | |||||
118 | buffer.seek(0) |
|
119 | buffer.seek(0) | |
119 | text = TextIOWrapper(buffer, encoding, line_buffering=True) |
|
120 | text = TextIOWrapper(buffer, encoding, line_buffering=True) | |
120 | text.mode = 'r' |
|
121 | text.mode = 'r' | |
121 | return text |
|
122 | return text | |
122 |
|
||||
123 | def open_url(url, errors='replace'): |
|
|||
124 | """Open a URL to a raw Python file, using the encoding detected by |
|
|||
125 | detect_encoding(). |
|
|||
126 | """ |
|
|||
127 | response = urllib.urlopen(url) |
|
|||
128 | buffer = io.BufferedRandom(response) |
|
|||
129 | encoding, lines = detect_encoding(buffer.readline) |
|
|||
130 | buffer.seek(0) |
|
|||
131 | text = TextIOWrapper(buffer, encoding, errors=errors, line_buffering=True) |
|
|||
132 | text.mode = 'r' |
|
|||
133 | return text |
|
|||
134 |
|
123 | |||
135 | def strip_encoding_cookie(filelike): |
|
124 | def strip_encoding_cookie(filelike): | |
136 | """Generator to pull lines from a text-mode file, skipping the encoding |
|
125 | """Generator to pull lines from a text-mode file, skipping the encoding | |
@@ -150,9 +139,24 b' def strip_encoding_cookie(filelike):' | |||||
150 | for line in it: |
|
139 | for line in it: | |
151 | yield line |
|
140 | yield line | |
152 |
|
141 | |||
153 | def read_py_file(filename, skip_encoding_cookie=True): |
|
142 | def read_py_file(filename, errors='replace', skip_encoding_cookie=True): | |
154 |
|
|
143 | with open(filename) as f: # the open function defined in this module. | |
|
144 | if skip_encoding_cookie: | |||
|
145 | return "".join(strip_encoding_cookie(f)) | |||
|
146 | else: | |||
|
147 | return f.read() | |||
|
148 | ||||
|
149 | def read_py_url(url, errors='replace', skip_encoding_cookie=True): | |||
|
150 | """Open a URL to a raw Python file, using the encoding detected by | |||
|
151 | detect_encoding(). | |||
|
152 | """ | |||
|
153 | response = urllib.urlopen(url) | |||
|
154 | buffer = io.BytesIO(response.read()) | |||
|
155 | encoding, lines = detect_encoding(buffer.readline) | |||
|
156 | buffer.seek(0) | |||
|
157 | text = TextIOWrapper(buffer, encoding, errors=errors, line_buffering=True) | |||
|
158 | text.mode = 'r' | |||
155 | if skip_encoding_cookie: |
|
159 | if skip_encoding_cookie: | |
156 |
return "".join(strip_encoding_cookie( |
|
160 | return "".join(strip_encoding_cookie(text)) | |
157 | else: |
|
161 | else: | |
158 |
return |
|
162 | return text.read() |
General Comments 0
You need to be logged in to leave comments.
Login now