Python cStringIO.InputType() Examples
The following are 6
code examples of cStringIO.InputType().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cStringIO
, or try the search function
.
Example #1
Source File: styles.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def pickleStringI(stringi): """ Reduce the given cStringI. This is only called on Python 2, because the cStringIO module only exists on Python 2. @param stringi: The string input to pickle. @type stringi: L{cStringIO.InputType} @return: a 2-tuple of (C{unpickleStringI}, (bytes, pointer)) @rtype: 2-tuple of (function, (bytes, int)) """ return unpickleStringI, (stringi.getvalue(), stringi.tell())
Example #2
Source File: test_futurize.py From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 | 5 votes |
def test_Py2_StringIO_module(self): """ This requires that the argument to io.StringIO be made a unicode string explicitly if we're not using unicode_literals: Ideally, there would be a fixer for this. For now: TODO: add the Py3 equivalent for this to the docs. Also add back a test for the unicode_literals case. """ before = """ import cStringIO import StringIO s1 = cStringIO.StringIO('my string') s2 = StringIO.StringIO('my other string') assert isinstance(s1, cStringIO.InputType) """ # There is no io.InputType in Python 3. futurize should change this to # something like this. But note that the input to io.StringIO # must be a unicode string on both Py2 and Py3. after = """ import io import io s1 = io.StringIO(u'my string') s2 = io.StringIO(u'my other string') assert isinstance(s1, io.StringIO) """ self.convert_check(before, after)
Example #3
Source File: styles.py From learn_python3_spider with MIT License | 5 votes |
def pickleStringI(stringi): """ Reduce the given cStringI. This is only called on Python 2, because the cStringIO module only exists on Python 2. @param stringi: The string input to pickle. @type stringi: L{cStringIO.InputType} @return: a 2-tuple of (C{unpickleStringI}, (bytes, pointer)) @rtype: 2-tuple of (function, (bytes, int)) """ return unpickleStringI, (stringi.getvalue(), stringi.tell())
Example #4
Source File: restclient.py From pyfan with MIT License | 5 votes |
def isIOBase(obj): return isinstance(obj, file) or isinstance(obj, StringIO) or isinstance(obj, InputType)
Example #5
Source File: compat.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 4 votes |
def ioType(fileIshObject, default=unicode): """ 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'. @param fileIshObject: Any object, but ideally one which resembles a file. @type fileIshObject: L{object} @param default: A default value to return when the type of C{fileIshObject} cannot be determined. @type default: L{type} @return: There are 3 possible return values: 1. L{unicode}, if the file is unambiguously opened in text mode. 2. L{bytes}, if the file is unambiguously opened in binary mode. 3. L{basestring}, if we are on python 2 (the L{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 L{bytes} from read methods. 4. The C{default} parameter, if the given type is not understood. @rtype: L{type} """ if isinstance(fileIshObject, TextIOBase): # If it's for text I/O, then it's for text I/O. return unicode if isinstance(fileIshObject, IOBase): # If it's for I/O but it's _not_ for text I/O, it's for bytes I/O. return bytes encoding = getattr(fileIshObject, 'encoding', None) import codecs if isinstance(fileIshObject, (codecs.StreamReader, codecs.StreamWriter)): # On StreamReaderWriter, the 'encoding' attribute has special meaning; # it is unambiguously unicode. if encoding: return unicode else: return bytes if not _PY3: # Special case: if we have an encoding file, we can *give* it unicode, # but we can't expect to *get* unicode. if isinstance(fileIshObject, file): if encoding is not None: return basestring else: return bytes from cStringIO import InputType, OutputType from StringIO import StringIO if isinstance(fileIshObject, (StringIO, InputType, OutputType)): return bytes return default
Example #6
Source File: compat.py From learn_python3_spider with MIT License | 4 votes |
def ioType(fileIshObject, default=unicode): """ 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'. @param fileIshObject: Any object, but ideally one which resembles a file. @type fileIshObject: L{object} @param default: A default value to return when the type of C{fileIshObject} cannot be determined. @type default: L{type} @return: There are 3 possible return values: 1. L{unicode}, if the file is unambiguously opened in text mode. 2. L{bytes}, if the file is unambiguously opened in binary mode. 3. L{basestring}, if we are on python 2 (the L{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 L{bytes} from read methods. 4. The C{default} parameter, if the given type is not understood. @rtype: L{type} """ if isinstance(fileIshObject, TextIOBase): # If it's for text I/O, then it's for text I/O. return unicode if isinstance(fileIshObject, IOBase): # If it's for I/O but it's _not_ for text I/O, it's for bytes I/O. return bytes encoding = getattr(fileIshObject, 'encoding', None) import codecs if isinstance(fileIshObject, (codecs.StreamReader, codecs.StreamWriter)): # On StreamReaderWriter, the 'encoding' attribute has special meaning; # it is unambiguously unicode. if encoding: return unicode else: return bytes if not _PY3: # Special case: if we have an encoding file, we can *give* it unicode, # but we can't expect to *get* unicode. if isinstance(fileIshObject, file): if encoding is not None: return basestring else: return bytes from cStringIO import InputType, OutputType from StringIO import StringIO if isinstance(fileIshObject, (StringIO, InputType, OutputType)): return bytes return default