Skip to content

Move the implicit loading deprecation warning into LoadImplicit and use #37

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 1 commit into from
Apr 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions pythonnet/src/runtime/assemblymanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,30 +221,40 @@ public static Assembly LoadAssemblyPath(string name) {
// Call ONLY for namespaces that HAVE NOT been cached yet.
//===================================================================

public static bool LoadImplicit(string name, out bool fromFile) {
// 2010-08-16: Deprecation support
// Added out param to detect fully qualified name load
fromFile = false;
public static bool LoadImplicit(string name, bool warn=true) {
string[] names = name.Split('.');
bool loaded = false;
string s = "";
Assembly lastAssembly = null;
HashSet<Assembly> assemblies = null;
for (int i = 0; i < names.Length; i++) {
s = (i == 0) ? names[0] : s + "." + names[i];
if (!probed.ContainsKey(s)) {
if (LoadAssemblyPath(s) != null) {
loaded = true;
if (assemblies == null) {
assemblies = new HashSet<Assembly>(AppDomain.CurrentDomain.GetAssemblies());
}
Assembly a = LoadAssemblyPath(s);
if (a == null) {
a = LoadAssembly(s);
}
else if (LoadAssembly(s) != null) {
if (a != null && !assemblies.Contains(a)) {
loaded = true;
lastAssembly = a;
}
probed[s] = 1;
// 2010-12-24: Deprecation logic
if (loaded && (s == name)) {
fromFile = true;
//break;
}
}
}

// Deprecation warning
if (warn && loaded)
{
string deprWarning = String.Format(
"\nThe module was found, but not in a referenced namespace.\n" +
"Implicit loading is deprecated. Please use clr.AddReference(\"{0}\").",
Path.GetFileNameWithoutExtension(lastAssembly.Location));
Exceptions.deprecation(deprWarning);
}

return loaded;
}

Expand Down
11 changes: 1 addition & 10 deletions pythonnet/src/runtime/importhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,7 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {

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

// See if sys.modules for this interpreter already has the
// requested module. If so, just return the exising module.

IntPtr modules = Runtime.PyImport_GetModuleDict();
IntPtr module = Runtime.PyDict_GetItem(modules, py_mod_name);

Expand Down
10 changes: 2 additions & 8 deletions pythonnet/src/runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,8 @@ public ManagedType GetAttribute(string name, bool guess) {
// thing happens with implicit assembly loading at a reasonable
// cost. Ask the AssemblyManager to do implicit loading for each
// of the steps in the qualified name, then try it again.
bool fromFile;
if (AssemblyManager.LoadImplicit(qname, out fromFile)) {
bool ignore = name.StartsWith("__");
if (true == fromFile && (!ignore)) {
string deprWarning = String.Format("\nThe module was found, but not in a referenced namespace.\n" +
"Implicit loading is deprecated. Please use clr.AddReference(\"{0}\").", qname);
Exceptions.deprecation(deprWarning);
}
bool ignore = name.StartsWith("__");
if (AssemblyManager.LoadImplicit(qname, !ignore)) {
if (AssemblyManager.IsValidNamespace(qname)) {
m = new ModuleObject(qname);
StoreAttribute(name, m);
Expand Down
1 change: 0 additions & 1 deletion pythonnet/src/testing/classtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

using System;
using System.Collections;
using System.Windows.Forms;

namespace Python.Test {

Expand Down
17 changes: 0 additions & 17 deletions pythonnet/src/testing/eventtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// ==========================================================================

using System;
using System.Windows.Forms;

namespace Python.Test {

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

public class EventTest {


public void WinFormTest() {
EventTest e = new EventTest();
EventHandler h = new EventHandler(e.ClickHandler);

Form f = new Form();
f.Click += h;
//f.Click(null, new EventArgs());
f.Click -= h;
}

public void ClickHandler(object sender, EventArgs e) {
Console.WriteLine("click");
}


public static event TestEventHandler PublicStaticEvent;

protected static event TestEventHandler ProtectedStaticEvent;
Expand Down
1 change: 0 additions & 1 deletion pythonnet/src/testing/generictest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

using System;
using System.Collections;
using System.Windows.Forms;

namespace Python.Test {

Expand Down
1 change: 0 additions & 1 deletion pythonnet/src/tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# ===========================================================================

from System.Collections import Hashtable
from Python.Test import ClassTest
import sys, os, string, unittest, types
Expand Down
47 changes: 22 additions & 25 deletions pythonnet/src/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# ===========================================================================

import clr
clr.AddReference('Python.Test')
clr.AddReference('System.Data')

# testImplicitAssemblyLoad() passes on deprecation warning; perfect! #
##clr.AddReference('System.Windows.Forms')
import sys, os, string, unittest, types
import sys, os, string, unittest, types, warnings


class ModuleTests(unittest.TestCase):
Expand Down Expand Up @@ -42,8 +41,8 @@ def test000importClr(self):
def testPreloadVar(self):
import clr
self.assertTrue(clr.getPreload() is False, clr.getPreload())
clr.setPreload(False)
self.assertTrue(clr.getPreload() is False, clr.getPreload())
clr.setPreload(False)
self.assertTrue(clr.getPreload() is False, clr.getPreload())
try:
clr.setPreload(True)
self.assertTrue(clr.getPreload() is True, clr.getPreload())
Expand Down Expand Up @@ -204,27 +203,25 @@ def testFromModuleImportStar(self):

def testImplicitAssemblyLoad(self):
"""Test implicit assembly loading via import."""
# this test only applies to windows
if sys.platform != "win32":
return

def test():
# This should fail until System.Windows.Forms has been
# imported or that assembly has been explicitly loaded.
# True for Windows; Not so for Mono 2.8.1
import System.Windows

# The test fails when the project is compiled with MS VS 2005. Dunno why :(
# Fails (as expected) on Late Binding model. Works as expected on an interactive sesson.
self.assertRaises(ImportError, test)

clr.AddReference("System.Windows.Forms")
import System.Windows.Forms as Forms
self.assertTrue(self.isCLRModule(Forms))
self.assertTrue(Forms.__name__ == 'System.Windows.Forms')
from System.Windows.Forms import Form
self.assertTrue(self.isCLRClass(Form))
self.assertTrue(Form.__name__ == 'Form')
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")

# should trigger a DeprecationWarning as Microsoft.Build hasn't
# been added as a reference yet (and should exist for mono)
import Microsoft.Build

self.assertEqual(len(w), 1)
self.assertTrue(isinstance(w[0].message, DeprecationWarning))

with warnings.catch_warnings(record=True) as w:
clr.AddReference("System.Windows.Forms")
import System.Windows.Forms as Forms
self.assertTrue(self.isCLRModule(Forms))
self.assertTrue(Forms.__name__ == 'System.Windows.Forms')
from System.Windows.Forms import Form
self.assertTrue(self.isCLRClass(Form))
self.assertTrue(Form.__name__ == 'Form')
self.assertEqual(len(w), 0)


def testExplicitAssemblyLoad(self):
Expand Down