annotate Lib/os.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 0b6052f2a8ee
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36360
4746fab34012 Patch #1410998: remove "DOS" from os.py docstring
Georg Brandl <georg@python.org>
parents: 35859
diff changeset
1 r"""OS routines for Mac, NT, or Posix depending on what system we're on.
580
3169d38f6774 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
3 This exports:
79480
15949f34e08f #16135: Removal of OS/2 support (Python code partial cleanup)
Jesus Cea <jcea@jcea.es>
parents: 78121
diff changeset
4 - all functions from posix, nt or ce, e.g. unlink, stat, etc.
47080
58a0d5524f9a Merged revisions 63119-63128,63130-63131,63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63189 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46978
diff changeset
5 - os.path is either posixpath or ntpath
79480
15949f34e08f #16135: Removal of OS/2 support (Python code partial cleanup)
Jesus Cea <jcea@jcea.es>
parents: 78121
diff changeset
6 - os.name is either 'posix', 'nt' or 'ce'.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
7 - os.curdir is a string representing the current directory ('.' or ':')
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
8 - os.pardir is a string representing the parent directory ('..' or '::')
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
9 - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
50076
c7abfab4dcdb Merged revisions 67525-67527,67529,67548-67549,67551-67552,67555,67578,67586,67593-67594,67596,67605-67606,67609,67612,67616-67617,67632,67635,67638 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48737
diff changeset
10 - os.extsep is the extension separator (always '.')
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11706
diff changeset
11 - os.altsep is the alternate pathname separator (None or '/')
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
12 - os.pathsep is the component separator used in $PATH etc
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11706
diff changeset
13 - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
14 - os.defpath is the default search path for executables
32302
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 31866
diff changeset
15 - os.devnull is the file path of the null device ('/dev/null', etc.)
580
3169d38f6774 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
17 Programs that import and use 'os' stand a better chance of being
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
18 portable between different platforms. Of course, they must then
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
19 only use functions that are defined by all platforms (e.g., unlink
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
20 and opendir), and leave all pathname manipulation to os.path
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
21 (e.g., split and join).
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
22 """
580
3169d38f6774 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
24 #'
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
25
44036
45ced6ab066d Merged revisions 59193-59201 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
26 import sys, errno
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
27 import stat as st
772
d945cf33a64f Added definition for pardir ('..')
Guido van Rossum <guido@python.org>
parents: 580
diff changeset
28
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
29 _names = sys.builtin_module_names
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
30
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
31 # Note: more names are added to __all__ later.
48737
f287beb352fe Partially revert r65795 by undoing change to 'os'.
Brett Cannon <bcannon@gmail.com>
parents: 48733
diff changeset
32 __all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
77115
352147bbefdb #14862: Add missing names to os.__all__
Petri Lehtinen <petri@digip.org>
parents: 76951
diff changeset
33 "defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR",
352147bbefdb #14862: Add missing names to os.__all__
Petri Lehtinen <petri@digip.org>
parents: 76951
diff changeset
34 "SEEK_END", "fsencode", "fsdecode", "get_exec_path", "fdopen",
352147bbefdb #14862: Add missing names to os.__all__
Petri Lehtinen <petri@digip.org>
parents: 76951
diff changeset
35 "popen", "extsep"]
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
36
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
37 def _exists(name):
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
38 return name in globals()
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
39
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
40 def _get_exports_list(module):
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
41 try:
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
42 return list(module.__all__)
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
43 except AttributeError:
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
44 return [n for n in dir(module) if n[0] != '_']
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
45
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 75097
diff changeset
46 # Any new dependencies of the os module and/or changes in path separator
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 75097
diff changeset
47 # requires updating importlib as well.
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
48 if 'posix' in _names:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
49 name = 'posix'
8413
55fc8d933087 introduce a new platform-specific variable: os.linesep is the
Guido van Rossum <guido@python.org>
parents: 6760
diff changeset
50 linesep = '\n'
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
51 from posix import *
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
52 try:
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
53 from posix import _exit
77115
352147bbefdb #14862: Add missing names to os.__all__
Petri Lehtinen <petri@digip.org>
parents: 76951
diff changeset
54 __all__.append('_exit')
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
55 except ImportError:
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
56 pass
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
57 import posixpath as path
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27897
diff changeset
58
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
59 try:
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
60 from posix import _have_functions
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
61 except ImportError:
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
62 pass
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
63
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
64 elif 'nt' in _names:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
65 name = 'nt'
8413
55fc8d933087 introduce a new platform-specific variable: os.linesep is the
Guido van Rossum <guido@python.org>
parents: 6760
diff changeset
66 linesep = '\r\n'
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
67 from nt import *
27174
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
68 try:
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
69 from nt import _exit
77115
352147bbefdb #14862: Add missing names to os.__all__
Petri Lehtinen <petri@digip.org>
parents: 76951
diff changeset
70 __all__.append('_exit')
27174
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
71 except ImportError:
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
72 pass
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
73 import ntpath as path
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27897
diff changeset
74
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
75 import nt
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
76 __all__.extend(_get_exports_list(nt))
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
77 del nt
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
78
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
79 try:
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
80 from nt import _have_functions
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
81 except ImportError:
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
82 pass
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
83
10971
ddd216003bd1 Mark Hammond: patch for Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10811
diff changeset
84 elif 'ce' in _names:
ddd216003bd1 Mark Hammond: patch for Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10811
diff changeset
85 name = 'ce'
ddd216003bd1 Mark Hammond: patch for Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10811
diff changeset
86 linesep = '\r\n'
ddd216003bd1 Mark Hammond: patch for Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10811
diff changeset
87 from ce import *
27174
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
88 try:
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
89 from ce import _exit
77115
352147bbefdb #14862: Add missing names to os.__all__
Petri Lehtinen <petri@digip.org>
parents: 76951
diff changeset
90 __all__.append('_exit')
27174
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
91 except ImportError:
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
92 pass
10971
ddd216003bd1 Mark Hammond: patch for Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10811
diff changeset
93 # We can use the standard Windows path.
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
94 import ntpath as path
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27897
diff changeset
95
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
96 import ce
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
97 __all__.extend(_get_exports_list(ce))
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
98 del ce
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
99
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
100 try:
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
101 from ce import _have_functions
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
102 except ImportError:
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
103 pass
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
104
1745
d5b67d2ec7ee Merge changes
Guido van Rossum <guido@python.org>
parents: 1463
diff changeset
105 else:
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42632
diff changeset
106 raise ImportError('no os specific module found')
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
107
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
108 sys.modules['os.path'] = path
50076
c7abfab4dcdb Merged revisions 67525-67527,67529,67548-67549,67551-67552,67555,67578,67586,67593-67594,67596,67605-67606,67609,67612,67616-67617,67632,67635,67638 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48737
diff changeset
109 from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
c7abfab4dcdb Merged revisions 67525-67527,67529,67548-67549,67551-67552,67555,67578,67586,67593-67594,67596,67605-67606,67609,67612,67616-67617,67632,67635,67638 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48737
diff changeset
110 devnull)
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
111
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
112 del _names
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
113
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
114
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
115 if _exists("_have_functions"):
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
116 _globals = globals()
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
117 def _add(str, fn):
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
118 if (fn in _globals) and (str in _have_functions):
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
119 _set.add(_globals[fn])
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
120
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
121 _set = set()
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
122 _add("HAVE_FACCESSAT", "access")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
123 _add("HAVE_FCHMODAT", "chmod")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
124 _add("HAVE_FCHOWNAT", "chown")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
125 _add("HAVE_FSTATAT", "stat")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
126 _add("HAVE_FUTIMESAT", "utime")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
127 _add("HAVE_LINKAT", "link")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
128 _add("HAVE_MKDIRAT", "mkdir")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
129 _add("HAVE_MKFIFOAT", "mkfifo")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
130 _add("HAVE_MKNODAT", "mknod")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
131 _add("HAVE_OPENAT", "open")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
132 _add("HAVE_READLINKAT", "readlink")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
133 _add("HAVE_RENAMEAT", "rename")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
134 _add("HAVE_SYMLINKAT", "symlink")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
135 _add("HAVE_UNLINKAT", "unlink")
77663
3b7230997425 Issue #15154: Add "dir_fd" parameter to os.rmdir, remove "rmdir"
Larry Hastings <larry@hastings.org>
parents: 77608
diff changeset
136 _add("HAVE_UNLINKAT", "rmdir")
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
137 _add("HAVE_UTIMENSAT", "utime")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
138 supports_dir_fd = _set
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
139
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
140 _set = set()
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
141 _add("HAVE_FACCESSAT", "access")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
142 supports_effective_ids = _set
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
143
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
144 _set = set()
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
145 _add("HAVE_FCHDIR", "chdir")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
146 _add("HAVE_FCHMOD", "chmod")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
147 _add("HAVE_FCHOWN", "chown")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
148 _add("HAVE_FDOPENDIR", "listdir")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
149 _add("HAVE_FEXECVE", "execve")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
150 _set.add(stat) # fstat always works
77694
da416ff1ecf5 Closes #15161: add support for giving path as a fd for truncate() and pathconf().
Georg Brandl <georg@python.org>
parents: 77663
diff changeset
151 _add("HAVE_FTRUNCATE", "truncate")
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
152 _add("HAVE_FUTIMENS", "utime")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
153 _add("HAVE_FUTIMES", "utime")
77694
da416ff1ecf5 Closes #15161: add support for giving path as a fd for truncate() and pathconf().
Georg Brandl <georg@python.org>
parents: 77663
diff changeset
154 _add("HAVE_FPATHCONF", "pathconf")
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
155 if _exists("statvfs") and _exists("fstatvfs"): # mac os x10.3
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
156 _add("HAVE_FSTATVFS", "statvfs")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
157 supports_fd = _set
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
158
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
159 _set = set()
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
160 _add("HAVE_FACCESSAT", "access")
77608
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
161 # Some platforms don't support lchmod(). Often the function exists
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
162 # anyway, as a stub that always returns ENOSUP or perhaps EOPNOTSUPP.
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
163 # (No, I don't know why that's a good design.) ./configure will detect
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
164 # this and reject it--so HAVE_LCHMOD still won't be defined on such
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
165 # platforms. This is Very Helpful.
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
166 #
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
167 # However, sometimes platforms without a working lchmod() *do* have
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
168 # fchmodat(). (Examples: Linux kernel 3.2 with glibc 2.15,
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
169 # OpenIndiana 3.x.) And fchmodat() has a flag that theoretically makes
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
170 # it behave like lchmod(). So in theory it would be a suitable
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
171 # replacement for lchmod(). But when lchmod() doesn't work, fchmodat()'s
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
172 # flag doesn't work *either*. Sadly ./configure isn't sophisticated
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
173 # enough to detect this condition--it only determines whether or not
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
174 # fchmodat() minimally works.
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
175 #
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
176 # Therefore we simply ignore fchmodat() when deciding whether or not
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
177 # os.chmod supports follow_symlinks. Just checking lchmod() is
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
178 # sufficient. After all--if you have a working fchmodat(), your
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
179 # lchmod() almost certainly works too.
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
180 #
66f7377547d5 Issue #14626: Fix buildbot issue on OpenIndiana 3.x machines. (Hopefully.)
Larry Hastings <larry@hastings.org>
parents: 77597
diff changeset
181 # _add("HAVE_FCHMODAT", "chmod")
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
182 _add("HAVE_FCHOWNAT", "chown")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
183 _add("HAVE_FSTATAT", "stat")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
184 _add("HAVE_LCHFLAGS", "chflags")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
185 _add("HAVE_LCHMOD", "chmod")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
186 if _exists("lchown"): # mac os x10.3
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
187 _add("HAVE_LCHOWN", "chown")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
188 _add("HAVE_LINKAT", "link")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
189 _add("HAVE_LUTIMES", "utime")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
190 _add("HAVE_LSTAT", "stat")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
191 _add("HAVE_FSTATAT", "stat")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
192 _add("HAVE_UTIMENSAT", "utime")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
193 _add("MS_WINDOWS", "stat")
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
194 supports_follow_symlinks = _set
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
195
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
196 del _set
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
197 del _have_functions
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
198 del _globals
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
199 del _add
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
200
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
201
34424
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
202 # Python uses fixed values for the SEEK_ constants; they are mapped
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
203 # to native constants if necessary in posixmodule.c
77571
de2a0cb6ba52 Closes #10142: Support for SEEK_HOLE/SEEK_DATA
Jesus Cea <jcea@jcea.es>
parents: 77334
diff changeset
204 # Other possible SEEK values are directly imported from posixmodule.c
34424
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
205 SEEK_SET = 0
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
206 SEEK_CUR = 1
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
207 SEEK_END = 2
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
208
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
209
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
210 def _get_masked_mode(mode):
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
211 mask = umask(0)
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
212 umask(mask)
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
213 return mode & ~mask
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
214
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
215 # Super directory utilities.
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
216 # (Inspired by Eric Raymond; the doc strings are mostly his)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
217
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
218 def makedirs(name, mode=0o777, exist_ok=False):
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
219 """makedirs(path [, mode=0o777][, exist_ok=False])
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
220
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
221 Super-mkdir; create a leaf directory and all intermediate ones.
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
222 Works like mkdir, except that any intermediate path segment (not
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
223 just the rightmost) will be created if it does not exist. If the
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
224 target directory with the same mode as we specified already exists,
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
225 raises an OSError if exist_ok is False, otherwise no exception is
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
226 raised. This is recursive.
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
227
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
228 """
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
229 head, tail = path.split(name)
13711
48886fa82ad5 makedirs(), removedirs(): If the tail of the path is empty, do a second
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
230 if not tail:
48886fa82ad5 makedirs(), removedirs(): If the tail of the path is empty, do a second
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
231 head, tail = path.split(head)
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
232 if head and tail and not path.exists(head):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
233 try:
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
234 makedirs(head, mode, exist_ok)
82177
79fd8659137b modernize some modules' code by replacing OSError->ENOENT/ENOTDIR/EPERM/EEXIST occurrences with the corresponding pep-3151 exceptions (FileNotFoundError, NotADirectoryError, etc.)
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81317
diff changeset
235 except FileExistsError:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
236 # be happy if someone already created the path
82177
79fd8659137b modernize some modules' code by replacing OSError->ENOENT/ENOTDIR/EPERM/EEXIST occurrences with the corresponding pep-3151 exceptions (FileNotFoundError, NotADirectoryError, etc.)
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81317
diff changeset
237 pass
81315
9458a516f769 Issue #15845: Fix comparison between bytes and string.
Serhiy Storchaka <storchaka@gmail.com>
parents: 77333
diff changeset
238 cdir = curdir
9458a516f769 Issue #15845: Fix comparison between bytes and string.
Serhiy Storchaka <storchaka@gmail.com>
parents: 77333
diff changeset
239 if isinstance(tail, bytes):
9458a516f769 Issue #15845: Fix comparison between bytes and string.
Serhiy Storchaka <storchaka@gmail.com>
parents: 77333
diff changeset
240 cdir = bytes(curdir, 'ASCII')
9458a516f769 Issue #15845: Fix comparison between bytes and string.
Serhiy Storchaka <storchaka@gmail.com>
parents: 77333
diff changeset
241 if tail == cdir: # xxx/newdir/. exists if xxx/newdir exists
31253
c1402176e1a6 [Bug #829532] Invoking os.makedirs() with an argument that contains a
Andrew M. Kuchling <amk@amk.ca>
parents: 31067
diff changeset
242 return
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
243 try:
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
244 mkdir(name, mode)
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
245 except OSError as e:
77329
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
246 dir_exists = path.isdir(name)
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
247 expected_mode = _get_masked_mode(mode)
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
248 if dir_exists:
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
249 # S_ISGID is automatically copied by the OS from parent to child
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
250 # directories on mkdir. Don't consider it being set to be a mode
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
251 # mismatch as mkdir does not unset it when not specified in mode.
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
252 actual_mode = st.S_IMODE(lstat(name).st_mode) & ~st.S_ISGID
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
253 else:
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
254 actual_mode = -1
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
255 if not (e.errno == errno.EEXIST and exist_ok and dir_exists and
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
256 actual_mode == expected_mode):
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
257 if dir_exists and actual_mode != expected_mode:
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
258 e.strerror += ' (mode %o != expected mode %o)' % (
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
259 actual_mode, expected_mode)
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
260 raise
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
261
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
262 def removedirs(name):
24095
dbb14f85ae7b Be consistent with the functions in the posix/nt module: docstrings
Fred Drake <fdrake@acm.org>
parents: 23620
diff changeset
263 """removedirs(path)
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
264
35859
6015df136b00 SF#1343671.
Fredrik Lundh <fredrik@pythonware.com>
parents: 35203
diff changeset
265 Super-rmdir; remove a leaf directory and all empty intermediate
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
266 ones. Works like rmdir except that, if the leaf directory is
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
267 successfully removed, directories corresponding to rightmost path
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
268 segments will be pruned away until either the whole path is
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
269 consumed or an error occurs. Errors during this latter phase are
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
270 ignored -- they generally mean that a directory was not empty.
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
271
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
272 """
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
273 rmdir(name)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
274 head, tail = path.split(name)
13711
48886fa82ad5 makedirs(), removedirs(): If the tail of the path is empty, do a second
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
275 if not tail:
48886fa82ad5 makedirs(), removedirs(): If the tail of the path is empty, do a second
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
276 head, tail = path.split(head)
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
277 while head and tail:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
278 try:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
279 rmdir(head)
81023
e2e5181b10f8 fix typo (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 81017
diff changeset
280 except OSError:
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
281 break
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
282 head, tail = path.split(head)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
283
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
284 def renames(old, new):
24095
dbb14f85ae7b Be consistent with the functions in the posix/nt module: docstrings
Fred Drake <fdrake@acm.org>
parents: 23620
diff changeset
285 """renames(old, new)
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
286
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
287 Super-rename; create directories as necessary and delete any left
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
288 empty. Works like rename, except creation of any intermediate
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
289 directories needed to make the new pathname good is attempted
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
290 first. After the rename, directories corresponding to rightmost
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
291 path segments of the old name will be pruned way until either the
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
292 whole path is consumed or a nonempty directory is found.
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
293
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
294 Note: this function can fail with the new directory structure made
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
295 if you lack permissions needed to unlink the leaf directory or
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
296 file.
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
297
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
298 """
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
299 head, tail = path.split(new)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
300 if head and tail and not path.exists(head):
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
301 makedirs(head)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
302 rename(old, new)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
303 head, tail = path.split(old)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
304 if head and tail:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
305 try:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
306 removedirs(head)
81017
6cfe2982de42 Use OESeeror instead of os.error (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80932
diff changeset
307 except OSError:
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
308 pass
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
309
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
310 __all__.extend(["makedirs", "removedirs", "renames"])
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
311
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
312 def walk(top, topdown=True, onerror=None, followlinks=False):
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
313 """Directory tree generator.
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
314
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
315 For each directory in the directory tree rooted at top (including top
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
316 itself, but excluding '.' and '..'), yields a 3-tuple
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
317
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
318 dirpath, dirnames, filenames
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
319
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
320 dirpath is a string, the path to the directory. dirnames is a list of
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
321 the names of the subdirectories in dirpath (excluding '.' and '..').
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
322 filenames is a list of the names of the non-directory files in dirpath.
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
323 Note that the names in the lists are just names, with no path components.
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
324 To get a full path (which begins with top) to a file or directory in
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
325 dirpath, do os.path.join(dirpath, name).
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
326
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
327 If optional arg 'topdown' is true or not specified, the triple for a
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
328 directory is generated before the triples for any of its subdirectories
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
329 (directories are generated top down). If topdown is false, the triple
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
330 for a directory is generated after the triples for all of its
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
331 subdirectories (directories are generated bottom up).
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
332
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
333 When topdown is true, the caller can modify the dirnames list in-place
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
334 (e.g., via del or slice assignment), and walk will only recurse into the
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
335 subdirectories whose names remain in dirnames; this can be used to prune
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
336 the search, or to impose a specific order of visiting. Modifying
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
337 dirnames when topdown is false is ineffective, since the directories in
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
338 dirnames have already been generated by the time dirnames itself is
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
339 generated.
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
340
29106
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
341 By default errors from the os.listdir() call are ignored. If
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
342 optional arg 'onerror' is specified, it should be a function; it
80932
47f98a550d42 Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79627
diff changeset
343 will be called with one argument, an OSError instance. It can
29106
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
344 report the error to continue with the walk, or raise the exception
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
345 to abort the walk. Note that the filename is available as the
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
346 filename attribute of the exception object.
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
347
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
348 By default, os.walk does not follow symbolic links to subdirectories on
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
349 systems that support them. In order to get this functionality, set the
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
350 optional argument 'followlinks' to true.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
351
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
352 Caution: if you pass a relative pathname for top, don't change the
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
353 current working directory between resumptions of walk. walk never
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
354 changes the current directory, and assumes that the client doesn't
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
355 either.
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
356
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
357 Example:
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
358
46690
c82f63efbc40 Merged revisions 62713,62715,62728,62737,62740,62744,62749,62756 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44036
diff changeset
359 import os
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
360 from os.path import join, getsize
46690
c82f63efbc40 Merged revisions 62713,62715,62728,62737,62740,62744,62749,62756 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44036
diff changeset
361 for root, dirs, files in os.walk('python/Lib/email'):
46978
55d8c35322a7 Convert a lot of print statements to print functions in docstrings,
Neal Norwitz <nnorwitz@gmail.com>
parents: 46867
diff changeset
362 print(root, "consumes", end="")
55d8c35322a7 Convert a lot of print statements to print functions in docstrings,
Neal Norwitz <nnorwitz@gmail.com>
parents: 46867
diff changeset
363 print(sum([getsize(join(root, name)) for name in files]), end="")
55d8c35322a7 Convert a lot of print statements to print functions in docstrings,
Neal Norwitz <nnorwitz@gmail.com>
parents: 46867
diff changeset
364 print("bytes in", len(files), "non-directory files")
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
365 if 'CVS' in dirs:
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
366 dirs.remove('CVS') # don't visit CVS directories
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
367 """
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
368
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55197
diff changeset
369 islink, join, isdir = path.islink, path.join, path.isdir
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
370
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
371 # We may not have read permission for top, in which case we can't
46800
87d854bc1e45 Removed remnants of os.path.walk().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46690
diff changeset
372 # get a list of the files the directory contains. os.walk
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
373 # always suppressed the exception then, rather than blow up for a
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
374 # minor reason when (say) a thousand readable directories are still
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
375 # left to visit. That logic is copied here.
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
376 try:
81017
6cfe2982de42 Use OESeeror instead of os.error (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80932
diff changeset
377 # Note that listdir is global in this module due
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
378 # to earlier import-*.
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
379 names = listdir(top)
81017
6cfe2982de42 Use OESeeror instead of os.error (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80932
diff changeset
380 except OSError as err:
29106
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
381 if onerror is not None:
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
382 onerror(err)
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
383 return
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
384
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
385 dirs, nondirs = [], []
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
386 for name in names:
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
387 if isdir(join(top, name)):
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
388 dirs.append(name)
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
389 else:
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
390 nondirs.append(name)
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
391
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
392 if topdown:
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
393 yield top, dirs, nondirs
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
394 for name in dirs:
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55197
diff changeset
395 new_path = join(top, name)
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55197
diff changeset
396 if followlinks or not islink(new_path):
76868
7c2df4af83dd use yield from
Benjamin Peterson <benjamin@python.org>
parents: 76571
diff changeset
397 yield from walk(new_path, topdown, onerror, followlinks)
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
398 if not topdown:
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
399 yield top, dirs, nondirs
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
400
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
401 __all__.append("walk")
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
402
77770
7bebd9870c75 Issue #15177: Added dir_fd parameter to os.fwalk().
Larry Hastings <larry@hastings.org>
parents: 77694
diff changeset
403 if {open, stat} <= supports_dir_fd and {listdir, stat} <= supports_fd:
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
404
78121
758a9023d836 Issue #15202: Consistently use the name "follow_symlinks" for
Larry Hastings <larry@hastings.org>
parents: 77770
diff changeset
405 def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None):
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
406 """Directory tree generator.
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
407
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
408 This behaves exactly like walk(), except that it yields a 4-tuple
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
409
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
410 dirpath, dirnames, filenames, dirfd
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
411
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
412 `dirpath`, `dirnames` and `filenames` are identical to walk() output,
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
413 and `dirfd` is a file descriptor referring to the directory `dirpath`.
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
414
77770
7bebd9870c75 Issue #15177: Added dir_fd parameter to os.fwalk().
Larry Hastings <larry@hastings.org>
parents: 77694
diff changeset
415 The advantage of fwalk() over walk() is that it's safe against symlink
78121
758a9023d836 Issue #15202: Consistently use the name "follow_symlinks" for
Larry Hastings <larry@hastings.org>
parents: 77770
diff changeset
416 races (when follow_symlinks is False).
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
417
77770
7bebd9870c75 Issue #15177: Added dir_fd parameter to os.fwalk().
Larry Hastings <larry@hastings.org>
parents: 77694
diff changeset
418 If dir_fd is not None, it should be a file descriptor open to a directory,
7bebd9870c75 Issue #15177: Added dir_fd parameter to os.fwalk().
Larry Hastings <larry@hastings.org>
parents: 77694
diff changeset
419 and top should be relative; top will then be relative to that directory.
7bebd9870c75 Issue #15177: Added dir_fd parameter to os.fwalk().
Larry Hastings <larry@hastings.org>
parents: 77694
diff changeset
420 (dir_fd is always supported for fwalk.)
7bebd9870c75 Issue #15177: Added dir_fd parameter to os.fwalk().
Larry Hastings <larry@hastings.org>
parents: 77694
diff changeset
421
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
422 Caution:
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
423 Since fwalk() yields file descriptors, those are only valid until the
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
424 next iteration step, so you should dup() them if you want to keep them
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
425 for a longer period.
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
426
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
427 Example:
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
428
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
429 import os
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
430 for root, dirs, files, rootfd in os.fwalk('python/Lib/email'):
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
431 print(root, "consumes", end="")
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
432 print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]),
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
433 end="")
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
434 print("bytes in", len(files), "non-directory files")
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
435 if 'CVS' in dirs:
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
436 dirs.remove('CVS') # don't visit CVS directories
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
437 """
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
438 # Note: To guard against symlink races, we use the standard
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
439 # lstat()/open()/fstat() trick.
77770
7bebd9870c75 Issue #15177: Added dir_fd parameter to os.fwalk().
Larry Hastings <larry@hastings.org>
parents: 77694
diff changeset
440 orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd)
7bebd9870c75 Issue #15177: Added dir_fd parameter to os.fwalk().
Larry Hastings <larry@hastings.org>
parents: 77694
diff changeset
441 topfd = open(top, O_RDONLY, dir_fd=dir_fd)
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
442 try:
78121
758a9023d836 Issue #15202: Consistently use the name "follow_symlinks" for
Larry Hastings <larry@hastings.org>
parents: 77770
diff changeset
443 if (follow_symlinks or (st.S_ISDIR(orig_st.st_mode) and
758a9023d836 Issue #15202: Consistently use the name "follow_symlinks" for
Larry Hastings <larry@hastings.org>
parents: 77770
diff changeset
444 path.samestat(orig_st, stat(topfd)))):
758a9023d836 Issue #15202: Consistently use the name "follow_symlinks" for
Larry Hastings <larry@hastings.org>
parents: 77770
diff changeset
445 yield from _fwalk(topfd, top, topdown, onerror, follow_symlinks)
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
446 finally:
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
447 close(topfd)
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
448
78121
758a9023d836 Issue #15202: Consistently use the name "follow_symlinks" for
Larry Hastings <larry@hastings.org>
parents: 77770
diff changeset
449 def _fwalk(topfd, toppath, topdown, onerror, follow_symlinks):
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
450 # Note: This uses O(depth of the directory tree) file descriptors: if
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
451 # necessary, it can be adapted to only require O(1) FDs, see issue
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
452 # #13734.
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
453
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
454 names = listdir(topfd)
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
455 dirs, nondirs = [], []
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
456 for name in names:
76951
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
457 try:
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
458 # Here, we don't use AT_SYMLINK_NOFOLLOW to be consistent with
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
459 # walk() which reports symlinks to directories as directories.
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
460 # We do however check for symlinks before recursing into
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
461 # a subdirectory.
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
462 if st.S_ISDIR(stat(name, dir_fd=topfd).st_mode):
76951
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
463 dirs.append(name)
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
464 else:
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
465 nondirs.append(name)
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
466 except FileNotFoundError:
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
467 try:
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
468 # Add dangling symlinks, ignore disappeared files
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
469 if st.S_ISLNK(stat(name, dir_fd=topfd, follow_symlinks=False)
76951
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
470 .st_mode):
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
471 nondirs.append(name)
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
472 except FileNotFoundError:
cbe7560d4443 #14773: Fix os.fwalk() failing on dangling symlinks
Hynek Schlawack <hs@ox.cx>
parents: 76868
diff changeset
473 continue
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
474
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
475 if topdown:
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
476 yield toppath, dirs, nondirs, topfd
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
477
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
478 for name in dirs:
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
479 try:
78121
758a9023d836 Issue #15202: Consistently use the name "follow_symlinks" for
Larry Hastings <larry@hastings.org>
parents: 77770
diff changeset
480 orig_st = stat(name, dir_fd=topfd, follow_symlinks=follow_symlinks)
77597
27f9c26fdd8b Issue #14626: Large refactoring of functions / parameters in the os module.
Larry Hastings <larry@hastings.org>
parents: 77571
diff changeset
481 dirfd = open(name, O_RDONLY, dir_fd=topfd)
81017
6cfe2982de42 Use OESeeror instead of os.error (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80932
diff changeset
482 except OSError as err:
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
483 if onerror is not None:
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
484 onerror(err)
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
485 return
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
486 try:
78121
758a9023d836 Issue #15202: Consistently use the name "follow_symlinks" for
Larry Hastings <larry@hastings.org>
parents: 77770
diff changeset
487 if follow_symlinks or path.samestat(orig_st, stat(dirfd)):
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
488 dirpath = path.join(toppath, name)
78121
758a9023d836 Issue #15202: Consistently use the name "follow_symlinks" for
Larry Hastings <larry@hastings.org>
parents: 77770
diff changeset
489 yield from _fwalk(dirfd, dirpath, topdown, onerror, follow_symlinks)
74795
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
490 finally:
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
491 close(dirfd)
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
492
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
493 if not topdown:
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
494 yield toppath, dirs, nondirs, topfd
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
495
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
496 __all__.append("fwalk")
773a97b3927d Issue #13734: Add os.fwalk(), a directory walking function yielding file
Charles-François Natali <neologix@free.fr>
parents: 68681
diff changeset
497
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
498 # Make sure os.environ exists, at least
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
499 try:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
500 environ
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
501 except NameError:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
502 environ = {}
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
503
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
504 def execl(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
505 """execl(file, *args)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
506
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
507 Execute the executable file with argument list args, replacing the
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
508 current process. """
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
509 execv(file, args)
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
510
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
511 def execle(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
512 """execle(file, *args, env)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
513
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
514 Execute the executable file with argument list args and
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
515 environment env, replacing the current process. """
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
516 env = args[-1]
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
517 execve(file, args[:-1], env)
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
518
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
519 def execlp(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
520 """execlp(file, *args)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
521
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
522 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
523 with argument list args, replacing the current process. """
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
524 execvp(file, args)
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
525
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
526 def execlpe(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
527 """execlpe(file, *args, env)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
528
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
529 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
530 with argument list args and environment env, replacing the current
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14913
diff changeset
531 process. """
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
532 env = args[-1]
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
533 execvpe(file, args[:-1], env)
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
534
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
535 def execvp(file, args):
58616
981a39f35914 Merged revisions 77881 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents: 57622
diff changeset
536 """execvp(file, args)
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
537
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
538 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
539 with argument list args, replacing the current process.
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 12502
diff changeset
540 args may be a list or tuple of strings. """
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
541 _execvpe(file, args)
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
542
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
543 def execvpe(file, args, env):
25102
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
544 """execvpe(file, args, env)
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
545
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
546 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
547 with argument list args and environment env , replacing the
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
548 current process.
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14913
diff changeset
549 args may be a list or tuple of strings. """
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
550 _execvpe(file, args, env)
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
551
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
552 __all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
553
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
554 def _execvpe(file, args, env=None):
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
555 if env is not None:
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
556 exec_func = execve
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
557 argrest = (args, env)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
558 else:
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
559 exec_func = execv
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
560 argrest = (args,)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
561 env = environ
24577
a32cff0c75dc SF patch 590294: os._execvpe security fix (Zack Weinberg).
Guido van Rossum <guido@python.org>
parents: 24095
diff changeset
562
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
563 head, tail = path.split(file)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
564 if head:
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
565 exec_func(file, *argrest)
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
566 return
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41843
diff changeset
567 last_exc = saved_exc = None
25102
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
568 saved_tb = None
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
569 path_list = get_exec_path(env)
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
570 if name != 'nt':
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
571 file = fsencode(file)
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
572 path_list = map(fsencode, path_list)
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
573 for dir in path_list:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
574 fullname = path.join(dir, file)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
575 try:
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
576 exec_func(fullname, *argrest)
81017
6cfe2982de42 Use OESeeror instead of os.error (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80932
diff changeset
577 except OSError as e:
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41843
diff changeset
578 last_exc = e
25102
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
579 tb = sys.exc_info()[2]
44036
45ced6ab066d Merged revisions 59193-59201 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
580 if (e.errno != errno.ENOENT and e.errno != errno.ENOTDIR
25102
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
581 and saved_exc is None):
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
582 saved_exc = e
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
583 saved_tb = tb
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
584 if saved_exc:
51746
c1bf037feefe Merged revisions 69754,69794 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50076
diff changeset
585 raise saved_exc.with_traceback(saved_tb)
c1bf037feefe Merged revisions 69754,69794 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50076
diff changeset
586 raise last_exc.with_traceback(tb)
16949
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
587
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
588
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
589 def get_exec_path(env=None):
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
590 """Returns the sequence of directories that will be searched for the
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
591 named executable (similar to a shell) when launching a process.
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
592
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
593 *env* must be an environment variable dict or None. If *env* is None,
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
594 os.environ will be used.
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
595 """
66027
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
596 # Use a local import instead of a global import to limit the number of
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
597 # modules loaded at startup: the os module is always loaded at startup by
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
598 # Python. It may also avoid a bootstrap issue.
65692
a56a2a803cd7 Issue #10210: os.get_exec_path() ignores BytesWarning warnings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65630
diff changeset
599 import warnings
a56a2a803cd7 Issue #10210: os.get_exec_path() ignores BytesWarning warnings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65630
diff changeset
600
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
601 if env is None:
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
602 env = environ
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
603
66067
418dcb1c45f7 os module: remove nonbreaking space in a comment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66027
diff changeset
604 # {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a
418dcb1c45f7 os module: remove nonbreaking space in a comment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66027
diff changeset
605 # BytesWarning when using python -b or python -bb: ignore the warning
66027
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
606 with warnings.catch_warnings():
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
607 warnings.simplefilter("ignore", BytesWarning)
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
608
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
609 try:
66027
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
610 path_list = env.get('PATH')
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
611 except TypeError:
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
612 path_list = None
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
613
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
614 if supports_bytes_environ:
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
615 try:
65692
a56a2a803cd7 Issue #10210: os.get_exec_path() ignores BytesWarning warnings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65630
diff changeset
616 path_listb = env[b'PATH']
66027
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
617 except (KeyError, TypeError):
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
618 pass
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
619 else:
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
620 if path_list is not None:
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
621 raise ValueError(
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
622 "env cannot contain 'PATH' and b'PATH' keys")
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
623 path_list = path_listb
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
624
66027
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
625 if path_list is not None and isinstance(path_list, bytes):
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
626 path_list = fsdecode(path_list)
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
627
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
628 if path_list is None:
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
629 path_list = defpath
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
630 return path_list.split(pathsep)
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
631
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
632
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
633 # Change environ to automatically call putenv(), unsetenv if they exist.
86273
0b6052f2a8ee Issue #19218: Rename collections.abc to _collections_abc in order to speed up interpreter start
Christian Heimes <christian@cheimes.de>
parents: 86216
diff changeset
634 from _collections_abc import MutableMapping
20551
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
635
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
636 class _Environ(MutableMapping):
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
637 def __init__(self, data, encodekey, decodekey, encodevalue, decodevalue, putenv, unsetenv):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
638 self.encodekey = encodekey
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
639 self.decodekey = decodekey
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
640 self.encodevalue = encodevalue
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
641 self.decodevalue = decodevalue
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
642 self.putenv = putenv
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
643 self.unsetenv = unsetenv
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
644 self._data = data
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
645
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
646 def __getitem__(self, key):
83369
72df981e83d3 Close #17702: os.environ now raises KeyError with the original environment
Victor Stinner <victor.stinner@gmail.com>
parents: 81316
diff changeset
647 try:
72df981e83d3 Close #17702: os.environ now raises KeyError with the original environment
Victor Stinner <victor.stinner@gmail.com>
parents: 81316
diff changeset
648 value = self._data[self.encodekey(key)]
72df981e83d3 Close #17702: os.environ now raises KeyError with the original environment
Victor Stinner <victor.stinner@gmail.com>
parents: 81316
diff changeset
649 except KeyError:
72df981e83d3 Close #17702: os.environ now raises KeyError with the original environment
Victor Stinner <victor.stinner@gmail.com>
parents: 81316
diff changeset
650 # raise KeyError with the original key value
85342
26c049dc1a4a Close #17702: On error, os.environb now removes suppress the except context
Victor Stinner <victor.stinner@gmail.com>
parents: 83369
diff changeset
651 raise KeyError(key) from None
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
652 return self.decodevalue(value)
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
653
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
654 def __setitem__(self, key, value):
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
655 key = self.encodekey(key)
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
656 value = self.encodevalue(value)
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
657 self.putenv(key, value)
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
658 self._data[key] = value
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
659
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
660 def __delitem__(self, key):
83369
72df981e83d3 Close #17702: os.environ now raises KeyError with the original environment
Victor Stinner <victor.stinner@gmail.com>
parents: 81316
diff changeset
661 encodedkey = self.encodekey(key)
72df981e83d3 Close #17702: os.environ now raises KeyError with the original environment
Victor Stinner <victor.stinner@gmail.com>
parents: 81316
diff changeset
662 self.unsetenv(encodedkey)
72df981e83d3 Close #17702: os.environ now raises KeyError with the original environment
Victor Stinner <victor.stinner@gmail.com>
parents: 81316
diff changeset
663 try:
72df981e83d3 Close #17702: os.environ now raises KeyError with the original environment
Victor Stinner <victor.stinner@gmail.com>
parents: 81316
diff changeset
664 del self._data[encodedkey]
72df981e83d3 Close #17702: os.environ now raises KeyError with the original environment
Victor Stinner <victor.stinner@gmail.com>
parents: 81316
diff changeset
665 except KeyError:
72df981e83d3 Close #17702: os.environ now raises KeyError with the original environment
Victor Stinner <victor.stinner@gmail.com>
parents: 81316
diff changeset
666 # raise KeyError with the original key value
85342
26c049dc1a4a Close #17702: On error, os.environb now removes suppress the except context
Victor Stinner <victor.stinner@gmail.com>
parents: 83369
diff changeset
667 raise KeyError(key) from None
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
668
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
669 def __iter__(self):
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
670 for key in self._data:
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
671 yield self.decodekey(key)
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
672
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
673 def __len__(self):
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
674 return len(self._data)
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
675
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
676 def __repr__(self):
63276
d55d21c396c5 Issue #9283: Oops, add missing { and } to repr(os.environ)
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63275
diff changeset
677 return 'environ({{{}}})'.format(', '.join(
63275
0132e4ee9e7f #9283: Fix repr(os.environ), display unicode keys and values on POSIX systems
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62276
diff changeset
678 ('{!r}: {!r}'.format(self.decodekey(key), self.decodevalue(value))
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
679 for key, value in self._data.items())))
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
680
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
681 def copy(self):
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
682 return dict(self)
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
683
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
684 def setdefault(self, key, value):
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
685 if key not in self:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
686 self[key] = value
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
687 return self[key]
3805
4f73962aaba6 Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents: 2921
diff changeset
688
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
689 try:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
690 _putenv = putenv
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
691 except NameError:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
692 _putenv = lambda key, value: None
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
693 else:
83672
32067784f198 Correction for 4f82b6cfee46.
Richard Oudkerk <shibturn@gmail.com>
parents: 83671
diff changeset
694 if "putenv" not in __all__:
32067784f198 Correction for 4f82b6cfee46.
Richard Oudkerk <shibturn@gmail.com>
parents: 83671
diff changeset
695 __all__.append("putenv")
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
696
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
697 try:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
698 _unsetenv = unsetenv
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
699 except NameError:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
700 _unsetenv = lambda key: _putenv(key, "")
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
701 else:
83672
32067784f198 Correction for 4f82b6cfee46.
Richard Oudkerk <shibturn@gmail.com>
parents: 83671
diff changeset
702 if "unsetenv" not in __all__:
32067784f198 Correction for 4f82b6cfee46.
Richard Oudkerk <shibturn@gmail.com>
parents: 83671
diff changeset
703 __all__.append("unsetenv")
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
704
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
705 def _createenviron():
79480
15949f34e08f #16135: Removal of OS/2 support (Python code partial cleanup)
Jesus Cea <jcea@jcea.es>
parents: 78121
diff changeset
706 if name == 'nt':
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
707 # Where Env Var Names Must Be UPPERCASE
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
708 def check_str(value):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
709 if not isinstance(value, str):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
710 raise TypeError("str expected, not %s" % type(value).__name__)
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
711 return value
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
712 encode = check_str
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
713 decode = str
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
714 def encodekey(key):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
715 return encode(key).upper()
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
716 data = {}
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
717 for key, value in environ.items():
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
718 data[encodekey(key)] = value
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
719 else:
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
720 # Where Env Var Names Can Be Mixed Case
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
721 encoding = sys.getfilesystemencoding()
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
722 def encode(value):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
723 if not isinstance(value, str):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
724 raise TypeError("str expected, not %s" % type(value).__name__)
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
725 return value.encode(encoding, 'surrogateescape')
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
726 def decode(value):
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
727 return value.decode(encoding, 'surrogateescape')
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
728 encodekey = encode
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
729 data = environ
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
730 return _Environ(data,
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
731 encodekey, decode,
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
732 encode, decode,
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
733 _putenv, _unsetenv)
20765
3a07bd2b0386 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
734
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
735 # unicode environ
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
736 environ = _createenviron()
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
737 del _createenviron
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
738
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
739
27167
7f09b1992ce9 Always define getenv(), as suggested by Guido. This means that os.getenv() is also defined for MacPython-OS9 (even though it doesn't actually do anything useful), and it shouldn't hurt on other platforms.
Jack Jansen <jack.jansen@cwi.nl>
parents: 25680
diff changeset
740 def getenv(key, default=None):
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27174
diff changeset
741 """Get an environment variable, return None if it doesn't exist.
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
742 The optional second argument can specify an alternate default.
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
743 key, default and the result are str."""
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27174
diff changeset
744 return environ.get(key, default)
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
745
79480
15949f34e08f #16135: Removal of OS/2 support (Python code partial cleanup)
Jesus Cea <jcea@jcea.es>
parents: 78121
diff changeset
746 supports_bytes_environ = (name != 'nt')
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
747 __all__.extend(("getenv", "supports_bytes_environ"))
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
748
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
749 if supports_bytes_environ:
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
750 def _check_bytes(value):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
751 if not isinstance(value, bytes):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
752 raise TypeError("bytes expected, not %s" % type(value).__name__)
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
753 return value
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
754
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
755 # bytes environ
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
756 environb = _Environ(environ._data,
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
757 _check_bytes, bytes,
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
758 _check_bytes, bytes,
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
759 _putenv, _unsetenv)
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
760 del _check_bytes
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
761
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
762 def getenvb(key, default=None):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
763 """Get an environment variable, return None if it doesn't exist.
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
764 The optional second argument can specify an alternate default.
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
765 key, default and the result are bytes."""
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
766 return environb.get(key, default)
63296
16189b7da3b6 #8603: Add environb to os.__all__
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63276
diff changeset
767
16189b7da3b6 #8603: Add environb to os.__all__
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63276
diff changeset
768 __all__.extend(("environb", "getenvb"))
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
769
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
770 def _fscodec():
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
771 encoding = sys.getfilesystemencoding()
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
772 if encoding == 'mbcs':
65630
159530f0e406 str.encode() doesn't accept None as errors: use 'strict' instead
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65626
diff changeset
773 errors = 'strict'
62276
3022904f9971 Issue #8969: On Windows, use mbcs codec in strict mode to encode and decode
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
774 else:
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
775 errors = 'surrogateescape'
64163
2cb2663e3368 Create os.fsdecode(): decode from the filesystem encoding with surrogateescape
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63296
diff changeset
776
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
777 def fsencode(filename):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
778 """
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
779 Encode filename to the filesystem encoding with 'surrogateescape' error
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
780 handler, return bytes unchanged. On Windows, use 'strict' error handler if
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
781 the file system encoding is 'mbcs' (which is the default encoding).
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
782 """
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
783 if isinstance(filename, bytes):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
784 return filename
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
785 elif isinstance(filename, str):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
786 return filename.encode(encoding, errors)
64163
2cb2663e3368 Create os.fsdecode(): decode from the filesystem encoding with surrogateescape
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63296
diff changeset
787 else:
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
788 raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
789
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
790 def fsdecode(filename):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
791 """
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
792 Decode filename from the filesystem encoding with 'surrogateescape' error
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
793 handler, return str unchanged. On Windows, use 'strict' error handler if
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
794 the file system encoding is 'mbcs' (which is the default encoding).
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
795 """
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
796 if isinstance(filename, str):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
797 return filename
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
798 elif isinstance(filename, bytes):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
799 return filename.decode(encoding, errors)
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
800 else:
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
801 raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
802
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
803 return fsencode, fsdecode
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
804
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
805 fsencode, fsdecode = _fscodec()
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
806 del _fscodec
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
807
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
808 # Supply spawn*() (probably only for Unix)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
809 if _exists("fork") and not _exists("spawnv") and _exists("execv"):
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
810
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
811 P_WAIT = 0
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
812 P_NOWAIT = P_NOWAITO = 1
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
813
77115
352147bbefdb #14862: Add missing names to os.__all__
Petri Lehtinen <petri@digip.org>
parents: 76951
diff changeset
814 __all__.extend(["P_WAIT", "P_NOWAIT", "P_NOWAITO"])
352147bbefdb #14862: Add missing names to os.__all__
Petri Lehtinen <petri@digip.org>
parents: 76951
diff changeset
815
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
816 # XXX Should we support P_DETACH? I suppose it could fork()**2
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
817 # and close the std I/O streams. Also, P_OVERLAY is the same
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
818 # as execv*()?
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
819
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
820 def _spawnvef(mode, file, args, env, func):
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
821 # Internal helper; func is the exec*() function to use
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
822 pid = fork()
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
823 if not pid:
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
824 # Child
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
825 try:
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
826 if env is None:
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
827 func(file, args)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
828 else:
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
829 func(file, args, env)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
830 except:
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
831 _exit(127)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
832 else:
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
833 # Parent
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
834 if mode == P_NOWAIT:
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
835 return pid # Caller is responsible for waiting!
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
836 while 1:
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
837 wpid, sts = waitpid(pid, 0)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
838 if WIFSTOPPED(sts):
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
839 continue
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
840 elif WIFSIGNALED(sts):
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
841 return -WTERMSIG(sts)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
842 elif WIFEXITED(sts):
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
843 return WEXITSTATUS(sts)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
844 else:
81017
6cfe2982de42 Use OESeeror instead of os.error (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80932
diff changeset
845 raise OSError("Not stopped, signaled or exited???")
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
846
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
847 def spawnv(mode, file, args):
12434
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
848 """spawnv(mode, file, args) -> integer
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
849
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
850 Execute file with arguments from args in a subprocess.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
851 If mode == P_NOWAIT return the pid of the process.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
852 If mode == P_WAIT return the process's exit code if it exits normally;
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14913
diff changeset
853 otherwise return -SIG, where SIG is the signal that killed it. """
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
854 return _spawnvef(mode, file, args, None, execv)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
855
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
856 def spawnve(mode, file, args, env):
12434
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
857 """spawnve(mode, file, args, env) -> integer
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
858
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
859 Execute file with arguments from args in a subprocess with the
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
860 specified environment.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
861 If mode == P_NOWAIT return the pid of the process.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
862 If mode == P_WAIT return the process's exit code if it exits normally;
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
863 otherwise return -SIG, where SIG is the signal that killed it. """
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
864 return _spawnvef(mode, file, args, env, execve)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
865
11431
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
866 # Note: spawnvp[e] is't currently supported on Windows
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
867
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
868 def spawnvp(mode, file, args):
12434
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
869 """spawnvp(mode, file, args) -> integer
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
870
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
871 Execute file (which is looked for along $PATH) with arguments from
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
872 args in a subprocess.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
873 If mode == P_NOWAIT return the pid of the process.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
874 If mode == P_WAIT return the process's exit code if it exits normally;
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
875 otherwise return -SIG, where SIG is the signal that killed it. """
11431
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
876 return _spawnvef(mode, file, args, None, execvp)
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
877
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
878 def spawnvpe(mode, file, args, env):
12434
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
879 """spawnvpe(mode, file, args, env) -> integer
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
880
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
881 Execute file (which is looked for along $PATH) with arguments from
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
882 args in a subprocess with the supplied environment.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
883 If mode == P_NOWAIT return the pid of the process.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
884 If mode == P_WAIT return the process's exit code if it exits normally;
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
885 otherwise return -SIG, where SIG is the signal that killed it. """
11431
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
886 return _spawnvef(mode, file, args, env, execvpe)
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
887
83671
4f82b6cfee46 Fix os.__all__ to is passes test___all__
Richard Oudkerk <shibturn@gmail.com>
parents: 83370
diff changeset
888
4f82b6cfee46 Fix os.__all__ to is passes test___all__
Richard Oudkerk <shibturn@gmail.com>
parents: 83370
diff changeset
889 __all__.extend(["spawnv", "spawnve", "spawnvp", "spawnvpe"])
4f82b6cfee46 Fix os.__all__ to is passes test___all__
Richard Oudkerk <shibturn@gmail.com>
parents: 83370
diff changeset
890
4f82b6cfee46 Fix os.__all__ to is passes test___all__
Richard Oudkerk <shibturn@gmail.com>
parents: 83370
diff changeset
891
11431
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
892 if _exists("spawnv"):
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
893 # These aren't supplied by the basic Windows code
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
894 # but can be easily implemented in Python
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
895
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
896 def spawnl(mode, file, *args):
12434
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
897 """spawnl(mode, file, *args) -> integer
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
898
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
899 Execute file with arguments from args in a subprocess.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
900 If mode == P_NOWAIT return the pid of the process.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
901 If mode == P_WAIT return the process's exit code if it exits normally;
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
902 otherwise return -SIG, where SIG is the signal that killed it. """
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
903 return spawnv(mode, file, args)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
904
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
905 def spawnle(mode, file, *args):
12434
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
906 """spawnle(mode, file, *args, env) -> integer
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
907
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
908 Execute file with arguments from args in a subprocess with the
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
909 supplied environment.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
910 If mode == P_NOWAIT return the pid of the process.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
911 If mode == P_WAIT return the process's exit code if it exits normally;
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
912 otherwise return -SIG, where SIG is the signal that killed it. """
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
913 env = args[-1]
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
914 return spawnve(mode, file, args[:-1], env)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
915
31866
552044b5058d OS/2 has support for spawnvp() and spawnvpe() in the C libraries supplied
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 31616
diff changeset
916
83671
4f82b6cfee46 Fix os.__all__ to is passes test___all__
Richard Oudkerk <shibturn@gmail.com>
parents: 83370
diff changeset
917 __all__.extend(["spawnl", "spawnle"])
31866
552044b5058d OS/2 has support for spawnvp() and spawnvpe() in the C libraries supplied
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 31616
diff changeset
918
552044b5058d OS/2 has support for spawnvp() and spawnvpe() in the C libraries supplied
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 31616
diff changeset
919
11431
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
920 if _exists("spawnvp"):
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
921 # At the moment, Windows doesn't implement spawnvp[e],
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
922 # so it won't have spawnlp[e] either.
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
923 def spawnlp(mode, file, *args):
29732
1247f84aa6a6 SF #764121, docstring for spawnlp incorrect
Neal Norwitz <nnorwitz@gmail.com>
parents: 29106
diff changeset
924 """spawnlp(mode, file, *args) -> integer
12434
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
925
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
926 Execute file (which is looked for along $PATH) with arguments from
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
927 args in a subprocess with the supplied environment.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
928 If mode == P_NOWAIT return the pid of the process.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
929 If mode == P_WAIT return the process's exit code if it exits normally;
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
930 otherwise return -SIG, where SIG is the signal that killed it. """
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
931 return spawnvp(mode, file, args)
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
932
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
933 def spawnlpe(mode, file, *args):
12434
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
934 """spawnlpe(mode, file, *args, env) -> integer
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
935
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
936 Execute file (which is looked for along $PATH) with arguments from
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
937 args in a subprocess with the supplied environment.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
938 If mode == P_NOWAIT return the pid of the process.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
939 If mode == P_WAIT return the process's exit code if it exits normally;
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
940 otherwise return -SIG, where SIG is the signal that killed it. """
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
941 env = args[-1]
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
942 return spawnvpe(mode, file, args[:-1], env)
12434
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
943
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
944
83671
4f82b6cfee46 Fix os.__all__ to is passes test___all__
Richard Oudkerk <shibturn@gmail.com>
parents: 83370
diff changeset
945 __all__.extend(["spawnlp", "spawnlpe"])
4f82b6cfee46 Fix os.__all__ to is passes test___all__
Richard Oudkerk <shibturn@gmail.com>
parents: 83370
diff changeset
946
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
947
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
948 # Supply os.popen()
68679
1dc52ecb8949 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 60975
diff changeset
949 def popen(cmd, mode="r", buffering=-1):
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43227
diff changeset
950 if not isinstance(cmd, str):
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
951 raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
952 if mode not in ("r", "w"):
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
953 raise ValueError("invalid mode %r" % mode)
79626
332b93ce7bf9 compare singletons by identity not equality (closes #16712)
Benjamin Peterson <benjamin@python.org>
parents: 78121
diff changeset
954 if buffering == 0 or buffering is None:
68679
1dc52ecb8949 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 60975
diff changeset
955 raise ValueError("popen() does not support unbuffered streams")
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
956 import subprocess, io
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
957 if mode == "r":
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
958 proc = subprocess.Popen(cmd,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
959 shell=True,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
960 stdout=subprocess.PIPE,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
961 bufsize=buffering)
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
962 return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
963 else:
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
964 proc = subprocess.Popen(cmd,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
965 shell=True,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
966 stdin=subprocess.PIPE,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
967 bufsize=buffering)
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
968 return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
969
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
970 # Helper for popen() -- a proxy for a file whose close waits for the process
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
971 class _wrap_close:
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
972 def __init__(self, stream, proc):
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
973 self._stream = stream
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
974 self._proc = proc
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
975 def close(self):
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
976 self._stream.close()
55197
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
977 returncode = self._proc.wait()
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
978 if returncode == 0:
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
979 return None
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
980 if name == 'nt':
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
981 return returncode
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
982 else:
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
983 return returncode << 8 # Shift left to match old behavior
57622
ccc5406cb381 Merged revisions 76723 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55197
diff changeset
984 def __enter__(self):
ccc5406cb381 Merged revisions 76723 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55197
diff changeset
985 return self
ccc5406cb381 Merged revisions 76723 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55197
diff changeset
986 def __exit__(self, *args):
ccc5406cb381 Merged revisions 76723 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55197
diff changeset
987 self.close()
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
988 def __getattr__(self, name):
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
989 return getattr(self._stream, name)
43227
c9ac5b8e4a81 Make the _wrap_close type (which is returned by os.popen) iterable.
Thomas Heller <theller@ctypes.org>
parents: 43102
diff changeset
990 def __iter__(self):
c9ac5b8e4a81 Make the _wrap_close type (which is returned by os.popen) iterable.
Thomas Heller <theller@ctypes.org>
parents: 43102
diff changeset
991 return iter(self._stream)
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
992
48599
631cd9ae5c8e #2491: os.fdopen() is now almost an alias to the builtin open(), and accepts the same parameters.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47080
diff changeset
993 # Supply os.fdopen()
631cd9ae5c8e #2491: os.fdopen() is now almost an alias to the builtin open(), and accepts the same parameters.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47080
diff changeset
994 def fdopen(fd, *args, **kwargs):
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
995 if not isinstance(fd, int):
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
996 raise TypeError("invalid fd type (%s, expected integer)" % type(fd))
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
997 import io
48599
631cd9ae5c8e #2491: os.fdopen() is now almost an alias to the builtin open(), and accepts the same parameters.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47080
diff changeset
998 return io.open(fd, *args, **kwargs)