Untitled diff
294 lines
# -*- Mode: Python; py-indent-offset: 4 -*-
# -*- Mode: Python; py-indent-offset: 4 -*-
# vim: tabstop=4 shiftwidth=4 expandtab
# vim: tabstop=4 shiftwidth=4 expandtab
#
#
# Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
# Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
#
#
# types.py: base types for introspected items.
# types.py: base types for introspected items.
#
#
# This library is free software; you can redistribute it and/or
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# version 2.1 of the License, or (at your option) any later version.
#
#
# This library is distributed in the hope that it will be useful,
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# Lesser General Public License for more details.
#
#
# You should have received a copy of the GNU Lesser General Public
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
# USA
from __future__ import absolute_import
from __future__ import absolute_import
import sys
import sys
from . import _gobject
import warnings
from ._constants import TYPE_INVALID
from .docstring import generate_doc_string
from ._gi import \
from ._gi import \
InterfaceInfo, \
InterfaceInfo, \
ObjectInfo, \
ObjectInfo, \
StructInfo, \
StructInfo, \
VFuncInfo, \
VFuncInfo, \
register_interface_info, \
register_interface_info, \
hook_up_vfunc_implementation
hook_up_vfunc_implementation, \
_gobject
if sys.version_info > (3, 0):
GInterface = _gobject.GInterface
StructInfo # pyflakes
from . import _propertyhelper as propertyhelper
from . import _signalhelper as signalhelper
if (3, 0) <= sys.version_info < (3, 3):
# callable not available for python 3.0 thru 3.2
def callable(obj):
def callable(obj):
return hasattr(obj, '__call__')
return hasattr(obj, '__call__')
def Function(info):
def function(*args, **kwargs):
return info.invoke(*args, **kwargs)
function.__info__ = info
function.__name__ = info.get_name()
function.__module__ = info.get_namespace()
return function
class NativeVFunc(object):
def __init__(self, info):
self._info = info
def __get__(self, instance, klass):
def native_vfunc(*args, **kwargs):
return self._info.invoke(klass.__gtype__, *args, **kwargs)
native_vfunc.__info__ = self._info
native_vfunc.__name__ = self._info.get_name()
native_vfunc.__module__ = self._info.get_namespace()
return native_vfunc
def Constructor(info):
def constructor(cls, *args, **kwargs):
cls_name = info.get_container().get_name()
if cls.__name__ != cls_name:
raise TypeError('%s constructor cannot be used to create instances of a subclass' % cls_name)
return info.invoke(cls, *args, **kwargs)
constructor.__info__ = info
constructor.__name__ = info.get_name()
constructor.__module__ = info.get_namespace()
return constructor
class MetaClassHelper(object):
class MetaClassHelper(object):
def _setup_constructors(cls):
for method_info in cls.__info__.get_methods():
if method_info.is_constructor():
name = method_info.get_name()
constructor = classmethod(Constructor(method_info))
setattr(cls, name, constructor)
def _setup_methods(cls):
def _setup_methods(cls):
for method_info in cls.__info__.get_methods():
for method_info in cls.__info__.get_methods():
name = method_info.get_name()
setattr(cls, method_info.__name__, method_info)
function = Function(method_info)
if method_info.is_method():
method = function
elif method_info.is_constructor():
continue
else:
method = staticmethod(function)
setattr(cls, name, method)
def _setup_fields(cls):
def _setup_fields(cls):
for field_info in cls.__info__.get_fields():
for field_info in cls.__info__.get_fields():
name = field_info.get_name().replace('-', '_')
name = field_info.get_name().replace('-', '_')
setattr(cls, name, property(field_info.get_value, field_info.set_value))
setattr(cls, name, property(field_info.get_value, field_info.set_value))
def _setup_constants(cls):
def _setup_constants(cls):
for constant_info in cls.__info__.get_constants():
for constant_info in cls.__info__.get_constants():
name = constant_info.get_name()
name = constant_info.get_name()
value = constant_info.get_value()
value = constant_info.get_value()
setattr(cls, name, value)
setattr(cls, name, value)
def _setup_vfuncs(cls):
def _setup_vfuncs(cls):
for vfunc_name, py_vfunc in cls.__dict__.items():
for vfunc_name, py_vfunc in cls.__dict__.items():
if not vfunc_name.startswith("do_") or not callable(py_vfunc):
if not vfunc_name.startswith("do_") or not callable(py_vfunc):
continue
continue
# If a method name starts with "do_" assume it is a vfunc, and search
# If a method name starts with "do_" assume it is a vfunc, and search
# in the base classes for a method with the same name to override.
# in the base classes for a method with the same name to override.
# Recursion is not necessary here because getattr() searches all
# Recursion is necessary as overriden methods in most immediate parent
# super class attributes as well.
# classes may shadow vfuncs from classes higher in the hierarchy.
vfunc_info = None
vfunc_info = None
for base in cls.__bases__:
for base in cls.__mro__:
method = getattr(base, vfunc_name, None)
method = getattr(base, vfunc_name, None)
if method is not None and hasattr(method, '__info__') and \
if method is not None and isinstance(method, VFuncInfo):
isinstance(method.__info__, VFuncInfo):
vfunc_info = method
vfunc_info = method.__info__
break
break
# If we did not find a matching method name in the bases, we might
# If we did not find a matching method name in the bases, we might
# be overriding an interface virtual method. Since interfaces do not
# be overriding an interface virtual method. Since interfaces do not
# provide implementations, there will be no method attribute installed
# provide implementations, there will be no method attribute installed
# on the object. Instead we have to search through
# on the object. Instead we have to search through
# InterfaceInfo.get_vfuncs(). Note that the infos returned by
# InterfaceInfo.get_vfuncs(). Note that the infos returned by
# get_vfuncs() use the C vfunc name (ie. there is no "do_" prefix).
# get_vfuncs() use the C vfunc name (ie. there is no "do_" prefix).
if vfunc_info is None:
if vfunc_info is None:
vfunc_info = find_vfunc_info_in_interface(cls.__bases__, vfunc_name[len("do_"):])
vfunc_info = find_vfunc_info_in_interface(cls.__bases__, vfunc_name[len("do_"):])
if vfunc_info is not None:
if vfunc_info is not None:
assert vfunc_name == ('do_' + vfunc_info.get_name())
assert vfunc_name == ('do_' + vfunc_info.get_name())
# Check to see if there are vfuncs with the same name in the bases.
# Check to see if there are vfuncs with the same name in the bases.
# We have no way of specifying which one we are supposed to override.
# We have no way of specifying which one we are supposed to override.
ambiguous_base = find_vfunc_conflict_in_bases(vfunc_info, cls.__bases__)
ambiguous_base = find_vfunc_conflict_in_bases(vfunc_info, cls.__bases__)
if ambiguous_base is not None:
if ambiguous_base is not None:
base_info = vfunc_info.get_container()
base_info = vfunc_info.get_container()
raise TypeError('Method %s() on class %s.%s is ambiguous '
raise TypeError('Method %s() on class %s.%s is ambiguous '
'with methods in base classes %s.%s and %s.%s' %
'with methods in base classes %s.%s and %s.%s' %
(vfunc_name,
(vfunc_name,
cls.__info__.get_namespace(),
cls.__info__.get_namespace(),
cls.__info__.get_name(),
cls.__info__.get_name(),
base_info.get_namespace(),
base_info.get_namespace(),
base_info.get_name(),
base_info.get_name(),
ambiguous_base.__info__.get_namespace(),
ambiguous_base.__info__.get_namespace(),
ambiguous_base.__info__.get_name()))
ambiguous_base.__info__.get_name()
))
hook_up_vfunc_implementation(vfunc_info, cls.__gtype__,
hook_up_vfunc_implementation(vfunc_info, cls.__gtype__,
py_vfunc)
py_vfunc)
def _setup_native_vfuncs(cls):
def _setup_native_vfuncs(cls):
# Only InterfaceInfo and ObjectInfo have the get_vfuncs() method.
# Only InterfaceInfo and ObjectInfo have the get_vfuncs() method.
# We skip InterfaceInfo because interfaces have no implementations for vfuncs.
# We skip InterfaceInfo because interfaces have no implementations for vfuncs.
# Also check if __info__ in __dict__, not hasattr('__info__', ...)
# Also check if __info__ in __dict__, not hasattr('__info__', ...)
# because we do not want to accidentally retrieve __info__ from a base class.
# because we do not want to accidentally retrieve __info__ from a base class.
class_info = cls.__dict__.get('__info__')
class_info = cls.__dict__.get('__info__')
if class_info is None or not isinstance(class_info, ObjectInfo):
if class_info is None or not isinstance(class_info, ObjectInfo):
return
return
# Special case skipping of vfuncs for GObject.Object because they will break
# the static bindings which will try to use them.
if cls.__module__ == 'gi.repository.GObject' and cls.__name__ == 'Object':
return
for vfunc_info in class_info.get_vfuncs():
for vfunc_info in class_info.get_vfuncs():
name = 'do_%s' % vfunc_info.get_name()
name = 'do_%s' % vfunc_info.__name__
value = NativeVFunc(vfunc_info)
setattr(cls, name, vfunc_info)
setattr(cls, name, value)
def find_vfunc_info_in_interface(bases, vfunc_name):
def find_vfunc_info_in_interface(bases, vfunc_name):
for base in bases:
for base in bases:
# All wrapped interfaces inherit from GInterface.
# All wrapped interfaces inherit from GInterface.
# This can be seen in IntrospectionModule.__getattr__() in module.py.
# This can be seen in IntrospectionModule.__getattr__() in module.py.
# We do not need to search regular classes here, only wrapped interfaces.
# We do not need to search regular classes here, only wrapped interfaces.
# We also skip GInterface, because it is not wrapped and has no __info__ attr.
# We also skip GInterface, because it is not wrapped and has no __info__ attr.
if base is _gobject.GInterface or\
# Skip bases without __info__ (static _gobject._gobject.GObject)
not issubclass(base, _gobject.GInterface) or\
if base is GInterface or\
not isinstance(base.__info__, InterfaceInfo):
not issubclass(base, GInterface) or\
not hasattr(base, '__info__'):
continue
continue
for vfunc in base.__info__.get_vfuncs():
# Only look at this classes vfuncs if it is an interface.
if vfunc.get_name() == vfunc_name:
if isinstance(base.__info__, InterfaceInfo):
return vfunc
for vfunc in base.__info__.get_vfuncs():
if vfunc.get_name() == vfunc_name:
return vfunc
# Recurse into the parent classes
vfunc = find_vfunc_info_in_interface(base.__bases__, vfunc_name)
vfunc = find_vfunc_info_in_interface(base.__bases__, vfunc_name)
if vfunc is not None:
if vfunc is not None:
return vfunc
return vfunc
return None
return None
def find_vfunc_conflict_in_bases(vfunc, bases):
def find_vfunc_conflict_in_bases(vfunc, bases):
for klass in bases:
for klass in bases:
if not hasattr(klass, '__info__') or \
if not hasattr(klass, '__info__') or \
not hasattr(klass.__info__, 'get_vfuncs'):
not hasattr(klass.__info__, 'get_vfuncs'):
continue
continue
vfuncs = klass.__info__.get_vfuncs()
vfuncs = klass.__info__.get_vfuncs()
vfunc_name = vfunc.get_name()
vfunc_name = vfunc.get_name()
for v in vfuncs:
for v in vfuncs:
if v.get_name() == vfunc_name and v != vfunc:
if v.get_name() == vfunc_name and v != vfunc:
return klass
return klass
aklass = find_vfunc_conflict_in_bases(vfunc, klass.__bases__)
aklass = find_vfunc_conflict_in_bases(vfunc, klass.__bases__)
if aklass is not None:
if aklass is not None:
return aklass
return aklass
return None
return None
class GObjectMeta(_gobject.GObjectMeta, MetaClassHelper):
class _GObjectMetaBase(type):
"""Metaclass for automatically registering GObject classes."""
def __init__(cls, name, bases, dict_):
type.__init__(cls, name, bases, dict_)
propertyhelper.install_properties(cls)
signalhelper.install_signals(cls)
cls._type_register(cls.__dict__)
def _type_register(cls, namespace):
## don't register the class if already registered
if '__gtype__' in namespace:
return
# Do not register a new GType for the overrides, as this would sort of
# defeat the purpose of overrides...
if cls.__module__.startswith('gi.overrides.'):
return
_gobject.type_register(cls, namespace.get('__gtype_name__'))
_gobject._install_metaclass(_GObjectMetaBase)
class GObjectMeta(_GObjectMetaBase, MetaClassHelper):
"""Meta class used for GI GObject based types."""
def __init__(cls, name, bases, dict_):
def __init__(cls, name, bases, dict_):
super(GObjectMeta, cls).__init__(name, bases, dict_)
super(GObjectMeta, cls).__init__(name, bases, dict_)
is_gi_defined = False
is_gi_defined = False
if cls.__module__ == 'gi.repository.' + cls.__info__.get_namespace():
if cls.__module__ == 'gi.repository.' + cls.__info__.get_namespace():
is_gi_defined = True
is_gi_defined = True
is_python_defined = False
is_python_defined = False
if not is_gi_defined and cls.__module__ != GObjectMeta.__module__:
if not is_gi_defined and cls.__module__ != GObjectMeta.__module__:
is_python_defined = True
is_python_defined = True
if is_python_defined:
if is_python_defined:
cls._setup_vfuncs()
cls._setup_vfuncs()
elif is_gi_defined:
elif is_gi_defined:
cls._setup_methods()
cls._setup_methods()
cls._setup_constants()
cls._setup_constants()
cls._setup_native_vfuncs()
cls._setup_native_vfuncs()
if isinstance(cls.__info__, ObjectInfo):
if isinstance(cls.__info__, ObjectInfo):
cls._setup_fields()
cls._setup_fields()
cls._setup_constructors()
elif isinstance(cls.__info__, InterfaceInfo):
elif isinstance(cls.__info__, InterfaceInfo):
register_interface_info(cls.__info__.get_g_type())
register_interface_info(cls.__info__.get_g_type())
def mro(cls):
def mro(cls):
return mro(cls)
return mro(cls)
@property
def __doc__(cls):
if cls == GObjectMeta:
return ''
return generate_doc_string(cls.__info__)
def mro(C):
def mro(C):
"""Compute the class precedence list (mro) according to C3
"""Compute the class precedence list (mro) according to C3, with GObject
interface considerations.
We override Python's MRO calculation to account for the fact that
GObject classes are not affected by the diamond problem:
http://en.wikipedia.org/wiki/Diamond_problem
Based on http://www.python.org/download/releases/2.3/mro/
Based on http://www.python.org/download/releases/2.3/mro/
Modified to consider that interfaces don't create the diamond problem
"""
"""
# TODO: If this turns out being too slow, consider using generators
# TODO: If this turns out being too slow, consider using generators
bases = []
bases = []
bases_of_subclasses = [[C]]
bases_of_subclasses = [[C]]
if C.__bases__:
if C.__bases__:
bases_of_subclasses += list(map(mro, C.__bases__)) + [list(C.__bases__)]
for base in C.__bases__:
# Python causes MRO's to be calculated starting with the lowest
# base class and working towards the descendant, storing the result
# in __mro__ at each point. Therefore at this point we know that
# we already have our base class MRO's available to us, there is
# no need for us to (re)calculate them.
if hasattr(base, '__mro__'):
bases_of_subclasses += [list(base.__mro__)]
else:
warnings.warn('Mixin class %s is an old style class, please '
'update this to derive from "object".' % base,
RuntimeWarning)
# For old-style classes (Python2 only), the MRO is not
# easily accessible. As we do need it here, we calculate
# it via recursion, according to the C3 algorithm. Using C3
# for old style classes deviates from Python's own behaviour,
# but visible effects here would be a corner case triggered by
# questionable design.
bases_of_subclasses += [mro(base)]
bases_of_subclasses += [list(C.__bases__)]
while bases_of_subclasses:
while bases_of_subclasses:
for subclass_bases in bases_of_subclasses:
for subclass_bases in bases_of_subclasses:
candidate = subclass_bases[0]
candidate = subclass_bases[0]
not_head = [s for s in bases_of_subclasses if candidate in s[1:]]
not_head = [s for s in bases_of_subclasses if candidate in s[1:]]
if not_head and _gobject.GInterface not in candidate.__bases__:
if not_head and GInterface not in candidate.__bases__:
candidate = None # conflict, reject candidate
candidate = None # conflict, reject candidate
else:
else:
break
break
if candidate is None:
if candidate is None:
raise TypeError('Cannot create a consistent method resolution '
raise TypeError('Cannot create a consistent method resolution '
'order (MRO)')
'order (MRO)')
bases.append(candidate)
bases.append(candidate)
for subclass_bases in bases_of_subclasses[:]: # remove candidate
for subclass_bases in bases_of_subclasses[:]: # remove candidate
if subclass_bases and subclass_bases[0] == candidate:
if subclass_bases and subclass_bases[0] == candidate:
del subclass_bases[0]
del subclass_bases[0]
if not subclass_bases:
if not subclass_bases:
bases_of_subclasses.remove(subclass_bases)
bases_of_subclasses.remove(subclass_bases)
return bases
return bases
class StructMeta(type, MetaClassHelper):
class StructMeta(type, MetaClassHelper):
"""Meta class used for GI Struct based types."""
def __init__(cls, name, bases, dict_):
def __init__(cls, name, bases, dict_):
super(StructMeta, cls).__init__(name, bases, dict_)
super(StructMeta, cls).__init__(name, bases, dict_)
# Avoid touching anything else than the base class.
# Avoid touching anything else than the base class.
g_type = cls.__info__.get_g_type()
g_type = cls.__info__.get_g_type()
if g_type != _gobject.TYPE_INVALID and g_type.pytype is not None:
if g_type != TYPE_INVALID and g_type.pytype is not None:
return
return
cls._setup_fields()
cls._setup_fields()
cls._setup_methods()
cls._setup_methods()
cls._setup_constructors()
for method_info in cls.__info__.get_methods():
for method_info in cls.__info__.get_methods():
if method_info.is_constructor() and \
if method_info.is_constructor() and \
method_info.get_name() == 'new' and \
method_info.__name__ == 'new' and \
not method_info.get_arguments():
not method_info.get_arguments():
cls.__new__ = staticmethod(Constructor(method_info))
cls.__new__ = staticmethod(method_info)
break
break
@property
def __doc__(cls):
if cls == StructMeta:
return ''
return generate_doc_string(cls.__info__)