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@7963 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-12-10 00:36:12 +00:00
commit 5d36d0409e
3 changed files with 77 additions and 63 deletions

View file

@ -6,15 +6,6 @@
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;
char* vptr = 0;
if (!pchar_info) pchar_info = SWIG_TypeQuery("char *");
if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) == SWIG_OK) {
if (cptr) *cptr = vptr;
if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0;
if (alloc) *alloc = SWIG_OLDOBJ;
return SWIG_OK;
} else {
if (PyString_Check(obj)) { if (PyString_Check(obj)) {
char *cstr; int len; char *cstr; int len;
PyString_AsStringAndSize(obj, &cstr, &len); PyString_AsStringAndSize(obj, &cstr, &len);
@ -22,14 +13,14 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
if (alloc) { if (alloc) {
/* /*
In python the user should not be able to modify the inner In python the user should not be able to modify the inner
string representation, so, by default we return a copy of string representation. To warranty that, if you define
the buffer, as in the wstring case. SWIG_PYTHON_SAFE_CSTRINGS, a new/copy of the python string
buffer is always returned.
But, if you use the -DSWIG_PYTHON_UNSAFE_CSTR at The default behavior is just to return the pointer value,
compilation time, you will get, at your own risk, the so, be careful.
inner string pointer instead, when possible.
*/ */
#ifndef SWIG_PYTHON_UNSAFE_CSTR #if defined(SWIG_PYTHON_SAFE_CSTRINGS)
if (*alloc != SWIG_OLDOBJ) if (*alloc != SWIG_OLDOBJ)
#else #else
if (*alloc == SWIG_NEWOBJ) if (*alloc == SWIG_NEWOBJ)
@ -48,6 +39,15 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
} }
if (psize) *psize = len + 1; if (psize) *psize = len + 1;
return SWIG_OK; return SWIG_OK;
} else {
char* vptr = 0;
static swig_type_info* pchar_info = 0;
if (!pchar_info) pchar_info = SWIG_TypeQuery("char *");
if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) == SWIG_OK) {
if (cptr) *cptr = vptr;
if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0;
if (alloc) *alloc = SWIG_OLDOBJ;
return SWIG_OK;
} }
} }
return SWIG_TypeError; return SWIG_TypeError;

View file

@ -6,14 +6,6 @@
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;
wchar_t * vptr = 0;
if (!pwchar_info) pwchar_info = SWIG_TypeQuery("wchar_t *");
if (SWIG_ConvertPtr(obj, (void**)&vptr, pwchar_info, 0) == SWIG_OK) {
if (cptr) *cptr = vptr;
if (psize) *psize = vptr ? (wcslen(vptr) + 1) : 0;
return SWIG_OK;
} else {
PyObject *tmp = 0; PyObject *tmp = 0;
int isunicode = PyUnicode_Check(obj); int isunicode = PyUnicode_Check(obj);
if (!isunicode && PyString_Check(obj)) { if (!isunicode && PyString_Check(obj)) {
@ -33,6 +25,14 @@ SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc)
if (alloc) *alloc = cptr ? SWIG_NEWOBJ : 0; if (alloc) *alloc = cptr ? SWIG_NEWOBJ : 0;
if (tmp) Py_DECREF(tmp); if (tmp) Py_DECREF(tmp);
return SWIG_OK; return SWIG_OK;
} else {
static swig_type_info* pwchar_info = 0;
wchar_t * vptr = 0;
if (!pwchar_info) pwchar_info = SWIG_TypeQuery("wchar_t *");
if (SWIG_ConvertPtr(obj, (void**)&vptr, pwchar_info, 0) == SWIG_OK) {
if (cptr) *cptr = vptr;
if (psize) *psize = vptr ? (wcslen(vptr) + 1) : 0;
return SWIG_OK;
} }
} }
return SWIG_TypeError; return SWIG_TypeError;

View file

@ -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");