NCBI C++ Toolkit Cross Reference

  C++/src/dbapi/lang_bind/python/pythonpp/pythonpp_ext.hpp


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 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
/* $Id: pythonpp_ext.hpp 60029 2013-10-17 18:30:19Z ucko $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Sergey Sikorskiy * * File Description: Tiny Python API wrappers * * Status: *Initial* * * =========================================================================== */ #ifndef PYTHONPP_EXT_H #define PYTHONPP_EXT_H #include "pythonpp_seq.hpp" #include "pythonpp_dict.hpp" BEGIN_NCBI_SCOPE namespace pythonpp { extern "C" { typedef PyObject* (*TMethodVarArgsHandler)( PyObject* self, PyObject* args ); typedef PyObject* (*TMethodKeywordHandler)( PyObject* self, PyObject* args, PyObject* dict ); } ////////////////////////////////////////////////////////////////////////// /// Introduces constructor methods for a python PyMethodDef structure ... struct SMethodDef : public PyMethodDef { public: SMethodDef(void) { ml_name = 0; ml_meth = 0; ml_flags = 0; ml_doc = 0; } SMethodDef(const char* name, PyCFunction func, int flags = 1, const char* doc = NULL) { ml_name = const_cast<char*>( name ); ml_meth = func; ml_flags = flags; ml_doc = const_cast<char*>( doc ); } SMethodDef(const SMethodDef& other) { ml_name = other.ml_name; ml_meth = other.ml_meth; ml_flags = other.ml_flags; ml_doc = other.ml_doc; } SMethodDef& operator = (const SMethodDef& other) { if (this != &other) { ml_name = other.ml_name; ml_meth = other.ml_meth; ml_flags = other.ml_flags; ml_doc = other.ml_doc; } return *this; } }; extern "C" void DoNotDeallocate( void* ) { } ////////////////////////////////////////////////////////////////////////// extern "C" void standard_dealloc( PyObject* obj ) { PyMem_DEL( obj ); } ////////////////////////////////////////////////////////////////////////// class CExtObjectBase; // PyTypeObject is inherited from a PyVarObject (C-kind of inheritance) ... class CExtType : public PyTypeObject { public: CExtType( size_t basic_size, destructor dr = standard_dealloc, PyTypeObject* base = &PyBaseObject_Type) { BasicInit(); ob_type = &PyType_Type; tp_basicsize = basic_size; tp_dealloc = dr; // Py_TPFLAGS_BASETYPE - means that the type is subtypable ... tp_flags = Py_TPFLAGS_DEFAULT; tp_base = base; // tp_bases = ??? // It should be NULL for statically defined types. // Finalize the type object including setting type of the new type // object; doing it here is required for portability to Windows // without requiring C++. // !!! This code does not work currently !!! // if ( PyType_Ready(this) == -1 ) { // throw CError("Cannot initialyze a type object"); // } } public: PyTypeObject* GetObjType( void ) { return this; } const PyTypeObject* GetObjType( void ) const { return this; } void SetName( const char* name ) { tp_name = const_cast<char*>( name ); } void SetDescription( const char* descr ) { tp_doc = const_cast<char*>( descr ); } public: void SupportGetAttr( getattrfunc func ) { tp_getattr = func; } private: void BasicInit( void ) { #ifdef Py_TRACE_REFS _ob_next = NULL; _ob_prev = NULL; #endif ob_refcnt = 1; ob_type = NULL; ob_size = 0; tp_name = NULL; tp_basicsize = 0; tp_itemsize = 0; // Methods to implement standard operations tp_dealloc = NULL; tp_print = NULL; tp_getattr = NULL; // This field is deprecated. tp_setattr = NULL; // This field is deprecated. tp_compare = NULL; tp_repr = NULL; // Method suites for standard classes tp_as_number = NULL; tp_as_sequence = NULL; tp_as_mapping = NULL; // More standard operations (here for binary compatibility) tp_hash = NULL; tp_call = NULL; tp_str = NULL; tp_getattro = NULL; tp_setattro = NULL; // Functions to access object as input/output buffer tp_as_buffer = NULL; // Flags to define presence of optional/expanded features tp_flags = 0L; // Documentation string tp_doc = NULL; // call function for all accessible objects tp_traverse = 0L; // delete references to contained objects tp_clear = 0L; // rich comparisons tp_richcompare = 0L; // weak reference enabler tp_weaklistoffset = 0L; // Iterators tp_iter = 0L; tp_iternext = 0L; // Attribute descriptor and subclassing stuff tp_methods = NULL; // Object's method table ( same as retturned by GetMethodHndlList() ) tp_members = NULL; tp_getset = NULL; tp_base = NULL; tp_dict = NULL; tp_descr_get = NULL; tp_descr_set = NULL; tp_dictoffset = 0; tp_init = NULL; tp_alloc = NULL; tp_new = NULL; tp_free = NULL; // Low-level free-memory routine tp_is_gc = NULL; // For PyObject_IS_GC tp_bases = NULL; tp_mro = NULL; // method resolution order tp_cache = NULL; tp_subclasses = NULL; tp_weaklist = NULL; tp_del = NULL; #ifdef COUNT_ALLOCS tp_alloc = 0; tp_free = 0; tp_maxalloc = 0; tp_next = 0; #endif } }; inline bool operator ==(const CObject& l, const CExtType& r) { return l.GetObjType() == r.GetObjType(); } inline bool operator ==(const CExtType& l, const CObject& r) { return l.GetObjType() == r.GetObjType(); } ////////////////////////////////////////////////////////////////////////// namespace bind { enum EBindType { eReadOnly, eReadWrite }; class CBase { public: CBase( EBindType type = eReadOnly ) : m_Type( type ) { } virtual ~CBase(void) { } public: bool IsReadOnly(void) const { return m_Type == eReadOnly; } virtual PyObject* Get(void) const = 0; void Set( PyObject* value ) const { if ( IsReadOnly() ) { throw CAttributeError("Read-only property"); } SetInternal( value ); } protected: virtual void SetInternal( PyObject* value ) const { } private: EBindType m_Type; }; class CLong : public CBase { public: CLong(long& value, EBindType type = eReadOnly ) : CBase( type ) , m_Value( &value ) { } virtual ~CLong(void) { } public: virtual PyObject* Get(void) const { return PyLong_FromLong( *m_Value ); } protected: virtual void SetInternal( PyObject* value ) const { long tmp_value = PyLong_AsLong( value ); CError::Check(); *m_Value = tmp_value; } private: long* const m_Value; }; class CString : public CBase { public: CString(string& value, EBindType type = eReadOnly ) : CBase( type ) , m_Value( &value ) { } virtual ~CString(void) { } public: virtual PyObject* Get(void) const { return PyString_FromStringAndSize( m_Value->data(), m_Value->size() ); } protected: virtual void SetInternal( PyObject* value ) const { string tmp_value = string( PyString_AsString( value ), static_cast<size_t>( PyString_Size( value ) ) ); CError::Check(); *m_Value = tmp_value; } private: string* const m_Value; }; template <class T> class CObject : public CBase { public: typedef PyObject* (T::*TGetFunc)( void ) const; typedef void (T::*TSetFunc)( PyObject* value ); public: CObject( T& obj, TGetFunc get, TSetFunc set = NULL ) : CBase( set == NULL ? eReadOnly : eReadWrite ) , m_Obj( &obj ) , m_GetFunc( get ) , m_SetFunc( set ) { } virtual ~CObject(void) { } public: virtual PyObject* Get(void) const { PyObject* obj = (m_Obj->*m_GetFunc)(); Py_INCREF(obj); return obj; } protected: virtual void SetInternal( PyObject* value ) const { (m_Obj->*m_SetFunc)( value ); } private: T* const m_Obj; TGetFunc m_GetFunc; TSetFunc m_SetFunc; }; } ////////////////////////////////////////////////////////////////////////// template<size_t N> inline void resize(vector<SMethodDef>& container) { if (container.size() < N) { container.resize(N); } } template<> inline void resize<0>(vector<SMethodDef>& /*container*/) { ; } ////////////////////////////////////////////////////////////////////////// template <class T> class CExtObject : public PyObject { public: CExtObject(void) { // This is not an appropriate place for initialization .... // PyObject_INIT( this, GetType().GetPyTypeObject() ); } ~CExtObject(void) { } public: typedef CObject (T::*TMethodVarArgsFunc)( const CTuple& args ); /// Workaround for GCC 2.95 struct SFunct { SFunct(const TMethodVarArgsFunc& funct) : m_Funct(funct) { } operator TMethodVarArgsFunc(void) const { return m_Funct; } TMethodVarArgsFunc m_Funct; }; // A helper class for generating functions ... template<size_t N = 0> class CClass { public: CClass(void) { // Finalyze a method definition ... GetMethodHndlList().push_back( SMethodDef() ); } public: CClass<N + 1> Def(const char* name, TMethodVarArgsFunc func, const char* doc = 0) { TMethodHndlList& hndl_list = GetMethodHndlList(); // Prepare data for python ... resize<N>(hndl_list); hndl_list[ePosition] = SMethodDef(name, (PyCFunction)HandleMethodVarArgs, METH_VARARGS, doc); // Prepare data for our handler ... GetMethodList().push_back(func); return CClass<N + 1>(); } static PyObject* HandleMethodVarArgs( PyObject* self, PyObject* args ) { const TMethodVarArgsFunc func = GetMethodList()[ePosition]; T* obj = static_cast<T*>(self); try { const CTuple args_tuple( args ); return IncRefCount((obj->*func)(args_tuple)); } /* CException is not defined here. We must translate all CException * to CError in user's code. catch (const CException& e) { CError::SetString(e.what()); } */ catch(const CError&) { // An error message is already set by a previosly raised exception ... return NULL; } catch(...) { CError::SetString("Unknown error during executing of a method"); } // NULL means "error". return NULL; } private: enum EPosition {ePosition = N}; }; #ifndef NCBI_COMPILER_ICC // ICC 10 considers this declaration to be an error, and previous // versions warn about it; all versions are happy to do without it. template <size_t N> friend class CClass; #endif static void Declare( const char* name, const char* descr = 0, PyTypeObject* base = &PyBaseObject_Type ) { _ASSERT(sm_Base == NULL); sm_Base = base; CExtType& type = GetType(); type.SetName(name); if ( descr ) { type.SetDescription(descr); } type.SupportGetAttr(GetAttrImpl); if (GetMethodHndlList().size() <= GetMethodList().size()) GetMethodHndlList().resize(GetMethodList().size() + 1); type.tp_methods = &GetMethodHndlList().front(); } static void Declare( const string& name, const char* descr = 0, PyTypeObject* base = &PyBaseObject_Type ) { Declare(name.c_str(), descr, base); } static CClass<1> Def(const char* name, TMethodVarArgsFunc func, const char* doc = 0) { return CClass<0>().Def(name, func, doc); } public: // Return a python object type. static CExtType& GetType(void) { _ASSERT(sm_Base != NULL); static CExtType obj_type( sizeof(T), deallocator, sm_Base ); return obj_type; } static CObject& GetTypeObject(void) { static CObject obj((PyObject*)&GetType(), pythonpp::eAcquireOwnership); // NCBI_FAKE_WARNING return obj; } // Just delete an object ... static void deallocator ( PyObject* obj ) { delete static_cast<T*>( obj ); } protected: static void PrepareForPython(CExtObject<T>* self) { // Borrowed reference. PyObject_Init( self, GetType().GetObjType() ); } protected: typedef map<string, AutoPtr<bind::CBase> > TAttrList; TAttrList m_AttrList; void ROAttr( const string& name, long& value ) { m_AttrList[ name ] = new bind::CLong( value ); } void ROAttr( const string& name, string& value ) { m_AttrList[ name ] = new bind::CString( value ); } void ROAttr( const string& name, CObject& value ) { m_AttrList[ name ] = new bind::CObject<CObject>( value, &CObject::Get ); } void RWAttr( const string& name, long& value ) { m_AttrList[ name ] = new bind::CLong( value, bind::eReadWrite ); } void RWAttr( const string& name, string& value ) { m_AttrList[ name ] = new bind::CString( value, bind::eReadWrite ); } private: static PyTypeObject* sm_Base; private: typedef vector<SMethodDef> TMethodHndlList; static TMethodHndlList sm_MethodHndlList; static TMethodHndlList& GetMethodHndlList(void) { return sm_MethodHndlList; } static PyObject* GetAttrImpl( PyObject* self, char* name ) { _ASSERT( self != NULL ); CExtObject<T>* obj_ptr = static_cast<CExtObject<T>* >( self ); TAttrList::const_iterator citer = obj_ptr->m_AttrList.find( name ); if ( citer != obj_ptr->m_AttrList.end() ) { // Return an attribute value ... return citer->second->Get(); } // Classic python implementation ... // It will do a linear search within the m_MethodHndlList table ... return Py_FindMethod( &GetMethodHndlList().front(), self, name ); } private: typedef vector<SFunct> TMethodList; static TMethodList sm_MethodList; static TMethodList& GetMethodList(void) { return sm_MethodList; } }; template <class T> PyTypeObject* CExtObject<T>::sm_Base = NULL; template <class T> typename CExtObject<T>::TMethodHndlList CExtObject<T>::sm_MethodHndlList; template <class T> typename CExtObject<T>::TMethodList CExtObject<T>::sm_MethodList; ////////////////////////////////////////////////////////////////////////// template <class T> class CExtModule : public PyObject { public: CExtModule(const char* name, const char* descr = 0) : m_Module(NULL) { m_Module = Py_InitModule4( const_cast<char*>(name), &GetMethodHndlList().front(), const_cast<char*>(descr), this, PYTHON_API_VERSION); if ( !m_Module ) { throw CSystemError("Cannot initialize module"); } } ~CExtModule(void) { } public: typedef CObject (T::*TMethodVarArgsFunc)( const CTuple& args ); /// Workaround for GCC 2.95 struct SFunct { SFunct(const TMethodVarArgsFunc& funct) : m_Funct(funct) { } operator TMethodVarArgsFunc(void) const { return m_Funct; } TMethodVarArgsFunc m_Funct; }; // A helper class for generating functions ... template<size_t N = 0> class CClass { public: CClass(void) { // Finalyze method definition ... GetMethodHndlList().push_back( SMethodDef() ); } public: CClass<N + 1> Def(const char* name, TMethodVarArgsFunc func, const char* doc = 0) { // Prepare data for python ... GetMethodHndlList()[ePosition] = SMethodDef(name, HandleMethodVarArgs, METH_VARARGS, doc); // Prepare data for our handler ... GetMethodList().push_back(func); return CClass<N + 1>(); } static PyObject* HandleMethodVarArgs( PyObject* self, PyObject* args ) { const TMethodVarArgsFunc func = GetMethodList()[ePosition]; T* obj = static_cast<T*>(self); try { const CTuple args_tuple( args ); return IncRefCount((obj->*func)(args_tuple)); } catch(const CError&) { // An error message is already set by an exception ... return NULL; } catch(...) { CError::SetString("Unknown error during executing of a method"); } // NULL means "error". return NULL; } private: enum {ePosition = N}; }; static CClass<1> Def(const char* name, TMethodVarArgsFunc func, const char* doc = 0) { return CClass<0>().Def(name, func, doc); } public: // Return a python object type. static CExtType& GetType(void) { static CExtType obj_type( sizeof(T), deallocator ); return obj_type; } // Just delete an object ... static void deallocator ( PyObject* obj ) { delete static_cast<T*>( obj ); } protected: static void PrepareForPython(CExtModule<T>* self) { // Borrowed reference. PyObject_Init( self, GetType().GetPyTypeObject() ); } private: typedef vector<SMethodDef> TMethodHndlList; static TMethodHndlList sm_MethodHndlList; static TMethodHndlList& GetMethodHndlList(void) { return sm_MethodHndlList; } static PyObject* GetAttrImpl( PyObject* self, char* name ) { // Classic python implementation ... // It will do a linear search within the m_MethodHndlList table ... return Py_FindMethod(&GetMethodHndlList().front(), self, name); } private: typedef vector<SFunct> TMethodList; static TMethodList sm_MethodList; static TMethodList& GetMethodList(void) { return sm_MethodList; } private: PyObject* m_Module; }; template <class T> typename CExtModule<T>::TMethodHndlList CExtModule<T>::sm_MethodHndlList; template <class T> typename CExtModule<T>::TMethodList CExtModule<T>::sm_MethodList; /////////////////////////////////////////////////////////////////////////////// // New development /////////////////////////////////////////////////////////////////////////////// // An attempt to wrap a module ... class CModuleExt { public: static void Declare(const string& name, PyMethodDef* methods); public: static const string& GetName(void) { return m_Name; } static PyObject* GetPyModule(void) { return m_Module; } static void AddConstValue( const string& name, PyObject* value ); static void AddConst( const string& name, const string& value ); static void AddConst( const string& name, long value ); private: static string m_Name; static PyObject* m_Module; }; string CModuleExt::m_Name; PyObject* CModuleExt::m_Module = NULL; void CModuleExt::Declare(const string& name, PyMethodDef* methods) { _ASSERT( m_Module == NULL ); m_Name = name; m_Module = Py_InitModule( const_cast<char*>( name.c_str() ), methods ); CError::Check(m_Module); } void CModuleExt::AddConstValue( const string& name, PyObject* value ) { CError::Check( value ); if ( PyModule_AddObject(m_Module, const_cast<char*>(name.c_str()), value ) == -1 ) { throw CSystemError("Failed to add a constant value to a module"); } } void CModuleExt::AddConst( const string& name, const string& value ) { PyObject* py_value = PyString_FromStringAndSize( value.data(), value.size() ); CError::Check( py_value ); AddConstValue( name, py_value ); } void CModuleExt::AddConst( const string& name, long value ) { PyObject* py_value = PyLong_FromLong( value ); CError::Check( py_value ); AddConstValue( name, py_value ); } /////////////////////////////////// // User-defined exceptions /////////////////////////////////// template <class T, class B = CStandardError> class CUserError : public B { public: CUserError(void) { } CUserError(const string& msg) : B( msg, GetPyException() ) { } protected: CUserError(const string& msg, PyObject* err_type) : B(msg, err_type) { } public: static void Declare(const string& name) { _ASSERT( m_Exception == NULL ); _ASSERT( CModuleExt::GetPyModule() ); const string full_name = CModuleExt::GetName() + "." + name; m_Exception = PyErr_NewException( const_cast<char*>(full_name.c_str()), B::GetPyException(), NULL ); CError::Check( m_Exception ); if ( PyModule_AddObject( CModuleExt::GetPyModule(), const_cast<char*>(name.c_str()), m_Exception ) == -1 ) { throw CSystemError( "Unable to add an object to a module" ); } } static void Declare(const string& name, const pythonpp::CDict& dict) { _ASSERT( m_Exception == NULL ); _ASSERT( CModuleExt::GetPyModule() ); const string full_name = CModuleExt::GetName() + "." + name; m_Exception = PyErr_NewException(const_cast<char*>(full_name.c_str()), B::GetPyException(), dict); CError::Check( m_Exception ); if ( PyModule_AddObject( CModuleExt::GetPyModule(), const_cast<char*>(name.c_str()), m_Exception ) == -1 ) { throw CSystemError( "Unable to add an object to a module" ); } } public: static PyObject* GetPyException(void) { _ASSERT( m_Exception ); return m_Exception; } private: static PyObject* m_Exception; }; template <class T, class B> PyObject* CUserError<T, B>::m_Exception(NULL); /// CThreadingGuard -- "Anti-guard" for Python's global interpreter /// lock, which it temporarily releases to allow other threads to /// proceed in parallel with blocking operations that don't involve /// Python-specific state. /// @note Does NOT support recursive usage at present. class CThreadingGuard { public: CThreadingGuard() : m_State(sm_MayRelease ? PyEval_SaveThread() : NULL) { } ~CThreadingGuard() { if (m_State != NULL) PyEval_RestoreThread(m_State); } static void SetMayRelease(bool may_release) { sm_MayRelease = may_release; } private: static bool sm_MayRelease; PyThreadState* m_State; }; #ifdef PYTHONPP_DEFINE_GLOBALS bool CThreadingGuard::sm_MayRelease = false; #endif } // namespace pythonpp END_NCBI_SCOPE #endif // PYTHONPP_EXT_H

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.