PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/nuitka/build/include/nuitka/helper/richcomparisons.hpp

https://bitbucket.org/pombredanne/nuitka
C++ Header | 341 lines | 233 code | 75 blank | 33 comment | 75 complexity | 0d6bd56da33b5a2b8709d219aa773e26 MD5 | raw file
  1. // Copyright 2013, Kay Hayen, mailto:kay.hayen@gmail.com
  2. //
  3. // Part of "Nuitka", an optimizing Python compiler that is compatible and
  4. // integrates with CPython, but also works on its own.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #ifndef __NUITKA_HELPER_RICHCOMPARISONS_H__
  19. #define __NUITKA_HELPER_RICHCOMPARISONS_H__
  20. #define RICH_COMPARE_LT( operand1, operand2 ) _RICH_COMPARE_LT( EVAL_ORDERED_2( operand1, operand2 ) )
  21. NUITKA_MAY_BE_UNUSED static PyObject *_RICH_COMPARE_LT( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  22. {
  23. PyObject *result = PyObject_RichCompare( operand1, operand2, Py_LT );
  24. if (unlikely( result == NULL ))
  25. {
  26. throw PythonException();
  27. }
  28. return result;
  29. }
  30. #define RICH_COMPARE_LE( operand1, operand2 ) _RICH_COMPARE_LE( EVAL_ORDERED_2( operand1, operand2 ) )
  31. NUITKA_MAY_BE_UNUSED static PyObject *_RICH_COMPARE_LE( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  32. {
  33. PyObject *result = PyObject_RichCompare( operand1, operand2, Py_LE );
  34. if (unlikely( result == NULL ))
  35. {
  36. throw PythonException();
  37. }
  38. return result;
  39. }
  40. #define RICH_COMPARE_EQ( operand1, operand2 ) _RICH_COMPARE_EQ( EVAL_ORDERED_2( operand1, operand2 ) )
  41. NUITKA_MAY_BE_UNUSED static PyObject *_RICH_COMPARE_EQ( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  42. {
  43. PyObject *result = PyObject_RichCompare( operand1, operand2, Py_EQ );
  44. if (unlikely( result == NULL ))
  45. {
  46. throw PythonException();
  47. }
  48. return result;
  49. }
  50. #define RICH_COMPARE_NE( operand1, operand2 ) _RICH_COMPARE_NE( EVAL_ORDERED_2( operand1, operand2 ) )
  51. NUITKA_MAY_BE_UNUSED static PyObject *_RICH_COMPARE_NE( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  52. {
  53. PyObject *result = PyObject_RichCompare( operand1, operand2, Py_NE );
  54. if (unlikely( result == NULL ))
  55. {
  56. throw PythonException();
  57. }
  58. return result;
  59. }
  60. #define RICH_COMPARE_GT( operand1, operand2 ) _RICH_COMPARE_GT( EVAL_ORDERED_2( operand1, operand2 ) )
  61. NUITKA_MAY_BE_UNUSED static PyObject *_RICH_COMPARE_GT( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  62. {
  63. PyObject *result = PyObject_RichCompare( operand1, operand2, Py_GT );
  64. if (unlikely( result == NULL ))
  65. {
  66. throw PythonException();
  67. }
  68. return result;
  69. }
  70. #define RICH_COMPARE_GE( operand1, operand2 ) _RICH_COMPARE_GE( EVAL_ORDERED_2( operand1, operand2 ) )
  71. NUITKA_MAY_BE_UNUSED static PyObject *_RICH_COMPARE_GE( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  72. {
  73. PyObject *result = PyObject_RichCompare( operand1, operand2, Py_GE );
  74. if (unlikely( result == NULL ))
  75. {
  76. throw PythonException();
  77. }
  78. return result;
  79. }
  80. #define RICH_COMPARE_BOOL_LT( operand1, operand2 ) _RICH_COMPARE_BOOL_LT( EVAL_ORDERED_2( operand1, operand2 ) )
  81. NUITKA_MAY_BE_UNUSED static bool _RICH_COMPARE_BOOL_LT( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  82. {
  83. PyObject *rich_result = PyObject_RichCompare( operand1, operand2, Py_LT );
  84. if (unlikely( rich_result == NULL ))
  85. {
  86. throw PythonException();
  87. }
  88. bool result;
  89. // Doing the quick tests on the outside spares the function call, with
  90. // "partial inline" this should become unneeded.
  91. if ( rich_result == Py_True )
  92. {
  93. result = true;
  94. }
  95. else if ( rich_result == Py_False || rich_result == Py_None )
  96. {
  97. result = false;
  98. }
  99. else
  100. {
  101. result = CHECK_IF_TRUE( rich_result );
  102. }
  103. Py_DECREF( rich_result );
  104. return result;
  105. }
  106. #define RICH_COMPARE_BOOL_LE( operand1, operand2 ) _RICH_COMPARE_BOOL_LE( EVAL_ORDERED_2( operand1, operand2 ) )
  107. NUITKA_MAY_BE_UNUSED static bool _RICH_COMPARE_BOOL_LE( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  108. {
  109. PyObject *rich_result = PyObject_RichCompare( operand1, operand2, Py_LE );
  110. if (unlikely( rich_result == NULL ))
  111. {
  112. throw PythonException();
  113. }
  114. bool result;
  115. // Doing the quick tests on the outside spares the function call, with
  116. // "partial inline" this should become unneeded.
  117. if ( rich_result == Py_True )
  118. {
  119. result = true;
  120. }
  121. else if ( rich_result == Py_False || rich_result == Py_None )
  122. {
  123. result = false;
  124. }
  125. else
  126. {
  127. result = CHECK_IF_TRUE( rich_result );
  128. }
  129. Py_DECREF( rich_result );
  130. return result;
  131. }
  132. NUITKA_MAY_BE_UNUSED static bool RICH_COMPARE_BOOL_EQ_PARAMETERS( PyObject *operand1, PyObject *operand2 )
  133. {
  134. assertObject( operand1 );
  135. assertObject( operand2 );
  136. PyObject *rich_result = PyObject_RichCompare( operand1, operand2, Py_EQ );
  137. // String comparisons cannot fail they say.
  138. assertObject( rich_result );
  139. bool result;
  140. // Doing the quick tests on the outside spares the function call, with
  141. // "partial inline" this should become unneeded.
  142. if ( rich_result == Py_True )
  143. {
  144. result = true;
  145. }
  146. else if ( rich_result == Py_False || rich_result == Py_None )
  147. {
  148. result = false;
  149. }
  150. else
  151. {
  152. result = CHECK_IF_TRUE( rich_result );
  153. }
  154. Py_DECREF( rich_result );
  155. return result;
  156. }
  157. #define RICH_COMPARE_BOOL_EQ( operand1, operand2 ) _RICH_COMPARE_BOOL_EQ( EVAL_ORDERED_2( operand1, operand2 ) )
  158. NUITKA_MAY_BE_UNUSED static bool _RICH_COMPARE_BOOL_EQ( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  159. {
  160. PyObject *rich_result = PyObject_RichCompare( operand1, operand2, Py_EQ );
  161. if (unlikely( rich_result == NULL ))
  162. {
  163. throw PythonException();
  164. }
  165. bool result;
  166. // Doing the quick tests on the outside spares the function call, with
  167. // "partial inline" this should become unneeded.
  168. if ( rich_result == Py_True )
  169. {
  170. result = true;
  171. }
  172. else if ( rich_result == Py_False || rich_result == Py_None )
  173. {
  174. result = false;
  175. }
  176. else
  177. {
  178. result = CHECK_IF_TRUE( rich_result );
  179. }
  180. Py_DECREF( rich_result );
  181. return result;
  182. }
  183. #define RICH_COMPARE_BOOL_NE( operand1, operand2 ) _RICH_COMPARE_BOOL_NE( EVAL_ORDERED_2( operand1, operand2 ) )
  184. NUITKA_MAY_BE_UNUSED static bool _RICH_COMPARE_BOOL_NE( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  185. {
  186. PyObject *rich_result = PyObject_RichCompare( operand1, operand2, Py_NE );
  187. if (unlikely( rich_result == NULL ))
  188. {
  189. throw PythonException();
  190. }
  191. bool result;
  192. // Doing the quick tests on the outside spares the function call, with
  193. // "partial inline" this should become unneeded.
  194. if ( rich_result == Py_True )
  195. {
  196. result = true;
  197. }
  198. else if ( rich_result == Py_False || rich_result == Py_None )
  199. {
  200. result = false;
  201. }
  202. else
  203. {
  204. result = CHECK_IF_TRUE( rich_result );
  205. }
  206. Py_DECREF( rich_result );
  207. return result;
  208. }
  209. #define RICH_COMPARE_BOOL_GT( operand1, operand2 ) _RICH_COMPARE_BOOL_GT( EVAL_ORDERED_2( operand1, operand2 ) )
  210. NUITKA_MAY_BE_UNUSED static bool _RICH_COMPARE_BOOL_GT( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  211. {
  212. PyObject *rich_result = PyObject_RichCompare( operand1, operand2, Py_GT );
  213. if (unlikely( rich_result == NULL ))
  214. {
  215. throw PythonException();
  216. }
  217. bool result;
  218. // Doing the quick tests on the outside spares the function call, with
  219. // "partial inline" this should become unneeded.
  220. if ( rich_result == Py_True )
  221. {
  222. result = true;
  223. }
  224. else if ( rich_result == Py_False || rich_result == Py_None )
  225. {
  226. result = false;
  227. }
  228. else
  229. {
  230. result = CHECK_IF_TRUE( rich_result );
  231. }
  232. Py_DECREF( rich_result );
  233. return result;
  234. }
  235. #define RICH_COMPARE_BOOL_GE( operand1, operand2 ) _RICH_COMPARE_BOOL_GE( EVAL_ORDERED_2( operand1, operand2 ) )
  236. NUITKA_MAY_BE_UNUSED static bool _RICH_COMPARE_BOOL_GE( EVAL_ORDERED_2( PyObject *operand1, PyObject *operand2 ) )
  237. {
  238. // Quick path for avoidable checks.
  239. if ( operand1 == operand2 )
  240. {
  241. return true;
  242. }
  243. PyObject *rich_result = PyObject_RichCompare( operand1, operand2, Py_GE );
  244. if (unlikely( rich_result == NULL ))
  245. {
  246. throw PythonException();
  247. }
  248. bool result;
  249. // Doing the quick tests on the outside spares the function call, with
  250. // "partial inline" this should become unneeded.
  251. if ( rich_result == Py_True )
  252. {
  253. result = true;
  254. }
  255. else if ( rich_result == Py_False || rich_result == Py_None )
  256. {
  257. result = false;
  258. }
  259. else
  260. {
  261. result = CHECK_IF_TRUE( rich_result );
  262. }
  263. Py_DECREF( rich_result );
  264. return result;
  265. }
  266. #endif