Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/c_lib/include/stdsage.h
4045 views
1
/******************************************************************************
2
Copyright (C) 2006 William Stein <[email protected]>
3
2006 Martin Albrecht <[email protected]>
4
5
Distributed under the terms of the GNU General Public License (GPL)
6
as published by the Free Software Foundation; either version 2 of
7
the License, or (at your option) any later version.
8
http://www.gnu.org/licenses/
9
10
******************************************************************************/
11
12
/**
13
* @file stdsage.h
14
*
15
* @author William Stein <[email protected]>
16
* @auhtor Martin Albrecht <[email protected]>
17
*
18
* @brief General C (.h) code this is useful to include in any pyrex module.
19
*
20
* Put
21
@verbatim
22
23
include 'relative/path/to/stdsage.pxi'
24
25
@endverbatim
26
*
27
* at the top of your Pyrex file.
28
*
29
* These are mostly things that can't be done in Pyrex.
30
*/
31
32
#ifndef STDSAGE_H
33
#define STDSAGE_H
34
35
#include "Python.h"
36
#include "memory.h"
37
38
/* Building with this not commented out causes
39
serious problems on RHEL5 64-bit for Kiran Kedlaya... i.e., it doesn't work. */
40
/* #include "ccobject.h" */
41
42
#ifdef __cplusplus
43
extern "C" {
44
#endif
45
46
47
/*****************************************
48
PARI array element assignment
49
*****************************************/
50
#define set_gel(x, n, z) (gel(x,n) = z)
51
#define set_gmael(x, i, j, z) (gmael(x,i,j) = z)
52
#define set_gcoeff(x, i, j, z) (gcoeff(x,i,j) = z)
53
54
55
/******************************************
56
Some macros exported for Pyrex in cdefs.pxi
57
****************************************/
58
59
/** Tests whether zzz_obj is of type zzz_type. The zzz_type must be a
60
* built-in or extension type. This is just a C++-compatible wrapper
61
* for PyObject_TypeCheck.
62
*/
63
#define PY_TYPE_CHECK(zzz_obj, zzz_type) \
64
(PyObject_TypeCheck((PyObject*)(zzz_obj), (PyTypeObject*)(zzz_type)))
65
66
/** Tests whether zzz_obj is exactly of type zzz_type. The zzz_type must be a
67
* built-in or extension type.
68
*/
69
#define PY_TYPE_CHECK_EXACT(zzz_obj, zzz_type) \
70
((PyTypeObject*)PY_TYPE(zzz_obj) == (PyTypeObject*)(zzz_type))
71
72
/** Returns the type field of a python object, cast to void*. The
73
* returned value should only be used as an opaque object e.g. for
74
* type comparisons.
75
*/
76
#define PY_TYPE(zzz_obj) ((void*)((zzz_obj)->ob_type))
77
78
/** Constructs a new object of type zzz_type by calling tp_new
79
* directly, with no arguments.
80
*/
81
82
#define PY_NEW(zzz_type) \
83
(((PyTypeObject*)(zzz_type))->tp_new((PyTypeObject*)(zzz_type), global_empty_tuple, NULL))
84
85
86
/** Constructs a new object of type the same type as zzz_obj by calling tp_new
87
* directly, with no arguments.
88
*/
89
90
#define PY_NEW_SAME_TYPE(zzz_obj) \
91
PY_NEW(PY_TYPE(zzz_obj))
92
93
/** Resets the tp_new slot of zzz_type1 to point to the tp_new slot of
94
* zzz_type2. This is used in SAGE to speed up Pyrex's boilerplate
95
* object construction code by skipping irrelevant base class tp_new
96
* methods.
97
*/
98
#define PY_SET_TP_NEW(zzz_type1, zzz_type2) \
99
(((PyTypeObject*)zzz_type1)->tp_new = ((PyTypeObject*)zzz_type2)->tp_new)
100
101
102
/**
103
* Tests whether the given object has a python dictionary.
104
*/
105
#define HAS_DICTIONARY(zzz_obj) \
106
(((PyObject*)(zzz_obj))->ob_type->tp_dictoffset != NULL)
107
108
/**
109
* Very very unsafe access to the list of pointers to PyObject*'s
110
* underlying a list / sequence. This does error checking of any kind
111
* -- make damn sure you hand it a list or sequence!
112
*/
113
#define FAST_SEQ_UNSAFE(zzz_obj) \
114
PySequence_Fast_ITEMS(PySequence_Fast(zzz_obj, "expected sequence type"))
115
116
/** Returns the type field of a python object, cast to void*. The
117
* returned value should only be used as an opaque object e.g. for
118
* type comparisons.
119
*/
120
#define PY_IS_NUMERIC(zzz_obj) \
121
(PyInt_Check(zzz_obj) || PyBool_Check(zzz_obj) || PyLong_Check(zzz_obj) || \
122
PyFloat_Check(zzz_obj) || PyComplex_Check(zzz_obj))
123
124
125
/** This is exactly the same as isinstance (and does return a Python
126
* bool), but the second argument must be a C-extension type -- so it
127
* can't be a Python class or a list. If you just want an int return
128
* value, i.e., aren't going to pass this back to Python, just use
129
* PY_TYPE_CHECK.
130
*/
131
#define IS_INSTANCE(zzz_obj, zzz_type) \
132
PyBool_FromLong(PY_TYPE_CHECK(zzz_obj, zzz_type))
133
134
135
/**
136
* A global empty python tuple object. This is used to speed up some
137
* python API calls where we want to avoid constructing a tuple every
138
* time.
139
*/
140
141
extern PyObject* global_empty_tuple;
142
143
144
/**
145
* Initialisation of signal handlers, global variables, etc. Called
146
* exactly once at Sage start-up.
147
*/
148
void init_csage(void);
149
150
/**
151
* Initialisation of a Cython module. Called exactly once for every
152
* Cython module. On Cygwin, this calls init_csage().
153
*/
154
void init_csage_module(void);
155
156
157
158
/**
159
* a handy macro to be placed at the top of a function definition
160
* below the variable declarations to ensure a function is called once
161
* at maximum.
162
*/
163
#define _CALLED_ONLY_ONCE static int ncalls = 0; if (ncalls>0) return; else ncalls++
164
165
#ifdef __cplusplus
166
}
167
#endif
168
169
#endif
170
171