# ROOT objects can no longer be pickled for shelve

**URL:** <https://root-forum.cern.ch/t/root-objects-can-no-longer-be-pickled-for-shelve/38924>\
**Category:** ROOT\
**Tags:** bug, python, io\
**Created:** [April 16, 2020, 2:08pm UTC](https://root-forum.cern.ch/t/root-objects-can-no-longer-be-pickled-for-shelve/38924 "2020-04-16T14:08:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [April 16, 2020, 2:08pm UTC](https://root-forum.cern.ch/t/root-objects-can-no-longer-be-pickled-for-shelve/38924/1 "2020-04-16T14:08:46Z")

</div>

When I try to save a ROOT object using `shelve`, it panics and fails. This started happening after I brought my local build up-to-date with the master branch–I am not sure what changes caused the problem. Reproducer follows. Output attached.

* * *

```python
import shelve
import ROOT
df = ROOT.RDataFrame(10).Define('e', 'rdfentry_')
h = df.Histo1D('e')
shelf = shelve.open('test', 'n')
shelf['h'] = h

```

* * *

_ROOT Version:_ master  
_Platform:_ macOS  
_Compiler:_ Not Provided

* * *

[output.txt](https://root-forum.cern.ch/uploads/short-url/1xJaeDiumwMDATgeU8pa1jiw60h.txt) (19.8 KB)

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 16, 2020, 3:30pm UTC](https://root-forum.cern.ch/t/root-objects-can-no-longer-be-pickled-for-shelve/38924/2 "2020-04-16T15:30:20Z")

</div>

Hi,  
at what commit was your local build before? Recently, for some definition of recently, ROOT master has switched to a completely new version of PyROOT. My first guess would be that. You can see if switching `-Dpyroot_experimental=OFF` when building fixes the problem, if yes, that’s it, and I guess you should open a jira ticket.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [April 16, 2020, 5:31pm UTC](https://root-forum.cern.ch/t/root-objects-can-no-longer-be-pickled-for-shelve/38924/3 "2020-04-16T17:31:07Z")

</div>

Unfortunately, setting that flag appears to break pyROOT completely:

```python
In [1]: import ROOT
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-ee8dc4376aa8> in <module>()
----> 1 import ROOT

/Applications/root_build/lib/ROOT.py in <module>()
     22 ### system and interpreter setup ------------------------------------------------
     23 import os, sys, types
---> 24 import cppyy
     25 
     26 ## there's no version_info in 1.5.2

/Applications/root_build/lib/cppyy.py in <module>()
     59 sys.setdlopenflags( 0x100 | 0x2 ) # RTLD_GLOBAL | RTLD_NOW
     60 
---> 61 import libPyROOT as _backend
     62 
     63 # reset dl flags if needed

ImportError: dlopen(/Applications/root_build/lib/libPyROOT.so, 2): Library not loaded: @rpath/Python3.framework/Versions/3.7/Python3
  Referenced from: /Applications/root_build/lib/libPyROOT.so
  Reason: image not found

```

Total build command:

```bash
cmake -Dall=On -Dimt=On -DPYTHON_EXECUTABLE=/Users/michael/anaconda2/bin/python2.7 -DPYTHON_INCLUDE_DIR=/Users/michael/anaconda2/include/python2.7/ -DPYTHON_LIBRARY=/Users/michael/anaconda2/lib/libpython2.7.dylib -Dpyroot_experimental=OFF ../root/ && make

```

Build log attached.  
[build\_log.txt](https://root-forum.cern.ch/uploads/short-url/5h67NpA84rAtLZ9PDHKaTS3fU5B.txt) (856.3 KB)

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 16, 2020, 5:42pm UTC](https://root-forum.cern.ch/t/root-objects-can-no-longer-be-pickled-for-shelve/38924/4 "2020-04-16T17:42:18Z")

</div>

Well – time to hand this off to PyROOT expert @etejedor 😃

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 17, 2020, 7:36am UTC](https://root-forum.cern.ch/t/root-objects-can-no-longer-be-pickled-for-shelve/38924/5 "2020-04-17T07:36:20Z")

</div>

Hi,

The regular pickling works in master with the new PyROOT:

```auto
>>> import ROOT, pickle
>>> h = ROOT.TH1D()
>>> pickle.dumps ( h )
b'\x80\x03clibROOTPythonizations3_7\n_CPPInstance __expand__ \nq\x00B1\x02\x00\x00@\x00\x02-\xff\xff\xff\xffTH1D\x00@\x00\x02 \x00\x03@\x00\x01\xfe\x00\x08@\x00\x00\x0e\x00\x01\x00\x01\x00\x00\x00\x00\x03\x00\x00\...'

```

From what I see in the error stack you sent, `shelve` uses cPickle:

```auto
[/Users/michael/anaconda2/lib/python2.7/lib-dynload/cPickle.so] save (no debug info)
[/Users/michael/anaconda2/lib/python2.7/lib-dynload/cPickle.so] Pickler_dump (no debug info)

```

If I try the code above the `cPickle` instead of `pickle` it also works.

I don’t know how exactly `shelve` uses cPickle to trigger that error. Do you know what are the requirements of `shelve` with respect to the objects to be “shelved”?

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [April 29, 2020, 10:45pm UTC](https://root-forum.cern.ch/t/root-objects-can-no-longer-be-pickled-for-shelve/38924/6 "2020-04-29T22:45:06Z")

</div>

Hi, @etejedor,

So it turns out the problem is just with pickling RDF pointers, not TH1D. So, adjusting the reproducer above,

```python
import shelve
import ROOT
df = ROOT.RDataFrame(10).Define('e', 'rdfentry_')
h = df.Histo1D('e')
h_ = h.GetPtr()
shelf = shelve.open('test', 'n')
shelf['h'] = h_
shelf.close()

```

works, but

```python
shelf['h'] = h

```

does not. Since this is a pointer to a non-pickled ROOT object, perhaps this behavior is expected.

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 30, 2020, 7:40am UTC](https://root-forum.cern.ch/t/root-objects-can-no-longer-be-pickled-for-shelve/38924/7 "2020-04-30T07:40:05Z")

</div>

Hi @mwilkins ,

Yes, indeed you found the reason. We can’t serialize `RResultPtr`s (which is what e.g. `Histo1D` returns) because they contain a shared pointer to the underlying object, and we can’t serialize shared pointers at the moment.

You can also use `GetValue` to get the underlying object and shelve that.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 14, 2020, 7:40am UTC](https://root-forum.cern.ch/t/root-objects-can-no-longer-be-pickled-for-shelve/38924/8 "2020-05-14T07:40:10Z")

</div>

Closed on May 14, 2020, 7:40am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
