Skip to content

Commit 5c9d6d4

Browse files
CPython Developersyouknowone
authored andcommitted
Update genericpath/posixpath from CPython 3.12.2
1 parent 8f6cf6f commit 5c9d6d4

4 files changed

Lines changed: 221 additions & 98 deletions

File tree

‎Lib/genericpath.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import stat
1111

1212
__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
13-
'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile',
13+
'getsize', 'isdir', 'isfile', 'islink', 'samefile', 'sameopenfile',
1414
'samestat']
1515

1616

@@ -48,6 +48,18 @@ def isdir(s):
4848
return stat.S_ISDIR(st.st_mode)
4949

5050

51+
# Is a path a symbolic link?
52+
# This will always return false on systems where os.lstat doesn't exist.
53+
54+
def islink(path):
55+
"""Test whether a path is a symbolic link"""
56+
try:
57+
st = os.lstat(path)
58+
except (OSError, ValueError, AttributeError):
59+
return False
60+
return stat.S_ISLNK(st.st_mode)
61+
62+
5163
def getsize(filename):
5264
"""Return the size of a file, reported by os.stat()."""
5365
return os.stat(filename).st_size

‎Lib/posixpath.py‎

Lines changed: 91 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,20 @@
2222
altsep = None
2323
devnull = '/dev/null'
2424

25-
try:
26-
import os
27-
except ImportError:
28-
import _dummy_os as os
25+
import os
2926
import sys
3027
import stat
3128
import genericpath
3229
from genericpath import *
3330

34-
__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
31+
__all__ = ["normcase","isabs","join","splitdrive","splitroot","split","splitext",
3532
"basename","dirname","commonprefix","getsize","getmtime",
3633
"getatime","getctime","islink","exists","lexists","isdir","isfile",
3734
"ismount", "expanduser","expandvars","normpath","abspath",
3835
"samefile","sameopenfile","samestat",
3936
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
4037
"devnull","realpath","supports_unicode_filenames","relpath",
41-
"commonpath"]
38+
"commonpath", "isjunction"]
4239

4340

4441
def _get_sep(path):
@@ -138,6 +135,35 @@ def splitdrive(p):
138135
return p[:0], p
139136

140137

138+
def splitroot(p):
139+
"""Split a pathname into drive, root and tail. On Posix, drive is always
140+
empty; the root may be empty, a single slash, or two slashes. The tail
141+
contains anything after the root. For example:
142+
143+
splitroot('foo/bar') == ('', '', 'foo/bar')
144+
splitroot('/foo/bar') == ('', '/', 'foo/bar')
145+
splitroot('//foo/bar') == ('', '//', 'foo/bar')
146+
splitroot('///foo/bar') == ('', '/', '//foo/bar')
147+
"""
148+
p = os.fspath(p)
149+
if isinstance(p, bytes):
150+
sep = b'/'
151+
empty = b''
152+
else:
153+
sep = '/'
154+
empty = ''
155+
if p[:1] != sep:
156+
# Relative path, e.g.: 'foo'
157+
return empty, empty, p
158+
elif p[1:2] != sep or p[2:3] == sep:
159+
# Absolute path, e.g.: '/foo', '///foo', '////foo', etc.
160+
return empty, sep, p[1:]
161+
else:
162+
# Precisely two leading slashes, e.g.: '//foo'. Implementation defined per POSIX, see
163+
# https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
164+
return empty, p[:2], p[2:]
165+
166+
141167
# Return the tail (basename) part of a path, same as split(path)[1].
142168

143169
def basename(p):
@@ -161,16 +187,14 @@ def dirname(p):
161187
return head
162188

163189

164-
# Is a path a symbolic link?
165-
# This will always return false on systems where os.lstat doesn't exist.
190+
# Is a path a junction?
191+
192+
def isjunction(path):
193+
"""Test whether a path is a junction
194+
Junctions are not a part of posix semantics"""
195+
os.fspath(path)
196+
return False
166197

167-
def islink(path):
168-
"""Test whether a path is a symbolic link"""
169-
try:
170-
st = os.lstat(path)
171-
except (OSError, ValueError, AttributeError):
172-
return False
173-
return stat.S_ISLNK(st.st_mode)
174198

175199
# Being true for dangling symbolic links is also useful.
176200

@@ -198,6 +222,7 @@ def ismount(path):
198222
if stat.S_ISLNK(s1.st_mode):
199223
return False
200224

225+
path = os.fspath(path)
201226
if isinstance(path, bytes):
202227
parent = join(path, b'..')
203228
else:
@@ -244,7 +269,11 @@ def expanduser(path):
244269
i = len(path)
245270
if i == 1:
246271
if 'HOME' not in os.environ:
247-
import pwd
272+
try:
273+
import pwd
274+
except ImportError:
275+
# pwd module unavailable, return path unchanged
276+
return path
248277
try:
249278
userhome = pwd.getpwuid(os.getuid()).pw_dir
250279
except KeyError:
@@ -254,7 +283,11 @@ def expanduser(path):
254283
else:
255284
userhome = os.environ['HOME']
256285
else:
257-
import pwd
286+
try:
287+
import pwd
288+
except ImportError:
289+
# pwd module unavailable, return path unchanged
290+
return path
258291
name = path[1:i]
259292
if isinstance(name, bytes):
260293
name = str(name, 'ASCII')
@@ -337,43 +370,47 @@ def expandvars(path):
337370
# It should be understood that this may change the meaning of the path
338371
# if it contains symbolic links!
339372

