Commit 23a92baa authored by Stefan Behnel's avatar Stefan Behnel

adapt metaclass usage to Py2/Py3

parent 595502fc
......@@ -33,6 +33,7 @@ from .StringEncoding import EncodedString, escape_byte_string, split_string_lite
from . import Future
from . import Options
from . import DebugFlags
from ..Utils import add_metaclass
absolute_path_length = 0
......@@ -178,15 +179,19 @@ class CheckAnalysers(type):
return super(CheckAnalysers, cls).__new__(cls, name, bases, attrs)
def _with_metaclass(cls):
if DebugFlags.debug_trace_code_generation:
return add_metaclass(VerboseCodeWriter)(cls)
#return add_metaclass(CheckAnalysers)(cls)
return cls
@_with_metaclass
class Node(object):
# pos (string, int, int) Source file position
# is_name boolean Is a NameNode
# is_literal boolean Is a ConstNode
#__metaclass__ = CheckAnalysers
if DebugFlags.debug_trace_code_generation:
__metaclass__ = VerboseCodeWriter
is_name = 0
is_none = 0
is_nonecheck = 0
......
......@@ -5,6 +5,8 @@ Note: debug information is already imported by the file generated by
Cython.Debugger.Cygdb.make_command_file()
"""
from __future__ import absolute_import
import os
import re
import sys
......@@ -21,9 +23,10 @@ from test import test_support
import gdb
from Cython.Debugger import libcython
from Cython.Debugger import libpython
from Cython.Debugger.Tests import TestLibCython as test_libcython
from .. import libcython
from .. import libpython
from . import TestLibCython as test_libcython
from ...Utils import add_metaclass
# for some reason sys.argv is missing in gdb
sys.argv = ['gdb']
......@@ -50,14 +53,13 @@ class TraceMethodCallMeta(type):
setattr(self, func_name, print_on_call_decorator(func))
@add_metaclass(TraceMethodCallMeta)
class DebugTestCase(unittest.TestCase):
"""
Base class for test cases. On teardown it kills the inferior and unsets
all breakpoints.
"""
__metaclass__ = TraceMethodCallMeta
def __init__(self, name):
super(DebugTestCase, self).__init__(name)
self.cy = libcython.cy
......
......@@ -183,6 +183,25 @@ class PrettyPrinterTrackerMeta(type):
all_pretty_typenames.add(self._typename)
# Class decorator that adds a metaclass and recreates the class with it.
# Copied from 'six'. See Cython/Utils.py.
def _add_metaclass(metaclass):
"""Class decorator for creating a class with a metaclass."""
def wrapper(cls):
orig_vars = cls.__dict__.copy()
slots = orig_vars.get('__slots__')
if slots is not None:
if isinstance(slots, str):
slots = [slots]
for slots_var in slots:
orig_vars.pop(slots_var)
orig_vars.pop('__dict__', None)
orig_vars.pop('__weakref__', None)
return metaclass(cls.__name__, cls.__bases__, orig_vars)
return wrapper
@_add_metaclass(PrettyPrinterTrackerMeta)
class PyObjectPtr(object):
"""
Class wrapping a gdb.Value that's a either a (PyObject*) within the
......@@ -195,8 +214,6 @@ class PyObjectPtr(object):
to corrupt data, etc; this is the debugger, after all.
"""
__metaclass__ = PrettyPrinterTrackerMeta
_typename = 'PyObject'
def __init__(self, gdbval, cast_to=None):
......
......@@ -414,3 +414,20 @@ class LazyStr:
def __radd__(self, left):
return left + self.callback()
# Class decorator that adds a metaclass and recreates the class with it.
# Copied from 'six'.
def add_metaclass(metaclass):
"""Class decorator for creating a class with a metaclass."""
def wrapper(cls):
orig_vars = cls.__dict__.copy()
slots = orig_vars.get('__slots__')
if slots is not None:
if isinstance(slots, str):
slots = [slots]
for slots_var in slots:
orig_vars.pop(slots_var)
orig_vars.pop('__dict__', None)
orig_vars.pop('__weakref__', None)
return metaclass(cls.__name__, cls.__bases__, orig_vars)
return wrapper
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment