/src/pyglue/PyUtil.cpp

http://github.com/imageworks/OpenColorIO · C++ · 771 lines · 572 code · 125 blank · 74 comment · 96 complexity · a448647036a65418edb518203cd57850 MD5 · raw file

  1. /*
  2. Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3. All Rights Reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Sony Pictures Imageworks nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <Python.h>
  28. #include <sstream>
  29. #include <OpenColorIO/OpenColorIO.h>
  30. #include "PyUtil.h"
  31. OCIO_NAMESPACE_ENTER
  32. {
  33. ///////////////////////////////////////////////////////////////////////////
  34. // http://docs.python.org/c-api/object.html#PyObject_IsTrue
  35. int ConvertPyObjectToBool(PyObject *object, void *valuePtr)
  36. {
  37. bool *boolPtr = static_cast<bool*>(valuePtr);
  38. int status = PyObject_IsTrue(object);
  39. if (status == -1 || PyErr_Occurred())
  40. {
  41. if (!PyErr_Occurred())
  42. {
  43. PyErr_SetString(PyExc_ValueError, "could not convert object to bool.");
  44. }
  45. return 0;
  46. }
  47. *boolPtr = (status == 1) ? true : false;
  48. return 1;
  49. }
  50. int ConvertPyObjectToAllocation(PyObject *object, void *valuePtr)
  51. {
  52. Allocation* allocPtr = static_cast<Allocation*>(valuePtr);
  53. if(!PyString_Check(object))
  54. {
  55. PyErr_SetString(PyExc_ValueError, "Object is not a string.");
  56. return 0;
  57. }
  58. *allocPtr = AllocationFromString(PyString_AsString( object ));
  59. return 1;
  60. }
  61. int ConvertPyObjectToInterpolation(PyObject *object, void *valuePtr)
  62. {
  63. Interpolation* interpPtr = static_cast<Interpolation*>(valuePtr);
  64. if(!PyString_Check(object))
  65. {
  66. PyErr_SetString(PyExc_ValueError, "Object is not a string.");
  67. return 0;
  68. }
  69. *interpPtr = InterpolationFromString(PyString_AsString( object ));
  70. return 1;
  71. }
  72. int ConvertPyObjectToTransformDirection(PyObject *object, void *valuePtr)
  73. {
  74. TransformDirection* dirPtr = static_cast<TransformDirection*>(valuePtr);
  75. if(!PyString_Check(object))
  76. {
  77. PyErr_SetString(PyExc_ValueError, "Object is not a string.");
  78. return 0;
  79. }
  80. *dirPtr = TransformDirectionFromString(PyString_AsString( object ));
  81. return 1;
  82. }
  83. int ConvertPyObjectToColorSpaceDirection(PyObject *object, void *valuePtr)
  84. {
  85. ColorSpaceDirection* dirPtr = static_cast<ColorSpaceDirection*>(valuePtr);
  86. if(!PyString_Check(object))
  87. {
  88. PyErr_SetString(PyExc_ValueError, "Object is not a string.");
  89. return 0;
  90. }
  91. *dirPtr = ColorSpaceDirectionFromString(PyString_AsString( object ));
  92. return 1;
  93. }
  94. int ConvertPyObjectToGpuLanguage(PyObject *object, void *valuePtr)
  95. {
  96. GpuLanguage* gpuLanguagePtr = static_cast<GpuLanguage*>(valuePtr);
  97. if(!PyString_Check(object))
  98. {
  99. PyErr_SetString(PyExc_ValueError, "Object is not a string.");
  100. return 0;
  101. }
  102. *gpuLanguagePtr = GpuLanguageFromString(PyString_AsString( object ));
  103. return 1;
  104. }
  105. int ConvertPyObjectToEnvironmentMode(PyObject *object, void *valuePtr)
  106. {
  107. EnvironmentMode* environmentmodePtr = static_cast<EnvironmentMode*>(valuePtr);
  108. if(!PyString_Check(object))
  109. {
  110. PyErr_SetString(PyExc_ValueError, "Object is not a string.");
  111. return 0;
  112. }
  113. *environmentmodePtr = EnvironmentModeFromString(PyString_AsString( object ));
  114. return 1;
  115. }
  116. ///////////////////////////////////////////////////////////////////////////
  117. bool GetIntFromPyObject(PyObject* object, int* val)
  118. {
  119. if(!val || !object) return false;
  120. if( PyInt_Check( object ) )
  121. {
  122. *val = static_cast<int>( PyInt_AS_LONG( object ) );
  123. return true;
  124. }
  125. if( PyFloat_Check( object ) )
  126. {
  127. *val = static_cast<int>( PyFloat_AS_DOUBLE( object ) );
  128. return true;
  129. }
  130. PyObject* intObject = PyNumber_Int(object);
  131. if(intObject)
  132. {
  133. *val = static_cast<int>( PyInt_AS_LONG( intObject ) );
  134. Py_DECREF(intObject);
  135. return true;
  136. }
  137. PyErr_Clear();
  138. return false;
  139. }
  140. bool GetFloatFromPyObject(PyObject* object, float* val)
  141. {
  142. if(!val || !object) return false;
  143. if( PyFloat_Check( object ) )
  144. {
  145. *val = static_cast<float>( PyFloat_AS_DOUBLE( object ) );
  146. return true;
  147. }
  148. if( PyInt_Check( object ) )
  149. {
  150. *val = static_cast<float>( PyInt_AS_LONG( object ) );
  151. return true;
  152. }
  153. PyObject* floatObject = PyNumber_Float(object);
  154. if(floatObject)
  155. {
  156. *val = static_cast<float>( PyFloat_AS_DOUBLE( floatObject ) );
  157. Py_DECREF(floatObject);
  158. return true;
  159. }
  160. PyErr_Clear();
  161. return false;
  162. }
  163. bool GetDoubleFromPyObject(PyObject* object, double* val)
  164. {
  165. if(!val || !object) return false;
  166. if( PyFloat_Check( object ) )
  167. {
  168. *val = PyFloat_AS_DOUBLE( object );
  169. return true;
  170. }
  171. if( PyInt_Check( object ) )
  172. {
  173. *val = static_cast<double>( PyInt_AS_LONG( object ) );
  174. return true;
  175. }
  176. PyObject* floatObject = PyNumber_Float(object);
  177. if(floatObject)
  178. {
  179. *val = PyFloat_AS_DOUBLE( floatObject );
  180. Py_DECREF(floatObject);
  181. return true;
  182. }
  183. PyErr_Clear();
  184. return false;
  185. }
  186. bool GetStringFromPyObject(PyObject* object, std::string* val)
  187. {
  188. if(!val || !object) return false;
  189. if( PyString_Check( object ) )
  190. {
  191. *val = std::string(PyString_AS_STRING(object));
  192. return true;
  193. }
  194. PyObject* strObject = PyObject_Str(object);
  195. if(strObject)
  196. {
  197. *val = std::string(PyString_AS_STRING(strObject));
  198. Py_DECREF(strObject);
  199. return true;
  200. }
  201. PyErr_Clear();
  202. return false;
  203. }
  204. ///////////////////////////////////////////////////////////////////////////
  205. PyObject* CreatePyListFromIntVector(const std::vector<int> &data)
  206. {
  207. PyObject* returnlist = PyList_New( data.size() );
  208. if(!returnlist) return 0;
  209. for(unsigned int i =0; i<data.size(); ++i)
  210. {
  211. PyList_SET_ITEM(returnlist, i, PyInt_FromLong(data[i]));
  212. }
  213. return returnlist;
  214. }
  215. PyObject* CreatePyListFromFloatVector(const std::vector<float> &data)
  216. {
  217. PyObject* returnlist = PyList_New( data.size() );
  218. if(!returnlist) return 0;
  219. for(unsigned int i =0; i<data.size(); ++i)
  220. {
  221. PyList_SET_ITEM(returnlist, i, PyFloat_FromDouble(data[i]));
  222. }
  223. return returnlist;
  224. }
  225. PyObject* CreatePyListFromDoubleVector(const std::vector<double> &data)
  226. {
  227. PyObject* returnlist = PyList_New( data.size() );
  228. if(!returnlist) return 0;
  229. for(unsigned int i =0; i<data.size(); ++i)
  230. {
  231. PyList_SET_ITEM(returnlist, i, PyFloat_FromDouble(data[i]));
  232. }
  233. return returnlist;
  234. }
  235. PyObject* CreatePyListFromStringVector(const std::vector<std::string> &data)
  236. {
  237. PyObject* returnlist = PyList_New( data.size() );
  238. if(!returnlist) return 0;
  239. for(unsigned int i =0; i<data.size(); ++i)
  240. {
  241. PyObject *str = PyString_FromString(data[i].c_str());
  242. if (str == NULL)
  243. {
  244. Py_DECREF(returnlist);
  245. return NULL;
  246. }
  247. PyList_SET_ITEM(returnlist, i, str);
  248. }
  249. return returnlist;
  250. }
  251. PyObject* CreatePyListFromTransformVector(const std::vector<ConstTransformRcPtr> &data)
  252. {
  253. PyObject* returnlist = PyList_New( data.size() );
  254. if(!returnlist) return 0;
  255. for(unsigned int i =0; i<data.size(); ++i)
  256. {
  257. PyList_SET_ITEM(returnlist, i, BuildConstPyTransform(data[i]));
  258. }
  259. return returnlist;
  260. }
  261. PyObject* CreatePyDictFromStringMap(const std::map<std::string, std::string> &data)
  262. {
  263. PyObject* returndict = PyDict_New();
  264. if(!returndict) return 0;
  265. std::map<std::string, std::string>::const_iterator iter;
  266. for(iter = data.begin(); iter != data.end(); ++iter)
  267. {
  268. int ret = PyDict_SetItem(returndict,
  269. PyString_FromString(iter->first.c_str()),
  270. PyString_FromString(iter->second.c_str()));
  271. if(ret)
  272. {
  273. Py_DECREF(returndict);
  274. return NULL;
  275. }
  276. }
  277. return returndict;
  278. }
  279. namespace
  280. {
  281. // These are safer than PySequence_Fast, as we can
  282. // Confirm that no exceptions will be set in the python runtime.
  283. inline bool PyListOrTuple_Check(PyObject* pyobj)
  284. {
  285. return (PyTuple_Check(pyobj) || PyList_Check(pyobj));
  286. }
  287. inline int PyListOrTuple_GET_SIZE(PyObject* pyobj)
  288. {
  289. if(PyList_Check(pyobj))
  290. {
  291. return static_cast<int>(PyList_GET_SIZE(pyobj));
  292. }
  293. else if(PyTuple_Check(pyobj))
  294. {
  295. return static_cast<int>(PyTuple_GET_SIZE(pyobj));
  296. }
  297. return -1;
  298. }
  299. // Return a boworrowed reference
  300. inline PyObject* PyListOrTuple_GET_ITEM(PyObject* pyobj, int index)
  301. {
  302. if(PyList_Check(pyobj))
  303. {
  304. return PyList_GET_ITEM(pyobj, index);
  305. }
  306. else if(PyTuple_Check(pyobj))
  307. {
  308. return PyTuple_GET_ITEM(pyobj, index);
  309. }
  310. return 0;
  311. }
  312. }
  313. ///////////////////////////////////////////////////////////////////////////
  314. /*
  315. A note on why PyErr_Clear is needed in multiple locations...
  316. Even though it's not immediately apparent, almost every function
  317. in the Abstract Objects Layer,
  318. http://www.python.org/doc/2.5/api/abstract.html
  319. can set a global excpetion under certain circumstances.
  320. For example, calling the equivalent of int( obj ) will set
  321. an exception if the object cannot be casted (such as None),
  322. or if it's a custom type that implements the number protocol
  323. but throws an exception during the cast.
  324. During iteration, even an object that implements the sequence
  325. protocol can raise an exception if the iteration fails.
  326. As we want to guarantee that an exception *never* remains on
  327. the stack after an internal failure, the simplest way to
  328. guarantee this is to always call PyErr_Clear() before
  329. returing the failure condition.
  330. */
  331. bool FillIntVectorFromPySequence(PyObject* datalist, std::vector<int> &data)
  332. {
  333. data.clear();
  334. // First, try list or tuple iteration (for speed).
  335. if(PyListOrTuple_Check(datalist))
  336. {
  337. int sequenceSize = PyListOrTuple_GET_SIZE(datalist);
  338. data.reserve(sequenceSize);
  339. for(int i=0; i < sequenceSize; i++)
  340. {
  341. PyObject* item = PyListOrTuple_GET_ITEM(datalist, i);
  342. int val;
  343. if (!GetIntFromPyObject(item, &val))
  344. {
  345. data.clear();
  346. return false;
  347. }
  348. data.push_back(val);
  349. }
  350. return true;
  351. }
  352. // As a fallback, try general iteration.
  353. else
  354. {
  355. PyObject *item;
  356. PyObject *iter = PyObject_GetIter(datalist);
  357. if (iter == NULL)
  358. {
  359. PyErr_Clear();
  360. return false;
  361. }
  362. while((item = PyIter_Next(iter)) != NULL)
  363. {
  364. int val;
  365. if (!GetIntFromPyObject(item, &val))
  366. {
  367. Py_DECREF(item);
  368. Py_DECREF(iter);
  369. data.clear();
  370. return false;
  371. }
  372. data.push_back(val);
  373. Py_DECREF(item);
  374. }
  375. Py_DECREF(iter);
  376. if (PyErr_Occurred())
  377. {
  378. PyErr_Clear();
  379. data.clear();
  380. return false;
  381. }
  382. return true;
  383. }
  384. }
  385. bool FillFloatVectorFromPySequence(PyObject* datalist, std::vector<float> &data)
  386. {
  387. data.clear();
  388. if(PyListOrTuple_Check(datalist))
  389. {
  390. int sequenceSize = PyListOrTuple_GET_SIZE(datalist);
  391. data.reserve(sequenceSize);
  392. for(int i=0; i < sequenceSize; i++)
  393. {
  394. PyObject* item = PyListOrTuple_GET_ITEM(datalist, i);
  395. float val;
  396. if (!GetFloatFromPyObject(item, &val))
  397. {
  398. data.clear();
  399. return false;
  400. }
  401. data.push_back(val);
  402. }
  403. return true;
  404. }
  405. else
  406. {
  407. PyObject *item;
  408. PyObject *iter = PyObject_GetIter(datalist);
  409. if (iter == NULL)
  410. {
  411. PyErr_Clear();
  412. return false;
  413. }
  414. while((item = PyIter_Next(iter)) != NULL)
  415. {
  416. float val;
  417. if (!GetFloatFromPyObject(item, &val))
  418. {
  419. Py_DECREF(item);
  420. Py_DECREF(iter);
  421. data.clear();
  422. return false;
  423. }
  424. data.push_back(val);
  425. Py_DECREF(item);
  426. }
  427. Py_DECREF(iter);
  428. if (PyErr_Occurred())
  429. {
  430. PyErr_Clear();
  431. data.clear();
  432. return false;
  433. }
  434. return true;
  435. }
  436. }
  437. bool FillDoubleVectorFromPySequence(PyObject* datalist, std::vector<double> &data)
  438. {
  439. data.clear();
  440. if(PyListOrTuple_Check(datalist))
  441. {
  442. int sequenceSize = PyListOrTuple_GET_SIZE(datalist);
  443. data.reserve(sequenceSize);
  444. for(int i=0; i < sequenceSize; i++)
  445. {
  446. PyObject* item = PyListOrTuple_GET_ITEM(datalist, i);
  447. double val;
  448. if (!GetDoubleFromPyObject(item, &val))
  449. {
  450. data.clear();
  451. return false;
  452. }
  453. data.push_back( val );
  454. }
  455. return true;
  456. }
  457. else
  458. {
  459. PyObject *item;
  460. PyObject *iter = PyObject_GetIter(datalist);
  461. if (iter == NULL)
  462. {
  463. PyErr_Clear();
  464. return false;
  465. }
  466. while((item = PyIter_Next(iter)) != NULL)
  467. {
  468. double val;
  469. if (!GetDoubleFromPyObject(item, &val))
  470. {
  471. Py_DECREF(item);
  472. Py_DECREF(iter);
  473. data.clear();
  474. return false;
  475. }
  476. data.push_back(val);
  477. Py_DECREF(item);
  478. }
  479. Py_DECREF(iter);
  480. if (PyErr_Occurred())
  481. {
  482. PyErr_Clear();
  483. data.clear();
  484. return false;
  485. }
  486. return true;
  487. }
  488. }
  489. bool FillStringVectorFromPySequence(PyObject* datalist, std::vector<std::string> &data)
  490. {
  491. data.clear();
  492. if(PyListOrTuple_Check(datalist))
  493. {
  494. int sequenceSize = PyListOrTuple_GET_SIZE(datalist);
  495. data.reserve(sequenceSize);
  496. for(int i=0; i < sequenceSize; i++)
  497. {
  498. PyObject* item = PyListOrTuple_GET_ITEM(datalist, i);
  499. std::string val;
  500. if (!GetStringFromPyObject(item, &val))
  501. {
  502. data.clear();
  503. return false;
  504. }
  505. data.push_back( val );
  506. }
  507. return true;
  508. }
  509. else
  510. {
  511. PyObject *item;
  512. PyObject *iter = PyObject_GetIter(datalist);
  513. if (iter == NULL)
  514. {
  515. PyErr_Clear();
  516. return false;
  517. }
  518. while((item = PyIter_Next(iter)) != NULL)
  519. {
  520. std::string val;
  521. if (!GetStringFromPyObject(item, &val))
  522. {
  523. Py_DECREF(item);
  524. Py_DECREF(iter);
  525. data.clear();
  526. return false;
  527. }
  528. data.push_back(val);
  529. Py_DECREF(item);
  530. }
  531. Py_DECREF(iter);
  532. if (PyErr_Occurred())
  533. {
  534. PyErr_Clear();
  535. data.clear();
  536. return false;
  537. }
  538. return true;
  539. }
  540. }
  541. bool FillTransformVectorFromPySequence(PyObject* datalist, std::vector<ConstTransformRcPtr> &data)
  542. {
  543. data.clear();
  544. if(PyListOrTuple_Check(datalist))
  545. {
  546. int sequenceSize = PyListOrTuple_GET_SIZE(datalist);
  547. data.reserve(sequenceSize);
  548. for(int i=0; i < sequenceSize; i++)
  549. {
  550. PyObject* item = PyListOrTuple_GET_ITEM(datalist, i);
  551. ConstTransformRcPtr val;
  552. try
  553. {
  554. val = GetConstTransform(item, true);
  555. }
  556. catch(...)
  557. {
  558. data.clear();
  559. return false;
  560. }
  561. data.push_back( val );
  562. }
  563. return true;
  564. }
  565. else
  566. {
  567. PyObject *item;
  568. PyObject *iter = PyObject_GetIter(datalist);
  569. if (iter == NULL)
  570. {
  571. PyErr_Clear();
  572. return false;
  573. }
  574. while((item = PyIter_Next(iter)) != NULL)
  575. {
  576. ConstTransformRcPtr val;
  577. try
  578. {
  579. val = GetConstTransform(item, true);
  580. }
  581. catch(...)
  582. {
  583. Py_DECREF(item);
  584. Py_DECREF(iter);
  585. data.clear();
  586. return false;
  587. }
  588. data.push_back(val);
  589. Py_DECREF(item);
  590. }
  591. Py_DECREF(iter);
  592. if (PyErr_Occurred())
  593. {
  594. PyErr_Clear();
  595. data.clear();
  596. return false;
  597. }
  598. return true;
  599. }
  600. }
  601. ///////////////////////////////////////////////////////////////////////////
  602. ///////////////////////////////////////////////////////////////////////////
  603. /* See the header for the justification for this function.
  604. The trick to making this technique work is that we know
  605. we've been called from within a catch block, so there
  606. is an exception on the stack. We can re-throw this
  607. exception, using the throw statement. By doing this
  608. inside a try...catch block, it's possible to use the
  609. standard catch mechanism to categorize whatever exception
  610. was actually caught by the caller.
  611. */
  612. void Python_Handle_Exception()
  613. {
  614. try
  615. {
  616. // Re-throw whatever exception is already on the stack.
  617. // This will fail horribly if no exception is already
  618. // on the stack, so this function must only be called
  619. // from inside an exception handler catch block!
  620. throw;
  621. }
  622. catch (ExceptionMissingFile & e)
  623. {
  624. PyErr_SetString(GetExceptionMissingFilePyType(), e.what());
  625. }
  626. catch (Exception & e)
  627. {
  628. PyErr_SetString(GetExceptionPyType(), e.what());
  629. }
  630. catch (std::exception& e)
  631. {
  632. PyErr_SetString(PyExc_RuntimeError, e.what());
  633. }
  634. catch (...)
  635. {
  636. PyErr_SetString(PyExc_RuntimeError, "Unknown C++ exception caught.");
  637. }
  638. }
  639. }
  640. OCIO_NAMESPACE_EXIT