From 5de428df07c571322cfbc055a780d41f0ce95a67 Mon Sep 17 00:00:00 2001 From: Patrick Lehmann Date: Tue, 21 Jun 2022 22:13:10 +0200 Subject: Change encoding to latin-1 for libghdl (to comply with standard). It can be monkey-patched via pyGHDL.libGHDL.ENCODING. (cherry picked from commit 2dd38de9d52493cde7c045ed73a987ed06bf617a) --- pyGHDL/dom/NonStandard.py | 6 +++--- pyGHDL/libghdl/__init__.py | 8 +++++--- pyGHDL/libghdl/errorout_memory.py | 3 ++- pyGHDL/libghdl/files_map_editor.py | 4 ++-- pyGHDL/libghdl/name_table.py | 7 ++++--- pyGHDL/libghdl/str_table.py | 3 ++- 6 files changed, 18 insertions(+), 13 deletions(-) (limited to 'pyGHDL') diff --git a/pyGHDL/dom/NonStandard.py b/pyGHDL/dom/NonStandard.py index 14f5e1eac..1dbef9a9a 100644 --- a/pyGHDL/dom/NonStandard.py +++ b/pyGHDL/dom/NonStandard.py @@ -59,7 +59,7 @@ from pyGHDL.libghdl import ( errorout_memory, LibGHDLException, utils, - files_map_editor, + files_map_editor, ENCODING, ) from pyGHDL.libghdl.flags import Flag_Gather_Comments from pyGHDL.libghdl.vhdl import nodes, sem_lib @@ -155,11 +155,11 @@ class Document(VHDLModel_Document): self.__domTranslateTime = time.perf_counter() - t1 def __loadFromPath(self): - with self._filename.open("r", encoding="utf-8") as file: + with self._filename.open("r", encoding=ENCODING) as file: self.__loadFromString(file.read()) def __loadFromString(self, sourceCode: str): - sourcesBytes = sourceCode.encode("utf-8") + sourcesBytes = sourceCode.encode(ENCODING) sourceLength = len(sourcesBytes) bufferLength = sourceLength + 128 self.__ghdlFileID = name_table.Get_Identifier(str(self._filename)) diff --git a/pyGHDL/libghdl/__init__.py b/pyGHDL/libghdl/__init__.py index 372f5968b..2cf9cff70 100644 --- a/pyGHDL/libghdl/__init__.py +++ b/pyGHDL/libghdl/__init__.py @@ -48,6 +48,8 @@ from pyGHDL import __version__ as ghdlVersion Nullable = Optional +ENCODING = "latin-1" + class LibGHDLException(GHDLBaseException): _internalErrors: Nullable[List[str]] @@ -164,7 +166,7 @@ def _initialize(): libghdl.libghdl__set_hooks_for_analysis() # Set the prefix in order to locate the VHDL libraries. - prefix = str(_libghdl_path.parent.parent).encode("utf-8") + prefix = str(_libghdl_path.parent.parent).encode(ENCODING) libghdl.libghdl__set_exec_prefix(c_char_p(prefix), len(prefix)) return libghdl @@ -197,7 +199,7 @@ def set_option(Opt: str) -> bool: :param Opt: Option to set. :return: Return ``True``, if the option is known and handled. """ - Opt = Opt.encode("utf-8") + Opt = Opt.encode(ENCODING) return libghdl.libghdl__set_option(c_char_p(Opt), len(Opt)) == 0 @@ -233,7 +235,7 @@ def analyze_file(fname: str) -> Iir: :param fname: File name :return: Internal Intermediate Representation (IIR) """ - fname = fname.encode("utf-8") + fname = fname.encode(ENCODING) return libghdl.libghdl__analyze_file(c_char_p(fname), len(fname)) diff --git a/pyGHDL/libghdl/errorout_memory.py b/pyGHDL/libghdl/errorout_memory.py index 9f75d0331..d0847417e 100644 --- a/pyGHDL/libghdl/errorout_memory.py +++ b/pyGHDL/libghdl/errorout_memory.py @@ -36,6 +36,7 @@ from ctypes import c_int8, c_int32, c_char_p, Structure from pyTooling.Decorators import export +from pyGHDL.libghdl import ENCODING from pyGHDL.libghdl._types import ErrorIndex from pyGHDL.libghdl._decorator import BindToLibGHDL @@ -122,7 +123,7 @@ def Get_Error_Message(Idx: ErrorIndex) -> str: :param Idx: Index from 1 to ``Nbr_Messages`` See :func:`Get_Nbr_Messages`. :return: Error message. """ - return _Get_Error_Message(Idx).decode("iso-8859-1") + return _Get_Error_Message(Idx).decode(ENCODING) @export diff --git a/pyGHDL/libghdl/files_map_editor.py b/pyGHDL/libghdl/files_map_editor.py index a67766c44..c7cfad4c2 100644 --- a/pyGHDL/libghdl/files_map_editor.py +++ b/pyGHDL/libghdl/files_map_editor.py @@ -36,7 +36,7 @@ from ctypes import c_int32, c_char_p, c_bool, c_uint32 from pyTooling.Decorators import export -from pyGHDL.libghdl import libghdl +from pyGHDL.libghdl import libghdl, ENCODING from pyGHDL.libghdl._decorator import BindToLibGHDL from pyGHDL.libghdl._types import SourceFileEntry @@ -85,7 +85,7 @@ def Replace_Text( :param Text: undocumented :return: Return True in case of success, False in case of failure (the gap is too small). """ - buffer = Text.encode("utf-8") + buffer = Text.encode(ENCODING) return _Replace_Text( File, Start_Line, diff --git a/pyGHDL/libghdl/name_table.py b/pyGHDL/libghdl/name_table.py index b29775213..e40bd635b 100644 --- a/pyGHDL/libghdl/name_table.py +++ b/pyGHDL/libghdl/name_table.py @@ -36,6 +36,7 @@ from ctypes import c_char, c_char_p from pyTooling.Decorators import export +from pyGHDL.libghdl import ENCODING from pyGHDL.libghdl._types import NameId from pyGHDL.libghdl._decorator import BindToLibGHDL @@ -73,7 +74,7 @@ def Get_Name_Ptr(Id: NameId) -> str: :param Id: NameId for the identifier to query. :return: Identifier as string. """ - return _Get_Name_Ptr(Id).decode("utf-8") + return _Get_Name_Ptr(Id).decode(ENCODING) # @export @@ -95,7 +96,7 @@ def Get_Character(Id: NameId) -> str: :param Id: NameId for the identifier to query. :return: Get the character of the identifier. """ - return _Get_Character(Id).decode("utf-8") + return _Get_Character(Id).decode(ENCODING) # @export @@ -119,5 +120,5 @@ def Get_Identifier(string: str) -> NameId: :param string: String to create or lookup. :return: Id in name table. """ - string = string.encode("utf-8") + string = string.encode(ENCODING) return _Get_Identifier(c_char_p(string), len(string)) diff --git a/pyGHDL/libghdl/str_table.py b/pyGHDL/libghdl/str_table.py index a91268bb4..3f55d1eb3 100644 --- a/pyGHDL/libghdl/str_table.py +++ b/pyGHDL/libghdl/str_table.py @@ -36,6 +36,7 @@ from ctypes import c_char_p from pyTooling.Decorators import export +from pyGHDL.libghdl import ENCODING from pyGHDL.libghdl._types import String8Id from pyGHDL.libghdl._decorator import BindToLibGHDL @@ -57,4 +58,4 @@ def Get_String8_Ptr(Id: String8Id, Length: int) -> str: :param Id: String8Id for the string to query. :return: String8 as string. """ - return _String8_Address(Id)[:Length].decode("utf-8") + return _String8_Address(Id)[:Length].decode(ENCODING) -- cgit v1.2.3