Skip to content

Commit

Permalink
fix conflicting name resolving being never called
Browse files Browse the repository at this point in the history
also improve code style on the way
  • Loading branch information
mara004 committed Dec 17, 2023
1 parent 3a39e43 commit 105a3c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 75 deletions.
93 changes: 19 additions & 74 deletions ctypesgen/processor/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,24 @@ def fix_conflicting_names(data, opts):
"""If any descriptions from the C code would overwrite Python builtins or
other important names, fix_conflicting_names() adds underscores to resolve
the name conflict."""


status_message("Looking for conflicting names...")

our_names = {"_libs", "_libs_info"}
# probably includes a bit more than we actually use ...
our_names |= {x for x in dir(ctypes) if not x.startswith("_")}

# This dictionary maps names to a string representing where the name came from.
important_names = {}
for name in our_names:
important_names[name] = "a name from ctypes or ctypesgen"
for name in dir(__builtins__):
important_names[name] = "a Python builtin"
for name in opts.imported_symbols:
important_names[name] = "a name from an included Python module"
for name in keyword.kwlist:
important_names[name] = "a Python keyword"

# This is the order of priority for names
descriptions = (
data.functions
Expand All @@ -100,79 +117,7 @@ def fix_conflicting_names(data, opts):
+ data.constants
+ data.macros
)

# This dictionary maps names to a string representing where the name
# came from.
important_names = {}

occupied_names = set()
occupied_names = occupied_names.union(
# ctypesgen names
[
"_libs",
"_libs_info",
# the following names are only accessed before the symbols list, so we don't strictly care about them being overridden
# "sys",
# "warnings",
# "pathlib",
# "_find_library",
# "_register_library",
]
)
occupied_names = occupied_names.union(
# ctypes names, required for symbol prints
[
"ctypes",
"addressof",
"ArgumentError",
"cast",
"CFUNCTYPE",
"pointer",
"POINTER",
"Union",
"sizeof",
"Structure",
"c_buffer",
"c_byte",
"c_char",
"c_char_p",
"c_double",
"c_float",
"c_int",
"c_int16",
"c_int32",
"c_int64",
"c_int8",
"c_long",
"c_longlong",
"c_ptrdiff_t",
"c_short",
"c_size_t",
"c_ubyte",
"c_uint",
"c_uint16",
"c_uint32",
"c_uint64",
"c_uint8",
"c_ulong",
"c_ulonglong",
"c_ushort",
"c_void",
"c_void_p",
"c_voidp",
"c_wchar",
"c_wchar_p",
]
)
for name in occupied_names:
important_names[name] = "a name needed by ctypes or ctypesgen"
for name in dir(__builtins__):
important_names[name] = "a Python builtin"
for name in opts.imported_symbols:
important_names[name] = "a name from an included Python module"
for name in keyword.kwlist:
important_names[name] = "a Python keyword"


for description in descriptions:
if description.py_name() in important_names:
conflict_name = important_names[description.py_name()]
Expand Down
2 changes: 1 addition & 1 deletion ctypesgen/processor/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def process(data, options):
remove_macros(data, options)
filter_by_regex_rules(data, options)
remove_NULL(data, options)
if options.output_language == "python":
if options.output_language.startswith("py"):
# this function is python specific
fix_conflicting_names(data, options)

Expand Down

0 comments on commit 105a3c6

Please sign in to comment.