[pypy-commit] cffi default: add an example
fijal
noreply at buildbot.pypy.org
Sun Jul 22 17:47:16 CEST 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch:
Changeset: r675:8e54d88832ca
Date: 2012-07-22 17:32 +0200
http://bitbucket.org/cffi/cffi/changeset/8e54d88832ca/
Log: add an example
diff --git a/doc/source/index.rst b/doc/source/index.rst
--- a/doc/source/index.rst
+++ b/doc/source/index.rst
@@ -247,7 +247,6 @@
The actual function calls should be obvious. It's like C.
-
=======================================================
Distributing modules using CFFI
@@ -620,6 +619,44 @@
it all the time.
+An example of calling a main-like thing
+---------------------------------------
+
+Imagine we have something like this:
+
+.. code-block:: python
+
+ from cffi import FFI
+ ffi = FFI()
+ ffi.cdef("""
+ int main_like(int argv, char *argv[]);
+ """)
+
+Now, everything is simple, except, how do we create ``char**`` argument here?
+The first idea:
+
+.. code-block:: python
+
+ argv = ffi.new("char *[]", ["arg0", "arg1"])
+
+Does not work, because the initializer receives python ``str`` instead of
+``char*``. Now, the following would work:
+
+.. code-block:: python
+
+ argv = ffi.new("char *[]", [ffi.new("char[]", "xyz")])
+
+However, the "xyz" string will not be automatically kept alive. Instead
+we need to make sure that the list is stored somewhere for long enough.
+For example:
+
+.. code-block:: python
+
+ argv_keepalive = [ffi.new("char[]", "xyz")]
+ argv = ffi.new("char *[]", argv_keepalive)
+
+would work.
+
Function calls
--------------
More information about the pypy-commit
mailing list