This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: CGIHTTPServer execfile should save cwd
Type: Stage:
Components: None Versions:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: akuchling, brett.cannon, dgrassi, tim.peters
Priority: normal Keywords: patch

Created on 2002-01-25 23:41 by dgrassi, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Messages (6)
msg38799 - (view) Author: Dan Grassi (dgrassi) Date: 2002-01-25 23:41
In CGIHTTPServer when running a script via execfile the 
cwd is not saved and restored.  If the executed script 
changes the cwd subsequent requests will fail because 
self.translate_path in SimpleHTTPServer relies on the 
cwd.

One fix is to suround the execfile call with
save_cwd = os.getcwd()
and
os.chdir(save_cwd)

Here is the proposed fix starting at line 254:

+	save_cwd = os.getcwd()
	try:
	    try:
	        sys.argv = [scriptfile]
	        if '=' not in decoded_query:
	            sys.argv.append(decoded_query)
	        sys.stdout = self.wfile
	        sys.stdin = self.rfile
	        execfile(scriptfile, {"__name__": 
"__main__"})
	    finally:
+	os.chdir(save_cwd)
msg38800 - (view) Author: Dan Grassi (dgrassi) Date: 2002-01-25 23:48
Logged In: YES 
user_id=366473


msg38801 - (view) Author: Dan Grassi (dgrassi) Date: 2002-01-25 23:55
Logged In: YES 
user_id=366473

Here is the (hopefully) properly indented fix:

+   save_cwd = os.getcwd()
    try:
        try:
            sys.argv = [scriptfile]
            if '=' not in decoded_query:
                sys.argv.append(decoded_query)
            sys.stdout = self.wfile
            sys.stdin = self.rfile
            execfile(scriptfile, {"__name__": "__main__"})
        finally:
+           os.chdir(save_cwd)
msg38802 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2003-05-17 00:47
Logged In: YES 
user_id=357491

Since a patch is in the comments I am making this a patch instead of a bug.

As for the idea, it seems good to me.  Anyone else care to comment?
msg38803 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2003-10-24 12:14
Logged In: YES 
user_id=11375

It looks like a bug worth fixing, and the proposed patch seems reasonable.
msg38804 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-03-20 22:03
Logged In: YES 
user_id=31435

I checked this in, but have no idea how to test it.  In part 
that's because the execfile path doesn't appear to be taken 
on Unixish *or* Windows systems.  Is this entire block of 
code actually unreachable (== is there an OS now that 
doesn't have fork or popen2 or popen3?).

Lib/CGIHTTPServer.py; new revision: 1.34
History
Date User Action Args
2022-04-10 16:04:55adminsetgithub: 35981
2002-01-25 23:41:54dgrassicreate