Skip to content

Commit 77939d2

Browse files
authored
Update platform from CPython 3.11.5 (RustPython#5060)
* Update platform and test from CPython 3.11.5 * sys.dllhandle (=0) * Unmark fixed test of test_sysconfig --------- Co-authored-by: CPython Developers <>
1 parent 9cf18a8 commit 77939d2

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

Lib/platform.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
If called from the command line, it prints the platform
77
information concatenated as single string to stdout. The output
8-
format is useable as part of a filename.
8+
format is usable as part of a filename.
99
1010
"""
1111
# This module is maintained by Marc-Andre Lemburg <[email protected]>.
@@ -116,7 +116,6 @@
116116
import os
117117
import re
118118
import sys
119-
import subprocess
120119
import functools
121120
import itertools
122121

@@ -169,7 +168,7 @@ def libc_ver(executable=None, lib='', version='', chunksize=16384):
169168
170169
Note that the function has intimate knowledge of how different
171170
libc versions add symbols to the executable and thus is probably
172-
only useable for executables compiled using gcc.
171+
only usable for executables compiled using gcc.
173172
174173
The file is read and scanned in chunks of chunksize bytes.
175174
@@ -187,12 +186,15 @@ def libc_ver(executable=None, lib='', version='', chunksize=16384):
187186

188187
executable = sys.executable
189188

189+
if not executable:
190+
# sys.executable is not set.
191+
return lib, version
192+
190193
V = _comparable_version
191-
if hasattr(os.path, 'realpath'):
192-
# Python 2.2 introduced os.path.realpath(); it is used
193-
# here to work around problems with Cygwin not being
194-
# able to open symlinks for reading
195-
executable = os.path.realpath(executable)
194+
# We use os.path.realpath()
195+
# here to work around problems with Cygwin not being
196+
# able to open symlinks for reading
197+
executable = os.path.realpath(executable)
196198
with open(executable, 'rb') as f:
197199
binary = f.read(chunksize)
198200
pos = 0
@@ -283,6 +285,7 @@ def _syscmd_ver(system='', release='', version='',
283285
stdin=subprocess.DEVNULL,
284286
stderr=subprocess.DEVNULL,
285287
text=True,
288+
encoding="locale",
286289
shell=True)
287290
except (OSError, subprocess.CalledProcessError) as why:
288291
#print('Command %s failed: %s' % (cmd, why))
@@ -609,7 +612,10 @@ def _syscmd_file(target, default=''):
609612
# XXX Others too ?
610613
return default
611614

612-
import subprocess
615+
try:
616+
import subprocess
617+
except ImportError:
618+
return default
613619
target = _follow_symlinks(target)
614620
# "file" output is locale dependent: force the usage of the C locale
615621
# to get deterministic behavior.
@@ -748,11 +754,16 @@ def from_subprocess():
748754
"""
749755
Fall back to `uname -p`
750756
"""
757+
try:
758+
import subprocess
759+
except ImportError:
760+
return None
751761
try:
752762
return subprocess.check_output(
753763
['uname', '-p'],
754764
stderr=subprocess.DEVNULL,
755765
text=True,
766+
encoding="utf8",
756767
).strip()
757768
except (OSError, subprocess.CalledProcessError):
758769
pass
@@ -776,6 +787,8 @@ class uname_result(
776787
except when needed.
777788
"""
778789

790+
_fields = ('system', 'node', 'release', 'version', 'machine', 'processor')
791+
779792
@functools.cached_property
780793
def processor(self):
781794
return _unknown_as_blank(_Processor.get())
@@ -789,7 +802,7 @@ def __iter__(self):
789802
@classmethod
790803
def _make(cls, iterable):
791804
# override factory to affect length check
792-
num_fields = len(cls._fields)
805+
num_fields = len(cls._fields) - 1
793806
result = cls.__new__(cls, *iterable)
794807
if len(result) != num_fields + 1:
795808
msg = f'Expected {num_fields} arguments, got {len(result)}'
@@ -803,7 +816,7 @@ def __len__(self):
803816
return len(tuple(iter(self)))
804817

805818
def __reduce__(self):
806-
return uname_result, tuple(self)[:len(self._fields)]
819+
return uname_result, tuple(self)[:len(self._fields) - 1]
807820

808821

809822
_uname_cache = None

Lib/test/test_platform.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def clear_caches(self):
7878
def test_architecture(self):
7979
res = platform.architecture()
8080

81-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
8281
@os_helper.skip_unless_symlink
82+
@support.requires_subprocess()
8383
def test_architecture_via_symlink(self): # issue3762
8484
with support.PythonSymlink() as py:
8585
cmd = "-c", "import platform; print(platform.architecture())"
@@ -269,7 +269,16 @@ def test_uname_slices(self):
269269
self.assertEqual(res[:], expected)
270270
self.assertEqual(res[:5], expected[:5])
271271

272+
def test_uname_fields(self):
273+
self.assertIn('processor', platform.uname()._fields)
274+
275+
def test_uname_asdict(self):
276+
res = platform.uname()._asdict()
277+
self.assertEqual(len(res), 6)
278+
self.assertIn('processor', res)
279+
272280
@unittest.skipIf(sys.platform in ['win32', 'OpenVMS'], "uname -p not used")
281+
@support.requires_subprocess()
273282
def test_uname_processor(self):
274283
"""
275284
On some systems, the processor must match the output
@@ -346,6 +355,7 @@ def test_mac_ver(self):
346355
else:
347356
self.assertEqual(res[2], 'PowerPC')
348357

358+
349359
@unittest.skipUnless(sys.platform == 'darwin', "OSX only test")
350360
def test_mac_ver_with_fork(self):
351361
# Issue7895: platform.mac_ver() crashes when using fork without exec
@@ -362,6 +372,7 @@ def test_mac_ver_with_fork(self):
362372
# parent
363373
support.wait_process(pid, exitcode=0)
364374

375+
@unittest.skipIf(support.is_emscripten, "Does not apply to Emscripten")
365376
def test_libc_ver(self):
366377
# check that libc_ver(executable) doesn't raise an exception
367378
if os.path.isdir(sys.executable) and \

Lib/test/test_sysconfig.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ def test_get_scheme_names(self):
346346
wanted.extend(['nt_user', 'osx_framework_user', 'posix_user'])
347347
self.assertEqual(get_scheme_names(), tuple(sorted(wanted)))
348348

349-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
350349
@skip_unless_symlink
351350
@requires_subprocess()
352351
def test_symlink(self): # Issue 7880

vm/src/stdlib/sys.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,15 @@ mod sys {
7171
const PS1: &str = ">>>>> ";
7272
#[pyattr(name = "ps2")]
7373
const PS2: &str = "..... ";
74+
7475
#[cfg(windows)]
7576
#[pyattr(name = "_vpath")]
7677
const VPATH: Option<&'static str> = None; // TODO: actual VPATH value
7778

79+
#[cfg(windows)]
80+
#[pyattr(name = "dllhandle")]
81+
const DLLHANDLE: usize = 0;
82+
7883
#[pyattr]
7984
fn default_prefix(_vm: &VirtualMachine) -> &'static str {
8085
// TODO: the windows one doesn't really make sense

0 commit comments

Comments
 (0)