mirror of
https://github.com/QingdaoU/Judger.git
synced 2024-12-28 16:01:41 +00:00
Merge pull request #9 from ShadowPower/newnew
Make binding compatible with Python 3
This commit is contained in:
commit
e6c6e7c121
@ -11,6 +11,10 @@
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#define PyString_Check PyUnicode_Check
|
||||
#define PyString_AsString(str) str
|
||||
#endif
|
||||
|
||||
static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
|
||||
struct config _config;
|
||||
@ -36,7 +40,6 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
|
||||
}
|
||||
|
||||
if (!PyList_Check(args_list)) {
|
||||
// fixme decref
|
||||
RaiseValueError("args must be a list");
|
||||
}
|
||||
_config.args[count++] = _config.exe_path;
|
||||
@ -47,21 +50,21 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
|
||||
break;
|
||||
}
|
||||
if (!PyString_Check(next)) {
|
||||
// Py_DECREF(next);
|
||||
// free memory before the program exits.
|
||||
Py_DECREF(next);
|
||||
Py_DECREF(args_iter);
|
||||
RaiseValueError("arg item must be a string");
|
||||
}
|
||||
_config.args[count] = PyString_AsString(next);
|
||||
// Py_DECREF(next);
|
||||
Py_DECREF(next);
|
||||
count++;
|
||||
}
|
||||
_config.args[count] = NULL;
|
||||
// Py_DECREF(args_list);
|
||||
// Py_DECREF(args_iter);
|
||||
Py_DECREF(args_iter);
|
||||
|
||||
count = 0;
|
||||
|
||||
if (!PyList_Check(env_list)) {
|
||||
// fixme decref
|
||||
RaiseValueError("env must be a list");
|
||||
}
|
||||
env_iter = PyObject_GetIter(env_list);
|
||||
@ -71,28 +74,25 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
|
||||
break;
|
||||
}
|
||||
if (!PyString_Check(next)) {
|
||||
// Py_DECREF(next);
|
||||
Py_DECREF(next);
|
||||
Py_DECREF(env_iter);
|
||||
RaiseValueError("env item must be a string");
|
||||
}
|
||||
_config.env[count] = PyString_AsString(next);
|
||||
// Py_DECREF(next);
|
||||
count++;
|
||||
Py_DECREF(next);
|
||||
}
|
||||
_config.env[count] = NULL;
|
||||
// Py_DECREF(env_list);
|
||||
// Py_DECREF(env_iter);
|
||||
|
||||
Py_DECREF(env_iter);
|
||||
|
||||
if (PyString_Check(rule_name)) {
|
||||
_config.seccomp_rule_name = PyString_AsString(rule_name);
|
||||
// Py_DECREF(rule_path);
|
||||
}
|
||||
else {
|
||||
if (rule_name == Py_None) {
|
||||
// Py_DECREF(rule_path);
|
||||
_config.seccomp_rule_name = NULL;
|
||||
}
|
||||
else {
|
||||
// fixme decref
|
||||
RaiseValueError("seccomp_rule_name must be string or None");
|
||||
}
|
||||
}
|
||||
@ -101,7 +101,6 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
|
||||
int (*judger_run)(struct config *, struct result *);
|
||||
|
||||
if (!handler) {
|
||||
// fixme decref
|
||||
RaiseValueError("dlopen error")
|
||||
}
|
||||
judger_run = dlsym(handler, "run");
|
||||
@ -119,13 +118,36 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
|
||||
|
||||
|
||||
static PyMethodDef judger_methods[] = {
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
{"run", (PyCFunction) judger_run, METH_VARARGS | METH_KEYWORDS, NULL},
|
||||
#else
|
||||
{"run", (PyCFunction) judger_run, METH_KEYWORDS, NULL},
|
||||
#endif
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
|
||||
PyMODINIT_FUNC init_judger(void) {
|
||||
static PyObject* moduleinit(void) {
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
static struct PyModuleDef judger_def = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"_judger", /* m_name */
|
||||
NULL, /* m_doc */
|
||||
-1, /* m_size */
|
||||
judger_methods, /* m_methods */
|
||||
NULL, /* m_reload */
|
||||
NULL, /* m_traverse */
|
||||
NULL, /* m_clear */
|
||||
NULL, /* m_free */
|
||||
};
|
||||
#endif
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
PyObject *module = PyModule_Create(&judger_def);
|
||||
#else
|
||||
PyObject *module = Py_InitModule3("_judger", judger_methods, NULL);
|
||||
#endif
|
||||
|
||||
PyModule_AddIntConstant(module, "VERSION", VERSION);
|
||||
PyModule_AddIntConstant(module, "UNLIMITED", UNLIMITED);
|
||||
PyModule_AddIntConstant(module, "RESULT_WRONG_ANSWER", WRONG_ANSWER);
|
||||
@ -147,4 +169,18 @@ PyMODINIT_FUNC init_judger(void) {
|
||||
PyModule_AddIntConstant(module, "ERROR_SETUID_FAILED", SETUID_FAILED);
|
||||
PyModule_AddIntConstant(module, "ERROR_EXECVE_FAILED", EXECVE_FAILED);
|
||||
PyModule_AddIntConstant(module, "ERROR_SPJ_ERROR", SPJ_ERROR);
|
||||
|
||||
return module;
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
PyMODINIT_FUNC PyInit__judger(void)
|
||||
{
|
||||
return moduleinit();
|
||||
}
|
||||
#else
|
||||
PyMODINIT_FUNC init_judger(void)
|
||||
{
|
||||
moduleinit();
|
||||
}
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user