Bug in PyCallable_Check

Discussion in 'Python' started by Arkon, May 3, 2004.

  1. Arkon

    Arkon Guest

    Hello,
    I encountered in a bug (or so I think) in the function
    PyCallable_Check, Python C API.

    The problem is that in Python code you can pass to a function that
    requests a function(pointer), types.FunctionType (Maybe other types
    too, didn't try though) which is in fact a type-type.

    And then the PyCallable_Check won't *detect* that it's not a real
    function(pointer) passed...then later when you intend to run that
    function, you get an error.

    Python:
    import types
    func(types.FunctionType)

    # instead of
    def f():
    print "bug?!"
    func(f)

    C:
    PyObject* module_func(PyObject* pSelf, PyObject* pArgs)
    {
    PyObject* CallbackFunction = NULL;
    if (!PyArg_ParseTuple(pArgs, "O", &CallbackFunction)) return NULL;
    // FLAW: NEXT IF STATEMENT WON'T DETECT IT'S A TYPE-TYPE AND NOT A
    REAL FUNCTION!
    if (!PyCallable_Check(CallbackFunction)) ...
    ....
    ....
    ....
    }

    I hope I made myself clear with this point.
    Let me know what you think.
    Arkon,
    http://www.ragestorm.net
     
    Arkon, May 3, 2004
    #1
    1. Advertising

  2. Am Montag, 3. Mai 2004 13:39 schrieb Arkon:
    > And then the PyCallable_Check won't *detect* that it's not a real
    > function(pointer) passed...then later when you intend to run that
    > function, you get an error.


    PyCallable_Check does as it is told, it checks whether the argument can be
    called.

    There are loads of callable things in Python, method references being just one
    of them. A class is callable (instantiation or overridden __metaclass__ or
    the like), an instance is callable (using the __call__ method of the class),
    and many other things too. What you're seeing (complaining about) is that
    types are nothing else than classes, and so they are callable too.

    If you really have to check that it is a function, you'll have to check the
    type of the reference for types.FunctionType.

    HTH!

    Heiko.
     
    Heiko Wundram, May 3, 2004
    #2
    1. Advertising

  3. Arkon wrote:

    > Hello,
    > I encountered in a bug (or so I think) in the function
    > PyCallable_Check, Python C API.
    >
    > The problem is that in Python code you can pass to a function that
    > requests a function(pointer), types.FunctionType (Maybe other types
    > too, didn't try though) which is in fact a type-type.
    >
    > And then the PyCallable_Check won't *detect* that it's not a real
    > function(pointer) passed...then later when you intend to run that
    > function, you get an error.


    FunctionType _is_ callable - in fact it can be used as a constructor:

    >>> import types
    >>> co = compile("print 1", "<string>", "exec")
    >>> f = types.FunctionType(co, locals())
    >>> f()

    1
    >>>


    So basically PyCallable_Check works properly. And as python is a
    dynamically typed language, I don't see how you can actually restrict the
    type of values passed beyond that callable-check.

    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, May 3, 2004
    #3
  4. Arkon

    Arkon Guest

    Heiko Wundram <> wrote in message news:<>...
    > Am Montag, 3. Mai 2004 13:39 schrieb Arkon:
    > > And then the PyCallable_Check won't *detect* that it's not a real
    > > function(pointer) passed...then later when you intend to run that
    > > function, you get an error.

    >
    > PyCallable_Check does as it is told, it checks whether the argument can be
    > called.
    >
    > There are loads of callable things in Python, method references being just one
    > of them. A class is callable (instantiation or overridden __metaclass__ or
    > the like), an instance is callable (using the __call__ method of the class),
    > and many other things too. What you're seeing (complaining about) is that
    > types are nothing else than classes, and so they are callable too.
    >
    > If you really have to check that it is a function, you'll have to check the
    > type of the reference for types.FunctionType.
    >
    > HTH!
    >
    > Heiko.


    Well, I wasn't sure from the beginning,
    but thanks for making it clear to me, I haven't thought of it in that way!

    As for Diez B. Roggisch example
    I get "TypeError: cannot create 'function' instances"...

    Arkon
     
    Arkon, May 3, 2004
    #4
  5. Arkon wrote:

    > As for Diez B. Roggisch example
    > I get "TypeError: cannot create 'function' instances"...


    Maybe its a version issue - it works for me with Python 2.2.3+ and Python
    2.3.3.


    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, May 3, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    Re: BUG? OR NOT A BUG?

    John, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    583
  2. RedEye
    Replies:
    2
    Views:
    610
    Jason Kester
    Dec 13, 2005
  3. Michel Joly de Lotbiniere

    Bug Parade Bug 4953793

    Michel Joly de Lotbiniere, Nov 30, 2003, in forum: Java
    Replies:
    4
    Views:
    662
    Michel
    Dec 2, 2003
  4. DarkSpy
    Replies:
    4
    Views:
    910
    tom_usenet
    Jun 27, 2003
  5. Steve Holden
    Replies:
    1
    Views:
    415
    Behrang Dadsetan
    Jul 2, 2003
Loading...

Share This Page