Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.9, 1.9.1, and 1.9.2

hglib: add fromunicode() to convert unicode to locale str

It prefers UnicodeEncodeError for failure than replacing silently.

Changeset bc932ef74a9c

Parent fc48fdac0213

by Yuya Nishihara

Changes to one file · Browse files at bc932ef74a9c Showing diff from parent fc48fdac0213 Diff from another changeset...

 
38
39
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
42
43
 
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@@ -38,6 +38,24 @@
  pass   return s.decode(_fallbackencoding, 'replace')   +def fromunicode(s, errors='strict'): + """ + Convert the encoding of string from Unicode to MBCS. + + Return 'str' type string. + + If you don't want an exception for conversion failure, + specify errors='replace'. + """ + s = unicode(s) # s can be QtCore.QString + for enc in (_encoding, _fallbackencoding): + try: + return s.encode(enc) + except UnicodeEncodeError: + pass + + return s.encode(_encoding, errors) # last ditch +  def toutf(s):   """   Convert the encoding of string from MBCS to UTF-8.