Calling foreign functions from Python? ctypes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Watson

    #1

    Calling foreign functions from Python? ctypes?

    I need to call some Windows APIs. Is the only way to download ctypes or
    the win32 interfaces? Is there any plan to get ctypes batteries into
    the standard Python build?
  • Martin v. Löwis

    #2
    Re: Calling foreign functions from Python? ctypes?

    Paul Watson wrote:[color=blue]
    > I need to call some Windows APIs. Is the only way to download ctypes or
    > the win32 interfaces?[/color]

    That depends on the specific win32 interface you want to call.
    Typically, the answer is "yes".
    [color=blue]
    > Is there any plan to get ctypes batteries into the standard Python build?[/color]

    I don't have such a plan. To my knowledge, ctypes has never been
    contributed to Python, either, so its author apparently has no plan,
    either.

    Regards,
    Martin

    Comment

    • Neil Hodgson

      #3
      Re: Calling foreign functions from Python? ctypes?

      Paul Watson:
      [color=blue]
      > Is there any plan to get ctypes batteries into
      > the standard Python build?[/color]

      It is unlikely that ctypes will be included in the standard Python
      build as it allows unsafe memory access making it much easier to crash
      Python.

      Neil

      Comment

      • Paul Rubin

        #4
        Re: Calling foreign functions from Python? ctypes?

        "Martin v. Löwis" <martin@v.loewi s.de> writes:[color=blue]
        > To my knowledge, ctypes has never been contributed to Python,
        > either, so its author apparently has no plan, either.[/color]

        Has anyone from Python.org asked him or her?

        Comment

        • Martin v. Löwis

          #5
          Re: Calling foreign functions from Python? ctypes?

          Paul Rubin wrote:[color=blue][color=green]
          >>To my knowledge, ctypes has never been contributed to Python,
          >>either, so its author apparently has no plan, either.[/color]
          >
          >
          > Has anyone from Python.org asked him or her?[/color]

          I can't answer that question. I haven't (and I personally
          share Neil Hodgson's reservation wrt. to the possibility
          of crashing the interpreter).

          Regards,
          Martin

          Comment

          • Paul Watson

            #6
            Re: Calling foreign functions from Python? ctypes?

            Neil Hodgson wrote:[color=blue]
            > Paul Watson:
            >[color=green]
            >> Is there any plan to get ctypes batteries into the standard Python build?[/color]
            >
            >
            > It is unlikely that ctypes will be included in the standard Python
            > build as it allows unsafe memory access making it much easier to crash
            > Python.[/color]

            Does extending Python with any C/C++ function not do the same thing?

            Comment

            • Paul Watson

              #7
              Re: Calling foreign functions from Python? ctypes?

              Martin v. Löwis wrote:[color=blue]
              > Paul Watson wrote:
              >[color=green]
              >>I need to call some Windows APIs. Is the only way to download ctypes or
              >>the win32 interfaces?[/color]
              >
              >
              > That depends on the specific win32 interface you want to call.
              > Typically, the answer is "yes".[/color]

              I have used _winreg and I see winsound in the doc.

              I need to call GetVersionInfo( ) and handle VERSIONINFO information. I
              thought that distutils might have something, but I do not see it yet.
              Any suggestions?

              Comment

              • Neil Hodgson

                #8
                Re: Calling foreign functions from Python? ctypes?

                Paul Watson:
                [color=blue]
                > Neil Hodgson wrote:[color=green]
                >> It is unlikely that ctypes will be included in the standard Python
                >> build as it allows unsafe memory access making it much easier to crash
                >> Python.[/color]
                > Does extending Python with any C/C++ function not do the same thing?[/color]

                No. It is the responsibility of the extension author to ensure that
                there is no possibility of crashing Python. With ctypes, you have a
                generic mechanism that enables Python code to cause a crash.

                Neil

                Comment

                • Paul Rubin

                  #9
                  Re: Calling foreign functions from Python? ctypes?

                  Neil Hodgson <nyamatongwe+th under@gmail.com > writes:[color=blue]
                  > No. It is the responsibility of the extension author to ensure
                  > that there is no possibility of crashing Python. With ctypes, you have
                  > a generic mechanism that enables Python code to cause a crash.[/color]


                  would seem to also do that.

                  Comment

                  • Martin v. Löwis

                    #10
                    Re: Calling foreign functions from Python? ctypes?

                    Paul Watson wrote:[color=blue]
                    > I need to call GetVersionInfo( ) and handle VERSIONINFO information. I
                    > thought that distutils might have something, but I do not see it yet.
                    > Any suggestions?[/color]

                    You could write this specific API in VB, and then run cscript.exe in
                    a pipe; or you could write a wrapper module just for that specific
                    API (in case PythonWin is too large for you).

                    Regards,
                    Martin

                    Comment

                    • Martin v. Löwis

                      #11
                      Re: Calling foreign functions from Python? ctypes?

                      Paul Rubin wrote:[color=blue]
                      > http://docs.python.org/lib/module-dl.html
                      > would seem to also do that.[/color]

                      That's correct. It has the same (unhealable) flaw.
                      No surprise, though: it provides the same functionality.

                      Regards,
                      Martin

                      Comment

                      • Carl Friedrich Bolz

                        #12
                        Re: Calling foreign functions from Python? ctypes?

                        Jean-Paul Calderone wrote:[color=blue]
                        >
                        > I could probably dig up a few more, if you want. So what's ctypes on top of this?
                        >[/color]

                        another one:

                        cfbolz@gauss:~$ python
                        Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
                        [GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
                        Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
                        >>> import weakref
                        >>> ref = None
                        >>> class Target:[/color][/color][/color]
                        .... def __del__(self):
                        .... global ref
                        .... ref = weakref.ref(sel f)
                        ....[color=blue][color=green][color=darkred]
                        >>> def g():[/color][/color][/color]
                        .... w = Target()
                        .... w = None
                        .... print ref()
                        ....[color=blue][color=green][color=darkred]
                        >>> g()[/color][/color][/color]
                        Segmentation fault

                        There are dozends of segfaults in Python indeed.

                        Cheers,

                        Carl Friedrich Bolz

                        Comment

                        • Paul Watson

                          #13
                          Re: Calling foreign functions from Python? ctypes?

                          Martin v. Löwis wrote:[color=blue]
                          > Paul Watson wrote:
                          >[color=green]
                          >>I need to call GetVersionInfo( ) and handle VERSIONINFO information. I
                          >>thought that distutils might have something, but I do not see it yet.
                          >>Any suggestions?[/color]
                          >
                          > You could write this specific API in VB, and then run cscript.exe in
                          > a pipe; or you could write a wrapper module just for that specific
                          > API (in case PythonWin is too large for you).[/color]

                          I cannot find any way to get to GetVersionInfo in VBScript (cscript).

                          If so, this appears to leave me with no choice but to produce a Windows
                          executable, whether a .exe or C code wrapped from Python. Right?

                          Comment

                          • Martin v. Löwis

                            #14
                            Re: Calling foreign functions from Python? ctypes?

                            Paul Watson wrote:[color=blue]
                            > I cannot find any way to get to GetVersionInfo in VBScript (cscript).[/color]

                            Well, in VB6, you have ctypes. So you can call any API function you
                            like to.

                            For one implementation, see

                            http://www.andreavb.com/tip030020.html

                            Regards,
                            Martin

                            Comment

                            • Thomas Heller

                              #15
                              Re: Calling foreign functions from Python? ctypes?

                              "=?UTF-8?B?Ik1hcnRpbiB 2LiBMw7Z3aXMi?= " <martin@v.loewi s.de> writes:
                              [color=blue]
                              > Paul Watson wrote:[color=green]
                              >> I need to call some Windows APIs. Is the only way to download ctypes or
                              >> the win32 interfaces?[/color]
                              >
                              > That depends on the specific win32 interface you want to call.
                              > Typically, the answer is "yes".
                              >[color=green]
                              >> Is there any plan to get ctypes batteries into the standard Python build?[/color]
                              >
                              > I don't have such a plan. To my knowledge, ctypes has never been
                              > contributed to Python, either, so its author apparently has no plan,
                              > either.[/color]

                              As the author, I would be happy to see ctypes included with the standard
                              Python build.

                              Thomas

                              Comment

                              Working...