summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-10-30 17:27:30 (GMT)
committerGuido van Rossum <guido@python.org>2007-10-30 17:27:30 (GMT)
commit2dced8b602df10531cab6cd87da5503c06f14888 (patch)
treebf87d57eb4945b66b672aadfb87c071449391441 /Lib
parent2673a5723433ff398fed901a8ebebb265031091e (diff)
downloadcpython-2dced8b602df10531cab6cd87da5503c06f14888.zip
cpython-2dced8b602df10531cab6cd87da5503c06f14888.tar.gz
cpython-2dced8b602df10531cab6cd87da5503c06f14888.tar.bz2
Patch 1329 (partial) by Christian Heimes.
Add a closefd flag to open() which can be set to False to prevent closing the file descriptor when close() is called or when the object is destroyed. Useful to ensure that sys.std{in,out,err} keep their file descriptors open when Python is uninitialized. (This was always a feature in 2.x, it just wasn't implemented in 3.0 yet.)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/io.py13
-rwxr-xr-xLib/quopri.py14
-rw-r--r--Lib/test/test_io.py3
3 files changed, 20 insertions, 10 deletions
diff --git a/Lib/io.py b/Lib/io.py
index c2d803c..d7709c4 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -49,7 +49,8 @@ class BlockingIOError(IOError):
self.characters_written = characters_written
-def open(file, mode="r", buffering=None, encoding=None, newline=None):
+def open(file, mode="r", buffering=None, encoding=None, newline=None,
+ closefd=True):
r"""Replacement for the built-in open function.
Args:
@@ -81,9 +82,12 @@ def open(file, mode="r", buffering=None, encoding=None, newline=None):
other legal values, any `'\n'` characters written are
translated to the given string.
+ closefd: optional argument to keep the underlying file descriptor
+ open when the file is closed. It must not be false when
+ a filename is given.
+
(*) If a file descriptor is given, it is closed when the returned
- I/O object is closed. If you don't want this to happen, use
- os.dup() to create a duplicate file descriptor.
+ I/O object is closed, unless closefd=False is give.
Mode strings characters:
'r': open for reading (default)
@@ -138,7 +142,8 @@ def open(file, mode="r", buffering=None, encoding=None, newline=None):
(reading and "r" or "") +
(writing and "w" or "") +
(appending and "a" or "") +
- (updating and "+" or ""))
+ (updating and "+" or ""),
+ closefd)
if buffering is None:
buffering = -1
if buffering < 0 and raw.isatty():
diff --git a/Lib/quopri.py b/Lib/quopri.py
index 62c0503..6b3d13e 100755
--- a/Lib/quopri.py
+++ b/Lib/quopri.py
@@ -227,12 +227,14 @@ def main():
sys.stderr.write("%s: can't open (%s)\n" % (file, msg))
sts = 1
continue
- if deco:
- decode(fp, sys.stdout.buffer)
- else:
- encode(fp, sys.stdout.buffer, tabs)
- if fp is not sys.stdin:
- fp.close()
+ try:
+ if deco:
+ decode(fp, sys.stdout.buffer)
+ else:
+ encode(fp, sys.stdout.buffer, tabs)
+ finally:
+ if file != '-':
+ fp.close()
if sts:
sys.exit(sts)
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index e826ff4..9d4163e 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -259,6 +259,9 @@ class IOTest(unittest.TestCase):
self.assertEqual(f.write(a), n)
f.close()
+ def test_closefd(self):
+ self.assertRaises(ValueError, io.open, test_support.TESTFN, 'w',
+ closefd=False)
class MemorySeekTestMixin: