Skip to content

Commit fe40b78

Browse files
committed
do not allow encoders for System.String
1 parent 104ee8c commit fe40b78

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/runtime/converter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ internal static IntPtr ToPython(object value, Type type)
134134
return result;
135135
}
136136

137-
if (!type.IsPrimitive && value.GetType() != typeof(object)) {
137+
if (EncodersAllowed(type)) {
138138
var encoded = PyObjectConversions.TryEncode(value, type);
139139
if (encoded != null) {
140140
result = encoded.Handle;
@@ -402,7 +402,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
402402
return false;
403403
}
404404

405-
if (!obType.IsPrimitive)
405+
if (EncodersAllowed(obType))
406406
{
407407
var pyType = new BorrowedReference(Runtime.PyObject_TYPE(value));
408408
if (PyObjectConversions.TryDecode(new BorrowedReference(value), pyType, obType, out result))
@@ -419,6 +419,8 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
419419
return ToPrimitive(value, obType, out result, setError);
420420
}
421421

422+
static bool EncodersAllowed(Type type) => !type.IsPrimitive && type != typeof(string);
423+
422424
internal delegate bool TryConvertFromPythonDelegate(BorrowedReference pyObj, out object result);
423425

424426
/// <summary>

src/runtime/converterextensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Python.Runtime
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Reflection;
8+
using System.Runtime.CompilerServices;
9+
810
using Python.Runtime.Codecs;
911

1012
/// <summary>
@@ -128,6 +130,8 @@ internal static bool TryDecode(BorrowedReference obj, BorrowedReference objType,
128130

129131
static Converter.TryConvertFromPythonDelegate GetDecoder(IntPtr sourceType, Type targetType)
130132
{
133+
RuntimeHelpers.EnsureSufficientExecutionStack();
134+
131135
IPyObjectDecoder decoder;
132136
var pyType = new PyObject(Runtime.SelfIncRef(sourceType));
133137
lock (decoders)

0 commit comments

Comments
 (0)