8000 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
[go: up one dir, main page]

Skip to content

Incorrectly using a non-generic type with type parameters now produces a helpful Python error instead of throwing NullReferenceException #1326

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 4 commits into from
Dec 18, 2020
Merged
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
Prev Previous commit
Next Next commit
Inlined GenericUtil.GenericsByName into GenericByName.
Removed unused GenericUtil.GenericsForType.
Other code quality improvements.
  • Loading branch information
tminka committed Dec 18, 2020
commit 8e754b664a0461fe65a0563cf233ce2de59aeebc
98 changes: 36 additions & 62 deletions src/runtime/genericutil.cs
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 @@ -85,81 +77,63 @@ public static Type GenericForType(Type t, int paramCount)
/// <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 name, int paramCount)
{
var types = GenericsByName(ns, name);
if (types != null)
{
foreach (Type t in types)
{
if (t.GetGenericArguments().Length == paramCount)
{
return t;
}
}
}
return null;
}

public static List<Type> GenericsForType(Type t)
public static Type GenericByName(string ns, string basename, int paramCount)
{
return GenericsByName(t.Namespace, t.Name);
}

public static List<Type> GenericsByName(string ns, string basename)
{
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;
}
}
}
}
0