Reposurgeon gives me a SyntaxError when using exec, why?

77 Views Asked by At

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

There are 1 best solutions below

0
On

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")