Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Python execution via execvp() #467

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/engines/python-engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,25 @@ class PythonEngine : public ScriptEngineBase
const char **argv = conf.getArgv();
unsigned int argc = conf.getArgc();

std::string s = fmt("%s %s ", command.c_str(), kcov_python_path.c_str());
for (unsigned int i = 0; i < argc; i++)
s += "'" + std::string(argv[i]) + "' ";

int res;
// Make a copy of the vector, with python + helper script first
char **vec;
int argcStart = 2;
vec = (char **) xmalloc(sizeof(char *) * (argc + 3));
vec[0] = xstrdup(command.c_str());
vec[1] = xstrdup(kcov_python_path.c_str());

for (unsigned i = 0; i < argc; i++)
vec[argcStart + i] = xstrdup(argv[i]);

/* Execute the script */
if (execvp(vec[0], vec))
{
perror("Failed to execute python helper");

res = system(s.c_str());
panic_if(res < 0, "Can't execute python helper");
free(vec);

exit(WEXITSTATUS(res));
return false;
}
}
else if (m_child < 0)
{
Expand Down
10 changes: 10 additions & 0 deletions tests/python/echo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python3

import sys

def main():
print('echo:', sys.argv)


if __name__ == '__main__':
main()
11 changes: 11 additions & 0 deletions tests/tools/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,14 @@ def runTest(self):
dom = cobertura.parseFile(self.outbase + "/kcov/main/cobertura.xml")
assert cobertura.hitsPerLine(dom, "second.py", 57) == 1
assert cobertura.hitsPerLine(dom, "second.py", 61) == 1


class python_single_quote_arg(libkcov.TestCase):
def runTest(self):
rv, o = self.do(
self.kcov + " " + self.outbase + "/kcov " + self.sources + "/tests/python/echo.py 1 two a'b"
)

assert b"1" in o
assert b"two" in o
assert b"a'b" in o
Loading