Skip to content

Commit

Permalink
python3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Riolo committed Apr 15, 2015
1 parent fad3a1e commit ab4f3e1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
9 changes: 4 additions & 5 deletions src/martian/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
#
##############################################################################

from zope.interface import implements
from zope.interface import implementer

from martian import util
from martian.error import GrokError
from martian.interfaces import IGrokker, IComponentGrokker
from martian.martiandirective import directive, component

@implementer(IGrokker)
class GrokkerBase(object):
implements(IGrokker)

def grok(self, name, obj, **kw):
raise NotImplementedError
Expand All @@ -34,10 +34,10 @@ def grok(self, name, obj, **kw):
raise NotImplementedError


@implementer(IComponentGrokker)
class ClassGrokker(GrokkerBase):
"""Grokker that groks classes in a module.
"""
implements(IComponentGrokker)

def grok(self, name, class_, module_info=None, **kw):
module = None
Expand Down Expand Up @@ -92,11 +92,10 @@ def grok(self, name, class_, module_info=None, **kw):
def execute(self, class_, method, **data):
raise NotImplementedError


@implementer(IComponentGrokker)
class InstanceGrokker(GrokkerBase):
"""Grokker that groks instances in a module.
"""
implements(IComponentGrokker)

def grok(self, name, class_, **kw):
return self.execute(class_, **kw)
Expand Down
11 changes: 7 additions & 4 deletions src/martian/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import types, inspect

from zope.interface import implements
from zope.interface import implementer

from martian.interfaces import IGrokker, IMultiGrokker
from martian import util, scan
Expand All @@ -9,8 +9,8 @@
from martian.error import GrokError
from martian.martiandirective import component, priority

@implementer(IMultiGrokker)
class MultiGrokkerBase(GrokkerBase):
implements(IMultiGrokker)

def register(self, grokker):
raise NotImplementedError
Expand All @@ -35,9 +35,10 @@ def clear(self):
def grokkers(self, name, obj):
raise NotImplementedError

def _grokker_sort_key((grokker, name, obj)):
def _grokker_sort_key(args):
"""Helper function to calculate sort order of grokker.
"""
grokker, name, obj = args
return priority.bind().get(grokker)

class ModuleGrokker(MultiGrokkerBase):
Expand Down Expand Up @@ -179,7 +180,9 @@ def clear(self):
self._multi_global_grokker = MultiGlobalGrokker()

def grokkers(self, name, obj):
if isinstance(obj, (type, types.ClassType)):
# python3 compatible
if isinstance(obj, type) or (hasattr(types, 'ClassType')
and isinstance(obj, types.ClassType)):
return self._multi_class_grokker.grokkers(name, obj)
elif isinstance(obj, types.ModuleType):
return self._multi_global_grokker.grokkers(name, obj)
Expand Down
8 changes: 4 additions & 4 deletions src/martian/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _default(mro, get_default):
if util.is_baseclass(base):
break
result = get_default(base, module_of_base)
except UnknownError, e:
except UnknownError as e:
# store error if this is the first UnknownError we ran into
if error is None:
error = e
Expand Down Expand Up @@ -246,8 +246,8 @@ def check_factory_signature(self, *arguments, **kw):
argspec = inspect.formatargspec(args[1:], varargs, varkw, defaults)
exec("def signature_checker" + argspec + ": pass")
try:
signature_checker(*arguments, **kw)
except TypeError, e:
locals()['signature_checker'](*arguments, **kw)
except TypeError as e:
message = e.args[0]
message = message.replace("signature_checker()", self.name)
raise TypeError(message)
Expand Down Expand Up @@ -335,4 +335,4 @@ def validateInterface(directive, value):
# in the fake module being tested and not in the FakeModule base class;
# the system cannot find it on the frame if it is in the base class.
def is_fake_module(frame):
return frame.f_locals.has_key('fake_module')
return 'fake_module' in frame.f_locals
6 changes: 3 additions & 3 deletions src/martian/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import os

from zope.interface import implements
from zope.interface import implementer

from martian.interfaces import IModuleInfo

Expand All @@ -29,8 +29,8 @@ def is_package(path):
return os.path.isfile(init_py) or os.path.isfile(init_pyc)


@implementer(IModuleInfo)
class ModuleInfo(object):
implements(IModuleInfo)

def __init__(self, path, dotted_name, exclude_filter=None,
ignore_nonsource=True):
Expand Down Expand Up @@ -151,8 +151,8 @@ class BuiltinDummyModule(object):
"""Needed for BuiltinModuleInfo"""
pass

@implementer(IModuleInfo)
class BuiltinModuleInfo(object):
implements(IModuleInfo)

# to let view grokking succeed in tests
package_dotted_name = 'dummy.dotted.name'
Expand Down
8 changes: 6 additions & 2 deletions src/martian/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
from martian.error import GrokError, GrokImportError

def not_unicode_or_ascii(value):
if isinstance(value, unicode):

# python3 compatibility
if sys.version_info<(3,) and isinstance(value, unicode):
return False
if not isinstance(value, str):
return True
Expand All @@ -36,7 +38,9 @@ def not_unicode_or_ascii(value):
def isclass(obj):
"""We cannot use ``inspect.isclass`` because it will return True
for interfaces"""
return isinstance(obj, (types.ClassType, type))
# python3 compatible
return isinstance(obj, type) or (hasattr(types, 'ClassType') and
isinstance(obj, types.ClassType))


def check_subclass(obj, class_):
Expand Down

0 comments on commit ab4f3e1

Please sign in to comment.