473,397 Members | 2,068 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,397 software developers and data experts.

Cyclic references et C libraries

Hi,

I have a problem with a python module that uses C objects. In this
module (not mine, found on the net), I have one parent objet that
maintains a list of child objects. The child objects have a reference on
the parent object, so there is a cycle.
The parent object has a file handle (it is a native C object, not a
python "file object") as a member. This handle can be used with
functions like "lib_open", "lib_close", "lib_read_section".
My problem is that:
- I can not release the parent object in a fully automated manner, so
there is a leak, and since there is a native object, the handle will not
be closed. I can not modify the library to wrap the handle in a class,
because if such a class has a "__del__" method the python garbage
collector will not clean the cycle

I find it not funny to have leaks in Python, and to have to clean objet
by hand.

One solution would be to wrap the parent objet in a new class
(forwarding all the calls to the parent object). In the "__del__" method
of this class, I would clean the list of children object, and then
remove the reference on the parent objet so its "__del__" method will be
called. Am I right ?

Last question: why does the python file objects not have a "__del__"
method ? How the file can be closed when the last reference is remove ?
If I do a simple test, there is not file leak:

class Toto:
def __init__(self):
self.file = open("tata")

while 1:
a = Toto()
b = Toto()
a.member = b
b.member = a

The GC can clean the cycle because the file object doesn't have a
"__del__" method.

Fabien

Jul 18 '05 #1
1 1549
Fabien SK <fa********@free.fr> writes:
One solution would be to wrap the parent objet in a new class
(forwarding all the calls to the parent object). In the "__del__"
method of this class, I would clean the list of children object, and
then remove the reference on the parent objet so its "__del__" method
will be called. Am I right ?
I would not wrap the parent object, but the file handle. So do

class Handle:
def __init__(self, *args):
self.h = lib_open(*args)
def read_section(self, *args):
return lib_read_section(self.h, *args)
def __del__(self):
lib_close(self.h)

class Parent:
def __init__(self, ...):
# change
# self.handle = lib_open(...)
# to
self.Handle(...)

Then, the Handle class won't participate in a cycle, and it can safely
have an __del__.
Last question: why does the python file objects not have a "__del__"
method ? How the file can be closed when the last reference is remove
?


If a Python type is implemented in C, it does not need to have an
__del__. Instead, having a tp_del slot in its PyTypeObject C structure
is sufficient.

Regards,
Martin
Jul 18 '05 #2

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

Similar topics

3
by: Thomas Mailund | last post by:
Hi group. I have a problem with some C extensions I am working with and hope that some of you can help. Basically, I am wrapping a a tree structure from C where I have python methods for...
7
by: Brian Sabolik | last post by:
I'm not sure if I've broken any Object Oriented rules or not, but ... I have projects in 2 different solutions that need to use each other's methods. Therefore I may have an "update" method in...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
24
by: MLH | last post by:
Most people tell me I only need 3 references. It seems that many of my problems are related to references. I don't know what a reference is, quite frankly. I wish that, instead of about 200...
0
by: Adam J. Schaff | last post by:
Hello all. I am part of a team about to start work on a large enterprise application. There will be a main application and a bunch of smaller applications that are all related and use the same...
2
by: Matthias Kramm | last post by:
Hi All, I'm having a little bit of trouble using the "imp" module to dynamically import modules. It seems that somehow cyclic references of modules don't work. I'm unable to get the following...
3
by: Pohihihi | last post by:
This is a report from VSS newsgroup in hope of getting some more suggestions. Thanks, Po ------------------------------------------- I guess I am not getting the whole picture here. How does...
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
10
by: toton | last post by:
Hi I have a class called Session, which stores a vector of Page, like vector<PageAlso each Page need's to know the Session to which it is attached. Thus I have, (the classes has many other...
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.