args list and env list parameters now support default value

This commit is contained in:
virusdefender 2016-01-20 20:13:48 +08:00
parent 194edfe2b9
commit c44ff13736

View File

@ -8,54 +8,17 @@ static PyObject *error;
static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
struct config config;
struct result result = {0, 0, 0, 0, 0};
PyObject *args_list, *env_list;
PyObject *args_list = NULL, *env_list = NULL, *next = NULL, *args_iter = NULL, *env_iter = NULL;
int count = 0;
static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time", "max_memory", "args", "env", NULL};
config.path = config.in_file = config.out_file = NULL;
static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time", "max_memory", "args", "env", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssiiOO", kwargs_list, &(config.path), &(config.in_file),
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssii|OO", kwargs_list, &(config.path), &(config.in_file),
&(config.out_file), &(config.max_cpu_time), &(config.max_memory),
&args_list, &env_list)) {
PyErr_SetString(error, "Invalid args and kwargs");
return NULL;
}
PyObject *args_iter = PyObject_GetIter(args_list);
while (1) {
PyObject *next = PyIter_Next((args_iter));
if (!next) {
break;
}
if (!PyString_Check(next)) {
PyErr_SetString(error, "arg must be string");
return NULL;
}
config.args[count] = PyString_AsString(next);
count++;
}
config.args[count] = NULL;
count = 0;
PyObject *env_iter = PyObject_GetIter(env_list);
while (1) {
PyObject *next = PyIter_Next(env_iter);
if (!next) {
break;
}
if (!PyString_Check(next)) {
PyErr_SetString(error, "env must be string");
return NULL;
}
config.env[count] = PyString_AsString(next);
count++;
}
config.env[count] = NULL;
if (!PyList_Check(args_list) || !PyList_Check(env_list)) {
PyErr_SetString(error, "args and env must be a list");
return NULL;
}
if (config.max_cpu_time <= 1) {
PyErr_SetString(error, "Max cpu time can not less than 1 ms");
return NULL;
@ -72,8 +35,50 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
PyErr_SetString(error, "Input file does not exist");
return NULL;
}
if (args_list != NULL) {
if (!PyList_Check(args_list)) {
PyErr_SetString(error, "args must be a list");
return NULL;
}
args_iter = PyObject_GetIter(args_list);
while (1) {
next = PyIter_Next((args_iter));
if (!next) {
break;
}
if (!PyString_Check(next)) {
PyErr_SetString(error, "arg must be string");
return NULL;
}
config.args[count] = PyString_AsString(next);
count++;
}
}
config.args[count] = NULL;
count = 0;
if (env_list != NULL) {
if (!PyList_Check(env_list)) {
PyErr_SetString(error, "env must be a list");
return NULL;
}
env_iter = PyObject_GetIter(env_list);
while (1) {
next = PyIter_Next(env_iter);
if (!next) {
break;
}
if (!PyString_Check(next)) {
PyErr_SetString(error, "env must be string");
return NULL;
}
config.env[count] = PyString_AsString(next);
count++;
}
}
config.env[count] = NULL;
// printf("%s %s %s %d %d", config.path, config.in_file, config.out_file, config.max_cpu_time, config.max_memory);
run(&config, &result);
return Py_BuildValue("{s:i, s:l, s:i, s:i, s:i, s:i}",
"cpu_time", result.cpu_time, "memory", result.memory, "real_time", result.real_time, "signal",