Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Extending/Embedding python

Reply
Thread Tools

Extending/Embedding python

 
 
Alicia Haumann
Guest
Posts: n/a
 
      09-01-2004
I accidentally sent this to http://www.velocityreviews.com/forums/(E-Mail Removed), 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.

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      09-02-2004
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
 
Reply With Quote
 
 
 
 
Robert M. Emmons
Guest
Posts: n/a
 
      09-04-2004

> 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
 
Reply With Quote
 
Robert M. Emmons
Guest
Posts: n/a
 
      09-04-2004

> 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
 
Reply With Quote
 
Rick Ratzel
Guest
Posts: n/a
 
      09-08-2004
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" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> I accidentally sent this to (E-Mail Removed), 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.

 
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
Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1 R. David Murray Python 0 01-17-2011 02:23 PM
Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1 Senthil Kumaran Python 0 01-17-2011 10:31 AM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Martin v. L÷wis Python 0 03-01-2008 10:51 PM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Paul Moore Python 0 03-01-2008 10:39 PM
Searching comp.lang.python/python-list@python.org (was: UTF-8) skip@pobox.com Python 0 03-10-2007 02:50 PM



Advertisments