Author: troycurtisjr
Date: Thu Dec 28 04:19:17 2017
New Revision: 1819394
URL: http://svn.apache.org/viewvc?rev=1819394&view=rev
Log:
On branch swig-py3: Remove use of hasattr() in python bindings since it can be
subtly different between Python 2 and 3.
* subversion/bindings/swig/include/proxy_apr.swg,
subversion/bindings/swig/python/tests/client.py,
subversion/bindings/swig/python/tests/mergeinfo.py,
subversion/bindings/swig/python/tests/trac/versioncontrol/svn_fs.py
(_mark_weakpool_invalid,
apr_pool_t.valid,
apr_pool_t.destroy
apr_pool_t._wrap,
testGnomeKeyring,
get_svn_merge_range_t_objects,
_get_history): Remove use of hasattr() to ensure consistent behavior with
both Python 2 and 3.
* subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
(svn_swig_ensure_valid_swig_wrapper): New private function for common wrapper
validation code.
(svn_swig_py_convert_ptr, svn_swig_py_must_get_ptr): Use new wrapper
validation function.
Modified:
subversion/branches/swig-py3/subversion/bindings/swig/include/proxy_apr.swg
subversion/branches/swig-py3/subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/client.py
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/mergeinfo.py
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/trac/versioncontrol/svn_fs.py
Modified:
subversion/branches/swig-py3/subversion/bindings/swig/include/proxy_apr.swg
URL:
http://svn.apache.org/viewvc/subversion/branches/swig-py3/subversion/bindings/swig/include/proxy_apr.swg?rev=1819394&r1=1819393&r2=1819394&view=diff
==============================================================================
--- subversion/branches/swig-py3/subversion/bindings/swig/include/proxy_apr.swg
(original)
+++ subversion/branches/swig-py3/subversion/bindings/swig/include/proxy_apr.swg
Thu Dec 28 04:19:17 2017
@@ -100,8 +100,14 @@ class GenericSWIGWrapper:
return self.this
def _mark_weakpool_invalid(weakpool):
- if weakpool and weakpool() and hasattr(weakpool(), "_is_valid"):
- del weakpool()._is_valid
+ if weakpool:
+ pool = weakpool()
+ if pool:
+ try:
+ del pool._is_valid
+ except AttributeError:
+ pass
+
%}
@@ -134,7 +140,11 @@ struct apr_pool_t {
def valid(self):
"""Check whether this memory pool and its parents
are still valid"""
- return hasattr(self,"_is_valid")
+ try:
+ self._is_valid
+ return True
+ except AttributeError:
+ return False
def assert_valid(self):
"""Assert that this memory_pool is still valid."""
@@ -165,10 +175,15 @@ struct apr_pool_t {
self._svn_swig_py_clear_application_pool()
# Mark self as invalid
- if hasattr(self, "_parent_pool"):
+ try:
del self._parent_pool
- if hasattr(self, "_is_valid"):
+ except AttributeError:
+ pass
+
+ try:
del self._is_valid
+ except AttributeError:
+ pass
def __del__(self):
"""Automatically destroy memory pools, if necessary"""
@@ -201,8 +216,11 @@ struct apr_pool_t {
def _wrap(self, obj):
"""Mark a SWIG object as owned by this pool"""
self.assert_valid()
- if hasattr(obj, "set_parent_pool"):
- obj.set_parent_pool(self)
+
+ fn = getattr(obj, 'set_parent_pool', None)
+
+ if fn is not None:
+ fn(self)
return obj
elif obj is None:
return None
Modified:
subversion/branches/swig-py3/subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
URL:
http://svn.apache.org/viewvc/subversion/branches/swig-py3/subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c?rev=1819394&r1=1819393&r2=1819394&view=diff
==============================================================================
---
subversion/branches/swig-py3/subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
(original)
+++
subversion/branches/swig-py3/subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
Thu Dec 28 04:19:17 2017
@@ -195,14 +195,25 @@ int svn_swig_py_get_pool_arg(PyObject *a
if (argnum >= 0)
{
PyObject *input = PyTuple_GET_ITEM(args, argnum);
- if (input != Py_None && PyObject_HasAttrString(input, markValid))
+ if (input != Py_None)
{
- *pool = svn_swig_py_must_get_ptr(input, type, argnum+1);
- if (*pool == NULL)
- return 1;
- *py_pool = input;
- Py_INCREF(input);
- return 0;
+ PyObject *fn;
+ if (NULL != (fn = PyObject_GetAttrString(input, markValid)))
+ {
+ Py_DECREF(fn);
+
+ *pool = svn_swig_py_must_get_ptr(input, type, argnum+1);
+ if (*pool == NULL)
+ return 1;
+ *py_pool = input;
+ Py_INCREF(input);
+ return 0;
+ }
+ else
+ {
+ /* Clear any getattr() error, it isn't needed. */
+ PyErr_Clear();
+ }
}
}
@@ -266,13 +277,20 @@ static int proxy_set_pool(PyObject **pro
{
if (pool == NULL)
{
- if (PyObject_HasAttrString(*proxy, setParentPool))
+ PyObject *setFn;
+ if (NULL != (setFn = PyObject_GetAttrString(*proxy, setParentPool)))
{
- result = PyObject_CallMethod(*proxy, setParentPool, emptyTuple);
+ result = PyObject_CallObject(setFn, NULL);
+ Py_DECREF(setFn);
if (result == NULL)
return 1;
Py_DECREF(result);
}
+ else
+ {
+ /* Clear any getattr() error, it isn't needed. */
+ PyErr_Clear();
+ }
}
else
{
@@ -330,23 +348,45 @@ static PyObject *svn_swig_NewPointerObjS
return svn_swig_py_new_pointer_obj(ptr, typeinfo, py_pool, NULL);
}
+static int svn_swig_ensure_valid_swig_wrapper(PyObject *input)
+{
+ PyObject *assertFn;
+ PyObject *unwrapFn;
+ if (NULL != (assertFn = PyObject_GetAttrString(input, assertValid)))
+ {
+ PyObject *result = PyObject_CallObject(assertFn, NULL);
+ Py_DECREF(assertFn);
+ if (result == NULL)
+ return 1;
+ Py_DECREF(result);
+ }
+ else
+ {
+ /* Clear any getattr() error, it isn't needed. */
+ PyErr_Clear();
+ }
+ if (NULL != (unwrapFn = PyObject_GetAttrString(input, unwrap)))
+ {
+ input = PyObject_CallObject(unwrapFn, NULL);
+ Py_DECREF(unwrapFn);
+ if (input == NULL)
+ return 1;
+ Py_DECREF(input);
+ }
+ else
+ {
+ /* Clear any getattr() error, it isn't needed. */
+ PyErr_Clear();
+ }
+
+ return 0;
+}
+
/** Wrapper for SWIG_ConvertPtr */
int svn_swig_py_convert_ptr(PyObject *input, void **obj, swig_type_info *type)
{
- if (PyObject_HasAttrString(input, assertValid))
- {
- PyObject *result = PyObject_CallMethod(input, assertValid, emptyTuple);
- if (result == NULL)
- return 1;
- Py_DECREF(result);
- }
- if (PyObject_HasAttrString(input, unwrap))
- {
- input = PyObject_CallMethod(input, unwrap, emptyTuple);
- if (input == NULL)
- return 1;
- Py_DECREF(input);
- }
+ if (svn_swig_ensure_valid_swig_wrapper(input))
+ return 1;
return SWIG_ConvertPtr(input, obj, type, SWIG_POINTER_EXCEPTION | 0);
}
@@ -361,21 +401,8 @@ static int svn_swig_ConvertPtrString(PyO
/** Wrapper for SWIG_MustGetPtr */
void *svn_swig_py_must_get_ptr(void *input, swig_type_info *type, int argnum)
{
- if (PyObject_HasAttrString(input, assertValid))
- {
- PyObject *result = PyObject_CallMethod(input, assertValid, emptyTuple);
- if (result == NULL)
- return NULL;
- Py_DECREF(result);
- }
-
- if (PyObject_HasAttrString(input, unwrap))
- {
- input = PyObject_CallMethod(input, unwrap, emptyTuple);
- if (input == NULL)
- return NULL;
- Py_DECREF((PyObject *) input);
- }
+ if (svn_swig_ensure_valid_swig_wrapper(input))
+ return NULL;
return SWIG_MustGetPtr(input, type, argnum, SWIG_POINTER_EXCEPTION | 0);
}
Modified:
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/client.py
URL:
http://svn.apache.org/viewvc/subversion/branches/swig-py3/subversion/bindings/swig/python/tests/client.py?rev=1819394&r1=1819393&r2=1819394&view=diff
==============================================================================
---
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/client.py
(original)
+++
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/client.py
Thu Dec 28 04:19:17 2017
@@ -381,7 +381,7 @@ class SubversionClientTestCase(unittest.
self.assertTrue(not [x for x in providers if not isinstance(x,
core.svn_auth_provider_object_t)])
def testGnomeKeyring(self):
- if not hasattr(core, 'svn_auth_set_gnome_keyring_unlock_prompt_func'):
+ if getattr(core, 'svn_auth_set_gnome_keyring_unlock_prompt_func', None) is
None:
# gnome-keying not compiled in, do nothing
return
Modified:
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/mergeinfo.py
URL:
http://svn.apache.org/viewvc/subversion/branches/swig-py3/subversion/bindings/swig/python/tests/mergeinfo.py?rev=1819394&r1=1819393&r2=1819394&view=diff
==============================================================================
---
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/mergeinfo.py
(original)
+++
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/mergeinfo.py
Thu Dec 28 04:19:17 2017
@@ -34,7 +34,7 @@ def get_svn_merge_range_t_objects():
garbage collector, used for detecting memory leaks."""
return [
o for o in gc.get_objects()
- if hasattr(o, '__class__') and
+ if getattr(o, '__class__', None) is not None and
o.__class__.__name__ == 'svn_merge_range_t'
]
Modified:
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/trac/versioncontrol/svn_fs.py
URL:
http://svn.apache.org/viewvc/subversion/branches/swig-py3/subversion/bindings/swig/python/tests/trac/versioncontrol/svn_fs.py?rev=1819394&r1=1819393&r2=1819394&view=diff
==============================================================================
---
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/trac/versioncontrol/svn_fs.py
(original)
+++
subversion/branches/swig-py3/subversion/bindings/swig/python/tests/trac/versioncontrol/svn_fs.py
Thu Dec 28 04:19:17 2017
@@ -63,7 +63,7 @@ _kindmap = {core.svn_node_dir: Node.DIRE
def _get_history(path, authz, fs_ptr, start, end, limit=None):
history = []
- if hasattr(repos, 'svn_repos_history2'):
+ if getattr(repos, 'svn_repos_history2', None) is not None:
# For Subversion >= 1.1
def authz_cb(root, path, pool):
if limit and len(history) >= limit: