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

Skip to content

Commit c15555c

Browse files
vmuriartyagweb
authored andcommitted
format cleanup
1 parent add5ba8 commit c15555c

File tree

2 files changed

+51
-60
lines changed

2 files changed

+51
-60
lines changed

src/embed_tests/pyscope.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1+
using System;
12
using NUnit.Framework;
23
using Python.Runtime;
34

45
namespace Python.EmbeddingTest
56
{
6-
[TestFixture]
77
public class PyScopeTest
88
{
9-
PyScope ps;
10-
9+
private PyScope ps;
10+
1111
[SetUp]
1212
public void SetUp()
1313
{
1414
ps = Py.Session("test");
1515
}
1616

1717
[TearDown]
18-
public void TearDown()
18+
public void Dispose()
1919
{
2020
ps.Dispose();
2121
}
@@ -27,7 +27,7 @@ public void TearDown()
2727
public void TestEval()
2828
{
2929
ps.SetVariable("a", 1);
30-
int result = ps.Eval<int>("a+2");
30+
var result = ps.Eval<int>("a + 2");
3131
Assert.AreEqual(result, 3);
3232
}
3333

@@ -39,8 +39,8 @@ public void TestExec()
3939
{
4040
ps.SetVariable("bb", 100); //declare a global variable
4141
ps.SetVariable("cc", 10); //declare a local variable
42-
ps.Exec("aa=bb+cc+3");
43-
int result = ps.GetVariable<System.Int32>("aa");
42+
ps.Exec("aa = bb + cc + 3");
43+
var result = ps.GetVariable<int>("aa");
4444
Assert.AreEqual(result, 113);
4545
}
4646

@@ -53,7 +53,7 @@ public void TestCompileExpression()
5353
{
5454
ps.SetVariable("bb", 100); //declare a global variable
5555
ps.SetVariable("cc", 10); //declare a local variable
56-
var script = ps.Compile("bb+cc+3", "", RunFlagType.Eval);
56+
PyObject script = ps.Compile("bb + cc + 3", "", RunFlagType.Eval);
5757
var result = ps.Execute<int>(script);
5858
Assert.AreEqual(result, 113);
5959
}
@@ -68,14 +68,14 @@ public void TestCompileStatements()
6868
{
6969
ps.SetVariable("bb", 100); //declare a global variable
7070
ps.SetVariable("cc", 10); //declare a local variable
71-
var script = ps.Compile("aa=bb+cc+3", "", RunFlagType.File);
71+
PyObject script = ps.Compile("aa = bb + cc + 3", "", RunFlagType.File);
7272
ps.Execute(script);
73-
int result = ps.GetVariable<int>("aa");
73+
var result = ps.GetVariable<int>("aa");
7474
Assert.AreEqual(result, 113);
7575
}
7676

7777
/// <summary>
78-
/// Exec Python statements in a subscope of the session then discard it.
78+
/// Exec Python statements in a SubScope of the session then discard it.
7979
/// </summary>
8080
[Test]
8181
public void TestSubScope()
@@ -84,8 +84,8 @@ public void TestSubScope()
8484
ps.SetVariable("cc", 10); //declare a local variable
8585

8686
PyScope scope = ps.SubScope();
87-
scope.Exec("aa=bb+cc+3");
88-
int result = scope.GetVariable<System.Int32>("aa");
87+
scope.Exec("aa = bb + cc + 3");
88+
var result = scope.GetVariable<int>("aa");
8989
Assert.AreEqual(result, 113); //
9090
scope.Dispose();
9191

@@ -103,8 +103,8 @@ public void TestImport()
103103
Assert.IsTrue(ps.ContainsVariable("sys"));
104104

105105
ps.Exec("sys.attr1 = 2");
106-
int value1 = ps.Eval<int>("sys.attr1");
107-
int value2 = (int)sys.attr1.AsManagedObject(typeof(int));
106+
var value1 = ps.Eval<int>("sys.attr1");
107+
var value2 = (int)sys.attr1.AsManagedObject(typeof(int));
108108
Assert.AreEqual(value1, 2);
109109
Assert.AreEqual(value2, 2);
110110
}
@@ -135,8 +135,8 @@ public void TestSuspend()
135135
PythonEngine.RunSimpleString("import sys;");
136136
}
137137

138-
ps.Exec("aa=bb+cc+3");
139-
int result = ps.GetVariable<System.Int32>("aa");
138+
ps.Exec("aa = bb + cc + 3");
139+
var result = ps.GetVariable<int>("aa");
140140
Assert.AreEqual(result, 113);
141141
}
142142
}

