1

This is the first time I post a question here, so I hope, I am doing it correctly otherwise please let me know.

I am trying to make a wrapper with c++ and call a python module. I am able to call a simple python module, however, as soon as I include a python module, which imports modules, I get a segmentation fault.

I import the code as, where I use a helper class:

setenv("PYTHONPATH", "./AugmentedAutoencoder/auto_pose/test", 0);

    CPyInstance hInstance;  
    if(!Py_IsInitialized()){
        std::cout << "unable to init Py" << std::endl;
    }
    else{
        CPyObject pName = PyUnicode_FromString("aae_webcam");

        CPyObject pModule = PyImport_Import(pName);




        if(pModule)
        {
            std::cout << "Module imported.. " << std::endl;

        }
        else
        {
            printf("ERROR: Module not imported \n");
            PyErr_Print();
        }
    }

The beginning of the python module imported is something like:

import cv2
import argparse
import tensorflow as tf
import numpy as np
import os
import configparser

from auto_pose.ae import factory
from auto_pose.ae import utils as u
from webcam_video_stream import WebcamVideoStream

parser = argparse.ArgumentParser()
parser.add_argument("experiment_name")
arguments = parser.parse_args()

full_name = arguments.experiment_name.split('/')

experiment_name = full_name.pop()
experiment_group = full_name.pop() if len(full_name) > 0 else ''

I've tried to look for the answer online, however, unsuccessful. Thus I hope you are able to come to my aid.

Edit: The CPyObject is an instance of a class:

class CPyInstance
{
public:
    CPyInstance()
    {
        Py_Initialize();
    }

    ~CPyInstance()
    {
        Py_Finalize();
    }
};


class CPyObject
{
private:
    PyObject *p;
public:
    CPyObject() : p(NULL)
    {}

    CPyObject(PyObject* _p) : p(_p)
    {}


    ~CPyObject()
    {
        Release();
    }

    PyObject* getObject()
    {
        return p;
    }

    PyObject* setObject(PyObject* _p)
    {
        return (p=_p);
    }

    PyObject* AddRef()
    {
        if(p)
        {
            Py_INCREF(p);
        }
        return p;
    }

    void Release()
    {
        if(p)
        {
            Py_DECREF(p);
        }

        p= NULL;
    }

    PyObject* operator ->()
    {
        return p;
    }

    bool is()
    {
        return p ? true : false;
    }

    operator PyObject*()
    {
        return p;
    }

    PyObject* operator = (PyObject* pp)
    {
        p = pp;
        return p;
    }

    operator bool()
    {
        return p ? true : false;
    }
};
Passbro
  • 11
  • 2
  • What's `CPyObject`? Because it certainly isn't part of the Python C API? – DavidW Apr 03 '20 at 16:27
  • Ah sorry @DavidW. CPyObject is from a class made in accordance with: https://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code This is to be able to store and use the results achieved from the python module. – Passbro Apr 05 '20 at 11:49
  • Running it in a debugger is possibly helpful. If the segmentation fault is coming from Python code then using the [`faulthandler` module](https://docs.python.org/3/library/faulthandler.html) can give you some useful information. However, if the failure is directly related to the import then that may not be too useful – DavidW Apr 05 '20 at 16:27
  • I will try that. Thank you. – Passbro Apr 06 '20 at 10:54
  • Seems as if it is a problem with the imports. I can import packages such as numpy. However as soon as I try to import cv2 or relative packages such as 'from auto_pose.ae import factory' – Passbro Apr 13 '20 at 09:11

0 Answers0