extending python with a C-written dll

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Baptiste PERIN

    #1

    extending python with a C-written dll

    Hi,

    I'm trying to make a windows dll reachable from a python script .. and
    I'm encountering troubles during link step ..

    I use "lcc" to compile and link
    I use python 2.3 under win XP

    Here's the compile command which seems to work well :
    -----------------------------------------------------
    lcclnk.exe -entry inithello -dll -o hello.dll hello.obj -L
    "C:\python23\li bs" python23.lib


    Here's the link command which fails:
    ------------------------------------
    lcclnk.exe -entry inithello -dll -o hello.dll hello.obj -L
    "C:\python23\li bs" python23.lib


    Here are the errors :
    ---------------------
    Error c:\...\plugins\ hello.c 14 undefined reference to __imp__Py_Build Value
    Error c:\...\plugins\ hello.c 46 undefined reference to __imp__Py_InitM odule4



    Here's the C code which compile but does not link :
    --------------------------------------------------

    #include <Python.h>

    PyObject *helloHello(PyO bject *self, PyObject *args)
    {
    printf("Hello World!");
    return Py_BuildValue(" i", 1);
    }

    PyObject *helloHelloStri ng(PyObject *self, PyObject *args)
    {
    return Py_BuildValue(" s", "Hello World!");
    }

    static char MHello__doc__[] = "This is a simple Python C extension example";
    static char Hello__doc__[] = "Prints in the console";
    static char HelloString__do c__[] = "Returns message as string";

    PyMethodDef helloMethods[] = {
    {"Hello", helloHello, METH_VARARGS, Hello__doc__},
    {"HelloStrin g", helloHelloStrin g, METH_VARARGS, HelloString__do c__},
    {NULL, NULL}
    };

    void __declspec(dlle xport) inithello()
    {
    (void)Py_InitMo dule3("hello", helloMethods, MHello__doc__);
    }

    Can anyone help me?
  • Grant Edwards

    #2
    Re: extending python with a C-written dll

    On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
    [color=blue]
    > I'm trying to make a windows dll reachable from a python script..[/color]

    FWIW, you can call dll's using the ctypes modules without
    mucking around in C. There may be performance reasons to build
    a "real" python module, but I haven't run across them yet.

    --
    Grant Edwards grante Yow! I KAISER ROLL?! What
    at good is a Kaiser Roll
    visi.com without a little COLE SLAW
    on the SIDE?

    Comment

    • Jean-Baptiste PERIN

      #3
      Re: extending python with a C-written dll

      Grant Edwards a écrit :[color=blue]
      > On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
      >
      >[color=green]
      >>I'm trying to make a windows dll reachable from a python script..[/color]
      >
      >
      > FWIW, you can call dll's using the ctypes modules without
      > mucking around in C. There may be performance reasons to build
      > a "real" python module, but I haven't run across them yet.
      >[/color]

      Not really a performance reason .. but mainly because I have a huge code
      produced in C and no time to port it in python ..
      Moreover .. I need the python program to interact with the C code
      ...(perfom calls to dll-defined functions)

      I don't know ctypes module .. do you really think it can help ?
      Do you have a link to quickly learn about it ?

      The only thing I found is :






      Comment

      • Grant Edwards

        #4
        Re: extending python with a C-written dll

        On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
        [color=blue][color=green][color=darkred]
        >>>I'm trying to make a windows dll reachable from a python script..[/color]
        >>
        >> FWIW, you can call dll's using the ctypes modules without
        >> mucking around in C. There may be performance reasons to build
        >> a "real" python module, but I haven't run across them yet.[/color]
        >
        > Not really a performance reason .. but mainly because I have a
        > huge code produced in C and no time to port it in python ..
        > Moreover .. I need the python program to interact with the C
        > code ..(perfom calls to dll-defined functions)
        >
        > I don't know ctypes module .. do you really think it can help?[/color]

        It lets you easily call functions in DLLs.
        [color=blue]
        > Do you have a link to quickly learn about it ?[/color]



        [color=blue]
        > The only thing I found is :
        > http://www.python.org/workshops/1994...Classes_5.html[/color]

        I think that is unrelated.

        --
        Grant Edwards grante Yow! I HAVE a towel.
        at
        visi.com

        Comment

        • Jean-Baptiste PERIN

          #5
          Re: extending python with a C-written dll

          Grant Edwards a écrit :
          [color=blue]
          > On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
          >
          >[color=green][color=darkred]
          >>>>I'm trying to make a windows dll reachable from a python script..
          >>>
          >>>FWIW, you can call dll's using the ctypes modules without
          >>>mucking around in C. There may be performance reasons to build
          >>>a "real" python module, but I haven't run across them yet.[/color]
          >>
          >>Not really a performance reason .. but mainly because I have a
          >>huge code produced in C and no time to port it in python ..
          >>Moreover .. I need the python program to interact with the C
          >>code ..(perfom calls to dll-defined functions)
          >>
          >>I don't know ctypes module .. do you really think it can help?[/color]
          >
          >
          > It lets you easily call functions in DLLs.
          >
          >[color=green]
          >>Do you have a link to quickly learn about it ?[/color]
          >
          >
          > http://starship.python.net/crew/theller/ctypes/
          > http://starship.python.net/crew/thel.../tutorial.html
          >
          >[color=green]
          >>The only thing I found is :
          >>http://www.python.org/workshops/1994...Classes_5.html[/color]
          >
          >
          > I think that is unrelated.
          >[/color]

          OK .. ctypes looks great .. and I plan to use it for futur purposes

          The fact is that the python interpreter I use is not a standard one
          It is a python interpreter delivered within a software named Blender.
          I don't know whether it is possible or not to add ctypes to it ..(I
          don't even have a python shell to perform the setup)

          I'm sure it is possible to use dll-built with it ..
          So please .. can you tell me where I can find Py_BuildValue and
          Py_InitModule4 to link with ?



          Comment

          • Grant Edwards

            #6
            Re: extending python with a C-written dll

            On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
            [color=blue]
            > OK .. ctypes looks great .. and I plan to use it for futur
            > purposes
            >
            > The fact is that the python interpreter I use is not a
            > standard one It is a python interpreter delivered within a
            > software named Blender. I don't know whether it is possible or
            > not to add ctypes to it ..(I don't even have a python shell to
            > perform the setup)
            >
            > I'm sure it is possible to use dll-built with it .. So please
            > .. can you tell me where I can find Py_BuildValue and
            > Py_InitModule4 to link with ?[/color]

            Sorry, I don't know anything about doing C lanugage extensions
            under Windows.

            --
            Grant Edwards grante Yow! Boy, am I glad it's
            at only 1971...
            visi.com

            Comment

            • Diez B. Roggisch

              #7
              Re: extending python with a C-written dll

              > The fact is that the python interpreter I use is not a standard one[color=blue]
              > It is a python interpreter delivered within a software named Blender.
              > I don't know whether it is possible or not to add ctypes to it ..(I
              > don't even have a python shell to perform the setup)
              >
              > I'm sure it is possible to use dll-built with it ..
              > So please .. can you tell me where I can find Py_BuildValue and
              > Py_InitModule4 to link with ?[/color]

              I doubt that adding new modules as dlls is easier than incorporating
              pure-python ones or a mixture of both - as ctypes most probably is. After
              all, its pythons module loading mechanism that kicks in for all cases.

              If ctypes suits your needs outside of blender, I'd go for trying to
              incorporate it into the blender interpreter.

              --
              Regards,

              Diez B. Roggisch

              Comment

              • Thomas Heller

                #8
                Re: extending python with a C-written dll

                Jean-Baptiste PERIN <jb_perin@yahoo .fr> writes:
                [color=blue]
                > Grant Edwards a écrit :
                >[color=green]
                >> On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
                >>[color=darkred]
                >>>>>I'm trying to make a windows dll reachable from a python script..
                >>>>
                >>>>FWIW, you can call dll's using the ctypes modules without
                >>>>mucking around in C. There may be performance reasons to build
                >>>>a "real" python module, but I haven't run across them yet.
                >>>
                >>>Not really a performance reason .. but mainly because I have a
                >>>huge code produced in C and no time to port it in python ..
                >>>Moreover .. I need the python program to interact with the C
                >>>code ..(perfom calls to dll-defined functions)
                >>>
                >>>I don't know ctypes module .. do you really think it can help?[/color]
                >> It lets you easily call functions in DLLs.
                >>[color=darkred]
                >>>Do you have a link to quickly learn about it ?[/color]
                >> http://starship.python.net/crew/theller/ctypes/
                >> http://starship.python.net/crew/thel.../tutorial.html
                >>[color=darkred]
                >>>The only thing I found is :
                >>>http://www.python.org/workshops/1994...Classes_5.html[/color]
                >> I think that is unrelated.
                >>[/color]
                >
                > OK .. ctypes looks great .. and I plan to use it for futur purposes
                >
                > The fact is that the python interpreter I use is not a standard one
                > It is a python interpreter delivered within a software named Blender.
                > I don't know whether it is possible or not to add ctypes to it ..(I
                > don't even have a python shell to perform the setup)[/color]

                You could try to download the ctypes windows installer, use winzip or
                equivalent to unpack the contained files (you need _ctypes.pyd, and the
                ctypes directory containing __init__.py), and copy them to some
                directory where python can find them.

                Thomas

                Comment

                • Jean-Baptiste PERIN

                  #9
                  Re: extending python with a C-written dll

                  thanks for your valuable help .. ctypes is not lost for ever ..I'm
                  going to explore a little bit .. it may suit my need in the end ..


                  I had read :



                  and eveything looked so simple in it ..
                  I'm sad to realize that it only works for linux plateform :(

                  I've tried to build and install ctypes from sources

                  I ran the following command:
                  [color=blue][color=green]
                  >> python setup.py build[/color][/color]

                  and I got the following message:
                  [color=blue][color=green]
                  >> error: Python was built with version 6 of Visual Studio, and[/color][/color]
                  extensions need to
                  be built with the same version of the compiler, but it isn't installed.


                  :( :( :( :( So sad ....

                  Comment

                  • Jean-Baptiste PERIN

                    #10
                    Re: extending python with a C-written dll

                    Diez B. Roggisch a écrit :
                    [color=blue][color=green]
                    >>The fact is that the python interpreter I use is not a standard one
                    >>It is a python interpreter delivered within a software named Blender.
                    >>I don't know whether it is possible or not to add ctypes to it ..(I
                    >>don't even have a python shell to perform the setup)
                    >>
                    >>I'm sure it is possible to use dll-built with it ..
                    >>So please .. can you tell me where I can find Py_BuildValue and
                    >>Py_InitModule 4 to link with ?[/color]
                    >
                    >
                    > I doubt that adding new modules as dlls is easier than incorporating
                    > pure-python ones or a mixture of both - as ctypes most probably is. After
                    > all, its pythons module loading mechanism that kicks in for all cases.
                    >
                    > If ctypes suits your needs outside of blender, I'd go for trying to
                    > incorporate it into the blender interpreter.
                    >[/color]


                    I've tried to build and install ctypes from sources

                    I ran the following command:
                    [color=blue][color=green]
                    >> python setup.py build[/color][/color]

                    and I got the following message:
                    [color=blue][color=green]
                    >> error: Python was built with version 6 of Visual Studio, and[/color][/color]
                    extensions need to
                    be built with the same version of the compiler, but it isn't installed.


                    :( :( :( :( So sad ....

                    Comment

                    • Jean-Baptiste PERIN

                      #11
                      Re: extending python with a C-written dll

                      Thomas Heller a écrit :
                      [color=blue]
                      > Jean-Baptiste PERIN <jb_perin@yahoo .fr> writes:
                      >
                      >[color=green]
                      >>Grant Edwards a écrit :
                      >>
                      >>[color=darkred]
                      >>>On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
                      >>>
                      >>>
                      >>>>>>I'm trying to make a windows dll reachable from a python script..
                      >>>>>
                      >>>>>FWIW, you can call dll's using the ctypes modules without
                      >>>>>mucking around in C. There may be performance reasons to build
                      >>>>>a "real" python module, but I haven't run across them yet.
                      >>>>
                      >>>>Not really a performance reason .. but mainly because I have a
                      >>>>huge code produced in C and no time to port it in python ..
                      >>>>Moreover .. I need the python program to interact with the C
                      >>>>code ..(perfom calls to dll-defined functions)
                      >>>>
                      >>>>I don't know ctypes module .. do you really think it can help?
                      >>>
                      >>>It lets you easily call functions in DLLs.
                      >>>
                      >>>
                      >>>>Do you have a link to quickly learn about it ?
                      >>>
                      >>>http://starship.python.net/crew/theller/ctypes/
                      >>>http://starship.python.net/crew/thel.../tutorial.html
                      >>>
                      >>>
                      >>>>The only thing I found is :
                      >>>>http://www.python.org/workshops/1994...Classes_5.html
                      >>>
                      >>>I think that is unrelated.
                      >>>[/color]
                      >>
                      >>OK .. ctypes looks great .. and I plan to use it for futur purposes
                      >>
                      >>The fact is that the python interpreter I use is not a standard one
                      >>It is a python interpreter delivered within a software named Blender.
                      >>I don't know whether it is possible or not to add ctypes to it ..(I
                      >>don't even have a python shell to perform the setup)[/color]
                      >
                      >
                      > You could try to download the ctypes windows installer, use winzip or
                      > equivalent to unpack the contained files (you need _ctypes.pyd, and the
                      > ctypes directory containing __init__.py), and copy them to some
                      > directory where python can find them.
                      >
                      > Thomas[/color]

                      I took a look at the ctypes tutorial .. Here's what I saw ..
                      [color=blue][color=green][color=darkred]
                      >>> from ctypes import *
                      >>> printf = cdll.msvcrt.pri ntf
                      >>> printf.argtypes = [c_char_p, c_char_p, c_int, c_double]
                      >>> printf("String '%s', Int %d, Double %f\n", "Hi", 10, 2.2)[/color][/color][/color]

                      just to perfom a printf !!!

                      now look at http://www.python.org/dev/doc/devel/ext/intro.html

                      and look at the expected result .. it is far simpler from the python
                      side view

                      I think ctype is uselful when dealing with dll you're not the author of

                      But if noone can help me to extend python with my own dll .. I going to
                      explore ctypes .. but I find it's bothering .. and frustrating not to
                      achieve what I wanted to do ..

                      Moreover .. I tried to build and install ctypes from source ..
                      I ran the command
                      [color=blue][color=green]
                      >> python setup.py build[/color][/color]

                      and I got the following message:
                      [color=blue][color=green]
                      >> error: Python was built with version 6 of Visual Studio, and[/color][/color]
                      extensions need to
                      be built with the same version of the compiler, but it isn't installed.


                      SO SAD :( :( :(

                      Comment

                      • Diez B. Roggisch

                        #12
                        Re: extending python with a C-written dll

                        > and look at the expected result .. it is far simpler from the python[color=blue]
                        > side view[/color]

                        But you have to do all the C-Work!
                        [color=blue]
                        >
                        > I think ctype is uselful when dealing with dll you're not the author of[/color]

                        It is useful, if you don't want to write an extension for the extra work
                        that is needed for that - for example if you are missing a C compiler.
                        [color=blue][color=green][color=darkred]
                        > >> error: Python was built with version 6 of Visual Studio, and[/color][/color]
                        > extensions need to
                        > be built with the same version of the compiler, but it isn't installed.[/color]

                        How do you expect to be able to write your own extension if you don't have
                        the environment to do so in the first place? that error has nothing to do
                        with python itself, but with you not having installed the right compiler.
                        THe same problem will arise building your own extension.

                        Others suggested trying the ctypes binary installer. Do so.
                        --
                        Regards,

                        Diez B. Roggisch

                        Comment

                        • Scott David Daniels

                          #13
                          Re: extending python with a C-written dll

                          Jean-Baptiste PERIN wrote:[color=blue]
                          > Hi,
                          >
                          > I'm trying to make a windows dll reachable from a python script .. and
                          > I'm encountering troubles during link step ..
                          >
                          > I use "lcc" to compile and link
                          > I use python 2.3 under win XP[/color]

                          Note that MinGW32 is a tested path for building (free) C extensions. I
                          don't know if anyone has a tested way of using lcc.

                          With MinGW32, your build step will be the delightfully easy:

                          python setup.py build --compiler=mingw3 2

                          --Scott David Daniels
                          Scott.Daniels@A cm.Org

                          Comment

                          • Jean-Baptiste PERIN

                            #14
                            Re: extending python with a C-written dll

                            [color=blue]
                            >[color=green][color=darkred]
                            >> >> error: Python was built with version 6 of Visual Studio, and[/color]
                            >>extensions need to
                            >>be built with the same version of the compiler, but it isn't installed.[/color]
                            >
                            >
                            > How do you expect to be able to write your own extension if you don't have
                            > the environment to do so in the first place? that error has nothing to do
                            > with python itself, but with you not having installed the right compiler.
                            > THe same problem will arise building your own extension.[/color]

                            I know it has nothing to do with python ..
                            It just makes me sad to see that python cannot get rid of Visual Studoi
                            to be extended . It's just amazing !!

                            Python is free
                            The compiler I use is free
                            The code I plan to produce will be free

                            Where can I find a free version of Visual Studio ?

                            the only thing I use that is not free is the operating system and it's
                            because I had no choice .. my company decided for it
                            [color=blue]
                            > Others suggested trying the ctypes binary installer. Do so.[/color]

                            Yes .. that's what I'm going to do .. but I find it frustrating ..

                            Comment

                            • Grant Edwards

                              #15
                              Re: extending python with a C-written dll

                              On 2004-12-20, Jean-Baptiste PERIN <jb_perin@yahoo .fr> wrote:
                              [color=blue]
                              > I know it has nothing to do with python ..
                              > It just makes me sad to see that python cannot get rid of Visual Studoi
                              > to be extended.[/color]

                              That's got nothing to do with Python. You have to compile
                              extensions using a compiler that has an ABI that's compatible
                              with the ABI of the compiler used to compile Python.

                              You appear to be using a binary distribution of Python that was
                              compiled with MSVS 6.0, therefore, you have to compile
                              extensions with a compiler that has an ABI compatible with MSVS
                              6.0.

                              It's no use complaining to us bout your C compiler not being
                              ABI compatible with MSVS 6.0.

                              If you don't like MSVS, then your options are to either use a
                              different Python binary distro that was compiled with an ABI
                              that's compatible with your C compiler, or build just build it
                              yourself using whatever compiler you want. I suspect the latter
                              will also require re-compiling Blender.

                              --
                              Grant Edwards grante Yow! Yow! Are we in the
                              at perfect mood?
                              visi.com

                              Comment

                              Working...