Comment 15 for bug 366353

Revision history for this message
Fernando Perez (fdo.perez) wrote :

Robert is right in his suggestion, since that prevents the magic stripping of the name. In general, whenever one is using a name with spaces in it, it's best to quote it. However, the problem is actually deeper. I made a little script from Brian's code:

import tempfile
import os
from IPython.platutils import get_long_path_name

f = tempfile.NamedTemporaryFile(suffix='.py')
f.write('print "In temp file"\n')
f.flush()

name = f.name
bname = get_long_path_name(name)

print 'name:',name
_ip.magic('run %s' % name)
print
print 'bname:',bname
_ip.magic('run %s' % bname)
print
print '"bname" "%s"' % bname
_ip.magic('run "%s"' % bname)

### EOF

and now we get this:

In [3]: run t366353.py
name: c:\docume~1\fperez\locals~1\temp\tmpiovihn.py
ERROR: File `c:docume~1fperezlocals~1temptmpiovihn.py` not found.

bname: c:\Documents and Settings\fperez\Local Settings\Temp\tmpiovihn.py
ERROR: File `c:Documents.py` not found.

"bname" "c:\Documents and Settings\fperez\Local Settings\Temp\tmpiovihn.py"
Could not open file <c:\Documents and Settings\fperez\Local Settings\Temp\tmpiovihn.py> fo
r safe execution.

So even the third option (Robert's quoting suggestion) still doesn't work, even though now %run gets the correct path. The problem is actually quite fundamental:

In [7]: bname
Out[7]: u'c:\\Documents and Settings\\fperez\\Local Settings\\Temp\\tmp1o8msd.py'

In [8]: execfile(bname)
---------------------------------------------------------------------------
IOError Traceback (most recent call last)

H:\ipython\ipython\IPython\tests\t366353.py in <module>()
----> 1
      2
      3
      4
      5

IOError: [Errno 13] Permission denied: 'c:\\Documents and Settings\\fperez\\Local Settings
\\Temp\\tmp1o8msd.py'

In [9]: !more "$bname"
Cannot access file C:\Documents and Settings\fperez\Local Settings\Temp\tmp1o8msd.py

Even though the file is perfectly writable:

In [10]: f
Out[10]: <open file '<fdopen>', mode 'w+b' at 0x012152F0>

In [11]: f.write('x=1\n')

In [12]: f.flush()

In [13]: f.seek(0)

I think the issue is this:

http://docs.python.org/library/tempfile.html#tempfile.NamedTemporaryFile

in particular:

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later)

So basically that function should only be used if you are only going to ever open the file once.

Since this isn't really a %run bug but a fundamental limitation of the Windows NamedTemporaryFile API, I'm closing this bug. I will fix the test suite so we use something else instead in our tests, so the *tests* also run fine in windows, but we don't have a real bug there.

This problem also bit us recently in the NIPY test suite, which is why this time I knew what to look for :)