python / cpython

The Python programming language
https://www.python.org/
Other
60.24k stars 29.15k forks source link

source file stays open after parsing is done (PR#209) #32687

Closed 50eff062-408a-4098-b1b2-8222303b9d0c closed 23 years ago

50eff062-408a-4098-b1b2-8222303b9d0c commented 23 years ago
BPO 210616
Nosy @gvanrossum

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = 'https://github.com/gvanrossum' closed_at = created_at = labels = ['interpreter-core'] title = 'source file stays open after parsing is done (PR#209)' updated_at = user = 'https://bugs.python.org/anonymous' ``` bugs.python.org fields: ```python activity = actor = 'gvanrossum' assignee = 'gvanrossum' closed = True closed_date = None closer = None components = ['Interpreter Core'] creation = creator = 'anonymous' dependencies = [] files = [] hgrepos = [] issue_num = 210616 keywords = [] message_count = 2.0 messages = ['97', '98'] nosy_count = 1.0 nosy_names = ['gvanrossum'] pr_nums = [] priority = 'normal' resolution = 'fixed' stage = None status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue210616' versions = [] ```

3772858d-27d8-44b0-a664-d68674859f36 commented 23 years ago

Jitterbug-Id: 209 Submitted-By: Guido van Rossum \guido@python.org\ Date: Tue, 22 Feb 2000 10:42:17 -0500 Version: None OS: None

The post below is evidence that there is a real need to close the source file sooner than when the program execution is over. Unfortunately, as Martin points out, this requires changing function signatures (in the sense that the behaviors change).

The close-on-exec solution is not good enough; apart from the portability issue it also doesn't solve other problems caused by keeping the file open too long.

--Guido van Rossum (home page: http://www.python.org/~guido/)

------- Forwarded Message

Date: Mon, 21 Feb 2000 19:00:32 +0100 From: Martin von Loewis \loewis@informatik.hu-berlin.de\ To: aliberi@acutronic.com cc: help@python.org, guido@CNRI.Reston.VA.US Subject: Re: [Python-Help] execve

I tried to submit the below report via the web interface, but kept getting the folowing message:

The system encountered a fatal error

After command:
Received:

The last error code was: Connection refused

So, the bug report is below. Thanks.

Thanks for your report. It looks like Python is not closing the file descriptor of the script being executed. Please have a look at Py_Main, where fp is the file descriptor of the script being executed (potentially stdin). That is passed to PyRun_AnyFile, which eventually calls the parser to read the file. When PyRun_AnyFile returns, the file is closed if it is not stdin (or, rather, does not have a name).

Unfortunately, if the script performs exec, that file descriptor is still open.

I see two solutions: a) close the file after it has been parsed, before execution starts. That appears to be the Right Way (TM), but requires changes to the signatures of a number of functions. b) set the close-on-exec flag for the script. That will automagically close it when necessary, but doing so is not very portable. It will probably work on both Linux and LynxOS, so you may consider fixing it that way yourself.

Good luck, Martin

P.S. CC'ed to Guido for inspection.

------- End of Forwarded Message

\==================================================================== Audit trail: Wed Feb 23 21:30:38 2000 guido moved from incoming to open

gvanrossum commented 23 years ago

Fixed as follows:

Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(), PyRun_FileEx(). These are the same as their non-Ex counterparts but have an extra argument, a flag telling them to close the file when done.

Then this is used by Py_Main() and execfile() to close the file after it is parsed but before it is executed.