Exemplo n.º 1
0
def importXML(jar, file, clue=''):
    import xml.parsers.expat
    if type(file) is str:
        file = open(file, 'rb')
    outfile = TemporaryFile()
    data = file.read()
    F = ppml.xmlPickler()
    F.end_handlers['record'] = save_record
    F.end_handlers['ZopeData'] = save_zopedata
    F.start_handlers['ZopeData'] = start_zopedata
    F.binary = 1
    F.file = outfile
    p = xml.parsers.expat.ParserCreate()
    p.CharacterDataHandler = F.handle_data
    p.StartElementHandler = F.unknown_starttag
    p.EndElementHandler = F.unknown_endtag
    r = p.Parse(data)
    outfile.seek(0)
    return jar.importFile(outfile, clue)
Exemplo n.º 2
0
def importXML(jar, file, clue=''):
    import xml.parsers.expat
    if type(file) is str:
        file=open(file, 'rb')
    outfile=TemporaryFile()
    data=file.read()
    F=ppml.xmlPickler()
    F.end_handlers['record'] = save_record
    F.end_handlers['ZopeData'] = save_zopedata
    F.start_handlers['ZopeData'] = start_zopedata
    F.binary=1
    F.file=outfile
    p=xml.parsers.expat.ParserCreate()
    p.CharacterDataHandler=F.handle_data
    p.StartElementHandler=F.unknown_starttag
    p.EndElementHandler=F.unknown_endtag
    r=p.Parse(data)
    outfile.seek(0)
    return jar.importFile(outfile,clue)
Exemplo n.º 3
0
    def test_reduce(self):
        """
    Make sure that a object which uses reduce for pickling can be pickled by xml pickler.
    """
        obj = DummyClass()
        obj.data.append(1)
        obj.data.append(obj)
        obj.data.append(obj.data)

        pattern = re.compile(
            'WAA')  # regex pattern object uses reduce.(See sre.py)
        obj.data.append(pattern)

        pickled_string = pickle.dumps(obj)
        f = StringIO(pickled_string)
        xmldata = str(ppml.ToXMLUnpickler(f).load())

        output = StringIO()

        F = ppml.xmlPickler()
        F.file = output
        F.binary = 1

        p = xml.parsers.pyexpat.ParserCreate()
        p.CharacterDataHandler = F.handle_data
        p.StartElementHandler = F.unknown_starttag
        p.EndElementHandler = F.unknown_endtag

        r = p.Parse(xmldata)

        reconstructed_pickled_data = F._stack[0][0]
        reconstructed_obj = pickle.loads(reconstructed_pickled_data)

        self.assert_(reconstructed_obj.__class__ is DummyClass)
        self.assert_(type(getattr(reconstructed_obj, 'data', None)) is list)
        self.assertEqual(reconstructed_obj.data[0], 1)
        self.assert_(reconstructed_obj.data[1] is reconstructed_obj)
        self.assert_(reconstructed_obj.data[2] is reconstructed_obj.data)
        self.assert_(type(reconstructed_obj.data[3]) is type(pattern))
        self.assertEqual(reconstructed_obj.data[3].pattern, 'WAA')
Exemplo n.º 4
0
  def test_reduce(self):
    """
    Make sure that a object which uses reduce for pickling can be pickled by xml pickler.
    """
    obj = DummyClass()
    obj.data.append(1)
    obj.data.append(obj)
    obj.data.append(obj.data)

    pattern = re.compile('WAA') # regex pattern object uses reduce.(See sre.py)
    obj.data.append(pattern)

    pickled_string = pickle.dumps(obj)
    f = StringIO(pickled_string)
    xmldata = str(ppml.ToXMLUnpickler(f).load())

    output = StringIO()

    F=ppml.xmlPickler()
    F.file = output
    F.binary = 1

    p=xml.parsers.pyexpat.ParserCreate()
    p.CharacterDataHandler=F.handle_data
    p.StartElementHandler=F.unknown_starttag
    p.EndElementHandler=F.unknown_endtag

    r = p.Parse(xmldata)

    reconstructed_pickled_data = F._stack[0][0]
    reconstructed_obj = pickle.loads(reconstructed_pickled_data)

    self.assert_(reconstructed_obj.__class__ is DummyClass)
    self.assert_(type(getattr(reconstructed_obj, 'data', None)) is list)
    self.assertEqual(reconstructed_obj.data[0], 1)
    self.assert_(reconstructed_obj.data[1] is reconstructed_obj)
    self.assert_(reconstructed_obj.data[2] is reconstructed_obj.data)
    self.assert_(type(reconstructed_obj.data[3]) is type(pattern))
    self.assertEqual(reconstructed_obj.data[3].pattern, 'WAA')