source: c_lib/stdsage.h @ 6672:ee3b6daead74

Revision 6672:ee3b6daead74, 5.3 KB checked in by Robert Bradshaw <robertwb@…>, 6 years ago (diff)

...

Line 
1/******************************************************************************
2       Copyright (C) 2006 William Stein <wstein@gmail.com>
3                     2006 Martin Albrecht <malb@informatik.uni-bremen.de>
4
5  Distributed under the terms of the GNU General Public License (GPL), Version 2.
6
7  The full text of the GPL is available at:
8                  http://www.gnu.org/licenses/
9
10******************************************************************************/
11
12/**
13 * @file stdsage.h
14 *
15 * @author William Stein <wstein@gmail.com>
16 * @auhtor Martin Albrecht <malb@informatik.uni-bremen.de>
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
37/* Building with this not commented out causes
38   serious problems on RHEL5 64-bit for Kiran Kedlaya... i.e., it doesn't work. */
39/* #include "ccobject.h" */
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/*****************************************
46          For PARI
47          Memory management
48
49 *****************************************/
50
51#define set_gel(x, n, z)  gel(x,n)=z;
52
53/******************************************
54 Some macros exported for Pyrex in cdefs.pxi
55 ****************************************/
56
57/** Tests whether zzz_obj is of type zzz_type. The zzz_type must be a
58 * built-in or extension type. This is just a C++-compatible wrapper
59 * for PyObject_TypeCheck.
60 */
61#define PY_TYPE_CHECK(zzz_obj, zzz_type) \
62    (PyObject_TypeCheck((PyObject*)(zzz_obj), (PyTypeObject*)(zzz_type)))
63
64/** Returns the type field of a python object, cast to void*. The
65 *  returned value should only be used as an opaque object e.g. for
66 *  type comparisons.
67 */
68#define PY_TYPE(zzz_obj) ((void*)((zzz_obj)->ob_type))
69
70/** Constructs a new object of type zzz_type by calling tp_new
71 *  directly, with no arguments.
72 */
73
74#define PY_NEW(zzz_type) \
75    (((PyTypeObject*)(zzz_type))->tp_new((PyTypeObject*)(zzz_type), global_empty_tuple, NULL))
76
77/** Resets the tp_new slot of zzz_type1 to point to the tp_new slot of
78 *  zzz_type2. This is used in SAGE to speed up Pyrex's boilerplate
79 *  object construction code by skipping irrelevant base class tp_new
80 *  methods.
81 */
82#define PY_SET_TP_NEW(zzz_type1, zzz_type2) \
83    (((PyTypeObject*)zzz_type1)->tp_new = ((PyTypeObject*)zzz_type2)->tp_new)
84
85
86/**
87 * Tests whether the given object has a python dictionary.
88 */
89#define HAS_DICTIONARY(zzz_obj) \
90    (((PyObject*)(zzz_obj))->ob_type->tp_dictoffset != NULL)
91
92
93/** Returns the type field of a python object, cast to void*. The
94 *  returned value should only be used as an opaque object e.g. for
95 *  type comparisons.
96 */
97#define PY_IS_NUMERIC(zzz_obj) \
98     (PyInt_Check(zzz_obj) ||  PyBool_Check(zzz_obj) || PyLong_Check(zzz_obj) || \
99       PyFloat_Check(zzz_obj) || PyComplex_Check(zzz_obj))
100
101
102/** This is exactly the same as isinstance (and does return a Python
103 *  bool), but the second argument must be a C-extension type -- so it
104 *  can't be a Python class or a list.  If you just want an int return
105 *  value, i.e., aren't going to pass this back to Python, just use
106 *  PY_TYPE_CHECK.
107 */
108#define IS_INSTANCE(zzz_obj, zzz_type) \
109    PyBool_FromLong(PY_TYPE_CHECK(zzz_obj, zzz_type))
110
111/**
112 * A global empty python tuple object. This is used to speed up some
113 * python API calls where we want to avoid constructing a tuple every
114 * time.
115 */
116
117extern PyObject* global_empty_tuple;
118
119
120
121/*****************************************
122          Memory management
123
124NOTE -- before changing these away from Python's keep the following in
125mind (from the Python C/API guide):
126
127"In most situations, however, it is recommended to allocate memory from
128the Python heap specifically because the latter is under control of
129the Python memory manager. For example, this is required when the
130interpreter is extended with new object types written in C. Another
131reason for using the Python heap is the desire to inform the Python
132memory manager about the memory needs of the extension module. Even
133when the requested memory is used exclusively for internal,
134highly-specific purposes, delegating all memory requests to the Python
135memory manager causes the interpreter to have a more accurate image of
136its memory footprint as a whole. Consequently, under certain
137circumstances, the Python memory manager may or may not trigger
138appropriate actions, like garbage collection, memory compaction or
139other preventive procedures. Note that by using the C library
140allocator as shown in the previous example, the allocated memory for
141the I/O buffer escapes completely the Python memory manager."
142
143 *****************************************/
144
145#define sage_malloc  malloc
146#define sage_free    free
147#define sage_realloc realloc
148
149/**
150 * Initialisation of singal handlers, global variables, etc. Called
151 * exactly once at SAGE start-up.
152 *
153 * @note: It is safe to call this function more than once but nothing
154 * will happen after the first call.
155 */
156void init_csage(void);
157
158
159
160/**
161 * a handy macro to be placed at the top of a function definition
162 * below the variable declarations to ensure a function is called once
163 * at maximum.
164 */
165#define _CALLED_ONLY_ONCE static int ncalls = 0; if (ncalls>0) return; else ncalls++
166
167#ifdef __cplusplus
168}
169#endif
170
171#endif
Note: See TracBrowser for help on using the repository browser.