view 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
line wrap: on
line source

''' Wrapper for getargspec to support other callables and python 3 support

In python 3 just uses getfullargspec which handles regular functions
and classes with __call__ methods.
'''

try:
    # Python 3+
    from inspect import getfullargspec as getargspec
    findargspec = getargspec
except:
    # Python 2.5-2.7 modified from https://bugs.python.org/issue20828
    import inspect

    def findargspec(fn):
        if inspect.isfunction(fn) or inspect.ismethod(fn):
            inspectable = fn
        elif inspect.isclass(fn):
            inspectable = fn.__init__
        elif hasattr(fn, '__call__'):
            inspectable = fn.__call__
        else:
            inspectable = fn

        try:
            return inspect.getargspec(inspectable)
        except TypeError:
            raise

Roundup Issue Tracker: http://roundup-tracker.org/