Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Last line of defense, best effort serialization #15

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 484 commits into from
May 26, 2023

Conversation

BadSingleton
Copy link

@BadSingleton BadSingleton commented Jun 28, 2022

What does this implement/fix? Explain your changes.

This commit adds a "last line of defense, best effort serialization"
to serialize types not marked as Serializable. Such objects are
deserialized as derived classes with all methods and properties
overriden to throw a "Not Serialized" Exception.
Fields are not initialized and may be null.

Private classes and other non-inheritable instances are set to null on deserialization

Does this close any currently open issues?

...

Any other comments?

...

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

there are only a few errors left in runtime.cs, but plenty outside of it
Copy link

@benoithudson benoithudson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome.

Should we backport to the 3.0 alpha tag as well?

[System.Serializable]
public sealed class NotSerializedException: SerializationException
{
static string _message = "The underlying C# object has been deleted.";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be const?

Copy link
Author

@BadSingleton BadSingleton Jun 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even the right message? This is a twisted joke I made to trigger PTSD of all the poor souls who had to work with PyQT/PySide.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a perfect message.


private static void CreateMethod(TypeBuilder tb, MethodInfo minfo)
{
Console.WriteLine($"overimplementing method for: {minfo} {minfo.IsVirtual} {minfo.IsFinal} ");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably meant to remove this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes


MaybeType type = obj.GetType();

var hasAttr = (from attr in obj.GetType().CustomAttributes select attr.AttributeType == typeof(PyNet_NotSerializedAttribute)).Count() != 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe type.CustomAttributes.Any(attr => attr.AttributeType == typeof(...)) ?

I don't know the fancy way to write it, but you don't need to count it, you can stop at the first.

(And you already got obj.GetType())

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's better

if (hasAttr)
{
// Don't serialize a _NotSerialized. Serialize the base type, and deserialize as a _NotSerialized
type = type.Value.BaseType;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take it we know that a NotSerialized always derives from a serializable object?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it always derives from a non-serializable object. That's why we end up here. We don't serialize the object; only it's type so we can re-create it in the next domain.

object nameObj = null!;
try
{
nameObj = info.GetValue($"notSerialized_tp", typeof(object));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to interpolate this string, there's no fields inside

}
else
{
selector = this;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both branches do it so you could do it before the if/else

@BadSingleton
Copy link
Author

Should we backport to the 3.0 alpha tag as well?

Considering that RC.1 has been declared the other day, I'll have to take a look to see if we can use it instead of the alpha

lostmsu and others added 23 commits July 5, 2022 21:35
fixed unhandled Python errors during comparison attempts

fixes pythonnet#1848
Additionally, fixes a type hint and makes sure that the new default
behaviour is to use the environment variable if given.
- Only include src/runtime, no other .NET subproject
- In particular, tests are not contained anymore in the sdist
- Fix accidentally including obj and bin directories in the sdist
* Add unit tests for (U)IntPtr conversions
* Special-case construction of (U)IntPtr
* Check (U)IntPtr size explicitly
This commit adds a "last line of defense, best effort serialization"
to serialize types not marked as Serializable. Such objects are
deserialized as derived classes with all methods and properties
overriden to throw a "Not Serialized" Exception.
Fields are not initialized and may be null.

Sealed classes and implemented interface methods are still a problem to
be solved.
plus guard rails for private classes

and review fixes
don't try to derive from sealed types
Allows the project to be referenced in other .NET projects without
adjusting its project file (pythonnet#1853).
There are small typos in:
- pythonnet/__init__.py
- tests/test_import.py

Fixes:
- Should read `splitted` rather than `splited`.
- Should read `loaded` rather than `laoded`.

Signed-off-by: Tim Gates <[email protected]>
@BadSingleton BadSingleton force-pushed the last-ditch-serialization branch from 6a88bd5 to 7ffad42 Compare July 18, 2022 17:31
@BadSingleton BadSingleton changed the base branch from unprotected_master to unity-custom May 25, 2023 16:21
latest clr_loader version breaks the python tests otherwise
@BadSingleton BadSingleton merged commit 37c4426 into unity-custom May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants