472,982 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

Extending/Embedding python

I accidentally sent this to we*******@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 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_AddModule("module"). What is the difference?
What does PyImport_AddModule() accomplish?

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

if (!Py_IsInitialized())
{
Py_Initialize();
}
PyRun_SimpleString("import MyModule");
PyRun_SimpleString("MyModule.init(1, 'c:\\diag\\dsp.ldr', 0x5555)");
PyRun_SimpleString("MyModule.MemoryTest(1, 0, 1)");
PyRun_SimpleString("MyModule.Shutdown()");
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_SimpleString("MyModule.init(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 2710
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<Foo>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_AddModule("module"). What is the difference?
What does PyImport_AddModule() 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_SimpleString seems to only take hard-coded
values. Can/How can I get around this?


Don't use PyRun_SimpleString; use PyObject_CallFunction/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_AddModule("module"). What is the difference?
What does PyImport_AddModule() 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_SimpleString seems to only take hard-coded
values. Can/How can I get around this? My code looks like:

if (!Py_IsInitialized())
{
Py_Initialize();
}
PyRun_SimpleString("import MyModule");
PyRun_SimpleString("MyModule.init(1, 'c:\\diag\\dsp.ldr', 0x5555)");
PyRun_SimpleString("MyModule.MemoryTest(1, 0, 1)");
PyRun_SimpleString("MyModule.Shutdown()");
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_SimpleString("MyModule.init(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 "SimpleString" 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_AddModule("module"). What is the difference?
What does PyImport_AddModule() 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_SimpleString seems to only take hard-coded
values. Can/How can I get around this? My code looks like:

if (!Py_IsInitialized())
{
Py_Initialize();
}
PyRun_SimpleString("import MyModule");
PyRun_SimpleString("MyModule.init(1, 'c:\\diag\\dsp.ldr', 0x5555)");
PyRun_SimpleString("MyModule.MemoryTest(1, 0, 1)");
PyRun_SimpleString("MyModule.Shutdown()");
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_SimpleString("MyModule.init(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 "SimpleString" 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**************************************@pyt hon.org>...
I accidentally sent this to we*******@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 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_AddModule("module"). What is the difference?
What does PyImport_AddModule() accomplish?

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

if (!Py_IsInitialized())
{
Py_Initialize();
}
PyRun_SimpleString("import MyModule");
PyRun_SimpleString("MyModule.init(1, 'c:\\diag\\dsp.ldr', 0x5555)");
PyRun_SimpleString("MyModule.MemoryTest(1, 0, 1)");
PyRun_SimpleString("MyModule.Shutdown()");
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_SimpleString("MyModule.init(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
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...
3
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...
1
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...
1
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...
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...
1
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...
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: 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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.