]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
move callable() here
authorGuido van Rossum <guido@python.org>
Thu, 26 Jan 1995 00:38:22 +0000 (00:38 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 26 Jan 1995 00:38:22 +0000 (00:38 +0000)
Objects/object.c

index 63c55ce2ad726e55de5ef00f871e8502a26375c7..73353e44691b959379db7842504692f4227a516a 100644 (file)
@@ -386,6 +386,35 @@ coerce(pv, pw)
 }
 
 
+/* Test whether an object can be called */
+
+int
+callable(x)
+       object *x;
+{
+       if (x == NULL)
+               return 0;
+       if (x->ob_type->tp_call != NULL ||
+           is_funcobject(x) ||
+           is_instancemethodobject(x) ||
+           is_methodobject(x) ||
+           is_classobject(x))
+               return 1;
+       if (is_instanceobject(x)) {
+               object *call = getattr(x, "__call__");
+               if (call == NULL) {
+                       err_clear();
+                       return 0;
+               }
+               /* Could test recursively but don't, for fear of endless
+                  recursion if some joker sets self.__call__ = self */
+               DECREF(call);
+               return 1;
+       }
+       return 0;
+}
+
+
 /*
 NoObject is usable as a non-NULL undefined value, used by the macro None.
 There is (and should be!) no way to create other objects of this type,