Exemplo n.º 1
0
 def visit_method(self, dirname, names):
     d = uri.URI.from_path(os.path.join(dirname, self.sys_path(os.curdir)))
     c = sys.getfilesystemencoding()
     for name in names:
         # on windows, c will be mbcs even if strings are unicode
         bname = name.encode(c) if is_unicode(name) else name
         if bname.startswith(b'??'):
             logging.warn("8-bit path tests limited to ASCII file names "
                          "by %s encoding", c)
             continue
         join_match = os.path.join(dirname, name)
         if is_unicode(name):
             seg_name = uri.escape_data(
                 name.encode(c), uri.is_path_segment_reserved)
         else:
             seg_name = uri.escape_data(name, uri.is_path_segment_reserved)
         u = uri.URI(seg_name)
         u = u.resolve(d)
         self.assertTrue(isinstance(u, uri.FileURL))
         joined = u.get_pathname()
         if isinstance(join_match, bytes) and is_unicode(joined):
             # if we're walking in 8-bit mode we need to downgrade to
             # compare
             joined = joined.encode(c)
         self.assertTrue(joined == join_match,
                         "Joined pathnames mismatch:\n%s\n%s" %
                         (joined, join_match))
Exemplo n.º 2
0
 def visit_method(self, dirname, names):
     d = uri.URI.from_path(os.path.join(dirname, os.curdir))
     c = sys.getfilesystemencoding()
     for name in names:
         if name.startswith('??'):
             logging.warn("8-bit path tests limited to ASCII file names "
                          "by %s encoding", c)
             continue
         join_match = os.path.join(dirname, name)
         if is_unicode(name):
             seg_name = uri.escape_data(
                 name.encode(c), uri.is_path_segment_reserved)
         else:
             seg_name = uri.escape_data(name, uri.is_path_segment_reserved)
         u = uri.URI(seg_name)
         u = u.resolve(d)
         self.assertTrue(isinstance(u, uri.FileURL))
         joined = u.get_pathname()
         if isinstance(join_match, bytes) and is_unicode(joined):
             # if we're walking in 8-bit mode we need to downgrade to
             # compare
             joined = joined.encode(c)
         self.assertTrue(joined == join_match,
                         "Joined pathnames mismatch:\n%s\n%s" %
                         (joined, join_match))
Exemplo n.º 3
0
 def test_constructor(self):
     u = uri.URI(SIMPLE_EXAMPLE)
     self.assertTrue(isinstance(u, uri.URI))
     self.assertTrue(str(u) == SIMPLE_EXAMPLE)
     self.assertTrue(is_unicode(u.octets),
                     "octets must be a character string")
     if py2:
         self.assertTrue(to_text(u) == SIMPLE_EXAMPLE)
     try:
         u = uri.URI(LIST_EXAMPLE)
         # we don't support this type of thing any more
         # self.assertTrue(str(u)==SIMPLE_EXAMPLE,"Simple from list")
     except uri.URIException:
         pass
     u = uri.URI.from_octets(u8(b'\xe8\x8b\xb1\xe5\x9b\xbd.xml'))
     self.assertTrue(
         str(u) == '%E8%8B%B1%E5%9B%BD.xml', "Unicode example: %s" % str(u))
     self.assertTrue(is_unicode(u.octets),
                     "octets must be a character string")
     try:
         u = uri.URI.from_octets(u8(b'\xe8\x8b\xb1\xe5\x9b\xbd.xml'),
                                 strict=True)
         self.fail("strict mode requires %-encoding")
     except uri.URIException:
         pass
     # binary string must be US-ASCII clean
     try:
         u = uri.URI.from_octets(b'Caf\xe9')
         self.fail("binary string must be US-ASCII")
     except UnicodeDecodeError:
         pass
     # but URI-encoded is OK even if it is binary
     u = uri.URI.from_octets(b'Caf%E9')
     self.assertTrue(is_unicode(u.octets),
                     "octets must be a character string")
