问题:如何确定我的python shell是在OS X上以32位还是64位模式执行?

我需要一种方法,从外壳程序中告诉外壳程序处于哪种模式。

我尝试查看平台模块,但似乎只告诉您“有关可执行文件所用的位体系结构和链接格式”的信息:尽管二进制文件编译为64位(我在OS X 10.6上运行),所以即使我使用此处介绍的方法强制使用32位模式,它似乎总是报告64 位。

I need a way to tell what mode the shell is in from within the shell.

I’ve tried looking at the platform module but it seems only to tell you about “about the bit architecture and the linkage format used for the executable”: the binary is compiled as 64bit though (I’m running on OS X 10.6) so it seems to always report 64bit even though I’m using the methods described here to force 32bit mode).


回答 0

一种方法是看sys.maxsize作为记录在这里

$ python-32 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffff', False)
$ python-64 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffffffffffff', True)

sys.maxsize是Python 2.6中引入的。如果您需要针对较旧系统的测试,则此稍微复杂一些的测试应适用于所有Python 2和3版本:

$ python-32 -c 'import struct;print( 8 * struct.calcsize("P"))'
32
$ python-64 -c 'import struct;print( 8 * struct.calcsize("P"))'
64

顺便说一句,您可能会想使用platform.architecture()它。不幸的是,其结果并不总是可靠的,特别是在OS X通用二进制文件的情况下

$ arch -x86_64 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit True
$ arch -i386 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit False

One way is to look at sys.maxsize as documented here:

$ python-32 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffff', False)
$ python-64 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffffffffffff', True)

sys.maxsize was introduced in Python 2.6. If you need a test for older systems, this slightly more complicated test should work on all Python 2 and 3 releases:

$ python-32 -c 'import struct;print( 8 * struct.calcsize("P"))'
32
$ python-64 -c 'import struct;print( 8 * struct.calcsize("P"))'
64

BTW, you might be tempted to use platform.architecture() for this. Unfortunately, its results are not always reliable, particularly in the case of OS X universal binaries.

$ arch -x86_64 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit True
$ arch -i386 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit False

回答 1

在终端/命令行中启动Python解释器时,您可能还会看到类似以下的行:

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32

其中[MSC v.1500 64 bit (AMD64)]表示64位Python。适用于我的特定设置。

When starting the Python interpreter in the terminal/command line you may also see a line like:

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32

Where [MSC v.1500 64 bit (AMD64)] means 64-bit Python. Works for my particular setup.


回答 2

基本上是马修·马歇尔(Matthew Marshall)回答的变体(带有来自标准库的结构):

import struct
print struct.calcsize("P") * 8

Basically a variant on Matthew Marshall’s answer (with struct from the std.library):

import struct
print struct.calcsize("P") * 8

回答 3

尝试使用ctypes获得void指针的大小:

import ctypes
print ctypes.sizeof(ctypes.c_voidp)

对于32位将是4,对于64位将是8。

Try using ctypes to get the size of a void pointer:

import ctypes
print ctypes.sizeof(ctypes.c_voidp)

It’ll be 4 for 32 bit or 8 for 64 bit.


回答 4

打开python控制台:

import platform
platform.architecture()[0]

它应根据您的平台显示“ 64bit”或“ 32bit”。

或者对于OS X二进制文件):

import sys
sys.maxsize > 2**32 
# it should display True in case of 64bit and False in case of 32bit

Open python console:

import platform
platform.architecture()[0]

it should display the ’64bit’ or ’32bit’ according to your platform.

Alternatively( in case of OS X binaries ):

import sys
sys.maxsize > 2**32 
# it should display True in case of 64bit and False in case of 32bit

回答 5

对于非编程解决方案,请查看活动监视器。它将64位进程的体系结构列为“ Intel(64位)”。

For a non-programmatic solution, look in the Activity Monitor. It lists the architecture of 64-bit processes as “Intel (64-bit)”.


回答 6

在我的Centos Linux系统上,我做了以下工作:

1)启动Python解释器(我使用的是2.6.6)
2)运行以下代码:

