我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用cStringIO.OutputType()。
def redirectSTDOUT(filename): if _DEBUG == True: print "redirectSTDOUT(): redirecting stdout/stderr to filename " + str(filename) if type(filename) == str: dirname = os.path.dirname(filename) if len(dirname) == 0 or \ (len(dirname) > 0 and os.path.isdir(dirname)): try: f = open(filename,'w') # Send stdout and stderr to provided filename sys.stdout = f sys.stderr = f except Exception, e: print "redirectSTDOUT(): ERROR - Unable to open file " + str(filename) + " for writing stdout and stderr " + str(e) elif type(filename) == cStringIO.OutputType: sys.stdout = filename sys.stderr = filename else: print 'redirectSTDOUT(): failed to redirect stdout/stderr to ' + str(filename) print 'redirectSTDOUT(): argument must be: string filename, cStringIO.StringIO object'
def determine_how_to_feed_output(handler, encoding, decode_errors): if callable(handler): process, finish = get_callback_chunk_consumer(handler, encoding, decode_errors) # in py3, this is used for bytes elif isinstance(handler, (cStringIO, iocStringIO)): process, finish = get_cstringio_chunk_consumer(handler) # in py3, this is used for unicode elif isinstance(handler, (StringIO, ioStringIO)): process, finish = get_stringio_chunk_consumer(handler, encoding, decode_errors) elif hasattr(handler, "write"): process, finish = get_file_chunk_consumer(handler) else: process = lambda chunk: False finish = lambda: None return process, finish
def unpickleStringO(val, sek): """ Convert the output of L{pickleStringO} into an appropriate type for the current python version. This may be called on Python 3 and will convert a cStringIO into an L{io.StringIO}. @param val: The content of the file. @type val: L{bytes} @param sek: The seek position of the file. @type sek: L{int} @return: a file-like object which you can write bytes to. @rtype: L{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3. """ x = _cStringIO() x.write(val) x.seek(sek) return x
def unpickleStringI(val, sek): """ Convert the output of L{pickleStringI} into an appropriate type for the current Python version. This may be called on Python 3 and will convert a cStringIO into an L{io.StringIO}. @param val: The content of the file. @type val: L{bytes} @param sek: The seek position of the file. @type sek: L{int} @return: a file-like object which you can read bytes from. @rtype: L{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3. """ x = _cStringIO(val) x.seek(sek) return x
def is_string_io(instance): if running_python_3: return isinstance(instance, BytesIO) else: return isinstance(instance, (StringIO.StringIO, cStringIO.InputType, cStringIO.OutputType))
def pickleStringO(stringo): """ Reduce the given cStringO. This is only called on Python 2, because the cStringIO module only exists on Python 2. @param stringo: The string output to pickle. @type stringo: L{cStringIO.OutputType} """ 'support function for copy_reg to pickle StringIO.OutputTypes' return unpickleStringO, (stringo.getvalue(), stringo.tell())
def addMessage(self, message, flags, date=None): """ Adds a message to this mailbox. :param message: the raw message :type message: str :param flags: flag list :type flags: list of str :param date: timestamp :type date: str, or None :return: a deferred that will be triggered with the UID of the added message. """ # TODO should raise ReadOnlyMailbox if not rw. # TODO have a look at the cases for internal date in the rfc # XXX we could treat the message as an IMessage from here # TODO -- fast appends should be definitely solved by Blobs. # A better solution will probably involve implementing MULTIAPPEND # extension or patching imap server to support pipelining. if isinstance(message, (cStringIO.OutputType, StringIO.StringIO, io.BytesIO)): message = message.getvalue() leap_assert_type(message, basestring) if flags is None: flags = tuple() else: flags = tuple(str(flag) for flag in flags) if date is None: date = formatdate(time.time()) d = self.collection.add_msg(message, flags, date=date) d.addCallback(lambda message: message.get_uid()) d.addErrback( lambda failure: self.log.failure('Error while adding msg')) return d
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