@@ -527,9 +527,8 @@ public enum RunFlagType
527
527
Eval = 258
528
528
}
529
529
530
- public class PySessionDisposedException : Exception
530
+ public class PySessionDisposedException : Exception
531
531
{
532
-
533
532
}
534
533
535
534
public class PyScope : IDisposable
@@ -564,7 +563,7 @@ public void ReleaseLock()
564
563
565
564
public void Dispose ( )
566
565
{
567
- this . ReleaseLock ( ) ;
566
+ ReleaseLock ( ) ;
568
567
GC . SuppressFinalize ( this ) ;
569
568
}
570
569
@@ -584,7 +583,7 @@ public void Dispose()
584
583
585
584
private PyScope ( GILState state )
586
585
{
587
- this . isDisposed = false ;
586
+ isDisposed = false ;
588
587
this . state = state ;
589
588
globals = Runtime . PyDict_New ( ) ;
590
589
if ( globals == IntPtr . Zero )
@@ -599,14 +598,14 @@ private PyScope(GILState state)
599
598
}
600
599
601
600
internal PyScope ( string name , GILState state )
602
- : this ( state )
601
+ : this ( state )
603
602
{
604
603
this . state = state ;
605
604
this . name = name ;
606
605
Runtime . PyDict_SetItemString (
607
- globals , "__builtins__" ,
608
- Runtime . PyEval_GetBuiltins ( )
609
- ) ;
606
+ globals , "__builtins__" ,
607
+ Runtime . PyEval_GetBuiltins ( )
608
+ ) ;
610
609
}
611
610
612
611
internal PyScope ( PyScope parent , GILState state )
@@ -639,29 +638,21 @@ internal PyScope(PyScope parent, GILState state)
639
638
/// <summary>
640
639
/// the dict for global variables
641
640
/// </summary>
642
- public IntPtr globals
643
- {
644
- get ;
645
- private set ;
646
- }
641
+ public IntPtr globals { get ; private set ; }
647
642
648
643
/// <summary>
649
644
/// the dict for local variables
650
645
/// </summary>
651
- public IntPtr locals
652
- {
653
- get ;
654
- private set ;
655
- }
646
+ public IntPtr locals { get ; private set ; }
656
647
657
648
public PyScope SubScope ( )
658
649
{
659
- return new PyScope ( this , this . state ) ;
650
+ return new PyScope ( this , state ) ;
660
651
}
661
652
662
653
public void Suspend ( )
663
654
{
664
- this . state . ReleaseLock ( ) ;
655
+ state . ReleaseLock ( ) ;
665
656
}
666
657
667
658
/// <summary>
@@ -685,7 +676,7 @@ public PyObject Import(string name)
685
676
/// </remarks>
686
677
public PyObject ImportAs ( string name , string asname )
687
678
{
688
- this . AcquireLock ( ) ;
679
+ AcquireLock ( ) ;
689
680
PyObject module = PythonEngine . ImportModule ( name ) ;
690
681
if ( asname == null )
691
682
{
@@ -699,7 +690,7 @@ public PyObject Execute(PyObject script)
699
690
{
700
691
IntPtr ptr = Runtime . PyEval_EvalCode ( script . Handle , globals , locals ) ;
701
692
Runtime . CheckExceptionOccurred ( ) ;
702
- if ( ptr == Runtime . PyNone )
693
+ if ( ptr == Runtime . PyNone )
703
694
{
704
695
Runtime . XDecref ( ptr ) ;
705
696
return null ;
@@ -709,12 +700,12 @@ public PyObject Execute(PyObject script)
709
700
710
701
public T Execute < T > ( PyObject script )
711
702
{
712
- var pyObj = this . Execute ( script ) ;
713
- if ( pyObj == null )
703
+ PyObject pyObj = Execute ( script ) ;
704
+ if ( pyObj == null )
714
705
{
715
706
return default ( T ) ;
716
707
}
717
- T obj = ( T ) pyObj . AsManagedObject ( typeof ( T ) ) ;
708
+ var obj = ( T ) pyObj . AsManagedObject ( typeof ( T ) ) ;
718
709
return obj ;
719
710
}
720
711
@@ -726,7 +717,7 @@ public T Execute<T>(PyObject script)
726
717
/// </remarks>
727
718
public PyObject Compile ( string code , string filename = "" , RunFlagType mode = RunFlagType . File )
728
719
{
729
- IntPtr flag = ( IntPtr ) mode ;
720
+ var flag = ( IntPtr ) mode ;
730
721
IntPtr ptr = Runtime . Py_CompileString ( code , filename , flag ) ;
731
722
Runtime . CheckExceptionOccurred ( ) ;
732
723
return new PyObject ( ptr ) ;
@@ -741,7 +732,7 @@ public PyObject Compile(string code, string filename = "", RunFlagType mode = Ru
741
732
/// </remarks>
742
733
public PyObject Eval ( string code )
743
734
{
744
- this . AcquireLock ( ) ;
735
+ AcquireLock ( ) ;
745
736
var flag = ( IntPtr ) Runtime . Py_eval_input ;
746
737
IntPtr ptr = Runtime . PyRun_String (
747
738
code , flag , globals , locals
@@ -759,7 +750,7 @@ public PyObject Eval(string code)
759
750
public T Eval < T > ( string code )
760
751
{
761
752
PyObject pyObj = Eval ( code ) ;
762
- T obj = ( T ) pyObj . AsManagedObject ( typeof ( T ) ) ;
753
+ var obj = ( T ) pyObj . AsManagedObject ( typeof ( T ) ) ;
763
754
return obj ;
764
755
}
765
756
@@ -771,8 +762,8 @@ public T Eval<T>(string code)
771
762
/// </remarks>
772
763
public void Exec ( string code )
773
764
{
774
- this . AcquireLock ( ) ;
775
- Exec ( code , this . globals , this . locals ) ;
765
+ AcquireLock ( ) ;
766
+ Exec ( code , globals , locals ) ;
776
767
}
777
768
778
769
private void Exec ( string code , IntPtr _globals , IntPtr _locals )
@@ -798,7 +789,7 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
798
789
/// </remarks>
799
790
internal void SetGlobalVariable ( string name , object value )
800
791
{
801
- this . AcquireLock ( ) ;
792
+ AcquireLock ( ) ;
802
793
using ( var pyKey = new PyString ( name ) )
803
794
{
804
795
IntPtr _value = GetInstHandle ( value ) ;
@@ -819,7 +810,7 @@ internal void SetGlobalVariable(string name, object value)
819
810
/// </remarks>
820
811
internal void RemoveGlobalVariable ( string name )
821
812
{
822
- this . AcquireLock ( ) ;
813
+ AcquireLock ( ) ;
823
814
using ( var pyKey = new PyString ( name ) )
824
815
{
825
816
int r = Runtime . PyObject_DelItem ( globals , pyKey . obj ) ;
@@ -839,7 +830,7 @@ internal void RemoveGlobalVariable(string name)
839
830
/// </remarks>
840
831
public void SetVariable ( string name , object value )
841
832
{
842
- this . AcquireLock ( ) ;
833
+ AcquireLock ( ) ;
843
834
using ( var pyKey = new PyString ( name ) )
844
835
{
845
836
IntPtr _value = GetInstHandle ( value ) ;
@@ -860,7 +851,7 @@ public void SetVariable(string name, object value)
860
851
/// </remarks>
861
852
public void RemoveVariable ( string name )
862
853
{
863
- this . AcquireLock ( ) ;
854
+ AcquireLock ( ) ;
864
855
using ( var pyKey = new PyString ( name ) )
865
856
{
866
857
int r = Runtime . PyObject_DelItem ( locals , pyKey . obj ) ;
@@ -879,7 +870,7 @@ public void RemoveVariable(string name)
879
870
/// </remarks>
880
871
public bool ContainsVariable ( string name )
881
872
{
882
- this . AcquireLock ( ) ;
873
+ AcquireLock ( ) ;
883
874
using ( var pyKey = new PyString ( name ) )
884
875
{
885
876
if ( Runtime . PyMapping_HasKey ( locals , pyKey . obj ) != 0 )
@@ -899,7 +890,7 @@ public bool ContainsVariable(string name)
899
890
/// </remarks>
900
891
public PyObject GetVariable ( string name )
901
892
{
902
- this . AcquireLock ( ) ;
893
+ AcquireLock ( ) ;
903
894
using ( var pyKey = new PyString ( name ) )
904
895
{
905
896
IntPtr op ;
@@ -925,7 +916,7 @@ public PyObject GetVariable(string name)
925
916
926
917
public T GetVariable < T > ( string name )
927
918
{
928
- PyObject obj = this . GetVariable ( name ) ;
919
+ PyObject obj = GetVariable ( name ) ;
929
920
return ( T ) obj . AsManagedObject ( typeof ( T ) ) ;
930
921
}
931
922
@@ -938,29 +929,29 @@ private static IntPtr GetInstHandle(object value)
938
929
}
939
930
else
940
931
{
941
- var ptr = Converter . ToPython ( value , value . GetType ( ) ) ;
932
+ IntPtr ptr = Converter . ToPython ( value , value . GetType ( ) ) ;
942
933
return ptr ;
943
934
}
944
935
}
945
936
946
937
private void AcquireLock ( )
947
938
{
948
- if ( isDisposed )
939
+ if ( isDisposed )
949
940
{
950
941
throw new PySessionDisposedException ( ) ;
951
942
}
952
- this . state . AcquireLock ( ) ;
943
+ state . AcquireLock ( ) ;
953
944
}
954
945
955
946
public virtual void Dispose ( )
956
947
{
957
- if ( isDisposed )
948
+ if ( isDisposed )
958
949
{
959
950
return ;
960
951
}
961
952
Runtime . XDecref ( globals ) ;
962
953
Runtime . XDecref ( locals ) ;
963
- if ( this . parent == null )
954
+ if ( parent == null )
964
955
{
965
956
Py . RemoveSession ( name ) ;
966
957
}
@@ -999,7 +990,7 @@ public static PyScope Session(string name)
999
990
{
1000
991
PythonEngine . Initialize ( ) ;
1001
992
}
1002
- if ( Sessions . ContainsKey ( name ) )
993
+ if ( Sessions . ContainsKey ( name ) )
1003
994
{
1004
995
return Sessions [ name ] ;
1005
996
}
0 commit comments