Skip to content

fix loadpy duplicating newlines #1207

merged 2 commits into from Jan 6, 2012

3 participants

IPython member
minrk commented Dec 24, 2011

%loadpy iterates through lines, but then does os.linesep.join(lines), thus duplicating all newlines. This uses splitlines(), so the content matches that fetched from a url - list of lines without endings.

closes #1204

IPython member
minrk commented Jan 1, 2012

@fperez - This is due to the code you and I reviewed together on IRC. The answer seems pretty straightforward. An alternative implementation that avoids iterating twice through the lines is here. But I think the splitlines() implementation is easier to read, and iterating through a list should be a small factor compared to opening/reading a file.

IPython member
fperez commented Jan 6, 2012

Problem. Running:



AttributeError                            Traceback (most recent call last)
/home/fperez/tmp/ipython-notebooks/<ipython-input-1-af6e804f992c> in <module>()
----> 1 get_ipython().magic(u'loadpy')

/home/fperez/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s, next_input)
   1995                 self._magic_locals = sys._getframe(1).f_locals
   1996             with self.builtin_trap:
-> 1997                 result = fn(magic_args)
   1998             # Ensure we're not keeping object references around:

   1999             self._magic_locals = {}

/home/fperez/usr/lib/python2.7/site-packages/IPython/core/magic.pyc in magic_loadpy(self, arg_s)
   2175             # logic, going with utf-8 is a simple solution likely to be right

   2176             # in most real-world cases.

-> 2177             with urllib2.urlopen(arg_s) as fileobj:
   2178                 linesource ='utf-8', 'replace').splitlines()
   2179         else:

AttributeError: addinfourl instance has no attribute '__exit__'

It looks like the urllib objects are file-like but not proper context managers. That may be why we had the uglier manual handling of file closing by hand.

IPython member
minrk commented Jan 6, 2012

ah, stupid and fixed. Thanks for the catch.

IPython member
fperez commented Jan 6, 2012

Fix works, thanks. Merging now.

@fperez fperez merged commit a827fe6 into ipython:master Jan 6, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.