Robot Framework Integrated Development Environment (RIDE)
compat.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 sys
17 
18 from .platform import IRONPYTHON, PY2
19 
20 
21 if PY2:
22  # io.StringIO only accepts u'foo' with Python 2.
23  from StringIO import StringIO
24 
25 
26  def py2to3(cls):
27  if hasattr(cls, '__unicode__'):
28  cls.__str__ = lambda self: unicode(self).encode('UTF-8')
29  return cls
30 
31 else:
32  from io import StringIO
33 
34 
35  def py2to3(cls):
36  if hasattr(cls, '__unicode__'):
37  cls.__str__ = lambda self: self.__unicode__()
38  if hasattr(cls, '__nonzero__'):
39  cls.__bool__ = lambda self: self.__nonzero__()
40  return cls
41 
42 
43 # Copied from Jinja2, released under the BSD license.
44 # https://github.com/mitsuhiko/jinja2/blob/743598d788528921df825479d64f492ef60bef82/jinja2/_compat.py#L88
45 
46 def with_metaclass(meta, *bases):
47  # This requires a bit of explanation: the basic idea is to make a
48  # dummy metaclass for one level of class instantiation that replaces
49  # itself with the actual metaclass.
50  class metaclass(type):
51  def __new__(cls, name, this_bases, d):
52  return meta(name, bases, d)
53  return type.__new__(metaclass, 'temporary_class', (), {})
54 
55 
56 # On IronPython sys.stdxxx.isatty() always returns True
57 if not IRONPYTHON:
58 
59  def isatty(stream):
60  # first check if buffer was detached
61  if hasattr(stream, 'buffer') and stream.buffer is None:
62  return False
63  return hasattr(stream, 'isatty') and stream.isatty()
64 
65 else:
66 
67  from ctypes import windll
68 
69 
72  _HANDLE_IDS = {sys.__stdout__ : -11, sys.__stderr__ : -12}
73 
76  _CONSOLE_TYPE = 2
77 
78  def isatty(stream):
79  if stream not in _HANDLE_IDS:
80  return False
81  handle = windll.kernel32.GetStdHandle(_HANDLE_IDS[stream])
82  return windll.kernel32.GetFileType(handle) == _CONSOLE_TYPE
unicode
Exceptions and return codes used internally.
Definition: errors.py:24
def with_metaclass(meta, *bases)
Create a base class with a metaclass.
Definition: compat.py:46