def execute(self, src, *args, **kwargs): """ Given Python code, execute it and report any Haml errors in a readable traceback. """ self.reset() if len(args) > 0: self.globals.update(args[0]) if self.op.debug: sys.stdout.write(src) finder = Finder(self) sys.meta_path.append(finder) try: ex(src, self.globals) return self.html.getvalue().strip() + '\n' except: exc_type, exc_value, exc_traceback = sys.exc_info() tb = traceback.extract_tb(exc_traceback) for i, (python_file_name, python_line_number, function_name, python_text, local) in enumerate(tb): if python_file_name == "<haml>": haml_file_name = exc_traceback.tb_frame.f_globals.get( "HAML_file_name", "<string>") haml_line_number, haml_line = self.get_haml_line_info( haml_file_name, python_line_number, python_text) tb[i] = (haml_file_name, haml_line_number, function_name, haml_line, local) exc_traceback = exc_traceback.tb_next formatted = ["Traceback (most recent call last):\n"] formatted += traceback.format_list(tb, with_vars=True) formatted += traceback.format_exception_only(exc_type, exc_value) raise HamlException, (-1, "HAML error", "".join(formatted))
def load_module(self, fullname, path, loader): code = self.cache(path) mod = imp.new_module(fullname) mod = sys.modules.setdefault(fullname, mod) mod.__file__ = path mod.__loader__ = loader mod.__dict__.update(self.globals) ex(code, mod.__dict__) return mod
def load_module(self, fullname, path, loader): code = self.cache(path) fullname = self._modulename(fullname) mod = imp.new_module(fullname) mod = sys.modules.setdefault(fullname, mod) mod.__file__ = path mod.__loader__ = loader mod.__dict__.update(self.globals) ex(code, mod.__dict__) return mod
def execute(self, src, *args, **kwargs): """ Given Python code, execute it and report any Haml errors in a readable traceback. """ self.reset() if len(args) > 0: self.globals.update(args[0]) if hasattr(self.op, "debug") and self.op.debug: sys.stdout.write(src) finder = Finder(self) # sys.meta_path.append(finder) try: ex(src, self.globals) return self.html.getvalue().strip() + '\n' except: logging.exception("error rendering haml:") exc_type, exc_value, exc_traceback = sys.exc_info() tb = traceback.extract_tb(exc_traceback) for i, trace in enumerate(tb): logging.info("trace: %r", trace) python_file_name = trace[0] python_line_number = trace[1] if len(trace) > 1 else None function_name = trace[2] if len(trace) > 2 else None python_text = trace[3] if len(trace) > 3 else None local = trace[4] if len(trace) > 4 else None if python_file_name == "<haml>": haml_file_name = exc_traceback.tb_frame.f_globals.get( "HAML_file_name", "<string>") haml_line_number, haml_line = self.get_haml_line_info( haml_file_name, python_line_number, python_text) tb[i] = (haml_file_name, haml_line_number, function_name, haml_line) exc_traceback = exc_traceback.tb_next formatted = ["Traceback (most recent call last):\n"] formatted += traceback.format_list(tb) formatted += traceback.format_exception_only(exc_type, exc_value) raise HamlException, "".join(formatted)