Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in creating an instance of structure. #69

Open
prashant-saxena opened this issue Jul 9, 2023 · 6 comments
Open

Error in creating an instance of structure. #69

prashant-saxena opened this issue Jul 9, 2023 · 6 comments

Comments

@prashant-saxena
Copy link

Hello,
struct in header file

typedef struct WGPURequestAdapterOptions {
    WGPUChainedStruct const * nextInChain;
    WGPUSurface compatibleSurface; // nullable
    WGPUPowerPreference powerPreference;
    bool forceFallbackAdapter;
} WGPURequestAdapterOptions;

I have added a print statement in ctypes.py

    def _get_type(self, typ, pointers=True):
        """Return a ctype object representing the named type.

        If pointers is True, the class returned includes all pointer/array
        specs provided. Otherwise, the class returned is just the base type
        with no pointers.

        """
        print(typ)

Loading the lib using parser object with prefix=['WGPU','wgpu'] options.
and the error is

Type('struct WGPURequestAdapterOptions')
Type('WGPUChainedStruct', '*', type_quals=(('const',), ()))
Type('struct WGPUChainedStruct', '*', type_quals=(('const',), ()))
Type('WGPUSType')
Type('WGPUSurface')
Type('WGPUPowerPreference')
Type('bool')
Traceback (most recent call last):
  File "E:\projects\webgpu\python\parselib.py", line 1, in <module>
    import webgpu as wgpu
  File "E:\projects\webgpu\python\webgpu.py", line 24, in <module>
    r = clib.WGPURequestAdapterOptions()
  File "E:\projects\webgpu\python\venv\lib\site-packages\pyclibrary\c_library.py", line 225, in __getattr__
    obj = self(k, n)
  File "E:\projects\webgpu\python\venv\lib\site-packages\pyclibrary\c_library.py", line 205, in __call__
    self._objs_[typ][name] = self._make_obj_(typ, name)
  File "E:\projects\webgpu\python\venv\lib\site-packages\pyclibrary\c_library.py", line 270, in _make_obj_
    return self._get_type(obj)
  File "E:\projects\webgpu\python\venv\lib\site-packages\pyclibrary\backends\ctypes.py", line 165, in _get_type
    cls = self._get_struct('structs',
  File "E:\projects\webgpu\python\venv\lib\site-packages\pyclibrary\backends\ctypes.py", line 304, in _get_struct
    s._fields_ = [(m[0], self._get_type(m[1])) if m[2] is None else
  File "E:\projects\webgpu\python\venv\lib\site-packages\pyclibrary\backends\ctypes.py", line 304, in <listcomp>
    s._fields_ = [(m[0], self._get_type(m[1])) if m[2] is None else
  File "E:\projects\webgpu\python\venv\lib\site-packages\pyclibrary\backends\ctypes.py", line 177, in _get_type
    raise KeyError("Can't find base type for {}".format(typ))
KeyError: "Can't find base type for ['bool']"
@MatthieuDartiailh
Copy link
Owner

From the top of my head bool is not a standard C type. So you need to add a definition and you can use the bool present in ctypes to represent it.

@prashant-saxena
Copy link
Author

Hello,
I did a minor change in _get_type method in ctypes.py:

...
elif typ[0] == 'bool':
    cls = c_bool
else:
    raise KeyError("Can't find base type for {}".format(typ))
...

and it's working. Are you suggesting the same? If not then could you please explain further.

@MatthieuDartiailh
Copy link
Owner

I am a bit confused because bool is present in the _types_ populated in https://github.com/MatthieuDartiailh/pyclibrary/blob/main/pyclibrary/backends/ctypes.py#L367C1-L367C1

Could you inspect this attribute and post the version you are using ?

@prashant-saxena
Copy link
Author

I am using version 0.2.1

@MatthieuDartiailh
Copy link
Owner

So it is the last version which is up to date with main. So can you check the content of the _types_ attribute on the library object ?

@prashant-saxena
Copy link
Author

I don't see this types attribute. Neither on parser nor on the library object. Here is the output:

print(dir(parser))
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_format_parsed_file', '_init', 'abstract_declarator', 'add_def', 'build_parser', 'cache_version', 'compile_fn_macro', 'compiled_types', 'current_file', 'data_list', 'decl_list', 'declarator', 'declarator_list', 'defs', 'enum_decl', 'enum_type', 'eval', 'eval_expr', 'eval_preprocessor_expr', 'eval_type', 'expand_fn_macro', 'expand_macros', 'file_defs', 'file_order', 'files', 'find', 'find_headers', 'find_text', 'function_decl', 'import_dict', 'init_opts', 'is_fund_type', 'load_cache', 'load_file', 'pack_list', 'packing_at', 'parse_defs', 'parser', 'pp_define', 'preprocess', 'print_all', 'process_all', 'process_declarator', 'process_enum', 'process_function', 'process_macro_defn', 'process_struct', 'process_type', 'process_typedef', 'process_variable', 'rem_def', 'remove_comments', 'struct_decl', 'struct_member', 'struct_type', 'type_decl', 'type_spec', 'typeless_function_decl', 'variable_decl', 'write_cache']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants