Visitar URL original
Ensure that the tests work even if BinaryFormatter is not available by filmor · Pull Request #2638 · pythonnet/pythonnet · GitHub
Skip to content
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
Prev Previous commit
Next Next commit
Remove shutdown from most tests, disable the rest for now
  • Loading branch information
filmor committed Dec 8, 2025
commit e7b51d770d6ea9705139a17d8b23a8f506239964
12 changes: 0 additions & 12 deletions src/embed_tests/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ namespace Python.EmbeddingTest;

public class Events
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

[Test]
public void UsingDoesNotLeak()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ public void Dispose()
}
}

[OneTimeSetUp]
public void OneTimeSetUp()
{
PythonEngine.Initialize();
}

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

/// <summary>
/// Eval a Python expression and obtain its return value.
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion src/embed_tests/StateSerialization/MethodSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void GenericRoundtrip()
}

[Test]
public void ConstrctorRoundtrip()
public void ConstructorRoundtrip()
{
var ctor = typeof(MethodTestHost).GetConstructor(new[] { typeof(int) });
var maybeConstructor = new MaybeMethodBase<MethodBase>(ctor);
Expand All @@ -33,6 +33,10 @@ static T SerializationRoundtrip<T>(T item)
{
using var buf = new MemoryStream();
var formatter = RuntimeData.CreateFormatter();
if (typeof(NoopFormatter).IsAssignableFrom(formatter.GetType()))
{
Assert.Inconclusive("NoopFormatter in use, cannot perform serialization test.");
}
formatter.Serialize(buf, item);
buf.Position = 0;
return (T)formatter.Deserialize(buf);
Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/TestFinalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void CollectBasicObject()
}

[Test]
[Ignore("Requires explicit shutdown")]
[Obsolete("GC tests are not guaranteed")]
public void CollectOnShutdown()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPyInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ namespace Python.EmbeddingTest
{
public class TestPyInt
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

[Test]
public void TestCtorInt()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPyIter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ namespace Python.EmbeddingTest
{
class TestPyIter
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

[Test]
public void KeepOldObjects()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPyList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ namespace Python.EmbeddingTest
{
public class TestPyList
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

[Test]
public void TestStringIsListType()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPyNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ namespace Python.EmbeddingTest
{
public class TestPyNumber
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

[Test]
public void IsNumberTypeTrue()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPyObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ namespace Python.EmbeddingTest
{
public class TestPyObject
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

[Test]
public void TestGetDynamicMemberNames()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPySequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ namespace Python.EmbeddingTest
{
public class TestPySequence
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

[Test]
public void TestIsSequenceTrue()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPyString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ namespace Python.EmbeddingTest
{
public class TestPyString
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

[Test]
public void TestStringCtor()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPyTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ namespace Python.EmbeddingTest
{
public class TestPyTuple
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

/// <summary>
/// Test IsTupleType without having to Initialize a tuple.
/// PyTuple constructor use IsTupleType. This decouples the tests.
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ namespace Python.EmbeddingTest
{
public class TestPyType
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

[Test]
public void CanCreateHeapType()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPyWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ namespace Python.EmbeddingTest
{
public class TestPyWith
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

/// <summary>
/// Test that exception is raised in context manager that ignores it.
/// </summary>
Expand Down
15 changes: 4 additions & 11 deletions src/embed_tests/TestPythonEngineProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class TestPythonEngineProperties
[Test]
public static void GetBuildinfoDoesntCrash()
{
PythonEngine.Initialize();
using (Py.GIL())
{
string s = PythonEngine.BuildInfo;
Expand All @@ -22,7 +21,6 @@ public static void GetBuildinfoDoesntCrash()
[Test]
public static void GetCompilerDoesntCrash()
{
PythonEngine.Initialize();
using (Py.GIL())
{
string s = PythonEngine.Compiler;
Expand All @@ -36,7 +34,6 @@ public static void GetCompilerDoesntCrash()
[Test]
public static void GetCopyrightDoesntCrash()
{
PythonEngine.Initialize();
using (Py.GIL())
{
string s = PythonEngine.Copyright;
Expand All @@ -49,7 +46,6 @@ public static void GetCopyrightDoesntCrash()
[Test]
public static void GetPlatformDoesntCrash()
{
PythonEngine.Initialize();
using (Py.GIL())
{
string s = PythonEngine.Platform;
Expand All @@ -62,7 +58,6 @@ public static void GetPlatformDoesntCrash()
[Test]
public static void GetVersionDoesntCrash()
{
PythonEngine.Initialize();
using (Py.GIL())
{
string s = PythonEngine.Version;
Expand All @@ -75,21 +70,17 @@ public static void GetVersionDoesntCrash()
[Test]
public static void GetPythonPathDefault()
{
PythonEngine.Initialize();
string s = PythonEngine.PythonPath;

StringAssert.Contains("python", s.ToLower());
PythonEngine.Shutdown();
}

[Test]
public static void GetProgramNameDefault()
{
PythonEngine.Initialize();
string s = PythonEngine.ProgramName;

Assert.NotNull(s);
PythonEngine.Shutdown();
}

/// <summary>
Expand All @@ -101,13 +92,12 @@ public static void GetPythonHomeDefault()
{
string envPythonHome = Environment.GetEnvironmentVariable("PYTHONHOME") ?? "";

PythonEngine.Initialize();
string enginePythonHome = PythonEngine.PythonHome;

Assert.AreEqual(envPythonHome, enginePythonHome);
PythonEngine.Shutdown();
}

[Ignore("Only works if we can shutdown and re-init the interpreter")]
[Test]
public void SetPythonHome()
{
Expand All @@ -130,6 +120,7 @@ public void SetPythonHome()
PythonEngine.PythonHome = pythonHomeBackup;
}

[Ignore("Only works if we can shutdown and re-init the interpreter")]
[Test]
public void SetPythonHomeTwice()
{
Expand Down Expand Up @@ -172,6 +163,7 @@ public void SetPythonHomeEmptyString()
PythonEngine.Shutdown();
}

[Ignore("Only works if we can shutdown and re-init the interpreter")]
[Test]
public void SetProgramName()
{
Expand All @@ -193,6 +185,7 @@ public void SetProgramName()
PythonEngine.ProgramName = programNameBackup;
}

[Ignore("Only works if we can shutdown and re-init the interpreter")]
[Test]
public void SetPythonPath()
{
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/TestPythonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ namespace Python.EmbeddingTest
{
public class TestPythonException
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

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

[Test]
public void TestMessage()
{
Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/TestRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Python.EmbeddingTest
{
[Ignore("Only works if we can shutdown and re-initialize the Python runtime")]
public class TestRuntime
{
[OneTimeSetUp]
Expand Down
3 changes: 2 additions & 1 deletion src/embed_tests/pyimport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public void SetUp()
[OneTimeTearDown]
public void Dispose()
{
// TODO Undo the above
using var _ = Py.GIL();
Py.Import("sys").GetAttr("path").InvokeMethod("remove", new PyString(TestPath));
}

/// <summary>
Expand Down
Loading