Browse Source

work around mystery print failure in Python

David Rose 22 years ago
parent
commit
f06c2c81f8

+ 5 - 0
direct/src/directnotify/DirectNotify.py

@@ -15,6 +15,11 @@ class DirectNotify:
         # create a default log file
         self.logger = Logger.Logger()
 
+        # This will get filled in later by ShowBase.py with a
+        # C++-level StreamWriter object for writing to standard
+        # output.
+        self.streamWriter = None
+
     def __str__(self):
         """__str__(self)
         Print handling routine"""

+ 18 - 11
direct/src/directnotify/Notifier.py

@@ -105,7 +105,7 @@ class Notifier:
         if (self.__warning):
             string = (self.getTime() + self.__name + '(warning): ' + warningString)
             self.__log(string)
-            print(string)
+            self.__print(string)
         return 1 # to allow assert(myNotify.warning("blah"))
 
     def setWarning(self, bool):
@@ -125,7 +125,7 @@ class Notifier:
         if (self.__debug):
             string = (self.getTime() + self.__name + '(debug): ' + debugString)
             self.__log(string)
-            print(string)
+            self.__print(string)
         return 1 # to allow assert(myNotify.debug("blah"))
 
     def setDebug(self, bool):
@@ -145,7 +145,7 @@ class Notifier:
         if (self.__info):
             string = (self.getTime() + self.__name + '(info): ' + infoString)
             self.__log(string)
-            print(string)
+            self.__print(string)
         return 1 # to allow assert(myNotify.info("blah"))
 
     def getInfo(self):
@@ -175,11 +175,18 @@ class Notifier:
         Set the logging flag to int (1=on, 0=off)"""
         self.__logging = bool
 
-
-
-
-
-
-
-
-
+    def __print(self, string):
+        """__print(self, string)
+        Prints the string to standard output followed by a newline.
+        """
+        # We could use the print command, but that seems to stop
+        # working when Python is in the program-level exception
+        # handler for some reason (!).  So we use C++-level output
+        # instead, as soon as we have the DirectNotify level
+        # streamWriter available.
+
+        import DirectNotifyGlobal
+        if DirectNotifyGlobal.directNotify.streamWriter:
+            DirectNotifyGlobal.directNotify.streamWriter.appendData(string + '\n')
+        else:
+            print string

+ 4 - 0
direct/src/showbase/ShowBase.py

@@ -190,6 +190,10 @@ class ShowBase(DirectObject.DirectObject):
         __builtins__["globalClock"] = ClockObject.getGlobalClock()
         __builtins__["vfs"] = vfs
 
+        # Route all of our DirectNotify output through the Panda level
+        # ostream object from now on.
+        directNotify.streamWriter = StreamWriter(ostream)
+
         # Now hang a hook on the window-event from Panda.  This allows
         # us to detect when the user resizes, minimizes, or closes the
         # main window.