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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:29:34+00:00 2026-05-27T16:29:34+00:00

In the context of a complex application, I need to import user-supplied ‘scripts’. Ideally,

  • 0

In the context of a complex application, I need to import user-supplied ‘scripts’. Ideally, a script would have

def init():
    blah

def execute():
    more blah

def cleanup():
    yadda

so I’d just

import imp
fname, path, desc = imp.find_module(userscript)
foo = imp.load_module(userscript, fname, path, desc)
foo.init()

However, as we all know, the user’s script is executed as soon as load_module runs.
Which means, a script can be something like this:

def init():
    blah

yadda

yielding to the yadda part being called as soon as I import the script.

What I need is a way to:

  1. check first whether it has init(), execute() and cleanup()
  2. if they exist, all is well
  3. if they don’t exist, complain
  4. don’t run any other code, or at least not until I know there’s no init()

Normally I’d force the use the same old if __name__ == '__main__' trick, but I have little control on the user-supplied script, so I’m looking for a relatively painless solution. I have seen all sorts of complicated tricks, including parsing the script, but nothing really simple. I’m surprised it does not exist.. or maybe I’m not getting something.

Thanks.

  • 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-27T16:29:35+00:00Added an answer on May 27, 2026 at 4:29 pm

    My attempt using the ast module:

    import ast
    
    # which syntax elements are allowed at module level?
    whitelist = [
      # docstring
      lambda x: isinstance(x, ast.Expr) \
                 and isinstance(x.value, ast.Str),
      # import
      lambda x: isinstance(x, ast.Import),
      # class
      lambda x: isinstance(x, ast.ClassDef),
      # function
      lambda x: isinstance(x, ast.FunctionDef),
    ]
    
    def validate(source, required_functions):
      tree = ast.parse(source)
    
      functions = set()
      required_functions = set(required_functions)
    
      for item in tree.body:
        if isinstance(item, ast.FunctionDef):
          functions.add(item.name)
          continue
    
        if all(not checker(item) for checker in whitelist):
          return False
    
      # at least the required functions must be there
      return len(required_functions - functions) == 0
    
    
    if __name__ == "__main__":
      required_funcs = [ "init", "execute", "cleanup" ]
      with open("/tmp/test.py", "rb") as f:
        print("yay!" if validate(f.read(), required_funcs) else "d'oh!")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A complex web-app can have a large amount of user and/or context specific meta
Context: There's an application where you draw things on canvas. Where user clicks there's
I have a somewhat complex data model in my iPad application (an OpenGL drawing
I have a fairly complex data model with approximately 10 entities. Some need to
I would like to write a somewhat complex web gui application. It will be
I have a n-tier application based on pretty classic different layers: User Interface, Services
Context: .Net, C# I want to print a complex number made from two doubles.
Context: So, I am attempting to build a ridiculously complex domain model. Talking with
I have a complex MySQL problem. SELECT * FROM banners, content_id_country, languages WHERE content_id_country.content_id
Context: I'm in charge of running a service written in .NET. Proprietary application. It

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.