Repository: cocotb/cocotb

Issue:
Binary representation with Cocotb 1.5
In the class BinaryValue there is the possibility to pass during initialization a parameter called binaryRepresentation
` def __init__(self, value=None, n_bits=None, bigEndian=True,
                 binaryRepresentation=BinaryRepresentation.UNSIGNED,
                 bits=None):`

This setting can later be read out over the 'binaryRepresentation' variable.
While this was editable in cocotb1.4, changing this variable in 1.5 has no influence on the conversion functions any more. 
The representation can only be set during creation of the object. 

I think this change was introduced by @marlonjames on Jan 5

I would suggest to
- make the variable binaryRepresentation private, so users don't try to change it any more. 
or
- update the conversion function when binaryRepresentation is chagned

Maybe you have other inputs?

<!--
In case you are reporting a problem with running cocotb,
please remember to add relevant information about your environment, such as
* the cocotb version used,
* the operating system and version (32/64 bit),
* the simulator and version (32/64 bit),
* the Python version, and where it's coming from (e.g. system, Anaconda, self-installed, ...),
* (part of) the log file (cleaned of proprietary information).
If you are unsure about any of the above items, open the issue anyway and we will figure it out together.
Thanks for helping improve cocotb!
-->



Discussion:
This was anticipated, from https://github.com/cocotb/cocotb/pull/1286#discussion_r364012422. I am surprised it worked for you at any point.

> I would suggest to
> 
> * make the variable binaryRepresentation private, so users don't try to change it any more.
>   or
> * update the conversion function when binaryRepresentation is chagned

For 1.5.x we should maintain backwards compatibility, so the second is the better option. Could you introduce a PR for 1.5.1 to take us back to the old behavior?

As for 2.0. We have stated that `BinaryValue` will be tossed and replaced with a much more usable set of types (#2059). The representation *will not* be associated with the object. They really isn't a good reason to. Instead, we will support multiple conversion functions:

```python
vec = LogicArray[1:'to':8].from_twos_complement(-123)
assert vec.to_unsigned() == 133
```

Or something similar to that. We haven't exactly nailed down how arrays are going to look. Or if we should have a single `to_int`, `from_int` pair that take representation strings or enums.

Resolving patch:
diff --git a/cocotb/binary.py b/cocotb/binary.py
--- a/cocotb/binary.py
+++ b/cocotb/binary.py
@@ -157,13 +157,19 @@ def __init__(self, value=None, n_bits=None, bigEndian=True,
 
         self._n_bits = n_bits
 
-        self._convert_to = self._convert_to_map[self.binaryRepresentation].__get__(self, self.__class__)
-
-        self._convert_from = self._convert_from_map[self.binaryRepresentation].__get__(self, self.__class__)
-
         if value is not None:
             self.assign(value)
 
+    @property
+    def binaryRepresentation(self):
+        return self._binaryRepresentation
+
+    @binaryRepresentation.setter
+    def binaryRepresentation(self, binaryRepresentation):
+        self._binaryRepresentation = binaryRepresentation
+        self._convert_to = self._convert_to_map[binaryRepresentation].__get__(self, self.__class__)
+        self._convert_from = self._convert_from_map[binaryRepresentation].__get__(self, self.__class__)
+
     def assign(self, value):
         """Decides how best to assign the value to the vector.
 


Test patch: