예제 #1
0
    def _run(self, environment):
        logging.info("Running %s %s"
                     % (self.__class__.__name__.lower(), self.path))
        self.paper.remove_owned_by(self.path)
        # A string uniquely identifying the paper from which the
        # calclet is called. Used in Importer.
        paper_id = hex(id(self.paper))[2:]
        script = utf8(self.node[...].flat[0])
        script = compile(script, ':'.join([paper_id, self.path]), 'exec')
        self._contents_module = imp.new_module('activepapers.contents')
        self._contents_module.data = DataGroup(self.paper, None,
                                               self.paper.data_group, self)
        self._contents_module.open = self.open_data_file
        self._contents_module.open_documentation = self.open_documentation_file
        self._contents_module.snapshot = self.paper.snapshot

        # The remaining part of this method is not thread-safe because
        # of the way the global state in sys.modules is modified.
        with codelet_lock:
            try:
                codelet_registry[(paper_id, self.path)] = self
                for name, module in self.paper._local_modules.items():
                    assert name not in sys.modules
                    sys.modules[name] = module
                sys.modules['activepapers.contents'] = self._contents_module
                execstring(script, environment)
            finally:
                del codelet_registry[(paper_id, self.path)]
                self._contents_module = None
                if 'activepapers.contents' in sys.modules:
                    del sys.modules['activepapers.contents']
                for name, module in self.paper._local_modules.items():
                    del sys.modules[name]
예제 #2
0
 def load_module(self, fullname):
     assert fullname == self.fullname
     if fullname in sys.modules:
         module = sys.modules[fullname]
         loader = getattr(module, '__loader__', None)
         if isinstance(loader, ModuleLoader):
             assert loader.paper is self.paper
         return module
     code = ascii(self.node[...].flat[0])
     module = imp.new_module(fullname)
     module.__file__ = os.path.abspath(self.node.file.filename) + ':' + \
                       self.node.name
     module.__loader__ = self
     if self._is_package:
         module.__path__ = []
         module.__package__ = fullname
     else:
         module.__package__ = fullname.rpartition('.')[0]
     sys.modules[fullname] = module
     self.paper._local_modules[fullname] = module
     try:
         execstring(code, module.__dict__)
     except:
         del sys.modules[fullname]
         del self.paper._local_modules[fullname]
         raise
     return module