- Notifications
You must be signed in to change notification settings - Fork749
__name__ and __signature__ for .NET methods#1133
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
5 changes: 3 additions & 2 deletionsCHANGELOG.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
25 changes: 25 additions & 0 deletionssrc/embed_tests/Inspect.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
1 change: 1 addition & 0 deletionssrc/runtime/exceptions.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
71 changes: 71 additions & 0 deletionssrc/runtime/methodbinding.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,5 +1,6 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
namespace Python.Runtime | ||
@@ -65,6 +66,67 @@ public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) | ||
return mb.pyHandle; | ||
} | ||
PyObject Signature | ||
{ | ||
get | ||
{ | ||
var infos = this.info.Valid ? new[] { this.info.Value } : this.m.info; | ||
Type type = infos.Select(i => i.DeclaringType) | ||
.OrderByDescending(t => t, new TypeSpecificityComparer()) | ||
.First(); | ||
infos = infos.Where(info => info.DeclaringType == type).ToArray(); | ||
// this is a primitive version | ||
// the overload with the maximum number of parameters should be used | ||
MethodInfo primary = infos.OrderByDescending(i => i.GetParameters().Length).First(); | ||
var primaryParameters = primary.GetParameters(); | ||
PyObject signatureClass = Runtime.InspectModule.GetAttr("Signature"); | ||
var primaryReturn = primary.ReturnParameter; | ||
using var parameters = new PyList(); | ||
using var parameterClass = primaryParameters.Length > 0 ? Runtime.InspectModule.GetAttr("Parameter") : null; | ||
using var positionalOrKeyword = parameterClass?.GetAttr("POSITIONAL_OR_KEYWORD"); | ||
for (int i = 0; i < primaryParameters.Length; i++) | ||
{ | ||
var parameter = primaryParameters[i]; | ||
var alternatives = infos.Select(info => | ||
{ | ||
ParameterInfo[] altParamters = info.GetParameters(); | ||
return i < altParamters.Length ? altParamters[i] : null; | ||
}).Where(p => p != null); | ||
using var defaultValue = alternatives | ||
.Select(alternative => alternative.DefaultValue != DBNull.Value ? alternative.DefaultValue.ToPython() : null) | ||
.FirstOrDefault(v => v != null) ?? parameterClass.GetAttr("empty"); | ||
if (alternatives.Any(alternative => alternative.Name != parameter.Name)) | ||
{ | ||
return signatureClass.Invoke(); | ||
} | ||
using var args = new PyTuple(new[] { parameter.Name.ToPython(), positionalOrKeyword }); | ||
using var kw = new PyDict(); | ||
if (defaultValue is not null) | ||
{ | ||
kw["default"] = defaultValue; | ||
} | ||
using var parameterInfo = parameterClass.Invoke(args: args, kw: kw); | ||
parameters.Append(parameterInfo); | ||
} | ||
// TODO: add return annotation | ||
return signatureClass.Invoke(parameters); | ||
filmor marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
} | ||
struct TypeSpecificityComparer : IComparer<Type> | ||
{ | ||
public int Compare(Type a, Type b) | ||
{ | ||
if (a == b) return 0; | ||
if (a.IsSubclassOf(b)) return 1; | ||
if (b.IsSubclassOf(a)) return -1; | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
/// <summary> | ||
/// MethodBinding __getattribute__ implementation. | ||
@@ -91,6 +153,15 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key) | ||
case "Overloads": | ||
var om = new OverloadMapper(self.m, self.target); | ||
return om.pyHandle; | ||
case "__signature__" when Runtime.InspectModule is not null: | ||
var sig = self.Signature; | ||
if (sig is null) | ||
{ | ||
return Runtime.PyObject_GenericGetAttr(ob, key); | ||
} | ||
return sig.NewReferenceOrNull().DangerousMoveToPointerOrNull(); | ||
case "__name__": | ||
return self.m.GetName().DangerousMoveToPointerOrNull(); | ||
default: | ||
return Runtime.PyObject_GenericGetAttr(ob, key); | ||
} | ||
12 changes: 11 additions & 1 deletionsrc/runtime/methodobject.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
3 changes: 3 additions & 0 deletionssrc/runtime/runtime.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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.