FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/moduleobject.cs at update/NetCore21 · losttech/pythonnet · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
losttech
/
pythonnet
Public
forked from
pythonnet/pythonnet
Notifications
You must be signed in to change notification settings
Fork
1
Star
2
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
moduleobject.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
552 lines (491 loc) · 18.3 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
moduleobject.cs
Copy path
File metadata and controls
552 lines (491 loc) · 18.3 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
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Linq
;
using
System
.
IO
;
using
System
.
Reflection
;
using
System
.
Runtime
.
InteropServices
;
namespace
Python
.
Runtime
{
/// <summary>
/// Implements a Python type that provides access to CLR namespaces. The
/// type behaves like a Python module, and can contain other sub-modules.
/// </summary>
[
Serializable
]
internal
class
ModuleObject
:
ExtensionType
{
private
Dictionary
<
string
,
ManagedType
>
cache
;
internal
string
moduleName
;
internal
IntPtr
dict
;
protected
string
_namespace
;
public
ModuleObject
(
string
name
)
{
if
(
name
==
string
.
Empty
)
{
throw
new
ArgumentException
(
"Name must not be empty!"
)
;
}
moduleName
=
name
;
cache
=
new
Dictionary
<
string
,
ManagedType
>
(
)
;
_namespace
=
name
;
// Use the filename from any of the assemblies just so there's something for
// anything that expects __file__ to be set.
var
filename
=
"unknown"
;
var
docstring
=
"Namespace containing types from the following assemblies:
\n
\n
"
;
foreach
(
Assembly
a
in
AssemblyManager
.
GetAssemblies
(
name
)
)
{
if
(
!
a
.
IsDynamic
&&
a
.
Location
!=
null
)
{
filename
=
a
.
Location
;
}
docstring
+=
"- "
+
a
.
FullName
+
"
\n
"
;
}
dict
=
Runtime
.
PyDict_New
(
)
;
IntPtr
pyname
=
Runtime
.
PyString_FromString
(
moduleName
)
;
IntPtr
pyfilename
=
Runtime
.
PyString_FromString
(
filename
)
;
IntPtr
pydocstring
=
Runtime
.
PyString_FromString
(
docstring
)
;
IntPtr
pycls
=
TypeManager
.
GetTypeHandle
(
GetType
(
)
)
;
Runtime
.
PyDict_SetItem
(
dict
,
PyIdentifier
.
__name__
,
pyname
)
;
Runtime
.
PyDict_SetItem
(
dict
,
PyIdentifier
.
__file__
,
pyfilename
)
;
Runtime
.
PyDict_SetItem
(
dict
,
PyIdentifier
.
__doc__
,
pydocstring
)
;
Runtime
.
PyDict_SetItem
(
dict
,
PyIdentifier
.
__class__
,
pycls
)
;
Runtime
.
XDecref
(
pyname
)
;
Runtime
.
XDecref
(
pyfilename
)
;
Runtime
.
XDecref
(
pydocstring
)
;
Runtime
.
XIncref
(
dict
)
;
SetObjectDict
(
pyHandle
,
dict
)
;
InitializeModuleMembers
(
)
;
}
/// <summary>
/// Returns a ClassBase object representing a type that appears in
/// this module's namespace or a ModuleObject representing a child
/// namespace (or null if the name is not found). This method does
/// not increment the Python refcount of the returned object.
/// </summary>
public
ManagedType
GetAttribute
(
string
name
,
bool
guess
)
{
ManagedType
cached
=
null
;
cache
.
TryGetValue
(
name
,
out
cached
)
;
if
(
cached
!=
null
)
{
return
cached
;
}
ModuleObject
m
;
ClassBase
c
;
Type
type
;
//if (AssemblyManager.IsValidNamespace(name))
//{
// IntPtr py_mod_name = Runtime.PyString_FromString(name);
// IntPtr modules = Runtime.PyImport_GetModuleDict();
// IntPtr module = Runtime.PyDict_GetItem(modules, py_mod_name);
// if (module != IntPtr.Zero)
// return (ManagedType)this;
// return null;
//}
string
qname
=
_namespace
==
string
.
Empty
?
name
:
_namespace
+
"."
+
name
;
// If the fully-qualified name of the requested attribute is
// a namespace exported by a currently loaded assembly, return
// a new ModuleObject representing that namespace.
if
(
AssemblyManager
.
IsValidNamespace
(
qname
)
)
{
m
=
new
ModuleObject
(
qname
)
;
StoreAttribute
(
name
,
m
)
;
m
.
DecrRefCount
(
)
;
return
m
;
}
// Look for a type in the current namespace. Note that this
// includes types, delegates, enums, interfaces and structs.
// Only public namespace members are exposed to Python.
type
=
AssemblyManager
.
LookupTypes
(
qname
)
.
FirstOrDefault
(
t
=>
t
.
IsPublic
)
;
if
(
type
!=
null
)
{
c
=
ClassManager
.
GetClass
(
type
)
;
StoreAttribute
(
name
,
c
)
;
return
c
;
}
// This is a little repetitive, but it ensures that the right
// thing happens with implicit assembly loading at a reasonable
// cost. Ask the AssemblyManager to do implicit loading for each
// of the steps in the qualified name, then try it again.
bool
ignore
=
name
.
StartsWith
(
"__"
)
;
if
(
AssemblyManager
.
LoadImplicit
(
qname
,
assemblyLoadErrorHandler
:
ImportWarning
,
!
ignore
)
)
{
if
(
AssemblyManager
.
IsValidNamespace
(
qname
)
)
{
m
=
new
ModuleObject
(
qname
)
;
StoreAttribute
(
name
,
m
)
;
m
.
DecrRefCount
(
)
;
return
m
;
}
type
=
AssemblyManager
.
LookupTypes
(
qname
)
.
FirstOrDefault
(
t
=>
t
.
IsPublic
)
;
if
(
type
!=
null
)
{
c
=
ClassManager
.
GetClass
(
type
)
;
StoreAttribute
(
name
,
c
)
;
return
c
;
}
}
// We didn't find the name, so we may need to see if there is a
// generic type with this base name. If so, we'll go ahead and
// return it. Note that we store the mapping of the unmangled
// name to generic type - it is technically possible that some
// future assembly load could contribute a non-generic type to
// the current namespace with the given basename, but unlikely
// enough to complicate the implementation for now.
if
(
guess
)
{
string
gname
=
GenericUtil
.
GenericNameForBaseName
(
_namespace
,
name
)
;
if
(
gname
!=
null
)
{
ManagedType
o
=
GetAttribute
(
gname
,
false
)
;
if
(
o
!=
null
)
{
StoreAttribute
(
name
,
o
)
;
return
o
;
}
}
}
return
null
;
}
static
void
ImportWarning
(
Exception
exception
)
{
Exceptions
.
warn
(
exception
.
ToString
(
)
,
Exceptions
.
ImportWarning
)
;
}
/// <summary>
/// Stores an attribute in the instance dict for future lookups.
/// </summary>
private
void
StoreAttribute
(
string
name
,
ManagedType
ob
)
{
if
(
Runtime
.
PyDict_SetItemString
(
dict
,
name
,
ob
.
pyHandle
)
!=
0
)
{
throw
new
PythonException
(
)
;
}
ob
.
IncrRefCount
(
)
;
cache
[
name
]
=
ob
;
}
/// <summary>
/// Preloads all currently-known names for the module namespace. This
/// can be called multiple times, to add names from assemblies that
/// may have been loaded since the last call to the method.
/// </summary>
public
void
LoadNames
(
)
{
ManagedType
m
=
null
;
foreach
(
string
name
in
AssemblyManager
.
GetNames
(
_namespace
)
)
{
cache
.
TryGetValue
(
name
,
out
m
)
;
if
(
m
!=
null
)
{
continue
;
}
IntPtr
attr
=
Runtime
.
PyDict_GetItemString
(
dict
,
name
)
;
// If __dict__ has already set a custom property, skip it.
if
(
attr
!=
IntPtr
.
Zero
)
{
continue
;
}
GetAttribute
(
name
,
true
)
;
}
}
/// <summary>
/// Initialize module level functions and attributes
/// </summary>
internal
void
InitializeModuleMembers
(
)
{
Type
funcmarker
=
typeof
(
ModuleFunctionAttribute
)
;
Type
propmarker
=
typeof
(
ModulePropertyAttribute
)
;
Type
ftmarker
=
typeof
(
ForbidPythonThreadsAttribute
)
;
Type
type
=
GetType
(
)
;
BindingFlags
flags
=
BindingFlags
.
Public
|
BindingFlags
.
Static
;
while
(
type
!=
null
)
{
MethodInfo
[
]
methods
=
type
.
GetMethods
(
flags
)
;
foreach
(
MethodInfo
method
in
methods
)
{
object
[
]
attrs
=
method
.
GetCustomAttributes
(
funcmarker
,
false
)
;
object
[
]
forbid
=
method
.
GetCustomAttributes
(
ftmarker
,
false
)
;
bool
allow_threads
=
forbid
.
Length
==
0
;
if
(
attrs
.
Length
>
0
)
{
string
name
=
method
.
Name
;
var
mi
=
new
MethodInfo
[
1
]
;
mi
[
0
]
=
method
;
var
m
=
new
ModuleFunctionObject
(
type
,
name
,
mi
,
allow_threads
)
;
StoreAttribute
(
name
,
m
)
;
m
.
DecrRefCount
(
)
;
}
}
PropertyInfo
[
]
properties
=
type
.
GetProperties
(
)
;
foreach
(
PropertyInfo
property
in
properties
)
{
object
[
]
attrs
=
property
.
GetCustomAttributes
(
propmarker
,
false
)
;
if
(
attrs
.
Length
>
0
)
{
string
name
=
property
.
Name
;
var
p
=
new
ModulePropertyObject
(
property
)
;
StoreAttribute
(
name
,
p
)
;
p
.
DecrRefCount
(
)
;
}
}
type
=
type
.
BaseType
;
}
}
/// <summary>
/// ModuleObject __getattribute__ implementation. Module attributes
/// are always either classes or sub-modules representing subordinate
/// namespaces. CLR modules implement a lazy pattern - the sub-modules
/// and classes are created when accessed and cached for future use.
/// </summary>
public
static
IntPtr
tp_getattro
(
IntPtr
ob
,
IntPtr
key
)
{
var
self
=
(
ModuleObject
)
GetManagedObject
(
ob
)
;
if
(
!
Runtime
.
PyString_Check
(
key
)
)
{
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
"string expected"
)
;
return
IntPtr
.
Zero
;
}
IntPtr
op
=
Runtime
.
PyDict_GetItem
(
self
.
dict
,
key
)
;
if
(
op
!=
IntPtr
.
Zero
)
{
Runtime
.
XIncref
(
op
)
;
return
op
;
}
string
name
=
InternString
.
GetManagedString
(
key
)
;
if
(
name
==
"__dict__"
)
{
Runtime
.
XIncref
(
self
.
dict
)
;
return
self
.
dict
;
}
ManagedType
attr
=
null
;
try
{
attr
=
self
.
GetAttribute
(
name
,
true
)
;
}
catch
(
Exception
e
)
{
Exceptions
.
SetError
(
e
)
;
return
IntPtr
.
Zero
;
}
if
(
attr
==
null
)
{
Exceptions
.
SetError
(
Exceptions
.
AttributeError
,
name
)
;
return
IntPtr
.
Zero
;
}
Runtime
.
XIncref
(
attr
.
pyHandle
)
;
return
attr
.
pyHandle
;
}
/// <summary>
/// ModuleObject __repr__ implementation.
/// </summary>
public
static
IntPtr
tp_repr
(
IntPtr
ob
)
{
var
self
=
(
ModuleObject
)
GetManagedObject
(
ob
)
;
return
Runtime
.
PyString_FromString
(
$
"<module '
{
self
.
moduleName
}
'>"
)
;
}
public
new
static
void
tp_dealloc
(
IntPtr
ob
)
{
var
self
=
(
ModuleObject
)
GetManagedObject
(
ob
)
;
tp_clear
(
ob
)
;
self
.
Dealloc
(
)
;
}
public
static
int
tp_traverse
(
IntPtr
ob
,
IntPtr
visit
,
IntPtr
arg
)
{
var
self
=
(
ModuleObject
)
GetManagedObject
(
ob
)
;
int
res
=
PyVisit
(
self
.
dict
,
visit
,
arg
)
;
if
(
res
!=
0
)
return
res
;
foreach
(
var
attr
in
self
.
cache
.
Values
)
{
res
=
PyVisit
(
attr
.
pyHandle
,
visit
,
arg
)
;
if
(
res
!=
0
)
return
res
;
}
return
0
;
}
public
static
int
tp_clear
(
IntPtr
ob
)
{
var
self
=
(
ModuleObject
)
GetManagedObject
(
ob
)
;
Runtime
.
Py_CLEAR
(
ref
self
.
dict
)
;
ClearObjectDict
(
ob
)
;
foreach
(
var
attr
in
self
.
cache
.
Values
)
{
Runtime
.
XDecref
(
attr
.
pyHandle
)
;
}
self
.
cache
.
Clear
(
)
;
return
0
;
}
protected
override
void
OnSave
(
InterDomainContext
context
)
{
base
.
OnSave
(
context
)
;
System
.
Diagnostics
.
Debug
.
Assert
(
dict
==
GetObjectDict
(
pyHandle
)
)
;
foreach
(
var
attr
in
cache
.
Values
)
{
Runtime
.
XIncref
(
attr
.
pyHandle
)
;
}
// Decref twice in tp_clear, equilibrate them.
Runtime
.
XIncref
(
dict
)
;
Runtime
.
XIncref
(
dict
)
;
}
protected
override
void
OnLoad
(
InterDomainContext
context
)
{
base
.
OnLoad
(
context
)
;
SetObjectDict
(
pyHandle
,
dict
)
;
}
}
/// <summary>
/// The CLR module is the root handler used by the magic import hook
/// to import assemblies. It has a fixed module name "clr" and doesn't
/// provide a namespace.
/// </summary>
[
Serializable
]
internal
class
CLRModule
:
ModuleObject
{
protected
static
bool
hacked
=
false
;
protected
static
bool
interactive_preload
=
true
;
internal
static
bool
preload
;
// XXX Test performance of new features //
internal
static
bool
_SuppressDocs
=
false
;
internal
static
bool
_SuppressOverloads
=
false
;
static
CLRModule
(
)
{
Reset
(
)
;
}
public
CLRModule
(
)
:
base
(
"clr"
)
{
_namespace
=
string
.
Empty
;
// This hackery is required in order to allow a plain Python to
// import the managed runtime via the CLR bootstrapper module.
// The standard Python machinery in control at the time of the
// import requires the module to pass PyModule_Check. :(
if
(
!
hacked
)
{
IntPtr
type
=
tpHandle
;
IntPtr
mro
=
Marshal
.
ReadIntPtr
(
type
,
TypeOffset
.
tp_mro
)
;
IntPtr
ext
=
Runtime
.
ExtendTuple
(
mro
,
Runtime
.
PyModuleType
)
;
Marshal
.
WriteIntPtr
(
type
,
TypeOffset
.
tp_mro
,
ext
)
;
Runtime
.
XDecref
(
mro
)
;
hacked
=
true
;
}
}
public
static
void
Reset
(
)
{
hacked
=
false
;
interactive_preload
=
true
;
preload
=
false
;
// XXX Test performance of new features //
_SuppressDocs
=
false
;
_SuppressOverloads
=
false
;
}
/// <summary>
/// The initializing of the preload hook has to happen as late as
/// possible since sys.ps1 is created after the CLR module is
/// created.
/// </summary>
internal
void
InitializePreload
(
)
{
if
(
interactive_preload
)
{
interactive_preload
=
false
;
if
(
!
Runtime
.
PySys_GetObject
(
"ps1"
)
.
IsNull
)
{
preload
=
true
;
}
else
{
Exceptions
.
Clear
(
)
;
preload
=
false
;
}
}
}
[
ModuleFunction
]
public
static
bool
getPreload
(
)
{
return
preload
;
}
[
ModuleFunction
]
public
static
void
setPreload
(
bool
preloadFlag
)
{
preload
=
preloadFlag
;
}
//[ModuleProperty]
public
static
bool
SuppressDocs
{
get
{
return
_SuppressDocs
;
}
set
{
_SuppressDocs
=
value
;
}
}
//[ModuleProperty]
public
static
bool
SuppressOverloads
{
get
{
return
_SuppressOverloads
;
}
set
{
_SuppressOverloads
=
value
;
}
}
[
ModuleFunction
]
[
ForbidPythonThreads
]
public
static
Assembly
AddReference
(
string
name
)
{
AssemblyManager
.
UpdatePath
(
)
;
Assembly
assembly
=
null
;
assembly
=
AssemblyManager
.
FindLoadedAssembly
(
name
)
;
if
(
assembly
==
null
)
{
assembly
=
AssemblyManager
.
LoadAssemblyPath
(
name
)
;
}
if
(
assembly
==
null
)
{
assembly
=
AssemblyManager
.
LoadAssembly
(
name
)
;
}
if
(
assembly
==
null
)
{
assembly
=
AssemblyManager
.
LoadAssemblyFullPath
(
name
)
;
}
if
(
assembly
==
null
)
{
throw
new
FileNotFoundException
(
$
"Unable to find assembly '
{
name
}
'."
)
;
}
return
assembly
;
}
/// <summary>
/// Get a Type instance for a class object.
/// clr.GetClrType(IComparable) gives you the Type for IComparable,
/// that you can e.g. perform reflection on. Similar to typeof(IComparable) in C#
/// or clr.GetClrType(IComparable) in IronPython.
///
/// </summary>
/// <param name="type"></param>
/// <returns>The Type object</returns>
[
ModuleFunction
]
[
ForbidPythonThreads
]
public
static
Type
GetClrType
(
Type
type
)
{
return
type
;
}
[
ModuleFunction
]
[
ForbidPythonThreads
]
public
static
string
FindAssembly
(
string
name
)
{
AssemblyManager
.
UpdatePath
(
)
;
return
AssemblyManager
.
FindAssembly
(
name
)
;
}
[
ModuleFunction
]
public
static
string
[
]
ListAssemblies
(
bool
verbose
)
{
AssemblyName
[
]
assnames
=
AssemblyManager
.
ListAssemblies
(
)
;
var
names
=
new
string
[
assnames
.
Length
]
;
for
(
var
i
=
0
;
i
<
assnames
.
Length
;
i
++
)
{
if
(
verbose
)
{
names
[
i
]
=
assnames
[
i
]
.
FullName
;
}
else
{
names
[
i
]
=
assnames
[
i
]
.
Name
;
}
}
return
names
;
}
[
ModuleFunction
]
public
static
int
_AtExit
(
)
{
return
Runtime
.
AtExit
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL