Skip to content Skip to sidebar Skip to footer

Evil Ctypes Hack In Python

I'd like to start by saying that this question is asked purely out of interest, and I by no means intend to use something so incredibly evil in any serious project. (yes, it's that

Solution 1:

8 would be "correct" on a 32-bit platform where ob_refcnt and ob_type are 4 bytes each; on a 64-bit platform this will be different. Essentially you're trying to go past PyObject_HEAD to the rest of the integer object, so try checking the size of PyObject in a compiler or debugger.

Obviously this will be different on Python 3, where there is only the long type so even small integers are variable-length; in that case you'll want PyObject_VAR_HEAD (and PyVarObject) instead of PyObject_HEAD.

A good place to start looking at this is the documentation inside object.h, also readable in the C API reference manual at https://docs.python.org/2/c-api/structures.html, and then at intobject.h, or longintrepr.h for Python 3.


Note: changing the value of 1 will still segfault, but for different reasons. Changing the value of a larger small integer such as 10 should be safe, though.

Post a Comment for "Evil Ctypes Hack In Python"