Functions called "stdeval" and "stdexec" that I made

I would like to make a new function called stdeval. It works like eval but it also captures stdout. Also, I would like to make another function named stdexec

Here’s the code for both (it depends on io and contextlib):

def stdeval(expression):
    f = io.StringIO()
    with contextlib.redirect_stdout(f):
        k=eval(expression)
        return [f.getvalue(), str(k)]

def stdexec(object, closure=None):
    f = io.StringIO()
    with contextlib.redirect_stdout(f):
        exec(object, closure=closure)
        return f.getvalue()

and here’s an example:

expression = '''
"Hello, World!"
'''

output = stdeval(expression)[1]
print(output+"\nqwertasdfgzxcvb\nyuiophjklnm")

I already put the definition for the functions in functions.rst.
I would like to put this in a future version of Python.

This sounds like a proposal to post in “Ideas” section.

There, you either:
a) Try to convince others to do it (very low success chance. Everyone has their ideas of what is needed most and your idea should be pretty great to convince someone that it is more important than their own ideas.)
b) Be ready to learn what you need to implement it yourself. But you will still need to convince that this is needed, acknowledge criticisms and go through the whole process.

This is unofficial guide to proposing your idea, I recommend you read it carefully before posting in “ideas” category: GitHub - nineteendo/welcome-to-python-ideas

I pulled it out of there, in fact, specifically because it isn’t really well formed as a proposal yet. If the goal is simply to share the code, there are other routes; if the goal is to make it a builtin for a local copy of Python, that’s a clear Python Help issue (and also probably wouldn’t solve a practical problem anyway). But if the goal is to get the code integrated into future versions of Python then this is, so far and IMO, nowhere near what the Ideas section needs.

2 Likes

Why do you need to code the function in C?
Is there a problem with the pure python version?

You say you do not know the C language, are you intended to learn enough C to write the code? If not are you asking someone to do it for you?

Ah ok, I see.

To add it for oneself locally without doing any C, it can be done by:

import builtins
builtins.stdeval = stdeval

If you want to have it whenever you open python, just set up a script to load automatically by setting:

export PYTHONSTARTUP='path_to_script'

Read more about why it could be a bad idea in: python - How to add builtin functions? - Stack Overflow

1 Like

Someone changed it, it was that before
Screenshot 2024-07-07 163051

I want to implement it into future versions of Python.

You will be asked why this cannot be a package on PyPI.
You will be asked if there are lots of user who would benefit from this change.
You will be asked why python should take on the burden of maintenance of this code for the next decade.

3 Likes

@barry-scott that was a little rude…

Another possibility rather than stdlib or PyPI is that you can just use this code by adding it to the script/project etc where you want it. That is how most functions are used.

It looks like these functions make good use of combining a few stdlib/builtin primitives to do something useful. I’ve never had a need for this particular functionality but it is good that if you want it the stdlib provides the pieces to make it easy.

That is how the stdlib is supposed to work though: it provides the base components and you combine them in your own code. The fact that these few lines of code achieve something useful suggests to me that the stdlib provides what is needed in this case.

As for many other suggestions it is not worth making a PyPI package for this but it could be worth adding it to a package that has other related functionality as well if there is one. Otherwise the code can be a recipe for others to use.

I doubt many would consider these good candidates for the stdlib because eval and exec are themselves discouraged for security and other reasons.

3 Likes

I would like it in a new version…

Some people might like it.

He’s not being rude, he’s being realistic[1]. These are questions that any proposal needs to answer.

Adding something to the standard library is a big deal. It doesn’t just happen, it takes a lot of time and involves a lot of discussion.


  1. arguably he’s being polite, providing some clearly-needed guidance instead of just ignoring this thread ↩︎

5 Likes