Skip to content

Commit

Permalink
Merge pull request #1 from AlexCatarino/master
Browse files Browse the repository at this point in the history
Support for decimal and fixing build issues in pythonnet
  • Loading branch information
jaredbroad authored Jan 6, 2017
2 parents ebf31a8 + d26f48a commit 9d2c31a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def build_extension(self, ext):
enable_shared = False

if not enable_shared:
defines.append("PYTHON_WITHOUT_ENABLE_SHARED")
pass#defines.append("PYTHON_WITHOUT_ENABLE_SHARED")

if hasattr(sys, "abiflags"):
if "d" in sys.abiflags:
Expand Down
26 changes: 25 additions & 1 deletion src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
return Runtime.PyLongType;
}
else if ((op == doubleType) ||
(op == singleType))
(op == singleType) ||
(op == decimalType))
{
return Runtime.PyFloatType;
}
Expand Down Expand Up @@ -214,6 +215,17 @@ internal static IntPtr ToPython(Object value, Type type)
case TypeCode.UInt64:
return Runtime.PyLong_FromUnsignedLongLong((ulong)value);

case TypeCode.Decimal:
IntPtr mod = Runtime.PyImport_ImportModule("decimal");
IntPtr ctor = Runtime.PyObject_GetAttrString(mod, "Decimal");

string d2s = ((decimal)value).ToString(nfi);
IntPtr d2p = Runtime.PyString_FromString(d2s);
IntPtr args = Runtime.PyTuple_New(1);
Runtime.PyTuple_SetItem(args, 0, d2p);

return Runtime.PyObject_CallObject(ctor, args);

default:
if (value is IEnumerable)
{
Expand Down Expand Up @@ -798,6 +810,18 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result,
}
result = d;
return true;

case TypeCode.Decimal:
op = Runtime.PyObject_Str(value);
decimal m;
string sm = Runtime.GetManagedString(op);
if (!Decimal.TryParse(sm, NumberStyles.Number, nfi, out m))
{
goto type_error;
}
Runtime.XDecref(op);
result = m;
return true;
}


Expand Down

0 comments on commit 9d2c31a

Please sign in to comment.