Repository navigation
Expand file tree
/
Copy pathCData.cs
More file actions
176 lines (145 loc) · 6.24 KB
/
Copy pathCData.cs
File metadata and controls
176 lines (145 loc) · 6.24 KB
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
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* [email protected]. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
#if FEATURE_NATIVE
#if CLR2
using Microsoft.Scripting.Math;
#else
using System.Numerics;
#endif
using System;
using System.Diagnostics;
using System.Threading;
using IronPython.Runtime;
using IronPython.Runtime.Types;
using System.Collections.Generic;
namespace IronPython.Modules {
/// <summary>
/// Provides support for interop with native code from Python code.
/// </summary>
public static partial class CTypes {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly PythonType _SimpleCData = SimpleType.MakeSystemType(typeof(SimpleCData));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly PythonType CFuncPtr = CFuncPtrType.MakeSystemType(typeof(_CFuncPtr));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly PythonType Structure = StructType.MakeSystemType(typeof(_Structure));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly PythonType Union = UnionType.MakeSystemType(typeof(_Union));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly PythonType _Pointer = PointerType.MakeSystemType(typeof(Pointer));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly PythonType Array = ArrayType.MakeSystemType(typeof(_Array));
/// <summary>
/// Base class for all ctypes interop types.
/// </summary>
[PythonType("_CData"), PythonHidden]
public abstract class CData : IPythonBufferable, IBufferProtocol {
internal MemoryHolder _memHolder;
// members: __setstate__, __reduce__ _b_needsfree_ __ctypes_from_outparam__ __hash__ _objects _b_base_ __doc__
protected CData() {
}
public int Size {
[PythonHidden]
get {
// TODO: What if a user directly subclasses CData?
return NativeType.Size;
}
}
// TODO: Accesses via Ops class
public IntPtr UnsafeAddress {
[PythonHidden]
get {
return _memHolder.UnsafeAddress;
}
}
byte[] IPythonBufferable.GetBytes(int offset, int length) {
int maxLen = checked(offset + length);
byte[] res = new byte[length];
for (int i = offset; i < maxLen; i++) {
res[i - offset] = _memHolder.ReadByte(i);
}
return res;
}
internal INativeType NativeType {
get {
return (INativeType)DynamicHelpers.GetPythonType(this);
}
}
public virtual object _objects {
get {
return _memHolder.Objects;
}
}
internal void SetAddress(IntPtr address) {
Debug.Assert(_memHolder == null);
_memHolder = new MemoryHolder(address, NativeType.Size);
}
internal virtual PythonTuple GetBufferInfo() {
return PythonTuple.MakeTuple(
NativeType.TypeFormat,
0,
PythonTuple.EMPTY
);
}
#region IBufferProtocol Members
Bytes IBufferProtocol.GetItem(int index) {
return new Bytes(((IPythonBufferable)this).GetBytes(index, this.NativeType.Size));
}
void IBufferProtocol.SetItem(int index, object value) {
throw new NotImplementedException();
}
void IBufferProtocol.SetSlice(Slice index, object value) {
throw new NotImplementedException();
}
public virtual int ItemCount {
[PythonHidden]
get {
return 1;
}
}
string IBufferProtocol.Format {
get { return NativeType.TypeFormat; }
}
public virtual BigInteger ItemSize {
[PythonHidden]
get { return this.NativeType.Size; }
}
BigInteger IBufferProtocol.NumberDimensions {
get { return 0; }
}
bool IBufferProtocol.ReadOnly {
get { return false; }
}
[PythonHidden]
public virtual IList<BigInteger> GetShape(int start, int? end) {
return null;
}
PythonTuple IBufferProtocol.Strides {
get { return null; }
}
object IBufferProtocol.SubOffsets {
get { return null; }
}
Bytes IBufferProtocol.ToBytes(int start, int? end) {
return new Bytes(((IPythonBufferable)this).GetBytes(start, this.NativeType.Size));
}
List IBufferProtocol.ToList(int start, int? end) {
return new List(((IBufferProtocol)this).ToBytes(start, end));
}
#endregion
}
}
}
#endif