Python Forum
eval lambda function with restricted context
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
eval lambda function with restricted context
#8
Regarding security, I know that it is not absolutely safe. But IMO it's better to do something partly than do nothing.

I think that Stranac gave a good hint! Use the globals! Accordingly, the following code is functional and seems to be satisfying for my need:

import numpy
symbol_table = numpy.__dict__.copy()
symbol_table['__builtins__'] = None         # remove built-in functions
f = eval('lambda x:exp(1)', symbol_table)
In my first post, I was not understanding the use of locals in the eval() function. I still do not understand exactly what it is meant to be used for, but I think that in the context of my question, the globals should be used.

Thank you for the help.

Olivier
Reply


Messages In This Thread
RE: eval lambda function with restricted context - by Olivier - Mar-04-2019, 10:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Add two resultant fields from python lambda function klllmmm 4 852 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  with open context inside of a recursive function billykid999 1 552 May-23-2023, 02:37 AM
Last Post: deanhystad
  Context-sensitive delimiter ZZTurn 9 1,394 May-16-2023, 07:31 AM
Last Post: Gribouillis
  Running 3rd party libs on Steam Deck (Arch Linux) with restricted access metulburr 0 1,794 Jan-07-2023, 10:41 PM
Last Post: metulburr
  How does open context manager work? deanhystad 7 1,266 Nov-08-2022, 02:45 PM
Last Post: deanhystad
  Decimal context stevendaprano 1 1,013 Apr-11-2022, 09:44 PM
Last Post: deanhystad
  How to avoid exec(), globals(), locals(), eval() paul18fr 10 4,906 Apr-21-2021, 05:53 PM
Last Post: snippsat
  Writing a lambda function that sorts dictionary GJG 1 1,989 Mar-09-2021, 06:44 PM
Last Post: buran
  int, floats, eval menator01 2 2,404 Jun-26-2020, 09:03 PM
Last Post: menator01
  translating lambda in function fabs 1 2,122 Apr-28-2020, 05:18 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020