|
59 | 59 | ENUM_TYPES_AND_MEMBERS = ( |
60 | 60 | (m.smallenum, SMALLENUM_MEMBERS), |
61 | 61 | (m.color, COLOR_MEMBERS), |
62 | | - (m.altitude, ALTITUDE_MEMBERS), |
63 | 62 | (m.flags_uchar, FLAGS_UCHAR_MEMBERS), |
64 | 63 | (m.flags_uint, FLAGS_UINT_MEMBERS), |
65 | 64 | (m.export_values, EXPORT_VALUES_MEMBERS), |
@@ -320,3 +319,59 @@ def test_native_enum_missing_finalize_failure(): |
320 | 319 | if not isinstance(m.native_enum_missing_finalize_failure, str): |
321 | 320 | m.native_enum_missing_finalize_failure() |
322 | 321 | pytest.fail("Process termination expected.") |
| 322 | + |
| 323 | + |
| 324 | +def test_unregister_native_enum_when_destroyed(): |
| 325 | + # For stability when running tests in parallel, this test should be the |
| 326 | + # only one that touches `m.altitude` or calls `m.bind_altitude`. |
| 327 | + |
| 328 | + def test_altitude_enum(): |
| 329 | + # Logic copied from test_enum_type / test_enum_members. |
| 330 | + # We don't test altitude there to avoid possible clashes if |
| 331 | + # parallelizing against other tests in this file, and we also |
| 332 | + # don't want to hold any references to the enumerators that |
| 333 | + # would prevent GCing the enum type below. |
| 334 | + assert isinstance(m.altitude, enum.EnumMeta) |
| 335 | + assert m.altitude.__module__ == m.__name__ |
| 336 | + for name, value in ALTITUDE_MEMBERS: |
| 337 | + assert m.altitude[name].value == value |
| 338 | + |
| 339 | + def test_altitude_binding(): |
| 340 | + assert m.is_high_altitude(m.altitude.high) |
| 341 | + assert not m.is_high_altitude(m.altitude.low) |
| 342 | + assert m.get_altitude() is m.altitude.high |
| 343 | + with pytest.raises(TypeError, match="incompatible function arguments"): |
| 344 | + m.is_high_altitude("oops") |
| 345 | + |
| 346 | + m.bind_altitude(m) |
| 347 | + test_altitude_enum() |
| 348 | + test_altitude_binding() |
| 349 | + |
| 350 | + if env.TYPES_ARE_IMMORTAL: |
| 351 | + pytest.skip("can't GC type objects on this platform") |
| 352 | + |
| 353 | + # Delete the enum type. Returning an instance from Python should fail |
| 354 | + # rather than accessing a deleted object. |
| 355 | + pytest.delattr_and_ensure_destroyed((m, "altitude")) |
| 356 | + with pytest.raises(TypeError, match="Unable to convert function return"): |
| 357 | + m.get_altitude() |
| 358 | + with pytest.raises(TypeError, match="incompatible function arguments"): |
| 359 | + m.is_high_altitude("oops") |
| 360 | + |
| 361 | + # Recreate the enum type; should not have any duplicate-binding error |
| 362 | + m.bind_altitude(m) |
| 363 | + test_altitude_enum() |
| 364 | + test_altitude_binding() |
| 365 | + |
| 366 | + # Remove the pybind11 capsule without removing the type; enum is still |
| 367 | + # usable but can't be passed to/from bound functions |
| 368 | + del m.altitude.__pybind11_native_enum__ |
| 369 | + pytest.gc_collect() |
| 370 | + test_altitude_enum() # enum itself still works |
| 371 | + |
| 372 | + with pytest.raises(TypeError, match="Unable to convert function return"): |
| 373 | + m.get_altitude() |
| 374 | + with pytest.raises(TypeError, match="incompatible function arguments"): |
| 375 | + m.is_high_altitude(m.altitude.high) |
| 376 | + |
| 377 | + del m.altitude |
0 commit comments