src/runtime/pythonengine.cs

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,8 @@ public enum RunFlagType
527527
Eval = 258
528528
}
529529

530-
public class PySessionDisposedException: Exception
530+
public class PySessionDisposedException : Exception
531531
{
532-
533532
}
534533

535534
public class PyScope : IDisposable
@@ -564,7 +563,7 @@ public void ReleaseLock()
564563

565564
public void Dispose()
566565
{
567-
this.ReleaseLock();
566+
ReleaseLock();
568567
GC.SuppressFinalize(this);
569568
}
570569

@@ -584,7 +583,7 @@ public void Dispose()
584583

585584
private PyScope(GILState state)
586585
{
587-
this.isDisposed = false;
586+
isDisposed = false;
588587
this.state = state;
589588
globals = Runtime.PyDict_New();
590589
if (globals == IntPtr.Zero)
@@ -599,14 +598,14 @@ private PyScope(GILState state)
599598
}
600599

601600
internal PyScope(string name, GILState state)
602-
:this(state)
601+
: this(state)
603602
{
604603
this.state = state;
605604
this.name = name;
606605
Runtime.PyDict_SetItemString(
607-
globals, "__builtins__",
608-
Runtime.PyEval_GetBuiltins()
609-
);
606+
globals, "__builtins__",
607+
Runtime.PyEval_GetBuiltins()
608+
);
610609
}
611610

612611
internal PyScope(PyScope parent, GILState state)
@@ -639,29 +638,21 @@ internal PyScope(PyScope parent, GILState state)
639638
/// <summary>
640639
/// the dict for global variables
641640
/// </summary>
642-
public IntPtr globals
643-
{
644-
get;
645-
private set;
646-
}
641+
public IntPtr globals { get; private set; }
647642

648643
/// <summary>
649644
/// the dict for local variables
650645
/// </summary>
651-
public IntPtr locals
652-
{
653-
get;
654-
private set;
655-
}
646+
public IntPtr locals { get; private set; }
656647

657648
public PyScope SubScope()
658649
{
659-
return new PyScope(this, this.state);
650+
return new PyScope(this, state);
660651
}
661652

662653
public void Suspend()
663654
{
664-
this.state.ReleaseLock();
655+
state.ReleaseLock();
665656
}
666657

