[yams] deal with py3k string types and builtin module

Related to #267910.

authorJulien Cristau <julien.cristau@logilab.fr>
changeset18791c4c4ae2
branchdefault
phasepublic
hiddenno
parent revision#5a242c279995 [diff] deal with py3k string types
child revision#704215d49a58 [exceptions] don't define __str__ in terms of unicode on py3k
files modified by this revision
__init__.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1413451501 -7200
# Thu Oct 16 11:25:01 2014 +0200
# Node ID 18791c4c4ae28dd99ddeb6ef44600668cc7cbfa0
# Parent 5a242c279995837b1464ad131f11aef592389767
[yams] deal with py3k string types and builtin module

Related to #267910.

diff --git a/__init__.py b/__init__.py
@@ -20,13 +20,15 @@
1  __docformat__ = "restructuredtext en"
2 
3  import warnings
4  from datetime import datetime, date, time
5 
6 +from six import string_types, text_type
7 +from six.moves import builtins
8 +
9  # XXX set _ builtin to unicode by default, should be overriden if necessary
10 -import __builtin__
11 -__builtin__._ = unicode
12 +builtins._ = text_type
13 
14  from logilab.common.date import strptime, strptime_time
15  from logilab.common import nullobject
16 
17  from yams.__pkginfo__ import version as __version__
@@ -77,11 +79,11 @@
18 
19 
20  def convert_default_value(rdef, default):
21      # rdef can be either a yams.schema.RelationDefinitionSchema or a yams.buildobjs.RelationDefinition
22      rtype = getattr(rdef, 'name', None) or rdef.rtype.type
23 -    if isinstance(default, basestring) and rdef.object != 'String':
24 +    if isinstance(default, string_types) and rdef.object != 'String':
25          # real Strings can be anything, including things that look like keywords
26          # for other base types
27          if rdef.object in KEYWORD_MAP:
28              try:
29                  return KEYWORD_MAP[rdef.object][default.upper()]()
@@ -99,11 +101,11 @@
30              except ValueError as verr:
31                  raise ValueError('creating a default value for attribute %s of type %s '
32                                   'from the string %r is not supported (cause %s)'
33                                   % (rtype, rdef.object, default, verr))
34      if rdef.object == 'String':
35 -        default = unicode(default)
36 +        default = text_type(default)
37      return default # general case: untouched default
38 
39 
40 
41  KNOWN_METAATTRIBUTES = set(('format', 'encoding', 'name'))