This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Py_BuildValue k format units don\'t work with big values
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, georg.brandl, loewis, moese
Priority: normal Keywords:

Created on 2005-09-03 22:12 by moese, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
buildvalue.diff georg.brandl, 2005-09-18 10:07
Messages (7)
msg26200 - (view) Author: Moese (moese) Date: 2005-09-03 22:12
Python 2.4 on Windows XP SP2

Consider this code:

unsigned long x = 0xaabbccdd;
PyObject* v = Py_BuildValue("k", x);
unsigned long y = PyLong_AsUnsignedLong(v);

y will be equal with -1 because PyLong_AsUnsignedLong
will raise an OverflowError since Py_BuildValue doesn't
create a long for the "k" format unit, but an int which
will be interpreted as a negative number.

The K format seems to have the same error,
PyLong_FromLongLong is used instead of
PyLong_FromUnsignedLongLong.

The do_mkvalue function from mod_support.c must be
fixed to use PyLong_FromUnsignedLong instead of
PyInt_FromLong for "k".

Also, the BHLkK  format units for Py_BuildValue should
be documented. In my Python 2.4 manual they do not appear.
msg26201 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-09-14 20:02
Logged In: YES 
user_id=1188172

I think you're right. Do you too, Martin?
msg26202 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-09-18 08:59
Logged In: YES 
user_id=21627

I'm not sure what it should do: the other option would be to
create an int if it fits, else a long. For 2.4.x atleast,
this would give better backwards compatibility given the
status quo.

I certainly agree that the documentation should be updated.
Patches are welcome.
msg26203 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-09-18 10:07
Logged In: YES 
user_id=1188172

Attaching patch (including doc changes). For I and k, it
creates an int if it fits, else a long.
msg26204 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-11-11 08:45
Logged In: YES 
user_id=1188172

Ping!
msg26205 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-11-13 10:31
Logged In: YES 
user_id=21627

The patch looks wrong: for 'I' (capital i), you va_arg
unsigned long; I think 'I' should do unsigned int instead.

A minor nit: why does it move the 'l' case (lower L)?

Apart from that, the patch looks fine.
msg26206 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-11-24 15:39
Logged In: YES 
user_id=1188172

Corrected patch committed in rev. 41527 and 41528 (2.4).
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42338
2005-09-03 22:12:33moesecreate