views:

581

answers:

4

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

+9  A: 

One way is to look at maxint:

$ /usr/bin/python2.6
Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; sys.maxint
9223372036854775807
>>> ^D
$ arch -i386 /usr/bin/python2.6
Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; sys.maxint
2147483647

[EDIT] BTW, if you run into a 64-bit universal build of Python3 on OS X, this trick won't work because sys.maxint has been removed in Python3. However, for Python3 you can use sys.maxsize instead with the same results. Don't use sys.maxsize for Python2.6, though, as it appears not to be dynamically determined. Ugh.

Ned Deily
+1, sounds like the best approach to me.
Alex Martelli
Ha! Why the heck was I banging my head on this one. Great answer! :)
mkelley33
+2  A: 

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

import ctypes
print ctypes.sizeof(ctypes.c_voidp)

It'll be 4 or 8 for 32 or 64 bit respectively.

Matthew Marshall
That works, too, although it does have the possible slight disadvantage of an unnecessary import and module load if you don't otherwise need ctypes: the sys module, otoh, is compiled into the interpreter.
Ned Deily
+2  A: 

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

import struct
print struct.calcsize("P") * 8
ChristopheD
Imho, better than ctypes version - works even with older Python.
yk4ever
+1  A: 

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

Peter Hosey
A very nice alternative answer for those of us using Mac OS 10.x.xThank you!
mkelley33