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

Commitb4e30ac

Browse files
authored
Merge branch 'master' into pyobject-finalizer
2 parents34713f7 +05a1451 commitb4e30ac

File tree

7 files changed

+74
-11
lines changed

7 files changed

+74
-11
lines changed

‎AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- Dmitriy Se ([@dmitriyse](https://github.com/dmitriyse))
2727
- He-chien Tsai ([@t3476](https://github.com/t3476))
2828
-   Ivan Cronyn ([@cronan](https://github.com/cronan))
29+
- Jan Krivanek ([@jakrivan](https://github.com/jakrivan))
2930
-   Jeff Reback ([@jreback](https://github.com/jreback))
3031
- Joe Frayne ([@jfrayne](https://github.com/jfrayne))
3132
- John Burnett ([@johnburnett](https://github.com/johnburnett))

‎CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
2020
- Implemented GetDynamicMemberNames() for PyObject to allow dynamic object members to be visible in the debugger ([#443][i443])([#690][p690])
2121
- Incorporated reference-style links to issues and pull requests in the CHANGELOG ([#608][i608])
2222
- Added PyObject finalizer support, Python objects referred by C# can be auto collect now ([#692][p692]).
23+
- Added detailed comments about aproaches and dangers to handle multi-app-domains ([#625][p625])
2324

2425
###Changed
2526
- PythonException included C# call stack
@@ -685,4 +686,5 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
685686
[p225]:https://github.com/pythonnet/pythonnet/pull/225
686687
[p78]:https://github.com/pythonnet/pythonnet/pull/78
687688
[p163]:https://github.com/pythonnet/pythonnet/pull/163
689+
[p625]:https://github.com/pythonnet/pythonnet/pull/625
688690
[i131]:https://github.com/pythonnet/pythonnet/issues/131

‎src/runtime/assemblymanager.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ internal class AssemblyManager
1717
{
1818
// modified from event handlers below, potentially triggered from different .NET threads
1919
// therefore this should be a ConcurrentDictionary
20+
//
21+
// WARNING: Dangerous if cross-app domain usage is ever supported
22+
// Reusing the dictionary with assemblies accross multiple initializations is problematic.
23+
// Loading happens from CurrentDomain (see line 53). And if the first call is from AppDomain that is later unloaded,
24+
// than it can end up referring to assemblies that are already unloaded (default behavior after unload appDomain -
25+
// unless LoaderOptimization.MultiDomain is used);
26+
// So for multidomain support it is better to have the dict. recreated for each app-domain initialization
2027
privatestaticConcurrentDictionary<string,ConcurrentDictionary<Assembly,string>>namespaces;
28+
2129
//private static Dictionary<string, Dictionary<string, string>> generics;
2230
privatestaticAssemblyLoadEventHandlerlhandler;
2331
privatestaticResolveEventHandlerrhandler;

‎src/runtime/importhook.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
237237
if(res!=IntPtr.Zero)
238238
{
239239
// There was no error.
240+
if(fromlist&&IsLoadAll(fromList))
241+
{
242+
varmod=ManagedType.GetManagedObject(res)asModuleObject;
243+
mod?.LoadNames();
244+
}
240245
returnres;
241246
}
242247
// There was an error
@@ -290,6 +295,11 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
290295
{
291296
if(fromlist)
292297
{
298+
if(IsLoadAll(fromList))
299+
{
300+
varmod=ManagedType.GetManagedObject(module)asModuleObject;
301+
mod?.LoadNames();
302+
}
293303
Runtime.XIncref(module);
294304
returnmodule;
295305
}
@@ -345,20 +355,33 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
345355
}
346356
}
347357

348-
ModuleObjectmod=fromlist?tail:head;
349-
350-
if(fromlist&&Runtime.PySequence_Size(fromList)==1)
351358
{
352-
IntPtrfp=Runtime.PySequence_GetItem(fromList,0);
353-
if(!CLRModule.preload&&Runtime.GetManagedString(fp)=="*")
359+
varmod=fromlist?tail:head;
360+
361+
if(fromlist&&IsLoadAll(fromList))
354362
{
355363
mod.LoadNames();
356364
}
357-
Runtime.XDecref(fp);
365+
366+
Runtime.XIncref(mod.pyHandle);
367+
returnmod.pyHandle;
358368
}
369+
}
359370

360-
Runtime.XIncref(mod.pyHandle);
361-
returnmod.pyHandle;
371+
privatestaticboolIsLoadAll(IntPtrfromList)
372+
{
373+
if(CLRModule.preload)
374+
{
375+
returnfalse;
376+
}
377+
if(Runtime.PySequence_Size(fromList)!=1)
378+
{
379+
returnfalse;
380+
}
381+
IntPtrfp=Runtime.PySequence_GetItem(fromList,0);
382+
boolres=Runtime.GetManagedString(fp)=="*";
383+
Runtime.XDecref(fp);
384+
returnres;
362385
}
363386
}
364387
}

‎src/runtime/moduleobject.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,17 @@ public void LoadNames()
190190
foreach(stringnameinAssemblyManager.GetNames(_namespace))
191191
{
192192
cache.TryGetValue(name,outm);
193-
if(m==null)
193+
if(m!=null)
194194
{
195-
ManagedTypeattr=GetAttribute(name,true);
195+
continue;
196196
}
197+
IntPtrattr=Runtime.PyDict_GetItemString(dict,name);
198+
// If __dict__ has already set a custom property, skip it.
199+
if(attr!=IntPtr.Zero)
200+
{
201+
continue;
202+
}
203+
GetAttribute(name,true);
197204
}
198205
}
199206

‎src/tests/importtest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding: utf-8 -*-
2+
3+
importsys
4+
try:
5+
delsys.modules["System.IO"]
6+
exceptKeyError:
7+
pass
8+
9+
assert"FileStream"notinglobals()
10+
importSystem.IO
11+
fromSystem.IOimport*
12+
13+
assert"FileStream"inglobals()

‎src/tests/test_import.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33
"""Test the import statement."""
44

55
importpytest
6-
6+
importsys
77

88
deftest_relative_missing_import():
99
"""Test that a relative missing import doesn't crash.
1010
Some modules use this to check if a package is installed.
1111
Relative import in the site-packages folder"""
1212
withpytest.raises(ImportError):
1313
from .import_missing_import
14+
15+
16+
deftest_import_all_on_second_time():
17+
"""Test import all attributes after a normal import without '*'.
18+
Due to import * only allowed at module level, the test body splited
19+
to a module file."""
20+
from .importimporttest
21+
delsys.modules[importtest.__name__]
22+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp