I noticed that passing Python objects to native code with ctypes can break mutability expectations.
For example, if I have a C function like:
int print_and_mutate(char *str)
{
str[0] = 'X';
return printf("%s\n", str);
}
and I call it like this:
from ctypes import *
lib = cdll.LoadLibrary("foo.so")
s = b"asdf"
lib.print_and_mutate(s)
The value of s changed, and is now b"Xsdf".
The Python docs say "You should be careful, however, not to pass them to functions expecting pointers to mutable memory.".
Is this only because it breaks expectations of which types are immutable, or can something else break as a result? In other words, if I go in with the clear understanding that my original bytes object will change, even though normally bytes are immutable, is that OK or will I get some kind of nasty surprise later if I don't use create_string_buffer like I'm supposed to?