Skip to content

Commit 506916b

Browse files
committed
Fix tests for CPython 3.14
1 parent 6f38439 commit 506916b

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

tests/suite/modules/type_related/test_ctypes.py

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import unittest
1616
from decimal import Decimal
1717

18-
from iptest import IronPythonTestCase, is_posix, is_cli, is_32, is_mono, is_netcoreapp, big, myint, run_test
18+
from iptest import IronPythonTestCase, is_posix, is_windows, is_cli, is_32, is_mono, is_netcoreapp, big, myint
1919

2020
class MyInt:
2121
def __init__(self, value):
@@ -212,7 +212,7 @@ class Test(Structure):
212212
msg = "int expected instead of float"
213213
self.assertRaisesMessage(TypeError, msg, Test, 2.3)
214214

215-
with self.assertRaisesMessage(ValueError, "number of bits invalid for bit field"):
215+
with self.assertRaisesRegex(ValueError, "^number of bits invalid for bit field"):
216216
class Test(Structure):
217217
_fields_ = [("x", c_int, 0)]
218218
# if c_long and c_int are the same size, c_long is used
@@ -294,7 +294,7 @@ class Test(Structure):
294294

295295
self.check_bitfield(Test.a, c_longlong, 0, 0, 3)
296296
if is_posix:
297-
if is_cli: # GCC results
297+
if is_cli or sys.version_info >= (3, 14): # GCC-compliant results
298298
self.check_bitfield(Test.b, c_int, 0, 3, 4)
299299
self.check_bitfield(Test.c, c_uint, 0, 7, 1)
300300
else: # bug in CPython
@@ -365,12 +365,12 @@ class Test(Structure):
365365
]
366366

367367
self.check_bitfield(Test.a, c_long, 0, 0, 3)
368-
if is_cli: # GCC results
368+
if is_cli or sys.version_info >= (3, 14): # GCC-compliant results
369369
self.check_bitfield(Test.b, c_int, 4, 0, 30)
370370
else: # bug in CPython
371371
self.check_bitfield(Test.b, c_int, 4, 3, 30)
372372
if is_posix:
373-
if is_cli: # GCC results
373+
if is_cli or sys.version_info >= (3, 14): # GCC-compliant results
374374
self.check_bitfield(Test.c, c_long, 0, 62, 2)
375375
else: # bug in CPython
376376
self.check_bitfield(Test.c, c_long, 0, 33, 2)
@@ -399,13 +399,14 @@ class Test(Structure):
399399

400400
self.check_bitfield(Test.a, c_longlong, 0, 0, 3)
401401
if is_posix:
402-
if is_cli: # GCC results
402+
if is_cli or sys.version_info >= (3, 14): # GCC-compliant results
403403
self.check_bitfield(Test.b, c_int, 4, 0, 32)
404404
self.check_bitfield(Test.c, c_longlong, 8, 0, 2)
405+
self.assertEqual(sizeof(Test), 16)
405406
else: # bug in CPython
406407
self.check_bitfield(Test.b, c_int, 4, 3, 32)
407408
self.check_bitfield(Test.c, c_longlong, 0, 35, 2)
408-
self.assertEqual(sizeof(Test), 16)
409+
self.assertEqual(sizeof(Test), 8)
409410
else:
410411
self.check_bitfield(Test.b, c_int, 8, 0, 32)
411412
self.check_bitfield(Test.c, c_longlong, 16, 0, 2)
@@ -435,13 +436,14 @@ class Test(Structure):
435436

436437
self.check_bitfield(Test.a, c_byte, 1, 0, 3)
437438
if is_posix:
438-
if is_cli: # GCC results
439+
if is_cli or sys.version_info >= (3, 14): # GCC-compliant results
439440
self.check_bitfield(Test.b, c_short, 0, 11, 4)
440441
self.check_bitfield(Test.c, c_longlong, 0, 15, 2)
442+
self.assertEqual(sizeof(Test), 8)
441443
else: # bug in CPython
442444
self.check_bitfield(Test.b, c_short, 1, 3, 4)
443445
self.check_bitfield(Test.c, c_longlong, 1, 7, 2)
444-
self.assertEqual(sizeof(Test), 8)
446+
self.assertEqual(sizeof(Test), 9)
445447
else:
446448
self.check_bitfield(Test.b, c_short, 2, 0, 4)
447449
self.check_bitfield(Test.c, c_longlong, 8, 0, 2)
@@ -468,7 +470,7 @@ class Test(Structure):
468470

469471
self.check_bitfield(Test.a, c_longlong, 0, 0, 20)
470472
if is_posix:
471-
if is_cli: # GCC results
473+
if is_cli or sys.version_info >= (3, 14): # GCC-compliant results
472474
self.check_bitfield(Test.b, c_short, 2, 4, 2)
473475
self.check_bitfield(Test.c, c_short, 4, 0, 15)
474476
else: # bug in CPython
@@ -505,14 +507,15 @@ class Test(Structure):
505507
]
506508

507509
self.check_bitfield(Test.a, c_longlong, 0, 0, 20)
508-
if is_posix:
509-
if is_cli: # GCC results
510+
if is_posix and (is_cli or sys.version_info < (3, 14)): # CPython 3.14 implements MSVC behaviour (bug)
511+
if is_cli: # GCC-compliant results
510512
self.check_bitfield(Test.b, c_short, 2, 4, 2)
511513
self.check_bitfield(Test.c, c_short, 2, 6, 15)
514+
self.assertEqual(sizeof(Test), 5)
512515
else: # bug in CPython
513516
self.check_bitfield(Test.b, c_short, 6, 20, 2)
514517
self.check_bitfield(Test.c, c_short, 6, 22, 15)
515-
self.assertEqual(sizeof(Test), 5)
518+
self.assertEqual(sizeof(Test), 8)
516519
else:
517520
self.check_bitfield(Test.b, c_short, 8, 0, 2)
518521
self.check_bitfield(Test.c, c_short, 10, 0, 15)
@@ -539,13 +542,14 @@ class Test(Structure):
539542

540543
self.check_bitfield(Test.a, c_longlong, 0, 0, 3)
541544
if is_posix:
542-
if is_cli: # GCC results
545+
if is_cli or sys.version_info >= (3, 14): # GCC-compliant results
543546
self.check_bitfield(Test.b, c_int, 4, 0, 31)
544547
self.check_bitfield(Test.c, c_longlong, 8, 0, 3)
548+
self.assertEqual(sizeof(Test), 16)
545549
else: # bug in CPython
546550
self.check_bitfield(Test.b, c_int, 4, 3, 31)
547551
self.check_bitfield(Test.c, c_longlong, 0, 34, 3)
548-
self.assertEqual(sizeof(Test), 16)
552+
self.assertEqual(sizeof(Test), 8)
549553
else:
550554
self.check_bitfield(Test.b, c_int, 8, 0, 31)
551555
self.check_bitfield(Test.c, c_longlong, 16, 0, 3)
@@ -572,7 +576,7 @@ class Test(Structure):
572576

573577
self.check_bitfield(Test.a, c_longlong, 0, 0, 3)
574578
if is_posix:
575-
if is_cli: # GCC results
579+
if is_cli or sys.version_info >= (3, 14): # GCC-compliant results
576580
self.check_bitfield(Test.b, c_int, 0, 3, 29)
577581
else: # bug in CPython
578582
self.check_bitfield(Test.b, c_int, 4, 3, 29)
@@ -604,7 +608,7 @@ class Test(Structure):
604608

605609
self.check_bitfield(Test.a, c_longlong, 0, 0, 4)
606610
if is_posix:
607-
if is_cli:
611+
if is_cli or sys.version_info >= (3, 14): # GCC-compliant results
608612
self.check_bitfield(Test.b, c_int, 4, 0, 29)
609613
self.check_bitfield(Test.c, c_longlong, 0, 61, 3)
610614
else: # bug in CPython
@@ -693,7 +697,8 @@ class Test(Structure):
693697
self.check_bitfield(Test.a, c_byte, 0, 0, 7)
694698
if is_posix:
695699
self.check_bitfield(Test.b, c_short, 0, 7, 2)
696-
self.check_bitfield(Test.c, c_byte, 1, 1, 7)
700+
if is_cli or sys.version_info >= (3, 14): # bug in CPython 3.13 and earlier
701+
self.check_bitfield(Test.c, c_byte, 1, 1, 7)
697702
self.assertEqual(sizeof(Test), 2)
698703
else:
699704
self.check_bitfield(Test.b, c_short, 2, 0, 2)
@@ -753,12 +758,13 @@ class Test(Structure):
753758
]
754759

755760
self.check_bitfield(Test.a, c_ushort, 0, 0, 8)
756-
if is_posix:
761+
if is_posix: # GCC-compliant results
757762
self.check_bitfield(Test.b, c_int, 0, 8, 16)
758763
self.check_bitfield(Test.c, c_uint, 4, 0, 29)
759-
self.check_bitfield(Test.d, c_longlong, 8, 0, 9)
760-
self.check_bitfield(Test.e, c_uint, 8, 9, 2)
761-
self.check_bitfield(Test.f, c_uint, 12, 0, 31)
764+
if is_cli or sys.version_info >= (3, 14): # bug in CPython 3.13 and earlier
765+
self.check_bitfield(Test.d, c_longlong, 8, 0, 9)
766+
self.check_bitfield(Test.e, c_uint, 8, 9, 2)
767+
self.check_bitfield(Test.f, c_uint, 12, 0, 31)
762768
self.assertEqual(sizeof(Test), 16)
763769
else:
764770
self.check_bitfield(Test.b, c_int, 4, 0, 16)
@@ -801,20 +807,21 @@ class Test(Structure):
801807
]
802808

