Skip to content

Commit

Permalink
Merge pull request #467 from jack-rann/250-python-execvp
Browse files Browse the repository at this point in the history
Handle Python execution via execvp()
  • Loading branch information
SimonKagstrom authored Sep 30, 2024
2 parents 840b091 + 96abdb5 commit 08a2140
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
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

0 comments on commit 08a2140

Please sign in to comment.