Skip to content

Commit 7882f4e

Browse files
committed
Cleanup not used parts of serialization.reflect module. Add tests for the rest and fix py 3 support.
1 parent 2485d92 commit 7882f4e

File tree

2 files changed

+114
-59
lines changed

2 files changed

+114
-59
lines changed

serialization/reflect.py

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from __future__ import absolute_import
2424

25+
from six.moves import builtins
2526
from functools import wraps
2627
import inspect
2728
import sys
@@ -69,17 +70,6 @@ def wrapper(*orig_args, **orig_kwargs):
6970
return wrapper
7071

7172

72-
@unicode_args
73-
def named_function(name):
74-
"""Gets a fully named module-global object."""
75-
name_parts = name.split('.')
76-
module = named_object('.'.join(name_parts[:-1]))
77-
func = getattr(module, name_parts[-1])
78-
if hasattr(func, 'original_func'):
79-
func = func.original_func
80-
return func
81-
82-
8373
@unicode_args
8474
def named_module(name):
8575
"""Returns a module given its name."""
@@ -112,52 +102,6 @@ def class_locals(depth, tag=None):
112102
return locals
113103

114104

115-
def class_canonical_name(depth):
116-
frame = sys._getframe(depth)
117-
module = frame.f_locals['__module__']
118-
class_name = frame.f_code.co_name
119-
return '.'.join([module, class_name])
120-
121-
122-
def inside_class_definition(depth):
123-
frame = sys._getframe(depth)
124-
locals = frame.f_locals
125-
# Try to make sure we were called from a class def. In 2.2.0 we can't
126-
# check for __module__ since it doesn't seem to be added to the locals
127-
# until later on. (Copied From zope.interfaces.declartion._implements)
128-
if ((locals is frame.f_globals)
129-
or (('__module__' not in locals)
130-
and sys.version_info[:3] > (2, 2, 0))):
131-
return False
132-
return True
133-
134-
135-
def formatted_function_name(function):
136-
if hasattr(function, 'original_func'):
137-
function = function.original_func
138-
argspec = inspect.getargspec(function)
139-
defaults = argspec.defaults and list(argspec.defaults) or list()
140-
141-
if argspec.args and argspec.args[0] == 'self':
142-
argspec.args.pop(0)
143-
if argspec.args and argspec.args[0] == 'state':
144-
argspec.args.pop(0)
145-
146-
args = argspec.args or list()
147-
display_args = [x if len(defaults) < -index
148-
else "%s=%s" % (x, defaults[index])
149-
for x, index in zip(args, range(-len(args), 0, 1))]
150-
if argspec.varargs:
151-
display_args += ['*%s' % (argspec.varargs)]
152-
if argspec.keywords:
153-
display_args += ['**%s' % (argspec.keywords)]
154-
155-
text = "%s(" % (function.__name__, )
156-
text += ', '.join(display_args)
157-
text += ')'
158-
return text
159-
160-
161105
### Private Methods ###
162106

163107

@@ -176,5 +120,4 @@ def _canonical_method(obj):
176120

177121

178122
def _canonical_builtin(obj):
179-
# TODO: py2 only
180-
return "__builtin__." + obj.__name__
123+
return builtins.__name__ + '.' + obj.__name__

tests/test_reflect.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# F3AT - Flumotion Asynchronous Autonomous Agent Toolkit
2+
# Copyright (C) 2010,2011 Flumotion Services, S.A.
3+
# All rights reserved.
4+
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 2 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License along
16+
# with this program; if not, write to the Free Software Foundation, Inc.,
17+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
19+
# See 'LICENSE.GPL' in the source distribution for more information.
20+
21+
# Headers in this file shall remain intact.
22+
# -*- coding: utf-8 -*-
23+
# -*- Mode: Python -*-
24+
# vi:si:et:sw=4:sts=4:ts=4
25+
26+
import pytest
27+
from serialization import reflect
28+
from six.moves import builtins
29+
from zope.interface import Interface
30+
31+
32+
class DummyInterface(Interface):
33+
pass
34+
35+
36+
class Dummy(object):
37+
38+
def spam(self):
39+
pass
40+
41+
42+
def bacon():
43+
pass
44+
45+
46+
class Meta(type):
47+
pass
48+
49+
50+
class MetaDummy(object):
51+
__metaclass__ = Meta
52+
53+
54+
class TestIntrospection(object):
55+
56+
@pytest.fixture(scope='session')
57+
def builtin_module(self):
58+
return builtins.__name__
59+
60+
def test_interface(self):
61+
assert (
62+
'tests.test_reflect.DummyInterface' ==
63+
reflect.canonical_name(DummyInterface))
64+
65+
def test_class(self, builtin_module):
66+
assert 'tests.test_reflect.Dummy' == reflect.canonical_name(Dummy)
67+
assert 'tests.test_reflect.Dummy' == reflect.canonical_name(Dummy())
68+
assert builtin_module + '.int' == reflect.canonical_name(int)
69+
assert builtin_module + '.str' == reflect.canonical_name('some string')
70+
71+
def test_class_with_meta(self):
72+
assert (
73+
'tests.test_reflect.MetaDummy' ==
74+
reflect.canonical_name(MetaDummy))
75+
76+
def test_method(self, builtin_module):
77+
assert (
78+
'tests.test_reflect.Dummy.spam' ==
79+
reflect.canonical_name(Dummy.spam))
80+
assert (
81+
'tests.test_reflect.Dummy.spam' ==
82+
reflect.canonical_name(Dummy().spam))
83+
assert (
84+
builtin_module + '.split' ==
85+
reflect.canonical_name('test'.split))
86+
87+
def test_function(self, builtin_module):
88+
assert 'tests.test_reflect.bacon' == reflect.canonical_name(bacon)
89+
assert builtin_module + '.getattr' == reflect.canonical_name(getattr)
90+
91+
def test_none(self):
92+
assert None == reflect.canonical_name(None)
93+
94+
def test_class_locals_not_from_class(self):
95+
with pytest.raises(TypeError):
96+
reflect.class_locals(depth=2)
97+
98+
99+
def simple(a, b):
100+
pass
101+
102+
103+
def defaults(a, b=3):
104+
pass
105+
106+
107+
def varargs(a, b=None, *args):
108+
pass
109+
110+
111+
def kwargs(a=None, b=3, *args, **kwargs):
112+
pass

0 commit comments

Comments
 (0)