eliteparakeet
eliteparakeet

Reputation: 879

Why does Python ctype c_char_p return different values on OSX vs. Windows?

I'm learning more about Pythons ctypes module, and have noticed an oddity that I am not sure how to explain.

On Windows (Python 2.7.4), the following is returned:

>>> from ctypes import *
>>> c_char_p("Hello World!")
c_char_p('Hello World!')

Whereas on OSX (Python 2.7.2), the following is returned:

>>> from ctypes import *
>>> c_char_p("Hello World!")
c_char_p(4479401388)

Additionally on OSX, c_char_p("Hello World!") returns a different numerical value every time it is run.

Could someone please shed some light on why this may be occuring?

Upvotes: 0

Views: 164

Answers (1)

icktoofay
icktoofay

Reputation: 129011

Looking at the source, __repr__ will only show the contents of the string if

  1. It is running on Windows; and
  2. IsBadStringPtr returns TRUE.

They probably want to show a string representation if possible, since that's a rather useful thing to show, but not crash your program if it points somewhere unexpected. IsBadStringPtr only exists on Windows, necessitating the first check. Frankly, I'm surprised they'd use it, as it's clearly marked obsolete, and for good reasons.

Upvotes: 2

Related Questions