FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/PythonEngine.cs at master · JakeJP/pythonnet · GitHub
JakeJP
/
pythonnet
Public
forked from
pythonnet/pythonnet
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
PythonEngine.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
672 lines (589 loc) · 23.4 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
PythonEngine.cs
Copy path
File metadata and controls
672 lines (589 loc) · 23.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
ComponentModel
;
using
System
.
Diagnostics
;
using
System
.
Linq
;
using
System
.
Reflection
;
using
System
.
Runtime
.
InteropServices
;
using
Python
.
Runtime
.
Native
;
namespace
Python
.
Runtime
{
/// <summary>
/// This class provides the public interface of the Python runtime.
/// </summary>
public
class
PythonEngine
:
IDisposable
{
private
static
DelegateManager
?
delegateManager
;
private
static
bool
initialized
;
private
static
IntPtr
_pythonHome
=
IntPtr
.
Zero
;
private
static
IntPtr
_programName
=
IntPtr
.
Zero
;
private
static
IntPtr
_pythonPath
=
IntPtr
.
Zero
;
private
static
InteropConfiguration
interopConfiguration
=
InteropConfiguration
.
MakeDefault
(
)
;
public
PythonEngine
(
)
{
Initialize
(
)
;
}
public
PythonEngine
(
params
string
[
]
args
)
{
Initialize
(
args
)
;
}
public
PythonEngine
(
IEnumerable
<
string
>
args
)
{
Initialize
(
args
)
;
}
public
void
Dispose
(
)
{
Shutdown
(
)
;
}
public
static
bool
IsInitialized
{
get
{
return
initialized
;
}
}
private
static
void
EnsureInitialized
(
)
{
if
(
!
IsInitialized
)
throw
new
InvalidOperationException
(
"Python must be initialized for this operation"
)
;
}
/// <summary>Set to <c>true</c> to enable GIL debugging assistance.</summary>
public
static
bool
DebugGIL
{
get
;
set
;
}
=
false
;
internal
static
DelegateManager
DelegateManager
{
get
{
if
(
delegateManager
==
null
)
{
throw
new
InvalidOperationException
(
"DelegateManager has not yet been initialized using Python.Runtime.PythonEngine.Initialize()."
)
;
}
return
delegateManager
;
}
}
public
static
InteropConfiguration
InteropConfiguration
{
get
=>
interopConfiguration
;
set
{
if
(
IsInitialized
)
throw
new
NotSupportedException
(
"Changing interop configuration when engine is running is not supported"
)
;
interopConfiguration
=
value
??
throw
new
ArgumentNullException
(
nameof
(
InteropConfiguration
)
)
;
}
}
public
static
string
ProgramName
{
get
{
IntPtr
p
=
Runtime
.
TryUsingDll
(
(
)
=>
Runtime
.
Py_GetProgramName
(
)
)
;
return
UcsMarshaler
.
PtrToPy3UnicodePy2String
(
p
)
??
""
;
}
set
{
Marshal
.
FreeHGlobal
(
_programName
)
;
_programName
=
Runtime
.
TryUsingDll
(
(
)
=>
UcsMarshaler
.
Py3UnicodePy2StringtoPtr
(
value
)
)
;
Runtime
.
Py_SetProgramName
(
_programName
)
;
}
}
public
static
string
PythonHome
{
get
{
EnsureInitialized
(
)
;
IntPtr
p
=
Runtime
.
TryUsingDll
(
(
)
=>
Runtime
.
Py_GetPythonHome
(
)
)
;
return
UcsMarshaler
.
PtrToPy3UnicodePy2String
(
p
)
??
""
;
}
set
{
// this value is null in the beginning
Marshal
.
FreeHGlobal
(
_pythonHome
)
;
_pythonHome
=
UcsMarshaler
.
Py3UnicodePy2StringtoPtr
(
value
)
;
Runtime
.
TryUsingDll
(
(
)
=>
Runtime
.
Py_SetPythonHome
(
_pythonHome
)
)
;
}
}
public
static
string
PythonPath
{
get
{
IntPtr
p
=
Runtime
.
TryUsingDll
(
(
)
=>
Runtime
.
Py_GetPath
(
)
)
;
return
UcsMarshaler
.
PtrToPy3UnicodePy2String
(
p
)
??
""
;
}
set
{
Marshal
.
FreeHGlobal
(
_pythonPath
)
;
_pythonPath
=
Runtime
.
TryUsingDll
(
(
)
=>
UcsMarshaler
.
Py3UnicodePy2StringtoPtr
(
value
)
)
;
Runtime
.
Py_SetPath
(
_pythonPath
)
;
}
}
public
static
Version
MinSupportedVersion
=>
new
(
3
,
7
)
;
public
static
Version
MaxSupportedVersion
=>
new
(
3
,
12
,
int
.
MaxValue
,
int
.
MaxValue
)
;
public
static
bool
IsSupportedVersion
(
Version
version
)
=>
version
>=
MinSupportedVersion
&&
version
<=
MaxSupportedVersion
;
public
static
string
Version
{
get
{
return
Marshal
.
PtrToStringAnsi
(
Runtime
.
Py_GetVersion
(
)
)
;
}
}
public
static
string
BuildInfo
{
get
{
return
Marshal
.
PtrToStringAnsi
(
Runtime
.
Py_GetBuildInfo
(
)
)
;
}
}
public
static
string
Platform
{
get
{
return
Marshal
.
PtrToStringAnsi
(
Runtime
.
Py_GetPlatform
(
)
)
;
}
}
public
static
string
Copyright
{
get
{
return
Marshal
.
PtrToStringAnsi
(
Runtime
.
Py_GetCopyright
(
)
)
;
}
}
public
static
string
Compiler
{
get
{
return
Marshal
.
PtrToStringAnsi
(
Runtime
.
Py_GetCompiler
(
)
)
;
}
}
/// <summary>
/// Set the NoSiteFlag to disable loading the site module.
/// Must be called before Initialize.
/// https://docs.python.org/3/c-api/init.html#c.Py_NoSiteFlag
/// </summary>
public
static
void
SetNoSiteFlag
(
)
{
Runtime
.
SetNoSiteFlag
(
)
;
}
public
static
int
RunSimpleString
(
string
code
)
{
return
Runtime
.
PyRun_SimpleString
(
code
)
;
}
public
static
void
Initialize
(
)
{
Initialize
(
setSysArgv
:
true
)
;
}
public
static
void
Initialize
(
bool
setSysArgv
=
true
,
bool
initSigs
=
false
)
{
Initialize
(
Enumerable
.
Empty
<
string
>
(
)
,
setSysArgv
:
setSysArgv
,
initSigs
:
initSigs
)
;
}
/// <summary>
/// Initialize Method
/// </summary>
/// <remarks>
/// Initialize the Python runtime. It is safe to call this method
/// more than once, though initialization will only happen on the
/// first call. It is *not* necessary to hold the Python global
/// interpreter lock (GIL) to call this method.
/// initSigs can be set to 1 to do default python signal configuration. This will override the way signals are handled by the application.
/// </remarks>
public
static
void
Initialize
(
IEnumerable
<
string
>
args
,
bool
setSysArgv
=
true
,
bool
initSigs
=
false
)
{
if
(
initialized
)
{
return
;
}
// Creating the delegateManager MUST happen before Runtime.Initialize
// is called. If it happens afterwards, DelegateManager's CodeGenerator
// throws an exception in its ctor. This exception is eaten somehow
// during an initial "import clr", and the world ends shortly thereafter.
// This is probably masking some bad mojo happening somewhere in Runtime.Initialize().
delegateManager
=
new
DelegateManager
(
)
;
Runtime
.
Initialize
(
initSigs
)
;
initialized
=
true
;
Exceptions
.
Clear
(
)
;
// Make sure we clean up properly on app domain unload.
AppDomain
.
CurrentDomain
.
DomainUnload
+=
OnDomainUnload
;
AppDomain
.
CurrentDomain
.
ProcessExit
+=
OnProcessExit
;
if
(
setSysArgv
)
{
Py
.
SetArgv
(
args
)
;
}
// Load the clr.py resource into the clr module
BorrowedReference
clr_dict
=
Runtime
.
PyModule_GetDict
(
ImportHook
.
ClrModuleReference
)
;
var
locals
=
new
PyDict
(
)
;
try
{
BorrowedReference
module
=
DefineModule
(
"clr._extras"
)
;
BorrowedReference
module_globals
=
Runtime
.
PyModule_GetDict
(
module
)
;
Assembly
assembly
=
Assembly
.
GetExecutingAssembly
(
)
;
// add the contents of clr.py to the module
string
clr_py
=
assembly
.
ReadStringResource
(
"clr.py"
)
;
Exec
(
clr_py
,
module_globals
,
locals
.
Reference
)
;
LoadSubmodule
(
module_globals
,
"clr.interop"
,
"interop.py"
)
;
LoadMixins
(
module_globals
)
;
// add the imported module to the clr module, and copy the API functions
// and decorators into the main clr module.
Runtime
.
PyDict_SetItemString
(
clr_dict
,
"_extras"
,
module
)
;
// append version
var
version
=
typeof
(
PythonEngine
)
.
Assembly
.
GetCustomAttribute
<
AssemblyInformationalVersionAttribute
>
(
)
.
InformationalVersion
;
using
var
versionObj
=
Runtime
.
PyString_FromString
(
version
)
;
Runtime
.
PyDict_SetItemString
(
clr_dict
,
"__version__"
,
versionObj
.
Borrow
(
)
)
;
using
var
keys
=
locals
.
Keys
(
)
;
foreach
(
PyObject
key
in
keys
)
{
if
(
!
key
.
ToString
(
)
!
.
StartsWith
(
"_"
)
)
{
using
PyObject
value
=
locals
[
key
]
;
Runtime
.
PyDict_SetItem
(
clr_dict
,
key
.
Reference
,
value
.
Reference
)
;
}
key
.
Dispose
(
)
;
}
}
finally
{
locals
.
Dispose
(
)
;
}
ImportHook
.
UpdateCLRModuleDict
(
)
;
}
static
BorrowedReference
DefineModule
(
string
name
)
{
var
module
=
Runtime
.
PyImport_AddModule
(
name
)
;
var
module_globals
=
Runtime
.
PyModule_GetDict
(
module
)
;
var
builtins
=
Runtime
.
PyEval_GetBuiltins
(
)
;
Runtime
.
PyDict_SetItemString
(
module_globals
,
"__builtins__"
,
builtins
)
;
return
module
;
}
static
void
LoadSubmodule
(
BorrowedReference
targetModuleDict
,
string
fullName
,
string
resourceName
)
{
string
?
memberName
=
fullName
.
AfterLast
(
'.'
)
;
Debug
.
Assert
(
memberName
!=
null
)
;
var
module
=
DefineModule
(
fullName
)
;
var
module_globals
=
Runtime
.
PyModule_GetDict
(
module
)
;
Assembly
assembly
=
Assembly
.
GetExecutingAssembly
(
)
;
string
pyCode
=
assembly
.
ReadStringResource
(
resourceName
)
;
Exec
(
pyCode
,
module_globals
,
module_globals
)
;
Runtime
.
PyDict_SetItemString
(
targetModuleDict
,
memberName
!
,
module
)
;
}
static
void
LoadMixins
(
BorrowedReference
targetModuleDict
)
{
foreach
(
string
nested
in
new
[
]
{
"collections"
}
)
{
LoadSubmodule
(
targetModuleDict
,
fullName
:
"clr._extras."
+
nested
,
resourceName
:
typeof
(
PythonEngine
)
.
Namespace
+
".Mixins."
+
nested
+
".py"
)
;
}
}
static
void
OnDomainUnload
(
object
_
,
EventArgs
__
)
{
Shutdown
(
)
;
}
static
void
OnProcessExit
(
object
_
,
EventArgs
__
)
{
Runtime
.
ProcessIsTerminating
=
true
;
Shutdown
(
)
;
}
/// <summary>
/// A helper to perform initialization from the context of an active
/// CPython interpreter process - this bootstraps the managed runtime
/// when it is imported by the CLR extension module.
/// </summary>
[
EditorBrowsable
(
EditorBrowsableState
.
Never
)
]
[
Obsolete
(
Util
.
InternalUseOnly
)
]
public
static
IntPtr
InitExt
(
)
{
try
{
if
(
Runtime
.
IsInitialized
)
{
var
builtins
=
Runtime
.
PyEval_GetBuiltins
(
)
;
var
runtimeError
=
Runtime
.
PyDict_GetItemString
(
builtins
,
"RuntimeError"
)
;
Exceptions
.
SetError
(
runtimeError
,
"Python.NET runtime is already initialized"
)
;
return
IntPtr
.
Zero
;
}
Runtime
.
HostedInPython
=
true
;
Initialize
(
setSysArgv
:
false
)
;
Finalizer
.
Instance
.
ErrorHandler
+=
AllowLeaksDuringShutdown
;
}
catch
(
PythonException
e
)
{
e
.
Restore
(
)
;
return
IntPtr
.
Zero
;
}
return
Python
.
Runtime
.
ImportHook
.
GetCLRModule
(
)
.
DangerousMoveToPointerOrNull
(
)
;
}
private
static
void
AllowLeaksDuringShutdown
(
object
sender
,
Finalizer
.
ErrorArgs
e
)
{
if
(
e
.
Error
is
RuntimeShutdownException
)
{
e
.
Handled
=
true
;
}
}
/// <summary>
/// Shutdown and release resources held by the Python runtime. The
/// Python runtime can no longer be used in the current process
/// after calling the Shutdown method.
/// </summary>
public
static
void
Shutdown
(
)
{
if
(
!
initialized
)
{
return
;
}
using
(
Py
.
GIL
(
)
)
{
if
(
Exceptions
.
ErrorOccurred
(
)
)
{
throw
new
InvalidOperationException
(
"Python error indicator is set"
,
innerException
:
PythonException
.
PeekCurrentOrNull
(
out
_
)
)
;
}
}
// If the shutdown handlers trigger a domain unload,
// don't call shutdown again.
AppDomain
.
CurrentDomain
.
DomainUnload
-=
OnDomainUnload
;
AppDomain
.
CurrentDomain
.
ProcessExit
-=
OnProcessExit
;
ExecuteShutdownHandlers
(
)
;
// Remember to shut down the runtime.
Runtime
.
Shutdown
(
)
;
initialized
=
false
;
InteropConfiguration
=
InteropConfiguration
.
MakeDefault
(
)
;
}
/// <summary>
/// Called when the engine is shut down.
///
/// Shutdown handlers are run in reverse order they were added, so that
/// resources available when running a shutdown handler are the same as
/// what was available when it was added.
/// </summary>
public
delegate
void
ShutdownHandler
(
)
;
static
readonly
List
<
ShutdownHandler
>
ShutdownHandlers
=
new
(
)
;
/// <summary>
/// Add a function to be called when the engine is shut down.
///
/// Shutdown handlers are executed in the opposite order they were
/// added, so that you can be sure that everything that was initialized
/// when you added the handler is still initialized when you need to shut
/// down.
///
/// If the same shutdown handler is added several times, it will be run
/// several times.
///
/// Don't add shutdown handlers while running a shutdown handler.
/// </summary>
public
static
void
AddShutdownHandler
(
ShutdownHandler
handler
)
{
ShutdownHandlers
.
Add
(
handler
)
;
}
/// <summary>
/// Remove a shutdown handler.
///
/// If the same shutdown handler is added several times, only the last
/// one is removed.
///
/// Don't remove shutdown handlers while running a shutdown handler.
/// </summary>
public
static
void
RemoveShutdownHandler
(
ShutdownHandler
handler
)
{
for
(
int
index
=
ShutdownHandlers
.
Count
-
1
;
index
>=
0
;
--
index
)
{
if
(
ShutdownHandlers
[
index
]
==
handler
)
{
ShutdownHandlers
.
RemoveAt
(
index
)
;
break
;
}
}
}
/// <summary>
/// Run all the shutdown handlers.
///
/// They're run in opposite order they were added.
/// </summary>
static
void
ExecuteShutdownHandlers
(
)
{
while
(
ShutdownHandlers
.
Count
>
0
)
{
var
handler
=
ShutdownHandlers
[
ShutdownHandlers
.
Count
-
1
]
;
ShutdownHandlers
.
RemoveAt
(
ShutdownHandlers
.
Count
-
1
)
;
handler
(
)
;
}
}
/// <summary>
/// AcquireLock Method
/// </summary>
/// <remarks>
/// Acquire the Python global interpreter lock (GIL). Managed code
/// *must* call this method before using any objects or calling any
/// methods on objects in the Python.Runtime namespace. The only
/// exception is PythonEngine.Initialize, which may be called without
/// first calling AcquireLock.
/// Each call to AcquireLock must be matched by a corresponding call
/// to ReleaseLock, passing the token obtained from AcquireLock.
/// For more information, see the "Extending and Embedding" section
/// of the Python documentation on www.python.org.
/// </remarks>
internal
static
PyGILState
AcquireLock
(
)
{
return
Runtime
.
PyGILState_Ensure
(
)
;
}
/// <summary>
/// ReleaseLock Method
/// </summary>
/// <remarks>
/// Release the Python global interpreter lock using a token obtained
/// from a previous call to AcquireLock.
/// For more information, see the "Extending and Embedding" section
/// of the Python documentation on www.python.org.
/// </remarks>
internal
static
void
ReleaseLock
(
PyGILState
gs
)
{
Runtime
.
PyGILState_Release
(
gs
)
;
}
/// <summary>
/// BeginAllowThreads Method
/// </summary>
/// <remarks>
/// Release the Python global interpreter lock to allow other threads
/// to run. This is equivalent to the Py_BEGIN_ALLOW_THREADS macro
/// provided by the C Python API.
/// For more information, see the "Extending and Embedding" section
/// of the Python documentation on www.python.org.
/// </remarks>
public
static
unsafe
IntPtr
BeginAllowThreads
(
)
{
return
(
IntPtr
)
Runtime
.
PyEval_SaveThread
(
)
;
}
/// <summary>
/// EndAllowThreads Method
/// </summary>
/// <remarks>
/// Re-aquire the Python global interpreter lock for the current
/// thread. This is equivalent to the Py_END_ALLOW_THREADS macro
/// provided by the C Python API.
/// For more information, see the "Extending and Embedding" section
/// of the Python documentation on www.python.org.
/// </remarks>
public
static
unsafe
void
EndAllowThreads
(
IntPtr
ts
)
{
Runtime
.
PyEval_RestoreThread
(
(
PyThreadState
*
)
ts
)
;
}
public
static
PyObject
Compile
(
string
code
,
string
filename
=
""
,
RunFlagType
mode
=
RunFlagType
.
File
)
{
var
flag
=
(
int
)
mode
;
NewReference
ptr
=
Runtime
.
Py_CompileString
(
code
,
filename
,
flag
)
;
PythonException
.
ThrowIfIsNull
(
ptr
)
;
return
ptr
.
MoveToPyObject
(
)
;
}
/// <summary>
/// Eval Method
/// </summary>
/// <remarks>
/// Evaluate a Python expression and returns the result.
/// It's a subset of Python eval function.
/// </remarks>
public
static
PyObject
Eval
(
string
code
,
PyDict
?
globals
=
null
,
PyObject
?
locals
=
null
)
{
PyObject
result
=
RunString
(
code
,
globals
.
BorrowNullable
(
)
,
locals
.
BorrowNullable
(
)
,
RunFlagType
.
Eval
)
;
return
result
;
}
/// <summary>
/// Exec Method
/// </summary>
/// <remarks>
/// Run a string containing Python code.
/// It's a subset of Python exec function.
/// </remarks>
public
static
void
Exec
(
string
code
,
PyDict
?
globals
=
null
,
PyObject
?
locals
=
null
)
{
using
PyObject
result
=
RunString
(
code
,
globals
.
BorrowNullable
(
)
,
locals
.
BorrowNullable
(
)
,
RunFlagType
.
File
)
;
if
(
result
.
obj
!=
Runtime
.
PyNone
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
/// <summary>
/// Exec Method
/// </summary>
/// <remarks>
/// Run a string containing Python code.
/// It's a subset of Python exec function.
/// </remarks>
internal
static
void
Exec
(
string
code
,
BorrowedReference
globals
,
BorrowedReference
locals
=
default
)
{
using
PyObject
result
=
RunString
(
code
,
globals
:
globals
,
locals
:
locals
,
RunFlagType
.
File
)
;
if
(
result
.
obj
!=
Runtime
.
PyNone
)
{
throw
PythonException
.
ThrowLastAsClrException
(
)
;
}
}
/// <summary>
/// Gets the Python thread ID.
/// </summary>
/// <returns>The Python thread ID.</returns>
public
static
ulong
GetPythonThreadID
(
)
{
using
PyObject
threading
=
Py
.
Import
(
"threading"
)
;
using
PyObject
id
=
threading
.
InvokeMethod
(
"get_ident"
)
;
return
id
.
As
<
ulong
>
(
)
;
}
/// <summary>
/// Interrupts the execution of a thread.
/// </summary>
/// <param name="pythonThreadID">The Python thread ID.</param>
/// <returns>The number of thread states modified; this is normally one, but will be zero if the thread id is not found.</returns>
public
static
int
Interrupt
(
ulong
pythonThreadID
)
{
if
(
RuntimeInformation
.
IsOSPlatform
(
OSPlatform
.
Windows
)
)
{
return
Runtime
.
PyThreadState_SetAsyncExcLLP64
(
(
uint
)
pythonThreadID
,
Exceptions
.
KeyboardInterrupt
)
;
}
return
Runtime
.
PyThreadState_SetAsyncExcLP64
(
pythonThreadID
,
Exceptions
.
KeyboardInterrupt
)
;
}
/// <summary>
/// RunString Method. Function has been deprecated and will be removed.
/// Use Exec/Eval/RunSimpleString instead.
/// </summary>
[
Obsolete
(
"RunString is deprecated and will be removed. Use Exec/Eval/RunSimpleString instead."
)
]
public
static
PyObject
RunString
(
string
code
,
PyDict
?
globals
=
null
,
PyObject
?
locals
=
null
)
{
return
RunString
(
code
,
globals
.
BorrowNullable
(
)
,
locals
.
BorrowNullable
(
)
,
RunFlagType
.
File
)
;
}
/// <summary>
/// Internal RunString Method.
/// </summary>
/// <remarks>
/// Run a string containing Python code. Returns the result of
/// executing the code string as a PyObject instance, or null if
/// an exception was raised.
/// </remarks>
internal
static
PyObject
RunString
(
string
code
,
BorrowedReference
globals
,
BorrowedReference
locals
,
RunFlagType
flag
)
{
if
(
code
is
null
)
throw
new
ArgumentNullException
(
nameof
(
code
)
)
;
NewReference
tempGlobals
=
default
;
if
(
globals
.
IsNull
)
{
globals
=
Runtime
.
PyEval_GetGlobals
(
)
;
if
(
globals
.
IsNull
)
{
tempGlobals
=
Runtime
.
PyDict_New
(
)
;
globals
=
tempGlobals
.
BorrowOrThrow
(
)
;
Runtime
.
PyDict_SetItem
(
globals
,
PyIdentifier
.
__builtins__
,
Runtime
.
PyEval_GetBuiltins
(
)
)
;
}
}
if
(
locals
==
null
)
{
locals
=
globals
;
}
try
{
NewReference
result
=
Runtime
.
PyRun_String
(
code
,
flag
,
globals
,
locals
)
;
PythonException
.
ThrowIfIsNull
(
result
)
;
return
result
.
MoveToPyObject
(
)
;
}
finally
{
tempGlobals
.
Dispose
(
)
;
}
}
}
public
enum
RunFlagType
:
int
{
Single
=
256
,
File
=
257
,
/* Py_file_input */
Eval
=
258
}
}
Back
|
FazBrowse Home
|
New Git URL