473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__builtins__ and PyImport_AddMod ule

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_AddMod ule("__main__") ;
PyObject* moduleDict = PyModule_GetDic t(module);
PyObject* runRetval = PyRun_String(co deStr, 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_AddMod ule(moduleName) ;
PyObject* moduleDict = PyModule_GetDic t(module);
PyObject* runRetval = PyRun_String(mo duleCode, 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_GetItem String(moduleDi ct, "__builtins __") == NULL)
{
PyObject* builtinMod = PyImport_Import Module("__built in__");
if (builtinMod == NULL ||
PyDict_SetItemS tring(moduleDic t, "__builtins __", builtinMod) != 0)
// blah blah, error handling.
;
Py_XDECREF(buil tinMod);
}

Why is __builtins__ not set in a module created by PyImport_AddMod ule?

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

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

Similar topics

1
1794
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 example.
1
1775
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 curious thing is that when i try from the interactive interpreter the thing just works: >>> def a(): .... print "ciao"
1
1357
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
1473
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 recently tripped up by this discrepancy, and googling the issue brings up half-a-dozen or so c.l.p threads where others have been bitten by this, too. ...
1
1730
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 the file testmod.py has these contents: print type(__builtins__) print "has str attr", hasattr(__builtins__, 'str') The output differs depending...
3
4162
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, and then 'modules', I'll be given a list of all modules available, thanks to this group.. But I have seen the differences between them and the one in...
0
1074
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, but it's hard to google for '__builtins__' so I didn't really find any documentation on the current CPython behavior, which in some cases seems quite...
3
1362
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 "right" or "wrong"). The exec statement, when given a string source, compiles it and eventually calls PyEval_EvalCodeEx, which creates a new frame...
0
1206
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 you might create global variable inside eval, and you want to see the value of that inner global, that's why python modified mydict as a...
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7689
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7456
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6022
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
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 we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.