473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

dumping in destructor

I have a class which I want to save it's data automatically on disc,
when it's destroyed. I have following code:

from cPickle import dump

class __Register(object):
def __init__(self):
self.dict = {}
def __del__(self):
fh = open('aaa', 'w')
dump(self.dict, fh)
fh.close()

g_register = __Register() # global instance. I do not destroy it
manually, so destructor is called on iterpreter exit

But when g_register is being destroyed, dump seems to be already dead,
so I get:

Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound method __Register.__del__ of <MyWiki.Register.__Register object
at 0x835a74c>ignored

can I somehow save my data from destructor?
Oct 20 '08 #1
7 2062
On Mon, 20 Oct 2008 01:12:06 -0700, Митя wrote:
I have a class which I want to save it's data automatically on disc,
when it's destroyed. I have following code:

from cPickle import dump

class __Register(object):
def __init__(self):
self.dict = {}
def __del__(self):
fh = open('aaa', 'w')
dump(self.dict, fh)
fh.close()

g_register = __Register() # global instance. I do not destroy it
manually, so destructor is called on iterpreter exit
The call to `__del__()` is not guaranteed.
can I somehow save my data from destructor?
Reliably? No! Change your design, it won't work this way.

Ciao,
Marc 'BlackJack' Rintsch
Oct 20 '08 #2
Митя <ne*****@gmail.comwrites:
I have a class which I want to save it's data automatically on disc,
when it's destroyed. I have following code:

from cPickle import dump

class __Register(object):
def __init__(self):
self.dict = {}
def __del__(self):
fh = open('aaa', 'w')
dump(self.dict, fh)
fh.close()

g_register = __Register() # global instance. I do not destroy it
manually, so destructor is called on iterpreter exit
In that case you'd be better off using something like:

import atexit
....
g_register = __Register()
atexit.register(g_register.save)

'save' being a method that dumps the instance to the file.
Oct 20 '08 #3
On Oct 20, 10:12am, <neti...@gmail.comwrote:
But when g_register is being destroyed, dump seems to be already dead,
so I get:

Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound method __Register.__del__ of <MyWiki.Register.__Register object
at 0x835a74c>ignored

can I somehow save my data from destructor?
The best thing is to use the 'with' statement, but it requires you to
rewrite all
of your code. Alternatively you can use the atexit module. I wrote
once a
recipe that may be of interest to you:

http://code.activestate.com/recipes/523007/
Oct 20 '08 #4
Thanks for your answers!

my g_register is a global register, wich contains all my objects and
lives all the program lifetime. So 'with' is not appliable. Am I
right?

But using atexit sounds to be a good solution

On Oct 20, 1:58*pm, Michele Simionato <michele.simion...@gmail.com>
wrote:
On Oct 20, 10:12am, <neti...@gmail.comwrote:
But when g_register is being destroyed, dump seems to be already dead,
so I get:
Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound method __Register.__del__ of <MyWiki.Register.__Register object
at 0x835a74c>ignored
can I somehow save my data from destructor?

The best thing is to use the 'with' statement, but it requires you to
rewrite all
of your code. Alternatively you can use the atexit module. I wrote
once a
recipe that may be of interest to you:

http://code.activestate.com/recipes/523007/
Oct 20 '08 #5
Thank you for your answers!

my g_register is a global object, and it lives all the program's
lifetime, so 'with' is not appliable. Am I right?

I tried to use atexit and wrote following:

class _Register(object):
def dump(self):
....

class Registerable(object):
....

g_register = _Register()
atexit.register(g_register.dump)
....
....
g_register.add(Registerable('aa'))

But now I get:

cPickle.PicklingError: Can't pickle <class '__main__.Registerable'>:
attribute lookup __main__.Registerable failed

Does that mean that by the time of atexit execution my Registerable
class is already dead?
Michele Simionato wrote:
On Oct 20, 10:12�am, ���� <neti....@gmail.comwrote:
But when g_register is being destroyed, dump seems to be already dead,
so I get:

Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound method __Register.__del__ of <MyWiki.Register.__Register object
at 0x835a74c>ignored

can I somehow save my data from destructor?

The best thing is to use the 'with' statement, but it requires you to
rewrite all
of your code. Alternatively you can use the atexit module. I wrote
once a
recipe that may be of interest to you:

http://code.activestate.com/recipes/523007/
Oct 20 '08 #6
En Mon, 20 Oct 2008 10:01:07 -0200, Митя <ne*****@gmail.comescribió:
Thank you for your answers!

my g_register is a global object, and it lives all the program's
lifetime, so 'with' is not appliable. Am I right?
Why not? You could use a with statement (or try/finally) around your main
entry point.
I tried to use atexit and wrote following:

class _Register(object):
def dump(self):
....

class Registerable(object):
....

g_register = _Register()
atexit.register(g_register.dump)
...
...
g_register.add(Registerable('aa'))

But now I get:

cPickle.PicklingError: Can't pickle <class '__main__.Registerable'>:
attribute lookup __main__.Registerable failed

Does that mean that by the time of atexit execution my Registerable
class is already dead?
No, at least not due to using atexit. When atexit functions are executed,
the interpreter is still in a fully working state. From pythonrun.c,
function Py_Finalize:

/* The interpreter is still entirely intact at this point, and the
* exit funcs may be relying on that. In particular, if some thread
* or exit func is still waiting to do an import, the import machinery
* expects Py_IsInitialized() to return true. So don't say the
* interpreter is uninitialized until after the exit funcs have run.
* Note that Threading.py uses an exit func to do a join on all the
* threads created thru it, so this also protects pending imports in
* the threads created via Threading.
*/

Probably you have another problem in your code; try to use pickle alone
(not within atexit) and see what happens.

--
Gabriel Genellina

Oct 21 '08 #7
Thank you! I have already implemented custom load/save operations
without pickle. And they work fine on atexit.

Just for information: pickle.dump worked OK when called manually, but
being called by atexit it produeced the above described error. I don't
know why.
On Oct 21, 7:54 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Mon, 20 Oct 2008 10:01:07 -0200, ڧ <neti...@gmail.comescribi:
Thank you for your answers!
my g_register is a global object, and it lives all the program's
lifetime, so 'with' is not appliable. Am I right?

Why not? You could use a with statement (or try/finally) around your main
entry point.


I tried to use atexit and wrote following:
class _Register(object):
def dump(self):
....
class Registerable(object):
....
g_register = _Register()
atexit.register(g_register.dump)
...
...
g_register.add(Registerable('aa'))
But now I get:
cPickle.PicklingError: Can't pickle <class '__main__.Registerable'>:
attribute lookup __main__.Registerable failed
Does that mean that by the time of atexit execution my Registerable
class is already dead?

No, at least not due to using atexit. When atexit functions are executed,
the interpreter is still in a fully working state. From pythonrun.c,
function Py_Finalize:

/* The interpreter is still entirely intact at this point, and the
* exit funcs may be relying on that. In particular, if some thread
* or exit func is still waiting to do an import, the import machinery
* expects Py_IsInitialized() to return true. So don't say the
* interpreter is uninitialized until after the exit funcs have run.
* Note that Threading.py uses an exit func to do a join on all the
* threads created thru it, so this also protects pending importsin
* the threads created via Threading.
*/

Probably you have another problem in your code; try to use pickle alone
(not within atexit) and see what happens.

--
Gabriel Genellina
Oct 21 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
6
by: Greg Brant | last post by:
Hi, how can i backup a table / entire DB and get the index's as well as the data / create table's etc cheers Greg
10
by: ken | last post by:
hello, i'm writing a c program on a linux system. i'm debugging a segmentation fault but i don't want it to dump a core file because the memory footprint of the program is over 300Mb and i don't...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
3
by: noleander | last post by:
My Vis C++ program takes forever to exit. Reason: it is dumping potential memory leaks. I like finding leaks once a month, but not every time I run. How do I turn mem leak dumping off? I...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
11
by: AB | last post by:
Hi All, I've got an array of objects, during the execution of the program I'd like to assign a particular object to a certain element in the object array. The sample code's like this... class...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.