1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * gmpy2.h                                                                 *
3  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4  * Python interface to the GMP or MPIR, MPFR, and MPC multiple precision   *
5  * libraries.                                                              *
6  *                                                                         *
7  * Copyright 2000 - 2009 Alex Martelli                                     *
8  *                                                                         *
9  * Copyright 2008 - 2021 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 
28 /*
29   gmpy C API extension header file.
30   Part of Python's gmpy module since version 0.4
31 
32   Created by Pearu Peterson <pearu@cens.ioc.ee>, November 2000.
33   Edited by A. Martelli <aleaxit@yahoo.com>, December 2000.
34   Edited by Case Van Horsen <casevh@gmail.com>, 2009, 2010, 2011.
35 
36   Version 1.02, February 2007.
37   Version 1.03, June 2008
38   Version 1.04, June 2008 (no changes)
39   Version 1.05, February 2009 (support MPIR)
40   Version 1.20, January 2010 (remove obsolete MS hacks) casevh
41   Version 2.00, April 2010 (change to gmpy2) casevh
42                 October 2010 (added Py_hash_t) casevh
43                 December 2010 (added mpfr, mpc) casevh
44                 January 2011 (add Pygmpy_context) casevh
45                 April 2011 (split into multiple files) casevh
46   Version 2.10  August 2014 (reflect major rewrite during 2013/2014) casevh
47  */
48 
49 #ifndef Py_GMPYMODULE_H
50 #define Py_GMPYMODULE_H
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 /* Structure of gmpy2.h
57  *
58  * Revised 17-APR-2017 casevh
59  *
60  * 1. Checks for specific Python versions.
61  * 2. Include headers for GMP/MPIR, MPFR, and MPC.
62  * 3. Define public C-API.
63  *    1. Define gmpy2 types.
64  *    2. Define the public API.
65  *
66  */
67 
68 /* Check for minimum Python version requirements. */
69 
70 #if PY_VERSION_HEX < 0x02060000
71 #  error "GMPY2 requires Python 2.6 or later."
72 #endif
73 
74 /* Include headers for GMP/MPIR, MPFR, and MPC. */
75 
76 #ifdef MPIR
77 #  include <mpir.h>
78 #else
79 #  include <gmp.h>
80 #endif
81 
82 #include <mpfr.h>
83 #include <mpc.h>
84 
85 /* Check MPFR and MPC versions. */
86 
87 #if (!defined(MPC_VERSION) || (MPC_VERSION < MPC_VERSION_NUM(1,0,3)))
88 #  error "GMPY2 requires MPC 1.0.3 or later."
89 #endif
90 
91 #if (defined(MPC_VERSION) && (MPC_VERSION >= MPC_VERSION_NUM(1,1,0)))
92 #  define MPC_110
93 #endif
94 
95 
96 #if PY_VERSION_HEX < 0x030200A4
97 typedef long Py_hash_t;
98 typedef unsigned long Py_uhash_t;
99 #  define _PyHASH_IMAG 1000003
100 #endif
101 
102 /* GMPY2 Public API */
103 
104 /* Types
105  *    MPZ_Object
106  *    XMPZ_Object        (mutable version of MPZ_Object)
107  *    MPQ_Object
108  *    XMPQ_Object        (mutable version of MPQ_Object)
109  *    MPFR_Object
110  *    XMPFR_Object       (mutable version of MPFR_Object)
111  *    MPC_Object
112  *    XMPC_Object        (mutable version of MPC_Object)
113  *    CTXT_Object
114  *    CTXT_Manager_Object
115  *    RandomState_Object
116  */
117 
118 typedef struct {
119     PyObject_HEAD
120     mpz_t z;
121     Py_hash_t hash_cache;
122 } MPZ_Object;
123 
124 typedef struct {
125     PyObject_HEAD
126     mpz_t z;
127 } XMPZ_Object;
128 
129 typedef struct {
130     PyObject_HEAD
131     mpq_t q;
132     Py_hash_t  hash_cache;
133 } MPQ_Object;
134 
135 typedef struct {
136     PyObject_HEAD
137     mpfr_t f;
138     Py_hash_t hash_cache;
139     int rc;
140 } MPFR_Object;
141 
142 typedef struct {
143     PyObject_HEAD
144     mpc_t c;
145     Py_hash_t hash_cache;
146     int rc;
147 } MPC_Object;
148 
149 typedef struct {
150     PyObject_HEAD
151     gmp_randstate_t state;
152 } RandomState_Object;
153 
154 typedef struct {
155     mpfr_prec_t mpfr_prec;   /* current precision in bits, for MPFR */
156     mpfr_rnd_t mpfr_round;   /* current rounding mode for float (MPFR) */
157     mpfr_exp_t emax;         /* maximum exponent */
158     mpfr_exp_t emin;         /* minimum exponent */
159     int subnormalize;        /* if 1, subnormalization is performed */
160     int underflow;           /* did an underflow occur? */
161     int overflow;            /* did an overflow occur? */
162     int inexact;             /* was the result inexact? */
163     int invalid;             /* invalid operation (i.e. NaN)? */
164     int erange;              /* did a range error occur? */
165     int divzero;             /* divided by zero? */
166     int traps;               /* if 0, do not trap any exceptions */
167                              /* if not 0, then raise traps per bits above  */
168     mpfr_prec_t real_prec;   /* current precision in bits, for Re(MPC) */
169     mpfr_prec_t imag_prec;   /* current precision in bits, for Im(MPC) */
170     mpfr_rnd_t real_round;   /* current rounding mode for Re(MPC) */
171     mpfr_rnd_t imag_round;   /* current rounding mode for Im(MPC) */
172     int allow_complex;       /* if 1, allow mpfr functions to return an mpc */
173     int rational_division;   /* if 1, mpz/mpz returns an mpq result */
174     int allow_release_gil;   /* if 1, allow mpz functions to release the GIL */
175 } gmpy_context;
176 
177 typedef struct {
178     PyObject_HEAD
179     gmpy_context ctx;
180 #ifndef WITHOUT_THREADS
181     PyThreadState *tstate;
182 #endif
183 } CTXT_Object;
184 
185 typedef struct {
186     PyObject_HEAD
187     CTXT_Object *new_context; /* Context that will be returned when
188                                * __enter__ is called. */
189     CTXT_Object *old_context; /* Context that will restored when
190                                * __exit__ is called. */
191 } CTXT_Manager_Object;
192 
193 #define MPZ(obj)  (((MPZ_Object*)(obj))->z)
194 #define MPQ(obj)  (((MPQ_Object*)(obj))->q)
195 #define MPFR(obj) (((MPFR_Object*)(obj))->f)
196 #define MPC(obj)  (((MPC_Object*)(obj))->c)
197 
198 /* Start of the C-API definitions */
199 
200 #define MPZ_Type_NUM          0
201 #define XMPZ_Type_NUM         1
202 #define MPQ_Type_NUM          2
203 #define XMPQ_Type_NUM         3
204 #define MPFR_Type_NUM         4
205 #define XMPFR_Type_NUM        5
206 #define MPC_Type_NUM          6
207 #define XMPC_Type_NUM         7
208 #define CTXT_Type_NUM         8
209 #define CTXT_Manager_Type_NUM 9
210 #define RandomState_Type_NUM  10
211 
212 /* The following functions are found in gmpy2_cache. */
213 
214 #define GMPy_MPZ_New_NUM            11
215 #define GMPy_MPZ_New_RETURN         MPZ_Object *
216 #define GMPy_MPZ_New_PROTO          (CTXT_Object *context)
217 
218 #define GMPy_MPZ_NewInit_NUM        12
219 #define GMPy_MPZ_NewInit_RETURN     PyObject *
220 #define GMPy_MPZ_NewInit_PROTO      (PyTypeObject *type, PyObject *args, PyObject *keywds)
221 
222 #define GMPy_MPZ_Dealloc_NUM        13
223 #define GMPy_MPZ_Dealloc_RETURN     void
224 #define GMPy_MPZ_Dealloc_PROTO      (MPZ_Object *self)
225 
226 /* The following function is found in gmpy2_convert_gmp. */
227 
228 #define GMPy_MPZ_ConvertArg_NUM     14
229 #define GMPy_MPZ_ConvertArg_RETURN  int
230 #define GMPy_MPZ_ConvertArg_PROTO   (PyObject *arg, PyObject **ptr)
231 
232 /* The following functions are found in gmpy2_cache. */
233 
234 #define GMPy_XMPZ_New_NUM           15
235 #define GMPy_XMPZ_New_RETURN        XMPZ_Object *
236 #define GMPy_XMPZ_New_PROTO         (CTXT_Object *context)
237 
238 #define GMPy_XMPZ_NewInit_NUM       16
239 #define GMPy_XMPZ_NewInit_RETURN    PyObject *
240 #define GMPy_XMPZ_NewInit_PROTO     (PyTypeObject *type, PyObject *args, PyObject *keywds)
241 
242 #define GMPy_XMPZ_Dealloc_NUM       17
243 #define GMPy_XMPZ_Dealloc_RETURN    void
244 #define GMPy_XMPZ_Dealloc_PROTO     (XMPZ_Object *self)
245 
246 /* The following functions are found in gmpy2_cache. */
247 
248 #define GMPy_MPQ_New_NUM            18
249 #define GMPy_MPQ_New_RETURN         MPQ_Object *
250 #define GMPy_MPQ_New_PROTO          (CTXT_Object *context)
251 
252 #define GMPy_MPQ_NewInit_NUM        19
253 #define GMPy_MPQ_NewInit_RETURN     PyObject *
254 #define GMPy_MPQ_NewInit_PROTO      (PyTypeObject *type, PyObject *args, PyObject *keywds)
255 
256 #define GMPy_MPQ_Dealloc_NUM        20
257 #define GMPy_MPQ_Dealloc_RETURN     void
258 #define GMPy_MPQ_Dealloc_PROTO      (MPQ_Object *self)
259 
260 /* The following function is found in gmpy2_convert_gmp. */
261 
262 #define GMPy_MPQ_ConvertArg_NUM     21
263 #define GMPy_MPQ_ConvertArg_RETURN  int
264 #define GMPy_MPQ_ConvertArg_PROTO   (PyObject *arg, PyObject **ptr)
265 
266 /* The following functions are found in gmpy2_cache. */
267 
268 #define GMPy_MPFR_New_NUM           22
269 #define GMPy_MPFR_New_RETURN        MPFR_Object *
270 #define GMPy_MPFR_New_PROTO         (mpfr_prec_t bits, CTXT_Object *context)
271 
272 #define GMPy_MPFR_NewInit_NUM       23
273 #define GMPy_MPFR_NewInit_RETURN    PyObject *
274 #define GMPy_MPFR_NewInit_PROTO     (PyTypeObject *type, PyObject *args, PyObject *keywds)
275 
276 #define GMPy_MPFR_Dealloc_NUM       24
277 #define GMPy_MPFR_Dealloc_RETURN    void
278 #define GMPy_MPFR_Dealloc_PROTO     (MPFR_Object *self)
279 
280 /* The following function is found in gmpy2_convert_gmp. */
281 
282 #define GMPy_MPFR_ConvertArg_NUM    25
283 #define GMPy_MPFR_ConvertArg_RETURN int
284 #define GMPy_MPFR_ConvertArg_PROTO  (PyObject *arg, PyObject **ptr)
285 
286 /* The following functions are found in gmpy2_cache. */
287 
288 #define GMPy_MPC_New_NUM             26
289 #define GMPy_MPC_New_RETURN          MPC_Object *
290 #define GMPy_MPC_New_PROTO           (mpfr_prec_t rprec, mpfr_prec_t iprec, CTXT_Object *context)
291 
292 #define GMPy_MPC_NewInit_NUM         27
293 #define GMPy_MPC_NewInit_RETURN      PyObject *
294 #define GMPy_MPC_NewInit_PROTO       (PyTypeObject *type, PyObject *args, PyObject *keywds)
295 
296 #define GMPy_MPC_Dealloc_NUM        28
297 #define GMPy_MPC_Dealloc_RETURN     void
298 #define GMPy_MPC_Dealloc_PROTO      (MPC_Object *self)
299 
300 /* The following function is found in gmpy2_convert_gmp. */
301 
302 #define GMPy_MPC_ConvertArg_NUM     29
303 #define GMPy_MPC_ConvertArg_RETURN  int
304 #define GMPy_MPC_ConvertArg_PROTO   (PyObject *arg, PyObject **ptr)
305 
306 /* Total number of C-API pointers. */
307 
308 #define GMPy_API_pointers 30
309 
310 /* End of C-API definitions. */
311 
312 #ifdef GMPY2_MODULE
313 
314 /* Define various macros to deal with differences between Python 2 and 3. */
315 
316 #if (PY_MAJOR_VERSION == 3)
317 #define PY3
318 #define Py2or3String_FromString     PyUnicode_FromString
319 #define Py2or3String_FromFormat     PyUnicode_FromFormat
320 #define Py2or3String_Check          PyUnicode_Check
321 #define Py2or3String_Format         PyUnicode_Format
322 #define Py2or3String_Type           Py_UCS4
323 #define Py2or3String_1Char(obj)     (PyUnicode_READY(obj) ? (Py_UCS4)0 : PyUnicode_READ_CHAR(obj, 0))
324 #define PyStrOrUnicode_Check(op)    (PyBytes_Check(op) || PyUnicode_Check(op))
325 #define PyIntOrLong_FromLong        PyLong_FromLong
326 #define PyIntOrLong_Check(op)       (PyLong_Check(op))
327 #define PyIntOrLong_CheckExact(op)  PyLong_CheckExact(op)
328 #define PyIntOrLong_FromSize_t      PyLong_FromSize_t
329 #define PyIntOrLong_FromSsize_t     PyLong_FromSsize_t
330 #define PyIntOrLong_AsSsize_t       PyLong_AsSsize_t
331 #define PyIntOrLong_AsLong          PyLong_AsLong
332 #else
333 #define PY2
334 #define Py2or3String_FromString     PyString_FromString
335 #define Py2or3String_FromFormat     PyString_FromFormat
336 #define Py2or3String_Check          PyString_Check
337 #define Py2or3String_Format         PyString_Format
338 #define Py2or3String_Type           char
339 #define Py2or3String_1Char(obj)     PyString_AsString(obj)[0]
340 #define PyStrOrUnicode_Check(op)    (PyString_Check(op) || PyUnicode_Check(op))
341 #define PyIntOrLong_FromLong        PyInt_FromLong
342 #define PyIntOrLong_Check(op)       (PyInt_Check(op) || PyLong_Check(op))
343 #define PyIntOrLong_CheckExact(op)  (PyInt_CheckExact(op) || PyLong_CheckExact(op))
344 #define PyIntOrLong_FromSize_t      PyInt_FromSize_t
345 #define PyIntOrLong_FromSsize_t     PyInt_FromSsize_t
346 #define PyIntOrLong_AsSsize_t       PyInt_AsSsize_t
347 #define PyIntOrLong_AsLong          PyInt_AsLong
348 #endif
349 
350 #ifndef ABS
351 #  define ABS(a)  (((a) < 0) ? -(a) : (a))
352 #endif
353 
354 #if defined(MS_WIN32) && defined(_MSC_VER)
355    /* so one won't need to link explicitly to gmp.lib...: */
356 #  if defined(MPIR)
357 #    pragma comment(lib,"mpir.lib")
358 #  else
359 #    pragma comment(lib,"gmp.lib")
360 #  endif
361 #  define USE_ALLOCA 1
362 #  define inline __inline
363 #endif
364 
365 #ifdef __GNUC__
366 #  define USE_ALLOCA 1
367 #endif
368 
369 #ifndef alloca
370 # ifdef __GNUC__
371 #  define alloca __builtin_alloca
372 # else
373 #   ifdef _MSC_VER
374 #    include <malloc.h>
375 #    define alloca _alloca
376 #   else
377 #    if HAVE_ALLOCA_H
378 #     include <alloca.h>
379 #    else
380        char *alloca ();
381 #    endif
382 #   endif
383 # endif
384 #endif
385 
386 #define ALLOC_THRESHOLD 8192
387 
388 #define INDEX_ERROR(msg)    PyErr_SetString(PyExc_IndexError, msg)
389 #define TYPE_ERROR(msg)     PyErr_SetString(PyExc_TypeError, msg)
390 #define VALUE_ERROR(msg)    PyErr_SetString(PyExc_ValueError, msg)
391 #define ZERO_ERROR(msg)     PyErr_SetString(PyExc_ZeroDivisionError, msg)
392 #define SYSTEM_ERROR(msg)   PyErr_SetString(PyExc_SystemError, msg)
393 #define OVERFLOW_ERROR(msg) PyErr_SetString(PyExc_OverflowError, msg)
394 #define RUNTIME_ERROR(msg)  PyErr_SetString(PyExc_RuntimeError, msg)
395 
396 #define GMPY_DEFAULT -1
397 
398 /* To prevent excessive memory usage, we don't want to save very large
399  * numbers in the cache. The default value specified in the options
400  * structure is 128 words (512 bytes on 32-bit platforms, 1024 bytes on
401  * 64-bit platforms).
402  */
403 #define MAX_CACHE_LIMBS 16384
404 
405 /* The maximum number of objects that can be saved in a cache is specified
406  * here. The default value is 100.*/
407 #define MAX_CACHE 1000
408 
409 #ifdef USE_ALLOCA
410 #  define TEMP_ALLOC(B, S)     \
411     if(S < ALLOC_THRESHOLD) {  \
412         B = alloca(S);         \
413     } else {                   \
414         if(!(B = malloc(S))) { \
415             PyErr_NoMemory();  \
416             return NULL;       \
417         }                      \
418     }
419 #  define TEMP_FREE(B, S) if(S >= ALLOC_THRESHOLD) free(B)
420 #else
421 #  define TEMP_ALLOC(B, S)     \
422     if(!(B = malloc(S)))  {    \
423         PyErr_NoMemory();      \
424         return NULL;           \
425     }
426 #  define TEMP_FREE(B, S) free(B)
427 #endif
428 
429 /* Various defs to mask differences between Python versions. */
430 #if PY_VERSION_HEX < 0x03050000
431 #define Py_RETURN_NOTIMPLEMENTED \
432     return Py_INCREF(Py_NotImplemented), Py_NotImplemented
433 #endif
434 
435 #ifndef Py_SIZE
436 #  define Py_SIZE(ob)     (((PyVarObject*)(ob))->ob_size)
437 #endif
438 
439 #ifndef Py_TYPE
440 #  define Py_TYPE(ob)     (((PyObject*)(ob))->ob_type)
441 #endif
442 
443 /* Import a collection of general purpose macros. */
444 
445 #include "gmpy2_macros.h"
446 
447 /* Import the files that complete the definition of the types defined above. */
448 
449 #include "gmpy2_mpz.h"
450 #include "gmpy2_xmpz.h"
451 #include "gmpy2_mpq.h"
452 #include "gmpy2_mpfr.h"
453 #include "gmpy2_mpc.h"
454 #include "gmpy2_context.h"
455 #include "gmpy2_random.h"
456 
457 /* Import the header files that provide the various functions. */
458 
459 /* Support object caching, creation, and deletion. */
460 
461 #include "gmpy2_cache.h"
462 
463 /* Suport for miscellaneous functions (ie. version, license, etc.). */
464 
465 #include "gmpy2_misc.h"
466 
467 /* Support conversion to/from binary format. */
468 
469 #include "gmpy2_binary.h"
470 
471 /* Support for mpz/xmpz specific functions. */
472 
473 #include "gmpy2_convert.h"
474 #include "gmpy2_convert_utils.h"
475 #include "gmpy2_convert_gmp.h"
476 #include "gmpy2_convert_mpfr.h"
477 #include "gmpy2_convert_mpc.h"
478 
479 #include "gmpy2_mpz_divmod.h"
480 #include "gmpy2_mpz_divmod2exp.h"
481 #include "gmpy2_mpz_pack.h"
482 #include "gmpy2_mpz_bitops.h"
483 #include "gmpy2_mpz_misc.h"
484 
485 #include "gmpy2_xmpz_inplace.h"
486 #include "gmpy2_xmpz_misc.h"
487 #include "gmpy2_xmpz_limbs.h"
488 
489 /* Support for mpq specific functions. */
490 
491 #include "gmpy2_mpq_misc.h"
492 
493 /* Support for mpfr specific functions. */
494 
495 #include "gmpy2_mpfr_misc.h"
496 
497 /* Support for mpc specific functions. */
498 
499 #include "gmpy2_mpc_misc.h"
500 
501 /* Support Lucas sequences. */
502 
503 #include "gmpy_mpz_lucas.h"
504 
505 /* Support probable-prime tests. */
506 
507 #include "gmpy_mpz_prp.h"
508 
509 /* Support higher-level Python methods and functions; generally not
510  * specific to a single type.
511  */
512 
513 #include "gmpy2_abs.h"
514 #include "gmpy2_add.h"
515 #include "gmpy2_divmod.h"
516 #include "gmpy2_floordiv.h"
517 #include "gmpy2_minus.h"
518 #include "gmpy2_mod.h"
519 #include "gmpy2_mul.h"
520 #include "gmpy2_plus.h"
521 #include "gmpy2_pow.h"
522 #include "gmpy2_sub.h"
523 #include "gmpy2_truediv.h"
524 #include "gmpy2_math.h"
525 #include "gmpy2_const.h"
526 #include "gmpy2_square.h"
527 #include "gmpy2_format.h"
528 #include "gmpy2_hash.h"
529 #include "gmpy2_fused.h"
530 #include "gmpy2_muldiv_2exp.h"
531 #include "gmpy2_predicate.h"
532 #include "gmpy2_sign.h"
533 #include "gmpy2_richcompare.h"
534 #include "gmpy2_cmp.h"
535 
536 #ifdef VECTOR
537 #  include "gmpy2_vector.h"
538 #endif /* defined(VECTOR) */
539 
540 #else /* defined(GMPY2_MODULE) */
541 
542 /* This section is used for other C-coded modules that use gmpy2's API. */
543 
544 static void **GMPy_C_API;
545 
546 #define MPZ_Check(op)    ((op)->ob_type == (PyTypeObject*)GMPy_C_API[MPZ_Type_NUM])
547 #define XMPZ_Check(op)   ((op)->ob_type == (PyTypeObject*)GMPy_C_API[XMPZ_Type_NUM])
548 #define MPQ_Check(op)    ((op)->ob_type == (PyTypeObject*)GMPy_C_API[MPQ_Type_NUM])
549 #define XMPQ_Check(op)   ((op)->ob_type == (PyTypeObject*)GMPy_C_API[XMPQ_Type_NUM])
550 #define MPFR_Check(op)   ((op)->ob_type == (PyTypeObject*)GMPy_C_API[MPFR_Type_NUM])
551 #define XMPFR_Check(op)  ((op)->ob_type == (PyTypeObject*)GMPy_C_API[XMPFR_Type_NUM])
552 #define MPC_Check(op)    ((op)->ob_type == (PyTypeObject*)GMPy_C_API[MPC_Type_NUM])
553 #define XMPC_Check(op)   ((op)->ob_type == (PyTypeObject*)GMPy_C_API[XMPC_Type_NUM])
554 
555 #define GMPy_MPZ_New         (*(GMPy_MPZ_New_RETURN         (*)GMPy_MPZ_New_PROTO)         GMPy_C_API[GMPy_MPZ_New_NUM])
556 #define GMPy_MPZ_NewInit     (*(GMPy_MPZ_NewInit_RETURN     (*)GMPy_MPZ_NewInit_PROTO)     GMPy_C_API[GMPy_MPZ_NewInit_NUM])
557 #define GMPy_MPZ_Dealloc     (*(GMPy_MPZ_Dealloc_RETURN     (*)GMPy_MPZ_Dealloc_PROTO)     GMPy_C_API[GMPy_MPZ_Dealloc_NUM])
558 #define GMPy_MPZ_ConvertArg  (*(GMPy_MPZ_ConvertArg_RETURN  (*)GMPy_MPZ_ConvertArg_PROTO)  GMPy_C_API[GMPy_MPZ_ConvertArg_NUM])
559 
560 #define GMPy_XMPZ_New        (*(GMPy_XMPZ_New_RETURN        (*)GMPy_XMPZ_New_PROTO)        GMPy_C_API[GMPy_XMPZ_New_NUM])
561 #define GMPy_XMPZ_NewInit    (*(GMPy_XMPZ_NewInit_RETURN    (*)GMPy_XMPZ_NewInit_PROTO)    GMPy_C_API[GMPy_XMPZ_NewInit_NUM])
562 #define GMPy_XMPZ_Dealloc    (*(GMPy_XMPZ_Dealloc_RETURN    (*)GMPy_XMPZ_Dealloc_PROTO)    GMPy_C_API[GMPy_XMPZ_Dealloc_NUM])
563 
564 #define GMPy_MPQ_New         (*(GMPy_MPQ_New_RETURN         (*)GMPy_MPQ_New_PROTO)         GMPy_C_API[GMPy_MPQ_New_NUM])
565 #define GMPy_MPQ_NewInit     (*(GMPy_MPQ_NewInit_RETURN     (*)GMPy_MPQ_NewInit_PROTO)     GMPy_C_API[GMPy_MPQ_NewInit_NUM])
566 #define GMPy_MPQ_Dealloc     (*(GMPy_MPQ_Dealloc_RETURN     (*)GMPy_MPQ_Dealloc_PROTO)     GMPy_C_API[GMPy_MPQ_Dealloc_NUM])
567 #define GMPy_MPQ_ConvertArg  (*(GMPy_MPQ_ConvertArg_RETURN  (*)GMPy_MPQ_ConvertArg_PROTO)  GMPy_C_API[GMPy_MPQ_ConvertArg_NUM])
568 
569 #define GMPy_MPFR_New        (*(GMPy_MPFR_New_RETURN        (*)GMPy_MPFR_New_PROTO)        GMPy_C_API[GMPy_MPFR_New_NUM])
570 #define GMPy_MPFR_NewInit    (*(GMPy_MPFR_NewInit_RETURN    (*)GMPy_MPFR_NewInit_PROTO)    GMPy_C_API[GMPy_MPFR_NewInit_NUM])
571 #define GMPy_MPFR_Dealloc    (*(GMPy_MPFR_Dealloc_RETURN    (*)GMPy_MPFR_Dealloc_PROTO)    GMPy_C_API[GMPy_MPFR_Dealloc_NUM])
572 #define GMPy_MPFR_ConvertArg (*(GMPy_MPFR_ConvertArg_RETURN (*)GMPy_MPFR_ConvertArg_PROTO) GMPy_C_API[GMPy_MPFR_ConvertArg_NUM])
573 
574 #define GMPy_MPC_New         (*(GMPy_MPC_New_RETURN         (*)GMPy_MPC_New_PROTO)         GMPy_C_API[GMPy_MPC_New_NUM])
575 #define GMPy_MPC_NewInit     (*(GMPy_MPC_NewInit_RETURN     (*)GMPy_MPC_NewInit_PROTO)     GMPy_C_API[GMPy_MPC_NewInit_NUM])
576 #define GMPy_MPC_Dealloc     (*(GMPy_MPC_Dealloc_RETURN     (*)GMPy_MPC_Dealloc_PROTO)     GMPy_C_API[GMPy_MPC_Dealloc_NUM])
577 #define GMPy_MPC_ConvertArg  (*(GMPy_MPC_ConvertArg_RETURN  (*)GMPy_MPC_ConvertArg_PROTO)  GMPy_C_API[GMPy_MPC_ConvertArg_NUM])
578 
579 static int
import_gmpy2(void)580 import_gmpy2(void)
581 {
582     GMPy_C_API = (void **)PyCapsule_Import("gmpy2._C_API", 0);
583     return (GMPy_C_API != NULL) ? 0 : -1;
584 }
585 
586 #endif /* defined(GMPY2_MODULE) */
587 
588 #ifdef __cplusplus
589 }
590 #endif /* defined(__cplusplus */
591 #endif /* !defined(Py_GMPYMODULE_H */
592