-
Notifications
You must be signed in to change notification settings - Fork 566
/
Copy pathdecimal.cpp
141 lines (106 loc) · 3.99 KB
/
decimal.cpp
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
#include "pyodbc.h"
#include "wrapper.h"
#include "textenc.h"
#include "decimal.h"
static PyObject* decimal = 0;
// The Decimal constructor.
static PyObject* re_sub = 0;
static PyObject* re_compile = 0;
static PyObject* re_escape = 0;
// In Python 2.7, the 3 strings below are bytes objects. In 3.x they are Unicode objects.
static PyObject* pDecimalPoint = 0;
// A "." object which we replace pLocaleDecimal with if they are not the same.
static PyObject* pLocaleDecimal = 0;
// The decimal character used by the locale. This can be overridden by the user.
//
// In 2.7 this is a bytes object, otherwise unicode.
static PyObject* pLocaleDecimalEscaped = 0;
// A version of pLocaleDecimal escaped to be used in a regular expression. (The character
// could be something special in regular expressions.) This is zero when pLocaleDecimal is
// ".", indicating no replacement is necessary.
static PyObject* pRegExpRemove = 0;
// A regular expression that matches characters we want to remove before parsing.
bool InitializeDecimal() {
// This is called when the module is initialized and creates globals.
Object d(PyImport_ImportModule("decimal"));
decimal = PyObject_GetAttrString(d, "Decimal");
if (!decimal)
return 0;
Object re(PyImport_ImportModule("re"));
re_sub = PyObject_GetAttrString(re, "sub");
re_escape = PyObject_GetAttrString(re, "escape");
re_compile = PyObject_GetAttrString(re, "compile");
Object module(PyImport_ImportModule("locale"));
Object ldict(PyObject_CallMethod(module, "localeconv", 0));
Object point(PyDict_GetItemString(ldict, "decimal_point"));
if (!point)
return false;
pDecimalPoint = PyUnicode_FromString(".");
if (!pDecimalPoint)
return false;
if (!SetDecimalPoint(point))
return false;
return true;
}
PyObject* GetDecimalPoint() {
Py_INCREF(pLocaleDecimal);
return pLocaleDecimal;
}
bool SetDecimalPoint(PyObject* pNew)
{
if (PyObject_RichCompareBool(pNew, pDecimalPoint, Py_EQ) == 1)
{
// They are the same.
Py_XDECREF(pLocaleDecimal);
pLocaleDecimal = pDecimalPoint;
Py_INCREF(pLocaleDecimal);
Py_XDECREF(pLocaleDecimalEscaped);
pLocaleDecimalEscaped = 0;
}
else
{
// They are different, so we'll need a regular expression to match it so it can be
// replaced in getdata GetDataDecimal.
Py_XDECREF(pLocaleDecimal);
pLocaleDecimal = pNew;
Py_INCREF(pLocaleDecimal);
Object e(PyObject_CallFunctionObjArgs(re_escape, pNew, 0));
if (!e)
return false;
Py_XDECREF(pLocaleDecimalEscaped);
pLocaleDecimalEscaped = e.Detach();
}
Object s(PyUnicode_FromFormat("[^0-9%U-]+", pLocaleDecimal));
if (!s)
return false;
Object r(PyObject_CallFunctionObjArgs(re_compile, s.Get(), 0));
if (!r)
return false;
Py_XDECREF(pRegExpRemove);
pRegExpRemove = r.Detach();
return true;
}
PyObject* DecimalFromText(const TextEnc& enc, const byte* pb, Py_ssize_t cb)
{
// Creates a Decimal object from a text buffer.
// The Decimal constructor requires the decimal point to be '.', so we need to convert the
// locale's decimal to it. We also need to remove non-decimal characters such as thousands
// separators and currency symbols.
//
// Remember that the thousands separate will often be '.', so have to do this carefully.
// We'll create a regular expression with 0-9 and whatever the thousands separator is.
Object text(TextBufferToObject(enc, pb, cb));
if (!text)
return 0;
Object cleaned(PyObject_CallMethod(pRegExpRemove, "sub", "sO", "", text.Get()));
if (!cleaned)
return 0;
if (pLocaleDecimalEscaped)
{
Object c2(PyObject_CallFunctionObjArgs(re_sub, pLocaleDecimalEscaped, pDecimalPoint, 0));
if (!c2)
return 0;
cleaned.Attach(c2.Detach());
}
return PyObject_CallFunctionObjArgs(decimal, cleaned.Get(), 0);
}