Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4e460a

Browse files
committed
Avoid import of string module; it is only needed for expandvars().
Never assume that os.sep is for the module-specific platform; use the right separator character directly. Fix some minor style consistency nits.
1 parent c0ab93e commit b4e460a

3 files changed

Lines changed: 46 additions & 48 deletions

File tree

Lib/dospath.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44
import stat
5-
import string
65

76

87
def normcase(s):
@@ -15,7 +14,7 @@ def normcase(s):
1514
single '_', but this has been removed. This functionality should
1615
possibly be added as a new function."""
1716

18-
return string.lower(string.replace(s, "/", "\\"))
17+
return s.replace("/", "\\").lower()
1918

2019

2120
def isabs(s):
@@ -39,7 +38,7 @@ def join(a, *p):
3938
elif path == '' or path[-1:] in '/\\:':
4039
path = path + b
4140
else:
42-
path = path + os.sep + b
41+
path = path + "\\" + b
4342
return path
4443

4544

@@ -225,8 +224,6 @@ def expanduser(path):
225224
return userhome + path[i:]
226225

227226

228-
varchars = string.letters + string.digits + '_-'
229-
230227
def expandvars(path):
231228
"""Expand paths containing shell variable substitutions.
232229
The following rules apply:
@@ -239,6 +236,8 @@ def expandvars(path):
239236

240237
if '$' not in path:
241238
return path
239+
import string
240+
varchars = string.letters + string.digits + '_-'
242241
res = ''
243242
index = 0
244243
pathlen = len(path)
@@ -248,9 +247,9 @@ def expandvars(path):
248247
path = path[index + 1:]
249248
pathlen = len(path)
250249
try:
251-
index = string.index(path, '\'')
250+
index = path.index('\'')
252251
res = res + '\'' + path[:index + 1]
253-
except string.index_error:
252+
except ValueError:
254253
res = res + path
255254
index = pathlen -1
256255
elif c == '$': # variable or '$$'
@@ -261,11 +260,11 @@ def expandvars(path):
261260
path = path[index+2:]
262261
pathlen = len(path)
263262
try:
264-
index = string.index(path, '}')
263+
index = path.index('}')
265264
var = path[:index]
266265
if os.environ.has_key(var):
267266
res = res + os.environ[var]
268-
except string.index_error:
267+
except ValueError:
269268
res = res + path
270269
index = pathlen - 1
271270
else:
@@ -290,35 +289,35 @@ def normpath(path):
290289
"""Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
291290
Also, components of the path are silently truncated to 8+3 notation."""
292291

293-
path = string.replace(path, "/", "\\")
292+
path = path.replace("/", "\\")
294293
prefix, path = splitdrive(path)
295-
while path[:1] == os.sep:
296-
prefix = prefix + os.sep
294+
while path[:1] == "\\":
295+
prefix = prefix + "\\"
297296
path = path[1:]
298-
comps = string.splitfields(path, os.sep)
297+
comps = path.split("\\")
299298
i = 0
300299
while i < len(comps):
301300
if comps[i] == '.':
302301
del comps[i]
303302
elif comps[i] == '..' and i > 0 and \
304303
comps[i-1] not in ('', '..'):
305304
del comps[i-1:i+1]
306-
i = i-1
305+
i = i - 1
307306
elif comps[i] == '' and i > 0 and comps[i-1] <> '':
308307
del comps[i]
309308
elif '.' in comps[i]:
310-
comp = string.splitfields(comps[i], '.')
309+
comp = comps[i].split('.')
311310
comps[i] = comp[0][:8] + '.' + comp[1][:3]
312-
i = i+1
311+
i = i + 1
313312
elif len(comps[i]) > 8:
314313
comps[i] = comps[i][:8]
315-
i = i+1
314+
i = i + 1
316315
else:
317-
i = i+1
316+
i = i + 1
318317
# If the path is now empty, substitute '.'
319318
if not prefix and not comps:
320319
comps.append('.')
321-
return prefix + string.joinfields(comps, os.sep)
320+
return prefix + "\\".join(comps)
322321

323322

324323

