Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit85c1ea4

Browse files
committed
Remove deprecated implicit assembly loading
Legacy behavior:`import Company.Product.Namespace` would search for`Company.Product.Namespace.dll` .NET assmebly, load it, and import thenamespace.New behavior:User must always explicitly add assembly reference using`clr.AddReference` to `Company.Product.Namespace` prior to attemptingimport
1 parent35e76f6 commit85c1ea4

File tree

7 files changed

+16
-132
lines changed

7 files changed

+16
-132
lines changed

‎CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ details about the cause of the failure
3333
- Fixed a bug where indexers could not be used if they were inherited
3434
- Made it possible to use`__len__` also on`ICollection<>` interface objects
3535

36+
###Removed
37+
38+
- implicit assembly loading (you have to explicitly`clr.AddReference` before doing import)
39+
3640
##[2.5.0][] - 2020-06-14
3741

3842
This version improves performance on benchmarks significantly compared to 2.3.

‎src/runtime/assemblymanager.cs

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -252,75 +252,6 @@ public static Assembly FindLoadedAssembly(string name)
252252
returnnull;
253253
}
254254

255-
/// <summary>
256-
/// Given a qualified name of the form A.B.C.D, attempt to load
257-
/// an assembly named after each of A.B.C.D, A.B.C, A.B, A. This
258-
/// will only actually probe for the assembly once for each unique
259-
/// namespace. Returns true if any assemblies were loaded.
260-
/// </summary>
261-
/// <remarks>
262-
/// TODO item 3 "* Deprecate implicit loading of assemblies":
263-
/// Set the fromFile flag if the name of the loaded assembly matches
264-
/// the fully qualified name that was requested if the framework
265-
/// actually loads an assembly.
266-
/// Call ONLY for namespaces that HAVE NOT been cached yet.
267-
/// </remarks>
268-
publicstaticboolLoadImplicit(stringname,Action<Exception>assemblyLoadErrorHandler,boolwarn=true)
269-
{
270-
string[]names=name.Split('.');
271-
varloaded=false;
272-
vars="";
273-
AssemblylastAssembly=null;
274-
HashSet<Assembly>assembliesSet=null;
275-
for(vari=0;i<names.Length;i++)
276-
{
277-
s=i==0?names[0]:s+"."+names[i];
278-
if(!probed.ContainsKey(s))
279-
{
280-
if(assembliesSet==null)
281-
{
282-
assembliesSet=newHashSet<Assembly>(AppDomain.CurrentDomain.GetAssemblies());
283-
}
284-
Assemblya=FindLoadedAssembly(s);
285-
try
286-
{
287-
if(a==null)
288-
{
289-
a=LoadAssemblyPath(s);
290-
}
291-
292-
if(a==null)
293-
{
294-
a=LoadAssembly(s);
295-
}
296-
}
297-
catch(FileLoadExceptione){assemblyLoadErrorHandler(e);}
298-
catch(BadImageFormatExceptione){assemblyLoadErrorHandler(e);}
299-
catch(System.Security.SecurityExceptione){assemblyLoadErrorHandler(e);}
300-
catch(PathTooLongExceptione){assemblyLoadErrorHandler(e);}
301-
302-
if(a!=null&&!assembliesSet.Contains(a))
303-
{
304-
loaded=true;
305-
lastAssembly=a;
306-
}
307-
probed[s]=1;
308-
}
309-
}
310-
311-
// Deprecation warning
312-
if(warn&&loaded)
313-
{
314-
stringlocation=Path.GetFileNameWithoutExtension(lastAssembly.Location);
315-
stringdeprWarning="The module was found, but not in a referenced namespace.\n"+
316-
$"Implicit loading is deprecated. Please use clr.AddReference('{location}').";
317-
Exceptions.deprecation(deprWarning);
318-
}
319-
320-
returnloaded;
321-
}
322-
323-
324255
/// <summary>
325256
/// Scans an assembly for exported namespaces, adding them to the
326257
/// mapping of valid namespaces. Note that for a given namespace

‎src/runtime/importhook.cs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -310,38 +310,6 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
310310

311311
string[]names=realname.Split('.');
312312

