def eval(elem, globals, locals=None): if locals is None: locals = {} not_found = object() old_values = [] all_var_names = elem.var_names + ['for'] for ass_names, _ in elem.assignments: all_var_names.extend(ass_names) for name in all_var_names: old_values.append(locals.get(name, not_found)) result = [] list = _eval(elem.code, globals, locals) if not list: if elem.else_: return elem.else_.eval(globals, locals) return '' for i, item in enumerate(list): if i and elem.separator: result.append(elem.separator.eval(globals, locals)) for name, value in izip(elem.var_names, item): locals[name] = value locals['for'] = i, len(list) for _, code in elem.assignments: exec code in globals, locals result.append(elem.markup.eval(globals, locals)) for name, old_value in izip(all_var_names, old_values): if old_value is not_found: del locals[name] else: locals[name] = old_value return elem.empty.join(result)
def _set_value(grid, value): rows = list(value) if len(rows) != len(grid._rows): raise TypeError('Incorrect row count') for i, row in enumerate(rows): if len(row) != len(grid.columns): raise TypeError('Incorrect col count in row %d: %d' % (i, len(row))) for i, row, values in enumerate(izip(grid._rows, rows)): for field, value in izip(row, values): if field is not None: field.value = value
def BUILD_CONST_KEY_MAP(decompiler, length): keys = decompiler.stack.pop() assert isinstance(keys, ast.Const) keys = [ ast.Const(key) for key in keys.value ] values = decompiler.pop_items(length) pairs = list(izip(keys, values)) return ast.Dict(pairs)
def visitGenExprInner(self, node): self.set_lineno(node) # setup list stack = [] for i, for_ in izip(range(len(node.quals)), node.quals): start, anchor, end = self.visit(for_) cont = None for if_ in for_.ifs: if cont is None: cont = self.newBlock() self.visit(if_, cont) stack.insert(0, (start, cont, anchor, end)) self.visit(node.expr) self.emit('YIELD_VALUE') self.emit('POP_TOP') for start, cont, anchor, end in stack: if cont: self.nextBlock(cont) self.emit('JUMP_ABSOLUTE', start) self.startBlock(anchor) self.emit('POP_BLOCK') self.setups.pop() self.nextBlock(end) self.emit('LOAD_CONST', None)
def are_comparable_types(t1, t2, op='=='): # types must be normalized already! tt1 = type(t1) tt2 = type(t2) if op in ('in', 'not in'): if tt2 is not SetType: return False op = '==' t2 = t2.item_type tt2 = type(t2) if op in ('is', 'is not'): return t1 is not None and t2 is NoneType if tt1 is tuple: if not tt2 is tuple: return False if len(t1) != len(t2): return False for item1, item2 in izip(t1, t2): if not are_comparable_types(item1, item2): return False return True if op in ('==', '<>', '!='): if t1 is NoneType and t2 is NoneType: return False if t1 is NoneType or t2 is NoneType: return True if t1 in primitive_types: if t1 is t2: return True if (t1, t2) in coercions: return True if tt1 is not type or tt2 is not type: return False if issubclass(t1, int_types) and issubclass(t2, basestring): return True if issubclass(t2, int_types) and issubclass(t1, basestring): return True return False if tt1.__name__ == tt2.__name__ == 'EntityMeta': return t1._root_ is t2._root_ return False if t1 is t2 and t1 in comparable_types: return True return (t1, t2) in coercions
def isbn10_checksum(digits): if len(digits) != 9: raise ValueError() reminder = sum(digit * coef for digit, coef in izip(imap(int, digits), xrange(10, 1, -1))) % 11 if reminder == 1: return "X" return reminder and str(11 - reminder) or "0"
def isbn10_checksum(digits): if len(digits) != 9: raise ValueError() reminder = sum( digit * coef for digit, coef in izip(imap(int, digits), xrange(10, 1, -1))) % 11 if reminder == 1: return 'X' return reminder and str(11 - reminder) or '0'
def invoke(url): if isinstance(url, str): try: url.decode('utf8') except UnicodeDecodeError: raise Http400BadRequest request = local.request response = local.response path, qlist = httputils.split_url(url) if path[:1] == ['static'] and len(path) > 1: return get_static_file(path[1:]) if path[:2] == ['pony', 'static'] and len(path) > 2: return get_static_file(path[2:], pony_static_dir, 30*60) qdict = dict(qlist) routes = routing.get_routes(path, qdict, request.method, request.host, request.port) if routes: pass elif request.method in ('GET', 'HEAD'): i = url.find('?') if i == -1: p, q = url, '' else: p, q = url[:i], url[i:] if p.endswith('/'): url2 = p[:-1] + q else: url2 = p + '/' + q path2, qlist = httputils.split_url(url2) routes = routing.get_routes(path2, qdict, request.method, request.host, request.port) if not routes: return get_static_file(path) script_name = request.environ.get('SCRIPT_NAME', '') url2 = script_name + url2 or '/' if url2 != script_name + url: raise HttpRedirect(url2) else: routes = routing.get_routes(path, qdict, 'GET', request.host, request.port) if routes: raise Http405MethodNotAllowed raise Http404NotFound route, args, kwargs = routes[0] if route.redirect: for alternative in route.func.routes: if not alternative.redirect: new_url = make_url(route.func, *args, **kwargs) status = '301 Moved Permanently' if isinstance(route.redirect, basestring): status = route.redirect elif isinstance(route.redirect, (int, long)) and 300 <= route.redirect < 400: status = str(route.redirect) raise HttpRedirect(new_url, status) response.headers.update(route.headers) names, argsname, keyargsname, defaults, converters = route.func.argspec params = request.params params.update(izip(names, args)) params.update(kwargs) middlewared_func = middleware.decorator_wrap(normalize_result_decorator(route.func)) result = middlewared_func(*args, **kwargs) headers = response.headers headers.setdefault('Expires', '0') max_age = headers.pop('Max-Age', '2') cache_control = headers.get('Cache-Control') if not cache_control: headers['Cache-Control'] = 'max-age=%s' % max_age headers.setdefault('Vary', 'Cookie') return result
def BUILD_MAP(decompiler, length): if sys.version_info < (3, 5): return ast.Dict(()) data = decompiler.pop_items(2 * length) # [key1, value1, key2, value2, ...] it = iter(data) pairs = list(izip(it, it)) # [(key1, value1), (key2, value2), ...] return ast.Dict(tuple(pairs))
def check_params(key_params_list, key_lineno, lstr, lstr_lineno, lang_code): lstr_params_list = [ match.group() for match in param_re.finditer(lstr) if match.group() != '$$' ] if len(key_params_list) != len(lstr_params_list): raise I18nParseError( "Parameters count in line %d doesn't match with line %d" % (key_lineno, lstr_lineno)) for a, b in izip(sorted(key_params_list), sorted(lstr_params_list)): if a != b: raise I18nParseError("Unknown parameter in line %d: %s (translation for %s)" % (lstr_lineno, b, lang_code))
def decompile(x): cells = {} t = type(x) if t is types.CodeType: codeobject = x elif t is types.GeneratorType: codeobject = x.gi_frame.f_code elif t is types.FunctionType: codeobject = x.func_code if PY2 else x.__code__ if PY2: if x.func_closure: cells = dict(izip(codeobject.co_freevars, x.func_closure)) else: if x.__closure__: cells = dict(izip(codeobject.co_freevars, x.__closure__)) else: throw(TypeError) key = get_codeobject_id(codeobject) result = ast_cache.get(key) if result is None: decompiler = Decompiler(codeobject) result = decompiler.ast, decompiler.external_names ast_cache[key] = result return result + (cells,)
def decompile(x): cells = {} t = type(x) if t is types.CodeType: codeobject = x elif t is types.GeneratorType: codeobject = x.gi_frame.f_code elif t is types.FunctionType: codeobject = x.func_code if PY2 else x.__code__ if PY2: if x.func_closure: cells = dict(izip(codeobject.co_freevars, x.func_closure)) else: if x.__closure__: cells = dict(izip(codeobject.co_freevars, x.__closure__)) else: throw(TypeError) key = id(codeobject) result = ast_cache.get(key) if result is None: codeobjects[key] = codeobject decompiler = Decompiler(codeobject) result = decompiler.ast, decompiler.external_names ast_cache[key] = result return result + (cells,)
def _set_value(composite, value): values = list(value) fields = [ field for field in composite.fields if not isinstance(field, Submit) ] if len(fields) != len(values): raise TypeError('Expected sequence of %d values. Got: %d' % (len(fields), len(values))) for field, value in izip(fields, values): field.value = value
def CALL_FUNCTION_KW(decompiler, argc): if sys.version_info < (3, 6): return decompiler.CALL_FUNCTION(argc, star2=decompiler.stack.pop()) keys = decompiler.stack.pop() assert isinstance(keys, ast.Const) keys = keys.value values = decompiler.pop_items(argc) assert len(keys) <= len(values) args = values[:-len(keys)] for key, value in izip(keys, values[-len(keys):]): args.append(ast.Keyword(key, value)) return decompiler._call_function(args)
def tag(grid): result = [ Html('\n<table><tr>') ] for column in grid.columns: result.append(Html('<th>%s</th>') % column) result.append(Html('</tr>\n')) for row, row_class in izip(grid._rows, cycle(('odd', 'even'))): result.append(Html('<tr class="%s">') % row_class) for field in row: if field is None: result.append(Html('<td> </td>')) else: result.append(Html('<td>%s</td>') % field.tag) result.append(Html('</tr>\n')) result.append(Html('</table>\n')) return htmljoin(result)
def tag(grid): result = [Html('\n<table><tr>')] for column in grid.columns: result.append(Html('<th>%s</th>') % column) result.append(Html('</tr>\n')) for row, row_class in izip(grid._rows, cycle(('odd', 'even'))): result.append(Html('<tr class="%s">') % row_class) for field in row: if field is None: result.append(Html('<td> </td>')) else: result.append(Html('<td>%s</td>') % field.tag) result.append(Html('</tr>\n')) result.append(Html('</table>\n')) return htmljoin(result)
def read_phrases(lines): kstr, lstr_list = None, [] for lineno, line in izip(count(1), lines): if not line or line.isspace() or line.lstrip().startswith('#'): continue elif line[0].isspace(): if kstr is None: raise I18nParseError( "Translation string found but key string was expected in line %d" % lineno) lstr_list.append((lineno, line)) elif kstr is None: kstr = lineno, line # assert lineno == 1 else: yield kstr, lstr_list kstr, lstr_list = (lineno, line), [] if kstr is not None: yield kstr, lstr_list
def check(route): names, argsname, keyargsname, defaults, converters = route.func.argspec if route.star and not argsname: raise TypeError( "Function %s does not accept arbitrary argument list" % route.func.__name__) args, kwargs = route.args, route.kwargs diff = len(names) - len(defaults) for i, name in enumerate(names[:diff]): if i not in args: raise TypeError('Undefined path parameter: %s' % name) for i, name, default in izip(xrange(diff, diff+len(defaults)), names[diff:], defaults): if default is __nodefault__ and i not in args: raise TypeError('Undefined path parameter: %s' % name) if args: for i in range(len(names), max(args)): if i not in args: raise TypeError('Undefined path parameter: %d' % (i+1))
def eval(elem, globals, locals=None): if locals is None: locals = {} not_found = object() old_values = [] all_var_names = elem.var_names + [ 'for' ] for ass_names, _ in elem.assignments: all_var_names.extend(ass_names) for name in all_var_names: old_values.append(locals.get(name, not_found)) result = [] list = _eval(elem.code, globals, locals) if not list: if elem.else_: return elem.else_.eval(globals, locals) return '' for i, item in enumerate(list): if i and elem.separator: result.append(elem.separator.eval(globals, locals)) for name, value in izip(elem.var_names, item): locals[name] = value locals['for'] = i, len(list) for _, code in elem.assignments: exec code in globals, locals result.append(elem.markup.eval(globals, locals)) for name, old_value in izip(all_var_names, old_values): if old_value is not_found: del locals[name] else: locals[name] = old_value return elem.empty.join(result)
def are_comparable_types(t1, t2, op="=="): # types must be normalized already! tt1 = type(t1) tt2 = type(t2) if op in ("in", "not in"): if tt2 is not SetType: return False op = "==" t2 = t2.item_type tt2 = type(t2) if op in ("is", "is not"): return t1 is not None and t2 is NoneType if tt1 is tuple: if not tt2 is tuple: return False if len(t1) != len(t2): return False for item1, item2 in izip(t1, t2): if not are_comparable_types(item1, item2): return False return True if op in ("==", "<>", "!="): if t1 is NoneType and t2 is NoneType: return False if t1 is NoneType or t2 is NoneType: return True if t1 in primitive_types: if t1 is t2: return True if (t1, t2) in coercions: return True if tt1 is not type or tt2 is not type: return False if issubclass(t1, int_types) and issubclass(t2, basestring): return True if issubclass(t2, int_types) and issubclass(t1, basestring): return True return False if tt1.__name__ == tt2.__name__ == "EntityMeta": return t1._root_ is t2._root_ return False if t1 is t2 and t1 in comparable_types: return True return (t1, t2) in coercions
def check(route): names, argsname, keyargsname, defaults, converters = route.func.argspec if route.star and not argsname: raise TypeError( "Function %s does not accept arbitrary argument list" % route.func.__name__) args, kwargs = route.args, route.kwargs diff = len(names) - len(defaults) for i, name in enumerate(names[:diff]): if i not in args: raise TypeError('Undefined path parameter: %s' % name) for i, name, default in izip(xrange(diff, diff + len(defaults)), names[diff:], defaults): if default is __nodefault__ and i not in args: raise TypeError('Undefined path parameter: %s' % name) if args: for i in range(len(names), max(args)): if i not in args: raise TypeError('Undefined path parameter: %d' % (i + 1))
def visitSetComp(self, node): self.set_lineno(node) # setup list self.emit('BUILD_SET', 0) stack = [] for i, for_ in izip(range(len(node.quals)), node.quals): start, anchor = self.visit(for_) cont = None for if_ in for_.ifs: if cont is None: cont = self.newBlock() self.visit(if_, cont) stack.insert(0, (start, cont, anchor)) self.visit(node.expr) self.emit('SET_ADD', len(node.quals) + 1) for start, cont, anchor in stack: if cont: self.nextBlock(cont) self.emit('JUMP_ABSOLUTE', start) self.startBlock(anchor)
def isbn13_checksum(digits): if len(digits) != 12: raise ValueError() reminder = sum(digit * coef for digit, coef in izip(imap(int, digits), (1, 3) * 6)) % 10 return reminder and str(10 - reminder) or '0'
def get_routes(path, qdict, method, host, port): # registry_lock.acquire() # try: variants = [ registry ] routes = [] for i, component in enumerate(path): new_variants = [] for d, list1, list2 in variants: variant = d.get(component) if variant: new_variants.append(variant) # if component: variant = d.get(None) if variant: new_variants.append(variant) routes.extend(list2) variants = new_variants for d, list1, list2 in variants: routes.extend(list1) # finally: registry_lock.release() result = [] not_found = object() for route in routes: args, kwargs = {}, {} priority = 0 if route.host is not None: if route.host != host: continue priority += 8000 if route.port is not None: if route.port != port: continue priority += 4000 if method == route.method: if method is not None: priority += 2000 elif route.method is None and method in ('HEAD', 'GET', 'POST'): pass elif route.method == 'GET' and method == 'HEAD': priority += 1000 else: continue for i, (is_param, x) in enumerate(route.parsed_path): if not is_param: priority += 1 continue value = path[i].decode('utf8') if isinstance(x, int): args[x] = value elif isinstance(x, basestring): kwargs[x] = value elif isinstance(x, list): match = x[1].match(value) if not match: break params = [ y for is_param, y in x[2:] if is_param ] groups = match.groups() n = len(x) - len(params) if not x[-1][0]: n += 1 priority += n assert len(params) == len(groups) for param, value in izip(params, groups): if isinstance(param, int): args[param] = value elif isinstance(param, basestring): kwargs[param] = value else: assert False # pragma: no cover else: assert False # pragma: no cover else: names, _, _, defaults, converters = route.func.argspec diff = len(names) - len(defaults) non_used_query_params = set(qdict) for name, is_param, x in route.parsed_query: non_used_query_params.discard(name) value = qdict.get(name, not_found) if value is not not_found: value = value.decode('utf8') if not is_param: if value != x: break priority += 1 elif isinstance(x, int): if value is not_found: if diff <= x < len(names): continue else: break else: args[x] = value elif isinstance(x, basestring): if value is not_found: break kwargs[x] = value elif isinstance(x, list): if value is not_found: for is_param, y in x[2:]: if not is_param: continue if isinstance(y, int) and diff <= y < len(names): continue break else: continue break match = x[1].match(value) if not match: break params = [ y for is_param, y in x[2:] if is_param ] groups = match.groups() n = len(x) - len(params) - 2 if not x[-1][0]: n += 1 priority += n assert len(params) == len(groups) for param, value in izip(params, groups): if isinstance(param, int): args[param] = value elif isinstance(param, basestring): kwargs[param] = value else: assert False # pragma: no cover else: assert False # pragma: no cover else: arglist = [ None ] * len(names) arglist[diff:] = defaults for i, value in sorted(args.items()): converter = converters.get(i) if converter is not None: try: value = converter(value) except: break try: arglist[i] = value except IndexError: assert i == len(arglist) arglist.append(value) else: if __nodefault__ in arglist[diff:]: continue if len(route.parsed_path) != len(path): assert route.star arglist.extend(path[len(route.parsed_path):]) result.append((route, arglist, kwargs, priority, len(non_used_query_params))) if result: x = max(tup[3] for tup in result) result = [ tup for tup in result if tup[3] == x ] x = min(tup[4] for tup in result) result = [ tup[:3] for tup in result if tup[4] == x ] return result
def invoke(url): if isinstance(url, str): try: url.decode('utf8') except UnicodeDecodeError: raise Http400BadRequest request = local.request response = local.response path, qlist = httputils.split_url(url) if path[:1] == ['static'] and len(path) > 1: return get_static_file(path[1:]) if path[:2] == ['pony', 'static'] and len(path) > 2: return get_static_file(path[2:], pony_static_dir, 30 * 60) qdict = dict(qlist) routes = routing.get_routes(path, qdict, request.method, request.host, request.port) if routes: pass elif request.method in ('GET', 'HEAD'): i = url.find('?') if i == -1: p, q = url, '' else: p, q = url[:i], url[i:] if p.endswith('/'): url2 = p[:-1] + q else: url2 = p + '/' + q path2, qlist = httputils.split_url(url2) routes = routing.get_routes(path2, qdict, request.method, request.host, request.port) if not routes: return get_static_file(path) script_name = request.environ.get('SCRIPT_NAME', '') url2 = script_name + url2 or '/' if url2 != script_name + url: raise HttpRedirect(url2) else: routes = routing.get_routes(path, qdict, 'GET', request.host, request.port) if routes: raise Http405MethodNotAllowed raise Http404NotFound route, args, kwargs = routes[0] if route.redirect: for alternative in route.func.routes: if not alternative.redirect: new_url = make_url(route.func, *args, **kwargs) status = '301 Moved Permanently' if isinstance(route.redirect, basestring): status = route.redirect elif isinstance(route.redirect, (int, long)) and 300 <= route.redirect < 400: status = str(route.redirect) raise HttpRedirect(new_url, status) response.headers.update(route.headers) names, argsname, keyargsname, defaults, converters = route.func.argspec params = request.params params.update(izip(names, args)) params.update(kwargs) middlewared_func = middleware.decorator_wrap( normalize_result_decorator(route.func)) result = middlewared_func(*args, **kwargs) headers = response.headers headers.setdefault('Expires', '0') max_age = headers.pop('Max-Age', '2') cache_control = headers.get('Cache-Control') if not cache_control: headers['Cache-Control'] = 'max-age=%s' % max_age headers.setdefault('Vary', 'Cookie') return result
def isbn13_checksum(digits): if len(digits) != 12: raise ValueError() reminder = sum(digit*coef for digit, coef in izip(imap(int, digits), (1, 3)*6)) % 10 return reminder and str(10 - reminder) or '0'
def _set_value(composite, value): values = list(value) fields = [ field for field in composite.fields if not isinstance(field, Submit) ] if len(fields) != len(values): raise TypeError( 'Expected sequence of %d values. Got: %d' % (len(fields), len(values))) for field, value in izip(fields, values): field.value = value
def adapter(values): return dict( ('p%d' % param.id, value) for param, value in izip(params, convert(values, params)))
def get_routes(path, qdict, method, host, port): # registry_lock.acquire() # try: variants = [registry] routes = [] for i, component in enumerate(path): new_variants = [] for d, list1, list2 in variants: variant = d.get(component) if variant: new_variants.append(variant) # if component: variant = d.get(None) if variant: new_variants.append(variant) routes.extend(list2) variants = new_variants for d, list1, list2 in variants: routes.extend(list1) # finally: registry_lock.release() result = [] not_found = object() for route in routes: args, kwargs = {}, {} priority = 0 if route.host is not None: if route.host != host: continue priority += 8000 if route.port is not None: if route.port != port: continue priority += 4000 if method == route.method: if method is not None: priority += 2000 elif route.method is None and method in ('HEAD', 'GET', 'POST'): pass elif route.method == 'GET' and method == 'HEAD': priority += 1000 else: continue for i, (is_param, x) in enumerate(route.parsed_path): if not is_param: priority += 1 continue value = path[i].decode('utf8') if isinstance(x, int): args[x] = value elif isinstance(x, basestring): kwargs[x] = value elif isinstance(x, list): match = x[1].match(value) if not match: break params = [y for is_param, y in x[2:] if is_param] groups = match.groups() n = len(x) - len(params) if not x[-1][0]: n += 1 priority += n assert len(params) == len(groups) for param, value in izip(params, groups): if isinstance(param, int): args[param] = value elif isinstance(param, basestring): kwargs[param] = value else: assert False # pragma: no cover else: assert False # pragma: no cover else: names, _, _, defaults, converters = route.func.argspec diff = len(names) - len(defaults) non_used_query_params = set(qdict) for name, is_param, x in route.parsed_query: non_used_query_params.discard(name) value = qdict.get(name, not_found) if value is not not_found: value = value.decode('utf8') if not is_param: if value != x: break priority += 1 elif isinstance(x, int): if value is not_found: if diff <= x < len(names): continue else: break else: args[x] = value elif isinstance(x, basestring): if value is not_found: break kwargs[x] = value elif isinstance(x, list): if value is not_found: for is_param, y in x[2:]: if not is_param: continue if isinstance(y, int) and diff <= y < len(names): continue break else: continue break match = x[1].match(value) if not match: break params = [y for is_param, y in x[2:] if is_param] groups = match.groups() n = len(x) - len(params) - 2 if not x[-1][0]: n += 1 priority += n assert len(params) == len(groups) for param, value in izip(params, groups): if isinstance(param, int): args[param] = value elif isinstance(param, basestring): kwargs[param] = value else: assert False # pragma: no cover else: assert False # pragma: no cover else: arglist = [None] * len(names) arglist[diff:] = defaults for i, value in sorted(args.items()): converter = converters.get(i) if converter is not None: try: value = converter(value) except: break try: arglist[i] = value except IndexError: assert i == len(arglist) arglist.append(value) else: if __nodefault__ in arglist[diff:]: continue if len(route.parsed_path) != len(path): assert route.star arglist.extend(path[len(route.parsed_path):]) result.append((route, arglist, kwargs, priority, len(non_used_query_params))) if result: x = max(tup[3] for tup in result) result = [tup for tup in result if tup[3] == x] x = min(tup[4] for tup in result) result = [tup[:3] for tup in result if tup[4] == x] return result
def adapter(values): return dict(('p%d' % param.id, value) for param, value in izip(params, convert(values, params)))