473,756 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Globals, Locals Dictionaries + Callbacks Question

Hello,

I'm having a bit of trouble isolating my scripts from each other in my
embedded Python interpreter, so that their global namespaces don't get
all entangled. I've had some luck with PyRun_FileEx(), as you can
specify dictionaries to use for the globals and locals, but it seems
that you can't do the same for PyEval_CallObje ct() (which i use for
calling the callbacks previously registered by scripts run via
PyRun_FileEx()) . Is there any way to substitute the current global/local
dictionaries, run the callback, then switch back to the default? It
would be just as good if I could switch between several sets of global
variable dictionaries, one for each script; unfortunately, the
documentation is less than informative on this point (there isn't even a
formal defintion for PyEval_CallObje ct()). Also, I'm aware that eval()
and exec() allow you to pass in global/local dictionaries, but I think
it'd be a bit wasteful to drop into Python, run exec(), then have that
call my callback, rather than just calling the callback directly from
the host program.

Thanks in advance
Dec 21 '05 #1
3 1497
Faisal Alquaddoomi wrote:
Hello,

I'm having a bit of trouble isolating my scripts from each other in my
embedded Python interpreter, so that their global namespaces don't get
all entangled.


[tales of woe ...]

Would it help to use PyNewInterprete r to create subinterpreters for each
of the different scripts?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 21 '05 #2
Steve Holden wrote:
Faisal Alquaddoomi wrote:
Hello,

I'm having a bit of trouble isolating my scripts from each other in my
embedded Python interpreter, so that their global namespaces don't get
all entangled.

[tales of woe ...]

Would it help to use PyNewInterprete r to create subinterpreters for each
of the different scripts?

regards
Steve


That sounds like a reasonable suggestion, but would it be inefficient to
spawn a whole new interpreter for each script, where what I really want
to do is just change the dictionaries used for globals and locals? I'll
try that out for now, but I'm still hoping there's some way to change
those dictionaries without having to do anything excessive.

Thanks for the advice,
Faisal
Dec 21 '05 #3
Faisal Alquaddoomi wrote:
Steve Holden wrote:
Faisal Alquaddoomi wrote:

Hello,

I'm having a bit of trouble isolating my scripts from each other in my
embedded Python interpreter, so that their global namespaces don't get
all entangled.

[tales of woe ...]

Would it help to use PyNewInterprete r to create subinterpreters for each
of the different scripts?

regards
Steve

That sounds like a reasonable suggestion, but would it be inefficient to
spawn a whole new interpreter for each script, where what I really want
to do is just change the dictionaries used for globals and locals? I'll
try that out for now, but I'm still hoping there's some way to change
those dictionaries without having to do anything excessive.

I guess only benchmarking can answer that question. However, given that
the code will be common to all instances you may find that it isn't too
resource-intensive. I agree it does seem much more heavyweight than
replacing execution context.

Good luck!

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 22 '05 #4

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

Similar topics

2
2390
by: Giles Brown | last post by:
I do not understand why the following code produces NameError: name 'FirstClass' is not defined when both a global and local dict are passed into exec, but not when only a global dict is passed in. I seek enlightenment! Thanks, Giles Brown
3
2078
by: Robert Dodier | last post by:
Hello, I'm interested in introducing new variables into the environment of a Python interpreter or program. In reading through old posts to this newsgroup, I see there is an often-repeating warning against modifying the contents of locals(). Fair enough. However, is there anything wrong with modifying globals() ?
2
4187
by: tedsuzman | last post by:
----- def f(): ret = 2 exec "ret += 10" return ret print f() ----- The above prints '12', as expected. However,
1
1692
by: Ted | last post by:
-------- def f(): ret = 2 exec "ret += 10" return ret print f() -------- The above example prints '12'. However, the following example prints
45
2683
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
4
1201
by: JerryB | last post by:
Hi, I have a dictionary, a string, and I'm creating another string, like this: dict = {} dict = "need" str = "love" mystr = """All you %(dict)s is %(str)s""" % locals()
18
2947
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you have a local identical variable name and want to save/load it to/from the global with same name * while you add code, the definition of globals moves more and more apart from their use cases -> weirdness; programmers thinking is fragmented * using...
1
1575
by: Paolo Pantaleo | last post by:
Hi this exaple: def lcl(): n=1 x=locals() x=100 print "n in lcl() is:" +str(n) #This will say Name error
2
3110
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
0
9455
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9869
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9838
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7242
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5140
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.