313-
// Now we need to decide if the name refers to a CLR module,
314-
// and may have to do an implicit load (for b/w compatibility)
315-
// using the AssemblyManager. The assembly manager tries
316-
// really hard not to use Python objects or APIs, because
317-
// parts of it can run recursively and on strange threads.
318-
//
319-
// It does need an opportunity from time to time to check to
320-
// see if sys.path has changed, in a context that is safe. Here
321-
// we know we have the GIL, so we'll let it update if needed.
322-
323-
AssemblyManager.UpdatePath();
324-
if(!AssemblyManager.IsValidNamespace(realname))
325-
{
326-
varloadExceptions=newList<Exception>();
327-
if(!AssemblyManager.LoadImplicit(realname,assemblyLoadErrorHandler:loadExceptions.Add))
328-
{
329-
// May be called when a module being imported imports a module.
330-
// In particular, I've seen decimal import copy import org.python.core
331-
IntPtrimportResult=Runtime.PyObject_Call(py_import,args,kw);
332-
// TODO: use ModuleNotFoundError in Python 3.6+
333-
if(importResult==IntPtr.Zero&&loadExceptions.Count>0
334-
&&Exceptions.ExceptionMatches(Exceptions.ImportError))
335-
{
336-
loadExceptions.Add(newPythonException());
337-
varimportError=newPyObject(newBorrowedReference(Exceptions.ImportError));
338-
importError.SetAttr("__cause__",newAggregateException(loadExceptions).ToPython());
339-
Runtime.PyErr_SetObject(newBorrowedReference(Exceptions.ImportError),importError.Reference);
340-
}
341-
returnimportResult;
342-
}
343-
}
344-
345313
// See if sys.modules for this interpreter already has the
346314
// requested module. If so, just return the existing module.
347315
IntPtrmodules=Runtime.PyImport_GetModuleDict();

‎src/runtime/moduleobject.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -118,30 +118,6 @@ public ManagedType GetAttribute(string name, bool guess)
118118
returnc;
119119
}
120120

121-
// This is a little repetitive, but it ensures that the right
122-
// thing happens with implicit assembly loading at a reasonable
123-
// cost. Ask the AssemblyManager to do implicit loading for each
124-
// of the steps in the qualified name, then try it again.
125-
boolignore=name.StartsWith("__");
126-
if(AssemblyManager.LoadImplicit(qname,assemblyLoadErrorHandler:ImportWarning,!ignore))
127-
{
128-
if(AssemblyManager.IsValidNamespace(qname))
129-
{
130-
m=newModuleObject(qname);
131-
StoreAttribute(name,m);
132-
m.DecrRefCount();
133-
returnm;
134-
}
135-
136-
type=AssemblyManager.LookupTypes(qname).FirstOrDefault(t=>t.IsPublic);
137-
if(type!=null)
138-
{
139-
c=ClassManager.GetClass(type);
140-
StoreAttribute(name,c);
141-
returnc;
142-
}
143-
}
144-
145121
// We didn't find the name, so we may need to see if there is a
146122
// generic type with this base name. If so, we'll go ahead and
147123
// return it. Note that we store the mapping of the unmangled

‎src/tests/test_array.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
"""Test support for managed arrays."""
44

5+
importclr
56
importPython.TestasTest
67
importSystem
78
importpytest
@@ -1143,6 +1144,8 @@ def test_boxed_value_type_mutation_result():
11431144
# to accidentally write code like the following which is not really
11441145
# mutating value types in-place but changing boxed copies.
11451146

1147+
clr.AddReference('System.Drawing')
1148+
11461149
fromSystem.DrawingimportPoint
11471150
fromSystemimportArray
11481151

‎src/tests/test_class.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Test CLR class support."""
55

6+
importclr
67
importPython.TestasTest
78
importSystem
89
importpytest
@@ -124,6 +125,9 @@ def __init__(self, v):
124125

125126
deftest_struct_construction():
126127
"""Test construction of structs."""
128+
129+
clr.AddReference('System.Drawing')
130+
127131
fromSystem.DrawingimportPoint
128132

129133
p=Point()

‎src/tests/test_module.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,14 @@ def test_from_module_import_star():
201201

202202
deftest_implicit_assembly_load():
203203
"""Test implicit assembly loading via import."""
204-
withwarnings.catch_warnings(record=True)asw:
205-
warnings.simplefilter("always")
204+
withpytest.raises(ImportError):
205+
# MS.Build should not have been added as a reference yet
206+
# (and should exist for mono)
206207

207-
#should trigger a DeprecationWarning as Microsoft.Build hasn't
208-
#been added as a reference yet (andshouldexist for mono)
208+
#The implicit behavior has been disabled in 3.0
209+
#therefore thisshouldfail
209210
importMicrosoft.Build
210211

211-
assertlen(w)==1
212-
assertisinstance(w[0].message,DeprecationWarning)
213-
214212
withwarnings.catch_warnings(record=True)asw:
215213
clr.AddReference("System.Windows.Forms")
216214
importSystem.Windows.FormsasForms

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp