Skip to content

Commit 668a1a9

Browse files
committed
method of a class usable as a check function.
1 parent 79dabd3 commit 668a1a9

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

roundup/anypy/findargspec.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
''' Wrapper for getargspec to support other callables and python 3 support
2+
3+
In python 3 just uses getfullargspec which handles regular functions
4+
and classes with __call__ methods.
5+
'''
6+
7+
try:
8+
# Python 3+
9+
from inspect import getfullargspec as getargspec
10+
findargspec = getargspec
11+
except:
12+
# Python 2.5-2.7 modified from https://bugs.python.org/issue20828
13+
import inspect
14+
15+
def findargspec(fn):
16+
if inspect.isfunction(fn) or inspect.ismethod(fn):
17+
inspectable = fn
18+
elif inspect.isclass(fn):
19+
inspectable = fn.__init__
20+
elif hasattr(fn, '__call__'):
21+
inspectable = fn.__call__
22+
else:
23+
inspectable = fn
24+
25+
try:
26+
return inspect.getargspec(inspectable)
27+
except TypeError:
28+
raise

roundup/security.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Permission:
4747

4848
def __init__(self, name='', description='', klass=None,
4949
properties=None, check=None, props_only=None):
50-
import inspect
50+
from roundup.anypy import findargspec
5151
self.name = name
5252
self.description = description
5353
self.klass = klass
@@ -75,11 +75,11 @@ def __init__(self, name='', description='', klass=None,
7575
if check is None:
7676
self.check_version = 0
7777
else:
78-
args=inspect.getargspec(check)
79-
# FIXME change args[2] to args.keywords since python
80-
# 2.6 made getargspec a named tuple once roundup 1.6 released.
78+
args=findargspec.findargspec(check)
79+
# args[2] is the keywords argument. Leave it as a subscript and
80+
# do not use named tuple reference as names change in python 3.
8181
# If there is a **parameter defined in the function spec, the
82-
# value of the 3rd argument in the tuple is not None.
82+
# value of the 3rd argument (2nd index) in the tuple is not None.
8383
if args[2] is None:
8484
# function definition is function(db, userid, itemid)
8585
self.check_version = 1

test/test_security.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,20 @@ def check(db,userid,itemid, **other):
181181
prop = other['permission']
182182
return (itemid == '1')
183183

184+
# also create a check as a callable of a class
185+
# http://issues.roundup-tracker.org/issue2550952
186+
class CheckClass(object):
187+
def __call__(self, db,userid,itemid, **other):
188+
prop = other['property']
189+
prop = other['classname']
190+
prop = other['permission']
191+
return (itemid == '1')
192+
184193
addRole(name='Role3')
185-
addToRole('Role3', add(name="Test", klass="test", check=check))
194+
# make sure check=CheckClass() and not check=CheckClass
195+
# otherwise we get:
196+
# inspectible <slot wrapper '__init__' of 'object' objects>
197+
addToRole('Role3', add(name="Test", klass="test", check=CheckClass()))
186198
user3 = self.db.user.create(username='user3', roles='Role3')
187199

188200
addRole(name='Role4')

0 commit comments

Comments
 (0)