Repository: rootpy/rootpy

Issue:
[bug] Directories with spaces show weird behaviour
Today I encountered weird behaviour in the File/Directory classes.
It is best demonstrated by this example:

``` python
from rootpy.io import File

f = File('test.root', 'recreate')
f.mkdir('TTbar_plus_X_analysis/EPlusJets/Ref selection', recurse=True)
f.mkdir('Ref selection')

# this one works
print 'Ref selection', f.__contains__('Ref selection')
# this one doesn't
print 'TTbar_plus_X_analysis/EPlusJets/Ref selection', f.__contains__('TTbar_plus_X_analysis/EPlusJets/Ref selection')

print list(f.walk())
```

In general directories/paths with spaces are fine. However, once you add subfolders, this is no longer the case. In the above example, rootpy is happy with `Ref selection` but not with `TTbar_plus_X_analysis/EPlusJets/Ref selection`. 
Similar behaviour can be seen in `roosh` where a `cd` into the directory works, but an `ls` fails.

I am going to have a second look at the code tomorrow, but if any of you already know where the buggy code might reside, please let me know.

Cheers,
Luke



Discussion:
The space isn't the problem since this also fails:

``` python
from rootpy.io import File

f = File('test.root', 'recreate')
f.mkdir('a/b/c', recurse=True)
print 'a/b/c' in f
```


Resolving patch:
diff --git a/rootpy/io/file.py b/rootpy/io/file.py
--- a/rootpy/io/file.py
+++ b/rootpy/io/file.py
@@ -55,7 +55,6 @@ def splitfile(path):
 
 
 def wrap_path_handling(f):
-
     def get(self, name, *args, **kwargs):
         _name = os.path.normpath(name)
         if _name == '.':
@@ -73,7 +72,7 @@ def get(self, name, *args, **kwargs):
                         raise DoesNotExist
                     _dir._parent = self
                     _dir._path = os.path.join(self._path, dirpath)
-                    thing = f(_dir, path, *args, **kwargs)
+                    thing = get(_dir, path, *args, **kwargs)
             else:
                 thing = f(self, _name, *args, **kwargs)
                 if isinstance(thing, _DirectoryBase):
@@ -393,7 +392,7 @@ def mkdir(self, path, title="", recurse=False):
         with preserve_current_directory():
             dest = self
             if recurse:
-                parent_dirs = head.split('/')
+                parent_dirs = head.split(os.path.sep)
                 for parent_dir in parent_dirs:
                     try:
                         newdest = dest.GetDirectory(parent_dir)


Test patch:
diff --git a/rootpy/io/tests/test_file.py b/rootpy/io/tests/test_file.py
--- a/rootpy/io/tests/test_file.py
+++ b/rootpy/io/tests/test_file.py
@@ -11,7 +11,7 @@
 from rootpy.testdata import get_file
 from rootpy import ROOT
 
-from nose.tools import assert_raises, assert_equal
+from nose.tools import assert_raises, assert_equal, assert_true
 
 import gc
 import os
@@ -20,7 +20,6 @@
 
 
 def test_tempfile():
-
     with TemporaryFile() as f:
         assert_equal(os.path.isfile(f.GetName()), True)
         assert_raises(DoesNotExist, f.Get, 'blah')
@@ -32,7 +31,6 @@ def test_tempfile():
 
 
 def test_memfile():
-
     with MemFile() as f:
         hist = Hist(1, 0, 1, name='test')
         hist.Write()
@@ -40,7 +38,6 @@ def test_memfile():
 
 
 def test_file_open():
-
     fname = 'test_file_open.root'
     with File.open(fname, 'w'):
         pass
@@ -52,7 +49,6 @@ def test_file_open():
 
 
 def test_context():
-
     with MemFile() as a:
         assert_equal(ROOT.gDirectory.func(), a)
         with MemFile() as b:
@@ -84,7 +80,6 @@ def test_context():
 
 
 def test_file_get():
-
     with get_file() as f:
         d = f.Get('means', rootpy=False)
         assert_equal(d.__class__.__name__, 'TDirectoryFile')
@@ -97,7 +92,6 @@ def test_file_get():
 
 
 def test_file_item():
-
     with TemporaryFile() as f:
         h = Hist(1, 0, 1, name='test')
         f['myhist'] = h
@@ -106,7 +100,6 @@ def test_file_item():
 
 
 def test_file_attr():
-
     with TemporaryFile() as f:
         h = Hist(1, 0, 1, name='test')
         f.myhist = h
@@ -119,14 +112,15 @@ def test_file_attr():
 
 
 def test_file_contains():
-
     with TemporaryFile() as f:
         assert_equal('some/thing' in f, False)
         rdir = f.mkdir('some')
         thing = Hist(10, 0, 1, name='thing')
         rdir.thing = thing
-        assert_equal('some/thing' in f, True)
-        assert_equal('thing' in rdir, True)
+        assert_true('some/thing' in f)
+        assert_true('thing' in rdir)
+        f.mkdir('a/b/c', recurse=True)
+        assert_true('a/b/c' in f)
 
 
 def test_no_dangling_files():
@@ -145,7 +139,6 @@ def foo():
 
 
 def test_keepalive():
-
     gc.collect()
     assert list(R.gROOT.GetListOfFiles()) == [], "There exist open ROOT files when there should not be"
 
@@ -154,15 +147,11 @@ def test_keepalive():
     # The purpose of this test is to ensure that everything is working as
     # expected.
     h = get_file().Get("means/hist1")
-
     gc.collect()
-
     assert h, "hist1 is not being kept alive"
-
     assert list(R.gROOT.GetListOfFiles()) != [], "Expected an open ROOT file.."
 
     h = None
-
     gc.collect()
     assert list(R.gROOT.GetListOfFiles()) == [], "There exist open ROOT files when there should not be"
 
@@ -174,7 +163,6 @@ def test_keepalive_canvas():
 
     with invisible_canvas() as c:
         get_file().Get("means/hist1").Draw()
-
         gc.collect()
         assert list(R.gROOT.GetListOfFiles()) != [], "Expected an open ROOT file.."