[Python-3000-checkins] r56230 - in python/branches/py3k-struni: Lib/test/test_urllib.py Lib/urllib.py Python/mactoolboxglue.c

guido.van.rossum python-3000-checkins at python.org
Tue Jul 10 10:30:03 CEST 2007


Author: guido.van.rossum
Date: Tue Jul 10 10:30:03 2007
New Revision: 56230

Modified:
   python/branches/py3k-struni/Lib/test/test_urllib.py
   python/branches/py3k-struni/Lib/urllib.py
   python/branches/py3k-struni/Python/mactoolboxglue.c
Log:
Make test_urllib.py pass.  Mostly str/bytes issues.
Also fix mac toolbox glue to accept str, str8, bytes for
255-byte strings.


Modified: python/branches/py3k-struni/Lib/test/test_urllib.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_urllib.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_urllib.py	Tue Jul 10 10:30:03 2007
@@ -30,7 +30,7 @@
 
     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 @@
 
     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 @@
 
         # 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)

Modified: python/branches/py3k-struni/Lib/urllib.py
==============================================================================
--- python/branches/py3k-struni/Lib/urllib.py	(original)
+++ python/branches/py3k-struni/Lib/urllib.py	Tue Jul 10 10:30:03 2007
@@ -245,7 +245,7 @@
             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 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()

Modified: python/branches/py3k-struni/Python/mactoolboxglue.c
==============================================================================
--- python/branches/py3k-struni/Python/mactoolboxglue.c	(original)
+++ python/branches/py3k-struni/Python/mactoolboxglue.c	Tue Jul 10 10:30:03 2007
@@ -209,14 +209,29 @@
 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;
 }
 


More information about the Python-3000-checkins mailing list