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

Incorrectly using a non-generic type with type parameters now produces a helpful Python error instead of throwing NullReferenceException by tminka · Pull Request #1326 · pythonnet/pythonnet · GitHub

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

Filter by extension

Filter by extension .cs  (2) .md  (2) .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
1 change: 1 addition & 0 deletions AUTHORS.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 @@ -60,6 +60,7 @@
- Sean Freitag ([@cowboygneox](https://github.com/cowboygneox))
- Serge Weinstock ([@sweinst](https://github.com/sweinst))
- Simon Mourier ([@smourier](https://github.com/smourier))
- Tom Minka ([@tminka](https://github.com/tminka))
- Viktoria Kovescses ([@vkovec](https://github.com/vkovec))
- Ville M. Vainio ([@vivainio](https://github.com/vivainio))
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
Expand Down
1 change: 1 addition & 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 @@ -42,6 +42,7 @@ details about the cause of the failure
- Made it possible to use `__len__` also on `ICollection<>` interface objects
- Made it possible to call `ToString`, `GetHashCode`, and `GetType` on inteface objects
- Fixed objects returned by enumerating `PyObject` being disposed too soon
- Incorrectly using a non-generic type with type parameters now produces a helpful Python error instead of throwing NullReferenceException

### Removed

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/classbase.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
Expand Up @@ -54,7 +54,7 @@ public virtual IntPtr type_subscript(IntPtr idx)
return c.pyHandle;
}

return Exceptions.RaiseTypeError("no type matches params");
return Exceptions.RaiseTypeError($"{type.Namespace}.{type.Name} does not accept {types.Length} generic parameters");
}

/// <summary>
Expand Down
99 changes: 40 additions & 59 deletions src/runtime/genericutil.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
Expand Up @@ -9,14 +9,13 @@ namespace Python.Runtime
/// This class is responsible for efficiently maintaining the bits
/// of information we need to support aliases with 'nice names'.
/// </summary>
internal class GenericUtil
internal static class GenericUtil
{
/// <summary>
/// Maps namespace -> generic base name -> list of generic type names
/// </summary>
private static Dictionary<string, Dictionary<string, List<string>>> mapping;

private GenericUtil()
{
}

public static void Reset()
{
mapping = new Dictionary<string, Dictionary<string, List<string>>>();
Expand All @@ -25,29 +24,23 @@ public static void Reset()
/// <summary>
/// Register a generic type that appears in a given namespace.
/// </summary>
/// <param name="t">A generic type definition (<c>t.IsGenericTypeDefinition</c> must be true)</param>
internal static void Register(Type t)
{
if (null == t.Namespace || null == t.Name)
{
return;
}

Dictionary<string, List<string>> nsmap = null;
mapping.TryGetValue(t.Namespace, out nsmap);
if (nsmap == null)
Dictionary<string, List<string>> nsmap;
if (!mapping.TryGetValue(t.Namespace, out nsmap))
{
nsmap = new Dictionary<string, List<string>>();
mapping[t.Namespace] = nsmap;
}
string basename = t.Name;
int tick = basename.IndexOf("`");
if (tick > -1)
{
basename = basename.Substring(0, tick);
}
List<string> gnames = null;
nsmap.TryGetValue(basename, out gnames);
if (gnames == null)
string basename = GetBasename(t.Name);
List<string> gnames;
if (!nsmap.TryGetValue(basename, out gnames))
{
gnames = new List<string>();
nsmap[basename] = gnames;
Expand All @@ -60,9 +53,8 @@ internal static void Register(Type t)
/// </summary>
public static List<string> GetGenericBaseNames(string ns)
{
Dictionary<string, List<string>> nsmap = null;
mapping.TryGetValue(ns, out nsmap);
if (nsmap == null)
Dictionary<string, List<string>> nsmap;
if (!mapping.TryGetValue(ns, out nsmap))
{
return null;
}
Expand All @@ -75,84 +67,73 @@ public static List<string> GetGenericBaseNames(string ns)
}

/// <summary>
/// xxx
/// Finds a generic type with the given number of generic parameters and the same name and namespace as <paramref name="t"/>.
/// </summary>
public static Type GenericForType(Type t, int paramCount)
{
return GenericByName(t.Namespace, t.Name, paramCount);
}

public static Type GenericByName(string ns, string name, int paramCount)
{
foreach (Type t in GenericsByName(ns, name))
{
if (t.GetGenericArguments().Length == paramCount)
{
return t;
}
}
return null;
}

public static List<Type> GenericsForType(Type t)
{
return GenericsByName(t.Namespace, t.Name);
}

public static List<Type> GenericsByName(string ns, string basename)
/// <summary>
/// Finds a generic type in the given namespace with the given name and number of generic parameters.
/// </summary>
public static Type GenericByName(string ns, string basename, int paramCount)
{
Dictionary<string, List<string>> nsmap = null;
mapping.TryGetValue(ns, out nsmap);
if (nsmap == null)
Dictionary<string, List<string>> nsmap;
if (!mapping.TryGetValue(ns, out nsmap))
{
return null;
}

int tick = basename.IndexOf("`");
if (tick > -1)
{
basename = basename.Substring(0, tick);
}

List<string> names = null;
nsmap.TryGetValue(basename, out names);
if (names == null)
List<string> names;
if (!nsmap.TryGetValue(GetBasename(basename), out names))
{
return null;
}

var result = new List<Type>();
foreach (string name in names)
{
string qname = ns + "." + name;
Type o = AssemblyManager.LookupTypes(qname).FirstOrDefault();
if (o != null)
if (o != null && o.GetGenericArguments().Length == paramCount)
{
result.Add(o);
return o;
}
}

return result;
return null;
}

/// <summary>
/// xxx
/// </summary>
public static string GenericNameForBaseName(string ns, string name)
{
Dictionary<string, List<string>> nsmap = null;
mapping.TryGetValue(ns, out nsmap);
if (nsmap == null)
Dictionary<string, List<string>> nsmap;
if (!mapping.TryGetValue(ns, out nsmap))
{
return null;
}
List<string> gnames = null;
List<string> gnames;
nsmap.TryGetValue(name, out gnames);
if (gnames?.Count > 0)
{
return gnames[0];
}
return null;
}

private static string GetBasename(string name)
{
int tick = name.IndexOf("`");
if (tick > -1)
{
return name.Substring(0, tick);
}
else
{
return name;
}
}
}
}
5 changes: 5 additions & 0 deletions src/tests/test_generic.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 @@ -745,3 +745,8 @@ def test_nested_generic_class():
"""Check nested generic classes."""
# TODO NotImplemented
pass

def test_missing_generic_type():
from System.Collections import IList
with pytest.raises(TypeError):
IList[bool]

Back | FazBrowse Home | New Git URL