[Python-Dev] [Python-3000] Questions on optional type annotations

Neal Norwitz nnorwitz at gmail.com
Fri May 12 09:30:11 CEST 2006


On 5/11/06, Edward Loper <edloper at gradient.cis.upenn.edu> wrote:
> Neal Norwitz wrote:
> > Another benefit of this is the ability to get more info through
> > introspection.  Right now, you can't even find the number of arguments
> > of a function implemented in C.  You only know if it takes 0, 1, or
> > variable # of arguments and if it accepts keywords.
>
> By this, do you mean that it's currently possible to distinguish these
> cases (builtins that take 0 args, builtins that take 1 arg, builtins
> that take varargs, builtins that take keywords) via introspection in
> python?  If so, could you let me know how this is done?  For epydoc,
> this would at least let me give a little more info when no signature is
> present on the first line of the docstring.

Hmm, well I only said the info was available.  I didn't say it was
available from Python code. :-)

Actually, with the patch below the flags are available:

>>> len.__flags__
8

The flags are METH_XXX flags defined in Include/methodobject.h:

#define METH_OLDARGS  0x0000
#define METH_VARARGS  0x0001
#define METH_KEYWORDS 0x0002
#define METH_NOARGS   0x0004
#define METH_O        0x0008

If you want this patch included in 2.5, feel free to ask on
python-dev.  I guess I'm +0 on it.  This could be a trivial extension
module for older versions of python.  Aaaa, screw it, I copied
python-dev, we'll see if anyone cares.

n
--
Index: Objects/methodobject.c
===================================================================
--- Objects/methodobject.c      (revision 45961)
+++ Objects/methodobject.c      (working copy)
@@ -146,6 +146,12 @@
        return PyString_FromString(m->m_ml->ml_name);
 }

+static PyObject *
+meth_get__flags__(PyCFunctionObject *m, void *closure)
+{
+       return PyInt_FromLong(m->m_ml->ml_flags);
+}
+
 static int
 meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
 {
@@ -174,6 +180,7 @@
        {"__doc__",  (getter)meth_get__doc__,  NULL, NULL},
        {"__name__", (getter)meth_get__name__, NULL, NULL},
        {"__self__", (getter)meth_get__self__, NULL, NULL},
+       {"__flags__", (getter)meth_get__flags__, NULL, NULL},
        {0}
 };


More information about the Python-Dev mailing list