-
Notifications
You must be signed in to change notification settings - Fork 762
Add more domain reload tests #1269
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
Closed
BadSingleton
wants to merge
7
commits into
pythonnet:master
from
Unity-Technologies:more_domain_reload_tests
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
efb5be6
(WIP) Add a test framework for domain reload test cases
BadSingleton 1f4205c
Add a second step that makes more sense
BadSingleton 7d6e6e9
Bug 1250: fix for missing FieldInfo
9591cec
Bug 1250: support for easier serialization
005a476
(wip)
BadSingleton a5cf8a4
(wip)
BadSingleton 4edc049
Settle on changing the class' name
BadSingleton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Bug 1250: support for easier serialization
Adds MaybeSerialize<T> which handles the reload nicely. Shows how to use it in FieldObject; it's very minimal changes to FieldObject to add this support. This is simpler than the proof of concept code.
- Loading branch information
commit 9591cec7e6b4cdfa52ed7c35b8ce7687acecf38c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using System.IO; | ||
|
||
namespace Python.Runtime | ||
{ | ||
/// <summary> | ||
/// A MaybeSerialize<T> delays errors from serialization and | ||
/// deserialization until the item is used. | ||
/// | ||
/// Python for .NET uses this in the C# reloading architecture. | ||
/// If e.g. a class member was renamed when reloading, references to the | ||
/// old field will be invalid, but the rest of the system will still work. | ||
/// Code that tries to use the old field will receive an exception. | ||
/// | ||
/// Assumption: the item being wrapped by MaybeSerialize will never be null. | ||
/// </summary> | ||
[Serializable] | ||
internal struct MaybeSerialize<T> : ISerializable where T : class | ||
{ | ||
/// <summary> | ||
/// The item being wrapped. | ||
/// | ||
/// If this is null, that means we failed to serialize or deserialize it. | ||
/// </summary> | ||
private T m_item; | ||
|
||
/// <summary> | ||
/// A string useful for debugging the error. | ||
/// | ||
/// This is null if m_item deserialized properly. | ||
/// Otherwise, it will be derived off of m_item.ToString() when we | ||
/// serialized. | ||
/// </summary> | ||
private string m_name; | ||
|
||
/// <summary> | ||
/// Store an item in such a way that it can be deserialized. | ||
/// | ||
/// It must not be null. | ||
/// </summary> | ||
public MaybeSerialize(T item) | ||
{ | ||
if (item == null) | ||
{ | ||
throw new System.ArgumentNullException("Trying to store a null"); | ||
} | ||
m_item = item; | ||
m_name = null; | ||
} | ||
|
||
/// <summary> | ||
/// Get the underlying deserialized value, or throw an exception | ||
/// if deserialiation failed. | ||
/// </summary> | ||
public T Value | ||
{ | ||
get | ||
{ | ||
if (m_item == null) | ||
{ | ||
throw new SerializationException($"The .NET object underlying {m_name} no longer exists"); | ||
} | ||
return m_item; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get a printable name. | ||
/// </summary> | ||
public string ToString() | ||
{ | ||
if (m_item == null) | ||
{ | ||
return $"(missing {m_name})"; | ||
} | ||
else | ||
{ | ||
return m_item.ToString(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Implements ISerializable | ||
/// </summary> | ||
public void GetObjectData(SerializationInfo info, StreamingContext context) | ||
{ | ||
if (m_item == null) | ||
{ | ||
// Save the name; this failed to reload in a previous | ||
// generation but we still need to remember what it was. | ||
info.AddValue("n", m_name); | ||
} | ||
else | ||
{ | ||
// Try to save the item. If it fails, too bad. | ||
try | ||
{ | ||
info.AddValue("i", m_item); | ||
} | ||
catch(SerializationException _) | ||
{ | ||
} | ||
|
||
// Also save the name in case the item doesn't deserialize | ||
info.AddValue("n", m_item.ToString()); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Implements ISerializable | ||
/// </summary> | ||
private MaybeSerialize(SerializationInfo info, StreamingContext context) | ||
{ | ||
try | ||
{ | ||
// Try to deserialize the item. It might fail, or it might | ||
// have already failed so there just isn't an "i" to find. | ||
m_item = (T)info.GetValue("i", typeof(T)); | ||
m_name = null; | ||
} | ||
catch (SerializationException _) | ||
{ | ||
// Getting the item failed, so get the name. | ||
m_item = null; | ||
m_name = info.GetString("n"); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the scenario when this could happen?