Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling foreign functions from Python? ctypes?

6 views
Skip to first unread message

Paul Watson

unread,
Jan 6, 2006, 5:58:01 PM1/6/06
to
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"

unread,
Jan 6, 2006, 6:09:09 PM1/6/06
to Paul Watson
Paul Watson wrote:
> I need to call some Windows APIs. Is the only way to download ctypes or
> the win32 interfaces?

That depends on the specific win32 interface you want to call.
Typically, the answer is "yes".

> Is there any plan to get ctypes batteries into the standard Python build?

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

Neil Hodgson

unread,
Jan 6, 2006, 6:09:50 PM1/6/06
to
Paul Watson:

> Is there any plan to get ctypes batteries into
> the standard Python build?

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

Paul Rubin

unread,
Jan 6, 2006, 6:18:59 PM1/6/06
to
"Martin v. Löwis" <mar...@v.loewis.de> writes:
> To my knowledge, ctypes has never been contributed to Python,
> either, so its author apparently has no plan, either.

Has anyone from Python.org asked him or her?

"Martin v. Löwis"

unread,
Jan 6, 2006, 6:34:37 PM1/6/06
to
Paul Rubin wrote:
>>To my knowledge, ctypes has never been contributed to Python,
>>either, so its author apparently has no plan, either.
>
>
> Has anyone from Python.org asked him or her?

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

Paul Watson

unread,
Jan 6, 2006, 7:36:34 PM1/6/06
to

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

Paul Watson

unread,
Jan 6, 2006, 7:48:22 PM1/6/06
to
Martin v. Löwis wrote:
> Paul Watson wrote:
>
>>I need to call some Windows APIs. Is the only way to download ctypes or
>>the win32 interfaces?
>
>
> That depends on the specific win32 interface you want to call.
> Typically, the answer is "yes".

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?

Neil Hodgson

unread,
Jan 6, 2006, 7:55:45 PM1/6/06
to
Paul Watson:

> Neil Hodgson wrote:
>> 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.
> Does extending Python with any C/C++ function not do the same thing?

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

Paul Rubin

unread,
Jan 6, 2006, 8:27:44 PM1/6/06
to
Neil Hodgson <nyamatong...@gmail.com> writes:
> 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.

http://docs.python.org/lib/module-dl.html
would seem to also do that.

"Martin v. Löwis"

unread,
Jan 6, 2006, 8:57:26 PM1/6/06
to Paul Watson

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

"Martin v. Löwis"

unread,
Jan 6, 2006, 9:10:32 PM1/6/06
to
Paul Rubin wrote:
> http://docs.python.org/lib/module-dl.html
> would seem to also do that.

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

Regards,
Martin

Jean-Paul Calderone

unread,
Jan 6, 2006, 9:23:25 PM1/6/06
to pytho...@python.org

Aahhh, come on. ctypes is crazy useful. Besides:

exarkun@kunai:~$ python < .
Segmentation fault
exarkun@kunai:~$ 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.
>>> import os
>>> import marshal
>>> for i in range(1024):
... try:
... marshal.loads(os.urandom(16))
... except:
... pass
...
Segmentation fault
exarkun@kunai:~$ 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.
>>> import dl
>>> dl.open('/lib/libc.so.6').call('memcpy', 1, 2, 3)
Segmentation fault
exarkun@kunai:~$ 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.
>>> import sys
>>> sys.setrecursionlimit(1000000000)
__main__:1: DeprecationWarning: integer argument expected, got float
>>> (lambda f: f(f))(lambda f: f(f))
Segmentation fault
exarkun@kunai:~$

I could probably dig up a few more, if you want. So what's ctypes on top of this?

Jean-Paul

>
> Neil
>--
>http://mail.python.org/mailman/listinfo/python-list
>

Carl Friedrich Bolz

unread,
Jan 7, 2006, 9:36:27 AM1/7/06
to pytho...@python.org
Jean-Paul Calderone wrote:
>
> I could probably dig up a few more, if you want. So what's ctypes on top of this?
>

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.

>>> import weakref
>>> ref = None
>>> class Target:
... def __del__(self):
... global ref
... ref = weakref.ref(self)
...
>>> def g():
... w = Target()
... w = None
... print ref()
...
>>> g()
Segmentation fault

There are dozends of segfaults in Python indeed.

Cheers,

Carl Friedrich Bolz

Paul Watson

unread,
Jan 8, 2006, 5:07:26 PM1/8/06
to

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?

"Martin v. Löwis"

unread,
Jan 8, 2006, 6:21:59 PM1/8/06
to Paul Watson
Paul Watson wrote:
> I cannot find any way to get to GetVersionInfo in VBScript (cscript).

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

Thomas Heller

unread,
Jan 9, 2006, 2:13:49 AM1/9/06
to
"=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=" <mar...@v.loewis.de> writes:

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

Thomas

Neil Hodgson

unread,
Jan 9, 2006, 7:42:52 AM1/9/06
to
Paul Watson:

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

Set objFSO = CreateObject("Scripting.FileSystemObject")
Wscript.Echo objFSO.GetFileVersion("c:\bin\SciLexer.dll")

Neil

Paul Watson

unread,
Jan 9, 2006, 12:07:44 PM1/9/06
to

Many thanks. Just what I needed. (Short of ctypes.) Thank you for
tolerating a VBScript question here.

Ziga Seilnacht

unread,
Jan 9, 2006, 12:31:23 PM1/9/06
to

Paul Watson wrote:
.
.

.
> 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?

This information is provided with sys.getwindowsversion().

Ziga Seilnacht

unread,
Jan 9, 2006, 12:40:25 PM1/9/06
to
Sorry, the previous post is wrong. I mixed the function names.

Delaney, Timothy (Tim)

unread,
Jan 9, 2006, 6:13:53 PM1/9/06
to Thomas Heller, pytho...@python.org
Thomas Heller wrote:

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

I'm sure you know the magical incantation to get that to happen ...

1. Propose it on python-dev.

2. Commit to maintain it in the python core (or alternatively, propose
to use the ElementTree maintenance method).

3. Get it accepted by Guido.

4. Do it.

If a library isn't put forward for inclusion by the *author*, it won't
be included - even if it's obviously best-of-breed. This was the
sticking point for ElementTree (until the author found out what was
going on and happily put it forward).

Tim Delaney

Thomas Heller

unread,
Jan 10, 2006, 6:07:06 AM1/10/06
to

I have done 1. I commit myself to 2. I promise to do 3., if someone
convinces Guido to accept it (or whoever has the power to accept it).

This is my response to Martin v. Löwis, who wrote:

> 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.

It has been proposed to include it in core Python, but Guido didn't
accept it. The thread starts here:

http://mail.python.org/pipermail/python-dev/2004-January/thread.html#41814

And Guido's last word was this (see the end of the message):

http://mail.python.org/pipermail/python-dev/2004-January/041856.html

Thomas

Gerald Klix

unread,
Jan 10, 2006, 7:25:23 AM1/10/06
to Thomas Heller, pytho...@python.org
I read the whol email thread carefully and could not find any sentence by
Guido, which states that he does not accept ctypes for the standard library.
He just declined to rewrite winreg. Did I miss something?

Cya,
Gerald

-----Ursprüngliche Nachricht-----
Von: python-list-bounces+gerald.klix=kli...@python.org
[mailto:python-list-bounces+gerald.klix=kli...@python.org] Im Auftrag von
Thomas Heller
Gesendet: Dienstag, 10. Januar 2006 12:07
An: pytho...@python.org
Betreff: Re: Calling foreign functions from Python? ctypes?

http://mail.python.org/pipermail/python-dev/2004-January/thread.html#41814

http://mail.python.org/pipermail/python-dev/2004-January/041856.html

Thomas
--
http://mail.python.org/mailman/listinfo/python-list

Thomas Heller

unread,
Jan 10, 2006, 11:06:18 AM1/10/06
to
"Gerald Klix" <Geral...@klix.ch> writes:

> I read the whol email thread carefully and could not find any sentence by
> Guido, which states that he does not accept ctypes for the standard library.
> He just declined to rewrite winreg. Did I miss something?

Maybe I misinterpreted what he wrote myself. Ok, I'll propose on
python-dev to include ctypes into core python.

Thomas

"Martin v. Löwis"

unread,
Jan 10, 2006, 4:00:28 PM1/10/06
to Thomas Heller
Thomas Heller wrote:
> It has been proposed to include it in core Python, but Guido didn't
> accept it. The thread starts here:
>
> http://mail.python.org/pipermail/python-dev/2004-January/thread.html#41814
>
> And Guido's last word was this (see the end of the message):
>
> http://mail.python.org/pipermail/python-dev/2004-January/041856.html

He didn't accept it - he didn't reject it, either.

Notice that this thread did *not* propose to include ctypes: instead,
it suggested to include ctypes *in order to implement _winreg*.

This is something completely different, and this is what Guido
rejected: Implementing _winreg with ctypes is indeed a bad idea,
IMO. Whether or not ctypes should be included is a different story.

Regards,
Martin

Thomas Heller

unread,
Jan 10, 2006, 4:03:47 PM1/10/06
to
"Martin v. Löwis" <mar...@v.loewis.de> writes:

It was a misunderstanding then - I simply wanted to demonstrate how some
of the _winreg functions could be implemented with ctypes.

Anyway, I've just posted a request to include ctypes to python-dev.
We'll see.

Thomas

sk...@pobox.com

unread,
Jan 10, 2006, 4:34:50 PM1/10/06
to Thomas Heller, pytho...@python.org
Thomas,

Ctypes doesn't work to call arbitrary C++ code, is that correct?

Skip

Thomas Heller

unread,
Jan 11, 2006, 2:06:36 AM1/11/06
to sk...@pobox.com, pytho...@python.org
sk...@pobox.com writes:

Yes, that's correct. It doesn't do C++ name-(un)mangling, nor has it
the C++ calling conventions. And so on...

Thomas


0 new messages