From 5e948636cd3ac0a982958dfc4488aca1968a0422 Mon Sep 17 00:00:00 2001 From: jay Date: Mon, 9 Jun 2014 13:35:34 +0000 Subject: [PATCH] Speedup Zenmap a little further by using cStringIO in zenmap/zenmapCore/NmapParser.py whenever possible (using StringIO as fallback). --- zenmap/zenmapCore/NmapParser.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/zenmap/zenmapCore/NmapParser.py b/zenmap/zenmapCore/NmapParser.py index 7b343d774..d6b08fd85 100644 --- a/zenmap/zenmapCore/NmapParser.py +++ b/zenmap/zenmapCore/NmapParser.py @@ -125,9 +125,14 @@ import os import os.path import time import socket -import StringIO import copy +# Use the faster cStringIO if available, fallback on StringIO if not +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + # Prevent loading PyXML import xml xml.__path__ = [x for x in xml.__path__ if "_xmlplus" not in x] @@ -476,7 +481,7 @@ class ParserBasics(object): } self.ops = NmapOptions() - self._nmap_output = StringIO.StringIO() + self._nmap_output = StringIO() def set_xml_is_temp(self, xml_is_temp): # This flag is False if a user has specified his own -oX option - in @@ -503,7 +508,8 @@ class ParserBasics(object): def set_nmap_output(self, nmap_output): self._nmap_output.close() del self._nmap_output - self._nmap_output = StringIO.StringIO(nmap_output) + self._nmap_output = StringIO() + self._nmap_output.write(nmap_output) def del_nmap_output(self): self._nmap_output.close() @@ -1092,7 +1098,7 @@ class NmapParserSAX(ParserBasics, ContentHandler): def get_xml(self): """Return a string containing the XML representation of this scan.""" - buffer = StringIO.StringIO() + buffer = StringIO() self.write_xml(buffer) string = buffer.getvalue() buffer.close() @@ -1362,7 +1368,7 @@ class NmapParserSAX(ParserBasics, ContentHandler): class OverrideEntityResolver(EntityResolver): """This class overrides the default behavior of xml.sax to download remote DTDs, instead returning blank strings""" - empty = StringIO.StringIO() + empty = StringIO() def resolveEntity(self, publicId, systemId): return OverrideEntityResolver.empty