Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ctypes pro and con

I have heard that Ctypes can cause crashes (or stop errors) in Python and windows. Should I stay away from their use? Where did I hear? It was back when I tried to control various aspects of windows, automation, that sort of thing.

I hear of swig, but I see Ctypes more often than not. Any danger here? If so, what should I watch out for?

I did search for ctype pro con python.

like image 729
phreaki Avatar asked Aug 23 '09 14:08

phreaki


2 Answers

In terms of robustness, I still think swig is somewhat superior to ctypes, because it's possible to have a C compiler check things more thoroughly for you; however, this is pretty moot by now (while it loomed larger in earlier ctypes versons), thanks to the argtypes feature @Mark already mentioned. However, there is no doubt that the runtime overhead IS much more significant for ctypes than for swig (and sip and boost python and other "wrapping" approaches): so, I think of ctypes as a convenient way to reach for a few functions within a DLL when the calls happen outside of a key bottleneck, not as a way to make large C libraries available to Python in performance-critical situations.

For a nice middle way between the runtime performance of swig (&c) and the convenience of ctypes, with the added bonus of being able to add more code that can use a subset of Python syntax yet run at just about C-code speeds, also consider Cython -- a python-like language that compiles down to C and is specialized for writing Python-callable extensions and wrapping C libraries (including ones that may be available only as static libraries, not DLLs: ctypes wouldn't let you play with those;-).

like image 137
Alex Martelli Avatar answered Oct 19 '22 03:10

Alex Martelli


ctypes is a safe module to use, if you use it right.

Some libraries provide a lower level access to things, some modules simply allow you to shoot yourself in the foot. So naturally some modules are more dangerous than others. This doesn't mean you should not use them though!

You probably heard someone referring to something like this:

#Crash python interpreter
from ctypes import *
def crashme():
    c = c_char('x')
    p = pointer(c)
    i = 0
    while True:
            p[i] = 'x'
            i += 1

The python interpreter crashing is different than just the python code itself erroring out with a runtime error. For example infinite recursion with a default recursion limit set would cause a runtime error but the python interpreter would still be alive afterwards.

Another good example of this is with the sys module. You wouldn't stop using the sys module though because it can crash the python interpreter.

import sys
sys.setrecursionlimit(2**30)
def f(x):
  f(x+1)

#This will cause no more resources left and then crash the python interpreter
f(1)

There are many libraries as well that provide lower level access. For example the The gc module can be manipulated to give access to partially constructed object, accessing fields of which can cause crashes.

Reference and ideas taken from: Crashing Python

like image 33
Brian R. Bondy Avatar answered Oct 19 '22 04:10

Brian R. Bondy