hdl.ast: use operator.index instead of int.

This ensures things like `Const(1.5)` raise an error.

`int(operator.index())` is used since `operator.index(True)` on Python
3.9 and earlier returns `True` instead of `1`.
This commit is contained in:
Wanda 2024-01-10 06:00:07 +01:00 committed by Catherine
parent f25bf51a92
commit 7e18786c97

View file

@ -2,6 +2,7 @@ from abc import ABCMeta, abstractmethod
import inspect import inspect
import warnings import warnings
import functools import functools
import operator
from collections import OrderedDict from collections import OrderedDict
from collections.abc import Iterable, MutableMapping, MutableSet, MutableSequence from collections.abc import Iterable, MutableMapping, MutableSet, MutableSequence
from enum import Enum, EnumMeta from enum import Enum, EnumMeta
@ -739,7 +740,7 @@ class Const(Value):
def __init__(self, value, shape=None, *, src_loc_at=0): def __init__(self, value, shape=None, *, src_loc_at=0):
# We deliberately do not call Value.__init__ here. # We deliberately do not call Value.__init__ here.
self.value = int(value) self.value = int(operator.index(value))
if shape is None: if shape is None:
shape = Shape(bits_for(self.value), signed=self.value < 0) shape = Shape(bits_for(self.value), signed=self.value < 0)
elif isinstance(shape, int): elif isinstance(shape, int):
@ -919,8 +920,8 @@ class Slice(Value):
super().__init__(src_loc_at=src_loc_at) super().__init__(src_loc_at=src_loc_at)
self.value = value self.value = value
self.start = int(start) self.start = int(operator.index(start))
self.stop = int(stop) self.stop = int(operator.index(stop))
def shape(self): def shape(self):
return Shape(self.stop - self.start) return Shape(self.stop - self.start)