LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2010, 07:43 AM   #1
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Python - stop an object being destroyed?


Hi there - I'm quite new to Python, but I've got a tricky situation and Google doesn't seem to know what to do about it.

I'm writing some code to interface to a library written in C using ctypes (which is very nice, I must say...). The C library is object-orientated, so my Python code has this general structure:

Code:
# initialise ctypes
LIB = ctypes.cdll.LoadLibrary("libname.so")

# a class has this basic skeleton:
class ClassName:
    # setup return/argument types for functions this class uses
    LIB.class_name_foo.restype = c_void_p
    LIB.class_name_foo.argtypes = [c_char_p]

    # constructor
    def __init__(self):
        # use LIB to initialise an instance of this class
        LIB.class_name_init()

    # destructor
    def __del__(self):
        # use LIB to destroy the instance
        LIB.class_name_free()
Which is all very well in normal use, but I get into trouble at program termination. When the program ends, it seems LIB is no longer around - it's been cleaned up, and when I try to use it, I get "Exception AttributeError: "'NoneType' object has no attribute ..." errors.

Why is LIB being cleaned-up prematurely in the first place - do functions not hold references to the things they'll use?

Ideally, I want a way to mark LIB as the last thing that should be deleted in that module (or even that it shouldn't be deleted at all...) - is that possible? Or is there a way to manually increment the reference counter for LIB?

Or I might be doing something wrong - this seems like the sort of thing a language like Python should really protect you from!
 
Old 11-28-2010, 02:34 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

If you create "LIB" in your "main" ... then it shouldn't be destroyed until "main" exits, should it?

http://docs.python.org/dev/tutorial/...and-namespaces

Last edited by paulsm4; 11-28-2010 at 02:40 AM.
 
Old 11-28-2010, 03:04 PM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Original Poster
Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by paulsm4 View Post
If you create "LIB" in your "main" ... then it shouldn't be destroyed until "main" exits, should it?
Yeah, but that seems unnecessarily... well, bad. I plan to use the module in many projects, so each one would have to have a separate copy of the module, simply to change one line in it:

Code:
from XXX import LIB
to use from different modules (at least I think that's how you'd do it, I am quite new to Python...).

Also, it seems like bad design for a module to require other modules to provide something only it uses.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python 3 Object Oriented Programming: Managing objects LXer Syndicated Linux News 0 08-13-2010 04:50 AM
Python: How can I add a method to an object at runtime? cantab Programming 8 11-02-2009 12:37 PM
python: AttributeError: 'list' object has no attribute 'Append' browny_amiga Programming 3 01-12-2009 04:58 AM
LXer: Object Oriented Programming in Python LXer Syndicated Linux News 0 01-07-2009 06:20 AM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration