Repository navigation
Expand file tree
/
Copy pathPythonExtensionPatterns.c
More file actions
157 lines (141 loc) · 3.36 KB
/
Copy pathPythonExtensionPatterns.c
File metadata and controls
157 lines (141 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// PythonExtensionPatterns.c
// PythonExtensionPatterns
//
// Created by Paul Ross on 07/05/2014.
// Copyright (c) 2014 Paul Ross. All rights reserved.
//
#include "Python.h"
#include <stdio.h>
#if 0
/* Examples of design patterns in C that are helpful for Python extensions. */
static PyObject *python_arithmitic_trad(PyObject *str_a, PyObject *str_b, int op) {
/* Local Python objects. */
PyObject *num_a = NULL;
PyObject *num_b = NULL;
/* The Python object to be returned. */
PyObject *ret = NULL;
if(op < 0 || op > 3) {
PyErr_SetString(PyExc_RuntimeError, "Operator not in range.");
return NULL;
}
num_a = PyLong_FromString(str_a, NULL, 0); /* New ref. */
if (! num_a) {
PyErr_SetString(PyExc_ValueError, "Can not read string a.");
return NULL;
}
num_b = PyLong_FromString(str_b, NULL, 0); /* New ref. */
if (! num_b) {
PyErr_SetString(PyExc_ValueError, "Can not read string a.");
return NULL; /* Ooops, forgot to free num_a. */
}
switch (op) {
case 0:
ret = PyNumber_Add(num_a, num_b);
break;
case 1:
ret = PyNumber_Subtract(num_a, num_b);
break;
case 2:
ret = PyNumber_Multiply(num_a, num_b);
break;
case 3:
ret = PyNumber_FloorDivide(num_a, num_b);
break;
default:
exit(1);
break;
}
/* Oh dear, if ret is NULL both loc_a and loc_b are leaked. */
return ret;
}
/* Does some arithmitic on two objects - Returns a new reference. */
static PyObject *_PyNumber_Operate(PyObject *o1, PyObject *o2, int op) {
PyObject *ret= NULL;
goto try;
try:
/* Increment the reference count of the arguments. */
if (o1) {
Py_INCREF(o1);
}
if (o2) {
Py_INCREF(o2);
}
switch (op) {
case 0:
ret = PyNumber_Add(o1, o2);
break;
case 1:
ret = PyNumber_Subtract(o1, o2);
break;
case 2:
ret = PyNumber_Multiply(o1, o2);
break;
case 3:
ret = PyNumber_FloorDivide(o1, o2);
break;
default:
exit(1);
break;
}
if (!ret) {
PyErr_SetString(PyExc_ValueError, "Can not do the math.");
goto except;
} else {
goto finally;
}
except:
/* Failure so Py_XDECREF the return value. */
Py_XDECREF(ret);
ret = NULL;
finally:
/* All _local_ PyObjects declared at the entry point are Py_XDECREF'd. */
/* (nothing to do here). */
/* Decrement the ref count of the arguments. */
Py_XDECREF(o2);
Py_XDECREF(o1);
return ret;
}
static PyObject *python_arithmitic(char *str_a, char *str_b, int op) {
/* Local C variables. */
int err;
/* Local Python objects. */
PyObject *loc_a = NULL;
PyObject *loc_b = NULL;
/* The Python object to be returned. */
PyObject *ret = NULL;
goto try;
try:
err = _check_operator(op);
if(err) {
if (err) {
PyErr_SetString(PyExc_RuntimeError, "Operator not in range.");
goto except;
}
}
loc_a = _PyObject_str_to_int(str_a);
if (!loc_a) {
PyErr_SetString(PyExc_ValueError, "Can not read string a.");
goto except;
}
loc_b = _PyObject_str_to_int(str_b);
if (!loc_b) {
PyErr_SetString(PyExc_ValueError, "Can not read string a.");
goto except;
}
ret = _PyNumber_Operate(loc_a, loc_b, op);
if (!ret) {
goto except;
}
goto finally;
except:
/* Failure so Py_XDECREF the return value. */
Py_XDECREF(ret);
ret = NULL;
finally:
/* All _local_ PyObjects declared at the entry point are Py_XDECREF'd. */
Py_XDECREF(loc_a);
Py_XDECREF(loc_b);
return ret;
}
#endif