mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-136066: simplify platform._platform()
(#136069)
This commit is contained in:
parent
30ba03ea8e
commit
bd928a3035
2 changed files with 21 additions and 17 deletions
|
@ -612,6 +612,9 @@ def system_alias(system, release, version):
|
||||||
|
|
||||||
### Various internal helpers
|
### Various internal helpers
|
||||||
|
|
||||||
|
# Table for cleaning up characters in filenames.
|
||||||
|
_SIMPLE_SUBSTITUTIONS = str.maketrans(r' /\:;"()', r'_-------')
|
||||||
|
|
||||||
def _platform(*args):
|
def _platform(*args):
|
||||||
|
|
||||||
""" Helper to format the platform string in a filename
|
""" Helper to format the platform string in a filename
|
||||||
|
@ -621,28 +624,13 @@ def _platform(*args):
|
||||||
platform = '-'.join(x.strip() for x in filter(len, args))
|
platform = '-'.join(x.strip() for x in filter(len, args))
|
||||||
|
|
||||||
# Cleanup some possible filename obstacles...
|
# Cleanup some possible filename obstacles...
|
||||||
platform = platform.replace(' ', '_')
|
platform = platform.translate(_SIMPLE_SUBSTITUTIONS)
|
||||||
platform = platform.replace('/', '-')
|
|
||||||
platform = platform.replace('\\', '-')
|
|
||||||
platform = platform.replace(':', '-')
|
|
||||||
platform = platform.replace(';', '-')
|
|
||||||
platform = platform.replace('"', '-')
|
|
||||||
platform = platform.replace('(', '-')
|
|
||||||
platform = platform.replace(')', '-')
|
|
||||||
|
|
||||||
# No need to report 'unknown' information...
|
# No need to report 'unknown' information...
|
||||||
platform = platform.replace('unknown', '')
|
platform = platform.replace('unknown', '')
|
||||||
|
|
||||||
# Fold '--'s and remove trailing '-'
|
# Fold '--'s and remove trailing '-'
|
||||||
while True:
|
return re.sub(r'-{2,}', '-', platform).rstrip('-')
|
||||||
cleaned = platform.replace('--', '-')
|
|
||||||
if cleaned == platform:
|
|
||||||
break
|
|
||||||
platform = cleaned
|
|
||||||
while platform and platform[-1] == '-':
|
|
||||||
platform = platform[:-1]
|
|
||||||
|
|
||||||
return platform
|
|
||||||
|
|
||||||
def _node(default=''):
|
def _node(default=''):
|
||||||
|
|
||||||
|
|
|
@ -133,6 +133,22 @@ class PlatformTest(unittest.TestCase):
|
||||||
for terse in (False, True):
|
for terse in (False, True):
|
||||||
res = platform.platform(aliased, terse)
|
res = platform.platform(aliased, terse)
|
||||||
|
|
||||||
|
def test__platform(self):
|
||||||
|
for src, res in [
|
||||||
|
('foo bar', 'foo_bar'),
|
||||||
|
(
|
||||||
|
'1/2\\3:4;5"6(7)8(7)6"5;4:3\\2/1',
|
||||||
|
'1-2-3-4-5-6-7-8-7-6-5-4-3-2-1'
|
||||||
|
),
|
||||||
|
('--', ''),
|
||||||
|
('-f', '-f'),
|
||||||
|
('-foo----', '-foo'),
|
||||||
|
('--foo---', '-foo'),
|
||||||
|
('---foo--', '-foo'),
|
||||||
|
]:
|
||||||
|
with self.subTest(src=src):
|
||||||
|
self.assertEqual(platform._platform(src), res)
|
||||||
|
|
||||||
def test_system(self):
|
def test_system(self):
|
||||||
res = platform.system()
|
res = platform.system()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue