thepyedpiper / pyp

The Pyed Piper: Python Power At the Prompt
13 stars 0 forks source link

Parity between pp and p in terms of available imports #3

Open krackers opened 1 year ago

krackers commented 1 year ago

Seems like the eval for pp and p are handled via separate codepaths, and the exposed imports accessible to each differ.

E.g. the imports available in the environment you eval an expression with p is given in translate_preset_variables, including:

                     'date': datetime.datetime.now(),
                     'math':math,
                     'env': os.environ.get,
                     'glob' : glob.glob,
                     'letters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
                     'digits': '0123456789',
                     'punctuation': """!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~""",

while the environment for pp is much more impoverished, containing only math.

I suggest that to provide parity between these two, the set of imports be hoisted to some common global like

in_eval_available_imports = {
                     'date': datetime.datetime.now(),
                     'math':math,
                     'random':random,
                     'env': os.environ.get,
                     'glob' : glob.glob,
                     'letters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
                     'digits': '0123456789',
                     'punctuation': """!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~""",
                    }

and then you could do could use **in_eval_available_imports to include it as part of the respective environment variables before eval'ing. This also allows users to add in any imports they might want (e.g. random).

thepyedpiper commented 1 year ago

Interesting, I'll take a look at this for our future updates.