gpt4 book ai didi

python - Windows 中的延迟加载

转载 作者:可可西里 更新时间:2023-11-01 11:07:17 25 4
gpt4 key购买 nike

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

编辑:原始代码 here .

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

#ifdef PY_WIN32_DELAYLOAD_PYTHON_DLL
:
#else
:
#endif

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

此 PY_WIN32_DELAYLOAD_PYTHON_DLL token 在 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

最佳答案

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

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

关于python - Windows 中的延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27139089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com