-2

Through a code written in C++ code that embeds python call external python class and execute a method of the class (FileHandler). That works. I generate a library of that code in C++ (libSome.so) for use in python with c_types and making a wrapper to try to run the above method gives segmentation fault. Any ideas?

(C++)This is the embedded code then generated as a shared library (libSome.so):

...
    /* Funcion de python */
        setenv("PYTHONPATH", ".", 1);

        Py_Initialize();

        PyObject* module = PyImport_ImportModule("filehandler");
        assert(module != NULL);

        PyObject* class = PyObject_GetAttrString(module, "FileHandler");
        assert(class != NULL);

        PyObject* inst = PyInstance_New(class, NULL, NULL);
        assert(inst != NULL);
        result = PyObject_CallMethod(inst, (char*)"write", (char*)"(iiii)",ori,serv, id, timeStamp);
        assert(result != NULL);
        Py_Finalize();

(Python) Code used by the library

import os

class FileHandler:
     def __init__(self):
          self.workingDirectory = os.getcwd()
          pass

     def write(self, NodoOrigen, Servicio, Id, payload):
          try:
           os.mkdir(str(NodoOrigen))
          except:
           pass

      os.chdir(str(NodoOrigen)+"/")
      try:
           os.mkdir(str(Servicio))
      except:
           pass

      os.chdir(self.workingDirectory)
      os.chdir(str(NodoOrigen)+"/"+str(Servicio)+"/")
      try:
           f = open(str(Id),"a")        
      except:
           print "No se puede abrir el archivo"
      f.write(str(payload))
      f.close()
      os.chdir(self.workingDirectory)
maekos
  • 5
  • 2
  • 1
    Serious lack of code and details. – mguijarr Oct 07 '13 at 19:58
  • 63rd line has a bug on the left. needs to be `buf-1` instead of `buf` – shoosh Oct 07 '13 at 22:12
  • Why line of code segfaults? I'm also confused by the reference to ctypes. If you want to embed C++ code in Python use an extension. Would be weird to use ctypes to load this code which then simply calls back into Python. Not even sure if that works. – David Heffernan Oct 08 '13 at 11:55
  • Hmm.. are you by any chance calling this c++ code from inside python, eg, through the `ctypes` library? – SingleNegationElimination Oct 08 '13 at 12:15
  • It's amazing that you have edited the question and still refuse to tell us which line of code results in segfault. You probably don't know. Which means you need to back up and do some debugging. – David Heffernan Oct 09 '13 at 16:57
  • David I edit the line that gives no segmentation fault. Sorry for my English, maybe can not express my question to you can understand it. I solved my problem by returning a value to the python code instead of instantiating an object python from C + +. Yet it was not the way I wanted. Perhaps the answer of "benpmorgan" would have been correct. Thank you very much to all and sorry again. – maekos Oct 10 '13 at 02:39

2 Answers2

0

I'm not sure if this is your issue as there isn't really enough information here, but ctypes is only meant to call C functions; to call C++ functions, you need to wrap your C++ functions with a function in an extern "C" block.

For an example of this, see this answer: https://stackoverflow.com/a/145649/121714

Community
  • 1
  • 1
benpmorgan
  • 604
  • 3
  • 9
  • python -> c + +. Works (wrapper) c + + -> python with shared library does not work. The problem is when from the library (c + +) I want to call a python class method from the generated shared library. Apologies, my English is not very good at explaining the problem correctly. – maekos Oct 08 '13 at 11:07
0

I think the problem might be with your use of PyInstance_New: https://mail.python.org/pipermail/python-list/2003-March/195516.html

Maybe try this instead:

PyObject* inst = PyObject_CallObject(class, NULL);

and make FileHandler inherit from object in your Python code.

class FileHandler(object):
    ...

Is this your actual code though? If it is C++, it shouldn't be letting you use a variable called class, and assert(instance != NULL); should read assert(inst != NULL);

Also, which line is actually causing the segfault?

Another possibility: if you are starting this from Python, then calling C++ which calls Python, the C++ code shouldn't call Py_Initialize(); or Py_Finalize(); (however, if you have a C++ application embedding Python, that would be fine)

benpmorgan
  • 604
  • 3
  • 9