Skip to content

Commit

Permalink
Fixing some formatting and tightening some of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpowell committed Aug 30, 2024
1 parent 41061c6 commit e320007
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 421 deletions.
200 changes: 101 additions & 99 deletions src/CSnakes.Runtime.Tests/Python/PyDictionaryTests.cs
Original file line number Diff line number Diff line change
@@ -1,100 +1,102 @@
using CSnakes.Runtime.Python;

namespace CSnakes.Runtime.Tests.Python;
public class PyDictionaryTests : RuntimeTestBase
{
[Fact]
public void TestIndex()
{
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.Equal("World?", pyDict["Hello"]);
Assert.Equal("Bar", pyDict["Foo"]);
// Try twice
Assert.Equal("World?", pyDict["Hello"]);
}

[Fact]
public void TestKeys()
{
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.Equal(2, pyDict.Count);
Assert.Contains("Hello", pyDict.Keys);
Assert.Contains("Foo", pyDict.Keys);
}

[Fact]
public void TestValues() {
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.Equal(2, pyDict.Count);
Assert.Contains("World?", pyDict.Values);
Assert.Contains("Bar", pyDict.Values);
}

[Fact]
public void TestCount()
{
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.Equal(2, pyDict.Count);
}

[Fact]
public void TestContainsKey() {
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.True(pyDict.ContainsKey("Hello"));
Assert.True(pyDict.ContainsKey("Foo"));
Assert.False(pyDict.ContainsKey("Bar"));
}

[Fact]
public void TestTryGetValue()
{
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.True(pyDict.TryGetValue("Hello", out var value));
Assert.Equal("World?", value);
Assert.True(pyDict.TryGetValue("Foo", out value));
Assert.Equal("Bar", value);
Assert.False(pyDict.TryGetValue("Bar", out value));
}
using CSnakes.Runtime.Python;

namespace CSnakes.Runtime.Tests.Python;
public class PyDictionaryTests : RuntimeTestBase
{
[Fact]
public void TestIndex()
{
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.Equal("World?", pyDict["Hello"]);
Assert.Equal("Bar", pyDict["Foo"]);
// Try twice
Assert.Equal("World?", pyDict["Hello"]);
}

[Fact]
public void TestKeys()
{
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.Equal(2, pyDict.Count);
Assert.Contains("Hello", pyDict.Keys);
Assert.Contains("Foo", pyDict.Keys);
}

[Fact]
public void TestValues()
{
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.Equal(2, pyDict.Count);
Assert.Contains("World?", pyDict.Values);
Assert.Contains("Bar", pyDict.Values);
}

[Fact]
public void TestCount()
{
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.Equal(2, pyDict.Count);
}

[Fact]
public void TestContainsKey()
{
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.True(pyDict.ContainsKey("Hello"));
Assert.True(pyDict.ContainsKey("Foo"));
Assert.False(pyDict.ContainsKey("Bar"));
}

[Fact]
public void TestTryGetValue()
{
var testDict = new Dictionary<string, string>()
{
["Hello"] = "World?",
["Foo"] = "Bar"
};
var pyObject = PyObject.From(testDict);
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>();
Assert.NotNull(pyDict);
Assert.True(pyDict.TryGetValue("Hello", out var value));
Assert.Equal("World?", value);
Assert.True(pyDict.TryGetValue("Foo", out value));
Assert.Equal("Bar", value);
Assert.False(pyDict.TryGetValue("Bar", out _));
}
}
1 change: 0 additions & 1 deletion src/CSnakes.Runtime.Tests/Python/PyObjectTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CSnakes.Runtime.Python;
using System.Collections;

namespace CSnakes.Runtime.Tests.Python;
public class PyObjectTests : RuntimeTestBase
Expand Down
37 changes: 18 additions & 19 deletions src/CSnakes.Runtime/CPython/Iter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using CSnakes.Runtime.Python;
using System.Runtime.InteropServices;


namespace CSnakes.Runtime.CPython;
internal unsafe partial class CPythonAPI
{
/// <summary>
/// Return the next value from the iterator o.
/// The object must be an iterator according to PyIter_Check()
/// (it is up to the caller to check this).
/// If there are no remaining values, returns NULL with no exception set.
/// If an error occurs while retrieving the item, returns NULL and passes along the exception.
/// </summary>
/// <param name="iter"></param>
/// <returns>New refernce to the next item</returns>
[LibraryImport(PythonLibraryName)]
internal static partial nint PyIter_Next(PyObject iter);
}
using CSnakes.Runtime.Python;
using System.Runtime.InteropServices;

namespace CSnakes.Runtime.CPython;
internal unsafe partial class CPythonAPI
{
/// <summary>
/// Return the next value from the iterator o.
/// The object must be an iterator according to PyIter_Check()
/// (it is up to the caller to check this).
/// If there are no remaining values, returns NULL with no exception set.
/// If an error occurs while retrieving the item, returns NULL and passes along the exception.
/// </summary>
/// <param name="iter"></param>
/// <returns>New refernce to the next item</returns>
[LibraryImport(PythonLibraryName)]
internal static partial nint PyIter_Next(PyObject iter);
}
3 changes: 1 addition & 2 deletions src/CSnakes.Runtime/Python/GeneratorIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ public bool MoveNext()

public void Reset() => throw new NotSupportedException();


public TYield Send(TSend value)
{
try
{
using PyObject sendValue = PyObject.From<TSend>(value) !;
using PyObject sendValue = PyObject.From(value);
using PyObject result = sendPyFunction.Call(sendValue);
current = result.As<TYield>();
return current;
Expand Down
Loading

0 comments on commit e320007

Please sign in to comment.