def load_module_py(module_id, path): with open(path, 'rb') as fp: mod = imp.load_source(module_id, path, fp) if py2k: source_encoding = parse_encoding(fp) if source_encoding: mod._alembic_source_encoding = source_encoding return mod
def load_module_py(module_id, path): # noqa with open(path, "rb") as fp: mod = imp.load_source(module_id, path, fp) if py2k: source_encoding = parse_encoding(fp) if source_encoding: mod._alembic_source_encoding = source_encoding del sys.modules[module_id] return mod
def load_module(module_id, path): fp = open(path, 'rb') try: mod = imp.load_source(module_id, path, fp) if py2k: source_encoding = parse_encoding(fp) if source_encoding: mod._alembic_source_encoding = source_encoding return mod finally: fp.close()
def _init(self, trcback): """format a traceback from sys.exc_info() into 7-item tuples, containing the regular four traceback tuple items, plus the original template filename, the line number adjusted relative to the template source, and code line from that line number of the template.""" import mako.template mods = {} rawrecords = traceback.extract_tb(trcback) new_trcback = [] for filename, lineno, function, line in rawrecords: if not line: line = "" try: (line_map, template_lines) = mods[filename] except KeyError: try: info = mako.template._get_module_info(filename) module_source = info.code template_source = info.source template_filename = info.template_filename or filename except KeyError: # A normal .py file (not a Template) if not compat.py3k: try: fp = open(filename, "rb") encoding = util.parse_encoding(fp) fp.close() except IOError: encoding = None if encoding: line = line.decode(encoding) else: line = line.decode("ascii", "replace") new_trcback.append(( filename, lineno, function, line, None, None, None, None, )) continue template_ln = 1 mtm = mako.template.ModuleInfo source_map = mtm.get_module_source_metadata(module_source, full_line_map=True) line_map = source_map["full_line_map"] template_lines = [ line_ for line_ in template_source.split("\n") ] mods[filename] = (line_map, template_lines) template_ln = line_map[lineno - 1] if template_ln <= len(template_lines): template_line = template_lines[template_ln - 1] else: template_line = None new_trcback.append(( filename, lineno, function, line, template_filename, template_ln, template_line, template_source, )) if not self.source: for l in range(len(new_trcback) - 1, 0, -1): if new_trcback[l][5]: self.source = new_trcback[l][7] self.lineno = new_trcback[l][5] break else: if new_trcback: try: # A normal .py file (not a Template) fp = open(new_trcback[-1][0], "rb") encoding = util.parse_encoding(fp) if compat.py3k and not encoding: encoding = "utf-8" fp.seek(0) self.source = fp.read() fp.close() if encoding: self.source = self.source.decode(encoding) except IOError: self.source = "" self.lineno = new_trcback[-1][1] return new_trcback
def _init(self, trcback): """format a traceback from sys.exc_info() into 7-item tuples, containing the regular four traceback tuple items, plus the original template filename, the line number adjusted relative to the template source, and code line from that line number of the template.""" import mako.template mods = {} rawrecords = traceback.extract_tb(trcback) new_trcback = [] for filename, lineno, function, line in rawrecords: if not line: line = '' try: (line_map, template_lines) = mods[filename] except KeyError: try: info = mako.template._get_module_info(filename) module_source = info.code template_source = info.source template_filename = info.template_filename or filename except KeyError: # A normal .py file (not a Template) if not util.py3k: try: fp = open(filename, 'rb') encoding = util.parse_encoding(fp) fp.close() except IOError: encoding = None if encoding: line = line.decode(encoding) else: line = line.decode('ascii', 'replace') new_trcback.append((filename, lineno, function, line, None, None, None, None)) continue template_ln = module_ln = 1 line_map = {} for line in module_source.split("\n"): match = re.match(r'\s*# SOURCE LINE (\d+)', line) if match: template_ln = int(match.group(1)) else: template_ln += 1 module_ln += 1 line_map[module_ln] = template_ln template_lines = [line for line in template_source.split("\n")] mods[filename] = (line_map, template_lines) template_ln = line_map[lineno] if template_ln <= len(template_lines): template_line = template_lines[template_ln - 1] else: template_line = None new_trcback.append((filename, lineno, function, line, template_filename, template_ln, template_line, template_source)) if not self.source: for l in range(len(new_trcback)-1, 0, -1): if new_trcback[l][5]: self.source = new_trcback[l][7] self.lineno = new_trcback[l][5] break else: if new_trcback: try: # A normal .py file (not a Template) fp = open(new_trcback[-1][0], 'rb') encoding = util.parse_encoding(fp) fp.seek(0) self.source = fp.read() fp.close() if encoding: self.source = self.source.decode(encoding) except IOError: self.source = '' self.lineno = new_trcback[-1][1] return new_trcback
def _init(self, trcback): """format a traceback from sys.exc_info() into 7-item tuples, containing the regular four traceback tuple items, plus the original template filename, the line number adjusted relative to the template source, and code line from that line number of the template.""" import mako.template mods = {} if not trcback: (type, value, trcback) = sys.exc_info() rawrecords = traceback.extract_tb(trcback) new_trcback = [] for filename, lineno, function, line in rawrecords: try: (line_map, template_lines) = mods[filename] except KeyError: try: info = mako.template._get_module_info(filename) module_source = info.code template_source = info.source template_filename = info.template_filename or filename except KeyError: new_trcback.append((filename, lineno, function, line, None, None, None, None)) continue template_ln = module_ln = 1 line_map = {} for line in module_source.split("\n"): match = re.match(r'\s*# SOURCE LINE (\d+)', line) if match: template_ln = int(match.group(1)) else: template_ln += 1 module_ln += 1 line_map[module_ln] = template_ln template_lines = [line for line in template_source.split("\n")] mods[filename] = (line_map, template_lines) template_ln = line_map[lineno] if template_ln <= len(template_lines): template_line = template_lines[template_ln - 1] else: template_line = None new_trcback.append( (filename, lineno, function, line, template_filename, template_ln, template_line, template_source)) if not self.source: for l in range(len(new_trcback) - 1, 0, -1): if new_trcback[l][5]: self.source = new_trcback[l][7] self.lineno = new_trcback[l][5] break else: try: # A normal .py file (not a Template) fp = open(new_trcback[-1][0]) encoding = util.parse_encoding(fp) fp.seek(0) self.source = fp.read() fp.close() if encoding: self.source = self.source.decode(encoding) except IOError: self.source = '' self.lineno = new_trcback[-1][1] return (type, value, new_trcback)
def _init(self, trcback): """format a traceback from sys.exc_info() into 7-item tuples, containing the regular four traceback tuple items, plus the original template filename, the line number adjusted relative to the template source, and code line from that line number of the template.""" import mako.template mods = {} rawrecords = traceback.extract_tb(trcback) new_trcback = [] for filename, lineno, function, line in rawrecords: if not line: line = "" try: (line_map, template_lines) = mods[filename] except KeyError: try: info = mako.template._get_module_info(filename) module_source = info.code template_source = info.source template_filename = info.template_filename or filename except KeyError: # A normal .py file (not a Template) if not compat.py3k: try: fp = open(filename, "rb") encoding = util.parse_encoding(fp) fp.close() except IOError: encoding = None if encoding: line = line.decode(encoding) else: line = line.decode("ascii", "replace") new_trcback.append((filename, lineno, function, line, None, None, None, None)) continue template_ln = 1 source_map = mako.template.ModuleInfo.get_module_source_metadata(module_source, full_line_map=True) line_map = source_map["full_line_map"] template_lines = [line_ for line_ in template_source.split("\n")] mods[filename] = (line_map, template_lines) template_ln = line_map[lineno - 1] if template_ln <= len(template_lines): template_line = template_lines[template_ln - 1] else: template_line = None new_trcback.append( (filename, lineno, function, line, template_filename, template_ln, template_line, template_source) ) if not self.source: for l in range(len(new_trcback) - 1, 0, -1): if new_trcback[l][5]: self.source = new_trcback[l][7] self.lineno = new_trcback[l][5] break else: if new_trcback: try: # A normal .py file (not a Template) fp = open(new_trcback[-1][0], "rb") encoding = util.parse_encoding(fp) fp.seek(0) self.source = fp.read() fp.close() if encoding: self.source = self.source.decode(encoding) except IOError: self.source = "" self.lineno = new_trcback[-1][1] return new_trcback