8000 use enum name in repr by koubaa · Pull Request #2239 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

use enum name in repr #2239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
use enum name in repr
  • Loading branch information
Mohamed Koubaa committed Sep 18, 2023
commit d9c326cd2bfb6941d435b6f1ed03ec4a11bca5e0
21 changes: 21 additions & 0 deletions src/runtime/Types/ClassObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ internal NewReference GetDocString()
}


/// <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");
}

var inst = co.inst;
var obType = inst.GetType();
if (obType.IsEnum)
{
var repr = string.Format("{0}.{1}", obType.Name, inst.ToString());
return Runtime.PyString_FromString(repr);
}

return ClassBase.tp_repr(ob);
}

/// <summary>
/// Implements __new__ for reflected classes and value types.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ 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) == "Monday"


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