667658
/// <summary>
@@ -685,7 +676,7 @@ public PyObject Import(string name)
685676
/// </remarks>
686677
public PyObject ImportAs(string name, string asname)
687678
{
688-
this.AcquireLock();
679+
AcquireLock();
689680
PyObject module = PythonEngine.ImportModule(name);
690681
if (asname == null)
691682
{
@@ -699,7 +690,7 @@ public PyObject Execute(PyObject script)
699690
{
700691
IntPtr ptr = Runtime.PyEval_EvalCode(script.Handle, globals, locals);
701692
Runtime.CheckExceptionOccurred();
702-
if(ptr == Runtime.PyNone)
693+
if (ptr == Runtime.PyNone)
703694
{
704695
Runtime.XDecref(ptr);
705696
return null;
@@ -709,12 +700,12 @@ public PyObject Execute(PyObject script)
709700

710701
public T Execute<T>(PyObject script)
711702
{
712-
var pyObj = this.Execute(script);
713-
if(pyObj == null)
703+
PyObject pyObj = Execute(script);
704+
if (pyObj == null)
714705
{
715706
return default(T);
716707
}
717-
T obj = (T)pyObj.AsManagedObject(typeof(T));
708+
var obj = (T)pyObj.AsManagedObject(typeof(T));
718709
return obj;
719710
}
720711

@@ -726,7 +717,7 @@ public T Execute<T>(PyObject script)
726717
/// </remarks>
727718
public PyObject Compile(string code, string filename = "", RunFlagType mode = RunFlagType.File)
728719
{
729-
IntPtr flag = (IntPtr)mode;
720+
var flag = (IntPtr)mode;
730721
IntPtr ptr = Runtime.Py_CompileString(code, filename, flag);
731722
Runtime.CheckExceptionOccurred();
732723
return new PyObject(ptr);
@@ -741,7 +732,7 @@ public PyObject Compile(string code, string filename = "", RunFlagType mode = Ru
741732
/// </remarks>
742733
public PyObject Eval(string code)
743734
{
744-
this.AcquireLock();
735+
AcquireLock();
745736
var flag = (IntPtr)Runtime.Py_eval_input;
746737
IntPtr ptr = Runtime.PyRun_String(
747738
code, flag, globals, locals
@@ -759,7 +750,7 @@ public PyObject Eval(string code)
759750
public T Eval<T>(string code)
760751
{
761752
PyObject pyObj = Eval(code);
762-
T obj = (T)pyObj.AsManagedObject(typeof(T));
753+
var obj = (T)pyObj.AsManagedObject(typeof(T));
763754
return obj;
764755
}
765756

@@ -771,8 +762,8 @@ public T Eval<T>(string code)
771762
/// </remarks>
772763
public void Exec(string code)
773764
{
774-
this.AcquireLock();
775-
Exec(code, this.globals, this.locals);
765+
AcquireLock();
766+
Exec(code, globals, locals);
776767
}
777768

778769
private void Exec(string code, IntPtr _globals, IntPtr _locals)
@@ -798,7 +789,7 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
798789
/// </remarks>
799790
internal void SetGlobalVariable(string name, object value)
800791
{
801-
this.AcquireLock();
792+
AcquireLock();
802793
using (var pyKey = new PyString(name))
803794
{
804795
IntPtr _value = GetInstHandle(value);
@@ -819,7 +810,7 @@ internal void SetGlobalVariable(string name, object value)
819810
/// </remarks>
820811
internal void RemoveGlobalVariable(string name)
821812
{
822-
this.AcquireLock();
813+
AcquireLock();
823814
using (var pyKey = new PyString(name))
824815
{
825816
int r = Runtime.PyObject_DelItem(globals, pyKey.obj);
@@ -839,7 +830,7 @@ internal void RemoveGlobalVariable(string name)
839830
/// </remarks>
840831
public void SetVariable(string name, object value)
841832
{
842-
this.AcquireLock();
833+
AcquireLock();
843834
using (var pyKey = new PyString(name))
844835
{
845836
IntPtr _value = GetInstHandle(value);
@@ -860,7 +851,7 @@ public void SetVariable(string name, object value)
860851
/// </remarks>
861852
public void RemoveVariable(string name)
862853
{
863-
this.AcquireLock();
854+
AcquireLock();
864855
using (var pyKey = new PyString(name))
865856
{
866857
int r = Runtime.PyObject_DelItem(locals, pyKey.obj);
@@ -879,7 +870,7 @@ public void RemoveVariable(string name)
879870
/// </remarks>
880871
public bool ContainsVariable(string name)
881872
{
882-
this.AcquireLock();
873+
AcquireLock();
883874
using (var pyKey = new PyString(name))
884875
{
885876
if (Runtime.PyMapping_HasKey(locals, pyKey.obj) != 0)
@@ -899,7 +890,7 @@ public bool ContainsVariable(string name)
899890
/// </remarks>
900891
public PyObject GetVariable(string name)
901892
{
902-
this.AcquireLock();
893+
AcquireLock();
903894
using (var pyKey = new PyString(name))
904895
{
905896
IntPtr op;
@@ -925,7 +916,7 @@ public PyObject GetVariable(string name)
925916

926917
public T GetVariable<T>(string name)
927918
{
928-
PyObject obj = this.GetVariable(name);
919+
PyObject obj = GetVariable(name);
929920
return (T)obj.AsManagedObject(typeof(T));
930921
}
931922

@@ -938,29 +929,29 @@ private static IntPtr GetInstHandle(object value)
938929
}
939930
else
940931
{
941-
var ptr = Converter.ToPython(value, value.GetType());
932+
IntPtr ptr = Converter.ToPython(value, value.GetType());
942933
return ptr;
943934
}
944935
}
945936

946937
private void AcquireLock()
947938
{
948-
if(isDisposed)
939+
if (isDisposed)
949940
{
950941
throw new PySessionDisposedException();
951942
}
952-
this.state.AcquireLock();
943+
state.AcquireLock();
953944
}
954945

955946
public virtual void Dispose()
956947
{
957-
if(isDisposed)
948+
if (isDisposed)
958949
{
959950
return;
960951
}
961952
Runtime.XDecref(globals);
962953
Runtime.XDecref(locals);
963-
if (this.parent == null)
954+
if (parent == null)
964955
{
965956
Py.RemoveSession(name);
966957
}
@@ -999,7 +990,7 @@ public static PyScope Session(string name)
999990
{
1000991
PythonEngine.Initialize();
1001992
}
1002-
if(Sessions.ContainsKey(name))
993+
if (Sessions.ContainsKey(name))
1003994
{
1004995
return Sessions[name];
1005996
}

0 commit comments

Comments
 (0)