summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Madden <jamadden@gmail.com>2017-11-07 09:54:55 -0600
committerJason Madden <jamadden@gmail.com>2017-11-07 10:04:04 -0600
commit43814c7caaa0c377dac4f06067080d7916fa3980 (patch)
treeb753ad7e445a504d9f11be37c3bfcf4ea2e56aa4
parentc155a0ffe2941c205d7e7a64fa0014549ca1b280 (diff)
downloadzope-proxy-43814c7caaa0c377dac4f06067080d7916fa3980.tar.gz
Simplify getting the char* in get/setattro for clarity and performance
Under Python 2, we were doing PyUnicode_check->PyUnicode_AsEncodedString->PyString_Check->PyString_ASSTRING. This can be simplified to PyString_AsString(). Internally it does the same unicode check if needed. It produces a better error message if the object is not a string or unicode. It is able to use the *cached* byte string, and it doesn't allocate a new object. Because we don't allocate a new object (sometimes) anymore, the extra INCREF/DECREF can go away. We're also able to drop one type check. Under Python 3, we were doing PyUnicode_Check->PyUnicode_AsUTF8String -> PyBytes_AS_STRING. This can be simified to PyUnicode_AsUTF8. This avoids an intermediate object and can use the cached byte string. We can also drop the extra INCREF/DECREF since we don't ever have a new object. Python 2 docs claimed that the underlying tp_getattro slots need a string object, but the unicode->string handling is done internally in Python's PyObject_GetAttr now, AFAICS. (It's difficult to get a unicode object in to these methods in the first place; someone must be explicitly calling the __getattribute__ dunder method.)
-rw-r--r--CHANGES.rst3
-rw-r--r--src/zope/proxy/_zope_proxy_proxy.c57
2 files changed, 13 insertions, 47 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 3f1131d..c475402 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -4,7 +4,8 @@ Changes
4.3.1 (unreleased)
------------------
-- Nothing changed yet.
+- Simplify the internal C handling of attribute names in
+ ``__getattribute__`` and ``__setattr__``.
4.3.0 (2017-09-13)
diff --git a/src/zope/proxy/_zope_proxy_proxy.c b/src/zope/proxy/_zope_proxy_proxy.c
index e201337..b64874a 100644
--- a/src/zope/proxy/_zope_proxy_proxy.c
+++ b/src/zope/proxy/_zope_proxy_proxy.c
@@ -45,10 +45,6 @@ empty_tuple = NULL;
// Compatibility with Python 2
#if PY_MAJOR_VERSION < 3
- #define IS_STRING PyString_Check
-
- #define MAKE_STRING(name) PyString_AS_STRING(name)
-
#define MOD_ERROR_VAL
#define MOD_SUCCESS_VAL(val)
@@ -59,12 +55,6 @@ empty_tuple = NULL;
ob = Py_InitModule3(name, methods, doc);
#else
-
- #define IS_STRING PyUnicode_Check
-
- #define MAKE_STRING(name) PyBytes_AS_STRING( \
- PyUnicode_AsUTF8String(name))
-
#define MOD_ERROR_VAL NULL
#define MOD_SUCCESS_VAL(val) val
@@ -244,26 +234,15 @@ wrap_getattro(PyObject *self, PyObject *name)
const char *name_as_string;
int maybe_special_name;
-#if PY_MAJOR_VERSION < 3 && defined(Py_USING_UNICODE)
- /* The Unicode to string conversion is done here because the
- existing tp_setattro slots expect a string object as name
- (except under Python 3) and we wouldn't want to break those. */
- if (PyUnicode_Check(name)) {
- name = PyUnicode_AsEncodedString(name, NULL, NULL);
- if (name == NULL)
- return NULL;
- }
- else
+#if PY_MAJOR_VERSION < 3
+ name_as_string = PyString_AsString(name);
+#else
+ name_as_string = PyUnicode_AsUTF8(name);
#endif
- if (!IS_STRING(name)){
- PyErr_SetString(PyExc_TypeError, "attribute name must be string");
+ if (name_as_string == NULL) {
return NULL;
}
- else
- Py_INCREF(name);
-
- name_as_string = MAKE_STRING(name);
wrapped = Proxy_GET_OBJECT(self);
if (wrapped == NULL) {
@@ -315,7 +294,6 @@ wrap_getattro(PyObject *self, PyObject *name)
res = PyObject_GetAttr(wrapped, name);
finally:
- Py_DECREF(name);
return res;
}
@@ -327,25 +305,15 @@ wrap_setattro(PyObject *self, PyObject *name, PyObject *value)
const char *name_as_string;
int res = -1;
-#if PY_MAJOR_VERSION < 3 && defined(Py_USING_UNICODE)
- /* The Unicode to string conversion is done here because the
- existing tp_setattro slots expect a string object as name
- (except under Python 3) and we wouldn't want to break those. */
-
- if (PyUnicode_Check(name)) {
- name = PyUnicode_AsEncodedString(name, NULL, NULL);
- if (name == NULL)
- return -1;
- }
- else
+#if PY_MAJOR_VERSION < 3
+ name_as_string = PyString_AsString(name);
+#else
+ name_as_string = PyUnicode_AsUTF8(name);
#endif
- if (!IS_STRING(name)){
- PyErr_SetString(PyExc_TypeError, "attribute name must be string");
- return -1;
+ if (name_as_string == NULL) {
+ return NULL;
}
- else
- Py_INCREF(name);
descriptor = WrapperType_Lookup(self->ob_type, name);
@@ -359,8 +327,6 @@ wrap_setattro(PyObject *self, PyObject *name, PyObject *value)
goto finally;
}
- name_as_string = MAKE_STRING(name);
-
wrapped = Proxy_GET_OBJECT(self);
if (wrapped == NULL) {
PyErr_Format(PyExc_RuntimeError,
@@ -371,7 +337,6 @@ wrap_setattro(PyObject *self, PyObject *name, PyObject *value)
res = PyObject_SetAttr(wrapped, name, value);
finally:
- Py_DECREF(name);
return res;
}