示例#1
0
    def upgrade_websocket(self, environ, start_response):
        """
        Attempt to upgrade the socket environ['wsgi.input'] into a websocket enabled connection.
        """
        websocket_version = environ.get('HTTP_SEC_WEBSOCKET_VERSION', '')
        if not websocket_version:
            raise UpgradeRequiredError
        elif websocket_version not in self.WS_VERSIONS:
            raise HandshakeError('Unsupported WebSocket Version: {0}'.format(websocket_version))

        key = environ.get('HTTP_SEC_WEBSOCKET_KEY', '').strip()
        if not key:
            raise HandshakeError('Sec-WebSocket-Key header is missing/empty')
        try:
            key_len = len(base64.b64decode(key))
        except TypeError:
            raise HandshakeError('Invalid key: {0}'.format(key))
        if key_len != 16:
            # 5.2.1 (3)
            raise HandshakeError('Invalid key: {0}'.format(key))

        sec_ws_accept = base64.b64encode(sha1(six.b(key) + self.WS_GUID).digest())
        if six.PY3:
            sec_ws_accept = sec_ws_accept.decode('ascii')
        headers = [
            ('Upgrade', 'websocket'),
            ('Connection', 'Upgrade'),
            ('Sec-WebSocket-Accept', sec_ws_accept),
            ('Sec-WebSocket-Version', str(websocket_version)),
        ]
        logger.debug('WebSocket request accepted, switching protocols')
        start_response(force_str('101 Switching Protocols'), headers)
        six.get_method_self(start_response).finish_content()
        return WebSocket(environ['wsgi.input'])
    def test_add_testcase_info(self, mock_methods):
        # Build a fake runner
        test_case = TestCase()
        runner = TestRunner(test_case)

        # Populate runner with fake structure
        runner.unittests = {}
        runner.unittests['mod class.sample_method1'] = True
        runner.unittests['mod class.sample_method2'] = False
        runner.unittests['mod class.sample_method3'] = True

        # Populate fake test_case with 3 fake methods
        self1_t = type(six.get_method_self(self.sample_method1))
        self2_t = type(six.get_method_self(self.sample_method2))
        self3_t = type(six.get_method_self(self.sample_method3))
        self1_t.__name__ = self2_t.__name__ = self3_t.__name__ = 'class'
        self1_t.__module__ = self2_t.__module__ = self3_t.__module__ = 'mod'

        test_methods = [self.sample_method1, self.sample_method2, self.sample_method3]

        # Run add_testcase_info
        mock_methods.return_value = test_methods
        add_testcase_info(test_case, runner)

        # Verify that unittests work
        suites1 = getattr(self.sample_method1.__func__, '_suites', [])
        self.assertEqual('unittest' in suites1, True)
        suites2 = getattr(self.sample_method2.__func__, '_suites', [])
        self.assertEqual('unittest' not in suites2, True)
        self.assertEqual('test_suite' in suites2, True)
        suites3 = getattr(self.sample_method3.__func__, '_suites', [])
        self.assertEqual('unittest' in suites3, True)
示例#3
0
文件: dill.py 项目: wxiang7/dill
def save_instancemethod0(pickler, obj):  # example: cStringIO.StringI
    log.info("Me: %s" % obj)             # XXX: obj.__dict__ handled elsewhere?

    args = (get_method_function(obj), get_method_self(obj)) if PY3 \
        else (get_method_function(obj), get_method_self(obj), obj.im_class)

    pickler.save_reduce(MethodType, args, obj=obj)

    log.info("# Me")
示例#4
0
def getCallableString(callable):
    '''
    Returns a translated human readable string representing a callable.

    Parameters
    ----------
    callable: callable
        *function*, *method*, *class* or *instance* to inspect

    Returns
    --------
    string:
        type and name of the callable
    '''
    if inspect.isfunction(callable):
        name = _('function %s') % (callable.__name__, )
    elif inspect.ismethod(callable):
        name = _('method %s') % (
            six.get_method_self(callable).__class__.__name__ + '.' +
            callable.__name__, )
    elif inspect.isclass(callable):
        name = _('class %s') % (callable.__name__, )
    else:
        name = str(callable)
    return name
示例#5
0
def get_method_self(method):
    if not inspect.ismethod(method):
        return None
    try:
        return six.get_method_self(method)
    except AttributeError:
        return None
示例#6
0
文件: api.py 项目: TheSriram/falcon
    def _prepare_mw(middleware=None):
        """Check middleware interface and prepare it to iterate.

        Args:
            middleware:  list (or object) of input middleware

        Returns:
            A middleware list
        """
        if middleware is None:
            middleware = []
        else:
            if not isinstance(middleware, list):
                middleware = [middleware]

        # check basic interface of middleware objects
        for mw in middleware:
            if not hasattr(mw, 'process_request') and not\
                    hasattr(mw, 'process_response'):

                raise TypeError('{0} is not a valid middlware'.format(str(mw)))

            # Check process_request and process_response are bounded methods
            for mw_method in ('process_request', 'process_response'):
                method_mw_bound = getattr(mw, mw_method, None)

                if method_mw_bound is not None:

                    if six.get_method_self(method_mw_bound) is None:
                        raise AttributeError(
                            '{0} must be a bound method'.format(method_mw_bound))\
                            # pragma: no cover

        return middleware
