简体   繁体   English

为什么Python ctype c_char_p在OSX和Windows上返回不同的值?

[英]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. 我正在学习更多有关Pythons ctypes模块的知识,并注意到一个奇怪的是我不确定如何解释。

On Windows (Python 2.7.4), the following is returned: 在Windows(Python 2.7.4)上,返回以下内容:

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

Whereas on OSX (Python 2.7.2), the following is returned: 而在OSX(Python 2.7.2)上,返回以下内容:

>>> 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. 此外,在OSX上, c_char_p("Hello World!")每次运行时都会返回不同的数值。

Could someone please shed some light on why this may be occuring? 有人可以说明为什么会发生这种情况吗?

Thanks in advanced. 提前致谢。

Looking at the source , __repr__ will only show the contents of the string if 查看源代码__repr__将仅显示字符串if的内容

  1. It is running on Windows; 它在Windows上运行; and
  2. IsBadStringPtr returns TRUE . IsBadStringPtr返回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. IsBadStringPtr仅存在于Windows上,需要进行第一次检查。 Frankly, I'm surprised they'd use it, as it's clearly marked obsolete, and for good reasons . 坦率地说,我很惊讶他们会使用它,因为它显然已经过时了,而且有充分的理由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM