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 7924425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:44:37+00:00 2026-06-03T17:44:37+00:00

I have two questions regarding Pysandbox: How do I achieve the functionality of eval

  • 0

I have two questions regarding Pysandbox:

  1. How do I achieve the functionality of eval? I understand sandbox.execute() is equivalent to exec, but I can’t find anything such that if the code entered were 2 + 2, then it would return 4, or something to that effect.

  2. By default, sandbox.execute() makes a passed-in environment read-only — i.e. if I do sandbox.execute('data.append(4)', locals={'data': [1, 2, 3]}), an error will occur. How do I make passed-in environments read-write?

  • 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-03T17:44:39+00:00Added an answer on June 3, 2026 at 5:44 pm

    Aha, I see that the last PySandbox release (1.5) does behave as you describe. I was trying on the git master branch.

    For question 1, I don’t think there are any straightforward options with PySandbox 1.5. The code has been written specifically to try and disallow modification or transmission of any state while inside the sandbox. sandbox.call(eval, CODE) is not a good option, since you can’t very easily pass in the locals and globals namespaces (you can try, but the sandbox will proxy them, and then eval() will fail because locals and globals have to be real dicts). And I believe you want to be able to set the locals and globals here.

    Question 2 isn’t straightforward either, for similar reasons. Both of these are possible on the current git master branch, but it’s not obvious whether that’s because of bugs or by design 🙂

    So here’s a solution to both questions which sort of extends the sandbox functionality and uses what is probably meant to be a private interface, but should still be as safe as the sandbox is otherwise:

    from sandbox.proxy import proxy
    from sandbox.sandbox_class import _call_exec
    
    def proxyNamespace(d):
        return dict((str(k), proxy(v)) for k, v in d.iteritems())
    
    def wrapeval(codestr, globs, locs):
        subglobs = proxyNamespace(globs)
        sublocs = proxyNamespace(locs)
        return eval(codestr, subglobs, sublocs)
    
    def eval_in_sandbox(sandbox, codestr, globs=None, locs=None):
        if globs is None:
            globs = {}
        if locs is None:
            locs = globs
        return sandbox._call(wrapeval, (codestr, locs, globs), {})
    
    def exec_in_sandbox_with_mutable_namespace(sandbox, codestr, globs=None, locs=None):
        if globs is None:
            globs = {}
        if locs is None:
            locs = globs
        subglobs = proxyNamespace(globs)
        sublocs = proxyNamespace(locs)
        sandbox._call(_call_exec, (codestr, subglobs, sublocs), {})
        globs.update(subglobs)
        locs.update(sublocs)
    

    Now, all that said, I strongly recommend not basing the safety of any actual production code or system on this PySandbox module. In looking through the code, it seems like it’s completely riddled with flaws and holes. I don’t think it would take an attacker longer than an hour or so to find ways past this sort of security. I also don’t think this has undergone any serious review by security professionals, or even a very big part of the community at large. If you want to run untrusted code and have it not be able to affect your process at all, you need something lots stronger, probably based around AppArmor or something like that. If you are only worried about accidentally misbehaving code, as opposed to malicious, this might be ok, but I’m actually not even sure about that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions regarding an NSArray: How can I add nil values to
I have two questions regarding the '::' case class. :: can be used as
I actually have two questions regarding the same problem but I think it is
I have two questions regarding RewriteRule, but they're both closely related so I hope
I actually have two questions regarding exception/error handling in the iPhone app that I
I need some help here, I have two questions regarding Qt's style sheet customization
I have two questions regarding the multidimensional arrays. I declared a 3D array using
I have two questions regarding ceil() function.. The ceil() function is implemented in C.
I have two questions regarding python libraries: I would like to know if there
I have two questions regarding Erlang file i/o; what is the best way to

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.