示例#7
0
def get_function_path(function, bound_to=None):
    """Get received function path (as string), to import function later
    with `import_string`.
    """
    if isinstance(function, six.string_types):
        return function

    # static and class methods
    if hasattr(function, '__func__'):
        real_function = function.__func__
    elif callable(function):
        real_function = function
    else:
        return function

    func_path = []

    module = getattr(real_function, '__module__', '__main__')
    if module:
        func_path.append(module)

    if not bound_to:
        try:
            bound_to = six.get_method_self(function)
        except AttributeError:
            pass

    if bound_to:
        if isinstance(bound_to, six.class_types):
            func_path.append(bound_to.__name__)
        else:
            func_path.append(bound_to.__class__.__name__)

    func_path.append(real_function.__name__)
    return '.'.join(func_path)
示例#8
0
def validate_args(fn, *args, **kwargs):
    """Check that the supplied args are sufficient for calling a function.

    >>> validate_args(lambda a: None)
    Traceback (most recent call last):
        ...
    MissingArgs: Missing argument(s): a
    >>> validate_args(lambda a, b, c, d: None, 0, c=1)
    Traceback (most recent call last):
        ...
    MissingArgs: Missing argument(s): b, d

    :param fn: the function to check
    :param arg: the positional arguments supplied
    :param kwargs: the keyword arguments supplied
    """
    argspec = inspect.getargspec(fn)

    num_defaults = len(argspec.defaults or [])
    required_args = argspec.args[:len(argspec.args) - num_defaults]

    if six.get_method_self(fn) is not None:
        required_args.pop(0)

    missing = [arg for arg in required_args if arg not in kwargs]
    missing = missing[len(args):]
    return missing
示例#9
0
def test_get_method_self():
    class X(object):
        def m(self):
            pass
    x = X()
    assert six.get_method_self(x.m) is x
    py.test.raises(AttributeError, six.get_method_self, 42)
示例#10
0
    def __init__(self, func):
        """Create a new ``WeakFunctionRef`` to encapsulate the given
        function or bound/unbound method.
        """

        # Bound method
        if self.__isMethod(func):

            boundMeth = six.get_method_function(func)
            boundSelf = six.get_method_self(func)

            # We can't take a weakref of the method
            # object, so we have to weakref the object
            # and the unbound class function. The
            # function method will search for and
            # return the bound method, though.
            self.obj = weakref.ref(boundSelf)
            self.func = weakref.ref(boundMeth)

            self.objType = type(boundSelf).__name__
            self.funcName = boundMeth.__name__

        # Unbound/class method or function
        else:

            self.obj = None
            self.objType = None
            self.func = weakref.ref(func)
            self.funcName = func.__name__
示例#11
0
 def to_dict(self):
     test_method_self_t = type(six.get_method_self(self.test_method))
     assert not isinstance(test_method_self_t, type(None))
     return {
         "previous_run": self.previous_run,
         "start_time": time.mktime(self.start_time.timetuple()) if self.start_time else None,
         "end_time": time.mktime(self.end_time.timetuple()) if self.end_time else None,
         "run_time": (self.run_time.seconds + float(self.run_time.microseconds) / 1000000)
         if self.run_time is not None
         else None,
         "normalized_run_time": None
         if not self.run_time
         else "%.2fs" % (self.run_time.seconds + (self.run_time.microseconds / 1000000.0)),
         "complete": self.complete,
         "success": self.success,
         "failure": self.failure,
         "error": self.error,
         "interrupted": self.interrupted,
         "exception_info": self.format_exception_info(),
         "exception_info_pretty": self.format_exception_info(pretty=True),
         "exception_only": self.format_exception_only(),
         "runner_id": self.runner_id,
         "method": {
             "module": test_method_self_t.__module__,
             "class": test_method_self_t.__name__,
             "name": self.test_method.__name__,
             "full_name": "%s %s.%s"
             % (test_method_self_t.__module__, test_method_self_t.__name__, self.test_method.__name__),
             "fixture_type": None
             if not inspection.is_fixture_method(self.test_method)
             else self.test_method._fixture_type,
         },
     }
示例#12
0
def get_bound_method(obj, method_name):
    method = getattr(obj, method_name, None)
    if method is not None:
        if six.get_method_self(method) is None:
            msg = '{0} must be a bound method'.format(method)
            raise AttributeError(msg)
    return method
