μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
__init__.py
Go to the documentation of this file.
1
2import sys
3
4try:
5 from ._core import *
6except ImportError as e:
7 message = 'Failed to load uHAL bindings.'
8 message += '\nDetails: "{}"'
9
10 if sys.version_info[0] > 2:
11 exec('raise type(e)(message.format(e.msg)).with_traceback(sys.exc_info()[2]) from None')
12 else:
13 exec('raise type(e), message.format(e.message), sys.exc_info()[2]')
14
15
16
18
19if sys.version_info[0] <= 2:
21 return long(int(self))
22 ValWord_uint32.__long__ = _ValWord_to_long
23
24ValWord_uint32.__index__ = ValWord_uint32.__int__
25
26
27def _add_int_method_to_ValWord(method_name, unary=False):
28 if unary:
29 def valWord_method(self):
30 int_method = getattr(type(int(self)), method_name)
31 return int_method( int(self) )
32 else:
33 def valWord_method(self, other):
34 int_type = int
35 if (sys.version_info[0] <= 2) and (isinstance(int(self), long) or ( not isinstance(other, str) and isinstance(int(other), long) )):
36 int_type = long
37 int_method = getattr(int_type, method_name)
38 if isinstance(other, int_type) or isinstance(other, str):
39 return int_method( int_type(self), other )
40 else:
41 return int_method( int_type(self), int_type(other) )
42
43 # Add wraparound method to ValWord_uint32
44 setattr(ValWord_uint32, method_name, valWord_method)
45
46
47def _add_int_methods_to_ValWord(method_names, unary=False):
48 for method_name in method_names:
49 _add_int_method_to_ValWord(method_name, unary)
50
51
52# Unary numeric methods
53_add_int_methods_to_ValWord(['__invert__', '__neg__', '__pos__'], unary=True)
54
55# Binary numeric methods
56_add_int_methods_to_ValWord(['__add__', '__radd__',
57 '__sub__', '__rsub__',
58 '__mul__', '__rmul__',
59 '__mod__', '__rmod__',
60 '__pow__', '__rpow__',
61 '__lshift__', '__rlshift__',
62 '__rshift__', '__rrshift__',
63 '__and__', '__rand__',
64 '__or__', '__ror__',
65 '__xor__', '__rxor__'
66 ])
67
68if sys.hexversion >= 0x020600F0:
69 _add_int_method_to_ValWord('__format__')
70
71# Unary comparison operator (used in "if valWord")
72_add_int_method_to_ValWord('__bool__' if (sys.version_info[0] > 2) else '__nonzero__', unary=True)
73
74# Binary comparison operators
75_add_int_methods_to_ValWord(['__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__'] if (sys.version_info[0] > 2) else ['__cmp__'])
76
Definition: pytypes.h:1167
object getattr(handle obj, handle name)
Definition: pytypes.h:537
void setattr(handle obj, handle name, handle value)
Definition: pytypes.h:569
bool isinstance(handle obj)
\rst Return true if obj is an instance of T.
Definition: pytypes.h:489
def _add_int_methods_to_ValWord(method_names, unary=False)
Definition: __init__.py:47
def _add_int_method_to_ValWord(method_name, unary=False)
Definition: __init__.py:27
def _ValWord_to_long(self)
Pythonic additions to the ValWord_uint32 API.
Definition: __init__.py:20
void exec(const str &expr, object global=globals(), object local=object())
Definition: eval.h:88