/******************************************************************************1Copyright (C) 2006 William Stein <[email protected]>22006 Martin Albrecht <[email protected]>34Distributed under the terms of the GNU General Public License (GPL)5as published by the Free Software Foundation; either version 2 of6the License, or (at your option) any later version.7http://www.gnu.org/licenses/89******************************************************************************/1011/**12* @file stdsage.h13*14* @author William Stein <[email protected]>15* @auhtor Martin Albrecht <[email protected]>16*17* @brief General C (.h) code this is useful to include in any pyrex module.18*19* Put20@verbatim2122include 'relative/path/to/stdsage.pxi'2324@endverbatim25*26* at the top of your Pyrex file.27*28* These are mostly things that can't be done in Pyrex.29*/3031#ifndef STDSAGE_H32#define STDSAGE_H3334#include "Python.h"35#include "memory.h"3637/* Building with this not commented out causes38serious problems on RHEL5 64-bit for Kiran Kedlaya... i.e., it doesn't work. */39/* #include "ccobject.h" */4041#ifdef __cplusplus42extern "C" {43#endif444546/*****************************************47PARI array element assignment48*****************************************/49#define set_gel(x, n, z) (gel(x,n) = z)50#define set_gmael(x, i, j, z) (gmael(x,i,j) = z)51#define set_gcoeff(x, i, j, z) (gcoeff(x,i,j) = z)525354/******************************************55Some macros exported for Pyrex in cdefs.pxi56****************************************/5758/** Tests whether zzz_obj is of type zzz_type. The zzz_type must be a59* built-in or extension type. This is just a C++-compatible wrapper60* for PyObject_TypeCheck.61*/62#define PY_TYPE_CHECK(zzz_obj, zzz_type) \63(PyObject_TypeCheck((PyObject*)(zzz_obj), (PyTypeObject*)(zzz_type)))6465/** Tests whether zzz_obj is exactly of type zzz_type. The zzz_type must be a66* built-in or extension type.67*/68#define PY_TYPE_CHECK_EXACT(zzz_obj, zzz_type) \69((PyTypeObject*)PY_TYPE(zzz_obj) == (PyTypeObject*)(zzz_type))7071/** Returns the type field of a python object, cast to void*. The72* returned value should only be used as an opaque object e.g. for73* type comparisons.74*/75#define PY_TYPE(zzz_obj) ((void*)((zzz_obj)->ob_type))7677/** Constructs a new object of type zzz_type by calling tp_new78* directly, with no arguments.79*/8081#define PY_NEW(zzz_type) \82(((PyTypeObject*)(zzz_type))->tp_new((PyTypeObject*)(zzz_type), global_empty_tuple, NULL))838485/** Constructs a new object of type the same type as zzz_obj by calling tp_new86* directly, with no arguments.87*/8889#define PY_NEW_SAME_TYPE(zzz_obj) \90PY_NEW(PY_TYPE(zzz_obj))9192/** Resets the tp_new slot of zzz_type1 to point to the tp_new slot of93* zzz_type2. This is used in SAGE to speed up Pyrex's boilerplate94* object construction code by skipping irrelevant base class tp_new95* methods.96*/97#define PY_SET_TP_NEW(zzz_type1, zzz_type2) \98(((PyTypeObject*)zzz_type1)->tp_new = ((PyTypeObject*)zzz_type2)->tp_new)99100101/**102* Tests whether the given object has a python dictionary.103*/104#define HAS_DICTIONARY(zzz_obj) \105(((PyObject*)(zzz_obj))->ob_type->tp_dictoffset != NULL)106107/**108* Very very unsafe access to the list of pointers to PyObject*'s109* underlying a list / sequence. This does error checking of any kind110* -- make damn sure you hand it a list or sequence!111*/112#define FAST_SEQ_UNSAFE(zzz_obj) \113PySequence_Fast_ITEMS(PySequence_Fast(zzz_obj, "expected sequence type"))114115/** Returns the type field of a python object, cast to void*. The116* returned value should only be used as an opaque object e.g. for117* type comparisons.118*/119#define PY_IS_NUMERIC(zzz_obj) \120(PyInt_Check(zzz_obj) || PyBool_Check(zzz_obj) || PyLong_Check(zzz_obj) || \121PyFloat_Check(zzz_obj) || PyComplex_Check(zzz_obj))122123124/** This is exactly the same as isinstance (and does return a Python125* bool), but the second argument must be a C-extension type -- so it126* can't be a Python class or a list. If you just want an int return127* value, i.e., aren't going to pass this back to Python, just use128* PY_TYPE_CHECK.129*/130#define IS_INSTANCE(zzz_obj, zzz_type) \131PyBool_FromLong(PY_TYPE_CHECK(zzz_obj, zzz_type))132133134/**135* A global empty python tuple object. This is used to speed up some136* python API calls where we want to avoid constructing a tuple every137* time.138*/139140extern PyObject* global_empty_tuple;141142143/**144* Initialisation of signal handlers, global variables, etc. Called145* exactly once at Sage start-up.146*/147void init_csage(void);148149/**150* Initialisation of a Cython module. Called exactly once for every151* Cython module. On Cygwin, this calls init_csage().152*/153void init_csage_module(void);154155156157/**158* a handy macro to be placed at the top of a function definition159* below the variable declarations to ensure a function is called once160* at maximum.161*/162#define _CALLED_ONLY_ONCE static int ncalls = 0; if (ncalls>0) return; else ncalls++163164#ifdef __cplusplus165}166#endif167168#endif169170171