def validate_clr_types(signature_types, var_signature = False):
if not isinstance(signature_types, tuple):
signature_types = (signature_types,)
for t in signature_types:
if type(t) is type(System.IComparable): # type overloaded on generic arity, eg IComparable and IComparable[T]
t = t[()] # select non-generic version
clr_type = clr.GetClrType(t)
if t == Void:
raise TypeError("Void cannot be used in signature")
is_typed = clr.GetPythonType(clr_type) == t
# is_typed needs to be weakened until the generated type
# gets explicitly published as the underlying CLR type
is_typed = is_typed or (hasattr(t, "__metaclass__") and t.__metaclass__ in [ClrInterface, ClrClass])
if not is_typed:
raise Exception, "Invalid CLR type %s" % str(t)
if not var_signature:
if clr_type.IsByRef:
raise TypeError("Byref can only be used as arguments and locals")
# ArgIterator is not present in Silverlight
if hasattr(System, "ArgIterator") and t == System.ArgIterator:
raise TypeError("Stack-referencing types can only be used as arguments and locals")
python类GetClrType()的实例源码
def make_cab(attrib_type, *args, **kwds):
clrtype = clr.GetClrType(attrib_type)
argtypes = tuple(map(lambda x:clr.GetClrType(type(x)), args))
ci = clrtype.GetConstructor(argtypes)
props = ([],[])
fields = ([],[])
for kwd in kwds:
pi = clrtype.GetProperty(kwd)
if pi is not None:
props[0].append(pi)
props[1].append(kwds[kwd])
else:
fi = clrtype.GetField(kwd)
if fi is not None:
fields[0].append(fi)
fields[1].append(kwds[kwd])
else:
raise TypeError("No %s Member found on %s" % (kwd, clrtype.Name))
return CustomAttributeBuilder(ci, args,
tuple(props[0]), tuple(props[1]),
tuple(fields[0]), tuple(fields[1]))
def update_details(self):
''' use model details to fill main infos,
default filter option and datagrid
'''
ref = self.model.ref
self.maindoc = self.model.name
self.maindoc += ' (Type : {} - Mem Size: {})'.format(
self.model.type,
sys.getsizeof(ref))
self.maindoc += '\nGetType() : ' + str(clr.GetClrType(type(ref)))
self.maindoc += '\n'*2 + str(self.model.templ_value(ref))
self.maindoc += '\n'*2 + str(self.model.templ_doc(ref))
self.options_templates = [
GridFilter('Template light', self.model.templ_members),
GridFilter('All Members', get_members_no_filter)
]
self.update_grid()
self.NotifyPropertyChanged("maindoc")
self.NotifyPropertyChanged("options_templates")
def get_template(ref):
''' define a members template to choose recursivity or not
next step : look in class members
'''
res = None
if type(ref).__name__ == 'namespace#':
res = get_members_recurs
elif type(ref).__name__ == 'module':
res = get_members_recurs
#elif isinstance(ref, type):
# clrtype = clr.GetClrType(ref)
# if clrtype.IsEnum:
# res = get_members
# elif clrtype.IsClass:
# res = get_members
return res
def collect_signatures(self, val):
doc = val.__doc__
type_obj = None
if isinstance(val, type) or isinstance(val, _OldClassType):
type_obj = val
val = val.__init__
try:
args, vargs, varkw, defaults = inspect.getargspec(val)
except TypeError:
# we're not doing inspect on a Python function...
if sys.platform == 'cli':
if type_obj is not None:
clr_type = clr.GetClrType(type_obj)
ctors = clr_type.GetConstructors()
return [self.get_ipy_sig(type_obj, ctor) for ctor in ctors]
elif type(val) is types.BuiltinFunctionType:
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
elif type(val) is builtin_method_descriptor_type:
val = PythonOps.GetBuiltinMethodDescriptorTemplate(val)
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
raise
remove_self = type_obj is not None or (type(val) is types.MethodType and
((sys.version_info >= (3,) and val.__self__ is not None) or
(sys.version_info < (3,) and val.im_self is not None)))
if remove_self:
# remove self for instance methods and types
args = args[1:]
if defaults is not None:
defaults = [repr(default) for default in defaults]
else:
defaults = []
return [(doc, args, vargs, varkw, defaults)]
def set_current_module(self, module):
mod = sys.modules.get(module)
if mod is not None:
_debug_write('Setting module to ' + module)
if sys.platform == 'cli':
self.exec_mod = clr.GetClrType(type(sys)).GetProperty('Scope', System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(sys, ())
else:
self.exec_mod = mod
elif module:
_debug_write('Unknown module ' + module)
def collect_signatures(self, val):
doc = val.__doc__
type_obj = None
if isinstance(val, type) or isinstance(val, _OldClassType):
type_obj = val
val = val.__init__
try:
args, vargs, varkw, defaults = inspect.getargspec(val)
except TypeError:
# we're not doing inspect on a Python function...
if sys.platform == 'cli':
if type_obj is not None:
clr_type = clr.GetClrType(type_obj)
ctors = clr_type.GetConstructors()
return [self.get_ipy_sig(type_obj, ctor) for ctor in ctors]
elif type(val) is types.BuiltinFunctionType:
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
elif type(val) is builtin_method_descriptor_type:
val = PythonOps.GetBuiltinMethodDescriptorTemplate(val)
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
raise
remove_self = type_obj is not None or (type(val) is types.MethodType and
((sys.version_info >= (3,) and val.__self__ is not None) or
(sys.version_info < (3,) and val.im_self is not None)))
if remove_self:
# remove self for instance methods and types
args = args[1:]
if defaults is not None:
defaults = [repr(default) for default in defaults]
else:
defaults = []
return [(doc, args, vargs, varkw, defaults)]
def set_current_module(self, module):
mod = sys.modules.get(module)
if mod is not None:
_debug_write('Setting module to ' + module)
if sys.platform == 'cli':
self.exec_mod = clr.GetClrType(type(sys)).GetProperty('Scope', System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(sys, ())
else:
self.exec_mod = mod
elif module:
_debug_write('Unknown module ' + module)
def collect_signatures(self, val):
doc = val.__doc__
type_obj = None
if isinstance(val, type) or isinstance(val, _OldClassType):
type_obj = val
val = val.__init__
try:
args, vargs, varkw, defaults = inspect.getargspec(val)
except TypeError:
# we're not doing inspect on a Python function...
if sys.platform == 'cli':
if type_obj is not None:
clr_type = clr.GetClrType(type_obj)
ctors = clr_type.GetConstructors()
return [self.get_ipy_sig(type_obj, ctor) for ctor in ctors]
elif type(val) is types.BuiltinFunctionType:
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
elif type(val) is builtin_method_descriptor_type:
val = PythonOps.GetBuiltinMethodDescriptorTemplate(val)
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
raise
remove_self = type_obj is not None or (type(val) is types.MethodType and
((sys.version_info >= (3,) and val.__self__ is not None) or
(sys.version_info < (3,) and val.im_self is not None)))
if remove_self:
# remove self for instance methods and types
args = args[1:]
if defaults is not None:
defaults = [repr(default) for default in defaults]
else:
defaults = []
return [(doc, args, vargs, varkw, defaults)]
def set_current_module(self, module):
mod = sys.modules.get(module)
if mod is not None:
_debug_write('Setting module to ' + module)
if sys.platform == 'cli':
self.exec_mod = clr.GetClrType(type(sys)).GetProperty('Scope', System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(sys, ())
else:
self.exec_mod = mod
else:
_debug_write('Unknown module ' + module)
def collect_signatures(self, val):
doc = val.__doc__
type_obj = None
if isinstance(val, type) or isinstance(val, _OldClassType):
type_obj = val
val = val.__init__
try:
args, vargs, varkw, defaults = inspect.getargspec(val)
except TypeError:
# we're not doing inspect on a Python function...
if sys.platform == 'cli':
if type_obj is not None:
clr_type = clr.GetClrType(type_obj)
ctors = clr_type.GetConstructors()
return [self.get_ipy_sig(type_obj, ctor) for ctor in ctors]
elif type(val) is types.BuiltinFunctionType:
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
elif type(val) is builtin_method_descriptor_type:
val = PythonOps.GetBuiltinMethodDescriptorTemplate(val)
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
raise
remove_self = type_obj is not None or (type(val) is types.MethodType and
((sys.version >= '3.0' and val.__self__ is not None) or
(sys.version < '3.0' and val.im_self is not None)))
if remove_self:
# remove self for instance methods and types
args = args[1:]
if defaults is not None:
defaults = [repr(default) for default in defaults]
return [(doc, args, vargs, varkw, defaults)]
def set_current_module(self, module):
mod = sys.modules.get(module)
if mod is not None:
_debug_write('Setting module to ' + module)
if sys.platform == 'cli':
self.exec_mod = clr.GetClrType(type(sys)).GetProperty('Scope', System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(sys, ())
else:
self.exec_mod = mod
else:
_debug_write('Unknown module ' + module)
def collect_signatures(self, val):
doc = val.__doc__
type_obj = None
if isinstance(val, type) or isinstance(val, _OldClassType):
type_obj = val
val = val.__init__
try:
args, vargs, varkw, defaults = inspect.getargspec(val)
except TypeError:
# we're not doing inspect on a Python function...
if sys.platform == 'cli':
if type_obj is not None:
clr_type = clr.GetClrType(type_obj)
ctors = clr_type.GetConstructors()
return [self.get_ipy_sig(type_obj, ctor) for ctor in ctors]
elif type(val) is types.BuiltinFunctionType:
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
elif type(val) is builtin_method_descriptor_type:
val = PythonOps.GetBuiltinMethodDescriptorTemplate(val)
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
raise
remove_self = type_obj is not None or (type(val) is types.MethodType and
((sys.version_info >= (3,) and val.__self__ is not None) or
(sys.version_info < (3,) and val.im_self is not None)))
if remove_self:
# remove self for instance methods and types
args = args[1:]
if defaults is not None:
defaults = [repr(default) for default in defaults]
else:
defaults = []
return [(doc, args, vargs, varkw, defaults)]
def set_current_module(self, module):
mod = sys.modules.get(module)
if mod is not None:
_debug_write('Setting module to ' + module)
if sys.platform == 'cli':
self.exec_mod = clr.GetClrType(type(sys)).GetProperty('Scope', System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(sys, ())
else:
self.exec_mod = mod
else:
_debug_write('Unknown module ' + module)
def collect_signatures(self, val):
doc = val.__doc__
type_obj = None
if isinstance(val, type) or isinstance(val, _OldClassType):
type_obj = val
val = val.__init__
try:
args, vargs, varkw, defaults = inspect.getargspec(val)
except TypeError:
# we're not doing inspect on a Python function...
if sys.platform == 'cli':
if type_obj is not None:
clr_type = clr.GetClrType(type_obj)
ctors = clr_type.GetConstructors()
return [self.get_ipy_sig(type_obj, ctor) for ctor in ctors]
elif type(val) is types.BuiltinFunctionType:
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
elif type(val) is builtin_method_descriptor_type:
val = PythonOps.GetBuiltinMethodDescriptorTemplate(val)
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
raise
remove_self = type_obj is not None or (type(val) is types.MethodType and
((sys.version_info >= (3,) and val.__self__ is not None) or
(sys.version_info < (3,) and val.im_self is not None)))
if remove_self:
# remove self for instance methods and types
args = args[1:]
if defaults is not None:
defaults = [repr(default) for default in defaults]
else:
defaults = []
return [(doc, args, vargs, varkw, defaults)]
def set_current_module(self, module):
mod = sys.modules.get(module)
if mod is not None:
_debug_write('Setting module to ' + module)
if sys.platform == 'cli':
self.exec_mod = clr.GetClrType(type(sys)).GetProperty('Scope', System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(sys, ())
else:
self.exec_mod = mod
else:
_debug_write('Unknown module ' + module)
def get_typed_properties(self):
for item_name, item in self.__dict__.items():
if isinstance(item, property):
if item.fget:
if not self.is_typed_method(item.fget): continue
prop_type = item.fget.return_type
else:
if not self.is_typed_method(item.fset): continue
prop_type = item.fset.arg_types[0]
validate_clr_types(prop_type)
clr_prop_type = clr.GetClrType(prop_type)
yield item, item_name, clr_prop_type
def emit_classattribs(self, typebld):
if hasattr(self, '_clrclassattribs'):
for attrib_info in self._clrclassattribs:
if isinstance(attrib_info, type):
ci = clr.GetClrType(attrib_info).GetConstructor(())
cab = CustomAttributeBuilder(ci, ())
elif isinstance(attrib_info, CustomAttributeDecorator):
cab = attrib_info.GetBuilder()
else:
make_decorator = attrib_info()
cab = make_decorator.GetBuilder()
typebld.SetCustomAttribute(cab)
def map_clr_type(self, clr_type):
"""
TODO - Currently "t = clr.GetPythonType(clr.GetClrType(C)); t == C" will be False
for C where C.__metaclass__ is ClrInterface, even though both t and C
represent the same CLR type. This can be fixed by publishing a mapping
between t and C in the IronPython runtime.
"""
pass
def emit_fields(self, typebld):
if hasattr(self, "_clrfields"):
for fldname in self._clrfields:
field_type = self._clrfields[fldname]
validate_clr_types(field_type)
typebld.DefineField(
fldname,
clr.GetClrType(field_type),
FieldAttributes.Public)
def collect_signatures(self, val):
doc = val.__doc__
type_obj = None
if isinstance(val, type) or isinstance(val, _OldClassType):
type_obj = val
val = val.__init__
try:
args, vargs, varkw, defaults = inspect.getargspec(val)
except TypeError:
# we're not doing inspect on a Python function...
if sys.platform == 'cli':
if type_obj is not None:
clr_type = clr.GetClrType(type_obj)
ctors = clr_type.GetConstructors()
return [self.get_ipy_sig(type_obj, ctor) for ctor in ctors]
elif type(val) is types.BuiltinFunctionType:
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
elif type(val) is builtin_method_descriptor_type:
val = PythonOps.GetBuiltinMethodDescriptorTemplate(val)
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
raise
remove_self = type_obj is not None or (type(val) is types.MethodType and
((sys.version_info >= (3,) and val.__self__ is not None) or
(sys.version_info < (3,) and val.im_self is not None)))
if remove_self:
# remove self for instance methods and types
args = args[1:]
if defaults is not None:
defaults = [repr(default) for default in defaults]
else:
defaults = []
return [(doc, args, vargs, varkw, defaults)]
def set_current_module(self, module):
mod = sys.modules.get(module)
if mod is not None:
_debug_write('Setting module to ' + module)
if sys.platform == 'cli':
self.exec_mod = clr.GetClrType(type(sys)).GetProperty('Scope', System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(sys, ())
else:
self.exec_mod = mod
else:
_debug_write('Unknown module ' + module)
def get_members_enum(enumobject):
''' template for enum types
'''
try:
lst_enum = enumobject.GetEnumNames()
except:
lst_enum = clr.GetClrType(enumobject).GetEnumNames()
for nameoption in lst_enum:
yield (nameoption, None)
def get_members_subenum(enumobject):
''' template for enum types
'''
try:
lst_enum = type(enumobject).GetEnumNames()
except:
lst_enum = clr.GetClrType(type(enumobject)).GetEnumNames()
for nameoption in lst_enum:
yield (nameoption, None)
def get_value_enum(enumobject):
''' clrtype enumeration
'''
try:
lst_enum = enumobject.GetEnumNames()
except:
lst_enum = clr.GetClrType(enumobject).GetEnumNames()
return '\n'.join(lst_enum)
def collect_signatures(self, val):
doc = val.__doc__
type_obj = None
if isinstance(val, type) or isinstance(val, _OldClassType):
type_obj = val
val = val.__init__
try:
args, vargs, varkw, defaults = inspect.getargspec(val)
except TypeError:
# we're not doing inspect on a Python function...
if sys.platform == 'cli':
if type_obj is not None:
clr_type = clr.GetClrType(type_obj)
ctors = clr_type.GetConstructors()
return [self.get_ipy_sig(type_obj, ctor) for ctor in ctors]
elif type(val) is types.BuiltinFunctionType:
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
elif type(val) is builtin_method_descriptor_type:
val = PythonOps.GetBuiltinMethodDescriptorTemplate(val)
return [self.get_ipy_sig(target, target.Targets[0]) for target in val.Overloads.Functions]
raise
remove_self = type_obj is not None or (type(val) is types.MethodType and
((sys.version_info >= (3,) and val.__self__ is not None) or
(sys.version_info < (3,) and val.im_self is not None)))
if remove_self:
# remove self for instance methods and types
args = args[1:]
if defaults is not None:
defaults = [repr(default) for default in defaults]
else:
defaults = []
return [(doc, args, vargs, varkw, defaults)]
def set_current_module(self, module):
mod = sys.modules.get(module)
if mod is not None:
_debug_write('Setting module to ' + module)
if sys.platform == 'cli':
self.exec_mod = clr.GetClrType(type(sys)).GetProperty('Scope', System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(sys, ())
else:
self.exec_mod = mod
else:
_debug_write('Unknown module ' + module)