2002-02-04 00:17:01 +00:00
#!/usr/bin/python -u
import libxml2
import sys
# Memory debug specific
libxml2 . debugMemory ( 1 )
doc = libxml2 . newDoc ( " 1.0 " )
comment = doc . newDocComment ( " This is a generated document " )
doc . addChild ( comment )
pi = libxml2 . newPI ( " test " , " PI content " )
doc . addChild ( pi )
root = doc . newChild ( None , " doc " , None )
ns = root . newNs ( " http://example.com/doc " , " my " )
root . setNs ( ns )
elem = root . newChild ( None , " foo " , " bar " )
elem . setBase ( " http://example.com/imgs " )
elem . setProp ( " img " , " image.gif " )
doc . saveFile ( " tmp.xml " )
doc . freeDoc ( )
doc = libxml2 . parseFile ( " tmp.xml " )
comment = doc . children
if comment . type != " comment " or \
comment . content != " This is a generated document " :
2013-03-30 21:38:20 +08:00
print ( " error rereading comment " )
2002-02-04 00:17:01 +00:00
sys . exit ( 1 )
pi = comment . next
if pi . type != " pi " or pi . name != " test " or pi . content != " PI content " :
2013-03-30 21:38:20 +08:00
print ( " error rereading PI " )
2002-02-04 00:17:01 +00:00
sys . exit ( 1 )
root = pi . next
if root . name != " doc " :
2013-03-30 21:38:20 +08:00
print ( " error rereading root " )
2002-02-04 00:17:01 +00:00
sys . exit ( 1 )
ns = root . ns ( )
if ns . name != " my " or ns . content != " http://example.com/doc " :
2013-03-30 21:38:20 +08:00
print ( " error rereading namespace " )
2002-02-04 00:17:01 +00:00
sys . exit ( 1 )
elem = root . children
if elem . name != " foo " :
2013-03-30 21:38:20 +08:00
print ( " error rereading elem " )
2002-02-04 00:17:01 +00:00
sys . exit ( 1 )
if elem . getBase ( None ) != " http://example.com/imgs " :
2013-03-30 21:38:20 +08:00
print ( " error rereading base " )
2002-02-04 00:17:01 +00:00
sys . exit ( 1 )
if elem . prop ( " img " ) != " image.gif " :
2013-03-30 21:38:20 +08:00
print ( " error rereading property " )
2002-02-04 00:17:01 +00:00
sys . exit ( 1 )
doc . freeDoc ( )
# Memory debug specific
libxml2 . cleanupParser ( )
if libxml2 . debugMemory ( 1 ) == 0 :
2013-03-30 21:38:20 +08:00
print ( " OK " )
2002-02-04 00:17:01 +00:00
else :
2013-03-30 21:38:20 +08:00
print ( " Memory leak %d bytes " % ( libxml2 . debugMemory ( 1 ) ) )
2002-02-04 00:17:01 +00:00
libxml2 . dumpMemory ( )