🌐 AI搜索 & 代理 主页
Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Line endings normalization
fd7c7e1cbd8e1d7bdb2ec2423c3cf9f95a3abed1
70 changes: 35 additions & 35 deletions doc/make.bat
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
110 changes: 55 additions & 55 deletions src/runtime/Codecs/IterableDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
using System;
using System.Collections.Generic;
namespace Python.Runtime.Codecs
{
public class IterableDecoder : IPyObjectDecoder
{
internal static bool IsIterable(Type targetType)
{
//if it is a plain IEnumerable, we can decode it using sequence protocol.
if (targetType == typeof(System.Collections.IEnumerable))
return true;
if (!targetType.IsGenericType)
return false;
return targetType.GetGenericTypeDefinition() == typeof(IEnumerable<>);
}
internal static bool IsIterable(PyType objectType)
{
return objectType.HasAttr("__iter__");
}
public bool CanDecode(PyType objectType, Type targetType)
{
return IsIterable(objectType) && IsIterable(targetType);
}
public bool TryDecode<T>(PyObject pyObj, out T value)
{
//first see if T is a plan IEnumerable
if (typeof(T) == typeof(System.Collections.IEnumerable))
{
object enumerable = new CollectionWrappers.IterableWrapper<object>(pyObj);
value = (T)enumerable;
return true;
}
var elementType = typeof(T).GetGenericArguments()[0];
var collectionType = typeof(CollectionWrappers.IterableWrapper<>).MakeGenericType(elementType);
var instance = Activator.CreateInstance(collectionType, new[] { pyObj });
value = (T)instance;
return true;
}
public static IterableDecoder Instance { get; } = new IterableDecoder();
public static void Register()
{
PyObjectConversions.RegisterDecoder(Instance);
}
}
}
using System;
using System.Collections.Generic;

namespace Python.Runtime.Codecs
{
public class IterableDecoder : IPyObjectDecoder
{
internal static bool IsIterable(Type targetType)
{
//if it is a plain IEnumerable, we can decode it using sequence protocol.
if (targetType == typeof(System.Collections.IEnumerable))
return true;

if (!targetType.IsGenericType)
return false;

return targetType.GetGenericTypeDefinition() == typeof(IEnumerable<>);
}

internal static bool IsIterable(PyType objectType)
{
return objectType.HasAttr("__iter__");
}

public bool CanDecode(PyType objectType, Type targetType)
{
return IsIterable(objectType) && IsIterable(targetType);
}

public bool TryDecode<T>(PyObject pyObj, out T value)
{
//first see if T is a plan IEnumerable
if (typeof(T) == typeof(System.Collections.IEnumerable))
{
object enumerable = new CollectionWrappers.IterableWrapper<object>(pyObj);
value = (T)enumerable;
return true;
}

var elementType = typeof(T).GetGenericArguments()[0];
var collectionType = typeof(CollectionWrappers.IterableWrapper<>).MakeGenericType(elementType);

var instance = Activator.CreateInstance(collectionType, new[] { pyObj });
value = (T)instance;
return true;
}

public static IterableDecoder Instance { get; } = new IterableDecoder();

public static void Register()
{
PyObjectConversions.RegisterDecoder(Instance);
}
}
}
100 changes: 50 additions & 50 deletions src/runtime/Codecs/ListDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
using System;
using System.Collections.Generic;
namespace Python.Runtime.Codecs
{
public class ListDecoder : IPyObjectDecoder
{
private static bool IsList(Type targetType)
{
if (!targetType.IsGenericType)
return false;
return targetType.GetGenericTypeDefinition() == typeof(IList<>);
}
private static bool IsList(PyType objectType)
{
//TODO accept any python object that implements the sequence and list protocols
//must implement sequence protocol to fully implement list protocol
//if (!SequenceDecoder.IsSequence(objectType)) return false;
//returns wheter the type is a list.
return PythonReferenceComparer.Instance.Equals(objectType, Runtime.PyListType);
}
public bool CanDecode(PyType objectType, Type targetType)
{
return IsList(objectType) && IsList(targetType);
}
public bool TryDecode<T>(PyObject pyObj, out T value)
{
if (pyObj == null) throw new ArgumentNullException(nameof(pyObj));
var elementType = typeof(T).GetGenericArguments()[0];
Type collectionType = typeof(CollectionWrappers.ListWrapper<>).MakeGenericType(elementType);
var instance = Activator.CreateInstance(collectionType, new[] { pyObj });
value = (T)instance;
return true;
}
public static ListDecoder Instance { get; } = new ListDecoder();
public static void Register()
{
PyObjectConversions.RegisterDecoder(Instance);
}
}
}
using System;
using System.Collections.Generic;

