1   
 2   
 3   
 4   
 5   
 6   
 7   
 8   
 9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22  """Supports a hybrid Unicode string that knows which encoding is preferable,  
23  and uses this when converting to a string.""" 
24   
26 -    def __new__(newtype, string=u"", encoding=None, errors=None): 
 27          if isinstance(string, unicode): 
28              if errors is None: 
29                  newstring = unicode.__new__(newtype, string) 
30              else: 
31                  newstring = unicode.__new__(newtype, string, errors=errors) 
32              if encoding is None and isinstance(string, autoencode): 
33                  newstring.encoding = string.encoding 
34              else: 
35                  newstring.encoding = encoding 
36          else: 
37              if errors is None and encoding is None: 
38                  newstring = unicode.__new__(newtype, string) 
39              elif errors is None: 
40                  try: 
41                      newstring = unicode.__new__(newtype, string, encoding) 
42                  except LookupError, e: 
43                      raise ValueError(str(e)) 
44              elif encoding is None: 
45                  newstring = unicode.__new__(newtype, string, errors) 
46              else: 
47                  newstring = unicode.__new__(newtype, string, encoding, errors) 
48              newstring.encoding = encoding 
49          return newstring 
 50   
51 -    def join(self, seq): 
 53   
55          if self.encoding is None: 
56              return super(autoencode, self).__str__() 
57          else: 
58              return self.encode(self.encoding)