##// END OF EJS Templates
add utils.getargspec...
Min RK -
Show More
@@ -0,0 +1,86 b''
1 # -*- coding: utf-8 -*-
2 """
3 getargspec excerpted from:
4
5 sphinx.util.inspect
6 ~~~~~~~~~~~~~~~~~~~
7 Helpers for inspecting Python modules.
8 :copyright: Copyright 2007-2015 by the Sphinx team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 import inspect
13 from IPython.utils.py3compat import PY3
14
15 # Unmodified from sphinx below this line
16
17 if PY3:
18 from functools import partial
19
20 def getargspec(func):
21 """Like inspect.getargspec but supports functools.partial as well."""
22 if inspect.ismethod(func):
23 func = func.__func__
24 if type(func) is partial:
25 orig_func = func.func
26 argspec = getargspec(orig_func)
27 args = list(argspec[0])
28 defaults = list(argspec[3] or ())
29 kwoargs = list(argspec[4])
30 kwodefs = dict(argspec[5] or {})
31 if func.args:
32 args = args[len(func.args):]
33 for arg in func.keywords or ():
34 try:
35 i = args.index(arg) - len(args)
36 del args[i]
37 try:
38 del defaults[i]
39 except IndexError:
40 pass
41 except ValueError: # must be a kwonly arg
42 i = kwoargs.index(arg)
43 del kwoargs[i]
44 del kwodefs[arg]
45 return inspect.FullArgSpec(args, argspec[1], argspec[2],
46 tuple(defaults), kwoargs,
47 kwodefs, argspec[6])
48 while hasattr(func, '__wrapped__'):
49 func = func.__wrapped__
50 if not inspect.isfunction(func):
51 raise TypeError('%r is not a Python function' % func)
52 return inspect.getfullargspec(func)
53
54 else: # 2.6, 2.7
55 from functools import partial
56
57 def getargspec(func):
58 """Like inspect.getargspec but supports functools.partial as well."""
59 if inspect.ismethod(func):
60 func = func.__func__
61 parts = 0, ()
62 if type(func) is partial:
63 keywords = func.keywords
64 if keywords is None:
65 keywords = {}
66 parts = len(func.args), keywords.keys()
67 func = func.func
68 if not inspect.isfunction(func):
69 raise TypeError('%r is not a Python function' % func)
70 args, varargs, varkw = inspect.getargs(func.__code__)
71 func_defaults = func.__defaults__
72 if func_defaults is None:
73 func_defaults = []
74 else:
75 func_defaults = list(func_defaults)
76 if parts[0]:
77 args = args[parts[0]:]
78 if parts[1]:
79 for arg in parts[1]:
80 i = args.index(arg) - len(args)
81 del args[i]
82 try:
83 del func_defaults[i]
84 except IndexError:
85 pass
86 return inspect.ArgSpec(args, varargs, varkw, func_defaults)
@@ -52,6 +52,7 b' try:'
52 except:
52 except:
53 ClassTypes = (type,)
53 ClassTypes = (type,)
54
54
55 from .getargspec import getargspec
55 from .importstring import import_item
56 from .importstring import import_item
56 from IPython.utils import py3compat
57 from IPython.utils import py3compat
57 from IPython.utils import eventful
58 from IPython.utils import eventful
@@ -281,85 +282,6 b' def dlink(source, *targets):'
281 return directional_link(source, *targets)
282 return directional_link(source, *targets)
282
283
283
284
284 # Alternative getargspec implementations taken from Sphinx
285 if sys.version_info >= (3, 0):
286
287 from functools import partial
288
289 def getargspec(func):
290 """Like inspect.getargspec but supports functools.partial as well."""
291 if inspect.ismethod(func):
292 func = func.__func__
293 if type(func) is partial:
294 orig_func = func.func
295 argspec = getargspec(orig_func)
296 args = list(argspec[0])
297 defaults = list(argspec[3] or ())
298 kwoargs = list(argspec[4])
299 kwodefs = dict(argspec[5] or {})
300 if func.args:
301 args = args[len(func.args):]
302 for arg in func.keywords or ():
303 try:
304 i = args.index(arg) - len(args)
305 del args[i]
306 try:
307 del defaults[i]
308 except IndexError:
309 pass
310 except ValueError: # must be a kwonly arg
311 i = kwoargs.index(arg)
312 del kwoargs[i]
313 del kwodefs[arg]
314 return inspect.FullArgSpec(args, argspec[1], argspec[2],
315 tuple(defaults), kwoargs,
316 kwodefs, argspec[6])
317 while hasattr(func, '__wrapped__'):
318 func = func.__wrapped__
319 if not inspect.isfunction(func):
320 raise TypeError('%r is not a Python function' % func)
321 return inspect.getfullargspec(func)
322 elif sys.version_info >= (2, 5):
323
324 from functools import partial
325
326 def getargspec(func):
327 """Like inspect.getargspec but supports functools.partial as well."""
328 if inspect.ismethod(func):
329 func = func.im_func
330 parts = 0, ()
331 if type(func) is partial:
332 keywords = func.keywords
333 if keywords is None:
334 keywords = {}
335 parts = len(func.args), keywords.keys()
336 func = func.func
337 if not inspect.isfunction(func):
338 raise TypeError('%r is not a Python function' % func)
339 args, varargs, varkw = inspect.getargs(func.func_code)
340 func_defaults = func.func_defaults
341 if func_defaults is None:
342 func_defaults = []
343 else:
344 func_defaults = list(func_defaults)
345 if parts[0]:
346 args = args[parts[0]:]
347 if parts[1]:
348 for arg in parts[1]:
349 i = args.index(arg) - len(args)
350 del args[i]
351 try:
352 del func_defaults[i]
353 except IndexError:
354 pass
355 if sys.version_info >= (2, 6):
356 return inspect.ArgSpec(args, varargs, varkw, func_defaults)
357 else:
358 return (args, varargs, varkw, func_defaults)
359 else:
360 getargspec = inspect.getargspec
361
362
363 #-----------------------------------------------------------------------------
285 #-----------------------------------------------------------------------------
364 # Base TraitType for all traits
286 # Base TraitType for all traits
365 #-----------------------------------------------------------------------------
287 #-----------------------------------------------------------------------------
General Comments 0
You need to be logged in to leave comments. Login now