Exemplo n.º 4
0
 def test_constructor(self):
     u = uri.URI(SIMPLE_EXAMPLE)
     self.assertTrue(isinstance(u, uri.URI))
     self.assertTrue(str(u) == SIMPLE_EXAMPLE)
     self.assertTrue(is_unicode(u.octets),
                     "octets must be a character string")
     if py2:
         self.assertTrue(to_text(u) == SIMPLE_EXAMPLE)
     try:
         u = uri.URI(LIST_EXAMPLE)
         # we don't support this type of thing any more
         # self.assertTrue(str(u)==SIMPLE_EXAMPLE,"Simple from list")
     except uri.URIException:
         pass
     u = uri.URI.from_octets(u8(b'\xe8\x8b\xb1\xe5\x9b\xbd.xml'))
     self.assertTrue(
         str(u) == '%E8%8B%B1%E5%9B%BD.xml', "Unicode example: %s" % str(u))
     self.assertTrue(is_unicode(u.octets),
                     "octets must be a character string")
     try:
         u = uri.URI.from_octets(u8(b'\xe8\x8b\xb1\xe5\x9b\xbd.xml'),
                                 strict=True)
         self.fail("strict mode requires %-encoding")
     except uri.URIException:
         pass
     # binary string must be US-ASCII clean
     try:
         u = uri.URI.from_octets(b'Caf\xe9')
         self.fail("binary string must be US-ASCII")
     except UnicodeDecodeError:
         pass
     # but URI-encoded is OK even if it is binary
     u = uri.URI.from_octets(b'Caf%E9')
     self.assertTrue(is_unicode(u.octets),
                     "octets must be a character string")
Exemplo n.º 5
0
 def test_text(self):
     data = "hello"
     udata = u"hello"
     bdata = b"hello"
     xdata = ['hello']
     self.assertTrue(py2.is_text(data))
     self.assertTrue(py2.is_text(udata))
     if sys.version_info[0] < 3:
         self.assertTrue(py2.is_text(bdata))
     else:
         self.assertFalse(py2.is_text(bdata))
     self.assertFalse(py2.is_text(xdata))
     if sys.version_info[0] < 3:
         self.assertFalse(py2.is_unicode(data))
     else:
         self.assertTrue(py2.is_unicode(data))
     self.assertTrue(py2.is_unicode(udata))
     self.assertFalse(py2.is_unicode(bdata))
     self.assertFalse(py2.is_unicode(xdata))
     # force text forces strings to be text
     self.assertTrue(data == py2.force_text(data))
     self.assertTrue(isinstance(py2.force_text(data), type(u"")))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_text(data), type("")))
     else:
         self.assertTrue(isinstance(py2.force_text(data), type("")))
     self.assertTrue(isinstance(py2.force_text(data), type(u"")))
     self.assertTrue(data == py2.force_text(udata))
     self.assertTrue(isinstance(py2.force_text(udata), type(u"")))
     if sys.version_info[0] < 3:
         # force_text will not throw an error in python 2
         pass
     else:
         try:
             py2.force_text(bdata)
             self.fail("force_text(bytes)")
         except TypeError:
             pass
     # this must work in both python 2 and 3 to prevent accidental
     # conversion to string.
     try:
         py2.force_text(xdata)
         self.fail("force_text(object)")
     except TypeError:
         pass
     # conversion to text
     self.assertTrue(data == py2.to_text(data))
     self.assertTrue(isinstance(py2.to_text(data), type(u"")))
     self.assertTrue(data == py2.to_text(udata))
     self.assertTrue(isinstance(py2.to_text(udata), type(u"")))
     self.assertTrue(data == py2.to_text(bdata))
     self.assertTrue(isinstance(py2.to_text(bdata), type(u"")))
     if sys.version_info[0] < 3:
         self.assertTrue(u"['hello']" == py2.to_text(xdata))
     else:
         self.assertTrue("['hello']" == py2.to_text(xdata))
Exemplo n.º 6
0
 def test_constructor(self):
     e = structures.XMLEntity(b"<hello>")
     self.assertTrue(e.line_num == 1)
     self.assertTrue(e.line_pos == 1)
     self.assertTrue(is_unicode(e.the_char) and e.the_char == '<')
     e = structures.XMLEntity(ul("<hello>"))
     self.assertTrue(is_unicode(e.the_char) and e.the_char == '<')
     e = structures.XMLEntity(StringIO(ul("<hello>")))
     self.assertTrue(e.line_num == 1)
     self.assertTrue(e.line_pos == 1)
     self.assertTrue(is_unicode(e.the_char) and e.the_char == '<')
Exemplo n.º 7
0
 def test_constructor(self):
     e = structures.XMLEntity(b"<hello>")
     self.assertTrue(e.line_num == 1)
     self.assertTrue(e.line_pos == 1)
     self.assertTrue(is_unicode(e.the_char) and e.the_char == '<')
     e = structures.XMLEntity(ul("<hello>"))
     self.assertTrue(is_unicode(e.the_char) and e.the_char == '<')
     e = structures.XMLEntity(StringIO(ul("<hello>")))
     self.assertTrue(e.line_num == 1)
     self.assertTrue(e.line_pos == 1)
     self.assertTrue(is_unicode(e.the_char) and e.the_char == '<')
Exemplo n.º 8
0
 def test_constructor(self):
     try:
         u = urn.URN()
         self.fail("empty constructor")
     except ValueError:
         pass
     try:
         u = urn.URN(nid="foo")
         self.fail("namespace-specific string missing")
     except ValueError:
         pass
     u = urn.URN(nid="foo", nss="a123,456")
     self.assertTrue(isinstance(u, urn.URN))
     self.assertTrue(str(u) == "urn:foo:a123,456")
     self.assertTrue(is_unicode(u.nid))
     self.assertTrue(is_unicode(u.nss))
     u = uri.URI.from_octets('urn:foo:a123,456')
     self.assertTrue(isinstance(u, urn.URN))
     self.assertTrue(str(u) == 'urn:foo:a123,456')
Exemplo n.º 9
0
 def test_unicode_pathnames(self):
     if isinstance(self.data_path, bytes):
         c = sys.getfilesystemencoding()
         data_path = self.data_path.decode(c)
     else:
         data_path = self.data_path
     base = uri.URI.from_path(data_path)
     if os.path.supports_unicode_filenames:
         data_path2 = base.get_pathname()
         self.assertTrue(is_unicode(data_path2),
                         "Expected get_pathname to return unicode")
         self.assertTrue(data_path2 == data_path,
                         ul("Expected %s found %s") %
                         (data_path, data_path2))
         # os.path.walk(data_path,self.visit_method,None)
         for dirpath, dirnames, filenames in os.walk(data_path):
             self.visit_method(dirpath, filenames)
     else:
         data_path2 = base.get_pathname()
         self.assertTrue(isinstance(data_path2, bytes),
                         "Expected get_pathname to return string")
         logging.warn("os.path.supports_unicode_filenames is False "
                      "(skipped unicode path tests)")
Exemplo n.º 10
0
 def test_unicode_pathnames(self):
     if isinstance(self.data_path, bytes):
         c = sys.getfilesystemencoding()
         data_path = self.data_path.encode(c)
     else:
         data_path = self.data_path
     base = uri.URI.from_path(data_path)
     if os.path.supports_unicode_filenames:
         data_path2 = base.get_pathname()
         self.assertTrue(is_unicode(data_path2),
                         "Expected get_pathname to return unicode")
         self.assertTrue(data_path2 == data_path,
                         ul("Expected %s found %s") %
                         (data_path, data_path2))
         # os.path.walk(data_path,self.visit_method,None)
         for dirpath, dirnames, filenames in os.walk(data_path):
             self.visit_method(dirpath, filenames)
     else:
         data_path2 = base.get_pathname()
         self.assertTrue(isinstance(data_path2, bytes),
                         "Expected get_pathname to return string")
         logging.warn("os.path.supports_unicode_filenames is False "
                      "(skipped unicode path tests)")
Exemplo n.º 11
0
 def sys_path(self, path):
     if is_unicode(path) and not os.path.supports_unicode_filenames:
         return path.encode(sys.getfilesystemencoding())
     else:
         return path
