修复了大量存在的问题,增加了假鸭子类型等等机制

This commit is contained in:
2026-06-25 14:49:46 +08:00
parent 19f2787db0
commit d88d11b646
827 changed files with 32617 additions and 18316 deletions

View File

@@ -57,7 +57,7 @@ DEF_MEM_LEVEL: t.CDefine = 8
# Version
ZLIB_VERSION: t.CDefine = "1.3.2"
pyzlib_error_msg: list[t.CChar, 512] = ""
pyzlib_error_msg: t.CArray[t.CChar, 512] = ""
pyzlib_error_code_val: t.CInt = 0
# ============================================================

View File

@@ -611,7 +611,7 @@ def test_huffman_tree():
printf(" All 32 distance codes: 5 bits each\n")
printf("\n --- Dynamic Huffman tree from frequencies ---\n")
freqs: list[t.CInt, 256]
freqs: t.CArray[t.CInt, 256]
memset(freqs, 0, freqs.__sizeof__())
freqs['A'] = 50
freqs['B'] = 25
@@ -641,8 +641,8 @@ def test_huffman_tree():
printf(" Active codes: %d, max code lengthgth: %d\n", total_codes, max_length_found)
printf(" Code assignments:\n")
names: list[str, None] = ["A","B","C","D","E","F"] # 一个都æ˜?char* 的数ç»?
syms: list[t.CInt, None] = ['A','B','C','D','E','F']
names: t.CArray[str, None] = ["A","B","C","D","E","F"] # 一个都æ˜?char* 的数ç»?
syms: t.CArray[t.CInt, None] = ['A','B','C','D','E','F']
for i in range(6):
printf(" '%s' (freq=%3d): code=0x%04X, bits=%d\n",
names[i], freqs[syms[i]],
@@ -660,7 +660,7 @@ def test_huffman_tree():
a_bits, f_bits, "YES" if (a_bits <= f_bits) else "NO")
printf("\n --- Huffman encode/decode roundtrip ---\n")
freqs: list[t.CInt, 288]
freqs: t.CArray[t.CInt, 288]
memset(freqs, 0, freqs.__sizeof__())
freqs['H'] = 10
freqs['e'] = 8
@@ -681,7 +681,7 @@ def test_huffman_tree():
writer = zdef.zbit_writer()
symbols: list[t.CInt, None] = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', 256]
symbols: t.CArray[t.CInt, None] = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', 256]
num_symbols: t.CInt = 13
for i in range(num_symbols):
@@ -707,7 +707,7 @@ def test_huffman_tree():
free(writer.buf)
printf("\n --- Overflow handling (many symbols, limited max_bits) ---\n")
freqs: list[t.CInt, 256]
freqs: t.CArray[t.CInt, 256]
for i in range(256):
freqs[i] = 1
freqs[256] = 1
@@ -722,7 +722,7 @@ def test_huffman_tree():
break
check("all code lengthgths <= 15 with 257 symbols", overflow_ok, "code lengthgth overflow")
bl_count: list[t.CInt, 16] = [0]
bl_count: t.CArray[t.CInt, 16] = [0]
for i in range(257):
if tree.codes[i].bits > 0:
bl_count[tree.codes[i].bits] += 1
@@ -760,7 +760,7 @@ def test_huffman_tree():
printf("\n --- Kraft inequality check ---\n")
freqs: list[t.CInt, 256]
freqs: t.CArray[t.CInt, 256]
memset(freqs, 0, freqs.__sizeof__())
freqs['X'] = 100
freqs['Y'] = 50

View File

