Skip to content

Commit 9d2c31a

Browse files
authored
Merge pull request #1 from AlexCatarino/master
Support for decimal and fixing build issues in pythonnet
2 parents ebf31a8 + d26f48a commit 9d2c31a

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def build_extension(self, ext):
160160
enable_shared = False
161161

162162
if not enable_shared:
163-
defines.append("PYTHON_WITHOUT_ENABLE_SHARED")
163+
pass#defines.append("PYTHON_WITHOUT_ENABLE_SHARED")
164164

165165
if hasattr(sys, "abiflags"):
166166
if "d" in sys.abiflags:

src/runtime/converter.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
101101
return Runtime.PyLongType;
102102
}
103103
else if ((op == doubleType) ||
104-
(op == singleType))
104+
(op == singleType) ||
105+
(op == decimalType))
105106
{
106107
return Runtime.PyFloatType;
107108
}
@@ -214,6 +215,17 @@ internal static IntPtr ToPython(Object value, Type type)
214215
case TypeCode.UInt64:
215216
return Runtime.PyLong_FromUnsignedLongLong((ulong)value);
216217

218+
case TypeCode.Decimal:
219+
IntPtr mod = Runtime.PyImport_ImportModule("decimal");
220+
IntPtr ctor = Runtime.PyObject_GetAttrString(mod, "Decimal");
221+
222+
string d2s = ((decimal)value).ToString(nfi);
223+
IntPtr d2p = Runtime.PyString_FromString(d2s);
224+
IntPtr args = Runtime.PyTuple_New(1);
225+
Runtime.PyTuple_SetItem(args, 0, d2p);
226+
227+
return Runtime.PyObject_CallObject(ctor, args);
228+
217229
default:
218230
if (value is IEnumerable)
219231
{
@@ -798,6 +810,18 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result,
798810
}
799811
result = d;
800812
return true;
813+
814+
case TypeCode.Decimal:
815+
op = Runtime.PyObject_Str(value);
816+
decimal m;
817+
string sm = Runtime.GetManagedString(op);
818+
if (!Decimal.TryParse(sm, NumberStyles.Number, nfi, out m))
819+
{
820+
goto type_error;
821+
}
822+
Runtime.XDecref(op);
823+
result = m;
824+
return true;
801825
}
802826

803827

0 commit comments

Comments
 (0)