Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function do_all does not rebuild the child class' parent fields #5

Open
Elfansoer opened this issue Dec 9, 2021 · 0 comments
Open

Comments

@Elfansoer
Copy link
Contributor

Current do_all function rebuilds Python class from C class through parameters. However, the callback function for rebuilding child class only passes child class' fields; it does not pass parent's fields. Consider a simple inheritance Sanajeh-Python program:

class SampleClass:
  def __init__(self):
    self.x: int = 0
    self.y: int = 0

  def SampleClass(self,seed: int):
    random.seed(seed) # this is hardcoded and must exist
    self.x = random.getrandbits(32)%50
    self.y = random.getrandbits(32)%50

class ChildClass(SampleClass):
  def __init__(self):
    super().__init__()
    self.color: int = 0

  def ChildClass(self,seed: int):
    super().SampleClass(seed)
    self.color = 150

# Main function
def main(allocator, do_render):
  allocator.initialize()
  allocator.parallel_new(SampleClass, 1)
  allocator.parallel_new(ChildClass, 1)

  # do_all function
  def do_render(tmp):
    clr = 999
    if hasattr(tmp,'color'):
      clr = tmp.color
    print("tmp  {} {} {}".format(tmp.x,tmp.y,clr))
  
  for i in range(1):
    # for rendering, do all
    allocator.do_all(SampleClass, do_render)
    allocator.do_all(ChildClass, do_render)

The rebuild class in do_all function is defined as:

@classmethod
def __rebuild_SampleClass(cls, random_state, x, y):
        new_object = cls.__new__(cls)
        new_object.random_state_ = random_state
        new_object.x = x
        new_object.y = y
        return new_object

@classmethod
def __rebuild_ChildClass(cls, color):
        new_object = cls.__new__(cls)
        new_object.color = color
        return new_object

while the respective cdef C function for rebuilding the class are defined as

int SampleClass_do_all(void (*pf)(int, int, int));
int ChildClass_do_all(void (*pf)(int));

Notice that the ChildClass rebuilding does not take any x or y attributes inherited from its parent.

Suggested solution is to change the cdef functions as well as the __rebuild function so that it checks parents' fields and put them too as parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant