Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix loadpy duplicating newlines #1207

Merged
merged 2 commits into from Jan 6, 2012
Merged

fix loadpy duplicating newlines #1207

merged 2 commits into from Jan 6, 2012

Conversation

minrk
Copy link
Member

@minrk minrk commented Dec 24, 2011

%loadpy script.py 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

@minrk
Copy link
Member Author

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.

@fperez
Copy link
Member

fperez commented Jan 6, 2012

Problem. Running:

%loadpy http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py

produces

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/fperez/tmp/ipython-notebooks/<ipython-input-1-af6e804f992c> in <module>()
----> 1 get_ipython().magic(u'loadpy http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py')

/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 = fileobj.read().decode('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.

@minrk
Copy link
Member Author

minrk commented Jan 6, 2012

ah, stupid and fixed. Thanks for the catch.

@fperez
Copy link
Member

fperez commented Jan 6, 2012

Fix works, thanks. Merging now.

fperez added a commit that referenced this pull request Jan 6, 2012
Fix loadpy duplicating newlines.
@fperez fperez merged commit a827fe6 into ipython:master Jan 6, 2012
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
Fix loadpy duplicating newlines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

double newline from %loadpy in python notebook (at least on mac)
3 participants