[Image-SIG] Detection of Mac OS X (darwin) in Image.py.

Graham Dumpleton grahamd at dscpl.com.au
Thu Sep 16 02:38:57 CEST 2004


In PIL/Image.py there are two checks for Mac OS X of the form:

  elif os.environ.get("OSTYPE") == "darwin":

This will not always work. Specifically, it will only work if you are
running tcsh.

If you are running bash, OSTYPE is set to "darwin7.0" (for 10.3.5)
and further, the OSTYPE environment variable is not exported by
the shell anyway.

The code that would be better used would be:

  elif sys.platform == "darwin":

which is always correct.

A patch to this effect has been getting made in the Fink version of
PIL on Mac OS X for over a year. See the email discussion below I had
about this back in April of last year with the Fink package maintainer. I
thought I had also posted to this mailing list this information back
then. Either it got overlooked, or I was dreaming about this bit.

Graham

\From: 	  Jeffrey.S.Whitaker at noaa.gov
Subject: 	Re: pil-1.1.4-3
Date: 	23 April 2003 9:22:36 PM
To: 	  grahamd at dscpl.com.au
Reply-To: 	  Jeffrey.S.Whitaker at noaa.gov

Graham:  Ahh, you are right.  I'll add a patch to the next revision of pil
as you suggested originally.

-Jeff

On Tue, 22 Apr 2003 grahamd at dscpl.com.au wrote:

You didn't pay head to my warning near the end of the mail. If you run bash from
inside of tcsh it simply inherits the OSTYPE value from tcsh. Run:

  unsetenv OSTYPE

before you run bash and then you will see what bash is truly setting it to if you have
bash as the login shell in the netinfo database.

[asdopey:~] grahamd% echo $OSTYPE
darwin
[asdopey:~] grahamd% python
Python 2.2.2 (#1, Jan 21 2003, 21:27:56)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import os
os.environ["OSTYPE"]
'darwin'
^D
[asdopey:~] grahamd% unsetenv OSTYPE
[asdopey:~] grahamd% exec bash
~ [501]$ echo $OSTYPE
darwin6.0
~ [502]$ python
Python 2.2.2 (#1, Jan 21 2003, 21:27:56)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import os
os.environ["OSTYPE"]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/Library/Frameworks/Python.framework/Versions/2.2/lib/python2.2/UserDict.py", line 14, in 
__getitem__
    def __getitem__(self, key): return self.data[key]
KeyError: OSTYPE



On Apr 22 18:29, Jeffrey.S.Whitaker at noaa.gov wrote:

Subject: Re: pil-1.1.4-3

Graham:  I have no such problem (OS 10.2.5), either in tcsh or bash.


[jsw at mac10:/Users/jsw] echo $OSTYPE
darwin
[jsw at mac10:/Users/jsw] python
Python 2.3a2 (#1, Apr  1 2003, 10:57:44)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import os
print os.environ.get("OSTYPE")
darwin

[jsw at mac10:/Users/jsw] bash
bash-2.05a$ echo $OSTYPE
darwin
bash-2.05a$ python
Python 2.3a2 (#1, Apr  1 2003, 10:57:44)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import os
print os.environ.get("OSTYPE")
darwin


-Jeff

On Tue, 22 Apr 2003 grahamd at dscpl.com.au wrote:

Sorry for the long mail, but have done so thorough checking.

On Apr 22 06:00, Jeffrey.S.Whitaker at noaa.gov wrote:

Subject: Re: pil-1.1.4-3

Graham:  Your OSTYPE variable should be set automatically, for bash and
csh.  Don't know why yours isn't.

Hmm, I checked again and OSTYPE is set in "bash", but it is set to "darwin6.0" which
in itself is also a problem as Python code only checks for "darwin" unless it gets
munged on the way through by Python somehow.

Now, if I run "/sw/bin/python", "/usr/bin/python" or Python installed from source as
a framework, none of them have OSTYPE set in os.environ.

If in the shell before running python I export OSTYPE explicitly, then it appears in
"os.environ" of Python, but, it is "darwin6.0". Since the PIL code checks for "darwin"
only, although it now doesn't give an exception, it doesn't try and run "Preview.app"
application and instead defaults to "xv", which I don't have installed as part of fink
and therefore get:

~/Workspaces/imaging [512]$ python BusinessCard.py
sh: xv: command not found
sh: xv: command not found

There thus seems to be two issues. The first is why OSTYPE isn't being exported by
bash automatically. The second is that the PIL code should possibly check for anything
starting with "darwin" since the version number very much seems to be getting added
which is going to cause it to use "xv" instead of "Preview.app".

Digging further, it seems that "tcsh" sets OSTYPE to "darwin" only. Thus perhaps the
code change was made by someone who runs "tcsh" and not "bash". Further, when
running "tcsh", the "OSTYPE" variable is getting exported automatically whereas in
"bash" it doesn't seem to be.

In summary, it seems:

  1. That bash doesn't export "OSTYPE" although it does set it.
  2. That bash sets "OSTYPE" to "darwin6.0" and not just "darwin".
  3. That tcsh sets "OSTYPE" to "darwin" and does actually export it.
  4. That sys.platform is set to "darwin" whether bash or tcsh is used.

If this is right, then I can't see that there is any choice but to look at "sys.platform"
as it is the only thing that works under both "bash" and "tcsh".

Suggest you try the following under bash:

~ [512]$ echo $OSTYPE
darwin6.0
~ [513]$ env | grep OSTYPE
~ [514]$

And then under tcsh.

[asdopey:~] grahamd% echo $OSTYPE
darwin
[asdopey:~] grahamd% env | grep OSTYPE
OSTYPE=darwin
[asdopey:~] grahamd%

Note, if you are a tcsh user, be careful about how you run bash to do the test as
it will simply inherit the tcsh environment.

[asdopey:~] grahamd% exec bash
~ [501]$ echo $OSTYPE
darwin
~ [502]$ env | grep OSTYPE
OSTYPE=darwin
~ [503]$

You will need to log into an account which actually has bash as the login shell as
well as the shell which "Terminal.app" or "ITerm.app" uses.

This may be why what I have behaves differently, in that I have use the NetInfo
Manager.app to change my shell to "/bin/bash". I would guess most people would
leave it at "/bin/tcsh" and if they want to use bash only change it in Terminal.app.

I think the trick if you run "tcsh" to show the problem is to "unsetenv" OSTYPE
before running bash.

~ [501]$ echo $OSTYPE
darwin6.0
~ [502]$ env | grep OSTYPE
~ [503]$ exec tcsh
[asdopey:~] grahamd% echo $OSTYPE
darwin
[asdopey:~] grahamd% env | grep OSTYPE
OSTYPE=darwin
[asdopey:~] grahamd% unsetenv OSTYPE
[asdopey:~] grahamd% echo $OSTYPE
OSTYPE: Undefined variable.
[asdopey:~] grahamd% exec bash
~ [501]$ echo $OSTYPE
darwin6.0
~ [502]$ env | grep OSTYPE
~ [503]$

This should then show that the variable is set differently and not exported under bash
login shell.


--
Jeffrey S. Whitaker         Phone : (303)497-6313
NOAA/OAR/CDC  R/CDC1        FAX   : (303)497-6449
325 Broadway                Web   : http://www.cdc.noaa.gov/~jsw
Boulder, CO, USA 80305-3328 Office: Skaggs Research Cntr 1D-124




-- 
Jeffrey S. Whitaker         Phone : (303)497-6313
NOAA/OAR/CDC  R/CDC1        FAX   : (303)497-6449
325 Broadway                Web   : http://www.cdc.noaa.gov/~jsw
Boulder, CO, USA 80305-3328 Office: Skaggs Research Cntr 1D-124


--
Graham Dumpleton (grahamd at dscpl.com.au)


More information about the Image-SIG mailing list