import platform
print(platform.architecture())

它给了我

(64bit, 'ELF')

On my Centos Linux system I did the following:

1) Started the Python interpreter (I’m using 2.6.6)
2) Ran the following code:

import platform
print(platform.architecture())

and it gave me

(64bit, 'ELF')

回答 7

platform.architecture() 笔记说:

注意:在Mac OS X(可能还有其他平台)上,可执行文件可能是包含多种体系结构的通用文件。

为了获得当前解释器的“ 64位”,查询sys.maxsize属性更加可靠:

import sys
is_64bits = sys.maxsize > 2**32

platform.architecture() notes say:

Note: On Mac OS X (and perhaps other platforms), executable files may be universal files containing multiple architectures.

To get at the “64-bitness” of the current interpreter, it is more reliable to query the sys.maxsize attribute:

import sys
is_64bits = sys.maxsize > 2**32

回答 8

分组一切…

考虑到:

  • 询问OSX的问题(我有一个旧的(且已破解的)VM,带有一个古老的Python版本)
  • 我的主要环境是Win
  • 我在Win上仅安装了32位版本(并且在Lnx上构建了一个“残缺的” 版本

我将使用Python 3Python 2在所有3个平台上进行示例。

  1. 检查[Python 3.Docs]:sys。maxsize值-与0x1000000002 ** 32)比较:对于64位,较大,对于32 ,较小:
    • OSX 9 x64
      • Python 2.7.10 x64
        >>> import sys
        >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
        'Python 2.7.10 (default, Oct 14 2015, 05:51:29) \n[GCC 4.8.2] on darwin'
        >>> hex(sys.maxsize), sys.maxsize > 0x100000000
        ('0x7fffffffffffffff', True)
    • Ubuntu 16 x64
      • Python 3.5.2 x64
        >>> import sys
        >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
        'Python 3.5.2 (default, Nov 23 2017, 16:37:01) \n[GCC 5.4.0 20160609] on linux'
        >>> hex(sys.maxsize), sys.maxsize > 0x100000000
        ('0x7fffffffffffffff', True)
      • Python 3.6.4 x86
        >>> import sys
        >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
        'Python 3.6.4 (default, Apr 25 2018, 23:55:56) \n[GCC 5.4.0 20160609] on linux'
        >>> hex(sys.maxsize), sys.maxsize > 0x100000000
        ('0x7fffffff', False)
    • 赢10 x64
      • Python 3.5.4 x64
        >>> import sys
        >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
        'Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32'
        >>> hex(sys.maxsize), sys.maxsize > 0x100000000
        ('0x7fffffffffffffff', True)
      • Python 3.6.2 x86
        >>> import sys
        >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
        'Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32'
        >>> hex(sys.maxsize), sys.maxsize > 0x100000000
        ('0x7fffffff', False)


  1. 使用[Python 3.Docs]:结构。calcsizeformat确定由(指针)格式产生的对象大小。换句话说,确定指针大小(sizeof(void*)):
    • OSX 9 x64
      • Python 2.7.10 x64
        >>> import struct
        >>> struct.calcsize("P") * 8
        64
    • Ubuntu 16 x64
      • Python 3.5.2 x64
        >>> import struct
        >>> struct.calcsize("P") * 8
        64
      • Python 3.6.4 x86
        >>> import struct
        >>> struct.calcsize("P") * 8
        32
    • 赢10 x64
      • Python 3.5.4 x64
        >>> import struct
        >>> struct.calcsize("P") * 8
        64
      • Python 3.6.2 x86
        >>> import struct
        >>> struct.calcsize("P") * 8
        32


  1. 使用[Python 3.Docs]:ctypes-Python的外部函数库。它还归结为确定指针(sizeof(void*))的大小。注意,ctypes使用#2。(不一定要完成此任务),通过“ $ {PYTHON_SRC_DIR} / Lib / ctypes / __ init __。py”(在第15附近):
    • OSX 9 x64
      • Python 2.7.10 x64
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        64
    • Ubuntu 16 x64
      • Python 3.5.2 x64
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        64
      • Python 3.6.4 x86
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        32
    • 赢10 x64
      • Python 3.5.4 x64
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        64
      • Python 3.6.2 x86
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        32


  1. [Python 3.Docs]:平台。架构可执行文件= sys.executable,位=”,链接=” !!! 在OSX上不可靠!由于采用多体系结构可执行文件(或.dylib)格式(在某些情况下,使用#2。):
    • OSX 9 x64
      • Python 2.7.10 x64
        >>> import platform
        >>> platform.architecture()
        ('64bit', '')
    • Ubuntu 16 x64
      • Python 3.5.2 x64
        >>> import platform
        >>> platform.architecture()
        ('64bit', 'ELF')
      • Python 3.6.4 x86
        >>> import platform
        >>> platform.architecture()
        ('32bit', 'ELF')
    • 赢10 x64
      • Python 3.5.4 x64
        >>> import platform
        >>> platform.architecture()
        ('64bit', 'WindowsPE')
      • Python 3.6.2 x86
        >>> import platform
        >>> platform.architecture()
        ('32bit', 'WindowsPE')


  1. me脚的解决方法(gainarie)- 通过[Python 3.Docs]:os调用外部命令([man7]:FILE(1)系统命令#4的局限性适用(有时可能甚至无法使用):
    • OSX 9 x64
      • Python 2.7.10 x64
        >>> import os
        >>> os.system("file {0:s}".format(os.path.realpath(sys.executable)))
        /opt/OPSWbuildtools/2.0.6/bin/python2.7.global: Mach-O 64-bit executable x86_64
    • Ubuntu 16 x64
      • Python 3.5.2 x64
        >>> import os
        >>> os.system("file {0:s}".format(os.path.realpath(sys.executable)))
        /usr/bin/python3.5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=59a8ef36ca241df24686952480966d7bc0d7c6ea, stripped
      • Python 3.6.4 x86
        >>> import os
        >>> os.system("file {0:s}".format(os.path.realpath(sys.executable)))
        /home/cfati/Work/Dev/Python-3.6.4/python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=5c3d4eeadbd13cd91445d08f90722767b0747de2, not stripped
    • 赢10 x64
      • 文件工具不存在,还有其他第三方工具可以使用,但我不会坚持使用它们


具体

  1. 通过[Python 3.Docs]检查环境变量(例如%PROCESSOR_ARCHITECTURE%(或其他)):操作系统。环境
    • 赢10 x64
      • Python 3.5.4 x64
        >>> import os
        >>> os.environ["PROCESSOR_ARCHITECTURE"]
        'AMD64'
      • Python 3.6.2 x86
        >>> import os
        >>> os.environ["PROCESSOR_ARCHITECTURE"]
        'x86'


  1. [Python 3.Docs]:sys。版本(也显示在1条第一线启动解释时)
    • 检查#1。

Grouping everything…

Considering that:

  • The question is asked for OSX (I have an old (and cracked) VM with an ancient Python version)
  • My main env is Win
  • I only have the 32bit version installed on Win (and I built a “crippled” one on Lnx)

I’m going to exemplify on all 3 platforms, using Python 3 and Python 2.

  1. Check [Python 3.Docs]: sys.maxsize value – compare it to 0x100000000 (2 ** 32): greater for 64bit, smaller for 32bit:
    • OSX 9 x64:
      • Python 2.7.10 x64:
        >>> import sys
        >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
        'Python 2.7.10 (default, Oct 14 2015, 05:51:29) \n[GCC 4.8.2] on darwin'
        >>> hex(sys.maxsize), sys.maxsize > 0x100000000
        ('0x7fffffffffffffff', True)
        
    • Ubuntu 16 x64:
      • Python 3.5.2 x64:
        >>> import sys
        >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
        'Python 3.5.2 (default, Nov 23 2017, 16:37:01) \n[GCC 5.4.0 20160609] on linux'
        >>> hex(sys.maxsize), sys.maxsize > 0x100000000
        ('0x7fffffffffffffff', True)
        
      • Python 3.6.4 x86:
        >>> import sys
        >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
        'Python 3.6.4 (default, Apr 25 2018, 23:55:56) \n[GCC 5.4.0 20160609] on linux'
        >>> hex(sys.maxsize), sys.maxsize > 0x100000000
        ('0x7fffffff', False)
        
    • Win 10 x64:
      • Python 3.5.4 x64:
        >>> import sys
        >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
        'Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32'
        >>> hex(sys.maxsize), sys.maxsize > 0x100000000
        ('0x7fffffffffffffff', True)
        
      • Python 3.6.2 x86:
        >>> import sys
        >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform)
        'Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32'
        >>> hex(sys.maxsize), sys.maxsize > 0x100000000
        ('0x7fffffff', False)
        


  1. Use [Python 3.Docs]: struct.calcsize(format) to determine the object size produced by the (pointer) format. In other words, determines the pointer size (sizeof(void*)):
    • OSX 9 x64:
      • Python 2.7.10 x64:
        >>> import struct
        >>> struct.calcsize("P") * 8
        64
        
    • Ubuntu 16 x64:
      • Python 3.5.2 x64:
        >>> import struct
        >>> struct.calcsize("P") * 8
        64
        
      • Python 3.6.4 x86:
        >>> import struct
        >>> struct.calcsize("P") * 8
        32
        
    • Win 10 x64:
      • Python 3.5.4 x64:
        >>> import struct
        >>> struct.calcsize("P") * 8
        64
        
      • Python 3.6.2 x86:
        >>> import struct
        >>> struct.calcsize("P") * 8
        32
        


  1. Use [Python 3.Docs]: ctypes – A foreign function library for Python. It also boils down to determining the size of a pointer (sizeof(void*)). As a note, ctypes uses #2. (not necessarily for this task) via “${PYTHON_SRC_DIR}/Lib/ctypes/__init__.py” (around line #15):
    • OSX 9 x64:
      • Python 2.7.10 x64:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        64
        
    • Ubuntu 16 x64:
      • Python 3.5.2 x64:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        64
        
      • Python 3.6.4 x86:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        32
        
    • Win 10 x64:
      • Python 3.5.4 x64:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        64
        
      • Python 3.6.2 x86:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        32
        


  1. [Python 3.Docs]: platform.architecture(executable=sys.executable, bits=”, linkage=”) !!! NOT reliable on OSX !!! due to multi arch executable (or .dylib) format (in some cases, uses #2.):
    • OSX 9 x64:
      • Python 2.7.10 x64:
        >>> import platform
        >>> platform.architecture()
        ('64bit', '')
        
    • Ubuntu 16 x64:
      • Python 3.5.2 x64:
        >>> import platform
        >>> platform.architecture()
        ('64bit', 'ELF')
        
      • Python 3.6.4 x86:
        >>> import platform
        >>> platform.architecture()
        ('32bit', 'ELF')
        
    • Win 10 x64:
      • Python 3.5.4 x64:
        >>> import platform
        >>> platform.architecture()
        ('64bit', 'WindowsPE')
        
      • Python 3.6.2 x86:
        >>> import platform
        >>> platform.architecture()
        ('32bit', 'WindowsPE')
        


  1. Lame workaround (gainarie) – invoke an external command ([man7]: FILE(1)) via [Python 3.Docs]: os.system(command). The limitations of #4. apply (sometimes it might not even work):
    • OSX 9 x64:
      • Python 2.7.10 x64:
        >>> import os
        >>> os.system("file {0:s}".format(os.path.realpath(sys.executable)))
        /opt/OPSWbuildtools/2.0.6/bin/python2.7.global: Mach-O 64-bit executable x86_64
        
    • Ubuntu 16 x64:
      • Python 3.5.2 x64:
        >>> import os
        >>> os.system("file {0:s}".format(os.path.realpath(sys.executable)))
        /usr/bin/python3.5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=59a8ef36ca241df24686952480966d7bc0d7c6ea, stripped
        
      • Python 3.6.4 x86:
        >>> import os
        >>> os.system("file {0:s}".format(os.path.realpath(sys.executable)))
        /home/cfati/Work/Dev/Python-3.6.4/python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=5c3d4eeadbd13cd91445d08f90722767b0747de2, not stripped
        
    • Win 10 x64:
      • file utility is not present, there are other 3rd Party tools that can be used, but I’m not going to insist on them


Win specific:

  1. Check env vars (e.g. %PROCESSOR_ARCHITECTURE% (or others)) via [Python 3.Docs]: os.environ:
    • Win 10 x64:
      • Python 3.5.4 x64:
        >>> import os
        >>> os.environ["PROCESSOR_ARCHITECTURE"]
        'AMD64'
        
      • Python 3.6.2 x86:
        >>> import os
        >>> os.environ["PROCESSOR_ARCHITECTURE"]
        'x86'
        


  1. [Python 3.Docs]: sys.version (also displayed in the 1st line when starting the interpreter)
    • Check #1.

回答 9

struct.calcsize("P")返回存储单个指针所需的字节大小。在32位系统上,它将返回4个字节。在64位系统上,它将返回8个字节。

因此,32如果您运行的是32位python并且64运行的是64 位python,则会返回以下内容:

Python 2

import struct;print struct.calcsize("P") * 8

Python 3

import struct;print(struct.calcsize("P") * 8)

struct.calcsize("P") returns size of the bytes required to store a single pointer. On a 32-bit system, it would return 4 bytes. On a 64-bit system, it would return 8 bytes.

So the following would return 32 if you’re running 32-bit python and 64 if you’re running 64-bit python:

Python 2

import struct;print struct.calcsize("P") * 8

Python 3

import struct;print(struct.calcsize("P") * 8)

回答 10

python -VV在命令行中执行a 。它应该返回版本。

Do a python -VV in the command line. It should return the version.


回答 11

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后

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.
>>>

after hitting python in cmd


回答 12

import sys
print(sys.version)

3.5.1(v3.5.1:37a07cee5969,2015年12月6日,01:54:25)[MSC v.1900 64位(AMD64) ]

import sys
print(sys.version)

3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)]


