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

Embedding Python: Creating Python Class from Application

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.

Jan 12 '06 #1
1 1818
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;

Jan 12 '06 #2

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

Similar topics

0
by: jordi | last post by:
Hi, I'm starting to use Python embedded in a C program. I'm using Python to execute several scripts using as a variables information retrieved for several multithread "agents" written in C. ...
0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
1
by: Martin | last post by:
Greetings, I am new to python and wish to embed python in an 3D graphics application to provide application automation. The high level goal is to be able to drive my app from a script for batch...
2
by: Roose | last post by:
With some googling I have found these resources: http://docs.python.org/ext/win-dlls.html http://www.python.org/doc/faq/windows.html I have a large Win32/MFC/C/C++ application that has an...
1
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
3
by: Marco Meoni | last post by:
Hi all! I've a problem with a C++ class that has to be included in a python application. One way to do it is Extending and Embedding the Python Interpreter Now i have 2 questions 1) Is there a...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
3
by: dmoore | last post by:
Hi Folks: I have a question about the use of static members in Python/C extensions. Take the simple example from the "Extending and Embedding the Python Interpreter" docs: A simple module...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.