# HG changeset patch # User Paul Boddie # Date 1480954895 -3600 # Node ID 0867d8924ed94b6d79df90267600782018df9f0d # Parent e39bf1c565b856807fc7b611048f51ab4921a398 Support classes whose names are redefined, making sure that such classes are initialised and avoiding re-initialisation of their type attributes. Added tests of class and function name redefinition. diff -r e39bf1c565b8 -r 0867d8924ed9 common.py --- a/common.py Mon Dec 05 17:19:12 2016 +0100 +++ b/common.py Mon Dec 05 17:21:35 2016 +0100 @@ -328,7 +328,7 @@ return self.process_structure_node(assignment) - def process_assignment_for_function(self, original_name, source): + def process_assignment_for_object(self, original_name, source): """ Return an assignment operation making 'original_name' refer to the given diff -r e39bf1c565b8 -r 0867d8924ed9 tests/class_names.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/class_names.py Mon Dec 05 17:21:35 2016 +0100 @@ -0,0 +1,14 @@ +class C: + pass + +def c(): + return 456 + +def f(): + return C() + +print f() # 123 + +C = c + +print f() # 456 diff -r e39bf1c565b8 -r 0867d8924ed9 tests/function_names.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/function_names.py Mon Dec 05 17:21:35 2016 +0100 @@ -0,0 +1,14 @@ +def f(): + return 123 + +def g(): + return 456 + +def h(): + return f() + +print h() # 123 + +f = g + +print h() # 456 diff -r e39bf1c565b8 -r 0867d8924ed9 translator.py --- a/translator.py Mon Dec 05 17:19:12 2016 +0100 +++ b/translator.py Mon Dec 05 17:21:35 2016 +0100 @@ -774,10 +774,21 @@ "Process the given class node 'n'." + class_name = self.get_object_path(n.name) + + # Where a class is set conditionally or where the name may refer to + # different values, assign the name. + + ref = self.importer.identify(class_name) + + if not ref.static(): + self.process_assignment_for_object( + n.name, make_expression("((__attr) {0, &%s})" % + encode_path(class_name))) + self.enter_namespace(n.name) if self.have_object(): - class_name = self.get_namespace_path() self.write_comment("Class: %s" % class_name) self.initialise_inherited_members(class_name) @@ -800,6 +811,11 @@ if ref: continue + # Ignore special type attributes. + + if is_type_attribute(name): + continue + # Reference inherited attributes. ref = self.importer.identify(path) @@ -941,11 +957,11 @@ ref = self.importer.identify(objpath) if self.in_conditional or self.in_function: - self.process_assignment_for_function(original_name, compiler.ast.Name(name)) + self.process_assignment_for_object(original_name, compiler.ast.Name(name)) elif not ref.static(): context = self.is_method(objpath) - self.process_assignment_for_function(original_name, + self.process_assignment_for_object(original_name, make_expression("((__attr) {%s, &%s})" % ( context and "&%s" % encode_path(context) or "0", encode_path(objpath))))