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

Function test_invalid_self

tests/test_factory_constructors.py:481–531  ·  view source on GitHub ↗

Tests invocation of the pybind-registered base class with an invalid `self` argument.

()

Source from the content-addressed store, hash-verified

479
480
481def test_invalid_self():
482 """Tests invocation of the pybind-registered base class with an invalid `self` argument."""
483
484 class NotPybindDerived:
485 pass
486
487 # Attempts to initialize with an invalid type passed as `self`:
488 class BrokenTF1(m.TestFactory1):
489 def __init__(self, bad):
490 if bad == 1:
491 a = m.TestFactory2(tag.pointer, 1)
492 m.TestFactory1.__init__(a, tag.pointer)
493 elif bad == 2:
494 a = NotPybindDerived()
495 m.TestFactory1.__init__(a, tag.pointer)
496
497 # Same as above, but for a class with an alias:
498 class BrokenTF6(m.TestFactory6):
499 def __init__(self, bad):
500 if bad == 0:
501 m.TestFactory6.__init__()
502 elif bad == 1:
503 a = m.TestFactory2(tag.pointer, 1)
504 m.TestFactory6.__init__(a, tag.base, 1)
505 elif bad == 2:
506 a = m.TestFactory2(tag.pointer, 1)
507 m.TestFactory6.__init__(a, tag.alias, 1)
508 elif bad == 3:
509 m.TestFactory6.__init__(
510 NotPybindDerived.__new__(NotPybindDerived), tag.base, 1
511 )
512 elif bad == 4:
513 m.TestFactory6.__init__(
514 NotPybindDerived.__new__(NotPybindDerived), tag.alias, 1
515 )
516
517 for arg in (1, 2):
518 with pytest.raises(TypeError) as excinfo:
519 BrokenTF1(arg)
520 assert (
521 str(excinfo.value)
522 == "__init__(self, ...) called with invalid or missing `self` argument"
523 )
524
525 for arg in (0, 1, 2, 3, 4):
526 with pytest.raises(TypeError) as excinfo:
527 BrokenTF6(arg)
528 assert (
529 str(excinfo.value)
530 == "__init__(self, ...) called with invalid or missing `self` argument"
531 )

Callers

nothing calls this directly

Calls 3

BrokenTF1Class · 0.85
strClass · 0.85
BrokenTF6Class · 0.85

Tested by

no test coverage detected