Skip to content

Commit

Permalink
Strip out encoding declaration in %loadpy
Browse files Browse the repository at this point in the history
Closes gh-1103
  • Loading branch information
takluyver committed Dec 15, 2011
1 parent b82247f commit 0c6c879
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions IPython/core/magic.py
Expand Up @@ -96,6 +96,8 @@ def needs_local_scope(func):
# Used for exception handling in magic_edit
class MacroToEdit(ValueError): pass

_encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)")

#***************************************************************************
# Main class implementing Magic functionality

Expand Down Expand Up @@ -2160,12 +2162,15 @@ def magic_loadpy(self, arg_s):
raise ValueError('%%load only works with .py files: %s' % arg_s)
if remote_url:
import urllib2
response = urllib2.urlopen(arg_s)
content = response.read()
fileobj = urllib2.urlopen(arg_s)
else:
with open(arg_s) as f:
content = f.read()
self.set_next_input(content)
fileobj = open(arg_s)

# Strip out encoding declarations
lines = [l for l in fileobj if not _encoding_declaration_re.match(l)]
fileobj.close()

self.set_next_input(os.linesep.join(lines))

def _find_edit_target(self, args, opts, last_call):
"""Utility method used by magic_edit to find what to edit."""
Expand Down

0 comments on commit 0c6c879

Please sign in to comment.