[python-win32] Memory access violation using pywin32 as WSH
Bob Kline
bkline at rksystems.com
Tue Nov 29 11:06:58 EST 2022
Cranking up the microscope (or spelunking deeper, whichever metaphor
you prefer), the first thing I'll say is that this code is devilishly
complex.
Here's what I've found in the latest pass.
First, there's a minor bug at line 353 of pyscript.py [1], where
globalNameSpaceModule is set to None when it's
self.globalNameSpaceModule which should be set to None. The bug is
currently innocuous, as the only invocation of the method in which
that assignment is made in this version of the code is from
PyScript.Close(), and that method makes the correct assignment:
self.globalNameSpaceModule = None
It seems to me that there's a more serious problem at line 350 of that
module [2], where that same method appears to be invoking a method
which does not exist, at least as far as I can determine (bearing in
mind that I might not completely understand all the nuances of the
tricky code here).
self.globalNameSpaceModule.ax._Reset_()
Working backward, to see if I can figure out where _Reset_ would be
defined, I found this code in the PyScript class:
def InitNew(self):
framework.COMScript.InitNew(self)
import imp
self.scriptDispatch = None
self.globalNameSpaceModule = imp.new_module("__ax_main__")
self.globalNameSpaceModule.__dict__['ax'] = AXScriptAttribute(self)
....
It's not completely clear how this method gets invoked (I don't see
any calls to it, but I do see that "InitNew" appears as one of the
strings in the _public_methods_ array attribute of the base class),
but I can tell from my logging that it does get called. However it
gets invoked, that means that the _Reset_() call being made above is
on an instance of the AXScriptAttribute class, which—as far as I can
tell—has no _Reset_() method (and no base class from which it could
inherit that method). This is confirmed by an exception which is
caught at shutdown time in the PyScript.ResetNamespace() method. The
handler for that exception looks like this:
except AttributeError:
pass # ???
The obvious questions would be
* what was the non-existent method expected to do?
* could the fact that it didn't get done be causing my problem?
The ResetNamespace method has no documentation, so all I would have
for answers to those questions would be wild guesses.
As a side note, imp.new_module() [3] was deprecated back in Python 3.4.
I'm attaching the logs I captured. One set is for a session in which
the macro was only invoked once (and therefore didn't result in any
observable failures) and the other for the session in which the macro
was invoked twice, with the symptoms described earlier in this thread.
Please let me know if anything else stands out as potentially useful
in the logs.
Cheers,
Bob
[1] https://github.com/SublimeText/Pywin32/blob/master/lib/x64/win32comext/axscript/client/pyscript.py#L353
[2] https://github.com/SublimeText/Pywin32/blob/master/lib/x64/win32comext/axscript/client/pyscript.py#L350
[3] https://docs.python.org/3/library/imp.html#imp.new_module
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pyscript-logs.tar
Type: application/x-tar
Size: 26624 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-win32/attachments/20221129/5d158360/attachment-0001.tar>
More information about the python-win32
mailing list