
Hi, which formatting code should be used for a Py_ssize_t value in e.g. PyString_FromFormat? '%zd' won't work since my value can be negative, and the 'z' modifier converts the argument to size_t. Georg

Georg Brandl wrote:
which formatting code should be used for a Py_ssize_t value in e.g. PyString_FromFormat?
%zd
'%zd' won't work since my value can be negative, and the 'z' modifier converts the argument to size_t.
That's a bug. It should print it signed. If unsigned printing of size_t is desired, %zu should be used. Regards, Martin

Georg Brandl wrote:
Right. At least, not with changing structmember.[ch].
Did you mean "without"?
Oops, right.
Can I submit a patch?
I personally don't mind having more types added to structmember, so I'm +0 on adding Py_ssize_t to the list of types supported. I wonder what the specific application is that you have in mind, though. Regards, Martin

On 5/14/06, "Martin v. Löwis" <martin@v.loewis.de> wrote:
I ended up needing T_SSIZE_T or T_SIZE_T (I forget which) in my first attempt to fully Py_ssize-t-ify ctypes (but I was learning a lot about ctypes at the same time, and I ended up breaking a lot of stuff.) I'm now not sure whether it's really necessary for ctypes, but it was trivial to add, not to mention symmetric and completely logical. However, I also have a C extension of my own that exposes something quite like 'length' as an attribute, and it really ought to be a Py_ssize_t struct member (instead of the long it is now.) -- Thomas Wouters <thomas@python.org> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

On 5/13/06, "Martin v. Löwis" <martin@v.loewis.de> wrote:
Looking in stringobject.c, I don't see how %zu (or %lu) can be used with String_FromFormatV. Here's the code around line 260 (note ssize_t in the comment really means Py_ssize_t): /* handle the long flag, but only for %ld. others can be added when necessary. */ if (*f == 'l' && *(f+1) == 'd') { longflag = 1; ++f; } /* handle the ssize_t flag. */ if (*f == 'z' && *(f+1) == 'd') { size_tflag = 1; ++f; } n

Neal Norwitz wrote:
Right. It currently cannot be used. So if it is desired, it needs to be added first, and then should be used. Looking at the checkin messages, I understand that the change from "d" to "u" was to make some compiler stop warning - that is the wrong motivation for such a behavioral change. Regards, Martin

[Neal]
Looking in stringobject.c, I don't see how %zu (or %lu) can be used with String_FromFormatV.
[Martin]
Right. It currently cannot be used. So if it is desired, it needs to be added first, and then should be used.
I added it: %u, %lu, and %zu can be used now in PyString_FromFormat, PyErr_Format, and PyString_FromFormatV. Since PyString_FromFormat and PyErr_Format have exactly the same rules (both inherited from PyString_FromFormatV), but their docs were way out of synch, it would be good if someone with more LaTeX Fu changed one of them to just point to the other. I simply did a massive copy+paste job to get them in synch again.

Georg Brandl wrote:
which formatting code should be used for a Py_ssize_t value in e.g. PyString_FromFormat?
%zd
'%zd' won't work since my value can be negative, and the 'z' modifier converts the argument to size_t.
That's a bug. It should print it signed. If unsigned printing of size_t is desired, %zu should be used. Regards, Martin

Georg Brandl wrote:
Right. At least, not with changing structmember.[ch].
Did you mean "without"?
Oops, right.
Can I submit a patch?
I personally don't mind having more types added to structmember, so I'm +0 on adding Py_ssize_t to the list of types supported. I wonder what the specific application is that you have in mind, though. Regards, Martin

On 5/14/06, "Martin v. Löwis" <martin@v.loewis.de> wrote:
I ended up needing T_SSIZE_T or T_SIZE_T (I forget which) in my first attempt to fully Py_ssize-t-ify ctypes (but I was learning a lot about ctypes at the same time, and I ended up breaking a lot of stuff.) I'm now not sure whether it's really necessary for ctypes, but it was trivial to add, not to mention symmetric and completely logical. However, I also have a C extension of my own that exposes something quite like 'length' as an attribute, and it really ought to be a Py_ssize_t struct member (instead of the long it is now.) -- Thomas Wouters <thomas@python.org> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

On 5/13/06, "Martin v. Löwis" <martin@v.loewis.de> wrote:
Looking in stringobject.c, I don't see how %zu (or %lu) can be used with String_FromFormatV. Here's the code around line 260 (note ssize_t in the comment really means Py_ssize_t): /* handle the long flag, but only for %ld. others can be added when necessary. */ if (*f == 'l' && *(f+1) == 'd') { longflag = 1; ++f; } /* handle the ssize_t flag. */ if (*f == 'z' && *(f+1) == 'd') { size_tflag = 1; ++f; } n

Neal Norwitz wrote:
Right. It currently cannot be used. So if it is desired, it needs to be added first, and then should be used. Looking at the checkin messages, I understand that the change from "d" to "u" was to make some compiler stop warning - that is the wrong motivation for such a behavioral change. Regards, Martin

[Neal]
Looking in stringobject.c, I don't see how %zu (or %lu) can be used with String_FromFormatV.
[Martin]
Right. It currently cannot be used. So if it is desired, it needs to be added first, and then should be used.
I added it: %u, %lu, and %zu can be used now in PyString_FromFormat, PyErr_Format, and PyString_FromFormatV. Since PyString_FromFormat and PyErr_Format have exactly the same rules (both inherited from PyString_FromFormatV), but their docs were way out of synch, it would be good if someone with more LaTeX Fu changed one of them to just point to the other. I simply did a massive copy+paste job to get them in synch again.
participants (5)
-
"Martin v. Löwis"
-
Georg Brandl
-
Neal Norwitz
-
Thomas Wouters
-
Tim Peters