473,748 Members | 9,931 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 2788
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
1409
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
2004
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
1835
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
1727
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
2052
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
1859
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
3008
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
2685
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
2114
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
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
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...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
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
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.