Add to unit tests APIs involding PyObject*, in order to test this type handler

This commit is contained in:
Gustavo J. A. M. Carneiro
2008-08-20 21:37:05 +01:00
parent 3c15d022a9
commit a92f6868cc
6 changed files with 58 additions and 12 deletions
+3 -2
View File
@@ -582,7 +582,7 @@ class ModuleParser(object):
return False
def parse(self, header_files, include_paths=None, whitelist_paths=None, includes=(),
pygen_sink=None, pygen_classifier=None):
pygen_sink=None, pygen_classifier=None, gccxml_options=None):
"""
parses a set of header files and returns a pybindgen Module instance.
It is equivalent to calling the following methods:
@@ -594,7 +594,8 @@ class ModuleParser(object):
The documentation for L{ModuleParser.parse_init} explains the parameters.
"""
self.parse_init(header_files, include_paths, whitelist_paths, includes, pygen_sink, pygen_classifier)
self.parse_init(header_files, include_paths, whitelist_paths, includes, pygen_sink,
pygen_classifier, gccxml_options)
self.scan_types()
self.scan_methods()
self.scan_functions()
+26 -3
View File
@@ -2,6 +2,7 @@
#ifndef FOO_H_
# define FOO_H_
#include <Python.h>
#include <string>
#include <iostream>
#include <sstream>
@@ -227,6 +228,8 @@ private:
Zbr *m_zbr;
Zbr *m_internal_zbr;
PyObject *m_pyobject;
public:
static std::string staticData;
@@ -245,7 +248,8 @@ public:
SomeObject (std::string const prefix)
: m_prefix (prefix), m_foo_ptr (0),
m_foo_shared_ptr (0), m_zbr (0),
m_internal_zbr (new Zbr)
m_internal_zbr (new Zbr),
m_pyobject (NULL)
{
SomeObject::instance_count++;
}
@@ -253,7 +257,8 @@ public:
SomeObject (int prefix_len)
: m_prefix (prefix_len, 'X'), m_foo_ptr (0),
m_foo_shared_ptr (0), m_zbr (0),
m_internal_zbr (new Zbr)
m_internal_zbr (new Zbr),
m_pyobject (NULL)
{
SomeObject::instance_count++;
}
@@ -303,6 +308,25 @@ public:
return out.str ();
}
// -#- @pyobject(transfer_ownership=false) -#-
virtual void set_pyobject (PyObject *pyobject) {
if (m_pyobject) {
Py_DECREF(m_pyobject);
}
Py_INCREF(pyobject);
m_pyobject = pyobject;
}
// -#- @return(caller_owns_return=true) -#-
virtual PyObject* get_pyobject (void) {
if (m_pyobject) {
Py_INCREF(m_pyobject);
return m_pyobject;
} else {
return NULL;
}
}
// pass by value, direction=in
void set_foo_value (Foo foo) {
m_foo_value = foo;
@@ -760,5 +784,4 @@ private:
SimpleStructList m_simpleList;
};
#endif /* !FOO_H_ */
+10 -4
View File
@@ -24,14 +24,20 @@ class MyPygenClassifier(PygenClassifier):
def my_module_gen():
pygen = [
PygenSection('__main__', FileCodeSink(open(sys.argv[2], "wt"))),
PygenSection('foomodulegen_module1', FileCodeSink(open(sys.argv[3], "wt")),
PygenSection('__main__', FileCodeSink(open(sys.argv[3], "wt"))),
PygenSection('foomodulegen_module1', FileCodeSink(open(sys.argv[4], "wt")),
'foomodulegen_module1_local'),
PygenSection('foomodulegen_module2', FileCodeSink(open(sys.argv[4], "wt")),
PygenSection('foomodulegen_module2', FileCodeSink(open(sys.argv[5], "wt")),
'foomodulegen_module2_local'),
]
module_parser = ModuleParser('foo4', '::')
module_parser.parse([sys.argv[1]], includes=['"foo.h"'], pygen_sink=pygen, pygen_classifier=MyPygenClassifier())
gccxml_options = dict(
include_paths=eval(sys.argv[2]),
)
module_parser.parse([sys.argv[1]], includes=['"foo.h"'], pygen_sink=pygen, pygen_classifier=MyPygenClassifier(),
gccxml_options=gccxml_options)
for sect in pygen:
sect.code_sink.file.close()
+10 -3
View File
@@ -1,7 +1,7 @@
#! /usr/bin/env python
import sys
import re
import os
import pybindgen
from pybindgen.typehandlers import base as typehandlers
@@ -15,9 +15,16 @@ import foomodulegen_common
def my_module_gen():
out = FileCodeSink(sys.stdout)
pygen_file = open(sys.argv[2], "wt")
pygen_file = open(sys.argv[3], "wt")
module_parser = ModuleParser('foo2', '::')
module = module_parser.parse([sys.argv[1]], includes=['"foo.h"'], pygen_sink=FileCodeSink(pygen_file))
print >> sys.stderr, "PYTHON_INCLUDES:", repr(sys.argv[2])
gccxml_options = dict(
include_paths=eval(sys.argv[2]),
)
module = module_parser.parse([sys.argv[1]], includes=['"foo.h"'], pygen_sink=FileCodeSink(pygen_file),
gccxml_options=gccxml_options)
pygen_file.close()
foomodulegen_common.customize_module(module)
+7
View File
@@ -155,6 +155,13 @@ int %s::custom_method_added_by_a_hook(int x)
[Parameter.new('int', 'x')],
is_virtual=True, is_const=True)
SomeObject.add_method('set_pyobject', None,
[Parameter.new('PyObject*', 'pyobject', transfer_ownership=False)],
is_virtual=True)
SomeObject.add_method('get_pyobject',
ReturnValue.new('PyObject*', caller_owns_return=True),
[],
is_virtual=True)
## add a function that appears as a method of an object
SomeObject.add_function_as_method('some_object_get_something_prefixed',
+2
View File
@@ -69,6 +69,7 @@ def build(bld):
DEPRECATION_ERRORS,
bindgen.input_file('foomodulegen-auto.py'),
bindgen.input_file('foo.h'),
repr(bindgen.env['CPPPATH_PYEXT']),
bindgen.output_file('foomodulegen_generated.py'),
]
bindgen.command = bindgen.env['PYTHON']
@@ -108,6 +109,7 @@ def build(bld):
DEPRECATION_ERRORS,
bindgen.input_file('foomodulegen-auto-split.py'),
bindgen.input_file('foo.h'),
repr(bindgen.env['CPPPATH_PYEXT']),
bindgen.output_file('foomodulegen_split.py'),
bindgen.output_file('foomodulegen_module1.py'),
bindgen.output_file('foomodulegen_module2.py'),