Jump to content

[Updated]Python3 - More damn problems


Recommended Posts

  • Contributor

Hello everyone,

 

I'm trying to move Tmp4's(or Mali's?) client source to python 3.

Basically, I replaced the include and lib and just followed the error trail. The client compiles with no errors, but whenever I try to start it, it throws this shit:

Fatal Python error: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Metin2Debug.exe has triggered a breakpoint.

Which basically comes when the app is instantiated in UserInterface.cpp, here:

	Tracen("Instantiating cPythonApplication");
	CPythonApplication* app = new CPythonApplication; // right here

	// and it never gets to the next line

	Tracen("Initializing app");
	app->Initialize(hInstance);

Here's the output, if you want to see it:

Spoiler

'Metin2Debug.exe' (Win32): Loaded 'D:\tmp4\Client\Metin2Debug.exe'. Module was built without symbols.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\win32u.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32full.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp_win.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbase.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ole32.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\combase.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\d3d8.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imagehlp.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ws2_32.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmm.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\version.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'D:\tmp4\Client\python310_d.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shell32.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleaut32.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'D:\tmp4\Client\granny2.dll'. 
'Metin2Debug.exe' (Win32): Loaded 'D:\tmp4\Client\devil.dll'. Module was built without symbols.
'Metin2Debug.exe' (Win32): Loaded 'D:\tmp4\Client\MSS32.DLL'. Module was built without symbols.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dinput8.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dwmapi.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ddraw.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\d3d8thk.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dxgi.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'D:\tmp4\Client\SpeedTreeRT.dll'. Module was built without symbols.
'Metin2Debug.exe' (Win32): Unloaded 'D:\tmp4\Client\SpeedTreeRT.dll'
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dciman32.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'D:\tmp4\Client\ucrtbased.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'D:\tmp4\Client\ucrtbased.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Unloaded 'D:\tmp4\Client\ucrtbased.dll'
'Metin2Debug.exe' (Win32): Loaded 'D:\tmp4\Client\SpeedTreeRT.dll'. Module was built without symbols.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp60.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\InputHost.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\SHCore.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\propsys.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\WinTypes.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\CoreUIComponents.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\CoreMessaging.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\CoreMessaging.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\CoreMessaging.dll'
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntmarta.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Symbols loaded.
Open console
Open log file
Initializing pack
Initializing locale
Instantiating cPythonApplication
Fatal Python error: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Metin2Debug.exe has triggered a breakpoint.

Debug Error!

Program: D:\tmp4\Client\Metin2Debug.exe

abort() has been called

(Press Retry to debug the application)
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\TextShaping.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Symbols loaded.
'Metin2Debug.exe' (Win32): Loaded 'C:\Windows\SysWOW64\TextInputFramework.dll'. Symbols loaded.
The thread 0x1b74 has exited with code 3 (0x3).
The thread 0xe0 has exited with code 3 (0x3).
The thread 0x18f8 has exited with code 3 (0x3).
The thread 0x263c has exited with code 3 (0x3).
The thread 0x1094 has exited with code 3 (0x3).
The thread 0xc48 has exited with code 3 (0x3).
The thread 0x253c has exited with code 3 (0x3).
The thread 0x1a04 has exited with code 3 (0x3).
The program '[6464] Metin2Debug.exe' has exited with code 3 (0x3).
 

Call Stack:

Spoiler

>    KernelBase.dll!wil::details::DebugBreak(void)    Unknown
     python310_d.dll!fatal_error_exit(int status) Line 2553    C
     python310_d.dll!fatal_error(int fd, int header, const char * prefix, const char * msg, int status) Line 2735    C
     python310_d.dll!_Py_FatalErrorFunc(const char * func, const char * msg) Line 2751    C
     python310_d.dll!_Py_FatalError_TstateNULL(const char * func) Line 271    C
     python310_d.dll!_PyInterpreterState_GET() Line 117    C
     python310_d.dll!get_tuple_state() Line 22    C
     python310_d.dll!tuple_get_empty() Line 131    C
     python310_d.dll!PyTuple_New(int size) Line 154    C
     python310_d.dll!do_mktuple(const char * * p_format, char * * p_va, char endchar, int n, int flags) Line 259    C
     python310_d.dll!do_mkvalue(const char * * p_format, char * * p_va, int flags) Line 296    C
     python310_d.dll!va_build_value(const char * format, char * va, int flags) Line 565    C
     python310_d.dll!Py_BuildValue(const char * format, ...) Line 522    C
     [External Code]    
     Metin2Debug.exe![Frames below may be incorrect and/or missing, no symbols loaded for Metin2Debug.exe]    Unknown
 

And the threads:

Spoiler

Not Flagged    >    7732    0    Main Thread    Main Thread    python310_d.dll!fatal_error_exit
Not Flagged        10460    0    Worker Thread    ntdll.dll!TppWorkerThread    ntdll.dll!_NtWaitForWorkViaWorkerFactory@20
Not Flagged        10316    0    Worker Thread    ntdll.dll!TppWorkerThread    ntdll.dll!_NtWaitForWorkViaWorkerFactory@20
Not Flagged        10284    0    Worker Thread    ntdll.dll!TppWorkerThread    ntdll.dll!_NtWaitForWorkViaWorkerFactory@20
Not Flagged        11128    0    Worker Thread    ntdll.dll!TppWorkerThread    ntdll.dll!_NtWaitForWorkViaWorkerFactory@20
Not Flagged        10400    0    Worker Thread    InputHost.dll!<lambda_52549c42edc0789d5b8e6fe768050260>::<lambda_invoker_stdcall>    CoreMessaging.dll!Microsoft::CoreUI::Dispatch::WaitAdapter::Callback_WaitAny

So, because of this

 	python310_d.dll!_Py_FatalError_TstateNULL(const char * func) Line 271	C
 	python310_d.dll!_PyInterpreterState_GET() Line 117	C

I would assume it's because the interpreter is not loaded.

Also, just in case, here's how the modules are created:

Spoiler

    static struct PyModuleDef cAppModule =
    {
        PyModuleDef_HEAD_INIT,
        "cAppModule", /* name of module */
        "",          /* module documentation, may be NULL */
        -1,          /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
        s_methods
    };

    PyObject* poModule = PyModule_Create(&cAppModule);

I don't think it's because of this, since we never get to load the modules with RunMainScript:

		CPythonLauncher pyLauncher;
		CPythonExceptionSender pyExceptionSender;
		SetExceptionSender(&pyExceptionSender);

		if (pyLauncher.Create())
		{
			ret = RunMainScript(pyLauncher, lpCmdLine);
		}

Does anyone have any clue?

 

Here's some pics:

Pic 1

Pic 2

Edited by Metin2 Dev
Core X - External 2 Internal
  • Good 1
  • Love 1
Link to comment
Share on other sites

  • Contributor

I'm still here.

I've started rewriting the functions and following the exact docs from:

https://docs.python.org/3/extending/extending.html

 

I'll keep you posted if anything changes and again.. if anyone has any ideas, please let me know.

  • Good 1
Link to comment
Share on other sites

  • Contributor

Ok, so I'm past that.

I've moved the initialization of modules in a separate function which is called before the python interpreter gets initialized(like the docs said).

Now in UserInterface, Main(), the workflow looks like this:

	if (LocaleService_LoadGlobal(hInstance))
		SetDefaultCodePage(LocaleService_GetCodePage());

	InitializeModules();
	Py_Initialize();

	CPythonApplication* app = new CPythonApplication;

	app->Initialize(hInstance);

Before the python app class is instantiated, we load the modules, then initialize the interpreter.

Also, in PythonLauncher.cpp, I'm doing this(for now):

CPythonLauncher::CPythonLauncher()
{
	if (!Py_IsInitialized()) {
		Py_Initialize();
	}
}

I should probably remove the initialization from here and just throw an error if it fails to initialize in UserInterface, but it's okay for now.. I have bigger fish to catch.

 

Now, next problem(which is probably the last one in the source regarding this subject):

In `UserInterface.cpp->RunMainScript()` we're calling 

pyLauncher.RunFile("system.py")

Which calls `CPythonLauncher::RunMemoryTextFile`

Which calls `CPythonLauncher::RunLine` with the file data as a string.

The problem is ```PyObject* v = PyRun_String((char*)c_szSrc, Py_file_input, m_poDic, m_poDic);``` always returns NULL instead of running the code.

Yes, the code gets there, I've already checked.

 

My thoughts are, it might be because of what's going on in RunMemoryTextFile, specifically in:

	stConvFileData += "exec(compile('''";

	// ConvertPythonTextFormat
	{
		for (UINT i = 0; i < uFileSize; ++i)
		{
			if (c_pcFileData[i] != 13)// carriage return
			{
				stConvFileData += c_pcFileData[i];
			}
		}
	}

	stConvFileData += "''', ";
	stConvFileData += "'";
	stConvFileData += c_szFileName;
	stConvFileData += "', ";
	stConvFileData += "'exec'))";
	stConvFileData += '\0';

The `compile()` function already takes method = 'exec', why do we need to call exec again?(see the first line in the code block before this)

Here's the docs: https://docs.python.org/3/library/functions.html#compile

If I keep it as `exec(compile(''' ` it always returns null when it's executed in RunLine and it throws a shitload of traceback errors.

If I remove the `exec` and just `compile` it with the exec method passed in the function, it doesn't return null anymore, but the code doesn't run, it just returns and jumps straight to exit(0)(in user interface), so I assume the code doesn't get executed.

 

I'll compile and post the errors in a few minutes.

Any ideas?

Here's the follow up:

With `exec(compile(`:

Spoiler

0228 18:18:00158 :: Load system.py
0228 18:18:00316 :: SYSERR: PyRun_String returned NULL. Tracing back...
0228 18:18:02573 :: Traceback:

Call: File "<string>", line 1, in <module>
Call: File "system.py", line 1, in <module>
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 987, in _find_and_load_unlocked
Call: File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
Call: File "<frozen importlib._bootstrap_external>", line 877, in exec_module
Call: File "<frozen importlib._bootstrap>", line 233, in _call_with_frames_removed
Call: File "D:\metin2\python3_branch\Client\lib\imp.py", line 1, in <module>
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 987, in _find_and_load_unlocked
Call: File "<frozen importlib._bootstrap>", line 233, in _call_with_frames_removed
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 987, in _find_and_load_unlocked
Call: File "<frozen importlib._bootstrap>", line 1053, in _handle_fromlist
Call: File "<frozen importlib._bootstrap>", line 233, in _call_with_frames_removed
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 1053, in _handle_fromlist
Call: File "<frozen importlib._bootstrap>", line 233, in _call_with_frames_removed
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 987, in _find_and_load_unlocked
Call: File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
Call: File "<frozen importlib._bootstrap_external>", line 877, in exec_module
Call: File "<frozen importlib._bootstrap>", line 233, in _call_with_frames_removed
Call: File "D:\metin2\python3_branch\Client\lib\importlib\util.py", line 1, in <module>
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 987, in _find_and_load_unlocked
Call: File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
Call: File "<frozen importlib._bootstrap_external>", line 877, in exec_module
Call: File "<frozen importlib._bootstrap>", line 233, in _call_with_frames_removed
Call: File "D:\metin2\python3_branch\Client\lib\contextlib.py", line 1, in <module>
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 987, in _find_and_load_unlocked
Call: File "<frozen importlib._bootstrap>", line 921, in _find_spec
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 987, in _find_and_load_unlocked
Call: File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
Call: File "<frozen importlib._bootstrap_external>", line 877, in exec_module
Call: File "<frozen importlib._bootstrap>", line 233, in _call_with_frames_removed
Call: File "D:\metin2\python3_branch\Client\lib\tokenize.py", line 1, in <module>
Call: File "D:\metin2\python3_branch\Client\lib\io.py", line 60, in __getattr__
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 987, in _find_and_load_unlocked
Call: File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
Call: File "<frozen importlib._bootstrap_external>", line 877, in exec_module
Call: File "<frozen importlib._bootstrap>", line 233, in _call_with_frames_removed
Call: File "D:\metin2\python3_branch\Client\lib\re.py", line 1, in <module>
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 987, in _find_and_load_unlocked
Call: File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
Call: File "<frozen importlib._bootstrap_external>", line 877, in exec_module
Call: File "<frozen importlib._bootstrap>", line 233, in _call_with_frames_removed
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "<frozen importlib._bootstrap>", line 169, in __enter__
Call: File "<frozen importlib._bootstrap>", line 987, in _find_and_load_unlocked
Call: File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load
Call: File "D:\metin2\python3_branch\Client\lib\re.py", line 249, in compile
Call: File "D:\metin2\python3_branch\Client\lib\re.py", line 288, in _compile
Exception: File "D:\metin2\python3_branch\Client\lib\re.py", line 293, in _compile
Call: File "D:\metin2\python3_branch\Client\lib\sre_compile.py", line 759, in compile
Call: File "D:\metin2\python3_branch\Client\lib\sre_parse.py", line 937, in parse
Call: File "D:\metin2\python3_branch\Client\lib\sre_compile.py", line 598, in _code
Call: File "D:\metin2\python3_branch\Client\lib\sre_compile.py", line 71, in _compile
Call: File "D:\metin2\python3_branch\Client\lib\sre_compile.py", line 71, in _compile
Call: File "D:\metin2\python3_branch\Client\lib\re.py", line 249, in compile
Call: File "D:\metin2\python3_branch\Client\lib\re.py", line 288, in _compile
Call: File "D:\metin2\python3_branch\Client\lib\warnings.py", line 96, in _showwarnmsg
Call: File "D:\metin2\python3_branch\Client\lib\warnings.py", line 20, in _showwarnmsg_impl
Call: File "<frozen importlib._bootstrap>", line 1022, in _find_and_load


0228 18:18:02579 :: SYSERR: RunMain Error
0228 18:18:02597 :: SYSERR: CPythonApplication::Destroy

So, the code gets executed but can't import anything?

Here's system.py:

Spoiler
import sys
import app
import dbg

sys.path.append("lib")

class TraceFile:
	def write(self, msg):
		dbg.Trace(msg)

class TraceErrorFile:
	def write(self, msg):
		dbg.TraceError(msg)
		dbg.RegisterExceptionString(msg)

class LogBoxFile:
	def __init__(self):
		self.stderrSave = sys.stderr
		self.msg = ""

	def __del__(self):
		self.restore()

	def restore(self):
		sys.stderr = self.stderrSave

	def write(self, msg):
		self.msg = self.msg + msg

	def show(self):
		dbg.LogBox(self.msg,"Error")

sys.stdout = TraceFile()
sys.stderr = TraceErrorFile()

#
# pack file support (must move to system.py, systemrelease.pyc)
#

import marshal
import imp
import pack

class pack_file_iterator(object):
	def __init__(self, packfile):
		self.pack_file = packfile
		
	def __next__(self):
		tmp = self.pack_file.readline()
		if tmp:
			return tmp
		raise StopIteration

_chr = __builtins__.chr

class pack_file(object):

	def __init__(self, filename, mode = 'rb'):
		assert mode in ('r', 'rb')
		if not pack.Exist(filename):
			raise IOError('No file or directory')
		self.data = pack.Get(filename)
		if mode == 'r':
			self.data=_chr(10).join(self.data.split(_chr(13)+_chr(10)))

	def __iter__(self):
		return pack_file_iterator(self)

	def read(self, len = None):
		if not self.data:
			return ''
		if len:
			tmp = self.data[:len]
			self.data = self.data[len:]
			return tmp
		else:
			tmp = self.data
			self.data = ''
			return tmp

	def readline(self):
		return self.read(self.data.find(_chr(10))+1)

	def readlines(self):
		return [x for x in self]

__builtins__.pack_open = pack_open = pack_file

_ModuleType = type(sys)

old_import = __import__
def _process_result(code, fqname):
	# did get_code() return an actual module? (rather than a code object)
	is_module = isinstance(code, _ModuleType)

	# use the returned module, or create a new one to exec code into
	if is_module:
		module = code
	else:
		module = imp.new_module(fqname)

	# insert additional values into the module (before executing the code)
	#module.__dict__.update(values)

	# the module is almost ready... make it visible
	sys.modules[fqname] = module

	# execute the code within the module's namespace
	if not is_module:
		exec(code, module.__dict__)

	# fetch from sys.modules instead of returning module directly.
	# also make module's __name__ agree with fqname, in case
	# the "exec code in module.__dict__" played games on us.
	module = sys.modules[fqname]
	module.__name__ = fqname
	return module

module_do = lambda x:None

def __pack_import(name,globals=None,locals=None,fromlist=None):
	if name in sys.modules:
		return sys.modules[name]

	filename = name + '.py'

	if pack.Exist(filename):
		dbg.Trace('importing from pack %s\\n' % name)

		newmodule = _process_result(compile(pack_file(filename,'r').read(),filename,'exec'),name)		

		module_do(newmodule)
		return newmodule
		#return imp.load_module(name, pack_file(filename,'r'),filename,('.py','r',imp.PY_SOURCE))
	else:
		dbg.Trace('importing from lib %s\\n' % name)
		return old_import(name,globals,locals,fromlist)

def splitext(p):
	root, ext = '', ''
	for c in p:
		if c in ['/']:
			root, ext = root + ext + c, ''
		elif c == '.':
			if ext:
				root, ext = root + ext, c
			else:
				ext = c
		elif ext:
			ext = ext + c
		else:
			root = root + c
	return root, ext

class PythonExecutioner: 

	def Run(kPESelf, sFileName, kDict): 
		if kPESelf.__IsCompiledFile__(sFileName): 
			kCode=kPESelf.__LoadCompiledFile__(sFileName) 
		else: 
			kCode=kPESelf.__LoadTextFile__(sFileName) 

		exec(kCode, kDict) 

	def __IsCompiledFile__(kPESelf, sFileName): 

		sBase, sExt = splitext(sFileName) 
		sExt=sExt.lower() 

		if sExt==".pyc" or sExt==".pyo": 
			return 1 
		else: 
			return 0 

	def __LoadTextFile__(kPESelf, sFileName): 
		sText=pack_open(sFileName,'r').read() 
		return compile(sText, sFileName, "exec") 

	def __LoadCompiledFile__(kPESelf, sFileName): 
		kFile=pack_open(sFileName)

		if kFile.read(4)!=imp.get_magic(): 
			raise 

		kFile.read(4) 

		kData=kFile.read() 
		return marshal.loads(kData) 

def execfile(fileName, dict): 
	kPE=PythonExecutioner() 
	kPE.Run(fileName, dict) 

def exec_add_module_do(mod):
	global execfile
	mod.__dict__['execfile'] = execfile

import builtins
builtins.__import__ = __pack_import
module_do = exec_add_module_do


def GetExceptionString(excTitle):
	(excType, excMsg, excTraceBack)=sys.exc_info()
	excText=""
	excText+=_chr(10)

	import traceback
	traceLineList=traceback.extract_tb(excTraceBack)

	for traceLine in traceLineList:
		if traceLine[3]:
			excText+="%s(line:%d) %s - %s" % (traceLine[0], traceLine[1], traceLine[2], traceLine[3])
		else:
			excText+="%s(line:%d) %s"  % (traceLine[0], traceLine[1], traceLine[2])

		excText+=_chr(10)
	
	excText+=_chr(10)
	excText+="%s - %s:%s" % (excTitle, excType, excMsg)		
	excText+=_chr(10)

	return excText

def ShowException(excTitle):
	excText=GetExceptionString(excTitle)
	dbg.TraceError(excText)
	app.Abort()

	return 0

def RunMainScript(name):
	try:		
		exec(compile(open(name, "rb").read(), name, 'exec'), __main__.__dict__)
	except RuntimeError as msg:
		msg = str(msg)

		import locale
		if locale.error:
			msg = locale.error.get(msg, msg)

		dbg.LogBox(msg)
		app.Abort()

	except:	
		msg = GetExceptionString("Run")
		dbg.LogBox(msg)
		app.Abort()
	
import debugInfo
debugInfo.SetDebugMode(__DEBUG__)

loginMark = "-cs"

app.__COMMAND_LINE__ = __COMMAND_LINE__
RunMainScript("prototype.py")

 

 

Link to comment
Share on other sites

  • 1 year later...
  • Active Member

Haha, i've followed your path, and ran into the same problems. The breakthrough was removing the debug sht from system.py:

import debuginfo
#debuginfo.SetDebugMode(__DEBUG__)
debuginfo.SetDebugMode(0)

After this i started to receive usable error messages... 
Next, i changed in PythonPAckModule.cpp (in PyObject * packGet(...))

			if (CEterPackManager::Instance().Get(file, strFileName, &pData))
			{
				//return Py_BuildValue("s#",pData, file.Size());
				return PyUnicode_FromStringAndSize((const char *)pData, file.Size());
			}

And voila, everything has started working (?). Got sht tons of messages like this, need to convert the python (and txt) files to utf-8.

Spoiler

spacer.png

 

Edited by Metin2 Dev International
Core X - External 2 Internal
Link to comment
Share on other sites

  • 8 months later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.