Python builtins 模块,len() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用builtins.len()

项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_modify_builtins_from_leaf_function(self):
        # Verify that modifications made by leaf functions percolate up the
        # callstack.
        with swap_attr(builtins, "len", len):
            def bar():
                builtins.len = lambda x: 4

            def foo(modifier):
                l = []
                l.append(len(range(7)))
                modifier()
                l.append(len(range(7)))
                return l
            self.configure_func(foo, lambda: None)

            self.assertEqual(foo(bar), [7, 4])
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _get_conditional_content(self, fname, spans, conditions, contents):
        out = []
        ieval = []
        peval = []
        multiline = (spans[0][0] != spans[-1][1])
        for condition, content, span in zip(conditions, contents, spans):
            try:
                cond = bool(self._evaluate(condition, fname, span[0]))
            except Exception as exc:
                msg = "exception occured when evaluating '{0}'"\
                      .format(condition)
                raise FyppFatalError(msg, fname, span, exc)
            if cond:
                if self._linenums and not self._diverted and multiline:
                    out.append(linenumdir(span[1], fname))
                outcont, ievalcont, pevalcont = self._render(content)
                ieval += _shiftinds(ievalcont, len(out))
                peval += pevalcont
                out += outcont
                break
        if self._linenums and not self._diverted and multiline:
            out.append(linenumdir(spans[-1][1], fname))
        return out, ieval, peval
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def __init__(self, maxlen=132, indent=4, method='smart', prefix='&',
                 suffix='&'):
        # Line length should be long enough that contintuation lines can host at
        # east one character apart of indentation and two continuation signs
        minmaxlen = indent + len(prefix) + len(suffix) + 1
        if maxlen < minmaxlen:
            msg = 'Maximal line length less than {0} when using an indentation'\
                  ' of {1}'.format(minmaxlen, indent)
            raise FyppFatalError(msg)
        self._maxlen = maxlen
        self._indent = indent
        self._prefix = ' ' * self._indent + prefix
        self._suffix = suffix
        if method not in ['brute', 'smart', 'simple']:
            raise FyppFatalError('invalid folding type')
        if method == 'brute':
            self._inherit_indent = False
            self._fold_position_finder = self._get_maximal_fold_pos
        elif method == 'simple':
            self._inherit_indent = True
            self._fold_position_finder = self._get_maximal_fold_pos
        elif method == 'smart':
            self._inherit_indent = True
            self._fold_position_finder = self._get_smart_fold_pos
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def run_fypp():
    '''Run the Fypp command line tool.'''
    options = FyppOptions()
    optparser = get_option_parser()
    opts, leftover = optparser.parse_args(values=options)
    infile = leftover[0] if len(leftover) > 0 else '-'
    outfile = leftover[1] if len(leftover) > 1 else '-'
    try:
        tool = Fypp(opts)
        tool.process_file(infile, outfile)
    except FyppStopRequest as exc:
        sys.stderr.write(_formatted_exception(exc))
        sys.exit(USER_ERROR_EXIT_CODE)
    except FyppFatalError as exc:
        sys.stderr.write(_formatted_exception(exc))
        sys.exit(ERROR_EXIT_CODE)
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _get_callable_argspec_py2(func):
    argspec = inspect.getargspec(func)
    if argspec.keywords is not None:
        msg = "variable length keyword argument '{0}' found"\
            .format(argspec.keywords)
        raise FyppFatalError(msg)
    vararg = argspec.varargs
    args = argspec.args
    tuplearg = False
    for elem in args:
        tuplearg = tuplearg or isinstance(elem, list)
    if tuplearg:
        msg = 'tuple argument(s) found'
        raise FyppFatalError(msg)
    defaults = {}
    if argspec.defaults is not None:
        for ind, default in enumerate(argspec.defaults):
            iarg = len(args) - len(argspec.defaults) + ind
            defaults[args[iarg]] = default
    return args, defaults, vararg
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_modify_builtins_from_leaf_function(self):
        # Verify that modifications made by leaf functions percolate up the
        # callstack.
        with swap_attr(builtins, "len", len):
            def bar():
                builtins.len = lambda x: 4

            def foo(modifier):
                l = []
                l.append(len(range(7)))
                modifier()
                l.append(len(range(7)))
                return l
            self.configure_func(foo, lambda: None)

            self.assertEqual(foo(bar), [7, 4])
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_modify_builtins_from_leaf_function(self):
        # Verify that modifications made by leaf functions percolate up the
        # callstack.
        with swap_attr(builtins, "len", len):
            def bar():
                builtins.len = lambda x: 4

            def foo(modifier):
                l = []
                l.append(len(range(7)))
                modifier()
                l.append(len(range(7)))
                return l
            self.configure_func(foo, lambda: None)

            self.assertEqual(foo(bar), [7, 4])
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_modify_builtins_from_leaf_function(self):
        # Verify that modifications made by leaf functions percolate up the
        # callstack.
        with swap_attr(builtins, "len", len):
            def bar():
                builtins.len = lambda x: 4

            def foo(modifier):
                l = []
                l.append(len(range(7)))
                modifier()
                l.append(len(range(7)))
                return l
            self.configure_func(foo, lambda: None)

            self.assertEqual(foo(bar), [7, 4])
项目:reframe    作者:eth-cscs    | 项目源码 | 文件源码
def count(iterable):
    """Return the element count of ``iterable``.

    This is similar to the built-in :func:`len() <python:len>`, except that it
    can also handle any argument that supports iteration, including
    generators.
    """
    try:
        return builtins.len(iterable)
    except TypeError:
        # Try to determine length by iterating over the iterable
        ret = 0
        for ret, _ in builtins.enumerate(iterable, start=1):
            pass

        return ret
项目:reframe    作者:eth-cscs    | 项目源码 | 文件源码
def test_findall(self):
        res = evaluate(findall('Step: \d+', self.tempfile))
        self.assertEqual(3, builtins.len(res))

        res = evaluate(findall('Step:.*', self.tempfile))
        self.assertEqual(3, builtins.len(res))

        res = evaluate(findall('Step: [12]', self.tempfile))
        self.assertEqual(2, builtins.len(res))

        # Check the matches
        for expected, match in builtins.zip(['Step: 1', 'Step: 2'], res):
            self.assertEqual(expected, match.group(0))

        # Check groups
        res = evaluate(findall('Step: (?P<no>\d+)', self.tempfile))
        for step, match in builtins.enumerate(res, start=1):
            self.assertEqual(step, builtins.int(match.group(1)))
            self.assertEqual(step, builtins.int(match.group('no')))
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_globals_shadow_builtins(self):
        # Modify globals() to shadow an entry in builtins.
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_item(globals(), "len", lambda x: 7):
            self.assertEqual(foo(), 7)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_modify_builtins(self):
        # Modify the builtins module directly.
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_attr(builtins, "len", lambda x: 7):
            self.assertEqual(foo(), 7)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_modify_builtins_while_generator_active(self):
        # Modify the builtins out from under a live generator.
        def foo():
            x = range(3)
            yield len(x)
            yield len(x)
        self.configure_func(foo)

        g = foo()
        self.assertEqual(next(g), 3)
        with swap_attr(builtins, "len", lambda x: 7):
            self.assertEqual(next(g), 7)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_cannot_change_globals_or_builtins_with_eval(self):
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        # Note that this *doesn't* change the definition of len() seen by foo().
        builtins_dict = {"len": lambda x: 7}
        globals_dict = {"foo": foo, "__builtins__": builtins_dict,
                        "len": lambda x: 8}
        self.assertEqual(eval("foo()", globals_dict), 3)

        self.assertEqual(eval("foo()", {"foo": foo}), 3)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_cannot_replace_builtins_dict_while_active(self):
        def foo():
            x = range(3)
            yield len(x)
            yield len(x)
        self.configure_func(foo)

        g = foo()
        self.assertEqual(next(g), 3)
        with swap_item(globals(), "__builtins__", {"len": lambda x: 7}):
            self.assertEqual(next(g), 3)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_cannot_replace_builtins_dict_between_calls(self):
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_item(globals(), "__builtins__", {"len": lambda x: 7}):
            self.assertEqual(foo(), 3)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_eval_gives_lambda_custom_globals(self):
        globals_dict = {"len": lambda x: 7}
        foo = eval("lambda: len([])", globals_dict)
        self.configure_func(foo)

        self.assertEqual(foo(), 7)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def len(obj):
    try:
        return _len(obj)
    except TypeError:
        try:
            # note: this is an internal undocumented API,
            # don't rely on it in your own programs
            return obj.__length_hint__()
        except AttributeError:
            raise TypeError
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def handle_include(self, span, fname):
        '''Should be called to signalize change to new file.

        Args:
            span (tuple of int): Start and end line of the include directive
                or None if called the first time for the main input.
            fname (str): Name of the file to be included.
        '''
        self._path.append(self._curnode)
        self._curnode = []
        self._open_blocks.append(
            ('include', self._curfile, [span], fname, None))
        self._curfile = fname
        self._nr_prev_blocks.append(len(self._open_blocks))
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def handle_endinclude(self, span, fname):
        '''Should be called when processing of a file finished.

        Args:
            span (tuple of int): Start and end line of the include directive
                or None if called the first time for the main input.
            fname (str): Name of the file which has been included.
        '''
        nprev_blocks = self._nr_prev_blocks.pop(-1)
        if len(self._open_blocks) > nprev_blocks:
            directive, fname, spans = self._open_blocks[-1][0:3]
            msg = '{0} directive still unclosed when reaching end of file'\
                  .format(directive)
            raise FyppFatalError(msg, self._curfile, spans[0])
        block = self._open_blocks.pop(-1)
        directive, blockfname, spans = block[0:3]
        if directive != 'include':
            msg = 'internal error: last open block is not \'include\' when '\
                  'closing file \'{0}\''.format(fname)
            raise FyppFatalError(msg)
        if span != spans[0]:
            msg = 'internal error: span for include and endinclude differ ('\
                  '{0} vs {1}'.format(span, spans[0])
            raise FyppFatalError(msg)
        oldfname, _ = block[3:5]
        if fname != oldfname:
            msg = 'internal error: mismatching file name in close_file event'\
                  " (expected: '{0}', got: '{1}')".format(oldfname, fname)
            raise FyppFatalError(msg, fname)
        block = directive, blockfname, spans, fname, self._curnode
        self._curnode = self._path.pop(-1)
        self._curnode.append(block)
        self._curfile = blockfname
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def handle_endcall(self, span, name):
        '''Should be called to signalize an endcall directive.

        Args:
            span (tuple of int): Start and end line of the directive.
            name (str): Name of the endcall statement. Could be None, if endcall
                was specified without name.
        '''
        self._check_for_open_block(span, 'endcall')
        block = self._open_blocks.pop(-1)
        directive, fname, spans = block[0:3]
        self._check_if_matches_last(directive, 'call', spans[0], span,
                                    'endcall')
        callname, callargexpr, args, argnames = block[3:7]
        if name is not None and name != callname:
            msg = "wrong name in endcall directive "\
                  "(expected '{0}', got '{1}')".format(callname, name)
            raise FyppFatalError(msg, fname, span)
        args.append(self._curnode)
        # If nextarg or endcall immediately followed call, then first argument
        # is empty and should be removed (to allow for calls without arguments
        # and named first argument in calls)
        if args and not args[0]:
            if len(argnames) == len(args):
                del argnames[0]
            del args[0]
            del spans[1]
        spans.append(span)
        block = (directive, fname, spans, callname, callargexpr, args, argnames)
        self._curnode = self._path.pop(-1)
        self._curnode.append(block)
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _check_for_open_block(self, span, directive):
        if len(self._open_blocks) <= self._nr_prev_blocks[-1]:
            msg = 'unexpected {0} directive'.format(directive)
            raise FyppFatalError(msg, self._curfile, span)
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _get_iterated_content(self, fname, spans, loopvars, loopiter, content):
        out = []
        ieval = []
        peval = []
        try:
            iterobj = iter(self._evaluate(loopiter, fname, spans[0][0]))
        except Exception as exc:
            msg = "exception occured when evaluating '{0}'"\
                .format(loopiter)
            raise FyppFatalError(msg, fname, spans[0], exc)
        multiline = (spans[0][0] != spans[-1][1])
        for var in iterobj:
            if len(loopvars) == 1:
                self._define(loopvars[0], var)
            else:
                for varname, value in zip(loopvars, var):
                    self._define(varname, value)
            if self._linenums and not self._diverted and multiline:
                out.append(linenumdir(spans[0][1], fname))
            outcont, ievalcont, pevalcont = self._render(content)
            ieval += _shiftinds(ievalcont, len(out))
            peval += pevalcont
            out += outcont
        if self._linenums and not self._diverted and multiline:
            out.append(linenumdir(spans[1][1], fname))
        return out, ieval, peval
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _get_call_arguments(self, fname, spans, argexpr, contents, argnames):
        if argexpr is None:
            posargs = []
            kwargs = {}
        else:
            # Parse and evaluate arguments passed in call header
            self._evaluator.openscope()
            try:
                posargs, kwargs = self._evaluate(
                    '__getargvalues(' + argexpr + ')', fname, spans[0][0])
            except Exception as exc:
                msg = "unable to parse argument expression '{0}'"\
                    .format(argexpr)
                raise FyppFatalError(msg, fname, spans[0], exc)
            self._evaluator.closescope()

        # Render arguments passed in call body
        args = []
        for content in contents:
            self._evaluator.openscope()
            rendered = self.render(content, divert=True)
            self._evaluator.closescope()
            if rendered.endswith('\n'):
                rendered = rendered[:-1]
            args.append(rendered)

        # Separate arguments in call body into positional and keyword ones:
        if argnames:
            posargs += args[:len(args) - len(argnames)]
            offset = len(args) - len(argnames)
            for iargname, argname in enumerate(argnames):
                ind = offset + iargname
                if argname in kwargs:
                    msg = "keyword argument '{0}' already defined"\
                        .format(argname)
                    raise FyppFatalError(msg, fname, spans[ind + 1])
                kwargs[argname] = args[ind]
        else:
            posargs += args

        return posargs, kwargs
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _get_included_content(self, fname, spans, includefname, content):
        includefile = spans[0] is not None
        out = []
        if self._linenums and not self._diverted:
            if includefile or self._linenum_gfortran5:
                out += linenumdir(0, includefname, _LINENUM_NEW_FILE)
            else:
                out += linenumdir(0, includefname)
        outcont, ieval, peval = self._render(content)
        ieval = _shiftinds(ieval, len(out))
        out += outcont
        if self._linenums and not self._diverted and includefile:
            out += linenumdir(spans[0][1], fname, _LINENUM_RETURN_TO_FILE)
        return out, ieval, peval
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _find_next_eol(output, ind):
        'Find last newline before current position.'
        # find first eol after expr. evaluation
        inext = ind + 1
        while inext < len(output):
            eolnext = output[inext].find('\n')
            if eolnext != -1:
                break
            inext += 1
        else:
            inext = len(output) - 1
            eolnext = len(output[-1]) - 1
        return inext, eolnext
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def define(self, name, value):
        '''Define a Python entity.

        Args:
            name (str): Name of the entity.
            value (Python object): Value of the entity.

        Raises:
            FyppFatalError: If name starts with the reserved prefix or if it is
                a reserved name.
        '''
        varnames = self._get_variable_names(name)
        if len(varnames) == 1:
            value = (value,)
        elif len(varnames) != len(value):
            msg = 'value for tuple assignment has incompatible length'
            raise FyppFatalError(msg)
        for varname, varvalue in zip(varnames, value):
            self._check_variable_name(varname)
            if self._locals is None:
                self._globals[varname] = varvalue
            else:
                if varname in self._globalrefs:
                    self._globals[varname] = varvalue
                else:
                    self._locals[varname] = varvalue
                self._scope[varname] = varvalue
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _func_setvar(self, *namesvalues):
        if len(namesvalues) % 2:
            msg = 'setvar function needs an even number of arguments'
            raise FyppFatalError(msg)
        for ind in range(0, len(namesvalues), 2):
            self.define(namesvalues[ind], namesvalues[ind + 1])
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _process_arguments(self, args, keywords):
        argdict = {}
        nargs = min(len(args), len(self._argnames))
        for iarg in range(nargs):
            argdict[self._argnames[iarg]] = args[iarg]
        if nargs < len(args):
            if self._varargs is None:
                msg = "macro '{0}' called with too many positional arguments "\
                      "(expected: {1}, received: {2})"\
                      .format(self._name, len(self._argnames), len(args))
                raise FyppFatalError(msg, self._fname, self._spans[0])
            else:
                argdict[self._varargs] = tuple(args[nargs:])
        elif self._varargs is not None:
            argdict[self._varargs] = ()
        for argname in self._argnames[:nargs]:
            if argname in keywords:
                msg = "got multiple values for argument '{0}'".format(argname)
                raise FyppFatalError(msg, self._fname, self._spans[0])
        if self._varargs is not None and self._varargs in keywords:
            msg = "got unexpected keyword argument '{0}'".format(self._varargs)
            raise FyppFatalError(msg, self._fname, self._spans[0])
        argdict.update(keywords)
        if nargs < len(self._argnames):
            for argname in self._argnames[nargs:]:
                if argname in argdict:
                    pass
                elif argname in self._defaults:
                    argdict[argname] = self._defaults[argname]
                else:
                    msg = "macro '{0}' called without mandatory positional "\
                          "argument '{1}'".format(self._name, argname)
                    raise FyppFatalError(msg, self._fname, self._spans[0])
        return argdict
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _apply_definitions(defines, evaluator):
        for define in defines:
            words = define.split('=', 2)
            name = words[0]
            value = None
            if len(words) > 1:
                try:
                    value = evaluator.evaluate(words[1])
                except Exception as exc:
                    msg = "exception at evaluating '{0}' in definition for " \
                          "'{1}'".format(words[1], name)
                    raise FyppFatalError(msg, cause=exc)
            evaluator.define(name, value)
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _split_line(line, maxlen, prefix, suffix, fold_position_finder):
        # length of continuation lines with 1 or two continuation chars.
        maxlen1 = maxlen - len(prefix)
        maxlen2 = maxlen1 - len(suffix)
        start = 0
        end = fold_position_finder(line, start, maxlen - len(suffix))
        result = [line[start:end] + suffix]
        while end < len(line) - maxlen1:
            start = end
            end = fold_position_finder(line, start, start + maxlen2)
            result.append(prefix + line[start:end] + suffix)
        result.append(prefix + line[end:])
        return result
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _argsplit_fortran(argtxt):
    txt = _INLINE_EVAL_REGION_REGEXP.sub(_blank_match, argtxt)
    splitpos = [-1]
    quote = None
    closing_brace_stack = []
    closing_brace = None
    for ind, char in enumerate(txt):
        if quote:
            if char == quote:
                quote = None
            continue
        if char in _QUOTES_FORTRAN:
            quote = char
            continue
        if char in _OPENING_BRACKETS_FORTRAN:
            closing_brace_stack.append(closing_brace)
            ind = _OPENING_BRACKETS_FORTRAN.index(char)
            closing_brace = _CLOSING_BRACKETS_FORTRAN[ind]
            continue
        if char in _CLOSING_BRACKETS_FORTRAN:
            if char == closing_brace:
                closing_brace = closing_brace_stack.pop(-1)
                continue
            else:
                msg = "unexpected closing delimiter '{0}' in expression '{1}' "\
                      "at position {2}".format(char, argtxt, ind + 1)
                raise FyppFatalError(msg)
        if not closing_brace and char == _ARGUMENT_SPLIT_CHAR_FORTRAN:
            splitpos.append(ind)
    if quote or closing_brace:
        msg = "open quotes or brackets in expression '{0}'".format(argtxt)
        raise FyppFatalError(msg)
    splitpos.append(len(txt))
    fragments = [argtxt[start + 1 : end]
                 for start, end in zip(splitpos, splitpos[1:])]
    return fragments
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_globals_shadow_builtins(self):
        # Modify globals() to shadow an entry in builtins.
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_item(globals(), "len", lambda x: 7):
            self.assertEqual(foo(), 7)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_modify_builtins(self):
        # Modify the builtins module directly.
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_attr(builtins, "len", lambda x: 7):
            self.assertEqual(foo(), 7)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_modify_builtins_while_generator_active(self):
        # Modify the builtins out from under a live generator.
        def foo():
            x = range(3)
            yield len(x)
            yield len(x)
        self.configure_func(foo)

        g = foo()
        self.assertEqual(next(g), 3)
        with swap_attr(builtins, "len", lambda x: 7):
            self.assertEqual(next(g), 7)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_cannot_change_globals_or_builtins_with_eval(self):
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        # Note that this *doesn't* change the definition of len() seen by foo().
        builtins_dict = {"len": lambda x: 7}
        globals_dict = {"foo": foo, "__builtins__": builtins_dict,
                        "len": lambda x: 8}
        self.assertEqual(eval("foo()", globals_dict), 3)

        self.assertEqual(eval("foo()", {"foo": foo}), 3)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_cannot_replace_builtins_dict_while_active(self):
        def foo():
            x = range(3)
            yield len(x)
            yield len(x)
        self.configure_func(foo)

        g = foo()
        self.assertEqual(next(g), 3)
        with swap_item(globals(), "__builtins__", {"len": lambda x: 7}):
            self.assertEqual(next(g), 3)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_cannot_replace_builtins_dict_between_calls(self):
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_item(globals(), "__builtins__", {"len": lambda x: 7}):
            self.assertEqual(foo(), 3)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_eval_gives_lambda_custom_globals(self):
        globals_dict = {"len": lambda x: 7}
        foo = eval("lambda: len([])", globals_dict)
        self.configure_func(foo)

        self.assertEqual(foo(), 7)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def len(obj):
    try:
        return _len(obj)
    except TypeError:
        try:
            # note: this is an internal undocumented API,
            # don't rely on it in your own programs
            return obj.__length_hint__()
        except AttributeError:
            raise TypeError
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_globals_shadow_builtins(self):
        # Modify globals() to shadow an entry in builtins.
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_item(globals(), "len", lambda x: 7):
            self.assertEqual(foo(), 7)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_modify_builtins(self):
        # Modify the builtins module directly.
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_attr(builtins, "len", lambda x: 7):
            self.assertEqual(foo(), 7)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_modify_builtins_while_generator_active(self):
        # Modify the builtins out from under a live generator.
        def foo():
            x = range(3)
            yield len(x)
            yield len(x)
        self.configure_func(foo)

        g = foo()
        self.assertEqual(next(g), 3)
        with swap_attr(builtins, "len", lambda x: 7):
            self.assertEqual(next(g), 7)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_cannot_change_globals_or_builtins_with_eval(self):
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        # Note that this *doesn't* change the definition of len() seen by foo().
        builtins_dict = {"len": lambda x: 7}
        globals_dict = {"foo": foo, "__builtins__": builtins_dict,
                        "len": lambda x: 8}
        self.assertEqual(eval("foo()", globals_dict), 3)

        self.assertEqual(eval("foo()", {"foo": foo}), 3)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_cannot_replace_builtins_dict_while_active(self):
        def foo():
            x = range(3)
            yield len(x)
            yield len(x)
        self.configure_func(foo)

        g = foo()
        self.assertEqual(next(g), 3)
        with swap_item(globals(), "__builtins__", {"len": lambda x: 7}):
            self.assertEqual(next(g), 3)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_cannot_replace_builtins_dict_between_calls(self):
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_item(globals(), "__builtins__", {"len": lambda x: 7}):
            self.assertEqual(foo(), 3)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_eval_gives_lambda_custom_globals(self):
        globals_dict = {"len": lambda x: 7}
        foo = eval("lambda: len([])", globals_dict)
        self.configure_func(foo)

        self.assertEqual(foo(), 7)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_globals_shadow_builtins(self):
        # Modify globals() to shadow an entry in builtins.
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_item(globals(), "len", lambda x: 7):
            self.assertEqual(foo(), 7)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_modify_builtins(self):
        # Modify the builtins module directly.
        def foo():
            return len([1, 2, 3])
        self.configure_func(foo)

        self.assertEqual(foo(), 3)
        with swap_attr(builtins, "len", lambda x: 7):
            self.assertEqual(foo(), 7)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_modify_builtins_while_generator_active(self):
        # Modify the builtins out from under a live generator.
        def foo():
            x = range(3)
            yield len(x)
            yield len(x)
        self.configure_func(foo)

        g = foo()
        self.assertEqual(next(g), 3)
        with swap_attr(builtins, "len", lambda x: 7):
            self.assertEqual(next(g), 7)