Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

python - exec returns a nonetype, when I need the return value

I asked a question earlier, but ran into a second problem.

I am writing a program that reads in a text file, and executes all code within the file. This is for class, and we have to use exec()

I get this error upon running the code, and countless searching has not led me to a solution.

Traceback (most recent call last):
  File "doxecute.py", line 28, in <module>
    replace_code(statements, contents)
  File "doxecute.py", line 17, in replace_code
    contents = contents.replace("{%" + statement + "%}", statement)
TypeError: Can't convert 'NoneType' object to str implicitly

Code is

import sys
import re



def sortecute(data): 
    funcs = re.findall(r'{%(.*?)%}',data,re.DOTALL)#find executable statements
    return funcs

def replace_code(statements, contents):
    for statement in statements:
        if not statement[5:].startswith("print("):
            exec(statement[5:]) #execute code after the (letter)
            contents = contents.replace("{%" + statement + "%}", "")
        else:
            statement = exec(statement[5:])#error is here
            contents = contents.replace("{%" + statement + "%}", statement)

    print(contents)

f = open(sys.argv[1],"r")
contents = f.read()
f.close()

statements = sortecute(contents) #get data from file
statements  = sorted(statements) #sorts by letter

replace_code(statements, contents)

this is the file I read in.

The number {% (c) print(x) %} is a random number between 1 and 6
inclusive. If we multiply it by 2, we get {% (d) print(2*x) %}.

What's interesting is that the statements may appear out of order in the
document. {% (a) import random %} Thus I might generate the random
number in a location in the document well after referencing it.
{% (b) x = random.randint(1,6) %}

I can not find out how to get the value of the statement for exec. Can someone explain to me how to use this properly in the way listed below

You will need to use the exec function in Python. To get the output back, you will need to redirect output to your own stream. Your program should accept a filename as a command-line argument to operate on [8]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

exec will always return None. From the documentation:

exec(object[, globals[, locals]])

This function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). [1] If it is a code object, it is simply executed. In all cases, the code that’s executed is expected to be valid as file input (see the section “File input” in the Reference Manual). Be aware that the
return and yield statements may not be used outside of function definitions even within the context of code passed to the exec() function. The return value is None.

This is a rather strange request. But you can capture the output like this:

>>> s = """The number {% (c) print(x) %} is a random number between 1 and 6
... inclusive. If we multiply it by 2, we get {% (d) print(2*x) %}.
...
... What's interesting is that the statements may appear out of order in the
... document. {% (a) import random %} Thus I might generate the random
... number in a location in the document well after referencing it.
... {% (b) x = random.randint(1,6) %}"""
>>> import re
>>> stmts = re.findall(r'{%s*((w*))s*(.*)%}',s)
>>> stmts
[('c', 'print(x) '), ('d', 'print(2*x) '), ('a', 'import random '), ('b', 'x = random.randint(1,6) ')]

Now, you have to redirect output to some stream which you can manipulate later:

>>> import io
>>> import sys
>>> stream = io.StringIO()
>>> stdout = sys.stdout # this keeps stdout so we can set it back
>>> sys.stdout = stream
>>> for _, statement in sorted(stmts):
...     exec(statement)
...
>>> sys.stdout = stdout # remember to reset stdout!

And now, you can get the values that were printed:

>>> stream.getvalue()
'5
10
'
>>> stream.getvalue().split()
['5', '10']

Although, I think an easier way is to pass a namespace to the dict:

>>> namespace = {}
>>> for _, statement in sorted(stmts):
...     exec(statement, namespace)
...
5
10
>>> namespace.keys()
dict_keys(['__builtins__', 'random', 'x'])

The namespace will get loaded with the normal __builtins__ unless you provide one yourself. So to get every name created in your executed code, you can find the difference between the namspace.keys dictview and a set contiaining the string "__builtins__"

>>> namespace.keys()
dict_keys(['__builtins__', 'random', 'x'])
>>> vals = namespace.keys() - {'__builtins__'}
>>> vals
{'random', 'x'}
>>> for val in vals:
...    print(namespace[val])
...
<module 'random' from '/Users/juan/anaconda3/lib/python3.5/random.py'>
5
>>>

Although, if you are on python 3.4 >= it's a lot easier to redirect stdout to some stream:

>>> import contextlib
>>> stream = io.StringIO()
>>> with contextlib.redirect_stdout(stream):
...     for _, statement in stmts:
...         exec(statement)
...
>>> stream.getvalue()
'5
10
'
>>> stream.getvalue().split()
['5', '10']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...