Your IP : 216.73.216.48


Current Path : /usr/doc/sip-4.18/html/
Upload File :
Current File : //usr/doc/sip-4.18/html/embedding.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Using the C API when Embedding &mdash; SIP 4.18 Reference Guide</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '4.18',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="shortcut icon" href="_static/logo_tn.ico"/>
    <link rel="top" title="SIP 4.18 Reference Guide" href="index.html" />
    <link rel="next" title="Python API for Applications" href="python_api.html" />
    <link rel="prev" title="C API for Handwritten Code" href="c_api.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="python_api.html" title="Python API for Applications"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="c_api.html" title="C API for Handwritten Code"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">SIP 4.18 Reference Guide</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="using-the-c-api-when-embedding">
<h1>Using the C API when Embedding<a class="headerlink" href="#using-the-c-api-when-embedding" title="Permalink to this headline">¶</a></h1>
<p>The <a class="reference internal" href="c_api.html#ref-c-api"><span class="std std-ref">C API</span></a> is intended to be called from handwritten code in
SIP generated modules.  However it is also often necessary to call it from C or
C++ applications that embed the Python interpreter and need to pass C or C++
instances between the application and the interpreter.</p>
<p>The API is exported by the SIP module as a <code class="docutils literal"><span class="pre">sipAPIDef</span></code> data structure
containing a set of function pointers.  The data structure is defined in the
SIP header file <code class="docutils literal"><span class="pre">sip.h</span></code>.  When using Python v2.7, or Python v3.1 or later the
data structure is wrapped as a Python <code class="docutils literal"><span class="pre">PyCapsule</span></code> object.  When using other
versions of Python the data structure is wrapped as a Python <code class="docutils literal"><span class="pre">PyCObject</span></code>
object.  It is referenced by the name <code class="docutils literal"><span class="pre">_C_API</span></code> in the SIP module dictionary.</p>
<p>Each member of the data structure is a pointer to one of the functions of the
SIP API.  The name of the member can be derived from the function name by
replacing the <code class="docutils literal"><span class="pre">sip</span></code> prefix with <code class="docutils literal"><span class="pre">api</span></code> and converting each word in the
name to lower case and preceding it with an underscore.  For example:</p>
<blockquote>
<div><p><code class="docutils literal"><span class="pre">sipExportSymbol</span></code> becomes <code class="docutils literal"><span class="pre">api_export_symbol</span></code></p>
<p><code class="docutils literal"><span class="pre">sipWrapperCheck</span></code> becomes <code class="docutils literal"><span class="pre">api_wrapper_check</span></code></p>
</div></blockquote>
<p>Note that the type objects that SIP generates for a wrapped module (see
<a class="reference internal" href="c_api.html#ref-type-structures"><span class="std std-ref">Generated Type Structures</span></a>, <a class="reference internal" href="c_api.html#ref-enum-type-objects"><span class="std std-ref">Generated Named Enum Type Objects</span></a> and
<a class="reference internal" href="c_api.html#ref-exception-objects"><span class="std std-ref">Generated Exception Objects</span></a>) cannot be refered to directly and must be
obtained using the <a class="reference internal" href="c_api.html#c.sipFindType" title="sipFindType"><code class="xref c c-func docutils literal"><span class="pre">sipFindType()</span></code></a> function.  Of course, the
corresponding modules must already have been imported into the interpreter.</p>
<p>The following code fragment shows how to get a pointer to the <code class="docutils literal"><span class="pre">sipAPIDef</span></code>
data structure:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>#include &lt;sip.h&gt;

const sipAPIDef *get_sip_api()
{
#if defined(SIP_USE_PYCAPSULE)
    return (const sipAPIDef *)PyCapsule_Import(&quot;sip._C_API&quot;, 0);
#else
    PyObject *sip_module;
    PyObject *sip_module_dict;
    PyObject *c_api;

    /* Import the SIP module. */
    sip_module = PyImport_ImportModule(&quot;sip&quot;);

    if (sip_module == NULL)
        return NULL;

    /* Get the module&#39;s dictionary. */
    sip_module_dict = PyModule_GetDict(sip_module);

    /* Get the &quot;_C_API&quot; attribute. */
    c_api = PyDict_GetItemString(sip_module_dict, &quot;_C_API&quot;);

    if (c_api == NULL)
        return NULL;

    /* Sanity check that it is the right type. */
    if (!PyCObject_Check(c_api))
        return NULL;

    /* Get the actual pointer from the object. */
    return (const sipAPIDef *)PyCObject_AsVoidPtr(c_api);
#endif
}
</pre></div>
</div>
<p>The use of <a class="reference internal" href="c_api.html#c.SIP_USE_PYCAPSULE" title="SIP_USE_PYCAPSULE"><code class="xref c c-macro docutils literal"><span class="pre">SIP_USE_PYCAPSULE</span></code></a> means that code will run under all
versions of Python.</p>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/logo.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="c_api.html"
                        title="previous chapter">C API for Handwritten Code</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="python_api.html"
                        title="next chapter">Python API for Applications</a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="python_api.html" title="Python API for Applications"
             >next</a> |</li>
        <li class="right" >
          <a href="c_api.html" title="C API for Handwritten Code"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">SIP 4.18 Reference Guide</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2015 Riverbank Computing Limited.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.
    </div>
  </body>
</html>