Skip to content

Commit

Permalink
Consolidate various Python 2/3 compatibility tests in pyrseas.lib.pyc…
Browse files Browse the repository at this point in the history
…ompat.

   pyrseas/lib/pycompat.py: Defines PY2 and strtypes.  PY2 is used to
   normalize various tests for Python 2.x.  strtypes is moved from
   pyrseas/dbobject/function.py so that it can be used elsewhere.

   Note: This breaks the autodoc and pagila functional tests under
   Python 2.x, due to use of yaml.safe_dump in change 1e4fa91.
  • Loading branch information
jmafc committed Sep 10, 2013
1 parent 1e4fa91 commit 629fe38
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 18 deletions.
10 changes: 5 additions & 5 deletions pyrseas/dbobject/__init__.py
Expand Up @@ -10,9 +10,9 @@
import os
import re
import string
import sys
from functools import wraps

from pyrseas.lib.pycompat import PY2, strtypes
from pyrseas.yamlutil import MultiLineStr, yamldump
from pyrseas.dbobject.privileges import privileges_to_map
from pyrseas.dbobject.privileges import add_grant, diff_privs
Expand Down Expand Up @@ -140,14 +140,14 @@ def __init__(self, **attrs):
for key, val in list(attrs.items()):
if val or key in self.keylist:
if key in ['definition', 'description', 'source'] and \
isinstance(val, str) and '\n' in val:
isinstance(val, strtypes) and '\n' in val:
newval = []
for line in val.split('\n'):
if line and line[-1] in (' ', '\t'):
line = line.rstrip()
newval.append(line)
strval = '\n'.join(newval)
if sys.version < '3':
if PY2:
strval = strval.decode('utf_8')
val = MultiLineStr(strval)
setattr(self, key, val)
Expand Down Expand Up @@ -199,12 +199,12 @@ def xfrm_filename(objtype, objid=None):
:return: filename string
"""
if objid:
if sys.version < '3':
if PY2:
objid = objid.decode('utf_8')
filename = '%s.%.*s.yaml' % (
objtype, MAX_IDENT_LEN, re.sub(
NON_FILENAME_CHARS, '_', objid))
if sys.version < '3':
if PY2:
filename = filename.encode('utf_8')
else:
filename = '%s.yaml' % objtype.replace(' ', '_')
Expand Down
7 changes: 1 addition & 6 deletions pyrseas/dbobject/function.py
Expand Up @@ -7,16 +7,11 @@
DbSchemaObject, Function and Aggregate derived from Proc, and
FunctionDict derived from DbObjectDict.
"""
import sys

from pyrseas.lib.pycompat import strtypes
from pyrseas.dbobject import DbObjectDict, DbSchemaObject
from pyrseas.dbobject import commentable, ownable, grantable, split_schema_obj
from pyrseas.dbobject.privileges import privileges_from_map

strtypes = (str, )
if sys.version_info[0] == 2:
strtypes = (str, unicode)

VOLATILITY_TYPES = {'i': 'immutable', 's': 'stable', 'v': 'volatile'}


Expand Down
3 changes: 2 additions & 1 deletion pyrseas/dbobject/table.py
Expand Up @@ -10,6 +10,7 @@
"""
import sys

from pyrseas.lib.pycompat import PY2
from pyrseas.dbobject import DbObjectDict, DbSchemaObject
from pyrseas.dbobject import quote_id, split_schema_obj
from pyrseas.dbobject import commentable, ownable, grantable
Expand Down Expand Up @@ -96,7 +97,7 @@ def to_map(self, opts):
elif key == 'min_value' and val == 1:
seq[key] = None
else:
if sys.version < '3':
if PY2:
if isinstance(val, (int, long)) and val <= sys.maxsize:
seq[key] = int(val)
else:
Expand Down
5 changes: 4 additions & 1 deletion pyrseas/lib/dbconn.py
Expand Up @@ -10,7 +10,10 @@

from psycopg2 import connect
from psycopg2.extras import DictConnection
if sys.version_info[0] == 2:

from .pycompat import PY2

if PY2:
from psycopg2.extensions import register_type, UNICODE
register_type(UNICODE)

Expand Down
15 changes: 15 additions & 0 deletions pyrseas/lib/pycompat.py
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
"""
pyrseas.lib.pycompat
~~~~~~~~~~~~~~~~~~~~
Helpers for compatibility between Python 2.x and 3.x.
"""
import sys

PY2 = sys.version_info[0] == 2

if not PY2:
strtypes = (str, )
else:
strtypes = (str, unicode)
5 changes: 2 additions & 3 deletions pyrseas/relation/attribute.py
Expand Up @@ -2,7 +2,7 @@
"""
pyrseas.relation.attribute
"""
import sys
from pyrseas.lib.pycompat import PY2


class Attribute(object):
Expand All @@ -25,8 +25,7 @@ def __init__(self, name, type_=str, value=None, nullable=False,
float(value) == value):
value = float(value)
if not isinstance(value, type_):
if not (sys.version_info < (3, 0) and type_ == str
and isinstance(value, unicode)):
if not (PY2 and type_ == str and isinstance(value, unicode)):
raise ValueError("Value (%s) of %r is not of type '%s'" %
(value, self, type_.__name__))
self.value = value
Expand Down
7 changes: 5 additions & 2 deletions pyrseas/yamlutil.py
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-
"""Pyrseas YAML utilities"""

import sys
from yaml import add_representer, dump

if sys.version_info[0] == 2:
from pyrseas.lib.pycompat import PY2


if PY2:
from yaml import safe_dump as dump

class MultiLineStr(unicode):
Expand Down

0 comments on commit 629fe38

Please sign in to comment.