Skip to content

Commit

Permalink
[Python] Use PyUnicode rather than PyBytes for strings
Browse files Browse the repository at this point in the history
Fixes: #2011.
  • Loading branch information
amadio committed Jun 6, 2023
1 parent 7163e7b commit 78ae5ad
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 48 deletions.
2 changes: 1 addition & 1 deletion bindings/python/src/ChunkIterator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace PyXRootD

else {
self->currentOffset += self->chunksize;
pychunk = PyBytes_FromStringAndSize( (const char*) chunk->GetBuffer(),
pychunk = PyUnicode_FromStringAndSize( (const char*) chunk->GetBuffer(),
chunk->GetSize() );
}

Expand Down
6 changes: 3 additions & 3 deletions bindings/python/src/Conversions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ namespace PyXRootD
{
static PyObject* Convert( XrdCl::Buffer *buffer )
{
return PyBytes_FromStringAndSize( buffer->GetBuffer(), buffer->GetSize() );
return PyUnicode_FromStringAndSize( buffer->GetBuffer(), buffer->GetSize() );
}
};

template<> struct PyDict<XrdCl::ChunkInfo>
{
static PyObject* Convert( XrdCl::ChunkInfo *chunk )
{
PyObject *o = PyBytes_FromStringAndSize( (const char*)chunk->buffer,
PyObject *o = PyUnicode_FromStringAndSize( (const char*)chunk->buffer,
chunk->length );
delete[] (char*) chunk->buffer;
return o;
Expand All @@ -268,7 +268,7 @@ namespace PyXRootD
for ( uint32_t i = 0; i < chunks.size(); ++i ) {
XrdCl::ChunkInfo chunk = chunks.at( i );

PyObject *buffer = PyBytes_FromStringAndSize( (const char *) chunk.buffer,
PyObject *buffer = PyUnicode_FromStringAndSize( (const char *) chunk.buffer,
chunk.length );
delete[] (char*) chunk.buffer;

Expand Down
19 changes: 7 additions & 12 deletions bindings/python/src/PyXRootD.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,18 @@

#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#define Py_TPFLAGS_HAVE_ITER 0
#else
#define PyUnicode_AsUTF8 PyString_AsString
#define PyUnicode_Check PyString_Check
#define PyUnicode_FromString PyString_FromString
#define PyUnicode_FromStringAndSize PyString_FromStringAndSize
#define PyUnicode_GET_LENGTH PyString_Size
#endif

#define async( func ) \
Py_BEGIN_ALLOW_THREADS \
func; \
Py_END_ALLOW_THREADS \

#ifdef IS_PY3K
#define Py_TPFLAGS_HAVE_ITER 0
#else
#if PY_MINOR_VERSION <= 5
#define PyUnicode_FromString PyString_FromString
#endif
#define PyBytes_Size PyString_Size
#define PyBytes_Check PyString_Check
#define PyBytes_FromString PyString_FromString
#define PyBytes_FromStringAndSize PyString_FromStringAndSize
#endif

#endif /* PYXROOTD_HH_ */
24 changes: 12 additions & 12 deletions bindings/python/src/PyXRootDFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ namespace PyXRootD
else {
uint32_t bytesRead = 0;
async( status = self->file->Read( offset, size, buffer, bytesRead, timeout ) );
pyresponse = PyBytes_FromStringAndSize( buffer, bytesRead );
pyresponse = PyUnicode_FromStringAndSize( buffer, bytesRead );
delete[] buffer;
}

Expand Down Expand Up @@ -295,10 +295,10 @@ namespace PyXRootD
if ( off_init == 0 )
self->currentOffset += line->GetSize();

pyline = PyBytes_FromStringAndSize( line->GetBuffer(), line->GetSize() );
pyline = PyUnicode_FromStringAndSize( line->GetBuffer(), line->GetSize() );
}
else
pyline = PyBytes_FromString( "" );
pyline = PyUnicode_FromString( "" );

delete line;
delete chunk;
Expand Down Expand Up @@ -346,7 +346,7 @@ namespace PyXRootD
{
line = self->ReadLine( self, args, kwds );

if ( !line || PyBytes_Size( line ) == 0 )
if ( !line || PyUnicode_GET_LENGTH( line ) == 0 )
break;

PyList_Append( lines, line );
Expand Down Expand Up @@ -800,14 +800,14 @@ namespace PyXRootD
return NULL;
// extract the attribute name from the tuple
PyObject *py_name = PyTuple_GetItem( item, 0 );
if( !PyBytes_Check( py_name ) )
if( !PyUnicode_Check( py_name ) )
return NULL;
std::string name = PyBytes_AsString( py_name );
std::string name = PyUnicode_AsUTF8( py_name );
// extract the attribute value from the tuple
PyObject *py_value = PyTuple_GetItem( item, 1 );
if( !PyBytes_Check( py_value ) )
if( !PyUnicode_Check( py_value ) )
return NULL;
std::string value = PyBytes_AsString( py_value );
std::string value = PyUnicode_AsUTF8( py_value );
// update the C++ list of xattrs
attrs.push_back( XrdCl::xattr_t( name, value ) );
}
Expand Down Expand Up @@ -864,9 +864,9 @@ namespace PyXRootD
// get the item at respective index
PyObject *item = PyList_GetItem( pyattrs, i );
// make sure the item is a string
if( !item || !PyBytes_Check( item ) )
if( !item || !PyUnicode_Check( item ) )
return NULL;
std::string name = PyBytes_AsString( item );
std::string name = PyUnicode_AsUTF8( item );
// update the C++ list of xattrs
attrs.push_back( name );
}
Expand Down Expand Up @@ -923,9 +923,9 @@ namespace PyXRootD
// get the item at respective index
PyObject *item = PyList_GetItem( pyattrs, i );
// make sure the item is a string
if( !item || !PyBytes_Check( item ) )
if( !item || !PyUnicode_Check( item ) )
return NULL;
std::string name = PyBytes_AsString( item );
std::string name = PyUnicode_AsUTF8( item );
// update the C++ list of xattrs
attrs.push_back( name );
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/src/PyXRootDFile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace PyXRootD
//--------------------------------------------------------------------------
// Raise StopIteration if the line we just read is empty
//--------------------------------------------------------------------------
if ( PyBytes_Size( line ) == 0 ) {
if ( PyUnicode_GET_LENGTH( line ) == 0 ) {
PyErr_SetNone( PyExc_StopIteration );
return NULL;
}
Expand Down
18 changes: 9 additions & 9 deletions bindings/python/src/PyXRootDFileSystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ namespace PyXRootD
for ( int i = 0; i < PyList_Size( pyfiles ); ++i ) {
pyfile = PyList_GetItem( pyfiles, i );
if ( !pyfile ) return NULL;
file = PyBytes_AsString( pyfile );
file = PyUnicode_AsUTF8( pyfile );
files.push_back( std::string( file ) );
}

Expand Down Expand Up @@ -769,14 +769,14 @@ namespace PyXRootD
return NULL;
// extract the attribute name from the tuple
PyObject *py_name = PyTuple_GetItem( item, 0 );
if( !PyBytes_Check( py_name ) )
if( !PyUnicode_Check( py_name ) )
return NULL;
std::string name = PyBytes_AsString( py_name );
std::string name = PyUnicode_AsUTF8( py_name );
// extract the attribute value from the tuple
PyObject *py_value = PyTuple_GetItem( item, 1 );
if( !PyBytes_Check( py_value ) )
if( !PyUnicode_Check( py_value ) )
return NULL;
std::string value = PyBytes_AsString( py_value );
std::string value = PyUnicode_AsUTF8( py_value );
// update the C++ list of xattrs
attrs.push_back( XrdCl::xattr_t( name, value ) );
}
Expand Down Expand Up @@ -831,9 +831,9 @@ namespace PyXRootD
// get the item at respective index
PyObject *item = PyList_GetItem( pyattrs, i );
// make sure the item is a string
if( !item || !PyBytes_Check( item ) )
if( !item || !PyUnicode_Check( item ) )
return NULL;
std::string name = PyBytes_AsString( item );
std::string name = PyUnicode_AsUTF8( item );
// update the C++ list of xattrs
attrs.push_back( name );
}
Expand Down Expand Up @@ -888,9 +888,9 @@ namespace PyXRootD
// get the item at respective index
PyObject *item = PyList_GetItem( pyattrs, i );
// make sure the item is a string
if( !item || !PyBytes_Check( item ) )
if( !item || !PyUnicode_Check( item ) )
return NULL;
std::string name = PyBytes_AsString( item );
std::string name = PyUnicode_AsUTF8( item );
// update the C++ list of xattrs
attrs.push_back( name );
}
Expand Down
20 changes: 10 additions & 10 deletions bindings/python/src/PyXRootDURL.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ namespace PyXRootD
//----------------------------------------------------------------------------
int URL::SetProtocol( URL *self, PyObject *protocol, void *closure )
{
if ( !PyBytes_Check( protocol ) ) {
if ( !PyUnicode_Check( protocol ) ) {
PyErr_SetString( PyExc_TypeError, "protocol must be string" );
return -1;
}

self->url->SetProtocol( std::string ( PyBytes_AsString( protocol ) ) );
self->url->SetProtocol( std::string ( PyUnicode_AsUTF8( protocol ) ) );
return 0;
}

Expand All @@ -78,12 +78,12 @@ namespace PyXRootD
//----------------------------------------------------------------------------
int URL::SetUserName( URL *self, PyObject *username, void *closure )
{
if ( !PyBytes_Check( username ) ) {
if ( !PyUnicode_Check( username ) ) {
PyErr_SetString( PyExc_TypeError, "username must be string" );
return -1;
}

self->url->SetUserName( std::string( PyBytes_AsString( username ) ) );
self->url->SetUserName( std::string( PyUnicode_AsUTF8( username ) ) );
return 0;
}

Expand All @@ -100,12 +100,12 @@ namespace PyXRootD
//----------------------------------------------------------------------------
int URL::SetPassword( URL *self, PyObject *password, void *closure )
{
if ( !PyBytes_Check( password ) ) {
if ( !PyUnicode_Check( password ) ) {
PyErr_SetString( PyExc_TypeError, "password must be string" );
return -1;
}

self->url->SetPassword( std::string( PyBytes_AsString( password ) ) );
self->url->SetPassword( std::string( PyUnicode_AsUTF8( password ) ) );
return 0;
}

Expand All @@ -122,12 +122,12 @@ namespace PyXRootD
//----------------------------------------------------------------------------
int URL::SetHostName( URL *self, PyObject *hostname, void *closure )
{
if ( !PyBytes_Check( hostname ) ) {
if ( !PyUnicode_Check( hostname ) ) {
PyErr_SetString( PyExc_TypeError, "hostname must be string" );
return -1;
}

self->url->SetHostName( std::string( PyBytes_AsString( hostname ) ) );
self->url->SetHostName( std::string( PyUnicode_AsUTF8( hostname ) ) );
return 0;
}

Expand Down Expand Up @@ -178,12 +178,12 @@ namespace PyXRootD
//----------------------------------------------------------------------------
int URL::SetPath( URL *self, PyObject *path, void *closure )
{
if ( !PyBytes_Check( path ) ) {
if ( !PyUnicode_Check( path ) ) {
PyErr_SetString( PyExc_TypeError, "path must be string" );
return -1;
}

self->url->SetPath( std::string( PyBytes_AsString( path ) ) );
self->url->SetPath( std::string( PyUnicode_AsUTF8( path ) ) );
return 0;
}

Expand Down

0 comments on commit 78ae5ad

Please sign in to comment.