File: char_enc3.py

package info (click to toggle)
libgetdata 0.11.0-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,144 kB
  • sloc: ansic: 100,814; cpp: 4,843; fortran: 4,548; f90: 2,561; python: 2,406; perl: 2,274; makefile: 1,487; php: 1,465; sh: 86
file content (378 lines) | stat: -rw-r--r-- 7,387 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Copyright (C) 2016 D. V. Wiebe
#
##########################################################################
#
# This file is part of the GetData project.
#
# GetData is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2.1 of the License, or (at your
# option) any later version.
#
# GetData is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with GetData; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Test character_encoding

import re
import os
import sys
import pygetdata

# Python2/3 abstraction:

# an encoded character (byte)
def E(c):
  if sys.version[:1] == '3':
    return bytes([c])
  return chr(c)

# an encoded string
def B(s):
  if sys.version[:1] == '3':
    return bytes(s, "UTF-8")
  return s

# a filesystem decoded string
def F(s):
  if sys.version[:1] == '3':
    return os.fsdecode(s)
  return s

# a decoded string
def U(s):
  if sys.version[:1] == '3':
    return s
  return unicode(s)

ne = 0

def CheckOK(t):
  global ne
  ne+=1
  print ("e[", t, "] =", sys.exc_info()[0], sys.exc_value)

def CheckSimple(t,v,g):
  global ne
  if (v != g):
    ne+=1
    print ("n[", t, "] =", v, "expected", g)

def CheckEOS(t,v,g):
  global ne
  if (v[-len(g):] != g):
    ne+=1
    print ("n[", t, "] =", repr(v), "expected", repr(g))

def CheckExc(n):
  global ne
  global e
  if (e):
    e=0
    ne+=1
    print ("Missing UnicodeDecodeError for " + str(n))

def CheckParms(n,c):
  global e
  try:
    e = 1
    print (c.parameters)
  except UnicodeDecodeError:
    e = 0

  CheckExc(n)

def CheckIns(n,c):
  global e
  try:
    e = 1
    print (c.in_fields)
  except UnicodeDecodeError:
    e = 0

  CheckExc(n)

# create the dirfile
os.system("rm -rf dirfile")
os.mkdir("dirfile")

# Encoded string (koi8-r)
estring =  E(0xF3) + E(0xD4) + E(0xD2) + E(0xCF) + E(0xCB) + E(0xC1)

# Filesystem *decoded* string
fstring = F(estring)

# byte-escaped (used for Dirfile metadata)
xstring = B('\\xF3\\xD4\\xD2\\xCF\\xCB\\xC1')

f=open("dirfile/format", "wb")
f.write(
        B("/ALIAS ") + xstring + B("_al ") + xstring + B("_t\n") +
        B("s1 STRING ") + xstring + B("1\n") +
        xstring + B("_s2 STRING ") + xstring + B("2\n") +
        xstring + B("_a1 CARRAY UINT64 1 2 3\n") +
        xstring + B("_a2 CARRAY UINT64 1 2 3\n") +
        xstring + B("_c1 CONST UINT8 1\n") +
        xstring + B("_c2 CONST UINT8 1\n") +
        xstring + B("_r1 RAW UINT8 ") + xstring + B("_spf\n") +
        B("r2 RAW UINT8 ") + xstring + B("_spf\n") +
        B("l1 LINTERP in ") + xstring + B("\n") +
        B("l1/") + xstring + B("_s1 STRING ") + xstring + B("1\n") +
        B("l1/") + xstring + B("_s2 STRING ") + xstring + B("2\n") +
        B("l1/") + xstring + B("_a1 CARRAY UINT64 1 2 3\n") +
        B("l1/") + xstring + B("_a2 CARRAY UINT64 1 2 3\n") +
        B("l1/") + xstring + B("_c1 CONST UINT8 1\n") +
        B("l1/") + xstring + B("_c2 CONST UINT8 2\n") +
        B("o1 LINCOM ") + xstring + B("_i ") + xstring + B("_m ") +
        xstring + B("_b\n") +
        B("o2 LINCOM ") +
        xstring + B("_i1 ") + xstring + B("_m1 ") + xstring + B("_b1 ") +
        xstring + B("_i2 ") + xstring + B("_m2 ") + xstring + B("_b2\n") +
        B("o3 LINCOM ") +
        xstring + B("_i1 ") + xstring + B("_m1 ") + xstring + B("_b1 ") +
        xstring + B("_i2 ") + xstring + B("_m2 ") + xstring + B("_b2 ") +
        xstring + B("_i3 ") + xstring + B("_m3 ") + xstring + B("_b3\n") +
        B("b1 BIT ") + xstring + B("_i ") +
        xstring + B("_bn ") + xstring + B("_nb\n") +
        B("e1 RECIP in ") + xstring + B("_dv\n") +
        B("p1 PHASE ") + xstring + B("_i ") + xstring + B("_ps\n") +
        B("m1 MPLEX a b ") + xstring + B("_cv ") + xstring + B("_pd\n") +
        B("y1 POLYNOM ") + xstring + B("_i ") + xstring + B("_y1 2 ") +
        xstring + B("_y3\n") +
        B("w1 WINDOW a b EQ ") + xstring + B("_t1\n") +
        B("w2 WINDOW a b SET ") + xstring + B("_t2\n") +
        B("w3 WINDOW a b GT ") + xstring + B("_t3\n") +
        B("d1 DIVIDE ") + xstring + B("_i1 ") + xstring + B("_i2\n")
        )