803809
self.check_bitfield(Test.a, c_ushort, 0, 0, 8)
804-
if is_posix:
810+
if is_posix and is_cli:
805811
self.check_bitfield(Test.b, c_int, 0, 8, 16)
806812
self.check_bitfield(Test.c, c_uint, 3, 0, 29) # ??
807813
self.check_bitfield(Test.d, c_longlong, 4, 21, 9)
808814
self.check_bitfield(Test.e, c_uint, 4, 30, 2)
809815
self.check_bitfield(Test.f, c_uint, 8, 0, 31)
810816
self.assertEqual(sizeof(Test), 12)
811817
else:
812-
self.check_bitfield(Test.b, c_int, 4, 0, 16)
813-
self.check_bitfield(Test.c, c_uint, 8, 0, 29)
814-
self.check_bitfield(Test.d, c_longlong, 12, 0, 9)
815-
self.check_bitfield(Test.e, c_uint, 20, 0, 2)
816-
self.check_bitfield(Test.f, c_uint, 24, 0, 31)
817-
self.assertEqual(sizeof(Test), 28)
818+
if is_windows or sys.version_info >= (3, 14): # CPython 3.14 implements MSVC behavior even on POSIX (bug), CPython 3.13 and earlier is hopelessly incorrect
819+
self.check_bitfield(Test.b, c_int, 4, 0, 16)
820+
self.check_bitfield(Test.c, c_uint, 8, 0, 29)
821+
self.check_bitfield(Test.d, c_longlong, 12, 0, 9)
822+
self.check_bitfield(Test.e, c_uint, 20, 0, 2)
823+
self.check_bitfield(Test.f, c_uint, 24, 0, 31)
824+
self.assertEqual(sizeof(Test), 28)
818825

819826

820827
def test_bitfield_mixed_H1(self):
@@ -834,7 +841,10 @@ class Test(Structure):
834841

835842
self.check_bitfield(Test.a, c_longlong, 0, 0, 52)
836843
if is_posix:
837-
self.check_bitfield(Test.b, c_byte, 6, 4, 3)
844+
if is_cli or sys.version_info >= (3, 14):
845+
self.check_bitfield(Test.b, c_byte, 6, 4, 3)
846+
else: # bug in CPython
847+
self.check_bitfield(Test.b, c_byte, 7, 52, 3)
838848
self.assertEqual(sizeof(Test), 8)
839849
else:
840850
self.check_bitfield(Test.b, c_byte, 8, 0, 3)
@@ -858,7 +868,10 @@ class Test(Structure):
858868

859869
self.check_bitfield(Test.a, c_longlong, 0, 0, 52)
860870
if is_posix:
861-
self.check_bitfield(Test.b, c_byte, 6, 4, 4)
871+
if is_cli or sys.version_info >= (3, 14):
872+
self.check_bitfield(Test.b, c_byte, 6, 4, 4)
873+
else: # bug in CPython
874+
self.check_bitfield(Test.b, c_byte, 7, 52, 4)
862875
self.assertEqual(sizeof(Test), 8)
863876
else:
864877
self.check_bitfield(Test.b, c_byte, 8, 0, 4)
@@ -882,7 +895,10 @@ class Test(Structure):
882895

883896
self.check_bitfield(Test.a, c_longlong, 0, 0, 52)
884897
if is_posix:
885-
self.check_bitfield(Test.b, c_byte, 7, 0, 5)
898+
if (is_cli or sys.version_info >= (3, 14)):
899+
self.check_bitfield(Test.b, c_byte, 7, 0, 5)
900+
else: # bug in CPython
901+
self.check_bitfield(Test.b, c_byte, 7, 52, 5)
886902
self.assertEqual(sizeof(Test), 8)
887903
else:
888904
self.check_bitfield(Test.b, c_byte, 8, 0, 5)
@@ -1063,4 +1079,5 @@ def test_conversions_overflow(self):
10631079
self.assertEqual(c_byte_value.value, -127)
10641080

10651081

1066-
run_test(__name__)
1082+
if __name__ == "__main__":
1083+
unittest.main()

0 commit comments

Comments
 (0)