Reposurgeon gives me a SyntaxError when using exec, why?

孤街醉人 提交于 2019-12-11 11:03:50

问题


Using reposurgeon and trying to extend its functionality, I am faced with:

reposurgeon: invalid syntax in extension function

which translates to a SyntaxError extension raised from the execfile() call in RepoSurgeon.do_exec(). What gives? The code I am trying to exec is as simple as:

print "Hello world"

I have also used the Python CLI and execfile and there are no complaints whatsoever?

Used version: reposurgeon 3.10


回答1:


This one took me a while to figure out, which is why I am posting it here.

The key is indeed in the single line of code we're trying to "source". While this is perfectly valid Python 2.x code, reposurgeon uses the print function from Python 3.x by doing:

from __future__ import print_function

Which causes print to require the use of parentheses, as it makes print a function instead of a statement.

Obviously we're running our extension code in the context of reposurgeon, which means that we're dependent on the rules it defines.

See this document.

Hence the following will work just fine:

print("Hello world")


来源:https://stackoverflow.com/questions/25146005/reposurgeon-gives-me-a-syntaxerror-when-using-exec-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!