Skip to content

Commit 2860632

Browse files
committed
Test to test if the test passes
1 parent f9a3974 commit 2860632

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

src/runtime/BorrowedReference.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ readonly ref struct BorrowedReference
99
{
1010
readonly IntPtr pointer;
1111
public bool IsNull => this.pointer == IntPtr.Zero;
12+
public bool IsNone => this.pointer == Runtime.PyNone;
1213

1314
/// <summary>Gets a raw pointer to the Python object</summary>
1415
public IntPtr DangerousGetAddress()

src/runtime/assemblymanager.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal class AssemblyManager
3939
internal static List<string> pypath;
4040

4141
// Triggered when a new namespace is added to the namespaces dictionary
42-
public static event Action<string> namespaceAdded;
42+
// public static event Action<string> namespaceAdded;
4343

4444
private AssemblyManager()
4545
{
@@ -287,17 +287,17 @@ internal static void ScanAssembly(Assembly assembly)
287287
if (ns != null)
288288
{
289289
namespaces[ns].TryAdd(assembly, string.Empty);
290-
try
291-
{
292-
namespaceAdded?.Invoke(ns);
293-
}
294-
catch (Exception e)
295-
{
296-
// For some reason, exceptions happening here does... nothing.
297-
// Even System.AccessViolationExceptions gets ignored.
298-
Console.WriteLine($"Namespace added callback failed with: {e}");
299-
throw;
300-
}
290+
// try
291+
// {
292+
// namespaceAdded?.Invoke(ns);
293+
// }
294+
// catch (Exception e)
295+
// {
296+
// // For some reason, exceptions happening here does... nothing.
297+
// // Even System.AccessViolationExceptions gets ignored.
298+
// Console.WriteLine($"Namespace added callback failed with: {e}");
299+
// throw;
300+
// }
301301
}
302302

303303
if (ns != null && t.IsGenericTypeDefinition)

src/runtime/importhook.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static void SetupImportHook()
147147
/// </summary>
148148
static void SetupNamespaceTracking()
149149
{
150-
var newset = Runtime.PySet_New(new BorrowedReference(IntPtr.Zero));
150+
var newset = Runtime.PySet_New(default);
151151
try
152152
{
153153
foreach (var ns in AssemblyManager.GetNamespaces())
@@ -176,8 +176,8 @@ static void SetupNamespaceTracking()
176176
newset.Dispose();
177177
}
178178

179-
AssemblyManager.namespaceAdded += OnNamespaceAdded;
180-
PythonEngine.AddShutdownHandler(() => AssemblyManager.namespaceAdded -= OnNamespaceAdded);
179+
// AssemblyManager.namespaceAdded += OnNamespaceAdded;
180+
// PythonEngine.AddShutdownHandler(() => AssemblyManager.namespaceAdded -= OnNamespaceAdded);
181181
}
182182

183183
/// <summary>
@@ -186,20 +186,25 @@ static void SetupNamespaceTracking()
186186
/// </summary>
187187
static void TeardownNameSpaceTracking()
188188
{
189-
AssemblyManager.namespaceAdded -= OnNamespaceAdded;
189+
// AssemblyManager.namespaceAdded -= OnNamespaceAdded;
190190
// If the C# runtime isn't loaded, then there are no namespaces available
191191
Runtime.PyDict_SetItemString(root.dict, availableNsKey, Runtime.PyNone);
192192
}
193193

194-
static void OnNamespaceAdded(string name)
194+
public static void OnNamespaceAdded(string name)
195195
{
196+
Console.WriteLine(System.Environment.StackTrace);
197+
Console.WriteLine("OnNamespaceAdded: acquiring");
198+
Console.Out.Flush();
196199
using (Py.GIL())
197200
{
201+
Console.WriteLine("OnNamespaceAdded: acquired");
202+
Console.Out.Flush();
198203
var pyNs = Runtime.PyString_FromString(name);
199204
try
200205
{
201206
var nsSet = Runtime.PyDict_GetItemString(new BorrowedReference(root.dict), availableNsKey);
202-
if (!nsSet.IsNull || nsSet.DangerousGetAddress() != Runtime.PyNone)
207+
if (!(nsSet.IsNull && nsSet.IsNone))
203208
{
204209
if (Runtime.PySet_Add(nsSet, new BorrowedReference(pyNs)) != 0)
205210
{
@@ -212,6 +217,8 @@ static void OnNamespaceAdded(string name)
212217
Runtime.XDecref(pyNs);
213218
}
214219
}
220+
Console.WriteLine("OnNamespaceAdded: released");
221+
Console.Out.Flush();
215222
}
216223

217224

src/runtime/moduleobject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,11 @@ public static Assembly AddReference(string name)
529529
}
530530
// Classes that are not in a namespace needs an extra nudge to be found.
531531
ImportHook.UpdateCLRModuleDict();
532+
533+
// Heavyhanded but otherwise we'd need a "addedSinceLastCall".
534+
foreach(var ns in AssemblyManager.GetNamespaces()){
535+
ImportHook.OnNamespaceAdded(ns);
536+
}
532537
return assembly;
533538
}
534539

0 commit comments

Comments
 (0)