| 1 |
"""Tests for ctypes.Structure |
| 2 |
|
| 3 |
Features common with Union should go in test_structunion.py instead. |
| 4 |
""" |
| 5 |
|
| 6 |
from platform import architecture as _architecture |
| 7 |
import struct |
| 8 |
import sys |
| 9 |
import unittest |
| 10 |
from ctypes import (CDLL, Structure, Union, POINTER, sizeof, byref, |
| 11 |
c_void_p, c_char, c_wchar, c_byte, c_ubyte, |
| 12 |
c_uint8, c_uint16, c_uint32, c_int, c_uint, |
| 13 |
c_long, c_ulong, c_longlong, c_float, c_double) |
| 14 |
from ctypes.util import find_library |
| 15 |
from collections import namedtuple |
| 16 |
from test import support |
| 17 |
from test.support import import_helper |
| 18 |
from ._support import StructCheckMixin |
| 19 |
_ctypes_test = import_helper.import_module("_ctypes_test") |
| 20 |
|
| 21 |
|
| 22 |
class StructureTestCase(unittest.TestCase, StructCheckMixin): |
| 23 |
def test_packed(self): |
| 24 |
class X(Structure): |
| 25 |
_fields_ = [("a", c_byte), |
| 26 |
("b", c_longlong)] |
| 27 |
_pack_ = 1 |
| 28 |
_layout_ = 'ms' |
| 29 |
self.check_struct(X) |
| 30 |
|
| 31 |
self.assertEqual(sizeof(X), 9) |
| 32 |
self.assertEqual(X.b.offset, 1) |
| 33 |
|
| 34 |
class X(Structure): |
| 35 |
_fields_ = [("a", c_byte), |
| 36 |
("b", c_longlong)] |
| 37 |
_pack_ = 2 |
| 38 |
_layout_ = 'ms' |
| 39 |
self.check_struct(X) |
| 40 |
self.assertEqual(sizeof(X), 10) |
| 41 |
self.assertEqual(X.b.offset, 2) |
| 42 |
|
| 43 |
longlong_size = struct.calcsize("q") |
| 44 |
longlong_align = struct.calcsize("bq") - longlong_size |
| 45 |
|
| 46 |
class X(Structure): |
| 47 |
_fields_ = [("a", c_byte), |
| 48 |
("b", c_longlong)] |
| 49 |
_pack_ = 4 |
| 50 |
_layout_ = 'ms' |
| 51 |
self.check_struct(X) |
| 52 |
self.assertEqual(sizeof(X), min(4, longlong_align) + longlong_size) |
| 53 |
self.assertEqual(X.b.offset, min(4, longlong_align)) |
| 54 |
|
| 55 |
class X(Structure): |
| 56 |
_fields_ = [("a", c_byte), |
| 57 |
("b", c_longlong)] |
| 58 |
_pack_ = 8 |
| 59 |
_layout_ = 'ms' |
| 60 |
self.check_struct(X) |
| 61 |
|
| 62 |
self.assertEqual(sizeof(X), min(8, longlong_align) + longlong_size) |
| 63 |
self.assertEqual(X.b.offset, min(8, longlong_align)) |
| 64 |
|
| 65 |
with self.assertRaises(ValueError): |
| 66 |
class X(Structure): |
| 67 |
_fields_ = [("a", "b"), ("b", "q")] |
| 68 |
_pack_ = -1 |
| 69 |
_layout_ = "ms" |
| 70 |
|
| 71 |
@support.cpython_only |
| 72 |
def test_packed_c_limits(self): |
| 73 |
# Issue 15989 |
| 74 |
import _testcapi |
| 75 |
with self.assertRaises(ValueError): |
| 76 |
class X(Structure): |
| 77 |
_fields_ = [("a", c_byte)] |
| 78 |
_pack_ = _testcapi.INT_MAX + 1 |
| 79 |
_layout_ = "ms" |
| 80 |
|
| 81 |
with self.assertRaises(ValueError): |
| 82 |
class X(Structure): |
| 83 |
_fields_ = [("a", c_byte)] |
| 84 |
_pack_ = _testcapi.UINT_MAX + 2 |
| 85 |
_layout_ = "ms" |
| 86 |
|
| 87 |
def test_initializers(self): |
| 88 |
class Person(Structure): |
| 89 |
_fields_ = [("name", c_char*6), |
| 90 |
("age", c_int)] |
| 91 |
|
| 92 |
self.assertRaises(TypeError, Person, 42) |
| 93 |
self.assertRaises(ValueError, Person, b"asldkjaslkdjaslkdj") |
| 94 |
self.assertRaises(TypeError, Person, "Name", "HI") |
| 95 |
|
| 96 |
# short enough |
| 97 |
self.assertEqual(Person(b"12345", 5).name, b"12345") |
| 98 |
# exact fit |
| 99 |
self.assertEqual(Person(b"123456", 5).name, b"123456") |
| 100 |
# too long |
| 101 |
self.assertRaises(ValueError, Person, b"1234567", 5) |
| 102 |
|
| 103 |
def test_conflicting_initializers(self): |
| 104 |
class POINT(Structure): |
| 105 |
_fields_ = [("phi", c_float), ("rho", c_float)] |
| 106 |
self.check_struct(POINT) |
| 107 |
# conflicting positional and keyword args |
| 108 |
self.assertRaisesRegex(TypeError, "phi", POINT, 2, 3, phi=4) |
| 109 |
self.assertRaisesRegex(TypeError, "rho", POINT, 2, 3, rho=4) |
| 110 |
|
| 111 |
# too many initializers |
| 112 |
self.assertRaises(TypeError, POINT, 2, 3, 4) |
| 113 |
|
| 114 |
def test_keyword_initializers(self): |
| 115 |
class POINT(Structure): |
| 116 |
_fields_ = [("x", c_int), ("y", c_int)] |
| 117 |
self.check_struct(POINT) |
| 118 |
pt = POINT(1, 2) |
| 119 |
self.assertEqual((pt.x, pt.y), (1, 2)) |
| 120 |
|
| 121 |
pt = POINT(y=2, x=1) |
| 122 |
self.assertEqual((pt.x, pt.y), (1, 2)) |
| 123 |
|
| 124 |
def test_nested_initializers(self): |
| 125 |
# test initializing nested structures |
| 126 |
class Phone(Structure): |
| 127 |
_fields_ = [("areacode", c_char*6), |
| 128 |
("number", c_char*12)] |
| 129 |
self.check_struct(Phone) |
| 130 |
|
| 131 |
class Person(Structure): |
| 132 |
_fields_ = [("name", c_char * 12), |
| 133 |
("phone", Phone), |
| 134 |
("age", c_int)] |
| 135 |
self.check_struct(Person) |
| 136 |
|
| 137 |
p = Person(b"Someone", (b"1234", b"5678"), 5) |
| 138 |
|
| 139 |
self.assertEqual(p.name, b"Someone") |
| 140 |
self.assertEqual(p.phone.areacode, b"1234") |
| 141 |
self.assertEqual(p.phone.number, b"5678") |
| 142 |
self.assertEqual(p.age, 5) |
| 143 |
|
| 144 |
def test_structures_with_wchar(self): |
| 145 |
class PersonW(Structure): |
| 146 |
_fields_ = [("name", c_wchar * 12), |
| 147 |
("age", c_int)] |
| 148 |
self.check_struct(PersonW) |
| 149 |
|
| 150 |
p = PersonW("Someone \xe9") |
| 151 |
self.assertEqual(p.name, "Someone \xe9") |
| 152 |
|
| 153 |
self.assertEqual(PersonW("1234567890").name, "1234567890") |
| 154 |
self.assertEqual(PersonW("12345678901").name, "12345678901") |
| 155 |
# exact fit |
| 156 |
self.assertEqual(PersonW("123456789012").name, "123456789012") |
| 157 |
#too long |
| 158 |
self.assertRaises(ValueError, PersonW, "1234567890123") |
| 159 |
|
| 160 |
def test_init_errors(self): |
| 161 |
class Phone(Structure): |
| 162 |
_fields_ = [("areacode", c_char*6), |
| 163 |
("number", c_char*12)] |
| 164 |
self.check_struct(Phone) |
| 165 |
|
| 166 |
class Person(Structure): |
| 167 |
_fields_ = [("name", c_char * 12), |
| 168 |
("phone", Phone), |
| 169 |
("age", c_int)] |
| 170 |
self.check_struct(Person) |
| 171 |
|
| 172 |
cls, msg = self.get_except(Person, b"Someone", (1, 2)) |
| 173 |
self.assertEqual(cls, RuntimeError) |
| 174 |
self.assertEqual(msg, |
| 175 |
"(Phone) TypeError: " |
| 176 |
"expected bytes, int found") |
| 177 |
|
| 178 |
cls, msg = self.get_except(Person, b"Someone", (b"a", b"b", b"c")) |
| 179 |
self.assertEqual(cls, RuntimeError) |
| 180 |
self.assertEqual(msg, |
| 181 |
"(Phone) TypeError: too many initializers") |
| 182 |
|
| 183 |
def get_except(self, func, *args): |
| 184 |
try: |
| 185 |
func(*args) |
| 186 |
except Exception as detail: |
| 187 |
return detail.__class__, str(detail) |
| 188 |
|
| 189 |
def test_positional_args(self): |
| 190 |
# see also http://bugs.python.org/issue5042 |
| 191 |
class W(Structure): |
| 192 |
_fields_ = [("a", c_int), ("b", c_int)] |
| 193 |
self.check_struct(W) |
| 194 |
|
| 195 |
class X(W): |
| 196 |
_fields_ = [("c", c_int)] |
| 197 |
self.check_struct(X) |
| 198 |
|
| 199 |
class Y(X): |
| 200 |
pass |
| 201 |
self.check_struct(Y) |
| 202 |
|
| 203 |
class Z(Y): |
| 204 |
_fields_ = [("d", c_int), ("e", c_int), ("f", c_int)] |
| 205 |
self.check_struct(Z) |
| 206 |
|
| 207 |
z = Z(1, 2, 3, 4, 5, 6) |
| 208 |
self.assertEqual((z.a, z.b, z.c, z.d, z.e, z.f), |
| 209 |
(1, 2, 3, 4, 5, 6)) |
| 210 |
z = Z(1) |
| 211 |
self.assertEqual((z.a, z.b, z.c, z.d, z.e, z.f), |
| 212 |
(1, 0, 0, 0, 0, 0)) |
| 213 |
self.assertRaises(TypeError, lambda: Z(1, 2, 3, 4, 5, 6, 7)) |
| 214 |
|
| 215 |
def test_pass_by_value(self): |
| 216 |
# This should mirror the Test structure |
| 217 |
# in Modules/_ctypes/_ctypes_test.c |
| 218 |
class Test(Structure): |
| 219 |
_fields_ = [ |
| 220 |
('first', c_ulong), |
| 221 |
('second', c_ulong), |
| 222 |
('third', c_ulong), |
| 223 |
] |
| 224 |
self.check_struct(Test) |
| 225 |
|
| 226 |
s = Test() |
| 227 |
s.first = 0xdeadbeef |
| 228 |
s.second = 0xcafebabe |
| 229 |
s.third = 0x0bad1dea |
| 230 |
dll = CDLL(_ctypes_test.__file__) |
| 231 |
func = dll._testfunc_large_struct_update_value |
| 232 |
func.argtypes = (Test,) |
| 233 |
func.restype = None |
| 234 |
func(s) |
| 235 |
self.assertEqual(s.first, 0xdeadbeef) |
| 236 |
self.assertEqual(s.second, 0xcafebabe) |
| 237 |
self.assertEqual(s.third, 0x0bad1dea) |
| 238 |
|
| 239 |
def test_pass_by_value_finalizer(self): |
| 240 |
# bpo-37140: Similar to test_pass_by_value(), but the Python structure |
| 241 |
# has a finalizer (__del__() method): the finalizer must only be called |
| 242 |
# once. |
| 243 |
|
| 244 |
finalizer_calls = [] |
| 245 |
|
| 246 |
class Test(Structure): |
| 247 |
_fields_ = [ |
| 248 |
('first', c_ulong), |
| 249 |
('second', c_ulong), |
| 250 |
('third', c_ulong), |
| 251 |
] |
| 252 |
def __del__(self): |
| 253 |
finalizer_calls.append("called") |
| 254 |
self.check_struct(Test) |
| 255 |
|
| 256 |
s = Test(1, 2, 3) |
| 257 |
# Test the StructUnionType_paramfunc() code path which copies the |
| 258 |
# structure: if the structure is larger than sizeof(void*). |
| 259 |
self.assertGreater(sizeof(s), sizeof(c_void_p)) |
| 260 |
|
| 261 |
dll = CDLL(_ctypes_test.__file__) |
| 262 |
func = dll._testfunc_large_struct_update_value |
| 263 |
func.argtypes = (Test,) |
| 264 |
func.restype = None |
| 265 |
func(s) |
| 266 |
# bpo-37140: Passing the structure by reference must not call |
| 267 |
# its finalizer! |
| 268 |
self.assertEqual(finalizer_calls, []) |
| 269 |
self.assertEqual(s.first, 1) |
| 270 |
self.assertEqual(s.second, 2) |
| 271 |
self.assertEqual(s.third, 3) |
| 272 |
|
| 273 |
# The finalizer must be called exactly once |
| 274 |
s = None |
| 275 |
support.gc_collect() |
| 276 |
self.assertEqual(finalizer_calls, ["called"]) |
| 277 |
|
| 278 |
def test_pass_by_value_in_register(self): |
| 279 |
class X(Structure): |
| 280 |
_fields_ = [ |
| 281 |
('first', c_uint), |
| 282 |
('second', c_uint) |
| 283 |
] |
| 284 |
self.check_struct(X) |
| 285 |
|
| 286 |
s = X() |
| 287 |
s.first = 0xdeadbeef |
| 288 |
s.second = 0xcafebabe |
| 289 |
dll = CDLL(_ctypes_test.__file__) |
| 290 |
func = dll._testfunc_reg_struct_update_value |
| 291 |
func.argtypes = (X,) |
| 292 |
func.restype = None |
| 293 |
func(s) |
| 294 |
self.assertEqual(s.first, 0xdeadbeef) |
| 295 |
self.assertEqual(s.second, 0xcafebabe) |
| 296 |
dll.get_last_tfrsuv_arg.argtypes = () |
| 297 |
dll.get_last_tfrsuv_arg.restype = X |
| 298 |
got = dll.get_last_tfrsuv_arg() |
| 299 |
self.assertEqual(s.first, got.first) |
| 300 |
self.assertEqual(s.second, got.second) |
| 301 |
|
| 302 |
def _test_issue18060(self, Vector): |
| 303 |
# Regression tests for gh-62260 |
| 304 |
|
| 305 |
# The call to atan2() should succeed if the |
| 306 |
# class fields were correctly cloned in the |
| 307 |
# subclasses. Otherwise, it will segfault. |
| 308 |
if sys.platform == 'win32': |
| 309 |
libm = CDLL(find_library('msvcrt.dll')) |
| 310 |
else: |
| 311 |
libm = CDLL(find_library('m')) |
| 312 |
|
| 313 |
libm.atan2.argtypes = [Vector] |
| 314 |
libm.atan2.restype = c_double |
| 315 |
|
| 316 |
arg = Vector(y=0.0, x=-1.0) |
| 317 |
self.assertAlmostEqual(libm.atan2(arg), 3.141592653589793) |
| 318 |
|
| 319 |
@unittest.skipIf(_architecture() == ('64bit', 'WindowsPE'), "can't test Windows x64 build") |
| 320 |
@unittest.skipUnless(sys.byteorder == 'little', "can't test on this platform") |
| 321 |
def test_issue18060_a(self): |
| 322 |
# This test case calls |
| 323 |
# PyCStructUnionType_update_stginfo() for each |
| 324 |
# _fields_ assignment, and PyCStgInfo_clone() |
| 325 |
# for the Mid and Vector class definitions. |
| 326 |
class Base(Structure): |
| 327 |
_fields_ = [('y', c_double), |
| 328 |
('x', c_double)] |
| 329 |
class Mid(Base): |
| 330 |
pass |
| 331 |
Mid._fields_ = [] |
| 332 |
class Vector(Mid): pass |
| 333 |
self._test_issue18060(Vector) |
| 334 |
|
| 335 |
@unittest.skipIf(_architecture() == ('64bit', 'WindowsPE'), "can't test Windows x64 build") |
| 336 |
@unittest.skipUnless(sys.byteorder == 'little', "can't test on this platform") |
| 337 |
def test_issue18060_b(self): |
| 338 |
# This test case calls |
| 339 |
# PyCStructUnionType_update_stginfo() for each |
| 340 |
# _fields_ assignment. |
| 341 |
class Base(Structure): |
| 342 |
_fields_ = [('y', c_double), |
| 343 |
('x', c_double)] |
| 344 |
class Mid(Base): |
| 345 |
_fields_ = [] |
| 346 |
class Vector(Mid): |
| 347 |
_fields_ = [] |
| 348 |
self._test_issue18060(Vector) |
| 349 |
|
| 350 |
@unittest.skipIf(_architecture() == ('64bit', 'WindowsPE'), "can't test Windows x64 build") |
| 351 |
@unittest.skipUnless(sys.byteorder == 'little', "can't test on this platform") |
| 352 |
def test_issue18060_c(self): |
| 353 |
# This test case calls |
| 354 |
# PyCStructUnionType_update_stginfo() for each |
| 355 |
# _fields_ assignment. |
| 356 |
class Base(Structure): |
| 357 |
_fields_ = [('y', c_double)] |
| 358 |
class Mid(Base): |
| 359 |
_fields_ = [] |
| 360 |
class Vector(Mid): |
| 361 |
_fields_ = [('x', c_double)] |
| 362 |
self._test_issue18060(Vector) |
| 363 |
|
| 364 |
def test_array_in_struct(self): |
| 365 |
# See bpo-22273 |
| 366 |
|
| 367 |
# Load the shared library |
| 368 |
dll = CDLL(_ctypes_test.__file__) |
| 369 |
|
| 370 |
# These should mirror the structures in Modules/_ctypes/_ctypes_test.c |
| 371 |
class Test2(Structure): |
| 372 |
_fields_ = [ |
| 373 |
('data', c_ubyte * 16), |
| 374 |
] |
| 375 |
self.check_struct(Test2) |
| 376 |
|
| 377 |
class Test3AParent(Structure): |
| 378 |
_fields_ = [ |
| 379 |
('data', c_float * 2), |
| 380 |
] |
| 381 |
self.check_struct(Test3AParent) |
| 382 |
|
| 383 |
class Test3A(Test3AParent): |
| 384 |
_fields_ = [ |
| 385 |
('more_data', c_float * 2), |
| 386 |
] |
| 387 |
self.check_struct(Test3A) |
| 388 |
|
| 389 |
class Test3B(Structure): |
| 390 |
_fields_ = [ |
| 391 |
('data', c_double * 2), |
| 392 |
] |
| 393 |
self.check_struct(Test3B) |
| 394 |
|
| 395 |
class Test3C(Structure): |
| 396 |
_fields_ = [ |
| 397 |
("data", c_double * 4) |
| 398 |
] |
| 399 |
self.check_struct(Test3C) |
| 400 |
|
| 401 |
class Test3D(Structure): |
| 402 |
_fields_ = [ |
| 403 |
("data", c_double * 8) |
| 404 |
] |
| 405 |
self.check_struct(Test3D) |
| 406 |
|
| 407 |
class Test3E(Structure): |
| 408 |
_fields_ = [ |
| 409 |
("data", c_double * 9) |
| 410 |
] |
| 411 |
self.check_struct(Test3E) |
| 412 |
|
| 413 |
|
| 414 |
# Tests for struct Test2 |
| 415 |
s = Test2() |
| 416 |
expected = 0 |
| 417 |
for i in range(16): |
| 418 |
s.data[i] = i |
| 419 |
expected += i |
| 420 |
func = dll._testfunc_array_in_struct2 |
| 421 |
func.restype = c_int |
| 422 |
func.argtypes = (Test2,) |
| 423 |
result = func(s) |
| 424 |
self.assertEqual(result, expected) |
| 425 |
# check the passed-in struct hasn't changed |
| 426 |
for i in range(16): |
| 427 |
self.assertEqual(s.data[i], i) |
| 428 |
|
| 429 |
# Tests for struct Test3A |
| 430 |
s = Test3A() |
| 431 |
s.data[0] = 3.14159 |
| 432 |
s.data[1] = 2.71828 |
| 433 |
s.more_data[0] = -3.0 |
| 434 |
s.more_data[1] = -2.0 |
| 435 |
expected = 3.14159 + 2.71828 - 3.0 - 2.0 |
| 436 |
func = dll._testfunc_array_in_struct3A |
| 437 |
func.restype = c_double |
| 438 |
func.argtypes = (Test3A,) |
| 439 |
result = func(s) |
| 440 |
self.assertAlmostEqual(result, expected, places=6) |
| 441 |
# check the passed-in struct hasn't changed |
| 442 |
self.assertAlmostEqual(s.data[0], 3.14159, places=6) |
| 443 |
self.assertAlmostEqual(s.data[1], 2.71828, places=6) |
| 444 |
self.assertAlmostEqual(s.more_data[0], -3.0, places=6) |
| 445 |
self.assertAlmostEqual(s.more_data[1], -2.0, places=6) |
| 446 |
|
| 447 |
# Test3B, Test3C, Test3D, Test3E have the same logic with different |
| 448 |
# sizes hence putting them in a loop. |
| 449 |
StructCtype = namedtuple( |
| 450 |
"StructCtype", |
| 451 |
["cls", "cfunc1", "cfunc2", "items"] |
| 452 |
) |
| 453 |
structs_to_test = [ |
| 454 |
StructCtype( |
| 455 |
Test3B, |
| 456 |
dll._testfunc_array_in_struct3B, |
| 457 |
dll._testfunc_array_in_struct3B_set_defaults, |
| 458 |
2), |
| 459 |
StructCtype( |
| 460 |
Test3C, |
| 461 |
dll._testfunc_array_in_struct3C, |
| 462 |
dll._testfunc_array_in_struct3C_set_defaults, |
| 463 |
4), |
| 464 |
StructCtype( |
| 465 |
Test3D, |
| 466 |
dll._testfunc_array_in_struct3D, |
| 467 |
dll._testfunc_array_in_struct3D_set_defaults, |
| 468 |
8), |
| 469 |
StructCtype( |
| 470 |
Test3E, |
| 471 |
dll._testfunc_array_in_struct3E, |
| 472 |
dll._testfunc_array_in_struct3E_set_defaults, |
| 473 |
9), |
| 474 |
] |
| 475 |
|
| 476 |
for sut in structs_to_test: |
| 477 |
s = sut.cls() |
| 478 |
|
| 479 |
# Test for cfunc1 |
| 480 |
expected = 0 |
| 481 |
for i in range(sut.items): |
| 482 |
float_i = float(i) |
| 483 |
s.data[i] = float_i |
| 484 |
expected += float_i |
| 485 |
func = sut.cfunc1 |
| 486 |
func.restype = c_double |
| 487 |
func.argtypes = (sut.cls,) |
| 488 |
result = func(s) |
| 489 |
self.assertEqual(result, expected) |
| 490 |
# check the passed-in struct hasn't changed |
| 491 |
for i in range(sut.items): |
| 492 |
self.assertEqual(s.data[i], float(i)) |
| 493 |
|
| 494 |
# Test for cfunc2 |
| 495 |
func = sut.cfunc2 |
| 496 |
func.restype = sut.cls |
| 497 |
result = func() |
| 498 |
# check if the default values have been set correctly |
| 499 |
for i in range(sut.items): |
| 500 |
self.assertEqual(result.data[i], float(i+1)) |
| 501 |
|
| 502 |
def test_38368(self): |
| 503 |
# Regression test for gh-82549 |
| 504 |
class U(Union): |
| 505 |
_fields_ = [ |
| 506 |
('f1', c_uint8 * 16), |
| 507 |
('f2', c_uint16 * 8), |
| 508 |
('f3', c_uint32 * 4), |
| 509 |
] |
| 510 |
self.check_union(U) |
| 511 |
|
| 512 |
u = U() |
| 513 |
u.f3[0] = 0x01234567 |
| 514 |
u.f3[1] = 0x89ABCDEF |
| 515 |
u.f3[2] = 0x76543210 |
| 516 |
u.f3[3] = 0xFEDCBA98 |
| 517 |
f1 = [u.f1[i] for i in range(16)] |
| 518 |
f2 = [u.f2[i] for i in range(8)] |
| 519 |
if sys.byteorder == 'little': |
| 520 |
self.assertEqual(f1, [0x67, 0x45, 0x23, 0x01, |
| 521 |
0xef, 0xcd, 0xab, 0x89, |
| 522 |
0x10, 0x32, 0x54, 0x76, |
| 523 |
0x98, 0xba, 0xdc, 0xfe]) |
| 524 |
self.assertEqual(f2, [0x4567, 0x0123, 0xcdef, 0x89ab, |
| 525 |
0x3210, 0x7654, 0xba98, 0xfedc]) |
| 526 |
|
| 527 |
@unittest.skipIf(True, 'Test disabled for now - see gh-60779/gh-60780') |
| 528 |
def test_union_by_value(self): |
| 529 |
# See gh-60779 |
| 530 |
|
| 531 |
# These should mirror the structures in Modules/_ctypes/_ctypes_test.c |
| 532 |
|
| 533 |
class Nested1(Structure): |
| 534 |
_fields_ = [ |
| 535 |
('an_int', c_int), |
| 536 |
('another_int', c_int), |
| 537 |
] |
| 538 |
self.check_struct(Nested1) |
| 539 |
|
| 540 |
class Test4(Union): |
| 541 |
_fields_ = [ |
| 542 |
('a_long', c_long), |
| 543 |
('a_struct', Nested1), |
| 544 |
] |
| 545 |
self.check_struct(Test4) |
| 546 |
|
| 547 |
class Nested2(Structure): |
| 548 |
_fields_ = [ |
| 549 |
('an_int', c_int), |
| 550 |
('a_union', Test4), |
| 551 |
] |
| 552 |
self.check_struct(Nested2) |
| 553 |
|
| 554 |
class Test5(Structure): |
| 555 |
_fields_ = [ |
| 556 |
('an_int', c_int), |
| 557 |
('nested', Nested2), |
| 558 |
('another_int', c_int), |
| 559 |
] |
| 560 |
self.check_struct(Test5) |
| 561 |
|
| 562 |
test4 = Test4() |
| 563 |
dll = CDLL(_ctypes_test.__file__) |
| 564 |
with self.assertRaises(TypeError) as ctx: |
| 565 |
func = dll._testfunc_union_by_value1 |
| 566 |
func.restype = c_long |
| 567 |
func.argtypes = (Test4,) |
| 568 |
result = func(test4) |
| 569 |
self.assertEqual(ctx.exception.args[0], 'item 1 in _argtypes_ passes ' |
| 570 |
'a union by value, which is unsupported.') |
| 571 |
test5 = Test5() |
| 572 |
with self.assertRaises(TypeError) as ctx: |
| 573 |
func = dll._testfunc_union_by_value2 |
| 574 |
func.restype = c_long |
| 575 |
func.argtypes = (Test5,) |
| 576 |
result = func(test5) |
| 577 |
self.assertEqual(ctx.exception.args[0], 'item 1 in _argtypes_ passes ' |
| 578 |
'a union by value, which is unsupported.') |
| 579 |
|
| 580 |
# passing by reference should be OK |
| 581 |
test4.a_long = 12345; |
| 582 |
func = dll._testfunc_union_by_reference1 |
| 583 |
func.restype = c_long |
| 584 |
func.argtypes = (POINTER(Test4),) |
| 585 |
result = func(byref(test4)) |
| 586 |
self.assertEqual(result, 12345) |
| 587 |
self.assertEqual(test4.a_long, 0) |
| 588 |
self.assertEqual(test4.a_struct.an_int, 0) |
| 589 |
self.assertEqual(test4.a_struct.another_int, 0) |
| 590 |
test4.a_struct.an_int = 0x12340000 |
| 591 |
test4.a_struct.another_int = 0x5678 |
| 592 |
func = dll._testfunc_union_by_reference2 |
| 593 |
func.restype = c_long |
| 594 |
func.argtypes = (POINTER(Test4),) |
| 595 |
result = func(byref(test4)) |
| 596 |
self.assertEqual(result, 0x12345678) |
| 597 |
self.assertEqual(test4.a_long, 0) |
| 598 |
self.assertEqual(test4.a_struct.an_int, 0) |
| 599 |
self.assertEqual(test4.a_struct.another_int, 0) |
| 600 |
test5.an_int = 0x12000000 |
| 601 |
test5.nested.an_int = 0x345600 |
| 602 |
test5.another_int = 0x78 |
| 603 |
func = dll._testfunc_union_by_reference3 |
| 604 |
func.restype = c_long |
| 605 |
func.argtypes = (POINTER(Test5),) |
| 606 |
result = func(byref(test5)) |
| 607 |
self.assertEqual(result, 0x12345678) |
| 608 |
self.assertEqual(test5.an_int, 0) |
| 609 |
self.assertEqual(test5.nested.an_int, 0) |
| 610 |
self.assertEqual(test5.another_int, 0) |
| 611 |
|
| 612 |
@unittest.skipIf(True, 'Test disabled for now - see gh-60779/gh-60780') |
| 613 |
def test_bitfield_by_value(self): |
| 614 |
# See gh-60780 |
| 615 |
|
| 616 |
# These should mirror the structures in Modules/_ctypes/_ctypes_test.c |
| 617 |
|
| 618 |
class Test6(Structure): |
| 619 |
_fields_ = [ |
| 620 |
('A', c_int, 1), |
| 621 |
('B', c_int, 2), |
| 622 |
('C', c_int, 3), |
| 623 |
('D', c_int, 2), |
| 624 |
] |
| 625 |
self.check_struct(Test6) |
| 626 |
|
| 627 |
test6 = Test6() |
| 628 |
# As these are signed int fields, all are logically -1 due to sign |
| 629 |
# extension. |
| 630 |
test6.A = 1 |
| 631 |
test6.B = 3 |
| 632 |
test6.C = 7 |
| 633 |
test6.D = 3 |
| 634 |
dll = CDLL(_ctypes_test.__file__) |
| 635 |
with self.assertRaises(TypeError) as ctx: |
| 636 |
func = dll._testfunc_bitfield_by_value1 |
| 637 |
func.restype = c_long |
| 638 |
func.argtypes = (Test6,) |
| 639 |
result = func(test6) |
| 640 |
self.assertEqual(ctx.exception.args[0], 'item 1 in _argtypes_ passes ' |
| 641 |
'a struct/union with a bitfield by value, which is ' |
| 642 |
'unsupported.') |
| 643 |
# passing by reference should be OK |
| 644 |
func = dll._testfunc_bitfield_by_reference1 |
| 645 |
func.restype = c_long |
| 646 |
func.argtypes = (POINTER(Test6),) |
| 647 |
result = func(byref(test6)) |
| 648 |
self.assertEqual(result, -4) |
| 649 |
self.assertEqual(test6.A, 0) |
| 650 |
self.assertEqual(test6.B, 0) |
| 651 |
self.assertEqual(test6.C, 0) |
| 652 |
self.assertEqual(test6.D, 0) |
| 653 |
|
| 654 |
class Test7(Structure): |
| 655 |
_fields_ = [ |
| 656 |
('A', c_uint, 1), |
| 657 |
('B', c_uint, 2), |
| 658 |
('C', c_uint, 3), |
| 659 |
('D', c_uint, 2), |
| 660 |
] |
| 661 |
self.check_struct(Test7) |
| 662 |
|
| 663 |
test7 = Test7() |
| 664 |
test7.A = 1 |
| 665 |
test7.B = 3 |
| 666 |
test7.C = 7 |
| 667 |
test7.D = 3 |
| 668 |
func = dll._testfunc_bitfield_by_reference2 |
| 669 |
func.restype = c_long |
| 670 |
func.argtypes = (POINTER(Test7),) |
| 671 |
result = func(byref(test7)) |
| 672 |
self.assertEqual(result, 14) |
| 673 |
self.assertEqual(test7.A, 0) |
| 674 |
self.assertEqual(test7.B, 0) |
| 675 |
self.assertEqual(test7.C, 0) |
| 676 |
self.assertEqual(test7.D, 0) |
| 677 |
|
| 678 |
# for a union with bitfields, the union check happens first |
| 679 |
class Test8(Union): |
| 680 |
_fields_ = [ |
| 681 |
('A', c_int, 1), |
| 682 |
('B', c_int, 2), |
| 683 |
('C', c_int, 3), |
| 684 |
('D', c_int, 2), |
| 685 |
] |
| 686 |
self.check_union(Test8) |
| 687 |
|
| 688 |
test8 = Test8() |
| 689 |
with self.assertRaises(TypeError) as ctx: |
| 690 |
func = dll._testfunc_bitfield_by_value2 |
| 691 |
func.restype = c_long |
| 692 |
func.argtypes = (Test8,) |
| 693 |
result = func(test8) |
| 694 |
self.assertEqual(ctx.exception.args[0], 'item 1 in _argtypes_ passes ' |
| 695 |
'a union by value, which is unsupported.') |
| 696 |
|
| 697 |
def test_do_not_share_pointer_type_cache_via_stginfo_clone(self): |
| 698 |
# This test case calls PyCStgInfo_clone() |
| 699 |
# for the Mid and Vector class definitions |
| 700 |
# and checks that pointer_type cache not shared |
| 701 |
# between subclasses. |
| 702 |
class Base(Structure): |
| 703 |
_fields_ = [('y', c_double), |
| 704 |
('x', c_double)] |
| 705 |
base_ptr = POINTER(Base) |
| 706 |
|
| 707 |
class Mid(Base): |
| 708 |
pass |
| 709 |
Mid._fields_ = [] |
| 710 |
mid_ptr = POINTER(Mid) |
| 711 |
|
| 712 |
class Vector(Mid): |
| 713 |
pass |
| 714 |
|
| 715 |
vector_ptr = POINTER(Vector) |
| 716 |
|
| 717 |
self.assertIsNot(base_ptr, mid_ptr) |
| 718 |
self.assertIsNot(base_ptr, vector_ptr) |
| 719 |
self.assertIsNot(mid_ptr, vector_ptr) |
| 720 |
|
| 721 |
|
| 722 |
if __name__ == '__main__': |
| 723 |
unittest.main() |