/src/gmpy_args.h

http://gmpy.googlecode.com/ · C Header · 630 lines · 460 code · 33 blank · 137 comment · 208 complexity · 2a02ab486727445e162c117d1da48ead MD5 · raw file

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. * gmpy_args.h *
  3. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4. * Python interface to the GMP or MPIR, MPFR, and MPC multiple precision *
  5. * *
  6. * Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
  7. * 2008, 2009 Alex Martelli *
  8. * *
  9. * Copyright 2008, 2009, 2010, 2011, 2012, 2013 Case Van Horsen *
  10. * *
  11. * This file is part of GMPY2. *
  12. * *
  13. * GMPY2 is free software: you can redistribute it and/or modify it under *
  14. * the terms of the GNU Lesser General Public License as published by the *
  15. * Free Software Foundation, either version 3 of the License, or (at your *
  16. * option) any later version. *
  17. * *
  18. * GMPY2 is distributed in the hope that it will be useful, but WITHOUT *
  19. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
  20. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
  21. * License for more details. *
  22. * *
  23. * You should have received a copy of the GNU Lesser General Public *
  24. * License along with GMPY2; if not, see <http://www.gnu.org/licenses/> *
  25. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  26. /* Various macros for parsing arguments. */
  27. #ifndef GMPY_ARG_H
  28. #define GMPY_ARG_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /*
  33. * Create two 'mpz' and a 2-tuple.
  34. */
  35. #define CREATE_TWO_MPZ_TUPLE(q, r, t) \
  36. q = (PympzObject*)Pympz_new(); \
  37. r = (PympzObject*)Pympz_new(); \
  38. t = PyTuple_New(2); \
  39. if (!q || !r || !t) { \
  40. Py_XDECREF(t); \
  41. Py_XDECREF((PyObject*)q); \
  42. Py_XDECREF((PyObject*)r); \
  43. return NULL; \
  44. }
  45. /*
  46. * Parses one, and only one, argument into "self" and converts it to an
  47. * mpz. Is faster, but not as generic, as using PyArg_ParseTuple. It
  48. * supports either gmpy.fname(z) or z.fname(). "self" must be decref'ed.
  49. * "msg" should be an error message that includes the function name and
  50. * describes the required arguments. Replaces SELF_MPZ_NO_ARG.
  51. *
  52. * Also considers an 'xmpz' to be equivalent to an 'mpz'.
  53. */
  54. #define PARSE_ONE_MPZ(msg) \
  55. if (self && CHECK_MPZANY(self)) { \
  56. if (PyTuple_GET_SIZE(args) != 0) { \
  57. PyErr_SetString(PyExc_TypeError, msg); \
  58. return NULL; \
  59. } \
  60. Py_INCREF(self); \
  61. } else { \
  62. if (PyTuple_GET_SIZE(args) != 1) { \
  63. PyErr_SetString(PyExc_TypeError, msg); \
  64. return NULL; \
  65. } \
  66. self = PyTuple_GET_ITEM(args, 0); \
  67. if(CHECK_MPZANY(self)) { \
  68. Py_INCREF((PyObject*)self); \
  69. } else { \
  70. self = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  71. } \
  72. if (!self) { \
  73. PyErr_SetString(PyExc_TypeError, msg); \
  74. return NULL; \
  75. } \
  76. }
  77. /*
  78. * Parses one, and only one, argument into "self" and converts it to an
  79. * mpfr. Is faster, but not as generic, as using PyArg_ParseTuple. It
  80. * supports either gmpy.fname(z) or z.fname(). "self" must be decref'ed.
  81. * "msg" should be an error message that includes the function name and
  82. * describes the required arguments. It assumes the functions is declared
  83. * as either METH_O or METH_NOARGS. It is faster than PARSE_ONE_MPFR and
  84. * passing a tuple as args.
  85. */
  86. #define PARSE_ONE_MPFR_OTHER(msg) \
  87. if (self && Pympfr_CheckAndExp(self)) { \
  88. Py_INCREF(self); \
  89. } \
  90. else if (Pympfr_CheckAndExp(other)) { \
  91. self = other; \
  92. Py_INCREF((PyObject*)self); \
  93. } \
  94. else if (!(self = (PyObject*)Pympfr_From_Real(other, 0))) { \
  95. PyErr_SetString(PyExc_TypeError, msg); \
  96. return NULL; \
  97. }
  98. /*
  99. * Parses one argument into "self" and an optional second argument into
  100. * "var". The second argument is converted into a C long. If there is not a
  101. * second argument, "var" is unchanged. Is faster, but not as generic, as
  102. * using PyArg_ParseTuple with "|l". It supports either gmpy.fname(z,l) or
  103. * z.fname(l). "self" must be decref'ed. "var" must be a pointer to a long.
  104. * "msg" should be an error message that includes the function name and
  105. * describes the required arguments. Replaces some uses of SELF_MPZ_ONE_ARG.
  106. *
  107. * Also considers an 'xmpz' to be equivalent to an 'mpz'.
  108. */
  109. #define PARSE_ONE_MPZ_OPT_CLONG(var, msg) \
  110. if (self && CHECK_MPZANY(self)) { \
  111. if (PyTuple_GET_SIZE(args) == 1) { \
  112. *var = clong_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  113. if (*var == -1 && PyErr_Occurred()) { \
  114. PyErr_SetString(PyExc_TypeError, msg); \
  115. return NULL; \
  116. } \
  117. } \
  118. else if (PyTuple_GET_SIZE(args) > 1) { \
  119. PyErr_SetString(PyExc_TypeError, msg); \
  120. return NULL; \
  121. } \
  122. Py_INCREF(self); \
  123. } \
  124. else { \
  125. if (PyTuple_GET_SIZE(args) == 2) { \
  126. *var = clong_From_Integer(PyTuple_GET_ITEM(args, 1)); \
  127. if (*var == -1 && PyErr_Occurred()) { \
  128. PyErr_SetString(PyExc_TypeError, msg); \
  129. return NULL; \
  130. } \
  131. self = PyTuple_GET_ITEM(args, 0); \
  132. if (CHECK_MPZANY(self)) { \
  133. Py_INCREF((PyObject*)self); \
  134. } \
  135. else { \
  136. self = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  137. } \
  138. } \
  139. else if (PyTuple_GET_SIZE(args) == 1) { \
  140. self = PyTuple_GET_ITEM(args, 0); \
  141. if (CHECK_MPZANY(self)) { \
  142. Py_INCREF((PyObject*)self); \
  143. } \
  144. else { \
  145. self = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  146. } \
  147. } \
  148. else { \
  149. PyErr_SetString(PyExc_TypeError, msg); \
  150. return NULL; \
  151. } \
  152. if (!self) { \
  153. PyErr_SetString(PyExc_TypeError, msg); \
  154. return NULL; \
  155. } \
  156. }
  157. /* Parses one argument into "self" and an optional second argument into
  158. * "var". The second argument is converted into an mpir_si. If there is not a
  159. * second argument, "var" is unchanged. It supports either gmpy.fname(z,l) or
  160. * z.fname(l). "self" must be decref'ed. "var" must be a pointer to an mpir_si.
  161. * "msg" should be an error message that includes the function name and
  162. * describes the required arguments. Replaces some uses of SELF_MPZ_ONE_ARG.
  163. *
  164. * Also considers an 'xmpz' to be equivalent to an 'mpz'.
  165. */
  166. #define PARSE_ONE_MPZ_OPT_SI(var, msg) \
  167. if (self && CHECK_MPZANY(self)) { \
  168. if (PyTuple_GET_SIZE(args) == 1) { \
  169. *var = SI_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  170. if (*var == -1 && PyErr_Occurred()) { \
  171. PyErr_SetString(PyExc_TypeError, msg); \
  172. return NULL; \
  173. } \
  174. } \
  175. else if (PyTuple_GET_SIZE(args) > 1) { \
  176. PyErr_SetString(PyExc_TypeError, msg); \
  177. return NULL; \
  178. } \
  179. Py_INCREF(self); \
  180. } \
  181. else { \
  182. if (PyTuple_GET_SIZE(args) == 2) { \
  183. *var = SI_From_Integer(PyTuple_GET_ITEM(args, 1)); \
  184. if (*var == -1 && PyErr_Occurred()) { \
  185. PyErr_SetString(PyExc_TypeError, msg); \
  186. return NULL; \
  187. } \
  188. self = PyTuple_GET_ITEM(args, 0); \
  189. if (CHECK_MPZANY(self)) { \
  190. Py_INCREF((PyObject*)self); \
  191. } \
  192. else { \
  193. self = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  194. } \
  195. } \
  196. else if (PyTuple_GET_SIZE(args) == 1) { \
  197. self = PyTuple_GET_ITEM(args, 0); \
  198. if (CHECK_MPZANY(self)) { \
  199. Py_INCREF((PyObject*)self); \
  200. } \
  201. else { \
  202. self = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  203. } \
  204. } \
  205. else { \
  206. PyErr_SetString(PyExc_TypeError, msg); \
  207. return NULL; \
  208. } \
  209. if (!self) { \
  210. PyErr_SetString(PyExc_TypeError, msg); \
  211. return NULL; \
  212. } \
  213. }
  214. /*
  215. * Parses one argument into "self" and an optional second argument into "var".
  216. * The second argument is converted into a Py_ssize_t. If there is not a
  217. * second argument, "var" is unchanged. Is faster, but not as generic, as
  218. * using PyArg_ParseTuple with "|l". It supports either gmpy.fname(z,l) or
  219. * z.fname(l). "self" must be decref'ed. "var" must be a pointer to a
  220. * Py_ssize_t. "msg" should be an error message that includes the function
  221. * name and describes the required arguments. Replaces some uses of
  222. * SELF_MPZ_ONE_ARG.
  223. *
  224. * Also considers an 'xmpz' to be equivalent to an 'mpz'.
  225. */
  226. #define PARSE_ONE_MPZ_OPT_SSIZE_T(var, msg) \
  227. if (self && CHECK_MPZANY(self)) { \
  228. if (PyTuple_GET_SIZE(args) == 1) { \
  229. *var = ssize_t_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  230. if(*var == -1 && PyErr_Occurred()) { \
  231. PyErr_SetString(PyExc_TypeError, msg); \
  232. return NULL; \
  233. } \
  234. } \
  235. else if (PyTuple_GET_SIZE(args) > 1) { \
  236. PyErr_SetString(PyExc_TypeError, msg); \
  237. return NULL; \
  238. } \
  239. Py_INCREF(self); \
  240. } \
  241. else { \
  242. if (PyTuple_GET_SIZE(args) == 2) { \
  243. *var = ssize_t_From_Integer(PyTuple_GET_ITEM(args, 1)); \
  244. if (*var == -1 && PyErr_Occurred()) { \
  245. PyErr_SetString(PyExc_TypeError, msg); \
  246. return NULL; \
  247. } \
  248. self = PyTuple_GET_ITEM(args, 0); \
  249. if (CHECK_MPZANY(self)) { \
  250. Py_INCREF((PyObject*)self); \
  251. } \
  252. else { \
  253. self = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  254. } \
  255. } \
  256. else if (PyTuple_GET_SIZE(args) == 1) { \
  257. self = PyTuple_GET_ITEM(args, 0); \
  258. if (CHECK_MPZANY(self)) { \
  259. Py_INCREF((PyObject*)self); \
  260. } \
  261. else { \
  262. self = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  263. } \
  264. } \
  265. else { \
  266. PyErr_SetString(PyExc_TypeError, msg); \
  267. return NULL; \
  268. } \
  269. if (!self) { \
  270. PyErr_SetString(PyExc_TypeError, msg); \
  271. return NULL; \
  272. } \
  273. }
  274. /*
  275. * Parses one argument into "self" and an optional second argument into
  276. * "var". The second argument is converted into a C long. If there is not a
  277. * second argument, "var" is unchanged. Is faster, but not as generic, as
  278. * using PyArg_ParseTuple with "|l". It supports either gmpy.fname(z,l) or
  279. * z.fname(l). "self" must be decref'ed. "var" must be a pointer to a long.
  280. * "msg" should be an error message that includes the function name and
  281. * describes the required arguments. Replaces some uses of SELF_MPF_ONE_ARG.
  282. */
  283. #define PARSE_ONE_MPFR_OPT_CLONG(var, msg) \
  284. if (self && Pympfr_CheckAndExp(self)) { \
  285. if (PyTuple_GET_SIZE(args) == 1) { \
  286. *var = clong_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  287. if (*var == -1 && PyErr_Occurred()) { \
  288. PyErr_SetString(PyExc_TypeError, msg); \
  289. return NULL; \
  290. } \
  291. } \
  292. else if (PyTuple_GET_SIZE(args) > 1) { \
  293. PyErr_SetString(PyExc_TypeError, msg); \
  294. return NULL; \
  295. } \
  296. Py_INCREF(self); \
  297. } \
  298. else { \
  299. if (PyTuple_GET_SIZE(args) == 2) { \
  300. *var = clong_From_Integer(PyTuple_GET_ITEM(args, 1)); \
  301. if (*var == -1 && PyErr_Occurred()) { \
  302. PyErr_SetString(PyExc_TypeError, msg); \
  303. return NULL; \
  304. } \
  305. self = PyTuple_GET_ITEM(args, 0); \
  306. if (Pympfr_CheckAndExp(self)) { \
  307. Py_INCREF((PyObject*)self); \
  308. } \
  309. else { \
  310. self = (PyObject*)Pympfr_From_Real(PyTuple_GET_ITEM(args, 0), 0); \
  311. } \
  312. } \
  313. else if (PyTuple_GET_SIZE(args) == 1) { \
  314. self = PyTuple_GET_ITEM(args, 0); \
  315. if(Pympfr_CheckAndExp(self)) { \
  316. Py_INCREF((PyObject*)self); \
  317. } \
  318. else { \
  319. self = (PyObject*)Pympfr_From_Real(self, 0); \
  320. } \
  321. } \
  322. else { \
  323. PyErr_SetString(PyExc_TypeError, msg); \
  324. return NULL; \
  325. } \
  326. if (!self) { \
  327. PyErr_SetString(PyExc_TypeError, msg); \
  328. return NULL; \
  329. } \
  330. }
  331. /*
  332. * Parses one argument into "self" and a required second argument into
  333. * "var". The second argument is converted into a C long. Is faster, but not
  334. * as generic, as using PyArg_ParseTuple with "l". It supports either
  335. * gmpy.fname(z,l) or z.fname(l). "self" must be decref'ed. "var" must be a
  336. * pointer to a long. "msg" should be an error message that includes the
  337. * function name and describes the required arguments. Replaces some uses of
  338. * SELF_MPZ_ONE_ARG.
  339. *
  340. * Also considers an 'xmpz' to be equivalent to an 'mpz'.
  341. */
  342. #define PARSE_ONE_MPZ_REQ_CLONG(var, msg) \
  343. if (self && CHECK_MPZANY(self)) { \
  344. if (PyTuple_GET_SIZE(args) != 1) { \
  345. PyErr_SetString(PyExc_TypeError, msg); \
  346. return NULL; \
  347. } \
  348. else { \
  349. *var = clong_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  350. if (*var == -1 && PyErr_Occurred()) { \
  351. PyErr_SetString(PyExc_TypeError, msg); \
  352. return NULL; \
  353. } \
  354. } \
  355. Py_INCREF(self); \
  356. } \
  357. else { \
  358. if (PyTuple_GET_SIZE(args) != 2) { \
  359. PyErr_SetString(PyExc_TypeError, msg); \
  360. return NULL; \
  361. } \
  362. else {\
  363. *var = clong_From_Integer(PyTuple_GET_ITEM(args, 1)); \
  364. if (*var == -1 && PyErr_Occurred()) { \
  365. PyErr_SetString(PyExc_TypeError, msg); \
  366. return NULL; \
  367. } \
  368. self = PyTuple_GET_ITEM(args, 0); \
  369. if (CHECK_MPZANY(self)) { \
  370. Py_INCREF((PyObject*)self); \
  371. } \
  372. else { \
  373. self = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  374. } \
  375. } \
  376. if (!self) { \
  377. PyErr_SetString(PyExc_TypeError, msg); \
  378. return NULL; \
  379. } \
  380. }
  381. /* Parses one argument into "self" and a required second argument into
  382. * "var". The second argument is converted into an mpir_si. It supports either
  383. * gmpy.fname(z,l) or z.fname(l). "self" must be decref'ed. "var" must be a
  384. * pointer to an mpir_si. "msg" should be an error message that includes the
  385. * function name and describes the required arguments. Replaces some uses of
  386. * SELF_MPZ_ONE_ARG.
  387. *
  388. * Also considers an 'xmpz' to be equivalent to an 'mpz'.
  389. */
  390. #define PARSE_ONE_MPZ_REQ_SI(var, msg) \
  391. if (self && CHECK_MPZANY(self)) { \
  392. if (PyTuple_GET_SIZE(args) != 1) { \
  393. PyErr_SetString(PyExc_TypeError, msg); \
  394. return NULL; \
  395. } \
  396. else { \
  397. *var = SI_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  398. if (*var == -1 && PyErr_Occurred()) { \
  399. PyErr_SetString(PyExc_TypeError, msg); \
  400. return NULL; \
  401. } \
  402. } \
  403. Py_INCREF(self); \
  404. } \
  405. else { \
  406. if (PyTuple_GET_SIZE(args) != 2) { \
  407. PyErr_SetString(PyExc_TypeError, msg); \
  408. return NULL; \
  409. } \
  410. else { \
  411. *var = SI_From_Integer(PyTuple_GET_ITEM(args, 1)); \
  412. if (*var == -1 && PyErr_Occurred()) { \
  413. PyErr_SetString(PyExc_TypeError, msg); \
  414. return NULL; \
  415. } \
  416. self = PyTuple_GET_ITEM(args, 0); \
  417. if (CHECK_MPZANY(self)) { \
  418. Py_INCREF((PyObject*)self); \
  419. } \
  420. else { \
  421. self = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  422. } \
  423. } \
  424. if (!self) { \
  425. PyErr_SetString(PyExc_TypeError, msg); \
  426. return NULL; \
  427. } \
  428. }
  429. /*
  430. * Parses one argument into "self" and a required second argument into
  431. * "var". The second argument is converted into a C long. Is faster, but not
  432. * as generic, as using PyArg_ParseTuple with "l". It supports either
  433. * gmpy.fname(z,l) or z.fname(l). "self" must be decref'ed. "var" must be a
  434. * pointer to a long. "msg" should be an error message that includes the
  435. * function name and describes the required arguments.
  436. */
  437. #define PARSE_ONE_MPFR_REQ_CLONG(var, msg) \
  438. if (self && Pympfr_CheckAndExp(self)) { \
  439. if (PyTuple_GET_SIZE(args) != 1) { \
  440. PyErr_SetString(PyExc_TypeError, msg); \
  441. return NULL; \
  442. } \
  443. else {\
  444. *var = clong_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  445. if (*var == -1 && PyErr_Occurred()) { \
  446. PyErr_SetString(PyExc_TypeError, msg); \
  447. return NULL; \
  448. } \
  449. } \
  450. Py_INCREF(self); \
  451. } \
  452. else { \
  453. if (PyTuple_GET_SIZE(args) != 2) { \
  454. PyErr_SetString(PyExc_TypeError, msg); \
  455. return NULL; \
  456. } \
  457. else { \
  458. *var = clong_From_Integer(PyTuple_GET_ITEM(args, 1)); \
  459. if (*var == -1 && PyErr_Occurred()) { \
  460. PyErr_SetString(PyExc_TypeError, msg); \
  461. return NULL; \
  462. } \
  463. self = PyTuple_GET_ITEM(args, 0); \
  464. if (Pympfr_CheckAndExp(self)) { \
  465. Py_INCREF((PyObject*)self); \
  466. } \
  467. else { \
  468. self = (PyObject*)Pympfr_From_Real(PyTuple_GET_ITEM(args, 0), 0); \
  469. } \
  470. } \
  471. if (!self) { \
  472. PyErr_SetString(PyExc_TypeError, msg); \
  473. return NULL; \
  474. } \
  475. }
  476. /*
  477. * Parses two, and only two, arguments into "self" and "var" and converts
  478. * them both to mpz. Is faster, but not as generic, as using PyArg_ParseTuple.
  479. * It supports either gmpy.fname(z,z) or z.fname(z). "self" & "var" must be
  480. * decref'ed after use. "msg" should be an error message that includes the
  481. * function name and describes the required arguments. Replaces
  482. * SELF_MPZ_ONE_ARG_CONVERTED(var).
  483. *
  484. * Also considers an 'xmpz' to be equivalent to an 'mpz'.
  485. */
  486. #define PARSE_TWO_MPZ(var, msg) \
  487. if (self && CHECK_MPZANY(self)) { \
  488. if (PyTuple_GET_SIZE(args) != 1) { \
  489. PyErr_SetString(PyExc_TypeError, msg); \
  490. return NULL; \
  491. } \
  492. var = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  493. if (!var) { \
  494. PyErr_SetString(PyExc_TypeError, msg); \
  495. return NULL; \
  496. } \
  497. Py_INCREF(self); \
  498. } \
  499. else { \
  500. if (PyTuple_GET_SIZE(args) != 2) { \
  501. PyErr_SetString(PyExc_TypeError, msg); \
  502. return NULL; \
  503. } \
  504. self = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 0)); \
  505. var = (PyObject*)Pympz_From_Integer(PyTuple_GET_ITEM(args, 1)); \
  506. if (!self || !var) { \
  507. PyErr_SetString(PyExc_TypeError, msg); \
  508. Py_XDECREF((PyObject*)self); \
  509. Py_XDECREF((PyObject*)var); \
  510. return NULL; \
  511. } \
  512. }
  513. #define PARSE_TWO_MPQ(var, msg) \
  514. if (self && Pympq_Check(self)) { \
  515. if (PyTuple_GET_SIZE(args) != 1) { \
  516. PyErr_SetString(PyExc_TypeError, msg); \
  517. return NULL; \
  518. } \
  519. var = (PyObject*)Pympq_From_Rational(PyTuple_GET_ITEM(args, 0)); \
  520. if (!var) { \
  521. PyErr_SetString(PyExc_TypeError, msg); \
  522. return NULL; \
  523. } \
  524. Py_INCREF(self); \
  525. } \
  526. else { \
  527. if (PyTuple_GET_SIZE(args) != 2) { \
  528. PyErr_SetString(PyExc_TypeError, msg); \
  529. return NULL; \
  530. } \
  531. self = (PyObject*)Pympq_From_Rational(PyTuple_GET_ITEM(args, 0)); \
  532. var = (PyObject*)Pympq_From_Rational(PyTuple_GET_ITEM(args, 1)); \
  533. if (!self || !var) { \
  534. PyErr_SetString(PyExc_TypeError, msg); \
  535. Py_XDECREF((PyObject*)self); \
  536. Py_XDECREF((PyObject*)var); \
  537. return NULL; \
  538. } \
  539. }
  540. /*
  541. * Parses two, and only two, arguments into "self" and "var" and converts
  542. * them both to mpfR. Is faster, but not as generic, as using PyArg_ParseTuple.
  543. * It supports either gmpy.fname(f,f) or f.fname(f). "self" & "var" must be
  544. * decref'ed after use. "msg" should be an error message that includes the
  545. * function name and describes the required arguments. Replaces
  546. * SELF_MPF_ONE_ARG_CONVERTED(var).
  547. */
  548. #define PARSE_TWO_MPFR_ARGS(var, msg) \
  549. if (self && Pympfr_Check(self)) { \
  550. if (PyTuple_GET_SIZE(args) != 1) { \
  551. TYPE_ERROR(msg); \
  552. return NULL; \
  553. } \
  554. self = (PyObject*)Pympfr_From_Real(self, 0); \
  555. var = (PyObject*)Pympfr_From_Real(PyTuple_GET_ITEM(args, 0), 0); \
  556. } \
  557. else { \
  558. if (PyTuple_GET_SIZE(args) != 2) { \
  559. TYPE_ERROR(msg); \
  560. return NULL; \
  561. } \
  562. self = (PyObject*)Pympfr_From_Real(PyTuple_GET_ITEM(args, 0), 0); \
  563. var = (PyObject*)Pympfr_From_Real(PyTuple_GET_ITEM(args, 1), 0); \
  564. } \
  565. if (!self || !var) { \
  566. TYPE_ERROR(msg); \
  567. Py_XDECREF((PyObject*)var); \
  568. Py_XDECREF((PyObject*)self); \
  569. return NULL; \
  570. }
  571. /* Define three different versions of the SELF_NO_ARG macro. Under Python
  572. 2.x, self is NULL when a function is called via gmpy.fname(..). But
  573. under Python 3.x, self is a module. */
  574. #define SELF_MPQ_NO_ARG \
  575. if (self && Pympq_Check(self)) { \
  576. if(!PyArg_ParseTuple(args, "")) \
  577. return NULL; \
  578. Py_INCREF(self); \
  579. } \
  580. else { \
  581. if(!PyArg_ParseTuple(args, "O&", Pympq_convert_arg, &self)) \
  582. return NULL; \
  583. }
  584. #define SELF_MPQ_ONE_ARG(fm, var) \
  585. if (self && Pympq_Check(self)) { \
  586. if (!PyArg_ParseTuple(args, fm, var)) \
  587. return NULL; \
  588. Py_INCREF(self); \
  589. } \
  590. else { \
  591. if (!PyArg_ParseTuple(args, "O&" fm, Pympq_convert_arg, &self, var)) \
  592. return NULL; \
  593. }
  594. #ifdef __cplusplus
  595. }
  596. #endif
  597. #endif /* !defined(Py_GMPYMODULE_H */