472,973 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,973 software developers and data experts.

Is this a bug in int()?

>>>int('0x', 16)
0

I'm working on a tokenizer and I'm thinking about returning a
MALFORMED_NUMBER token (1.2E, .5E+)
Dec 20 '07 #1
7 1087
Ma************@gmail.com wrote under the subject line "Is this a
bug in int()?":
>>>>int('0x', 16)
0
I think it is a general problem in the tokenizer, not just the 'int'
constructor. The syntax for integers says:

hexinteger ::= "0" ("x" | "X") hexdigit+

but 0x appears to be accepted in source code as an integer.

If I were you, I'd try reporting it as a bug.
I'm working on a tokenizer and I'm thinking about returning a
MALFORMED_NUMBER token (1.2E, .5E+)
Why would you return a token rather than throwing an exception?
Dec 20 '07 #2
Ma************@gmail.com wrote:
>>>int('0x', 16)
0

I'm working on a tokenizer and I'm thinking about returning a
MALFORMED_NUMBER token (1.2E, .5E+)
Somewhat surprisingly, "0x" is a valid integer literal in Python:
>>0x
0

</F>

Dec 20 '07 #3

"Duncan Booth" <du**********@invalid.invalidwrote in message
news:Xn*************************@127.0.0.1...
| Ma************@gmail.com wrote under the subject line "Is this a
| bug in int()?":
| >>>>int('0x', 16)
| 0
| >
| I think it is a general problem in the tokenizer, not just the 'int'
| constructor. The syntax for integers says:
|
| hexinteger ::= "0" ("x" | "X") hexdigit+
|
| but 0x appears to be accepted in source code as an integer.
|
| If I were you, I'd try reporting it as a bug.

The mismatch between doc and behavior certainly is a bug.
One should change.

Dec 21 '07 #4


Duncan Booth wrote:
Why would you return a token rather than throwing an exception?
Tokenizers have lots of uses. Colorizing text in an editor, for
example. We've got a MALFORMED_NUMBER when you type '0x'. We've got an
INTEGER when we get your next keystroke (probably).
Dec 21 '07 #5
Tokenizer bug reported.

MartinRineh...@gmail.com wrote:
>>int('0x', 16)
0

I'm working on a tokenizer and I'm thinking about returning a
MALFORMED_NUMBER token (1.2E, .5E+)
Dec 21 '07 #6
Tokenizer accepts "0x" as zero. Spec says its an error not to have at
least one hex digit after "0x".

This is a more serious bug than I had originally thought. Consider
this:

Joe types "security_code = 0x" and then goes off to the Guardian-of-
the-Codes to get the appropriate hex string. Returning to computer,
Joe's boss grabs him. Tells him that effective immediately he's on the
"rescue us from this crisis" team; his other project can wait.

Some hours, days or weeks later Joe returns to the first project. At
this point Joe has a line of code that says "security_code = 0x". I
think Joe would be well-served by a compiler error on that line. As is
now, Joe's program assigns 0 to security_code and compiles without
complaint. I'm pretty sure any line of the form "name = 0x" was a
product of some form of programmer interruptus.

Dec 22 '07 #7
On Dec 22, 5:03 pm, MartinRineh...@gmail.com wrote:
Tokenizer accepts "0x" as zero. Spec says its an error not to have at
least one hex digit after "0x".

This is a more serious bug than I had originally thought. Consider
this:

Joe types "security_code = 0x" and then goes off to the Guardian-of-
the-Codes to get the appropriate hex string. Returning to computer,
Joe's boss grabs him. Tells him that effective immediately he's on the
"rescue us from this crisis" team; his other project can wait.

Some hours, days or weeks later Joe returns to the first project. At
this point Joe has a line of code that says "security_code = 0x". I
think Joe would be well-served by a compiler error on that line. As is
now, Joe's program assigns 0 to security_code and compiles without
complaint. I'm pretty sure any line of the form "name = 0x" was a
product of some form of programmer interruptus.
:-) Are you a fiction writer by any chance ? Nice story but I somehow
doubt that the number of lines of the form "name = 0x" ever written in
Python is greater than a single digit (with zero the most likely one).

George
Dec 23 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
1
by: akickdoe22 | last post by:
Please help me finish this program. i have completed the addition and the subtraction parts, but i am stuck on the multiplication and division. any suggestions, hints, code, anyhting. it's not a...
5
by: Kemal Ozan | last post by:
Hi, I am studying K&R book. On the multidimensional Arrays chapter they say "int (*daytab) is a pointer to an array of 13 integers. The parenthesis are necessary since brackets have higher...
7
by: Andrej Prsa | last post by:
Hello, everyone! When a const int * argument is passed to a function, i.e. int f (const int *var) { printf ("%d\n", *var); } int main ()
7
by: Jeff K | last post by:
Can you pass an int array by reference to a function and modify selective elements? Here is my code: #include <stdio.h> #define COLUMNSIZE 30 #define ASIZE 5...
1
by: Felix | last post by:
I am a bit confused as to why conversion from float/double to int is handled in c# in the way that it is. It differs from my implementation of C and C++ in a suprising way (to me). Here is a...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
14
by: yang__lee | last post by:
Hi, You all know typedef typedef struct g { int a; int b; } google;
16
by: Julia | last post by:
Hi, there, In C programming, for pointer, I saw two programming styles: one is connecting '*' with variable, like, 'int *i'; the other is connecting '*' with data type, like, 'int* i' I...
8
by: beagle197 | last post by:
Folks, Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...} allocated using malloc/calloc, but the arguments I'm passing to qsort are producing the incorrect results (see...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.