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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:04:33+00:00 2026-06-11T20:04:33+00:00

The title seems stupid but i don’t know how to express it precisely, sorry.

  • 0

The title seems stupid but i don’t know how to express it precisely, sorry.

I have a program that need to eval some user code (through RestrictedPython for security) and I want to put a function into the eval’s globals so that it can print out some debug information to me when eval, for example (simplify):

class UserException(Exception):
    pass


def err(msg):
    # ? how to get the globals variable in eval ?
    A = globals().get('A', 'A not found')
    return UserException("%s and A's value is %r" % (msg, A))

g = {
    'err': err,
    'A': None,
    '__builtins__': {},
}

print eval('A or err("A not true")', g)

this will give the resut:

A not true and A's value is 'A not found'

Using ‘globals()’ here insde ‘err’ is of course wrong. But how can i get the value in ‘g’ inside ‘err’?

  • 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-11T20:04:34+00:00Added an answer on June 11, 2026 at 8:04 pm

    Any reference to globals() from inside a function will always give you the globals that were in scope when the function was defined. What you are seeing here is no different than when you import a function from one module into another: the imported function still refers to the globals of the module where it was defined.

    The simplest way to get the function to use g as its globals() would be to exec the definition using g as the globals. If you do change the globals for the function then don’t foget you also need to include any other globals that the function uses; in this case UserException.

    Alternatively you could make err() inspect the stack frame of its caller and use the caller’s globals. That’s messy, but if it’s for debug information may be acceptable to you.

    >>> def err(msg):
        # ? how to get the globals variable in eval ?
        A = sys._getframe(1).f_globals.get('A', 'A not found')
        return UserException("%s and A's value is %r" % (msg, A))
    
    >>> import sys
    >>> g = {
        'err': err,
        'A': None,
        '__builtins__': {},
    }
    >>> print eval('A or err("A not true")', g, g)
    A not true and A's value is None
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that my title seems with others from StackOverflow but my intention is
Really sorry for the extremely stupid title, but if I know what it is,
I see another question on stackoverflow.com whose title seems similar but that doesnot fulfil
Sorry for the long title, but it seems most descriptive for my question. Basically,
I know the title of this question is stupid, but I couldn't come up
the title seems stupid. but i met the problem, now i can just type
The question's title may sound stupid, but I have beenw wondering about the following
I'm sorry if the question looks stupid,but It seems I can't use my head
Sorry if the title seems confusing, but some examples are in order. Let's say
I'm sorry if my question title seems fundamentally uninformed. Let me explain what I

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.