I want to test an argument to see if it is a 'lightweight pointer' as returned by ctypes.byref, and also whether the target of that 'pointer' is of the type I am expecting.
At least 3 possible solutions spring to mind - as in the code below - but I can't help feeling they are all rather inelegant. Examining repr's seems particularly suspect, and even creating my BYREF_TYPE constant is not great. Added to that, I am having to use the _obj attribute of the byref object, which is not public.
import ctypes
#Type of any object returned by byref. (c_int is just an example - could have been anything)
BYREF_TYPE = type(ctypes.byref(ctypes.c_int())) #used by test 1 below
arg = ctypes.byref(ctypes.c_wchar('z'))
target_type = ctypes.c_wchar
#1
print(type(arg) is BYREF_TYPE and type(arg._obj) is target_type)
#2
print(repr(type(arg)) == "<class 'CArgObject'>" and type(arg._obj) is target_type)
#3
print(repr(arg).startswith("<cparam 'P'") and type(arg._obj) is target_type)
The above code prints the expected 'True' for each version my test, but surely I must be missing something better?