pycurious/fix
changeset 1:1f54edaaab02 tip
Added SWIG typemap example.
| author | James Brotchie <brotchie@gmail.com> |
|---|---|
| date | Fri Dec 04 22:50:30 2009 +1000 (2 years ago) |
| parents | f7a26febb785 |
| children | |
| files | basic/example.py basic/setup.py basic/test.c basic/test.h basic/test.i |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/basic/example.py Fri Dec 04 22:50:30 2009 +1000 1.3 @@ -0,0 +1,19 @@ 1.4 +""" 1.5 +Example to exercise the test SWIG module. 1.6 + 1.7 +""" 1.8 +import os 1.9 +import test 1.10 + 1.11 +# Create and open pipe as file objects 1.12 +(r,w) = os.pipe() 1.13 +fr, fw = os.fdopen(r, 'r'), os.fdopen(w, 'w') 1.14 + 1.15 +# Write and close 1.16 +fw.write('Hello World') 1.17 +fw.close() 1.18 + 1.19 +# Read data from pipe using test module 1.20 +print test.message(fr) 1.21 + 1.22 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/basic/setup.py Fri Dec 04 22:50:30 2009 +1000 2.3 @@ -0,0 +1,13 @@ 2.4 +#!/usr/bin/env python 2.5 +from distutils.core import setup, Extension 2.6 + 2.7 +test_module = Extension('_test', 2.8 + sources=['test.c', 'test.i']) 2.9 + 2.10 +setup (name = 'test', 2.11 + version = '0.1', 2.12 + author = "James Brotchie <brotchie@gmail.com>", 2.13 + description = """Example SWIG wrapper using a typemap""", 2.14 + ext_modules = [test_module], 2.15 + py_modules = ["test"], 2.16 + )
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/basic/test.c Fri Dec 04 22:50:30 2009 +1000 3.3 @@ -0,0 +1,11 @@ 3.4 +#include "test.h" 3.5 + 3.6 +char buffer[1024]; 3.7 + 3.8 +char* 3.9 +message(FILE *input) 3.10 +{ 3.11 + memset(buffer, 0, sizeof(buffer)); 3.12 + fread(buffer, sizeof(buffer), 1, input); 3.13 + return buffer; 3.14 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/basic/test.h Fri Dec 04 22:50:30 2009 +1000 4.3 @@ -0,0 +1,9 @@ 4.4 +#ifndef __INCLUDE_TEST_H__ 4.5 +#define __INCLUDE_TEST_H__ 4.6 + 4.7 +#include <stdio.h> 4.8 +#include <string.h> 4.9 + 4.10 +char *message(FILE *input); 4.11 + 4.12 +#endif /* __INCLUDE_TEST_H__ */
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/basic/test.i Fri Dec 04 22:50:30 2009 +1000 5.3 @@ -0,0 +1,16 @@ 5.4 +%module test 5.5 +%{ 5.6 +#include "test.h" 5.7 +%} 5.8 + 5.9 +/* Converts a PyFile instance to a stdio FILE* */ 5.10 +%typemap(in) FILE* { 5.11 + if ( PyFile_Check($input) ){ 5.12 + $1 = PyFile_AsFile($input); 5.13 + } else { 5.14 + PyErr_SetString(PyExc_TypeError, "$1_name must be a file type."); 5.15 + return NULL; 5.16 + } 5.17 +} 5.18 + 5.19 +char *message(FILE *input);