示例#13
0
    def ensure_generator(self, fixture):
        if fixture._fixture_type in HYBRID_FIXTURES:
            # already a context manager, nothing to do
            return fixture

        if fixture._fixture_type in SETUP_FIXTURES:

            def wrapper(self):
                fixture()
                yield

        elif fixture._fixture_type in TEARDOWN_FIXTURES:

            def wrapper(self):
                yield
                fixture()

        wrapper.__name__ = fixture.__name__
        wrapper.__doc__ = fixture.__doc__
        wrapper._fixture_type = fixture._fixture_type
        wrapper._fixture_id = fixture._fixture_id
        wrapper._defining_class_depth = fixture._defining_class_depth

        # http://stackoverflow.com/q/4364565
        func_self = six.get_method_self(fixture)
        assert func_self is not None
        return wrapper.__get__(func_self, type(func_self))
示例#14
0
文件: misc.py 项目: balsagoth/jasmin
def get_bound_method(obj, method_name):
    """Get a bound method of the given object by name.

    Args:
        obj: Object on which to look up the method.
        method_name: Name of the method to retrieve.

    Returns:
        Bound method, or ``None`` if the method does not exist on
        the object.

    Raises:
        AttributeError: The method exists, but it isn't
            bound (most likely a class was passed, rather than
            an instance of that class).

    """

    method = getattr(obj, method_name, None)
    if method is not None:
        # NOTE(kgriffs): Ensure it is a bound method
        if six.get_method_self(method) is None:
            # NOTE(kgriffs): In Python 3 this code is unreachable
            # because the above will raise AttributeError on its
            # own.
            msg = '{0} must be a bound method'.format(method)
            raise AttributeError(msg)

    return method
示例#15
0
文件: utils.py 项目: yochow/nova
def validate_args(fn, *args, **kwargs):
    """Check that the supplied args are sufficient for calling a function.

    >>> validate_args(lambda a: None)
    Traceback (most recent call last):
        ...
    MissingArgs: Missing argument(s): a
    >>> validate_args(lambda a, b, c, d: None, 0, c=1)
    Traceback (most recent call last):
        ...
    MissingArgs: Missing argument(s): b, d

    :param fn: the function to check
    :param arg: the positional arguments supplied
    :param kwargs: the keyword arguments supplied
    """
    argspec = inspect.getargspec(fn)

    num_defaults = len(argspec.defaults or [])
    required_args = argspec.args[:len(argspec.args) - num_defaults]

    if six.get_method_self(fn) is not None:
        required_args.pop(0)

    missing = [arg for arg in required_args if arg not in kwargs]
    missing = missing[len(args):]
    return missing
示例#16
0
文件: property.py 项目: b3j0f/utils
def find_ctx(elt):
    """Get the right ctx related to input elt.

    In order to keep safe memory as much as possible, it is important to find
    the right context element. For example, instead of putting properties on
    a function at the level of an instance, it is important to save such
    property on the instance because the function.__dict__ is shared with
    instance class function, and so, if the instance is deleted from memory,
    the property is still present in the class memory. And so on, it is
    impossible to identify the right context in such case if all properties
    are saved with the same key in the same function which is the function.
    """

    result = elt  # by default, result is ctx

    # if elt is ctx and elt is a method, it is possible to find the best ctx
    if ismethod(elt):
        # get instance and class of the elt
        instance = get_method_self(elt)
        # if instance is not None, the right context is the instance
        if instance is not None:
            result = instance

        elif PY2:
            result = elt.im_class

    return result
示例#17
0
    def __isMethod(self, func):
        """Returns ``True`` if the given function is a bound method,
        ``False`` otherwise.

        This seems to be one of the few areas where python 2 and 3 are
        irreconcilably incompatible (or just where :mod:`six` does not have a
        function to help us).

        In Python 2 there is no difference between an unbound method and a
        function. But in Python 3, an unbound method is still a method (and
        inspect.ismethod returns True).
        """

        ismethod = False

        # Therefore, in python2 we need to test
        # whether the function is a method, and
        # also test whether it is bound.
        if six.PY2:
            ismethod = (inspect.ismethod(func)
                        and six.get_method_self(func) is not None)

        # But in python3, if the function is a
        # method it is, by definition, bound.
        elif six.PY3:
            ismethod = inspect.ismethod(func)

        return ismethod
示例#18
0
 def clear_api_path_map_cache(self):
     """Clear out cache for api_path_map."""
     self._api_path_cache = None
     for api_provider in self.api_providers:
         if six.get_method_self(
                 api_provider.clear_api_path_map_cache, ) is not None:
             api_provider.clear_api_path_map_cache()
示例#19
0
def get_bound_method(obj, method_name):
    """Get a bound method of the given object by name.

    Args:
        obj: Object on which to look up the method.
        method_name: Name of the method to retrieve.

    Returns:
        Bound method, or ``None`` if the method does not exist on
        the object.

    Raises:
        AttributeError: The method exists, but it isn't
            bound (most likely a class was passed, rather than
            an instance of that class).

    """

    method = getattr(obj, method_name, None)
    if method is not None:
        # NOTE(kgriffs): Ensure it is a bound method
        if six.get_method_self(method) is None:
            # NOTE(kgriffs): In Python 3 this code is unreachable
            # because the above will raise AttributeError on its
            # own.
            msg = '{0} must be a bound method'.format(method)
            raise AttributeError(msg)

    return method
示例#20
0
 def raiser(*args, **kwargs):  # pylint: disable= missing-docstring
     name = func.__name__
     if inspect.ismethod(func):
         name = ".".join(
             [six.get_method_self(func).__class__.__name__, name])
     msg = txt % (name)
     raise NotImplementedError(msg)
示例#21
0
文件: test_six.py 项目: A-Maze/A-Pc
def test_get_method_self():
    class X(object):
        def m(self):
            pass
    x = X()
    assert six.get_method_self(x.m) is x
    py.test.raises(AttributeError, six.get_method_self, 42)
示例#22
0
    def ensure_generator(self, fixture):
        if fixture._fixture_type in HYBRID_FIXTURES:
            # already a context manager, nothing to do
            return fixture

        if fixture._fixture_type in SETUP_FIXTURES:

            def wrapper(self):
                fixture()
                yield
        elif fixture._fixture_type in TEARDOWN_FIXTURES:

            def wrapper(self):
                yield
                fixture()

        wrapper.__name__ = fixture.__name__
        wrapper.__doc__ = fixture.__doc__
        wrapper._fixture_type = fixture._fixture_type
        wrapper._fixture_id = fixture._fixture_id
        wrapper._defining_class_depth = fixture._defining_class_depth

        # http://stackoverflow.com/q/4364565
        func_self = six.get_method_self(fixture)
        assert func_self is not None
        return wrapper.__get__(func_self, type(func_self))
示例#23
0
def get_method_self(method):
    if not inspect.ismethod(method):
        return None
    try:
        return six.get_method_self(method)
    except AttributeError:
        return None
示例#24
0
    def runiter(self, slot_name, **kwargs):
        """Return an iterable of PluginHookRunner objects.

        The iterable will return a PluginHookRunner object
        for each plugin hook mapped to slot_name. Multiple plugins
        with hooks for the same slot will result in multiple
        PluginHookRunners in the iterable.

        See run() docs for what to expect from PluginHookRunner.run().
        """
        # slot's called should always exist here, if not
        if slot_name not in self._slot_to_funcs:
            raise SlotNameException(slot_name)

        for func in self._slot_to_funcs[slot_name]:
            module = inspect.getmodule(func)
            func_module_name = getattr(func, '__module__')
            if not func_module_name:
                if module:
                    func_module_name = module.__name__
                else:
                    func_module_name = 'unknown_module'
            func_class_name = six.get_method_self(func).__class__.__name__
            plugin_key = ".".join([func_module_name, func_class_name])
            log.debug("Running %s in %s" %
                      (six.get_method_function(func).__name__, plugin_key))
            # resolve slot_name to conduit
            # FIXME: handle cases where we don't have a conduit for a slot_name
            #   (should be able to handle this since we map those at the same time)
            conduit = self._slot_to_conduit[slot_name]

            try:
                # create a Conduit
                # FIXME: handle cases where we can't create a Conduit()
                conduit_instance = conduit(
                    six.get_method_self(func).__class__, **kwargs)
            # TypeError tends to mean we provided the wrong kwargs for this
            # conduit
            # if we get an Exception above, should we exit early, or
            # continue onto other hooks. A conduit could fail for
            # something specific to func.__class__, but unlikely
            except Exception as e:
                log.exception(e)
                raise

            runner = PluginHookRunner(conduit_instance, func)
            yield runner
示例#25
0
    def key_names(self):
        # If the current page has a last_evaluated_key, use it to determine key attributes
        if self._last_evaluated_key:
            return self._last_evaluated_key.keys()

        # Use the table meta data to determine the key attributes
        table_meta = six.get_method_self(self._operation).get_meta_table()  # type: ignore  # method_self cannot be None
        return table_meta.get_key_names(self._kwargs.get('index_name'))
示例#26
0
 def clear_api_path_map_cache(self):
     """Clear out cache for api_path_map."""
     self._api_path_cache = None
     for api_provider in self.api_providers:
         if six.get_method_self(
             api_provider.clear_api_path_map_cache,
         ) is not None:
             api_provider.clear_api_path_map_cache()
示例#27
0
    def key_names(self):
        # If the current page has a last_evaluated_key, use it to determine key attributes
        if self._last_evaluated_key:
            return self._last_evaluated_key.keys()

        # Use the table meta data to determine the key attributes
        table_meta = six.get_method_self(self._operation).get_meta_table()
        return table_meta.get_key_names(self._kwargs.get('index_name'))
示例#28
0
def _is_bound_method(method):
    """
    bound_method 's self is a obj
    unbound_method 's self is None
    """
    if isinstance(method, types.MethodType) and six.get_method_self(method):
        return True
    return False
示例#29
0
def get_method_self(method):
    """Gets the ``self`` object attached to this method (or none)."""
    if not inspect.ismethod(method):
        return None
    try:
        return six.get_method_self(method)
    except AttributeError:
        return None
示例#30
0
def get_method_self(method):
    """Gets the ``self`` object attached to this method (or none)."""
    if not inspect.ismethod(method):
        return None
    try:
        return six.get_method_self(method)
    except AttributeError:
        return None
