Пример #1
0
 def __get_dynamic_attr(self, attname, obj, default=None):
     """ Returns first defined occurence of the following:
             self.$attname(obj)
             self.$attname()
             self.$attname
             default
         Taken from django.contrib.syndication.views.Feed
     """
     try:
         attr = getattr(self, attname)
     except AttributeError:
         return default
     if callable(attr):
         # Check co_argcount rather than try/excepting the function and
         # catching the TypeError, because something inside the function
         # may raise the TypeError. This technique is more accurate.
         try:
             code = six.get_function_code(attr)
         except AttributeError:
             code = six.get_function_code(attr.__call__)
         if code.co_argcount == 2:       # one argument is 'self'
             return attr(obj)
         else:
             return attr()
     return attr
 def __get_dynamic_attr(self, attname, obj, default=None):
     try:
         attr = getattr(self, attname)
     except AttributeError:
         return default
     if callable(attr):
         try:
             code = six.get_function_code(attr)
         except AttributeError:
             code = six.get_function_code(attr.__call__)
         if code.co_argcount == 2:
             return attr(obj)
         else:
             return attr()
     return attr
 def __get_dynamic_attr(self, attname, obj, default=None):
     try:
         attr = getattr(self, attname)
     except AttributeError:
         return default
     if callable(attr):
         try:
             code = six.get_function_code(attr)
         except AttributeError:
             code = six.get_function_code(attr.__call__)
         if code.co_argcount == 2:
             return attr(obj)
         else:
             return attr()
     return attr
Пример #4
0
 def wrapped_target(*args, **kwargs):
     with silk_meta_profiler():
         try:
             func_code = six.get_function_code(target)
         except AttributeError:
             raise NotImplementedError(
                 'Profile not implemented to decorate type %s' %
                 target.__class__.__name__)
         line_num = func_code.co_firstlineno
         file_path = func_code.co_filename
         func_name = target.__name__
         if not self.name:
             self.name = func_name
         self.profile = {
             'func_name': func_name,
             'name': self.name,
             'file_path': file_path,
             'line_num': line_num,
             'dynamic': self._dynamic,
             'start_time': timezone.now(),
             'request': DataCollector().request
         }
         self._start_queries()
     try:
         result = target(*args, **kwargs)
     except Exception:
         self.profile['exception_raised'] = True
         raise
     finally:
         with silk_meta_profiler():
             self.profile['end_time'] = timezone.now()
             self._finalise_queries()
     return result
Пример #5
0
 def wrapped_target(*args, **kwargs):
     with silk_meta_profiler():
         try:
             func_code = six.get_function_code(target)
         except AttributeError:
             raise NotImplementedError('Profile not implemented to decorate type %s' % target.__class__.__name__)
         line_num = func_code.co_firstlineno
         file_path = func_code.co_filename
         func_name = target.__name__
         if not self.name:
             self.name = func_name
         self.profile = {
             'func_name': func_name,
             'name': self.name,
             'file_path': file_path,
             'line_num': line_num,
             'dynamic': self._dynamic,
             'start_time': timezone.now(),
             'request': DataCollector().request
         }
         self._start_queries()
     try:
         result = target(*args, **kwargs)
     except Exception:
         self.profile['exception_raised'] = True
         raise
     finally:
         with silk_meta_profiler():
             self.profile['end_time'] = timezone.now()
             self._finalise_queries()
     return result
Пример #6
0
    def _find_lineno(self, obj, source_lines):
        """
        Return a line number of the given object's docstring.  Note:
        this method assumes that the object has a docstring.
        """
        lineno = None

        # Find the line number for modules.
        if inspect.ismodule(obj):
            lineno = 0

        # Find the line number for classes.
        # Note: this could be fooled if a class is defined multiple
        # times in a single file.
        if inspect.isclass(obj):
            if source_lines is None:
                return None
            pat = re.compile(r'^\s*class\s*%s\b' %
                             getattr(obj, '__name__', '-'))
            for i, line in enumerate(source_lines):
                if pat.match(line):
                    lineno = i
                    break

        # Find the line number for functions & methods.
        if inspect.ismethod(obj): obj = obj.__func__
        if inspect.isfunction(obj): obj = six.get_function_code(obj)
        if inspect.istraceback(obj): obj = obj.tb_frame
        if inspect.isframe(obj): obj = obj.f_code
        if inspect.iscode(obj):
            lineno = getattr(obj, 'co_firstlineno', None)-1

        # Find the line number where the docstring starts.  Assume
        # that it's the first line that begins with a quote mark.
        # Note: this 
Пример #7
0
 def __get_dynamic_attr(self, attname, obj, default=None):
     try:
         attr = getattr(self, attname)
     except AttributeError:
         return default
     if callable(attr):
         # Check co_argcount rather than try/excepting the function and
         # catching the TypeError, because something inside the function
         # may raise the TypeError. This technique is more accurate.
         try:
             code = six.get_function_code(attr)
         except AttributeError:
             code = six.get_function_code(attr.__call__)
         if code.co_argcount == 2:       # one argument is 'self'
             return attr(obj)
         else:
             return attr()
     return attr
Пример #8
0
 def __get_dynamic_attr(self, attname, obj, default=None):
     try:
         attr = getattr(self, attname)
     except AttributeError:
         return default
     if callable(attr):
         # Check co_argcount rather than try/excepting the function and
         # catching the TypeError, because something inside the function
         # may raise the TypeError. This technique is more accurate.
         try:
             code = six.get_function_code(attr)
         except AttributeError:
             code = six.get_function_code(attr.__call__)
         if code.co_argcount == 2:  # one argument is 'self'
             return attr(obj)
         else:
             return attr()
     return attr
Пример #9
0
    def _resolve_methods(self):
        callback = self.pattern.callback

        try:
            closure = six.get_function_closure(callback)
            code = six.get_function_code(callback)

            while getattr(code, 'co_name') != 'view':
                # lets unwrap!
                view = getattr(closure[0], 'cell_contents')
                closure = six.get_function_closure(view)
                code = six.get_function_code(view)

            freevars = code.co_freevars
        except (AttributeError, IndexError):
            raise RuntimeError('Unable to use callback invalid closure/function specified.')
        else:
            return closure[freevars.index('actions')].cell_contents
Пример #10
0
def closure_n_code(func):
    return unwrappage(six.get_function_closure(func),
                      six.get_function_code(func))
Пример #11
0
def closure_n_code(func):
    return unwrappage(
        six.get_function_closure(func),
        six.get_function_code(func))