📄 swigutil_py.c
字号:
/* * swigutil_py.c: utility functions for the SWIG Python bindings * * ==================================================================== * Copyright (c) 2000-2004 CollabNet. All rights reserved. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at http://subversion.tigris.org/license-1.html. * If newer versions of this license are posted there, you may use a * newer version instead, at your option. * * This software consists of voluntary contributions made by many * individuals. For exact contribution history, see the revision * history and logs, available at http://subversion.tigris.org/. * ==================================================================== *//* Tell swigutil_py.h that we're inside the implementation */#define SVN_SWIG_SWIGUTIL_PY_C#include <Python.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <apr_pools.h>#include <apr_hash.h>#include <apr_portable.h>#include <apr_thread_proc.h>#include "svn_client.h"#include "svn_string.h"#include "svn_opt.h"#include "svn_delta.h"#include "svn_auth.h"#include "svn_pools.h"#include "svn_private_config.h" /* for SVN_APR_INT64_T_PYCFMT */#include "swig_python_external_runtime.swg"#include "swigutil_py.h"/*** Manage the Global Interpreter Lock ***//* If both Python and APR have threads available, we can optimize ourselves * by releasing the global interpreter lock when we drop into our SVN calls. * * In svn_types.i, svn_swig_py_release_py_lock is called before every * function, then svn_swig_py_acquire_py_lock is called after every * function. So, if these functions become no-ops, then Python will * start to block... * * The Subversion libraries can be assumed to be thread-safe *only* when * APR_HAS_THREAD is 1. The APR pool allocations aren't thread-safe unless * APR_HAS_THREAD is 1. */#if defined(WITH_THREAD) && APR_HAS_THREADS#define ACQUIRE_PYTHON_LOCK#endif#ifdef ACQUIRE_PYTHON_LOCKstatic apr_threadkey_t *_saved_thread_key = NULL;static apr_pool_t *_saved_thread_pool = NULL;#endifvoid svn_swig_py_release_py_lock(void){#ifdef ACQUIRE_PYTHON_LOCK PyThreadState *thread_state; if (_saved_thread_key == NULL) { /* Obviously, creating a top-level pool for this is pretty stupid. */ apr_pool_create(&_saved_thread_pool, NULL); apr_threadkey_private_create(&_saved_thread_key, NULL, _saved_thread_pool); } thread_state = PyEval_SaveThread(); apr_threadkey_private_set(thread_state, _saved_thread_key);#endif}void svn_swig_py_acquire_py_lock(void){#ifdef ACQUIRE_PYTHON_LOCK void *val; PyThreadState *thread_state; apr_threadkey_private_get(&val, _saved_thread_key); thread_state = val; PyEval_RestoreThread(thread_state);#endif}/*** Automatic Pool Management Functions ***//* The application pool */static apr_pool_t *_global_pool = NULL;static PyObject *_global_svn_swig_py_pool = NULL;static char assertValid[] = "assert_valid";static char parentPool[] = "_parent_pool";static char wrap[] = "_wrap";static char unwrap[] = "_unwrap";static char setParentPool[] = "set_parent_pool";static char emptyTuple[] = "()";static char objectTuple[] = "(O)";apr_status_t svn_swig_py_initialize(void){ apr_status_t status; if ((status = apr_initialize()) != APR_SUCCESS) return status; if (atexit(apr_terminate) != 0) return APR_EGENERAL; return APR_SUCCESS;}/* Set the application pool */void svn_swig_py_set_application_pool(PyObject *py_pool, apr_pool_t *pool) { _global_pool = pool; _global_svn_swig_py_pool = py_pool;}/* Clear the application pool */void svn_swig_py_clear_application_pool(){ _global_pool = NULL; _global_svn_swig_py_pool = NULL;}/* Get the application pool */void svn_swig_get_application_pool(PyObject **py_pool, apr_pool_t **pool){ *pool = _global_pool; *py_pool = _global_svn_swig_py_pool;}/* Set the parent pool of a proxy object */static int proxy_set_pool(PyObject **proxy, PyObject *pool){ PyObject *result; if (*proxy != NULL) { if (pool == NULL) { if (PyObject_HasAttrString(*proxy, setParentPool)) { result = PyObject_CallMethod(*proxy, setParentPool, emptyTuple); if (result == NULL) { return 1; } Py_DECREF(result); } } else { result = PyObject_CallMethod(pool, wrap, objectTuple, *proxy); Py_DECREF(*proxy); *proxy = result; } } return 0;}/* Wrapper for SWIG_TypeQuery */#define svn_swig_TypeQuery(x) SWIG_TypeQuery(x)/** Wrapper for SWIG_NewPointerObj */PyObject *svn_swig_NewPointerObj(void *obj, swig_type_info *type, PyObject *pool){ PyObject *proxy = SWIG_NewPointerObj(obj, type, 0); if (proxy == NULL) { return NULL; } if (proxy_set_pool(&proxy, pool)) { Py_DECREF(proxy); return NULL; } return proxy;}/** svn_swig_NewPointerObj, except a string is used to describe the type */static PyObject *svn_swig_NewPointerObjString(void *ptr, const char *type, PyObject *py_pool){ swig_type_info *typeinfo = svn_swig_TypeQuery(type); if (typeinfo == NULL) { PyErr_SetString(PyExc_TypeError, "Cannot find required typeobject"); return NULL; } /* ### cache the swig_type_info at some point? */ return svn_swig_NewPointerObj(ptr, typeinfo, py_pool);}/** Wrapper for SWIG_ConvertPtr */int svn_swig_ConvertPtr(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); } return SWIG_ConvertPtr(input, obj, type, SWIG_POINTER_EXCEPTION | 0);}/** svn_swig_ConvertPtr, except a string is used to describe the type */static int svn_swig_ConvertPtrString(PyObject *input, void **obj, const char *type){ return svn_swig_ConvertPtr(input, obj, svn_swig_TypeQuery(type));}/** Wrapper for SWIG_MustGetPtr */void *svn_swig_MustGetPtr(void *input, swig_type_info *type, int argnum, PyObject **py_pool){ if (PyObject_HasAttrString(input, assertValid)) { PyObject *result = PyObject_CallMethod(input, assertValid, emptyTuple); if (result == NULL) { return NULL; } Py_DECREF(result); } if (py_pool != NULL) { if (PyObject_HasAttrString(input, parentPool)) { *py_pool = PyObject_GetAttrString(input, parentPool); Py_DECREF(*py_pool); } else { *py_pool = _global_svn_swig_py_pool; } } if (PyObject_HasAttrString(input, unwrap)) { input = PyObject_CallMethod(input, unwrap, emptyTuple); if (input == NULL) { return NULL; } Py_DECREF((PyObject *) input); } return SWIG_MustGetPtr(input, type, argnum, SWIG_POINTER_EXCEPTION | 0);} /*** Custom SubversionException stuffs. ***//* Global SubversionException class object. */static PyObject *SubversionException = NULL;PyObject *svn_swig_py_exception_type(void){ Py_INCREF(SubversionException); return SubversionException;}PyObject *svn_swig_py_register_exception(void){ /* If we haven't created our exception class, do so. */ if (SubversionException == NULL) { SubversionException = PyErr_NewException ((char *)"libsvn._core.SubversionException", NULL, NULL); } /* Regardless, return the exception class. */ return svn_swig_py_exception_type();}void svn_swig_py_svn_exception(svn_error_t *err){ PyObject *exc_ob, *apr_err_ob; if (err == NULL) return; /* Make an integer for the error code. */ apr_err_ob = PyInt_FromLong(err->apr_err); if (apr_err_ob == NULL) return; /* Instantiate a SubversionException object. */ exc_ob = PyObject_CallFunction(SubversionException, (char *)"sO", err->message, apr_err_ob); if (exc_ob == NULL) { Py_DECREF(apr_err_ob); return; } /* Set the "apr_err" attribute of the exception to our error code. */ if (PyObject_SetAttrString(exc_ob, (char *)"apr_err", apr_err_ob) == -1) { Py_DECREF(apr_err_ob); Py_DECREF(exc_ob); return; } /* Finished with the apr_err object. */ Py_DECREF(apr_err_ob); /* Set the error state to our exception object. */ PyErr_SetObject(SubversionException, exc_ob); /* Finished with the exc_ob object. */ Py_DECREF(exc_ob);}/*** Helper/Conversion Routines ***//* Functions for making Python wrappers around Subversion structs */static PyObject *make_ob_pool(void *pool){ /* Return a brand new default pool to Python. This pool isn't * normally used for anything. It's just here for compatibility * with Subversion 1.2. */ apr_pool_t *new_pool = svn_pool_create(_global_pool); PyObject *new_py_pool = svn_swig_NewPointerObj(new_pool, svn_swig_TypeQuery("apr_pool_t *"), _global_svn_swig_py_pool); (void) pool; /* Silence compiler warnings about unused parameter. */ return new_py_pool;}static PyObject *make_ob_fs_root(svn_fs_root_t *ptr, PyObject *py_pool){ return svn_swig_NewPointerObjString(ptr, "svn_fs_root_t *", py_pool);} /***//* Conversion from Python single objects (not hashes/lists/etc.) to Subversion types. */static const char *make_string_from_ob(PyObject *ob, apr_pool_t *pool){ if (ob == Py_None) return NULL; if (! PyString_Check(ob)) { PyErr_SetString(PyExc_TypeError, "not a string"); return NULL; } return apr_pstrdup(pool, PyString_AS_STRING(ob));}static svn_string_t *make_svn_string_from_ob(PyObject *ob, apr_pool_t *pool){ if (ob == Py_None) return NULL; if (! PyString_Check(ob)) { PyErr_SetString(PyExc_TypeError, "not a string"); return NULL; } return svn_string_create(PyString_AS_STRING(ob), pool);}/***/static PyObject *convert_hash(apr_hash_t *hash, PyObject * (*converter_func)(void *value, void *ctx, PyObject *py_pool), void *ctx, PyObject *py_pool){ apr_hash_index_t *hi; PyObject *dict = PyDict_New(); if (dict == NULL) return NULL; for (hi = apr_hash_first(NULL, hash); hi; hi = apr_hash_next(hi)) { const void *key; void *val; PyObject *value; apr_hash_this(hi, &key, NULL, &val); value = (*converter_func)(val, ctx, py_pool); if (value == NULL) { Py_DECREF(dict); return NULL; } /* ### gotta cast this thing cuz Python doesn't use "const" */ if (PyDict_SetItemString(dict, (char *)key, value) == -1) { Py_DECREF(value); Py_DECREF(dict); return NULL; } Py_DECREF(value); } return dict;}static PyObject *convert_to_swigtype(void *value, void *ctx, PyObject *py_pool){ /* ctx is a 'swig_type_info *' */ return svn_swig_NewPointerObj(value, ctx, py_pool);}static PyObject *convert_svn_string_t(void *value, void *ctx, PyObject *py_pool){ /* ctx is unused */ const svn_string_t *s = value; /* ### gotta cast this thing cuz Python doesn't use "const" */ return PyString_FromStringAndSize((void *)s->data, s->len);}static PyObject *convert_svn_client_commit_item_t(void *value, void *ctx){ PyObject *list; PyObject *path, *kind, *url, *rev, *cf_url, *state; svn_client_commit_item_t *item = value; /* ctx is unused */ list = PyList_New(6); if (item->path) path = PyString_FromString(item->path); else { path = Py_None; Py_INCREF(Py_None); } if (item->url) url = PyString_FromString(item->url); else { url = Py_None; Py_INCREF(Py_None); } if (item->copyfrom_url) cf_url = PyString_FromString(item->copyfrom_url); else { cf_url = Py_None; Py_INCREF(Py_None); } kind = PyInt_FromLong(item->kind); rev = PyInt_FromLong(item->revision); state = PyInt_FromLong(item->state_flags); if (! (list && path && kind && url && rev && cf_url && state)) { Py_XDECREF(list); Py_XDECREF(path); Py_XDECREF(kind); Py_XDECREF(url); Py_XDECREF(rev); Py_XDECREF(cf_url); Py_XDECREF(state); return NULL; } PyList_SET_ITEM(list, 0, path); PyList_SET_ITEM(list, 1, kind); PyList_SET_ITEM(list, 2, url); PyList_SET_ITEM(list, 3, rev); PyList_SET_ITEM(list, 4, cf_url); PyList_SET_ITEM(list, 5, state); return list;}PyObject *svn_swig_py_prophash_to_dict(apr_hash_t *hash){ return convert_hash(hash, convert_svn_string_t, NULL, NULL);}PyObject *svn_swig_py_locationhash_to_dict(apr_hash_t *hash){ /* Need special code for this because of the darned svn_revnum_t keys. */ apr_hash_index_t *hi; PyObject *dict = PyDict_New(); if (dict == NULL) return NULL; for (hi = apr_hash_first(NULL, hash); hi; hi = apr_hash_next(hi)) { const void *k; void *v; PyObject *key, *value; apr_hash_this(hi, &k, NULL, &v); key = PyLong_FromLong(*(svn_revnum_t *)k); if (key == NULL) { Py_DECREF(dict); return NULL; } value = PyString_FromString((char *)v); if (value == NULL) { Py_DECREF(key); Py_DECREF(dict); return NULL; } if (PyDict_SetItem(dict, key, value) == -1) { Py_DECREF(value); Py_DECREF(dict); return NULL; } Py_DECREF(value); Py_DECREF(key); } return dict;}PyObject *svn_swig_py_convert_hash(apr_hash_t *hash, swig_type_info *type, PyObject *py_pool){ return convert_hash(hash, convert_to_swigtype, type, py_pool);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -