FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

use enum name in repr by koubaa · Pull Request #2239 · pythonnet/pythonnet · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (1) .md  (1) .py  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

### Added

- use enum name in repr

### Changed

### Fixed
Expand Down
51 changes: 51 additions & 0 deletions src/runtime/Types/ClassObject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;

namespace Python.Runtime
Expand Down Expand Up @@ -47,6 +49,55 @@ internal NewReference GetDocString()
return Runtime.PyString_FromString(str);
}

private static string ConvertFlags(Enum value)
{
Type primitiveType = value.GetType().GetEnumUnderlyingType();
string format = "X" + (Marshal.SizeOf(primitiveType) * 2).ToString(CultureInfo.InvariantCulture);
var primitive = (IFormattable)Convert.ChangeType(value, primitiveType);
return "0x" + primitive.ToString(format, null);

}

private static string ConvertValue(Enum value)
{
Type primitiveType = value.GetType().GetEnumUnderlyingType();
return Convert.ChangeType(value, primitiveType).ToString()!;
}

/// <summary>
/// given an enum, write a __repr__ string formatted in the same
/// way as a python repr string. Something like:
/// '&lt;Color.GREEN: 2&gt;';
/// with a binary value for [Flags] enums
/// </summary>
/// <param name="inst">Instace of the enum object</param>
/// <returns></returns>
private static string GetEnumReprString(Enum inst)
{
var obType = inst.GetType();

string strValue2 = obType.IsFlagsEnum() ? ConvertFlags(inst) : ConvertValue(inst);

var repr = $"<{obType.Name}.{inst}: {strValue2}>";
return repr;
}

/// <summary>
/// ClassObject __repr__ implementation.
/// </summary>
public new static NewReference tp_repr(BorrowedReference ob)
{
if (GetManagedObject(ob) is not CLRObject co)
{
return Exceptions.RaiseTypeError("invalid object");
}
if (co.inst.GetType().IsEnum)
{
return Runtime.PyString_FromString(GetEnumReprString((Enum)co.inst));
}

return ClassBase.tp_repr(ob);
}

/// <summary>
/// Implements __new__ for reflected classes and value types.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_enum.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ def test_enum_undefined_value():
Test.FieldTest().EnumField = Test.ShortEnum(20, True)


def test_enum_repr():
"""Test enumeration repr."""
from System import DayOfWeek

assert repr(DayOfWeek.Monday) == "<DayOfWeek.Monday: 1>"

assert repr(Test.FlagsEnum(7)) == "<FlagsEnum.Two, Five: 0x00000007>"
assert repr(Test.FlagsEnum(8)) == "<FlagsEnum.8: 0x00000008>"


def test_enum_conversion():
"""Test enumeration conversion."""
ob = Test.FieldTest()
Expand Down

Back | FazBrowse Home | New Git URL