示例#31
0
    def runiter(self, slot_name, **kwargs):
        """Return an iterable of PluginHookRunner objects.

        The iterable will return a PluginHookRunner object
        for each plugin hook mapped to slot_name. Multiple plugins
        with hooks for the same slot will result in multiple
        PluginHookRunners in the iterable.

        See run() docs for what to expect from PluginHookRunner.run().
        """
        # slot's called should always exist here, if not
        if slot_name not in self._slot_to_funcs:
            raise SlotNameException(slot_name)

        for func in self._slot_to_funcs[slot_name]:
            module = inspect.getmodule(func)
            func_module_name = getattr(func, '__module__')
            if not func_module_name:
                if module:
                    func_module_name = module.__name__
                else:
                    func_module_name = 'unknown_module'
            func_class_name = six.get_method_self(func).__class__.__name__
            plugin_key = ".".join([func_module_name, func_class_name])
            log.debug("Running %s in %s" % (six.get_method_function(func).__name__, plugin_key))
            # resolve slot_name to conduit
            # FIXME: handle cases where we don't have a conduit for a slot_name
            #   (should be able to handle this since we map those at the same time)
            conduit = self._slot_to_conduit[slot_name]

            try:
                # create a Conduit
                # FIXME: handle cases where we can't create a Conduit()
                conduit_instance = conduit(six.get_method_self(func).__class__, **kwargs)
            # TypeError tends to mean we provided the wrong kwargs for this
            # conduit
            # if we get an Exception above, should we exit early, or
            # continue onto other hooks. A conduit could fail for
            # something specific to func.__class__, but unlikely
            except Exception as e:
                log.exception(e)
                raise

            runner = PluginHookRunner(conduit_instance, func)
            yield runner
示例#32
0
 def process_request(request):
     for method in self.methods['process_request']:
         response = yield method(request=request, spider=spider)
         assert response is None or isinstance(response, (Response, Request)), \
                 'Middleware %s.process_request must return None, Response or Request, got %s' % \
                 (six.get_method_self(method).__class__.__name__, response.__class__.__name__)  # 规定返回类型,None,response,request这三种
         if response:  # 如果是空,则会继续处理所有的中间件,但是一旦不是了,就会提前跑路,对,应该就是这个意思,可以很骚
             defer.returnValue(response)
     defer.returnValue((yield download_func(request=request,spider=spider)))
示例#33
0
 def process_request(request):
     for method in self.methods['process_request']:
         response = method(request=request, spider=spider)
         assert response is None or isinstance(response, (Response, Request)), \
                 'Middleware %s.process_request must return None, Response or Request, got %s' % \
                 (six.get_method_self(method).__class__.__name__, response.__class__.__name__)
         if response:
             return response
     return download_func(request=request, spider=spider)
示例#34
0
 def process_request(request):
     for method in self.methods['process_request']:
         response = yield method(request=request, spider=spider)
         assert response is None or isinstance(response, (Response, Request)), \
                 'Middleware %s.process_request must return None, Response or Request, got %s' % \
                 (six.get_method_self(method).__class__.__name__, response.__class__.__name__)
         if response:#重要:::若某个中间件返回的结果是response,就直接返回,步子啊进行后续的处理啦
             defer.returnValue(response)
     defer.returnValue((yield download_func(request=request,spider=spider)))
示例#35
0
文件: middleware.py 项目: 01-/scrapy
 def process_request(request):
     for method in self.methods['process_request']:
         response = yield method(request=request, spider=spider)
         assert response is None or isinstance(response, (Response, Request)), \
                 'Middleware %s.process_request must return None, Response or Request, got %s' % \
                 (six.get_method_self(method).__class__.__name__, response.__class__.__name__)
         if response:
             defer.returnValue(response)
     defer.returnValue((yield download_func(request=request,spider=spider)))
示例#36
0
文件: DFT.py 项目: ankitdobhal/thug
    def _init_pyhooks(self):
        hooks = log.PyHooks.get('DFT', None)
        if hooks is None:
            return

        for label, hook in hooks.items():
            name   = "{}_hook".format(label)
            _hook = six.get_method_function(hook) if six.get_method_self(hook) else hook
            method = six.create_bound_method(_hook, DFT)
            setattr(self, name, method)
示例#37
0
 def get_test_method_name(cls, test_method):
     test_method_self_t = type(six.get_method_self(test_method))
     # PY2, unbound methods hit this
     # PY3 you get attributeerror on __self__ one line above
     assert not isinstance(test_method_self_t, type(None))
     return '%s %s.%s' % (
         test_method_self_t.__module__,
         test_method_self_t.__name__,
         test_method.__name__,
     )
示例#38
0
def _find_method(obj, func):
    if obj:
        try:
            func_self = six.get_method_self(func)
        except AttributeError:  # func has no __self__
            pass
        else:
            if func_self is obj:
                return six.get_method_function(func).__name__
    raise ValueError("Function %s is not a method of: %s" % (func, obj))
