views:

707

answers:

4
+2  Q: 

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.

+3  A: 

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

Brian R. Bondy
Since I normally break things quite a bit, this is a good overview on ctypes that I did not know about.
phreaki
+2  A: 

ctypes can indeed cause crashes, if the C library you're using can already cause crashes.

If anything, ctypes can help reduce crashes, because you can enforce runtime type safety with the argtypes property on C functions using ctypes.

But if your C library is already stable and tested, there is absolutely no reason not to use ctypes if it performs what you need in terms of bringing C and Python together.

Mark Rushakoff
Of course what I could use it for is not my C code. This might give me a reason to learn more than I know.
phreaki
+8  A: 

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

Alex Martelli
+1  A: 

I highly suggest you look into reading this book:

Gray Hat Python: Python Programming for Hackers and Reverse Engineers

Kenneth Reitz
Please explain why. Not that I doubt you (and I own this book), but it would be helpful to know your reasons.
Ryan Ginstrom
I as well. I'm not so much into hacking or reversing anything here. I wanted to access parts of my machine not easily seen by Python.
phreaki
If you learn the same sorts of things that hackers and reverse engineers need to know for their purposes then you will also learn how those techniques can be applied to "access parts of your machine not easily seen by Python"
Jim Dennis
the book functions as an in-depth tutorial for the ctypes library, and shows you how to run incredibly low-level code.
Kenneth Reitz