473,480 Members | 4,827 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

__builtins__ and PyImport_AddModule

Good day,

I'm writing an application which creates modules and runs Python code on
the fly, embedding Python. The main function of the code is loaded into
the __main__ module: (Error checking omitted for brevity.)

PyObject* module = PyImport_AddModule("__main__");
PyObject* moduleDict = PyModule_GetDict(module);
PyObject* runRetval = PyRun_String(codeStr, Py_file_input,
moduleDict, moduleDict);

I ran into a problem when I tried to do a similar thing with the modules
that 'codeStr' may reference:

PyObject* module = PyImport_AddModule(moduleName);
PyObject* moduleDict = PyModule_GetDict(module);
PyObject* runRetval = PyRun_String(moduleCode, Py_file_input,
moduleDict, moduleDict);

The code in 'moduleCode' was unable to do any imports (__import__ was
not defined), nor use any builtin methods or classes (like Exception).
I dig a bit further into it, and found that __builtins__ was not set in
my new module's dict. This is set for __main__ in initmain() in
pythonrun.c. I ended up having to add the following code to import
__builtin__:

if (PyDict_GetItemString(moduleDict, "__builtins__") == NULL)
{
PyObject* builtinMod = PyImport_ImportModule("__builtin__");
if (builtinMod == NULL ||
PyDict_SetItemString(moduleDict, "__builtins__", builtinMod) != 0)
// blah blah, error handling.
;
Py_XDECREF(builtinMod);
}

Why is __builtins__ not set in a module created by PyImport_AddModule?

--
Mathieu Fenniak <la****@pobox.com>
PGP Key ID 0xE131EF7B
http://www.stompstompstomp.com/
Jul 18 '05 #1
0 1728

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1785
by: Opinder | last post by:
Hi, For the Python experts out there: Are there any side effects in assigning new variables to the __builtins__ module for the purpose of exposing variables to imported modules. For...
1
1769
by: Matteo Merli | last post by:
Hi, is there a way to save objects in the __builtins__ namespace? the goal is to make them available in all the modules of my app, without reimporting all the things and keep references... The...
1
1353
by: Michael Hohn | last post by:
Hi, using the file builtin_check.py with content # Module builtin_check # Inconstency in the binding of __builtins__ def get_binding(name): return locals() def get_global_binding(name):
4
1467
by: Collin Winter | last post by:
Hallo all, As it currently stands, the type of the global __builtins__ differs depending on whether you're in the __main__ namespace (__builtins__ is a module) or not (its a dict). I was...
1
1715
by: Adam Hupp | last post by:
I've noticed some unexpected behavior with __builtins__ during module import. It seems that during module import __builtins__ is a dict but at all other times it is a module. For example, if...
3
4155
by: loquehumaine | last post by:
Hi there, I'm a newby in python (I know a little in programmation) and I have a lot of questions on builtins but my first one is about modules... I have seen that if I type help() at a prompt,...
0
1071
by: Patrick Maupin | last post by:
__builtins__ in 2.5.2 doesn't seem to behave like I remember it did the last time I did some custom stuff with it, a very long time ago. This isn't surprising, because of ongoing optimization,...
3
1350
by: Gabriel Genellina | last post by:
En Sun, 07 Sep 2008 14:00:48 -0300, Patrick Maupin <pmaupin@gmail.comescribió: Python takes some shortcuts when dealing with builtins. I'll just describe what happens (I won't say whether it is...
0
1201
by: Lie Ryan | last post by:
On Tue, 30 Sep 2008 16:04:34 -0500, William Purcell wrote: when you pass mydict, it is used as the global variables in the eval, right? Then, you passed a code to eval('...', mydict), sometimes...
0
6905
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.