Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3237406
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:44:06+00:00 2026-05-17T17:44:06+00:00

I’m working on embedding Python 2.6 into an existing c++ application. So far I

  • 0

I’m working on embedding Python 2.6 into an existing c++ application. So far I have the Libraries linked in and am able to successfully initialize the Python Interpreter and can also transfer data to Python. I’m having trouble retrieving it, and hope someone can steer me the right direction. I’m working with this:

Py_Initialize();

pModule = PyImport_ImportModule("cBuffers"); // This crashes after 1st call.
pDict = PyModule_GetDict(pModule);
pClass = PyDict_GetItemString(pDict, "rf_pdf");
pMeth = PyString_FromString("main");

if (PyCallable_Check(pClass) && PyClass_Check(pClass)) {
  pInstance = PyInstance_New(pClass, NULL, NULL);
  pOutput = PyObject_CallMethodObjArgs(pInstance, pMeth, pOpts, pInput, NULL);
}

if (pOutput != NULL) {
  string pPdf = PyString_AsString(pOutput);
  Py_DECREF(pOutput);
} else {
  PyErr_Print();
}

// Cleanup
Py_DECREF(pModule);
Py_DECREF(pModule); // Has an extra reference, not positive why.
Py_DECREF(pMeth);
Py_DECREF(pInstance);
Py_DECREF(pOpts);
Py_DECREF(pInput);

Py_Finalize();

pOpts and pInput are both generated using PyString_FromString earlier in the code. The trouble I’m having is that when I attempt to retrieve the output using PyString_AsString the return value is NUL Terminated. Unfortunately, because I’m generating PDF Documents, NULs are not only allowed, they’re almost guaranteed. Can anyone tell me how I return String Data from Python back to C++ without ending at the first NUL it encounters?

As an additional question, This code can be called multiple times as a part of a background service that’s creating PDF Documents from incoming Print Data. The first time this code is called into it works as expected. Any subsequent calls fail at the indicated line just after Py_Initialize(). Help on how to determine what’s going on there would be most appreciated as well. Thanks in advance,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:44:07+00:00Added an answer on May 17, 2026 at 5:44 pm

    A few points:

    • Don’t use strings. You might even be
      able to make them work here with some
      contortions on *_StringAndSize()
      functions, but it won’t be what you
      want. You should store your data in
      a custom data structure (or a buffer) that is just
      a sequence of bytes (do you really
      want clients performing string
      operations on this data in Python?). If your object really is a buffer object, you should use the Buffer API.

    • Your imported module has a refcount of 2 because it’s being held in
      sys.modules (for efficiency for the next time you try to import it). Never decref
      references you don’t own or you’ll
      crash your program. The Importing
      Modules
      section of the
      documentation should really cover
      this, but it doesn’t.

    • It’s pretty expensive to initialize Python and tear it down every time you do these operations. You should try to reorganize your use case such that you can call Py_Initialize only once when your application starts (or the first time it needs Python), and then only call Py_Finalize when your application is definitely done with Python, or when it quits.

    • You’re being very lazy with error checking – most of the Python C/API functions can return NULL to indicate that an exception has been thrown, and you’re almost never checking this value. If something fails you’re going to start crashing in very odd places. You can read about this in the Exception Handling section of the C/API manual.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.