PageRenderTime 43ms CodeModel.GetById 31ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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