Skip to content

Commit bdec50f

Browse files
committed
Feature request #935915: Add os.path.devnull.
1 parent f30d60e commit bdec50f

8 files changed

Lines changed: 31 additions & 6 deletions

File tree

Doc/lib/libos.tex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,3 +1808,11 @@ \subsection{Miscellaneous System Information \label{os-path}}
18081808
n'} for \POSIX{} or \code{'\e r'} for Mac OS, or multiple characters,
18091809
for example, \code{'\e r\e n'} for Windows.
18101810
\end{datadesc}
1811+
1812+
\begin{datadesc}{devnull}
1813+
The file path of the null device.
1814+
For example: \code{'/dev/null'} for \POSIX{} or \code{'Dev:Nul'} for the
1815+
Macintosh.
1816+
Also available via \module{os.path}.
1817+
\versionadded{2.4}
1818+
\end{datadesc}

Lib/macpath.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"getatime","getctime", "islink","exists","isdir","isfile",
99
"walk","expanduser","expandvars","normpath","abspath",
1010
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
11-
"realpath","supports_unicode_filenames"]
11+
"devnull","realpath","supports_unicode_filenames"]
1212

1313
# strings representing various path-related bits and pieces
1414
curdir = ':'
@@ -18,6 +18,7 @@
1818
pathsep = '\n'
1919
defpath = ':'
2020
altsep = None
21+
devnull = 'Dev:Null'
2122

2223
# Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
2324

Lib/ntpath.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"getatime","getctime", "islink","exists","isdir","isfile","ismount",
1515
"walk","expanduser","expandvars","normpath","abspath","splitunc",
1616
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
17-
"realpath","supports_unicode_filenames"]
17+
"devnull","realpath","supports_unicode_filenames"]
1818

1919
# strings representing various path-related bits and pieces
2020
curdir = '.'
@@ -29,6 +29,7 @@
2929
elif 'os2' in sys.builtin_module_names:
3030
# OS/2 w/ VACPP
3131
altsep = '/'
32+
devnull = 'nul'
3233

3334
# Normalize the case of a pathname and map slashes to backslashes.
3435
# Other normalizations (such as optimizing '../' away) are not done

Lib/os.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- os.pathsep is the component separator used in $PATH etc
1313
- os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
1414
- os.defpath is the default search path for executables
15+
- os.devnull is the file path of the null device ('/dev/null', etc.)
1516
1617
Programs that import and use 'os' stand a better chance of being
1718
portable between different platforms. Of course, they must then
@@ -28,7 +29,7 @@
2829

2930
# Note: more names are added to __all__ later.
3031
__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
31-
"defpath", "name", "path"]
32+
"defpath", "name", "path", "devnull"]
3233

3334
def _get_exports_list(module):
3435
try:
@@ -129,7 +130,8 @@ def _get_exports_list(module):
129130
raise ImportError, 'no os specific module found'
130131

131132
sys.modules['os.path'] = path
132-
from os.path import curdir, pardir, sep, pathsep, defpath, extsep, altsep
133+
from os.path import curdir, pardir, sep, pathsep, defpath, extsep, altsep, \
134+
devnull
133135

134136
del _names
135137

Lib/os2emxpath.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"getatime","getctime", "islink","exists","isdir","isfile","ismount",
1414
"walk","expanduser","expandvars","normpath","abspath","splitunc",
1515
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
16-
"realpath","supports_unicode_filenames"]
16+
"devnull","realpath","supports_unicode_filenames"]
1717

1818
# strings representing various path-related bits and pieces
1919
curdir = '.'
@@ -23,6 +23,7 @@
2323
altsep = '\\'
2424
pathsep = ';'
2525
defpath = '.;C:\\bin'
26+
devnull = 'nul'
2627

2728
# Normalize the case of a pathname and map slashes to backslashes.
2829
# Other normalizations (such as optimizing '../' away) are not done

Lib/posixpath.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"walk","expanduser","expandvars","normpath","abspath",
2020
"samefile","sameopenfile","samestat",
2121
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
22-
"realpath","supports_unicode_filenames"]
22+
"devnull","realpath","supports_unicode_filenames"]
2323

2424
# strings representing various path-related bits and pieces
2525
curdir = '.'
@@ -29,6 +29,7 @@
2929
pathsep = ':'
3030
defpath = ':/bin:/usr/bin'
3131
altsep = None
32+
devnull = '/dev/null'
3233

3334
# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
3435
# On MS-DOS this may also turn slashes into backslashes; however, other

Lib/test/test_os.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,14 @@ def tearDown(self):
334334

335335
os.removedirs(path)
336336

337+
class DevNullTests (unittest.TestCase):
338+
def test_devnull(self):
339+
f = file(os.devnull, 'w')
340+
f.write('hello')
341+
f.close()
342+
f = file(os.devnull, 'r')
343+
assert f.read() == ''
344+
f.close()
337345

338346
def test_main():
339347
test_support.run_unittest(
@@ -342,6 +350,7 @@ def test_main():
342350
EnvironTests,
343351
WalkTests,
344352
MakedirTests,
353+
DevNullTests,
345354
)
346355

347356
if __name__ == "__main__":

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ Extension modules
328328
Library
329329
-------
330330

331+
- os.path.devnull has been added for all supported platforms.
332+
331333
- Fixed #877165: distutils now picks the right C++ compiler command
332334
on cygwin and mingw32.
333335

0 commit comments

Comments
 (0)