/* Support for dynamic loading of extension modules */ #include "Python.h" #ifdef HAVE_DIRECT_H #include #endif #include #include "importdl.h" #include // "activation context" magic - see dl_nt.c... #if HAVE_SXS extern ULONG_PTR _Py_ActivateActCtx(); void _Py_DeactivateActCtx(ULONG_PTR cookie); #endif const char *_PyImport_DynLoadFiletab[] = { #ifdef _DEBUG "_d.pyd", #else ".pyd", #endif NULL }; /* Case insensitive string compare, to avoid any dependencies on particular C RTL implementations */ static int strcasecmp (char *string1, char *string2) { int first, second; do { first = tolower(*string1); second = tolower(*string2); string1++; string2++; } while (first && first == second); return (first - second); } /* Function to return the name of the "python" DLL that the supplied module directly imports. Looks through the list of imported modules and returns the first entry that starts with "python" (case sensitive) and is followed by nothing but numbers until the separator (period). Returns a pointer to the import name, or NULL if no matching name was located. This function parses through the PE header for the module as loaded in memory by the system loader. The PE header is accessed as documented by Microsoft in the MSDN PE and COFF specification (2/99), and handles both PE32 and PE32+. It only worries about the direct import table and not the delay load import table since it's unlikely an extension is going to be delay loading Python (after all, it's already loaded). If any magic values are not found (e.g., the PE header or optional header magic), then this function simply returns NULL. */ #define DWORD_AT(mem) (*(DWORD *)(mem)) #define WORD_AT(mem) (*(WORD *)(mem)) static char *GetPythonImport (HINSTANCE hModule) { unsigned char *dllbase, *import_data, *import_name; DWORD pe_offset, opt_offset; WORD opt_magic; int num_dict_off, import_off; /* Safety check input */ if (hModule == NULL) { return NULL; } /* Module instance is also the base load address. First portion of memory is the MS-DOS loader, which holds the offset to the PE header (from the load base) at 0x3C */ dllbase = (unsigned char *)hModule; pe_offset = DWORD_AT(dllbase + 0x3C); /* The PE signature must be "PE\0\0" */ if (memcmp(dllbase+pe_offset,"PE\0\0",4)) { return NULL; } /* Following the PE signature is the standard COFF header (20 bytes) and then the optional header. The optional header starts with a magic value of 0x10B for PE32 or 0x20B for PE32+ (PE32+ uses 64-bits for some fields). It might also be 0x107 for a ROM image, but we don't process that here. The optional header ends with a data dictionary that directly points to certain types of data, among them the import entries (in the second table entry). Based on the header type, we determine offsets for the data dictionary count and the entry within the dictionary pointing to the imports. */ opt_offset = pe_offset + 4 + 20; opt_magic = WORD_AT(dllbase+opt_offset); if (opt_magic == 0x10B) { /* PE32 */ num_dict_off = 92; import_off = 104; } else if (opt_magic == 0x20B) { /* PE32+ */ num_dict_off = 108; import_off = 120; } else { /* Unsupported */ return NULL; } /* Now if an import table exists, offset to it and walk the list of imports. The import table is an array (ending when an entry has empty values) of structures (20 bytes each), which contains (at offset 12) a relative address (to the module base) at which a string constant holding the import name is located. */ if (DWORD_AT(dllbase + opt_offset + num_dict_off) >= 2) { /* We have at least 2 tables - the import table is the second one. But still it may be that the table size is zero */ if (0 == DWORD_AT(dllbase + opt_offset + import_off + sizeof(DWORD))) return NULL; import_data = dllbase + DWORD_AT(dllbase + opt_offset + import_off); while (DWORD_AT(import_data)) { import_name = dllbase + DWORD_AT(import_data+12); if (strlen(import_name) >= 6 && !strncmp(import_name,"python",6)) { char *pch; #ifndef _DEBUG /* In a release version, don't claim that python3.dll is a Python DLL. */ if (strcmp(import_name, "python3.dll") == 0) { import_data += 20; continue; } #endif /* Ensure python prefix is followed only by numbers to the end of the basename */ pch = import_name + 6; #ifdef _DEBUG while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') { #else while (*pch && *pch != '.') { #endif if (*pch >= '0' && *pch <= '9') { pch++; } else { pch = NULL; break; } } if (pch) { /* Found it - return the name */ return import_name; } } import_data += 20; } } return NULL; } dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname, PyObject *pathname, FILE *fp) { dl_funcptr p; char funcname[258], *import_python; wchar_t *wpathname; #ifndef _DEBUG _Py_CheckPython3(); #endif wpathname = PyUnicode_AsUnicode(pathname); if (wpathname == NULL) return NULL; PyOS_snprintf(funcname, sizeof(funcname), "PyInit_%.200s", shortname); { HINSTANCE hDLL = NULL; unsigned int old_mode; #if HAVE_SXS ULONG_PTR cookie = 0; #endif /* Don't display a message box when Python can't load a DLL */ old_mode = SetErrorMode(SEM_FAIL/**************************************************************************** ** ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). ** All rights reserved. ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** This file is part of the documentation of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** No Commercial Usage ** This file contains pre-release code and may not be distributed. ** You may use this file in accordance with the terms and conditions ** contained in the Technology Preview License Agreement accompanying ** this package. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain additional ** rights. These rights are described in the Nokia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** If you have questions regarding the use of this file, please contact ** Nokia at qt-info@nokia.com. ** ** ** ** ** ** ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ //! [0] class MyScriptableObject: public QObject, protected QScriptable { Q_OBJECT ... public slots: void doSomething(); double doSomethingElse(); } //! [0] //! [1] void MyScriptableObject::doSomething() { context()->throwError("Threw an error from a slot"); } double MyScriptableObject::doSomethingElse() { return qscriptvalue_cast<double>(thisObject()); } //! [1]