MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_unregister_native_enum_when_destroyed

Function test_unregister_native_enum_when_destroyed

tests/test_native_enum.py:324–377  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

322
323
324def 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

Callers

nothing calls this directly

Calls 2

test_altitude_enumFunction · 0.85
test_altitude_bindingFunction · 0.85

Tested by

no test coverage detected