我怎么能从壳内告诉壳处于什么模式?
我已经试着查看了平台模块,但它似乎只告诉您“用于可执行文件的位架构和链接格式”。我的二进制文件被编译为64位(我在OS X 10.6上运行),所以它似乎总是报告64位,即使我使用这里描述的方法强制32位模式。
我怎么能从壳内告诉壳处于什么模式?
我已经试着查看了平台模块,但它似乎只告诉您“用于可执行文件的位架构和链接格式”。我的二进制文件被编译为64位(我在OS X 10.6上运行),所以它似乎总是报告64位,即使我使用这里描述的方法强制32位模式。
当前回答
尝试使用ctypes来获取void指针的大小:
import ctypes
print ctypes.sizeof(ctypes.c_voidp)
32位是4位,64位是8位。
其他回答
根据abe32的回答,
import sys
n_bits = 32 << bool(sys.maxsize >> 32)
N_bits将有32或64位。
对于32位,它将返回32,对于64位,它将返回64
import struct
print(struct.calcsize("P") * 8)
C:\Users\xyz>python
Python 2.7.6 (default, Nov XY ..., 19:24:24) **[MSC v.1500 64 bit (AMD64)] on win
32**
Type "help", "copyright", "credits" or "license" for more information.
>>>
在CMD中点击python后
尝试使用ctypes来获取void指针的大小:
import ctypes
print ctypes.sizeof(ctypes.c_voidp)
32位是4位,64位是8位。
平台架构不是可靠的方法。 相反,我们:
$ arch -i386 /usr/local/bin/python2.7
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform, sys
>>> platform.architecture(), sys.maxsize
(('64bit', ''), 2147483647)
>>> ^D
$ arch -x86_64 /usr/local/bin/python2.7
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform, sys
>>> platform.architecture(), sys.maxsize
(('64bit', ''), 9223372036854775807)