def _find_method(obj, func):
    if obj:
        try:
            func_self = six.get_method_self(func)
        except AttributeError:  # func has no __self__
            pass
        else:
            if func_self is obj:
                return six.get_method_function(func).__name__
    raise ValueError("Function %s is not a method of: %s" % (func, obj))
示例#40
0
 def get_test_method_name(cls, test_method):
     test_method_self_t = type(six.get_method_self(test_method))
     # PY2, unbound methods hit this
     # PY3 you get attributeerror on __self__ one line above
     assert not isinstance(test_method_self_t, type(None))
     return '%s %s.%s' % (
         test_method_self_t.__module__,
         test_method_self_t.__name__,
         test_method.__name__,
     )
示例#41
0
 def process_exception(_failure):
     exception = _failure.value
     for method in self.methods['process_exception']:
         response = yield method(request=request, exception=exception, spider=spider)
         if response is not None and not isinstance(response, (Response, Request)):
             raise _InvalidOutput('Middleware %s.process_exception must return None, Response or Request, got %s' % \
                                  (six.get_method_self(method).__class__.__name__, type(response)))
         if response:
             defer.returnValue(response)
     defer.returnValue(_failure)
示例#42
0
 def process_exception(_failure):
     exception = _failure.value
     for method in self.methods['process_exception']:
         response = method(request=request, exception=exception, spider=spider)
         assert response is None or isinstance(response, (Response, Request)), \
             'Middleware %s.process_exception must return None, Response or Request, got %s' % \
             (six.get_method_self(method).__class__.__name__, type(response))
         if response:
             return response
     return _failure
示例#43
0
文件: http.py 项目: zi-ming/Drogon
def _find_method(obj, func):
    if obj:
        try:
            fun_self = six.get_method_self(func)
        except:  # func has no __self__
            pass
        else:
            if fun_self is obj:
                return six.get_method_function(func).__name__
    raise AttributeError('Function {} is not a method of {}'.format(func, obj))
示例#44
0
文件: publisher.py 项目: nokia/moler
    def _get_subscriber_key_and_value(subscriber):
        """
        Allow Subscribers to be garbage collected while still subscribed inside Publisher.

        Subscribing methods of objects is tricky::

            class TheObserver(object):
                def __init__(self):
                    self.received_data = []

                def on_new_data(self, data):
                    self.received_data.append(data)

            observer1 = TheObserver()
            observer2 = TheObserver()

            subscribe(observer1.on_new_data)
            subscribe(observer2.on_new_data)
            subscribe(observer2.on_new_data)

        Even if it looks like 2 different subscriptions they all
        pass 3 different bound-method objects (different id()).
        This is so since access via observer1.on_new_data
        creates new object (bound method) on the fly.

        We want to use weakref but weakref to bound method doesn't work
        see: http://code.activestate.com/recipes/81253/
        and : https://stackoverflow.com/questions/599430/why-doesnt-the-weakref-work-on-this-bound-method
        When we wrap bound-method into weakref it may quickly disappear
        if that is only reference to bound method.
        So, we need to unbind it to have access to real method + self instance

        Unbinding above 3 examples of on_new_data will give:
        1) self                      - 2 different id()
        2) function object of class  - all 3 have same id()

        Observer key is pair: (self-id, function-id)
        """
        try:
            self_or_none = six.get_method_self(subscriber)
            self_id = instance_id(self_or_none)
            self_or_none = weakref.proxy(self_or_none)
        except AttributeError:
            self_id = 0  # default for not bound methods
            self_or_none = None

        try:
            func = six.get_method_function(subscriber)
        except AttributeError:
            func = subscriber
        function_id = instance_id(func)

        subscription_key = (self_id, function_id)
        subscription_value = (self_or_none, weakref.proxy(func))
        return subscription_key, subscription_value
示例#45
0
def handle_security(controller, im_self=None):
    """ Checks the security of a controller.  """
    if controller._pecan.get('secured', False):
        check_permissions = controller._pecan['check_permissions']

        if isinstance(check_permissions, six.string_types):
            check_permissions = getattr(
                im_self or six.get_method_self(controller), check_permissions)

        if not check_permissions():
            raise exc.HTTPUnauthorized
