Skip to content

gh-136066: simplify platform._platform() #136069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,9 @@ def system_alias(system, release, version):

### Various internal helpers

# Table for cleaning up characters in filenames.
_SIMPLE_SUBSTITUTIONS = str.maketrans(r' /\:;"()', r'_-------')

def _platform(*args):

""" Helper to format the platform string in a filename
Expand All @@ -621,28 +624,13 @@ def _platform(*args):
platform = '-'.join(x.strip() for x in filter(len, args))

# Cleanup some possible filename obstacles...
platform = platform.replace(' ', '_')
platform = platform.replace('/', '-')
platform = platform.replace('\\', '-')
platform = platform.replace(':', '-')
platform = platform.replace(';', '-')
platform = platform.replace('"', '-')
platform = platform.replace('(', '-')
platform = platform.replace(')', '-')
platform = platform.translate(_SIMPLE_SUBSTITUTIONS)

# No need to report 'unknown' information...
platform = platform.replace('unknown', '')

# Fold '--'s and remove trailing '-'
while True:
cleaned = platform.replace('--', '-')
if cleaned == platform:
break
platform = cleaned
while platform and platform[-1] == '-':
platform = platform[:-1]

return platform
return re.sub(r'-{2,}', '-', platform).rstrip('-')

def _node(default=''):

Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ def test_platform(self):
for terse in (False, True):
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):
res = platform.system()

Expand Down
Loading