<div>Hello.</div><div>I have quite a peculiar problem.</div><div> </div><div>A little overview of our situation:</div><div>Our program enables our users to write their own python code (which they use extensively).</div><div>
Unfortunately, (due to us actually encouraging this in an earlier release (!stupid!)) this meant that, in several cases, there are modules which use __builtins__ as a kind of global dictionary, where various variables are inserted, changed and read.</div>
<div> </div><div>Now, currently we are updating our program from a single-threaded to a multithreaded architecture.</div><div>-> Of course, this means that the same module can now run in several threads at the same time, and, since __builtins__ is pretty much global in the system, they all share the same __builtins__.</div>
<div> </div><div>Unfortunately, the obvious solution (use something else instead of abusing __builtins__) won't do, because, like I said, there are various existing modules out there that we cannot control and which must continue to run as expected.</div>
<div> </div><div>One possible solution is to somehow redirect every __builtins__ to a function that returns a different __builtins__ dictionary for each thread (such a function already exists).</div><div> </div><div>MY QUESTION:</div>
<div>How can I redirect __builtins__ to a built-in function?</div><div>My first idea was using __getattr__, but this won't work for most modules, because __getattr__ only works with classes, and the customer .py-files might not have any.</div>
<div>This means the customer modules are simply objects of the class <modules>, which is of course an unchangeable metaclass. (so, no way to add any methods here)</div><div>I could do it if there was any way to load .py-files as any other class than <modules>, maybe as a subclass, but I do not know how this could be done.</div>
<div> </div><div>So, I am really at a loss here.</div><div>Any help would be greatly appreciated.</div><div> </div><div>Regards,</div><div>Juergen Bartholomae</div><div> </div><div> </div>