annotate Lib/os.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents f3ce3e874a58
children 9458a516f769 5c0ee973a39a
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:
47080
58a0d5524f9a Merged revisions 63119-63128,63130-63131,63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63189 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46978
diff changeset
4 - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc.
58a0d5524f9a Merged revisions 63119-63128,63130-63131,63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63189 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46978
diff changeset
5 - os.path is either posixpath or ntpath
58a0d5524f9a Merged revisions 63119-63128,63130-63131,63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63189 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46978
diff changeset
6 - os.name is either 'posix', 'nt', 'os2' or 'ce'.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
7 - os.curdir is a string representing the current directory ('.' or ':')
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
8 - os.pardir is a string representing the parent directory ('..' or '::')
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
9 - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
50076
c7abfab4dcdb Merged revisions 67525-67527,67529,67548-67549,67551-67552,67555,67578,67586,67593-67594,67596,67605-67606,67609,67612,67616-67617,67632,67635,67638 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48737
diff changeset
10 - os.extsep is the extension separator (always '.')
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11706
diff changeset
11 - os.altsep is the alternate pathname separator (None or '/')
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
12 - os.pathsep is the component separator used in $PATH etc
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11706
diff changeset
13 - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
14 - os.defpath is the default search path for executables
32302
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 31866
diff changeset
15 - os.devnull is the file path of the null device ('/dev/null', etc.)
580
3169d38f6774 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
17 Programs that import and use 'os' stand a better chance of being
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
18 portable between different platforms. Of course, they must then
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
19 only use functions that are defined by all platforms (e.g., unlink
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
20 and opendir), and leave all pathname manipulation to os.path
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
21 (e.g., split and join).
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11431
diff changeset
22 """
580
3169d38f6774 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
24 #'
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
25
44036
45ced6ab066d Merged revisions 59193-59201 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
26 import sys, errno
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.
48737
f287beb352fe Partially revert r65795 by undoing change to 'os'.
Brett Cannon <bcannon@gmail.com>
parents: 48733
diff changeset
31 __all__ = ["altsep", "curdir", "pardir", "sep", "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
1745
d5b67d2ec7ee Merge changes
Guido van Rossum <guido@python.org>
parents: 1463
diff changeset
102 else:
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42632
diff changeset
103 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
104
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27507
diff changeset
105 sys.modules['os.path'] = path
50076
c7abfab4dcdb Merged revisions 67525-67527,67529,67548-67549,67551-67552,67555,67578,67586,67593-67594,67596,67605-67606,67609,67612,67616-67617,67632,67635,67638 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48737
diff changeset
106 from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
c7abfab4dcdb Merged revisions 67525-67527,67529,67548-67549,67551-67552,67555,67578,67586,67593-67594,67596,67605-67606,67609,67612,67616-67617,67632,67635,67638 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48737
diff changeset
107 devnull)
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
108
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
109 del _names
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
110
34424
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
111 # 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
112 # 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
113 SEEK_SET = 0
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
114 SEEK_CUR = 1
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
115 SEEK_END = 2
5bd2b3e4fdcc Added SEEK_* constants. Fixes #711830.
Martin v. Löwis <martin@v.loewis.de>
parents: 33720
diff changeset
116
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
117
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
118 def _get_masked_mode(mode):
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
119 mask = umask(0)
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
120 umask(mask)
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
121 return mode & ~mask
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
122
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
123 #'
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
124
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
125 # Super directory utilities.
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
126 # (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
127
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
128 def makedirs(name, mode=0o777, exist_ok=False):
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
129 """makedirs(path [, mode=0o777][, exist_ok=False])
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
130
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
131 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
132 Works like mkdir, except that any intermediate path segment (not
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
133 just the rightmost) will be created if it does not exist. If the
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
134 target directory with the same mode as we specified already exists,
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
135 raises an OSError if exist_ok is False, otherwise no exception is
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
136 raised. This is recursive.
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
137
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 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
140 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
141 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
142 if head and tail and not path.exists(head):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
143 try:
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
144 makedirs(head, mode, exist_ok)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
145 except OSError as e:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
146 # be happy if someone already created the path
44036
45ced6ab066d Merged revisions 59193-59201 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
147 if e.errno != errno.EEXIST:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
148 raise
31253
c1402176e1a6 [Bug #829532] Invoking os.makedirs() with an argument that contains a
Andrew M. Kuchling <amk@amk.ca>
parents: 31067
diff changeset
149 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
150 return
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
151 try:
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
152 mkdir(name, mode)
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
153 except OSError as e:
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
154 import stat as st
77329
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
155 dir_exists = path.isdir(name)
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
156 expected_mode = _get_masked_mode(mode)
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
157 if dir_exists:
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
158 # S_ISGID is automatically copied by the OS from parent to child
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
159 # directories on mkdir. Don't consider it being set to be a mode
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
160 # mismatch as mkdir does not unset it when not specified in mode.
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
161 actual_mode = st.S_IMODE(lstat(name).st_mode) & ~st.S_ISGID
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
162 else:
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
163 actual_mode = -1
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
164 if not (e.errno == errno.EEXIST and exist_ok and dir_exists and
fef529f3de5b Fixes Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
Gregory P. Smith <greg@krypto.org>
parents: 75073
diff changeset
165 actual_mode == expected_mode):
66601
89cda0833ba6 Issue 9299 Add exist_ok parameter to os.makedirs to suppress 'File exists' exception. Patch by Ray Allen.
Terry Reedy <tjreedy@udel.edu>
parents: 66067
diff changeset
166 raise
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
167
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
168 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
169 """removedirs(path)
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
170
35859
6015df136b00 SF#1343671.
Fredrik Lundh <fredrik@pythonware.com>
parents: 35203
diff changeset
171 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
172 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
173 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
174 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
175 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
176 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
177
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
178 """
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
179 rmdir(name)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
180 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
181 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
182 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
183 while head and tail:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
184 try:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
185 rmdir(head)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
186 except error:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
187 break
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
188 head, tail = path.split(head)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
189
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
190 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
191 """renames(old, new)
8744
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
192
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
193 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
194 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
195 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
196 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
197 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
198 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
199
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
200 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
201 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
202 file.
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
203
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
204 """
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
205 head, tail = path.split(new)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
206 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
207 makedirs(head)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
208 rename(old, new)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
209 head, tail = path.split(old)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
210 if head and tail:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
211 try:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
212 removedirs(head)
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
213 except error:
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
214 pass
1d581a6dfeee Add makedirs(), removedirs(), renames() -- ESR-inspired super-versions
Guido van Rossum <guido@python.org>
parents: 8413
diff changeset
215
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
216 __all__.extend(["makedirs", "removedirs", "renames"])
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
217
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
218 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
219 """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
220
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 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
222 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
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 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
225
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 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
227 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
228 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
229 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
230 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
231 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
232
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 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
234 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
235 (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
236 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
237 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
238
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
239 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
240 (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
241 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
242 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
243 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
244 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
245 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
246
29106
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
247 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
248 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
249 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
250 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
251 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
252 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
253
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
254 By default, os.walk does not follow symbolic links to subdirectories on
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
255 systems that support them. In order to get this functionality, set the
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
256 optional argument 'followlinks' to true.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
257
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
258 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
259 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
260 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
261 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
262
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 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
264
46690
c82f63efbc40 Merged revisions 62713,62715,62728,62737,62740,62744,62749,62756 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44036
diff changeset
265 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
266 from os.path import join, getsize
46690
c82f63efbc40 Merged revisions 62713,62715,62728,62737,62740,62744,62749,62756 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44036
diff changeset
267 for root, dirs, files in os.walk('python/Lib/email'):
46978
55d8c35322a7 Convert a lot of print statements to print functions in docstrings,
Neal Norwitz <nnorwitz@gmail.com>
parents: 46867
diff changeset
268 print(root, "consumes", end="")
55d8c35322a7 Convert a lot of print statements to print functions in docstrings,
Neal Norwitz <nnorwitz@gmail.com>
parents: 46867
diff changeset
269 print(sum([getsize(join(root, name)) for name in files]), end="")
55d8c35322a7 Convert a lot of print statements to print functions in docstrings,
Neal Norwitz <nnorwitz@gmail.com>
parents: 46867
diff changeset
270 print("bytes in", len(files), "non-directory files")
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
271 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
272 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
273 """
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
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55197
diff changeset
275 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
276
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
277 # We may not have read permission for top, in which case we can't
46800
87d854bc1e45 Removed remnants of os.path.walk().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46690
diff changeset
278 # get a list of the files the directory contains. os.walk
28820
60e722384d62 New generator os.walk() does a bit more than os.path.walk() does, and
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
279 # 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
280 # 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
281 # 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
282 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
283 # 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
284 # 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
285 names = listdir(top)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
286 except error as err:
29106
91de3a0924a2 Add optional 'onerror' argument to os.walk(), to control error
Guido van Rossum <guido@python.org>
parents: 28820
diff changeset
287 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
288 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
289 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
290
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 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
292 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
293 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
294 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
295 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
296 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
297
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 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
299 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
300 for name in dirs:
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55197
diff changeset
301 new_path = join(top, name)
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55197
diff changeset
302 if followlinks or not islink(new_path):
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55197
diff changeset
303 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
304 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
305 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
306 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
307
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
308 __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
309
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
310 # 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
311 try:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
312 environ
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
313 except NameError:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
314 environ = {}
6204
24d668454cb0 Write out the dynamic OS choice, to avoid exec statements.
Guido van Rossum <guido@python.org>
parents: 3805
diff changeset
315
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
316 def execl(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
317 """execl(file, *args)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
318
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
319 Execute the executable file with argument list args, replacing the
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
320 current process. """
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
321 execv(file, args)
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
322
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
323 def execle(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
324 """execle(file, *args, env)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
325
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
326 Execute the executable file with argument list args and
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
327 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
328 env = args[-1]
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
329 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
330
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
331 def execlp(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
332 """execlp(file, *args)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
333
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
334 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
335 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
336 execvp(file, args)
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
337
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
338 def execlpe(file, *args):
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
339 """execlpe(file, *args, env)
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 and environment env, replacing the current
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14913
diff changeset
343 process. """
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
344 env = args[-1]
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
345 execvpe(file, args[:-1], env)
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
346
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
347 def execvp(file, args):
58616
981a39f35914 Merged revisions 77881 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents: 57622
diff changeset
348 """execvp(file, args)
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
349
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
350 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
351 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
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)
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
354
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
355 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
356 """execvpe(file, args, env)
12488
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
357
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
358 Execute the executable file (which is searched for along $PATH)
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
359 with argument list args and environment env , replacing the
8b330d163800 Michael Hudson:
Guido van Rossum <guido@python.org>
parents: 12434
diff changeset
360 current process.
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14913
diff changeset
361 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
362 _execvpe(file, args, env)
2539
534a97c400cc add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents: 1932
diff changeset
363
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
364 __all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
365
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
366 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
367 if env is not None:
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
368 exec_func = execve
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
369 argrest = (args, env)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
370 else:
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
371 exec_func = execv
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
372 argrest = (args,)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
373 env = environ
24577
a32cff0c75dc SF patch 590294: os._execvpe security fix (Zack Weinberg).
Guido van Rossum <guido@python.org>
parents: 24095
diff changeset
374
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
375 head, tail = path.split(file)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
376 if head:
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
377 exec_func(file, *argrest)
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
378 return
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41843
diff changeset
379 last_exc = saved_exc = None
25102
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
380 saved_tb = None
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
381 path_list = get_exec_path(env)
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
382 if name != 'nt':
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
383 file = fsencode(file)
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
384 path_list = map(fsencode, path_list)
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
385 for dir in path_list:
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
386 fullname = path.join(dir, file)
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
387 try:
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
388 exec_func(fullname, *argrest)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
389 except error as e:
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41843
diff changeset
390 last_exc = e
25102
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
391 tb = sys.exc_info()[2]
44036
45ced6ab066d Merged revisions 59193-59201 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
392 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
393 and saved_exc is None):
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
394 saved_exc = e
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
395 saved_tb = tb
8a16de0be111 Fix for SF bug 601077 by Zack Weinberg.
Guido van Rossum <guido@python.org>
parents: 24577
diff changeset
396 if saved_exc:
51746
c1bf037feefe Merged revisions 69754,69794 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50076
diff changeset
397 raise saved_exc.with_traceback(saved_tb)
c1bf037feefe Merged revisions 69754,69794 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50076
diff changeset
398 raise last_exc.with_traceback(tb)
16949
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16820
diff changeset
399
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
400
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
401 def get_exec_path(env=None):
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
402 """Returns the sequence of directories that will be searched for the
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
403 named executable (similar to a shell) when launching a process.
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
404
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
405 *env* must be an environment variable dict or None. If *env* is None,
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
406 os.environ will be used.
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
407 """
66027
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
408 # Use a local import instead of a global import to limit the number of
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
409 # modules loaded at startup: the os module is always loaded at startup by
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
410 # Python. It may also avoid a bootstrap issue.
65692
a56a2a803cd7 Issue #10210: os.get_exec_path() ignores BytesWarning warnings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65630
diff changeset
411 import warnings
a56a2a803cd7 Issue #10210: os.get_exec_path() ignores BytesWarning warnings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65630
diff changeset
412
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
413 if env is None:
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
414 env = environ
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
415
66067
418dcb1c45f7 os module: remove nonbreaking space in a comment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66027
diff changeset
416 # {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a
418dcb1c45f7 os module: remove nonbreaking space in a comment
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66027
diff changeset
417 # BytesWarning when using python -b or python -bb: ignore the warning
66027
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
418 with warnings.catch_warnings():
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
419 warnings.simplefilter("ignore", BytesWarning)
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
420
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
421 try:
66027
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
422 path_list = env.get('PATH')
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
423 except TypeError:
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
424 path_list = None
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
425
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
426 if supports_bytes_environ:
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
427 try:
65692
a56a2a803cd7 Issue #10210: os.get_exec_path() ignores BytesWarning warnings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65630
diff changeset
428 path_listb = env[b'PATH']
66027
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
429 except (KeyError, TypeError):
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
430 pass
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
431 else:
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
432 if path_list is not None:
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
433 raise ValueError(
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
434 "env cannot contain 'PATH' and b'PATH' keys")
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
435 path_list = path_listb
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
436
66027
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
437 if path_list is not None and isinstance(path_list, bytes):
f457c46f9098 os.get_exec_path() ignores BytesWarning instead of recoding them
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65692
diff changeset
438 path_list = fsdecode(path_list)
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
439
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
440 if path_list is None:
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
441 path_list = defpath
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
442 return path_list.split(pathsep)
59166
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
443
2b68e3142d17 Add an os.get_exec_path() function to return the list of directories
Gregory P. Smith <greg@mad-scientist.com>
parents: 59010
diff changeset
444
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
445 # Change environ to automatically call putenv(), unsetenv if they exist.
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
446 from _abcoll import MutableMapping # Can't use collections (bootstrap)
20551
c9608c790f6f SF patch #460805 by Chris Gonnerman: Support for unsetenv()
Guido van Rossum <guido@python.org>
parents: 17007
diff changeset
447
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
448 class _Environ(MutableMapping):
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
449 def __init__(self, data, encodekey, decodekey, encodevalue, decodevalue, putenv, unsetenv):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
450 self.encodekey = encodekey
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
451 self.decodekey = decodekey
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
452 self.encodevalue = encodevalue
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
453 self.decodevalue = decodevalue
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
454 self.putenv = putenv
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
455 self.unsetenv = unsetenv
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
456 self._data = data
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
457
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
458 def __getitem__(self, key):
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
459 value = self._data[self.encodekey(key)]
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
460 return self.decodevalue(value)
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
461
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
462 def __setitem__(self, key, value):
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
463 key = self.encodekey(key)
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
464 value = self.encodevalue(value)
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
465 self.putenv(key, value)
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
466 self._data[key] = value
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
467
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
468 def __delitem__(self, key):
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
469 key = self.encodekey(key)
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
470 self.unsetenv(key)
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
471 del self._data[key]
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
472
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
473 def __iter__(self):
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
474 for key in self._data:
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
475 yield self.decodekey(key)
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
476
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
477 def __len__(self):
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
478 return len(self._data)
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
479
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
480 def __repr__(self):
63276
d55d21c396c5 Issue #9283: Oops, add missing { and } to repr(os.environ)
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63275
diff changeset
481 return 'environ({{{}}})'.format(', '.join(
63275
0132e4ee9e7f #9283: Fix repr(os.environ), display unicode keys and values on POSIX systems
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62276
diff changeset
482 ('{!r}: {!r}'.format(self.decodekey(key), self.decodevalue(value))
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
483 for key, value in self._data.items())))
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
484
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
485 def copy(self):
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
486 return dict(self)
59011
5d7741e1e672 Merged revisions 78316 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58616
diff changeset
487
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
488 def setdefault(self, key, value):
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
489 if key not in self:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
490 self[key] = value
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
491 return self[key]
3805
4f73962aaba6 Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents: 2921
diff changeset
492
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
493 try:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
494 _putenv = putenv
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
495 except NameError:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
496 _putenv = lambda key, value: None
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
497 else:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
498 __all__.append("putenv")
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
499
42632
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
500 try:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
501 _unsetenv = unsetenv
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
502 except NameError:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
503 _unsetenv = lambda key: _putenv(key, "")
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
504 else:
48a31b0bf501 some RiscOS stuff I missed before (was only looking for "RISCOS")
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
505 __all__.append("unsetenv")
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
506
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
507 def _createenviron():
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
508 if name in ('os2', 'nt'):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
509 # Where Env Var Names Must Be UPPERCASE
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
510 def check_str(value):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
511 if not isinstance(value, str):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
512 raise TypeError("str expected, not %s" % type(value).__name__)
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
513 return value
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
514 encode = check_str
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
515 decode = str
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
516 def encodekey(key):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
517 return encode(key).upper()
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
518 data = {}
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
519 for key, value in environ.items():
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
520 data[encodekey(key)] = value
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
521 else:
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
522 # Where Env Var Names Can Be Mixed Case
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
523 encoding = sys.getfilesystemencoding()
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
524 def encode(value):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
525 if not isinstance(value, str):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
526 raise TypeError("str expected, not %s" % type(value).__name__)
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
527 return value.encode(encoding, 'surrogateescape')
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
528 def decode(value):
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
529 return value.decode(encoding, 'surrogateescape')
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
530 encodekey = encode
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
531 data = environ
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
532 return _Environ(data,
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
533 encodekey, decode,
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
534 encode, decode,
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
535 _putenv, _unsetenv)
20765
3a07bd2b0386 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
536
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
537 # unicode environ
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
538 environ = _createenviron()
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
539 del _createenviron
6760
0204d461d219 Reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 6690
diff changeset
540
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
541
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
542 def getenv(key, default=None):
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27174
diff changeset
543 """Get an environment variable, return None if it doesn't exist.
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
544 The optional second argument can specify an alternate default.
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
545 key, default and the result are str."""
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27174
diff changeset
546 return environ.get(key, default)
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
547
61693
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
548 supports_bytes_environ = name not in ('os2', 'nt')
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
549 __all__.extend(("getenv", "supports_bytes_environ"))
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
550
cf74ec3e1f82 Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61406
diff changeset
551 if supports_bytes_environ:
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
552 def _check_bytes(value):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
553 if not isinstance(value, bytes):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
554 raise TypeError("bytes expected, not %s" % type(value).__name__)
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
555 return value
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
556
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
557 # bytes environ
64625
e827b9b8a7b4 Issue #8603: Environ.data is now protected -> Environ._data
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64173
diff changeset
558 environb = _Environ(environ._data,
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
559 _check_bytes, bytes,
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
560 _check_bytes, bytes,
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
561 _putenv, _unsetenv)
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
562 del _check_bytes
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
563
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
564 def getenvb(key, default=None):
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
565 """Get an environment variable, return None if it doesn't exist.
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
566 The optional second argument can specify an alternate default.
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
567 key, default and the result are bytes."""
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
568 return environb.get(key, default)
63296
16189b7da3b6 #8603: Add environb to os.__all__
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63276
diff changeset
569
16189b7da3b6 #8603: Add environb to os.__all__
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63276
diff changeset
570 __all__.extend(("environb", "getenvb"))
61332
7e5bdf59a487 Issue #8603: Create a bytes version of os.environ for Unix
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60902
diff changeset
571
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
572 def _fscodec():
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
573 encoding = sys.getfilesystemencoding()
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
574 if encoding == 'mbcs':
65630
159530f0e406 str.encode() doesn't accept None as errors: use 'strict' instead
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65626
diff changeset
575 errors = 'strict'
62276
3022904f9971 Issue #8969: On Windows, use mbcs codec in strict mode to encode and decode
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61693
diff changeset
576 else:
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
577 errors = 'surrogateescape'
64163
2cb2663e3368 Create os.fsdecode(): decode from the filesystem encoding with surrogateescape
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63296
diff changeset
578
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
579 def fsencode(filename):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
580 """
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
581 Encode filename to the filesystem encoding with 'surrogateescape' error
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
582 handler, return bytes unchanged. On Windows, use 'strict' error handler if
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
583 the file system encoding is 'mbcs' (which is the default encoding).
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
584 """
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
585 if isinstance(filename, bytes):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
586 return filename
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
587 elif isinstance(filename, str):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
588 return filename.encode(encoding, errors)
64163
2cb2663e3368 Create os.fsdecode(): decode from the filesystem encoding with surrogateescape
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63296
diff changeset
589 else:
65626
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
590 raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
591
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
592 def fsdecode(filename):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
593 """
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
594 Decode filename from the filesystem encoding with 'surrogateescape' error
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
595 handler, return str unchanged. On Windows, use 'strict' error handler if
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
596 the file system encoding is 'mbcs' (which is the default encoding).
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
597 """
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
598 if isinstance(filename, str):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
599 return filename
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
600 elif isinstance(filename, bytes):
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
601 return filename.decode(encoding, errors)
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
602 else:
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
603 raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
604
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
605 return fsencode, fsdecode
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
606
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
607 fsencode, fsdecode = _fscodec()
1908a146e961 os: fsencode(), fsdecode() and os.environ(b) internal encode-decode methods
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64625
diff changeset
608 del _fscodec
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
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 _exists(name):
54703
6370d71e3ca3 Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51746
diff changeset
611 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
612
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
613 # 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
614 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
615
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
616 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
617 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
618
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 # 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
620 # 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
621 # 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
622
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
623 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
624 # 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
625 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
626 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
627 # 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
628 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
629 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
630 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
631 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
632 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
633 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
634 _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
635 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
636 # 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
637 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
638 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
639 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
640 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
641 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
642 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
643 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
644 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
645 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
646 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
647 else:
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42632
diff changeset
648 raise error("Not stopped, signaled or exited???")
11427
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
649
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
650 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
651 """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
652
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
653 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
654 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
655 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
656 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
657 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
658
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
659 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
660 """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
661
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
662 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
663 specified environment.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
664 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
665 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
666 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
667 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
668
11431
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
669 # 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
670
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
671 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
672 """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
673
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
674 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
675 args in a subprocess.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
676 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
677 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
678 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
679 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
680
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
681 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
682 """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
683
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
684 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
685 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
686 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
687 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
688 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
689 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
690
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
691 if _exists("spawnv"):
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
692 # 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
693 # 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
694
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
695 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
696 """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
697
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
698 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
699 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
700 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
701 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
702 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
703
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
704 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
705 """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
706
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
707 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
708 supplied environment.
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
709 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
710 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
711 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
712 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
713 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
714
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
715
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
716 __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
717
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
718
11431
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
719 if _exists("spawnvp"):
a316cf9f81a7 Oops. spawnl() and spawnle() should be implemented on Windows too.
Guido van Rossum <guido@python.org>
parents: 11427
diff changeset
720 # 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
721 # 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
722 def spawnlp(mode, file, *args):
29732
1247f84aa6a6 SF #764121, docstring for spawnlp incorrect
Neal Norwitz <nnorwitz@gmail.com>
parents: 29106
diff changeset
723 """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
724
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
725 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
726 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
727 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
728 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
729 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
730 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
731
98997aa49164 Checking in a bunch of spawn functions. These are only defined if we
Guido van Rossum <guido@python.org>
parents: 10971
diff changeset
732 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
733 """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
734
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
735 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
736 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
737 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
738 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
739 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
740 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
741 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
742
6a8cd0682096 Doc strings for the spawn* functions, by Michael Hudson.
Guido van Rossum <guido@python.org>
parents: 12232
diff changeset
743
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
744 __all__.extend(["spawnvp", "spawnvpe", "spawnlp", "spawnlpe",])
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
745
46867
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46800
diff changeset
746 import copyreg as _copyreg
22154
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
747
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
748 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
749 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
750
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
751 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
752 (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
753 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
754
22291
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
755 try:
46867
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46800
diff changeset
756 _copyreg.pickle(stat_result, _pickle_stat_result, _make_stat_result)
22291
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
757 except NameError: # stat_result may not exist
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
758 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
759
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
760 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
761 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
762
e81a4e99ecfa Special support for pickling os.stat and os.stat_vfs results portably
Michael W. Hudson <mwh@python.net>
parents: 22013
diff changeset
763 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
764 (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
765 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
766
22291
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
767 try:
46867
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46800
diff changeset
768 _copyreg.pickle(statvfs_result, _pickle_statvfs_result,
22291
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
769 _make_statvfs_result)
22292
Michael W. Hudson <mwh@python.net>
parents: 22291
diff changeset
770 except NameError: # statvfs_result may not exist
22291
Michael W. Hudson <mwh@python.net>
parents: 22154
diff changeset
771 pass
33317
cf0ea19869af Patch #934711: Expose platform-specific entropy.
Martin v. Löwis <martin@v.loewis.de>
parents: 32302
diff changeset
772
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
773 # Supply os.popen()
68679
1dc52ecb8949 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 60975
diff changeset
774 def popen(cmd, mode="r", buffering=-1):
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43227
diff changeset
775 if not isinstance(cmd, str):
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
776 raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
777 if mode not in ("r", "w"):
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
778 raise ValueError("invalid mode %r" % mode)
68679
1dc52ecb8949 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 60975
diff changeset
779 if buffering == 0 or buffering == None:
1dc52ecb8949 Issue #11459: A `bufsize` value of 0 in subprocess.Popen() really creates
Antoine Pitrou <solipsis@pitrou.net>
parents: 60975
diff changeset
780 raise ValueError("popen() does not support unbuffered streams")
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
781 import subprocess, io
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
782 if mode == "r":
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
783 proc = subprocess.Popen(cmd,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
784 shell=True,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
785 stdout=subprocess.PIPE,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
786 bufsize=buffering)
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
787 return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
788 else:
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
789 proc = subprocess.Popen(cmd,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
790 shell=True,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
791 stdin=subprocess.PIPE,
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
792 bufsize=buffering)
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
793 return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
794
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
795 # Helper for popen() -- a proxy for a file whose close waits for the process
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
796 class _wrap_close:
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
797 def __init__(self, stream, proc):
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
798 self._stream = stream
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
799 self._proc = proc
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
800 def close(self):
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
801 self._stream.close()
55197
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
802 returncode = self._proc.wait()
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
803 if returncode == 0:
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
804 return None
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
805 if name == 'nt':
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
806 return returncode
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
807 else:
e536a3b3a8d0 Merged revisions 73934 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54703
diff changeset
808 return returncode << 8 # Shift left to match old behavior
57622
ccc5406cb381 Merged revisions 76723 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55197
diff changeset
809 def __enter__(self):
ccc5406cb381 Merged revisions 76723 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55197
diff changeset
810 return self
ccc5406cb381 Merged revisions 76723 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55197
diff changeset
811 def __exit__(self, *args):
ccc5406cb381 Merged revisions 76723 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55197
diff changeset
812 self.close()
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
813 def __getattr__(self, name):
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
814 return getattr(self._stream, name)
43227
c9ac5b8e4a81 Make the _wrap_close type (which is returned by os.popen) iterable.
Thomas Heller <theller@ctypes.org>
parents: 43102
diff changeset
815 def __iter__(self):
c9ac5b8e4a81 Make the _wrap_close type (which is returned by os.popen) iterable.
Thomas Heller <theller@ctypes.org>
parents: 43102
diff changeset
816 return iter(self._stream)
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
817
48599
631cd9ae5c8e #2491: os.fdopen() is now almost an alias to the builtin open(), and accepts the same parameters.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47080
diff changeset
818 # Supply os.fdopen()
631cd9ae5c8e #2491: os.fdopen() is now almost an alias to the builtin open(), and accepts the same parameters.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47080
diff changeset
819 def fdopen(fd, *args, **kwargs):
41843
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
820 if not isinstance(fd, int):
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
821 raise TypeError("invalid fd type (%s, expected integer)" % type(fd))
1f7891d84d93 Remove native popen() and fdopen(), replacing them with subprocess calls.
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
822 import io
48599
631cd9ae5c8e #2491: os.fdopen() is now almost an alias to the builtin open(), and accepts the same parameters.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47080
diff changeset
823 return io.open(fd, *args, **kwargs)