<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>1.10.4 NULL Pointers
</TITLE>
<META NAME="description" CONTENT="1.10.4 NULL Pointers
">
<META NAME="keywords" CONTENT="ext">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="ext.css">
<LINK REL="previous" HREF="thinIce.html">
<LINK REL="up" HREF="refcounts.html">
<LINK REL="next" HREF="cplusplus.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="cplusplus.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="refcounts.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="thinIce.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Extending and Embedding the Python Interpreter</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="cplusplus.html">1.11 Writing Extensions in</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="refcounts.html">1.10 Reference Counts</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="thinIce.html">1.10.3 Thin Ice</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION0031040000000000000000"></A>
<BR>
1.10.4 NULL Pointers
            
</H2>

<P>
In general, functions that take object references as arguments do not
expect you to pass them <tt>NULL</tt> pointers, and will dump core (or
cause later core dumps) if you do so.  Functions that return object
references generally return <tt>NULL</tt> only to indicate that an
exception occurred.  The reason for not testing for <tt>NULL</tt>
arguments is that functions often pass the objects they receive on to
other function -- if each function were to test for <tt>NULL</tt>,
there would be a lot of redundant tests and the code would run slower.

<P>
It is better to test for <tt>NULL</tt> only at the ``source'', i.e. when a
pointer that may be <tt>NULL</tt> is received, e.g. from
<tt class=cfunction>malloc()</tt> or from a function that may raise an exception.

<P>
The macros <tt class=cfunction>Py_INCREF()</tt> and <tt class=cfunction>Py_DECREF()</tt>
do not check for <tt>NULL</tt> pointers -- however, their variants
<tt class=cfunction>Py_XINCREF()</tt> and <tt class=cfunction>Py_XDECREF()</tt> do.

<P>
The macros for checking for a particular object type
(<tt>Py<var>type</var>_Check()</tt>) don't check for <tt>NULL</tt> pointers --
again, there is much code that calls several of these in a row to test
an object against various different expected types, and this would
generate redundant tests.  There are no variants with <tt>NULL</tt>
checking.

<P>
The C function calling mechanism guarantees that the argument list
passed to C functions (<tt>args</tt> in the examples) is never
<tt>NULL</tt> -- in fact it guarantees that it is always a tuple.<A NAME="tex2html4"
 HREF="#foot559"><SUP>1.4</SUP></A>
<P>
It is a severe error to ever let a <tt>NULL</tt> pointer ``escape'' to
the Python user.  

<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot559">... tuple.</A><A NAME="foot559"
 HREF="nullPointers.html#tex2html4"><SUP>1.4</SUP></A>
<DD>
These guarantees don't hold when you use the ``old'' style
calling convention -- this is still found in much existing code.

</DL><p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="cplusplus.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="refcounts.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="thinIce.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Extending and Embedding the Python Interpreter</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="cplusplus.html">1.11 Writing Extensions in</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="refcounts.html">1.10 Reference Counts</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="thinIce.html">1.10.3 Thin Ice</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
