Qualified class names are a problem?

Asked by Paul Melis

Given classes.h:

#include <string>

namespace mynamespace {

class Module
{
public:
    Module (std::string s) {}
};

class ModuleCreator
{
public:
    static Module* create() { return new Module("doh"); }
};

}

and a module to wrap these classes:

#! /usr/bin/env python
import sys

import pybindgen
from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink
from pybindgen.utils import retval

def my_module_gen(out_file):
    mod = Module('blah')
    mod.add_include('"classes.h"')

    c_Module = mod.add_class('mynamespace::Module', python_name='Module')
    c_Module.add_constructor([Parameter.new('std::string', 's')])

    c_ModuleCreator = mod.add_class('mynamespace::ModuleCreator', python_name='ModuleCreator')
    c_ModuleCreator.add_method('create', retval('mynamespace::Module *', caller_owns_return=True), [], is_static=True)
    mod.generate(FileCodeSink(out_file))

if __name__ == '__main__':
    my_module_gen(sys.stdout)

The generated module doesn't compile, as the fully-qualified class names are used in variable names. Relevant piece of generated module code:

PyObject *
_wrap_PyMynamespaceModuleCreator_create(void)
{
    PyObject *py_retval;
    mynamespace::Module *retval;
    PyMynamespaceModule *py_mynamespace::Module;

    retval = mynamespace::ModuleCreator::create();
    if (!(retval)) {
        Py_INCREF(Py_None);
        return Py_None;
    }
    py_mynamespace::Module = PyObject_New(PyMynamespaceModule, &PyMynamespaceModule_Type);
    py_mynamespace::Module->obj = retval;
    py_retval = Py_BuildValue((char *) "N", py_mynamespace::Module);
    return py_retval;
}

I don't want to add a submodule, so is there a way to accomplish what I want? (other than including a "using mynamespace;" line)

Question information

Language:
English Edit question
Status:
Solved
For:
PyBindGen Edit question
Assignee:
No assignee Edit question
Solved by:
Gustavo Carneiro
Solved:
Last query:
Last reply:
Revision history for this message
Best Gustavo Carneiro (gjc) said :
#1

You are doing it wrong. Register the module as:

    mod = Module('blah', cpp_namespace='mynamespace')

then you can register classes in that module/namespace and omit the namespace:

    c_Module = mod.add_class('Module')

Revision history for this message
Paul Melis (paul-floorball-flamingos) said :
#2

Thanks for the clearification!