Skip to content

Commit 192034d

Browse files
committed
Fixed problem with decimal.Decimal conversions
1 parent a320f25 commit 192034d

File tree

9 files changed

+367
-18
lines changed

9 files changed

+367
-18
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2010-02-15 Federico Di Gregorio <fog@initd.org>
2+
3+
* Added new Decimal adapter that correctly converts NaN and infinity
4+
to PostgreSQL NaN numeric values. Also added tests.
5+
16
2010-02-15 Daniele Varrazzo <daniele.varrazzo@gmail.com>
27

38
* lib/errorcodes.py: Updated to PostgreSQL 8.4; added lookup() function.

psycopg/adapter_pdecimal.c

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
/* adapter_pdecimal.c - psycopg Decimal type wrapper implementation
2+
*
3+
* Copyright (C) 2003-2010 Federico Di Gregorio <fog@debian.org>
4+
*
5+
* This file is part of psycopg.
6+
*
7+
* psycopg2 is free software: you can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* In addition, as a special exception, the copyright holders give
13+
* permission to link this program with the OpenSSL library (or with
14+
* modified versions of OpenSSL that use the same license as OpenSSL),
15+
* and distribute linked combinations including the two.
16+
*
17+
* You must obey the GNU Lesser General Public License in all respects for
18+
* all of the code used other than OpenSSL.
19+
*
20+
* psycopg2 is distributed in the hope that it will be useful, but WITHOUT
21+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
23+
* License for more details.
24+
*/
25+
26+
#define PY_SSIZE_T_CLEAN
27+
#include <Python.h>
28+
#include <structmember.h>
29+
#include <floatobject.h>
30+
#include <math.h>
31+
32+
#define PSYCOPG_MODULE
33+
#include "psycopg/config.h"
34+
#include "psycopg/python.h"
35+
#include "psycopg/psycopg.h"
36+
#include "psycopg/adapter_pdecimal.h"
37+
#include "psycopg/microprotocols_proto.h"
38+
39+
40+
/** the Decimal object **/
41+
42+
static PyObject *
43+
pdecimal_str(pdecimalObject *self)
44+
{
45+
PyObject *res = NULL;
46+
PyObject *check = PyObject_CallMethod(self->wrapped, "is_finite", NULL);
47+
48+
if (check == Py_True)
49+
res = PyObject_Str(self->wrapped);
50+
else
51+
res = PyString_FromString("'NaN'::numeric");
52+
53+
Py_DECREF(check);
54+
return res;
55+
}
56+
57+
static PyObject *
58+
pdecimal_getquoted(pdecimalObject *self, PyObject *args)
59+
{
60+
if (!PyArg_ParseTuple(args, "")) return NULL;
61+
return pdecimal_str(self);
62+
}
63+
64+
static PyObject *
65+
pdecimal_conform(pdecimalObject *self, PyObject *args)
66+
{
67+
PyObject *res, *proto;
68+
69+
if (!PyArg_ParseTuple(args, "O", &proto)) return NULL;
70+
71+
if (proto == (PyObject*)&isqlquoteType)
72+
res = (PyObject*)self;
73+
else
74+
res = Py_None;
75+
76+
Py_INCREF(res);
77+
return res;
78+
}
79+
80+
/** the Decimal object */
81+
82+
/* object member list */
83+
84+
static struct PyMemberDef pdecimalObject_members[] = {
85+
{"adapted", T_OBJECT, offsetof(pdecimalObject, wrapped), RO},
86+
{NULL}
87+
};
88+
89+
/* object method table */
90+
91+
static PyMethodDef pdecimalObject_methods[] = {
92+
{"getquoted", (PyCFunction)pdecimal_getquoted, METH_VARARGS,
93+
"getquoted() -> wrapped object value as SQL-quoted string"},
94+
{"__conform__", (PyCFunction)pdecimal_conform, METH_VARARGS, NULL},
95+
{NULL} /* Sentinel */
96+
};
97+
98+
/* initialization and finalization methods */
99+
100+
static int
101+
pdecimal_setup(pdecimalObject *self, PyObject *obj)
102+
{
103+
Dprintf("pdecimal_setup: init pdecimal object at %p, refcnt = "
104+
FORMAT_CODE_PY_SSIZE_T,
105+
self, ((PyObject *)self)->ob_refcnt
106+
);
107+
108+
Py_INCREF(obj);
109+
self->wrapped = obj;
110+
111+
Dprintf("pdecimal_setup: good pdecimal object at %p, refcnt = "
112+
FORMAT_CODE_PY_SSIZE_T,
113+
self, ((PyObject *)self)->ob_refcnt
114+
);
115+
return 0;
116+
}
117+
118+
static int
119+
pdecimal_traverse(PyObject *obj, visitproc visit, void *arg)
120+
{
121+
pdecimalObject *self = (pdecimalObject *)obj;
122+
123+
Py_VISIT(self->wrapped);
124+
return 0;
125+
}
126+
127+
static void
128+
pdecimal_dealloc(PyObject* obj)
129+
{
130+
pdecimalObject *self = (pdecimalObject *)obj;
131+
132+
Py_CLEAR(self->wrapped);
133+
134+
Dprintf("pdecimal_dealloc: deleted pdecimal object at %p, refcnt = "
135+
FORMAT_CODE_PY_SSIZE_T,
136+
obj, obj->ob_refcnt
137+
);
138+
139+
obj->ob_type->tp_free(obj);
140+
}
141+
142+
static int
143+
pdecimal_init(PyObject *obj, PyObject *args, PyObject *kwds)
144+
{
145+
PyObject *o;
146+
147+
if (!PyArg_ParseTuple(args, "O", &o))
148+
return -1;
149+
150+
return pdecimal_setup((pdecimalObject *)obj, o);
151+
}
152+
153+
static PyObject *
154+
pdecimal_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
155+
{
156+
return type->tp_alloc(type, 0);
157+
}
158+
159+
static void
160+
pdecimal_del(PyObject* self)
161+
{
162+
PyObject_GC_Del(self);
163+
}
164+
165+
static PyObject *
166+
pdecimal_repr(pdecimalObject *self)
167+
{
168+
return PyString_FromFormat("<psycopg2._psycopg.Float object at %p>",
169+
self);
170+
}
171+
172+
173+
/* object type */
174+
175+
#define pdecimalType_doc \
176+
"Decimal(str) -> new Decimal adapter object"
177+
178+
PyTypeObject pdecimalType = {
179+
PyObject_HEAD_INIT(NULL)
180+
0,
181+
"psycopg2._psycopg.Decimal",
182+
sizeof(pdecimalObject),
183+
0,
184+
pdecimal_dealloc, /*tp_dealloc*/
185+
0, /*tp_print*/
186+
187+
0, /*tp_getattr*/
188+
0, /*tp_setattr*/
189+
190+
0, /*tp_compare*/
191+
192+
(reprfunc)pdecimal_repr, /*tp_repr*/
193+
0, /*tp_as_number*/
194+
0, /*tp_as_sequence*/
195+
0, /*tp_as_mapping*/
196+
0, /*tp_hash */
197+
198+
0, /*tp_call*/
199+
(reprfunc)pdecimal_str, /*tp_str*/
200+
201+
0, /*tp_getattro*/
202+
0, /*tp_setattro*/
203+
0, /*tp_as_buffer*/
204+
205+
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
206+
pdecimalType_doc, /*tp_doc*/
207+
208+
pdecimal_traverse, /*tp_traverse*/
209+
0, /*tp_clear*/
210+
211+
0, /*tp_richcompare*/
212+
0, /*tp_weaklistoffset*/
213+
214+
0, /*tp_iter*/
215+
0, /*tp_iternext*/
216+
217+
/* Attribute descriptor and subclassing stuff */
218+
219+
pdecimalObject_methods, /*tp_methods*/
220+
pdecimalObject_members, /*tp_members*/
221+
0, /*tp_getset*/
222+
0, /*tp_base*/
223+
0, /*tp_dict*/
224+
225+
0, /*tp_descr_get*/
226+
0, /*tp_descr_set*/
227+
0, /*tp_dictoffset*/
228+
229+
pdecimal_init, /*tp_init*/
230+
0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
231+
pdecimal_new, /*tp_new*/
232+
(freefunc)pdecimal_del, /*tp_free Low-level free-memory routine */
233+
0, /*tp_is_gc For PyObject_IS_GC */
234+
0, /*tp_bases*/
235+
0, /*tp_mro method resolution order */
236+
0, /*tp_cache*/
237+
0, /*tp_subclasses*/
238+
0 /*tp_weaklist*/
239+
};
240+
241+
242+
/** module-level functions **/
243+
244+
PyObject *
245+
psyco_Decimal(PyObject *module, PyObject *args)
246+
{
247+
PyObject *obj;
248+
249+
if (!PyArg_ParseTuple(args, "O", &obj))
250+
return NULL;
251+
252+
return PyObject_CallFunction((PyObject *)&pdecimalType, "O", obj);
253+
}

