From 0de91f9313a4c76445ada1617fff69d97fe12217 Mon Sep 17 00:00:00 2001 From: Patrick Lehmann Date: Wed, 6 Jan 2021 11:27:59 +0100 Subject: Make API more pythonic be replacing C-like byte arrays with str. Abstracted utf-8 encoding/decoding. --- pyGHDL/dom/Common.py | 4 ++-- pyGHDL/libghdl/errorout_memory.py | 5 ++--- pyGHDL/libghdl/name_table.py | 14 ++++++++------ pyGHDL/lsp/document.py | 4 ++-- pyGHDL/lsp/references.py | 2 +- pyGHDL/lsp/workspace.py | 8 ++++---- 6 files changed, 19 insertions(+), 18 deletions(-) (limited to 'pyGHDL') diff --git a/pyGHDL/dom/Common.py b/pyGHDL/dom/Common.py index 4e02b9678..faa4cc0db 100644 --- a/pyGHDL/dom/Common.py +++ b/pyGHDL/dom/Common.py @@ -72,9 +72,9 @@ class GHDLMixin: } @classmethod - def _ghdlNodeToName(cls, node): + def _ghdlNodeToName(cls, node) -> str: """Return the python string from node :obj:`node` identifier""" - return name_table.Get_Name_Ptr(nodes.Get_Identifier(node)).decode("utf-8") + return name_table.Get_Name_Ptr(nodes.Get_Identifier(node)) @classmethod def _ghdlPortToMode(cls, port): diff --git a/pyGHDL/libghdl/errorout_memory.py b/pyGHDL/libghdl/errorout_memory.py index f9977ec27..444c53e80 100644 --- a/pyGHDL/libghdl/errorout_memory.py +++ b/pyGHDL/libghdl/errorout_memory.py @@ -79,13 +79,12 @@ def Get_Error_Record(Idx: ErrorIndex): # FIXME: returns Error_Message @export -def Get_Error_Message(Idx: ErrorIndex) -> str: # FIXME: check '*_addr' vs string return value +def Get_Error_Message(Idx: ErrorIndex) -> str: func = libghdl.errorout__memory__get_error_message_addr func.argstype = [c_int32] func.restype = c_char_p - # FIXME: don't we need to encode to utf-8? - return func(Idx) + return func(Idx).decode("utf-8") @export diff --git a/pyGHDL/libghdl/name_table.py b/pyGHDL/libghdl/name_table.py index b82207fb3..72b6dfd8c 100644 --- a/pyGHDL/libghdl/name_table.py +++ b/pyGHDL/libghdl/name_table.py @@ -56,7 +56,7 @@ def Get_Name_Length(Id: NameId) -> int: @export -def Get_Name_Ptr(Id: NameId) -> bytes: +def Get_Name_Ptr(Id: NameId) -> str: """ Get the address of the first character of ID. The address is valid until the next call to Get_Identifier (which may reallocate the string table). @@ -67,8 +67,8 @@ def Get_Name_Ptr(Id: NameId) -> bytes: """ func = libghdl.name_table__get_name_ptr func.restype = c_char_p - # FIXME: don't we need to encode to utf-8? - return func(Id) + + return func(Id).decode("utf-8") @export @@ -76,9 +76,11 @@ def Get_Identifier(string: str) -> NameId: """ Get or create an entry in the name table. - Note: - * an identifier is represented in all lower case letter, - * an extended identifier is represented in backslashes, double internal backslashes are simplified. + .. note:: + + * an identifier is represented in all lower case letter, + * an extended identifier is represented in backslashes, double internal + backslashes are simplified. :param string: String to create or lookup. :return: Id in name table. diff --git a/pyGHDL/lsp/document.py b/pyGHDL/lsp/document.py index 1b988220c..1ef510d52 100644 --- a/pyGHDL/lsp/document.py +++ b/pyGHDL/lsp/document.py @@ -40,11 +40,11 @@ class Document(object): src_bytes = source.encode(Document.encoding, "replace") src_len = len(src_bytes) buf_len = src_len + Document.initial_gap_size - fileid = name_table.Get_Identifier(filename.encode("utf-8")) + fileid = name_table.Get_Identifier(filename) if os.path.isabs(filename): dirid = name_table.Null_Identifier else: - dirid = name_table.Get_Identifier(dirname.encode("utf-8")) + dirid = name_table.Get_Identifier(dirname) sfe = files_map.Reserve_Source_File(dirid, fileid, buf_len) files_map_editor.Fill_Text(sfe, ctypes.c_char_p(src_bytes), src_len) return sfe diff --git a/pyGHDL/lsp/references.py b/pyGHDL/lsp/references.py index 2725f40f2..c7d0f730d 100644 --- a/pyGHDL/lsp/references.py +++ b/pyGHDL/lsp/references.py @@ -94,7 +94,7 @@ def goto_definition(n, loc): log.debug( "for loc %u id=%s", loc, - name_table.Get_Name_Ptr(nodes.Get_Identifier(ref)).decode("utf-8"), + name_table.Get_Name_Ptr(nodes.Get_Identifier(ref)), ) ent = nodes.Get_Named_Entity(ref) return None if ent == nodes.Null_Iir else ent diff --git a/pyGHDL/lsp/workspace.py b/pyGHDL/lsp/workspace.py index 8c60edab9..980289806 100644 --- a/pyGHDL/lsp/workspace.py +++ b/pyGHDL/lsp/workspace.py @@ -194,7 +194,7 @@ class Workspace(object): return log.info("Using options: %s", ghdl_opts) for opt in ghdl_opts: - if not libghdl.set_option(opt.encode("utf-8")): + if not libghdl.set_option(opt): self._server.show_message( lsp.MessageType.Error, "error with option: {}".format(opt) ) @@ -234,7 +234,7 @@ class Workspace(object): diag = {} for i in range(nbr_msgs): hdr = errorout_memory.Get_Error_Record(i + 1) - msg = errorout_memory.Get_Error_Message(i + 1).decode("utf-8") + msg = errorout_memory.Get_Error_Message(i + 1) if hdr.file == 0: # Possible for error limit reached. continue @@ -452,12 +452,12 @@ class Workspace(object): return res # Find library - lib_id = name_table.Get_Identifier(library.encode("utf-8")) + lib_id = name_table.Get_Identifier(library) lib = libraries.Get_Library_No_Create(lib_id) if lib == name_table.Null_Identifier: return None # Find entity - ent_id = name_table.Get_Identifier(name.encode("utf-8")) + ent_id = name_table.Get_Identifier(name) unit = libraries.Find_Primary_Unit(lib, ent_id) if unit == nodes.Null_Iir: return None -- cgit v1.2.3