python_exec.c 3.4 KB

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