Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit22d07dd

Browse files
koubaaMohamed Koubaa
and
Mohamed Koubaa
authored
custom repr for enum values (#2239)
Examples:<DayOfWeek.Monday: 1><FlagsEnum.Two, Five: 0x00000007><FlagsEnum.8: 0x00000008>---------Co-authored-by: Mohamed Koubaa <koubaa@github.com>
1 parent171cee4 commit22d07dd

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

‎CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
99

1010
###Added
1111

12+
- use enum name in repr
13+
1214
###Changed
1315

1416
###Fixed

‎src/runtime/Types/ClassObject.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
usingSystem;
22
usingSystem.Diagnostics;
3+
usingSystem.Globalization;
34
usingSystem.Linq;
45
usingSystem.Reflection;
6+
usingSystem.Runtime.InteropServices;
57
usingSystem.Runtime.Serialization;
68

79
namespacePython.Runtime
@@ -47,6 +49,55 @@ internal NewReference GetDocString()
4749
returnRuntime.PyString_FromString(str);
4850
}
4951

52+
privatestaticstringConvertFlags(Enumvalue)
53+
{
54+
TypeprimitiveType=value.GetType().GetEnumUnderlyingType();
55+
stringformat="X"+(Marshal.SizeOf(primitiveType)*2).ToString(CultureInfo.InvariantCulture);
56+
varprimitive=(IFormattable)Convert.ChangeType(value,primitiveType);
57+
return"0x"+primitive.ToString(format,null);
58+
59+
}
60+
61+
privatestaticstringConvertValue(Enumvalue)
62+
{
63+
TypeprimitiveType=value.GetType().GetEnumUnderlyingType();
64+
returnConvert.ChangeType(value,primitiveType).ToString()!;
65+
}
66+
67+
/// <summary>
68+
/// given an enum, write a __repr__ string formatted in the same
69+
/// way as a python repr string. Something like:
70+
/// '&lt;Color.GREEN: 2&gt;';
71+
/// with a binary value for [Flags] enums
72+
/// </summary>
73+
/// <param name="inst">Instace of the enum object</param>
74+
/// <returns></returns>
75+
privatestaticstringGetEnumReprString(Enuminst)
76+
{
77+
varobType=inst.GetType();
78+
79+
stringstrValue2=obType.IsFlagsEnum()?ConvertFlags(inst):ConvertValue(inst);
80+
81+
varrepr=$"<{obType.Name}.{inst}:{strValue2}>";
82+
returnrepr;
83+
}
84+
85+
/// <summary>
86+
/// ClassObject __repr__ implementation.
87+
/// </summary>
88+
publicnewstaticNewReferencetp_repr(BorrowedReferenceob)
89+
{
90+
if(GetManagedObject(ob)is notCLRObjectco)
91+
{
92+
returnExceptions.RaiseTypeError("invalid object");
93+
}
94+
if(co.inst.GetType().IsEnum)
95+
{
96+
returnRuntime.PyString_FromString(GetEnumReprString((Enum)co.inst));
97+
}
98+
99+
returnClassBase.tp_repr(ob);
100+
}
50101

51102
/// <summary>
52103
/// Implements __new__ for reflected classes and value types.

‎tests/test_enum.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ def test_enum_undefined_value():
143143
Test.FieldTest().EnumField=Test.ShortEnum(20,True)
144144

145145

146+
deftest_enum_repr():
147+
"""Test enumeration repr."""
148+
fromSystemimportDayOfWeek
149+
150+
assertrepr(DayOfWeek.Monday)=="<DayOfWeek.Monday: 1>"
151+
152+
assertrepr(Test.FlagsEnum(7))=="<FlagsEnum.Two, Five: 0x00000007>"
153+
assertrepr(Test.FlagsEnum(8))=="<FlagsEnum.8: 0x00000008>"
154+
155+
146156
deftest_enum_conversion():
147157
"""Test enumeration conversion."""
148158
ob=Test.FieldTest()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp