8000 updated according to filmor's comments · pythonnet/pythonnet@2c3db3d · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c3db3d

Browse files
committed
updated according to filmor's comments
1 parent ebf5a2b commit 2c3db3d

File tree

3 files changed

+87
-91
lines changed

3 files changed

+87
-91
lines changed

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
6161

6262
- Deprecated `RunString` (#401)
6363

64-
### Deprecated
65-
66-
- Deprecated `RunString` (#401)
67-
6864
### Fixed
6965

7066
- Fixed crash during Initialization (#262)(#343)

src/embed_tests/TestPyScope.cs

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,13 @@ public void TestImportScope()
192192
ps.SetVariable("bb", 100);
193193
ps.SetVariable("cc", 10);
194194

195-
PyScope scope = Py.CreateScope();
196-
scope.Import(ps, "ps");
197-
198-
scope.Exec("aa = ps.bb + ps.cc + 3");
199-
var result = scope.GetVariable<int>("aa");
200-
Assert.AreEqual(113, result);
201-
202-
scope.Dispose();
195+
using (var scope = Py.CreateScope())
196+
{
197+
scope.Import(ps, "ps");
198+
scope.Exec("aa = ps.bb + ps.cc + 3");
199+
var result = scope.GetVariable<int>("aa");
200+
Assert.AreEqual(113, result);
201+
}
203202

204203
Assert.IsFalse(ps.ContainsVariable("aa"));
205204
}
@@ -217,13 +216,12 @@ public void TestImportAllFromScope()
217216
ps.SetVariable("bb", 100);
218217
ps.SetVariable("cc", 10);
219218

220-
PyScope scope = ps.NewScope();
221-
222-
scope.Exec("aa = bb + cc + 3");
223-
var result = scope.GetVariable<int>("aa");
224-
Assert.AreEqual(113, result);
225-
226-
scope.Dispose();
219+
using (var scope = ps.NewScope())
220+
{
221+
scope.Exec("aa = bb + cc + 3");
222+
var result = scope.GetVariable<int>("aa");
223+
Assert.AreEqual(113, result);
224+
}
227225

228226
Assert.IsFalse(ps.ContainsVariable("aa"));
229227
}
@@ -244,28 +242,27 @@ public void TestImportScopeFunction()
244242
"def func1():\n" +
245243
" return cc + bb\n");
246244

247-
PyScope scope = ps.NewScope();
248-
249-
//'func1' is imported from the origion scope
250-
scope.Exec(
251-
"def func2():\n" +
252-
" return func1() - cc - bb\n");
253-
dynamic func2 = scope.GetVariable("func2");
254-
255-
var result1 = func2().As<int>();
256-
Assert.AreEqual(0, result1);
257-
258-
scope.SetVariable("cc", 20);//it has no effect on the globals of 'func1'
259-
var result2 = func2().As<int>();
260-
Assert.AreEqual(-10, result2);
261-
scope.SetVariable("cc", 10); //rollback
262-
263-
ps.SetVariable("cc", 20);
264-
var result3 = func2().As<int>();
265-
Assert.AreEqual(10, result3);
266-
ps.SetVariable("cc", 10); //rollback
267-
268-
scope.Dispose();
245+
using (PyScope scope = ps.NewScope())
246+
{
247+
//'func1' is imported from the origion scope
248+
scope.Exec(
249+
"def func2():\n" +
250+
" return func1() - cc - bb\n");
251+
dynamic func2 = scope.GetVariable("func2");
252+
253+
var result1 = func2().As<int>();
254+
Assert.AreEqual(0, result1);
255+
256+
scope.SetVariable("cc", 20);//it has no effect on the globals of 'func1'
257+
var result2 = func2().As<int>();
258+
Assert.AreEqual(-10, result2);
259+
scope.SetVariable("cc", 10); //rollback
260+
261+
ps.SetVariable("cc", 20);
262+
var result3 = func2().As<int>();
263+
Assert.AreEqual(10, result3);
264+
ps.SetVariable("cc", 10); //rollback
265+
}
269266
}
270267
}
271268

@@ -280,11 +277,13 @@ public void TestImportScopeByName()
280277
{
281278
ps.SetVariable("bb", 100);
282279

283-
var scope = Py.CreateScope();
284-
scope.ImportAll("test");
285-
//scope.ImportModule("test");
280+
using (var scope = Py.CreateScope())
281+
{
282+
scope.ImportAll("test");
283+
//scope.ImportModule("test");
286284

287-
Assert.IsTrue(scope.ContainsVariable("bb"));
285+
Assert.IsTrue(scope.ContainsVariable("bb"));
286+
}
288287
}
289288
}
290289

@@ -306,9 +305,10 @@ public void TestVariables()
306305
var a2 = ps.GetVariable<int>("ee");
307306
Assert.AreEqual(220, a2);
308307

309-
var item = ps.Variables();
310-
item["ee"] = new PyInt(230);
311-
item.Dispose();
308+
using (var item = ps.Variables())
309+
{
310+
item["ee"] = new PyInt(230);
311+
}
312312
var a3 = ps.GetVariable<int>("ee");
313313
Assert.AreEqual(230, a3);
314314
}

src/runtime/pyscope.cs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public class PyScope : DynamicObject, IDisposable
3838
internal readonly PyScopeManager Manager;
3939

