~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Doc/extending/extending.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
The compilation of an extension module depends on its intended use as well as on
21
21
your system setup; details are given in later chapters.
22
22
 
 
23
Do note that if your use case is calling C library functions or system calls,
 
24
you should consider using the :mod:`ctypes` module rather than writing custom
 
25
C code. Not only does :mod:`ctypes` let you write Python code to interface
 
26
with C code, but it is more portable between implementations of Python than
 
27
writing and compiling an extension module which typically ties you to CPython.
 
28
 
 
29
 
23
30
 
24
31
.. _extending-simpleexample:
25
32
 
1219
1226
   static int
1220
1227
   import_spam(void)
1221
1228
   {
1222
 
       PyObject *module = PyImport_ImportModule("spam");
1223
 
 
1224
 
       if (module != NULL) {
1225
 
           PyObject *c_api_object = PyObject_GetAttrString(module, "_C_API");
1226
 
           if (c_api_object == NULL)
1227
 
               return -1;
1228
 
           if (PyCObject_Check(c_api_object))
1229
 
               PySpam_API = (void **)PyCObject_AsVoidPtr(c_api_object);
1230
 
           Py_DECREF(c_api_object);
 
1229
       PyObject *c_api_object;
 
1230
       PyObject *module;
 
1231
 
 
1232
       module = PyImport_ImportModule("spam");
 
1233
       if (module == NULL)
 
1234
           return -1;
 
1235
 
 
1236
       c_api_object = PyObject_GetAttrString(module, "_C_API");
 
1237
       if (c_api_object == NULL) {
 
1238
           Py_DECREF(module);
 
1239
           return -1;
1231
1240
       }
 
1241
       if (PyCObject_Check(c_api_object))
 
1242
           PySpam_API = (void **)PyCObject_AsVoidPtr(c_api_object);
 
1243
 
 
1244
       Py_DECREF(c_api_object);
 
1245
       Py_DECREF(module);
1232
1246
       return 0;
1233
1247
   }
1234
1248