Ошибка в коде экспорта вложенного пространства имен boost-python - PullRequest
0 голосов
/ 31 января 2019

Я работаю над экспортом двух классов: класс zoo (пространства имен extzoo и intzoo), class animal (пространства имен extanim и intanim) », которые определены в двухуровневых пространствах имен, вложенных в один уровень. Я хочу представить эти методы интерпретатору Python, откуда яя могу получить к ним доступ. Я написал код, создал общую библиотеку, но когда я импортирую ее в python, я получаю сообщение об ошибке. Буду признателен за ваши рекомендации по этому вопросу.

Я следовал ответу, приведенному по следующей ссылкев аналогичном контексте предоставления вложенных пространств имен C ++: создание вложенного пространства имен boost-python

zoo.h:

namespace extzoo
{
namespace intzoo
{
class zoo
{
public:
const std::string hello_zoo();
const std::string getname_zoo();
};
}
}      

zoo.cpp:

using namespace extzoo;
using namespace intzoo;

const std::string zoo::hello_zoo() {
     return std::string("hello, zoo");
}

const std::string zoo::getname_zoo() {
      std::string input;
      std::cout<<"Please enter your favorit zoo name: ";
      std::getline(std::cin,input);
      return std::string("Your favorit zoo name is: ").append(input);
}

animal.h:

namespace extanim
{
namespace intanim
{
class animal
{
public:
const std::string hello_animal();     
const std::string getname_animal();
};
}
}

animal.cpp:

using namespace extanim;
using namespace intanim;

const std::string animal::hello_animal() {
      return std::string("hello, animal");
}
const std::string animal::getname_animal() {
      std::string input;
      std::cout<<"Please enter your favorit animal name: ";
      std::getline(std::cin,input);
      return std::string("Your favorit animal name is: ").append(input);
     }

pyintf.cpp:

// An established convention for using boost.python.
using namespace boost::python;
//user defined ns
using namespace extzoo::intzoo;
using namespace extzoo;
using namespace extanim::intanim;
using namespace extanim;

class DummyZoo{};
class DummyAnimal{};

BOOST_PYTHON_MODULE(pyintf) {

    scope intzoo
    = class_<DummyZoo>("intzoo");

    class_<extzoo::intzoo::zoo>("zoo")
    // Expose the function hello_zoo().
       .def("hello_zoo", &extzoo::intzoo::zoo::hello_zoo)
        // Expose the function getname_zoo().    
        .def("getname_zoo", &extzoo::intzoo::zoo::getname_zoo)
        ;



    scope intanim
        = class_<DummyAnimal>("intanim"); 

    class_<extanim::intanim::animal>("animal")
        // Expose the function hello_animal().
        .def("hello_animal", &extanim::intanim::animal::hello_animal)
        // Expose the function getname_animal().    
        .def("getname_animal", &extanim::intanim::animal::getname_animal)
        ;
}

Я скомпилировал приведенный выше код, используя следующиекоманда:

g++ -shared -o pyintf.so -fPIC pyintf.cpp zoo.h zoo.cpp animal.h animal.cpp -lboost_python -lpython2.7 -I/usr/include/python2.7

Я получаю сообщение об ошибке при импорте общей библиотеки

import pyintf Traceback (последний вызов был последним): File "", строка1, в ImportError: ./pyintf.so: unопределенный символ: _ZN7extanim7intanim6animal14getname_animalEv

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Я изменил код, основываясь на предложении @doqtor.Однако теперь я не могу импортировать в Python пространство имен 'intanim', определенное в одном файле, в то время как другой может 'intzoo'.Как вы можете видеть ниже.

>>> import pyintf 
>>> pyintf.intanim.animal().hello_animal()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'intanim'
>>> from pyintf import intanim
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name intanim
>>> from pyintf import extanim
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name extanim
>>> from pyintf import intzoo
>>> intzoo.zoo().hello_zoo()
'hello, zoo'

1 Ответ

0 голосов
/ 01 февраля 2019

В ваших файлах cpp вы должны поместить определения методов класса в соответствующие пространства имен вместо объявлений using namespace ....Например, для zoo.cpp это будет:

namespace extzoo
{
namespace intzoo
{
const std::string zoo::hello_zoo() {
// ... code
}

const std::string zoo::getname_zoo() {
// ... code
}
}
}

--------------------------------------------- Обновление ------------------------------------------------------------------------------

Добавление области в коде c ++ для разделения определений области Python с помощью {}:

BOOST_PYTHON_MODULE(pyintf) { // set scope to pyintf

{
    scope intzoo
    = class_<DummyZoo>("intzoo"); // define and set scope to pyintf.intzoo

    class_<extzoo::intzoo::zoo>("zoo")
    // Expose the function hello_zoo().
       .def("hello_zoo", &extzoo::intzoo::zoo::hello_zoo)
        // Expose the function getname_zoo().    
        .def("getname_zoo", &extzoo::intzoo::zoo::getname_zoo)
        ;
} // revert scope to pyintf


{
    scope intanim
        = class_<DummyAnimal>("intanim"); // define and set scope to pyintf.intanim

    class_<extanim::intanim::animal>("animal")
        // Expose the function hello_animal().
        .def("hello_animal", &extanim::intanim::animal::hello_animal)
        // Expose the function getname_animal().    
        .def("getname_animal", &extanim::intanim::animal::getname_animal)
        ;
} // revert scope to pyintf

}

и теста:

>>> import pyintf
>>> dir(pyintf)
['__doc__', '__file__', '__name__', '__package__', 'intanim', 'intzoo']
>>> pyintf.intanim.animal().hello_animal()
'hello, animal'
>>> from pyintf import intanim
>>> from pyintf import extanim
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name extanim
>>> from pyintf import intzoo
>>> 

Обратите внимание, что ошибка при импорте extanim ожидается, так как область действия такого имени не определена.

...