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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de>              *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This program is free software; you can redistribute it and/or modify  *

 *   it under the terms of the GNU Library General Public License (LGPL)   *

 *   as published by the Free Software Foundation; either version 2 of     *

 *   the License, or (at your option) any later version.                   *

 *   for detail see the LICENCE text file.                                 *

 *                                                                         *

 *   FreeCAD is distributed in the hope that it will be useful,            *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with FreeCAD; if not, write to the Free Software        *

 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *

 *   USA                                                                   *

 *                                                                         *

 ***************************************************************************/

#include <utility>

#include <FCConfig.h>

#include "Console.h"
#include "PyObjectBase.h"

#include "Exception.h"

FC_LOG_LEVEL_INIT("Exception", true, true)

using namespace Base;


TYPESYSTEM_SOURCE(Base::Exception, Base::BaseClass)
Exception::Exception(std::string message)
    : errorMessage {std::move(message)}
{}

Exception::Exception(const Exception& inst) = default;

Exception::Exception(Exception&& inst) noexcept = default;

Exception& Exception::operator=(const Exception& inst)
{
    errorMessage = inst.errorMessage;
    fileName = inst.fileName;
    lineNum = inst.lineNum;
    functionName = inst.functionName;
    isTranslatable = inst.isTranslatable;
    return *this;
}

Exception& Exception::operator=(Exception&& inst) noexcept
{
    errorMessage = std::move(inst.errorMessage);
    fileName = std::move(inst.fileName);
    lineNum = inst.lineNum;
    functionName = std::move(inst.functionName);
    isTranslatable = inst.isTranslatable;
    return *this;
}

const char* Exception::what() const noexcept

{
    return errorMessage.c_str();
}

void Exception::reportException() const

{
    if (hasBeenReported) {
        return;
    }

    std::string msg = errorMessage.empty() ? typeid(*this).name() : errorMessage;

#ifdef FC_DEBUG
    if (!functionName.empty()) {
        msg = functionName + " -- " + msg;
    }
#endif

    _FC_ERR(fileName.c_str(), lineNum, msg);
    hasBeenReported = true;
}

PyObject* Exception::getPyObject()

{
    Py::Dict edict;
    edict.setItem("sclassname", Py::String(typeid(*this).name()));
    edict.setItem("sErrMsg", Py::String(getMessage()));
    edict.setItem("sfile", Py::String(getFile()));
    edict.setItem("iline", Py::Long(getLine()));
    edict.setItem("sfunction", Py::String(getFunction()));
    edict.setItem("swhat", Py::String(what()));
    edict.setItem("btranslatable", Py::Boolean(getTranslatable()));
    edict.setItem("breported", Py::Boolean(hasBeenReported));
    return new_reference_to(edict);
}

void Exception::setPyObject(PyObject* pydict)

{
    try {
        if (pydict && Py::_Dict_Check(pydict)) {
            const Py::Dict edict(pydict);
            if (edict.hasKey("sfile")) {
                fileName = Py::String(edict.getItem("sfile"));
            }

            if (edict.hasKey("sfunction")) {
                functionName = Py::String(edict.getItem("sfunction"));
            }

            if (edict.hasKey("sErrMsg")) {
                errorMessage = Py::String(edict.getItem("sErrMsg"));
            }

            if (edict.hasKey("iline")) {
                lineNum = static_cast<int>(Py::Long(edict.getItem("iline")));
            }
            if (edict.hasKey("btranslatable")) {
                isTranslatable = static_cast<bool>(Py::Boolean(edict.getItem("btranslatable")));
            }
            if (edict.hasKey("breported")) {
                hasBeenReported = static_cast<bool>(Py::Boolean(edict.getItem("breported")));
            }
        }
    }
    catch (Py::Exception& e) {
        e.clear();  // should never happen
    }
}

PyObject* Exception::getPyExceptionType() const

{
    return PyExc_FC_GeneralError;
}

void Exception::setPyException() const

{
    PyObject* exc = getPyExceptionType();
    if (!exc) {
        exc = PyExc_FC_GeneralError;
    }

    PyErr_SetString(exc, what());
}

// ---------------------------------------------------------

TYPESYSTEM_SOURCE(Base::AbortException, Base::Exception)

AbortException::AbortException(const std::string& message)
    : Exception(message)
{}

const char* AbortException::what() const noexcept

{
    return Exception::what();
}

PyObject* AbortException::getPyExceptionType() const

{
    return PyExc_FC_FreeCADAbort;
}

// ---------------------------------------------------------

XMLBaseException::XMLBaseException(const std::string& message)
    : Exception(message)
{}

PyObject* XMLBaseException::getPyExceptionType() const

{
    return PyExc_FC_XMLBaseException;
}

// ---------------------------------------------------------

