Mailing List Archive

Exception compatibility with aliens
I have sent this email to two mailing lists for programming language
proposals, the one for C++ and the one for Python. If you reply to
this email, please make sure you reply to both. You can see the
mailing list archives for this month for each list here:

C++ : https://lists.isocpp.org/std-proposals/2023/01/date.php
Python : https://mail.python.org/archives/list/python-dev@python.org/2023/1/

Both C++ and Python have exception handling, however a C++ program
which links with a Python library is unable to handle an exception
thrown from Python.

Is it conceivable in the future that the C++ Standards Committee and
the Python Steering Council could cooperate and furnish each other
with an exhaustive list of exceptions thrown from their respective
standard libraries, so that both languages can implement compatibility
with each other's exceptions?

If such a list were to accompany each C++ standard and each Python
Language Reference, then for example the C++ standard library could
provide classes for handling Python exceptions:

namespace std {

struct alien_exception /* does not inherit from std::exception */ {
enum class language : unsigned int { java=1u, csharp, python };
virtual language lang(void) const noexcept = 0;
virtual char const *what(void) const noexcept = 0;
};

namespace aliens {

namespace java {
struct exception : alien_exception {
char const *getLocalizedMessage(void) const noexcept;
char const *getMessage(void) const noexcept;
std::stacktrace getStackTrace(void) const noexcept;
char const *what(void) const noexcept override { return
this->getMessage(); }
language lang(void) const noexcept override { return language::java; }
};
}

namespace python {
struct exception : alien_exception {
char const *const *notes(void) const noexcept; /* null terminated */
std::stacktrace traceback() const noexcept;
char const *what(void) const noexcept override { return
this->notes()[0u]; }
language lang(void) const noexcept override { return language::python; }
};
}

namespace python {
struct BaseException : exception {};
struct BaseExceptionGroup : BaseException {};
struct GeneratorExit : BaseException {};
struct KeyboardInterrupt : BaseException {};
struct SystemExit : BaseException {};
struct Exception : BaseException {};
struct ArithmeticError : Exception {};
struct FloatingPointError : ArithmeticError {};
struct OverflowError : ArithmeticError {};
struct ZeroDivisionError : ArithmeticError {};
struct AssertionError : Exception {};
struct AttributeError : Exception {};
struct BufferError : Exception {};
struct EOFError : Exception {};
struct ExceptionGroupBaseExceptionGroup : Exception {};
struct ImportError : Exception {};
struct ModuleNotFoundError : ImportError {};
struct LookupError : Exception {};
struct IndexError : LookupError {};
struct KeyError : LookupError {};
struct MemoryError : Exception {};
struct NameError : Exception {};
struct UnboundLocalError : NameError {};
struct OSError : Exception {};
struct BlockingIOError : OSError {};
struct ChildProcessError : OSError {};
struct ConnectionError : OSError {};
struct BrokenPipeError : ConnectionError {};
struct ConnectionAbortedError : ConnectionError {};
struct ConnectionRefusedError : ConnectionError {};
struct ConnectionResetError : ConnectionError {};
struct FileExistsError : OSError {};
struct FileNotFoundError : OSError {};
struct InterruptedError : OSError {};
struct IsADirectoryError : OSError {};
struct NotADirectoryError : OSError {};
struct PermissionError : OSError {};
struct ProcessLookupError : OSError {};
struct TimeoutError : OSError {};
struct ReferenceError : Exception {};
struct RuntimeError : Exception {};
struct NotImplementedError : RuntimeError {};
struct RecursionError : RuntimeError {};
struct StopAsyncIteration : Exception {};
struct StopIteration : Exception {};
struct SyntaxError : Exception {};
struct IndentationError : SyntaxError {};
struct TabError : IndentationError {};
struct SystemError : Exception {};
struct TypeError : Exception {};
struct ValueError : Exception {};
struct UnicodeError : ValueError {};
struct UnicodeDecodeError : UnicodeError {};
struct UnicodeEncodeError : UnicodeError {};
struct UnicodeTranslateError : UnicodeError {};
struct Warning : Exception {};
struct BytesWarning : Warning {};
struct DeprecationWarning : Warning {};
struct EncodingWarning : Warning {};
struct FutureWarning : Warning {};
struct ImportWarning : Warning {};
struct PendingDeprecationWarning : Warning {};
struct ResourceWarning : Warning {};
struct RuntimeWarning : Warning {};
struct SyntaxWarning : Warning {};
struct UnicodeWarning : Warning {};
struct UserWarning : Warning {};
} // close namespace 'python'
} // close namespace 'aliens'

class forced_stack_unwind {};

} // close namespace 'std'

extern int Some_Func_In_Python_Library(void)
{
return 7;
/* or throws an exception */
}

int main(void)
{
try { Some_Func_In_Python_Library(); }
catch ( std::aliens::python::FloatingPointError const & )
{
// The specific Python exception we want to handle
}
catch ( std::aliens::python::exception const & )
{
// Any exception thrown from Python
}
catch ( std::alien_exception const &)
{
// Maybe the Python library linked with an
// Eiffel library that threw an exception
}
catch ( std::forced_stack_unwind )
{

}
}
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/CYAIPU2R4A2HMAEDX6XDMWJ3FZDS6JOL/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: Exception compatibility with aliens [ In reply to ]
On Wed, 18 Jan 2023 12:04:41 +0200
Ville Voutilainen via Std-Proposals <std-proposals@lists.isocpp.org>
wrote:
> On Wed, 18 Jan 2023 at 11:45, Frederick Virchanza Gotham via
> Std-Proposals <std-proposals@lists.isocpp.org> wrote:
> >
> > I have sent this email to two mailing lists for programming language
> > proposals, the one for C++ and the one for Python. If you reply to
> > this email, please make sure you reply to both. You can see the
> > mailing list archives for this month for each list here:
> >
> > C++ : https://lists.isocpp.org/std-proposals/2023/01/date.php
> > Python : https://mail.python.org/archives/list/python-dev@python.org/2023/1/
> >
> > Both C++ and Python have exception handling, however a C++ program
> > which links with a Python library is unable to handle an exception
> > thrown from Python.
>
> Perhaps you should just use
> https://www.boost.org/doc/libs/1_81_0/libs/python/doc/html/index.html

I would suggest taking a look at pybind11 for a potentially more modern
design (also, free of boost dependencies).
https://pybind11.readthedocs.io/en/latest/advanced/exceptions.html

Regards

Antoine.


_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/WGSJJBQA4DK2NK2LQCNLTQZIKML5XVX7/
Code of Conduct: http://python.org/psf/codeofconduct/