Skip to content

Support clr.GetClrType() - as in IronPython #433

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 7 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Sam Winstanley ([@swinstanley](https://github.com/swinstanley))
- Sean Freitag ([@cowboygneox](https://github.com/cowboygneox))
- Serge Weinstock ([@sweinst](https://github.com/sweinst))
- Ville M. Vainio ([@vivainio](https://github.com/vivainio))
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
- Wenguang Yang ([@yagweb](https://github.com/yagweb))
- Xavier Dupré ([@sdpython](https://github.com/sdpython))
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
## [unreleased][]

### Added

- Added clr.GetClrType (#432)(#433)
- Added `Foo` feature

### Changed
Expand Down
17 changes: 17 additions & 0 deletions src/runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,23 @@ public static Assembly AddReference(string name)
return assembly;
}

/// <summary>
/// Get a Type instance for a class object.
/// clr.GetClrType(IComparable) gives you the Type for IComparable,
/// that you can e.g. perform reflection on. Similar to typeof(IComparable) in C#
/// or clr.GetClrType(IComparable) in IronPython.
///
/// </summary>
/// <param name="type"></param>
/// <returns>The Type object</returns>

[ModuleFunction]
[ForbidPythonThreads]
public static Type GetClrType(Type type)
{
return type;
}

[ModuleFunction]
[ForbidPythonThreads]
public static string FindAssembly(string name)
Expand Down
20 changes: 20 additions & 0 deletions src/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,26 @@ def test_clr_add_reference():
with pytest.raises(FileNotFoundException):
AddReference("somethingtotallysilly")

def test_clr_get_clr_type():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the test to invoke the ArgumentException error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"""Test clr.GetClrType()."""
from clr import GetClrType
import System
from System import IComparable
from System import ArgumentException
assert GetClrType(System.String).FullName == "System.String"
comparable = GetClrType(IComparable)
assert comparable.FullName == "System.IComparable"
assert comparable.IsInterface
assert GetClrType(int).FullName == "System.Int32"
assert GetClrType(str).FullName == "System.String"
assert GetClrType(float).FullName == "System.Double"
dblarr = System.Array[System.Double]
assert GetClrType(dblarr).FullName == "System.Double[]"

with pytest.raises(TypeError):
GetClrType(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you compare also against types, not instance of types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate, what kind of comparison I should add?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetClrType(int), GetClrType(str)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check lines 365-367 in the diff :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see now let's wait for @filmor to approve

with pytest.raises(TypeError):
GetClrType("thiswillfail")

def test_assembly_load_thread_safety():
from Python.Test import ModuleTest
Expand Down