MCPcopy Index your code
hub / github.com/python/cpython / codegen

Function codegen

Tools/unicode/gencodec.py:253–356  ·  view source on GitHub ↗

Returns Python source for the given map. Comments are included in the source, if comments is true (default).

(name, map, encodingname, comments=1)

Source from the content-addressed store, hash-verified

251 return l
252
253def codegen(name, map, encodingname, comments=1):
254
255 """ Returns Python source for the given map.
256
257 Comments are included in the source, if comments is true (default).
258
259 """
260 # Generate code
261 decoding_map_code = python_mapdef_code(
262 'decoding_map',
263 map,
264 comments=comments)
265 decoding_table_code = python_tabledef_code(
266 'decoding_table',
267 map,
268 comments=comments)
269 encoding_map_code = python_mapdef_code(
270 'encoding_map',
271 codecs.make_encoding_map(map),
272 comments=comments,
273 precisions=(4, 2))
274
275 if decoding_table_code:
276 suffix = 'table'
277 else:
278 suffix = 'map'
279
280 l = [
281 '''\
282""" Python Character Mapping Codec %s generated from '%s' with gencodec.py.
283
284"""#"
285
286import codecs
287
288### Codec APIs
289
290class Codec(codecs.Codec):
291
292 def encode(self, input, errors='strict'):
293 return codecs.charmap_encode(input, errors, encoding_%s)
294
295 def decode(self, input, errors='strict'):
296 return codecs.charmap_decode(input, errors, decoding_%s)
297''' % (encodingname, name, suffix, suffix)]
298 l.append('''\
299class IncrementalEncoder(codecs.IncrementalEncoder):
300 def encode(self, input, final=False):
301 return codecs.charmap_encode(input, self.errors, encoding_%s)[0]
302
303class IncrementalDecoder(codecs.IncrementalDecoder):
304 def decode(self, input, final=False):
305 return codecs.charmap_decode(input, self.errors, decoding_%s)[0]''' %
306 (suffix, suffix))
307
308 l.append('''
309class StreamWriter(Codec, codecs.StreamWriter):
310 pass

Callers 2

genwincodecFunction · 0.90
pymapFunction · 0.85

Calls 7

python_mapdef_codeFunction · 0.85
python_tabledef_codeFunction · 0.85
appendMethod · 0.45
replaceMethod · 0.45
extendMethod · 0.45
expandtabsMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…