示例#46
0
def pytest_runtest_teardown(item, nextitem):
    """Pytest hook to dispatch destructive scenarios."""
    do_revert = True

    # Prevent reverting after skipped tests
    if getattr(item, SKIPPED, False):
        do_revert = False

    # Revert only destructive tests
    if not item.get_marker(DESTRUCTIVE):
        do_revert = False

    snapshot_name = item.session.config.option.snapshot_name

    # Prevent reverting if no snapshot_name passed
    if snapshot_name is None:
        do_revert = False

    if do_revert:
        destructor = item._request.getfixturevalue('os_faults_client')
        # reject finalizers of all fixture scopes
        for finalizers in item.session._setupstate._finalizers.values():
            for finalizer in finalizers:

                # There are finalizers in the form of lambda function without
                # name. That looks as internal pytest specifics. We should skip
                # them.
                try:
                    fixture_def = six.get_method_self(finalizer)
                except AttributeError:
                    continue
                if not hasattr(fixture_def.func, INDESTRUCTIBLE):
                    LOG.debug('Clear {} finalizers'.format(fixture_def))
                    fixture_def._finalizer[:] = []

                    # Clear fixture cached result to force fixture with any
                    # scope to restart in next test.
                    if hasattr(fixture_def, "cached_result"):
                        LOG.debug('Clear {} cache'.format(fixture_def))
                        del fixture_def.cached_result

    outcome = yield

    # Prevent reverting after last test
    if nextitem is None or item.session.shouldstop:
        do_revert = False

    # Prevent reverting after KeyboardInterrupt
    if outcome.excinfo is not None and outcome.excinfo[0] is KeyboardInterrupt:
        do_revert = False

    if do_revert and destructor:
        revert_environment(destructor, snapshot_name)
        time.sleep(item.session.config.option.revert_timeout * 60)
示例#47
0
def _reject_self_from_args(func, args):
    if len(args) == 0:
        return args
    try:
        self = six.get_method_self(func)
    except Exception:
        return args
    if args[0] == self:
        return args[1:]
    else:
        return args
示例#48
0
 def process_exception(_failure):
     exception = _failure.value
     for method in self.methods['process_exception']:
         response = yield method(request=request, exception=exception,
                                 spider=spider)
         assert response is None or isinstance(response, (Response, Request)), \
             'Middleware %s.process_exception must return None, Response or Request, got %s' % \
             (six.get_method_self(method).__class__.__name__, type(response))
         if response:
             defer.returnValue(response)
     defer.returnValue(_failure)
示例#49
0
 def process_exception(_failure):
     exception = _failure.value
     for method in self.methods["process_exception"]:
         response = yield method(request=request, exception=exception, spider=spider)
         assert response is None or isinstance(response, (Response, Request)), (
             "Middleware %s.process_exception must return None, Response or Request, got %s"
             % (six.get_method_self(method).__class__.__name__, type(response))
         )
         if response:
             defer.returnValue(response)
     defer.returnValue(_failure)
示例#50
0
 def process_request(request):
     for method in self.methods['process_request']:
         response = yield method(request=request, spider=spider)
         if response is not None and not isinstance(
                 response, (Response, Request)):
             raise _InvalidOutput('Middleware %s.process_request must return None, Response or Request, got %s' % \
                                  (six.get_method_self(method).__class__.__name__, response.__class__.__name__))
         if response:
             defer.returnValue(response)
     defer.returnValue((yield download_func(request=request,
                                            spider=spider)))
    def upgrade_websocket(self, environ, start_response):
        """
        Attempt to upgrade the socket environ['wsgi.input'] into a websocket enabled connection.
        """
        websocket_version = environ.get('HTTP_SEC_WEBSOCKET_VERSION', '')
        if not websocket_version:
            raise UpgradeRequiredError
        elif websocket_version not in self.WS_VERSIONS:
            raise HandshakeError(
                'Unsupported WebSocket Version: {0}'.format(websocket_version))

        key = environ.get('HTTP_SEC_WEBSOCKET_KEY', '').strip()
        if not key:
            raise HandshakeError('Sec-WebSocket-Key header is missing/empty')
        try:
            key_len = len(base64.b64decode(key))
        except TypeError:
            raise HandshakeError('Invalid key: {0}'.format(key))
        if key_len != 16:
            # 5.2.1 (3)
            raise HandshakeError('Invalid key: {0}'.format(key))

        sec_ws_accept = base64.b64encode(
            sha1(six.b(key) + self.WS_GUID).digest())
        if six.PY3:
            sec_ws_accept = sec_ws_accept.decode('ascii')
        headers = [('Upgrade', 'websocket'), ('Connection', 'Upgrade'),
                   ('Sec-WebSocket-Accept', sec_ws_accept),
                   ('Sec-WebSocket-Version', str(websocket_version))]
        if environ.get('HTTP_SEC_WEBSOCKET_PROTOCOL') is not None:
            headers.append(('Sec-WebSocket-Protocol',
                            environ.get('HTTP_SEC_WEBSOCKET_PROTOCOL')))

        logger.debug('WebSocket request accepted, switching protocols')
        start_response(force_str('101 Switching Protocols'), headers)
        six.get_method_self(start_response).finish_content()
        if (django.VERSION[:3] >= (2, 1, 5)):
            wsgi_input = environ['wsgi.input'].stream
        else:
            wsgi_input = environ['wsgi.input']
        return WebSocket(wsgi_input)
