Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Last active June 8, 2025 15:27
Show Gist options
  • Save atifaziz/823bce2f9197d1873128bdf60b60cfd6 to your computer and use it in GitHub Desktop.
Save atifaziz/823bce2f9197d1873128bdf60b60cfd6 to your computer and use it in GitHub Desktop.
from collections.abc import Buffer
def test(length: int = 10000000, fill: int = 42) -> Buffer:
return bytearray([42] * length)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<None Remove="*.py" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="*.py">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="CSnakes.Runtime" Version="1.0.35" />
</ItemGroup>
</Project>
using System;
using CSnakes.Runtime;
using CSnakes.Runtime.Locators;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
var builder = Host.CreateApplicationBuilder();
_ = builder.Services
.WithPython()
.FromRedistributable(RedistributablePythonVersion.Python3_12);
builder.Logging.SetMinimumLevel(LogLevel.Debug);
var app = builder.Build();
using var env = app.Services.GetRequiredService<IPythonEnvironment>();
using var test = env.BufferTest();
Span<byte> GetBufferSpan(byte fill)
{
using var buffer = test.Test(fill: fill);
return buffer.AsSpan<byte>();
}
const byte fill = 42;
var span = GetBufferSpan(fill);
if (span.IndexOfAnyExcept(fill) is var i and >= 0)
throw new($"Buffer contains unexpected value at index {i} (expected {fill}, but was {span[i]}).");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment