PyTuple_Check and other type check functions didn't check the NULLpointer

Discussion in 'Python' started by NotGuru, Mar 23, 2008.

  1. NotGuru

    NotGuru Guest

    I was writing some C extensions for Python and use PyTupleType_Check
    extensively. I found that all the PySomeType_Check macros directly
    delegate the job to PyObject_TypeCheck(op, &PyType_Type). The
    PyObject_TypeCheck(op, &PyType_Type) is again a macro and defined as
    ((ob)->ob_type == (tp) || PyType_IsSubtype((ob)->ob_type, (tp)))

    in object.h.

    My questions is: is it necessary to check the null pointer in the
    macro or it's a job for the user? Semantically all the type check
    should report a false if a null pointer is encountered. I've already
    had the patch to this issue but I am not sure if I think this problem
    right. I don't know if there are some python core developers around
    but I would like to hear all opinions towards this.

    BTW, if the user didn't check null pointer before call the function, a
    segmentation fault might occur.
     
    NotGuru, Mar 23, 2008
    #1
    1. Advertising

  2. NotGuru

    John Machin Guest

    Re: PyTuple_Check and other type check functions didn't check theNULL pointer

    On Mar 24, 10:01 am, NotGuru <> wrote:
    > I was writing some C extensions for Python and use PyTupleType_Check
    > extensively. I found that all the PySomeType_Check macros directly
    > delegate the job to PyObject_TypeCheck(op, &PyType_Type). The
    > PyObject_TypeCheck(op, &PyType_Type) is again a macro and defined as
    > ((ob)->ob_type == (tp) || PyType_IsSubtype((ob)->ob_type, (tp)))
    >
    > in object.h.
    >
    > My questions is: is it necessary to check the null pointer in the
    > macro or it's a job for the user? Semantically all the type check
    > should report a false if a null pointer is encountered. I've already
    > had the patch to this issue but I am not sure if I think this problem
    > right. I don't know if there are some python core developers around
    > but I would like to hear all opinions towards this.
    >
    > BTW, if the user didn't check null pointer before call the function, a
    > segmentation fault might occur.


    You should check for null pointer returned by any C-API function that
    is meant to return an object pointer; this indicates that an exception
    has happened; your code should clean up and exit. See section 1.3 of
    the Python/C API Reference Manual.
     
    John Machin, Mar 23, 2008
    #2
    1. Advertising

  3. Re: PyTuple_Check and other type check functions didn't check theNULL pointer

    NotGuru schrieb:
    > My questions is: is it necessary to check the null pointer in the
    > macro or it's a job for the user? Semantically all the type check
    > should report a false if a null pointer is encountered. I've already
    > had the patch to this issue but I am not sure if I think this problem
    > right. I don't know if there are some python core developers around
    > but I would like to hear all opinions towards this.



    Unless stated otherwise no Py* or PY* function is NULL safe. You have to
    check for NULL unless the docs *explicitly* say it's safe to call it
    with a NULL argument.

    Christian
     
    Christian Heimes, Mar 24, 2008
    #3
  4. NotGuru

    NotGuru Guest

    Re: PyTuple_Check and other type check functions didn't check theNULL pointer

    On Mar 23, 8:24 pm, Christian Heimes <> wrote:
    > NotGuru schrieb:
    >
    > > My questions is: is it necessary to check the null pointer in the
    > > macro or it's a job for the user? Semantically all the type check
    > > should report a false if a null pointer is encountered. I've already
    > > had the patch to this issue but I am not sure if I think this problem
    > > right.  I don't know if there are some python core developers around
    > > but I would like to hear all opinions towards this.

    >
    > Unless stated otherwise no Py* or PY* function is NULL safe. You have to
    > check for NULL unless the docs *explicitly* say it's safe to call it
    > with a NULL argument.
    >
    > Christian


    Thank you Christian and John, I skipped Section 1.3 of that document,
    so shameful. ;)
     
    NotGuru, Mar 24, 2008
    #4
    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. Jack
    Replies:
    7
    Views:
    2,556
  2. Jens Mueller
    Replies:
    0
    Views:
    607
    Jens Mueller
    Apr 12, 2005
  3. VitaminB
    Replies:
    1
    Views:
    796
    Oliver Wong
    Apr 26, 2006
  4. Meikel
    Replies:
    0
    Views:
    489
    Meikel
    Jan 17, 2005
  5. FX
    Replies:
    3
    Views:
    305
    Thomas Fritsch
    Sep 12, 2006
Loading...

Share This Page