4040
public event Action<PyScope> OnDispose;
41-
42-
public PyScope(IntPtr ptr, PyScopeManager manager)
41+
42+
internal PyScope(IntPtr ptr, PyScopeManager manager)
4343
{
4444
if (Runtime.PyObject_Type(ptr) != Runtime.PyModuleType)
4545
{
4646
throw new PyScopeException("object is not a module");
4747
}
48-
if(manager == null)
48+
if (manager == null)
4949
{
5050
manager = PyScopeManager.Global;
5151
}
@@ -80,19 +80,23 @@ public PyScope NewScope()
8080
public dynamic Import(string name, string asname = null)
8181
{
8282
Check();
83-
if (asname == null)
83+
if (String.IsNullOrEmpty(asname))
8484
{
8585
asname = name;
8686
}
87-
var scope = Manager.TryGet(name);
88-
if(scope != null)
87+
PyScope scope;
88+
Manager.TryGet(name, out scope);
89+
if (scope != null)
8990
{
9091
Import(scope, asname);
9192
return scope;
9293
}
93-
PyObject module = PythonEngine.ImportModule(name);
94-
Import(module, asname);
95-
return module;
94+
else
95+
{
96+
PyObject module = PythonEngine.ImportModule(name);
97+
Import(module, asname);
98+
return module;
99+
}
96100
}
97101

98102
public void Import(PyScope scope, string asname)
@@ -109,7 +113,7 @@ public void Import(PyScope scope, string asname)
109113
/// </remarks>
110114
public void Import(PyObject module, string asname = null)
111115
{
112-
if (asname == null)
116+
if (String.IsNullOrEmpty(asname))
113117
{
114118
asname = module.GetAttr("__name__").ToString();
115119
}
@@ -118,14 +122,18 @@ public void Import(PyObject module, string asname = null)
118122

119123
public void ImportAll(string name)
120124
{
121-
var scope = Manager.TryGet(name);
122-
if(scope != null)
125+
PyScope scope;
126+
Manager.TryGet(name, out scope);
127+
if (scope != null)
123128
{
124129
ImportAll(scope);
125130
return;
126131
}
127-
PyObject module = PythonEngine.ImportModule(name);
128-
ImportAll(module);
132+
else
133+
{
134+
PyObject module = PythonEngine.ImportModule(name);
135+
ImportAll(module);
136+
}
129137
}
130138

131139
public void ImportAll(PyScope scope)
@@ -323,27 +331,13 @@ public bool ContainsVariable(string name)
323331
/// </remarks>
324332
public PyObject GetVariable(string name)
325333
{
326-
Check();
327-
using (var pyKey = new PyString(name))
334+
PyObject scope;
335+
var state = TryGetVariable(name, out scope);
336+
if(!state)
328337
{
329-
if (Runtime.PyMapping_HasKey(variables, pyKey.obj) != 0)
330-
{
331-
IntPtr op = Runtime.PyObject_GetItem(variables, pyKey.obj);
332-
if (op == IntPtr.Zero)
333-
{
334-
throw new PythonException();
335-
}
336-
if (op == Runtime.PyNone)
337-
{
338-
return null;
339-
}
340-
return new PyObject(op);
341-
}
342-
else
343-
{
344-
throw new PyScopeException(String.Format("'ScopeStorage' object has no attribute '{0}'", name));
345-
}
338+
throw new PyScopeException($"The scope of name '{Name}' has no attribute '{name}'");
346339
}
340+
return scope;
347341
}
348342

349343
/// <summary>
@@ -367,6 +361,7 @@ public bool TryGetVariable(string name, out PyObject value)
367361
}
368362
if (op == Runtime.PyNone)
369363
{
364+
Runtime.XDecref(op);
370365
value = null;
371366
return true;
372367
}
@@ -401,11 +396,18 @@ public bool TryGetVariable<T>(string name, out T value)
401396
{
402397
value = default(T);
403398
return false;
404-
}
399+
}
405400
if (pyObj == null)
406401
{
407-
value = default(T);
408-
return true;
402+
if(typeof(T).IsValueType)
403+
{
404+
throw new PyScopeException($"The value of the attribute '{name}' is None which cannot be convert to '{typeof(T).ToString()}'");
405+
}
406+
else
407+
{
408+
value = default(T);
409+
return true;
410+
}
409411
}
410412
value = pyObj.As<T>();
411413
return true;
@@ -427,7 +429,7 @@ private void Check()
427429
{
428430
if (isDisposed)
429431
{
430-
throw new PyScopeException( F2DC "'ScopeStorage' object has been disposed");
432+
throw new PyScopeException($"The scope of name '{Name}' object has been disposed");
431433
}
432434
}
433435

@@ -484,7 +486,7 @@ public PyScope Create(string name)
484486
}
485487
if (name != null && NamedScopes.ContainsKey(name))
486488
{
487-
throw new PyScopeException($"PyScope '{name}' has existed");
489+
throw new PyScopeException($"A scope of name '{name}' does already exist");
488490
}
489491
var scope = this.NewScope(name);
490492
scope.OnDispose += Remove;
@@ -507,14 +509,12 @@ public PyScope Get(string name)
507509
{
508510
return NamedScopes[name];
509511
}
510-
throw new PyScopeException($"PyScope '{name}' not exist");
512+
throw new PyScopeException($"There is no scope named '{name}' registered in this manager");
511513
}
512514

513-
public PyScope TryGet(string name)
515+
public bool TryGet(string name, out PyScope scope)
514516
{
515-
PyScope value;
516-
NamedScopes.TryGetValue(name, out value);
517-
return value;
517+
return NamedScopes.TryGetValue(name, out scope);
518518
}
519519

520520
public void Remove(PyScope scope)

0 commit comments

Comments
 (0)
0