@@ -511,16 +511,6 @@ public void Dispose()
511
511
512
512
private GILState state ;
513
513
514
- /// <summary>
515
- /// the dict for global variables
516
- /// </summary>
517
- private IntPtr globals ;
518
-
519
- /// <summary>
520
- /// the dict for local variables
521
- /// </summary>
522
- private IntPtr locals ;
523
-
524
514
internal PyScope ( )
525
515
: this ( null , new GILState ( ) )
526
516
{
@@ -536,18 +526,52 @@ internal PyScope(PyScope parent, GILState state)
536
526
{
537
527
throw new PythonException ( ) ;
538
528
}
539
- Runtime . PyDict_SetItemString (
540
- globals , "__builtins__" ,
541
- Runtime . PyEval_GetBuiltins ( )
542
- ) ;
543
529
locals = Runtime . PyDict_New ( ) ;
544
530
if ( locals == IntPtr . Zero )
545
531
{
546
532
throw new PythonException ( ) ;
547
533
}
534
+ if ( parent == null )
535
+ {
536
+ Runtime . PyDict_SetItemString (
537
+ globals , "__builtins__" ,
538
+ Runtime . PyEval_GetBuiltins ( )
539
+ ) ;
540
+ }
541
+ else
542
+ {
543
+ int result = Runtime . PyDict_Update ( globals , parent . globals ) ;
544
+ if ( result < 0 )
545
+ {
546
+ throw new PythonException ( ) ;
547
+ }
548
+ result = Runtime . PyDict_Update ( globals , parent . locals ) ;
549
+ if ( result < 0 )
550
+ {
551
+ throw new PythonException ( ) ;
552
+ }
553
+ }
554
+ }
555
+
556
+ /// <summary>
557
+ /// the dict for global variables
558
+ /// </summary>
559
+ public IntPtr globals
560
+ {
561
+ get ;
562
+ private set ;
563
+ }
564
+
565
+ /// <summary>
566
+ /// the dict for local variables
567
+ /// </summary>
568
+ public IntPtr locals
569
+ {
570
+ get ;
571
+ private set ;
548
572
}
549
573
550
- public PyScope Scope ( )
574
+ public PyScope SubScope ( )
551
575
{
552
576
return new PyScope ( this , this . state ) ;
553
577
}
@@ -558,18 +582,21 @@ public void Suspend()
558
582
}
559
583
560
584
/// <summary>
561
- /// ImportModule Method
585
+ /// Import Method
562
586
/// </summary>
563
587
/// <remarks>
564
- /// Given a fully-qualified module or package name, import the
565
- /// module , add it to the global variable dict and return the resulting module object as a PyObject
566
- /// or null if an exception is raised.
588
+ /// The import .. as .. statement in Python.
589
+ /// Import a module ,add it to the local variable dict and return the resulting module object as a PyObject.
567
590
/// </remarks>
568
- public PyObject Import ( string name )
591
+ public PyObject Import ( string name , string asname = null )
569
592
{
570
593
this . state . AcquireLock ( ) ;
571
594
PyObject module = PythonEngine . ImportModule ( name ) ;
572
- SetGlobal ( name , module ) ;
595
+ if ( asname == null )
596
+ {
597
+ asname = name ;
598
+ }
599
+ SetLocal ( asname , module ) ;
573
600
return module ;
574
601
}
575
602
@@ -592,28 +619,22 @@ public PyObject Eval(string code)
592
619
}
593
620
594
621
/// <summary>
595
- /// ExecIn Method
622
+ /// Exec Method
596
623
/// </summary>
597
624
/// <remarks>
598
- /// Evaluate a Python script and save its local variables in local variable dict.
625
+ /// Evaluate a Python script and save its local variables in the current local variable dict.
599
626
/// </remarks>
600
- public void ExecIn ( string code )
627
+ public void Exec ( string code )
601
628
{
602
- Exec ( code , locals ) ;
629
+ this . state . AcquireLock ( ) ;
630
+ Exec ( code , this . globals , this . locals ) ;
603
631
}
604
-
605
- /// <summary>
606
- /// ExecIn Method
607
- /// </summary>
608
- /// <remarks>
609
- /// Evaluate a Python script and save its local variables in local variable dict.
610
- /// </remarks>
611
- private void Exec ( string code , IntPtr _locals )
632
+
633
+ private void Exec ( string code , IntPtr _globals , IntPtr _locals )
612
634
{
613
- this . state . AcquireLock ( ) ;
614
635
var flag = ( IntPtr ) Runtime . Py_file_input ;
615
636
IntPtr ptr = Runtime . PyRun_String (
616
- code , flag , globals , locals
637
+ code , flag , _globals , _locals
617
638
) ;
618
639
Py . Throw ( ) ;
619
640
if ( ptr != Runtime . PyNone )
@@ -622,30 +643,7 @@ private void Exec(string code, IntPtr _locals)
622
643
}
623
644
Runtime . XDecref ( ptr ) ;
624
645
}
625
-
626
- /// <summary>
627
- /// Exec Method
628
- /// </summary>
629
- /// <remarks>
630
- /// Evaluate a Python script and discard its local variables.
631
- /// </remarks>
632
- public void Exec ( string code )
633
- {
634
- this . state . AcquireLock ( ) ;
635
- IntPtr _locals = Runtime . PyDict_New ( ) ;
636
- if ( _locals == IntPtr . Zero )
637
- {
638
- throw new PythonException ( ) ;
639
- }
640
- int result = Runtime . PyDict_Update ( _locals , locals ) ;
641
- if ( result < 0 )
642
- {
643
- throw new PythonException ( ) ;
644
- }
645
- Exec ( code , _locals ) ;
646
- Runtime . XDecref ( _locals ) ;
647
- }
648
-
646
+
649
647
/// <summary>
650
648
/// SetGlobal Method
651
649
/// </summary>
0 commit comments