Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Fix node init
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Nov 5, 2024
1 parent d8f0416 commit 857f8a5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/faebryk/core/cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import logging
from importlib.metadata import Distribution

from faebryk.libs.util import at_exit

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -114,3 +116,11 @@ def compile_and_load():
from faebryk_core_cpp_editable import * # type: ignore # noqa: E402, F403
else:
from faebryk_core_cpp import * # type: ignore # noqa: E402, F403


def cleanup():
print("\n--- Nanobind leakage analysis ".ljust(80, "-"))
# nanobind automatically prints leaked objects at exit


at_exit(cleanup)
1 change: 0 additions & 1 deletion src/faebryk/core/cpp/src/graph/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Node::Node()
, children(GraphInterfaceHierarchical::factory(true))
, parent(GraphInterfaceHierarchical::factory(false)) {

printf("Node constructor\n");
this->self->set_name("self");

this->children->set_name("children");
Expand Down
4 changes: 0 additions & 4 deletions src/faebryk/core/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ def setup_field(name, obj):

def __new__(cls, *args, **kwargs):
out = super().__new__(cls)
print("Called __new__", type(out))
return out

def _setup(self) -> None:
Expand Down Expand Up @@ -483,7 +482,6 @@ def _setup(self) -> None:
base.__postinit__(self)

def __init__(self):
print("Called __init__", type(self))
super().__init__()
CNode.transfer_ownership(self)
assert not hasattr(self, "_is_setup")
Expand All @@ -494,11 +492,9 @@ def __preinit__(self): ...
def __postinit__(self): ...

def __post_init__(self, *args, **kwargs):
print("Called __post_init__", type(self))
self._setup()

def __init_subclass__(cls, *, init: bool = True) -> None:
print("Called __init_subclass__", cls.__qualname__)
cls._init = init
post_init_decorator(cls)

Expand Down
13 changes: 8 additions & 5 deletions src/faebryk/libs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,20 +875,23 @@ def post_init_decorator(cls):
__init__ has been called.
Attention: Needs to be called on cls in __init_subclass__ of decorated class.
"""
if getattr(cls, "__post_init_decorator", False):
return cls
post_init_base = getattr(cls, "__post_init_decorator", None)
if post_init_base is cls:
return

original_init = cls.__init__
if post_init_base:
original_init = cls.__original_init__
else:
original_init = cls.__init__

def new_init(self, *args, **kwargs):
original_init(self, *args, **kwargs)
if hasattr(self, "__post_init__") and type(self) is cls:
self.__post_init__(*args, **kwargs)

print("Setting __init__", cls.__qualname__)
cls.__init__ = new_init
cls.__original_init__ = original_init
cls.__post_init_decorator = True
cls.__post_init_decorator = cls
return cls


Expand Down
1 change: 1 addition & 0 deletions test/core/cpp/test_importcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_pynode():

n = Node()
print(n)
print("---")

class SubNode(Node):
a: Node
Expand Down

0 comments on commit 857f8a5

Please sign in to comment.