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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:33:23+00:00 2026-05-22T18:33:23+00:00

In Lua, there’s a built-in function called setfenv() , which allows you to supply

  • 0

In Lua, there’s a built-in function called setfenv(), which allows you to supply a table of variables as an environment for a function. Here’s an example that uses it:

foo = 1
function f()
    print(blech) --Note that no variable named blech has been defined.
end
variableTable = {blech = foo}
setfenv(f, variableTable)
f() -- This will work and print 1, because blech has been defined as foo's value in the variableTable

My motivation for doing this is that it allows me to set up a platform so that users can write easy-to-understand scripts in Lua (they can write nullary functions and can simply trust that variables will be there for them). It also provides an elegant method of excluding modules and functions that I don’t want them to use for security reason (e.g. if you define a variable table that doesn’t have the os module defined, there’s no way a more knowledgeable user could use it maliciously).

I would like to be able to achieve the same thing in Python. Is there something similar that can be done in Python?

  • 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-05-22T18:33:24+00:00Added an answer on May 22, 2026 at 6:33 pm

    There’s exec:

    def foo():
        print bar
    
    exec foo.func_code in {"bar": "baz"}
    

    This isn’t particularly good style (I would even say that it is particularly bad). It makes the implementation of foo difficult to understand since it relies on knowing about the special way in which it will be called. Instead, pass arguments to functions.

    This does not provide any measure of security. As a first approximation, Python does not include any restricted execution capabilities. Nothing stops a user from supplying this definition of foo instead:

    def foo():
        import os
        os.system("rm -rf /")
    

    Just because you didn’t supply os in the mapping doesn’t mean they can’t go get it themselves. If you really need restricted execution, then you might want to stick with Lua, or at least investigate PyPy sandboxed mode.

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

Sidebar

Related Questions

There are heaps of examples of how to clone a Lua table in Lua,
In Lua the the __index metamethod can be either a function or a table
Is there is simple POW like web development environment for Lua ? The idea
Calling a Lua function from C is fairly straight forward but is there a
Up to lua 2.4, there used to be an readuntil function that can read
Are there any scientific packages for Lua comparable to Scipy?
Is there an easy way to create standalone .exe files from Lua scripts? Basically
Is there a compendium of virtual machines and languages derived or inspired by Lua?
I was wondering if there's a way to do a lua file only once
I am embedding Lua into a C/C++ application. Is there any way to call

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.