Fix ipa console filename

THe ipa console command takes an optional filename argument. The
filename argument was broken, because the implementation passed a file
object to exec() instead of a string or compiled object.

ipa console now uses compile() to compile the code with print_function
__future__ feature.

Signed-off-by: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
This commit is contained in:
Christian Heimes 2018-07-12 14:50:40 +02:00
parent 5affc9b982
commit 87904b8f6b
2 changed files with 25 additions and 3 deletions

View File

@ -992,12 +992,18 @@ class console(frontend.Command):
)
if filename:
try:
script = open(filename)
with open(filename) as f:
source = f.read()
except IOError as e:
sys.exit("%s: %s" % (e.filename, e.strerror))
try:
with script:
exec(script, globals(), local)
compiled = compile(
source,
filename,
'exec',
flags=print_function.compiler_flag
)
exec(compiled, globals(), local)
except Exception:
traceback.print_exc()
sys.exit(1)

View File

@ -164,3 +164,19 @@ class TestIPACommand(IntegrationTest):
assert result.returncode == 0
assert "SELinux user map order: {}".format(
maporder) in result.stdout_text
def test_ipa_console(self):
result = self.master.run_command(
["ipa", "console"],
stdin_text="api.env"
)
assert "ipalib.config.Env" in result.stdout_text
filename = tasks.upload_temp_contents(
self.master,
"print(api.env)\n"
)
result = self.master.run_command(
["ipa", "console", filename],
)
assert "ipalib.config.Env" in result.stdout_text