Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

Class destructor

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


nmm1 at cus

Feb 28, 2007, 1:00 AM

Post #1 of 8 (470 views)
Permalink
Class destructor

I am gradually making progress with my binary floating-point software,
but have had to rewrite several times as I have forgotten most of the
details of how to do it! After 30 years, I can't say I am surprised.

But I need to clean up workspace when a class (not object) is
deallocated. I can't easily use attributes, as people suggested,
because there is no anonymous storage built-in type. I could subvert
one of the existing storage types (buffer, string etc.), but that is
unclean. And I could write one, but that is excessive.

So far, I have been unable to track down how to get something called
when a class is destroyed. The obvious attempts all didn't work, in
a variety of ways. Surely there must be a method? This could be in
either Python or C.

Thanks.


Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1[at]cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


pje at telecommunity

Feb 28, 2007, 8:40 AM

Post #2 of 8 (432 views)
Permalink
Re: Class destructor [In reply to]

At 09:00 AM 2/28/2007 +0000, Nick Maclaren wrote:
>I am gradually making progress with my binary floating-point software,
>but have had to rewrite several times as I have forgotten most of the
>details of how to do it! After 30 years, I can't say I am surprised.
>
>But I need to clean up workspace when a class (not object) is
>deallocated. I can't easily use attributes, as people suggested,
>because there is no anonymous storage built-in type. I could subvert
>one of the existing storage types (buffer, string etc.), but that is
>unclean. And I could write one, but that is excessive.
>
>So far, I have been unable to track down how to get something called
>when a class is destroyed. The obvious attempts all didn't work, in
>a variety of ways. Surely there must be a method? This could be in
>either Python or C.

Have you tried a PyCObject? This is pretty much what they're for:

http://www.python.org/doc/1.5.2/api/cObjects.html

And yes, they're still around today:

http://www.python.org/doc/2.5/api/cObjects.html

(with an extra PyCObject_SetVoidPtr API added in in 2.4).

_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


nmm1 at cus

Feb 28, 2007, 9:24 AM

Post #3 of 8 (435 views)
Permalink
Re: Class destructor [In reply to]

"Phillip J. Eby" <pje[at]telecommunity.com> wrote:
>
> >But I need to clean up workspace when a class (not object) is
> >deallocated. I can't easily use attributes, as people suggested,
> >because there is no anonymous storage built-in type. I could subvert
> >one of the existing storage types (buffer, string etc.), but that is
> >unclean. And I could write one, but that is excessive.
> >
> >So far, I have been unable to track down how to get something called
> >when a class is destroyed. The obvious attempts all didn't work, in
> >a variety of ways. Surely there must be a method? This could be in
> >either Python or C.
>
> Have you tried a PyCObject? This is pretty much what they're for:

Oh, yes, I use them in several places, but they don't really help.

Their first problem is that they take a 'void *' and not a request
for space, so I have to allocate and deallocate the space manually.
Now, I could add a destructor to each of them and do that, but it
isn't really much prettier than subverting one of the semi-generic
storage types for an improper purpose!

It would be a heck of a lot cleaner to deallocate all of my space
in exactly the converse way that I allocate and initialise it. It
would also all me to collect and log statistics, should I so choose.
This could be VERY useful for tuning! I haven't done that, yet, but
might well do so.

All in all, what I need is some way to get a callback when a class
object is destroyed. Well, actually, any time from its last use
for object work and the time that its space is reclaimed - I don't
need any more precise time than that.

I suppose that I could add a C object as an attribute that points to
a block of memory that contains copies of all my workspace pointers,
and use the object deallocator to clean up. If all else fails, I
will try that, but it seems a hell of a long way round for what I
would have thought was a basic requirement.


Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1[at]cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


pje at telecommunity

Feb 28, 2007, 9:51 AM

Post #4 of 8 (434 views)
Permalink
Re: Class destructor [In reply to]

At 05:24 PM 2/28/2007 +0000, Nick Maclaren wrote:
>I suppose that I could add a C object as an attribute that points to
>a block of memory that contains copies of all my workspace pointers,
>and use the object deallocator to clean up. If all else fails, I
>will try that, but it seems a hell of a long way round for what I
>would have thought was a basic requirement.

Well, you could use a custom metaclass with a tp_dealloc or whatever. But
I just mainly meant that a PyCObject is almost as good as a weakref for
certain purposes -- i.e. it's got a pointer and a callback.

You could of course also use weak references, but that's a bit more awkward
as well.

_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


