]> granicus.if.org Git - python/commitdiff
Merged revisions 79645 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Sat, 3 Apr 2010 01:46:38 +0000 (01:46 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sat, 3 Apr 2010 01:46:38 +0000 (01:46 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r79645 | benjamin.peterson | 2010-04-02 20:40:24 -0500 (Fri, 02 Apr 2010) | 13 lines

  Merged revisions 79642,79644 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r79642 | benjamin.peterson | 2010-04-02 20:08:34 -0500 (Fri, 02 Apr 2010) | 1 line

    split out large test function
  ........
    r79644 | benjamin.peterson | 2010-04-02 20:28:57 -0500 (Fri, 02 Apr 2010) | 1 line

    give TypeError when trying to set T_STRING_INPLACE
  ........
................

Lib/test/test_structmembers.py
Misc/NEWS
Modules/_testcapimodule.c
Python/structmember.c

index 93e040cdc85e5cbd56bbdecc1093d49f56ffe594..3679ba2bea0bec6e9a923275601597ca76fa87b0 100644 (file)
@@ -8,17 +8,30 @@ from _testcapi import test_structmembersType, \
 import warnings, unittest, sys
 from test import support
 
-ts=test_structmembersType(False, 1, 2, 3, 4, 5, 6, 7, 8,
-                          9.99999, 10.1010101010)
+ts=test_structmembersType(False,  # T_BOOL
+                          1,      # T_BYTE
+                          2,      # T_UBYTE
+                          3,      # T_SHORT
+                          4,      # T_USHORT
+                          5,      # T_INT
+                          6,      # T_UINT
+                          7,      # T_LONG
+                          8,      # T_ULONG
+                          9.99999,# T_FLOAT
+                          10.1010101010, # T_DOUBLE
+                          "hi" # T_STRING_INPLACE
+                          )
 
 class ReadWriteTests(unittest.TestCase):
-    def test_types(self):
+
+    def test_bool(self):
         ts.T_BOOL = True
         self.assertEquals(ts.T_BOOL, True)
         ts.T_BOOL = False
         self.assertEquals(ts.T_BOOL, False)
         self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1)
 
+    def test_byte(self):
         ts.T_BYTE = CHAR_MAX
         self.assertEquals(ts.T_BYTE, CHAR_MAX)
         ts.T_BYTE = CHAR_MIN
@@ -26,6 +39,7 @@ class ReadWriteTests(unittest.TestCase):
         ts.T_UBYTE = UCHAR_MAX
         self.assertEquals(ts.T_UBYTE, UCHAR_MAX)
 
+    def test_short(self):
         ts.T_SHORT = SHRT_MAX
         self.assertEquals(ts.T_SHORT, SHRT_MAX)
         ts.T_SHORT = SHRT_MIN
@@ -33,6 +47,7 @@ class ReadWriteTests(unittest.TestCase):
         ts.T_USHORT = USHRT_MAX
         self.assertEquals(ts.T_USHORT, USHRT_MAX)
 
+    def test_int(self):
         ts.T_INT = INT_MAX
         self.assertEquals(ts.T_INT, INT_MAX)
         ts.T_INT = INT_MIN
@@ -40,6 +55,7 @@ class ReadWriteTests(unittest.TestCase):
         ts.T_UINT = UINT_MAX
         self.assertEquals(ts.T_UINT, UINT_MAX)
 
+    def test_long(self):
         ts.T_LONG = LONG_MAX
         self.assertEquals(ts.T_LONG, LONG_MAX)
         ts.T_LONG = LONG_MIN
@@ -47,21 +63,26 @@ class ReadWriteTests(unittest.TestCase):
         ts.T_ULONG = ULONG_MAX
         self.assertEquals(ts.T_ULONG, ULONG_MAX)
 
-        ## T_LONGLONG and T_ULONGLONG may not be present on some platforms
-        if hasattr(ts, 'T_LONGLONG'):
-            ts.T_LONGLONG = LLONG_MAX
-            self.assertEquals(ts.T_LONGLONG, LLONG_MAX)
-            ts.T_LONGLONG = LLONG_MIN
-            self.assertEquals(ts.T_LONGLONG, LLONG_MIN)
-
-            ts.T_ULONGLONG = ULLONG_MAX
-            self.assertEquals(ts.T_ULONGLONG, ULLONG_MAX)
-
-            ## make sure these will accept a plain int as well as a long
-            ts.T_LONGLONG = 3
-            self.assertEquals(ts.T_LONGLONG, 3)
-            ts.T_ULONGLONG = 4
-            self.assertEquals(ts.T_ULONGLONG, 4)
+    @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present")
+    def test_longlong(self):
+        ts.T_LONGLONG = LLONG_MAX
+        self.assertEquals(ts.T_LONGLONG, LLONG_MAX)
+        ts.T_LONGLONG = LLONG_MIN
+        self.assertEquals(ts.T_LONGLONG, LLONG_MIN)
+
+        ts.T_ULONGLONG = ULLONG_MAX
+        self.assertEquals(ts.T_ULONGLONG, ULLONG_MAX)
+
+        ## make sure these will accept a plain int as well as a long
+        ts.T_LONGLONG = 3
+        self.assertEquals(ts.T_LONGLONG, 3)
+        ts.T_ULONGLONG = 4
+        self.assertEquals(ts.T_ULONGLONG, 4)
+
+    def test_inplace_string(self):
+        self.assertEquals(ts.T_STRING_INPLACE, "hi")
+        self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s")
+        self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE")
 
 
 class TestWarnings(unittest.TestCase):
index 36204c32941ac39fd5f9d5d6111e7cbf3d68196b..7ba37819d4fb4a636ff57fa846d18a8707a6b8b7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.1.3?
 Core and Builtins
 -----------------
 
+- Raise a TypeError when trying to delete a T_STRING_INPLACE struct member.
+
 - Issue #8226: sys.setfilesystemencoding() raises a LookupError if the encoding
   is unknown
 
index 2d88175c128725bd523b425ed299d176fea1e7aa..0def2307d4c0426720ab233852b903e8755ebbf8 100644 (file)
@@ -1672,6 +1672,7 @@ typedef struct {
        unsigned long ulong_member;
        float float_member;
        double double_member;
+       char inplace_member[5];
 #ifdef HAVE_LONG_LONG
        PY_LONG_LONG longlong_member;
        unsigned PY_LONG_LONG ulonglong_member;
@@ -1695,6 +1696,7 @@ static struct PyMemberDef test_members[] = {
        {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
        {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
        {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
+       {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
 #ifdef HAVE_LONG_LONG
        {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
        {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
@@ -1709,17 +1711,19 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
        static char *keywords[] = {
                "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
                "T_INT", "T_UINT", "T_LONG", "T_ULONG",
-               "T_FLOAT", "T_DOUBLE",
+               "T_FLOAT", "T_DOUBLE", "T_INPLACE_STRING",
 #ifdef HAVE_LONG_LONG  
                "T_LONGLONG", "T_ULONGLONG",
 #endif
                NULL};
-       static char *fmt = "|bbBhHiIlkfd"
+       static char *fmt = "|bbBhHiIlkfds#"
 #ifdef HAVE_LONG_LONG
                "LK"
 #endif
                ;
        test_structmembers *ob;
+       const char *s;
+       Py_ssize_t string_len = 0;
        ob = PyObject_New(test_structmembers, type);
        if (ob == NULL)
                return NULL;
@@ -1735,7 +1739,8 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
                                         &ob->structmembers.long_member,
                                         &ob->structmembers.ulong_member,
                                         &ob->structmembers.float_member,
-                                        &ob->structmembers.double_member
+                                        &ob->structmembers.double_member,
+                                        &s, &string_len
 #ifdef HAVE_LONG_LONG
                                         , &ob->structmembers.longlong_member,
                                         &ob->structmembers.ulonglong_member
@@ -1744,6 +1749,12 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
                Py_DECREF(ob);
                return NULL;
        }
+       if (string_len > 5) {
+               Py_DECREF(ob);
+               PyErr_SetString(PyExc_ValueError, "string too long");
+               return NULL;
+       }
+       strcpy(ob->structmembers.inplace_member, s);
        return (PyObject *)ob;
 }
 
index 8edc35410633db110e7a405cda26e195f5e2096a..711cfe57bdab7cbfb283449370f461ec5b8ca2ff 100644 (file)
@@ -106,7 +106,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
 
        addr += l->offset;
 
-       if ((l->flags & READONLY) || l->type == T_STRING)
+       if ((l->flags & READONLY))
        {
                PyErr_SetString(PyExc_AttributeError, "readonly attribute");
                return -1;
@@ -265,6 +265,10 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
                *(char*)addr = string[0];
                break;
                }
+       case T_STRING:
+        case T_STRING_INPLACE:
+               PyErr_SetString(PyExc_TypeError, "readonly attribute");
+               return -1;
 #ifdef HAVE_LONG_LONG
        case T_LONGLONG:{
                PY_LONG_LONG value;