Mercurial > p > roundup > code
annotate roundup/anypy/findargspec.py @ 5650:e8ca7072c629
Fix Python 3 issues in REST code.
* Need to use .get not .getheader for HTTP headers (see hg commit
fec18298ae02, "Python 3 preparation: HTTP headers handling in
roundup_server.py.").
* Need to use key not cmp with sort (see hg commit 3fa026621f69,
"Python 3 preparation: comparisons.").
* dispatch output must be bytes, not str, otherwise writing it to the
socket (e.g. in roundup-server) will fail.
This fixes issues shown up attempting to access the REST interface
with a browser with Python 3 (as opposed to with the Roundup
testsuite, which also has known REST issues with Python 3).
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Sun, 17 Mar 2019 16:25:36 +0000 |
| parents | c94fd717e28c |
| children | c588e64718fc |
| rev | line source |
|---|---|
|
5269
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
1 ''' Wrapper for getargspec to support other callables and python 3 support |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
2 |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
3 In python 3 just uses getfullargspec which handles regular functions |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
4 and classes with __call__ methods. |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
5 ''' |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
6 |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
7 try: |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
8 # Python 3+ |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
9 from inspect import getfullargspec as getargspec |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
10 findargspec = getargspec |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
11 except: |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
12 # Python 2.5-2.7 modified from https://bugs.python.org/issue20828 |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
13 import inspect |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
14 |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
15 def findargspec(fn): |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
16 if inspect.isfunction(fn) or inspect.ismethod(fn): |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
17 inspectable = fn |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
18 elif inspect.isclass(fn): |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
19 inspectable = fn.__init__ |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
20 elif hasattr(fn, '__call__'): |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
21 inspectable = fn.__call__ |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
22 else: |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
23 inspectable = fn |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
24 |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
25 try: |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
26 return inspect.getargspec(inspectable) |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
27 except TypeError: |
|
c94fd717e28c
Fix http://issues.roundup-tracker.org/issue2550952 make __call__
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
28 raise |
