Skip to content

Commit fe9418e

Browse files
committed
Fix clippy warnings and make ctypes snippet test platform-independent
1 parent 3d2f09a commit fe9418e

2 files changed

Lines changed: 29 additions & 20 deletions

File tree

‎crates/vm/src/stdlib/_ctypes/function.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,12 +2578,12 @@ raising = RaisingConverter()
25782578
let point_conv = scope.globals.get_item("point_conv", vm).unwrap();
25792579
let raising = scope.globals.get_item("raising", vm).unwrap();
25802580

2581-
let tuple_valid = vm.ctx.new_tuple(vec![valid.clone()]);
2581+
let tuple_valid = vm.ctx.new_tuple(vec![valid]);
25822582
let res = extract_arg_types_and_converters(tuple_valid.as_object(), vm);
25832583
assert!(res.is_ok());
25842584
let (types, converters) = res.unwrap();
25852585

2586-
let tuple_invalid = vm.ctx.new_tuple(vec![invalid.clone()]);
2586+
let tuple_invalid = vm.ctx.new_tuple(vec![invalid]);
25872587
let err = extract_arg_types_and_converters(tuple_invalid.as_object(), vm).unwrap_err();
25882588
assert!(
25892589
err.as_object()

‎extra_tests/snippets/stdlib_ctypes.py‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,11 @@ def __index__(self):
12791279

12801280

12811281
# Tests for argtypes validation and custom converters (Issue #8341)
1282+
@ctypes.CFUNCTYPE(None, ctypes.c_uint)
1283+
def test_cfunc(val):
1284+
pass
1285+
1286+
12821287
class ValidConverter:
12831288
called = 0
12841289

@@ -1309,53 +1314,56 @@ def from_param(cls, val):
13091314

13101315
# 1. Assignment validation: sequence item lacking from_param
13111316
try:
1312-
libc.srand.argtypes = [InvalidConverter]
1317+
test_cfunc.argtypes = [InvalidConverter]
13131318
except TypeError as e:
13141319
assert "item 1 in _argtypes_ has no from_param method" in str(e)
13151320
else:
13161321
raise AssertionError("expected TypeError for item without from_param")
13171322

13181323
# 2. Custom converter class in argtypes
1319-
libc.srand.argtypes = [ValidConverter]
1320-
libc.srand(10)
1324+
test_cfunc.argtypes = [ValidConverter]
1325+
test_cfunc(10)
13211326
assert ValidConverter.called == 1
13221327

13231328
# 3. Custom converter instance in argtypes
13241329
adapter = Adapter()
1325-
libc.srand.argtypes = [adapter]
1326-
libc.srand(20)
1330+
test_cfunc.argtypes = [adapter]
1331+
test_cfunc(20)
13271332
assert adapter.called == 1
13281333

13291334
# 4. Conversion failure wraps in ArgumentError and chains __cause__
1330-
libc.srand.argtypes = [FailingConverter]
1335+
test_cfunc.argtypes = [FailingConverter]
13311336
try:
1332-
libc.srand(30)
1337+
test_cfunc(30)
13331338
except Exception as e:
13341339
assert type(e).__name__ == "ArgumentError"
13351340
assert "argument 1: ValueError: failing conversion" in str(e)
1336-
assert isinstance(e.__cause__, ValueError)
1337-
assert str(e.__cause__) == "failing conversion"
1341+
if _sys.implementation.name == "rustpython":
1342+
assert isinstance(e.__cause__, ValueError)
1343+
assert str(e.__cause__) == "failing conversion"
13381344
else:
13391345
raise AssertionError("expected ArgumentError from failing converter")
13401346

1347+
13411348
# 5. Cached converter resolved at assignment (mutating from_param afterwards does not affect calls)
13421349
class MutatingConverter:
13431350
@classmethod
13441351
def from_param(cls, val):
13451352
return int(val)
13461353

13471354

1348-
libc.srand.argtypes = [MutatingConverter]
1355+
test_cfunc.argtypes = [MutatingConverter]
13491356
MutatingConverter.from_param = None
1350-
libc.srand(40)
1357+
test_cfunc(40)
13511358

13521359
# 6. In-place mutation of assigned argtypes list does not affect call snapshot
13531360
assigned_list = [ctypes.c_uint]
1354-
libc.srand.argtypes = assigned_list
1361+
test_cfunc.argtypes = assigned_list
13551362
assigned_list.append(ctypes.c_int)
13561363
# Function call uses assignment snapshot (1 argument), while getter preserves assigned list
1357-
libc.srand(50)
1358-
assert libc.srand.argtypes == assigned_list
1364+
test_cfunc(50)
1365+
assert test_cfunc.argtypes == assigned_list
1366+
13591367

13601368
# 7. Callback argtypes validates ctypes types without querying from_param
13611369
class CallbackArgType(ctypes.c_int):
@@ -1367,6 +1375,7 @@ def from_param(cls, val):
13671375
CB = ctypes.CFUNCTYPE(None, CallbackArgType)
13681376
cb_inst = CB(lambda x: None)
13691377

1378+
13701379
# 8. Prototype with class-level _argtypes_ resolves converters when prototype type is created
13711380
class ProtoConverter:
13721381
called = 0
@@ -1382,18 +1391,18 @@ def from_param(cls, val):
13821391
ProtoConverter.from_param = None
13831392

13841393
# Case A: constructed from function address
1385-
proto_func = Proto(ctypes.cast(libc.srand, ctypes.c_void_p).value)
1394+
proto_func = Proto(ctypes.cast(test_cfunc, ctypes.c_void_p).value)
13861395
proto_func(60)
13871396
assert ProtoConverter.called == 1
13881397

13891398
# Case B: constructed via ctypes.cast (into_view / from_cdata path)
1390-
proto_cast = ctypes.cast(libc.srand, Proto)
1399+
proto_cast = ctypes.cast(test_cfunc, Proto)
13911400
proto_cast(70)
13921401
assert ProtoConverter.called == 2
13931402

13941403
# 9. Reset argtypes
1395-
libc.srand.argtypes = [ctypes.c_uint]
1396-
libc.srand(0)
1404+
test_cfunc.argtypes = [ctypes.c_uint]
1405+
test_cfunc(0)
13971406

13981407
# 10. Windows-specific test for Issue #8341: custom converter returning a Structure
13991408
if _sys.platform == "win32":

0 commit comments

Comments
 (0)