summaryrefslogtreecommitdiffstats
path: root/generator/nanopb_generator.py
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2015-09-26 12:32:12 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2015-09-26 12:32:12 +0300
commit3cb3872a1adc7e8f098595cc64f3c7ac820928d7 (patch)
treef8469ad754a7c49fead0ff0fa84d4caef88a835c /generator/nanopb_generator.py
parentc13b3676617131c9449568ddb8ea48ba94717dcc (diff)
Clean up the python2/python3 string type handling
Diffstat (limited to 'generator/nanopb_generator.py')
-rwxr-xr-xgenerator/nanopb_generator.py19
1 files changed, 9 insertions, 10 deletions
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 7fe0db95..37f7beb5 100755
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -74,6 +74,12 @@ intsizes = {
nanopb_pb2.IS_64: 'int64_t',
}
+# String types (for python 2 / python 3 compatibility)
+try:
+ strtypes = (unicode, str)
+except NameError:
+ strtypes = (str, )
+
class Names:
'''Keeps a set of nested names and formats them to C identifier.'''
def __init__(self, parts = ()):
@@ -85,14 +91,7 @@ class Names:
return '_'.join(self.parts)
def __add__(self, other):
- # The fdesc names are unicode and need to be handled for
- # python2 and python3
- try:
- realstr = unicode
- except NameError:
- realstr = str
-
- if isinstance(other, realstr):
+ if isinstance(other, strtypes):
return Names(self.parts + (other,))
elif isinstance(other, tuple):
return Names(self.parts + other)
@@ -126,7 +125,7 @@ class EncodedSize:
'''Class used to represent the encoded size of a field or a message.
Consists of a combination of symbolic sizes and integer sizes.'''
def __init__(self, value = 0, symbols = []):
- if isinstance(value, (str, Names)):
+ if isinstance(value, strtypes + (Names,)):
symbols = [str(value)]
value = 0
self.value = value
@@ -135,7 +134,7 @@ class EncodedSize:
def __add__(self, other):
if isinstance(other, int):
return EncodedSize(self.value + other, self.symbols)
- elif isinstance(other, (str, Names)):
+ elif isinstance(other, strtypes + (Names,)):
return EncodedSize(self.value, self.symbols + [str(other)])
elif isinstance(other, EncodedSize):
return EncodedSize(self.value + other.value, self.symbols + other.symbols)