pybind/pybind11

[BUG]: user can call other functions before calling super().__init__

Open

#3652 aperta il 26 gen 2022

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)C++ (2005 fork)batch import
enhancementhelp wanted

Metriche repository

Star
 (14.677 star)
Metriche merge PR
 (Merge medio 5g 2h) (10 PR mergiate in 30 g)

Descrizione

Required prerequisites

Problem description

Right now we have a check that warns a user that they forgot to call super().__init__() in their overridden __init__ function. This is really useful and handles most bugs that users might accidentally make if they're not super familiar with pybind11.

However, if they don't call super().__init__ first, then other kinds of potentially undefined behavior could occur, which are also going to be difficult to diagnose from pure python. I wonder if there's a clever way that we can redirect all python method calls / attribute accesses on an object IFF that object has an __init__ to some method that tells the user to call super().__init__ first? Then after the base __init__ is called that sets the methods up to point at the correct functions?

I don't think we can add checks at any method call because the performance hit would be too high. It's possible other ways to do this would also result in performance hits.

Reproducible example code

This doesn't actually break in all cases, but I'd like this to throw an exception if possible.


from pybound_library import CppClass

class PyClass(CppClass):
    def __init__(self):
        self.cppMethod()
        super().__init__()

Guida contributor