1 #!/usr/bin/env python 2 3 """ 4 Import logic. 5 6 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 7 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 from errors import ProgramError 24 from os.path import exists, extsep, getmtime, join 25 from os import listdir, makedirs, remove 26 from common import init_item, readfile, writefile 27 from modules import CachedModule 28 from referencing import Reference 29 import inspector 30 import sys 31 32 class Importer: 33 34 "An import machine, searching for and loading modules." 35 36 def __init__(self, path, cache=None, verbose=False): 37 38 """ 39 Initialise the importer with the given search 'path' - a list of 40 directories to search for Python modules. 41 42 The optional 'cache' should be the name of a directory used to store 43 cached module information. 44 45 The optional 'verbose' parameter causes output concerning the activities 46 of the object to be produced if set to a true value (not the default). 47 """ 48 49 self.path = path 50 self.cache = cache 51 self.verbose = verbose 52 53 # Module importing queue, required modules, removed modules and active 54 # modules in the final program. 55 56 self.to_import = set() 57 self.required = set(["__main__"]) 58 self.removed = {} 59 self.modules = {} 60 61 # Module relationships and invalidated cached modules. 62 63 self.accessing_modules = {} 64 self.invalidated = set() 65 66 # Basic program information. 67 68 self.objects = {} 69 self.classes = {} 70 self.function_parameters = {} 71 self.function_defaults = {} 72 self.function_locals = {} 73 self.function_targets = {} 74 self.function_arguments = {} 75 76 # Unresolved names. 77 78 self.missing = set() 79 80 # Derived information. 81 82 self.subclasses = {} 83 84 # Attributes of different object types. 85 86 self.all_class_attrs = {} 87 self.all_instance_attrs = {} 88 self.all_instance_attr_constants = {} 89 self.all_combined_attrs = {} 90 self.all_module_attrs = {} 91 self.all_shadowed_attrs = {} 92 93 # References to external names and aliases within program units. 94 95 self.all_name_references = {} 96 self.all_initialised_names = {} 97 self.all_aliased_names = {} 98 99 # General attribute accesses. 100 101 self.all_attr_accesses = {} 102 self.all_const_accesses = {} 103 self.all_attr_access_modifiers = {} 104 105 # Constant literals and values. 106 107 self.all_constants = {} 108 self.all_constant_values = {} 109 110 self.make_cache() 111 112 def make_cache(self): 113 if self.cache and not exists(self.cache): 114 makedirs(self.cache) 115 116 def check_cache(self, details): 117 118 """ 119 Check whether the cache applies for the given 'details', invalidating it 120 if it does not. 121 """ 122 123 recorded_details = self.get_cache_details() 124 125 if recorded_details != details: 126 self.remove_cache() 127 128 writefile(self.get_cache_details_filename(), details) 129 130 def get_cache_details_filename(self): 131 132 "Return the filename for the cache details." 133 134 return join(self.cache, "$details") 135 136 def get_cache_details(self): 137 138 "Return details of the cache." 139 140 details_filename = self.get_cache_details_filename() 141 142 if not exists(details_filename): 143 return None 144 else: 145 return readfile(details_filename) 146 147 def remove_cache(self): 148 149 "Remove the contents of the cache." 150 151 for filename in listdir(self.cache): 152 remove(join(self.cache, filename)) 153 154 def to_cache(self): 155 156 "Write modules to the cache." 157 158 if self.cache: 159 for module_name, module in self.modules.items(): 160 module.to_cache(join(self.cache, module_name)) 161 162 # Object retrieval and storage. 163 164 def get_object(self, name): 165 166 """ 167 Return a reference for the given 'name' or None if no such object 168 exists. 169 """ 170 171 return self.objects.get(name) 172 173 def set_object(self, name, value=None): 174 175 "Set the object with the given 'name' and the given 'value'." 176 177 if isinstance(value, Reference): 178 ref = value.alias(name) 179 else: 180 ref = Reference(value, name) 181 182 self.objects[name] = ref 183 184 # Identification of both stored object names and name references. 185 186 def identify(self, name): 187 188 "Identify 'name' using stored object and external name records." 189 190 return self.objects.get(name) or self.all_name_references.get(name) 191 192 # Indirect object retrieval. 193 194 def get_attributes(self, ref, attrname): 195 196 """ 197 Return attributes provided by 'ref' for 'attrname'. Class attributes 198 may be provided by instances. 199 """ 200 201 kind = ref.get_kind() 202 if kind == "<class>": 203 ref = self.get_class_attribute(ref.get_origin(), attrname) 204 return ref and set([ref]) or set() 205 elif kind == "<instance>": 206 return self.get_combined_attributes(ref.get_origin(), attrname) 207 elif kind == "<module>": 208 ref = self.get_module_attribute(ref.get_origin(), attrname) 209 return ref and set([ref]) or set() 210 else: 211 return set() 212 213 def get_class_attribute(self, object_type, attrname): 214 215 "Return from 'object_type' the details of class attribute 'attrname'." 216 217 attrs = self.all_class_attrs.get(object_type) 218 attr = attrs and attrs.get(attrname) 219 return attr and self.get_object(attr) 220 221 def get_instance_attributes(self, object_type, attrname): 222 223 """ 224 Return from 'object_type' the details of instance attribute 'attrname'. 225 """ 226 227 consts = self.all_instance_attr_constants.get(object_type) 228 attrs = set() 229 for attr in self.all_instance_attrs[object_type].get(attrname, []): 230 attrs.add(consts and consts.get(attrname) or Reference("<var>", attr)) 231 return attrs 232 233 def get_combined_attributes(self, object_type, attrname): 234 235 """ 236 Return from 'object_type' the details of class or instance attribute 237 'attrname'. 238 """ 239 240 ref = self.get_class_attribute(object_type, attrname) 241 refs = ref and set([ref]) or set() 242 refs.update(self.get_instance_attributes(object_type, attrname)) 243 return refs 244 245 def get_module_attribute(self, object_type, attrname): 246 247 "Return from 'object_type' the details of module attribute 'attrname'." 248 249 if attrname in self.all_module_attrs[object_type]: 250 return self.get_object("%s.%s" % (object_type, attrname)) 251 else: 252 return None 253 254 # Convenience methods for deducing which kind of object provided an 255 # attribute. 256 257 def get_attribute_provider(self, ref, attrname): 258 259 """ 260 Return the kind of provider of the attribute accessed via 'ref' using 261 'attrname'. 262 """ 263 264 kind = ref.get_kind() 265 266 if kind in ["<class>", "<module>"]: 267 return kind 268 else: 269 return self.get_instance_attribute_provider(ref.get_origin(), attrname) 270 271 def get_instance_attribute_provider(self, object_type, attrname): 272 273 """ 274 Return the kind of provider of the attribute accessed via an instance of 275 'object_type' using 'attrname'. 276 """ 277 278 if self.get_class_attribute(object_type, attrname): 279 return "<class>" 280 else: 281 return "<instance>" 282 283 # Module management. 284 285 def queue_module(self, name, accessor, required=False): 286 287 """ 288 Queue the module with the given 'name' for import from the given 289 'accessor' module. If 'required' is true (it is false by default), the 290 module will be required in the final program. 291 """ 292 293 if not self.modules.has_key(name): 294 self.to_import.add(name) 295 296 if required: 297 self.required.add(name) 298 299 init_item(self.accessing_modules, name, set) 300 self.accessing_modules[name].add(accessor.name) 301 302 def get_modules(self): 303 304 "Return all modules known to the importer." 305 306 return self.modules.values() 307 308 def get_module(self, name): 309 310 "Return the module with the given 'name'." 311 312 if not self.modules.has_key(name): 313 return None 314 315 return self.modules[name] 316 317 # Program operations. 318 319 def initialise(self, filename, reset=False): 320 321 """ 322 Initialise a program whose main module is 'filename', resetting the 323 cache if 'reset' is true. Return the main module. 324 """ 325 326 if reset: 327 self.remove_cache() 328 self.check_cache(filename) 329 330 # Load the program itself. 331 332 m = self.load_from_file(filename) 333 334 # Load any queued modules. 335 336 while self.to_import: 337 for name in list(self.to_import): # avoid mutation issue 338 self.load(name) 339 340 # Resolve dependencies between modules. 341 342 self.resolve() 343 344 # Record the type of all classes. 345 346 self.type_ref = self.get_object("__builtins__.type") 347 348 # Resolve dependencies within the program. 349 350 for module in self.modules.values(): 351 module.complete() 352 353 # Remove unneeded modules. 354 355 all_modules = self.modules.items() 356 357 for name, module in all_modules: 358 if name not in self.required: 359 module.unpropagate() 360 del self.modules[name] 361 self.removed[name] = module 362 363 # Collect redundant objects. 364 365 for module in self.removed.values(): 366 module.collect() 367 368 # Assert module objects where aliases have been removed. 369 370 for name in self.required: 371 if not self.objects.has_key(name): 372 self.objects[name] = Reference("<module>", name) 373 374 return m 375 376 def finalise(self): 377 378 """ 379 Finalise the inspected program, returning whether the program could be 380 finalised. 381 """ 382 383 self.finalise_classes() 384 self.to_cache() 385 386 if self.missing: 387 return False 388 389 self.set_class_types() 390 self.define_instantiators() 391 self.collect_constants() 392 393 return True 394 395 # Supporting operations. 396 397 def resolve(self): 398 399 "Resolve dependencies between modules." 400 401 self.waiting = {} 402 self.depends = {} 403 404 for module in self.modules.values(): 405 406 # Resolve all deferred references in each module. 407 408 original_deferred = [] 409 410 for ref in module.deferred: 411 412 # Retain original references for caching. 413 414 original_deferred.append(ref.copy()) 415 416 # Update references throughout the program. 417 418 found = self.find_dependency(ref) 419 if not found: 420 self.missing.add((module.name, ref.get_origin())) 421 422 # Record the resolved names and identify required modules. 423 424 else: 425 # Find the providing module of this reference. 426 # Where definitive details of the origin cannot be found, 427 # identify the provider using the deferred reference. 428 # NOTE: This may need to test for static origins. 429 430 provider = self.get_module_provider(found.unresolved() and ref or found) 431 ref.mutate(found) 432 433 # Record any external dependency. 434 435 if provider and provider != module.name: 436 437 # Record the provider dependency. 438 439 module.required.add(provider) 440 self.accessing_modules[provider].add(module.name) 441 442 # Postpone any inclusion of the provider until this 443 # module becomes required. 444 445 if module.name not in self.required: 446 init_item(self.waiting, module.name, set) 447 self.waiting[module.name].add(provider) 448 if self.verbose: 449 print >>sys.stderr, "Noting", provider, "for", ref, "from", module.name 450 451 # Make this module required in the accessing module. 452 453 elif provider not in self.required: 454 self.required.add(provider) 455 if self.verbose: 456 print >>sys.stderr, "Requiring", provider, "for", ref, "from", module.name 457 458 # Record a module ordering dependency. 459 460 self.test_dependency(found, module.name, provider) 461 462 module.deferred = original_deferred 463 464 # Check modules again to see if they are now required and should now 465 # cause the inclusion of other modules providing objects to the program. 466 467 for module_name in self.waiting.keys(): 468 self.require_providers(module_name) 469 470 def test_dependency(self, ref, module_name, provider): 471 472 """ 473 Test 'ref' for establishing a dependency from 'module_name' to 474 'provider'. 475 """ 476 477 if not ref.static() or self.uses_dynamic_callable(ref): 478 self.add_provider(module_name, provider) 479 480 def add_provider(self, module_name, provider): 481 482 "Add a dependency for 'module_name' of 'provider'." 483 484 init_item(self.depends, module_name, set) 485 self.depends[module_name].add(provider) 486 487 def require_providers(self, module_name): 488 489 """ 490 Test if 'module_name' is itself required and, if so, require modules 491 containing objects provided to the module. 492 """ 493 494 if module_name in self.required and self.waiting.has_key(module_name): 495 for provider in self.waiting[module_name]: 496 if provider not in self.required: 497 self.required.add(provider) 498 if self.verbose: 499 print >>sys.stderr, "Requiring", provider 500 self.require_providers(provider) 501 502 def uses_callable(self, ref): 503 504 "Return whether 'ref' refers to a callable." 505 506 # Find the function or method associated with the reference. 507 508 if ref.has_kind("<function>"): 509 return ref.get_origin() 510 elif ref.has_kind("<class>"): 511 return "%s.__init__" % ref.get_origin() 512 else: 513 return False 514 515 def uses_dynamic_callable(self, ref): 516 517 """ 518 Return whether 'ref' refers to a callable employing defaults that may 519 need initialising before the callable can be used. 520 """ 521 522 origin = self.uses_callable(ref) 523 if not origin: 524 return False 525 526 if ref.has_kind("<class>"): 527 return True 528 529 # Find any defaults for the function or method. 530 531 defaults = self.function_defaults.get(origin) 532 if not defaults: 533 return False 534 535 # Identify non-constant defaults. 536 537 for name, ref in defaults: 538 if not ref.is_constant_alias(): 539 return True 540 541 return False 542 543 def order_modules(self): 544 545 "Produce a module initialisation ordering." 546 547 self.check_ordering() 548 549 module_names = self.modules.keys() 550 551 # Record the number of modules using or depending on each module. 552 553 usage = {} 554 555 for module_name in module_names: 556 usage[module_name] = 0 557 558 for module_name, depend_names in self.depends.items(): 559 if module_name in module_names: 560 for depend_name in depend_names: 561 if depend_name in module_names: 562 usage[depend_name] += 1 563 564 # Produce an ordering by obtaining exposed modules (required by modules 565 # already processed) and putting them at the start of the list. 566 567 ordered = [] 568 569 while usage: 570 for module_name, n in usage.items(): 571 if n == 0: 572 ordered.insert(0, module_name) 573 module_names = self.depends.get(module_name) 574 575 # Reduce usage of the referenced modules. 576 577 if module_names: 578 for name in module_names: 579 usage[name] -= 1 580 581 del usage[module_name] 582 583 ordered.remove("__main__") 584 ordered.append("__main__") 585 return ordered 586 587 def check_ordering(self): 588 589 "Check the ordering dependencies." 590 591 mutual = set() 592 593 # Get dependency relationships for each module. 594 595 for module_name, modules in self.depends.items(): 596 597 # Find the reverse relationship. 598 599 for provider in modules: 600 if self.depends.has_key(provider) and module_name in self.depends[provider]: 601 602 # Record the module names in order. 603 604 mutual.add((module_name < provider and module_name or provider, 605 module_name > provider and module_name or provider)) 606 607 if not mutual: 608 return 609 610 # Format the dependencies. 611 612 mutual = list(mutual) 613 mutual.sort() 614 l = [] 615 616 for module_name, provider in mutual: 617 l.append("(%s <-> %s)" % (module_name, provider)) 618 619 raise ProgramError, "Modules may not depend on each other for non-static objects:\n%s" % "\n".join(l) 620 621 def find_dependency(self, ref): 622 623 "Find the ultimate dependency for 'ref'." 624 625 found = set() 626 while ref and ref.has_kind("<depends>") and not ref in found: 627 found.add(ref) 628 ref = self.identify(ref.get_origin()) 629 return ref 630 631 def get_module_provider(self, ref): 632 633 "Identify the provider of the given 'ref'." 634 635 for ancestor in ref.ancestors(): 636 if self.modules.has_key(ancestor): 637 return ancestor 638 return None 639 640 def finalise_classes(self): 641 642 "Finalise the class relationships and attributes." 643 644 self.derive_inherited_attrs() 645 self.derive_subclasses() 646 self.derive_shadowed_attrs() 647 648 def derive_inherited_attrs(self): 649 650 "Derive inherited attributes for classes throughout the program." 651 652 for name in self.classes.keys(): 653 self.propagate_attrs_for_class(name) 654 655 def propagate_attrs_for_class(self, name, visited=None): 656 657 "Propagate inherited attributes for class 'name'." 658 659 # Visit classes only once. 660 661 if self.all_combined_attrs.has_key(name): 662 return 663 664 visited = visited or [] 665 666 if name in visited: 667 raise ProgramError, "Class %s may not inherit from itself: %s -> %s." % (name, " -> ".join(visited), name) 668 669 visited.append(name) 670 671 class_attrs = {} 672 instance_attrs = {} 673 674 # Aggregate the attributes from base classes, recording the origins of 675 # applicable attributes. 676 677 for base in self.classes[name][::-1]: 678 679 # Get the identity of the class from the reference. 680 681 base = base.get_origin() 682 683 # Define the base class completely before continuing with this 684 # class. 685 686 self.propagate_attrs_for_class(base, visited) 687 class_attrs.update(self.all_class_attrs[base]) 688 689 # Instance attribute origins are combined if different. 690 691 for key, values in self.all_instance_attrs[base].items(): 692 init_item(instance_attrs, key, set) 693 instance_attrs[key].update(values) 694 695 # Class attributes override those defined earlier in the hierarchy. 696 697 class_attrs.update(self.all_class_attrs.get(name, {})) 698 699 # Instance attributes are merely added if not already defined. 700 701 for key in self.all_instance_attrs.get(name, []): 702 if not instance_attrs.has_key(key): 703 instance_attrs[key] = set(["%s.%s" % (name, key)]) 704 705 self.all_class_attrs[name] = class_attrs 706 self.all_instance_attrs[name] = instance_attrs 707 self.all_combined_attrs[name] = set(class_attrs.keys()).union(instance_attrs.keys()) 708 709 def derive_subclasses(self): 710 711 "Derive subclass details for classes." 712 713 for name, bases in self.classes.items(): 714 for base in bases: 715 716 # Get the identity of the class from the reference. 717 718 base = base.get_origin() 719 self.subclasses[base].add(name) 720 721 def derive_shadowed_attrs(self): 722 723 "Derive shadowed attributes for classes." 724 725 for name, attrs in self.all_instance_attrs.items(): 726 attrs = set(attrs.keys()).intersection(self.all_class_attrs[name].keys()) 727 if attrs: 728 self.all_shadowed_attrs[name] = attrs 729 730 def set_class_types(self): 731 732 "Set the type of each class." 733 734 for attrs in self.all_class_attrs.values(): 735 attrs["__class__"] = self.type_ref.get_origin() 736 737 def define_instantiators(self): 738 739 """ 740 Consolidate parameter and default details, incorporating initialiser 741 details to define instantiator signatures. 742 """ 743 744 for cls, attrs in self.all_class_attrs.items(): 745 initialiser = attrs["__init__"] 746 self.function_parameters[cls] = self.function_parameters[initialiser] 747 self.function_defaults[cls] = self.function_defaults[initialiser] 748 749 def collect_constants(self): 750 751 "Get constants from all active modules." 752 753 for module in self.modules.values(): 754 self.all_constants.update(module.constants) 755 756 # Import methods. 757 758 def find_in_path(self, name): 759 760 """ 761 Find the given module 'name' in the search path, returning None where no 762 such module could be found, or a 2-tuple from the 'find' method 763 otherwise. 764 """ 765 766 for d in self.path: 767 m = self.find(d, name) 768 if m: return m 769 return None 770 771 def find(self, d, name): 772 773 """ 774 In the directory 'd', find the given module 'name', where 'name' can 775 either refer to a single file module or to a package. Return None if the 776 'name' cannot be associated with either a file or a package directory, 777 or a 2-tuple from '_find_package' or '_find_module' otherwise. 778 """ 779 780 m = self._find_package(d, name) 781 if m: return m 782 m = self._find_module(d, name) 783 if m: return m 784 return None 785 786 def _find_module(self, d, name): 787 788 """ 789 In the directory 'd', find the given module 'name', returning None where 790 no suitable file exists in the directory, or a 2-tuple consisting of 791 None (indicating that no package directory is involved) and a filename 792 indicating the location of the module. 793 """ 794 795 name_py = name + extsep + "py" 796 filename = self._find_file(d, name_py) 797 if filename: 798 return None, filename 799 return None 800 801 def _find_package(self, d, name): 802 803 """ 804 In the directory 'd', find the given package 'name', returning None 805 where no suitable package directory exists, or a 2-tuple consisting of 806 a directory (indicating the location of the package directory itself) 807 and a filename indicating the location of the __init__.py module which 808 declares the package's top-level contents. 809 """ 810 811 filename = self._find_file(d, name) 812 if filename: 813 init_py = "__init__" + extsep + "py" 814 init_py_filename = self._find_file(filename, init_py) 815 if init_py_filename: 816 return filename, init_py_filename 817 return None 818 819 def _find_file(self, d, filename): 820 821 """ 822 Return the filename obtained when searching the directory 'd' for the 823 given 'filename', or None if no actual file exists for the filename. 824 """ 825 826 filename = join(d, filename) 827 if exists(filename): 828 return filename 829 else: 830 return None 831 832 def load(self, name): 833 834 """ 835 Load the module or package with the given 'name'. Return an object 836 referencing the loaded module or package, or None if no such module or 837 package exists. 838 """ 839 840 # Loaded modules are returned immediately. 841 # Modules may be known but not yet loading (having been registered as 842 # submodules), loading, loaded, or completely unknown. 843 844 module = self.get_module(name) 845 846 if module: 847 return self.modules[name] 848 849 # Otherwise, modules are loaded. 850 851 # Split the name into path components, and try to find the uppermost in 852 # the search path. 853 854 path = name.split(".") 855 path_so_far = [] 856 module = None 857 858 for p in path: 859 860 # Get the module's filesystem details. 861 862 if not path_so_far: 863 m = self.find_in_path(p) 864 elif d: 865 m = self.find(d, p) 866 else: 867 m = None 868 869 path_so_far.append(p) 870 module_name = ".".join(path_so_far) 871 872 # Return None if the module could not be located. 873 874 if not m: 875 if self.verbose: 876 print >>sys.stderr, "Not found (%s)" % name 877 return None 878 879 # Get the directory and module filename. 880 881 d, filename = m 882 883 # Get the module itself. 884 885 return self.load_from_file(filename, module_name) 886 887 def load_from_file(self, filename, module_name=None): 888 889 "Load the module from the given 'filename'." 890 891 if module_name is None: 892 module_name = "__main__" 893 894 module = self.modules.get(module_name) 895 896 if not module: 897 898 # Try to load from cache. 899 900 module = self.load_from_cache(filename, module_name) 901 if module: 902 return module 903 904 # If no cache entry exists, load from file. 905 906 module = inspector.InspectedModule(module_name, self) 907 self.add_module(module_name, module) 908 self.update_cache_validity(module) 909 910 self._load(module, module_name, lambda m: m.parse, filename) 911 912 return module 913 914 def update_cache_validity(self, module): 915 916 "Make 'module' valid in the cache, but invalidate accessing modules." 917 918 accessing = self.accessing_modules.get(module.name) 919 if accessing: 920 self.invalidated.update(accessing) 921 if module.name in self.invalidated: 922 self.invalidated.remove(module.name) 923 924 def source_is_new(self, filename, module_name): 925 926 "Return whether 'filename' is newer than the cached 'module_name'." 927 928 if self.cache: 929 cache_filename = join(self.cache, module_name) 930 return not exists(cache_filename) or \ 931 getmtime(filename) > getmtime(cache_filename) or \ 932 module_name in self.invalidated 933 else: 934 return True 935 936 def load_from_cache(self, filename, module_name): 937 938 "Return a module residing in the cache." 939 940 module = self.modules.get(module_name) 941 942 if not module and not self.source_is_new(filename, module_name): 943 module = CachedModule(module_name, self) 944 self.add_module(module_name, module) 945 946 filename = join(self.cache, module_name) 947 self._load(module, module_name, lambda m: m.from_cache, filename) 948 949 return module 950 951 def _load(self, module, module_name, fn, filename): 952 953 """ 954 Load 'module' for the given 'module_name', and with 'fn' performing an 955 invocation on the module with the given 'filename'. 956 """ 957 958 # Load the module. 959 960 if self.verbose: 961 print >>sys.stderr, module_name in self.required and "Required" or "Loading", module_name, "from", filename 962 fn(module)(filename) 963 964 # Add the module object if not already defined. 965 966 if not self.objects.has_key(module_name): 967 self.objects[module_name] = Reference("<module>", module_name) 968 969 def add_module(self, module_name, module): 970 971 """ 972 Return the module with the given 'module_name', adding a new module 973 object if one does not already exist. 974 """ 975 976 self.modules[module_name] = module 977 if module_name in self.to_import: 978 self.to_import.remove(module_name) 979 980 # vim: tabstop=4 expandtab shiftwidth=4