spglib.error module

spglib.error module#

Exception types raised in the python API.

Warning

Exception support is still a work in progress. Not all exceptions may be raised as SpglibError. Please report any exceptions that are not caught by SpglibError.

Deprecated since version 2.7.0: Currently the exception raising is opt-in, requiring to set spglib.error.OLD_ERROR_HANDLING to False, otherwise all exceptions are redirected to spglib.error.get_error_message().

For example you could use the following snippet to opt-in this new interface.

try:
    spglib.error.OLD_ERROR_HANDLING = False
except AttributeError:
    pass

or alternatively you can enable it by setting the environment variable SPGLIB_OLD_ERROR_HANDLING to either 0 or False.

Starting from version 2.8.0 the default option will be flipped to always throw these exceptions, and the old error handling will be removed in version 3.0. Please test out the functionality and provide feedback on the GitHub issues.

Note that spglib.error.OLD_ERROR_HANDLING will be removed in a future version, so the snippet above is not advised to be used for long-term support. Use it only for experimentations and preparing the code base to try ... except the spglib api calls and wait for the 2.8.0 release.

Added in version 2.7.0.

spglib.error.OLD_ERROR_HANDLING: bool = True#

Use the old error handling.

Note that this variable may be removed in the future or change value in the future. You can also use SPGLIB_OLD_ERROR_HANDLING instead of altering this value.

exception spglib.error.SpglibError[source]#

Bases: Exception

Base exception type for all errors raised by spglib.

Use this error type in a try ... except to process any physical or other spglib defined errors, such as the number of atom types not matching the positions or the distance between atoms being too close. For example:

symprec = 1e-10
while (symprec<1):
    try:
        dataset = spglib.get_symmetry(cell, symprec)
    except spglib.SpglibError as exc:
        # Expected issues by spglib
        # Try again if the symprec was too tight
        if str(exc) == "too close distance between atoms":
            symprec *= 10
            continue
        # Otherwise fail gracefully
        print(f"Failed to calculate symmetry:\n{exc}")
        break
    except Exception:
        # Unexpected issues, raise as an unexpected exception
        raise

Note

More fine-grained exceptions are not defined yet. Please provide feedback on what kind of exception separation would be desired.

spglib.error.get_error_message()[source]#

Return error message why spglib failed.

Warning

This method is not thread safe, i.e., only safely usable when calling one spglib method per process.

Added in version 1.9.5.

Deprecated since version 2.7.0.

Return type:

str