diff options
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | documentation/index.rst | 10 | ||||
-rw-r--r-- | six.py | 6 | ||||
-rw-r--r-- | test_six.py | 16 |
4 files changed, 34 insertions, 0 deletions
@@ -6,6 +6,8 @@ This file lists the changes in each six version. Development version ------------------- +- Issue #21: Add methods to access the closure and globals of a function. + - In six.iter(items/keys/values/lists), passed keyword arguments through to the underlying method. diff --git a/documentation/index.rst b/documentation/index.rst index 7d215f5..0880cad 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -127,6 +127,11 @@ functions and methods is the stdlib :mod:`py3:inspect` module. Get the ``self`` of bound method *meth*. +.. function:: get_function_closure(func) + + Get the closure (list of cells) associated with *func*. + + .. function:: get_function_code(func) Get the code object associated with *func*. @@ -137,6 +142,11 @@ functions and methods is the stdlib :mod:`py3:inspect` module. Get the defaults tuple associated with *func*. +.. function:: get_function_globals(func) + + Get the globals of *func*. + + .. function:: next(it) .. function:: advance_iterator(it) @@ -209,8 +209,10 @@ if PY3: _meth_func = "__func__" _meth_self = "__self__" + _func_closure = "__closure__" _func_code = "__code__" _func_defaults = "__defaults__" + _func_globals = "__globals__" _iterkeys = "keys" _itervalues = "values" @@ -220,8 +222,10 @@ else: _meth_func = "im_func" _meth_self = "im_self" + _func_closure = "func_closure" _func_code = "func_code" _func_defaults = "func_defaults" + _func_globals = "func_globals" _iterkeys = "iterkeys" _itervalues = "itervalues" @@ -265,8 +269,10 @@ _add_doc(get_unbound_function, get_method_function = operator.attrgetter(_meth_func) get_method_self = operator.attrgetter(_meth_self) +get_function_closure = operator.attrgetter(_func_closure) get_function_code = operator.attrgetter(_func_code) get_function_defaults = operator.attrgetter(_func_defaults) +get_function_globals = operator.attrgetter(_func_globals) def iterkeys(d, **kw): diff --git a/test_six.py b/test_six.py index 4bcfb13..58a3a19 100644 --- a/test_six.py +++ b/test_six.py @@ -211,6 +211,16 @@ def test_get_method_function(): py.test.raises(AttributeError, six.get_method_function, hasattr) +def test_get_function_closure(): + def f(): + x = 42 + def g(): + return x + return g + cell = six.get_function_closure(f())[0] + assert type(cell).__name__ == "cell" + + def test_get_function_code(): def f(): pass @@ -225,6 +235,12 @@ def test_get_function_defaults(): assert six.get_function_defaults(f) == (3, 4) +def test_get_function_globals(): + def f(): + pass + assert six.get_function_globals(f) is globals() + + def test_dictionary_iterators(monkeypatch): class MyDict(dict): if not six.PY3: |