1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/inheritance.py Sun Nov 27 22:40:49 2016 +0100
1.3 @@ -0,0 +1,19 @@
1.4 +class C:
1.5 + a = 1
1.6 + b = 2
1.7 +
1.8 +class D(C):
1.9 + pass
1.10 +
1.11 +def f():
1.12 + C.a = 3 # only changes C.a, not D.a
1.13 +
1.14 +print C.a # 1
1.15 +print D.a # 1
1.16 +print C.b # 2
1.17 +print D.b # 2
1.18 +
1.19 +f()
1.20 +
1.21 +print C.a # 3
1.22 +print D.a # 1
2.1 --- a/translator.py Sun Nov 27 20:54:08 2016 +0100
2.2 +++ b/translator.py Sun Nov 27 22:40:49 2016 +0100
2.3 @@ -754,10 +754,37 @@
2.4 class_name = self.get_namespace_path()
2.5 self.write_comment("Class: %s" % class_name)
2.6
2.7 + self.initialise_inherited_members(class_name)
2.8 +
2.9 self.process_structure(n)
2.10 + self.write_comment("End class: %s" % class_name)
2.11
2.12 self.exit_namespace()
2.13
2.14 + def initialise_inherited_members(self, class_name):
2.15 +
2.16 + "Initialise members of 'class_name' inherited from its ancestors."
2.17 +
2.18 + for name, path in self.importer.all_class_attrs[class_name].items():
2.19 + target = "%s.%s" % (class_name, name)
2.20 +
2.21 + # Ignore attributes with definitions.
2.22 +
2.23 + ref = self.importer.identify(target)
2.24 + if ref:
2.25 + continue
2.26 +
2.27 + # Reference inherited attributes.
2.28 +
2.29 + ref = self.importer.identify(path)
2.30 + if ref and not ref.static():
2.31 + parent, attrname = path.rsplit(".", 1)
2.32 +
2.33 + self.writestmt("__store_via_object(&%s, %s, __load_via_object(&%s, %s));" % (
2.34 + encode_path(class_name), encode_symbol("pos", name),
2.35 + encode_path(parent), encode_symbol("pos", attrname)
2.36 + ))
2.37 +
2.38 def process_function_body_node(self, n):
2.39
2.40 """
2.41 @@ -816,10 +843,18 @@
2.42 else:
2.43 return
2.44
2.45 + # Produce an appropriate access to an attribute's value.
2.46 +
2.47 + parameters = self.importer.function_parameters.get(self.get_namespace_path())
2.48 + if parameters and name in parameters:
2.49 + name_to_value = "%s->value" % name
2.50 + else:
2.51 + name_to_value = "%s.value" % name
2.52 +
2.53 # Write a test that raises a TypeError upon failure.
2.54
2.55 - self.writestmt("if (!__test_%s_%s(%s->value, %s)) __raise_type_error();" % (
2.56 - guard, guard_type, name, argstr))
2.57 + self.writestmt("if (!__test_%s_%s(%s, %s)) __raise_type_error();" % (
2.58 + guard, guard_type, name_to_value, argstr))
2.59
2.60 def process_function_node(self, n):
2.61