8000 Check bookland code in ISBN · waldemar-becker/python-stdnum@a71a1ac · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 4, 2019. It is now read-only.

Commit a71a1ac

Browse files
committed
Check bookland code in ISBN
This ensures that an InvalidComponent() exception is raised when an unknown EAN bookland code is found. It will also raise this exception when using to_isbn10() when not using the 978 code.
1 parent 8f6fa7d commit a71a1ac

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

stdnum/isbn.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def validate(number, convert=False):
100100
raise InvalidChecksum()
101101
elif len(number) == 13:
102102
ean.validate(number)
103+
if number[:3] not in ('978', '979'):
104+
raise InvalidComponent()
103105
else:
104106
raise InvalidLength()
105107
if convert:
@@ -159,7 +161,7 @@ def to_isbn10(number):
159161
elif isbn_type(min_number) != 'ISBN13':
160162
raise InvalidFormat('Not a valid ISBN13.')
161163
elif not number.startswith('978'):
162-
raise InvalidFormat('Does not use 978 Bookland prefix.')
164+
raise InvalidComponent('Does not use 978 Bookland prefix.')
163165
# strip EAN prefix
164166
number = number[3:-1].strip().strip('-')
165167
digit = _calc_isbn10_check_digit(min_number[3:-1])

tests/test_isbn.doctest

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ really useful as module documentation.
2525
>>> from stdnum import isbn
2626

2727

28-
Tests for mangling and incorect check digits.
28+
Tests for mangling and incorrect check digits.
2929

3030
>>> isbn.validate('08515x-629-2') # added X in the middle
3131
Traceback (most recent call last):
@@ -43,6 +43,10 @@ InvalidFormat: ...
4343
Traceback (most recent call last):
4444
...
4545
InvalidLength: ...
46+
>>> isbn.validate('7501031311309') # valid EAN, unknown bookland code
47+
Traceback (most recent call last):
48+
...
49+
InvalidComponent: ...
4650

4751

4852
See if ISBN10 to 13 conversion works.
@@ -88,7 +92,7 @@ InvalidFormat: ...
8892
>>> isbn.to_isbn10('9791843123391')
8993
Traceback (most recent call last):
9094
...
91-
InvalidFormat: ...
95+
InvalidComponent: ...
9296

9397

9498
Regrouping tests.
@@ -99,7 +103,7 @@ Regrouping tests.
99103
('', '99996', '', '7827', '0')
100104
>>> isbn.split('979-20-1234567-8')
101105
('979', '', '', '201234567', '8')
102-
>>> isbn.split('5413170121522') # valid checkdigit, unknown prefix
106+
>>> isbn.split('5413170121522') # valid check digit, unknown prefix
103107
('', '', '', '541317012152', '2')
104108

105109

0 commit comments

Comments
 (0)
0