[Python-Dev] Recommended way to tell platform

Tim Peters tim.peters at gmail.com
Sat Aug 7 23:31:31 CEST 2004


>> [Tim] almost always use
>>
>>      if sys.platform in ('win32',):

[Guido]
> Why the 'in singleton tuple'?

Just because it often turns out that someone else needs to add another
platform later.  Like

        if sys.platform in ('win32', 'os2emx', 'mac'):
            # There's no distinction among 'user', 'group' and 'world';
            # replicate the 'user' bits.
            user = expected >> 6
            expected = user * (1 + 8 + 64)

> Still holding out for that compiler optimization that promises to make tuples of
> constants code constants themselves?

I take it you haven't checked compile.c lately <wink>.

>> I dislike using os.name == 'nt', mostly because if we asked all
>> Windows Python users which OS they use, only a tiny percentage would
>> answer "NT".  Since NT is a dying OS, 'nt' is an increasingly
>> odd-looking thing to test agaist.

> And so is 'posix' when you're looking at Linux. :-)
> 
> We can't change these values easily so they will look odder and odder
> as time progresses.

Which is why I prefer using something with 'win32' in its name now. 
That's equally good for Windows 95, 98, 98SE, ME, NT, 2000, XP.  Never
mind that it also says 'win32' on Win64, since nobody uses the latter
yet.

>> OTOH, I have no idea when or why sys.platform == "win32" would give
>> a different result than os.name == "nt".
 
> Maybe on cygwin?  I really don't know.

Nope.  sys.platform is 'cygwin' there, and os.name is 'posix'.  So
it's impossible to mistake Cygwin for a real Windows app <wink>.


More information about the Python-Dev mailing list