Robot Framework Integrated Development Environment (RIDE)
encodingsniffer.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 import locale
19 
20 from .platform import JYTHON, PY2, PY3, UNIXY, WINDOWS
21 
22 
23 if UNIXY:
24  DEFAULT_CONSOLE_ENCODING = 'UTF-8'
25  DEFAULT_SYSTEM_ENCODING = 'UTF-8'
26 else:
27  DEFAULT_CONSOLE_ENCODING = 'cp437'
28  DEFAULT_SYSTEM_ENCODING = 'cp1252'
29 
30 
32  platform_getters = [(True, _get_python_system_encoding),
33  (JYTHON, _get_java_system_encoding),
34  (UNIXY, _get_unixy_encoding),
35  (WINDOWS, _get_windows_system_encoding)]
36  return _get_encoding(platform_getters, DEFAULT_SYSTEM_ENCODING)
37 
38 
40  platform_getters = [(True, _get_stream_output_encoding),
41  (UNIXY, _get_unixy_encoding),
42  (WINDOWS, _get_windows_output_encoding)]
43  return _get_encoding(platform_getters, DEFAULT_CONSOLE_ENCODING)
44 
45 
46 def _get_encoding(platform_getters, default):
47  for platform, getter in platform_getters:
48  if platform:
49  encoding = getter()
50  if _is_valid(encoding):
51  return encoding
52  return default
53 
54 
56  # `locale.getpreferredencoding(False)` returns exactly what we want, but
57  # it doesn't seem to work outside Windows on Python 2. Luckily on these
58  # platforms `sys.getfilesystemencoding()` seems to do the right thing.
59  if PY2 and not WINDOWS:
60  return sys.getfilesystemencoding()
61  return locale.getpreferredencoding(False)
62 
63 
65  from java.lang import System
66  return System.getProperty('file.encoding')
67 
68 
70  # Cannot use `locale.getdefaultlocale()` because it raises ValueError
71  # if encoding is invalid. Using same environment variables here anyway.
72  # https://docs.python.org/3/library/locale.html#locale.getdefaultlocale
73  for name in 'LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE':
74  if name in os.environ:
75  # Encoding can be in format like `UTF-8` or `en_US.UTF-8`
76  encoding = os.environ[name].split('.')[-1]
77  if _is_valid(encoding):
78  return encoding
79  return None
80 
81 
83  # Python < 3.6 on Windows returns different encoding depending on are
84  # outputs redirected or not, and Python >= 3.6 always use UTF-8. We
85  # want the real console encoding regardless the platform.
86  if WINDOWS and PY3:
87  return None
88  # Stream may not have encoding attribute if intercepted outside RF in
89  # Python. Encoding is None if process output is redirected and Python < 3.
90  for stream in sys.__stdout__, sys.__stderr__, sys.__stdin__:
91  encoding = getattr(stream, 'encoding', None)
92  if _is_valid(encoding):
93  return encoding
94  return None
95 
96 
98  return _get_code_page('GetACP')
99 
100 
102  return _get_code_page('GetOEMCP')
103 
104 
105 def _get_code_page(method_name):
106  from ctypes import cdll
107  try:
108  method = getattr(cdll.kernel32, method_name)
109  except TypeError: # Occurred few times with IronPython on CI.
110  return None
111  method.argtypes = () # Needed with Jython.
112  return 'cp%s' % method()
113 
114 
115 def _is_valid(encoding):
116  if not encoding:
117  return False
118  try:
119  'test'.encode(encoding)
120  except LookupError:
121  return False
122  else:
123  return True
def _get_encoding(platform_getters, default)