Lib/macpath.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""Pathname and path-related operations for the Macintosh."""
22

3-
import string
43
import os
54
from stat import *
65

76

8-
# Normalize the case of a pathname. Dummy in Posix, but string.lower here.
7+
# Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
98

10-
normcase = string.lower
9+
def normcase(path):
10+
return path.lower()
1111

1212

1313
def isabs(s):
@@ -44,7 +44,7 @@ def split(s):
4444
if ':' not in s: return '', s
4545
colon = 0
4646
for i in range(len(s)):
47-
if s[i] == ':': colon = i+1
47+
if s[i] == ':': colon = i + 1
4848
path, file = s[:colon-1], s[colon:]
4949
if path and not ':' in path:
5050
path = path + ':'
@@ -175,20 +175,20 @@ def normpath(s):
175175
if ":" not in s:
176176
return ":"+s
177177

178-
comps = string.splitfields(s, ":")
178+
comps = s.split(":")
179179
i = 1
180180
while i < len(comps)-1:
181181
if comps[i] == "" and comps[i-1] != "":
182182
if i > 1:
183183
del comps[i-1:i+1]
184-
i = i-1
184+
i = i - 1
185185
else:
186186
# best way to handle this is to raise an exception
187187
raise norm_error, 'Cannot use :: immedeately after volume name'
188188
else:
189189
i = i + 1
190190

191-
s = string.join(comps, ":")
191+
s = ":".join(comps)
192192

193193
# remove trailing ":" except for ":" and "Volume:"
194194
if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):

Lib/ntpath.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import os
99
import stat
10-
import string
1110

1211

1312
# Normalize the case of a pathname and map slashes to backslashes.
@@ -18,7 +17,7 @@ def normcase(s):
1817
"""Normalize case of pathname.
1918
2019
Makes all characters lowercase and all slashes into backslashes."""
21-
return string.lower(string.replace(s, "/", "\\"))
20+
return s.replace("/", "\\").lower()
2221

2322

2423
# Return whether a path is absolute.
@@ -44,7 +43,7 @@ def join(a, *p):
4443
elif path == '' or path[-1:] in '/\\:':
4544
path = path + b
4645
else:
47-
path = path + os.sep + b
46+
path = path + "\\" + b
4847
return path
4948

5049

@@ -77,11 +76,11 @@ def splitunc(p):
7776
# \\machine\mountpoint\directories...
7877
# directory ^^^^^^^^^^^^^^^
7978
normp = normcase(p)
80-
index = string.find(normp, '\\', 2)
79+
index = normp.find('\\', 2)
8180
if index == -1:
8281
##raise RuntimeError, 'illegal UNC path: "' + p + '"'
8382
return ("", p)
84-
index = string.find(normp, '\\', index + 1)
83+
index = normp.find('\\', index + 1)
8584
if index == -1:
8685
index = len(p)
8786
return p[:index], p[index:]
@@ -241,7 +240,7 @@ def ismount(path):
241240
if unc:
242241
return rest in ("", "/", "\\")
243242
p = splitdrive(path)[1]
244-
return len(p)==1 and p[0] in '/\\'
243+
return len(p) == 1 and p[0] in '/\\'
245244

246245

247246
# Directory tree walk.
@@ -288,15 +287,15 @@ def expanduser(path):
288287
return path
289288
i, n = 1, len(path)
290289
while i < n and path[i] not in '/\\':
291-
i = i+1
290+
i = i + 1
292291
if i == 1:
293292
if os.environ.has_key('HOME'):
294293
userhome = os.environ['HOME']
295294
elif not os.environ.has_key('HOMEPATH'):
296295
return path
297296
else:
298297
try:
299-
drive=os.environ['HOMEDRIVE']
298+
drive = os.environ['HOMEDRIVE']
300299
except KeyError:
301300
drive = ''
302301
userhome = join(drive, os.environ['HOMEPATH'])
@@ -314,14 +313,14 @@ def expanduser(path):
314313
# XXX With COMMAND.COM you can use any characters in a variable name,
315314
# XXX except '^|<>='.
316315

317-
varchars = string.letters + string.digits + '_-'
318-
319316
def expandvars(path):
320317
"""Expand shell variables of form $var and ${var}.
321318
322319
Unknown variables are left unchanged."""
323320
if '$' not in path:
324321
return path
322+
import string
323+
varchars = string.letters + string.digits + '_-'
325324
res = ''
326325
index = 0
327326
pathlen = len(path)
@@ -331,11 +330,11 @@ def expandvars(path):
331330
path = path[index + 1:]
332331
pathlen = len(path)
333332
try:
334-
index = string.index(path, '\'')
333+
index = path.index('\'')
335334
res = res + '\'' + path[:index + 1]
336-
except string.index_error:
335+
except ValueError:
337336
res = res + path
338-
index = pathlen -1
337+
index = pathlen - 1
339338
elif c == '$': # variable or '$$'
340339
if path[index + 1:index + 2] == '$':
341340
res = res + c
@@ -344,11 +343,11 @@ def expandvars(path):
344343
path = path[index+2:]
345344
pathlen = len(path)
346345
try:
347-
index = string.index(path, '}')
346+
index = path.index('}')
348347
var = path[:index]
349348
if os.environ.has_key(var):
350349
res = res + os.environ[var]
351-
except string.index_error:
350+
except ValueError:
352351
res = res + path
353352
index = pathlen - 1
354353
else:
@@ -375,27 +374,27 @@ def expandvars(path):
375374

376375
def normpath(path):
377376
"""Normalize path, eliminating double slashes, etc."""
378-
path = string.replace(path, "/", "\\")
377+
path = path.replace("/", "\\")
379378
prefix, path = splitdrive(path)
380-
while path[:1] == os.sep:
381-
prefix = prefix + os.sep
379+
while path[:1] == "\\":
380+
prefix = prefix + "\\"
382381
path = path[1:]
383-
comps = string.splitfields(path, os.sep)
382+
comps = path.split("\\")
384383
i = 0
385384
while i < len(comps):
386385
if comps[i] == '.':
387386
del comps[i]
388387
elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
389388
del comps[i-1:i+1]
390-
i = i-1
389+
i = i - 1
391390
elif comps[i] == '' and i > 0 and comps[i-1] <> '':
392391
del comps[i]
393392
else:
394-
i = i+1
393+
i = i + 1
395394
# If the path is now empty, substitute '.'
396395
if not prefix and not comps:
397396
comps.append('.')
398-
return prefix + string.joinfields(comps, os.sep)
397+
return prefix + "\\".join(comps)
399398

400399

401400
# Return an absolute path.

0 commit comments

Comments
 (0)