Compatibility module to provide backwards compatibility for useful Python features.

This is mainly for use of internal Twisted code. We encourage you to use the latest version of Python directly from your code, if possible.

Variable unicode The type of Unicode strings, unicode on Python 2 and str on Python 3.
Variable NativeStringIO An in-memory file-like object that operates on the native string type (bytes in Python 2, unicode in Python 3).
Variable urllib_parse a URL-parsing module (urlparse on Python 2, urllib.parse on Python 3)
Function currentframe In Python 3, inspect.currentframe does not take a stack-level argument. Restore that functionality from Python 2 so we don't have to re-implement the f_back-walking loop in places where it's called.
Function inet_pton Emulator of socket.inet_pton.
Function inet_ntop Undocumented
Function execfile Execute a Python script in the given namespaces.
Function cmp Compare two objects.
Function comparable Class decorator that ensures support for the special __cmp__ method.
Function ioType Determine the type which will be returned from the given file object's read() and accepted by its write() method as an argument.
Function nativeString Convert bytes or unicode to the native str type, using ASCII encoding if conversion is necessary.
Function reraise Undocumented
Function iterbytes 0 Undocumented
Function intToBytes 0 Undocumented
Function lazyByteSlice Return a copy of the given bytes-like object.
Function networkString 0 Undocumented
Function iterbytes Undocumented
Function intToBytes Undocumented
Function networkString Undocumented
Function iteritems 0 Undocumented
Function itervalues 0 Undocumented
Function items 0 Undocumented
Function iteritems Undocumented
Function itervalues Undocumented
Function items Undocumented
Function bytesEnviron Return a dict of os.environ where all text-strings are encoded into bytes.
Function _shouldEnableNewStyle 0 No summary
Function _shouldEnableNewStyle No summary
Function _matchingString No summary
Function _keys Return a list of the keys of d.
Function _constructMethod Construct a bound method.
Function _bytesChr Like chr but always works on ASCII, returning bytes.
Function _coercedUnicode Coerce ASCII-only byte strings into unicode for Python 2.
Function _bytesRepr Provide a repr for a byte string that begins with 'b' on both Python 2 and 3.
unicode =
The type of Unicode strings, unicode on Python 2 and str on Python 3.
NativeStringIO =
An in-memory file-like object that operates on the native string type (bytes in Python 2, unicode in Python 3).
urllib_parse =
a URL-parsing module (urlparse on Python 2, urllib.parse on Python 3)
def _shouldEnableNewStyle 0(): (source)

Returns whether or not we should enable the new-style conversion of old-style classes. It inspects the environment for TWISTED_NEWSTYLE, accepting an empty string, no, false, False, and 0 as falsey values and everything else as a truthy value.

Returns (type: bool)
def _shouldEnableNewStyle(): (source)

Returns whether or not we should enable the new-style conversion of old-style classes. It inspects the environment for TWISTED_NEWSTYLE, accepting an empty string, no, false, False, and 0 as falsey values and everything else as a truthy value.

Returns (type: bool)
def currentframe(n=0): (source)

In Python 3, inspect.currentframe does not take a stack-level argument. Restore that functionality from Python 2 so we don't have to re-implement the f_back-walking loop in places where it's called.

ParametersnThe number of stack levels above the caller to walk. (type: int)
Returnsa frame, n levels up the stack from the caller. (type: types.FrameType)
def inet_pton(af, addr): (source)

Emulator of socket.inet_pton.

ParametersafAn address family to parse; socket.AF_INET or socket.AF_INET6. (type: int)
addrAn address. (type: native str)
ReturnsThe binary packed version of the passed address. (type: bytes)
def inet_ntop(af, addr): (source)
Undocumented
def execfile(filename, globals, locals=None): (source)

Execute a Python script in the given namespaces.

Similar to the execfile builtin, but a namespace is mandatory, partly because that's a sensible thing to require, and because otherwise we'd have to do some frame hacking.

This is a compatibility implementation for Python 3 porting, to avoid the use of the deprecated builtin execfile function.

def cmp(a, b): (source)

Compare two objects.

Returns a negative number if a < b, zero if they are equal, and a positive number if a > b.

def comparable(klass): (source)

Class decorator that ensures support for the special __cmp__ method.

On Python 2 this does nothing.

On Python 3, __eq__, __lt__, etc. methods are added to the class, relying on __cmp__ to implement their comparisons.

