473,699 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extending/Embedding python

I accidentally sent this to we*******@pytho n.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 working with Python for
a year or more now, but only doing simple extending in C/C++. I'm now
attempting some embedding and several questions have come to mind.

BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
studio.

1. (Not extending/embedding related at all) How can I pass in a load/bunch
of defines so I can use them over and over again, instead of having to copy
them in every *.py script. All my scripts use an "extension" dll that I
wrote that require a lot of constants. I looked a lot at that PyMemberDef
and Type stuff but didn't get it and don't know if that's the solution
anyway.

2. A couple simple examples I've seen for initModule() are written
differently. One only calls Py_InitModule(" module", module_methods) , but
the other also calls PyImport_AddMod ule("module"). What is the difference?
What does PyImport_AddMod ule() accomplish?

3. When embedding Python into my simple application, why can't I pass
application parameters? PyRun_SimpleStr ing seems to only take hard-coded
values. Can/How can I get around this? My code looks like:

if (!Py_IsInitiali zed())
{
Py_Initialize() ;
}
PyRun_SimpleStr ing("import MyModule");
PyRun_SimpleStr ing("MyModule.i nit(1, 'c:\\diag\\dsp. ldr', 0x5555)");
PyRun_SimpleStr ing("MyModule.M emoryTest(1, 0, 1)");
PyRun_SimpleStr ing("MyModule.S hutdown()");
Py_Finalize();

But I'd like to pass application variables instead of the hard-coded 1, 0, 1
and 0x5555, such as:

int appInt = 0x5555;
PyRun_SimpleStr ing("MyModule.i nit(1, 'c:\\diag\\dsp. ldr', appInt)");

I know I'm missing something fundamental here. Please advise.

Also, is there a mailing list that I should join for this topic?

Thank you!!!!!!

Alicia.

Jul 18 '05 #1
4 2785
Alicia Haumann wrote:
1. (Not extending/embedding related at all) How can I pass in a load/bunch
of defines so I can use them over and over again, instead of having to copy
them in every *.py script.
You should use PyModule_Add<Fo o>Constant.
2. A couple simple examples I've seen for initModule() are written
differently. One only calls Py_InitModule(" module", module_methods) , but
the other also calls PyImport_AddMod ule("module"). What is the difference?
What does PyImport_AddMod ule() accomplish?
See

http://docs.python.org/api/importing.html
3. When embedding Python into my simple application, why can't I pass
application parameters? PyRun_SimpleStr ing seems to only take hard-coded
values. Can/How can I get around this?


Don't use PyRun_SimpleStr ing; use PyObject_CallFu nction/Method instead.

HTH,
Martin
Jul 18 '05 #2
Thanks for any help that can be offered. I've been working with Python for
a year or more now, but only doing simple extending in C/C++. I'm now
attempting some embedding and several questions have come to mind.
Your ahead of me!
BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
studio.
I won't hold that against you. :)
1. (Not extending/embedding related at all) How can I pass in a load/bunch
of defines so I can use them over and over again, instead of having to copy
them in every *.py script. All my scripts use an "extension" dll that I
wrote that require a lot of constants. I looked a lot at that PyMemberDef
and Type stuff but didn't get it and don't know if that's the solution
anyway.
Why not just import these with a single import at the top of your python
script. There is probably also a way to make C do this to setup the
name space first too.
2. A couple simple examples I've seen for initModule() are written
differently. One only calls Py_InitModule(" module", module_methods) , but
the other also calls PyImport_AddMod ule("module"). What is the difference?
What does PyImport_AddMod ule() accomplish?
Can't help you there. I don't know much about actual embedding.
3. When embedding Python into my simple application, why can't I pass
application parameters? PyRun_SimpleStr ing seems to only take hard-coded
values. Can/How can I get around this? My code looks like:

if (!Py_IsInitiali zed())
{
Py_Initialize() ;
}
PyRun_SimpleStr ing("import MyModule");
PyRun_SimpleStr ing("MyModule.i nit(1, 'c:\\diag\\dsp. ldr', 0x5555)");
PyRun_SimpleStr ing("MyModule.M emoryTest(1, 0, 1)");
PyRun_SimpleStr ing("MyModule.S hutdown()");
Py_Finalize();

