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

Skip to content

Commit daa2901

Browse files
committed
attempt to fix PyScopeTest.TestThread() reading stale value from res in Python 2.7 x64
1 parent 39b2347 commit daa2901

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/embed_tests/TestPyScope.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using NUnit.Framework;
34
using Python.Runtime;
45

@@ -337,9 +338,12 @@ public void TestThread()
337338
//add function to the scope
338339
//can be call many times, more efficient than ast
339340
ps.Exec(
341+
"import clr\n" +
342+
"from System.Threading import Thread\n" +
340343
"def update():\n" +
341344
" global res, th_cnt\n" +
342345
" res += bb + 1\n" +
346+
" Thread.MemoryBarrier()\n" +
343347
" th_cnt += 1\n"
344348
);
345349
}
@@ -364,8 +368,9 @@ public void TestThread()
364368
{
365369
cnt = ps.Get<int>("th_cnt");
366370
}
367-
System.Threading.Thread.Sleep(10);
371+
Thread.Sleep(10);
368372
}
373+
Thread.MemoryBarrier();
369374
using (Py.GIL())
370375
{
371376
var result = ps.Get<int>("res");

src/runtime/assemblymanager.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ public static List<string> GetNames(string nsname)
452452
/// looking in the currently loaded assemblies for the named
453453
/// type. Returns null if the named type cannot be found.
454454
/// </summary>
455+
[Obsolete("Use LookupTypes and handle name conflicts")]
455456
public static Type LookupType(string qname)
456457
{
457458
foreach (Assembly assembly in assemblies)
@@ -465,6 +466,14 @@ public static Type LookupType(string qname)
465466
return null;
466467
}
467468

469+
/// <summary>
470+
/// Returns the <see cref="Type"/> objects for the given qualified name,
471+
/// looking in the currently loaded assemblies for the named
472+
/// type.
473+
/// </summary>
474+
public static IEnumerable<Type> LookupTypes(string qualifiedName)
475+
=> assemblies.Select(assembly => assembly.GetType(qualifiedName)).Where(type => type != null);
476+
468477
internal static Type[] GetTypes(Assembly a)
469478
{
470479
if (a.IsDynamic)
@@ -492,4 +501,4 @@ internal static Type[] GetTypes(Assembly a)
492501
}
493502
}
494503
}
495-
}
504+
}

src/runtime/moduleobject.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.IO;
45
using System.Reflection;
56
using System.Runtime.InteropServices;
@@ -105,13 +106,9 @@ public ManagedType GetAttribute(string name, bool guess)
105106
// Look for a type in the current namespace. Note that this
106107
// includes types, delegates, enums, interfaces and structs.
107108
// Only public namespace members are exposed to Python.
108-
type = AssemblyManager.LookupType(qname);
109+
type = AssemblyManager.LookupTypes(qname).FirstOrDefault(t => t.IsPublic);
109110
if (type != null)
110111
{
111-
if (!type.IsPublic)
112-
{
113-
return null;
114-
}
115112
c = ClassManager.GetClass(type);
116113
StoreAttribute(name, c);
117114
return c;
@@ -131,13 +128,9 @@ public ManagedType GetAttribute(string name, bool guess)
131128
return m;
132129
}
133130

134-
type = AssemblyManager.LookupType(qname);
131+
type = AssemblyManager.LookupTypes(qname).FirstOrDefault(t => t.IsPublic);
135132
if (type != null)
136133
{
137-
if (!type.IsPublic)
138-
{
139-
return null;
140-
}
141134
c = ClassManager.GetClass(type);
142135
StoreAttribute(name, c);
143136
return c;

0 commit comments

Comments
 (0)