Robot Framework Integrated Development Environment (RIDE)
encoding.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import os
17 import sys
18 
19 from .encodingsniffer import get_console_encoding, get_system_encoding
20 from .compat import isatty
21 from .platform import JYTHON, IRONPYTHON, PY3
22 from .robottypes import is_unicode
23 from .unic import unic
24 
25 
26 CONSOLE_ENCODING = get_console_encoding()
27 SYSTEM_ENCODING = get_system_encoding()
28 # IronPython and Jython streams have wrong encoding if outputs are redirected.
29 # Jython gets it right if PYTHONIOENCODING is set, though.
30 NON_TTY_ENCODING_CAN_BE_TRUSTED = \
31  not (IRONPYTHON or JYTHON and not os.getenv('PYTHONIOENCODING'))
32 
33 
34 
45 def console_decode(string, encoding=CONSOLE_ENCODING, force=False):
46  if is_unicode(string) and not (IRONPYTHON and force):
47  return string
48  encoding = {'CONSOLE': CONSOLE_ENCODING,
49  'SYSTEM': SYSTEM_ENCODING}.get(encoding.upper(), encoding)
50  try:
51  return string.decode(encoding)
52  except UnicodeError:
53  return unic(string)
54 
55 
56 
62 def console_encode(string, errors='replace', stream=sys.__stdout__):
63  encoding = _get_console_encoding(stream)
64  if PY3 and encoding != 'UTF-8':
65  return string.encode(encoding, errors).decode(encoding)
66  if PY3 or IRONPYTHON:
67  return string
68  return string.encode(encoding, errors)
69 
70 
72  encoding = getattr(stream, 'encoding', None)
73  if encoding and (NON_TTY_ENCODING_CAN_BE_TRUSTED or isatty(stream)):
74  return encoding
75  return CONSOLE_ENCODING if isatty(stream) else SYSTEM_ENCODING
76 
77 
78 # These interpreters handle communication with system APIs using Unicode.
79 if PY3 or JYTHON or IRONPYTHON:
80 
81  def system_decode(string):
82  return string if is_unicode(string) else unic(string)
83 
84  def system_encode(string, errors='replace'):
85  return string if is_unicode(string) else unic(string)
86 
87 else:
88 
89 
90  def system_decode(string):
91  try:
92  return string.decode(SYSTEM_ENCODING)
93  except UnicodeError:
94  return unic(string)
95 
96 
100  def system_encode(string, errors='replace'):
101  if not is_unicode(string):
102  string = unic(string)
103  return string.encode(SYSTEM_ENCODING, errors)
def system_decode(string)
Decodes bytes from system (e.g.
Definition: encoding.py:81
def console_decode(string, encoding=CONSOLE_ENCODING, force=False)
Decodes bytes from console encoding to Unicode.
Definition: encoding.py:45
def system_encode(string, errors='replace')
Encodes Unicode to system encoding (e.g.
Definition: encoding.py:84
def console_encode(string, errors='replace', stream=sys.__stdout__)
Encodes Unicode to bytes in console or system encoding.
Definition: encoding.py:62