Skip to content

Commit d97616d

Browse files
committed
Merge pull request #37 from tonyroberts/develop
Move the implicit loading deprecation warning into LoadImplicit
2 parents 12b0a77 + b123d58 commit d97616d

File tree

8 files changed

+47
-75
lines changed

8 files changed

+47
-75
lines changed

pythonnet/src/runtime/assemblymanager.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,30 +221,40 @@ public static Assembly LoadAssemblyPath(string name) {
221221
// Call ONLY for namespaces that HAVE NOT been cached yet.
222222
//===================================================================
223223

224-
public static bool LoadImplicit(string name, out bool fromFile) {
225-
// 2010-08-16: Deprecation support
226-
// Added out param to detect fully qualified name load
227-
fromFile = false;
224+
public static bool LoadImplicit(string name, bool warn=true) {
228225
string[] names = name.Split('.');
229226
bool loaded = false;
230227
string s = "";
228+
Assembly lastAssembly = null;
229+
HashSet<Assembly> assemblies = null;
231230
for (int i = 0; i < names.Length; i++) {
232231
s = (i == 0) ? names[0] : s + "." + names[i];
233232
if (!probed.ContainsKey(s)) {
234-
if (LoadAssemblyPath(s) != null) {
235-
loaded = true;
233+
if (assemblies == null) {
234+
assemblies = new HashSet<Assembly>(AppDomain.CurrentDomain.GetAssemblies());
235+
}
236+
Assembly a = LoadAssemblyPath(s);
237+
if (a == null) {
238+
a = LoadAssembly(s);
236239
}
237-
else if (LoadAssembly(s) != null) {
240+
if (a != null && !assemblies.Contains(a)) {
238241
loaded = true;
242+
lastAssembly = a;
239243
}
240244
probed[s] = 1;
241-
// 2010-12-24: Deprecation logic
242-
if (loaded && (s == name)) {
243-
fromFile = true;
244-
//break;
245-
}
246245
}
247246
}
247+
248+
// Deprecation warning
249+
if (warn && loaded)
250+
{
251+
string deprWarning = String.Format(
252+
"\nThe module was found, but not in a referenced namespace.\n" +
253+
"Implicit loading is deprecated. Please use clr.AddReference(\"{0}\").",
254+
Path.GetFileNameWithoutExtension(lastAssembly.Location));
255+
Exceptions.deprecation(deprWarning);
256+
}
257+
248258
return loaded;
249259
}
250260

pythonnet/src/runtime/importhook.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,7 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {
156156

157157
AssemblyManager.UpdatePath();
158158
if (!AssemblyManager.IsValidNamespace(realname)) {
159-
bool fromFile = false;
160-
if (AssemblyManager.LoadImplicit(realname, out fromFile)) {
161-
if (true == fromFile) {
162-
string deprWarning = String.Format("\nThe module was found, but not in a referenced namespace.\n" +
163-
"Implicit loading is deprecated. Please use clr.AddReference(\"{0}\").", realname);
164-
Exceptions.deprecation(deprWarning);
165-
}
166-
}
167-
else
159+
if (!AssemblyManager.LoadImplicit(realname))
168160
{
169161
// May be called when a module being imported imports a module.
170162
// In particular, I've seen decimal import copy import org.python.core
@@ -174,7 +166,6 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {
174166

175167
// See if sys.modules for this interpreter already has the
176168
// requested module. If so, just return the exising module.
177-
178169
IntPtr modules = Runtime.PyImport_GetModuleDict();
179170
IntPtr module = Runtime.PyDict_GetItem(modules, py_mod_name);
180171

pythonnet/src/runtime/moduleobject.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,8 @@ public ManagedType GetAttribute(string name, bool guess) {
109109
// thing happens with implicit assembly loading at a reasonable
110110
// cost. Ask the AssemblyManager to do implicit loading for each
111111
// of the steps in the qualified name, then try it again.
112-
bool fromFile;
113-
if (AssemblyManager.LoadImplicit(qname, out fromFile)) {
114-
bool ignore = name.StartsWith("__");
115-
if (true == fromFile && (!ignore)) {
116-
string deprWarning = String.Format("\nThe module was found, but not in a referenced namespace.\n" +
117-
"Implicit loading is deprecated. Please use clr.AddReference(\"{0}\").", qname);
118-
Exceptions.deprecation(deprWarning);
119-
}
112+
bool ignore = name.StartsWith("__");
113+
if (AssemblyManager.LoadImplicit(qname, !ignore)) {
120114
if (AssemblyManager.IsValidNamespace(qname)) {
121115
m = new ModuleObject(qname);
122116
StoreAttribute(name, m);

pythonnet/src/testing/classtest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
using System;
1111
using System.Collections;
12-
using System.Windows.Forms;
1312

1413
namespace Python.Test {
1514

pythonnet/src/testing/eventtest.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// ==========================================================================
99

1010
using System;
11-
using System.Windows.Forms;
1211

1312
namespace Python.Test {
1413

@@ -21,22 +20,6 @@ namespace Python.Test {
2120

2221
public class EventTest {
2322

24-
25-
public void WinFormTest() {
26-
EventTest e = new EventTest();
27-
EventHandler h = new EventHandler(e.ClickHandler);
28-
29-
Form f = new Form();
30-
f.Click += h;
31-
//f.Click(null, new EventArgs());
32-
f.Click -= h;
33-
}
34-
35-
public void ClickHandler(object sender, EventArgs e) {
36-
Console.WriteLine("click");
37-
}
38-
39-
4023
public static event TestEventHandler PublicStaticEvent;
4124

4225
protected static event TestEventHandler ProtectedStaticEvent;

pythonnet/src/testing/generictest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
using System;
1111
using System.Collections;
12-
using System.Windows.Forms;
1312

1413
namespace Python.Test {
1514

pythonnet/src/tests/test_class.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
77
# FOR A PARTICULAR PURPOSE.
88
# ===========================================================================
9-
109
from System.Collections import Hashtable
1110
from Python.Test import ClassTest
1211
import sys, os, string, unittest, types

pythonnet/src/tests/test_module.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
77
# FOR A PARTICULAR PURPOSE.
88
# ===========================================================================
9-
109
import clr
1110
clr.AddReference('Python.Test')
1211
clr.AddReference('System.Data')
1312

1413
# testImplicitAssemblyLoad() passes on deprecation warning; perfect! #
1514
##clr.AddReference('System.Windows.Forms')
16-
import sys, os, string, unittest, types
15+
import sys, os, string, unittest, types, warnings
1716

1817

1918
class ModuleTests(unittest.TestCase):
@@ -42,8 +41,8 @@ def test000importClr(self):
4241
def testPreloadVar(self):
4342
import clr
4443
self.assertTrue(clr.getPreload() is False, clr.getPreload())
45-
clr.setPreload(False)
46-
self.assertTrue(clr.getPreload() is False, clr.getPreload())
44+
clr.setPreload(False)
45+
self.assertTrue(clr.getPreload() is False, clr.getPreload())
4746
try:
4847
clr.setPreload(True)
4948
self.assertTrue(clr.getPreload() is True, clr.getPreload())
@@ -204,27 +203,25 @@ def testFromModuleImportStar(self):
204203

205204
def testImplicitAssemblyLoad(self):
206205
"""Test implicit assembly loading via import."""
207-
# this test only applies to windows
208-
if sys.platform != "win32":
209-
return
210-
211-
def test():
212-
# This should fail until System.Windows.Forms has been
213-
# imported or that assembly has been explicitly loaded.
214-
# True for Windows; Not so for Mono 2.8.1
215-
import System.Windows
216-
217-
# The test fails when the project is compiled with MS VS 2005. Dunno why :(
218-
# Fails (as expected) on Late Binding model. Works as expected on an interactive sesson.
219-
self.assertRaises(ImportError, test)
220-
221-
clr.AddReference("System.Windows.Forms")
222-
import System.Windows.Forms as Forms
223-
self.assertTrue(self.isCLRModule(Forms))
224-
self.assertTrue(Forms.__name__ == 'System.Windows.Forms')
225-
from System.Windows.Forms import Form
226-
self.assertTrue(self.isCLRClass(Form))
227-
self.assertTrue(Form.__name__ == 'Form')
206+
with warnings.catch_warnings(record=True) as w:
207+
warnings.simplefilter("always")
208+
209+
# should trigger a DeprecationWarning as Microsoft.Build hasn't
210+
# been added as a reference yet (and should exist for mono)
211+
import Microsoft.Build
212+
213+
self.assertEqual(len(w), 1)
214+
self.assertTrue(isinstance(w[0].message, DeprecationWarning))
215+
216+
with warnings.catch_warnings(record=True) as w:
217+
clr.AddReference("System.Windows.Forms")
218+
import System.Windows.Forms as Forms
219+
self.assertTrue(self.isCLRModule(Forms))
220+
self.assertTrue(Forms.__name__ == 'System.Windows.Forms')
221+
from System.Windows.Forms import Form
222+
self.assertTrue(self.isCLRClass(Form))
223+
self.assertTrue(Form.__name__ == 'Form')
224+
self.assertEqual(len(w), 0)
228225

229226

230227
def testExplicitAssemblyLoad(self):

0 commit comments

Comments
 (0)