Browse Source

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

rdb 5 years ago
parent
commit
dae9e31223

+ 15 - 6
direct/src/fsm/State.py

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

+ 8 - 3
direct/src/interval/FunctionInterval.py

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

+ 6 - 2
direct/src/showbase/Messenger.py

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

+ 4 - 1
direct/src/showbase/PythonUtil.py

@@ -1600,7 +1600,10 @@ def appendStr(obj, st):
             return s
         oldStr = Functor(stringer, str(obj))
         stringer = None
-    obj.__str__ = types.MethodType(Functor(appendedStr, oldStr, st), obj, obj.__class__)
+    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__)
     appendedStr = None
     return obj
 

+ 7 - 3
direct/src/task/Task.py

@@ -599,9 +599,13 @@ class TaskManager:
         else:
             function = method
         if (function == oldMethod):
-            newMethod = types.MethodType(newFunction,
-                                         method.__self__,
-                                         method.__self__.__class__)
+            if sys.version_info >= (3, 0):
+                newMethod = types.MethodType(newFunction,
+                                             method.__self__)
+            else:
+                newMethod = types.MethodType(newFunction,
+                                             method.__self__,
+                                             method.__self__.__class__)
             task.setFunction(newMethod)
             # Found a match
             return 1