Repository: rootpy/rootpy

Issue:
AttributeError when using root_open
I am pretty new to both root and rootpy. But this seams wrong Dont really know how to find the error. I am just trying to open a root file using rootpy and I get an AttributeError - No matter if the file exists or not.

I can open a root file using PyROOT though. Any thoughts

Ipython session:
```
In [1]: import ROOT

In [2]: ROOT.gROOT.GetVersion()
Out[2]: '6.09/01'

In [3]: f = ROOT.TFile.Open("blah.root", "new")

In [4]: from rootpy.io import root_open

In [5]: myfile = root_open('myfile.root', 'new')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-b3f40971348f> in <module>()
----> 1 myfile = root_open('myfile.root', 'new')

/usr/local/lib/python2.7/dist-packages/rootpy-0.8.3.dev0-py2.7.egg/rootpy/io/file.pyc in root_open(filename, mode)
    135 
    136     filename = expand_path(filename)
--> 137     prev_dir = ROOT.gDirectory.func()
    138     root_file = ROOT.R.TFile.Open(filename, mode)
    139     if not root_file:

/usr/local/lib/python2.7/dist-packages/rootpy-0.8.3.dev0-py2.7.egg/rootpy/utils/module_facade.pyc in __getattr__(self, key)
     82                 sup = super(_wrapper_cls, self)
     83                 if hasattr(sup, "__getattr__"):
---> 84                     result = sup.__getattr__(key)
     85                     if result is not None:
     86                         return result

/usr/local/lib/python2.7/dist-packages/rootpy-0.8.3.dev0-py2.7.egg/rootpy/ROOT.pyc in __getattr__(self, what)
    116 
    117         # Memoize
--> 118         setattr(self, what, result)
    119         return result
    120 

AttributeError: can't set attribute

In [6]: from rootpy import asrootpy

In [7]: frpy = asrootpy(f)

In [8]: frpy.ls()
TFile**		blah.root	
 TFile*		blah.root	

```


Discussion:
That's strange. I'll have a closer look but it's working for me with the same version of ROOT:

```python
In [1]: import ROOT

In [2]: ROOT.gROOT.GetVersion()
Out[2]: '6.09/01'

In [3]: f = ROOT.TFile.Open("blah.root", "new")

In [4]: from rootpy.io import root_open

In [5]: myfile = root_open('myfile.root', 'new')

In [6]: 

```
Can you try running the following? What does it give you?

```python
python -c "from rootpy import ROOT;print(ROOT.gDirectory.func())"
```
You should see something like:
```
<ROOT.TROOT object ("PyROOT") at 0x7fe462514a00>
```
This is what I get:
```
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/rootpy-0.8.3.dev0-py2.7.egg/rootpy/utils/module_facade.py", line 84, in __getattr__
    result = sup.__getattr__(key)
  File "/usr/local/lib/python2.7/dist-packages/rootpy-0.8.3.dev0-py2.7.egg/rootpy/ROOT.py", line 118, in __getattr__
    setattr(self, what, result)
AttributeError: can't set attribute
```

This is very strange since I cannot reproduce it. I don't know why it would fail to set the attribute for you... However since it's just memoizing the result, and this is not entirely essential, I will put a try/except around it to ignore the exception in your case.

Resolving patch:
diff --git a/rootpy/ROOT.py b/rootpy/ROOT.py
--- a/rootpy/ROOT.py
+++ b/rootpy/ROOT.py
@@ -114,8 +114,13 @@ def __getattr__(self, what):
                     'and rootpy does not contain the class `{0}`'.format(what))
             return result
 
-        # Memoize
-        setattr(self, what, result)
+        try:
+            # Memoize
+            setattr(self, what, result)
+        except AttributeError:
+            # Oops... Oh well. I tried.
+            pass
+
         return result
 
     @property


Test patch: