# HG changeset patch # User Paul Boddie # Date 1480198029 -3600 # Node ID d35cdc51ff845ca8ee2240d67aed67396bd7adf0 # Parent 399a99086301bb90e16d4c98fa2f1347739a0d2b Added support for function names using the __fname__ attribute, combining it with __name__ support for other object kinds. diff -r 399a99086301 -r d35cdc51ff84 common.py --- a/common.py Sat Nov 26 23:05:45 2016 +0100 +++ b/common.py Sat Nov 26 23:07:09 2016 +0100 @@ -231,9 +231,26 @@ # Record the value and type for the constant. - self.constant_values[objpath] = name_ref.value, name_ref.get_origin() + self._reserve_constant(objpath, name_ref.value, name_ref.get_origin()) return name_ref + def reserve_constant(self, objpath, value, origin): + + """ + Reserve a constant within 'objpath' with the given 'value' and having a + type with the given 'origin'. + """ + + constant_name = self.get_constant_name(value) + objpath = self.get_object_path(constant_name) + self._reserve_constant(objpath, value, origin) + + def _reserve_constant(self, objpath, value, origin): + + "Store a constant for 'objpath' with the given 'value' and 'origin'." + + self.constant_values[objpath] = value, origin + def get_literal_reference(self, name, ref, items, cls): """ diff -r 399a99086301 -r d35cdc51ff84 generator.py --- a/generator.py Sat Nov 26 23:05:45 2016 +0100 +++ b/generator.py Sat Nov 26 23:07:09 2016 +0100 @@ -783,6 +783,18 @@ encode_literal_constant_value(attr))) continue + # Special cases. + + elif attrname in ("__fname__", "__name__"): + path = ref.get_origin() + local_number = self.importer.all_constants[path][path] + constant_name = "$c%d" % local_number + attr_path = "%s.%s" % (path, constant_name) + constant_number = self.optimiser.constant_numbers[attr_path] + constant_value = "__const%d" % constant_number + structure.append("%s /* %s */" % (constant_value, attrname)) + continue + structure.append(self.encode_member(origin, attrname, attr, kind)) def encode_member(self, path, name, ref, structure_type): @@ -813,16 +825,6 @@ attr_path = encode_predefined_reference("%s.%s" % (path, name)) return "{&%s, &%s} /* %s */" % (attr_path, attr_path, name) - # Special cases. - - if name == "__name__": - local_number = self.importer.all_constants[path][path] - constant_name = "$c%d" % local_number - attr_path = "%s.%s" % (path, constant_name) - constant_number = self.optimiser.constant_numbers[attr_path] - constant_value = "__const%d" % constant_number - return "%s /* %s */" % (constant_value, name) - # General undetermined members. if kind in ("", ""): diff -r 399a99086301 -r d35cdc51ff84 inspector.py --- a/inspector.py Sat Nov 26 23:05:45 2016 +0100 +++ b/inspector.py Sat Nov 26 23:07:09 2016 +0100 @@ -640,6 +640,11 @@ self.enter_namespace(name) + # Define a name attribute value for the function instance. + + ref = self.get_builtin_class("string") + self.reserve_constant(function_name, function_name, ref.get_origin()) + # Track attribute usage within the namespace. path = self.get_namespace_path() @@ -1256,12 +1261,16 @@ using the optional 'ref'. """ - init_item(self.instance_attrs, self.in_class, set) - self.instance_attrs[self.in_class].add(name) + self._set_instance_attr(self.in_class, name, ref) + + def _set_instance_attr(self, path, name, ref=None): + + init_item(self.instance_attrs, path, set) + self.instance_attrs[path].add(name) if ref: - init_item(self.instance_attr_constants, self.in_class, dict) - self.instance_attr_constants[self.in_class][name] = ref + init_item(self.instance_attr_constants, path, dict) + self.instance_attr_constants[path][name] = ref def get_initialising_value(self, value): diff -r 399a99086301 -r d35cdc51ff84 lib/__builtins__/core.py --- a/lib/__builtins__/core.py Sat Nov 26 23:05:45 2016 +0100 +++ b/lib/__builtins__/core.py Sat Nov 26 23:07:09 2016 +0100 @@ -60,6 +60,7 @@ self.__fn__ = None self.__args__ = None + self.__fname__ = None def __bool__(self): @@ -71,7 +72,7 @@ "Return a string representation." - return "" # NOTE: Could be made specific. + return self.__fname__ __repr__ = __str__ diff -r 399a99086301 -r d35cdc51ff84 tests/aliases.py --- a/tests/aliases.py Sat Nov 26 23:05:45 2016 +0100 +++ b/tests/aliases.py Sat Nov 26 23:07:09 2016 +0100 @@ -24,8 +24,8 @@ print c # "" print d # "" - print cm # "" - print dm # "" + print cm # "__main__.C.m" + print dm # "__main__.C.m" c = E d = F # E @@ -34,8 +34,8 @@ print c # "" print d # "" - print cm # "" - print dm # "" + print cm # "__main__.E.m" + print dm # "__main__.E.m" f() @@ -44,17 +44,17 @@ Em = E.m Fm = F.m -print Cm # "" -print Dm # "" -print Em # "" -print Fm # "" +print Cm # "__main__.C.m" +print Dm # "__main__.C.m" +print Em # "__main__.E.m" +print Fm # "__main__.E.m" def g(): Cm = E.m Dm = F.m # E.m - print Cm # "" - print Dm # "" + print Cm # "__main__.E.m" + print Dm # "__main__.E.m" g() @@ -63,11 +63,14 @@ Em = C.m Fm = D.m # C.m - print Cm # "" - print Dm # "" + print Em # "__main__.C.m" + print Fm # "__main__.C.m" h() +print Em # "__main__.C.m" +print Fm # "__main__.C.m" + Ci = C() Ei = E() @@ -89,6 +92,8 @@ j() +print Ei # "__main__.C" + L = [] M = [1]