add safecstrings option and leave the default of string conversions as in older version, ie, return the pointer not a copy
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7963 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
efcc7aeacf
commit
e0eea5f625
3 changed files with 77 additions and 63 deletions
|
|
@ -6,47 +6,47 @@
|
||||||
SWIGINTERN int
|
SWIGINTERN int
|
||||||
SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
|
SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
|
||||||
{
|
{
|
||||||
static swig_type_info* pchar_info = 0;
|
if (PyString_Check(obj)) {
|
||||||
char* vptr = 0;
|
char *cstr; int len;
|
||||||
if (!pchar_info) pchar_info = SWIG_TypeQuery("char *");
|
PyString_AsStringAndSize(obj, &cstr, &len);
|
||||||
if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) == SWIG_OK) {
|
if (cptr) {
|
||||||
if (cptr) *cptr = vptr;
|
if (alloc) {
|
||||||
if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0;
|
/*
|
||||||
if (alloc) *alloc = SWIG_OLDOBJ;
|
In python the user should not be able to modify the inner
|
||||||
|
string representation. To warranty that, if you define
|
||||||
|
SWIG_PYTHON_SAFE_CSTRINGS, a new/copy of the python string
|
||||||
|
buffer is always returned.
|
||||||
|
|
||||||
|
The default behavior is just to return the pointer value,
|
||||||
|
so, be careful.
|
||||||
|
*/
|
||||||
|
#if defined(SWIG_PYTHON_SAFE_CSTRINGS)
|
||||||
|
if (*alloc != SWIG_OLDOBJ)
|
||||||
|
#else
|
||||||
|
if (*alloc == SWIG_NEWOBJ)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
*cptr = %new_copy_array(cstr, len + 1, char);
|
||||||
|
*alloc = SWIG_NEWOBJ;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*cptr = PyString_AsString(obj);
|
||||||
|
*alloc = SWIG_OLDOBJ;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*cptr = PyString_AsString(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (psize) *psize = len + 1;
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
} else {
|
} else {
|
||||||
if (PyString_Check(obj)) {
|
char* vptr = 0;
|
||||||
char *cstr; int len;
|
static swig_type_info* pchar_info = 0;
|
||||||
PyString_AsStringAndSize(obj, &cstr, &len);
|
if (!pchar_info) pchar_info = SWIG_TypeQuery("char *");
|
||||||
if (cptr) {
|
if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) == SWIG_OK) {
|
||||||
if (alloc) {
|
if (cptr) *cptr = vptr;
|
||||||
/*
|
if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0;
|
||||||
In python the user should not be able to modify the inner
|
if (alloc) *alloc = SWIG_OLDOBJ;
|
||||||
string representation, so, by default we return a copy of
|
|
||||||
the buffer, as in the wstring case.
|
|
||||||
|
|
||||||
But, if you use the -DSWIG_PYTHON_UNSAFE_CSTR at
|
|
||||||
compilation time, you will get, at your own risk, the
|
|
||||||
inner string pointer instead, when possible.
|
|
||||||
*/
|
|
||||||
#ifndef SWIG_PYTHON_UNSAFE_CSTR
|
|
||||||
if (*alloc != SWIG_OLDOBJ)
|
|
||||||
#else
|
|
||||||
if (*alloc == SWIG_NEWOBJ)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
*cptr = %new_copy_array(cstr, len + 1, char);
|
|
||||||
*alloc = SWIG_NEWOBJ;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
*cptr = PyString_AsString(obj);
|
|
||||||
*alloc = SWIG_OLDOBJ;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
*cptr = PyString_AsString(obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (psize) *psize = len + 1;
|
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,32 +6,32 @@
|
||||||
SWIGINTERN int
|
SWIGINTERN int
|
||||||
SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc)
|
SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc)
|
||||||
{
|
{
|
||||||
static swig_type_info* pwchar_info = 0;
|
PyObject *tmp = 0;
|
||||||
wchar_t * vptr = 0;
|
int isunicode = PyUnicode_Check(obj);
|
||||||
if (!pwchar_info) pwchar_info = SWIG_TypeQuery("wchar_t *");
|
if (!isunicode && PyString_Check(obj)) {
|
||||||
if (SWIG_ConvertPtr(obj, (void**)&vptr, pwchar_info, 0) == SWIG_OK) {
|
if (cptr) {
|
||||||
if (cptr) *cptr = vptr;
|
obj = tmp = PyUnicode_FromObject(obj);
|
||||||
if (psize) *psize = vptr ? (wcslen(vptr) + 1) : 0;
|
}
|
||||||
|
isunicode = 1;
|
||||||
|
}
|
||||||
|
if (isunicode) {
|
||||||
|
int len = PyUnicode_GetSize(obj);
|
||||||
|
if (cptr) {
|
||||||
|
*cptr = %new_array(len + 1, wchar_t);
|
||||||
|
PyUnicode_AsWideChar((PyUnicodeObject *)obj, *cptr, len);
|
||||||
|
(*cptr)[len] = 0;
|
||||||
|
}
|
||||||
|
if (psize) *psize = (size_t) len + 1;
|
||||||
|
if (alloc) *alloc = cptr ? SWIG_NEWOBJ : 0;
|
||||||
|
if (tmp) Py_DECREF(tmp);
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
} else {
|
} else {
|
||||||
PyObject *tmp = 0;
|
static swig_type_info* pwchar_info = 0;
|
||||||
int isunicode = PyUnicode_Check(obj);
|
wchar_t * vptr = 0;
|
||||||
if (!isunicode && PyString_Check(obj)) {
|
if (!pwchar_info) pwchar_info = SWIG_TypeQuery("wchar_t *");
|
||||||
if (cptr) {
|
if (SWIG_ConvertPtr(obj, (void**)&vptr, pwchar_info, 0) == SWIG_OK) {
|
||||||
obj = tmp = PyUnicode_FromObject(obj);
|
if (cptr) *cptr = vptr;
|
||||||
}
|
if (psize) *psize = vptr ? (wcslen(vptr) + 1) : 0;
|
||||||
isunicode = 1;
|
|
||||||
}
|
|
||||||
if (isunicode) {
|
|
||||||
int len = PyUnicode_GetSize(obj);
|
|
||||||
if (cptr) {
|
|
||||||
*cptr = %new_array(len + 1, wchar_t);
|
|
||||||
PyUnicode_AsWideChar((PyUnicodeObject *)obj, *cptr, len);
|
|
||||||
(*cptr)[len] = 0;
|
|
||||||
}
|
|
||||||
if (psize) *psize = (size_t) len + 1;
|
|
||||||
if (alloc) *alloc = cptr ? SWIG_NEWOBJ : 0;
|
|
||||||
if (tmp) Py_DECREF(tmp);
|
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,7 @@ static int threads = 0;
|
||||||
static int nothreads = 0;
|
static int nothreads = 0;
|
||||||
static int classptr = 0;
|
static int classptr = 0;
|
||||||
static int shadowimport = 1;
|
static int shadowimport = 1;
|
||||||
|
static int safecstrings = 0;
|
||||||
|
|
||||||
/* flags for the make_autodoc function */
|
/* flags for the make_autodoc function */
|
||||||
enum autodoc_t {
|
enum autodoc_t {
|
||||||
|
|
@ -95,6 +96,8 @@ Python Options (available with -python)\n\
|
||||||
-noh - Don't generate the output header file\n\
|
-noh - Don't generate the output header file\n\
|
||||||
-noproxy - Don't generate proxy classes \n\
|
-noproxy - Don't generate proxy classes \n\
|
||||||
-noproxyimport - Don't insert proxy import statements derived from the %import directive \n\
|
-noproxyimport - Don't insert proxy import statements derived from the %import directive \n\
|
||||||
|
-safecstrings - Use safer (but slower) C string mapping, generating copies from Python -> C/C++\n\
|
||||||
|
-nosafecstrings - Avoid extra strings copies when possible (default)\n\
|
||||||
\n";
|
\n";
|
||||||
|
|
||||||
class PYTHON : public Language {
|
class PYTHON : public Language {
|
||||||
|
|
@ -254,6 +257,12 @@ public:
|
||||||
/* Turn off thread suppor mode */
|
/* Turn off thread suppor mode */
|
||||||
nothreads = 1;
|
nothreads = 1;
|
||||||
Swig_mark_arg(i);
|
Swig_mark_arg(i);
|
||||||
|
} else if (strcmp(argv[i],"-safecstrings") == 0) {
|
||||||
|
safecstrings = 1;
|
||||||
|
Swig_mark_arg(i);
|
||||||
|
} else if (strcmp(argv[i],"-nosafecstrings") == 0) {
|
||||||
|
safecstrings = 0;
|
||||||
|
Swig_mark_arg(i);
|
||||||
} else if (strcmp(argv[i],"-modern") == 0) {
|
} else if (strcmp(argv[i],"-modern") == 0) {
|
||||||
apply = 0;
|
apply = 0;
|
||||||
classic = 0;
|
classic = 0;
|
||||||
|
|
@ -383,6 +392,11 @@ public:
|
||||||
Printf(f_runtime,"#define SWIG_PYTHON_THREADS\n");
|
Printf(f_runtime,"#define SWIG_PYTHON_THREADS\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (safecstrings) {
|
||||||
|
Printf(f_runtime,"#define SWIG_PYTHON_SAFE_CSTRINGS\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Set module name */
|
/* Set module name */
|
||||||
module = Copy(Getattr(n,"name"));
|
module = Copy(Getattr(n,"name"));
|
||||||
mainmodule = Getattr(n,"name");
|
mainmodule = Getattr(n,"name");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue