1
0

convert.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Loco program runner core
  3. * Copyright (C) 2011 Lodevil(Du Jiong)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "convert.h"
  19. int initCalls(PyObject *li, u_char calls[]) {
  20. PyObject *t;
  21. Py_ssize_t len, i;
  22. memset(calls, 0, sizeof(u_char) * CALLS_MAX);
  23. len = PyList_Size(li);
  24. for (i = 0; i < len; i++) {
  25. t = PyList_GetItem(li, i);
  26. if (t == NULL) {return -1;}
  27. #ifdef IS_PY3
  28. if (!PyLong_Check(t))
  29. RAISE1("calls must be a list of numbers.");
  30. calls[PyLong_AsLong(t)] = 1;
  31. #else
  32. if (!PyInt_Check(t) && !PyLong_Check(t))
  33. RAISE1("calls must be a list of numbers.");
  34. calls[PyLong_AsLong(t)] = 1;
  35. #endif
  36. }
  37. return 0;
  38. }
  39. PyObject *genResult(struct Result *rst) {
  40. PyObject *rst_obj, *j, *t, *m;
  41. if ((rst_obj = PyDict_New()) == NULL)
  42. RAISE0("new dict failure");
  43. j = PyLong_FromLong(rst->judge_result);
  44. t = PyLong_FromLong(rst->time_used);
  45. m = PyLong_FromLong(rst->memory_used);
  46. if (!j || !t || !m)
  47. RAISE0("set item falure(1)");
  48. if (PyDict_SetItemString(rst_obj, "result", j)) {
  49. RAISE0("set item falure");
  50. }
  51. if (PyDict_SetItemString(rst_obj, "timeused", t)
  52. || PyDict_SetItemString(rst_obj, "memoryused", m)) {
  53. RAISE0("set item failure");
  54. }
  55. if (rst->re_signum) {
  56. PyDict_SetItemString(rst_obj, "re_signum",
  57. PyLong_FromLong(rst->re_signum));
  58. }
  59. if (rst->re_call != -1) {
  60. PyDict_SetItemString(rst_obj, "re_call", PyLong_FromLong(rst->re_call));
  61. }
  62. if (rst->re_file) {
  63. #ifdef IS_PY3
  64. PyObject *re_file = PyUnicode_FromString(rst->re_file);
  65. #else
  66. PyObject *re_file = PyString_FromString(rst->re_file);
  67. #endif
  68. PyDict_SetItemString(rst_obj, "re_file", re_file);
  69. PyDict_SetItemString(rst_obj, "re_file_flag",
  70. PyLong_FromLong(rst->re_file_flag));
  71. }
  72. return rst_obj;
  73. }
  74. char * const * genRunArgs(PyObject *args_obj) { //generate the argsments for exec*
  75. PyObject *arg;
  76. const char **args;
  77. int len, i;
  78. if (!PyList_Check(args_obj))
  79. RAISE0("args must be a list")
  80. len = PyList_GET_SIZE(args_obj);
  81. args = (const char**) malloc(sizeof(char*) * (len + 1));
  82. for (i = 0; i < len; i++) {
  83. #ifdef IS_PY3
  84. PyObject *arg_bytes;
  85. #endif
  86. if ((arg = PyList_GetItem(args_obj, i)) == NULL) {
  87. free(args);
  88. return NULL;
  89. }
  90. #ifdef IS_PY3
  91. arg_bytes = PyUnicode_AsUTF8String(arg);
  92. if (arg_bytes == NULL) {return NULL;}
  93. args[i] = PyBytes_AsString(arg_bytes);
  94. #else
  95. args[i] = PyString_AsString(arg);
  96. #endif
  97. if (args[i] == NULL) {return NULL;}
  98. }
  99. args[i] = NULL;
  100. return (char * const *) args;
  101. }