Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pythonnet
=========

This fork of http://sourceforge.net/projects/pythonnet/ allows easy calling of python functions from C#.

+ All calls to python should be inside a "using (Py.GIL()) {/* Your code here */}" block.
+ Import python modules using dynamic mod = Py.Import("mod"), then you can call functions as normal, eg mod.func(args).
+ Use mod.func(args, Py.kw("keywordargname", keywordargvalue)) to apply keyword arguments.
+ All python objects should be declared as 'dynamic' type.
+ Mathematical operations involving python and literal/managed types must have the python object first, eg np.pi*2 works, 2*np.pi doesn't

EG:
```csharp
static void Main(string[] args)
{
using (Py.GIL()) {
dynamic np = Py.Import("numpy");
dynamic sin = np.sin;
Console.WriteLine(np.cos(np.pi*2));
Console.WriteLine(sin(5));
double c = np.cos(5) + sin(5);
Console.WriteLine(c);
dynamic a = np.array(new List<float> { 1, 2, 3 });
dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32));
Console.WriteLine(a.dtype);
Console.WriteLine(b.dtype);
Console.WriteLine(a * b);
Console.ReadKey();
}
}
```
outputs:
```
1.0
-0.958924274663
-0.6752620892
float64
int32
[ 6. 10. 12.]
```
34 changes: 18 additions & 16 deletions pythonnet/src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>.\bin\Debug\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>.\bin\Release\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;PYTHON27, UCS2</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;PYTHON27, UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'EmbeddingTest|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\EmbeddingTest\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
Expand All @@ -37,7 +37,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'UnitTests|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\UnitTests\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
Expand All @@ -52,25 +52,26 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >PYTHON27, UCS4</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">PYTHON27, UCS4</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
<DefineConstants>PYTHON27,UCS2</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'EmbeddingTest|x86'">
<DebugSymbols>true</DebugSymbols>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
Expand All @@ -82,7 +83,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'UnitTests|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\UnitTests\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
Expand All @@ -91,7 +92,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugMono_x86|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\DebugMono_x86\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
Expand All @@ -103,7 +104,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugMono_x86|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\DebugMono_x86\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
Expand All @@ -112,7 +113,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
Expand All @@ -121,7 +122,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >PYTHON32, UCS2</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">PYTHON32, UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
Expand All @@ -132,7 +133,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'EmbeddingTest|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\EmbeddingTest\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
Expand All @@ -144,7 +145,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'UnitTests|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\UnitTests\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
Expand All @@ -156,13 +157,14 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugMono_x86|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\DebugMono_x86\</OutputPath>
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
<DefineConstants Condition=" '$(DefineConstants)'==''">TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -244,4 +246,4 @@ copy "$(TargetDir)clr.pyd" "$(SolutionDir)"
<PreBuildEvent>del "$(TargetDir)clr.pyd"</PreBuildEvent>
</PropertyGroup>
<Import Project="../../packages/UnmanagedExports.1.2.3-Beta/tools/RGiesecke.DllExport.targets" />
</Project>
</Project>
22 changes: 18 additions & 4 deletions pythonnet/src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Runtime.InteropServices;
using System.Globalization;
using System.Security;
using System.Collections;

namespace Python.Runtime {

Expand Down Expand Up @@ -91,6 +92,16 @@ internal static IntPtr ToPython(Object value, Type type) {
return result;
}

if (value is IEnumerable)
{
var resultlist = new PyList();
foreach (object o in (IEnumerable)value)
{
resultlist.Append(new PyObject(ToPython(o, o.GetType())));
}
return resultlist.Handle;
}

// hmm - from Python, we almost never care what the declared
// type is. we'd rather have the object bound to the actual
// implementing class.
Expand Down Expand Up @@ -738,10 +749,13 @@ static bool ToEnum(IntPtr value, Type obType, out Object result,
return false;

}



}


public static class ConverterExtension
{
public static PyObject ToPython(this object o)
{
return new PyObject(Converter.ToPython(o, o.GetType()));
}
}
}
Loading