@@ -454,7 +454,7 @@ def generate_aio_async_method(method: Method, model: Model) -> Optional[str]:
454454 if method .return_value is not None :
455455 call = wrap_result (call , method .return_value .type , model )
456456
457- ret = "None" if method . return_value is None else pyi_type (method . return_value . type , model )
457+ ret = facade_return_type (method , model )
458458
459459 return f""" async def { method .name } ({ signature } ) -> { ret } :
460460 return { call } """
@@ -478,7 +478,7 @@ def generate_custom_facade_method(
478478 body = textwrap .indent (logic .strip (), " " * 8 )
479479 keyword = "async def" if awaitable else "def"
480480
481- ret = "None" if method . return_value is None else pyi_type (method . return_value . type , model )
481+ ret = facade_return_type (method , model )
482482
483483 return f""" { keyword } { method .name } ({ signature } ) -> { ret } :
484484{ body }
@@ -685,7 +685,7 @@ def generate_py_sync_method(method: Method, model: Model) -> Optional[str]:
685685 if method .return_value is not None :
686686 call = wrap_result (call , method .return_value .type , model )
687687
688- ret = "None" if method . return_value is None else pyi_type (method . return_value . type , model )
688+ ret = facade_return_type (method , model )
689689
690690 return f""" def { method .name } ({ facade_signature (method , params )} ) -> { ret } :
691691 return { call } """
@@ -721,7 +721,7 @@ def generate_py_async_method(method: Method, model: Model) -> Optional[str]:
721721 if method .return_value is not None :
722722 call = wrap_result (call , method .return_value .type , model )
723723
724- ret = "None" if method . return_value is None else pyi_type (method . return_value . type , model )
724+ ret = facade_return_type (method , model )
725725
726726 return f""" def { method .name } ({ signature } ) -> { ret } :
727727 return { call } """
@@ -811,8 +811,8 @@ def generate_pyi_class(otype: ObjectType, model: Model) -> List[str]:
811811
812812 body = []
813813
814- primary = primary_constructor (otype )
815- if primary is not None and not primary . throws :
814+ primary = inline_constructor (otype )
815+ if primary is not None :
816816 params = pyi_parameters (primary .input_parameters , model )
817817 if params is not None :
818818 signature = ", " .join (["self" ] + params )
@@ -887,7 +887,13 @@ def pyi_method(method: Method, model: Model) -> Optional[str]:
887887 if params is None :
888888 return None
889889 signature = ", " .join (["self" ] + params )
890- ret = "None" if method .return_value is None else pyi_type (method .return_value .type , model )
890+ out = method .optional_out_parameter
891+ if out is not None :
892+ ret = f"Optional[{ pyi_type (out .type , model )} ]"
893+ elif method .return_value is None :
894+ ret = "None"
895+ else :
896+ ret = pyi_type (method .return_value .type , model )
891897 return f" def { method .name } ({ signature } ) -> { ret } : ..."
892898
893899
@@ -1394,6 +1400,11 @@ def generate_method(otype: ObjectType, method: Method) -> Optional[Tuple[str, st
13941400 marshal = synchronous_method_return_marshal (method )
13951401 if marshal is None :
13961402 return None
1403+ out = method .optional_out_parameter
1404+ if out is not None :
1405+ marshal = build_return_marshal (out .type , method .object_type .model )
1406+ if marshal is None :
1407+ return None
13971408 params = []
13981409 for param in method .input_parameters :
13991410 sync_param = build_sync_param (param )
@@ -1737,14 +1748,17 @@ def generate_synchronous_method(otype: ObjectType, method: Method, marshal: str,
17371748 indent = " " * (len (otype .c_symbol_prefix ) + len (method .name ) + 3 )
17381749 handle = f"({ otype .c_type } *) PY_GOBJECT_HANDLE (self)"
17391750 returns_strv = method .return_value is not None and method .return_value .type .name == "utf8[]"
1751+ out = method .optional_out_parameter
17401752 call_arg_list = [handle ] + [p .call_arg for p in params ]
17411753 if returns_strv :
17421754 call_arg_list .append ("&retval_length" )
1755+ if out is not None :
1756+ call_arg_list .append (f"&{ out .name } " )
17431757 if method .throws :
17441758 call_arg_list .append ("&error" )
17451759 call = f"{ method .c_identifier } ({ ', ' .join (call_arg_list )} )"
17461760
1747- if not params and not method .throws and not returns_strv :
1761+ if not params and not method .throws and not returns_strv and out is None :
17481762 if method .return_value is None :
17491763 body = f" { call } ;\n \n PyFrida_RETURN_NONE;"
17501764 else :
@@ -1763,6 +1777,8 @@ def generate_synchronous_method(otype: ObjectType, method: Method, marshal: str,
17631777 decls .append (" GError * error = NULL;" )
17641778 if returns_strv :
17651779 decls .append (" gint retval_length;" )
1780+ if out is not None :
1781+ decls .append (f" { out_parameter_storage_type (out )} { out .name } ;" )
17661782 for p in params :
17671783 decls += [f" { line } " for line in p .decl .splitlines ()]
17681784
@@ -1777,7 +1793,15 @@ def generate_synchronous_method(otype: ObjectType, method: Method, marshal: str,
17771793
17781794 pre = "" .join (f"\n { p .pre } \n " for p in params if p .pre )
17791795
1780- if method .return_value is None :
1796+ if out is not None :
1797+ invocation = f" gboolean found = { call } ;"
1798+ success = (
1799+ " if (found)\n "
1800+ f" result = { marshal .format (value = out .name )} ;\n "
1801+ " else\n "
1802+ " {\n Py_IncRef (Py_None);\n result = Py_None;\n }"
1803+ )
1804+ elif method .return_value is None :
17811805 invocation = f" { call } ;"
17821806 success = " Py_IncRef (Py_None);\n result = Py_None;"
17831807 elif returns_strv :
@@ -1822,6 +1846,22 @@ def generate_synchronous_method(otype: ObjectType, method: Method, marshal: str,
18221846"""
18231847
18241848
1849+ def facade_return_type (method : Method , model : Model ) -> str :
1850+ out = method .optional_out_parameter
1851+ if out is not None :
1852+ return f"Optional[{ pyi_type (out .type , model )} ]"
1853+ if method .return_value is None :
1854+ return "None"
1855+ return pyi_type (method .return_value .type , model )
1856+
1857+
1858+ def out_parameter_storage_type (param : Parameter ) -> str :
1859+ """What the caller declares: the out parameter's own type, less the pointer."""
1860+ c = param .type .c
1861+ assert c .endswith ("*" ), c
1862+ return c [:- 1 ].strip ()
1863+
1864+
18251865def synchronous_method_return_marshal (method : Method ) -> Optional [str ]:
18261866 if method .is_async or method .is_property_accessor :
18271867 return None
@@ -2351,8 +2391,16 @@ def primary_constructor(otype: ObjectType) -> Optional[Procedure]:
23512391 return next ((c for c in otype .constructors if c .name == "new" ), otype .constructors [0 ])
23522392
23532393
2394+ def inline_constructor (otype : ObjectType ) -> Optional [Procedure ]:
2395+ """The constructor __init__ takes; one that throws is reached as a factory instead."""
2396+ ctor = primary_constructor (otype )
2397+ if ctor is None or ctor .throws :
2398+ return None
2399+ return ctor
2400+
2401+
23542402def factory_constructors (otype : ObjectType ) -> List [Procedure ]:
2355- primary = primary_constructor (otype )
2403+ primary = inline_constructor (otype )
23562404 return [
23572405 ctor
23582406 for ctor in otype .constructors
0 commit comments