;ò
ëŒü?c           @   st   d  Z  y d k l Z Wn e j
 o d Z n Xd g Z d f  d „  ƒ  YZ d „  Z e d j o e ƒ  n d S(   s	  File-like objects that read from or write to a string buffer.

This implements (nearly) all stdio methods.

f = StringIO()      # ready for writing
f = StringIO(buf)   # ready for reading
f.close()           # explicitly release resources held
flag = f.isatty()   # always false
pos = f.tell()      # get current position
f.seek(pos)         # set current position
f.seek(pos, mode)   # mode 0: absolute; 1: relative; 2: relative to EOF
buf = f.read()      # read until EOF
buf = f.read(n)     # read up to n bytes
buf = f.readline()  # read until end of line ('
') or EOF
list = f.readlines()# list of f.readline() results until EOF
f.truncate([size])  # truncate file at to at most size (default: current pos)
f.write(buf)        # write at current position
f.writelines(list)  # for line in list: f.write(line)
f.getvalue()        # return whole file's contents as a string

Notes:
- Using a real file is often faster (but less convenient).
- There's also a much faster implementation in C, called cStringIO, but
  it's not subclassable.
- fileno() is left unimplemented so that code which uses it triggers
  an exception early.
- Seeking far beyond EOF and then writing will insert real null
  bytes that occupy space in the buffer.
- There's a simple test set (see end of this file).
(   s   EINVALi   s   StringIOc           B   s§   t  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d d „ Z d	 „  Z	 d
 d „ Z
 e d „ Z d d „ Z e d „ Z d „  Z d „  Z d „  Z d „  Z RS(   sô  class StringIO([buffer])

    When a StringIO object is created, it can be initialized to an existing
    string by passing the string to the constructor. If no string is given,
    the StringIO will start empty.

    The StringIO object can accept either Unicode or 8-bit strings, but
    mixing the two may take some care. If both are used, 8-bit strings that
    cannot be interpreted as 7-bit ASCII (that use the 8th bit) will cause
    a UnicodeError to be raised when getvalue() is called.
    s    c         C   sa   t  | t ƒ o t | ƒ } n | |  _ t | ƒ |  _ g  |  _ d |  _ d |  _ d |  _	 d  S(   Ni    (
   s
   isinstances   bufs
   basestrings   strs   selfs   lens   buflists   poss   closeds	   softspace(   s   selfs   buf(    (    s%   /mit/python/lib/python2.3/StringIO.pys   __init__2   s    				c         C   s   |  Sd  S(   N(   s   self(   s   self(    (    s%   /mit/python/lib/python2.3/StringIO.pys   __iter__=   s    c         C   s:   |  i o
 t ‚ n |  i ƒ  } | o
 t ‚ n | Sd  S(   N(   s   selfs   closeds   StopIterations   readlines   r(   s   selfs   r(    (    s%   /mit/python/lib/python2.3/StringIO.pys   next@   s    


c         C   s(   |  i o d |  _ |  ` |  ` n d S(   s    Free the memory buffer.
        i   N(   s   selfs   closeds   bufs   pos(   s   self(    (    s%   /mit/python/lib/python2.3/StringIO.pys   closeH   s     	c         C   s   |  i o t d ‚ n t Sd  S(   Ns   I/O operation on closed file(   s   selfs   closeds
   ValueErrors   False(   s   self(    (    s%   /mit/python/lib/python2.3/StringIO.pys   isattyO   s    
i    c         C   s›   |  i o t d ‚ n |  i o( |  i d i |  i ƒ 7_ g  |  _ n | d j o | |  i 7} n | d j o | |  i 7} n t	 d | ƒ |  _ d  S(   Ns   I/O operation on closed files    i   i   i    (
   s   selfs   closeds
   ValueErrors   buflists   bufs   joins   modes   poss   lens   max(   s   selfs   poss   mode(    (    s%   /mit/python/lib/python2.3/StringIO.pys   seekT   s    

c         C   s"   |  i o t d ‚ n |  i Sd  S(   Ns   I/O operation on closed file(   s   selfs   closeds
   ValueErrors   pos(   s   self(    (    s%   /mit/python/lib/python2.3/StringIO.pys   tell`   s    
iÿÿÿÿc         C   s    |  i o t d ‚ n |  i o( |  i d i |  i ƒ 7_ g  |  _ n | d j  o |  i } n t	 |  i
 | |  i ƒ } |  i |  i
 | !} | |  _
 | Sd  S(   Ns   I/O operation on closed files    i    (   s   selfs   closeds
   ValueErrors   buflists   bufs   joins   ns   lens   newposs   mins   poss   r(   s   selfs   ns   newposs   r(    (    s%   /mit/python/lib/python2.3/StringIO.pys   reade   s    

	c         C   sß   |  i o t d ‚ n |  i o( |  i d i |  i ƒ 7_ g  |  _ n |  i i d |  i ƒ } | d j  o |  i	 } n | d } | t j	 o) |  i | | j  o |  i | } q» n |  i |  i | !} | |  _ | Sd  S(   Ns   I/O operation on closed files    s   
i    i   (   s   selfs   closeds
   ValueErrors   buflists   bufs   joins   finds   poss   is   lens   newposs   lengths   Nones   r(   s   selfs   lengths   is   newposs   r(    (    s%   /mit/python/lib/python2.3/StringIO.pys   readlines   s    


	c         C   s{   d } g  } |  i ƒ  } xX | oP | i | ƒ | t | ƒ 7} d | j  o
 | j n o Pn |  i ƒ  } q W| Sd  S(   Ni    (   s   totals   liness   selfs   readlines   lines   appends   lens   sizehint(   s   selfs   sizehints   liness   lines   total(    (    s%   /mit/python/lib/python2.3/StringIO.pys	   readlines…   s     c         C   s…   |  i o t d ‚ n | t j o |  i } n> | d j  o t t d ƒ ‚ n | |  i j  o | |  _ n |  i ƒ  |  |  _	 d  S(   Ns   I/O operation on closed filei    s   Negative size not allowed(
   s   selfs   closeds
   ValueErrors   sizes   Nones   poss   IOErrors   EINVALs   getvalues   buf(   s   selfs   size(    (    s%   /mit/python/lib/python2.3/StringIO.pys   truncate‘   s    
c         C   sœ  |  i o t d ‚ n | o d  Sn t | t ƒ o t | ƒ } n |  i |  i j o5 |  i	 i
 | ƒ |  i t | ƒ |  _ |  _ d  Sn |  i |  i j o. |  i	 i
 d |  i |  i ƒ |  i |  _ n |  i t | ƒ } |  i |  i j  o |  i	 o( |  i d i |  i	 ƒ 7_ g  |  _	 n |  i |  i  | |  i | g |  _	 d |  _ | |  i j o | |  _ qn |  i	 i
 | ƒ | |  _ | |  _ d  S(   Ns   I/O operation on closed files    s    (   s   selfs   closeds
   ValueErrors   ss
   isinstances
   basestrings   strs   poss   lens   buflists   appends   newposs   bufs   join(   s   selfs   ss   newpos(    (    s%   /mit/python/lib/python2.3/StringIO.pys   writeœ   s2    
 
#		c         C   s   |  i d i | ƒ ƒ d  S(   Ns    (   s   selfs   writes   joins   list(   s   selfs   list(    (    s%   /mit/python/lib/python2.3/StringIO.pys
   writelines¸   s    c         C   s   |  i o t d ‚ n d  S(   Ns   I/O operation on closed file(   s   selfs   closeds
   ValueError(   s   self(    (    s%   /mit/python/lib/python2.3/StringIO.pys   flush»   s    
c         C   s=   |  i o( |  i d i |  i ƒ 7_ g  |  _ n |  i Sd S(   sº  
        Retrieve the entire contents of the "file" at any time before
        the StringIO object's close() method is called.

        The StringIO object can accept either Unicode or 8-bit strings,
        but mixing the two may take some care. If both are used, 8-bit
        strings that cannot be interpreted as 7-bit ASCII (that use the
        8th bit) will cause a UnicodeError to be raised when getvalue()
        is called.
        s    N(   s   selfs   buflists   bufs   join(   s   self(    (    s%   /mit/python/lib/python2.3/StringIO.pys   getvalue¿   s
    
 
(   s   __name__s
   __module__s   __doc__s   __init__s   __iter__s   nexts   closes   isattys   seeks   tells   reads   Nones   readlines	   readliness   truncates   writes
   writeliness   flushs   getvalue(    (    (    s%   /mit/python/lib/python2.3/StringIO.pys   StringIO&   s     								c    	      C   s8  d  k  } | i d o | i d } n d } t | d ƒ i ƒ  } t | d ƒ i ƒ  } t ƒ  }  x | d  D] } |  i | ƒ ql W|  i | d ƒ |  i ƒ  | j o t d ‚ n |  i ƒ  } d G| GH|  i t | d ƒ ƒ |  i | d ƒ |  i d ƒ d G|  i ƒ  GHd	 G|  i ƒ  GH|  i ƒ  } d
 G| GH|  i t | ƒ d ƒ |  i t | ƒ ƒ } | | j o t d ‚ n |  i t | ƒ d ƒ |  i ƒ  } | d } |  i |  i ƒ  t | ƒ ƒ |  i ƒ  } | | j o t d ‚ n d Gt | ƒ Gd GHd G|  i ƒ  GH|  i ƒ  | j o t d ‚ n |  i ƒ  d  S(   Ni   s   /etc/passwds   riþÿÿÿs   write faileds   File length =i    s   First line =s
   Position =s   Second line =s   bad result after seek backiÿÿÿÿs#   bad result after seek back from EOFs   Reads
   more liness
   bad length(   s   syss   argvs   files   opens	   readliness   liness   reads   texts   StringIOs   fs   lines   writes
   writeliness   getvalues   RuntimeErrors   tells   lengths   seeks   lens   readlines   line2s   lists   close(	   s   fs   texts   liness   line2s   syss   lists   lengths   files   line(    (    s%   /mit/python/lib/python2.3/StringIO.pys   testÒ   sL    		 	

s   __main__N(   s   __doc__s   errnos   EINVALs   ImportErrors   __all__s   StringIOs   tests   __name__(   s   __all__s   StringIOs   tests   EINVAL(    (    s%   /mit/python/lib/python2.3/StringIO.pys   ?   s   	¬	(