interfaceMakerPython.cxx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Filename: interfaceMakerPython.cxx
  2. // Created by: drose (21Sep01)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "interfaceMakerPython.h"
  15. #include "interrogate.h"
  16. ////////////////////////////////////////////////////////////////////
  17. // Function: InterfaceMakerPython::Constructor
  18. // Access: Public
  19. // Description:
  20. ////////////////////////////////////////////////////////////////////
  21. InterfaceMakerPython::
  22. InterfaceMakerPython(InterrogateModuleDef *def) :
  23. InterfaceMaker(def)
  24. {
  25. }
  26. ////////////////////////////////////////////////////////////////////
  27. // Function: InterfaceMakerPython::write_includes
  28. // Access: Public, Virtual
  29. // Description: Generates the list of #include ... whatever that's
  30. // required by this particular interface to the
  31. // indicated output stream.
  32. ////////////////////////////////////////////////////////////////////
  33. void InterfaceMakerPython::
  34. write_includes(ostream &out) {
  35. InterfaceMaker::write_includes(out);
  36. out << "#undef _POSIX_C_SOURCE\n"
  37. << "#define PY_SSIZE_T_CLEAN 1\n\n"
  38. << "#if PYTHON_FRAMEWORK\n"
  39. << " #include \"Python/Python.h\"\n"
  40. << "#else\n"
  41. << " #include \"Python.h\"\n"
  42. << "#endif\n";
  43. }
  44. ////////////////////////////////////////////////////////////////////
  45. // Function: InterfaceMakerPython::test_assert
  46. // Access: Protected
  47. // Description: Outputs code to check to see if an assertion has
  48. // failed while the C++ code was executing, and report
  49. // this failure back to Python.
  50. ////////////////////////////////////////////////////////////////////
  51. void InterfaceMakerPython::
  52. test_assert(ostream &out, int indent_level) const {
  53. if (watch_asserts) {
  54. out << "#ifndef NDEBUG\n";
  55. indent(out, indent_level)
  56. << "Notify *notify = Notify::ptr();\n";
  57. indent(out, indent_level)
  58. << "if (notify->has_assert_failed()) {\n";
  59. indent(out, indent_level + 2)
  60. << "PyErr_SetString(PyExc_AssertionError, notify->get_assert_error_message().c_str());\n";
  61. indent(out, indent_level + 2)
  62. << "notify->clear_assert_failed();\n";
  63. indent(out, indent_level + 2)
  64. << "return (PyObject *)NULL;\n";
  65. indent(out, indent_level)
  66. << "}\n";
  67. indent(out, indent_level)
  68. << "if (PyErr_Occurred()) {\n";
  69. indent(out, indent_level + 2)
  70. << "return (PyObject *)NULL;\n";
  71. indent(out, indent_level)
  72. << "}\n";
  73. out << "#endif\n";
  74. }
  75. }