Commits

Gregory Petukhov committed 05bde74

Fix bug: segfault when multiple Grab instance with Kit transport are created

Comments (0)

Files changed (3)

grab/kit/__init__.py

 
 
 class Kit(object):
+    _app = None
+
     def __init__(self, gui=False):
-        self.app = QApplication(sys.argv)
+        if not Kit._app:
+            Kit._app = QApplication([])
 
         manager = KitNetworkAccessManager()
         manager.finished.connect(self.network_reply_handler)
 
         self.view = KitWebView()
         self.view.setPage(self.page)
-        self.view.setApplication(self.app)
+        self.view.setApplication(Kit._app)
 
         if gui:
             self.view.show()

grab/transport/kit.py

     Grab network transport powered with QtWebKit module
     """
 
-    pass
-    #def __init__(self):
-        #self.curl = pycurl.Curl()
+    def __init__(self):
+        self.kit = Kit()
 
     #def setup_body_file(self, storage_dir, storage_filename):
         #if storage_filename is None:
         self.request_object['user_agent'] = grab.config['user_agent']
 
     def request(self):
-        kit = Kit()
         req = self.request_object
-        self.kit_response = kit.request(
+        self.kit_response = self.kit.request(
             url=req['url'],
             cookies=req['cookies'],
             method=req['method'],
 
         return self.kit_response.cookies
 
-    #def __getstate__(self):
-        #"""
-        #Reset curl attribute which could not be pickled.
-        #"""
-        #state = self.__dict__.copy()
-        #state['curl'] = None
-        #return state
+    def __getstate__(self):
+        """
+        Reset curl attribute which could not be pickled.
+        """
+        state = self.__dict__.copy()
+        state['kit'] = None
+        return state
 
-    #def __setstate__(self, state):
-        #"""
-        #Create pycurl instance after Grag instance was restored
-        #from pickled state.
-        #"""
+    def __setstate__(self, state):
+        """
+        Create pycurl instance after Grag instance was restored
+        from pickled state.
+        """
 
-        #state['curl'] = pycurl.Curl()
-        #self.__dict__ = state
+        state['kit'] = Kit()
+        self.__dict__ = state
 
 
 class GrabKit(Grab):

test/grab_pickle.py

             text = g2.doc.select('//textarea').text()
             resultq.put(text)
 
-        open('data', 'w').write(data)
         result_queue = Queue()
         p = Process(target=func, args=[data, result_queue])
         p.start()