But I'd like to pass application variables instead of the hard-coded 1, 0, 1
and 0x5555, such as:

int appInt = 0x5555;
PyRun_SimpleStr ing("MyModule.i nit(1, 'c:\\diag\\dsp. ldr', appInt)");

I know I'm missing something fundamental here. Please advise.


I'm not certain this is what you mean -- but if by appInt -- you mean
appInt is defined in the C code, then you need to convert it's value
into a string then send it as a value like you were alread able to do.
I'm not embedding expert but it looks like the "SimpleStri ng" procedure
is just sending python code as text.

The other thing is that if you want to pass down actual variable data
into python it probably has to be coded as a python object -- remember
that python and C don't use the same data construct's, all of these must
be converted and the embedding stuff has routines to do this.

Also as an aside -- you might want to just make an ActiveX/COM extension
rather than dong embedding if your just using windows. See the book
Python Programming on Win32 to see how.

Rob
Jul 18 '05 #3
Thanks for any help that can be offered. I've been working with Python for
a year or more now, but only doing simple extending in C/C++. I'm now
attempting some embedding and several questions have come to mind.
Your ahead of me!
BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
studio.
I won't hold that against you. :)
1. (Not extending/embedding related at all) How can I pass in a load/bunch
of defines so I can use them over and over again, instead of having to copy
them in every *.py script. All my scripts use an "extension" dll that I
wrote that require a lot of constants. I looked a lot at that PyMemberDef
and Type stuff but didn't get it and don't know if that's the solution
anyway.
Why not just import these with a single import at the top of your python
script. There is probably also a way to make C do this to setup the
name space first too.
2. A couple simple examples I've seen for initModule() are written
differently. One only calls Py_InitModule(" module", module_methods) , but
the other also calls PyImport_AddMod ule("module"). What is the difference?
What does PyImport_AddMod ule() accomplish?
Can't help you there. I don't know much about actual embedding.
3. When embedding Python into my simple application, why can't I pass
application parameters? PyRun_SimpleStr ing seems to only take hard-coded
values. Can/How can I get around this? My code looks like:

if (!Py_IsInitiali zed())
{
Py_Initialize() ;
}
PyRun_SimpleStr ing("import MyModule");
PyRun_SimpleStr ing("MyModule.i nit(1, 'c:\\diag\\dsp. ldr', 0x5555)");
PyRun_SimpleStr ing("MyModule.M emoryTest(1, 0, 1)");
PyRun_SimpleStr ing("MyModule.S hutdown()");
Py_Finalize();

But I'd like to pass application variables instead of the hard-coded 1, 0, 1
and 0x5555, such as:

int appInt = 0x5555;
PyRun_SimpleStr ing("MyModule.i nit(1, 'c:\\diag\\dsp. ldr', appInt)");

I know I'm missing something fundamental here. Please advise.


I'm not certain this is what you mean -- but if by appInt -- you mean
appInt is defined in the C code, then you need to convert it's value
into a string then send it as a value like you were alread able to do.
I'm not embedding expert but it looks like the "SimpleStri ng" procedure
is just sending python code as text.

The other thing is that if you want to pass down actual variable data
into python it probably has to be coded as a python object -- remember
that python and C don't use the same data construct's, all of these must
be converted and the embedding stuff has routines to do this.

Also as an aside -- you might want to just make an ActiveX/COM extension
rather than dong embedding if your just using windows. See the book
Python Programming on Win32 to see how.

Rob
Jul 18 '05 #4
Have you checked out elmer?

http://elmer.sourceforge.net

It generates the code needed to embed a Python module into a C
application. Hope that helps,

Rick.
"Alicia Haumann" <al************ @orthodyne.com> wrote in message news:<ma******* *************** *************** *@python.org>.. .
I accidentally sent this to we*******@pytho n.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 working with Python for
a year or more now, but only doing simple extending in C/C++. I'm now
attempting some embedding and several questions have come to mind.

BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
studio.

1. (Not extending/embedding related at all) How can I pass in a load/bunch
of defines so I can use them over and over again, instead of having to copy
them in every *.py script. All my scripts use an "extension" dll that I
wrote that require a lot of constants. I looked a lot at that PyMemberDef
and Type stuff but didn't get it and don't know if that's the solution
anyway.

2. A couple simple examples I've seen for initModule() are written
differently. One only calls Py_InitModule(" module", module_methods) , but
the other also calls PyImport_AddMod ule("module"). What is the difference?
What does PyImport_AddMod ule() accomplish?

3. When embedding Python into my simple application, why can't I pass
application parameters? PyRun_SimpleStr ing seems to only take hard-coded
values. Can/How can I get around this? My code looks like:

if (!Py_IsInitiali zed())
{
Py_Initialize() ;
}
PyRun_SimpleStr ing("import MyModule");
PyRun_SimpleStr ing("MyModule.i nit(1, 'c:\\diag\\dsp. ldr', 0x5555)");
PyRun_SimpleStr ing("MyModule.M emoryTest(1, 0, 1)");
PyRun_SimpleStr ing("MyModule.S hutdown()");
Py_Finalize();

But I'd like to pass application variables instead of the hard-coded 1, 0, 1
and 0x5555, such as:

int appInt = 0x5555;
PyRun_SimpleStr ing("MyModule.i nit(1, 'c:\\diag\\dsp. ldr', appInt)");

I know I'm missing something fundamental here. Please advise.

Also, is there a mailing list that I should join for this topic?

Thank you!!!!!!

Alicia.

Jul 18 '05 #5

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

Similar topics

1
1408
by: Mark Harrison | last post by:
Is the document Extending and Embedding the Python Interpreter http://www.python.org/doc/current/ext/ext.html easily available in any other form? Pointers to either PDF or a single web page would be most appreciated! Many TIA,
3
1997
by: stefan | last post by:
Hi Folks, I currenty extended some of my C++ functionality to python and also embedded python to use python functionality in my C++ system (and use as well these extended functions). While this works fine with the core python functionality, as soon as I run a script (on the embedded system) which tries to import modules which are not in the core system, like "xml" or "re", it fails and says it cannot find the related dll (for example...
1
1833
by: Tommy Nordgren | last post by:
I want to write an application that embeds and extends (at least) the Python and Perl interpreters. Now i want to find as much as possible about the Python tools used for extending and embedding Python. To be more specific: My app should: 1. Parse an input file. 2. Call a script in some scripting language, to generate an output file, for example in C++. For task 2 I need to call an embedded interpreter, and also provide call backs from...
1
1722
by: Richard Townsend | last post by:
In the "Extending and Embedding" part of the Python documentation: section 5.4 "Extending Embedded Python" - it describes how to use a Python extension module from Python that is embedded in a C application. Is it safe to call Py_InitModule() more than once in the same application - in order to be able to use more than one extension module? -- Richard
3
2050
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 one-file version of this tutorial? 2) Is there anyone that can help me with this problem? The class is attached. Thanks all. Marco
1
1856
by: jeremito | last post by:
I am trying to learn how to extend and/or embed Python. I have looked at the document "Extending and Embedding the Python Interpreter" and also "Python/C API Reference Manual. In the examples shown in "Extending..." there are some things I ma not familiar with so I turn to the index in the Reference Manual, but they are not listed. For example, PyEval_CallObject and PyDict_GetAttrString. My question is this: Is the documentation out of...
6
3001
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 interface is providing a convenient scripting toolkit for the application. One example is that a user can write a python script like: player = Player() game.loadPlayer(player) player.moveTo(location)
3
2681
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 method: static PyObject * spam_system(PyObject *self, PyObject *args)
0
2109
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via callback.setHandler1(callback1) this only seems to affect pythons ability to trigger an "event" in c. PyObject *Handler is always NULL even after I call Register_Handler(...). I thought there was some magic here that was assigning the pointer *Handler to my python...
0
9184
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9048
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8931
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7767
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5878
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4635
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.