Commit 0f791eac authored by Tres Seaver's avatar Tres Seaver

Tolerate unicode attribute names.

Only in ASCII encoding.

Fixes LP #143358.

Cherry-pick 8e455700 from master.
parent 6fb96530
...@@ -4,6 +4,8 @@ Changelog ...@@ -4,6 +4,8 @@ Changelog
2.13.9 (unreleased) 2.13.9 (unreleased)
------------------- -------------------
- Tolerate Unicode attribute names (ASCII only). LP #143358.
- Make module-level ``aq_acquire`` API respect the ``default`` parameter. - Make module-level ``aq_acquire`` API respect the ``default`` parameter.
LP #1387363. LP #1387363.
......
...@@ -474,10 +474,16 @@ Wrapper_findattr(Wrapper *self, PyObject *oname, ...@@ -474,10 +474,16 @@ Wrapper_findattr(Wrapper *self, PyObject *oname,
attribute. attribute.
*/ */
{ {
PyObject *r, *v, *tb; PyObject *r, *v, *tb, *tmp;
char *name=""; char *name="";
if (PyString_Check(oname)) name=PyString_AS_STRING(oname); if (PyString_Check(oname)) name=PyString_AS_STRING(oname);
if (PyUnicode_Check(oname)) {
tmp=PyUnicode_AsASCIIString(oname);
if (tmp==NULL) return NULL;
name=PyString_AS_STRING(tmp);
Py_DECREF(tmp);
}
if ((*name=='a' && name[1]=='q' && name[2]=='_') || if ((*name=='a' && name[1]=='q' && name[2]=='_') ||
(strcmp(name, "__parent__")==0)) (strcmp(name, "__parent__")==0))
{ {
...@@ -716,10 +722,17 @@ Wrapper_getattro(Wrapper *self, PyObject *oname) ...@@ -716,10 +722,17 @@ Wrapper_getattro(Wrapper *self, PyObject *oname)
static PyObject * static PyObject *
Xaq_getattro(Wrapper *self, PyObject *oname) Xaq_getattro(Wrapper *self, PyObject *oname)
{ {
PyObject *tmp;
char *name=""; char *name="";
/* Special case backward-compatible acquire method. */ /* Special case backward-compatible acquire method. */
if (PyString_Check(oname)) name=PyString_AS_STRING(oname); if (PyString_Check(oname)) name=PyString_AS_STRING(oname);
if (PyUnicode_Check(oname)) {
tmp=PyUnicode_AsASCIIString(oname);
if (tmp==NULL) return NULL;
name=PyString_AS_STRING(tmp);
Py_DECREF(tmp);
}
if (*name=='a' && name[1]=='c' && strcmp(name+2,"quire")==0) if (*name=='a' && name[1]=='c' && strcmp(name+2,"quire")==0)
return Py_FindAttr(OBJECT(self),oname); return Py_FindAttr(OBJECT(self),oname);
...@@ -733,10 +746,17 @@ Xaq_getattro(Wrapper *self, PyObject *oname) ...@@ -733,10 +746,17 @@ Xaq_getattro(Wrapper *self, PyObject *oname)
static int static int
Wrapper_setattro(Wrapper *self, PyObject *oname, PyObject *v) Wrapper_setattro(Wrapper *self, PyObject *oname, PyObject *v)
{ {
PyObject *tmp;
char *name=""; char *name="";
/* Allow assignment to parent, to change context. */ /* Allow assignment to parent, to change context. */
if (PyString_Check(oname)) name=PyString_AS_STRING(oname); if (PyString_Check(oname)) name=PyString_AS_STRING(oname);
if (PyUnicode_Check(oname)) {
tmp=PyUnicode_AsASCIIString(oname);
if (tmp==NULL) return -1;
name=PyString_AS_STRING(tmp);
Py_DECREF(tmp);
}
if ((*name=='a' && name[1]=='q' && name[2]=='_' if ((*name=='a' && name[1]=='q' && name[2]=='_'
&& strcmp(name+3,"parent")==0) || (strcmp(name, "__parent__")==0)) && strcmp(name+3,"parent")==0) || (strcmp(name, "__parent__")==0))
{ {
......
...@@ -2528,6 +2528,11 @@ class TestAcquire(unittest.TestCase): ...@@ -2528,6 +2528,11 @@ class TestAcquire(unittest.TestCase):
def test_unwrapped_falls_back_to_default(self): def test_unwrapped_falls_back_to_default(self):
self.assertEqual(self.acquire(object(), 'nonesuch', default=4), 4) self.assertEqual(self.acquire(object(), 'nonesuch', default=4), 4)
def test_w_unicode_attr_name(self):
# See https://bugs.launchpad.net/acquisition/+bug/143358
found = self.acquire(self.a.b.c, u'aq_parent')
self.assertTrue(found.aq_self is self.a.b.aq_self)
class TestUnicode(unittest.TestCase): class TestUnicode(unittest.TestCase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment