classification
Title: In re's examples the example with scanf() contains wrong analog for %x, %X specifiers
Type: enhancement Stage: resolved
Components: Documentation, Regular Expressions Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: docs@python, ezio.melotti, mrabarnett, py.user, python-dev, r.david.murray, terry.reedy
Priority: normal Keywords: easy, patch

Created on 2012-04-07 00:19 by py.user, last changed 2012-04-29 08:50 by ezio.melotti. This issue is now closed.

Messages (8)
msg157711 - (view) Author: py.user (py.user) * Date: 2012-04-07 00:19
http://docs.python.org/py3k/library/re.html#simulating-scanf

0[xX][\dA-Fa-f]+   ->   (0[xX])?[\dA-Fa-f]+
msg157716 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-04-07 02:53
The documentation appears to be correct to me.  Can you demonstrate your suggestion with some examples?
msg157717 - (view) Author: py.user (py.user) * Date: 2012-04-07 03:17
the prefix "0x" is not necessary for the %x specifier in C
if the pattern will see "ABC", it will not match with it, but it should match
msg157718 - (view) Author: py.user (py.user) * Date: 2012-04-07 03:19
#include <stdio.h>

int main(void)
{
    unsigned n;
    
    scanf("%x", &n);
    printf("%u\n", n);
    return 0;
}


[guest@localhost c]$ .ansi t.c -o t
[guest@localhost c]$ ./t
0xa
10
[guest@localhost c]$ ./t
a
10
[guest@localhost c]$
[guest@localhost c]$ alias .ansi
alias .ansi='gcc -ansi -pedantic -Wall'
[guest@localhost c]$
msg158212 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-04-13 17:02
I checked Standard C by Plauger & Brodie and as I read it, it agrees with py.user and his C compiler. For stdlib strtol() and strtoul(), the 0x/0X prefixes are accepted but optional for explicit base 16. If base is given as 0, they are accepted and set the base to 16 (which is otherwise 10). Except for %i, Xscanf functions apparently call either of the above with an explicit base, which is 16 for the %x specifiers.
msg158310 - (view) Author: py.user (py.user) * Date: 2012-04-15 03:58
the same problem in the %o analog

valid strings for the %x specifier of scanf():
"+0xabc"
"-0xabc"
"+abc"
"-abc"

valid strings for the %o specifier of scanf():
"+0123"
"-0123"
"+123"
"-123"

how to patch
the %x specifier:
0[xX][\dA-Fa-f]+   ->   [-+]?(0[xX])?[\dA-Fa-f]+
the %o specifier:
0[0-7]*            ->   [-+]?[0-7]+
msg159588 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-04-29 08:49
New changeset b26471a2a115 by Ezio Melotti in branch '2.7':
#14519: fix the regex used in the scanf example.
http://hg.python.org/cpython/rev/b26471a2a115

New changeset e317d651ccf8 by Ezio Melotti in branch '3.2':
#14519: fix the regex used in the scanf example.
http://hg.python.org/cpython/rev/e317d651ccf8

New changeset 7cc1cddb378d by Ezio Melotti in branch 'default':
#14519: merge with 3.2.
http://hg.python.org/cpython/rev/7cc1cddb378d
msg159589 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-04-29 08:50
Fixed, thanks for the report and the suggestions!
History
Date User Action Args
2012-04-29 08:50:58ezio.melottisetstatus: open -> closed
type: enhancement
messages: + msg159589

assignee: docs@python -> ezio.melotti
resolution: fixed
stage: needs patch -> resolved
2012-04-29 08:49:03python-devsetnosy: + python-dev
messages: + msg159588
2012-04-15 03:58:31py.usersetmessages: + msg158310
2012-04-13 17:02:37terry.reedysetversions: + Python 2.7, Python 3.3
nosy: + terry.reedy

messages: + msg158212

keywords: + patch, easy
stage: needs patch
2012-04-07 03:19:40py.usersetmessages: + msg157718
2012-04-07 03:17:38py.usersetmessages: + msg157717
2012-04-07 02:53:29r.david.murraysetnosy: + r.david.murray
messages: + msg157716
2012-04-07 00:19:55py.usercreate