示例#52
0
 def to_dict(self):
     test_method_self_t = type(six.get_method_self(self.test_method))
     assert not isinstance(test_method_self_t, type(None))
     return {
         'previous_run':
         self.previous_run,
         'start_time':
         time.mktime(self.start_time.timetuple())
         if self.start_time else None,
         'end_time':
         time.mktime(self.end_time.timetuple()) if self.end_time else None,
         'run_time': (self.run_time.seconds +
                      float(self.run_time.microseconds) / 1000000)
         if self.run_time is not None else None,
         'normalized_run_time':
         None if not self.run_time else "%.2fs" %
         (self.run_time.seconds + (self.run_time.microseconds / 1000000.0)),
         'complete':
         self.complete,
         'success':
         self.success,
         'failure':
         self.failure,
         'error':
         self.error,
         'interrupted':
         self.interrupted,
         'exception_info':
         self.format_exception_info(),
         'exception_info_pretty':
         self.format_exception_info(pretty=True),
         'exception_only':
         self.format_exception_only(),
         'runner_id':
         self.runner_id,
         'method': {
             'module':
             test_method_self_t.__module__,
             'class':
             test_method_self_t.__name__,
             'name':
             self.test_method.__name__,
             'full_name':
             '%s %s.%s' % (
                 test_method_self_t.__module__,
                 test_method_self_t.__name__,
                 self.test_method.__name__,
             ),
             'fixture_type':
             None if not inspection.is_fixture_method(self.test_method) else
             self.test_method._fixture_type,
         }
     }
示例#53
0
def is_same_callback(callback1, callback2, strict=True):
    """Returns if the two callbacks are the same."""
    if callback1 is callback2:
        # This happens when plain methods are given (or static/non-bound
        # methods).
        return True
    if callback1 == callback2:
        if not strict:
            return True
        # Two bound methods are equal if functions themselves are equal and
        # objects they are applied to are equal. This means that a bound
        # method could be the same bound method on another object if the
        # objects have __eq__ methods that return true (when in fact it is a
        # different bound method). Python u so crazy!
        try:
            self1 = six.get_method_self(callback1)
            self2 = six.get_method_self(callback2)
            return self1 is self2
        except AttributeError:
            pass
    return False
示例#54
0
def _attach_method(method, callbacks):
    def make_wrapper(func):
        @six.wraps(func)
        def wrapper(*args, **kwargs):
            value = func(*args, **kwargs)
            for callback in callbacks:
                callback(value)
            return value
        return wrapper

    setattr(six.get_method_self(method), method.__func__.__name__,
            make_wrapper(method))
示例#55
0
def handle_security(controller, im_self=None):
    """ Checks the security of a controller.  """
    if controller._pecan.get('secured', False):
        check_permissions = controller._pecan['check_permissions']

        if isinstance(check_permissions, six.string_types):
            check_permissions = getattr(
                im_self or six.get_method_self(controller),
                check_permissions
            )

        if not check_permissions():
            raise exc.HTTPUnauthorized
示例#56
0
def get_func_name(func):
  """Returns name of passed callable."""
  _, func = tf_decorator.unwrap(func)
  if callable(func):
    if tf_inspect.isfunction(func):
      return func.__name__
    elif tf_inspect.ismethod(func):
      return '%s.%s' % (six.get_method_self(func).__class__.__name__,
                        six.get_method_function(func).__name__)
    else:  # Probably a class instance with __call__
      return str(type(func))
  else:
    raise ValueError('Argument must be callable')
示例#57
0
        def process_response(response):
            assert response is not None, 'Received None in process_response'
            if isinstance(response, Request):
                defer.returnValue(response)

            for method in self.methods['process_response']:
                response = yield method(request=request, response=response, spider=spider)
                if not isinstance(response, (Response, Request)):
                    raise _InvalidOutput('Middleware %s.process_response must return Response or Request, got %s' % \
                                         (six.get_method_self(method).__class__.__name__, type(response)))
                if isinstance(response, Request):
                    defer.returnValue(response)
            defer.returnValue(response)
示例#58
0
        def process_response(response):
            assert response is not None, 'Received None in process_response'
            if isinstance(response, Request):
                return response

            for method in self.methods['process_response']:
                response = method(request=request, response=response, spider=spider)
                assert isinstance(response, (Response, Request)), \
                    'Middleware %s.process_response must return Response or Request, got %s' % \
                    (six.get_method_self(method).__class__.__name__, type(response))
                if isinstance(response, Request):
                    return response
            return response
示例#59
0
        def process_response(response):
            assert response is not None, "Received None in process_response"
            if isinstance(response, Request):
                defer.returnValue(response)

            for method in self.methods["process_response"]:
                response = yield method(request=request, response=response, spider=spider)
                assert isinstance(response, (Response, Request)), (
                    "Middleware %s.process_response must return Response or Request, got %s"
                    % (six.get_method_self(method).__class__.__name__, type(response))
                )
                if isinstance(response, Request):
                    defer.returnValue(response)
            defer.returnValue(response)
示例#60
0
文件: probe.py 项目: sprousewd/desmod
def _attach_component_method(method, callbacks):
    component = six.get_method_self(method)
    _detach_methods(component, [method.__func__.__name__])

    if callbacks:
        def make_wrapper(func):
            @six.wraps(func)
            def wrapper(*args, **kwargs):
                value = func(*args, **kwargs)
                for callback in callbacks:
                    callback(value)
                return value
            return wrapper

        setattr(component, method.__func__.__name__, make_wrapper(method))