340-
def normpath(path):
341-
"""Normalize path, eliminating double slashes, etc."""
342-
path = os.fspath(path)
343-
if isinstance(path, bytes):
344-
sep = b'/'
345-
empty = b''
346-
dot = b'.'
347-
dotdot = b'..'
348-
else:
349-
sep = '/'
350-
empty = ''
351-
dot = '.'
352-
dotdot = '..'
353-
if path == empty:
354-
return dot
355-
initial_slashes = path.startswith(sep)
356-
# POSIX allows one or two initial slashes, but treats three or more
357-
# as single slash.
358-
# (see http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13)
359-
if (initial_slashes and
360-
path.startswith(sep*2) and not path.startswith(sep*3)):
361-
initial_slashes = 2
362-
comps = path.split(sep)
363-
new_comps = []
364-
for comp in comps:
365-
if comp in (empty, dot):
366-
continue
367-
if (comp != dotdot or (not initial_slashes and not new_comps) or
368-
(new_comps and new_comps[-1] == dotdot)):
369-
new_comps.append(comp)
370-
elif new_comps:
371-
new_comps.pop()
372-
comps = new_comps
373-
path = sep.join(comps)
374-
if initial_slashes:
375-
path = sep*initial_slashes + path
376-
return path or dot
373+
try:
374+
from posix import _path_normpath
375+
376+
except ImportError:
377+
def normpath(path):
378+
"""Normalize path, eliminating double slashes, etc."""
379+
path = os.fspath(path)
380+
if isinstance(path, bytes):
381+
sep = b'/'
382+
empty = b''
383+
dot = b'.'
384+
dotdot = b'..'
385+
else:
386+
sep = '/'
387+
empty = ''
388+
dot = '.'
389+
dotdot = '..'
390+
if path == empty:
391+
return dot
392+
_, initial_slashes, path = splitroot(path)
393+
comps = path.split(sep)
394+
new_comps = []
395+
for comp in comps:
396+
if comp in (empty, dot):
397+
continue
398+
if (comp != dotdot or (not initial_slashes and not new_comps) or
399+
(new_comps and new_comps[-1] == dotdot)):
400+
new_comps.append(comp)
401+
elif new_comps:
402+
new_comps.pop()
403+
comps = new_comps
404+
path = initial_slashes + sep.join(comps)
405+
return path or dot
406+
407+
else:
408+
def normpath(path):
409+
"""Normalize path, eliminating double slashes, etc."""
410+
path = os.fspath(path)
411+
if isinstance(path, bytes):
412+
return os.fsencode(_path_normpath(os.fsdecode(path))) or b"."
413+
return _path_normpath(path) or "."
377414

378415

379416
def abspath(path):

‎Lib/test/test_genericpath.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import unittest
99
import warnings
10+
from test.support import is_emscripten
1011
from test.support import os_helper
1112
from test.support import warnings_helper
1213
from test.support.script_helper import assert_python_ok
@@ -154,6 +155,7 @@ def test_exists(self):
154155
self.assertIs(self.pathmodule.lexists(bfilename + b'\x00'), False)
155156

156157
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
158+
@unittest.skipIf(is_emscripten, "Emscripten pipe fds have no stat")
157159
def test_exists_fd(self):
158160
r, w = os.pipe()
159161
try:
@@ -246,6 +248,7 @@ def _test_samefile_on_link_func(self, func):
246248
def test_samefile_on_symlink(self):
247249
self._test_samefile_on_link_func(os.symlink)
248250

251+
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
249252
def test_samefile_on_link(self):
250253
try:
251254
self._test_samefile_on_link_func(os.link)
@@ -288,6 +291,7 @@ def _test_samestat_on_link_func(self, func):
288291
def test_samestat_on_symlink(self):
289292
self._test_samestat_on_link_func(os.symlink)
290293

294+
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
291295
def test_samestat_on_link(self):
292296
try:
293297
self._test_samestat_on_link_func(os.link)
@@ -456,6 +460,10 @@ def test_normpath_issue5827(self):
456460
for path in ('', '.', '/', '\\', '///foo/.//bar//'):
457461
self.assertIsInstance(self.pathmodule.normpath(path), str)
458462

463+
def test_normpath_issue106242(self):
464+
for path in ('\x00', 'foo\x00bar', '\x00\x00', '\x00foo', 'foo\x00'):
465+
self.assertEqual(self.pathmodule.normpath(path), path)
466+
459467
def test_abspath_issue3426(self):
460468
# Check that abspath returns unicode when the arg is unicode
461469
# with both ASCII and non-ASCII cwds.
@@ -476,11 +484,11 @@ def test_abspath_issue3426(self):
476484

477485
def test_nonascii_abspath(self):
478486
if (os_helper.TESTFN_UNDECODABLE
479-
# Mac OS X denies the creation of a directory with an invalid
480-
# UTF-8 name. Windows allows creating a directory with an
487+
# macOS and Emscripten deny the creation of a directory with an
488+
# invalid UTF-8 name. Windows allows creating a directory with an
481489
# arbitrary bytes name, but fails to enter this directory
482490
# (when the bytes name is used).
483-
and sys.platform not in ('win32', 'darwin')):
491+
and sys.platform not in ('win32', 'darwin', 'emscripten', 'wasi')):
484492
name = os_helper.TESTFN_UNDECODABLE
485493
elif os_helper.TESTFN_NONASCII:
486494
name = os_helper.TESTFN_NONASCII

0 commit comments

Comments
 (0)