Skip to content

Commit bd928a3

Browse files
authored
gh-136066: simplify platform._platform() (#136069)
1 parent 30ba03e commit bd928a3

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

Lib/platform.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,9 @@ def system_alias(system, release, version):
612612

613613
### Various internal helpers
614614

615+
# Table for cleaning up characters in filenames.
616+
_SIMPLE_SUBSTITUTIONS = str.maketrans(r' /\:;"()', r'_-------')
617+
615618
def _platform(*args):
616619

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

623626
# Cleanup some possible filename obstacles...
624-
platform = platform.replace(' ', '_')
625-
platform = platform.replace('/', '-')
626-
platform = platform.replace('\\', '-')
627-
platform = platform.replace(':', '-')
628-
platform = platform.replace(';', '-')
629-
platform = platform.replace('"', '-')
630-
platform = platform.replace('(', '-')
631-
platform = platform.replace(')', '-')
627+
platform = platform.translate(_SIMPLE_SUBSTITUTIONS)
632628

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

636632
# Fold '--'s and remove trailing '-'
637-
while True:
638-
cleaned = platform.replace('--', '-')
639-
if cleaned == platform:
640-
break
641-
platform = cleaned
642-
while platform and platform[-1] == '-':
643-
platform = platform[:-1]
644-
645-
return platform
633+
return re.sub(r'-{2,}', '-', platform).rstrip('-')
646634

647635
def _node(default=''):
648636

Lib/test/test_platform.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ def test_platform(self):
133133
for terse in (False, True):
134134
res = platform.platform(aliased, terse)
135135

136+
def test__platform(self):
137+
for src, res in [
138+
('foo bar', 'foo_bar'),
139+
(
140+
'1/2\\3:4;5"6(7)8(7)6"5;4:3\\2/1',
141+
'1-2-3-4-5-6-7-8-7-6-5-4-3-2-1'
142+
),
143+
('--', ''),
144+
('-f', '-f'),
145+
('-foo----', '-foo'),
146+
('--foo---', '-foo'),
147+
('---foo--', '-foo'),
148+
]:
149+
with self.subTest(src=src):
150+
self.assertEqual(platform._platform(src), res)
151+
136152
def test_system(self):
137153
res = platform.system()
138154

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy