[XML-SIG] [ pyxml-Bugs-616431 ] prepare_input_source and relative path

noreply@sourceforge.net noreply@sourceforge.net
Mon, 30 Sep 2002 02:04:34 -0700


Bugs item #616431, was opened at 2002-09-30 01:04
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=106473&aid=616431&group_id=6473

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Stéphane Bidoul (sbidoul)
Assigned to: Nobody/Anonymous (nobody)
Summary: prepare_input_source and relative path

Initial Comment:
I'm trying to upgrade to PyXML 0.8.1 from 0.7.

Now that expatreader tries to resolve external entities by 
default (which is a good thing), I'm discovering problems 
with relative references to the external subset.

The problem lies in prepare_input_source that reports:

  [...]
  File "C:\soft\Python22\lib\site-
packages\_xmlplus\sax\saxutils.py", line 465, in 
prepare_input_source
    f = urllib2.urlopen(source.getSystemId())
  File "c:\soft\python22\lib\urllib2.py", line 138, in urlopen
    return _opener.open(url, data)
  File "c:\soft\python22\lib\urllib2.py", line 320, in open
    type_ = req.get_type()
  File "c:\soft\python22\lib\urllib2.py", line 224, in 
get_type
    raise ValueError, "unknown url type: %s" % 
self.__original
ValueError: unknown url 
type: ../../../../test/suitedata/lib/suite.dtd

At the end of prepare_input_source, there is:

        if os.path.isfile(sysid):
            basehead = os.path.split(os.path.normpath
(base))[0]
            source.setSystemId(os.path.join(basehead, 
sysid))
            f = open(sysid, "rb")
        else:
            source.setSystemId(urlparse.urljoin(base, 
sysid))
            f = urllib2.urlopen(source.getSystemId())

In my case:
sysid = ..\lib\suite.dtd
base = ..\..\..\..\test\suitedata\cases\cases1.xml

In my test, since the current directory is not the same 
as base's directory, os.isfile("..\lib\suite.dtd") fails, but 
os.isfile("..\..\..\..\test\suitedata\cases\..\lib\suite.dtd") 
succeeds.

Here is a proposed patch that tries to open as a file also 
when the sysid is relative AND the base is a file.

***************
*** 454,463 ****

      if source.getByteStream() is None:
          sysid = source.getSystemId()
!         if os.path.isfile(sysid):
              basehead = os.path.split(os.path.normpath
(base))[0]
              source.setSystemId(os.path.join(basehead, 
sysid))
!             f = open(sysid, "rb")
          else:
              source.setSystemId(urlparse.urljoin(base, 
sysid))
              f = urllib2.urlopen(source.getSystemId())
--- 454,464 ----

      if source.getByteStream() is None:
          sysid = source.getSystemId()
!         if  os.path.isfile(sysid) or \
!             (sysid.startswith(".") and os.path.isfile(base)):
              basehead = os.path.split(os.path.normpath
(base))[0]
              source.setSystemId(os.path.join(basehead, 
sysid))
!             f = open(source.getSystemId(), "rb")
          else:
              source.setSystemId(urlparse.urljoin(base, 
sysid))
              f = urllib2.urlopen(source.getSystemId())


This fix works for me, although it is probably not the 
optimal solution...


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=106473&aid=616431&group_id=6473