Trying to embed python into C, help!

Discussion in 'Python' started by disgracelands, Aug 10, 2003.

  1. I've been looking at using python and it's been going fairly well so far, i
    can call C from python and python from C fine but now i've hit a snag and
    i'm wondering how you guys have got around it.

    I have to initialise python inside my C project so that i can make calls to
    python modules whenever i need to but i can't create a dll for the project
    as it's got to produce an exe or at least a lib so that i can execute the
    initialisation code. The only way around it would seem to be to create a
    separate dll project containing the python callback code that can then
    import it to python and have my C code load it too. The problem with this
    approach is that i'm doubtfull that the storage space for the callbacks will
    be consistant between the python using the dll and C loading it. Basically
    i *think* that i won't be able to get the callback data from python to C.

    I know theres got to be a way around it but apart from the above i haven't
    got a clue. Any help would be appreciated.

    dis
     
    disgracelands, Aug 10, 2003
    #1
    1. Advertising

  2. disgracelands wrote:

    > I've been looking at using python and it's been going fairly well so far,
    > i can call C from python and python from C fine but now i've hit a snag
    > and i'm wondering how you guys have got around it.


    To be honest, I'm having some trouble understanding exactly what
    your snag IS.


    > I have to initialise python inside my C project so that i can make calls
    > to python modules whenever i need to but i can't create a dll for the
    > project as it's got to produce an exe or at least a lib so that i can
    > execute the initialisation code.


    ....but why ever would you WANT to "create a dll" and what does THAT have
    to do with "initializing Python"...?


    > The only way around it would seem to be to create a
    > separate dll project containing the python callback code that can then
    > import it to python and have my C code load it too. The problem with this


    I'm trying to guess, from this sentence, that you labor under a
    mis-apprehension that your C code, which embeds Python, can only
    provide "Python extension modules" by supplying them as a separate
    DLL. Is that what you mean by "the python callback code that can then
    import it to python"...? Sorry if my guess is way off, but really I
    find this sentence unparsable and incomprehensible.

    Anyway, if that's your 'snag', rest assured that there is no such
    need. Your C program can extend Python, creating modules that
    Python code can import, without any DLL whatsoever. For example,
    get the source distribution of Python [I don't think the demos
    come with e.g. the Windows installer binary] and look at source
    file Demo/embed/demo.c -- you'll see it adds to Python, as a
    static module, a module 'xyzzy' from which Python code could
    import and call, without arguments, a function 'foo'. Unfortunately
    the Python code executed in the demo doesn't show that ability
    being used, but at least you'll see 'xyzzy' listed among the
    "builtin modules"; just add two lines such as:

    PyRun_SimpleString("import xyzzy\n");
    PyRun_SimpleString("print 'the answer is', xyzzy.foo()\n");

    among the other calls to PyRun_SimpleString, and you'll see it work.


    Alex
     
    Alex Martelli, Aug 10, 2003
    #2
    1. Advertising

  3. disgracelands

    Syver Enstad Guest

    "disgracelands" <> writes:

    > To be honest the entire idea of using dlls puzzled me since python
    > isn't platform dependant but thats the way its done in the
    > tutorials. The chances are that i've mis-interpreted something.


    You don't have to use .dll to extend python. You can compile it
    statically into the interpreter. classes such as str and dict are
    compiled into the interpreter statically (as far as I know). On
    platforms where some sort of dynamic linking is available such as
    linux and win32, python supports linking dynamically with that
    platforms dynamic link library type. That type is .dll for win32.

    Clear?


    --

    Vennlig hilsen

    Syver Enstad
     
    Syver Enstad, Aug 10, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olav Tollefsen

    How to embed dynamic generated image into HTML?

    Olav Tollefsen, Mar 1, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,939
    Olav Tollefsen
    Mar 1, 2004
  2. jstack
    Replies:
    1
    Views:
    628
    Tor Iver Wilhelmsen
    Jul 4, 2003
  3. A. Cavallo
    Replies:
    0
    Views:
    480
    A. Cavallo
    May 24, 2009
  4. Ryniek90
    Replies:
    1
    Views:
    439
    André
    Jul 29, 2009
  5. Marwan
    Replies:
    6
    Views:
    127
    Michael Torrie
    Feb 28, 2013
Loading...

Share This Page