Linux websever 5.15.0-153-generic #163-Ubuntu SMP Thu Aug 7 16:37:18 UTC 2025 x86_64
Apache/2.4.52 (Ubuntu)
: 192.168.3.70 | : 192.168.1.99
Cant Read [ /etc/named.conf ]
8.1.2-1ubuntu2.23
urlab
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
CREATE WP USER
README
+ Create Folder
+ Create File
/
usr /
lib /
python3 /
dist-packages /
[ HOME SHELL ]
Name
Size
Permission
Action
Automat-20.2.0.egg-info
[ DIR ]
drwxr-xr-x
Babel-2.8.0.egg-info
[ DIR ]
drwxr-xr-x
CommandNotFound
[ DIR ]
drwxr-xr-x
ConfigArgParse-1.5.3.egg-info
[ DIR ]
drwxr-xr-x
DistUpgrade
[ DIR ]
drwxr-xr-x
HweSupportStatus
[ DIR ]
drwxr-xr-x
Jinja2-3.0.3.egg-info
[ DIR ]
drwxr-xr-x
Markdown-3.3.6.egg-info
[ DIR ]
drwxr-xr-x
MarkupSafe-2.0.1.egg-info
[ DIR ]
drwxr-xr-x
NvidiaDetector
[ DIR ]
drwxr-xr-x
OpenSSL
[ DIR ]
drwxr-xr-x
PyGObject-3.42.1.egg-info
[ DIR ]
drwxr-xr-x
PyHamcrest-2.0.2.egg-info
[ DIR ]
drwxr-xr-x
PyICU-2.8.1.egg-info
[ DIR ]
drwxr-xr-x
PyJWT-2.3.0.egg-info
[ DIR ]
drwxr-xr-x
PyYAML-5.4.1.egg-info
[ DIR ]
drwxr-xr-x
Pygments-2.11.2.egg-info
[ DIR ]
drwxr-xr-x
Quirks
[ DIR ]
drwxr-xr-x
SSSDConfig
[ DIR ]
drwxr-xr-x
SecretStorage-3.3.1.egg-info
[ DIR ]
drwxr-xr-x
Twisted-22.1.0.egg-info
[ DIR ]
drwxr-xr-x
UbuntuDrivers
[ DIR ]
drwxr-xr-x
UpdateManager
[ DIR ]
drwxr-xr-x
__pycache__
[ DIR ]
drwxr-xr-x
_distutils_hack
[ DIR ]
drwxr-xr-x
_yaml
[ DIR ]
drwxr-xr-x
acme
[ DIR ]
drwxr-xr-x
acme-1.21.0.egg-info
[ DIR ]
drwxr-xr-x
apport
[ DIR ]
drwxr-xr-x
apt
[ DIR ]
drwxr-xr-x
apt_inst-stubs
[ DIR ]
drwxr-xr-x
apt_pkg-stubs
[ DIR ]
drwxr-xr-x
aptsources
[ DIR ]
drwxr-xr-x
attr
[ DIR ]
drwxr-xr-x
attrs-21.2.0.egg-info
[ DIR ]
drwxr-xr-x
automat
[ DIR ]
drwxr-xr-x
babel
[ DIR ]
drwxr-xr-x
bcrypt
[ DIR ]
drwxr-xr-x
bcrypt-3.2.0.egg-info
[ DIR ]
drwxr-xr-x
blinker
[ DIR ]
drwxr-xr-x
certbot
[ DIR ]
drwxr-xr-x
certbot-1.21.0.egg-info
[ DIR ]
drwxr-xr-x
certbot_apache
[ DIR ]
drwxr-xr-x
certbot_apache-1.21.0.egg-info
[ DIR ]
drwxr-xr-x
certifi
[ DIR ]
drwxr-xr-x
certifi-2020.6.20.egg-info
[ DIR ]
drwxr-xr-x
chardet
[ DIR ]
drwxr-xr-x
chardet-4.0.0.egg-info
[ DIR ]
drwxr-xr-x
click
[ DIR ]
drwxr-xr-x
click-8.0.3.egg-info
[ DIR ]
drwxr-xr-x
cloud_init-25.1.4.egg-info
[ DIR ]
drwxr-xr-x
cloudinit
[ DIR ]
drwxr-xr-x
colorama
[ DIR ]
drwxr-xr-x
colorama-0.4.4.egg-info
[ DIR ]
drwxr-xr-x
configobj-5.0.6.egg-info
[ DIR ]
drwxr-xr-x
constantly
[ DIR ]
drwxr-xr-x
constantly-15.1.0.egg-info
[ DIR ]
drwxr-xr-x
cryptography
[ DIR ]
drwxr-xr-x
cryptography-3.4.8.egg-info
[ DIR ]
drwxr-xr-x
dbus
[ DIR ]
drwxr-xr-x
dbus_python-1.2.18.egg-info
[ DIR ]
drwxr-xr-x
debian
[ DIR ]
drwxr-xr-x
debian_bundle
[ DIR ]
drwxr-xr-x
decorator-4.4.2.egg-info
[ DIR ]
drwxr-xr-x
distro
[ DIR ]
drwxr-xr-x
distro-1.7.0.egg-info
[ DIR ]
drwxr-xr-x
distro_info-1.1build1.egg-info
[ DIR ]
drwxr-xr-x
dns
[ DIR ]
drwxr-xr-x
dnspython-2.1.0.egg-info
[ DIR ]
drwxr-xr-x
gi
[ DIR ]
drwxr-xr-x
gpg
[ DIR ]
drwxr-xr-x
gyp
[ DIR ]
drwxr-xr-x
gyp-0.1.egg-info
[ DIR ]
drwxr-xr-x
hamcrest
[ DIR ]
drwxr-xr-x
httplib2
[ DIR ]
drwxr-xr-x
httplib2-0.20.2.egg-info
[ DIR ]
drwxr-xr-x
hyperlink
[ DIR ]
drwxr-xr-x
hyperlink-21.0.0.egg-info
[ DIR ]
drwxr-xr-x
icu
[ DIR ]
drwxr-xr-x
idna
[ DIR ]
drwxr-xr-x
idna-3.3.egg-info
[ DIR ]
drwxr-xr-x
importlib_metadata
[ DIR ]
drwxr-xr-x
importlib_metadata-4.6.4.egg-i...
[ DIR ]
drwxr-xr-x
incremental
[ DIR ]
drwxr-xr-x
incremental-21.3.0.egg-info
[ DIR ]
drwxr-xr-x
janitor
[ DIR ]
drwxr-xr-x
jeepney
[ DIR ]
drwxr-xr-x
jeepney-0.7.1.dist-info
[ DIR ]
drwxr-xr-x
jinja2
[ DIR ]
drwxr-xr-x
josepy
[ DIR ]
drwxr-xr-x
josepy-1.10.0.egg-info
[ DIR ]
drwxr-xr-x
jsonpatch-1.32.egg-info
[ DIR ]
drwxr-xr-x
jsonpointer-2.0.egg-info
[ DIR ]
drwxr-xr-x
jsonschema
[ DIR ]
drwxr-xr-x
jsonschema-3.2.0.egg-info
[ DIR ]
drwxr-xr-x
jwt
[ DIR ]
drwxr-xr-x
keyring
[ DIR ]
drwxr-xr-x
keyring-23.5.0.egg-info
[ DIR ]
drwxr-xr-x
landscape
[ DIR ]
drwxr-xr-x
launchpadlib
[ DIR ]
drwxr-xr-x
launchpadlib-1.10.16.egg-info
[ DIR ]
drwxr-xr-x
lazr
[ DIR ]
drwxr-xr-x
lazr.restfulclient-0.14.4.egg-...
[ DIR ]
drwxr-xr-x
lazr.uri-1.0.6.egg-info
[ DIR ]
drwxr-xr-x
magic
[ DIR ]
drwxr-xr-x
markdown
[ DIR ]
drwxr-xr-x
markupsafe
[ DIR ]
drwxr-xr-x
more_itertools
[ DIR ]
drwxr-xr-x
more_itertools-8.10.0.egg-info
[ DIR ]
drwxr-xr-x
netifaces-0.11.0.egg-info
[ DIR ]
drwxr-xr-x
networkx
[ DIR ]
drwxr-xr-x
networkx-2.4.egg-info
[ DIR ]
drwxr-xr-x
numpy
[ DIR ]
drwxr-xr-x
numpy-1.21.5.egg-info
[ DIR ]
drwxr-xr-x
oauthlib
[ DIR ]
drwxr-xr-x
oauthlib-3.2.0.egg-info
[ DIR ]
drwxr-xr-x
parsedatetime
[ DIR ]
drwxr-xr-x
parsedatetime-2.6.egg-info
[ DIR ]
drwxr-xr-x
pexpect
[ DIR ]
drwxr-xr-x
pip
[ DIR ]
drwxr-xr-x
pip-22.0.2.dist-info
[ DIR ]
drwxr-xr-x
pkg_resources
[ DIR ]
drwxr-xr-x
ptyprocess
[ DIR ]
drwxr-xr-x
ptyprocess-0.7.0.dist-info
[ DIR ]
drwxr-xr-x
pyOpenSSL-21.0.0.egg-info
[ DIR ]
drwxr-xr-x
pyRFC3339-1.1.egg-info
[ DIR ]
drwxr-xr-x
pyasn1
[ DIR ]
drwxr-xr-x
pyasn1-0.4.8.egg-info
[ DIR ]
drwxr-xr-x
pyasn1_modules
[ DIR ]
drwxr-xr-x
pygments
[ DIR ]
drwxr-xr-x
pygtkcompat
[ DIR ]
drwxr-xr-x
pyparsing-2.4.7.egg-info
[ DIR ]
drwxr-xr-x
pyrfc3339
[ DIR ]
drwxr-xr-x
pyrsistent
[ DIR ]
drwxr-xr-x
pyrsistent-0.18.1.egg-info
[ DIR ]
drwxr-xr-x
pyserial-3.5.egg-info
[ DIR ]
drwxr-xr-x
python_apt-2.4.0+ubuntu4.1.egg...
[ DIR ]
drwxr-xr-x
python_debian-0.1.43ubuntu1.eg...
[ DIR ]
drwxr-xr-x
python_magic-0.4.24.egg-info
[ DIR ]
drwxr-xr-x
pytz
[ DIR ]
drwxr-xr-x
pytz-2022.1.egg-info
[ DIR ]
drwxr-xr-x
requests
[ DIR ]
drwxr-xr-x
requests-2.25.1.egg-info
[ DIR ]
drwxr-xr-x
requests_toolbelt
[ DIR ]
drwxr-xr-x
requests_toolbelt-0.9.1.egg-in...
[ DIR ]
drwxr-xr-x
samba
[ DIR ]
drwxr-xr-x
secretstorage
[ DIR ]
drwxr-xr-x
selinux
[ DIR ]
drwxr-xr-x
sepolgen
[ DIR ]
drwxr-xr-x
sepolicy
[ DIR ]
drwxr-xr-x
serial
[ DIR ]
drwxr-xr-x
service_identity
[ DIR ]
drwxr-xr-x
service_identity-18.1.0.egg-in...
[ DIR ]
drwxr-xr-x
setools
[ DIR ]
drwxr-xr-x
setools-4.4.0.egg-info
[ DIR ]
drwxr-xr-x
setuptools
[ DIR ]
drwxr-xr-x
setuptools-59.6.0.egg-info
[ DIR ]
drwxr-xr-x
six-1.16.0.egg-info
[ DIR ]
drwxr-xr-x
softwareproperties
[ DIR ]
drwxr-xr-x
sos
[ DIR ]
drwxr-xr-x
ssh_import_id
[ DIR ]
drwxr-xr-x
ssh_import_id-5.11.egg-info
[ DIR ]
drwxr-xr-x
sssd
[ DIR ]
drwxr-xr-x
systemd
[ DIR ]
drwxr-xr-x
twisted
[ DIR ]
drwxr-xr-x
uaclient
[ DIR ]
drwxr-xr-x
ubuntu_advantage_tools-8001.eg...
[ DIR ]
drwxr-xr-x
ubuntu_drivers_common-0.0.0.eg...
[ DIR ]
drwxr-xr-x
ufw
[ DIR ]
drwxr-xr-x
unattended_upgrades-0.1.egg-in...
[ DIR ]
drwxr-xr-x
urllib3
[ DIR ]
drwxr-xr-x
urllib3-1.26.5.egg-info
[ DIR ]
drwxr-xr-x
wadllib
[ DIR ]
drwxr-xr-x
wadllib-1.3.6.egg-info
[ DIR ]
drwxr-xr-x
wheel
[ DIR ]
drwxr-xr-x
wheel-0.37.1.egg-info
[ DIR ]
drwxr-xr-x
xkit
[ DIR ]
drwxr-xr-x
yaml
[ DIR ]
drwxr-xr-x
zipp-1.0.0.egg-info
[ DIR ]
drwxr-xr-x
zope
[ DIR ]
drwxr-xr-x
zope.component-4.3.0.egg-info
[ DIR ]
drwxr-xr-x
zope.event-4.4.egg-info
[ DIR ]
drwxr-xr-x
zope.hookable-5.1.0.egg-info
[ DIR ]
drwxr-xr-x
zope.interface-5.4.0.egg-info
[ DIR ]
drwxr-xr-x
_audit.cpython-310-x86_64-linu...
185.33
KB
-rw-r--r--
_cffi_backend.cpython-310-x86_...
189.91
KB
-rw-r--r--
_dbus_bindings.cpython-310-x86...
164.21
KB
-rw-r--r--
_dbus_glib_bindings.cpython-31...
22.5
KB
-rw-r--r--
_ldb_text.py
3.45
KB
-rw-r--r--
_pyrsistent_version.py
23
B
-rw-r--r--
_selinux.cpython-310-x86_64-li...
259.98
KB
-rw-r--r--
_semanage.cpython-310-x86_64-l...
311.98
KB
-rw-r--r--
_snack.cpython-310-x86_64-linu...
46.77
KB
-rw-r--r--
_tdb_text.py
3.27
KB
-rw-r--r--
_version.py
21
B
-rw-r--r--
apport_python_hook.py
7.87
KB
-rw-r--r--
apt_inst.cpython-310-x86_64-li...
58.66
KB
-rw-r--r--
apt_pkg.cpython-310-x86_64-lin...
338.96
KB
-rw-r--r--
audit.py
38.93
KB
-rw-r--r--
augeas.py
23
KB
-rw-r--r--
auparse.cpython-310-x86_64-lin...
69.23
KB
-rw-r--r--
blinker-1.4.egg-info
3.81
KB
-rw-r--r--
command_not_found-0.3.egg-info
189
B
-rw-r--r--
configargparse.py
48.71
KB
-rw-r--r--
configobj.py
87.51
KB
-rw-r--r--
deb822.py
273
B
-rw-r--r--
debconf.py
6.61
KB
-rw-r--r--
decorator.py
16.82
KB
-rw-r--r--
distro_info.py
12.23
KB
-rw-r--r--
gpg-1.16.0_unknown-py3.10.egg-...
2.21
KB
-rw-r--r--
jsonpatch.py
28.14
KB
-rw-r--r--
jsonpointer.py
9.57
KB
-rw-r--r--
ldb.cpython-310-x86_64-linux-g...
102.24
KB
-rw-r--r--
lsb_release.py
14.55
KB
-rw-r--r--
netifaces.cpython-310-x86_64-l...
22.69
KB
-rw-r--r--
pexpect-4.8.0.egg-info
2.23
KB
-rw-r--r--
problem_report.py
26.24
KB
-rw-r--r--
pvectorc.cpython-310-x86_64-li...
36.67
KB
-rw-r--r--
pyasn1_modules-0.2.1.egg-info
1.68
KB
-rw-r--r--
pyparsing.py
266.96
KB
-rw-r--r--
pysss.cpython-310-x86_64-linux...
39.87
KB
-rw-r--r--
pysss_murmur.cpython-310-x86_6...
14.49
KB
-rw-r--r--
python_augeas-0.5.0.egg-info
238
B
-rw-r--r--
selinux-3.3.egg-info
221
B
-rw-r--r--
semanage.py
38.21
KB
-rw-r--r--
seobject.py
103.29
KB
-rw-r--r--
sepolicy-3.3.egg-info
227
B
-rw-r--r--
six.py
33.74
KB
-rw-r--r--
snack.py
30.4
KB
-rw-r--r--
sos-4.4.egg-info
335
B
-rw-r--r--
systemd_python-234.egg-info
586
B
-rw-r--r--
talloc.cpython-310-x86_64-linu...
15.63
KB
-rw-r--r--
tdb.cpython-310-x86_64-linux-g...
36.29
KB
-rw-r--r--
ufw-0.36.1.egg-info
263
B
-rw-r--r--
validate.py
46.14
KB
-rw-r--r--
xkit-0.0.0.egg-info
266
B
-rw-r--r--
zipp.py
6.75
KB
-rw-r--r--
zope.component-4.3.0-nspkg.pth
529
B
-rw-r--r--
zope.event-4.4-nspkg.pth
529
B
-rw-r--r--
zope.hookable-5.1.0-nspkg.pth
529
B
-rw-r--r--
zope.interface-5.4.0-nspkg.pth
529
B
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : decorator.py
# ######################### LICENSE ############################ # # Copyright (c) 2005-2018, Michele Simionato # All rights reserved. # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # Redistributions in bytecode form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. """ Decorator module, see http://pypi.python.org/pypi/decorator for the documentation. """ from __future__ import print_function import re import sys import inspect import operator import itertools import collections __version__ = '4.4.2' if sys.version_info >= (3,): from inspect import getfullargspec def get_init(cls): return cls.__init__ else: FullArgSpec = collections.namedtuple( 'FullArgSpec', 'args varargs varkw defaults ' 'kwonlyargs kwonlydefaults annotations') def getfullargspec(f): "A quick and dirty replacement for getfullargspec for Python 2.X" return FullArgSpec._make(inspect.getargspec(f) + ([], None, {})) def get_init(cls): return cls.__init__.__func__ try: iscoroutinefunction = inspect.iscoroutinefunction except AttributeError: # let's assume there are no coroutine functions in old Python def iscoroutinefunction(f): return False try: from inspect import isgeneratorfunction except ImportError: # assume no generator function in old Python versions def isgeneratorfunction(caller): return False DEF = re.compile(r'\s*def\s*([_\w][_\w\d]*)\s*\(') # basic functionality class FunctionMaker(object): """ An object with the ability to create functions with a given signature. It has attributes name, doc, module, signature, defaults, dict and methods update and make. """ # Atomic get-and-increment provided by the GIL _compile_count = itertools.count() # make pylint happy args = varargs = varkw = defaults = kwonlyargs = kwonlydefaults = () def __init__(self, func=None, name=None, signature=None, defaults=None, doc=None, module=None, funcdict=None): self.shortsignature = signature if func: # func can be a class or a callable, but not an instance method self.name = func.__name__ if self.name == '<lambda>': # small hack for lambda functions self.name = '_lambda_' self.doc = func.__doc__ self.module = func.__module__ if inspect.isfunction(func): argspec = getfullargspec(func) self.annotations = getattr(func, '__annotations__', {}) for a in ('args', 'varargs', 'varkw', 'defaults', 'kwonlyargs', 'kwonlydefaults'): setattr(self, a, getattr(argspec, a)) for i, arg in enumerate(self.args): setattr(self, 'arg%d' % i, arg) allargs = list(self.args) allshortargs = list(self.args) if self.varargs: allargs.append('*' + self.varargs) allshortargs.append('*' + self.varargs) elif self.kwonlyargs: allargs.append('*') # single star syntax for a in self.kwonlyargs: allargs.append('%s=None' % a) allshortargs.append('%s=%s' % (a, a)) if self.varkw: allargs.append('**' + self.varkw) allshortargs.append('**' + self.varkw) self.signature = ', '.join(allargs) self.shortsignature = ', '.join(allshortargs) self.dict = func.__dict__.copy() # func=None happens when decorating a caller if name: self.name = name if signature is not None: self.signature = signature if defaults: self.defaults = defaults if doc: self.doc = doc if module: self.module = module if funcdict: self.dict = funcdict # check existence required attributes assert hasattr(self, 'name') if not hasattr(self, 'signature'): raise TypeError('You are decorating a non function: %s' % func) def update(self, func, **kw): "Update the signature of func with the data in self" func.__name__ = self.name func.__doc__ = getattr(self, 'doc', None) func.__dict__ = getattr(self, 'dict', {}) func.__defaults__ = self.defaults func.__kwdefaults__ = self.kwonlydefaults or None func.__annotations__ = getattr(self, 'annotations', None) try: frame = sys._getframe(3) except AttributeError: # for IronPython and similar implementations callermodule = '?' else: callermodule = frame.f_globals.get('__name__', '?') func.__module__ = getattr(self, 'module', callermodule) func.__dict__.update(kw) def make(self, src_templ, evaldict=None, addsource=False, **attrs): "Make a new function from a given template and update the signature" src = src_templ % vars(self) # expand name and signature evaldict = evaldict or {} mo = DEF.search(src) if mo is None: raise SyntaxError('not a valid function template\n%s' % src) name = mo.group(1) # extract the function name names = set([name] + [arg.strip(' *') for arg in self.shortsignature.split(',')]) for n in names: if n in ('_func_', '_call_'): raise NameError('%s is overridden in\n%s' % (n, src)) if not src.endswith('\n'): # add a newline for old Pythons src += '\n' # Ensure each generated function has a unique filename for profilers # (such as cProfile) that depend on the tuple of (<filename>, # <definition line>, <function name>) being unique. filename = '<decorator-gen-%d>' % next(self._compile_count) try: code = compile(src, filename, 'single') exec(code, evaldict) except Exception: print('Error in generated code:', file=sys.stderr) print(src, file=sys.stderr) raise func = evaldict[name] if addsource: attrs['__source__'] = src self.update(func, **attrs) return func @classmethod def create(cls, obj, body, evaldict, defaults=None, doc=None, module=None, addsource=True, **attrs): """ Create a function from the strings name, signature and body. evaldict is the evaluation dictionary. If addsource is true an attribute __source__ is added to the result. The attributes attrs are added, if any. """ if isinstance(obj, str): # "name(signature)" name, rest = obj.strip().split('(', 1) signature = rest[:-1] # strip a right parens func = None else: # a function name = None signature = None func = obj self = cls(func, name, signature, defaults, doc, module) ibody = '\n'.join(' ' + line for line in body.splitlines()) caller = evaldict.get('_call_') # when called from `decorate` if caller and iscoroutinefunction(caller): body = ('async def %(name)s(%(signature)s):\n' + ibody).replace( 'return', 'return await') else: body = 'def %(name)s(%(signature)s):\n' + ibody return self.make(body, evaldict, addsource, **attrs) def decorate(func, caller, extras=()): """ decorate(func, caller) decorates a function using a caller. If the caller is a generator function, the resulting function will be a generator function. """ evaldict = dict(_call_=caller, _func_=func) es = '' for i, extra in enumerate(extras): ex = '_e%d_' % i evaldict[ex] = extra es += ex + ', ' if '3.5' <= sys.version < '3.6': # with Python 3.5 isgeneratorfunction returns True for all coroutines # however we know that it is NOT possible to have a generator # coroutine in python 3.5: PEP525 was not there yet generatorcaller = isgeneratorfunction( caller) and not iscoroutinefunction(caller) else: generatorcaller = isgeneratorfunction(caller) if generatorcaller: fun = FunctionMaker.create( func, "for res in _call_(_func_, %s%%(shortsignature)s):\n" " yield res" % es, evaldict, __wrapped__=func) else: fun = FunctionMaker.create( func, "return _call_(_func_, %s%%(shortsignature)s)" % es, evaldict, __wrapped__=func) if hasattr(func, '__qualname__'): fun.__qualname__ = func.__qualname__ return fun def decorator(caller, _func=None): """decorator(caller) converts a caller function into a decorator""" if _func is not None: # return a decorated function # this is obsolete behavior; you should use decorate instead return decorate(_func, caller) # else return a decorator function defaultargs, defaults = '', () if inspect.isclass(caller): name = caller.__name__.lower() doc = 'decorator(%s) converts functions/generators into ' \ 'factories of %s objects' % (caller.__name__, caller.__name__) elif inspect.isfunction(caller): if caller.__name__ == '<lambda>': name = '_lambda_' else: name = caller.__name__ doc = caller.__doc__ nargs = caller.__code__.co_argcount ndefs = len(caller.__defaults__ or ()) defaultargs = ', '.join(caller.__code__.co_varnames[nargs-ndefs:nargs]) if defaultargs: defaultargs += ',' defaults = caller.__defaults__ else: # assume caller is an object with a __call__ method name = caller.__class__.__name__.lower() doc = caller.__call__.__doc__ evaldict = dict(_call=caller, _decorate_=decorate) dec = FunctionMaker.create( '%s(func, %s)' % (name, defaultargs), 'if func is None: return lambda func: _decorate_(func, _call, (%s))\n' 'return _decorate_(func, _call, (%s))' % (defaultargs, defaultargs), evaldict, doc=doc, module=caller.__module__, __wrapped__=caller) if defaults: dec.__defaults__ = (None,) + defaults return dec # ####################### contextmanager ####################### # try: # Python >= 3.2 from contextlib import _GeneratorContextManager except ImportError: # Python >= 2.5 from contextlib import GeneratorContextManager as _GeneratorContextManager class ContextManager(_GeneratorContextManager): def __call__(self, func): """Context manager decorator""" return FunctionMaker.create( func, "with _self_: return _func_(%(shortsignature)s)", dict(_self_=self, _func_=func), __wrapped__=func) init = getfullargspec(_GeneratorContextManager.__init__) n_args = len(init.args) if n_args == 2 and not init.varargs: # (self, genobj) Python 2.7 def __init__(self, g, *a, **k): return _GeneratorContextManager.__init__(self, g(*a, **k)) ContextManager.__init__ = __init__ elif n_args == 2 and init.varargs: # (self, gen, *a, **k) Python 3.4 pass elif n_args == 4: # (self, gen, args, kwds) Python 3.5 def __init__(self, g, *a, **k): return _GeneratorContextManager.__init__(self, g, a, k) ContextManager.__init__ = __init__ _contextmanager = decorator(ContextManager) def contextmanager(func): # Enable Pylint config: contextmanager-decorators=decorator.contextmanager return _contextmanager(func) # ############################ dispatch_on ############################ # def append(a, vancestors): """ Append ``a`` to the list of the virtual ancestors, unless it is already included. """ add = True for j, va in enumerate(vancestors): if issubclass(va, a): add = False break if issubclass(a, va): vancestors[j] = a add = False if add: vancestors.append(a) # inspired from simplegeneric by P.J. Eby and functools.singledispatch def dispatch_on(*dispatch_args): """ Factory of decorators turning a function into a generic function dispatching on the given arguments. """ assert dispatch_args, 'No dispatch args passed' dispatch_str = '(%s,)' % ', '.join(dispatch_args) def check(arguments, wrong=operator.ne, msg=''): """Make sure one passes the expected number of arguments""" if wrong(len(arguments), len(dispatch_args)): raise TypeError('Expected %d arguments, got %d%s' % (len(dispatch_args), len(arguments), msg)) def gen_func_dec(func): """Decorator turning a function into a generic function""" # first check the dispatch arguments argset = set(getfullargspec(func).args) if not set(dispatch_args) <= argset: raise NameError('Unknown dispatch arguments %s' % dispatch_str) typemap = {} def vancestors(*types): """ Get a list of sets of virtual ancestors for the given types """ check(types) ras = [[] for _ in range(len(dispatch_args))] for types_ in typemap: for t, type_, ra in zip(types, types_, ras): if issubclass(t, type_) and type_ not in t.mro(): append(type_, ra) return [set(ra) for ra in ras] def ancestors(*types): """ Get a list of virtual MROs, one for each type """ check(types) lists = [] for t, vas in zip(types, vancestors(*types)): n_vas = len(vas) if n_vas > 1: raise RuntimeError( 'Ambiguous dispatch for %s: %s' % (t, vas)) elif n_vas == 1: va, = vas mro = type('t', (t, va), {}).mro()[1:] else: mro = t.mro() lists.append(mro[:-1]) # discard t and object return lists def register(*types): """ Decorator to register an implementation for the given types """ check(types) def dec(f): check(getfullargspec(f).args, operator.lt, ' in ' + f.__name__) typemap[types] = f return f return dec def dispatch_info(*types): """ An utility to introspect the dispatch algorithm """ check(types) lst = [] for anc in itertools.product(*ancestors(*types)): lst.append(tuple(a.__name__ for a in anc)) return lst def _dispatch(dispatch_args, *args, **kw): types = tuple(type(arg) for arg in dispatch_args) try: # fast path f = typemap[types] except KeyError: pass else: return f(*args, **kw) combinations = itertools.product(*ancestors(*types)) next(combinations) # the first one has been already tried for types_ in combinations: f = typemap.get(types_) if f is not None: return f(*args, **kw) # else call the default implementation return func(*args, **kw) return FunctionMaker.create( func, 'return _f_(%s, %%(shortsignature)s)' % dispatch_str, dict(_f_=_dispatch), register=register, default=func, typemap=typemap, vancestors=vancestors, ancestors=ancestors, dispatch_info=dispatch_info, __wrapped__=func) gen_func_dec.__name__ = 'dispatch_on' + dispatch_str return gen_func_dec
Close