borni-dhifi / vatnumber

Automatically exported from code.google.com/p/vatnumber
GNU General Public License v3.0
0 stars 0 forks source link

VAT number is incorrectly evaluated as a negative integer in the try: int(vat) #20

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

The VAT number is correctly evaluated as a negative integer in the try: 
int(vat) which is used for a lot of countries.

However, there should have been a False return as later on the following error 
occurs:
"ValueError: invalid literal for int() with base 10: '-'"

I think this can be solved with a check on a negative integer. And already 
doing so a check on an integer equal to 0 is also useful.

What steps will reproduce the problem?
See below for a code example.

What is the expected output? What do you see instead?
A False return from function check_vat with VAT number RO-7793957 and not a 
hard error.

What version of the product are you using? On what operating system?
1.0

Please provide any additional information below.
'''
Start of example code snippet
'''
def check_vat_ro(vat):
    '''
    Check Romania VAT number.
    '''
    try:
        int(vat)
    except ValueError:
        return False
    # New code to evaluate a negative integer(and 0)
    if int(vat) <= 0:
        return False
    # End successfully when int(vat) > 0
    else: return True

def check_vat(vat):
    '''
    Check VAT number.
    '''
    code = vat[:2].lower()
    print(code)
    number = vat[2:]
    print(number)
    try:
        checker = globals()['check_vat_%s' % code]
    except KeyError:
        return False
    return checker(number)

# Negative integer example
vat_code_in = 'RO-7793957'
# Incorrect string example
# vat_code_in = 'RO/7793957'
print(check_vat(vat_code_in))

'''
End of example code snippet
'''

Original issue reported on code.google.com by Jannesv...@gmail.com on 17 Oct 2011 at 9:39

GoogleCodeExporter commented 8 years ago
Or what I realized later on, the expected input is something like 'RO7793957' 
and the input should be sanitized/rejected before calling the function 
vatnumber.

Am I correct in this assumption?

Sanitizing is not so difficult. Something like upper() and regex ['rA-Z0-9] and 
compare to original input would solve this too.

Original comment by Jannesv...@gmail.com on 18 Oct 2011 at 6:27

GoogleCodeExporter commented 8 years ago
Fixed in r98719bed743b

Original comment by cedric.krier@b2ck.com on 8 Nov 2012 at 11:35