Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions w3/python/core/fundamental_interface/DOMException.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import enum
from typing import Final
from ctypes import c_ushort


class ExceptionCode(enum.IntEnum):
"""Definition group `ExceptionCode`
An integer indicating the type of error generated.
"""
INDEX_SIZE_ERR = 1
DOMSTRING_SIZE_ERR = 2
HIERARCHY_REQUEST_ERR = 3
WRONG_DOCUMENT_ERR = 4
INVALID_CHARACTER_ERR = 5
NO_DATA_ALLOWED_ERR = 6
NO_MODIFICATION_ALLOWED_ERR = 7
NOT_FOUND_ERR = 8
NOT_SUPPORTED_ERR = 9
INUSE_ATTRIBUTE_ERR = 10
class DOMException(Exception):
"""Exception `DOMException`

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`.

class DOMException(Exception):
"""Exception DOMException
Implementations may raise other exceptions under other circumstances.
For example, implementations may raise an implementation-dependent exception if a `None` argument is passed.

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`.
Implementations may raise other exceptions under other circumstances. For example, implementations may raise an implementation-dependent exception if a null argument is passed.
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.
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.
"""
INDEX_SIZE_ERR = ExceptionCode.INDEX_SIZE_ERR
DOMSTRING_SIZE_ERR = ExceptionCode.DOMSTRING_SIZE_ERR
HIERARCHY_REQUEST_ERR = ExceptionCode.HIERARCHY_REQUEST_ERR
WRONG_DOCUMENT_ERR = ExceptionCode.WRONG_DOCUMENT_ERR
INVALID_CHARACTER_ERR = ExceptionCode.INVALID_CHARACTER_ERR
NO_DATA_ALLOWED_ERR = ExceptionCode.NO_DATA_ALLOWED_ERR
NO_MODIFICATION_ALLOWED_ERR = ExceptionCode.NO_MODIFICATION_ALLOWED_ERR
NOT_FOUND_ERR = ExceptionCode.NOT_FOUND_ERR
NOT_SUPPORTED_ERR = ExceptionCode.NOT_SUPPORTED_ERR
INUSE_ATTRIBUTE_ERR = ExceptionCode.INUSE_ATTRIBUTE_ERR

# Definition group `ExceptionCode`
# An integer indicating the type of error generated.
INDEX_SIZE_ERR: Final[c_ushort] = 1
DOMSTRING_SIZE_ERR: Final[c_ushort] = 2
HIERARCHY_REQUEST_ERR: Final[c_ushort] = 3
WRONG_DOCUMENT_ERR: Final[c_ushort] = 4
INVALID_CHARACTER_ERR: Final[c_ushort] = 5
NO_DATA_ALLOWED_ERR: Final[c_ushort] = 6
NO_MODIFICATION_ALLOWED_ERR: Final[c_ushort] = 7
NOT_FOUND_ERR: Final[c_ushort] = 8
NOT_SUPPORTED_ERR: Final[c_ushort] = 9
INUSE_ATTRIBUTE_ERR: Final[c_ushort] = 10

def __init__(self, error_code: c_ushort, *args: object) -> None:
super().__init__(*args)
self.code: c_ushort = error_code
10 changes: 4 additions & 6 deletions w3/python/core/fundamental_interface/DOMException_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
from w3.dom import DOMException


class Test_DOMException(unittest.TestCase):
class TestProperty_Code(unittest.TestCase):
def test_raise_INDEX_SIZE_ERR(self):
try:
with self.assertRaises(DOMException) as context_manager:
raise DOMException(DOMException.INDEX_SIZE_ERR)
except DOMException as e:
code = e.args[0]
self.assertEqual(code, DOMException.INDEX_SIZE_ERR)
else:
self.fail()
self.assertEqual(context_manager.exception.code,
DOMException.INDEX_SIZE_ERR)


if __name__ == '__main__':
Expand Down
20 changes: 10 additions & 10 deletions w3/python/core/fundamental_interface/Node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def testSetter_ReadOnly(self):
# Testing
with self.assertRaises(DOMException) as context_manager:
text.node_value = 'bar'
self.assertEqual(context_manager.exception.args[0],
self.assertEqual(context_manager.exception.code,
DOMException.NO_MODIFICATION_ALLOWED_ERR)
# `node_value` should not be modified.
self.assertEqual(text.node_value, 'foo')
Expand Down Expand Up @@ -238,7 +238,7 @@ def test_Raises_WRONG_DOCUMENT_ERR(self):
# Testing
with self.assertRaises(DOMException) as context_manager:
elem_node_1.insert_before(elem_node_2)
self.assertEqual(context_manager.exception.args[0],
self.assertEqual(context_manager.exception.code,
DOMException.WRONG_DOCUMENT_ERR)

def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
Expand All @@ -256,7 +256,7 @@ def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
# Testing
with self.assertRaises(DOMException) as context_manager:
parent_node_readonly.insert_before(new_child_node)
self.assertEqual(context_manager.exception.args[0],
self.assertEqual(context_manager.exception.code,
DOMException.NO_MODIFICATION_ALLOWED_ERR)

def test_Raises_NOT_FOUND_ERR(self):
Expand All @@ -273,7 +273,7 @@ def test_Raises_NOT_FOUND_ERR(self):
# Testing
with self.assertRaises(DOMException) as context_manager:
parent_node.insert_before(new_child_node, ref_child_node)
self.assertEqual(context_manager.exception.args[0],
self.assertEqual(context_manager.exception.code,
DOMException.NOT_FOUND_ERR)


Expand Down Expand Up @@ -363,7 +363,7 @@ def test_Raises_WRONG_DOCUMENT_ERR(self):
# Testing
with self.assertRaises(DOMException) as context_manager:
elem_node_1.append_child(elem_node_2)
self.assertEqual(context_manager.exception.args[0],
self.assertEqual(context_manager.exception.code,
DOMException.WRONG_DOCUMENT_ERR)

def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
Expand All @@ -381,7 +381,7 @@ def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
# Testing
with self.assertRaises(DOMException) as context_manager:
parent_node_readonly.append_child(new_child_node)
self.assertEqual(context_manager.exception.args[0],
self.assertEqual(context_manager.exception.code,
DOMException.NO_MODIFICATION_ALLOWED_ERR)


Expand Down Expand Up @@ -436,7 +436,7 @@ def test_Raises_WRONG_DOCUMENT_ERR(self):
# Testing
with self.assertRaises(DOMException) as context_manager:
parent_node.replace_child(new_child_node, old_child_node)
self.assertEqual(context_manager.exception.args[0],
self.assertEqual(context_manager.exception.code,
DOMException.WRONG_DOCUMENT_ERR)

def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
Expand All @@ -454,7 +454,7 @@ def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
# Testing
with self.assertRaises(DOMException) as context_manager:
parent_node_readonly.append_child(new_child_node)
self.assertEqual(context_manager.exception.args[0],
self.assertEqual(context_manager.exception.code,
DOMException.NO_MODIFICATION_ALLOWED_ERR)


Expand Down Expand Up @@ -521,7 +521,7 @@ def test_Raises_NOT_FOUND_ERR(self):
# Testing
with self.assertRaises(DOMException) as context_manager:
parent_node.remove_child(child_node)
self.assertEqual(context_manager.exception.args[0],
self.assertEqual(context_manager.exception.code,
DOMException.NOT_FOUND_ERR)

def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
Expand All @@ -541,7 +541,7 @@ def test_Raises_NO_MODIFICATION_ALLOWED_ERR(self):
# Testing
with self.assertRaises(DOMException) as context_manager:
parent_node.remove_child(child_node)
self.assertEqual(context_manager.exception.args[0],
self.assertEqual(context_manager.exception.code,
DOMException.NO_MODIFICATION_ALLOWED_ERR)


Expand Down