f.close()

try:
  D=pygetdata.dirfile("dirfile", pygetdata.RDONLY)
except pygetdata.DirfileError:
  CheckOK(0)

# Attempt 3: use an incorrect correct character encoding
D.character_encoding = 'utf-8'

CheckSimple(1,D.character_encoding,'utf-8')

try:
  D.validate(estring)
except pygetdata.DirfileError:
  CheckEOS(2,D.error_string,estring)

try:
  e = 1
  c = D.carrays(return_type=pygetdata.NULL)
except UnicodeDecodeError:
  e = 0

CheckExc(0)

try:
  e = 1
  c = D.constants(return_type=pygetdata.UINT8)
except UnicodeDecodeError:
  e = 0

CheckExc(1)

try:
  e = 1
  c = D.mcarrays("l1", return_type=pygetdata.NULL)
except UnicodeDecodeError:
  e = 0

CheckExc(2)

try:
  e = 1
  c = D.mconstants("l1", return_type=pygetdata.UINT8)
except UnicodeDecodeError:
  e = 0

CheckExc(2)

try:
  e = 1
  c = D.strings()
except UnicodeDecodeError:
  e = 0

CheckExc(3)

try:
  e = 1
  c = D.mstrings("l1")
except UnicodeDecodeError:
  e = 0

CheckExc(4)

try:
  e = 1
  c = D.reference
except UnicodeDecodeError:
  e = 0

CheckExc(5)

try:
  e = 1
  c = D.get_string("s1")
except UnicodeDecodeError:
  e = 0

CheckExc(6)

try:
  e = 1
  c = D.alias_target(estring + B("_al"))
except UnicodeDecodeError:
  e = 0

CheckExc(7)

c = D.entry("r2")
try:
  e = 1
  print (c.spf)
except UnicodeDecodeError:
  e = 0

CheckExc(8)
CheckParms(9,c)

# This works because LINTERP table is FS encoded
c = D.entry("l1")
CheckSimple(10,c.parameters,("in", fstring))

c = D.entry("d1")
CheckIns(11, c)
CheckParms(12,c)

c = D.entry("e1")
try:
  e = 1
  print (c.dividend)
except UnicodeDecodeError:
  e = 0

CheckExc(13)
CheckParms(14,c)

c = D.entry("p1")
try:
  e = 1
  print (c.shift)
except UnicodeDecodeError:
  e = 0

CheckExc(15)
CheckIns(16,c)
CheckParms(17,c)

c = D.entry("y1")
try:
  e = 1
  print (c.a)
except UnicodeDecodeError:
  e = 0

CheckExc(18)
CheckParms(19, c)

c = D.entry("o1")
try:
  e = 1
  print (c.m)
except UnicodeDecodeError:
  e = 0

CheckExc(20)
try:
  e = 1
  print (c.b)
except UnicodeDecodeError:
  e = 0

CheckExc(21)
CheckIns(22,c)
CheckParms(23,c)

c = D.entry("o2")
try:
  e = 1
  print (c.m)
except UnicodeDecodeError:
  e = 0

CheckExc(24)
try:
  e = 1
  print (c.b)
except UnicodeDecodeError:
  e = 0

CheckExc(25)
CheckIns(26,c)
CheckParms(27,c)

c = D.entry("o3")
try:
  e = 1
  print (c.m)
except UnicodeDecodeError:
  e = 0

CheckExc(28)
try:
  e = 1
  print (c.b)
except UnicodeDecodeError:
  e = 0

CheckExc(29)
CheckIns(30,c)
CheckParms(31,c)

c = D.entry("w1")
CheckParms(32,c)

c = D.entry("w2")
CheckParms(33,c)

c = D.entry("w3")
CheckParms(34,c)

c = D.entry("m1")
try:
  e = 1
  print (c.count_val)
except UnicodeDecodeError:
  e = 0

CheckExc(35)
try:
  e = 1
  print (c.period)
except UnicodeDecodeError:
  e = 0

CheckExc(36)
CheckParms(35,c)

D.discard()
del D
os.system("rm -rf dirfile")

if (ne > 0):
  print ("ne = ",ne)
  sys.exit(1)