# HG changeset patch # User Paul Boddie # Date 1485117997 -3600 # Node ID b833666d476c9e4be86a5f2ed67a27762bedb8d3 # Parent 37ff992ea25a5e7d44e1d9c313926e8527ee4735 Introduced common __name__ and __parent__ attributes for classes and modules and for function instances, removing __fname__, __mname__ and __oname__. diff -r 37ff992ea25a -r b833666d476c generator.py --- a/generator.py Sun Jan 22 01:16:01 2017 +0100 +++ b/generator.py Sun Jan 22 21:46:37 2017 +0100 @@ -914,7 +914,7 @@ # Special cases. - elif attrname in ("__file__", "__fname__", "__mname__", "__name__", "__oname__"): + elif attrname in ("__file__", "__name__"): path = ref.get_origin() value_type = self.string_type @@ -927,15 +927,9 @@ # Function and class names are leafnames. - elif attrname in ("__fname__", "__name__"): + elif attrname == "__name__" and not ref.has_kind(""): value = path.rsplit(".", 1)[-1] - # Object names of classes and functions are derived from - # their object paths. - - elif attrname == "__oname__": - value = path.rsplit(".", 1)[0] - # All other names just use the object path information. else: @@ -951,6 +945,16 @@ structure.append("%s /* %s */" % (constant_value, attrname)) continue + elif attrname == "__parent__": + path = ref.get_origin() + + # Parents of classes and functions are derived from their + # object paths. + + value = path.rsplit(".", 1)[0] + structure.append("{.context=0, .value=&%s}" % encode_path(value)) + continue + # Special class relationship attributes. elif is_type_attribute(attrname): diff -r 37ff992ea25a -r b833666d476c importer.py --- a/importer.py Sun Jan 22 01:16:01 2017 +0100 +++ b/importer.py Sun Jan 22 21:46:37 2017 +0100 @@ -607,7 +607,7 @@ # NOTE: Consolidate this information in a common location. - special_attributes = ("__args__", "__file__", "__fn__", "__fname__", "__mname__", "__name__", "__oname__") + special_attributes = ("__args__", "__file__", "__fn__", "__name__", "__parent__") def is_dynamic(self, ref): return not ref or not ref.static() and not ref.is_constant_alias() and not ref.is_predefined_value() diff -r 37ff992ea25a -r b833666d476c inspector.py --- a/inspector.py Sun Jan 22 01:16:01 2017 +0100 +++ b/inspector.py Sun Jan 22 21:46:37 2017 +0100 @@ -77,7 +77,7 @@ ref = self.get_builtin("module") self.set_name("__class__", ref) - self.set_name("__mname__", self.get_constant("string", self.name).reference()) + self.set_name("__name__", self.get_constant("string", self.name).reference()) self.set_name("__file__", self.get_constant("string", filename).reference()) # Reserve a constant for the encoding. @@ -524,10 +524,11 @@ self.set_name("__fn__") # special instantiator attribute self.set_name("__args__") # special instantiator attribute - # Provide leafname and object name attributes. + # Provide leafname and parent attributes. - self.set_name("__name__", self.get_constant("string", class_name.rsplit(".", 1)[-1]).reference()) - self.set_name("__oname__", self.get_constant("string", class_name.rsplit(".", 1)[0]).reference()) + parent, leafname = class_name.rsplit(".", 1) + self.set_name("__name__", self.get_constant("string", leafname).reference()) + self.set_name("__parent__") self.process_structure_node(n.code) self.exit_namespace() @@ -646,11 +647,10 @@ self.enter_namespace(name) - # Define leafname and object name attribute values for the function instance. + # Define a leafname attribute value for the function instance. ref = self.get_builtin_class("string") self.reserve_constant(function_name, name, ref.get_origin()) - self.reserve_constant(function_name, function_name.rsplit(".", 1)[0], ref.get_origin()) # Track attribute usage within the namespace. diff -r 37ff992ea25a -r b833666d476c lib/__builtins__/core.py --- a/lib/__builtins__/core.py Sun Jan 22 01:16:01 2017 +0100 +++ b/lib/__builtins__/core.py Sun Jan 22 21:46:37 2017 +0100 @@ -41,7 +41,10 @@ "Return a string representation." - return str(buffer(["<", self.__oname__, ".", self.__name__, " instance>"])) + # The string representation of the class should be provided by the + # type.__str__ method using the class as self. + + return str(buffer(["<", self.__class__, " instance>"])) __repr__ = __str__ @@ -56,13 +59,13 @@ """ self.__file__ = None - self.__mname__ = None + self.__name__ = None def __str__(self): "Return a string representation." - return self.__mname__ + return self.__name__ __repr__ = __str__ @@ -81,8 +84,8 @@ self.__fn__ = None self.__args__ = None - self.__fname__ = None - self.__oname__ = None + self.__name__ = None + self.__parent__ = None def __bool__(self): @@ -94,7 +97,9 @@ "Return a string representation." - return str(buffer([self.__oname__, ".", self.__fname__])) + # Combine the function's parent representation with the function's name. + + return str(buffer([self.__parent__, ".", self.__name__])) __repr__ = __str__ @@ -110,7 +115,10 @@ "Return a string representation." - return str(buffer([self.__oname__, ".", self.__name__])) + # With the class as self, combine the class's parent representation with + # the class's name. + + return str(buffer([self.__parent__, ".", self.__name__])) __repr__ = __str__ diff -r 37ff992ea25a -r b833666d476c lib/__builtins__/span.py --- a/lib/__builtins__/span.py Sun Jan 22 01:16:01 2017 +0100 +++ b/lib/__builtins__/span.py Sun Jan 22 21:46:37 2017 +0100 @@ -45,7 +45,7 @@ "Return a string representation." - b = buffer([self.__oname__, ".", self.__name__, "(", self.start, ", ", self.end, ", ", self.step, ")"]) + b = buffer([self.__parent__.__name__, ".", self.__name__, "(", self.start, ", ", self.end, ", ", self.step, ")"]) return str(b) __repr__ = __str__ diff -r 37ff992ea25a -r b833666d476c tests/name_attribute.py --- a/tests/name_attribute.py Sun Jan 22 01:16:01 2017 +0100 +++ b/tests/name_attribute.py Sun Jan 22 21:46:37 2017 +0100 @@ -3,21 +3,54 @@ class C: def f(self): pass +def name(x): + print x.__name__ + print x.__parent__.__name__ + print str(x) + +def attrname(x): + print x.f.__name__ + print x.f.__parent__.__name__ + print str(x.f) + c = C() print c.__name__ # C -print c.__oname__ # __main__ +print c.__parent__.__name__ # __main__ +print str(c) # <__main__.C instance> print C.__name__ # C -print C.__oname__ # __main__ -print c.f.__fname__ # f -print c.f.__oname__ # __main__.C -print C.f.__fname__ # f -print C.f.__oname__ # __main__.C +print C.__parent__.__name__ # __main__ +print str(C) # __main__.C +print c.f.__name__ # f +print c.f.__parent__.__name__ # C +print str(c.f) # __main__.C.f +print C.f.__name__ # f +print C.f.__parent__.__name__ # C +print str(C.f) # __main__.C.f + +name(c) # C + # __main__ + # <__main__.C instance> +name(C) # C + # __main__ + # __main__.C +attrname(c) # f + # C + # __main__.C.f +attrname(C) # f + # C + # __main__.C.f +name(c.f) # f + # C + # __main__.C.f +name(C.f) # f + # C + # __main__.C.f # If it were defined, operator.__name__ would be module. -print operator.__mname__ # operator +print operator.__name__ # operator # If it were defined, operator.add.__name__ would be function. -print operator.add.__fname__ # add -print operator.add.__oname__ # operator.binary +print operator.add.__name__ # add +print operator.add.__parent__.__name__ # operator.binary