Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Embedding Python: Creating Python Class from Application

Reply
Thread Tools

Embedding Python: Creating Python Class from Application

 
 
Kakacek
Guest
Posts: n/a
 
      01-12-2006
Hello All,

Let's say I have a following python code:

class hw_class:
def __init__(self):
pass
def hello_world(self):
print 'Hello World!'
create_instance('hw_class', 'hw')
hw.hello_world()
hw = None

The 'create_instance' function should be implemented in the application
(powered by Delphi - P4D) which is embedding the Python.dll.

I am trying to do this for some time having no success. I am missing
these informations:
1. How to register global python variable from application which is
embedding python?
2. How to assign this variable with the class instance.
3. How to create the instance of class which is unknown at the compile
time.

Could someone supply the (C/Pascal) code fragment for this?

Thanx,

Jiri.

 
Reply With Quote
 
 
 
 
Kakacek
Guest
Posts: n/a
 
      01-12-2006
In the mean time I did this. It works however, it creates memory leaks.
Any idea what could be wrong?

procedure TForm1.PyCreateInstance(Sender: TObject; PSelf, Args:
PPyObject;
var Result: PPyObject);
var
BClassName: PChar;
BInstName: PChar;
//
BB: Boolean;
BModule: PPyObject;
BClass: PPyObject;
BInstance: PPyObject;
begin
// create_instance
PE.PyArg_ParseTuple(Args, 'ss:create_instance', [@BClassName,
@BInstName]);
PE.CheckError;
// Find module
BModule := PE.FindModule('__main__');
PE.CheckError;
if BModule <> nil then begin
// Find class object in the module
BClass := PE.PyObject_GetAttrString(BModule, BClassName);
PE.CheckError;
if BClass <> nil then begin
// Check it really is a class object
BB := PE.PyClass_Check(BClass);
PE.CheckError;
if BB then begin
// Calling the class object (to create a new instance)
BInstance := PE.PyObject_CallObject(BClass, nil);
PE.CheckError;
if BInstance <> nil then begin
// Add the instance to the module object
PE.PyObject_SetAttrString(BModule, BInstName, BInstance);
PE.CheckError;
end;
end;
PE.Py_DECREF(BClass);
end;
end;
//
Result := PE.ReturnNone;
PE.CheckError;
end;

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedding a python console inside a python application Zorigaman Python 1 08-17-2009 10:26 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Re: Embedding Python in a multi-threaded C++ application Mustafa Demirhan Python 0 11-09-2004 10:08 PM
Embedding Python in a multi-threaded C++ application Mustafa Demirhan Python 0 11-09-2004 07:06 PM
Embedding Python in a multi-threaded C++ application Mustafa Demirhan Python 0 11-09-2004 06:49 PM



Advertisments