aglyph.compat — Python version/variant compatibility

Release:2.1.1

Note

The details shown here reflect CPython 3 because the documentation is generated by Sphinx running under CPython 3.

This module defines constants, functions, and classes that are used to enable Python version and variant cross-compatibility.

There are two primary goals:

  1. Make the differences between Python 2 str/unicode and Python 3 bytes/str as transparent as possible.
  2. Hide Python API differences behind aliases that can be used in any version or variant.

Keeping these constructs contained in a separate module makes them easier to maintain (and easier to remove later).

aglyph.compat.is_python_2 = False

True if the Python MAJOR version is 2.

aglyph.compat.is_python_3 = True

True if the Python MAJOR version is 3.

aglyph.compat.is_ironpython = False

True if the runtime Python implementation is IronPython.

aglyph.compat.is_jython = False

True if the runtime Python implementation is Jython.

aglyph.compat.is_pypy = False

True if the runtime Python implementation is PyPy.

aglyph.compat.is_stackless = False

True if the runtime Python implementation is Stackless Python.

aglyph.compat.is_gae = False

True if running on the Google App Engine

aglyph.compat.python_implementation = 'CPython'

The name of the runtime Python implementation.

Deprecated since version 2.1.0: Superceded by platform_detail and will be removed inrelease 3.0.0.

aglyph.compat.platform_detail = 'CPython 3.5.2 (Darwin-15.6.0-x86_64-i386-64bit)'

The python implementation, version, and platform information.

aglyph.compat.TextType

The type of Unicode text.

Note

The Python 2 Unicode text type is unicode, while the Python 3 Unicode text type is str.

alias of str

aglyph.compat.DataType

The type of encoded byte data.

Note

The Python 2 encoded byte data type is str, while the Python 3 encoded byte data type is bytes.

alias of bytes

aglyph.compat.StringTypes = (<class 'str'>, <class 'bytes'>)

The types recognized generically as “strings.”

Note

StringTypes is always the 2-tuple ( TextType , DataType ).

aglyph.compat.NoOpLoggingHandler

alias of NullHandler

class aglyph.compat.OrderedDict[source]

Bases: dict

Dictionary that remembers insertion order

clear() → None. Remove all items from od.
copy() → a shallow copy of od
fromkeys(S[, v]) → New ordered dictionary with keys from S.

If not specified, the value defaults to None.

move_to_end()

Move an existing element to the end (or beginning if last==False).

Raises KeyError if the element does not exist. When last=True, acts like a fast version of self[key]=self.pop(key).

pop(k[, d]) → v, remove specified key and return the corresponding

value. If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), return and remove a (key, value) pair.

Pairs are returned in LIFO order if last is true or FIFO order if false.

setdefault(k[, d]) → od.get(k,d), also set od[k]=d if k not in od
aglyph.compat.NULL_LOGGING_HANDLER = <logging.NullHandler object>

The null (no-op) logging handler instance.

aglyph.compat.ClassAndFunctionTypes = (<class 'type'>, <class 'function'>, <class 'builtin_function_or_method'>)

The types of classes and functions.

Note

Old-style classes need to be taken into account in Python 2, but do not exist in Python 3.

aglyph.compat.is_callable()

Return True if the argument is a callable object.

Note

The builtin callable function is not available in Python 3.0 or Python 3.1.

aglyph.compat.new_instance(cls)

Create an uninitialized instance of the class object argument.

Note

Old-style classes need to be taken into account in Python 2, but do not exist in Python 3.

aglyph.compat.RESTRICTED_BUILTINS = <module 'aglyph.compat.RESTRICTED_BUILTINS'>

A module object that defines a “safe” subset of Python builtins.

These builtins are passed in the globals to the builtin eval function.

Deprecated since version 2.0.0: Use a top-level <component> instead of a nested <eval>. This member will be removed in release 3.0.0.

See also

Eval really is dangerous
Ned Batchelder’s insanely thorough discussion of eval
class aglyph.compat.DoctypeTreeBuilder[source]

Bases: xml.etree.ElementTree.TreeBuilder

An xml.etree.ElementTree.TreeBuilder that avoids deprecation warnings for xml.etree.ElementTree.XMLParser.doctype().

See also

Issue14007
xml.etree.ElementTree - XMLParser and TreeBuilder’s doctype() method missing
aglyph.compat.etree_iter(root, tag=None)[source]

Return an iterator over elements named tag rooted at root.

Parameters:root – an xml.etree.ElementTree.ElementTree or xml.etree.ElementTree.Element

Note

Refer to xml.etree.ElementTree.Element.iter() for an explanation of the tag keyword and the return value/type.

xml.etree.ElementTree.Element.iter() is not available in Python 2.6 and 3.1, but is available in 2.7, 3.2, and 3.3.

xml.etree.ElementTree.Element.getiterator() is deprecated, but only in 2.7 and 3.2.

As a work-around, this function will use root.iter if it is defined, or will fall back to using root.getiterator.

aglyph.compat.log_compatibility()[source]

Log the runtime compatibility details.