Python Forum
More Python Embedding Trouble
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
More Python Embedding Trouble
#1
Shy
I'm trying to run a basic C/Python API, I am using Python3.6
https://docs.python.org/3.6/extending/em...n-overview

My Python File Looks like
import numpy as np
#import random
class Multiply:
    def __init__(self):
        self.x = 2
        self.y = 2
        self.a = 0

    def multiply(self):
        self.a = self.x * self.y
        print("Result of: {} * {}".format(self.x,self.y))
        return self.a


My C File Looks Like
#include </usr/include/python3.6m/Python.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pClass;
    PyObject  *pValue, *pDict, *pInstance;

    int i, arg[2];

    Py_Initialize();

    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append(\".\")");


    pName = PyUnicode_DecodeFSDefault("with_numpy_import");
    pModule = PyImport_Import(pName);
    pDict = PyModule_GetDict(pModule);

    pClass = PyDict_GetItemString(pDict, "Multiply");

    Py_DECREF(pName);

    if (PyCallable_Check(pClass)){
    pInstance = PyObject_CallObject(pClass, NULL);
    }


    if( argc > 4 ){
      for (i = 0; i < argc - 4; i++){
          arg[i] = atoi(argv[i + 4]);
  }

      pValue = PyObject_CallMethod(pInstance, argv[3], "(ii)", arg[0], arg[1]);

  }else{
      pValue = PyObject_CallMethod(pInstance, "multiply", NULL);
  }

  if(pValue != NULL){
      printf("Return of call : %d\n", PyLong_AsLong(pValue));
      Py_DECREF(pValue);
  }else{
  PyErr_Print();
  }

  // Clean up
  Py_DECREF(pModule);
  Py_DECREF(pDict);
  Py_DECREF(pInstance);
  Py_DECREF(pClass);

  Py_Finalize();

  //////// Start Over ////////

  Py_Initialize();

  PyRun_SimpleString("import sys");
  PyRun_SimpleString("sys.path.append(\".\")");


  pName = PyUnicode_DecodeFSDefault("with_numpy_import");
  pModule = PyImport_Import(pName);
  
  

  return 0;
}



Why do I get a segmentation error when I import numpy? I can import random and it runs perfectly fine after I comment out import numpy. I have observed that this segmentation error occurs when I use modules that have been brought in with pip.

Is it safe to assume I can run Py_Initialize(); and Py_Finalize(); multiple times in one program?

Please advise. Confused
Reply
#2
It could be related to the Bugs and Caveats section in the documentation of Py_FinalizeEx(). Try to use Py_Initialize() and Py_Finalize() only once to see if it changes anything.
Reply
#3
If I only call Py_Initialize() and Py_Finalize() once it works, but is there a reason why Py_Initialize() and Py_Finalize() can't be called twice? It works with import random?
Reply
#4
The Python Documentation Wrote:Bugs and caveats: The destruction of modules and objects in modules is done in random order; this may cause destructors (__del__() methods) to fail when they depend on other objects (even functions) or modules. Dynamically loaded extension modules loaded by Python are not unloaded. Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it). Memory tied up in circular references between objects is not freed. Some memory allocated by extension modules may not be freed. Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_FinalizeEx() more than once.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with installing python domingo251 2 592 Sep-23-2023, 12:03 AM
Last Post: ICanIBB
  Embedding python script into html via pyscript pyscript_dude 7 1,550 Apr-16-2023, 11:17 PM
Last Post: pyscript_dude
  C++ python embedding comarius 0 825 Aug-26-2022, 02:01 AM
Last Post: comarius
Question Embedding a python file online Dreary35 0 1,520 Jun-10-2021, 05:05 PM
Last Post: Dreary35
  Embedding python cause crash when use boost::asio multi threading udvatt108 0 1,716 Oct-04-2020, 03:15 PM
Last Post: udvatt108
  Need help merging/embedding duckredbeard 10 3,382 Aug-13-2020, 04:48 AM
Last Post: duckredbeard
  New to python, having trouble with an exercise Salkay 3 2,146 Feb-18-2020, 01:42 AM
Last Post: Salkay
  Embedding return in a print statement Tapster 3 2,276 Oct-07-2019, 03:10 PM
Last Post: Tapster
  Calling Extended Embedding Python as shared library jibarra 0 2,215 Jul-19-2019, 05:22 PM
Last Post: jibarra
  Python Embedding jibarra 4 3,055 Jul-11-2019, 03:25 PM
Last Post: jibarra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020