Menu

LEX File Exchange
EA Support Files
SC4 Wikipedia
Network Addon Mod
Dependencies
Chat
Welcome, Guest. Please login or register.
Did you miss your activation email?

September 23, 2021, 04:06:56 AM

Login with username, password and session length

Author Topic: Python Woes  (Read 2104 times)

0 Members and 1 Guest are viewing this topic.

Offline Bdswim

Python Woes
« on: March 25, 2013, 12:29:31 PM »
(Okay, I don't know if this is the best place to ask this question, but I don't see any forums for programming here, so...)

I have this C code for compressing QFS files, and I've tried to translate it to Python. I'm not sure what I'm doing wrong, but the code won't run.

The code in question is from ILive Reader.

This is the Python code, and this is the error:
Code: [Select]
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    test()
  File "C:\...\qfs.py", line 147, in test
    len(FILE_TO_COMPRESS))
  File "C:\...\qfs.py", line 68, in compress_data
    outbuf[outpos] = 0xE0 + _len
IndexError: list assignment index out of range

What could be going wrong here? My Python code should be doing the same exact thing as the C/C++ code, but apparently, it's not.
« Last Edit: March 25, 2013, 12:34:16 PM by Bdswim »

Offline ACEfanatic02

Re: Python Woes
« Reply #1 on: March 25, 2013, 01:40:38 PM »
Well, to state the obvious, you're writing outside the bounds of the list.  Without some sample input to trace through it's hard to be more specific.

You can get away with this (usually) with C arrays because C does no bounds checking at runtime -- as long as you don't blow the stack or write outside your allocated memory C could care less (until you *do* and you segfault).  Python, on the other hand, will crash.

The block that's throwing the error is a pretty tight loop with no checks on outpos.  Try this experiment: in the original C code, store the length of the output buffer, and then add an assert in that block to notify you if outpos ever exceeds it.  If this doesn't bring up anything, try wrapping the Python statement in try: except: blocks, catch the IndexError, and log outpos and the output buffer length at that point.
日本語勉強中-最近読んだ本はこちらで見えます。
Python Enthusiast

Offline wouanagaine

  • NHP Team
  • Forums Guru
  • *
  • Posts: 2854
  • Total likes: 9
  • Reputation: 43
  • CL: :computer: SC4 CodeMaster
Re: Python Woes
« Reply #2 on: March 26, 2013, 02:14:19 AM »
Why don't you use the qfs C code and use it as a python module ?
Given the source of qfs and its algorithm, I expect python not to be the best tool to make it efficient (but I can be wrong)

If you need such code, https://github.com/wouanagaine/SC4Mapper-2013/tree/master/Modules there is a qfs.c that can be made to qfs.pyd with setuptools (thanks to JoeST)


New Horizons Productions
Berethor ♦ beskhu3epnm ♦ blade2k5 ♦ dmscopio ♦ dedgren ♦ emilin ♦ Ennedi ♦ Heblem ♦ jplumbley
M4346 ♦ moganite ♦ Papab2000 ♦ Shadow Assassin ♦ Tarkus ♦ wouanagaine
Divide wouanagaine by zero and you will in fact get one...one bad-ass that is - Alek King of SC4