I need a way to tell what mode the shell is in from within the shell.
While I\'m primarily an OS X user, I\'d be interested in knowing about other platforms as well.<
Grouping everything...
Considering that:
I'm going to exemplify on all 3 platforms, using Python 3 and Python 2.
0x100000000
(2 ** 32
): greater for 64bit, smaller for 32bit:
>>> 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)
>>> 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)
>>> 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)
>>> 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)
>>> 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)
sizeof(void*)
):
>>> import struct >>> struct.calcsize("P") * 8 64
>>> import struct >>> struct.calcsize("P") * 8 64
>>> import struct >>> struct.calcsize("P") * 8 32
>>> import struct >>> struct.calcsize("P") * 8 64
>>> import struct >>> struct.calcsize("P") * 8 32
sizeof(void*)
). As a note, ctypes uses #2. (not necessarily for this task) via "${PYTHON_SRC_DIR}/Lib/ctypes/__init__.py" (around line #15):
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 32
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 32
>>> import platform >>> platform.architecture() ('64bit', '')
>>> import platform >>> platform.architecture() ('64bit', 'ELF')
>>> import platform >>> platform.architecture() ('32bit', 'ELF')
>>> import platform >>> platform.architecture() ('64bit', 'WindowsPE')
>>> import platform >>> platform.architecture() ('32bit', 'WindowsPE')
>>> 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
>>> 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
>>> 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
>>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'AMD64'
>>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'x86'
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.
import sys
print(sys.version)
3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)]
Based On abe32's answer,
import sys
n_bits = 32 << bool(sys.maxsize >> 32)
n_bits will have 32 or 64 bits.
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?"
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.