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