namespace Python.Runtime.Codecs
{
public class ListDecoder : IPyObjectDecoder
{
private static bool IsList(Type targetType)
{
if (!targetType.IsGenericType)
return false;

return targetType.GetGenericTypeDefinition() == typeof(IList<>);
}

private static bool IsList(PyType objectType)
{
//TODO accept any python object that implements the sequence and list protocols
//must implement sequence protocol to fully implement list protocol
//if (!SequenceDecoder.IsSequence(objectType)) return false;

//returns wheter the type is a list.
return PythonReferenceComparer.Instance.Equals(objectType, Runtime.PyListType);
}

public bool CanDecode(PyType objectType, Type targetType)
{
return IsList(objectType) && IsList(targetType);
}

public bool TryDecode<T>(PyObject pyObj, out T value)
{
if (pyObj == null) throw new ArgumentNullException(nameof(pyObj));

var elementType = typeof(T).GetGenericArguments()[0];
Type collectionType = typeof(CollectionWrappers.ListWrapper<>).MakeGenericType(elementType);

var instance = Activator.CreateInstance(collectionType, new[] { pyObj });
value = (T)instance;
return true;
}

public static ListDecoder Instance { get; } = new ListDecoder();

public static void Register()
{
PyObjectConversions.RegisterDecoder(Instance);
}
}
}
96 changes: 48 additions & 48 deletions src/runtime/Codecs/SequenceDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
using System;
using System.Collections.Generic;
namespace Python.Runtime.Codecs
{
public class SequenceDecoder : IPyObjectDecoder
{
internal static bool IsSequence(Type targetType)
{
if (!targetType.IsGenericType)
return false;
return targetType.GetGenericTypeDefinition() == typeof(ICollection<>);
}
internal static bool IsSequence(PyType objectType)
{
//must implement iterable protocol to fully implement sequence protocol
if (!IterableDecoder.IsIterable(objectType)) return false;
using System;
using System.Collections.Generic;

namespace Python.Runtime.Codecs
{
public class SequenceDecoder : IPyObjectDecoder
{
internal static bool IsSequence(Type targetType)
{
if (!targetType.IsGenericType)
return false;

return targetType.GetGenericTypeDefinition() == typeof(ICollection<>);
}

internal static bool IsSequence(PyType objectType)
{
//must implement iterable protocol to fully implement sequence protocol
if (!IterableDecoder.IsIterable(objectType)) return false;

//returns wheter it implements the sequence protocol
//according to python doc this needs to exclude dict subclasses
//but I don't know how to look for that given the objectType
//rather than the instance.
return objectType.HasAttr("__getitem__");
}
public bool CanDecode(PyType objectType, Type targetType)
{
return IsSequence(objectType) && IsSequence(targetType);
}
public bool TryDecode<T>(PyObject pyObj, out T value)
{
if (pyObj == null) throw new ArgumentNullException(nameof(pyObj));
var elementType = typeof(T).GetGenericArguments()[0];
Type collectionType = typeof(CollectionWrappers.SequenceWrapper<>).MakeGenericType(elementType);
var instance = Activator.CreateInstance(collectionType, new[] { pyObj });
value = (T)instance;
return true;
}
public static SequenceDecoder Instance { get; } = new SequenceDecoder();
public static void Register()
{
PyObjectConversions.RegisterDecoder(Instance);
}
}
}
return objectType.HasAttr("__getitem__");
}

public bool CanDecode(PyType objectType, Type targetType)
{
return IsSequence(objectType) && IsSequence(targetType);
}

public bool TryDecode<T>(PyObject pyObj, out T value)
{
if (pyObj == null) throw new ArgumentNullException(nameof(pyObj));

var elementType = typeof(T).GetGenericArguments()[0];
Type collectionType = typeof(CollectionWrappers.SequenceWrapper<>).MakeGenericType(elementType);

var instance = Activator.CreateInstance(collectionType, new[] { pyObj });
value = (T)instance;
return true;
}

public static SequenceDecoder Instance { get; } = new SequenceDecoder();

public static void Register()
{
PyObjectConversions.RegisterDecoder(Instance);
}
}
}
2 changes: 1 addition & 1 deletion src/runtime/Types/ManagedTypes.cd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Class Name="Python.Runtime.ClassBase" Collapsed="true">
<Position X="15" Y="1.75" Width="1.5" />
Expand Down
2 changes: 1 addition & 1 deletion tests/domain_tests/App.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
Expand Down
Loading
Loading