@@ -27,7 +27,6 @@ private Converter()
2727 private static Type int16Type ;
2828 private static Type int32Type ;
2929 private static Type int64Type ;
30- private static Type flagsType ;
3130 private static Type boolType ;
3231 private static Type typeType ;
3332
@@ -42,7 +41,6 @@ static Converter()
4241 singleType = typeof ( Single ) ;
4342 doubleType = typeof ( Double ) ;
4443 decimalType = typeof ( Decimal ) ;
45- flagsType = typeof ( FlagsAttribute ) ;
4644 boolType = typeof ( Boolean ) ;
4745 typeType = typeof ( Type ) ;
4846 }
@@ -148,7 +146,8 @@ internal static IntPtr ToPython(object value, Type type)
148146 return result ;
149147 }
150148
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 ) {
152151 var encoded = PyObjectConversions . TryEncode ( value , type ) ;
153152 if ( encoded != null ) {
154153 result = encoded . Handle ;
@@ -203,6 +202,11 @@ internal static IntPtr ToPython(object value, Type type)
203202
204203 type = value . GetType ( ) ;
205204
205+ if ( type . IsEnum )
206+ {
207+ return CLRObject . GetInstHandle ( value , type ) ;
208+ }
209+
206210 TypeCode tc = Type . GetTypeCode ( type ) ;
207211
208212 switch ( tc )
@@ -317,6 +321,18 @@ internal static bool ToManaged(IntPtr value, Type type,
317321 }
318322 return Converter . ToManagedValue ( value , type , out result , setError ) ;
319323 }
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 ) ;
320336
321337 internal static bool ToManagedValue ( BorrowedReference value , Type obType ,
322338 out object result , bool setError )
@@ -398,11 +414,6 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
398414 return ToArray ( value , obType , out result , setError ) ;
399415 }
400416
401- if ( obType . IsEnum )
402- {
403- return ToEnum ( value , obType , out result , setError ) ;
404- }
405-
406417 // Conversion to 'Object' is done based on some reasonable default
407418 // conversions (Python string -> managed string, Python int -> Int32 etc.).
408419 if ( obType == objectType )
@@ -497,7 +508,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
497508 }
498509
499510 TypeCode typeCode = Type . GetTypeCode ( obType ) ;
500- if ( typeCode == TypeCode . Object )
511+ if ( typeCode == TypeCode . Object || obType . IsEnum )
501512 {
502513 IntPtr pyType = Runtime . PyObject_TYPE ( value ) ;
503514 if ( PyObjectConversions . TryDecode ( value , pyType , obType , out result ) )
@@ -516,8 +527,17 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
516527 /// </summary>
517528 private static bool ToPrimitive ( IntPtr value , Type obType , out object result , bool setError )
518529 {
519- TypeCode tc = Type . GetTypeCode ( obType ) ;
520530 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 ) ;
521541 IntPtr op = IntPtr . Zero ;
522542
523543 switch ( tc )
@@ -876,40 +896,6 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
876896 result = items ;
877897 return true ;
878898 }
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- }
913899 }
914900
915901 public static class ConverterExtension
0 commit comments