How to monkeypatch?
#1
Hi,

I'm trying to come up with a successful way of monkeypatching a module so that all calls to the module's function get routed through the patch.

Background:

I hate doing this:

http://wiki.xbmc.org/index.php?title=HOW...XBMC_paths

Instructing people to modify Eclipse source code just feels wrong to me.

So I wrote this:

Code:
#default.py
import xbmc
import pydevd_file_utils
from functools import wraps

def xbmcfilenamedecorator(func):
    @wraps(func)
    def xbmcfilename(filename):
        filename = xbmc.translatePath(filename)
        return func(filename)
    return xbmcfilename

pydevd_file_utils._NormFile = xbmcfilenamedecorator(pydevd_file_utils._NormFile)
pydevd_file_utils.NormFileToServer = xbmcfilenamedecorator(pydevd_file_utils.NormFileToServer)
pydevd_file_utils.NormFileToClient = xbmcfilenamedecorator(pydevd_file_utils.NormFileToClient)

I tried dropping that in an addon that gets autorun at startup, which works for that run. But it seems the python interpreter is restarted several times, so one script that monkey patches will not force all others too.

I'm probably coming at this from the wrong angle. Advice welcome.
Reply
#2
OK, so I can make the patch work simply by calling the patch file before every call to pydevd.

Eg, everytime you want to debug, you do this:

Code:
import pydev_file_utils_patch #contains the code above
import pydevd
pydevd.settrace('localhost', stdoutToServer=True, stderrToServer=True)

I suppose what I wanted was something implicit, so there's no need to import 'pydev_file_utils_patch'.
Reply
#3
have you tried subclassing the class in pydev your interested in?
For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply
#4
That's an interesting idea. Just looking at the code though, the class I would need to subclass is PyDB, which is approx 1000 lines long, and the bits I need to poke at are nestled in various if/else clauses. The file utils are not built into classes. I'd also need to make sure that the subclass is always called which comes back to the last three lines of the example I gave above (default.py). Ie I need to force the patch in somewhere.

Another option is to have the "import pydevd" in the same file as the patch code, and then refer to it through the patch, eg patch.pydevd.trace(...)

That too seems clunky. Or maybe it's OK/normal. Hmm.
Reply

Logout Mark Read Team Forum Stats Members Help
How to monkeypatch?0