Exemplo n.º 12
0
 def test_text(self):
     data = "hello"
     udata = u"hello"
     bdata = b"hello"
     xdata = ['hello']
     self.assertTrue(py2.is_string(data))
     self.assertTrue(py2.is_string(udata))
     self.assertTrue(py2.is_string(bdata))
     self.assertFalse(py2.is_string(xdata))
     self.assertTrue(py2.is_text(data))
     self.assertTrue(py2.is_text(udata))
     if sys.version_info[0] < 3:
         self.assertTrue(py2.is_text(bdata))
     else:
         self.assertFalse(py2.is_text(bdata))
     self.assertFalse(py2.is_text(xdata))
     if sys.version_info[0] < 3:
         self.assertFalse(py2.is_unicode(data))
     else:
         self.assertTrue(py2.is_unicode(data))
     self.assertTrue(py2.is_unicode(udata))
     self.assertFalse(py2.is_unicode(bdata))
     self.assertFalse(py2.is_unicode(xdata))
     # force text forces strings to be text
     self.assertTrue(data == py2.force_text(data))
     self.assertTrue(isinstance(py2.force_text(data), type(u"")))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_text(data), type("")))
     else:
         self.assertTrue(isinstance(py2.force_text(data), type("")))
     self.assertTrue(data == py2.force_text(udata))
     self.assertTrue(isinstance(py2.force_text(udata), type(u"")))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_text(udata), type("")))
     else:
         self.assertTrue(isinstance(py2.force_text(udata), type("")))
     if sys.version_info[0] < 3:
         # force_text will not throw an error in python 2
         pass
     else:
         try:
             py2.force_text(bdata)
             self.fail("force_text(bytes)")
         except TypeError:
             pass
     # this must work in both python 2 and 3 to prevent accidental
     # conversion to string.
     try:
         py2.force_text(xdata)
         self.fail("force_text(object)")
     except TypeError:
         pass
     # force ascii forces strings to be ascii text
     self.assertTrue(data == py2.force_ascii(data))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_ascii(data), type(u"")))
     else:
         self.assertTrue(isinstance(py2.force_ascii(data), type(u"")))
     self.assertTrue(isinstance(py2.force_ascii(data), type("")))
     self.assertTrue(data == py2.force_ascii(udata))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_ascii(udata), type(u"")))
     else:
         self.assertTrue(isinstance(py2.force_ascii(udata), type(u"")))
     self.assertTrue(isinstance(py2.force_ascii(udata), type("")))
     if sys.version_info[0] < 3:
         self.assertTrue(bdata == py2.force_ascii(bdata))
         self.assertFalse(isinstance(py2.force_ascii(bdata), type(u"")))
     else:
         # can't compare different types in Python 3
         self.assertFalse(bdata == py2.force_ascii(bdata))
         self.assertTrue(isinstance(py2.force_ascii(bdata), type(u"")))
     self.assertTrue(isinstance(py2.force_ascii(bdata), type("")))
     # this must work in both python 2 and 3 to prevent accidental
     # conversion to string.
     try:
         py2.force_ascii(xdata)
         self.fail("force_ascii(object)")
     except TypeError:
         pass
     # conversion to text
     self.assertTrue(data == py2.to_text(data))
     self.assertTrue(isinstance(py2.to_text(data), type(u"")))
     self.assertTrue(data == py2.to_text(udata))
     self.assertTrue(isinstance(py2.to_text(udata), type(u"")))
     self.assertTrue(data == py2.to_text(bdata))
     self.assertTrue(isinstance(py2.to_text(bdata), type(u"")))
     if sys.version_info[0] < 3:
         self.assertTrue(u"['hello']" == py2.to_text(xdata))
     else:
         self.assertTrue("['hello']" == py2.to_text(xdata))
     # check the empty text constant:
     self.assertTrue(isinstance(py2.uempty, type(u"")))
     self.assertFalse(py2.uempty)
     self.assertTrue(len(py2.uempty) == 0)
Exemplo n.º 13
0
 def test_text(self):
     data = "hello"
     udata = u"hello"
     bdata = b"hello"
     xdata = ['hello']
     self.assertTrue(py2.is_string(data))
     self.assertTrue(py2.is_string(udata))
     self.assertTrue(py2.is_string(bdata))
     self.assertFalse(py2.is_string(xdata))
     self.assertTrue(py2.is_text(data))
     self.assertTrue(py2.is_text(udata))
     if sys.version_info[0] < 3:
         self.assertTrue(py2.is_text(bdata))
     else:
         self.assertFalse(py2.is_text(bdata))
     self.assertFalse(py2.is_text(xdata))
     if sys.version_info[0] < 3:
         self.assertFalse(py2.is_unicode(data))
     else:
         self.assertTrue(py2.is_unicode(data))
     self.assertTrue(py2.is_unicode(udata))
     self.assertFalse(py2.is_unicode(bdata))
     self.assertFalse(py2.is_unicode(xdata))
     # force text forces strings to be text
     self.assertTrue(data == py2.force_text(data))
     self.assertTrue(isinstance(py2.force_text(data), type(u"")))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_text(data), type("")))
     else:
         self.assertTrue(isinstance(py2.force_text(data), type("")))
     self.assertTrue(data == py2.force_text(udata))
     self.assertTrue(isinstance(py2.force_text(udata), type(u"")))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_text(udata), type("")))
     else:
         self.assertTrue(isinstance(py2.force_text(udata), type("")))
     if sys.version_info[0] < 3:
         # force_text will not throw an error in python 2
         pass
     else:
         try:
             py2.force_text(bdata)
             self.fail("force_text(bytes)")
         except TypeError:
             pass
     # this must work in both python 2 and 3 to prevent accidental
     # conversion to string.
     try:
         py2.force_text(xdata)
         self.fail("force_text(object)")
     except TypeError:
         pass
     # force ascii forces strings to be ascii text
     self.assertTrue(data == py2.force_ascii(data))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_ascii(data), type(u"")))
     else:
         self.assertTrue(isinstance(py2.force_ascii(data), type(u"")))
     self.assertTrue(isinstance(py2.force_ascii(data), type("")))
     self.assertTrue(data == py2.force_ascii(udata))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_ascii(udata), type(u"")))
     else:
         self.assertTrue(isinstance(py2.force_ascii(udata), type(u"")))
     self.assertTrue(isinstance(py2.force_ascii(udata), type("")))
     if sys.version_info[0] < 3:
         self.assertTrue(bdata == py2.force_ascii(bdata))
         self.assertFalse(isinstance(py2.force_ascii(bdata), type(u"")))
     else:
         # can't compare different types in Python 3
         self.assertFalse(bdata == py2.force_ascii(bdata))
         self.assertTrue(isinstance(py2.force_ascii(bdata), type(u"")))
     self.assertTrue(isinstance(py2.force_ascii(bdata), type("")))
     # this must work in both python 2 and 3 to prevent accidental
     # conversion to string.
     try:
         py2.force_ascii(xdata)
         self.fail("force_ascii(object)")
     except TypeError:
         pass
     # conversion to text
     self.assertTrue(data == py2.to_text(data))
     self.assertTrue(isinstance(py2.to_text(data), type(u"")))
     self.assertTrue(data == py2.to_text(udata))
     self.assertTrue(isinstance(py2.to_text(udata), type(u"")))
     self.assertTrue(data == py2.to_text(bdata))
     self.assertTrue(isinstance(py2.to_text(bdata), type(u"")))
     if sys.version_info[0] < 3:
         self.assertTrue(u"['hello']" == py2.to_text(xdata))
     else:
         self.assertTrue("['hello']" == py2.to_text(xdata))
     # check the empty text constant:
     self.assertTrue(isinstance(py2.uempty, type(u"")))
     self.assertFalse(py2.uempty)
     self.assertTrue(len(py2.uempty) == 0)
Exemplo n.º 14
0
 def sys_path(self, path):
     if is_unicode(path) and not os.path.supports_unicode_filenames:
         return path.encode(sys.getfilesystemencoding())
     else:
         return path