# HG changeset patch # User Paul Boddie # Date 1481387536 -3600 # Node ID 6d63ae9b8b7afcb265d0ed40aee620f664918112 # Parent 719d231198184b32d72c8904341395b97690a711 Tidied up built-in type path and module computation, also handling stray None constants produced by Sliceobj nodes. diff -r 719d23119818 -r 6d63ae9b8b7a common.py --- a/common.py Sat Dec 10 14:04:01 2016 +0100 +++ b/common.py Sat Dec 10 17:32:16 2016 +0100 @@ -915,6 +915,22 @@ assigned.append(attrname) return assigned +# Type and module functions. + +def get_builtin_module(name): + + "Return the module name containing the given type 'name'." + + # NOTE: This makes assumptions about the __builtins__ structure. + + return name == "string" and "str" or name == "NoneType" and "none" or name + +def get_builtin_type(name): + + "Return the type name provided by the given Python value 'name'." + + return name == "str" and "string" or name + # Useful data. predefined_constants = "False", "None", "NotImplemented", "True" diff -r 719d23119818 -r 6d63ae9b8b7a generator.py --- a/generator.py Sat Dec 10 14:04:01 2016 +0100 +++ b/generator.py Sat Dec 10 17:32:16 2016 +0100 @@ -19,7 +19,7 @@ this program. If not, see . """ -from common import CommonOutput +from common import CommonOutput, get_builtin_module, get_builtin_type from encoders import encode_bound_reference, encode_function_pointer, \ encode_instantiator_pointer, \ encode_literal_constant, encode_literal_constant_member, \ @@ -446,8 +446,8 @@ # NOTE: This makes assumptions about the __builtins__ structure. - modname = value.__class__.__name__ - typename = modname == "str" and "string" or modname + typename = get_builtin_type(value.__class__.__name__) + modname = get_builtin_module(typename) ref = Reference("", "__builtins__.%s.%s" % (modname, typename)) self.make_constant(f_decls, f_defs, ref, const_path, structure_name, value) diff -r 719d23119818 -r 6d63ae9b8b7a inspector.py --- a/inspector.py Sat Dec 10 14:04:01 2016 +0100 +++ b/inspector.py Sat Dec 10 17:32:16 2016 +0100 @@ -21,7 +21,8 @@ """ from branching import BranchTracker -from common import CommonModule, get_argnames, init_item, predefined_constants +from common import CommonModule, get_argnames, get_builtin_type, init_item, \ + predefined_constants from modules import BasicModule, CacheWritingModule, InspectionNaming from errors import InspectError from referencing import Reference @@ -294,7 +295,7 @@ elif isinstance(n, compiler.ast.Const): typename = n.value.__class__.__name__ - return self.get_literal_instance(n, typename == "str" and "string" or typename) + return self.get_literal_instance(n, get_builtin_type(typename)) elif isinstance(n, compiler.ast.Dict): return self.get_literal_instance(n, "dict") @@ -1386,6 +1387,11 @@ "For node 'n', return a reference to an instance of 'name'." + # Handle stray None constants (Sliceobj seems to produce them). + + if name == "NoneType": + return self.process_name_node(compiler.ast.Name("None")) + # Get a reference to the built-in class. ref = self.get_builtin_class(name) diff -r 719d23119818 -r 6d63ae9b8b7a modules.py --- a/modules.py Sat Dec 10 14:04:01 2016 +0100 +++ b/modules.py Sat Dec 10 17:32:16 2016 +0100 @@ -20,7 +20,7 @@ this program. If not, see . """ -from common import init_item, remove_items, CommonModule +from common import get_builtin_module, init_item, remove_items, CommonModule from encoders import decode_modifier_term, decode_usage, encode_modifiers, encode_usage from referencing import decode_reference, Reference from results import ResolvedNameRef @@ -299,11 +299,12 @@ # NOTE: This makes assumptions about the __builtins__ structure. - modname = name == "string" and "str" or name + modname = get_builtin_module(name) + module_name = "__builtins__.%s" % modname - module_name = "__builtins__.%s" % modname if self.name != module_name: self.queue_module(module_name, True) + return Reference("", "__builtins__.%s.%s" % (modname, name)) def get_object(self, path, defer=True): diff -r 719d23119818 -r 6d63ae9b8b7a translator.py --- a/translator.py Sat Dec 10 14:04:01 2016 +0100 +++ b/translator.py Sat Dec 10 17:32:16 2016 +0100 @@ -321,7 +321,8 @@ # NOTE: This makes assumptions about the __builtins__ structure. - return self.importer.get_object("__builtins__.%s.%s" % (name, name)) + modname = get_builtin_module(name) + return self.importer.get_object("__builtins__.%s.%s" % (modname, name)) def is_method(self, path): @@ -410,6 +411,11 @@ For node 'n', return a reference for the type of the given 'name'. """ + # Handle stray None constants (Sliceobj seems to produce them). + + if name == "NoneType": + return self.process_name_node(compiler.ast.Name("None")) + ref = self.get_builtin_class(name) if name in ("dict", "list", "tuple"):