fastavro / fastavro / six.py

# cython: auto_cpdef=True

'''Compatiblity for Python versions.

Some of this code is "lifted" from CherryPy.
'''
import sys
import json
from sys import stdout

_encoding = 'UTF-8'

if sys.version_info >= (3, 0):
    from io import BytesIO as MemoryIO
    xrange = range

    def py3_btou(n, encoding=_encoding):
        return n.decode(encoding)

    def py3_utob(n, encoding=_encoding):
        return bytes(n, encoding)

    unicode = str
    long = int

    def json_dump(obj, indent):
        json.dump(obj, stdout, indent=indent)

else:  # Python 2x
    from cStringIO import StringIO as MemoryIO
    xrange = xrange

    def py2_btou(n, encoding=_encoding):
        return n


    def py2_utob(n, encoding=_encoding):
        return n

    unicode = unicode
    long = long

    _outenc = getattr(stdout, 'encoding', _encoding)
    def json_dump(obj, indent):
        json.dump(obj, stdout, indent=indent, encoding=_outenc)

# We do it this way and not just redifine function since Cython do not like it
if sys.version_info >= (3, 0):
    btou = py3_btou
    utob = py3_utob
else:
    btou = py2_btou
    utob = py2_utob
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.