Utilities.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../../Include/Rocket/Core/Python/Utilities.h"
  29. namespace Rocket {
  30. namespace Core {
  31. namespace Python {
  32. bool Utilities::IsCallable(PyObject* self, const char* method_name)
  33. {
  34. PyObject* object = NULL;
  35. // If its a dictionary, access it like a dictionary
  36. if (PyDict_Check(self))
  37. {
  38. object = PyDict_GetItemString(self, (char*)method_name);
  39. // This is a borrowed ref, Incref so the decref below is cancelled out
  40. Py_XINCREF( object );
  41. }
  42. // otherwise attempt normal object access
  43. else
  44. {
  45. object = PyObject_GetAttrString(self, (char*)method_name);
  46. }
  47. // If we couldn't find it, clear error and return false
  48. if (!object)
  49. {
  50. PyErr_Clear();
  51. return false;
  52. }
  53. int callable = PyCallable_Check(object);
  54. Py_DECREF(object);
  55. return callable == 1;
  56. }
  57. bool Utilities::Call(PyObject* object, const char* method_name, const python::tuple& args)
  58. {
  59. PyObject* result = PythonCall(object, method_name, args);
  60. if (result)
  61. Py_DECREF(result);
  62. return result != NULL;
  63. }
  64. // Print the pending python error to stderr, optionally clearing it
  65. void Utilities::PrintError(bool clear_error)
  66. {
  67. // Print the error and restore it to the caller
  68. PyObject *type, *value, *traceback;
  69. PyErr_Fetch(&type, &value, &traceback);
  70. Py_XINCREF(type);
  71. Py_XINCREF(value);
  72. Py_XINCREF(traceback);
  73. PyErr_Restore(type, value, traceback);
  74. PyErr_Print();
  75. if (clear_error)
  76. {
  77. Py_XDECREF(type);
  78. Py_XDECREF(value);
  79. Py_XDECREF(traceback);
  80. }
  81. else
  82. {
  83. PyErr_Restore(type, value, traceback);
  84. }
  85. }
  86. // Converts a python object into an Rocket variant.
  87. bool Utilities::ConvertToVariant(Variant& variant, PyObject* object)
  88. {
  89. if (PyFloat_Check(object))
  90. {
  91. variant.Set((float) PyFloat_AsDouble(object));
  92. return true;
  93. }
  94. else if (PyInt_Check(object))
  95. {
  96. variant.Set((int) PyInt_AsLong(object));
  97. return true;
  98. }
  99. else if (PyString_Check(object))
  100. {
  101. variant.Set(String(PyString_AsString(object)));
  102. return true;
  103. }
  104. return false;
  105. }
  106. // Internal Python call method
  107. PyObject* Utilities::PythonCall(PyObject* object, const char* method_name, const python::tuple& args )
  108. {
  109. ROCKET_ASSERT(IsCallable(object, method_name));
  110. PyObject* result = NULL;
  111. PyObject* method = NULL;
  112. if (PyDict_Check(object))
  113. {
  114. method = PyDict_GetItemString(object, (char*)method_name);
  115. // Borrowed reference, so increase so dec at bottom matches
  116. Py_XINCREF(method);
  117. }
  118. else
  119. {
  120. method = PyObject_GetAttrString(object, (char*)method_name);
  121. }
  122. if (!method)
  123. return NULL;
  124. result = PyObject_CallObject(method, args.ptr());
  125. Py_DECREF(method);
  126. if (!result)
  127. {
  128. PrintError();
  129. return NULL;
  130. }
  131. return result;
  132. }
  133. }
  134. }
  135. }