##// END OF EJS Templates
Strip out encoding declaration in %loadpy...
Thomas Kluyver -
Show More
@@ -96,6 +96,8 b' def needs_local_scope(func):'
96 # Used for exception handling in magic_edit
96 # Used for exception handling in magic_edit
97 class MacroToEdit(ValueError): pass
97 class MacroToEdit(ValueError): pass
98
98
99 _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)")
100
99 #***************************************************************************
101 #***************************************************************************
100 # Main class implementing Magic functionality
102 # Main class implementing Magic functionality
101
103
@@ -2160,12 +2162,15 b' Currently the magic system has the following functions:\\n"""'
2160 raise ValueError('%%load only works with .py files: %s' % arg_s)
2162 raise ValueError('%%load only works with .py files: %s' % arg_s)
2161 if remote_url:
2163 if remote_url:
2162 import urllib2
2164 import urllib2
2163 response = urllib2.urlopen(arg_s)
2165 fileobj = urllib2.urlopen(arg_s)
2164 content = response.read()
2165 else:
2166 else:
2166 with open(arg_s) as f:
2167 fileobj = open(arg_s)
2167 content = f.read()
2168
2168 self.set_next_input(content)
2169 # Strip out encoding declarations
2170 lines = [l for l in fileobj if not _encoding_declaration_re.match(l)]
2171 fileobj.close()
2172
2173 self.set_next_input(os.linesep.join(lines))
2169
2174
2170 def _find_edit_target(self, args, opts, last_call):
2175 def _find_edit_target(self, args, opts, last_call):
2171 """Utility method used by magic_edit to find what to edit."""
2176 """Utility method used by magic_edit to find what to edit."""
General Comments 0
You need to be logged in to leave comments. Login now