| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| using Python.Runtime; | ||
|
|
||
| using PyRuntime = Python.Runtime.Runtime; | ||
|
|
||
| namespace Python.EmbeddingTest | ||
| { | ||
| public class TestClass | ||
| { | ||
| public class MyClass | ||
| { | ||
| } | ||
|
|
||
| [OneTimeSetUp] | ||
| public void SetUp() | ||
| { | ||
| PythonEngine.Initialize(); | ||
| } | ||
|
|
||
| [OneTimeTearDown] | ||
| public void Dispose() | ||
| { | ||
| PythonEngine.Shutdown(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void WeakRefForClrObject() | ||
| { | ||
| var obj = new MyClass(); | ||
| using var scope = Py.CreateScope(); | ||
| scope.Set("clr_obj", obj); | ||
| scope.Exec(@" | ||
| import weakref | ||
| ref = weakref.ref(clr_obj) | ||
| "); | ||
| using PyObject pyobj = scope.Get("clr_obj"); | ||
| ValidateAttachedGCHandle(obj, pyobj.Handle); | ||
| } | ||
|
|
||
| [Test] | ||
| public void WeakRefForSubClass() | ||
| { | ||
| using (var scope = Py.CreateScope()) | ||
| { | ||
| scope.Exec(@" | ||
| from Python.EmbeddingTest import TestClass | ||
| import weakref | ||
|
|
||
| class Sub(TestClass.MyClass): | ||
| pass | ||
|
|
||
| obj = Sub() | ||
| ref = weakref.ref(obj) | ||
| "); | ||
| using (PyObject pyobj = scope.Get("obj")) | ||
| { | ||
| IntPtr op = pyobj.Handle; | ||
| IntPtr type = PyRuntime.PyObject_TYPE(op); | ||
| IntPtr clrHandle = Marshal.ReadIntPtr(op, ObjectOffset.magic(type)); | ||
| var clobj = (CLRObject)GCHandle.FromIntPtr(clrHandle).Target; | ||
| Assert.IsTrue(clobj.inst is MyClass); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void ValidateAttachedGCHandle(object obj, IntPtr op) | ||
| { | ||
| IntPtr type = PyRuntime.PyObject_TYPE(op); | ||
| IntPtr clrHandle = Marshal.ReadIntPtr(op, ObjectOffset.magic(type)); | ||
| var clobj = (CLRObject)GCHandle.FromIntPtr(clrHandle).Target; | ||
| Assert.True(ReferenceEquals(clobj.inst, obj)); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -85,6 +85,13 @@ internal static Exception ToException(IntPtr ob) | |
| } | ||
| return Runtime.PyUnicode_FromString(message); | ||
| } | ||
|
|
||
| public static int tp_init(IntPtr ob, IntPtr args, IntPtr kwds) | ||
| { | ||
| Exceptions.SetArgsAndCause(ob); | ||
|
Comment thread
Copy link
Copy Markdown
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityPlease, move the original comment here.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWhat comment?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityNIT: You seem to have moved this from CLRObject, and removed // Fix the BaseException args (and __cause__ in case of Python 3) // slot if wrapping a CLR exception
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThe comment on SetArgsAndCause won't be enough?
Sorry, something went wrong.
All reactions
|
||
| return 0; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /// <summary> | ||
| Expand Down Expand Up | @@ -180,16 +187,23 @@ internal static void SetArgsAndCause(IntPtr ob) | |
| args = Runtime.PyTuple_New(0); | ||
| } | ||
|
|
||
| Marshal.WriteIntPtr(ob, ExceptionOffset.args, args); | ||
|
|
||
| int baseOffset = OriginalObjectOffsets.Size; | ||
| Runtime.Py_SETREF(ob, baseOffset + ExceptionOffset.args, args); | ||
|
|
||
| if (e.InnerException != null) | ||
| { | ||
| // Note: For an AggregateException, InnerException is only the first of the InnerExceptions. | ||
| IntPtr cause = CLRObject.GetInstHandle(e.InnerException); | ||
| Marshal.WriteIntPtr(ob, ExceptionOffset.cause, cause); | ||
| IntPtr cause = GetExceptHandle(e.InnerException); | ||
| Runtime.Py_SETREF(ob, baseOffset + ExceptionOffset.cause, cause); | ||
| } | ||
| } | ||
|
|
||
| internal static IntPtr GetExceptHandle(Exception e) | ||
| { | ||
| IntPtr op = CLRObject.GetInstHandle(e); | ||
| SetArgsAndCause(op); | ||
| return op; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Shortcut for (pointer == NULL) -> throw PythonException | ||
| /// </summary> | ||
| Expand Down Expand Up | @@ -289,7 +303,7 @@ public static void SetError(Exception e) | |
| return; | ||
| } | ||
|
|
||
| IntPtr op = CLRObject.GetInstHandle(e); | ||
| IntPtr op = GetExceptHandle(e); | ||
| IntPtr etype = Runtime.PyObject_GetAttr(op, PyIdentifier.__class__); | ||
| Runtime.PyErr_SetObject(new BorrowedReference(etype), new BorrowedReference(op)); | ||
| Runtime.XDecref(etype); | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.