Calling foreign functions from Python? ctypes?

Jean-Paul Calderone exarkun at divmod.com
Fri Jan 6 21:23:25 EST 2006


On Sat, 07 Jan 2006 00:55:45 GMT, Neil Hodgson <nyamatongwe+thunder at gmail.com> wrote:
>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.

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

  exarkun at kunai:~$ python < .
  Segmentation fault
  exarkun at 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 at 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 at 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 at 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
>



More information about the Python-list mailing list