Opened 13 years ago

Last modified 5 years ago

#3353 new Bugs

warning C4244: Py_ssize_t to unsigned int

Reported by: davidm@… Owned by: Dave Abrahams
Milestone: Boost 1.40.0 Component: python USE GITHUB
Version: Boost 1.39.0 Severity: Problem
Keywords: Cc:

Description

I am seeing a compiler warning regarding an unsafe type conversion:

1>c:\program files\boost\boost_1_39\boost\python\detail\caller.hpp(55) : warning C4244: 'return' : conversion from 'Py_ssize_t' to 'unsigned int', possible loss of data

Visual Studio 2005

Note that this may be a known issue or regression, as it was discussed 2 years ago here: http://lists.boost.org/Archives/boost/2007/04/120377.php

Change History (8)

comment:1 by anonymous, 10 years ago

Hi,

this is also an issue with visual studio 2010 x64. Is it ok to do....?

diff -r 6c6ba1352fa2 boost/boost/python/detail/caller.hpp --- a/boost/boost/python/detail/caller.hpp Tue Oct 23 11:44:57 2012

+0200

+++ b/boost/boost/python/detail/caller.hpp Tue Oct 23 14:07:25 2012

+0200

@@ -50,7 +50,7 @@

return PyTuple_GET_ITEM(args_,N);

}

-inline unsigned arity(PyObject* const& args_) +inline Py_ssize_t arity(PyObject* const& args_)

{

return PyTuple_GET_SIZE(args_);

}

Chris

comment:2 by dominik.berner@…, 8 years ago

It seems that it wasn't fixed until now (1.57.0) so I created a small patch to fix the compiler warnings generated by the Visual Studio 2013 compiler:

Index: Dependencies64/include/boost/python/list.hpp
===================================================================
--- Dependencies64/include/boost/python/list.hpp	(revision 29348)
+++ Dependencies64/include/boost/python/list.hpp	(revision 29350)
@@ -73,7 +73,7 @@
     }
 
     template <class T>
-    long count(T const& value) const
+    ssize_t count(T const& value) const
     {
         return base::count(object(value));
     }
Index: Dependencies64/include/boost/python/detail/caller.hpp
===================================================================
--- Dependencies64/include/boost/python/detail/caller.hpp	(revision 29348)
+++ Dependencies64/include/boost/python/detail/caller.hpp	(revision 29350)
@@ -50,7 +50,7 @@
     return PyTuple_GET_ITEM(args_,N);
 }
 
-inline unsigned arity(PyObject* const& args_)
+inline Py_ssize_t arity(PyObject* const& args_)
 {
     return PyTuple_GET_SIZE(args_);
 }
Index: Dependencies64/include/indexing_suite/slice_handler.hpp
===================================================================
--- Dependencies64/include/indexing_suite/slice_handler.hpp	(revision 29348)
+++ Dependencies64/include/indexing_suite/slice_handler.hpp	(revision 29350)
@@ -286,7 +286,7 @@
 
     boost::python::object length
       ((boost::python::handle<>
-        (PyInt_FromLong (Algorithms::size (c)))));
+        (PyInt_FromSsize_t (Algorithms::size (c)))));
 
     slice sl
       ((boost::python::handle<>

comment:3 by teeks99@…, 7 years ago

I like the solution of actually using the ssize_t instead of casting (down on 64 bit?) to a long/unsigned, however, I don't think that is defined on windows. Is this the same as ptrdiff_t? Does boost have a solution for this somewhere?

comment:3 by teeks99@…, 7 years ago

I like the solution of actually using the ssize_t instead of casting (down on 64 bit?) to a long/unsigned, however, I don't think that is defined on windows. Is this the same as ptrdiff_t? Does boost have a solution for this somewhere?

comment:4 by dominik.berner@…, 7 years ago

That's right ssize_t is not defined for MSVC by default, however Python 2.7.x does define it in pyconfig.h which is used by boost python. the define is around ln 198 in pyconfig.h (2.7.4)

/// pyconfig.h
#ifdef MS_WIN64
typedef __int64 ssize_t;
#else
typedef _W64 int ssize_t;
#endif
#define HAVE_SSIZE_T 1

If it's not there boost python does typedef int as ssize_t in boost/python/ssize_t.h

comment:5 by teeks99@…, 7 years ago

Ahh, cool, then I definitely like this solution better than #4596.

I've created a pull request for your patch as #21 on the github python tracker.

I'm not sure where the slice_handler.hpp file is, I didn't see it anywhere in the boost master project, so I didn't include that.

comment:6 by anonymous, 6 years ago

The issue still exists in boost 1.63 using visual studio 2015 compiling for x64

comment:7 by anonymous, 5 years ago

Issue still seems to exist on Boost 1.64 with VS 2017 (compiling x64).

Note: See TracTickets for help on using tickets.