Author: guido.van.rossum
Date: Fri Nov  2 20:33:24 2007
New Revision: 58787

Modified:
   python/branches/py3k-pep3137/Lib/struct.py
   python/branches/py3k-pep3137/Lib/test/test_struct.py
   python/branches/py3k-pep3137/Modules/_struct.c
Log:
Make test_struct pass.
Accept anything that's bytes as the data input, but not str instances.


Modified: python/branches/py3k-pep3137/Lib/struct.py
==============================================================================
--- python/branches/py3k-pep3137/Lib/struct.py  (original)
+++ python/branches/py3k-pep3137/Lib/struct.py  Fri Nov  2 20:33:24 2007
@@ -26,8 +26,6 @@
 The variable struct.error is an exception raised on errors.
 """
 
-# XXX Move the bytes and str8 casts into the _struct module
-
 __version__ = '3.0'
 
 
@@ -36,7 +34,9 @@
 class Struct(_Struct):
     def __init__(self, fmt):
         if isinstance(fmt, str):
-            fmt = bytes(fmt, 'latin1')
+            fmt = bytes(fmt, 'ascii')
+        elif isinstance(fmt, buffer):
+            fmt = bytes(fmt)
         _Struct.__init__(self, fmt)
 
 _MAXCACHE = 100

Modified: python/branches/py3k-pep3137/Lib/test/test_struct.py
==============================================================================
--- python/branches/py3k-pep3137/Lib/test/test_struct.py        (original)
+++ python/branches/py3k-pep3137/Lib/test/test_struct.py        Fri Nov  2 
20:33:24 2007
@@ -96,7 +96,7 @@
 simple_err(struct.pack, 'i', 3, 3, 3)
 simple_err(struct.pack, 'i', 'foo')
 simple_err(struct.pack, 'P', 'foo')
-simple_err(struct.unpack, 'd', 'flap')
+simple_err(struct.unpack, 'd', b'flap')
 s = struct.pack('ii', 1, 2)
 simple_err(struct.unpack, 'iii', s)
 simple_err(struct.unpack, 'i', s)
@@ -560,7 +560,7 @@
     test_string = b'abcd01234'
     fmt = '4s'
     s = struct.Struct(fmt)
-    for cls in (str, buffer, bytes):
+    for cls in (buffer, bytes):
         if verbose:
             print("test_unpack_from using", cls.__name__)
         data = cls(test_string)
@@ -575,7 +575,7 @@
             vereq(s.unpack_from(data, i), (bytes_data[i:i+4],))
         for i in range(6, len(test_string) + 1):
             simple_err(s.unpack_from, data, i)
-    for cls in (str, buffer, bytes):
+    for cls in (buffer, bytes):
         data = cls(test_string)
         vereq(struct.unpack_from(fmt, data), (b'abcd',))
         vereq(struct.unpack_from(fmt, data, 2), (b'cd01',))

Modified: python/branches/py3k-pep3137/Modules/_struct.c
==============================================================================
--- python/branches/py3k-pep3137/Modules/_struct.c      (original)
+++ python/branches/py3k-pep3137/Modules/_struct.c      Fri Nov  2 20:33:24 2007
@@ -12,11 +12,6 @@
 
 static PyTypeObject PyStructType;
 
-/* compatibility macros */
-#if (PY_VERSION_HEX < 0x02050000)
-typedef int Py_ssize_t;
-#endif
-
 /* If PY_STRUCT_OVERFLOW_MASKING is defined, the struct module will wrap all 
input
    numbers for explicit endians such that they fit in the given type, much
    like explicit casting in C. A warning will be raised if the number did
@@ -411,7 +406,7 @@
                if (msg == NULL)
                        return -1;
                rval = PyErr_WarnEx(PyExc_DeprecationWarning,
-                                   PyString_AS_STRING(msg), 2);
+                                   PyUnicode_AsString(msg), 2);
                Py_DECREF(msg);
                if (rval == 0)
                        return 0;
@@ -1535,37 +1530,25 @@
 strings.");
 
 static PyObject *
-s_unpack(PyObject *self, PyObject *inputstr)
+s_unpack(PyObject *self, PyObject *input)
 {
-       char *start;
-       Py_ssize_t len;
-       PyObject *args=NULL, *result;
+       Py_buffer vbuf;
+       PyObject *result;
        PyStructObject *soself = (PyStructObject *)self;
+
        assert(PyStruct_Check(self));
        assert(soself->s_codes != NULL);
-       if (inputstr == NULL)
-               goto fail;
-       if (PyString_Check(inputstr) &&
-               PyString_GET_SIZE(inputstr) == soself->s_size) {
-                       return s_unpack_internal(soself, 
PyString_AS_STRING(inputstr));
-       }
-       args = PyTuple_Pack(1, inputstr);
-       if (args == NULL)
+       if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
                return NULL;
-       if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len))
-               goto fail;
-       if (soself->s_size != len)
-               goto fail;
-       result = s_unpack_internal(soself, start);
-       Py_DECREF(args);
+       if (vbuf.len != soself->s_size) {
+               PyErr_Format(StructError,
+                            "unpack requires a bytes argument of length %zd",
+                            soself->s_size);
+               return NULL;
+       }
+       result = s_unpack_internal(soself, vbuf.buf);
+       PyObject_ReleaseBuffer(input, &vbuf);
        return result;
-
-fail:
-       Py_XDECREF(args);
-       PyErr_Format(StructError,
-               "unpack requires a string argument of length %zd",
-               soself->s_size);
-       return NULL;
 }
 
 PyDoc_STRVAR(s_unpack_from__doc__,
@@ -1580,37 +1563,33 @@
 s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
 {
        static char *kwlist[] = {"buffer", "offset", 0};
-#if (PY_VERSION_HEX < 0x02050000)
-       static char *fmt = "z#|i:unpack_from";
-#else
-       static char *fmt = "z#|n:unpack_from";
-#endif
-       Py_ssize_t buffer_len = 0, offset = 0;
-       char *buffer = NULL;
+
+       PyObject *input;
+       Py_ssize_t offset = 0;
+       Py_buffer vbuf;
+       PyObject *result;
        PyStructObject *soself = (PyStructObject *)self;
+
        assert(PyStruct_Check(self));
        assert(soself->s_codes != NULL);
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
-                                        &buffer, &buffer_len, &offset))
+       if (!PyArg_ParseTupleAndKeywords(args, kwds,
+                                        "O|n:unpack_from", kwlist,
+                                        &input, &offset))
                return NULL;
-
-       if (buffer == NULL) {
-               PyErr_Format(StructError,
-                       "unpack_from requires a buffer argument");
+       if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
                return NULL;
-       }
-
        if (offset < 0)
-               offset += buffer_len;
-
-       if (offset < 0 || (buffer_len - offset) < soself->s_size) {
+               offset += vbuf.len;
+       if (offset < 0 || vbuf.len - offset < soself->s_size) {
                PyErr_Format(StructError,
                        "unpack_from requires a buffer of at least %zd bytes",
                        soself->s_size);
                return NULL;
        }
-       return s_unpack_internal(soself, buffer + offset);
+       result = s_unpack_internal(soself, vbuf.buf + offset);
+       PyObject_ReleaseBuffer(input, &vbuf);
+       return result;
 }
 
 
_______________________________________________
Python-3000-checkins mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000-checkins

Reply via email to