nmm1 at cus

Feb 28, 2007, 10:00 AM

Post #5 of 8 (436 views)
Permalink
Re: Class destructor [In reply to]

"Phillip J. Eby" <pje[at]telecommunity.com> wrote:
>
> Well, you could use a custom metaclass with a tp_dealloc or whatever.

Yes, I thought of that, but a custom metaclass to provide one callback
is pretty fair overkill!

> But I just mainly meant that a PyCObject is almost as good as a weakref
> for certain purposes -- i.e. it's got a pointer and a callback.

Ah. Yes. Thanks for suggesting it - if it is the simplest way, I
may as well do it.

> You could of course also use weak references, but that's a bit more
> awkward as well.

Yes. And they aren't a technology I have used (in Python), so I would
have to find out about them. Attributes etc. I have already played
with.


Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1[at]cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


guido at python

Mar 1, 2007, 5:16 PM

Post #6 of 8 (431 views)
Permalink
Re: Class destructor [In reply to]

On 2/28/07, Nick Maclaren <nmm1[at]cus.cam.ac.uk> wrote:
> I am gradually making progress with my binary floating-point software,
> but have had to rewrite several times as I have forgotten most of the
> details of how to do it! After 30 years, I can't say I am surprised.
>
> But I need to clean up workspace when a class (not object) is
> deallocated. I can't easily use attributes, as people suggested,
> because there is no anonymous storage built-in type. I could subvert
> one of the existing storage types (buffer, string etc.), but that is
> unclean. And I could write one, but that is excessive.

Can you explain the reason for cleaning up in this scenario? Are you
rapidly creating and destroying temporary class objects? Why can't you
rely on the regular garbage collection process? Or does you class
create an external resource like a temp file?

--
--Guido van Rossum (home page: http://www.python.org/~guido/)
_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


nmm1 at cus

Mar 2, 2007, 1:23 AM

Post #7 of 8 (429 views)
Permalink
Re: Class destructor [In reply to]

"Guido van Rossum" <guido[at]python.org> wrote:
>
> Can you explain the reason for cleaning up in this scenario? Are you
> rapidly creating and destroying temporary class objects? Why can't you
> rely on the regular garbage collection process? Or does you class
> create an external resource like a temp file?

Effectively the latter. The C level defines a meta-class, which is
instantiated with a specific precision, range etc. to derive the class
that can actually be used. There can be an arbitrary number of such
derived classes, with different properties. Very like Decimal, but
with the context as part of the derived class.

The instantiation creates quite a lot of constants and scratch space,
some of which are Python objects but others of which are just Python
memory (PyMem_Malloc); this is where an anonymous storage built-in
type would be useful. The contents of these are of no interest to
any Python code, and even the objects are ones which mustn't be
accessed by the exported interfaces.

Also, on efficiency grounds, all of those need to be accessible by
C pointers from the exported class. Searching by name every time they
are needed is far too much overhead. Note that, as with Decimal, the
issue is that they are arbitrary sized and therefore can't simply be
put in the class structure.

Now, currently, I have implemented the suggestion of using the callback
on the C object that points to the structure that contains the pointers
to all of those. I need to investigate it in more detail, because I
have had mixed success - that could well be the result of another bug
in my code, so let's not worry about it.

In THIS case, I am now pretty sure that I don't need any more, but I
can imagine classes where it wouldn't be adequate. In particular,
THIS code doesn't need to do anything other than free memory, so I
don't care whether the C object attribute callback is called before
or after the class object is disposed of. But that is obviously not
the case in general.


Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1[at]cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


nmm1 at cus

Mar 2, 2007, 1:53 AM

Post #8 of 8 (430 views)
Permalink
Re: Class destructor [In reply to]

Sorry about a second message, but I mentioned this aspect earlier,
and it is semi-independent.

If I want to produce statistics, such as the times spent in various
operations, I need a callback when the class is disposed of. Now,
that WOULD be inconvenient to use the C object attribute callback,
unless I could be sure that would be called while the class structure
is still around. That could be resolved by taking a copy, of course,
but that is messy.

This also relates to one of my problems with the callback. I am not
being called back if the class is still live at program termination;
ones that have had their use counts drop to zero do cause a callback,
but not ones whose use count is above zero. I am not sure whether
this is my error or a feature of the garbage collector.

If the latter, it doesn't matter from the point of view of freeing
space, but is assuredly a real pain for producing statistics. I
haven't looked into it, as it is not an immediate task.


Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1[at]cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.