Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
8.3 KiB

  1. #ifndef Py_PYFPE_H
  2. #define Py_PYFPE_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /*
  7. ---------------------------------------------------------------------
  8. / Copyright (c) 1996. \
  9. | The Regents of the University of California. |
  10. | All rights reserved. |
  11. | |
  12. | Permission to use, copy, modify, and distribute this software for |
  13. | any purpose without fee is hereby granted, provided that this en- |
  14. | tire notice is included in all copies of any software which is or |
  15. | includes a copy or modification of this software and in all |
  16. | copies of the supporting documentation for such software. |
  17. | |
  18. | This work was produced at the University of California, Lawrence |
  19. | Livermore National Laboratory under contract no. W-7405-ENG-48 |
  20. | between the U.S. Department of Energy and The Regents of the |
  21. | University of California for the operation of UC LLNL. |
  22. | |
  23. | DISCLAIMER |
  24. | |
  25. | This software was prepared as an account of work sponsored by an |
  26. | agency of the United States Government. Neither the United States |
  27. | Government nor the University of California nor any of their em- |
  28. | ployees, makes any warranty, express or implied, or assumes any |
  29. | liability or responsibility for the accuracy, completeness, or |
  30. | usefulness of any information, apparatus, product, or process |
  31. | disclosed, or represents that its use would not infringe |
  32. | privately-owned rights. Reference herein to any specific commer- |
  33. | cial products, process, or service by trade name, trademark, |
  34. | manufacturer, or otherwise, does not necessarily constitute or |
  35. | imply its endorsement, recommendation, or favoring by the United |
  36. | States Government or the University of California. The views and |
  37. | opinions of authors expressed herein do not necessarily state or |
  38. | reflect those of the United States Government or the University |
  39. | of California, and shall not be used for advertising or product |
  40. \ endorsement purposes. /
  41. ---------------------------------------------------------------------
  42. */
  43. /*
  44. * Define macros for handling SIGFPE.
  45. * Lee Busby, LLNL, November, 1996
  46. * busby1@llnl.gov
  47. *
  48. *********************************************
  49. * Overview of the system for handling SIGFPE:
  50. *
  51. * This file (Include/pyfpe.h) defines a couple of "wrapper" macros for
  52. * insertion into your Python C code of choice. Their proper use is
  53. * discussed below. The file Python/pyfpe.c defines a pair of global
  54. * variables PyFPE_jbuf and PyFPE_counter which are used by the signal
  55. * handler for SIGFPE to decide if a particular exception was protected
  56. * by the macros. The signal handler itself, and code for enabling the
  57. * generation of SIGFPE in the first place, is in a (new) Python module
  58. * named fpectl. This module is standard in every respect. It can be loaded
  59. * either statically or dynamically as you choose, and like any other
  60. * Python module, has no effect until you import it.
  61. *
  62. * In the general case, there are three steps toward handling SIGFPE in any
  63. * Python code:
  64. *
  65. * 1) Add the *_PROTECT macros to your C code as required to protect
  66. * dangerous floating point sections.
  67. *
  68. * 2) Turn on the inclusion of the code by adding the ``--with-fpectl''
  69. * flag at the time you run configure. If the fpectl or other modules
  70. * which use the *_PROTECT macros are to be dynamically loaded, be
  71. * sure they are compiled with WANT_SIGFPE_HANDLER defined.
  72. *
  73. * 3) When python is built and running, import fpectl, and execute
  74. * fpectl.turnon_sigfpe(). This sets up the signal handler and enables
  75. * generation of SIGFPE whenever an exception occurs. From this point
  76. * on, any properly trapped SIGFPE should result in the Python
  77. * FloatingPointError exception.
  78. *
  79. * Step 1 has been done already for the Python kernel code, and should be
  80. * done soon for the NumPy array package. Step 2 is usually done once at
  81. * python install time. Python's behavior with respect to SIGFPE is not
  82. * changed unless you also do step 3. Thus you can control this new
  83. * facility at compile time, or run time, or both.
  84. *
  85. ********************************
  86. * Using the macros in your code:
  87. *
  88. * static PyObject *foobar(PyObject *self,PyObject *args)
  89. * {
  90. * ....
  91. * PyFPE_START_PROTECT("Error in foobar", return 0)
  92. * result = dangerous_op(somearg1, somearg2, ...);
  93. * PyFPE_END_PROTECT(result)
  94. * ....
  95. * }
  96. *
  97. * If a floating point error occurs in dangerous_op, foobar returns 0 (NULL),
  98. * after setting the associated value of the FloatingPointError exception to
  99. * "Error in foobar". ``Dangerous_op'' can be a single operation, or a block
  100. * of code, function calls, or any combination, so long as no alternate
  101. * return is possible before the PyFPE_END_PROTECT macro is reached.
  102. *
  103. * The macros can only be used in a function context where an error return
  104. * can be recognized as signaling a Python exception. (Generally, most
  105. * functions that return a PyObject * will qualify.)
  106. *
  107. * Guido's original design suggestion for PyFPE_START_PROTECT and
  108. * PyFPE_END_PROTECT had them open and close a local block, with a locally
  109. * defined jmp_buf and jmp_buf pointer. This would allow recursive nesting
  110. * of the macros. The Ansi C standard makes it clear that such local
  111. * variables need to be declared with the "volatile" type qualifier to keep
  112. * setjmp from corrupting their values. Some current implementations seem
  113. * to be more restrictive. For example, the HPUX man page for setjmp says
  114. *
  115. * Upon the return from a setjmp() call caused by a longjmp(), the
  116. * values of any non-static local variables belonging to the routine
  117. * from which setjmp() was called are undefined. Code which depends on
  118. * such values is not guaranteed to be portable.
  119. *
  120. * I therefore decided on a more limited form of nesting, using a counter
  121. * variable (PyFPE_counter) to keep track of any recursion. If an exception
  122. * occurs in an ``inner'' pair of macros, the return will apparently
  123. * come from the outermost level.
  124. *
  125. */
  126. #ifdef WANT_SIGFPE_HANDLER
  127. #include <signal.h>
  128. #include <setjmp.h>
  129. #include <math.h>
  130. extern jmp_buf PyFPE_jbuf;
  131. extern int PyFPE_counter;
  132. extern double PyFPE_dummy(void *);
  133. #define PyFPE_START_PROTECT(err_string, leave_stmt) \
  134. if (!PyFPE_counter++ && setjmp(PyFPE_jbuf)) { \
  135. PyErr_SetString(PyExc_FloatingPointError, err_string); \
  136. PyFPE_counter = 0; \
  137. leave_stmt; \
  138. }
  139. /*
  140. * This (following) is a heck of a way to decrement a counter. However,
  141. * unless the macro argument is provided, code optimizers will sometimes move
  142. * this statement so that it gets executed *before* the unsafe expression
  143. * which we're trying to protect. That pretty well messes things up,
  144. * of course.
  145. *
  146. * If the expression(s) you're trying to protect don't happen to return a
  147. * value, you will need to manufacture a dummy result just to preserve the
  148. * correct ordering of statements. Note that the macro passes the address
  149. * of its argument (so you need to give it something which is addressable).
  150. * If your expression returns multiple results, pass the last such result
  151. * to PyFPE_END_PROTECT.
  152. *
  153. * Note that PyFPE_dummy returns a double, which is cast to int.
  154. * This seeming insanity is to tickle the Floating Point Unit (FPU).
  155. * If an exception has occurred in a preceding floating point operation,
  156. * some architectures (notably Intel 80x86) will not deliver the interrupt
  157. * until the *next* floating point operation. This is painful if you've
  158. * already decremented PyFPE_counter.
  159. */
  160. #define PyFPE_END_PROTECT(v) PyFPE_counter -= (int)PyFPE_dummy(&(v));
  161. #else
  162. #define PyFPE_START_PROTECT(err_string, leave_stmt)
  163. #define PyFPE_END_PROTECT(v)
  164. #endif
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif /* !Py_PYFPE_H */