[bfprog] Unregistering Handlers
Forrest Thiessen
thiessen at cyberscapearena.com
Sun Jul 24 18:46:17 PDT 2005
Thanks for the suggestion. . . it turns out this won't work, though, for
a complicated reason:
When a handler is registered, the BF2 Python host gets a reference to
the callback function. If you change the function by any normal method
(for example, setting the attribute that contains the reference to it to
point to a different function), the event system in the Python host
still has it's ORIGINAL reference to the ORIGINAL callback--and that's
where the callback will go. That's why I wanted to unregister the
handler: because as long as it remains registered, the callbacks go to
the same place, no matter what you do. Even if you somehow managed to
nuke the original handler so that it looks like it's gone, Python keeps
track of objects via reference counting, and it would *know* that a
reference still existed somewhere to the original handler, and so would
not let it be destroyed.
I eventually figured out a byzantine way around the problem, at least
for my purposes:
http://bf2.fun-o-matic.org/index.php/Cookbook:Dynamically_Changing_an_Event_Handler
The method I came up with recognizes that I can't do anything about the
original handler function object, but I *can* swap out the code object
inside of it. It doesn't result in the handler becoming unregistered,
but what I was really after was a way to replace a standard handler with
one of my own, anyway, so this fits the bill.
If you really, really wanted to get an effect identical to
unregistering, you could just make the replacement function in the
recipie implement nothing but a "pass" statement.
--Forrest (aka "Woody")
>Date: Sat, 23 Jul 2005 23:34:59 +0200
>From: "Einar S. Ids?" <esi at itk.ntnu.no>
>Subject: Re: [bfprog] Unregistering Handlers
>To: Battlefield Programming <bfprog at lists.matureasskickers.net>
>Message-ID: <42E2B803.9060205 at itk.ntnu.no>
>Content-Type: text/plain; charset=ISO-8859-1
>
>Like Steven said, this method don't seem to exist. I don't know what
>technique Steven uses in his Modmanager, though I am looking forward to
>seeing his work, but, depending on why you need to unregister the event
>handler, you might get by by simply reassigning the function being
>called to do absolutely nothing? It would still incur the time to call,
>obviously, but just let it pass/return immediately.
>
>You could use a simple dummy-function for this which takes nothing but
>key/val arguments, so that each and every function you want to
>cancel/unregister can be redirected to this single dummy-function.
>
>Einar
>
More information about the BFProg
mailing list