def ioType(fileIshObject, default=unicode): (source)

Determine the type which will be returned from the given file object's read() and accepted by its write() method as an argument.

In other words, determine whether the given file is 'opened in text mode'.

ParametersfileIshObjectAny object, but ideally one which resembles a file. (type: object)
defaultA default value to return when the type of fileIshObject cannot be determined. (type: type)
ReturnsThere are 3 possible return values:
  1. unicode, if the file is unambiguously opened in text mode.
  2. bytes, if the file is unambiguously opened in binary mode.
  3. basestring, if we are on python 2 (the basestring type does not exist on python 3) and the file is opened in binary mode, but has an encoding and can therefore accept both bytes and text reliably for writing, but will return bytes from read methods.
  4. The default parameter, if the given type is not understood.
(type: type)
def nativeString(s): (source)

Convert bytes or unicode to the native str type, using ASCII encoding if conversion is necessary.

RaisesUnicodeErrorThe input string is not ASCII encodable/decodable.
TypeErrorThe input is neither bytes nor unicode.
def _matchingString(constantString, inputString): (source)

Some functions, such as os.path.join, operate on string arguments which may be bytes or text, and wish to return a value of the same type. In those cases you may wish to have a string constant (in the case of os.path.join, that constant would be os.path.sep) involved in the parsing or processing, that must be of a matching type in order to use string operations on it. _matchingString will take a constant string (either bytes or unicode) and convert it to the same type as the input string. constantString should contain only characters from ASCII; to ensure this, it will be encoded or decoded regardless.

ParametersconstantStringA string literal used in processing. (type: unicode or bytes)
inputStringA byte string or text string provided by the user. (type: unicode or bytes)
ReturnsconstantString converted into the same type as inputString (type: the type of inputString)
def reraise(exception, traceback): (source)
Undocumented
def iterbytes 0(originalBytes): (source)
Undocumented
def intToBytes 0(i): (source)
Undocumented
def lazyByteSlice(object, offset=0, size=None): (source)

Return a copy of the given bytes-like object.

If an offset is given, the copy starts at that offset. If a size is given, the copy will only be of that length.

Parametersobjectbytes to be copied.
offsetint, starting index of copy.
sizeOptional, if an int is given limit the length of copy to this size.
def networkString 0(s): (source)
Undocumented
def iterbytes(originalBytes): (source)
Undocumented
def intToBytes(i): (source)
Undocumented
def networkString(s): (source)
Undocumented
def iteritems 0(d): (source)
Undocumented
def itervalues 0(d): (source)
Undocumented
def items 0(d): (source)
Undocumented
def iteritems(d): (source)
Undocumented
def itervalues(d): (source)
Undocumented
def items(d): (source)
Undocumented
def _keys(d): (source)

Return a list of the keys of d.

Returns (type: list)
def bytesEnviron(): (source)

Return a dict of os.environ where all text-strings are encoded into bytes.

This function is POSIX only; environment variables are always text strings on Windows.

def _constructMethod(cls, name, self): (source)

Construct a bound method.

ParametersclsThe class that the method should be bound to. (type: types.ClassType or type.)
nameThe name of the method. (type: native str)
selfThe object that the method is bound to. (type: any object)
Returnsa bound method (type: types.MethodType)
def _bytesChr(i): (source)

Like chr but always works on ASCII, returning bytes.

ParametersiThe ASCII code point to return. (type: int)
Returns (type: bytes)
def _coercedUnicode(s): (source)

Coerce ASCII-only byte strings into unicode for Python 2.

In Python 2 unicode(b'bytes') returns a unicode string 'bytes'. In Python 3, the equivalent str(b'bytes') will return "b'bytes'" instead. This function mimics the behavior for Python 2. It will decode the byte string as ASCII. In Python 3 it simply raises a TypeError when passing a byte string. Unicode strings are returned as-is.

ParameterssThe string to coerce. (type: bytes or unicode)
RaisesUnicodeErrorThe input bytes is not ASCII decodable.
TypeErrorThe input is bytes on Python 3.
def _bytesRepr(bytestring): (source)

Provide a repr for a byte string that begins with 'b' on both Python 2 and 3.

ParametersbytestringThe string to repr. (type: bytes)
ReturnsThe repr with a leading 'b'. (type: bytes)
RaisesTypeErrorThe input is not bytes.
API Documentation for Twisted, generated by pydoctor at 2017-06-11 10:59:01.