Skip to content

Commit

Permalink
Remove outdated pystrings.swg
Browse files Browse the repository at this point in the history
Remove pystring.swg - a very ancient copy from SWIG which does not
compile when using swig-4.2.0. Instead SWIG's version of pystrings.swg
that is kept up to date in SWIG is used. SWIG_PYTHON_STRICT_BYTE_CHAR
needs to be defined in order to maintain the current behaviour of only
allowing Python 3 byte type instead of Python 3 string type as input.

As the output of swig is in graphviz_wrap.c and is committed to the
repo, the updates committed in this file (keeping the current swig-4.1.1
version). A later commit could update it to swig-4.2.0.

Fixes build problems on Fedora where SWIG_Python_str_AsChar no longer exists,
as reported at swig/swig#2778.
  • Loading branch information
wsfulton committed Jan 24, 2024
1 parent 011907e commit ce4e9cc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 112 deletions.
4 changes: 4 additions & 0 deletions pygraphviz/graphviz.i
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
%module graphviz

%begin %{
#define SWIG_PYTHON_STRICT_BYTE_CHAR
%}

%{
#include "graphviz/cgraph.h"
#include "graphviz/gvc.h"
Expand Down
106 changes: 80 additions & 26 deletions pygraphviz/graphviz_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */

#define SWIG_PYTHON_STRICT_BYTE_CHAR



#define SWIG_VERSION 0x040101
#define SWIGPYTHON
Expand Down Expand Up @@ -3038,49 +3041,96 @@ SWIGINTERN int
SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
{
#if PY_VERSION_HEX>=0x03000000
#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
if (PyBytes_Check(obj))
#else
if (PyUnicode_Check(obj))
#endif
#else
if (PyString_Check(obj))
#endif
{
char *cstr; Py_ssize_t len;
int ret = SWIG_OK;
#if PY_VERSION_HEX>=0x03000000
PyBytes_AsStringAndSize(obj, &cstr, &len);
if(alloc) *alloc = SWIG_NEWOBJ;
#if !defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
if (!alloc && cptr) {
/* We can't allow converting without allocation, since the internal
representation of string in Python 3 is UCS-2/UCS-4 but we require
a UTF-8 representation.
TODO(bhy) More detailed explanation */
return SWIG_RuntimeError;
}
obj = PyUnicode_AsUTF8String(obj);
if (!obj)
return SWIG_TypeError;
if (alloc)
*alloc = SWIG_NEWOBJ;
#endif
if (PyBytes_AsStringAndSize(obj, &cstr, &len) == -1)
return SWIG_TypeError;
#else
PyString_AsStringAndSize(obj, &cstr, &len);
if (PyString_AsStringAndSize(obj, &cstr, &len) == -1)
return SWIG_TypeError;
#endif
if (cptr) {
if (alloc) {
/*
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 = (char *)memcpy(malloc((len + 1)*sizeof(char)), cstr, sizeof(char)*(len + 1));
*alloc = SWIG_NEWOBJ;
}
else {
if (*alloc == SWIG_NEWOBJ) {
*cptr = (char *)memcpy(malloc((len + 1)*sizeof(char)), cstr, sizeof(char)*(len + 1));
*alloc = SWIG_NEWOBJ;
} else {
*cptr = cstr;
*alloc = SWIG_OLDOBJ;
}
} else {
#if PY_VERSION_HEX>=0x03000000
#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
*cptr = PyBytes_AsString(obj);
#else
assert(0); /* Should never reach here with Unicode strings in Python 3 */
#endif
#else
*cptr = SWIG_Python_str_AsChar(obj);
if (!*cptr)
ret = SWIG_TypeError;
#endif
}
}
if (psize) *psize = len + 1;
return SWIG_OK;
#if PY_VERSION_HEX>=0x03000000 && !defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
Py_XDECREF(obj);
#endif
return ret;
} else {
#if defined(SWIG_PYTHON_2_UNICODE)
#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
#error "Cannot use both SWIG_PYTHON_2_UNICODE and SWIG_PYTHON_STRICT_BYTE_CHAR at once"
#endif
#if PY_VERSION_HEX<0x03000000
if (PyUnicode_Check(obj)) {
char *cstr; Py_ssize_t len;
if (!alloc && cptr) {
return SWIG_RuntimeError;
}
obj = PyUnicode_AsUTF8String(obj);
if (!obj)
return SWIG_TypeError;
if (PyString_AsStringAndSize(obj, &cstr, &len) != -1) {
if (cptr) {
if (alloc) *alloc = SWIG_NEWOBJ;
*cptr = (char *)memcpy(malloc((len + 1)*sizeof(char)), cstr, sizeof(char)*(len + 1));
}
if (psize) *psize = len + 1;

Py_XDECREF(obj);
return SWIG_OK;
} else {
Py_XDECREF(obj);
}
}
#endif
#endif

swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
if (pchar_descriptor) {
void* vptr = 0;
Expand Down Expand Up @@ -3311,13 +3361,17 @@ SWIG_FromCharPtrAndSize(const char* carray, size_t size)
if (carray) {
if (size > INT_MAX) {
swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
return pchar_descriptor ?
return pchar_descriptor ?
SWIG_InternalNewPointerObj((char *)(carray), pchar_descriptor, 0) : SWIG_Py_Void();
} else {
#if PY_VERSION_HEX >= 0x03000000
return PyBytes_FromStringAndSize(carray, (int)(size));
#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
return PyBytes_FromStringAndSize(carray, (Py_ssize_t)(size));
#else
return PyUnicode_DecodeUTF8(carray, (Py_ssize_t)(size), "surrogateescape");
#endif
#else
return PyString_FromStringAndSize(carray, (int)(size));
return PyString_FromStringAndSize(carray, (Py_ssize_t)(size));
#endif
}
} else {
Expand Down
86 changes: 0 additions & 86 deletions pystrings.swg

This file was deleted.

0 comments on commit ce4e9cc

Please sign in to comment.