python_exec.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. #include <Python.h>
  22. #include "../../mem/mem.h"
  23. #include "../../data_lump.h"
  24. #include "../../parser/parse_param.h"
  25. #include "../../parser/msg_parser.h"
  26. #include "../../dprint.h"
  27. #include "../../action.h"
  28. #include "../../config.h"
  29. #include "../../parser/parse_uri.h"
  30. #include "python_exec.h"
  31. #include "python_mod.h"
  32. #include "python_msgobj.h"
  33. #include "python_support.h"
  34. int
  35. python_exec1(struct sip_msg* _msg, char* method_name, char *foobar)
  36. {
  37. return python_exec2(_msg, method_name, NULL);
  38. }
  39. int
  40. python_exec2(struct sip_msg *_msg, char *method_name, char *mystr)
  41. {
  42. PyObject *pFunc, *pArgs, *pValue, *pResult;
  43. PyObject *msg;
  44. int rval;
  45. PyEval_AcquireLock();
  46. PyThreadState_Swap(myThreadState);
  47. pFunc = PyObject_GetAttrString(handler_obj, method_name);
  48. if (pFunc == NULL || !PyCallable_Check(pFunc)) {
  49. LM_ERR("%s not found or is not callable\n", method_name);
  50. Py_XDECREF(pFunc);
  51. PyThreadState_Swap(NULL);
  52. PyEval_ReleaseLock();
  53. return -1;
  54. }
  55. msg = newmsgobject(_msg);
  56. if (msg == NULL) {
  57. LM_ERR("can't create MSGtype instance\n");
  58. Py_DECREF(pFunc);
  59. PyThreadState_Swap(NULL);
  60. PyEval_ReleaseLock();
  61. return -1;
  62. }
  63. pArgs = PyTuple_New(mystr == NULL ? 1 : 2);
  64. if (pArgs == NULL) {
  65. LM_ERR("PyTuple_New() has failed\n");
  66. msg_invalidate(msg);
  67. Py_DECREF(msg);
  68. Py_DECREF(pFunc);
  69. PyThreadState_Swap(NULL);
  70. PyEval_ReleaseLock();
  71. return -1;
  72. }
  73. PyTuple_SetItem(pArgs, 0, msg);
  74. /* Tuple steals msg */
  75. if (mystr != NULL) {
  76. pValue = PyString_FromString(mystr);
  77. if (pValue == NULL) {
  78. LM_ERR("PyString_FromString(%s) has failed\n", mystr);
  79. msg_invalidate(msg);
  80. Py_DECREF(pArgs);
  81. Py_DECREF(pFunc);
  82. PyThreadState_Swap(NULL);
  83. PyEval_ReleaseLock();
  84. return -1;
  85. }
  86. PyTuple_SetItem(pArgs, 1, pValue);
  87. /* Tuple steals pValue */
  88. }
  89. pResult = PyObject_CallObject(pFunc, pArgs);
  90. msg_invalidate(msg);
  91. Py_DECREF(pArgs);
  92. Py_DECREF(pFunc);
  93. if (PyErr_Occurred()) {
  94. Py_XDECREF(pResult);
  95. python_handle_exception("python_exec2");
  96. PyThreadState_Swap(NULL);
  97. PyEval_ReleaseLock();
  98. return -1;
  99. }
  100. if (pResult == NULL) {
  101. LM_ERR("PyObject_CallObject() returned NULL\n");
  102. PyThreadState_Swap(NULL);
  103. PyEval_ReleaseLock();
  104. return -1;
  105. }
  106. rval = PyInt_AsLong(pResult);
  107. Py_DECREF(pResult);
  108. PyThreadState_Swap(NULL);
  109. PyEval_ReleaseLock();
  110. return rval;
  111. }