@@ -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+
12821287class ValidConverter :
12831288 called = 0
12841289
@@ -1309,53 +1314,56 @@ def from_param(cls, val):
13091314
13101315# 1. Assignment validation: sequence item lacking from_param
13111316try :
1312- libc . srand .argtypes = [InvalidConverter ]
1317+ test_cfunc .argtypes = [InvalidConverter ]
13131318except TypeError as e :
13141319 assert "item 1 in _argtypes_ has no from_param method" in str (e )
13151320else :
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 )
13211326assert ValidConverter .called == 1
13221327
13231328# 3. Custom converter instance in argtypes
13241329adapter = Adapter ()
1325- libc . srand .argtypes = [adapter ]
1326- libc . srand (20 )
1330+ test_cfunc .argtypes = [adapter ]
1331+ test_cfunc (20 )
13271332assert adapter .called == 1
13281333
13291334# 4. Conversion failure wraps in ArgumentError and chains __cause__
1330- libc . srand .argtypes = [FailingConverter ]
1335+ test_cfunc .argtypes = [FailingConverter ]
13311336try :
1332- libc . srand (30 )
1337+ test_cfunc (30 )
13331338except 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"
13381344else :
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)
13421349class 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 ]
13491356MutatingConverter .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
13531360assigned_list = [ctypes .c_uint ]
1354- libc . srand .argtypes = assigned_list
1361+ test_cfunc .argtypes = assigned_list
13551362assigned_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
13611369class CallbackArgType (ctypes .c_int ):
@@ -1367,6 +1375,7 @@ def from_param(cls, val):
13671375CB = ctypes .CFUNCTYPE (None , CallbackArgType )
13681376cb_inst = CB (lambda x : None )
13691377
1378+
13701379# 8. Prototype with class-level _argtypes_ resolves converters when prototype type is created
13711380class ProtoConverter :
13721381 called = 0
@@ -1382,18 +1391,18 @@ def from_param(cls, val):
13821391ProtoConverter .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 )
13861395proto_func (60 )
13871396assert 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 )
13911400proto_cast (70 )
13921401assert 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
13991408if _sys .platform == "win32" :
0 commit comments