~ubuntu-branches/ubuntu/feisty/python-numpy/feisty

« back to all changes in this revision

Viewing changes to numpy/core/code_generators/generate_ufunc_api.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-07-12 10:00:24 UTC
  • Revision ID: james.westby@ubuntu.com-20060712100024-5lw9q2yczlisqcrt
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import genapi
 
3
 
 
4
h_template = r"""
 
5
#ifdef _UMATHMODULE
 
6
 
 
7
static PyTypeObject PyUFunc_Type;
 
8
 
 
9
%s
 
10
 
 
11
#else
 
12
 
 
13
#if defined(PY_UFUNC_UNIQUE_SYMBOL)
 
14
#define PyUFunc_API PY_UFUNC_UNIQUE_SYMBOL
 
15
#endif
 
16
 
 
17
#if defined(NO_IMPORT) || defined(NO_IMPORT_UFUNC)
 
18
extern void **PyUFunc_API;
 
19
#else
 
20
#if defined(PY_UFUNC_UNIQUE_SYMBOL)
 
21
void **PyUFunc_API;
 
22
#else
 
23
static void **PyUFunc_API=NULL;
 
24
#endif
 
25
#endif
 
26
 
 
27
#define PyUFunc_Type (*(PyTypeObject *)PyUFunc_API[0])
 
28
 
 
29
%s
 
30
 
 
31
static int
 
32
import_umath(void)
 
33
{
 
34
  PyObject *numpy = PyImport_ImportModule("numpy.core.umath");
 
35
  PyObject *c_api = NULL;
 
36
 
 
37
  if (numpy == NULL) return -1;
 
38
  c_api = PyObject_GetAttrString(numpy, "_UFUNC_API");
 
39
  if (c_api == NULL) {Py_DECREF(numpy); return -1;}
 
40
  if (PyCObject_Check(c_api)) {
 
41
      PyUFunc_API = (void **)PyCObject_AsVoidPtr(c_api);
 
42
  }
 
43
  Py_DECREF(c_api);
 
44
  Py_DECREF(numpy);
 
45
  if (PyUFunc_API == NULL) return -1;
 
46
  return 0;
 
47
}
 
48
 
 
49
#define import_ufunc import_umath
 
50
 
 
51
#endif
 
52
"""
 
53
 
 
54
c_template = r"""
 
55
/* These pointers will be stored in the C-object for use in other
 
56
    extension modules
 
57
*/
 
58
 
 
59
void *PyUFunc_API[] = {
 
60
        (void *) &PyUFunc_Type,
 
61
%s
 
62
};
 
63
"""
 
64
 
 
65
def generate_api(output_dir):
 
66
    ufunc_api_list = genapi.get_api_functions('UFUNC_API',
 
67
                                              'ufunc_api_order.txt')
 
68
 
 
69
    # API fixes for __arrayobject_api.h
 
70
 
 
71
    fixed = 1
 
72
    nummulti = len(ufunc_api_list)
 
73
    numtotal = fixed + nummulti
 
74
 
 
75
    module_list = []
 
76
    extension_list = []
 
77
    init_list = []
 
78
 
 
79
    #setup object API
 
80
    genapi.add_api_list(fixed, 'PyUFunc_API', ufunc_api_list,
 
81
                        module_list, extension_list, init_list)
 
82
 
 
83
    # Write to header
 
84
    fid = open(os.path.join(output_dir, '__ufunc_api.h'),'w')
 
85
    s = h_template % ('\n'.join(module_list), '\n'.join(extension_list))
 
86
    fid.write(s)
 
87
    fid.close()
 
88
 
 
89
    # Write to c-code
 
90
    fid = open(os.path.join(output_dir, '__ufunc_api.c'),'w')
 
91
    s = c_template % '\n'.join(init_list)
 
92
    fid.write(s)
 
93
    fid.close()