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

Commitc7c7379

Browse files
author
jbw3
committed
Addressing code review comments
1 parentcefba0e commitc7c7379

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

‎README.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -517,17 +517,9 @@ which may be called at startup without having acquired the GIL.
517517

518518
When finished using Python APIs, managed code must call a corresponding
519519
`PythonEngine.ReleaseLock` to release the GIL and allow other threads
520-
to use Python:
520+
to use Python.
521521

522-
```csharp
523-
IntPtrgilState=PythonEngine.AcquireLock();
524-
525-
PythonEngine.Exec("doStuff()");
526-
527-
PythonEngine.ReleaseLock(gilState);
528-
```
529-
530-
A`using` statement may also be used to acquire and release the GIL:
522+
A`using` statement may be used to acquire and release the GIL:
531523

532524
```csharp
533525
using (Py.GIL())
@@ -561,9 +553,9 @@ public class Person
561553
```
562554

563555
In order to pass a C# object to the Python runtime, it must be converted to a
564-
`PyObject`. This is done using the`ToPython` extension method. The`PyObject`
565-
may then beadded to adictionary of local variables and passed to the
566-
`PythonEngine.Exec` function:
556+
`PyObject`. This is done using the`ToPython()` extension method. The`PyObject`
557+
may then beset as avariable in a`PyScope`. Code executed from the scope
558+
will have access to the variable:
567559

568560
```csharp
569561
// create a person object
@@ -572,16 +564,19 @@ Person person = new Person("John", "Smith");
572564
// acquire the GIL before using the Python interpreter
573565
using (Py.GIL())
574566
{
575-
// convert the Person object to a PyObject
576-
PyObjectpyPerson=person.ToPython();
567+
// create a Python scope
568+
using (PyScopescope=Py.CreateScope())
569+
{
570+
// convert the Person object to a PyObject
571+
PyObjectpyPerson=person.ToPython();
577572

578-
// create a Python variable "person"
579-
PyDictlocals=newPyDict();
580-
locals["person"]=pyPerson;
573+
// create a Python variable "person"
574+
scope.Set("person",pyPerson);
581575

582-
// the person object may now be used in Python
583-
stringcode="fullName = person.FirstName + ' ' + person.LastName";
584-
PythonEngine.Exec(code,null,locals.Handle);
576+
// the person object may now be used in Python
577+
stringcode="fullName = person.FirstName + ' ' + person.LastName";
578+
scope.Exec(code);
579+
}
585580
}
586581
```
587582

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp