|
rdobuilder |
db622b |
From 522eb96525eebcc0fde047603f00c8df15fd98ef Mon Sep 17 00:00:00 2001
|
|
rdobuilder |
4c636a |
From: mprahl <mprahl@redhat.com>
|
|
rdobuilder |
4c636a |
Date: Thu, 16 Jun 2022 22:26:53 +0200
|
|
rdobuilder |
4c636a |
Subject: [PATCH] Remove the bundled decorator package
|
|
rdobuilder |
4c636a |
MIME-Version: 1.0
|
|
rdobuilder |
4c636a |
Content-Type: text/plain; charset=UTF-8
|
|
rdobuilder |
4c636a |
Content-Transfer-Encoding: 8bit
|
|
rdobuilder |
4c636a |
|
|
rdobuilder |
4c636a |
Co-Authored-By: Miro HronĨok <miro@hroncok.cz>
|
|
rdobuilder |
4c636a |
---
|
|
rdobuilder |
4c636a |
NOTICE | 3 -
|
|
rdobuilder |
4c636a |
prometheus_client/context_managers.py | 2 +-
|
|
rdobuilder |
4c636a |
prometheus_client/decorator.py | 427 --------------------------
|
|
rdobuilder |
4c636a |
setup.py | 1 +
|
|
rdobuilder |
4c636a |
tests/test_core.py | 12 +-
|
|
rdobuilder |
4c636a |
5 files changed, 13 insertions(+), 432 deletions(-)
|
|
rdobuilder |
4c636a |
delete mode 100644 prometheus_client/decorator.py
|
|
rdobuilder |
4c636a |
|
|
rdobuilder |
4c636a |
diff --git a/NOTICE b/NOTICE
|
|
rdobuilder |
4c636a |
index 59efb6c..0675ae1 100644
|
|
rdobuilder |
4c636a |
--- a/NOTICE
|
|
rdobuilder |
4c636a |
+++ b/NOTICE
|
|
rdobuilder |
4c636a |
@@ -1,5 +1,2 @@
|
|
rdobuilder |
4c636a |
Prometheus instrumentation library for Python applications
|
|
rdobuilder |
4c636a |
Copyright 2015 The Prometheus Authors
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-This product bundles decorator 4.0.10 which is available under a "2-clause BSD"
|
|
rdobuilder |
4c636a |
-license. For details, see prometheus_client/decorator.py.
|
|
rdobuilder |
4c636a |
diff --git a/prometheus_client/context_managers.py b/prometheus_client/context_managers.py
|
|
rdobuilder |
db622b |
index 3988ec2..4ff071b 100644
|
|
rdobuilder |
4c636a |
--- a/prometheus_client/context_managers.py
|
|
rdobuilder |
4c636a |
+++ b/prometheus_client/context_managers.py
|
|
rdobuilder |
db622b |
@@ -5,7 +5,7 @@ from typing import (
|
|
rdobuilder |
db622b |
Union,
|
|
rdobuilder |
db622b |
)
|
|
rdobuilder |
4c636a |
|
|
rdobuilder |
4c636a |
-from .decorator import decorate
|
|
rdobuilder |
4c636a |
+from decorator import decorate
|
|
rdobuilder |
4c636a |
|
|
rdobuilder |
4c636a |
if TYPE_CHECKING:
|
|
rdobuilder |
4c636a |
from . import Counter
|
|
rdobuilder |
4c636a |
diff --git a/prometheus_client/decorator.py b/prometheus_client/decorator.py
|
|
rdobuilder |
4c636a |
deleted file mode 100644
|
|
rdobuilder |
4c636a |
index 1ad2c97..0000000
|
|
rdobuilder |
4c636a |
--- a/prometheus_client/decorator.py
|
|
rdobuilder |
4c636a |
+++ /dev/null
|
|
rdobuilder |
4c636a |
@@ -1,427 +0,0 @@
|
|
rdobuilder |
4c636a |
-# ######################### LICENSE ############################ #
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-# Copyright (c) 2005-2016, Michele Simionato
|
|
rdobuilder |
4c636a |
-# All rights reserved.
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-# Redistribution and use in source and binary forms, with or without
|
|
rdobuilder |
4c636a |
-# modification, are permitted provided that the following conditions are
|
|
rdobuilder |
4c636a |
-# met:
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-# Redistributions of source code must retain the above copyright
|
|
rdobuilder |
4c636a |
-# notice, this list of conditions and the following disclaimer.
|
|
rdobuilder |
4c636a |
-# Redistributions in bytecode form must reproduce the above copyright
|
|
rdobuilder |
4c636a |
-# notice, this list of conditions and the following disclaimer in
|
|
rdobuilder |
4c636a |
-# the documentation and/or other materials provided with the
|
|
rdobuilder |
4c636a |
-# distribution.
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
rdobuilder |
4c636a |
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
rdobuilder |
4c636a |
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
rdobuilder |
4c636a |
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
rdobuilder |
4c636a |
-# HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
rdobuilder |
4c636a |
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
rdobuilder |
4c636a |
-# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
rdobuilder |
4c636a |
-# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
rdobuilder |
4c636a |
-# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
|
rdobuilder |
4c636a |
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
|
rdobuilder |
4c636a |
-# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
rdobuilder |
4c636a |
-# DAMAGE.
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-"""
|
|
rdobuilder |
4c636a |
-Decorator module, see http://pypi.python.org/pypi/decorator
|
|
rdobuilder |
4c636a |
-for the documentation.
|
|
rdobuilder |
4c636a |
-"""
|
|
rdobuilder |
4c636a |
-from __future__ import print_function
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-import collections
|
|
rdobuilder |
4c636a |
-import inspect
|
|
rdobuilder |
4c636a |
-import itertools
|
|
rdobuilder |
4c636a |
-import operator
|
|
rdobuilder |
4c636a |
-import re
|
|
rdobuilder |
4c636a |
-import sys
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-__version__ = '4.0.10'
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-if sys.version_info >= (3,):
|
|
rdobuilder |
4c636a |
- from inspect import getfullargspec
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def get_init(cls):
|
|
rdobuilder |
4c636a |
- return cls.__init__
|
|
rdobuilder |
4c636a |
-else:
|
|
rdobuilder |
4c636a |
- class getfullargspec(object):
|
|
rdobuilder |
4c636a |
- "A quick and dirty replacement for getfullargspec for Python 2.X"
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def __init__(self, f):
|
|
rdobuilder |
4c636a |
- self.args, self.varargs, self.varkw, self.defaults = \
|
|
rdobuilder |
4c636a |
- inspect.getargspec(f)
|
|
rdobuilder |
4c636a |
- self.kwonlyargs = []
|
|
rdobuilder |
4c636a |
- self.kwonlydefaults = None
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def __iter__(self):
|
|
rdobuilder |
4c636a |
- yield self.args
|
|
rdobuilder |
4c636a |
- yield self.varargs
|
|
rdobuilder |
4c636a |
- yield self.varkw
|
|
rdobuilder |
4c636a |
- yield self.defaults
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- getargspec = inspect.getargspec
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def get_init(cls):
|
|
rdobuilder |
4c636a |
- return cls.__init__.__func__
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-# getargspec has been deprecated in Python 3.5
|
|
rdobuilder |
4c636a |
-ArgSpec = collections.namedtuple(
|
|
rdobuilder |
4c636a |
- 'ArgSpec', 'args varargs varkw defaults')
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-def getargspec(f):
|
|
rdobuilder |
4c636a |
- """A replacement for inspect.getargspec"""
|
|
rdobuilder |
4c636a |
- spec = getfullargspec(f)
|
|
rdobuilder |
4c636a |
- return ArgSpec(spec.args, spec.varargs, spec.varkw, spec.defaults)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-DEF = re.compile(r'\s*def\s*([_\w][_\w\d]*)\s*\(')
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-# basic functionality
|
|
rdobuilder |
4c636a |
-class FunctionMaker(object):
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- An object with the ability to create functions with a given signature.
|
|
rdobuilder |
4c636a |
- It has attributes name, doc, module, signature, defaults, dict and
|
|
rdobuilder |
4c636a |
- methods update and make.
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- # Atomic get-and-increment provided by the GIL
|
|
rdobuilder |
4c636a |
- _compile_count = itertools.count()
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def __init__(self, func=None, name=None, signature=None,
|
|
rdobuilder |
4c636a |
- defaults=None, doc=None, module=None, funcdict=None):
|
|
rdobuilder |
4c636a |
- self.shortsignature = signature
|
|
rdobuilder |
4c636a |
- if func:
|
|
rdobuilder |
4c636a |
- # func can be a class or a callable, but not an instance method
|
|
rdobuilder |
4c636a |
- self.name = func.__name__
|
|
rdobuilder |
4c636a |
- if self.name == '<lambda>': # small hack for lambda functions
|
|
rdobuilder |
4c636a |
- self.name = '_lambda_'
|
|
rdobuilder |
4c636a |
- self.doc = func.__doc__
|
|
rdobuilder |
4c636a |
- self.module = func.__module__
|
|
rdobuilder |
4c636a |
- if inspect.isfunction(func):
|
|
rdobuilder |
4c636a |
- argspec = getfullargspec(func)
|
|
rdobuilder |
4c636a |
- self.annotations = getattr(func, '__annotations__', {})
|
|
rdobuilder |
4c636a |
- for a in ('args', 'varargs', 'varkw', 'defaults', 'kwonlyargs',
|
|
rdobuilder |
4c636a |
- 'kwonlydefaults'):
|
|
rdobuilder |
4c636a |
- setattr(self, a, getattr(argspec, a))
|
|
rdobuilder |
4c636a |
- for i, arg in enumerate(self.args):
|
|
rdobuilder |
4c636a |
- setattr(self, 'arg%d' % i, arg)
|
|
rdobuilder |
4c636a |
- if sys.version_info < (3,): # easy way
|
|
rdobuilder |
4c636a |
- self.shortsignature = self.signature = (
|
|
rdobuilder |
4c636a |
- inspect.formatargspec(
|
|
rdobuilder |
4c636a |
- formatvalue=lambda val: "", *argspec)[1:-1])
|
|
rdobuilder |
4c636a |
- else: # Python 3 way
|
|
rdobuilder |
4c636a |
- allargs = list(self.args)
|
|
rdobuilder |
4c636a |
- allshortargs = list(self.args)
|
|
rdobuilder |
4c636a |
- if self.varargs:
|
|
rdobuilder |
4c636a |
- allargs.append('*' + self.varargs)
|
|
rdobuilder |
4c636a |
- allshortargs.append('*' + self.varargs)
|
|
rdobuilder |
4c636a |
- elif self.kwonlyargs:
|
|
rdobuilder |
4c636a |
- allargs.append('*') # single star syntax
|
|
rdobuilder |
4c636a |
- for a in self.kwonlyargs:
|
|
rdobuilder |
4c636a |
- allargs.append('%s=None' % a)
|
|
rdobuilder |
4c636a |
- allshortargs.append('%s=%s' % (a, a))
|
|
rdobuilder |
4c636a |
- if self.varkw:
|
|
rdobuilder |
4c636a |
- allargs.append('**' + self.varkw)
|
|
rdobuilder |
4c636a |
- allshortargs.append('**' + self.varkw)
|
|
rdobuilder |
4c636a |
- self.signature = ', '.join(allargs)
|
|
rdobuilder |
4c636a |
- self.shortsignature = ', '.join(allshortargs)
|
|
rdobuilder |
4c636a |
- self.dict = func.__dict__.copy()
|
|
rdobuilder |
4c636a |
- # func=None happens when decorating a caller
|
|
rdobuilder |
4c636a |
- if name:
|
|
rdobuilder |
4c636a |
- self.name = name
|
|
rdobuilder |
4c636a |
- if signature is not None:
|
|
rdobuilder |
4c636a |
- self.signature = signature
|
|
rdobuilder |
4c636a |
- if defaults:
|
|
rdobuilder |
4c636a |
- self.defaults = defaults
|
|
rdobuilder |
4c636a |
- if doc:
|
|
rdobuilder |
4c636a |
- self.doc = doc
|
|
rdobuilder |
4c636a |
- if module:
|
|
rdobuilder |
4c636a |
- self.module = module
|
|
rdobuilder |
4c636a |
- if funcdict:
|
|
rdobuilder |
4c636a |
- self.dict = funcdict
|
|
rdobuilder |
4c636a |
- # check existence required attributes
|
|
rdobuilder |
4c636a |
- assert hasattr(self, 'name')
|
|
rdobuilder |
4c636a |
- if not hasattr(self, 'signature'):
|
|
rdobuilder |
4c636a |
- raise TypeError('You are decorating a non function: %s' % func)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def update(self, func, **kw):
|
|
rdobuilder |
4c636a |
- "Update the signature of func with the data in self"
|
|
rdobuilder |
4c636a |
- func.__name__ = self.name
|
|
rdobuilder |
4c636a |
- func.__doc__ = getattr(self, 'doc', None)
|
|
rdobuilder |
4c636a |
- func.__dict__ = getattr(self, 'dict', {})
|
|
rdobuilder |
4c636a |
- func.__defaults__ = getattr(self, 'defaults', ())
|
|
rdobuilder |
4c636a |
- func.__kwdefaults__ = getattr(self, 'kwonlydefaults', None)
|
|
rdobuilder |
4c636a |
- func.__annotations__ = getattr(self, 'annotations', None)
|
|
rdobuilder |
4c636a |
- try:
|
|
rdobuilder |
4c636a |
- frame = sys._getframe(3)
|
|
rdobuilder |
4c636a |
- except AttributeError: # for IronPython and similar implementations
|
|
rdobuilder |
4c636a |
- callermodule = '?'
|
|
rdobuilder |
4c636a |
- else:
|
|
rdobuilder |
4c636a |
- callermodule = frame.f_globals.get('__name__', '?')
|
|
rdobuilder |
4c636a |
- func.__module__ = getattr(self, 'module', callermodule)
|
|
rdobuilder |
4c636a |
- func.__dict__.update(kw)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def make(self, src_templ, evaldict=None, addsource=False, **attrs):
|
|
rdobuilder |
4c636a |
- "Make a new function from a given template and update the signature"
|
|
rdobuilder |
4c636a |
- src = src_templ % vars(self) # expand name and signature
|
|
rdobuilder |
4c636a |
- evaldict = evaldict or {}
|
|
rdobuilder |
4c636a |
- mo = DEF.match(src)
|
|
rdobuilder |
4c636a |
- if mo is None:
|
|
rdobuilder |
4c636a |
- raise SyntaxError('not a valid function template\n%s' % src)
|
|
rdobuilder |
4c636a |
- name = mo.group(1) # extract the function name
|
|
rdobuilder |
4c636a |
- names = set([name] + [arg.strip(' *') for arg in
|
|
rdobuilder |
4c636a |
- self.shortsignature.split(',')])
|
|
rdobuilder |
4c636a |
- for n in names:
|
|
rdobuilder |
4c636a |
- if n in ('_func_', '_call_'):
|
|
rdobuilder |
4c636a |
- raise NameError('%s is overridden in\n%s' % (n, src))
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- if not src.endswith('\n'): # add a newline for old Pythons
|
|
rdobuilder |
4c636a |
- src += '\n'
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- # Ensure each generated function has a unique filename for profilers
|
|
rdobuilder |
4c636a |
- # (such as cProfile) that depend on the tuple of (<filename>,
|
|
rdobuilder |
4c636a |
- # <definition line>, <function name>) being unique.
|
|
rdobuilder |
4c636a |
- filename = '<decorator-gen-%d>' % (next(self._compile_count),)
|
|
rdobuilder |
4c636a |
- try:
|
|
rdobuilder |
4c636a |
- code = compile(src, filename, 'single')
|
|
rdobuilder |
4c636a |
- exec(code, evaldict)
|
|
rdobuilder |
4c636a |
- except:
|
|
rdobuilder |
4c636a |
- print('Error in generated code:', file=sys.stderr)
|
|
rdobuilder |
4c636a |
- print(src, file=sys.stderr)
|
|
rdobuilder |
4c636a |
- raise
|
|
rdobuilder |
4c636a |
- func = evaldict[name]
|
|
rdobuilder |
4c636a |
- if addsource:
|
|
rdobuilder |
4c636a |
- attrs['__source__'] = src
|
|
rdobuilder |
4c636a |
- self.update(func, **attrs)
|
|
rdobuilder |
4c636a |
- return func
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- @classmethod
|
|
rdobuilder |
4c636a |
- def create(cls, obj, body, evaldict, defaults=None,
|
|
rdobuilder |
4c636a |
- doc=None, module=None, addsource=True, **attrs):
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- Create a function from the strings name, signature and body.
|
|
rdobuilder |
4c636a |
- evaldict is the evaluation dictionary. If addsource is true an
|
|
rdobuilder |
4c636a |
- attribute __source__ is added to the result. The attributes attrs
|
|
rdobuilder |
4c636a |
- are added, if any.
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- if isinstance(obj, str): # "name(signature)"
|
|
rdobuilder |
4c636a |
- name, rest = obj.strip().split('(', 1)
|
|
rdobuilder |
4c636a |
- signature = rest[:-1] # strip a right parens
|
|
rdobuilder |
4c636a |
- func = None
|
|
rdobuilder |
4c636a |
- else: # a function
|
|
rdobuilder |
4c636a |
- name = None
|
|
rdobuilder |
4c636a |
- signature = None
|
|
rdobuilder |
4c636a |
- func = obj
|
|
rdobuilder |
4c636a |
- self = cls(func, name, signature, defaults, doc, module)
|
|
rdobuilder |
4c636a |
- ibody = '\n'.join(' ' + line for line in body.splitlines())
|
|
rdobuilder |
4c636a |
- return self.make('def %(name)s(%(signature)s):\n' + ibody,
|
|
rdobuilder |
4c636a |
- evaldict, addsource, **attrs)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-def decorate(func, caller):
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- decorate(func, caller) decorates a function using a caller.
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- evaldict = dict(_call_=caller, _func_=func)
|
|
rdobuilder |
4c636a |
- fun = FunctionMaker.create(
|
|
rdobuilder |
4c636a |
- func, "return _call_(_func_, %(shortsignature)s)",
|
|
rdobuilder |
4c636a |
- evaldict, __wrapped__=func)
|
|
rdobuilder |
4c636a |
- if hasattr(func, '__qualname__'):
|
|
rdobuilder |
4c636a |
- fun.__qualname__ = func.__qualname__
|
|
rdobuilder |
4c636a |
- return fun
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-def decorator(caller, _func=None):
|
|
rdobuilder |
4c636a |
- """decorator(caller) converts a caller function into a decorator"""
|
|
rdobuilder |
4c636a |
- if _func is not None: # return a decorated function
|
|
rdobuilder |
4c636a |
- # this is obsolete behavior; you should use decorate instead
|
|
rdobuilder |
4c636a |
- return decorate(_func, caller)
|
|
rdobuilder |
4c636a |
- # else return a decorator function
|
|
rdobuilder |
4c636a |
- if inspect.isclass(caller):
|
|
rdobuilder |
4c636a |
- name = caller.__name__.lower()
|
|
rdobuilder |
4c636a |
- doc = 'decorator(%s) converts functions/generators into ' \
|
|
rdobuilder |
4c636a |
- 'factories of %s objects' % (caller.__name__, caller.__name__)
|
|
rdobuilder |
4c636a |
- elif inspect.isfunction(caller):
|
|
rdobuilder |
4c636a |
- if caller.__name__ == '<lambda>':
|
|
rdobuilder |
4c636a |
- name = '_lambda_'
|
|
rdobuilder |
4c636a |
- else:
|
|
rdobuilder |
4c636a |
- name = caller.__name__
|
|
rdobuilder |
4c636a |
- doc = caller.__doc__
|
|
rdobuilder |
4c636a |
- else: # assume caller is an object with a __call__ method
|
|
rdobuilder |
4c636a |
- name = caller.__class__.__name__.lower()
|
|
rdobuilder |
4c636a |
- doc = caller.__call__.__doc__
|
|
rdobuilder |
4c636a |
- evaldict = dict(_call_=caller, _decorate_=decorate)
|
|
rdobuilder |
4c636a |
- return FunctionMaker.create(
|
|
rdobuilder |
4c636a |
- '%s(func)' % name, 'return _decorate_(func, _call_)',
|
|
rdobuilder |
4c636a |
- evaldict, doc=doc, module=caller.__module__,
|
|
rdobuilder |
4c636a |
- __wrapped__=caller)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-# ####################### contextmanager ####################### #
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-try: # Python >= 3.2
|
|
rdobuilder |
4c636a |
- from contextlib import _GeneratorContextManager
|
|
rdobuilder |
4c636a |
-except ImportError: # Python >= 2.5
|
|
rdobuilder |
4c636a |
- from contextlib import GeneratorContextManager as _GeneratorContextManager
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-class ContextManager(_GeneratorContextManager):
|
|
rdobuilder |
4c636a |
- def __call__(self, func):
|
|
rdobuilder |
4c636a |
- """Context manager decorator"""
|
|
rdobuilder |
4c636a |
- return FunctionMaker.create(
|
|
rdobuilder |
4c636a |
- func, "with _self_: return _func_(%(shortsignature)s)",
|
|
rdobuilder |
4c636a |
- dict(_self_=self, _func_=func), __wrapped__=func)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-init = getfullargspec(_GeneratorContextManager.__init__)
|
|
rdobuilder |
4c636a |
-n_args = len(init.args)
|
|
rdobuilder |
4c636a |
-if n_args == 2 and not init.varargs: # (self, genobj) Python 2.7
|
|
rdobuilder |
4c636a |
- def __init__(self, g, *a, **k):
|
|
rdobuilder |
4c636a |
- return _GeneratorContextManager.__init__(self, g(*a, **k))
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- ContextManager.__init__ = __init__
|
|
rdobuilder |
4c636a |
-elif n_args == 2 and init.varargs: # (self, gen, *a, **k) Python 3.4
|
|
rdobuilder |
4c636a |
- pass
|
|
rdobuilder |
4c636a |
-elif n_args == 4: # (self, gen, args, kwds) Python 3.5
|
|
rdobuilder |
4c636a |
- def __init__(self, g, *a, **k):
|
|
rdobuilder |
4c636a |
- return _GeneratorContextManager.__init__(self, g, a, k)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- ContextManager.__init__ = __init__
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-contextmanager = decorator(ContextManager)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-# ############################ dispatch_on ############################ #
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-def append(a, vancestors):
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- Append ``a`` to the list of the virtual ancestors, unless it is already
|
|
rdobuilder |
4c636a |
- included.
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- add = True
|
|
rdobuilder |
4c636a |
- for j, va in enumerate(vancestors):
|
|
rdobuilder |
4c636a |
- if issubclass(va, a):
|
|
rdobuilder |
4c636a |
- add = False
|
|
rdobuilder |
4c636a |
- break
|
|
rdobuilder |
4c636a |
- if issubclass(a, va):
|
|
rdobuilder |
4c636a |
- vancestors[j] = a
|
|
rdobuilder |
4c636a |
- add = False
|
|
rdobuilder |
4c636a |
- if add:
|
|
rdobuilder |
4c636a |
- vancestors.append(a)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
-# inspired from simplegeneric by P.J. Eby and functools.singledispatch
|
|
rdobuilder |
4c636a |
-def dispatch_on(*dispatch_args):
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- Factory of decorators turning a function into a generic function
|
|
rdobuilder |
4c636a |
- dispatching on the given arguments.
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- assert dispatch_args, 'No dispatch args passed'
|
|
rdobuilder |
4c636a |
- dispatch_str = '(%s,)' % ', '.join(dispatch_args)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def check(arguments, wrong=operator.ne, msg=''):
|
|
rdobuilder |
4c636a |
- """Make sure one passes the expected number of arguments"""
|
|
rdobuilder |
4c636a |
- if wrong(len(arguments), len(dispatch_args)):
|
|
rdobuilder |
4c636a |
- raise TypeError('Expected %d arguments, got %d%s' %
|
|
rdobuilder |
4c636a |
- (len(dispatch_args), len(arguments), msg))
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def gen_func_dec(func):
|
|
rdobuilder |
4c636a |
- """Decorator turning a function into a generic function"""
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- # first check the dispatch arguments
|
|
rdobuilder |
4c636a |
- argset = set(getfullargspec(func).args)
|
|
rdobuilder |
4c636a |
- if not set(dispatch_args) <= argset:
|
|
rdobuilder |
4c636a |
- raise NameError('Unknown dispatch arguments %s' % dispatch_str)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- typemap = {}
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def vancestors(*types):
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- Get a list of sets of virtual ancestors for the given types
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- check(types)
|
|
rdobuilder |
4c636a |
- ras = [[] for _ in range(len(dispatch_args))]
|
|
rdobuilder |
4c636a |
- for types_ in typemap:
|
|
rdobuilder |
4c636a |
- for t, type_, ra in zip(types, types_, ras):
|
|
rdobuilder |
4c636a |
- if issubclass(t, type_) and type_ not in t.__mro__:
|
|
rdobuilder |
4c636a |
- append(type_, ra)
|
|
rdobuilder |
4c636a |
- return [set(ra) for ra in ras]
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def ancestors(*types):
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- Get a list of virtual MROs, one for each type
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- check(types)
|
|
rdobuilder |
4c636a |
- lists = []
|
|
rdobuilder |
4c636a |
- for t, vas in zip(types, vancestors(*types)):
|
|
rdobuilder |
4c636a |
- n_vas = len(vas)
|
|
rdobuilder |
4c636a |
- if n_vas > 1:
|
|
rdobuilder |
4c636a |
- raise RuntimeError(
|
|
rdobuilder |
4c636a |
- 'Ambiguous dispatch for %s: %s' % (t, vas))
|
|
rdobuilder |
4c636a |
- elif n_vas == 1:
|
|
rdobuilder |
4c636a |
- va, = vas
|
|
rdobuilder |
4c636a |
- mro = type('t', (t, va), {}).__mro__[1:]
|
|
rdobuilder |
4c636a |
- else:
|
|
rdobuilder |
4c636a |
- mro = t.__mro__
|
|
rdobuilder |
4c636a |
- lists.append(mro[:-1]) # discard t and object
|
|
rdobuilder |
4c636a |
- return lists
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def register(*types):
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- Decorator to register an implementation for the given types
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- check(types)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def dec(f):
|
|
rdobuilder |
4c636a |
- check(getfullargspec(f).args, operator.lt, ' in ' + f.__name__)
|
|
rdobuilder |
4c636a |
- typemap[types] = f
|
|
rdobuilder |
4c636a |
- return f
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- return dec
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def dispatch_info(*types):
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- An utility to introspect the dispatch algorithm
|
|
rdobuilder |
4c636a |
- """
|
|
rdobuilder |
4c636a |
- check(types)
|
|
rdobuilder |
4c636a |
- lst = []
|
|
rdobuilder |
4c636a |
- for anc in itertools.product(*ancestors(*types)):
|
|
rdobuilder |
4c636a |
- lst.append(tuple(a.__name__ for a in anc))
|
|
rdobuilder |
4c636a |
- return lst
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- def _dispatch(dispatch_args, *args, **kw):
|
|
rdobuilder |
4c636a |
- types = tuple(type(arg) for arg in dispatch_args)
|
|
rdobuilder |
4c636a |
- try: # fast path
|
|
rdobuilder |
4c636a |
- f = typemap[types]
|
|
rdobuilder |
4c636a |
- except KeyError:
|
|
rdobuilder |
4c636a |
- pass
|
|
rdobuilder |
4c636a |
- else:
|
|
rdobuilder |
4c636a |
- return f(*args, **kw)
|
|
rdobuilder |
4c636a |
- combinations = itertools.product(*ancestors(*types))
|
|
rdobuilder |
4c636a |
- next(combinations) # the first one has been already tried
|
|
rdobuilder |
4c636a |
- for types_ in combinations:
|
|
rdobuilder |
4c636a |
- f = typemap.get(types_)
|
|
rdobuilder |
4c636a |
- if f is not None:
|
|
rdobuilder |
4c636a |
- return f(*args, **kw)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- # else call the default implementation
|
|
rdobuilder |
4c636a |
- return func(*args, **kw)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- return FunctionMaker.create(
|
|
rdobuilder |
4c636a |
- func, 'return _f_(%s, %%(shortsignature)s)' % dispatch_str,
|
|
rdobuilder |
4c636a |
- dict(_f_=_dispatch), register=register, default=func,
|
|
rdobuilder |
4c636a |
- typemap=typemap, vancestors=vancestors, ancestors=ancestors,
|
|
rdobuilder |
4c636a |
- dispatch_info=dispatch_info, __wrapped__=func)
|
|
rdobuilder |
4c636a |
-
|
|
rdobuilder |
4c636a |
- gen_func_dec.__name__ = 'dispatch_on' + dispatch_str
|
|
rdobuilder |
4c636a |
- return gen_func_dec
|
|
rdobuilder |
4c636a |
diff --git a/setup.py b/setup.py
|
|
rdobuilder |
db622b |
index 92f6f47..25c4843 100644
|
|
rdobuilder |
4c636a |
--- a/setup.py
|
|
rdobuilder |
4c636a |
+++ b/setup.py
|
|
rdobuilder |
4c636a |
@@ -26,6 +26,7 @@ setup(
|
|
rdobuilder |
4c636a |
package_data={
|
|
rdobuilder |
4c636a |
'prometheus_client': ['py.typed']
|
|
rdobuilder |
4c636a |
},
|
|
rdobuilder |
4c636a |
+ install_requires=['decorator'],
|
|
rdobuilder |
4c636a |
extras_require={
|
|
rdobuilder |
4c636a |
'twisted': ['twisted'],
|
|
rdobuilder |
4c636a |
},
|
|
rdobuilder |
4c636a |
diff --git a/tests/test_core.py b/tests/test_core.py
|
|
rdobuilder |
4c636a |
index 6f7c9d1..0555e08 100644
|
|
rdobuilder |
4c636a |
--- a/tests/test_core.py
|
|
rdobuilder |
4c636a |
+++ b/tests/test_core.py
|
|
rdobuilder |
4c636a |
@@ -1,4 +1,6 @@
|
|
rdobuilder |
4c636a |
from concurrent.futures import ThreadPoolExecutor
|
|
rdobuilder |
4c636a |
+import collections
|
|
rdobuilder |
4c636a |
+from inspect import getfullargspec
|
|
rdobuilder |
4c636a |
import os
|
|
rdobuilder |
4c636a |
import time
|
|
rdobuilder |
4c636a |
import unittest
|
|
rdobuilder |
4c636a |
@@ -12,10 +14,18 @@ from prometheus_client.core import (
|
|
rdobuilder |
4c636a |
HistogramMetricFamily, Info, InfoMetricFamily, Metric, Sample,
|
|
rdobuilder |
4c636a |
StateSetMetricFamily, Summary, SummaryMetricFamily, UntypedMetricFamily,
|
|
rdobuilder |
4c636a |
)
|
|
rdobuilder |
4c636a |
-from prometheus_client.decorator import getargspec
|
|
rdobuilder |
4c636a |
from prometheus_client.metrics import _get_use_created
|
|
rdobuilder |
4c636a |
|
|
rdobuilder |
4c636a |
|
|
rdobuilder |
4c636a |
+ArgSpec = collections.namedtuple(
|
|
rdobuilder |
4c636a |
+ 'ArgSpec', 'args varargs varkw defaults')
|
|
rdobuilder |
4c636a |
+
|
|
rdobuilder |
4c636a |
+def getargspec(f):
|
|
rdobuilder |
4c636a |
+ """A replacement for inspect.getargspec"""
|
|
rdobuilder |
4c636a |
+ spec = getfullargspec(f)
|
|
rdobuilder |
4c636a |
+ return ArgSpec(spec.args, spec.varargs, spec.varkw, spec.defaults)
|
|
rdobuilder |
4c636a |
+
|
|
rdobuilder |
4c636a |
+
|
|
rdobuilder |
4c636a |
def assert_not_observable(fn, *args, **kwargs):
|
|
rdobuilder |
4c636a |
"""
|
|
rdobuilder |
4c636a |
Assert that a function call falls with a ValueError exception containing
|
|
rdobuilder |
4c636a |
--
|
|
rdobuilder |
db622b |
2.41.0
|
|
rdobuilder |
4c636a |
|