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

Commit2c3db3d

Browse files
committed
updated according to filmor's comments
1 parentebf5a2b commit2c3db3d

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-
PyScopescope=Py.CreateScope();
196-
scope.Import(ps,"ps");
197-
198-
scope.Exec("aa = ps.bb + ps.cc + 3");
199-
varresult=scope.GetVariable<int>("aa");
200-
Assert.AreEqual(113,result);
201-
202-
scope.Dispose();
195+
using(varscope=Py.CreateScope())
196+
{
197+
scope.Import(ps,"ps");
198+
scope.Exec("aa = ps.bb + ps.cc + 3");
199+
varresult=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-
PyScopescope=ps.NewScope();
221-
222-
scope.Exec("aa = bb + cc + 3");
223-
varresult=scope.GetVariable<int>("aa");
224-
Assert.AreEqual(113,result);
225-
226-
scope.Dispose();
219+
using(varscope=ps.NewScope())
220+
{
221+
scope.Exec("aa = bb + cc + 3");
222+
varresult=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-
PyScopescope=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-
dynamicfunc2=scope.GetVariable("func2");
254-
255-
varresult1=func2().As<int>();
256-
Assert.AreEqual(0,result1);
257-
258-
scope.SetVariable("cc",20);//it has no effect on the globals of 'func1'
259-
varresult2=func2().As<int>();
260-
Assert.AreEqual(-10,result2);
261-
scope.SetVariable("cc",10);//rollback
262-
263-
ps.SetVariable("cc",20);
264-
varresult3=func2().As<int>();
265-
Assert.AreEqual(10,result3);
266-
ps.SetVariable("cc",10);//rollback
267-
268-
scope.Dispose();
245+
using(PyScopescope=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+
dynamicfunc2=scope.GetVariable("func2");
252+
253+
varresult1=func2().As<int>();
254+
Assert.AreEqual(0,result1);
255+
256+
scope.SetVariable("cc",20);//it has no effect on the globals of 'func1'
257+
varresult2=func2().As<int>();
258+
Assert.AreEqual(-10,result2);
259+
scope.SetVariable("cc",10);//rollback
260+
261+
ps.SetVariable("cc",20);
262+
varresult3=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-
varscope=Py.CreateScope();
284-
scope.ImportAll("test");
285-
//scope.ImportModule("test");
280+
using(varscope=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
vara2=ps.GetVariable<int>("ee");
307306
Assert.AreEqual(220,a2);
308307

309-
varitem=ps.Variables();
310-
item["ee"]=newPyInt(230);
311-
item.Dispose();
308+
using(varitem=ps.Variables())
309+
{
310+
item["ee"]=newPyInt(230);
311+
}
312312
vara3=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
internalreadonlyPyScopeManagerManager;
3939

4040
publiceventAction<PyScope>OnDispose;
41-
42-
publicPyScope(IntPtrptr,PyScopeManagermanager)
41+
42+
internalPyScope(IntPtrptr,PyScopeManagermanager)
4343
{
4444
if(Runtime.PyObject_Type(ptr)!=Runtime.PyModuleType)
4545
{
4646
thrownewPyScopeException("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
publicdynamicImport(stringname,stringasname=null)
8181
{
8282
Check();
83-
if(asname==null)
83+
if(String.IsNullOrEmpty(asname))
8484
{
8585
asname=name;
8686
}
87-
varscope=Manager.TryGet(name);
88-
if(scope!=null)
87+
PyScopescope;
88+
Manager.TryGet(name,outscope);
89+
if(scope!=null)
8990
{
9091
Import(scope,asname);
9192
returnscope;
9293
}
93-
PyObjectmodule=PythonEngine.ImportModule(name);
94-
Import(module,asname);
95-
returnmodule;
94+
else
95+
{
96+
PyObjectmodule=PythonEngine.ImportModule(name);
97+
Import(module,asname);
98+
returnmodule;
99+
}
96100
}
97101

98102
publicvoidImport(PyScopescope,stringasname)
@@ -109,7 +113,7 @@ public void Import(PyScope scope, string asname)
109113
/// </remarks>
110114
publicvoidImport(PyObjectmodule,stringasname=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
publicvoidImportAll(stringname)
120124
{
121-
varscope=Manager.TryGet(name);
122-
if(scope!=null)
125+
PyScopescope;
126+
Manager.TryGet(name,outscope);
127+
if(scope!=null)
123128
{
124129
ImportAll(scope);
125130
return;
126131
}
127-
PyObjectmodule=PythonEngine.ImportModule(name);
128-
ImportAll(module);
132+
else
133+
{
134+
PyObjectmodule=PythonEngine.ImportModule(name);
135+
ImportAll(module);
136+
}
129137
}
130138

131139
publicvoidImportAll(PyScopescope)
@@ -323,27 +331,13 @@ public bool ContainsVariable(string name)
323331
/// </remarks>
324332
publicPyObjectGetVariable(stringname)
325333
{
326-
Check();
327-
using(varpyKey=newPyString(name))
334+
PyObjectscope;
335+
varstate=TryGetVariable(name,outscope);
336+
if(!state)
328337
{
329-
if(Runtime.PyMapping_HasKey(variables,pyKey.obj)!=0)
330-
{
331-
IntPtrop=Runtime.PyObject_GetItem(variables,pyKey.obj);
332-
if(op==IntPtr.Zero)
333-
{
334-
thrownewPythonException();
335-
}
336-
if(op==Runtime.PyNone)
337-
{
338-
returnnull;
339-
}
340-
returnnewPyObject(op);
341-
}
342-
else
343-
{
344-
thrownewPyScopeException(String.Format("'ScopeStorage' object has no attribute '{0}'",name));
345-
}
338+
thrownewPyScopeException($"The scope of name '{Name}' has no attribute '{name}'");
346339
}
340+
returnscope;
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
returntrue;
372367
}
@@ -401,11 +396,18 @@ public bool TryGetVariable<T>(string name, out T value)
401396
{
402397
value=default(T);
403398
returnfalse;
404-
}
399+
}
405400
if(pyObj==null)
406401
{
407-
value=default(T);
408-
returntrue;
402+
if(typeof(T).IsValueType)
403+
{
404+
thrownewPyScopeException($"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+
returntrue;
410+
}
409411
}
410412
value=pyObj.As<T>();
411413
returntrue;
@@ -427,7 +429,7 @@ private void Check()
427429
{
428430
if(isDisposed)
429431
{
430-
thrownewPyScopeException("'ScopeStorage' object has been disposed");
432+
thrownewPyScopeException($"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-
thrownewPyScopeException($"PyScope'{name}'has existed");
489+
thrownewPyScopeException($"A scope of name'{name}'does already exist");
488490
}
489491
varscope=this.NewScope(name);
490492
scope.OnDispose+=Remove;
@@ -507,14 +509,12 @@ public PyScope Get(string name)
507509
{
508510
returnNamedScopes[name];
509511
}
510-
thrownewPyScopeException($"PyScope'{name}'not exist");
512+
thrownewPyScopeException($"There is no scope named'{name}'registered in this manager");
511513
}
512514

513-
publicPyScopeTryGet(stringname)
515+
publicboolTryGet(stringname,outPyScopescope)
514516
{
515-
PyScopevalue;
516-
NamedScopes.TryGetValue(name,outvalue);
517-
returnvalue;
517+
returnNamedScopes.TryGetValue(name,outscope);
518518
}
519519

520520
publicvoidRemove(PyScopescope)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp