Merge pull request #1763 from youknowone/exceptions

Add missing exceptions
This commit is contained in:
Noah
2020-02-27 12:12:34 -06:00
committed by GitHub
3 changed files with 190 additions and 164 deletions

View File

@@ -18,8 +18,6 @@ class ExceptionClassTests(unittest.TestCase):
"%s missing %s attribute" %
(ins.__class__.__name__, attr))
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_inheritance(self):
# Make sure the inheritance hierarchy matches the documentation
exc_set = set()
@@ -88,8 +86,6 @@ class ExceptionClassTests(unittest.TestCase):
self.assertEqual(given, expected, "%s: %s != %s" % (test_name,
given, expected))
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_interface_single_arg(self):
# Make sure interface works properly when given a single argument
arg = "spam"

View File

@@ -848,68 +848,75 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
"NotImplemented" => ctx.not_implemented(),
"Ellipsis" => vm.ctx.ellipsis.clone(),
// ordered by exception_hierarachy.txt
// Exceptions:
"BaseException" => ctx.exceptions.base_exception_type.clone(),
"SystemExit" => ctx.exceptions.system_exit.clone(),
"KeyboardInterrupt" => ctx.exceptions.keyboard_interrupt.clone(),
"GeneratorExit" => ctx.exceptions.generator_exit.clone(),
"Exception" => ctx.exceptions.exception_type.clone(),
"ArithmeticError" => ctx.exceptions.arithmetic_error.clone(),
"AssertionError" => ctx.exceptions.assertion_error.clone(),
"AttributeError" => ctx.exceptions.attribute_error.clone(),
"NameError" => ctx.exceptions.name_error.clone(),
"OverflowError" => ctx.exceptions.overflow_error.clone(),
"RuntimeError" => ctx.exceptions.runtime_error.clone(),
"ReferenceError" => ctx.exceptions.reference_error.clone(),
"SyntaxError" => ctx.exceptions.syntax_error.clone(),
"IndentationError" => ctx.exceptions.indentation_error.clone(),
"TabError" => ctx.exceptions.tab_error.clone(),
"NotImplementedError" => ctx.exceptions.not_implemented_error.clone(),
"RecursionError" => ctx.exceptions.recursion_error.clone(),
"TypeError" => ctx.exceptions.type_error.clone(),
"ValueError" => ctx.exceptions.value_error.clone(),
"IndexError" => ctx.exceptions.index_error.clone(),
"ImportError" => ctx.exceptions.import_error.clone(),
"LookupError" => ctx.exceptions.lookup_error.clone(),
"StopIteration" => ctx.exceptions.stop_iteration.clone(),
"StopAsyncIteration" => ctx.exceptions.stop_async_iteration.clone(),
"ArithmeticError" => ctx.exceptions.arithmetic_error.clone(),
"FloatingPointError" => ctx.exceptions.floating_point_error.clone(),
"OverflowError" => ctx.exceptions.overflow_error.clone(),
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
"AssertionError" => ctx.exceptions.assertion_error.clone(),
"AttributeError" => ctx.exceptions.attribute_error.clone(),
"BufferError" => ctx.exceptions.buffer_error.clone(),
"EOFError" => ctx.exceptions.eof_error.clone(),
"ImportError" => ctx.exceptions.import_error.clone(),
"ModuleNotFoundError" => ctx.exceptions.module_not_found_error.clone(),
"LookupError" => ctx.exceptions.lookup_error.clone(),
"IndexError" => ctx.exceptions.index_error.clone(),
"KeyError" => ctx.exceptions.key_error.clone(),
"MemoryError" => ctx.exceptions.memory_error.clone(),
"NameError" => ctx.exceptions.name_error.clone(),
"UnboundLocalError" => ctx.exceptions.unbound_local_error.clone(),
"OSError" => ctx.exceptions.os_error.clone(),
"BlockingIOError" => ctx.exceptions.blocking_io_error.clone(),
"ChildProcessError" => ctx.exceptions.child_process_error.clone(),
"ConnectionError" => ctx.exceptions.connection_error.clone(),
"BrokenPipeError" => ctx.exceptions.broken_pipe_error.clone(),
"ConnectionAbortedError" => ctx.exceptions.connection_aborted_error.clone(),
"ConnectionRefusedError" => ctx.exceptions.connection_refused_error.clone(),
"ConnectionResetError" => ctx.exceptions.connection_reset_error.clone(),
"FileExistsError" => ctx.exceptions.file_exists_error.clone(),
"FileNotFoundError" => ctx.exceptions.file_not_found_error.clone(),
"InterruptedError" => ctx.exceptions.interrupted_error.clone(),
"IsADirectoryError" => ctx.exceptions.is_a_directory_error.clone(),
"NotADirectoryError" => ctx.exceptions.not_a_directory_error.clone(),
"PermissionError" => ctx.exceptions.permission_error.clone(),
"ProcessLookupError" => ctx.exceptions.process_lookup_error.clone(),
"TimeoutError" => ctx.exceptions.timeout_error.clone(),
"ReferenceError" => ctx.exceptions.reference_error.clone(),
"RuntimeError" => ctx.exceptions.runtime_error.clone(),
"NotImplementedError" => ctx.exceptions.not_implemented_error.clone(),
"RecursionError" => ctx.exceptions.recursion_error.clone(),
"SyntaxError" => ctx.exceptions.syntax_error.clone(),
"TargetScopeError" => ctx.exceptions.target_scope_error.clone(),
"IndentationError" => ctx.exceptions.indentation_error.clone(),
"TabError" => ctx.exceptions.tab_error.clone(),
"SystemError" => ctx.exceptions.system_error.clone(),
"TypeError" => ctx.exceptions.type_error.clone(),
"ValueError" => ctx.exceptions.value_error.clone(),
"UnicodeError" => ctx.exceptions.unicode_error.clone(),
"UnicodeDecodeError" => ctx.exceptions.unicode_decode_error.clone(),
"UnicodeEncodeError" => ctx.exceptions.unicode_encode_error.clone(),
"UnicodeTranslateError" => ctx.exceptions.unicode_translate_error.clone(),
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
"KeyError" => ctx.exceptions.key_error.clone(),
"ModuleNotFoundError" => ctx.exceptions.module_not_found_error.clone(),
"EOFError" => ctx.exceptions.eof_error.clone(),
"MemoryError" => ctx.exceptions.memory_error.clone(),
"OSError" => ctx.exceptions.os_error.clone(),
"IOError" => ctx.exceptions.os_error.clone(),
"FileNotFoundError" => ctx.exceptions.file_not_found_error.clone(),
"PermissionError" => ctx.exceptions.permission_error.clone(),
"FileExistsError" => ctx.exceptions.file_exists_error.clone(),
"BlockingIOError" => ctx.exceptions.blocking_io_error.clone(),
"InterruptedError" => ctx.exceptions.interrupted_error.clone(),
"ConnectionError" => ctx.exceptions.connection_error.clone(),
"ConnectionResetError" => ctx.exceptions.connection_reset_error.clone(),
"ConnectionRefusedError" => ctx.exceptions.connection_refused_error.clone(),
"ConnectionAbortedError" => ctx.exceptions.connection_aborted_error.clone(),
"BrokenPipeError" => ctx.exceptions.broken_pipe_error.clone(),
// Warnings
"Warning" => ctx.exceptions.warning.clone(),
"BytesWarning" => ctx.exceptions.bytes_warning.clone(),
"UnicodeWarning" => ctx.exceptions.unicode_warning.clone(),
"DeprecationWarning" => ctx.exceptions.deprecation_warning.clone(),
"PendingDeprecationWarning" => ctx.exceptions.pending_deprecation_warning.clone(),
"RuntimeWarning" => ctx.exceptions.runtime_warning.clone(),
"SyntaxWarning" => ctx.exceptions.syntax_warning.clone(),
"UserWarning" => ctx.exceptions.user_warning.clone(),
"FutureWarning" => ctx.exceptions.future_warning.clone(),
"ImportWarning" => ctx.exceptions.import_warning.clone(),
"SyntaxWarning" => ctx.exceptions.syntax_warning.clone(),
"UnicodeWarning" => ctx.exceptions.unicode_warning.clone(),
"BytesWarning" => ctx.exceptions.bytes_warning.clone(),
"ResourceWarning" => ctx.exceptions.resource_warning.clone(),
"RuntimeWarning" => ctx.exceptions.runtime_warning.clone(),
"UserWarning" => ctx.exceptions.user_warning.clone(),
"KeyboardInterrupt" => ctx.exceptions.keyboard_interrupt.clone(),
"GeneratorExit" => ctx.exceptions.generator_exit.clone(),
"SystemExit" => ctx.exceptions.system_exit.clone(),
});
}

