summaryrefslogtreecommitdiff
path: root/cffi
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2020-07-24 12:56:14 +0200
committerArmin Rigo <arigo@tunes.org>2020-07-24 12:56:14 +0200
commit102d52ff9669b55dfa12fff2563483948065fa3b (patch)
tree612f01c49786cb9764f9baa2208d6cb8afce5a9a /cffi
parent764eadc587f8ab2d67389bb1c1f850921b9f1bae (diff)
downloadcffi-102d52ff9669b55dfa12fff2563483948065fa3b.tar.gz
Allow ffi.embedding_init_code("unicode-with-non-ascii-chars")
Diffstat (limited to 'cffi')
-rw-r--r--cffi/recompiler.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/cffi/recompiler.py b/cffi/recompiler.py
index d66ff7f..1309572 100644
--- a/cffi/recompiler.py
+++ b/cffi/recompiler.py
@@ -1296,14 +1296,28 @@ class Recompiler:
def _print_string_literal_in_array(self, s):
prnt = self._prnt
prnt('// # NB. this is not a string because of a size limit in MSVC')
+ if not isinstance(s, bytes): # unicode
+ s = s.encode('utf-8') # -> bytes
+ else:
+ s.decode('utf-8') # got bytes, check for valid utf-8
+ try:
+ s.decode('ascii')
+ except UnicodeDecodeError:
+ s = b'# -*- encoding: utf8 -*-\n' + s
for line in s.splitlines(True):
- prnt(('// ' + line).rstrip())
+ comment = line
+ if type('//') is bytes: # python2
+ line = map(ord, line) # make a list of integers
+ else: # python3
+ # type(line) is bytes, which enumerates like a list of integers
+ comment = ascii(comment)[1:-1]
+ prnt(('// ' + comment).rstrip())
printed_line = ''
for c in line:
if len(printed_line) >= 76:
prnt(printed_line)
printed_line = ''
- printed_line += '%d,' % (ord(c),)
+ printed_line += '%d,' % (c,)
prnt(printed_line)
# ----------