Debian Codegraph @

src/Base/QuantityPyImp.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2013 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include <cmath>
26 #include <functional>
27 #include <limits>
28 #include <optional>
29 #include <sstream>
30 #include <string>
31 #include <vector>
32
33 #include "Unit.h"
34
35 // generated out of Quantity.pyi
36 #include "QuantityPy.h"
37 #include "QuantityPy.cpp"
38
39 #include "UnitPy.h"
40
41 using Base::Quantity;
42
43 // returns a string which represents the object e.g. when printed in python
44 std::string QuantityPy::representation() const
45 {
46 std::stringstream ss;
47 // Use Python's implementation to repr() a float
48 Py::Float flt(getQuantityPtr()->getValue());
49 ss << static_cast<std::string>(flt.repr());
50 if (!getQuantityPtr()->isDimensionless()) {
51 ss << " " << getQuantityPtr()->getUnit().getString();
52 }
53
54 return ss.str();
55 }
56
57 PyObject* QuantityPy::toStr(PyObject* args) const
58 {
59 int prec = getQuantityPtr()->getFormat().getPrecision();
60 if (!PyArg_ParseTuple(args, "|i", &prec)) {
61 return nullptr;
62 }
63
64 double val = getQuantityPtr()->getValue();
65
66 std::stringstream ss;
67 ss.precision(prec);
68 ss.setf(std::ios::fixed, std::ios::floatfield);
69 ss << val;
70 if (!getQuantityPtr()->isDimensionless()) {
71 ss << " " << getQuantityPtr()->getUnit().getString();
72 }
73
74 return Py_BuildValue("s", ss.str().c_str());
75 }
76
77 PyObject* QuantityPy::PyMake(PyTypeObject* /*unused*/, PyObject* /*unused*/, PyObject* /*unused*/)
78 {
79 // create a new instance of QuantityPy and the Twin object
80 return new QuantityPy(new Quantity);
81 }
82
83 // constructor method
84 int QuantityPy::PyInit(PyObject* args, PyObject* /*kwd*/)
85 {
86 Quantity* self = getQuantityPtr();
87
88 PyErr_Clear(); // set by PyArg_ParseTuple()
89 PyObject* object {};
90 if (PyArg_ParseTuple(args, "O!", &(QuantityPy::Type), &object)) {
91 *self = *(static_cast<QuantityPy*>(object)->getQuantityPtr());
92 return 0;
93 }
94 PyErr_Clear(); // set by PyArg_ParseTuple()
95
96 double f = std::numeric_limits<double>::max();
97 if (PyArg_ParseTuple(args, "dO!", &f, &(UnitPy::Type), &object)) {
98 *self = Quantity(f, *(static_cast<UnitPy*>(object)->getUnitPtr()));
99 return 0;
100 }
101 PyErr_Clear(); // set by PyArg_ParseTuple()
102
103 if (PyArg_ParseTuple(args, "dO!", &f, &(QuantityPy::Type), &object)) {
104 PyErr_SetString(PyExc_TypeError, "Second argument must be a Unit not a Quantity");
105 return -1;
106 }
107 PyErr_Clear(); // set by PyArg_ParseTuple()
108
109 int i1 {0};
110 int i2 {0};
111 int i3 {0};
112 int i4 {0};
113 int i5 {0};
114 int i6 {0};
115 int i7 {0};
116 int i8 {0};
117 if (PyArg_ParseTuple(args, "|diiiiiiii", &f, &i1, &i2, &i3, &i4, &i5, &i6, &i7, &i8)) {
118 if (f < std::numeric_limits<double>::max()) {
119 *self = Quantity {f, Unit(i1, i2, i3, i4, i5, i6, i7, i8)};
120 }
121 return 0;
122 }
123 PyErr_Clear(); // set by PyArg_ParseTuple()
124
125 char* string {};
126 if (PyArg_ParseTuple(args, "et", "utf-8", &string)) {
127 std::string str(string);
128 PyMem_Free(string);
129 try {
130 *self = Quantity::parse(str);
131 }
132 catch (const ParserError& e) {
133 PyErr_SetString(PyExc_ValueError, e.what());
134 return -1;
135 }
136
137 return 0;
138 }
139 PyErr_Clear(); // set by PyArg_ParseTuple()
140
141 if (PyArg_ParseTuple(args, "det", &f, "utf-8", &string)) {
142 std::string str(string);
143 PyMem_Free(string);
144 try {
145 *self = Quantity(f, str);
146 }
147 catch (const ParserError& e) {
148 PyErr_SetString(PyExc_ValueError, e.what());
149 return -1;
150 }
151
152 return 0;
153 }
154
155 PyErr_SetString(PyExc_TypeError, "Either quantity, float with units or string expected");
156 return -1;
157 }
158
159 PyObject* QuantityPy::getUserPreferred(PyObject* /*args*/) const
160 {
161 std::string uus;
162 double factor {};
163 Py::Tuple res(3);
164
165 auto uss = getQuantityPtr()->getUserString(factor, uus);
166
167 res[0] = Py::String(uss, "utf-8");
168 res[1] = Py::Float(factor);
169 res[2] = Py::String(uus, "utf-8");
170
171 return Py::new_reference_to(res);
172 }
173
174 PyObject* QuantityPy::getValueAs(PyObject* args) const
175 {
176 auto tryQuantity = [&]() -> std::optional<Quantity> {
177 PyObject* object {};
178 if (!PyArg_ParseTuple(args, "O!", &(QuantityPy::Type), &object)) {
179 return std::nullopt;
180 }
181
182 return *static_cast<QuantityPy*>(object)->getQuantityPtr();
183 };
184
185 auto tryUnit = [&]() -> std::optional<Quantity> {
186 PyObject* object {};
187 if (!PyArg_ParseTuple(args, "O!", &(UnitPy::Type), &object)) {
188 return std::nullopt;
189 }
190
191 return Quantity {1.0, *static_cast<UnitPy*>(object)->getUnitPtr()};
192 };
193
194 auto tryUnitAndValue = [&]() -> std::optional<Quantity> {
195 PyObject* object {};
196 double value {};
197 if (!PyArg_ParseTuple(args, "dO!", &value, &(UnitPy::Type), &object)) {
198 return std::nullopt;
199 }
200
201 return Quantity {value, *static_cast<UnitPy*>(object)->getUnitPtr()};
202 };
203
204 auto tryUnitPartsAndValue = [&]() -> std::optional<Quantity> {
205 double f;
206 int i1 {0};
207 int i2 {0};
208 int i3 {0};
209 int i4 {0};
210 int i5 {0};
211 int i6 {0};
212 int i7 {0};
213 int i8 {0};
214 if (!PyArg_ParseTuple(args, "d|iiiiiiii", &f, &i1, &i2, &i3, &i4, &i5, &i6, &i7, &i8)) {
215 return std::nullopt;
216 }
217
218 return Quantity {f, Unit(i1, i2, i3, i4, i5, i6, i7, i8)};
219 };
220
221 auto tryString = [&]() -> std::optional<Quantity> {
222 char* string {};
223 if (!PyArg_ParseTuple(args, "et", "utf-8", &string)) {
224 return std::nullopt;
225 }
226
227 const std::string str {string};
228 PyMem_Free(string);
229 return Quantity::parse(str);
230 };
231
232 const std::vector<std::function<std::optional<Quantity>()>> funcs
233 = {tryQuantity, tryUnit, tryUnitAndValue, tryUnitPartsAndValue, tryString};
234
235 auto tryFuncs = [&]() -> std::optional<Quantity> {
236 for (const auto& func : funcs) {
237 PyErr_Clear();
238 if (auto quant = func(); quant.has_value()) {
239 return quant;
240 }
241 }
242 return std::nullopt;
243 };
244
245 auto checkQuant = [&](const Quantity& quant) -> bool {
246 auto err = [&](const std::string& str) {
247 PyErr_SetString(PyExc_ValueError, str.c_str());
248 };
249
250 const auto* qPtr = getQuantityPtr();
251 if (!qPtr) {
252 err("QuantityPtr is null");
253 return false;
254 }
255
256 const auto qpUnit = qPtr->getUnit();
257 if (const auto qUnit = quant.getUnit(); qUnit != qpUnit) {
258 err("Unit mismatch (`" + qUnit.getString() + "` != `" + qpUnit.getString() + "`)");
259 return false;
260 }
261
262 return true;
263 };
264
265 //----------------------------------------------------------------------------------------------
266
267 const auto optQuant = tryFuncs();
268 if (!optQuant.has_value()) {
269 PyErr_SetString(PyExc_TypeError, "Expected quantity, string, float or unit");
270 return nullptr;
271 }
272
273 const auto quant = optQuant.value();
274 if (!quant.isDimensionless()) {
275 if (!checkQuant(quant)) {
276 return nullptr;
277 }
278 }
279
280 return new QuantityPy(new Quantity(getQuantityPtr()->getValue() / quant.getValue()));
281 }
282
283 PyObject* QuantityPy::__round__(PyObject* args) const
284 {
285 double val = getQuantityPtr()->getValue();
286 Unit unit = getQuantityPtr()->getUnit();
287 Py::Float flt(val);
288 Py::Callable func(flt.getAttr("__round__"));
289 double rnd = static_cast<double>(Py::Float(func.apply(args)));
290
291 return new QuantityPy(new Quantity(rnd, unit));
292 }
293
294 PyObject* QuantityPy::number_float_handler(PyObject* self)
295 {
296 if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
297 PyErr_SetString(PyExc_TypeError, "Arg must be Quantity");
298 return nullptr;
299 }
300
301 QuantityPy* q = static_cast<QuantityPy*>(self);
302 return PyFloat_FromDouble(q->getValue());
303 }
304
305 PyObject* QuantityPy::number_int_handler(PyObject* self)
306 {
307 if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
308 PyErr_SetString(PyExc_TypeError, "Arg must be Quantity");
309 return nullptr;
310 }
311
312 QuantityPy* q = static_cast<QuantityPy*>(self);
313 return PyLong_FromLong(long(q->getValue()));
314 }
315
316 PyObject* QuantityPy::number_negative_handler(PyObject* self)
317 {
318 if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
319 PyErr_SetString(PyExc_TypeError, "Arg must be Quantity");
320 return nullptr;
321 }
322
323 Quantity* a = static_cast<QuantityPy*>(self)->getQuantityPtr();
324 double b = -1;
325 return new QuantityPy(new Quantity(*a * b));
326 }
327
328 PyObject* QuantityPy::number_positive_handler(PyObject* self)
329 {
330 if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
331 PyErr_SetString(PyExc_TypeError, "Arg must be Quantity");
332 return nullptr;
333 }
334
335 Quantity* a = static_cast<QuantityPy*>(self)->getQuantityPtr();
336 return new QuantityPy(new Quantity(*a));
337 }
338
339 PyObject* QuantityPy::number_absolute_handler(PyObject* self)
340 {
341 if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
342 PyErr_SetString(PyExc_TypeError, "Arg must be Quantity");
343 return nullptr;
344 }
345
346 Quantity* a = static_cast<QuantityPy*>(self)->getQuantityPtr();
347 return new QuantityPy(new Quantity(fabs(a->getValue()), a->getUnit()));
348 }
349
350 static Quantity& pyToQuantity(Quantity& q, PyObject* pyobj)
351 {
352 if (PyObject_TypeCheck(pyobj, &QuantityPy::Type)) {
353 q = *static_cast<QuantityPy*>(pyobj)->getQuantityPtr();
354 }
355 else if (PyFloat_Check(pyobj)) {
356 q = Quantity(PyFloat_AsDouble(pyobj));
357 }
358 else if (PyLong_Check(pyobj)) {
359 q = Quantity(PyLong_AsLong(pyobj));
360 }
361 else {
362 PyErr_Format(PyExc_TypeError, "Cannot convert %s to Quantity", Py_TYPE(pyobj)->tp_name);
363 throw Py::Exception();
364 }
365 return q;
366 }
367
368 PyObject* QuantityPy::number_add_handler(PyObject* self, PyObject* other)
369 {
370 Quantity* pa = nullptr;
371 Quantity* pb = nullptr;
372 Quantity a;
373 Quantity b;
374 PY_TRY
375 {
376 if (PyObject_TypeCheck(self, &(QuantityPy::Type))) {
377 pa = static_cast<QuantityPy*>(self)->getQuantityPtr();
378 }
379 else {
380 pa = &pyToQuantity(a, self);
381 }
382
383 if (PyObject_TypeCheck(other, &(QuantityPy::Type))) {
384 pb = static_cast<QuantityPy*>(other)->getQuantityPtr();
385 }
386 else {
387 pb = &pyToQuantity(b, other);
388 }
389 return new QuantityPy(new Quantity(*pa + *pb));
390 }
391 PY_CATCH
392 }
393
394 PyObject* QuantityPy::number_subtract_handler(PyObject* self, PyObject* other)
395 {
396 Quantity* pa = nullptr;
397 Quantity* pb = nullptr;
398 Quantity a;
399 Quantity b;
400 PY_TRY
401 {
402 if (PyObject_TypeCheck(self, &(QuantityPy::Type))) {
403 pa = static_cast<QuantityPy*>(self)->getQuantityPtr();
404 }
405 else {
406 pa = &pyToQuantity(a, self);
407 }
408
409 if (PyObject_TypeCheck(other, &(QuantityPy::Type))) {
410 pb = static_cast<QuantityPy*>(other)->getQuantityPtr();
411 }
412 else {
413 pb = &pyToQuantity(b, other);
414 }
415 return new QuantityPy(new Quantity(*pa - *pb));
416 }
417 PY_CATCH
418 }
419
420 PyObject* QuantityPy::number_multiply_handler(PyObject* self, PyObject* other)
421 {
422 Quantity* pa = nullptr;
423 Quantity* pb = nullptr;
424 Quantity a;
425 Quantity b;
426 PY_TRY
427 {
428 if (PyObject_TypeCheck(self, &(QuantityPy::Type))) {
429 pa = static_cast<QuantityPy*>(self)->getQuantityPtr();
430 }
431 else {
432 pa = &pyToQuantity(a, self);
433 }
434
435 if (PyObject_TypeCheck(other, &(QuantityPy::Type))) {
436 pb = static_cast<QuantityPy*>(other)->getQuantityPtr();
437 }
438 else {
439 pb = &pyToQuantity(b, other);
440 }
441 return new QuantityPy(new Quantity(*pa * *pb));
442 }
443 PY_CATCH
444 }
445
446 PyObject* QuantityPy::number_divide_handler(PyObject* self, PyObject* other)
447 {
448 Quantity* pa = nullptr;
449 Quantity* pb = nullptr;
450 Quantity a;
451 Quantity b;
452 PY_TRY
453 {
454 if (PyObject_TypeCheck(self, &(QuantityPy::Type))) {
455 pa = static_cast<QuantityPy*>(self)->getQuantityPtr();
456 }
457 else {
458 pa = &pyToQuantity(a, self);
459 }
460
461 if (PyObject_TypeCheck(other, &(QuantityPy::Type))) {
462 pb = static_cast<QuantityPy*>(other)->getQuantityPtr();
463 }
464 else {
465 pb = &pyToQuantity(b, other);
466 }
467 return new QuantityPy(new Quantity(*pa / *pb));
468 }
469 PY_CATCH
470 }
471
472 PyObject* QuantityPy::number_remainder_handler(PyObject* self, PyObject* other)
473 {
474 if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
475 PyErr_SetString(PyExc_TypeError, "First arg must be Quantity");
476 return nullptr;
477 }
478
479 double d1 {};
480 double d2 {};
481 Quantity* a = static_cast<QuantityPy*>(self)->getQuantityPtr();
482 d1 = a->getValue();
483
484 if (PyObject_TypeCheck(other, &(QuantityPy::Type))) {
485 Quantity* b = static_cast<QuantityPy*>(other)->getQuantityPtr();
486 d2 = b->getValue();
487 }
488 else if (PyFloat_Check(other)) {
489 d2 = PyFloat_AsDouble(other);
490 }
491 else if (PyLong_Check(other)) {
492 d2 = (double)PyLong_AsLong(other);
493 }
494 else {
495 PyErr_SetString(PyExc_TypeError, "Expected quantity or number");
496 return nullptr;
497 }
498
499 PyObject* p1 = PyFloat_FromDouble(d1);
500 PyObject* p2 = PyFloat_FromDouble(d2);
501 PyObject* r = PyNumber_Remainder(p1, p2);
502 Py_DECREF(p1);
503 Py_DECREF(p2);
504 if (!r) {
505 return nullptr;
506 }
507 double q = PyFloat_AsDouble(r);
508 Py_DECREF(r);
509 return new QuantityPy(new Quantity(q, a->getUnit()));
510 }
511
512 PyObject* QuantityPy::number_divmod_handler(PyObject* /*self*/, PyObject* /*other*/)
513 {
514 // PyNumber_Divmod();
515 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
516 return nullptr;
517 }
518
519 PyObject* QuantityPy::number_power_handler(PyObject* self, PyObject* other, PyObject* /*modulo*/)
520 {
521 if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
522 PyErr_SetString(PyExc_TypeError, "First arg must be Quantity");
523 return nullptr;
524 }
525
526 PY_TRY
527 {
528 if (PyObject_TypeCheck(other, &(QuantityPy::Type))) {
529 Quantity* a = static_cast<QuantityPy*>(self)->getQuantityPtr();
530 Quantity* b = static_cast<QuantityPy*>(other)->getQuantityPtr();
531 Quantity q(a->pow(*b)); // to prevent memory leak in case of exception
532
533 return new QuantityPy(new Quantity(q));
534 }
535 if (PyFloat_Check(other)) {
536 Quantity* a = static_cast<QuantityPy*>(self)->getQuantityPtr();
537 double b = PyFloat_AsDouble(other);
538 return new QuantityPy(new Quantity(a->pow(b)));
539 }
540 if (PyLong_Check(other)) {
541 Quantity* a = static_cast<QuantityPy*>(self)->getQuantityPtr();
542 double b = (double)PyLong_AsLong(other);
543 return new QuantityPy(new Quantity(a->pow(b)));
544 }
545 PyErr_SetString(PyExc_TypeError, "Expected quantity or number");
546 return nullptr;
547 }
548 PY_CATCH
549 }
550
551 int QuantityPy::number_nonzero_handler(PyObject* self)
552 {
553 if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) {
554 return 1;
555 }
556
557 Quantity* a = static_cast<QuantityPy*>(self)->getQuantityPtr();
558 return a->getValue() != 0.0;
559 }
560
561 PyObject* QuantityPy::richCompare(PyObject* v, PyObject* w, int op)
562 {
563 if (PyObject_TypeCheck(v, &(QuantityPy::Type)) && PyObject_TypeCheck(w, &(QuantityPy::Type))) {
564 const Quantity* u1 = static_cast<QuantityPy*>(v)->getQuantityPtr();
565 const Quantity* u2 = static_cast<QuantityPy*>(w)->getQuantityPtr();
566
567 PyObject* res = nullptr;
568 switch (op) {
569 case Py_NE:
570 res = (!(*u1 == *u2)) ? Py_True : Py_False;
571 Py_INCREF(res);
572 return res;
573 case Py_LT:
574 res = (*u1 < *u2) ? Py_True : Py_False;
575 Py_INCREF(res);
576 return res;
577 case Py_LE:
578 res = (*u1 < *u2) || (*u1 == *u2) ? Py_True : Py_False;
579 Py_INCREF(res);
580 return res;
581 case Py_GT:
582 res = (!(*u1 < *u2)) && (!(*u1 == *u2)) ? Py_True : Py_False;
583 Py_INCREF(res);
584 return res;
585 case Py_GE:
586 res = (!(*u1 < *u2)) ? Py_True : Py_False;
587 Py_INCREF(res);
588 return res;
589 case Py_EQ:
590 res = (*u1 == *u2) ? Py_True : Py_False;
591 Py_INCREF(res);
592 return res;
593 }
594 }
595 else if (PyNumber_Check(v) && PyNumber_Check(w)) {
596 // Try to get floating numbers
597 double u1 = PyFloat_AsDouble(v);
598 double u2 = PyFloat_AsDouble(w);
599 PyObject* res = nullptr;
600 switch (op) {
601 case Py_NE:
602 res = (u1 != u2) ? Py_True : Py_False;
603 Py_INCREF(res);
604 return res;
605 case Py_LT:
606 res = (u1 < u2) ? Py_True : Py_False;
607 Py_INCREF(res);
608 return res;
609 case Py_LE:
610 res = (u1 <= u2) ? Py_True : Py_False;
611 Py_INCREF(res);
612 return res;
613 case Py_GT:
614 res = (u1 > u2) ? Py_True : Py_False;
615 Py_INCREF(res);
616 return res;
617 case Py_GE:
618 res = (u1 >= u2) ? Py_True : Py_False;
619 Py_INCREF(res);
620 return res;
621 case Py_EQ:
622 res = (u1 == u2) ? Py_True : Py_False;
623 Py_INCREF(res);
624 return res;
625 }
626 }
627
628 // This always returns False
629 Py_INCREF(Py_NotImplemented);
630 return Py_NotImplemented;
631 }
632
633 Py::Float QuantityPy::getValue() const
634 {
635 return Py::Float(getQuantityPtr()->getValue());
636 }
637
638 void QuantityPy::setValue(Py::Float arg)
639 {
640 getQuantityPtr()->setValue(arg);
641 }
642
643 Py::Object QuantityPy::getUnit() const
644 {
645 return Py::asObject(new UnitPy(new Unit(getQuantityPtr()->getUnit())));
646 }
647
648 void QuantityPy::setUnit(Py::Object arg)
649 {
650 Py::Type UnitType(getTypeAsObject(&UnitPy::Type));
651 if (!arg.isType(UnitType)) {
652 throw Py::AttributeError("Not yet implemented");
653 }
654
655 getQuantityPtr()->setUnit(*static_cast<UnitPy*>((*arg))->getUnitPtr());
656 }
657
658 Py::String QuantityPy::getUserString() const
659 {
660 return {getQuantityPtr()->getUserString(), "utf-8"};
661 }
662
663 Py::Dict QuantityPy::getFormat() const
664 {
665 QuantityFormat fmt = getQuantityPtr()->getFormat();
666
667 Py::Dict dict;
668 dict.setItem("Precision", Py::Long(fmt.getPrecision()));
669 dict.setItem("NumberFormat", Py::Char(fmt.toFormat()));
670 dict.setItem("Denominator", Py::Long(fmt.getDenominator()));
671 return dict;
672 }
673
674 void QuantityPy::setFormat(Py::Dict arg)
675 {
676 QuantityFormat fmt = getQuantityPtr()->getFormat();
677
678 if (arg.hasKey("Precision")) {
679 Py::Long prec(arg.getItem("Precision"));
680 fmt.setPrecision(static_cast<int>(prec));
681 }
682
683 if (arg.hasKey("NumberFormat")) {
684 Py::Object item = arg.getItem("NumberFormat");
685 if (item.isNumeric()) {
686 int format = static_cast<int>(Py::Long(item));
687 if (format < 0 || format > QuantityFormat::Scientific) {
688 throw Py::ValueError("Invalid format value");
689 }
690 fmt.format = static_cast<QuantityFormat::NumberFormat>(format);
691 }
692 else {
693 Py::Char form(item);
694 std::string fmtstr = static_cast<std::string>(Py::String(form));
695 if (fmtstr.size() != 1) {
696 throw Py::ValueError("Invalid format character");
697 }
698
699 bool ok = false;
700 fmt.format = QuantityFormat::toFormat(fmtstr[0], &ok);
701 if (!ok) {
702 throw Py::ValueError("Invalid format character");
703 }
704 }
705 }
706
707 if (arg.hasKey("Denominator")) {
708 Py::Long denom(arg.getItem("Denominator"));
709 int fracInch = static_cast<int>(denom);
710 // check that the value is positive and a power of 2
711 if (fracInch <= 0) {
712 throw Py::ValueError("Denominator must be higher than zero");
713 }
714 // bitwise check
715 if (fracInch & (fracInch - 1)) {
716 throw Py::ValueError("Denominator must be a power of two");
717 }
718 fmt.setDenominator(fracInch);
719 }
720
721 getQuantityPtr()->setFormat(fmt);
722 }
723
724 PyObject* QuantityPy::getCustomAttributes(const char* attr) const
725 {
726 QuantityPy* py = nullptr;
727 if (strcmp(attr, "Torr") == 0) {
728 py = new QuantityPy(new Quantity(Quantity::Torr));
729 }
730 else if (strcmp(attr, "mTorr") == 0) {
731 py = new QuantityPy(new Quantity(Quantity::mTorr));
732 }
733 else if (strcmp(attr, "yTorr") == 0) {
734 py = new QuantityPy(new Quantity(Quantity::yTorr));
735 }
736 else if (strcmp(attr, "PoundForce") == 0) {
737 py = new QuantityPy(new Quantity(Quantity::PoundForce));
738 }
739 else if (strcmp(attr, "AngularMinute") == 0) {
740 py = new QuantityPy(new Quantity(Quantity::AngMinute));
741 }
742 else if (strcmp(attr, "AngularSecond") == 0) {
743 py = new QuantityPy(new Quantity(Quantity::AngSecond));
744 }
745
746 if (py) {
747 py->setNotTracking();
748 }
749
750 return py;
751 }
752
753 int QuantityPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
754 {
755 return 0;
756 }
757
758 PyObject* QuantityPy::number_invert_handler(PyObject* /*self*/)
759 {
760 PyErr_SetString(PyExc_TypeError, "bad operand type for unary ~");
761 return nullptr;
762 }
763
764 PyObject* QuantityPy::number_lshift_handler(PyObject* /*self*/, PyObject* /*other*/)
765 {
766 PyErr_SetString(PyExc_TypeError, "unsupported operand type(s) for <<");
767 return nullptr;
768 }
769
770 PyObject* QuantityPy::number_rshift_handler(PyObject* /*self*/, PyObject* /*other*/)
771 {
772 PyErr_SetString(PyExc_TypeError, "unsupported operand type(s) for >>");
773 return nullptr;
774 }
775
776 PyObject* QuantityPy::number_and_handler(PyObject* /*self*/, PyObject* /*other*/)
777 {
778 PyErr_SetString(PyExc_TypeError, "unsupported operand type(s) for &");
779 return nullptr;
780 }
781
782 PyObject* QuantityPy::number_xor_handler(PyObject* /*self*/, PyObject* /*other*/)
783 {
784 PyErr_SetString(PyExc_TypeError, "unsupported operand type(s) for ^");
785 return nullptr;
786 }
787
788 PyObject* QuantityPy::number_or_handler(PyObject* /*self*/, PyObject* /*other*/)
789 {
790 PyErr_SetString(PyExc_TypeError, "unsupported operand type(s) for |");
791 return nullptr;
792 }