Example #1
0
from __future__ import absolute_import, division

import types
import inspect

from twisted.python.reflect import namedAny, fullyQualifiedName
from twisted.python.modules import getModule
from twisted.python.compat import _PY3, _shouldEnableNewStyle
from twisted.trial import unittest
from twisted.python import _oldstyle

_skip = None

if _PY3:
    _skip = "Not relevant on Python 3."
elif not _shouldEnableNewStyle():
    _skip = "Not running with TWISTED_NEWSTYLE=1"

forbiddenModules = [
    "twisted._threads",
    "twisted.internet",
    "twisted.logger",
    "twisted.plugins",
    "twisted.positioning",
    "twisted.protocols.haproxy",
    "twisted.python",
    "twisted.script",
    "twisted.tap",
    "twisted.trial",
]
from __future__ import absolute_import, division

import types
import inspect

from twisted.python.reflect import namedAny, fullyQualifiedName
from twisted.python.modules import getModule
from twisted.python.compat import _PY3, _shouldEnableNewStyle
from twisted.trial import unittest
from twisted.python import _oldstyle

_skip = None

if _PY3:
    _skip = "Not relevant on Python 3."
elif not _shouldEnableNewStyle():
    _skip = "Not running with TWISTED_NEWSTYLE=1"



class SomeOldStyleClass:
    """
    I am a docstring!
    """
    bar = "baz"

    def func(self):
        """
        A function on an old style class.

        @return: "hi", for testing.
    """
    if not type(cls) is types.ClassType:
        from twisted.python.reflect import fullyQualifiedName

        raise ValueError(
            ("twisted.python._oldstyle._oldStyle is being used to decorate a "
             "new-style class ({cls}). This should only be used to "
             "decorate old-style classes.").format(
                 cls=fullyQualifiedName(cls)))

    return cls



@_replaceIf(_PY3, passthru)
@_replaceIf(not _shouldEnableNewStyle(), _ensureOldClass)
def _oldStyle(cls):
    """
    A decorator which conditionally converts old-style classes to new-style
    classes. If it is Python 3, or if the C{TWISTED_NEWSTYLE} environment
    variable has a falsey (C{no}, C{false}, C{False}, or C{0}) value in the
    environment, this decorator is a no-op.

    @param cls: An old-style class to convert to new-style.
    @type cls: L{types.ClassType}

    @return: A new-style version of C{cls}.
    """
    _ensureOldClass(cls)
    _bases = cls.__bases__ + (object,)
    return type(cls.__name__, _bases, cls.__dict__)
Example #4
0
    @raises: L{ValueError} if it is a new-style class.
    """
    if not type(cls) is types.ClassType:
        from twisted.python.reflect import fullyQualifiedName

        raise ValueError(
            ("twisted.python._oldstyle._oldStyle is being used to decorate a "
             "new-style class ({cls}). This should only be used to "
             "decorate old-style classes.").format(
                 cls=fullyQualifiedName(cls)))

    return cls


@_replaceIf(_PY3, passthru)
@_replaceIf(not _shouldEnableNewStyle(), _ensureOldClass)
def _oldStyle(cls):
    """
    A decorator which conditionally converts old-style classes to new-style
    classes. If it is Python 3, or if the C{TWISTED_NEWSTYLE} environment
    variable has a falsey (C{no}, C{false}, C{False}, or C{0}) value in the
    environment, this decorator is a no-op.

    @param cls: An old-style class to convert to new-style.
    @type cls: L{types.ClassType}

    @return: A new-style version of C{cls}.
    """
    _ensureOldClass(cls)
    _bases = cls.__bases__ + (object, )
    return type(cls.__name__, _bases, cls.__dict__)