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
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
Incorrectly using a non-generic type with type parameters now produce…
…s a helpful Python error instead of throwing NullReferenceException
  • Loading branch information
tminka committed Dec 18, 2020
commit 0fe042c803840ad929b2f7733f09d8e2ee6e80e6
1 change: 1 addition & 0 deletions AUTHORS.md
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
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
35 changes: 35 additions & 0 deletions src/embed_tests/TestList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestList
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void MissingGenericTypeTest()
{
Assert.Throws<PythonException>(() =>
PythonEngine.Exec($@"
from System.Collections import IList
IList[bool]
"));
}
}
}
2 changes: 1 addition & 1 deletion src/runtime/classbase.cs
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
15 changes: 11 additions & 4 deletions src/runtime/genericutil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,27 @@ 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);
}

/// <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)
{
foreach (Type t in GenericsByName(ns, name))
var types = GenericsByName(ns, name);
if (types != null)
{
if (t.GetGenericArguments().Length == paramCount)
foreach (Type t in types)
{
return t;
if (t.GetGenericArguments().Length == paramCount)
{
return t;
}
}
}
return null;
Expand Down
0