Extending/Embedding python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alicia Haumann

    Extending/Embedding python

    I accidentally sent this to webmaster@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.

  • Martin v. Löwis

    #2
    Re: Extending/Embedding python

    Alicia Haumann wrote:[color=blue]
    > 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.[/color]

    You should use PyModule_Add<Fo o>Constant.
    [color=blue]
    > 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?[/color]

    See


    [color=blue]
    > 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?[/color]

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

    HTH,
    Martin

    Comment

    • Robert M. Emmons

      #3
      Re: Extending/Embedding python

      [color=blue]
      > 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.[/color]

      Your ahead of me!
      [color=blue]
      > BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
      > studio.[/color]

      I won't hold that against you. :)
      [color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > 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?[/color]

      Can't help you there. I don't know much about actual embedding.
      [color=blue]
      > 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.[/color]

      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

      Comment

      • Robert M. Emmons

        #4
        Re: Extending/Embedding python

        [color=blue]
        > 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.[/color]

        Your ahead of me!
        [color=blue]
        > BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
        > studio.[/color]

        I won't hold that against you. :)
        [color=blue]
        > 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.[/color]

        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.
        [color=blue]
        > 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?[/color]

        Can't help you there. I don't know much about actual embedding.
        [color=blue]
        > 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.[/color]

        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

        Comment

        • Rick Ratzel

          #5
          Re: Extending/Embedding python

          Have you checked out elmer?



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

          Rick.


          "Alicia Haumann" <alicia.haumann @orthodyne.com> wrote in message news:<mailman.2 744.1094082648. 5135.python-list@python.org >...[color=blue]
          > I accidentally sent this to webmaster@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.[/color]

          Comment

          Working...