@@ -13,7 +13,7 @@ def zchecksum_adler32(data: UINT8PTR, length: t.CSizeT, init: UINT32) -> t.CUInt
b = (b + a) % 65521
return (b << 16) | a
crc32_table: list[t.CUInt32T, 256] = [
crc32_table: t.CArray[t.CUInt32T, 256] = [
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,

View File

@@ -33,7 +33,7 @@ ZDEFLATE_END_OF_BLOCK: t.CDefine = 256
# ============================================================
# Length / Distance extra bits tables (RFC 1951)
# ============================================================
zdeflate_len_extra_bits: list[t.CInt, 29] = [
zdeflate_len_extra_bits: t.CArray[t.CInt, 29] = [
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1,
2, 2, 2, 2,
@@ -43,7 +43,7 @@ zdeflate_len_extra_bits: list[t.CInt, 29] = [
0
]
zdeflate_len_base: list[t.CInt, 29] = [
zdeflate_len_base: t.CArray[t.CInt, 29] = [
3, 4, 5, 6, 7, 8, 9, 10,
11, 13, 15, 17,
19, 23, 27, 31,
@@ -53,7 +53,7 @@ zdeflate_len_base: list[t.CInt, 29] = [
258
]
zdeflate_dist_extra_bits: list[t.CInt, 30] = [
zdeflate_dist_extra_bits: t.CArray[t.CInt, 30] = [
0, 0, 0, 0,
1, 1,
2, 2,
@@ -70,7 +70,7 @@ zdeflate_dist_extra_bits: list[t.CInt, 30] = [
13, 13
]
zdeflate_dist_base: list[t.CInt, 30] = [
zdeflate_dist_base: t.CArray[t.CInt, 30] = [
1, 2, 3, 4,
5, 7,
9, 13,
@@ -87,7 +87,7 @@ zdeflate_dist_base: list[t.CInt, 30] = [
16385, 24577
]
zdeflate_codelen_order: list[int, 19] = [
zdeflate_codelen_order: t.CArray[int, 19] = [
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
]

View File

@@ -119,8 +119,8 @@ class zdeflate_stream:
def write_dynamic_block(self, data: UINT8PTR, length: t.CSizeT, final: t.CInt):
lit_freqs: list[t.CInt, 288]
dist_freqs: list[t.CInt, 32]
lit_freqs: t.CArray[t.CInt, 288]
dist_freqs: t.CArray[t.CInt, 32]
zdeflate_count_freqs(lit_freqs, dist_freqs, self, data, length)
hlit: t.CInt = 286
while hlit > 257 and lit_freqs[hlit - 1] == 0: hlit -= 1
@@ -130,11 +130,11 @@ class zdeflate_stream:
dist_tree = zhuff.zhuff_tree()
lit_tree.build_codes(lit_freqs, hlit, zdef.ZDEFLATE_MAX_BITS)
dist_tree.build_codes(dist_freqs, hdist, zdef.ZDEFLATE_MAX_BITS)
all_lengths: list[t.CInt, 288 + 32]
all_lengths: t.CArray[t.CInt, 288 + 32]
for i in range(hlit): all_lengths[i] = lit_tree.codes[i].bits
for i in range(hdist): all_lengths[hlit + i] = dist_tree.codes[i].bits
total_lengths: t.CInt = hlit + hdist
cl_freqs: list[t.CInt, 19]
cl_freqs: t.CArray[t.CInt, 19]
zdeflate_count_cl_freqs(all_lengths, total_lengths, cl_freqs)
cl_tree = zhuff.zhuff_tree()
cl_tree.build_codes(cl_freqs, 19, 7)

View File

@@ -13,7 +13,7 @@ class zhuff_code:
@t.Object
class zhuff_tree:
codes: list[zhuff_code, ZHUFF_MAX_CODES]
codes: t.CArray[zhuff_code, ZHUFF_MAX_CODES]
count: t.CInt
max_bits: t.CInt
@@ -21,9 +21,9 @@ class zhuff_tree:
pass
def build_codes(self, freqs: INTPTR, count: t.CInt, max_bits: t.CInt):
lengths: list[t.CInt, ZHUFF_MAX_CODES]
bl_count: list[t.CInt, ZHUFF_MAX_BITS + 1]
next_code: list[UINT, ZHUFF_MAX_BITS + 1]
lengths: t.CArray[t.CInt, ZHUFF_MAX_CODES]
bl_count: t.CArray[t.CInt, ZHUFF_MAX_BITS + 1]
next_code: t.CArray[UINT, ZHUFF_MAX_BITS + 1]
self.count = count
self.max_bits = max_bits
self.build_code_lengths(lengths, freqs, count, max_bits)
@@ -45,9 +45,9 @@ class zhuff_tree:
self.codes[i].code = 0
def build_fixed_lit_tree(self):
lengths: list[t.CInt, 288]
bl_count: list[t.CInt, 16]
next_code: list[t.CUnsignedInt, 16]
lengths: t.CArray[t.CInt, 288]
bl_count: t.CArray[t.CInt, 16]
next_code: t.CArray[t.CUnsignedInt, 16]
self.get_fixed_lit_lengths(lengths)
memset(bl_count, 0, bl_count.__sizeof__())
for i in range(288):
@@ -69,9 +69,9 @@ class zhuff_tree:
self.codes[i].code = 0
def build_fixed_dist_tree(self):
lengths: list[t.CInt, 32]
bl_count: list[t.CInt, 16]
next_code: list[t.CUnsignedInt, 16]
lengths: t.CArray[t.CInt, 32]
bl_count: t.CArray[t.CInt, 16]
next_code: t.CArray[t.CUnsignedInt, 16]
self.get_fixed_dist_lengths(lengths)
memset(bl_count, 0, bl_count.__sizeof__())
for i in range(32):
@@ -101,7 +101,7 @@ class zhuff_tree:
def build_code_lengths(self, lengths: t.CInt | t.CPtr, freqs: t.CInt | t.CPtr, count: t.CInt, max_bits: t.CInt):
bl_count: list[t.CInt, ZHUFF_MAX_BITS + 2]
bl_count: t.CArray[t.CInt, ZHUFF_MAX_BITS + 2]
sort_count: t.CInt = 0
memset(bl_count, 0, bl_count.__sizeof__())
memset(lengths, 0, int.__sizeof__() * count)
@@ -270,8 +270,8 @@ class zhuff_tree:
lengths[i] = 5
def build_tree_from_lengths(self, lengths: INTPTR, count: t.CInt, max_bits: t.CInt):
bl_count: list[t.CInt, 16]
next_code: list[t.CUnsignedInt, 16]
bl_count: t.CArray[t.CInt, 16]
next_code: t.CArray[t.CUnsignedInt, 16]
memset(bl_count, 0, bl_count.__sizeof__())
for i in range(count):
@@ -296,13 +296,13 @@ class zhuff_tree:
class zhuff_decode_node:
children: list[t.CInt, 2]
children: t.CArray[t.CInt, 2]
symbol: t.CInt
@t.Object
class zhuff_decode_tree:
nodes: list[zhuff_decode_node, 2 * ZHUFF_MAX_CODES]
nodes: t.CArray[zhuff_decode_node, 2 * ZHUFF_MAX_CODES]
node_count: t.CInt
root: t.CInt

View File

@@ -212,7 +212,7 @@ class zinflate_stream:
hdist: t.CInt = t.CInt(hdist_val) + 1
hclen: t.CInt = t.CInt(hclen_val) + 4
cl_lengths: list[t.CInt, 19]
cl_lengths: t.CArray[t.CInt, 19]
memset(cl_lengths, 0, cl_lengths.__sizeof__())
for i in range(hclen):
len_val: UINT
@@ -282,13 +282,13 @@ class zinflate_stream:
lit_tree = zhuff.zhuff_tree()
dist_tree = zhuff.zhuff_tree()
lit_lengths: list[t.CInt, 288]
lit_lengths: t.CArray[t.CInt, 288]
memset(lit_lengths, 0, lit_lengths.__sizeof__())
for i in range(hlit):
lit_lengths[i] = all_lengths[i]
lit_tree.build_tree_from_lengths(lit_lengths, hlit, zdef.ZDEFLATE_MAX_BITS)
dist_lengths: list[t.CInt, 32]
dist_lengths: t.CArray[t.CInt, 32]
memset(dist_lengths, 0, dist_lengths.__sizeof__())
for i in range(hdist):
dist_lengths[i] = all_lengths[hlit + i]

View File

@@ -10,7 +10,10 @@ import __zlib.zhuff as zhuff
import __zlib.test_pyzlib as tpz
import w32.win32memory
import c
import testcheck
def main() -> t.CInt:
testcheck.begin("ZlibTest: Zlib 压缩库测试")
tpz.main123456()
return 0
testcheck.ok("zlib test_pyzlib completed")
return testcheck.end()