| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,22 @@ | |||
| 1 | + namespace Python.Runtime; | ||
| 2 | + | ||
| 3 | + using System; | ||
| 4 | + | ||
| 5 | + /// <summary> | ||
| 6 | + /// Defines <see cref="PyObject"/> conversion to CLR types (unmarshalling) | ||
| 7 | + /// </summary> | ||
| 8 | + public interface IPyObjectDecoder | ||
| 9 | + { | ||
| 10 | + /// <summary> | ||
| 11 | + /// Checks if this decoder can decode from <paramref name="objectType"/> to <paramref name="targetType"/> | ||
| 12 | + /// </summary> | ||
| 13 | + bool CanDecode(PyType objectType, Type targetType); | ||
| 14 | + /// <summary> | ||
| 15 | + /// Attempts do decode <paramref name="pyObj"/> into a variable of specified type | ||
| 16 | + /// </summary> | ||
| 17 | + /// <typeparam name="T">CLR type to decode into</typeparam> | ||
| 18 | + /// <param name="pyObj">Object to decode</param> | ||
| 19 | + /// <param name="value">The variable, that will receive decoding result</param> | ||
| 20 | + /// <returns></returns> | ||
| 21 | + bool TryDecode<T>(PyObject pyObj, out T? value); | ||
| 22 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,18 @@ | |||
| 1 | + namespace Python.Runtime; | ||
| 2 | + | ||
| 3 | + using System; | ||
| 4 | + | ||
| 5 | + /// <summary> | ||
| 6 | + /// Defines conversion from CLR objects into Python objects (e.g. <see cref="PyObject"/>) (marshalling) | ||
| 7 | + /// </summary> | ||
| 8 | + public interface IPyObjectEncoder | ||
| 9 | + { | ||
| 10 | + /// <summary> | ||
| 11 | + /// Checks if encoder can encode CLR objects of specified type | ||
| 12 | + /// </summary> | ||
| 13 | + bool CanEncode(Type type); | ||
| 14 | + /// <summary> | ||
| 15 | + /// Attempts to encode CLR object <paramref name="value"/> into Python object | ||
| 16 | + /// </summary> | ||
| 17 | + PyObject? TryEncode(object value); | ||
| 18 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,41 +6,8 @@ namespace Python.Runtime | |||
| 6 | 6 | using System.Diagnostics; | |
| 7 | 7 | using System.Linq; | |
| 8 | 8 | using System.Reflection; | |
| 9 | - using Python.Runtime.Codecs; | ||
| 10 | - | ||
| 11 | - /// <summary> | ||
| 12 | - /// Defines <see cref="PyObject"/> conversion to CLR types (unmarshalling) | ||
| 13 | - /// </summary> | ||
| 14 | - public interface IPyObjectDecoder | ||
| 15 | - { | ||
| 16 | - /// <summary> | ||
| 17 | - /// Checks if this decoder can decode from <paramref name="objectType"/> to <paramref name="targetType"/> | ||
| 18 | - /// </summary> | ||
| 19 | - bool CanDecode(PyType objectType, Type targetType); | ||
| 20 | - /// <summary> | ||
| 21 | - /// Attempts do decode <paramref name="pyObj"/> into a variable of specified type | ||
| 22 | - /// </summary> | ||
| 23 | - /// <typeparam name="T">CLR type to decode into</typeparam> | ||
| 24 | - /// <param name="pyObj">Object to decode</param> | ||
| 25 | - /// <param name="value">The variable, that will receive decoding result</param> | ||
| 26 | - /// <returns></returns> | ||
| 27 | - bool TryDecode<T>(PyObject pyObj, out T? value); | ||
| 28 | - } | ||
| 29 | 9 | ||
| 30 | - /// <summary> | ||
| 31 | - /// Defines conversion from CLR objects into Python objects (e.g. <see cref="PyObject"/>) (marshalling) | ||
| 32 | - /// </summary> | ||
| 33 | - public interface IPyObjectEncoder | ||
| 34 | - { | ||
| 35 | - /// <summary> | ||
| 36 | - /// Checks if encoder can encode CLR objects of specified type | ||
| 37 | - /// </summary> | ||
| 38 | - bool CanEncode(Type type); | ||
| 39 | - /// <summary> | ||
| 40 | - /// Attempts to encode CLR object <paramref name="value"/> into Python object | ||
| 41 | - /// </summary> | ||
| 42 | - PyObject? TryEncode(object value); | ||
| 43 | - } | ||
| 10 | + using Python.Runtime.Codecs; | ||
| 44 | 11 | ||
| 45 | 12 | /// <summary> | |
| 46 | 13 | /// This class allows to register additional marshalling codecs. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,87 +5,6 @@ | |||
| 5 | 5 | ||
| 6 | 6 | namespace Python.Runtime | |
| 7 | 7 | { | |
| 8 | - /// <summary> | ||
| 9 | - /// Base class for Python types that reflect managed exceptions based on | ||
| 10 | - /// System.Exception | ||
| 11 | - /// </summary> | ||
| 12 | - /// <remarks> | ||
| 13 | - /// The Python wrapper for managed exceptions LIES about its inheritance | ||
| 14 | - /// tree. Although the real System.Exception is a subclass of | ||
| 15 | - /// System.Object the Python type for System.Exception does NOT claim that | ||
| 16 | - /// it subclasses System.Object. Instead TypeManager.CreateType() uses | ||
| 17 | - /// Python's exception.Exception class as base class for System.Exception. | ||
| 18 | - /// </remarks> | ||
| 19 | - [Serializable] | ||
| 20 | - internal class ExceptionClassObject : ClassObject | ||
| 21 | - { | ||
| 22 | - internal ExceptionClassObject(Type tp) : base(tp) | ||
| 23 | - { | ||
| 24 | - } | ||
| 25 | - | ||
| 26 | - internal static Exception? ToException(BorrowedReference ob) | ||
| 27 | - { | ||
| 28 | - var co = GetManagedObject(ob) as CLRObject; | ||
| 29 | - return co?.inst as Exception; | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - /// <summary> | ||
| 33 | - /// Exception __repr__ implementation | ||
| 34 | - /// </summary> | ||
| 35 | - public new static NewReference tp_repr(BorrowedReference ob) | ||
| 36 | - { | ||
| 37 | - Exception? e = ToException(ob); | ||
| 38 | - if (e == null) | ||
| 39 | - { | ||
| 40 | - return Exceptions.RaiseTypeError("invalid object"); | ||
| 41 | - } | ||
| 42 | - string name = e.GetType().Name; | ||
| 43 | - string message; | ||
| 44 | - if (e.Message != String.Empty) | ||
| 45 | - { | ||
| 46 | - message = String.Format("{0}('{1}')", name, e.Message); | ||
| 47 | - } | ||
| 48 | - else | ||
| 49 | - { | ||
| 50 | - message = String.Format("{0}()", name); | ||
| 51 | - } | ||
| 52 | - return Runtime.PyString_FromString(message); | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | - /// <summary> | ||
| 56 | - /// Exception __str__ implementation | ||
| 57 | - /// </summary> | ||
| 58 | - public new static NewReference tp_str(BorrowedReference ob) | ||
| 59 | - { | ||
| 60 | - Exception? e = ToException(ob); | ||
| 61 | - if (e == null) | ||
| 62 | - { | ||
| 63 | - return Exceptions.RaiseTypeError("invalid object"); | ||
| 64 | - } | ||
| 65 | - | ||
| 66 | - string message = e.ToString(); | ||
| 67 | - string fullTypeName = e.GetType().FullName; | ||
| 68 | - string prefix = fullTypeName + ": "; | ||
| 69 | - if (message.StartsWith(prefix)) | ||
| 70 | - { | ||
| 71 | - message = message.Substring(prefix.Length); | ||
| 72 | - } | ||
| 73 | - else if (message.StartsWith(fullTypeName)) | ||
| 74 | - { | ||
| 75 | - message = message.Substring(fullTypeName.Length); | ||
| 76 | - } | ||
| 77 | - return Runtime.PyString_FromString(message); | ||
| 78 | - } | ||
| 79 | - | ||
| 80 | - public override bool Init(BorrowedReference obj, BorrowedReference args, BorrowedReference kw) | ||
| 81 | - { | ||
| 82 | - if (!base.Init(obj, args, kw)) return false; | ||
| 83 | - | ||
| 84 | - var e = (CLRObject)GetManagedObject(obj)!; | ||
| 85 | - | ||
| 86 | - return Exceptions.SetArgsAndCause(obj, (Exception)e.inst); | ||
| 87 | - } | ||
| 88 | - } | ||
| 89 | 8 | ||
| 90 | 9 | /// <summary> | |
| 91 | 10 | /// Encapsulates the Python exception APIs. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments