1.1 --- a/classhook.py Mon Nov 15 00:54:28 2004 +0100
1.2 +++ b/classhook.py Mon Nov 15 01:00:53 2004 +0100
1.3 @@ -88,26 +88,68 @@
1.4
1.5 print "Loading", file, filename, info
1.6
1.7 - # Prepare a dictionary of globals.
1.8 -
1.9 - global_names = {}
1.10 - global_names.update(__builtins__.__dict__)
1.11 -
1.12 # Set up the module.
1.13
1.14 module = self.hooks.add_module(name)
1.15 module.__path__ = [filename]
1.16
1.17 + # Prepare a dictionary of globals.
1.18 +
1.19 + global_names = module.__dict__
1.20 + global_names["__builtins__"] = __builtins__
1.21 +
1.22 # Process each class file, producing a genuine Python class.
1.23
1.24 class_files = []
1.25 classes = []
1.26 +
1.27 + # Load the class files.
1.28 +
1.29 + class_files = {}
1.30 for class_filename in glob.glob(os.path.join(filename, "*" + os.extsep + "class")):
1.31 - print "Importing class", class_filename
1.32 + print "Loading class", class_filename
1.33 f = open(class_filename, "rb")
1.34 s = f.read()
1.35 f.close()
1.36 class_file = classfile.ClassFile(s)
1.37 + class_files[str(class_file.this_class.get_name())] = class_file
1.38 +
1.39 + # Get an index of the class files.
1.40 +
1.41 + class_file_index = class_files.keys()
1.42 +
1.43 + # NOTE: Unnecessary sorting for test purposes.
1.44 +
1.45 + class_file_index.sort()
1.46 +
1.47 + # Now go through the classes arranging them in a safe loading order.
1.48 +
1.49 + position = 0
1.50 + while position < len(class_file_index):
1.51 + class_name = class_file_index[position]
1.52 + super_class_name = str(class_files[class_name].super_class.get_name())
1.53 +
1.54 + # Discover whether the superclass appears later.
1.55 +
1.56 + try:
1.57 + super_class_position = class_file_index.index(super_class_name)
1.58 + if super_class_position > position:
1.59 +
1.60 + # If the superclass appears later, swap this class and the
1.61 + # superclass, then process the superclass.
1.62 +
1.63 + class_file_index[position] = super_class_name
1.64 + class_file_index[super_class_position] = class_name
1.65 + continue
1.66 +
1.67 + except ValueError:
1.68 + pass
1.69 +
1.70 + position += 1
1.71 +
1.72 + class_files = [class_files[class_name] for class_name in class_file_index]
1.73 +
1.74 + for class_file in class_files:
1.75 translator = bytecode.ClassTranslator(class_file)
1.76 cls = translator.process(global_names)
1.77 module.__dict__[cls.__name__] = cls
1.78 @@ -115,9 +157,9 @@
1.79
1.80 # Finally, call __clinit__ methods for all relevant classes.
1.81
1.82 - #for cls in classes:
1.83 - # if hasattr(cls, "__clinit__"):
1.84 - # cls.__clinit__()
1.85 + for cls in classes:
1.86 + if hasattr(cls, "__clinit__"):
1.87 + cls.__clinit__()
1.88
1.89 return module
1.90