summaryrefslogtreecommitdiff
path: root/cad/src/experimental/pyrex-atoms-bonds/proto.c
blob: b8224b991042a2fa24926fce08ef63638cd05d23 (plain)
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
// Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 
/*
 * C code for prototyping C with Python
 *
 * gcc -Wall -I/usr/include/python2.4 -shared -o proto.so proto.c
 */

#include "Python.h"
PyObject *ProtoError;

#if 1
#define XX(x)  x
#else
#define XX(x)
#endif

static char pointer2int_doc[] =
    "pointer2int(x) -- convert a PyObject* pointer to an int";

static PyObject *
pointer2int(PyObject * self, PyObject * args)
{
    PyObject *x;
    if (!PyArg_ParseTuple (args, "O", &x)) {
        PyErr_SetString (ProtoError, "Argument must be a Python object");
        return NULL;
    }
    return Py_BuildValue("l", x);
}


static char int2pointer_doc[] =
    "int2pointer(x) -- convert an int to a PyObject* pointer";

static PyObject *
int2pointer(PyObject * self, PyObject * args)
{
    long i;
    PyObject *x;
    if (!PyArg_ParseTuple (args, "l", &i)) {
        PyErr_SetString (ProtoError, "Argument must be an integer");
	return NULL;
    }
    x = (PyObject*) i;
    if (x != NULL)
	Py_INCREF(x);
    else
	PyErr_SetString (ProtoError, "Returning null object");
    return x;
}

static char incref_doc[] =
    "incref(x) -- increment the Python reference count for this object";

static PyObject *
incref(PyObject * self, PyObject * args)
{
    long i;
    PyObject *x;
    if (!PyArg_ParseTuple (args, "l", &i)) {
        PyErr_SetString (ProtoError, "Argument must be an integer");
	return NULL;
    }
    x = (PyObject*) i;
    Py_INCREF(x);
    Py_INCREF(Py_None);
    return Py_None;
}

static char decref_doc[] =
    "decref(x) -- decrement the Python reference count for this object";

static PyObject *
decref(PyObject * self, PyObject * args)
{
    long i;
    PyObject *x;
    if (!PyArg_ParseTuple (args, "l", &i)) {
        PyErr_SetString (ProtoError, "Argument must be an integer");
	return NULL;
    }
    x = (PyObject*) i;
    if (x == NULL) {
        PyErr_SetString (ProtoError, "Argument should be non-zero");
	return NULL;
    }
    Py_DECREF(x);
    Py_INCREF(Py_None);
    return Py_None;
}

static char xdecref_doc[] =
    "xdecref(x) -- decrement the refcount for this object if it's not a null pointer";

static PyObject *
xdecref(PyObject * self, PyObject * args)
{
    long i;
    PyObject *x;
    if (!PyArg_ParseTuple (args, "l", &i)) {
        PyErr_SetString (ProtoError, "Argument must be an integer");
	return NULL;
    }
    x = (PyObject*) i;
    Py_XDECREF(x);
    Py_INCREF(Py_None);
    return Py_None;
}

static struct PyMethodDef proto_methods[] = {
    {"pointer2int", (PyCFunction) pointer2int,
     METH_VARARGS|METH_KEYWORDS, pointer2int_doc},
    {"int2pointer", (PyCFunction) int2pointer,
     METH_VARARGS|METH_KEYWORDS, int2pointer_doc},
    {"incref", (PyCFunction) incref,
     METH_VARARGS|METH_KEYWORDS, incref_doc},
    {"decref", (PyCFunction) decref,
     METH_VARARGS|METH_KEYWORDS, decref_doc},
    {"xdecref", (PyCFunction) xdecref,
     METH_VARARGS|METH_KEYWORDS, xdecref_doc},
    {NULL, NULL}
};

static char proto_doc[] =
    "Docstring not written yet.";

DL_EXPORT(void) initproto(void)
{
    PyObject *m, *d;

    /* Create the module and add the functions */
    m = Py_InitModule3("proto", proto_methods, proto_doc);
    /* Add some symbolic constants to the module */
    d = PyModule_GetDict (m);
    ProtoError = PyString_FromString ("ProtoError");
    PyDict_SetItemString (d, "ProtoError", ProtoError);
}