Repository: rootpy/rootpy

Issue:
List index out of range
I'm in ipython, the latest released rootpy, and I get this when I try to access a TTree called `composed`.

``` ipython
In [9]: f.composed
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-9-b49340cac05a> in <module>()
----> 1 f.composed

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/io/file.py in __getattr__(self, attr)
     89         in Get this can end up in an "infinite" recursion and stack overflow
     90         """
---> 91         return self.Get(attr)
     92 
     93     def __getitem__(self, name):

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/io/file.py in get(self, name, **kwargs)
     52                     thing = _dir.Get(path, **kwargs)
     53             else:
---> 54                 thing = f(self, _name, **kwargs)
     55                 if isinstance(thing, _DirectoryBase):
     56                     thing._parent = self

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/io/file.py in Get(self, name, **kwargs)
    118         if not thing:
    119             raise DoesNotExist
--> 120         return asrootpy(thing, **kwargs)
    121 
    122     def GetRaw(self, name):

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/__init__.py in asrootpy(thing, **kwargs)
    113 
    114     thing_cls = thing.__class__
--> 115     rootpy_cls = lookup(thing_cls)
    116     if rootpy_cls is None:
    117         log.warn("a subclass of %s is not implemented in rootpy" %

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/__init__.py in lookup(cls)
    131 
    132     cls_name = cls.__name__
--> 133     return lookup_by_name(cls_name)
    134 
    135 

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/__init__.py in lookup_by_name(cls_name)
    149     path, rootpy_cls_name = '.'.join(path_tokens[:-1]), path_tokens[-1]
    150     rootpy_module = __import__(
--> 151             path, globals(), locals(), [rootpy_cls_name], -1)
    152     rootpy_cls = getattr(rootpy_module, rootpy_cls_name)
    153     if dynamic_kwargs is not None:

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/tree/__init__.py in <module>()
      3 from .. import log; log = log[__name__]
      4 
----> 5 from .buffer import TreeBuffer
      6 from .tree import Tree
      7 from .model import TreeModel

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/tree/buffer.py in <module>()
     10 from .. import create
     11 from ..core import _resetable_mixin, _copy_construct_mixin
---> 12 from .. import stl
     13 
     14 

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/stl.py in <module>()
     43 from .defaults import extra_initialization
     44 from .util.cpp import CPPGrammar
---> 45 from . import compiled
     46 from . import userdata
     47 from . import lookup_by_name, register, QROOT

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/compiled/__init__.py in <module>()
     16 import ROOT
     17 
---> 18 import rootpy.userdata as userdata
     19 
     20 from .. import log; log = log[__name__]

/home/pwaller/.local/lib/python2.7/site-packages/rootpy/userdata.py in <module>()
     40 DATA_ROOT = CONFIG_ROOT = None
     41 if (os.getenv('ROOTPY_GRIDMODE') not in ('1', 'true') and
---> 42     not sys.argv[0].endswith('nosetests')) or os.getenv('DEBUG', None):
     43     DATA_ROOT = ensure_directory('ROOTPY_DATA', '${XDG_CACHE_HOME}/rootpy')
     44     CONFIG_ROOT = ensure_directory('ROOTPY_CONFIG', '${XDG_CONFIG_HOME}/rootpy')

IndexError: list index out of range

In [10]: import sys

In [11]: sys.argv
Out[11]: []
```



Discussion:
I don't understand why `sys.argv` was empty. I can't now reproduce it. Probably it's a bug that it became empty.


Resolving patch:
diff --git a/rootpy/userdata.py b/rootpy/userdata.py
--- a/rootpy/userdata.py
+++ b/rootpy/userdata.py
@@ -48,8 +48,14 @@ def ensure_directory(variable, default):
 
 
 DATA_ROOT = CONFIG_ROOT = None
-if (os.getenv('ROOTPY_GRIDMODE') not in ('1', 'true') and
-        not sys.argv[0].endswith('nosetests')) or os.getenv('DEBUG', None):
+
+in_nosetests = False
+if sys.argv and sys.argv[0].endswith('nosetests'):
+    in_nosetests = True
+
+grid_mode = os.getenv('ROOTPY_GRIDMODE') in ('1', 'true')
+
+if (os.getenv('DEBUG', None) or not (grid_mode or in_nosetests)):
     DATA_ROOT = ensure_directory(
         'ROOTPY_DATA', '${XDG_CACHE_HOME}/rootpy')
     CONFIG_ROOT = ensure_directory(


Test patch: