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

  • Home
  • SEARCH
  • 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 7751043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:26:51+00:00 2026-06-01T11:26:51+00:00

I’m working on embedding Python in our test suite application. The purpose is to

  • 0

I’m working on embedding Python in our test suite application. The purpose is to use Python to run several tests scripts to collect data and make a report of tests. Multiple test scripts for one test run can create global variables and functions that can be used in the next script.

The application also provides extension modules that are imported in the embedded interpreter, and are used to exchange some data with the application.

But the user can also make multiple test runs. I don’t want to share those globals, imports and the exchanged data between multiple test runs. I have to be sure I restart in a genuine state to control the test environment and get the same results.

How should I reinitialise the interpreter?

I used Py_Initialize() and Py_Finalize(), but get an exception on the second run when initialising a second time the extension modules I provide to the interpreter.
And the documentation warns against using it more than once.

Using sub-interpreters seems to have the same caveats with extension modules initialization.

I suspect that I’m doing something wrong with the initialisation of my extension modules, but I fear that the same problem happens with 3rd party extension modules.

Maybe it’s possible to get it to work by launching the interpreter in it’s own process, so as to be sure that all the memory is released.

By the way, I’m using boost-python for it, that also warns AGAINST using Py_Finalize!

Any suggestion?

Thanks

  • 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-06-01T11:26:52+00:00Added an answer on June 1, 2026 at 11:26 am

    Here is another way I found to achieve what I want, start with a clean slate in the interpreter.

    I can control the global and local namespaces I use to execute the code:

    // get the dictionary from the main module
    // Get pointer to main module of python script
    object main_module = import("__main__");
    // Get dictionary of main module (contains all variables and stuff)
    object main_namespace = main_module.attr("__dict__");
    
    // define the dictionaries to use in the interpreter
    dict global_namespace;
    dict local_namespace;
    
    // add the builtins
    global_namespace["__builtins__"] = main_namespace["__builtins__"];
    

    I can then use use the namespaces for execution of code contained in pyCode:

    exec( pyCode, global_namespace, lobaca_namespace );
    

    I can clean the namespaces when I want to run a new instance of my test, by cleaning the dictionaries:

    // empty the interpreters namespaces
    global_namespace.clear();
    local_namespace.clear();        
    
    // Copy builtins to new global namespace
    global_namespace["__builtins__"] = main_namespace["__builtins__"];
    

    Depending at what level I want the execution, I can use global = local

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.