Add a new pybindgen.write_preamble() API, to write the #include <Python.h> and some other stuff needed to compile with Python < 2.5.

This commit is contained in:
Gustavo J. A. M. Carneiro
2007-06-03 23:09:11 +01:00
parent eca95f54b4
commit 56958cd448
3 changed files with 36 additions and 4 deletions
+3 -1
View File
@@ -2,12 +2,14 @@
import sys
import pybindgen
from pybindgen import (ReturnValue, Parameter, Module, Function, FileCodeSink)
from pybindgen import (CppMethod, CppConstructor, CppClass)
def my_module_gen(out_file):
print >> out_file, "#include <Python.h>"
pybindgen.write_preamble(FileCodeSink(out_file))
print >> out_file, "#include \"foo.h\""
mod = Module('foo')
+30 -1
View File
@@ -2,6 +2,35 @@
from typehandlers.base import ReturnValue, Parameter
from module import Module
from function import Function
from typehandlers.codesink import FileCodeSink
from typehandlers.codesink import CodeSink, FileCodeSink
from cppclass import CppMethod, CppClass, CppConstructor
def write_preamble(code_sink, min_python_version=(2, 3)):
"""
Write a preamble, containing includes, #define's and typedef's
necessary to correctly compile the code with the given minimum python
version.
"""
assert isinstance(code_sink, CodeSink)
assert isinstance(min_python_version, tuple)
code_sink.writeln('''
#define PY_SSIZE_T_CLEAN
#include <Python.h>
''')
if min_python_version < (2, 5):
code_sink.writeln('''
#if PY_VERSION_HEX < 0x02050000
typedef int Py_ssize_t;
# define PY_SSIZE_T_MAX INT_MAX
# define PY_SSIZE_T_MIN INT_MIN
typedef inquiry lenfunc;
typedef intargfunc ssizeargfunc;
typedef intobjargproc ssizeobjargproc;
#endif
''')
+3 -2
View File
@@ -2,7 +2,7 @@
import sys
import pybindgen
from pybindgen import typehandlers
from pybindgen.typehandlers import codesink
from pybindgen.typehandlers.base import Parameter, ReturnValue
@@ -21,7 +21,8 @@ class MyReverseWrapper(typehandlers.base.ReverseWrapperBase):
def test():
print "#include <Python.h>"
pybindgen.write_preamble(codesink.FileCodeSink(sys.stdout))
print
print "#include <string>"
print