[C++-sig] cPickle in extension module

Norbert Riedlin nr at netatec.de
Thu Jun 20 15:18:33 CEST 2002


Hi all,

in my application (embedding a python interpreter and using an
extension-module) I want to send python-objects between two python scripts.
So in my extension-module I expose the functions "module.to_script(obj)" and
"module.get_object()". Those functions are implemented using the
cPickle-module I import in my C++-application (I use a
cPickle-Singleton-class). My problem seems to be in "module.get_object()",
which is essentially implemented like the following:

  // this fn exposed as get_object
  PyObject* get_object(const std::string& msg)
  {
    return Netatec::PickleModule::Instance().Loads(msg);
  }

And PickleModule::Loads() looks like this:

  PyObject* Netatec::PickleModule::Loads(const std::string& theString) const
  {
    // m_loads previsously assigned to cPickle.loads
    return PyObject_CallFunction(m_loads, "s", theString.c_str());
  }

Now in python I can write:

  import module

  c = SomeClass()
  open("test.txt", "w").write(module.to_script(c))


The problem arises in a second script:

  import module

  def read():
    f=open("test.txt", "r")
    s = f.readlines()
    s1 =""
    for i in s:
      s1 += i
    t=module.get_object(s1) # 1.
    # use t

  read()

Everything works like expected. In the line marked 1. "t" is the previously
pickled object and I can use it. BUT: "t"'s reference-count seems to be too
high, "t" will never be deleted (It's __del__-method is never invoked). Am I
doing something wrong? Is there a better (more boostish way) to call
cPickle.loads()?


I'm using boost.python version 1.22.0. I know it's a bit aged, but in this
stage of our project we don't want to change core-parts of our application.

Thanks in advance for your help

Norbert

--
Norbert Riedlin
Tel.: +49 (0)7243-2176-24
Fax.: +49 (0)7243-2176-19
mailto:norbert.riedlin at netatec.de

netatec GmbH
Am Hardtwald 3
76275 Ettlingen







More information about the Cplusplus-sig mailing list