I am having a funny issue with ctypes; while it seems to work in regular python scripts, when I use it in the interpreter with printf() it prints the length of the string after the string itself. A demo:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> libc = CDLL("libc.so.6")
>>> libc.printf("Test")
Test4
>>> int = 55
>>> libc.printf("Test %d", int)
Test 557
>>> int = c_int(55)
>>> libc.printf("Test %d", int)
Test 557
Does anyone know why this happens?
From the printf(3) man page:
The python interpreter is displaying the return code of
printf()after you call it. Since you don’t have a newline\nat the end of your strings the length is getting printed immediately after the printout. Note that this won’t happen in a script, only when you use python interactively.You could hide this with an assignment: