Skip to content

Commit

Permalink
Tolerate unicode attribute names.
Browse files Browse the repository at this point in the history
Only in ASCII encoding.

Fixes LP #143358.

Cherry-pick 8e45570 from master.
  • Loading branch information
tseaver committed Feb 17, 2015
1 parent 6fb9653 commit 0f791ea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
2.13.9 (unreleased)
-------------------

- Tolerate Unicode attribute names (ASCII only). LP #143358.

- Make module-level ``aq_acquire`` API respect the ``default`` parameter.
LP #1387363.

Expand Down
22 changes: 21 additions & 1 deletion src/Acquisition/_Acquisition.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,16 @@ Wrapper_findattr(Wrapper *self, PyObject *oname,
attribute.
*/
{
PyObject *r, *v, *tb;
PyObject *r, *v, *tb, *tmp;
char *name="";

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]=='_') ||
(strcmp(name, "__parent__")==0))
{
Expand Down Expand Up @@ -716,10 +722,17 @@ Wrapper_getattro(Wrapper *self, PyObject *oname)
static PyObject *
Xaq_getattro(Wrapper *self, PyObject *oname)
{
PyObject *tmp;
char *name="";

/* Special case backward-compatible acquire method. */
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)
return Py_FindAttr(OBJECT(self),oname);

Expand All @@ -733,10 +746,17 @@ Xaq_getattro(Wrapper *self, PyObject *oname)
static int
Wrapper_setattro(Wrapper *self, PyObject *oname, PyObject *v)
{
PyObject *tmp;
char *name="";

/* Allow assignment to parent, to change context. */
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]=='_'
&& strcmp(name+3,"parent")==0) || (strcmp(name, "__parent__")==0))
{
Expand Down
5 changes: 5 additions & 0 deletions src/Acquisition/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2528,6 +2528,11 @@ class NotWrapped(object):
def test_unwrapped_falls_back_to_default(self):
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):

Expand Down

0 comments on commit 0f791ea

Please sign in to comment.