annotate Lib/os.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents a0f43f4481e0
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:
46942
5a7ccd65e59b Remove references to platform 'mac'
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46865
diff changeset
4 - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc.
5a7ccd65e59b Remove references to platform 'mac'
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46865
diff changeset
5 - os.path is one of the modules posixpath, or ntpath
5a7ccd65e59b Remove references to platform 'mac'
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46865
diff changeset
6 - os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos'
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 '\\')
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20551
diff changeset
10 - os.extsep is the extension separator ('.' or '/')
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
44565
2cdb661675f9 Backport of issue 1755179 fix.
Facundo Batista <facundobatista@gmail.com>
parents: 40443
diff changeset
26 import sys, errno
772
d945cf33a64f Added definition for pardir ('..')
Guido van Rossum <guido@python.org>
parents: 580
diff changeset
27
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
28 _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
29
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
30 # Note: more names are added to __all__ later.
48278
62c69d88e985 Update __all__ for cookielib, csv, os, and urllib2 for objects imported into
Brett Cannon <bcannon@gmail.com>
parents: 47189
diff changeset
31 __all__ = ["altsep", "curdir", "pardir", "sep", "extsep", "pathsep", "linesep",
34424
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
32 "defpath", "name", "path", "devnull",
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
33 "SEEK_SET", "SEEK_CUR", "SEEK_END"]
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
34
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
35 def _get_exports_list(module):
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
36 try:
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
37 return list(module.__all__)
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
38 except AttributeError:
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
39 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
40
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
41 if 'posix' in _names:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
42 name = 'posix'
8413
55fc8d933087 introduce a new platform-specific variable: os.linesep is the
Guido van Rossum <guido@python.org>
parents: 6760
diff changeset
43 linesep = '\n'
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
44 from posix import *
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
45 try:
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
46 from posix import _exit
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
47 except ImportError:
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
48 pass
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
49 import posixpath as path
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27897
diff changeset
50
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
51 import posix
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
52 __all__.extend(_get_exports_list(posix))
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
53 del posix
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
54
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
55 elif 'nt' in _names:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
56 name = 'nt'
8413
55fc8d933087 introduce a new platform-specific variable: os.linesep is the
Guido van Rossum <guido@python.org>
parents: 6760
diff changeset
57 linesep = '\r\n'
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
58 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
59 try:
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
60 from nt import _exit
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
61 except ImportError:
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
62 pass
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
63 import ntpath as path
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27897
diff changeset
64
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
65 import nt
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
66 __all__.extend(_get_exports_list(nt))
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
67 del nt
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
68
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6204
diff changeset
69 elif 'os2' in _names:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
70 name = 'os2'
8413
55fc8d933087 introduce a new platform-specific variable: os.linesep is the
Guido van Rossum <guido@python.org>
parents: 6760
diff changeset
71 linesep = '\r\n'
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
72 from os2 import *
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
73 try:
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
74 from os2 import _exit
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
75 except ImportError:
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
76 pass
22013
9885da3feeec OS/2 EMX port Library and regression test changes:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 20765
diff changeset
77 if sys.version.find('EMX GCC') == -1:
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
78 import ntpath as path
22013
9885da3feeec OS/2 EMX port Library and regression test changes:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 20765
diff changeset
79 else:
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
80 import os2emxpath as path
31067
0ec4d3ebd433 OS/2+EMX: make the link() emulation available as os.link()
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 29732
diff changeset
81 from _emx_link import link
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27897
diff changeset
82
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
83 import os2
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
84 __all__.extend(_get_exports_list(os2))
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
85 del os2
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
86
10971
ddd216003bd1 Mark Hammond: patch for Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10811
diff changeset
87 elif 'ce' in _names:
ddd216003bd1 Mark Hammond: patch for Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10811
diff changeset
88 name = 'ce'
ddd216003bd1 Mark Hammond: patch for Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10811
diff changeset
89 linesep = '\r\n'
ddd216003bd1 Mark Hammond: patch for Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10811
diff changeset
90 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
91 try:
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
92 from ce import _exit
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
93 except ImportError:
0a9796a6468e SF patch 661583: Remove old code from lib\os.py
Tim Peters <tim.peters@gmail.com>
parents: 27167
diff changeset
94 pass
10971
ddd216003bd1 Mark Hammond: patch for Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10811
diff changeset
95 # 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
96 import ntpath as path
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27897
diff changeset
97
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
98 import ce
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
99 __all__.extend(_get_exports_list(ce))
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
100 del ce
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
101
16949
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
102 elif 'riscos' in _names:
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
103 name = 'riscos'
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
104 linesep = '\n'
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
105 from riscos import *
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
106 try:
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
107 from riscos import _exit
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
108 except ImportError:
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
109 pass
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
110 import riscospath as path
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27897
diff changeset
111
16949
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
112 import riscos
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
113 __all__.extend(_get_exports_list(riscos))
17000
e59eda525fc1 fix typo in extending __all__ for riscos platform - closes bug 406296
Skip Montanaro <skip@pobox.com>
parents: 16972
diff changeset
114 del riscos
16949
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
115
1745
d5b67d2ec7ee Merge changes
Guido van Rossum <guido@python.org>
parents: 1463
diff changeset
116 else:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
117 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
118
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
119 sys.modules['os.path'] = path
33387
943c22a7c701 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33356
diff changeset
120 from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
33356
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33323
diff changeset
121 devnull)
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
122
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
123 del _names
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
124
34424
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
125 # 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
126 # to native constants if necessary in posixmodule.c
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
127 SEEK_SET = 0
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
128 SEEK_CUR = 1
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
129 SEEK_END = 2
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
130
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
131 #'
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
132
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
133 # Super directory utilities.
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
134 # (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
135
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
136 def makedirs(name, mode=0777):
24095
dbb14f85ae7b Be consistent with the functions in the posix/nt module: docstrings
Fred Drake <fdrake@acm.org>
parents: 23620
diff changeset
137 """makedirs(path [, mode=0777])
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
138
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
139 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
140 Works like mkdir, except that any intermediate path segment (not
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
141 just the rightmost) will be created if it does not exist. This is
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
142 recursive.
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
143
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
144 """
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
145 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
146 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
147 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
148 if head and tail and not path.exists(head):
40437
9d472555b90d Patch #1608267: fix a race condition in os.makedirs() is the directory
Georg Brandl <georg@python.org>
parents: 39342
diff changeset
149 try:
9d472555b90d Patch #1608267: fix a race condition in os.makedirs() is the directory
Georg Brandl <georg@python.org>
parents: 39342
diff changeset
150 makedirs(head, mode)
9d472555b90d Patch #1608267: fix a race condition in os.makedirs() is the directory
Georg Brandl <georg@python.org>
parents: 39342
diff changeset
151 except OSError, e:
9d472555b90d Patch #1608267: fix a race condition in os.makedirs() is the directory
Georg Brandl <georg@python.org>
parents: 39342
diff changeset
152 # be happy if someone already created the path
44565
2cdb661675f9 Backport of issue 1755179 fix.
Facundo Batista <facundobatista@gmail.com>
parents: 40443
diff changeset
153 if e.errno != errno.EEXIST:
40437
9d472555b90d Patch #1608267: fix a race condition in os.makedirs() is the directory
Georg Brandl <georg@python.org>
parents: 39342
diff changeset
154 raise
31253
c1402176e1a6 [Bug #829532] Invoking os.makedirs() with an argument that contains a
Andrew M. Kuchling <amk@amk.ca>
parents: 31067
diff changeset
155 if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists
c1402176e1a6 [Bug #829532] Invoking os.makedirs() with an argument that contains a
Andrew M. Kuchling <amk@amk.ca>
parents: 31067
diff changeset
156 return
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
157 mkdir(name, mode)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
158
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
159 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
160 """removedirs(path)
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
161
35859
6015df136b00 SF#1343671.
Fredrik Lundh <fredrik@pythonware.com>
parents: 35203
diff changeset
162 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
163 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
164 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
165 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
166 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
167 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
168
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
169 """
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
170 rmdir(name)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
171 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
172 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
173 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
174 while head and tail:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
175 try:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
176 rmdir(head)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
177 except error:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
178 break
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
179 head, tail = path.split(head)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
180
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
181 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
182 """renames(old, new)
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
183
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
184 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
185 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
186 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
187 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
188 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
189 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
190
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
191 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
192 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
193 file.
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
194
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
195 """
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
196 head, tail = path.split(new)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
197 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
198 makedirs(head)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
199 rename(old, new)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
200 head, tail = path.split(old)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
201 if head and tail:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
202 try:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
203 removedirs(head)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
204 except error:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
205 pass
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
206
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
207 __all__.extend(["makedirs", "removedirs", "renames"])
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
208
41258
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
209 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
210 """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
211
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
212 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
213 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
214
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
215 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
216
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
217 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
218 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
219 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
220 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
221 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
222 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
223
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
224 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
225 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
226 (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
227 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
228 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
229
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
230 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
231 (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
232 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
233 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
234 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
235 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
236 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
237
29106
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
238 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
239 optional arg 'onerror' is specified, it should be a function; it
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
240 will be called with one argument, an os.error instance. It can
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
241 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
242 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
243 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
244
41258
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
245 By default, os.walk does not follow symbolic links to subdirectories on
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
246 systems that support them. In order to get this functionality, set the
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
247 optional argument 'followlinks' to true.
1dc4c4ce2db9 New test for rev. 54407 which only uses directories under TESTFN.
Georg Brandl <georg@python.org>
parents: 41242
diff changeset
248
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
249 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
250 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
251 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
252 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
253
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
254 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
255
46688
11ed405d99cb Fix os.walk docstring example - issue2707
Gregory P. Smith <greg@mad-scientist.com>
parents: 44565
diff changeset
256 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
257 from os.path import join, getsize
46688
11ed405d99cb Fix os.walk docstring example - issue2707
Gregory P. Smith <greg@mad-scientist.com>
parents: 44565
diff changeset
258 for root, dirs, files in os.walk('python/Lib/email'):
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
259 print root, "consumes",
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
260 print sum([getsize(join(root, name)) for name in files]),
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
261 print "bytes in", len(files), "non-directory files"
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
262 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
263 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
264 """
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
265
57380
e83e3db62fc7 avoid doing an uneeded import in a function
Benjamin Peterson <benjamin@python.org>
parents: 56294
diff changeset
266 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
267
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 # We may not have read permission for top, in which case we can'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
269 # get a list of the files the directory contains. os.path.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
270 # 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
271 # 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
272 # 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
273 try:
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
274 # Note that listdir and error are globals in this module due
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
275 # 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
276 names = listdir(top)
29106
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
277 except error, err:
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
278 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
279 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
280 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
281
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
282 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
283 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
284 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
285 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
286 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
287 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
288
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
289 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
290 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
291 for name in dirs:
57380
e83e3db62fc7 avoid doing an uneeded import in a function
Benjamin Peterson <benjamin@python.org>
parents: 56294
diff changeset
292 new_path = join(top, name)
e83e3db62fc7 avoid doing an uneeded import in a function
Benjamin Peterson <benjamin@python.org>
parents: 56294
diff changeset
293 if followlinks or not islink(new_path):
e83e3db62fc7 avoid doing an uneeded import in a function
Benjamin Peterson <benjamin@python.org>
parents: 56294
diff changeset
294 for x in 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
295 yield x
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
296 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
297 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
298
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
299 __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
300
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
301 # 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
302 try:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
303 environ
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
304 except NameError:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
305 environ = {}
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
306
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
307 def execl(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
308 """execl(file, *args)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
309
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
310 Execute the executable file with argument list args, replacing the
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
311 current process. """
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
312 execv(file, args)
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
313
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
314 def execle(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
315 """execle(file, *args, env)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
316
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
317 Execute the executable file with argument list args and
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
318 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
319 env = args[-1]
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
320 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
321
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
322 def execlp(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
323 """execlp(file, *args)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
324
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
325 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
326 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
327 execvp(file, args)
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
328
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
329 def execlpe(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
330 """execlpe(file, *args, env)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
331
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
332 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
333 with argument list args and environment env, replacing the current
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14913
diff changeset
334 process. """
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
335 env = args[-1]
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
336 execvpe(file, args[:-1], env)
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
337
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
338 def execvp(file, args):
58614
6b33ea948896 Merged revisions 77879 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents: 56295
diff changeset
339 """execvp(file, args)
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
340
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
341 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
342 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
343 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
344 _execvpe(file, args)
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
345
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
346 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
347 """execvpe(file, args, env)
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
348
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
349 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
350 with argument list args and environment env , replacing the
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
351 current process.
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14913
diff changeset
352 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
353 _execvpe(file, args, env)
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
354
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
355 __all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
356
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
357 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
358 if env is not None:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
359 func = execve
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
360 argrest = (args, env)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
361 else:
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
362 func = execv
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
363 argrest = (args,)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
364 env = environ
24577
a32cff0c75dc SF patch 590294: os._execvpe security fix (Zack Weinberg).
Guido van Rossum <guido@python.org>
parents: 24095
diff changeset
365
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
366 head, tail = path.split(file)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
367 if head:
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
368 func(file, *argrest)
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
369 return
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23354
diff changeset
370 if 'PATH' in env:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
371 envpath = env['PATH']
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
372 else:
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
373 envpath = defpath
12232
63135e9def24 Patch by Fred Gansevles.
Guido van Rossum <guido@python.org>
parents: 11708
diff changeset
374 PATH = envpath.split(pathsep)
25102
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
375 saved_exc = None
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
376 saved_tb = None
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
377 for dir in PATH:
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
378 fullname = path.join(dir, file)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
379 try:
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
380 func(fullname, *argrest)
25102
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
381 except error, e:
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
382 tb = sys.exc_info()[2]
44565
2cdb661675f9 Backport of issue 1755179 fix.
Facundo Batista <facundobatista@gmail.com>
parents: 40443
diff changeset
383 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
384 and saved_exc is None):
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
385 saved_exc = e
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
386 saved_tb = tb
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
387 if saved_exc:
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
388 raise error, saved_exc, saved_tb
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
389 raise error, e, tb
16949
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
390
17007
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
391 # Change environ to automatically call putenv() if it exists
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
392 try:
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
393 # This will fail if there's no putenv
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
394 putenv
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
395 except NameError:
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
396 pass
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
397 else:
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
398 import UserDict
3805
4f73962aaba6 Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents: 2921
diff changeset
399
20551
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
400 # Fake unsetenv() for Windows
25680
65dffc9c0a16 Remove more DOS support.
Martin v. Löwis <martin@v.loewis.de>
parents: 25161
diff changeset
401 # not sure about os2 here but
20551
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
402 # I'm guessing they are the same.
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
403
25680
65dffc9c0a16 Remove more DOS support.
Martin v. Löwis <martin@v.loewis.de>
parents: 25161
diff changeset
404 if name in ('os2', 'nt'):
20551
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
405 def unsetenv(key):
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
406 putenv(key, "")
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
407
17007
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
408 if name == "riscos":
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
409 # On RISC OS, all env access goes through getenv and putenv
4c17cb2c621e Unify _Environ processing on riscos with other platforms.
Martin v. Löwis <martin@v.loewis.de>
parents: 17000
diff changeset
410 from riscosenviron import _Environ
25680
65dffc9c0a16 Remove more DOS support.
Martin v. Löwis <martin@v.loewis.de>
parents: 25161
diff changeset
411 elif name in ('os2', 'nt'): # Where Env Var Names Must Be UPPERCASE
8814
6738e5383977 Support case insensitive treatment of os.environ keys on Windows and
Guido van Rossum <guido@python.org>
parents: 8744
diff changeset
412 # But we store them as upper case
25143
65e0f2e395ac Have os.environ() inherit from the iterable version of UserDict.
Raymond Hettinger <python@rcn.com>
parents: 25102
diff changeset
413 class _Environ(UserDict.IterableUserDict):
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
414 def __init__(self, environ):
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
415 UserDict.UserDict.__init__(self)
8814
6738e5383977 Support case insensitive treatment of os.environ keys on Windows and
Guido van Rossum <guido@python.org>
parents: 8744
diff changeset
416 data = self.data
6738e5383977 Support case insensitive treatment of os.environ keys on Windows and
Guido van Rossum <guido@python.org>
parents: 8744
diff changeset
417 for k, v in environ.items():
12232
63135e9def24 Patch by Fred Gansevles.
Guido van Rossum <guido@python.org>
parents: 11708
diff changeset
418 data[k.upper()] = v
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
419 def __setitem__(self, key, item):
8814
6738e5383977 Support case insensitive treatment of os.environ keys on Windows and
Guido van Rossum <guido@python.org>
parents: 8744
diff changeset
420 putenv(key, item)
12232
63135e9def24 Patch by Fred Gansevles.
Guido van Rossum <guido@python.org>
parents: 11708
diff changeset
421 self.data[key.upper()] = item
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
422 def __getitem__(self, key):
12232
63135e9def24 Patch by Fred Gansevles.
Guido van Rossum <guido@python.org>
parents: 11708
diff changeset
423 return self.data[key.upper()]
20551
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
424 try:
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
425 unsetenv
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
426 except NameError:
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
427 def __delitem__(self, key):
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
428 del self.data[key.upper()]
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
429 else:
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
430 def __delitem__(self, key):
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
431 unsetenv(key)
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
432 del self.data[key.upper()]
43417
46c467dee659 Patch #1181: add os.environ.clear() method.
Georg Brandl <georg@python.org>
parents: 41690
diff changeset
433 def clear(self):
46c467dee659 Patch #1181: add os.environ.clear() method.
Georg Brandl <georg@python.org>
parents: 41690
diff changeset
434 for key in self.data.keys():
46c467dee659 Patch #1181: add os.environ.clear() method.
Georg Brandl <georg@python.org>
parents: 41690
diff changeset
435 unsetenv(key)
46c467dee659 Patch #1181: add os.environ.clear() method.
Georg Brandl <georg@python.org>
parents: 41690
diff changeset
436 del self.data[key]
43674
900ef0eb3f94 Bug #1287: make os.environ.pop() work as expected.
Georg Brandl <georg@python.org>
parents: 43417
diff changeset
437 def pop(self, key, *args):
900ef0eb3f94 Bug #1287: make os.environ.pop() work as expected.
Georg Brandl <georg@python.org>
parents: 43417
diff changeset
438 unsetenv(key)
43690
fc6ba088bd77 Fix new pop() method on os.environ on ignorecase-platforms.
Georg Brandl <georg@python.org>
parents: 43674
diff changeset
439 return self.data.pop(key.upper(), *args)
10811
d73a3293c47b The case-insensitive _Environ class was lacking a case-insensitive has_key().
Guido van Rossum <guido@python.org>
parents: 10266
diff changeset
440 def has_key(self, key):
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23354
diff changeset
441 return key.upper() in self.data
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23354
diff changeset
442 def __contains__(self, key):
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23354
diff changeset
443 return key.upper() in self.data
12232
63135e9def24 Patch by Fred Gansevles.
Guido van Rossum <guido@python.org>
parents: 11708
diff changeset
444 def get(self, key, failobj=None):
63135e9def24 Patch by Fred Gansevles.
Guido van Rossum <guido@python.org>
parents: 11708
diff changeset
445 return self.data.get(key.upper(), failobj)
34516
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
446 def update(self, dict=None, **kwargs):
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
447 if dict:
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
448 try:
34607
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
449 keys = dict.keys()
34516
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
450 except AttributeError:
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
451 # List of (key, value)
34607
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
452 for k, v in dict:
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
453 self[k] = v
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
454 else:
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
455 # got keys
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
456 # cannot use items(), since mappings
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
457 # may not have them.
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
458 for k in keys:
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
459 self[k] = dict[k]
34516
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
460 if kwargs:
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
461 self.update(kwargs)
23354
d466bc19f731 Patch 550804: Make os.environ.copy() return a copy.
Martin v. Löwis <martin@v.loewis.de>
parents: 22830
diff changeset
462 def copy(self):
d466bc19f731 Patch 550804: Make os.environ.copy() return a copy.
Martin v. Löwis <martin@v.loewis.de>
parents: 22830
diff changeset
463 return dict(self)
3805
4f73962aaba6 Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents: 2921
diff changeset
464
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
465 else: # Where Env Var Names Can Be Mixed Case
25161
c213e9b80026 Change UserDict to IterableUserDict
Raymond Hettinger <python@rcn.com>
parents: 25143
diff changeset
466 class _Environ(UserDict.IterableUserDict):
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
467 def __init__(self, environ):
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
468 UserDict.UserDict.__init__(self)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
469 self.data = environ
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
470 def __setitem__(self, key, item):
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
471 putenv(key, item)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
472 self.data[key] = item
34516
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
473 def update(self, dict=None, **kwargs):
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
474 if dict:
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
475 try:
34607
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
476 keys = dict.keys()
34516
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
477 except AttributeError:
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
478 # List of (key, value)
34607
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
479 for k, v in dict:
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
480 self[k] = v
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
481 else:
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
482 # got keys
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
483 # cannot use items(), since mappings
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
484 # may not have them.
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
485 for k in keys:
4b1207cb01ea Avoid using items() in environ.update(). Fixes #1124513.
Martin v. Löwis <martin@v.loewis.de>
parents: 34516
diff changeset
486 self[k] = dict[k]
34516
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
487 if kwargs:
658512774468 Revert os.py 1.75, and directly implement update.
Martin v. Löwis <martin@v.loewis.de>
parents: 34424
diff changeset
488 self.update(kwargs)
20551
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
489 try:
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
490 unsetenv
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
491 except NameError:
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
492 pass
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
493 else:
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
494 def __delitem__(self, key):
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
495 unsetenv(key)
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
496 del self.data[key]
43417
46c467dee659 Patch #1181: add os.environ.clear() method.
Georg Brandl <georg@python.org>
parents: 41690
diff changeset
497 def clear(self):
46c467dee659 Patch #1181: add os.environ.clear() method.
Georg Brandl <georg@python.org>
parents: 41690
diff changeset
498 for key in self.data.keys():
46c467dee659 Patch #1181: add os.environ.clear() method.
Georg Brandl <georg@python.org>
parents: 41690
diff changeset
499 unsetenv(key)
46c467dee659 Patch #1181: add os.environ.clear() method.
Georg Brandl <georg@python.org>
parents: 41690
diff changeset
500 del self.data[key]
43674
900ef0eb3f94 Bug #1287: make os.environ.pop() work as expected.
Georg Brandl <georg@python.org>
parents: 43417
diff changeset
501 def pop(self, key, *args):
900ef0eb3f94 Bug #1287: make os.environ.pop() work as expected.
Georg Brandl <georg@python.org>
parents: 43417
diff changeset
502 unsetenv(key)
900ef0eb3f94 Bug #1287: make os.environ.pop() work as expected.
Georg Brandl <georg@python.org>
parents: 43417
diff changeset
503 return self.data.pop(key, *args)
23354
d466bc19f731 Patch 550804: Make os.environ.copy() return a copy.
Martin v. Löwis <martin@v.loewis.de>
parents: 22830
diff changeset
504 def copy(self):
d466bc19f731 Patch 550804: Make os.environ.copy() return a copy.
Martin v. Löwis <martin@v.loewis.de>
parents: 22830
diff changeset
505 return dict(self)
20765
3a07bd2b0386 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
506
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
507
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
508 environ = _Environ(environ)
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
509
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
510 def getenv(key, default=None):
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27174
diff changeset
511 """Get an environment variable, return None if it doesn't exist.
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27174
diff changeset
512 The optional second argument can specify an alternate default."""
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27174
diff changeset
513 return environ.get(key, default)
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
514 __all__.append("getenv")
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
515
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
516 def _exists(name):
54651
d49d48f335c9 Avoid invoking the parser/compiler just to test the presence of a function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48955
diff changeset
517 return name in globals()
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
518
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
519 # 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
520 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
521
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
522 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
523 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
524
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
525 # 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
526 # 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
527 # 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
528
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
529 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
530 # 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
531 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
532 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
533 # 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
534 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
535 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
536 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
537 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
538 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
539 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
540 _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
541 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
542 # 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
543 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
544 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
545 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
546 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
547 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
548 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
549 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
550 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
551 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
552 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
553 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
554 raise error, "Not stopped, signaled or exited???"
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
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
556 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
557 """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
558
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
559 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
560 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
561 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
562 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
563 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
564
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
565 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
566 """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
567
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
568 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
569 specified environment.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
570 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
571 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
572 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
573 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
574
11431
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
575 # 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
576
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
577 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
578 """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
579
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
580 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
581 args in a subprocess.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
582 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
583 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
584 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
585 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
586
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
587 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
588 """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
589
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
590 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
591 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
592 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
593 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
594 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
595 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
596
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
597 if _exists("spawnv"):
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
598 # 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
599 # 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
600
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
601 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
602 """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
603
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
604 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
605 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
606 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
607 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
608 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
609
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
610 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
611 """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
612
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
613 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
614 supplied environment.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
615 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
616 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
617 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
618 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
619 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
620
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
621
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
622 __all__.extend(["spawnv", "spawnve", "spawnl", "spawnle",])
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
623
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
624
11431
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
625 if _exists("spawnvp"):
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
626 # 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
627 # 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
628 def spawnlp(mode, file, *args):
29732
1247f84aa6a6 SF #764121, docstring for spawnlp incorrect
Neal Norwitz <nnorwitz@gmail.com>
parents: 29106
diff changeset
629 """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
630
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
631 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
632 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
633 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
634 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
635 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
636 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
637
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
638 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
639 """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
640
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
641 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
642 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
643 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
644 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
645 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
646 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
647 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
648
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
649
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
650 __all__.extend(["spawnvp", "spawnvpe", "spawnlp", "spawnlpe",])
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
651
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
652
14675
22011d976917 Only supply popen2, popen3 when fork exists.
Guido van Rossum <guido@python.org>
parents: 14236
diff changeset
653 # Supply popen2 etc. (for Unix)
22011d976917 Only supply popen2, popen3 when fork exists.
Guido van Rossum <guido@python.org>
parents: 14236
diff changeset
654 if _exists("fork"):
22011d976917 Only supply popen2, popen3 when fork exists.
Guido van Rossum <guido@python.org>
parents: 14236
diff changeset
655 if not _exists("popen2"):
22011d976917 Only supply popen2, popen3 when fork exists.
Guido van Rossum <guido@python.org>
parents: 14236
diff changeset
656 def popen2(cmd, mode="t", bufsize=-1):
33720
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
657 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd'
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
658 may be a sequence, in which case arguments will be passed directly to
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
659 the program without shell intervention (as with os.spawnv()). If 'cmd'
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
660 is a string it will be passed to the shell (as with os.system()). If
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
661 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
662 file objects (child_stdin, child_stdout) are returned."""
41690
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
663 import warnings
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
664 msg = "os.popen2 is deprecated. Use the subprocess module."
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
665 warnings.warn(msg, DeprecationWarning, stacklevel=2)
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
666
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
667 import subprocess
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
668 PIPE = subprocess.PIPE
56295
7e1856167c4a Merged revisions 75143 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 48955
diff changeset
669 p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),
7e1856167c4a Merged revisions 75143 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 48955
diff changeset
670 bufsize=bufsize, stdin=PIPE, stdout=PIPE,
7e1856167c4a Merged revisions 75143 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 48955
diff changeset
671 close_fds=True)
41690
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
672 return p.stdin, p.stdout
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
673 __all__.append("popen2")
14236
58d7995ce795 Added os.popen2() and os.popen3() for non-Windows platforms.
Fred Drake <fdrake@acm.org>
parents: 13711
diff changeset
674
14675
22011d976917 Only supply popen2, popen3 when fork exists.
Guido van Rossum <guido@python.org>
parents: 14236
diff changeset
675 if not _exists("popen3"):
22011d976917 Only supply popen2, popen3 when fork exists.
Guido van Rossum <guido@python.org>
parents: 14236
diff changeset
676 def popen3(cmd, mode="t", bufsize=-1):
33720
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
677 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd'
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
678 may be a sequence, in which case arguments will be passed directly to
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
679 the program without shell intervention (as with os.spawnv()). If 'cmd'
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
680 is a string it will be passed to the shell (as with os.system()). If
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
681 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
682 file objects (child_stdin, child_stdout, child_stderr) are returned."""
41690
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
683 import warnings
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
684 msg = "os.popen3 is deprecated. Use the subprocess module."
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
685 warnings.warn(msg, DeprecationWarning, stacklevel=2)
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
686
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
687 import subprocess
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
688 PIPE = subprocess.PIPE
56295
7e1856167c4a Merged revisions 75143 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 48955
diff changeset
689 p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),
7e1856167c4a Merged revisions 75143 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 48955
diff changeset
690 bufsize=bufsize, stdin=PIPE, stdout=PIPE,
7e1856167c4a Merged revisions 75143 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 48955
diff changeset
691 stderr=PIPE, close_fds=True)
41690
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
692 return p.stdin, p.stdout, p.stderr
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
693 __all__.append("popen3")
14913
a062749b80f8 popen4(): Added for Unix.
Fred Drake <fdrake@acm.org>
parents: 14675
diff changeset
694
a062749b80f8 popen4(): Added for Unix.
Fred Drake <fdrake@acm.org>
parents: 14675
diff changeset
695 if not _exists("popen4"):
a062749b80f8 popen4(): Added for Unix.
Fred Drake <fdrake@acm.org>
parents: 14675
diff changeset
696 def popen4(cmd, mode="t", bufsize=-1):
33720
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
697 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd'
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
698 may be a sequence, in which case arguments will be passed directly to
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
699 the program without shell intervention (as with os.spawnv()). If 'cmd'
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
700 is a string it will be passed to the shell (as with os.system()). If
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
701 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
c2ae482ff42d Document that on Unix, the 'cmd' argument to the os.popen2/3/4 and
Johannes Gijsbers <jlg@dds.nl>
parents: 33514
diff changeset
702 file objects (child_stdin, child_stdout_stderr) are returned."""
41690
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
703 import warnings
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
704 msg = "os.popen4 is deprecated. Use the subprocess module."
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
705 warnings.warn(msg, DeprecationWarning, stacklevel=2)
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
706
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
707 import subprocess
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
708 PIPE = subprocess.PIPE
56295
7e1856167c4a Merged revisions 75143 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 48955
diff changeset
709 p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),
7e1856167c4a Merged revisions 75143 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 48955
diff changeset
710 bufsize=bufsize, stdin=PIPE, stdout=PIPE,
41690
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
711 stderr=subprocess.STDOUT, close_fds=True)
6c4aa4611c88 Deprecate os.popen* and popen2 module in favor of the subprocess module.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41258
diff changeset
712 return p.stdin, p.stdout
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
713 __all__.append("popen4")
22154
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
714
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
715 import copy_reg as _copy_reg
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
716
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
717 def _make_stat_result(tup, dict):
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
718 return stat_result(tup, dict)
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
719
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
720 def _pickle_stat_result(sr):
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
721 (type, args) = sr.__reduce__()
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
722 return (_make_stat_result, args)
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
723
22291
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
724 try:
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
725 _copy_reg.pickle(stat_result, _pickle_stat_result, _make_stat_result)
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
726 except NameError: # stat_result may not exist
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
727 pass
22154
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
728
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
729 def _make_statvfs_result(tup, dict):
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
730 return statvfs_result(tup, dict)
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
731
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
732 def _pickle_statvfs_result(sr):
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
733 (type, args) = sr.__reduce__()
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
734 return (_make_statvfs_result, args)
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
735
22291
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
736 try:
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
737 _copy_reg.pickle(statvfs_result, _pickle_statvfs_result,
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
738 _make_statvfs_result)
22292
Michael W. Hudson <mwh@python.net>
parents: 22291
diff changeset
739 except NameError: # statvfs_result may not exist
22291
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
740 pass