Skip to content

Move code to subdirectories and rename or split up #1665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2022
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
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions src/runtime/Codecs/IPyObjectDecoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Python.Runtime;

using System;

/// <summary>
/// Defines <see cref="PyObject"/> conversion to CLR types (unmarshalling)
/// </summary>
public interface IPyObjectDecoder
{
/// <summary>
/// Checks if this decoder can decode from <paramref name="objectType"/> to <paramref name="targetType"/>
/// </summary>
bool CanDecode(PyType objectType, Type targetType);
/// <summary>
/// Attempts do decode <paramref name="pyObj"/> into a variable of specified type
/// </summary>
/// <typeparam name="T">CLR type to decode into</typeparam>
/// <param name="pyObj">Object to decode</param>
/// <param name="value">The variable, that will receive decoding result</param>
/// <returns></returns>
bool TryDecode<T>(PyObject pyObj, out T? value);
}
18 changes: 18 additions & 0 deletions src/runtime/Codecs/IPyObjectEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Python.Runtime;

using System;

/// <summary>
/// Defines conversion from CLR objects into Python objects (e.g. <see cref="PyObject"/>) (marshalling)
/// </summary>
public interface IPyObjectEncoder
{
/// <summary>
/// Checks if encoder can encode CLR objects of specified type
/// </summary>
bool CanEncode(Type type);
/// <summary>
/// Attempts to encode CLR object <paramref name="value"/> into Python object
/// </summary>
PyObject? TryEncode(object value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,8 @@ namespace Python.Runtime
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Python.Runtime.Codecs;

/// <summary>
/// Defines <see cref="PyObject"/> conversion to CLR types (unmarshalling)
/// </summary>
public interface IPyObjectDecoder
{
/// <summary>
/// Checks if this decoder can decode from <paramref name="objectType"/> to <paramref name="targetType"/>
/// </summary>
bool CanDecode(PyType objectType, Type targetType);
/// <summary>
/// Attempts do decode <paramref name="pyObj"/> into a variable of specified type
/// </summary>
/// <typeparam name="T">CLR type to decode into</typeparam>
/// <param name="pyObj">Object to decode</param>
/// <param name="value">The variable, that will receive decoding result</param>
/// <returns></returns>
bool TryDecode<T>(PyObject pyObj, out T? value);
}

/// <summary>
/// Defines conversion from CLR objects into Python objects (e.g. <see cref="PyObject"/>) (marshalling)
/// </summary>
public interface IPyObjectEncoder
{
/// <summary>
/// Checks if encoder can encode CLR objects of specified type
/// </summary>
bool CanEncode(Type type);
/// <summary>
/// Attempts to encode CLR object <paramref name="value"/> into Python object
/// </summary>
PyObject? TryEncode(object value);
}
using Python.Runtime.Codecs;

/// <summary>
/// This class allows to register additional marshalling codecs.
Expand Down
File renamed without changes.
File renamed without changes.
81 changes: 0 additions & 81 deletions src/runtime/exceptions.cs → src/runtime/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,6 @@

namespace Python.Runtime
{
/// <summary>
/// Base class for Python types that reflect managed exceptions based on
/// System.Exception
/// </summary>
/// <remarks>
/// The Python wrapper for managed exceptions LIES about its inheritance
/// tree. Although the real System.Exception is a subclass of
/// System.Object the Python type for System.Exception does NOT claim that
/// it subclasses System.Object. Instead TypeManager.CreateType() uses
/// Python's exception.Exception class as base class for System.Exception.
/// </remarks>
[Serializable]
internal class ExceptionClassObject : ClassObject
{
internal ExceptionClassObject(Type tp) : base(tp)
{
}

internal static Exception? ToException(BorrowedReference ob)
{
var co = GetManagedObject(ob) as CLRObject;
return co?.inst as Exception;
}

/// <summary>
/// Exception __repr__ implementation
/// </summary>
public new static NewReference tp_repr(BorrowedReference ob)
{
Exception? e = ToException(ob);
if (e == null)
{
return Exceptions.RaiseTypeError("invalid object");
}
string name = e.GetType().Name;
string message;
if (e.Message != String.Empty)
{
message = String.Format("{0}('{1}')", name, e.Message);
}
else
{
message = String.Format("{0}()", name);
}
return Runtime.PyString_FromString(message);
}

/// <summary>
/// Exception __str__ implementation
/// </summary>
public new static NewReference tp_str(BorrowedReference ob)
{
Exception? e = ToException(ob);
if (e == null)
{
return Exceptions.RaiseTypeError("invalid object");
}

string message = e.ToString();
string fullTypeName = e.GetType().FullName;
string prefix = fullTypeName + ": ";
if (message.StartsWith(prefix))
{
message = message.Substring(prefix.Length);
}
else if (message.StartsWith(fullTypeName))
{
message = message.Substring(fullTypeName.Length);
}
return Runtime.PyString_FromString(message);
}

public override bool Init(BorrowedReference obj, BorrowedReference args, BorrowedReference kw)
{
if (!base.Init(obj, args, kw)) return false;

var e = (CLRObject)GetManagedObject(obj)!;

return Exceptions.SetArgsAndCause(obj, (Exception)e.inst);
}
}

/// <summary>
/// Encapsulates the Python exception APIs.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="resources\clr.py">
<EmbeddedResource Include="Resources\clr.py">
<LogicalName>clr.py</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="resources\interop.py">
<EmbeddedResource Include="Resources\interop.py">
<LogicalName>interop.py</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Mixins\*.py" />
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading