fparser Reference Guide  0.0.14
__init__.py
1 # Copyright (c) 2017-2022 Science and Technology Facilities Council.
2 # Original work Copyright (c) 1999-2008 Pearu Peterson
3 
4 # All rights reserved.
5 
6 # Modifications made as part of the fparser project are distributed
7 # under the following license:
8 
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions are
11 # met:
12 
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
19 
20 # 3. Neither the name of the copyright holder nor the names of its
21 # contributors may be used to endorse or promote products derived from
22 # this software without specific prior written permission.
23 
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36 # --------------------------------------------------------------------
37 
38 # The original software (in the f2py project) was distributed under
39 # the following license:
40 
41 # Redistribution and use in source and binary forms, with or without
42 # modification, are permitted provided that the following conditions are met:
43 
44 # a. Redistributions of source code must retain the above copyright notice,
45 # this list of conditions and the following disclaimer.
46 # b. Redistributions in binary form must reproduce the above copyright
47 # notice, this list of conditions and the following disclaimer in the
48 # documentation and/or other materials provided with the distribution.
49 # c. Neither the name of the F2PY project nor the names of its
50 # contributors may be used to endorse or promote products derived from
51 # this software without specific prior written permission.
52 
53 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
57 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
60 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
63 # DAMAGE.
64 
65 # First version by: Pearu Peterson <pearu@cens.ioc.ee>
66 # First created: Oct 2006
67 
68 import logging
69 import codecs
70 
71 logging.getLogger(__name__).addHandler(logging.NullHandler())
72 
73 
74 def log_decode_error_handler(err):
75  """
76  A custom error handler for use when reading files. Removes any
77  characters that cause decoding errors and logs the error.
78 
79  :returns: 2-tuple containing replacement for bad chars (an empty string \
80  and the position from where encoding should continue.
81  :rtype: Tuple[str, int]
82 
83  """
84  message = f"character in input file. Error returned was {str(err)}."
85  # Log the fact that this character will be removed from the input file
86  logging.getLogger(__name__).warning("Skipped bad %s", message)
87  return ("", err.end)
88 
89 
90 codecs.register_error("fparser-logging", log_decode_error_handler)