XMLParseException::XMLParseException(const std::string& message)
    : XMLBaseException(message)
{}

const char* XMLParseException::what() const noexcept

{
    return XMLBaseException::what();
}

PyObject* XMLParseException::getPyExceptionType() const

{
    return PyExc_FC_XMLParseException;
}

// ---------------------------------------------------------

XMLAttributeError::XMLAttributeError(const std::string& message)
    : XMLBaseException(message)
{}

const char* XMLAttributeError::what() const noexcept

{
    return XMLBaseException::what();
}

PyObject* XMLAttributeError::getPyExceptionType() const

{
    return PyExc_FC_XMLAttributeError;
}

// ---------------------------------------------------------

FileException::FileException(const std::string& message, const std::string& fileName)
    : Exception(message)
    , file(fileName)
{
    setFileName(fileName);
}

FileException::FileException(const std::string& message, const FileInfo& File)
    : Exception(message)
    , file(File)
{
    setFileName(File.filePath());
}

void FileException::setFileName(const std::string& fileName)

{
    file.setFile(fileName);
    _sErrMsgAndFileName = getMessage();
    if (!getFile().empty()) {
        _sErrMsgAndFileName += ": ";
        _sErrMsgAndFileName += fileName;
    }
}

std::string FileException::getFileName() const

{
    return file.fileName();
}

const char* FileException::what() const noexcept

{
    return _sErrMsgAndFileName.c_str();
}

void FileException::reportException() const

{
    if (getReported()) {
        return;
    }
    std::string msg = _sErrMsgAndFileName.empty() ? typeid(*this).name() : _sErrMsgAndFileName;

#ifdef FC_DEBUG
    if (!getFunction().empty()) {
        msg = getFunction() + " -- " + msg;
    }
#endif

    _FC_ERR(getFile().c_str(), getLine(), msg);
    setReported(true);
}

PyObject* FileException::getPyObject()

{
    Py::Dict edict(Exception::getPyObject(), true);
    edict.setItem("filename", Py::String(this->file.fileName()));
    return new_reference_to(edict);
}

void FileException::setPyObject(PyObject* pydict)

{
    if (pydict) {
        Exception::setPyObject(pydict);

        if (const Py::Dict edict(pydict); edict.hasKey("filename")) {
            setFileName(Py::String(edict.getItem("filename")).as_std_string("utf-8"));
        }
    }
}

PyObject* FileException::getPyExceptionType() const

{
    return PyExc_IOError;
}

// ---------------------------------------------------------

FileSystemError::FileSystemError(const std::string& message)
    : Exception(message)
{}

PyObject* FileSystemError::getPyExceptionType() const

{
    return PyExc_IOError;
}

// ---------------------------------------------------------

BadFormatError::BadFormatError(const std::string& message)
    : Exception(message)
{}

PyObject* BadFormatError::getPyExceptionType() const

{
    return PyExc_FC_BadFormatError;
}

// ---------------------------------------------------------

MemoryException::MemoryException(const std::string& message)
    : Exception(message)  // NOLINT(*-throw-keyword-missing)
{}

#if defined(__GNUC__)
const char* MemoryException::what() const noexcept

{
    return Exception::what();  // from Exception, not std::bad_alloc
}
#endif

PyObject* MemoryException::getPyExceptionType() const

{
    return PyExc_MemoryError;
}

// ---------------------------------------------------------

AccessViolation::AccessViolation(const std::string& message)
    : Exception(message)
{}

PyObject* AccessViolation::getPyExceptionType() const

{
    return PyExc_OSError;
}

// ---------------------------------------------------------

AbnormalProgramTermination::AbnormalProgramTermination(const std::string& message)
    : Exception(message)
{}

PyObject* AbnormalProgramTermination::getPyExceptionType() const

{
    return PyExc_InterruptedError;
}

// ---------------------------------------------------------

UnknownProgramOption::UnknownProgramOption(const std::string& message)
    : Exception(message)
{}

PyObject* UnknownProgramOption::getPyExceptionType() const

{
    return PyExc_FC_UnknownProgramOption;
}

// ---------------------------------------------------------

ProgramInformation::ProgramInformation(const std::string& message)
    : Exception(message)
{}

// ---------------------------------------------------------

TypeError::TypeError(const std::string& message)
    : Exception(message)
{}

PyObject* TypeError::getPyExceptionType() const

{
    return PyExc_TypeError;
}

// ---------------------------------------------------------

ValueError::ValueError(const std::string& message)
    : Exception(message)
{}

PyObject* ValueError::getPyExceptionType() const

{
    return PyExc_ValueError;
}

// ---------------------------------------------------------

IndexError::IndexError(const std::string& message)
    : Exception(message)
{}

PyObject* IndexError::getPyExceptionType() const

{
    return PyExc_IndexError;
}

