1
/* adapter_float.c - psycopg pfloat type wrapper implementation
3
* Copyright (C) 2003-2009 Federico Di Gregorio <fog@debian.org>
5
* This file is part of psycopg.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2,
10
* or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
#define PY_SSIZE_T_CLEAN
24
#include <structmember.h>
25
#include <floatobject.h>
28
#define PSYCOPG_MODULE
29
#include "psycopg/config.h"
30
#include "psycopg/python.h"
31
#include "psycopg/psycopg.h"
32
#include "psycopg/adapter_pfloat.h"
33
#include "psycopg/microprotocols_proto.h"
36
/** the Float object **/
39
pfloat_str(pfloatObject *self)
41
double n = PyFloat_AsDouble(self->wrapped);
43
return PyString_FromString("'NaN'::float");
45
return PyString_FromString("'Infinity'::float");
47
return PyObject_Str(self->wrapped);
51
pfloat_getquoted(pfloatObject *self, PyObject *args)
53
if (!PyArg_ParseTuple(args, "")) return NULL;
54
return pfloat_str(self);
58
pfloat_conform(pfloatObject *self, PyObject *args)
60
PyObject *res, *proto;
62
if (!PyArg_ParseTuple(args, "O", &proto)) return NULL;
64
if (proto == (PyObject*)&isqlquoteType)
65
res = (PyObject*)self;
73
/** the Float object */
75
/* object member list */
77
static struct PyMemberDef pfloatObject_members[] = {
78
{"adapted", T_OBJECT, offsetof(pfloatObject, wrapped), RO},
82
/* object method table */
84
static PyMethodDef pfloatObject_methods[] = {
85
{"getquoted", (PyCFunction)pfloat_getquoted, METH_VARARGS,
86
"getquoted() -> wrapped object value as SQL-quoted string"},
87
{"__conform__", (PyCFunction)pfloat_conform, METH_VARARGS, NULL},
91
/* initialization and finalization methods */
94
pfloat_setup(pfloatObject *self, PyObject *obj)
96
Dprintf("pfloat_setup: init pfloat object at %p, refcnt = "
97
FORMAT_CODE_PY_SSIZE_T,
98
self, ((PyObject *)self)->ob_refcnt
104
Dprintf("pfloat_setup: good pfloat object at %p, refcnt = "
105
FORMAT_CODE_PY_SSIZE_T,
106
self, ((PyObject *)self)->ob_refcnt
112
pfloat_traverse(PyObject *obj, visitproc visit, void *arg)
114
pfloatObject *self = (pfloatObject *)obj;
116
Py_VISIT(self->wrapped);
121
pfloat_dealloc(PyObject* obj)
123
pfloatObject *self = (pfloatObject *)obj;
125
Py_CLEAR(self->wrapped);
127
Dprintf("pfloat_dealloc: deleted pfloat object at %p, refcnt = "
128
FORMAT_CODE_PY_SSIZE_T,
132
obj->ob_type->tp_free(obj);
136
pfloat_init(PyObject *obj, PyObject *args, PyObject *kwds)
140
if (!PyArg_ParseTuple(args, "O", &o))
143
return pfloat_setup((pfloatObject *)obj, o);
147
pfloat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
149
return type->tp_alloc(type, 0);
153
pfloat_del(PyObject* self)
155
PyObject_GC_Del(self);
159
pfloat_repr(pfloatObject *self)
161
return PyString_FromFormat("<psycopg2._psycopg.Float object at %p>",
168
#define pfloatType_doc \
169
"Float(str) -> new Float adapter object"
171
PyTypeObject pfloatType = {
172
PyObject_HEAD_INIT(NULL)
174
"psycopg2._psycopg.Float",
175
sizeof(pfloatObject),
177
pfloat_dealloc, /*tp_dealloc*/
185
(reprfunc)pfloat_repr, /*tp_repr*/
187
0, /*tp_as_sequence*/
192
(reprfunc)pfloat_str, /*tp_str*/
198
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
199
pfloatType_doc, /*tp_doc*/
201
pfloat_traverse, /*tp_traverse*/
204
0, /*tp_richcompare*/
205
0, /*tp_weaklistoffset*/
210
/* Attribute descriptor and subclassing stuff */
212
pfloatObject_methods, /*tp_methods*/
213
pfloatObject_members, /*tp_members*/
222
pfloat_init, /*tp_init*/
223
0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
224
pfloat_new, /*tp_new*/
225
(freefunc)pfloat_del, /*tp_free Low-level free-memory routine */
226
0, /*tp_is_gc For PyObject_IS_GC */
228
0, /*tp_mro method resolution order */
235
/** module-level functions **/
238
psyco_Float(PyObject *module, PyObject *args)
242
if (!PyArg_ParseTuple(args, "O", &obj))
245
return PyObject_CallFunction((PyObject *)&pfloatType, "O", obj);