Skip to content

Commit

Permalink
Made JyNI operational on Windows: Fixed and improved makefile.win. Fi…
Browse files Browse the repository at this point in the history
…xed plenty of undefined symbol errors.
  • Loading branch information
Stewori committed Aug 8, 2017
1 parent bf3036d commit fad658b
Show file tree
Hide file tree
Showing 25 changed files with 4,454 additions and 692 deletions.
2 changes: 2 additions & 0 deletions DemoExtension/DemoExtensionmodule.c
Expand Up @@ -48,6 +48,7 @@ hello_world(PyObject *self, PyObject *args)
//If nothing shall be returned, do:
//Py_INCREF(Py_None);
//return Py_None;
//PyThreadState_Get();

Py_RETURN_NONE;
}
Expand Down Expand Up @@ -516,6 +517,7 @@ PyMethodDef DemoExtensionMethods[] = {
PyMODINIT_FUNC
initDemoExtension(void)
{
//puts("initDemoExtension...");
//PyErr_Format(PyExc_ImportError, "test-error");
(void)Py_InitModule3("DemoExtension", DemoExtensionMethods, "This is a pure demo extension.");
}
14 changes: 4 additions & 10 deletions DemoExtension/makefile.win
Expand Up @@ -36,23 +36,17 @@

# These two lines must be configured by the user (for now):
# (get the compiler from https://www.microsoft.com/en-us/download/details.aspx?id=44266)
VC_Python = "C:\Users\Stefan\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0"
PYTHON_HOME = "C:\Program Files\Python\Python2.7.13"
VC_Python = "C:\Users\Stefan\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0"

CC = $(VC_Python)\VC\bin\x86_amd64\cl
LK = $(VC_Python)\VC\bin\x86_amd64\link

OUTPUTDIR = build

VCINSTALLDIR=$(VC_Python)\
WindowsSdkDir=$(VC_Python)\WinSDK\
VCINCLUDE=$(VCINSTALLDIR)Include $(WindowsSdkDir)Include
LIB=$(VCINSTALLDIR)Lib;$(WindowsSdkDir)Lib

INCLUDES = /I $(PYTHON_HOME)\include /I $(VC_Python)\VC\Include /I $(VC_Python)\WinSDK\Include

LIBS64 = $(PYTHON_HOME)/libs/python27.lib $(VC_Python)\VC\lib\amd64\libcmt.lib $(VC_Python)\VC\lib\amd64\OLDNAMES.lib $(VC_Python)\WinSDK\lib\x64\Kernel32.lib
INCLUDES = /I $(PYTHON_HOME)/include /I $(VC_Python)/VC/Include /I $(VC_Python)/WinSDK/Include

LIBS64 = $(PYTHON_HOME)/libs/python27.lib $(VC_Python)/VC/lib/amd64/libcmt.lib $(VC_Python)/VC/lib/amd64/OLDNAMES.lib $(VC_Python)/WinSDK/lib/x64/Kernel32.lib
CFLAGS = $(INCLUDES)

SOURCES = DemoExtensionmodule.c
Expand All @@ -68,7 +62,7 @@ $(OUTPUTDIR):
$(CC) /c $(CFLAGS) $<

demoExt: $(OBJECTS)
$(LK) /DLL /OUT:$(OUTPUTDIR)\DemoExtension.pyd $(OBJECTS) $(LIBS64)
$(LK) /DLL /OUT:$(OUTPUTDIR)/DemoExtension.pyd $(OBJECTS) $(LIBS64)

clean:
del *.obj
Expand Down
2 changes: 1 addition & 1 deletion JyNI-C/include/Python_JyNI/Python_JyNI.h
Expand Up @@ -183,7 +183,7 @@
#include "dtoa_JyNI.h"

/* _Py_Mangle is defined in compile.c */
//PyAPI_FUNC(PyObject*) _Py_Mangle(PyObject *p, PyObject *name);
PyAPI_FUNC(PyObject*) _Py_Mangle(PyObject *p, PyObject *name);

/* PyArg_GetInt is deprecated and should not be used, use PyArg_Parse(). */
//#define PyArg_GetInt(v, a) PyArg_Parse((v), "i", (a))
Expand Down
1 change: 1 addition & 0 deletions JyNI-C/include/Python_JyNI/pyerrors_JyNI.h
Expand Up @@ -125,6 +125,7 @@ PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *);
PyAPI_FUNC(int) PyErr_ExceptionMatches(PyObject *);
PyAPI_FUNC(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**);
PyAPI_FUNC(void) _PyErr_ReplaceException(PyObject *, PyObject *, PyObject *);

//#define PyExceptionClass_Check2(x) \
// (PyClass_Check((x)) || ( \
Expand Down
43 changes: 40 additions & 3 deletions JyNI-C/src/JyNI.c
Expand Up @@ -33,6 +33,7 @@
*
* Created on: 14.03.2013, 00:49:46
* Author: Stefan Richthofer
*
*/

#include <JyNI.h>
Expand All @@ -45,6 +46,11 @@
//#include <dlfcn.h>
//#include "stringlib/string_format.h"

#ifdef MS_WINDOWS
#include <windows.h>
#include <DbgHelp.h>
#endif

const char* excPrefix = "exceptions.";
//jlong JyNIDebugMode = 0;

Expand Down Expand Up @@ -88,9 +94,11 @@ jobject JyNI_loadModule(JNIEnv *env, jclass class, jstring moduleName, jstring m
VLA_DECL(char, mPath);

// jputs("JyNI_loadModule...");
// JyNI_jprintJ(moduleName);
// jputsLong(tstate);
//JyNI_jprintJ(moduleName);
// jputsLong(_PyThreadState_Current);
RE_ENTER_JyNI
// jputsLong(_PyThreadState_Current);
if (PyErr_Occurred()) jputs("PyErrOccured01 (beginning of JyNI_loadModule)");//this should never happen!
utf_string = (*env)->GetStringUTFChars(env, moduleName, NULL);
//"+1" for 0-termination:
Expand All @@ -109,14 +117,14 @@ jobject JyNI_loadModule(JNIEnv *env, jclass class, jstring moduleName, jstring m
if (fp == NULL)
//PyErr_SetFromErrno(PyExc_IOError);
jputs("some error happened opening the file");

er = _PyImport_LoadDynamicModuleJy(mName, mPath, fp);
if (fclose(fp)) jputs("Some error occurred on file close");
} else
// Attempt to load a module statically linked into JyNI.so:
er = _PyImport_LoadDynamicModuleJy(mName, NULL, NULL);

RE_LEAVE_JyNI
// jputs("JyNI_loadModule done");
return er;
}

Expand Down Expand Up @@ -2535,7 +2543,36 @@ inline void putsPy(PyObject* o)
void jPrintCStackTrace()
{
#ifdef MS_WINDOWS
jputs("jPrintCStackTrace is not yet supported on Windows.");
// jputs("jPrintCStackTrace is not yet supported on Windows.");
HANDLE process;
SYMBOL_INFO *symbol;
WORD numberOfFrames;
void *stack[255];
int i;
puts("jPrintCStackTrace windows...");
process = GetCurrentProcess();
SymInitialize(process, NULL, TRUE);
numberOfFrames = CaptureStackBackTrace(0, 255, stack, NULL);
symbol = (SYMBOL_INFO *)malloc(sizeof(SYMBOL_INFO)+(255 - 1) * sizeof(TCHAR));
symbol->MaxNameLen = 255;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
//DWORD displacement;
//IMAGEHLP_LINE64 *line = (IMAGEHLP_LINE64 *)malloc(sizeof(IMAGEHLP_LINE64));
//line->SizeOfStruct = sizeof(IMAGEHLP_LINE64);
for (i = 0; i < numberOfFrames; i++)
{
DWORD64 address = (DWORD64)(stack[i]);
SymFromAddr(process, address, NULL, symbol);
puts(symbol->Name);
/*if (SymGetLineFromAddr64(process, address, &displacement, line))
{
printf("\tat %s in %s: line: %lu: address: 0x%0X\n", symbol->Name, line->FileName, line->LineNumber, symbol->Address);
} else
{
printf("\tSymGetLineFromAddr64 returned error code %lu.\n", GetLastError());
printf("\tat %s, address 0x%0X.\n", symbol->Name, symbol->Address);
}*/
}
#else
void* buf[300];
int size;
Expand Down

0 comments on commit fad658b

Please sign in to comment.