470,624 Members | 2,472 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,624 developers. It's quick & easy.

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

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.

Mar 23 '08 #1
2 2000
On Mar 24, 10:01 am, NotGuru <xu.math...@gmail.comwrote:
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.
Mar 23 '08 #2
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

Mar 24 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Catherine Lynn Smith | last post: by
5 posts views Thread by Rick | last post: by
10 posts views Thread by Giovanni Bajo | last post: by
20 posts views Thread by elderic | last post: by
5 posts views Thread by Fei Liu | last post: by
19 posts views Thread by zz12 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.