def openStream(self, source):
        """Produces a file object from source.

        source can be either a file object, local filename or a string.

        """
        # Already a file object
        if hasattr(source, 'read'):
            stream = source
        else:
            # Otherwise treat source as a string and convert to a file object
            if isinstance(source, unicode):
                source = source.encode('utf-8')
                self.charEncoding = ("utf-8", "certain")
            try:
                from io import BytesIO
            except:
                # 2to3 converts this line to: from io import StringIO  
                from cStringIO import StringIO as BytesIO
            stream = BytesIO(source)

        if (not(hasattr(stream, "tell") and hasattr(stream, "seek")) or
            stream is sys.stdin):
            stream = BufferedStream(stream)

        return stream
Produces a file object from source.

        source can be either a file object, local filename or a string.

        
html5lib/inputstream.py