psycopg/adapter_pdecimal.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* adapter_pdecimal.h - definition for the psycopg Decimal type wrapper
2+
*
3+
* Copyright (C) 2003-2010 Federico Di Gregorio <fog@debian.org>
4+
*
5+
* This file is part of psycopg.
6+
*
7+
* psycopg2 is free software: you can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* In addition, as a special exception, the copyright holders give
13+
* permission to link this program with the OpenSSL library (or with
14+
* modified versions of OpenSSL that use the same license as OpenSSL),
15+
* and distribute linked combinations including the two.
16+
*
17+
* You must obey the GNU Lesser General Public License in all respects for
18+
* all of the code used other than OpenSSL.
19+
*
20+
* psycopg2 is distributed in the hope that it will be useful, but WITHOUT
21+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
23+
* License for more details.
24+
*/
25+
26+
#ifndef PSYCOPG_PDECIMAL_H
27+
#define PSYCOPG_PDECIMAL_H 1
28+
29+
#define PY_SSIZE_T_CLEAN
30+
#include <Python.h>
31+
32+
#include "psycopg/config.h"
33+
34+
#ifdef __cplusplus
35+
extern "C" {
36+
#endif
37+
38+
extern HIDDEN PyTypeObject pdecimalType;
39+
40+
typedef struct {
41+
PyObject_HEAD
42+
43+
/* this is the real object we wrap */
44+
PyObject *wrapped;
45+
46+
} pdecimalObject;
47+
48+
/* functions exported to psycopgmodule.c */
49+
50+
HIDDEN PyObject *psyco_Decimal(PyObject *module, PyObject *args);
51+
#define psyco_Decimal_doc \
52+
"Decimal(obj) -> new decimal.Decimal value"
53+
54+
#ifdef __cplusplus
55+
}
56+
#endif
57+
58+
#endif /* !defined(PSYCOPG_PDECIMAL_H) */

0 commit comments

Comments
 (0)