Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f5690bd

Browse files
authored
Merge pull request #41 from Hepheir/issue21
Add `code` property for `DOMException` Close #21
2 parents b12f74e + fab2d98 commit f5690bd

File tree

3 files changed

+43
-46
lines changed

3 files changed

+43
-46
lines changed
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
1-
import enum
1+
from typing import Final
2+
from ctypes import c_ushort
23

34

4-
class ExceptionCode(enum.IntEnum):
5-
"""Definition group `ExceptionCode`
6-
An integer indicating the type of error generated.
7-
"""
8-
INDEX_SIZE_ERR = 1
9-
DOMSTRING_SIZE_ERR = 2
10-
HIERARCHY_REQUEST_ERR = 3
11-
WRONG_DOCUMENT_ERR = 4
12-
INVALID_CHARACTER_ERR = 5
13-
NO_DATA_ALLOWED_ERR = 6
14-
NO_MODIFICATION_ALLOWED_ERR = 7
15-
NOT_FOUND_ERR = 8
16-
NOT_SUPPORTED_ERR = 9
17-
INUSE_ATTRIBUTE_ERR = 10
5+
class DOMException(Exception):
6+
"""Exception `DOMException`
187
8+
DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation is impossible to perform
9+
(either for logical reasons, because data is lost, or because the implementation has become unstable).
10+
In general, DOM methods return specific error values in ordinary processing situation, such as out-of-bound errors when using `NodeList`.
1911
20-
classDOMException(Exception):
21-
"""Exception DOMException
12+
Implementations may raise other exceptions under other circumstances.
13+
For example, implementations may raise an implementation-dependent exception if a `None` argument is passed.
2214
23-
DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation is impossible to perform (either for logical reasons, because data is lost, or because the implementation has become unstable). In general, DOM methods return specific error values in ordinary processing situation, such as out-of-bound errors when using `NodeList`.
24-
Implementations may raise other exceptions under other circumstances. For example, implementations may raise an implementation-dependent exception if a null argument is passed.
25-
Some languages and object systems do not support the concept of exceptions. For such systems, error conditions may be indicated using native error reporting mechanisms. For some bindings, for example, methods may return error codes similar to those listed in the corresponding method descriptions.
15+
Some languages and object systems do not support the concept of exceptions.
16+
For such systems, error conditions may be indicated using native error reporting mechanisms.
17+
For some bindings, for example, methods may return error codes similar to those listed in the corresponding method descriptions.
2618
"""
27-
INDEX_SIZE_ERR = ExceptionCode.INDEX_SIZE_ERR
28-
DOMSTRING_SIZE_ERR = ExceptionCode.DOMSTRING_SIZE_ERR
29-
HIERARCHY_REQUEST_ERR = ExceptionCode.HIERARCHY_REQUEST_ERR
30-
WRONG_DOCUMENT_ERR = ExceptionCode.WRONG_DOCUMENT_ERR
31-
INVALID_CHARACTER_ERR = ExceptionCode.INVALID_CHARACTER_ERR
32-
NO_DATA_ALLOWED_ERR = ExceptionCode.NO_DATA_ALLOWED_ERR
33-
NO_MODIFICATION_ALLOWED_ERR = ExceptionCode.NO_MODIFICATION_ALLOWED_ERR
34-
NOT_FOUND_ERR = ExceptionCode.NOT_FOUND_ERR
35-
NOT_SUPPORTED_ERR = ExceptionCode.NOT_SUPPORTED_ERR
36-
INUSE_ATTRIBUTE_ERR = ExceptionCode.INUSE_ATTRIBUTE_ERR
19+
20+
# Definition group `ExceptionCode`
21+
# An integer indicating the type of error generated.
22+
INDEX_SIZE_ERR: Final[c_ushort] = 1
23+
DOMSTRING_SIZE_ERR: Final[c_ushort] = 2
24+
HIERARCHY_REQUEST_ERR: Final[c_ushort] = 3
25+
WRONG_DOCUMENT_ERR: Final[c_ushort] = 4
26+
INVALID_CHARACTER_ERR: Final[c_ushort] = 5
27+
NO_DATA_ALLOWED_ERR: Final[c_ushort] = 6
28+
NO_MODIFICATION_ALLOWED_ERR: Final[c_ushort] = 7
29+
NOT_FOUND_ERR: Final[c_ushort] = 8
30+
NOT_SUPPORTED_ERR: Final[c_ushort] = 9
31+
INUSE_ATTRIBUTE_ERR: Final[c_ushort] = 10
32+
33+
def __init__(self, error_code: c_ushort, *args: object) -> None:
34+
super().__init__(*args)
35+
self.code: c_ushort = error_code

‎w3/python/core/fundamental_interface/DOMException_test.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
from w3.dom import DOMException
44

55

6-
class Test_DOMException(unittest.TestCase):
6+
class TestProperty_Code(unittest.TestCase):
77
def test_raise_INDEX_SIZE_ERR(self):
8-
try:
8+
withself.assertRaises(DOMException) ascontext_manager:
99
raise DOMException(DOMException.INDEX_SIZE_ERR)
10-
except DOMException as e:
11-
code = e.args[0]
12-
self.assertEqual(code, DOMException.INDEX_SIZE_ERR)
13-
else:
1410
self.fail()
11+
self.assertEqual(context_manager.exception.code,
12+
DOMException.INDEX_SIZE_ERR)
1513

1614

1715
if __name__ == '__main__':

‎w3/python/core/fundamental_interface/Node_test.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def testSetter_ReadOnly(self):
6767
# Testing
6868
with self.assertRaises(DOMException) as context_manager:
6969
text.node_value = 'bar'
70-
self.assertEqual(context_manager.exception.args[0],
70+
self.assertEqual(context_manager.exception.code,
7171
DOMException.NO_MODIFICATION_ALLOWED_ERR)
7272
# `node_value` should not be modified.
7373
self.assertEqual(text.node_value, 'foo')
@@ -238,7 +238,7 @@ def test_Raises_WRONG_DOCUMENT_ERR(self):
238238
# Testing
239239
with self.assertRaises(DOMException) as context_manager:
240240
elem_node_1.insert_before(elem_node_2)
241-
self.assertEqual(context_manager.exception.args[0],
241+
self.assertEqual(context_manager.exception.code,
242242
DOMException.WRONG_DOCUMENT_ERR)
243243

244244
def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
@@ -256,7 +256,7 @@ def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
256256
# Testing
257257
with self.assertRaises(DOMException) as context_manager:
258258
parent_node_readonly.insert_before(new_child_node)
259-
self.assertEqual(context_manager.exception.args[0],
259+
self.assertEqual(context_manager.exception.code,
260260
DOMException.NO_MODIFICATION_ALLOWED_ERR)
261261

262262
def test_Raises_NOT_FOUND_ERR(self):
@@ -273,7 +273,7 @@ def test_Raises_NOT_FOUND_ERR(self):
273273
# Testing
274274
with self.assertRaises(DOMException) as context_manager:
275275
parent_node.insert_before(new_child_node, ref_child_node)
276-
self.assertEqual(context_manager.exception.args[0],
276+
self.assertEqual(context_manager.exception.code,
277277
DOMException.NOT_FOUND_ERR)
278278

279279

@@ -363,7 +363,7 @@ def test_Raises_WRONG_DOCUMENT_ERR(self):
363363
# Testing
364364
with self.assertRaises(DOMException) as context_manager:
365365
elem_node_1.append_child(elem_node_2)
366-
self.assertEqual(context_manager.exception.args[0],
366+
self.assertEqual(context_manager.exception.code,
367367
DOMException.WRONG_DOCUMENT_ERR)
368368

369369
def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
@@ -381,7 +381,7 @@ def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
381381
# Testing
382382
with self.assertRaises(DOMException) as context_manager:
383383
parent_node_readonly.append_child(new_child_node)
384-
self.assertEqual(context_manager.exception.args[0],
384+
self.assertEqual(context_manager.exception.code,
385385
DOMException.NO_MODIFICATION_ALLOWED_ERR)
386386

387387

@@ -436,7 +436,7 @@ def test_Raises_WRONG_DOCUMENT_ERR(self):
436436
# Testing
437437
with self.assertRaises(DOMException) as context_manager:
438438
parent_node.replace_child(new_child_node, old_child_node)
439-
self.assertEqual(context_manager.exception.args[0],
439+
self.assertEqual(context_manager.exception.code,
440440
DOMException.WRONG_DOCUMENT_ERR)
441441

442442
def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
@@ -454,7 +454,7 @@ def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
454454
# Testing
455455
with self.assertRaises(DOMException) as context_manager:
456456
parent_node_readonly.append_child(new_child_node)
457-
self.assertEqual(context_manager.exception.args[0],
457+
self.assertEqual(context_manager.exception.code,
458458
DOMException.NO_MODIFICATION_ALLOWED_ERR)
459459

460460

@@ -521,7 +521,7 @@ def test_Raises_NOT_FOUND_ERR(self):
521521
# Testing
522522
with self.assertRaises(DOMException) as context_manager:
523523
parent_node.remove_child(child_node)
524-
self.assertEqual(context_manager.exception.args[0],
524+
self.assertEqual(context_manager.exception.code,
525525
DOMException.NOT_FOUND_ERR)
526526

527527
def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
@@ -541,7 +541,7 @@ def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
541541
# Testing
542542
with self.assertRaises(DOMException) as context_manager:
543543
parent_node.remove_child(child_node)
544-
self.assertEqual(context_manager.exception.args[0],
544+
self.assertEqual(context_manager.exception.code,
545545
DOMException.NO_MODIFICATION_ALLOWED_ERR)
546546

547547

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /