def getMethods(self): """Get all methods of this class.""" methods = [] # remove the security proxy, so that `attr` is not proxied. We could # unproxy `attr` for each turn, but that would be less efficient. # # `getPermissionIds()` also expects the class's security checker not # to be proxied. klass = removeSecurityProxy(self.context) for name, attr, iface in klass.getMethodDescriptors(): entry = {'name': name, 'signature': "(...)", 'doc': renderText(attr.__doc__ or '', inspect.getmodule(attr)), 'interface': getInterfaceInfo(iface)} entry.update(getPermissionIds(name, klass.getSecurityChecker())) methods.append(entry) for name, attr, iface in klass.getMethods(): entry = {'name': name, 'signature': getFunctionSignature(attr), 'doc': renderText(attr.__doc__ or '', inspect.getmodule(attr)), 'interface': getInterfaceInfo(iface)} entry.update(getPermissionIds(name, klass.getSecurityChecker())) methods.append(entry) return methods
def getMethods(self): """Get all methods of an object. Get a list of dicts, describing the methods of an object. The dicts support keys ``name`` (name of method), ``signature``, ``doc`` (method's docstring or ``None``) and ``interface`` (the interface, where the method was defined or ``None``). """ klass = removeSecurityProxy(self.__klass) obj = removeSecurityProxy(self.obj) for name in dir(obj): value = getattr(obj, name, None) if value is None: continue if not (inspect.ismethod(value) and not inspect.ismethoddescriptor(value)): continue if inspect.ismethod(value): signature = utilities.getFunctionSignature(value) else: signature = '(...)' entry = { 'name': name, 'signature': signature, 'doc': getattr(value, '__doc__', None), 'interface': utilities.getInterfaceForAttribute(name, klass=klass), 'value_linkable': self.isLinkable(value), } entry.update( utilities.getPermissionIds(name, getCheckerForInstancesOf(klass))) yield entry
def getMethods(self): """Get all methods of this class.""" methods = [] # remove the security proxy, so that `attr` is not proxied. We could # unproxy `attr` for each turn, but that would be less efficient. # # `getPermissionIds()` also expects the class's security checker not # to be proxied. klass = removeSecurityProxy(self.apidoc) for name, attr, iface in klass.getMethodDescriptors(): entry = { 'name': name, 'signature': "(...)", 'doc': attr.__doc__ or '', 'attr': attr, 'interface': iface } entry.update(getPermissionIds(name, klass.getSecurityChecker())) methods.append(entry) for name, attr, iface in klass.getMethods(): entry = { 'name': name, 'signature': getFunctionSignature(attr), 'doc': attr.__doc__ or '', 'attr': attr, 'interface': iface } entry.update(getPermissionIds(name, klass.getSecurityChecker())) methods.append(entry) return methods
def test_unpack_methods(self): from zope.app.apidoc.utilities import getFunctionSignature import six six.exec_("def f((a, b)): pass") self.assertEqual("((a, b))", getFunctionSignature(locals()['f']))
def getConstructor(self): """Get info about the constructor, or None if there isn't one.""" attr = self.context.getConstructor() if attr is None: return None attr = removeSecurityProxy(attr) return { 'signature': getFunctionSignature(attr), 'doc': renderText(attr.__doc__ or '', inspect.getmodule(attr)), }
def test_keyword_only_arguments(self): from zope.app.apidoc.utilities import getFunctionSignature from zope.app.apidoc.utilities import _simpleGetFunctionSignature from socket import socket try: simple_sig = _simpleGetFunctionSignature(socket.makefile) except ValueError: # On Python 3, socket.makefile has keyword args, which aren't handled # by the simple function self.assertGreater(sys.version_info, (3,0)) self.assertEqual( "(self, mode='r', buffering=None, *, encoding=None, errors=None, newline=None)", getFunctionSignature(socket.makefile)) self.assertEqual( "(mode='r', buffering=None, *, encoding=None, errors=None, newline=None)", getFunctionSignature(socket.makefile, ignore_self=True)) # Extra coverage to make sure the alternate branches are taken self.assertEqual( '()', getFunctionSignature(self.test_keyword_only_arguments)) self.assertEqual( '(self)', # Note the difference with Python 2; this is what ignore_self # is for. getFunctionSignature(TestUtilities.test_keyword_only_arguments)) else: self.assertEqual(simple_sig, "(mode='r', bufsize=-1)") # Extra coverage to make sure the alternate branches are taken self.assertEqual( '()', getFunctionSignature(self.test_keyword_only_arguments)) self.assertEqual( '()', getFunctionSignature(TestUtilities.test_keyword_only_arguments))
def getSignature(self): """See IFunctionDocumentation.""" return getFunctionSignature(self.__func)
def getSignature(self): """See :class:`~zope.app.apidoc.codemodule.interfaces.IFunctionDocumentation`.""" return getFunctionSignature(self.__func)
def test_slot_methodspypy2(self): # pragma: no cover (we don't run coverage on pypy) from zope.app.apidoc.utilities import getFunctionSignature self.assertEqual("(obj, *args, **keywords)", getFunctionSignature(object.__init__))
def test_slot_methods3(self): from zope.app.apidoc.utilities import getFunctionSignature self.assertEqual( '(self, /, *args, **kwargs)', getFunctionSignature(object.__init__))
def test_slot_methods2(self): from zope.app.apidoc.utilities import getFunctionSignature self.assertEqual("(<unknown>)", getFunctionSignature(object.__init__))