[Python-modules-commits] [python-decorator] 01/04: Import python-decorator_4.1.2.orig.tar.gz
Piotr Ożarowski
piotr at moszumanska.debian.org
Fri Sep 29 09:10:09 UTC 2017
This is an automated email from the git hooks/post-receive script.
piotr pushed a commit to branch master
in repository python-decorator.
commit 0f680f6bde8b6594655edc33ba923d6d8c763400
Author: Piotr Ożarowski <piotr at debian.org>
Date: Fri Sep 29 11:06:19 2017 +0200
Import python-decorator_4.1.2.orig.tar.gz
---
CHANGES.md | 7 ++++++-
PKG-INFO | 11 ++++++-----
docs/README.rst | 9 +++++----
src/decorator.egg-info/PKG-INFO | 11 ++++++-----
src/decorator.py | 11 +++++------
src/tests/documentation.py | 26 ++++++++++++++++++++------
src/tests/test.py | 13 +++++++++++--
7 files changed, 59 insertions(+), 29 deletions(-)
diff --git a/CHANGES.md b/CHANGES.md
index ce2339a..148c1a3 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -3,7 +3,12 @@ HISTORY
## Unreleased
-## 4.1.1 (2017-06-15)
+## 4.1.2 (2017-07-23)
+
+Made it possible to define decorators converting coroutines into regular
+functions, as requested by Itaï Ben Yaacov.
+
+## 4.1.1 (2017-06-16)
Changed the documentation build system to sphinx and uploaded the docs
on readthedocs.org.
diff --git a/PKG-INFO b/PKG-INFO
index 9353c58..0f895dc 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
Metadata-Version: 1.1
Name: decorator
-Version: 4.1.1
+Version: 4.1.2
Summary: Better living through Python with decorators
Home-page: https://github.com/micheles/decorator
Author: Michele Simionato
@@ -26,19 +26,19 @@ Description: Decorator module
which will install just the module on your system.
If you prefer to install the full distribution from source, including
- the documentation, clone the [GitHub repo](https://github.com/micheles/decorator) or download the tarball_, unpack it and run
+ the documentation, clone the `GitHub repo`_ or download the tarball_, unpack it and run
`$ pip install .`
in the main directory, possibly as superuser.
.. _tarball: http://pypi.python.org/pypi/decorator
-
+ .. _GitHub repo: https://github.com/micheles/decorator
Testing
--------
- If you have the source code installtion you can run the tests with
+ If you have the source code installation you can run the tests with
`$ python src/tests/test.py -v`
@@ -61,7 +61,8 @@ Description: Decorator module
Documentation
---------------
- The documentation has been moved to http://decorator.readthedocs.io/en/latest/
+ The documentation has been moved to http://decorator.readthedocs.io/en/stable/
+ You can download a PDF version of it from http://media.readthedocs.org/pdf/decorator/stable/decorator.pdf
Keywords: decorators generic utility
Platform: All
diff --git a/docs/README.rst b/docs/README.rst
index 5a68b63..6915e58 100644
--- a/docs/README.rst
+++ b/docs/README.rst
@@ -18,19 +18,19 @@ If you are lazy, just perform
which will install just the module on your system.
If you prefer to install the full distribution from source, including
-the documentation, clone the [GitHub repo](https://github.com/micheles/decorator) or download the tarball_, unpack it and run
+the documentation, clone the `GitHub repo`_ or download the tarball_, unpack it and run
`$ pip install .`
in the main directory, possibly as superuser.
.. _tarball: http://pypi.python.org/pypi/decorator
-
+.. _GitHub repo: https://github.com/micheles/decorator
Testing
--------
-If you have the source code installtion you can run the tests with
+If you have the source code installation you can run the tests with
`$ python src/tests/test.py -v`
@@ -53,4 +53,5 @@ The project is hosted on GitHub. You can look at the source here:
Documentation
---------------
-The documentation has been moved to http://decorator.readthedocs.io/en/latest/
+The documentation has been moved to http://decorator.readthedocs.io/en/stable/
+You can download a PDF version of it from http://media.readthedocs.org/pdf/decorator/stable/decorator.pdf
diff --git a/src/decorator.egg-info/PKG-INFO b/src/decorator.egg-info/PKG-INFO
index 9353c58..0f895dc 100644
--- a/src/decorator.egg-info/PKG-INFO
+++ b/src/decorator.egg-info/PKG-INFO
@@ -1,6 +1,6 @@
Metadata-Version: 1.1
Name: decorator
-Version: 4.1.1
+Version: 4.1.2
Summary: Better living through Python with decorators
Home-page: https://github.com/micheles/decorator
Author: Michele Simionato
@@ -26,19 +26,19 @@ Description: Decorator module
which will install just the module on your system.
If you prefer to install the full distribution from source, including
- the documentation, clone the [GitHub repo](https://github.com/micheles/decorator) or download the tarball_, unpack it and run
+ the documentation, clone the `GitHub repo`_ or download the tarball_, unpack it and run
`$ pip install .`
in the main directory, possibly as superuser.
.. _tarball: http://pypi.python.org/pypi/decorator
-
+ .. _GitHub repo: https://github.com/micheles/decorator
Testing
--------
- If you have the source code installtion you can run the tests with
+ If you have the source code installation you can run the tests with
`$ python src/tests/test.py -v`
@@ -61,7 +61,8 @@ Description: Decorator module
Documentation
---------------
- The documentation has been moved to http://decorator.readthedocs.io/en/latest/
+ The documentation has been moved to http://decorator.readthedocs.io/en/stable/
+ You can download a PDF version of it from http://media.readthedocs.org/pdf/decorator/stable/decorator.pdf
Keywords: decorators generic utility
Platform: All
diff --git a/src/decorator.py b/src/decorator.py
index aa09566..788f4bb 100644
--- a/src/decorator.py
+++ b/src/decorator.py
@@ -40,7 +40,7 @@ import operator
import itertools
import collections
-__version__ = '4.1.1'
+__version__ = '4.1.2'
if sys.version >= '3':
from inspect import getfullargspec
@@ -97,7 +97,6 @@ class FunctionMaker(object):
def __init__(self, func=None, name=None, signature=None,
defaults=None, doc=None, module=None, funcdict=None):
self.shortsignature = signature
- self.coro = False
if func:
# func can be a class or a callable, but not an instance method
self.name = func.__name__
@@ -106,7 +105,6 @@ class FunctionMaker(object):
self.doc = func.__doc__
self.module = func.__module__
if inspect.isfunction(func):
- self.coro = iscoroutinefunction(func)
argspec = getfullargspec(func)
self.annotations = getattr(func, '__annotations__', {})
for a in ('args', 'varargs', 'varkw', 'defaults', 'kwonlyargs',
@@ -223,7 +221,8 @@ class FunctionMaker(object):
func = obj
self = cls(func, name, signature, defaults, doc, module)
ibody = '\n'.join(' ' + line for line in body.splitlines())
- if self.coro:
+ 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:
@@ -263,9 +262,9 @@ def decorator(caller, _func=None):
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)
+ evaldict = dict(_call=caller, _decorate_=decorate)
return FunctionMaker.create(
- '%s(func)' % name, 'return _decorate_(func, _call_)',
+ '%s(func)' % name, 'return _decorate_(func, _call)',
evaldict, doc=doc, module=caller.__module__,
__wrapped__=caller)
diff --git a/src/tests/documentation.py b/src/tests/documentation.py
index eb69b9d..e8fcc55 100644
--- a/src/tests/documentation.py
+++ b/src/tests/documentation.py
@@ -2,7 +2,6 @@ from __future__ import print_function
doc = r"""\
The ``decorator`` module
-------------------------
:Author: Michele Simionato
:E-mail: michele.simionato at gmail.com
@@ -22,7 +21,7 @@ kicking. It is used by several frameworks (IPython, scipy, authkit,
pylons, pycuda, sugar, ...) and has been stable for a *long*
time. It is your best option if you want to preserve the signature of
decorated functions in a consistent way across Python
-releases. Version 4.0 is fully compatible with the past, except for
+releases. Version 4 is fully compatible with the past, except for
one thing: support for Python 2.4 and 2.5 has been dropped. That
decision made it possible to use a single code base both for Python
2.X and Python 3.X. This is a *huge* bonus, since I could remove over
@@ -72,7 +71,7 @@ What's New in version 4
- **Python 3.5 coroutines**
From version 4.1 it is possible to decorate coroutines, i.e. functions
- defined with the `async def` syntax, and to mantain the
+ defined with the `async def` syntax, and to maintain the
`inspect.iscoroutinefunction` check working for the decorated function.
Usefulness of decorators
@@ -739,7 +738,7 @@ write code like the following:
tasks = [make_task(3), make_task(2), make_task(1)]
get_event_loop().run_until_complete(wait(tasks))
-and you will get at output like this::
+and you will get an output like this::
INFO:root:Starting make_task(1,)
INFO:root:Starting make_task(3,)
@@ -753,8 +752,23 @@ with a particularly complex chain of coroutines. With a single line you
can decorate the troubling coroutine function, understand what happens, fix the
issue and then remove the decorator (or keep it if continuous monitoring
of the coroutines makes sense). Notice that
-`inspect.iscoroutinefunction(make_task)`
-will return then right answer (i.e. `True`).
+``inspect.iscoroutinefunction(make_task)``
+will return the right answer (i.e. ``True``).
+
+It is also possible to define decorators converting coroutine functions
+into regular functions, such as the following:
+
+.. code-block:: python
+
+ @decorator
+ def coro_to_func(coro, *args, **kw):
+ "Convert a coroutine into a function"
+ return get_event_loop().run_until_complete(coro(*args, **kw))
+
+Notice the diffence: the caller in ``log_start_stop`` was a coroutine
+function and the associate decorator was converting coroutines->coroutines;
+the caller in ``coro_to_func`` is a regular function and converts
+coroutines -> functions.
Multiple dispatch
-------------------------------------------
diff --git a/src/tests/test.py b/src/tests/test.py
index d882418..7eb8391 100644
--- a/src/tests/test.py
+++ b/src/tests/test.py
@@ -30,15 +30,25 @@ if sys.version >= '3.5':
async def before_after(coro, *args, **kwargs):
return "<before>" + (await coro(*args, **kwargs)) + "<after>"
+ at decorator
+def coro_to_func(coro, *args, **kw):
+ return get_event_loop().run_until_complete(coro(*args, **kw))
class CoroutineTestCase(unittest.TestCase):
- def test(self):
+ def test_before_after(self):
@before_after
async def coro(x):
return x
self.assertTrue(inspect.iscoroutinefunction(coro))
out = get_event_loop().run_until_complete(coro('x'))
self.assertEqual(out, '<before>x<after>')
+
+ def test_coro_to_func(self):
+ @coro_to_func
+ async def coro(x):
+ return x
+ self.assertFalse(inspect.iscoroutinefunction(coro))
+ self.assertEqual(coro('x'), 'x')
''')
@@ -92,7 +102,6 @@ class ExtraTestCase(unittest.TestCase):
@d1
def f1(x, y, z):
pass
-
self.assertNotEqual(d1.__code__.co_filename, d2.__code__.co_filename)
self.assertNotEqual(f1.__code__.co_filename, f2.__code__.co_filename)
self.assertNotEqual(f1_orig.__code__.co_filename,
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-decorator.git
More information about the Python-modules-commits
mailing list