Skip to content

Commit

Permalink
Merge branch 'main' into pythongh-126211-fix-cases-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
mpage committed Oct 31, 2024
2 parents 07edacf + 951cb2c commit 1d9f67d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
7 changes: 1 addition & 6 deletions Lib/nturl2path.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,11 @@ def pathname2url(p):
if p[:4] == '\\\\?\\':
p = p[4:]
if p[:4].upper() == 'UNC\\':
p = '\\' + p[4:]
p = '\\\\' + p[4:]
elif p[1:2] != ':':
raise OSError('Bad path: ' + p)
if not ':' in p:
# No drive specifier, just convert slashes and quote the name
if p[:2] == '\\\\':
# path is something like \\host\path\on\remote\host
# convert this to ////host/path/on/remote/host
# (notice doubling of slashes at the start of the path)
p = '\\\\' + p
components = p.split('\\')
return urllib.parse.quote('/'.join(components))
comp = p.split(':', maxsplit=2)
Expand Down
25 changes: 17 additions & 8 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,8 @@ def __init__(self, name=None, mode="r", fileobj=None, format=None,
# current position in the archive file
self.inodes = {} # dictionary caching the inodes of
# archive members already added
self._unames = {} # Cached mappings of uid -> uname
self._gnames = {} # Cached mappings of gid -> gname

try:
if self.mode == "r":
Expand Down Expand Up @@ -2138,16 +2140,23 @@ def gettarinfo(self, name=None, arcname=None, fileobj=None):
tarinfo.mtime = statres.st_mtime
tarinfo.type = type
tarinfo.linkname = linkname

# Calls to pwd.getpwuid() and grp.getgrgid() tend to be expensive. To
# speed things up, cache the resolved usernames and group names.
if pwd:
try:
tarinfo.uname = pwd.getpwuid(tarinfo.uid)[0]
except KeyError:
pass
if tarinfo.uid not in self._unames:
try:
self._unames[tarinfo.uid] = pwd.getpwuid(tarinfo.uid)[0]
except KeyError:
self._unames[tarinfo.uid] = ''
tarinfo.uname = self._unames[tarinfo.uid]
if grp:
try:
tarinfo.gname = grp.getgrgid(tarinfo.gid)[0]
except KeyError:
pass
if tarinfo.gid not in self._gnames:
try:
self._gnames[tarinfo.gid] = grp.getgrgid(tarinfo.gid)[0]
except KeyError:
self._gnames[tarinfo.gid] = ''
tarinfo.gname = self._gnames[tarinfo.gid]

if type in (CHRTYPE, BLKTYPE):
if hasattr(os, "major") and hasattr(os, "minor"):
Expand Down
14 changes: 7 additions & 7 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ def test_pathname2url_win(self):
# Test special prefixes are correctly handled in pathname2url()
fn = urllib.request.pathname2url
self.assertEqual(fn('\\\\?\\C:\\dir'), '///C:/dir')
self.assertEqual(fn('\\\\?\\unc\\server\\share\\dir'), '/server/share/dir')
self.assertEqual(fn('\\\\?\\unc\\server\\share\\dir'), '//server/share/dir')
self.assertEqual(fn("C:"), '///C:')
self.assertEqual(fn("C:\\"), '///C:')
self.assertEqual(fn('C:\\a\\b.c'), '///C:/a/b.c')
Expand All @@ -1535,14 +1535,14 @@ def test_pathname2url_win(self):
self.assertRaises(IOError, fn, "XX:\\")
# No drive letter
self.assertEqual(fn("\\folder\\test\\"), '/folder/test/')
self.assertEqual(fn("\\\\folder\\test\\"), '////folder/test/')
self.assertEqual(fn("\\\\\\folder\\test\\"), '/////folder/test/')
self.assertEqual(fn('\\\\some\\share\\'), '////some/share/')
self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '////some/share/a/b.c')
self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), '////some/share/a/b%25%23c%C3%A9')
self.assertEqual(fn("\\\\folder\\test\\"), '//folder/test/')
self.assertEqual(fn("\\\\\\folder\\test\\"), '///folder/test/')
self.assertEqual(fn('\\\\some\\share\\'), '//some/share/')
self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '//some/share/a/b.c')
self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), '//some/share/a/b%25%23c%C3%A9')
# Round-tripping
urls = ['///C:',
'/////folder/test/',
'///folder/test/',
'///C:/foo/bar/spam.foo']
for url in urls:
self.assertEqual(fn(urllib.request.url2pathname(url)), url)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the performance of :mod:`tarfile` when writing files, by caching user names
and group names.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix issue where :func:`urllib.request.pathname2url` generated URLs beginning
with four slashes (rather than two) when given a Windows UNC path.

0 comments on commit 1d9f67d

Please sign in to comment.