<p># Copyright 2008-2015 Nokia Networks</p><p># Copyright 2016- Robot Framework Foundation</p><p>#</p><p># Licensed under the Apache License, Version 2.0 (the "License");</p><p># you may not use this file except in compliance with the License.</p><p># You may obtain a copy of the License at</p><p>#</p><p># http://www.apache.org/licenses/LICENSE-2.0</p><p>#</p><p># Unless required by applicable law or agreed to in writing, software</p><p># distributed under the License is distributed on an "AS IS" BASIS,</p><p># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</p><p># See the License for the specific language governing permissions and</p><p># limitations under the License.</p><p>
</p><p> def unic(item):</p><p> if isinstance(item, unicode):</p><p> return item</p><p> if isinstance(item, (bytes, bytearray)):</p><p> try:</p><p> return item.decode('ASCII')</p><p> except UnicodeError:</p><p> return u''.join(chr(b) if b < 128 else '\\x%x' % b</p><p> for b in bytearray(item))</p><p> </p><p> if isinstance(item, (list, dict, tuple)):</p><p> try:</p><p> item = json.dumps(item, ensure_ascii=False, encoding='utf-8')</p><p> except UnicodeDecodeError:</p><p> try:</p><p> item = json.dumps(item, ensure_ascii=False, encoding='gbk')</p><p> except:</p><p> pass</p><p> except:</p><p> pass</p><p> try:</p><p> try:</p><p> return unicode(item)</p><p> except UnicodeError:</p><p> return unic(str(item))</p><p> except:</p><p> return _unrepresentable_object(item)</p><p>
</p><p>else:</p><p>
</p><p> def unic(item):</p><p> if isinstance(item, str):</p><p> return item</p><p> if isinstance(item, (bytes, bytearray)):</p><p> try:</p><p> return item.decode('ASCII')</p><p> except UnicodeError:</p><p> return ''.join(chr(b) if b < 128 else '\\x%x' % b</p><p> for b in item)</p><p> try:</p><p> return str(item)</p><p> except:</p><p> return _unrepresentable_object(item)</p><p>
</p><p>
</p><p># JVM and .NET seem to handle Unicode normalization automatically. Importing</p><p># unicodedata on Jython also takes some time so it's better to avoid it.</p><p>if not (JYTHON or IRONPYTHON):</p><p>
</p><p> from unicodedata import normalize</p><p> _unic = unic</p><p>