回答 13

根据abe32的回答,

import sys
n_bits = 32 << bool(sys.maxsize >> 32)

n_bits将具有32或64位。

Based On abe32’s answer,

import sys
n_bits = 32 << bool(sys.maxsize >> 32)

n_bits will have 32 or 64 bits.


回答 14

对于32位,它将返回32,对于64位,它将返回64

import struct
print(struct.calcsize("P") * 8)

For 32 bit it will return 32 and for 64 bit it will return 64

import struct
print(struct.calcsize("P") * 8)

回答 15

平台架构不是可靠的方法。相反,我们:

$ 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)

Platform Architecture is not the reliable way. Instead us:

$ 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)

回答 16

platform.architecture() 是有问题的(而且昂贵)。

sys.maxsize > 2**32从Py2.6开始方便地进行测试。

这是对实际(默认)指针大小的可靠测试,至少从Py2.3:开始兼容struct.calcsize('P') == 8。也:ctypes.sizeof(ctypes.c_void_p) == 8

注意:可以使用gcc选项构建-mx32,这是64位体系结构的应用程序,但是默认使用32位指针(节省内存和速度)。’sys.maxsize = ssize_t’可能不严格表示C指针大小(2**31 - 1无论如何通常如此)。而且,有些系统的代码和数据的指针大小不同,因此需要澄清辨别“ 32位或64位模式”的目的是什么?

platform.architecture() is problematic (and expensive).

Conveniently test for sys.maxsize > 2**32 since Py2.6 .

This is a reliable test for the actual (default) pointer size and compatible at least since Py2.3: struct.calcsize('P') == 8. Also: ctypes.sizeof(ctypes.c_void_p) == 8.

Notes: There can be builds with gcc option -mx32 or so, which are 64bit architecture applications, but use 32bit pointers as default (saving memory and speed). ‘sys.maxsize = ssize_t’ may not strictly represent the C pointer size (its usually 2**31 - 1 anyway). And there were/are systems which have different pointer sizes for code and data and it needs to be clarified what exactly is the purpose of discerning “32bit or 64bit mode?”


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。