# HG changeset patch
# User Mitesh Patel <qed777@gmail.com>
# Date 1266817684 28800
# Node ID 01baeb94ad88583b52a6eb67297daf8790ca2561
# Parent  81e5376e9b228d2cdca50d367dab4eecc91e84a9
#8325: Alternate way to get Cython argspec

diff --git a/sage/misc/sageinspect.py b/sage/misc/sageinspect.py
--- a/sage/misc/sageinspect.py
+++ b/sage/misc/sageinspect.py
@@ -294,11 +294,19 @@ def _sage_getargspec_cython(source):
 
     EXAMPLES::
 
-        sage: from sage.misc.sageinspect import _sage_getargspec_cython
-        sage: _sage_getargspec_cython("def init(self, x=None, base=0):")
+        sage: from sage.misc.sageinspect import _sage_getargspec_cython as sgc
+        sage: sgc("cpdef double abc(self, x=None, base=0):")
         (['self', 'x', 'base'], None, None, (None, 0))
-        sage: _sage_getargspec_cython("def __init__(self, x=None, unsigned int base=0):")
+        sage: sgc("def __init__(self, x=None, unsigned int base=0):")
         (['self', 'x', 'base'], None, None, (None, 0))
+        sage: sgc('def o(p, *q, r={}, **s) except? -1:')
+        (['p', '*q', 'r', '**s'], None, None, ({},))
+        sage: sgc('cpdef how(r=(None, "u:doing?")):')
+        ArgSpec(args=['r'], varargs=None, keywords=None, defaults=((None, 'u:doing?'),))
+        sage: sgc('def _(x="):"):')
+        Traceback (most recent call last):
+        ...
+        ValueError: Could not parse cython argspec
 
     AUTHOR:
     
@@ -353,7 +361,15 @@ def _sage_getargspec_cython(source):
 
         return (argnames, None, None, argdefs)
     except:
-        raise ValueError, "Could not parse cython argspec"
+        # Try to make a dummy Python function with the same argspec.
+        try:
+            beg = re.search(r'def([ ]+\w+)+[ ]*\(', source).end() - 1
+            end = re.search(r'\)[ ]*:', source).end()
+            context = {}
+            exec compile('def dummy' + source[beg:end] + '\n    return', '<string>', 'single') in context
+            return inspect.getargspec(context['dummy'])
+        except:
+            raise ValueError, "Could not parse cython argspec"
 
 def sage_getfile(obj):
     r"""
