module documentation
(source)

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 iteritems Return an iterable of the items of d.
Function itervalues Return an iterable of the values of d.
Function items Return a list of the items of d.
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 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 str to str type, using ASCII encoding if conversion is necessary.
Function reraise Re-raise an exception, with an optional traceback.
Function iterbytes Return an iterable wrapper for a bytes object that provides the behavior of iterating over bytes on Python 2.
Function intToBytes Convert the given integer into bytes, as ASCII-encoded Arab numeral.
Function lazyByteSlice Return a copy of the given bytes-like object.
Function networkString Convert a string to bytes using ASCII encoding.
Function bytesEnviron Return a dict of os.environ where all text-strings are encoded into bytes.
Variable _PY37PLUS Undocumented
Variable _PYPY Undocumented
Function _matchingString No summary
Function _constructMethod Construct a bound method.
Function _get_async_param Provide a backwards-compatible way to get async param value that does not cause a syntax error under Python 3.7.
Function _pypy3BlockingHack Work around this pypy bug by replacing socket.fromfd with a more conservative version.
unicode = (source)
The type of Unicode strings, unicode on Python 2 and str on Python 3.
NativeStringIO = (source)
An in-memory file-like object that operates on the native string type (bytes in Python 2, unicode in Python 3).
urllib_parse = (source)
a URL-parsing module (urlparse on Python 2, urllib.parse on Python 3)
_PY37PLUS = (source)

Undocumented

(type: bool)
_PYPY = (source)

Undocumented

(type: bool)
def iteritems(d): (source)

Return an iterable of the items of d.

ParametersdUndocumented (type: dict)
ReturnsUndocumented (type: iterable)
def itervalues(d): (source)

Return an iterable of the values of d.

ParametersdUndocumented (type: dict)
ReturnsUndocumented (type: iterable)
def items(d): (source)

Return a list of the items of d.

ParametersdUndocumented (type: dict)
ReturnsUndocumented (type: list)
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 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.

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

def ioType(fileIshObject, default=str): (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. str, if the file is unambiguously opened in text mode.
  2. bytes, if the file is unambiguously opened in binary mode.
  3. The default parameter, if the given type is not understood.
(type: type)
def nativeString(s): (source)

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

ParameterssUndocumented (type: AnyStr)
ReturnsUndocumented (type: str)
RaisesUnicodeErrorThe input string is not ASCII encodable/decodable.
TypeErrorThe input is neither bytes nor str.
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 str) 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: str or bytes)
inputStringA byte string or text string provided by the user. (type: str or bytes)
ReturnsconstantString converted into the same type as inputString (type: the type of inputString)
def reraise(exception, traceback): (source)

Re-raise an exception, with an optional traceback.

Re-raised exceptions will be mutated, with their __traceback__ attribute being set.

ParametersexceptionThe exception instance.
tracebackThe traceback to use, or None indicating a new traceback.
def iterbytes(originalBytes): (source)

Return an iterable wrapper for a bytes object that provides the behavior of iterating over bytes on Python 2.

In particular, the results of iteration are the individual bytes (rather than integers as on Python 3).

ParametersoriginalBytesA bytes object that will be wrapped.
def intToBytes(i): (source)

Convert the given integer into bytes, as ASCII-encoded Arab numeral.

ParametersiThe int to convert to bytes. (type: int)
ReturnsUndocumented (type: bytes)
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(s): (source)

Convert a string to bytes using ASCII encoding.

This is useful for sending text-like bytes that are constructed using string interpolation. For example:

    networkString("Hello %d" % (n,))
ParameterssA string to convert to bytes. (type: str)
ReturnsUndocumented (type: bytes)
RaisesUnicodeErrorThe input string is not ASCII encodable.
TypeErrorThe input is not str.
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: 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 _get_async_param(isAsync=None, **kwargs): (source)

Provide a backwards-compatible way to get async param value that does not cause a syntax error under Python 3.7.

ParametersisAsyncisAsync param value (should default to None) (type: bool)
kwargskeyword arguments of the caller (only async is allowed) (type: dict)
ReturnsFinal isAsync param value (type: bool)
RaisesTypeErrorBoth isAsync and async specified.
def _pypy3BlockingHack(): (source)

Work around this pypy bug by replacing socket.fromfd with a more conservative version.

API Documentation for Twisted, generated by pydoctor 20.12.1 at 2021-02-28 19:53:36.