// ---------------------------------------------------------

NameError::NameError(const std::string& message)
    : Exception(message)
{}

PyObject* NameError::getPyExceptionType() const

{
    return PyExc_NameError;
}

// ---------------------------------------------------------

ImportError::ImportError(const std::string& message)
    : Exception(message)
{}

PyObject* ImportError::getPyExceptionType() const

{
    return PyExc_ImportError;
}

// ---------------------------------------------------------

AttributeError::AttributeError(const std::string& message)
    : Exception(message)
{}

PyObject* AttributeError::getPyExceptionType() const

{
    return PyExc_AttributeError;
}

// ---------------------------------------------------------

PropertyError::PropertyError(const std::string& message)
    : AttributeError(message)
{}

PyObject* PropertyError::getPyExceptionType() const

{
    return PyExc_FC_PropertyError;
}

// ---------------------------------------------------------

RuntimeError::RuntimeError(const std::string& message)
    : Exception(message)
{}

PyObject* RuntimeError::getPyExceptionType() const

{
    return PyExc_RuntimeError;
}

// ---------------------------------------------------------

BadGraphError::BadGraphError(const std::string& message)
    : RuntimeError(message)
{}

PyObject* BadGraphError::getPyExceptionType() const

{
    return PyExc_FC_BadGraphError;
}

// ---------------------------------------------------------

NotImplementedError::NotImplementedError(const std::string& message)
    : Exception(message)
{}

PyObject* NotImplementedError::getPyExceptionType() const

{
    return PyExc_NotImplementedError;
}

// ---------------------------------------------------------

ZeroDivisionError::ZeroDivisionError(const std::string& message)
    : Exception(message)
{}

PyObject* ZeroDivisionError::getPyExceptionType() const

{
    return PyExc_ZeroDivisionError;
}

// ---------------------------------------------------------

ReferenceError::ReferenceError(const std::string& message)
    : Exception(message)
{}

PyObject* ReferenceError::getPyExceptionType() const

{
    return PyExc_ReferenceError;
}

// ---------------------------------------------------------

ExpressionError::ExpressionError(const std::string& message)
    : Exception(message)
{}

PyObject* ExpressionError::getPyExceptionType() const

{
    return PyExc_FC_ExpressionError;
}

// ---------------------------------------------------------

ParserError::ParserError(const std::string& message)
    : Exception(message)
{}

PyObject* ParserError::getPyExceptionType() const

{
    return PyExc_FC_ParserError;
}

// ---------------------------------------------------------

UnicodeError::UnicodeError(const std::string& message)
    : Exception(message)
{}

PyObject* UnicodeError::getPyExceptionType() const

{
    return PyExc_UnicodeError;
}

// ---------------------------------------------------------

OverflowError::OverflowError(const std::string& message)
    : Exception(message)
{}

PyObject* OverflowError::getPyExceptionType() const

{
    return PyExc_OverflowError;
}

// ---------------------------------------------------------

UnderflowError::UnderflowError(const std::string& message)
    : Exception(message)
{}

PyObject* UnderflowError::getPyExceptionType() const

{
    return PyExc_ArithmeticError;
}

// ---------------------------------------------------------

UnitsMismatchError::UnitsMismatchError(const std::string& message)
    : Exception(message)
{}

PyObject* UnitsMismatchError::getPyExceptionType() const

{
    return PyExc_ArithmeticError;
}

// ---------------------------------------------------------

CADKernelError::CADKernelError(const std::string& message)
    : Exception(message)
{}

PyObject* CADKernelError::getPyExceptionType() const

{
    return PyExc_FC_CADKernelError;
}

// ---------------------------------------------------------

RestoreError::RestoreError(const std::string& message)
    : Exception(message)
{}

PyObject* RestoreError::getPyExceptionType() const

{
    return PyExc_IOError;
}

// ---------------------------------------------------------

#if defined(__GNUC__) && defined(FC_OS_LINUX)
# include <stdexcept>
# include <iostream>
# include <csignal>

SignalException::SignalException()
{
    memset(&new_action, 0, sizeof(new_action));
    new_action.sa_handler = throw_signal;
    sigemptyset(&new_action.sa_mask);
    new_action.sa_flags = 0;
    ok = (sigaction(SIGSEGV, &new_action, &old_action) < 0);
# ifdef _DEBUG
    std::cout << "Set new signal handler" << std::endl;
# endif
}

SignalException::~SignalException()
{
    sigaction(SIGSEGV, &old_action, nullptr);
# ifdef _DEBUG
    std::cout << "Restore old signal handler" << std::endl;
# endif
}

void SignalException::throw_signal(const int signum)

{
    std::cerr << "SIGSEGV signal raised: " << signum << std::endl;
    throw std::runtime_error("throw_signal");
}
#endif