1

我试图了解一些代码(直接从 PyCXX 改编)。它是一个多平台 C++ Python 包装器。

编辑:这里的原始代码。

它似乎迎合了一些只存在于 Windows 中的特殊现象:

#ifdef PY_WIN32_DELAYLOAD_PYTHON_DLL
:
#else
:
#endif

我将在下面给出完整的文件列表,它很长。

这个 PY_WIN32_DELAYLOAD_PYTHON_DLL 令牌在 CPython 中不存在,它也没有在 PyCXX 中定义。因此我只能想象 PyCXX 打算将它作为可选的编译器标志提供。

我想知道的是:它的目的是什么?它在解决什么问题?为什么这种机制甚至存在?

也许熟悉Windows编程的人可以从代码中弄清楚?

我想知道它正在解决的问题是否仍然存在于现代 Windows 中,因为代码已经超过 15 年了。

关键问题是:我可以删除它,还是用更清洁的东西代替它?

我非常想把它剪掉;但它在现代 Windows 环境中仍然有一些有用的用途吗?

代码:

#include "Base.hxx" //"IndirectPythonInterface.hxx"

namespace Py
{

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

#ifdef PY_WIN32_DELAYLOAD_PYTHON_DLL

#   ifndef MS_WINDOWS
#       error "Can only delay load under Win32"
#   endif
#   include <windows.h> // !!! Is it possible we ever want Windows.h but no delay load? If so, this is wrong...

static HMODULE python_dll;

#   define DECLARE_ERROR( THE_ERROR ) \
        static PyObject* ptr__Exc_##THE_ERROR = nullptr;
ALL_ERRORS( DECLARE_ERROR )


static PyObject* ptr__PyNone        = nullptr;
static PyObject* ptr__PyFalse       = nullptr;
static PyObject* ptr__PyTrue        = nullptr;

#   define DECLARE_TYPE( T ) \
        static PyTypeObject* ptr__##T##_Type = nullptr;
ALL_TYPES( DECLARE_TYPE )


static int* ptr_Py_DebugFlag        = nullptr;
static int* ptr_Py_InteractiveFlag  = nullptr;
static int* ptr_Py_OptimizeFlag     = nullptr;
static int* ptr_Py_NoSiteFlag       = nullptr;
static int* ptr_Py_VerboseFlag      = nullptr;

static char** ptr__Py_PackageContext = nullptr;

#   ifdef Py_REF_DEBUG
int* ptr_Py_RefTotal; // !!! Why not static?
#   endif


//--------------------------------------------------------------------------------
class GetAddressException
{
public:
    GetAddressException( const char* _name )
        : name( _name )
    { }
    virtual ~GetAddressException() { }
    const char* name;
};


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

#   define GET_PTR( FUNC, RETURN_TYPE ) \
        static FUNC( const char *name ) \
            { \
                FARPROC addr = GetProcAddress( python_dll, name ); \
                if( addr == nullptr ) \
                    throw GetAddressException( name ); \
                \
                return RETURN_TYPE addr; \
            }

GET_PTR( PyObject *             GetPyObjectPointer_As_PyObjectPointer       , *(PyObject **)        )
GET_PTR( PyObject *                    GetPyObject_As_PyObjectPointer       ,  (PyObject *)         )
GET_PTR( PyTypeObject *     GetPyTypeObjectPointer_As_PyTypeObjectPointer   , *(PyTypeObject**)     )
GET_PTR( PyTypeObject *            GetPyTypeObject_As_PyTypeObjectPointer   ,  (PyTypeObject*)      )
GET_PTR( int *                              GetInt_as_IntPointer            ,  (int*)               )
GET_PTR( char **                    GetCharPointer_as_CharPointerPointer    ,  (char**)             )


#   ifdef _DEBUG
static const char python_dll_name_format[] = "PYTHON%1.1d%1.1d_D.DLL";
#   else
static const char python_dll_name_format[] = "PYTHON%1.1d%1.1d.DLL";
#   endif

//--------------------------------------------------------------------------------
bool InitialisePythonIndirectInterface()
{
    char python_dll_name[sizeof(python_dll_name_format)];

    _snprintf( python_dll_name, sizeof(python_dll_name_format) / sizeof(char) - 1, python_dll_name_format, PY_MAJOR_VERSION, PY_MINOR_VERSION );

    python_dll = LoadLibraryA( python_dll_name );
    if( python_dll == nullptr )
        return false;

    try
    {
#   ifdef Py_REF_DEBUG
        ptr_Py_RefTotal             = GetInt_as_IntPointer( "_Py_RefTotal" );
#   endif
        ptr_Py_DebugFlag            = GetInt_as_IntPointer( "Py_DebugFlag" );
        ptr_Py_InteractiveFlag      = GetInt_as_IntPointer( "Py_InteractiveFlag" );
        ptr_Py_OptimizeFlag         = GetInt_as_IntPointer( "Py_OptimizeFlag" );
        ptr_Py_NoSiteFlag           = GetInt_as_IntPointer( "Py_NoSiteFlag" );
        ptr_Py_VerboseFlag          = GetInt_as_IntPointer( "Py_VerboseFlag" );
        ptr__Py_PackageContext      = GetCharPointer_as_CharPointerPointer( "_Py_PackageContext" );

#   define ASSIGN_PTR( E ) \
        ptr__Exc_##E    = GetPyObjectPointer_As_PyObjectPointer( "PyExc_" #E );
        ALL_ERRORS( ASSIGN_PTR )

        ptr__PyNone                 = GetPyObject_As_PyObjectPointer( "_Py_NoneStruct" );
        ptr__PyFalse                = GetPyObject_As_PyObjectPointer( "_Py_ZeroStruct" );
        ptr__PyTrue                 = GetPyObject_As_PyObjectPointer( "_Py_TrueStruct" );

#   define MAKE_PTR( TYPENAME ) \
        ptr__##TYPENAME##_Type      = GetPyTypeObject_As_PyTypeObjectPointer( "Py" #TYPENAME "_Type" );
        ALL_TYPES( MAKE_PTR )
    }
    catch( GetAddressException &e )
    {
        OutputDebugStringA( python_dll_name );
        OutputDebugStringA( " does not contain symbol " );
        OutputDebugStringA( e.name );
        OutputDebugStringA( "\n" );

        return false;
    }

    return true;
}


//#if 0
//#define Py_INCREF(op) (                         \
//    _Py_INC_REFTOTAL  _Py_REF_DEBUG_COMMA       \
//    ((PyObject*)(op))->ob_refcnt++)
//
//#define Py_DECREF(op)                           \
//    if (_Py_DEC_REFTOTAL  _Py_REF_DEBUG_COMMA   \
//        --((PyObject*)(op))->ob_refcnt != 0)    \
//        _Py_CHECK_REFCNT(op)                    \
//    else                                        \
//        _Py_Dealloc((PyObject *)(op))
//#endif

void _XINCREF( PyObject* op )
{
    // This function must match the contents of Py_XINCREF(op)
    if( op == nullptr )
        return;

#   ifdef Py_REF_DEBUG
    (*ptr_Py_RefTotal)++;
#   endif
    (op)->ob_refcnt++;

}

void _XDECREF( PyObject* op )
{
    // This function must match the contents of Py_XDECREF(op);
    if( op == nullptr )
        return;

#   ifdef Py_REF_DEBUG
    (*ptr_Py_RefTotal)--;
#   endif

    if (--(op)->ob_refcnt == 0)
        _Py_Dealloc((PyObject *)(op));
}


#else // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


//
//    Needed to keep the abstactions for delayload interface
//
// !!! π Py_XDECREF has been deprecated in favour of Py_CLEAR

void _XINCREF( PyObject* op )
{
    Py_XINCREF( op );
}

void _XDECREF( PyObject* op )
{
    Py_XDECREF( op );
}

#endif // PY_WIN32_DELAYLOAD_PYTHON_DLL

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =


//
//    Wrap Python-types, type checks, errors, flags, etc as function calls
//

#ifdef PY_WIN32_DELAYLOAD_PYTHON_DLL
#   define IF_DELAYLOAD_ELSE( A, B ) A
#else
#   define IF_DELAYLOAD_ELSE( A, B ) B
#endif


#define _Foo_Check( TYPENAME ) \
    bool _##TYPENAME##_Check( PyObject *pyob )  \
    { \
        return pyob->ob_type == _##TYPENAME##_Type(); \
    }
ALL_TYPES( _Foo_Check )

#define _Foo_Type( TYPENAME ) \
    PyTypeObject* _##TYPENAME##_Type() \
    { \
        return IF_DELAYLOAD_ELSE( ptr__##TYPENAME##_Type, & Py##TYPENAME##_Type ); \
    }
ALL_TYPES( _Foo_Type )



#define _Exc_Foo( E ) \
    PyObject* _Exc_##E() \
    { \
        return IF_DELAYLOAD_ELSE( ptr__Exc_##E, ::PyExc_##E ); \
    }
ALL_ERRORS( _Exc_Foo )


int& _Py_DebugFlag()                    { return IF_DELAYLOAD_ELSE( *ptr_Py_DebugFlag       , Py_DebugFlag ); }
int& _Py_InteractiveFlag()              { return IF_DELAYLOAD_ELSE( *ptr_Py_InteractiveFlag , Py_InteractiveFlag ); }
int& _Py_OptimizeFlag()                 { return IF_DELAYLOAD_ELSE( *ptr_Py_OptimizeFlag    , Py_OptimizeFlag ); }
int& _Py_NoSiteFlag()                   { return IF_DELAYLOAD_ELSE( *ptr_Py_NoSiteFlag      , Py_NoSiteFlag ); }
int& _Py_VerboseFlag()                  { return IF_DELAYLOAD_ELSE( *ptr_Py_VerboseFlag     , Py_VerboseFlag ); }

char* __Py_PackageContext()             { return IF_DELAYLOAD_ELSE( *ptr__Py_PackageContext , _Py_PackageContext ); }


PyObject* _None()                       { return IF_DELAYLOAD_ELSE( ptr__PyNone             , &::_Py_NoneStruct ); }
PyObject* _False()                      { return IF_DELAYLOAD_ELSE( ptr__PyFalse            , Py_False ); }
PyObject* _True()                       { return IF_DELAYLOAD_ELSE( ptr__PyTrue             , Py_True ); }

} // namespace Py
4

2 回答 2

4

在 Win32 上,延迟加载是一种机制,允许PE文件引用另一个 PE 文件,该文件不是加载器在文件启动时期望的位置,或者如果它根本不存在,则优雅地回退。在我看来,这似乎是为了迎合嵌入 python 本身的 Windows 程序,但不希望包含 python 的 DLL 位于 PATH 中。

一些谷歌搜索进一步表明,这与避免 python 和由 python 加载的模块之间的循环有关。

于 2014-11-26T00:06:27.090 回答
2

可以使用预处理器参数定义宏,这就是为什么您在任何地方都看不到它们的原因。Microsoft 编译器带有 /D 参数。

/D 与源代码文件开头的#define 指令具有相同的效果,除了/D 去除命令行上的引号而#define 保留它们。

http://msdn.microsoft.com/en-us/library/hhzbb5c8.aspx

使用 gcc 与 -D 检查此参考链接:

https://www.daniweb.com/software-development/c/threads/348802/passing-string-as-d-compiler-option

延迟加载是一种仅在使用库时才加载库的机制,而不是在应用程序启动之前由操作系统执行。它可以节省宝贵的内存和加载时间,因为(取决于代码流)dll 可能根本不会加载。

如果定义了宏,则此代码将尝试自行实现延迟加载,否则就按正常方式进行。Microsoft 链接器可以自动为您完成工作(即您无需编写任何程序)。请记住,这不是平台的功能,而是链接器功能。

检查此参考: http ://en.wikipedia.org/wiki/Dynamic-link_library#Delayed_loading

如果需要,您可以取消代码并指示 Microsoft 链接器为您添加代码。

您可以使用 /DELAYLOAD 参数来执行此操作,如本文所述:

http://msdn.microsoft.com/en-us/library/yx9zd12s.aspx

如果找不到 dll,请确保捕获正确的异常和钩子。

于 2015-01-16T22:18:24.583 回答