<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style id="css_styles" type="text/css"><!--blockquote.cite { margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc }
blockquote.cite2 {margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc; margin-top: 3px; padding-top: 0px; }
a img { border: 0px; }
li[style='text-align: center;'], li[style='text-align: center; '], li[style='text-align: right;'], li[style='text-align: right; '] {  list-style-position: inside;}
body { font-family: 'Segoe UI'; font-size: 12pt; }
.quote { margin-left: 1em; margin-right: 1em; border-left: 5px #ebebeb solid; padding-left: 0.3em; }
--></style>
</head>
<body>
<div>Thanks. I'll submit a PR.  </div>
<div><br>
</div>
<div>When calling from a Delphi COM client (using OleVariant ),  arg[0] will be set by Delphi a value of <span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81); font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; white-space: pre-wrap;">0x8002000C,
 when the target Python method has NO argument (i.e., taking only "self"). This makes args non-empty.</span></div>
<div><span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81); font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; white-space: pre-wrap;"><br>
</span></div>
<div><span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81); font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; white-space: pre-wrap;">When
 calling from a C++ MFC/COM client, arg[0] will be set a value of "None", when the target Python method has NO argument (i.e., talking only "self"). This makes args non-empty.</span></div>
<div><span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81); font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; white-space: pre-wrap;"><br>
</span></div>
<div><span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81); font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; white-space: pre-wrap;">The
 following code would be a "protection" for parameterless Python method.</span></div>
<div><span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81); font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; white-space: pre-wrap;"><br>
</span></div>
<div>
<div id="x9e091b2fc5004ac884695a53e656dc43">
<div style="background-color:rgba(0,0,0,0);font-family:'JetBrains Mono','Courier New',monospace;font-size:13.3333px;margin:0px;">
<span style="background-color:#F3F3F3;"><font color="#444444">    import inspect</font></span></div>
<div style="background-color:rgba(0,0,0,0);font-family:'JetBrains Mono','Courier New',monospace;font-size:13.3333px;margin:0px;">
<span style="background-color:#F3F3F3;"><font color="#444444">    if len(inspect.getfullargspec(func).args) == 1:</font></span></div>
<div style="background-color:rgba(0,0,0,0);font-family:'JetBrains Mono','Courier New',monospace;font-size:13.3333px;margin:0px;">
<span style="background-color:#F3F3F3;"><font color="#444444">        return func()</font></span></div>
<div style="background-color:rgba(0,0,0,0);font-family:'JetBrains Mono','Courier New',monospace;font-size:13.3333px;margin:0px;">
<span style="background-color:#F3F3F3;"><font color="#444444">    else:</font></span></div>
<div style="background-color:rgba(0,0,0,0);font-family:'JetBrains Mono','Courier New',monospace;font-size:13.3333px;margin:0px;">
<span style="background-color:#F3F3F3;"><font color="#444444">        return func(*args)</font></span></div>
</div>
</div>
<div><span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81); font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; white-space: pre-wrap;"><br>
</span></div>
<div><span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81); font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; white-space: pre-wrap;">Wuping</span></div>
<div style="clear:both"><br>
</div>
<div><br>
</div>
<div>
<div>------ Original Message ------</div>
<div>From "Mark Hammond" <<a href="mailto:mhammond@skippinet.com.au">mhammond@skippinet.com.au</a>></div>
<div>To "Wuping Xin" <<a href="mailto:wuping@caliper.com">wuping@caliper.com</a>></div>
<div>Cc "<a href="mailto:Python-win32@python.org">Python-win32@python.org</a>" <<a href="mailto:Python-win32@python.org">Python-win32@python.org</a>></div>
<div>Date 4/17/2023 8:14:31 AM</div>
<div>Subject Re: win32com, server/policy.py bug? policy.py Line 639</div>
</div>
<div><br>
</div>
<div id="xeb9413d4e18246b">
<blockquote cite="4287db71-984c-60e7-c533-0f6b1101daca@skippinet.com.au" type="cite" class="cite2">
<p class="MsoNormal"><strong><span style="font-size:13.0pt;line-height:107%;
font-family:"Calibri",sans-serif;color:darkred
">WARNING EXTERNAL EMAIL
</span></strong></p>
<br>
<br>
<div>
<p><br>
</p>
<div class="moz-cite-prefix">On 16/4/2023 11:48 pm, Wuping Xin wrote:<br>
</div>
<blockquote type="cite" cite="mid:em9791b992-1989-43aa-8323-44f63ee72edc@f1ca3060.com" class="cite">
<style type="text/css"><!--#xeb9413d4e18246b #x652cddefdee64d0 blockquote.cite2, #xeb9413d4e18246b blockquote.cite2
{margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right: 0px; border-left: 1px solid rgb(204, 204, 204); margin-top: 3px; padding-top: 0px;}
#xeb9413d4e18246b #x652cddefdee64d0, #xeb9413d4e18246b
{font-family: "Segoe UI"; font-size: 12pt;}
#xeb9413d4e18246b blockquote.cite
{margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right: 0px; border-left: 1px solid rgb(204, 204, 204);}
#xeb9413d4e18246b a img
{border: 0px;}
#xeb9413d4e18246b li#xeb9413d4e18246b [style="'text-align: center;'"], #xeb9413d4e18246b li#xeb9413d4e18246b [style="'text-align: center; '"], #xeb9413d4e18246b li#xeb9413d4e18246b [style="'text-align: right;'"], #xeb9413d4e18246b li#xeb9413d4e18246b [style="'text-align: right; '"]
{list-style-position: inside;}
#xeb9413d4e18246b .quote
{margin-left: 1em; margin-right: 1em; border-left: 5px solid rgb(235, 235, 235); padding-left: 0.3em;}
--></style>
<div>A better fix is to change Line 639 of win32com/server/policy.py to the following:</div>
<div><br>
</div>
<div>
<div style="font-family:JetBrains Mono,'Courier New',monospace;font-size:10pt;white-space:pre;">
<div><span style="background-color: rgb(243, 243, 243);"><font color="#444444">                    import inspect</font></span></div>
<div><span style="background-color: rgb(243, 243, 243);"><font color="#444444">                    if len(inspect.getfullargspec(func).args) == 1:</font></span></div>
<div><span style="background-color: rgb(243, 243, 243);"><font color="#444444">                        return func()</font></span></div>
<div><span style="background-color: rgb(243, 243, 243);"><font color="#444444">                    else:</font></span></div>
<div><span style="background-color: rgb(243, 243, 243);"><font color="#444444">                        return func(*args)</font></span>
</div>
</div>
</div>
<div style="clear:both"><br>
</div>
<div>Then we can make VBA, Delphi, C++ .... all clients happy.</div>
<div><br>
</div>
<div>Agree? Should I submit a pull request?</div>
</blockquote>
<p>I'm not sure ATM - I'd be quite surprised if there was a bug in COM functions with no args, so it may be more subtle than that. But you should certainly get either a PR or issue on github for this, ideally with a complete repro. It might be quite some time
 (ie, many weeks) before I can look at this in more detail.</p>
<p>Thanks,</p>
<p>Mark<br>
</p>
<p><br>
</p>
<blockquote type="cite" cite="mid:em9791b992-1989-43aa-8323-44f63ee72edc@f1ca3060.com" class="cite">
<div><br>
</div>
<div>Wuping</div>
<div><br>
</div>
<div>
<div>------ Original Message ------</div>
<div>From "Wuping Xin" <<a href="mailto:wuping@caliper.com" moz-do-not-send="true" class="moz-txt-link-freetext">wuping@caliper.com</a>></div>
<div>To "Mark Hammond" <<a href="mailto:mhammond@skippinet.com.au" moz-do-not-send="true" class="moz-txt-link-freetext">mhammond@skippinet.com.au</a>></div>
<div>Cc "<a href="mailto:Python-win32@python.org" moz-do-not-send="true" class="moz-txt-link-freetext">Python-win32@python.org</a>" <<a href="mailto:Python-win32@python.org" moz-do-not-send="true" class="moz-txt-link-freetext">Python-win32@python.org</a>></div>
<div>Date 4/16/2023 9:28:48 AM</div>
<div>Subject win32com, server/policy.py bug? policy.py Line 639</div>
</div>
<div><br>
</div>
<div id="x652cddefdee64d0">
<blockquote cite="em24180be7-7da7-473b-9f07-47d0bc67fd66@90b98bbc.com" type="cite" class="cite2">
<div style="font-family: "JetBrains Mono", "Courier New", monospace; white-space: pre;">
<div style=""><font style="font-size: 13.3px;" size="1">For the following code, the method "GetPerson" has no explicit argument (except self).</font></div>
<div style=""></div>
<div style="font-size: 10pt;"><span style="background-color: white;"><span style="color:#9C9;">class</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#C9C;font-weight:bold;">MyCOMObject</span><span style="color:#CCC;">:</span></span></div>
<div style="font-size: 10pt;"><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">    </span><span style="color:#CCC;">_public_methods_</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">[</span><span style="color:#F99157;">"GetPerson"</span><span style="color:#CCC;">]</span></span></div>
<div style="font-size: 10pt;"><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">    </span><span style="color:#999;">#_reg_clsid_ = '{44ee76c7-1290-4ea6-8189-00d5d7cd712a}'</span></span></div>
<div style="font-size: 10pt;"><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">    </span><span style="color:#999;">#_reg_desc_ = "My COM server"</span></span></div>
<div style="font-size: 10pt;"><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">    </span><span style="color:#999;">#_reg_progid_ = "MyCOMObject"
</span></span></div>
<div style="font-size: 10pt;"></div>
<div style="font-size: 10pt;"><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">    </span><span style="color:#9C9;">def</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#69C;font-weight:bold;">get_person</span><span style="color:#CCC;">(</span><span style="color:#9C9;">self</span><span style="color:#CCC;">):</span></span></div>
<div style="font-size: 10pt;"><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">        </span><span style="color:#CCC;">person</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">Person(</span><span style="color:#F99157;">"haha"</span><span style="color:#CCC;">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#F99157;">45</span><span style="color:#CCC;">)</span></span></div>
<div style="font-size: 10pt;"><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">        </span><span style="color:#CCC;">wrapped</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">win32com.server.util.wrap(person,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">useDispatcher</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">my_dispatcher)</span></span></div>
<div style="font-size: 10pt;"><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">        </span><span style="color:#9C9;">return</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">wrapped
</span></span></div>
<div style="font-size: 10pt;"></div>
<div style="font-size: 10pt;"></div>
<div style="font-size: 10pt;">When calling from VBA, like below , it works fine.</div>
<div style="font-size: 10pt;">    Set o = CreateObject("MyCOMObject") </div>
<div style="font-size: 10pt;">    Set p = o.get_person()</div>
<div style="font-size: 10pt;"></div>
<div style="font-size: 10pt;">However, when calling from C++, or Delphi, the following error will be thrown:
</div>
<div style="font-size: 10pt;"><i>"TypeError: MyCOMObject.get_person() takes 1 positional argument but 2 were given"</i></div>
<div style="font-size: 10pt;"><i></i></div>
<div style="font-size: 10pt;">Delphi code:</div>
<div style="font-size: 10pt;"></div>
<div style="font-size: 10pt;"><i>procedure TForm1.Button1Click(Sender: TObject); </i>
</div>
<div style="font-size: 10pt;"><i>var </i></div>
<div style="font-size: 10pt;"><i>  o: OleVariant; </i></div>
<div style="font-size: 10pt;"><i>  p: OleVariant; </i></div>
<div style="font-size: 10pt;"><i style="font-size: 10pt;">begin </i></div>
<div style="font-size: 10pt;"><i>  o := CreateOleObject('MyCOMObject'); </i></div>
<div style="font-size: 10pt;"><i style="font-size: 10pt;"> <b> p := o.get_person()</b>; // error will be thrown
</i></div>
<div style="font-size: 10pt;"><i>  s := p.name; </i></div>
<div style="font-size: 10pt;"><i>  age := p.age; </i></div>
<div style="font-size: 10pt;"><i>  ShowMessage(s); </i></div>
<div style="font-size: 10pt;"><i>end;</i></div>
<div style="font-size: 10pt;"><i></i></div>
<div style=""><font style="font-size: 13.3px;" size="1">The fix is to change Line 639 of policy.py to the following:</font></div>
<div style=""><font style="font-size: 13.3px;" size="1"></font></div>
<div style=""><font style="font-size: 13.3px; background-color: white;" size="1">    
</font></div>
<div style="">
<div style="font-size: 10pt;">
<div><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">                
</span><span style="color:#9C9;">if</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#9C9;">len</span><span style="color:#CCC;">(args)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">==</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#F99157;">1</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#9C9;">and</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">(args[</span><span style="color:#F99157;">0</span><span style="color:#CCC;">]</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">==</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">-</span><span style="color:#F99157;">2147352572</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#9C9;">or</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">args[</span><span style="color:#F99157;">0</span><span style="color:#CCC;">]</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">==</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#9C9;">None</span><span style="color:#CCC;">):</span></span></div>
<div><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">                
</span><span style="color:#9C9;">return</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">func()</span></span></div>
<div><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">                
</span><span style="color:#9C9;">else</span><span style="color:#CCC;">:</span></span></div>
<div><span style="background-color: white;"><span style="color: rgb(192, 192, 192);">                
</span><span style="color:#9C9;">return</span><span style="color: rgb(192, 192, 192);"> </span><span style="color:#CCC;">func(*args)</span></span>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</body>
</html>