]> granicus.if.org Git - python/commitdiff
Make test_urllib.py pass. Mostly str/bytes issues.
authorGuido van Rossum <guido@python.org>
Tue, 10 Jul 2007 08:30:03 +0000 (08:30 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 10 Jul 2007 08:30:03 +0000 (08:30 +0000)
Also fix mac toolbox glue to accept str, str8, bytes for
255-byte strings.

Lib/test/test_urllib.py
Lib/urllib.py
Python/mactoolboxglue.c

index 7a3f2075fca6d64e86219efbcd453b622e7262ff..e94fc22324051f81c424b44c4d1a2fc3add41ddc 100644 (file)
@@ -30,7 +30,7 @@ class urlopen_FileTests(unittest.TestCase):
 
     def setUp(self):
         """Setup of a temp file to use for testing"""
-        self.text = "test_urllib: %s\n" % self.__class__.__name__
+        self.text = bytes("test_urllib: %s\n" % self.__class__.__name__)
         FILE = open(test_support.TESTFN, 'wb')
         try:
             FILE.write(self.text)
@@ -57,7 +57,7 @@ class urlopen_FileTests(unittest.TestCase):
 
     def test_readline(self):
         self.assertEqual(self.text, self.returned_obj.readline())
-        self.assertEqual('', self.returned_obj.readline(),
+        self.assertEqual(b'', self.returned_obj.readline(),
                          "calling readline() after exhausting the file did not"
                          " return an empty string")
 
@@ -150,7 +150,7 @@ class urlretrieve_FileTests(unittest.TestCase):
 
         # Create a temporary file.
         self.registerFileForCleanUp(test_support.TESTFN)
-        self.text = 'testing urllib.urlretrieve'
+        self.text = b'testing urllib.urlretrieve'
         try:
             FILE = open(test_support.TESTFN, 'wb')
             FILE.write(self.text)
index c6bd87fc568eb0372794107509bf0dad756b639a..dcd906cb2af4b1770b9a97d3a08ab4425cde48f0 100644 (file)
@@ -245,7 +245,7 @@ class URLopener:
             reporthook(blocknum, bs, size)
         while 1:
             block = fp.read(bs)
-            if block == "":
+            if not block:
                 break
             read += len(block)
             tfp.write(block)
@@ -976,7 +976,7 @@ def toBytes(url):
 
 def unwrap(url):
     """unwrap('<URL:type://host/path>') --> 'type://host/path'."""
-    url = url.strip()
+    url = str(url).strip()
     if url[:1] == '<' and url[-1:] == '>':
         url = url[1:-1].strip()
     if url[:4] == 'URL:': url = url[4:].strip()
index 9a8d30b9545c87313290aeedc2d310978d6a7957..85acb5173a5e3224681a9336f379c29a31f4cae4 100644 (file)
@@ -209,14 +209,29 @@ PyMac_BuildNumVersion(NumVersion t)
 int
 PyMac_GetStr255(PyObject *v, Str255 pbuf)
 {
-       int len;
-       if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) {
+       char *ptr;
+       Py_ssize_t len = 1000;
+
+       if (PyUnicode_Check(v)) {
+               v = _PyUnicode_AsDefaultEncodedString(v, NULL);
+               if (v == NULL)
+                       return NULL;
+       }
+       if (PyString_Check(v)) {
+               ptr = PyString_AS_STRING(v);
+               len = PyString_GET_SIZE(v);
+       }
+       else if (PyBytes_Check(v)) {
+               ptr = PyBytes_AS_STRING(v);
+               len = PyBytes_GET_SIZE(v);
+       }
+       if (len > 255) {
                PyErr_SetString(PyExc_TypeError,
-                       "Str255 arg must be string of at most 255 chars");
+                       "Str255 arg must be string/bytes of at most 255 chars");
                return 0;
        }
        pbuf[0] = len;
-       memcpy((char *)(pbuf+1), PyString_AsString(v), len);
+       memcpy((char *)(pbuf+1), ptr, len);
        return 1;
 }