direct: fix various uses of types.MethodType in Python 3 (from #1000)

This commit is contained in:
rdb 2020-09-01 10:54:38 +02:00
parent 1cc82f47b6
commit dae9e31223
5 changed files with 40 additions and 15 deletions

View File

@ -4,6 +4,7 @@ __all__ = ['State']
from direct.directnotify.DirectNotifyGlobal import directNotify from direct.directnotify.DirectNotifyGlobal import directNotify
from direct.showbase.DirectObject import DirectObject from direct.showbase.DirectObject import DirectObject
import sys
class State(DirectObject): class State(DirectObject):
@ -32,6 +33,10 @@ class State(DirectObject):
if type(enterFunc) == types.MethodType: if type(enterFunc) == types.MethodType:
if enterFunc.__func__ == oldFunction: if enterFunc.__func__ == oldFunction:
# print 'found: ', enterFunc, oldFunction # print 'found: ', enterFunc, oldFunction
if sys.version_info >= (3, 0):
state.setEnterFunc(types.MethodType(newFunction,
enterFunc.__self__))
else:
state.setEnterFunc(types.MethodType(newFunction, state.setEnterFunc(types.MethodType(newFunction,
enterFunc.__self__, enterFunc.__self__,
enterFunc.__self__.__class__)) enterFunc.__self__.__class__))
@ -39,6 +44,10 @@ class State(DirectObject):
if type(exitFunc) == types.MethodType: if type(exitFunc) == types.MethodType:
if exitFunc.__func__ == oldFunction: if exitFunc.__func__ == oldFunction:
# print 'found: ', exitFunc, oldFunction # print 'found: ', exitFunc, oldFunction
if sys.version_info >= (3, 0):
state.setExitFunc(types.MethodType(newFunction,
exitFunc.__self__))
else:
state.setExitFunc(types.MethodType(newFunction, state.setExitFunc(types.MethodType(newFunction,
exitFunc.__self__, exitFunc.__self__,
exitFunc.__self__.__class__)) exitFunc.__self__.__class__))

View File

@ -7,6 +7,7 @@ from panda3d.direct import *
from direct.showbase.MessengerGlobal import * from direct.showbase.MessengerGlobal import *
from direct.directnotify.DirectNotifyGlobal import directNotify from direct.directnotify.DirectNotifyGlobal import directNotify
from . import Interval from . import Interval
import sys
############################################################# #############################################################
@ -36,6 +37,10 @@ class FunctionInterval(Interval.Interval):
if type(ival.function) == types.MethodType: if type(ival.function) == types.MethodType:
if ival.function.__func__ == oldFunction: if ival.function.__func__ == oldFunction:
# print 'found: ', ival.function, oldFunction # print 'found: ', ival.function, oldFunction
if sys.version_info >= (3, 0):
ival.function = types.MethodType(newFunction,
ival.function.__self__)
else:
ival.function = types.MethodType(newFunction, ival.function = types.MethodType(newFunction,
ival.function.__self__, ival.function.__self__,
ival.function.__self__.__class__) ival.function.__self__.__class__)

View File

@ -8,6 +8,7 @@ __all__ = ['Messenger']
from .PythonUtil import * from .PythonUtil import *
from direct.directnotify import DirectNotifyGlobal from direct.directnotify import DirectNotifyGlobal
import types import types
import sys
from direct.stdpy.threading import Lock from direct.stdpy.threading import Lock
@ -464,6 +465,9 @@ class Messenger:
# 'oldMethod: ' + repr(oldMethod) + '\n' + # 'oldMethod: ' + repr(oldMethod) + '\n' +
# 'newFunction: ' + repr(newFunction) + '\n') # 'newFunction: ' + repr(newFunction) + '\n')
if (function == oldMethod): if (function == oldMethod):
if sys.version_info >= (3, 0):
newMethod = types.MethodType(newFunction, method.__self__)
else:
newMethod = types.MethodType( newMethod = types.MethodType(
newFunction, method.__self__, method.__self__.__class__) newFunction, method.__self__, method.__self__.__class__)
params[0] = newMethod params[0] = newMethod

View File

@ -1600,6 +1600,9 @@ def appendStr(obj, st):
return s return s
oldStr = Functor(stringer, str(obj)) oldStr = Functor(stringer, str(obj))
stringer = None stringer = None
if sys.version_info >= (3, 0):
obj.__str__ = types.MethodType(Functor(appendedStr, oldStr, st), obj)
else:
obj.__str__ = types.MethodType(Functor(appendedStr, oldStr, st), obj, obj.__class__) obj.__str__ = types.MethodType(Functor(appendedStr, oldStr, st), obj, obj.__class__)
appendedStr = None appendedStr = None
return obj return obj

View File

@ -599,6 +599,10 @@ class TaskManager:
else: else:
function = method function = method
if (function == oldMethod): if (function == oldMethod):
if sys.version_info >= (3, 0):
newMethod = types.MethodType(newFunction,
method.__self__)
else:
newMethod = types.MethodType(newFunction, newMethod = types.MethodType(newFunction,
method.__self__, method.__self__,
method.__self__.__class__) method.__self__.__class__)