FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/extensiontype.cs at skip-while-initializing · Peter-ouyang/pythonnet · GitHub
Peter-ouyang
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
extensiontype.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
104 lines (85 loc) · 3.51 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
extensiontype.cs
Copy path
File metadata and controls
104 lines (85 loc) · 3.51 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
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
System
.
Runtime
.
InteropServices
;
namespace
Python
.
Runtime
{
/// <summary>
/// Base class for extensions whose instances *share* a single Python
/// type object, such as the types that represent CLR methods, fields,
/// etc. Instances implemented by this class do not support sub-typing.
/// </summary>
[
Serializable
]
internal
abstract
class
ExtensionType
:
ManagedType
{
public
virtual
NewReference
Alloc
(
)
{
// Create a new PyObject whose type is a generated type that is
// implemented by the particular concrete ExtensionType subclass.
// The Python instance object is related to an instance of a
// particular concrete subclass with a hidden CLR gchandle.
BorrowedReference
tp
=
TypeManager
.
GetTypeReference
(
GetType
(
)
)
;
//int rc = (int)Util.ReadIntPtr(tp, TypeOffset.ob_refcnt);
//if (rc > 1050)
//{
// DebugUtil.Print("tp is: ", tp);
// DebugUtil.DumpType(tp);
//}
NewReference
py
=
Runtime
.
PyType_GenericAlloc
(
tp
,
0
)
;
#if
DEBUG
GetGCHandle
(
py
.
BorrowOrThrow
(
)
,
tp
,
out
var
existing
)
;
System
.
Diagnostics
.
Debug
.
Assert
(
existing
==
IntPtr
.
Zero
)
;
#endif
SetupGc
(
py
.
Borrow
(
)
,
tp
)
;
return
py
;
}
public
PyObject
AllocObject
(
)
=>
new
PyObject
(
Alloc
(
)
.
Steal
(
)
)
;
// "borrowed" references
internal
static
readonly
HashSet
<
IntPtr
>
loadedExtensions
=
new
(
)
;
void
SetupGc
(
BorrowedReference
ob
,
BorrowedReference
tp
)
{
GCHandle
gc
=
GCHandle
.
Alloc
(
this
)
;
InitGCHandle
(
ob
,
tp
,
gc
)
;
bool
isNew
=
loadedExtensions
.
Add
(
ob
.
DangerousGetAddress
(
)
)
;
Debug
.
Assert
(
isNew
)
;
// We have to support gc because the type machinery makes it very
// hard not to - but we really don't have a need for it in most
// concrete extension types, so untrack the object to save calls
// from Python into the managed runtime that are pure overhead.
Runtime
.
PyObject_GC_UnTrack
(
ob
)
;
}
/// <summary>
/// Type __setattr__ implementation.
/// </summary>
public
static
int
tp_setattro
(
BorrowedReference
ob
,
BorrowedReference
key
,
BorrowedReference
val
)
{
var
message
=
"type does not support setting attributes"
;
if
(
val
==
null
)
{
message
=
"readonly attribute"
;
}
Exceptions
.
SetError
(
Exceptions
.
AttributeError
,
message
)
;
return
-
1
;
}
public
unsafe
static
void
tp_dealloc
(
NewReference
lastRef
)
{
Runtime
.
PyObject_GC_UnTrack
(
lastRef
.
Borrow
(
)
)
;
tp_clear
(
lastRef
.
Borrow
(
)
)
;
bool
deleted
=
loadedExtensions
.
Remove
(
lastRef
.
DangerousGetAddress
(
)
)
;
Debug
.
Assert
(
deleted
)
;
// we must decref our type: https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc
DecrefTypeAndFree
(
lastRef
.
Steal
(
)
)
;
}
public
static
int
tp_clear
(
BorrowedReference
ob
)
{
TryFreeGCHandle
(
ob
)
;
int
res
=
ClassBase
.
BaseUnmanagedClear
(
ob
)
;
return
res
;
}
protected
override
void
OnLoad
(
BorrowedReference
ob
,
InterDomainContext
?
context
)
{
base
.
OnLoad
(
ob
,
context
)
;
SetupGc
(
ob
,
Runtime
.
PyObject_TYPE
(
ob
)
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL