@@ -27,7 +27,6 @@ private Converter()
27
27
private static Type int16Type ;
28
28
private static Type int32Type ;
29
29
private static Type int64Type ;
30
- private static Type flagsType ;
31
30
private static Type boolType ;
32
31
private static Type typeType ;
33
32
@@ -42,7 +41,6 @@ static Converter()
42
41
singleType = typeof ( Single ) ;
43
42
doubleType = typeof ( Double ) ;
44
43
decimalType = typeof ( Decimal ) ;
45
- flagsType = typeof ( FlagsAttribute ) ;
46
44
boolType = typeof ( Boolean ) ;
47
45
typeType = typeof ( Type ) ;
48
46
}
@@ -148,7 +146,8 @@ internal static IntPtr ToPython(object value, Type type)
148
146
return result ;
149
147
}
150
148
151
- if ( Type . GetTypeCode ( type ) == TypeCode . Object && value . GetType ( ) != typeof ( object ) ) {
149
+ if ( Type . GetTypeCode ( type ) == TypeCode . Object && value . GetType ( ) != typeof ( object )
150
+ || type . IsEnum ) {
152
151
var encoded = PyObjectConversions . TryEncode ( value , type ) ;
153
152
if ( encoded != null ) {
154
153
result = encoded . Handle ;
@@ -203,6 +202,11 @@ internal static IntPtr ToPython(object value, Type type)
203
202
204
203
type = value . GetType ( ) ;
205
204
205
+ if ( type . IsEnum )
206
+ {
207
+ return CLRObject . GetInstHandle ( value , type ) ;
208
+ }
209
+
206
210
TypeCode tc = Type . GetTypeCode ( type ) ;
207
211
208
212
switch ( tc )
@@ -317,6 +321,18 @@ internal static bool ToManaged(IntPtr value, Type type,
317
321
}
318
322
return Converter . ToManagedValue ( value , type , out result , setError ) ;
319
323
}
324
+ /// <summary>
325
+ /// Return a managed object for the given Python object, taking funny
326
+ /// byref types into account.
327
+ /// </summary>
328
+ /// <param name="value">A Python object</param>
329
+ /// <param name="type">The desired managed type</param>
330
+ /// <param name="result">Receives the managed object</param>
331
+ /// <param name="setError">If true, call <c>Exceptions.SetError</c> with the reason for failure.</param>
332
+ /// <returns>True on success</returns>
333
+ internal static bool ToManaged ( BorrowedReference value , Type type ,
334
+ out object result , bool setError )
335
+ => ToManaged ( value . DangerousGetAddress ( ) , type , out result , setError ) ;
320
336
321
337
internal static bool ToManagedValue ( BorrowedReference value , Type obType ,
322
338
out object result , bool setError )
@@ -398,11 +414,6 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
398
414
return ToArray ( value , obType , out result , setError ) ;
399
415
}
400
416
401
- if ( obType . IsEnum )
402
- {
403
- return ToEnum ( value , obType , out result , setError ) ;
404
- }
405
-
406
417
// Conversion to 'Object' is done based on some reasonable default
407
418
// conversions (Python string -> managed string, Python int -> Int32 etc.).
408
419
if ( obType == objectType )
@@ -497,7 +508,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
497
508
}
498
509
499
510
TypeCode typeCode = Type . GetTypeCode ( obType ) ;
500
- if ( typeCode == TypeCode . Object )
511
+ if ( typeCode == TypeCode . Object || obType . IsEnum )
501
512
{
502
513
IntPtr pyType = Runtime . PyObject_TYPE ( value ) ;
503
514
if ( PyObjectConversions . TryDecode ( value , pyType , obType , out result ) )
@@ -516,8 +527,17 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
516
527
/// </summary>
517
528
private static bool ToPrimitive ( IntPtr value , Type obType , out object result , bool setError )
518
529
{
519
- TypeCode tc = Type . GetTypeCode ( obType ) ;
520
530
result = null ;
531
+ if ( obType . IsEnum )
532
+ {
533
+ if ( setError )
534
+ {
535
+ Exceptions . SetError ( Exceptions . TypeError , "since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value)" ) ;
536
+ }
537
+ return false ;
538
+ }
539
+
540
+ TypeCode tc = Type . GetTypeCode ( obType ) ;
521
541
IntPtr op = IntPtr . Zero ;
522
542
523
543
switch ( tc )
@@ -876,40 +896,6 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
876
896
result = items ;
877
897
return true ;
878
898
}
879
-
880
-
881
- /// <summary>
882
- /// Convert a Python value to a correctly typed managed enum instance.
883
- /// </summary>
884
- private static bool ToEnum ( IntPtr value , Type obType , out object result , bool setError )
885
- {
886
- Type etype = Enum . GetUnderlyingType ( obType ) ;
887
- result = null ;
888
-
889
- if ( ! ToPrimitive ( value , etype , out result , setError ) )
890
- {
891
- return false ;
892
- }
893
-
894
- if ( Enum . IsDefined ( obType , result ) )
895
- {
896
- result = Enum . ToObject ( obType , result ) ;
897
- return true ;
898
- }
899
-
900
- if ( obType . GetCustomAttributes ( flagsType , true ) . Length > 0 )
901
- {
902
- result = Enum . ToObject ( obType , result ) ;
903
- return true ;
904
- }
905
-
906
- if ( setError )
907
- {
908
- Exceptions . SetError ( Exceptions . ValueError , "invalid enumeration value" ) ;
909
- }
910
-
911
- return false ;
912
- }
913
899
}
914
900
915
901
public static class ConverterExtension
0 commit comments