# HG changeset patch # User Paul Boddie # Date 1480349083 -3600 # Node ID 85569702637b7653ac184f2625808943669f1ec8 # Parent c3fd0320f50b60af48c3429b1e6befbefcc25808 Added module name and filename information to module instances. diff -r c3fd0320f50b -r 85569702637b generator.py --- a/generator.py Mon Nov 28 16:23:38 2016 +0100 +++ b/generator.py Mon Nov 28 17:04:43 2016 +0100 @@ -788,9 +788,16 @@ # Special cases. - elif attrname in ("__fname__", "__name__"): + elif attrname in ("__file__", "__fname__", "__mname__", "__name__"): path = ref.get_origin() - local_number = self.importer.all_constants[path][path] + + if attrname == "__file__": + module = self.importer.get_module(path) + value = module.filename + else: + value = path + + local_number = self.importer.all_constants[path][value] constant_name = "$c%d" % local_number attr_path = "%s.%s" % (path, constant_name) constant_number = self.optimiser.constant_numbers[attr_path] diff -r c3fd0320f50b -r 85569702637b inspector.py --- a/inspector.py Mon Nov 28 16:23:38 2016 +0100 +++ b/inspector.py Mon Nov 28 17:04:43 2016 +0100 @@ -70,14 +70,14 @@ # Detect and record imports and globals declared in the module. - self.assign_general_local("__name__", self.get_constant("string", self.name)) - self.assign_general_local("__file__", self.get_constant("string", filename)) self.process_structure(self.astnode) # Set the class of the module after the definition has occurred. ref = self.get_builtin("module") self.set_name("__class__", ref) + self.set_name("__mname__", self.get_constant("string", self.name).reference()) + self.set_name("__file__", self.get_constant("string", filename).reference()) # Get module-level attribute usage details. diff -r c3fd0320f50b -r 85569702637b lib/__builtins__/core.py --- a/lib/__builtins__/core.py Mon Nov 28 16:23:38 2016 +0100 +++ b/lib/__builtins__/core.py Mon Nov 28 17:04:43 2016 +0100 @@ -49,11 +49,20 @@ "The class of module objects." + def __init__(self): + + """ + Reserve special attributes for module instances. + """ + + self.__file__ = None + self.__mname__ = None + def __str__(self): "Return a string representation." - return self.__name__ + return self.__mname__ __repr__ = __str__