From 98ad34f0df6be93db98046d4b34d35f6476d5179 Mon Sep 17 00:00:00 2001 From: Giovanni Bussi Date: Fri, 1 Dec 2023 17:18:34 +0100 Subject: [PATCH] pass filesystem_error to python, including paths --- python/cplumed.pxd | 6 ++++++ python/plumed.pyx | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/python/cplumed.pxd b/python/cplumed.pxd index fd90c97a2b..440b03981a 100644 --- a/python/cplumed.pxd +++ b/python/cplumed.pxd @@ -35,10 +35,16 @@ cdef extern from "Plumed.h": ctypedef struct plumed_nothrow_handler: void* ptr void (*handler)(void*,int,const char*,const void*) + ctypedef struct plumed_error_filesystem_path: + size_t numbytes + void* ptr ctypedef struct plumed_error: int code const char* what void* nested + int error_code + plumed_error_filesystem_path path1 + plumed_error_filesystem_path path2 # ignore other members void plumed_cmd_safe_nothrow(plumed p,const char*key,plumed_safeptr safe,plumed_nothrow_handler nothrow) void plumed_error_set(void*ptr,int code,const char*what,const void* opt) diff --git a/python/plumed.pyx b/python/plumed.pyx index cfb43c986d..318734c063 100644 --- a/python/plumed.pyx +++ b/python/plumed.pyx @@ -130,6 +130,17 @@ cdef class Plumed: raise_from = None msg = error.what what = msg.decode("utf-8") + + # this is likely not working on Windows, where encoding would be different: + if error.path1.ptr: + path1=(error.path1.ptr).decode("utf-8") + else: + path1=None + if error.path2.ptr: + path2=(error.path2.ptr).decode("utf-8") + else: + path2=None + # this map is from cython doc if error.code>=20300 and error.code<20400: # PLMD::Plumed::ExceptionTypeError raise TypeError(what) from raise_from @@ -143,6 +154,8 @@ cdef class Plumed: raise ValueError(what) from raise_from elif error.code>=10105 and error.code<10110: # std::invalid_argument raise ValueError(what) from raise_from + elif error.code==10229: # filesystem::filesystem_error + raise OSError(error.error_code,what,path1,None,path2) from raise_from elif error.code>=10230 and error.code<10240: # std::ios_base::failure # Unfortunately, in standard C++ we have no way of distinguishing EOF # from other errors here; be careful with the exception mask