View File

@@ -139,10 +139,7 @@ impl PyBaseException {
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> String {
let repr_args = exception_args_as_string(vm, zelf.args(), false);
let cls = zelf.class();
match repr_args.into_iter().exactly_one() {
Ok(one) => format!("{}({},)", cls.name, one),
Err(i) => format!("{}({})", cls.name, i.format(", ")),
}
format!("{}({})", cls.name, repr_args.iter().format(", "))
}
}
@@ -358,25 +355,51 @@ pub fn normalize(
#[derive(Debug)]
pub struct ExceptionZoo {
pub arithmetic_error: PyClassRef,
pub assertion_error: PyClassRef,
pub attribute_error: PyClassRef,
pub base_exception_type: PyClassRef,
pub system_exit: PyClassRef,
pub keyboard_interrupt: PyClassRef,
pub generator_exit: PyClassRef,
pub exception_type: PyClassRef,
pub import_error: PyClassRef,
pub index_error: PyClassRef,
pub key_error: PyClassRef,
pub lookup_error: PyClassRef,
pub module_not_found_error: PyClassRef,
pub name_error: PyClassRef,
pub not_implemented_error: PyClassRef,
pub recursion_error: PyClassRef,
pub overflow_error: PyClassRef,
pub reference_error: PyClassRef,
pub runtime_error: PyClassRef,
pub stop_iteration: PyClassRef,
pub stop_async_iteration: PyClassRef,
pub arithmetic_error: PyClassRef,
pub floating_point_error: PyClassRef,
pub overflow_error: PyClassRef,
pub zero_division_error: PyClassRef,
pub assertion_error: PyClassRef,
pub attribute_error: PyClassRef,
pub buffer_error: PyClassRef,
pub eof_error: PyClassRef,
pub import_error: PyClassRef,
pub module_not_found_error: PyClassRef,
pub lookup_error: PyClassRef,
pub index_error: PyClassRef,
pub key_error: PyClassRef,
pub memory_error: PyClassRef,
pub name_error: PyClassRef,
pub unbound_local_error: PyClassRef,
pub os_error: PyClassRef,
pub blocking_io_error: PyClassRef,
pub child_process_error: PyClassRef,
pub connection_error: PyClassRef,
pub broken_pipe_error: PyClassRef,
pub connection_aborted_error: PyClassRef,
pub connection_refused_error: PyClassRef,
pub connection_reset_error: PyClassRef,
pub file_exists_error: PyClassRef,
pub file_not_found_error: PyClassRef,
pub interrupted_error: PyClassRef,
pub is_a_directory_error: PyClassRef,
pub not_a_directory_error: PyClassRef,
pub permission_error: PyClassRef,
pub process_lookup_error: PyClassRef,
pub timeout_error: PyClassRef,
pub reference_error: PyClassRef,
pub runtime_error: PyClassRef,
pub not_implemented_error: PyClassRef,
pub recursion_error: PyClassRef,
pub syntax_error: PyClassRef,
pub target_scope_error: PyClassRef,
pub indentation_error: PyClassRef,
pub tab_error: PyClassRef,
pub system_error: PyClassRef,
@@ -386,37 +409,18 @@ pub struct ExceptionZoo {
pub unicode_decode_error: PyClassRef,
pub unicode_encode_error: PyClassRef,
pub unicode_translate_error: PyClassRef,
pub zero_division_error: PyClassRef,
pub eof_error: PyClassRef,
pub memory_error: PyClassRef,
pub os_error: PyClassRef,
pub file_not_found_error: PyClassRef,
pub permission_error: PyClassRef,
pub file_exists_error: PyClassRef,
pub blocking_io_error: PyClassRef,
pub interrupted_error: PyClassRef,
pub connection_error: PyClassRef,
pub connection_reset_error: PyClassRef,
pub connection_refused_error: PyClassRef,
pub connection_aborted_error: PyClassRef,
pub broken_pipe_error: PyClassRef,
pub warning: PyClassRef,
pub bytes_warning: PyClassRef,
pub unicode_warning: PyClassRef,
pub deprecation_warning: PyClassRef,
pub pending_deprecation_warning: PyClassRef,
pub runtime_warning: PyClassRef,
pub syntax_warning: PyClassRef,
pub user_warning: PyClassRef,
pub future_warning: PyClassRef,
pub import_warning: PyClassRef,
pub syntax_warning: PyClassRef,
pub unicode_warning: PyClassRef,
pub bytes_warning: PyClassRef,
pub resource_warning: PyClassRef,
pub runtime_warning: PyClassRef,
pub user_warning: PyClassRef,
pub keyboard_interrupt: PyClassRef,
pub generator_exit: PyClassRef,
pub system_exit: PyClassRef,
}
impl ExceptionZoo {
@@ -428,91 +432,127 @@ impl ExceptionZoo {
};
// Sorted By Hierarchy then alphabetized.
let base_exception_type = create_exception_type("BaseException", &object_type);
let system_exit = create_exception_type("SystemExit", &base_exception_type);
let keyboard_interrupt = create_exception_type("KeyboardInterrupt", &base_exception_type);
let generator_exit = create_exception_type("GeneratorExit", &base_exception_type);
let exception_type = create_exception_type("Exception", &base_exception_type);
let stop_iteration = create_exception_type("StopIteration", &exception_type);
let stop_async_iteration = create_exception_type("StopAsyncIteration", &exception_type);
let arithmetic_error = create_exception_type("ArithmeticError", &exception_type);
let floating_point_error = create_exception_type("FloatingPointError", &arithmetic_error);
let overflow_error = create_exception_type("OverflowError", &arithmetic_error);
let zero_division_error = create_exception_type("ZeroDivisionError", &arithmetic_error);
let assertion_error = create_exception_type("AssertionError", &exception_type);
let attribute_error = create_exception_type("AttributeError", &exception_type);
let buffer_error = create_exception_type("BufferError", &exception_type);
let eof_error = create_exception_type("EOFError", &exception_type);
let import_error = create_exception_type("ImportError", &exception_type);
let module_not_found_error = create_exception_type("ModuleNotFoundError", &import_error);
let lookup_error = create_exception_type("LookupError", &exception_type);
let index_error = create_exception_type("IndexError", &lookup_error);
let key_error = create_exception_type("KeyError", &lookup_error);
let memory_error = create_exception_type("MemoryError", &exception_type);
let name_error = create_exception_type("NameError", &exception_type);
let runtime_error = create_exception_type("RuntimeError", &exception_type);
let unbound_local_error = create_exception_type("UnboundLocalError", &name_error);
// os errors
let os_error = create_exception_type("OSError", &exception_type);
let blocking_io_error = create_exception_type("BlockingIOError", &os_error);
let child_process_error = create_exception_type("ChildProcessError", &os_error);
let connection_error = create_exception_type("ConnectionError", &os_error);
let connection_aborted_error =
create_exception_type("ConnectionAbortedError", &connection_error);
let connection_refused_error =
create_exception_type("ConnectionRefusedError", &connection_error);
let connection_reset_error =
create_exception_type("ConnectionResetError", &connection_error);
let file_exists_error = create_exception_type("FileExistsError", &os_error);
let file_not_found_error = create_exception_type("FileNotFoundError", &os_error);
let interrupted_error = create_exception_type("InterruptedError", &os_error);
let is_a_directory_error = create_exception_type("IsADirectoryError", &os_error);
let not_a_directory_error = create_exception_type("NotADirectoryError", &os_error);
let broken_pipe_error = create_exception_type("BrokenPipeError", &connection_error);
let permission_error = create_exception_type("PermissionError", &os_error);
let process_lookup_error = create_exception_type("ProcessLookupError", &os_error);
let timeout_error = create_exception_type("TimeoutError", &os_error);
let reference_error = create_exception_type("ReferenceError", &exception_type);
let stop_iteration = create_exception_type("StopIteration", &exception_type);
let stop_async_iteration = create_exception_type("StopAsyncIteration", &exception_type);
let runtime_error = create_exception_type("RuntimeError", &exception_type);
let not_implemented_error = create_exception_type("NotImplementedError", &runtime_error);
let recursion_error = create_exception_type("RecursionError", &runtime_error);
let syntax_error = create_exception_type("SyntaxError", &exception_type);
let indentation_error = create_exception_type("IndentationError", &syntax_error);
let tab_error = create_exception_type("TabError", &indentation_error);
let target_scope_error = create_exception_type("TargetScopeError", &syntax_error);
let system_error = create_exception_type("SystemError", &exception_type);
let type_error = create_exception_type("TypeError", &exception_type);
let value_error = create_exception_type("ValueError", &exception_type);
let overflow_error = create_exception_type("OverflowError", &arithmetic_error);
let zero_division_error = create_exception_type("ZeroDivisionError", &arithmetic_error);
let module_not_found_error = create_exception_type("ModuleNotFoundError", &import_error);
let not_implemented_error = create_exception_type("NotImplementedError", &runtime_error);
let recursion_error = create_exception_type("RecursionError", &runtime_error);
let eof_error = create_exception_type("EOFError", &exception_type);
let indentation_error = create_exception_type("IndentationError", &syntax_error);
let tab_error = create_exception_type("TabError", &indentation_error);
let unicode_error = create_exception_type("UnicodeError", &value_error);
let unicode_decode_error = create_exception_type("UnicodeDecodeError", &unicode_error);
let unicode_encode_error = create_exception_type("UnicodeEncodeError", &unicode_error);
let unicode_translate_error =
create_exception_type("UnicodeTranslateError", &unicode_error);
let memory_error = create_exception_type("MemoryError", &exception_type);
// os errors
let os_error = create_exception_type("OSError", &exception_type);
let file_not_found_error = create_exception_type("FileNotFoundError", &os_error);
let permission_error = create_exception_type("PermissionError", &os_error);
let file_exists_error = create_exception_type("FileExistsError", &os_error);
let blocking_io_error = create_exception_type("BlockingIOError", &os_error);
let interrupted_error = create_exception_type("InterruptedError", &os_error);
let connection_error = create_exception_type("ConnectionError", &os_error);
let connection_reset_error =
create_exception_type("ConnectionResetError", &connection_error);
let connection_refused_error =
create_exception_type("ConnectionRefusedError", &connection_error);
let connection_aborted_error =
create_exception_type("ConnectionAbortedError", &connection_error);
let broken_pipe_error = create_exception_type("BrokenPipeError", &connection_error);
let warning = create_exception_type("Warning", &exception_type);
let bytes_warning = create_exception_type("BytesWarning", &warning);
let unicode_warning = create_exception_type("UnicodeWarning", &warning);
let deprecation_warning = create_exception_type("DeprecationWarning", &warning);
let pending_deprecation_warning =
create_exception_type("PendingDeprecationWarning", &warning);
let runtime_warning = create_exception_type("RuntimeWarning", &warning);
let syntax_warning = create_exception_type("SyntaxWarning", &warning);
let user_warning = create_exception_type("UserWarning", &warning);
let future_warning = create_exception_type("FutureWarning", &warning);
let import_warning = create_exception_type("ImportWarning", &warning);
let syntax_warning = create_exception_type("SyntaxWarning", &warning);
let unicode_warning = create_exception_type("UnicodeWarning", &warning);
let bytes_warning = create_exception_type("BytesWarning", &warning);
let resource_warning = create_exception_type("ResourceWarning", &warning);
let runtime_warning = create_exception_type("RuntimeWarning", &warning);
let user_warning = create_exception_type("UserWarning", &warning);
let keyboard_interrupt = create_exception_type("KeyboardInterrupt", &base_exception_type);
let generator_exit = create_exception_type("GeneratorExit", &base_exception_type);
let system_exit = create_exception_type("SystemExit", &base_exception_type);
ExceptionZoo {
arithmetic_error,
assertion_error,
attribute_error,
base_exception_type,
system_exit,
keyboard_interrupt,
generator_exit,
exception_type,
import_error,
index_error,
key_error,
lookup_error,
module_not_found_error,
name_error,
not_implemented_error,
recursion_error,
overflow_error,
runtime_error,
stop_iteration,
stop_async_iteration,
arithmetic_error,
floating_point_error,
overflow_error,
zero_division_error,
assertion_error,
attribute_error,
buffer_error,
eof_error,
import_error,
module_not_found_error,
lookup_error,
index_error,
key_error,
memory_error,
name_error,
unbound_local_error,
os_error,
blocking_io_error,
child_process_error,
connection_error,
broken_pipe_error,
connection_aborted_error,
connection_refused_error,
connection_reset_error,
file_exists_error,
file_not_found_error,
interrupted_error,
is_a_directory_error,
not_a_directory_error,
permission_error,
process_lookup_error,
timeout_error,
reference_error,
runtime_error,
not_implemented_error,
recursion_error,
syntax_error,
target_scope_error,
indentation_error,
tab_error,
system_error,
@@ -522,35 +562,18 @@ impl ExceptionZoo {
unicode_decode_error,
unicode_encode_error,
unicode_translate_error,
zero_division_error,
eof_error,
memory_error,
os_error,
file_not_found_error,
permission_error,
file_exists_error,
blocking_io_error,
interrupted_error,
connection_error,
connection_reset_error,
connection_refused_error,
connection_aborted_error,
broken_pipe_error,
warning,
bytes_warning,
unicode_warning,
deprecation_warning,
pending_deprecation_warning,
runtime_warning,
syntax_warning,
user_warning,
future_warning,
import_warning,
syntax_warning,
unicode_warning,
bytes_warning,
resource_warning,
runtime_warning,
reference_error,
user_warning,
keyboard_interrupt,
generator_exit,
system_exit,
}
}
}