[BUG] http proxy overriding problem / env var problem on the other OS

Edouard Gomez ed.gomez at free.fr
Wed Aug 3 06:35:33 CDT 2005


K Thananchayan <thananck <at> yahoo.com> writes:
> While Win32 api functions
> GetEnvironmentVariable/SetEnvironmentVariable
> manipulate environment variables in case insensitive
> and case preserving fashion (Python does not use these
> functions), I do not think that this is the cause of
> your problems.

Well i think it is. Here's the proof.

I'm using a slightly modified version of hg, the diff follows:
diff -r eef752151556 mercurial/hg.py
--- a/mercurial/hg.py   Tue Aug 02 07:34:23 2005
+++ b/mercurial/hg.py   Wed Aug 03 13:15:52 2005
@@ -1770,7 +1770,8 @@
         # Note: urllib2 takes proxy values from the environment and those will
         # take precedence
         for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]:
-            if os.environ.has_key(env):
+            if env in os.environ:
+                print "Deleting map entry: ", env
                 del os.environ[env]
 
         proxy_handler = urllib2.BaseHandler()

Here is my environment (MSYS defined, i'm not using bare cmd.exe):
$ env | grep http
http_proxy=http://127.0.0.1:6666/

And here is the hg output:
$ hg pull http://www.selenic.com/hg
pulling from http://www.selenic.com/hg
Deleting map entry:  HTTP_PROXY
abort: No such file or directory: None

Running the same python code on my Linux box doesn't trigger HTTP_PROXY deletion.

> Can you please add the following after the last line 
>        urllib2.install_opener(opener)
> of httprepository.__init__ method having same
> indentaton
>  and let us know what is output by hg?
> 
>        print "Environment:", os.environ
>        print "opener: ", urilib2._opener.handle_open
>        print "proxy handler:", proxy_handler
>        print "proxy_handler.http_open default args"
>        print proxy_handler.http_open.func_defaults

diff -r eef752151556 mercurial/hg.py
--- a/mercurial/hg.py   Tue Aug 02 07:34:23 2005
+++ b/mercurial/hg.py   Wed Aug 03 13:15:52 2005
@@ -1785,7 +1786,11 @@
 
         opener = urllib2.build_opener(proxy_handler, authinfo)
         urllib2.install_opener(opener)
-
+        print "Environment:", os.environ
+        print "opener: ", urilib2._opener.handle_open
+        print "proxy handler:", proxy_handler
+        print "proxy_handler.http_open default args"
+        print proxy_handler.http_open.func_defaults
     def dev(self):
         return -1
 
Applied. Sorry i never reach this code. But i can output bits of information
earlier.

Environment:  
{'TMP': 'C:/DOCUME~1/edgomez/LOCALS~1/Temp',
 'SSL_HOME': 'd:\\Programs\\openssl',
 'COMPUTERNAME': 'EDGOMEZ',
 'HOSTNAME': 'EDGOMEZ',
 'HISTFILE': 'D:/Programs/msys/home/edgomez/.bash_history',
 'USERDOMAIN': 'CEGEDIM',
 'MSYSFGCOLOR': 'Black',
 'HOSTTYPE': 'i686',
 'COMMONPROGRAMFILES': 'C:\\Program Files\\Fichiers communs',
 'PROCESSOR_IDENTIFIER': 'x86 Family 15 Model 1 Stepping 2, GenuineIntel',
 'CVS_RSH': 'D:\\Programs\\putty\\plink.exe',
 'LOGNAME': 'edgomez',
 'PROGRAMFILES': 'C:\\Program Files',
 'PROCESSOR_REVISION': '0102',
 'PATH': '[... not pasted because gmane complains about line size],
 'HOME': 'D:/Programs/msys/home/edgomez',
 'DISPLAY': ':0',
 'MINGW_HOME': 'd:\\Programs\\mingw',
 'COMSPEC': 'C:\\WINNT\\system32\\cmd.exe',
 'LANG': 'FR',
 'MAKE_MODE': 'unix',
 'TERM': 'msys',
 'SHELL': 'D:/Programs/msys/bin/sh',
 'COLORFGBG': 'default;default',
 'TEMP': 'C:/DOCUME~1/edgomez/LOCALS~1/Temp',
 'SHLVL': '2',
 'PROCESSOR_ARCHITECTURE': 'x86',
 'ALLUSERSPROFILE': 'C:\\Documents and Settings\\All Users',
 '!::': '::\\',
 'WINDOWID': '167838912',
 'OSTYPE': 'msys',
 'CVS_HOME': 'd:\\Programs\\wincvs\\cvsnt',
 'JAVA_HOME': 'd:\\Programs\\java\\java-1.4',
 'FGCOLOR': 'Navy',
 'MSYS_HOME': 'D:\\Programs\\msys',
 'USERNAME': 'edgomez',
 'GPG_HOME': 'd:\\Programs\\gnupg',
 'LOGONSERVER': '\\\\CEGEDIM1',
 'PROMPT': '$P$G',
 'MSYSTEM': 'MINGW32',
 'SYSTEMROOT': 'C:\\WINNT',
 'AVENGINE': 'C:\\PROGRA~1\\CA\\SHARED~1\\SCANEN~1',
 'INOCULAN': 'C:\\PROGRA~1\\CA\\ETRUST~1',
 'MINGW32BGCOLOR': 'LightYellow',
 'HOMEPATH': '\\',
 'USERDNSDOMAIN': 'cegedim.cegedimgroup',
 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH',
 '_': 'd:/Programs/python/python',
 'MSYSBGCOLOR': 'White',
 'WINDIR': 'C:\\WINNT',
 'MINGW32FGCOLOR': 'Navy',
 'APPDATA': 'C:\\Documents and Settings\\edgomez\\Application Data',
 'HOMEDRIVE': 'C:',
 'BGCOLOR': 'LightYellow',
 'SYSTEMDRIVE': 'C:',
 'HTTP_PROXY': 'http://127.0.0.1:6666/',
 'MONO_HOME': 'd:\\Programs\\mono',
 'NUMBER_OF_PROCESSORS': '1',
 'PWD': 'D:/Programs/msys/home/edgomez/mercurial-snapshot',
 'PROCESSOR_LEVEL': '15',
 'COLORTERM': 'rxvt',
 'PYTHON_HOME': 'D:\\Programs\\python',
 'OS2LIBPATH': 'C:\\WINNT\\system32\\os2\\dll;',
 'MACHTYPE': 'i686-pc-msys',
 'OS': 'Windows_NT',
 'USERPROFILE': 'C:\\Documents and Settings\\edgomez'
}

So this again, proves the fact that case insensitive env vars are used as, i
define http_proxy, and get HTTP_PROXY listed. The same output could be obtained
from bare cmd.exe.

We should definetly trap exceptions there, and continue if the deletion failed.
This will probably help MSYS/Cygwin shell users.

But as i said in my previous email (did i ?):
try: del os.environ[env]
except: pass

didn't do the trick. The Python interpreter was crashing (v2.4.1).

> Although the code comments says that proxy specified
> in the environment variables override the one
> explicitly given to the proxy_handler, I am unable to
> figure out how that is possible? 

Python lib does this in your back. On Unix systems, the urllib2 creates a proxy
aware instance if there is a valid HTTP_PROXY env entry. On windows, if
HTTP_PROXY or IE proxy registry keys are defined, then the urllib2 returns a
proxy aware instance.

What the code tries to do, is disable this behavior by deleting the local copy
of the environment entries so that we are sure the returned instance respects
the local hgrc files wishes, not global options set in env or registry.

-- 
Edouard Gomez



More information about the Mercurial mailing list