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

Skip to content

Commit 497d7aa

Browse files
committed
cleaned up the Import methods
1 parent f35d75b commit 497d7aa

3 files changed

Lines changed: 107 additions & 76 deletions

File tree

src/embed_tests/TestPyScope.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void TestImportModule()
165165
{
166166
using (Py.GIL())
167167
{
168-
dynamic sys = ps.ImportModule("sys");
168+
dynamic sys = ps.Import("sys");
169169
Assert.IsTrue(ps.ContainsVariable("sys"));
170170

171171
ps.Exec("sys.attr1 = 2");
@@ -175,7 +175,7 @@ public void TestImportModule()
175175
Assert.AreEqual(2, value2);
176176

177177
//import as
178-
ps.ImportModule("sys", "sys1");
178+
ps.Import("sys", "sys1");
179179
Assert.IsTrue(ps.ContainsVariable("sys1"));
180180
}
181181
}
@@ -193,7 +193,7 @@ public void TestImportScope()
193193
ps.SetVariable("cc", 10);
194194

195195
PyScope scope = Py.CreateScope();
196-
scope.ImportScope(ps, "ps");
196+
scope.Import(ps, "ps");
197197

198198
scope.Exec("aa = ps.bb + ps.cc + 3");
199199
var result = scope.GetVariable<int>("aa");
@@ -281,7 +281,7 @@ public void TestImportScopeByName()
281281
ps.SetVariable("bb", 100);
282282

283283
var scope = Py.CreateScope();
284-
scope.ImportAllFromScope("test");
284+
scope.ImportAll("test");
285285
//scope.ImportModule("test");
286286

287287
Assert.IsTrue(scope.ContainsVariable("bb"));

src/runtime/pyscope.cs

Lines changed: 100 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,21 @@ public class PyScope : DynamicObject, IDisposable
3535

3636
private bool isDisposed;
3737

38-
internal PyScopeManager Manager;
38+
internal readonly PyScopeManager Manager;
3939

4040
public event Action<PyScope> OnDispose;
4141

42-
internal static PyScope New(string name = null)
42+
public PyScope(IntPtr ptr, PyScopeManager manager)
4343
{
44-
if (name == null)
44+
if (Runtime.PyObject_Type(ptr) != Runtime.PyModuleType)
4545
{
46-
name = "";
46+
throw new PyScopeException("object is not a module");
4747
}
48-
var module = Runtime.PyModule_New(name);
49-
if (module == IntPtr.Zero)
48+
if(manager == null)
5049
{
51-
throw new PythonException();
50+
manager = PyScopeManager.Global;
5251
}
53-
return new PyScope(module);
54-
}
55-
56-
private PyScope(IntPtr ptr)
57-
{
52+
Manager = manager;
5853
obj = ptr;
5954
//Refcount of the variables not increase
6055
variables = Runtime.PyModule_GetDict(obj);
@@ -77,70 +72,92 @@ public PyDict Variables()
7772

7873
public PyScope NewScope()
7974
{
80-
var scope = PyScope.New();
81-
scope.ImportAllFromScope(this);
75+
var scope = Manager.Create();
76+
scope.ImportAll(this);
8277
return scope;
8378
}
8479

85-
public void ImportAllFromScope(string name)
86-
{
87-
var scope = Manager.Get(name);
88-
ImportAllFromScope(scope);
89-
}
90-
91-
public void ImportAllFromScope(PyScope scope)
80+
public dynamic Import(string name, string asname = null)
9281
{
93-
int result = Runtime.PyDict_Update(variables, scope.variables);
94-
if (result < 0)
82+
Check();
83+
if (asname == null)
9584
{
96-
throw new PythonException();
85+
asname = name;
9786
}
98-
}
99-
100-
public void ImportScope(string name, string asname = null)
101-
{
102-
var scope = Manager.Get(name);
103-
if(asname == null)
87+
var scope = Manager.TryGet(name);
88+
if(scope != null)
10489
{
105-
asname = name;
90+
Import(scope, asname);
91+
return scope;
10692
}
107-
ImportScope(scope, asname);
93+
PyObject module = PythonEngine.ImportModule(name);
94+
Import(module, asname);
95+
return module;
10896
}
10997

110-
public void ImportScope(PyScope scope, string asname)
98+
public void Import(PyScope scope, string asname)
11199
{
112100
this.SetVariable(asname, scope.obj);
113101
}
114102

115103
/// <summary>
116-
/// ImportModule Method
104+
/// Import Method
117105
/// </summary>
118106
/// <remarks>
119107
/// The import .. as .. statement in Python.
120-
/// Import a module ,add it to the variables dict and return the resulting module object as a PyObject.
108+
/// Import a module,add it to the variables dict and return the resulting module object as a PyObject.
121109
/// </remarks>
122-
public PyObject ImportModule(string name)
110+
public void Import(PyObject module, string asname = null)
123111
{
124-
return ImportModule(name, name);
112+
if (asname == null)
113+
{
114+
asname = module.GetAttr("__name__").ToString();
115+
}
116+
SetVariable(asname, module);
125117
}
126118

127-
/// <summary>
128-
/// ImportModule Method
129-
/// </summary>
130-
/// <remarks>
131-
/// The import .. as .. statement in Python.
132-
/// Import a module ,add it to the variables dict and return the resulting module object as a PyObject.
133-
/// </remarks>
134-
public PyObject ImportModule(string name, string asname)
119+
public void ImportAll(string name)
135120
{
136-
Check();
121+
var scope = Manager.TryGet(name);
122+
if(scope != null)
123+
{
124+
ImportAll(scope);
125+
return;
126+
}
137127
PyObject module = PythonEngine.ImportModule(name);
138-
if (asname == null)
128+
ImportAll(module);
129+
}
130+
131+
public void ImportAll(PyScope scope)
132+
{
133+
int result = Runtime.PyDict_Update(variables, scope.variables);
134+
if (result < 0)
139135
{
140-
asname = name;
136+
throw new PythonException();
137+
}
138+
}
139+
140+
public void ImportAll(PyObject module)
141+
{
142+
if (Runtime.PyObject_Type(module.obj) != Runtime.PyModuleType)
143+
{
144+
throw new PyScopeException("object is not a module");
145+
}
146+
var module_dict = Runtime.PyModule_GetDict(module.obj);
147+
int result = Runtime.PyDict_Update(variables, module_dict);
148+
if (result < 0)
149+
{
150+
throw new PythonException();
151+
}
152+
}
153+
154+
public void ImportAll(PyDict dict)
155+
{
156+
int result = Runtime.PyDict_Update(variables, dict.obj);
157+
if (result < 0)
158+
{
159+
throw new PythonException();
141160
}
142-
SetVariable(asname, module);
143-
return module;
144161
}
145162

146163
/// <summary>
@@ -263,15 +280,6 @@ private void SetVariable(string name, IntPtr value)
263280
}
264281
}
265282

266-
public void AddVariables(PyDict dict)
267-
{
268-
int result = Runtime.PyDict_Update(variables, dict.obj);
269-
if (result < 0)
270-
{
271-
throw new PythonException();
272-
}
273-
}
274-
275283
/// <summary>
276284
/// RemoveVariable Method
277285
/// </summary>
@@ -442,13 +450,28 @@ public void Dispose()
442450

443451
public class PyScopeManager
444452
{
453+
public readonly static PyScopeManager Global = new PyScopeManager();
454+
445455
private Dictionary<string, PyScope> NamedScopes = new Dictionary<string, PyScope>();
446456

457+
internal PyScope NewScope(string name)
458+
{
459+
if (name == null)
460+
{
461+
name = "";
462+
}
463+
var module = Runtime.PyModule_New(name);
464+
if (module == IntPtr.Zero)
465+
{
466+
throw new PythonException();
467+
}
468+
return new PyScope(module, this);
469+
}
470+
447471
[PyGIL]
448472
public PyScope Create()
449473
{
450-
var scope = PyScope.New();
451-
scope.Manager = this;
474+
var scope = this.NewScope(null);
452475
return scope;
453476
}
454477

@@ -457,14 +480,13 @@ public PyScope Create(string name)
457480
{
458481
if (String.IsNullOrEmpty(name))
459482
{
460-
throw new PyScopeException("Name of ScopeStorage must not be empty");
483+
throw new ArgumentNullException(nameof(name));
461484
}
462485
if (name != null && NamedScopes.ContainsKey(name))
463486
{
464-
throw new PyScopeException(String.Format("ScopeStorage '{0}' has existed", name));
487+
throw new PyScopeException($"PyScope '{name}' has existed");
465488
}
466-
var scope = PyScope.New(name);
467-
scope.Manager = this;
489+
var scope = this.NewScope(name);
468490
scope.OnDispose += Remove;
469491
NamedScopes[name] = scope;
470492
return scope;
@@ -477,11 +499,22 @@ public bool Contains(string name)
477499

478500
public PyScope Get(string name)
479501
{
480-
if (name != null && NamedScopes.ContainsKey(name))
502+
if (name == null)
503+
{
504+
throw new ArgumentNullException(nameof(name));
505+
}
506+
if (NamedScopes.ContainsKey(name))
481507
{
482508
return NamedScopes[name];
483509
}
484-
throw new PyScopeException(String.Format("ScopeStorage '{0}' not exist", name));
510+
throw new PyScopeException($"PyScope '{name}' not exist");
511+
}
512+
513+
public PyScope TryGet(string name)
514+
{
515+
PyScope value;
516+
NamedScopes.TryGetValue(name, out value);
517+
return value;
485518
}
486519

487520
public void Remove(PyScope scope)

src/runtime/pythonengine.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public static void Shutdown()
294294
{
295295
if (initialized)
296296
{
297-
Py.PyScopeManager.Clear();
297+
PyScopeManager.Global.Clear();
298298
Marshal.FreeHGlobal(_pythonHome);
299299
_pythonHome = IntPtr.Zero;
300300
Marshal.FreeHGlobal(_programName);
@@ -546,18 +546,16 @@ public static GILState GIL()
546546

547547
return new GILState();
548548
}
549-
550-
internal static PyScopeManager PyScopeManager = new PyScopeManager();
551549

552550
public static PyScope CreateScope()
553551
{
554-
var scope = PyScopeManager.Create();
552+
var scope = PyScopeManager.Global.Create();
555553
return scope;
556554
}
557555

558556
public static PyScope CreateScope(string name)
559557
{
560-
var scope = PyScopeManager.Create(name);
558+
var scope = PyScopeManager.Global.Create(name);
561559
return scope;
562560
}
563561

0 commit comments

Comments
 (0)