Skip to content

Commit

Permalink
Remove unicode compatibility fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Jan 23, 2022
1 parent 1defc71 commit 1fb4cc8
Show file tree
Hide file tree
Showing 46 changed files with 172 additions and 178 deletions.
27 changes: 13 additions & 14 deletions pony/converting.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# coding: cp1251

from __future__ import absolute_import, print_function
from pony.py23compat import unicode

import re
from datetime import datetime, date, time, timedelta
Expand Down Expand Up @@ -226,23 +225,23 @@ def timedelta2str(td):
return '-' + result

converters = {
int: (int, unicode, 'Incorrect number'),
float: (float, unicode, 'Must be a real number'),
'IP': (check_ip, unicode, 'Incorrect IP address'),
'positive': (check_positive, unicode, 'Must be a positive number'),
'identifier': (check_identifier, unicode, 'Incorrect identifier'),
'ISBN': (check_isbn, unicode, 'Incorrect ISBN'),
'email': (check_email, unicode, 'Incorrect e-mail address'),
'rfc2822_email': (check_rfc2822_email, unicode, 'Must be correct e-mail address'),
date: (str2date, unicode, 'Must be correct date (mm/dd/yyyy or dd.mm.yyyy)'),
time: (str2time, unicode, 'Must be correct time (hh:mm or hh:mm:ss)'),
datetime: (str2datetime, unicode, 'Must be correct date & time'),
int: (int, str, 'Incorrect number'),
float: (float, str, 'Must be a real number'),
'IP': (check_ip, str, 'Incorrect IP address'),
'positive': (check_positive, str, 'Must be a positive number'),
'identifier': (check_identifier, str, 'Incorrect identifier'),
'ISBN': (check_isbn, str, 'Incorrect ISBN'),
'email': (check_email, str, 'Incorrect e-mail address'),
'rfc2822_email': (check_rfc2822_email, str, 'Must be correct e-mail address'),
date: (str2date, str, 'Must be correct date (mm/dd/yyyy or dd.mm.yyyy)'),
time: (str2time, str, 'Must be correct time (hh:mm or hh:mm:ss)'),
datetime: (str2datetime, str, 'Must be correct date & time'),
}

def str2py(value, type):
if type is None or not isinstance(value, unicode): return value
if type is None or not isinstance(value, str): return value
if isinstance(type, tuple): str2py, py2str, err_msg = type
else: str2py, py2str, err_msg = converters.get(type, (type, unicode, None))
else: str2py, py2str, err_msg = converters.get(type, (type, str, None))
try: return str2py(value)
except ValidationError: raise
except:
Expand Down
6 changes: 3 additions & 3 deletions pony/orm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2199,8 +2199,8 @@ def validate(attr, val, obj=None, entity=None, from_db=False):
if from_db: return converter.sql2py(val)
val = converter.validate(val, obj)
except UnicodeDecodeError as e:
throw(ValueError, 'Value for attribute %s cannot be converted to %s: %s'
% (attr, unicode.__name__, truncate_repr(val)))
throw(ValueError,
'Value for attribute %s cannot be converted to str: %s' % (attr, truncate_repr(val)))
else:
rentity = reverse.entity
if not isinstance(val, rentity):
Expand Down Expand Up @@ -4324,7 +4324,7 @@ def _parse_row_(entity, row, attr_offsets):
discr_offset = attr_offsets[discr_attr][0]
discr_value = discr_attr.validate(row[discr_offset], None, entity, from_db=True)
real_entity_subclass = discr_attr.code2cls[discr_value]
discr_value = real_entity_subclass._discriminator_ # To convert unicode to str in Python 2.x
discr_value = real_entity_subclass._discriminator_ # To convert str to str in Python 2.x

database = entity._database_
cache = local.db2cache[database]
Expand Down
8 changes: 4 additions & 4 deletions pony/orm/dbapiprovider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import, print_function, division
from pony.py23compat import unicode, buffer, int_types
from pony.py23compat import buffer, int_types

import os, re, json
from decimal import Decimal, InvalidOperation
Expand Down Expand Up @@ -459,8 +459,8 @@ def init(converter, kwargs):
converter.db_encoding = kwargs.pop('db_encoding', None)
converter.autostrip = kwargs.pop('autostrip', True)
def validate(converter, val, obj=None):
if not isinstance(val, unicode): throw(TypeError,
'Value type for attribute %s must be %s. Got: %r' % (converter.attr, unicode.__name__, type(val)))
if not isinstance(val, str): throw(TypeError,
'Value type for attribute %s must be str. Got: %r' % (converter.attr, type(val)))
if converter.autostrip: val = val.strip()
max_len = converter.max_len
val_len = len(val)
Expand Down Expand Up @@ -822,7 +822,7 @@ def sql_type(converter):
class ArrayConverter(Converter):
array_types = {
int: ('int', IntConverter),
unicode: ('text', StrConverter),
str: ('text', StrConverter),
float: ('real', RealConverter)
}

Expand Down
4 changes: 2 additions & 2 deletions pony/orm/dbproviders/cockroach.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import
from pony.py23compat import unicode, buffer, int_types
from pony.py23compat import buffer, int_types

from decimal import Decimal
from datetime import datetime, date, time, timedelta
Expand Down Expand Up @@ -60,7 +60,7 @@ def py2sql(converter, val):
class CRArrayConverter(PGArrayConverter):
array_types = {
int: ('INT', PGIntConverter),
unicode: ('STRING', PGStrConverter),
str: ('STRING', PGStrConverter),
float: ('DOUBLE PRECISION', PGRealConverter)
}

Expand Down
4 changes: 2 additions & 2 deletions pony/orm/dbproviders/oracle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import
from pony.py23compat import unicode, buffer, int_types
from pony.py23compat import buffer, int_types

import os
os.environ["NLS_LANG"] = "AMERICAN_AMERICA.UTF8"
Expand Down Expand Up @@ -566,7 +566,7 @@ def output_type_handler(cursor, name, defaultType, size, precision, scale):
if scale != -127:
return cursor.var(cx_Oracle.STRING, 100, cursor.arraysize, outconverter=to_decimal)
elif defaultType in (cx_Oracle.STRING, cx_Oracle.FIXED_CHAR):
return cursor.var(unicode, size, cursor.arraysize) # from cx_Oracle example
return cursor.var(str, size, cursor.arraysize) # from cx_Oracle example
return None

class OraPool(object):
Expand Down
4 changes: 2 additions & 2 deletions pony/orm/dbproviders/postgres.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import
from pony.py23compat import unicode, buffer, int_types
from pony.py23compat import buffer, int_types

from decimal import Decimal
from datetime import datetime, date, time, timedelta
Expand Down Expand Up @@ -165,7 +165,7 @@ def sql_type(self):
class PGArrayConverter(dbapiprovider.ArrayConverter):
array_types = {
int: ('int', PGIntConverter),
unicode: ('text', dbapiprovider.StrConverter),
str: ('text', dbapiprovider.StrConverter),
float: ('double precision', PGRealConverter)
}

Expand Down
14 changes: 7 additions & 7 deletions pony/orm/dbproviders/sqlite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import
from pony.py23compat import buffer, int_types, unicode
from pony.py23compat import buffer, int_types

import os.path, sys, re, json
import sqlite3 as sqlite
Expand Down Expand Up @@ -174,7 +174,7 @@ def JSON_QUERY(builder, expr, path):
fname = 'json_extract' if builder.json1_available else 'py_json_extract'
path_sql, has_params, has_wildcards = builder.build_json_path(path)
return 'py_json_unwrap(', fname, '(', builder(expr), ', null, ', path_sql, '))'
json_value_type_mapping = {unicode: 'text', bool: 'integer', int: 'integer', float: 'real'}
json_value_type_mapping = {str: 'text', bool: 'integer', int: 'integer', float: 'real'}
def JSON_VALUE(builder, expr, path, type):
func_name = 'json_extract' if builder.json1_available else 'py_json_extract'
path_sql, has_params, has_wildcards = builder.build_json_path(path)
Expand Down Expand Up @@ -271,7 +271,7 @@ def dumps(items):
class SQLiteArrayConverter(dbapiprovider.ArrayConverter):
array_types = {
int: ('int', SQLiteIntConverter),
unicode: ('text', dbapiprovider.StrConverter),
str: ('text', dbapiprovider.StrConverter),
float: ('real', dbapiprovider.RealConverter)
}

Expand Down Expand Up @@ -502,18 +502,18 @@ def func(value):
if value is None:
return None
t = type(value)
if t is not unicode:
if t is not str:
if t is buffer:
value = hexlify(value).decode('ascii')
else:
value = unicode(value)
value = str(value)
result = base_func(value)
return result
func.__name__ = name
return func

py_upper = make_string_function('py_upper', unicode.upper)
py_lower = make_string_function('py_lower', unicode.lower)
py_upper = make_string_function('py_upper', str.upper)
py_lower = make_string_function('py_lower', str.lower)

def py_json_unwrap(value):
# [null,some-value] -> some-value
Expand Down
18 changes: 9 additions & 9 deletions pony/orm/ormtypes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import, print_function, division
from pony.py23compat import unicode, buffer, int_types
from pony.py23compat import buffer, int_types

import sys, types, weakref
from decimal import Decimal
Expand Down Expand Up @@ -159,8 +159,8 @@ def normalize(value):
entity = value.entity
return SetType(entity), entity

if isinstance(value, unicode):
return unicode, value
if isinstance(value, str):
return str, value

if t in function_types:
return FuncType(value), value
Expand All @@ -184,7 +184,7 @@ def normalize_type(t):
t = type_normalization_dict.get(t, t)
if t in primitive_types: return t
if t in (slice, type(Ellipsis)): return t
if issubclass(t, str): return unicode
if issubclass(t, str): return str
if issubclass(t, (dict, Json)): return Json
if issubclass(t, Array): return t
throw(TypeError, 'Unsupported type %r' % t.__name__)
Expand Down Expand Up @@ -218,7 +218,7 @@ def are_comparable_types(t1, t2, op='=='):
tt2 = type(t2)

t12 = {t1, t2}
if Json in t12 and t12 < {Json, str, unicode, int, bool, float}:
if Json in t12 and t12 < {Json, str, str, int, bool, float}:
return True
if op in ('in', 'not in'):
if tt2 is RawSQLType: return True
Expand Down Expand Up @@ -323,7 +323,7 @@ def get_untracked(self):

def validate_item(item_type, item):
if not isinstance(item, item_type):
if item_type is not unicode and hasattr(item, '__index__'):
if item_type is not str and hasattr(item, '__index__'):
return item.__index__()
throw(TypeError, 'Cannot store %r item in array of %r' % (type(item).__name__, item_type.__name__))
return item
Expand Down Expand Up @@ -377,22 +377,22 @@ class IntArray(Array):


class StrArray(Array):
item_type = unicode
item_type = str


class FloatArray(Array):
item_type = float


numeric_types = {bool, int, float, Decimal}
comparable_types = {int, float, Decimal, unicode, date, time, datetime, timedelta, bool, UUID, IntArray, StrArray, FloatArray}
comparable_types = {int, float, Decimal, str, date, time, datetime, timedelta, bool, UUID, IntArray, StrArray, FloatArray}
primitive_types = comparable_types | {buffer}
function_types = {type, types.FunctionType, types.BuiltinFunctionType}
type_normalization_dict = {}

array_types = {
int: IntArray,
float: FloatArray,
unicode: StrArray
str: StrArray
}

4 changes: 2 additions & 2 deletions pony/orm/sqlbuilding.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import, print_function, division
from pony.py23compat import unicode, buffer, int_types
from pony.py23compat import int_types

from operator import attrgetter
from decimal import Decimal
Expand Down Expand Up @@ -185,7 +185,7 @@ def __init__(builder, provider, ast):
if param.id is None: param.id = i + 1
layout.append(param.paramkey)
builder.layout = layout
builder.sql = u''.join(map(unicode, builder.result)).rstrip('\n')
builder.sql = u''.join(map(str, builder.result)).rstrip('\n')
if paramstyle in ('qmark', 'format'):
def adapter(values):
return tuple(param.eval(values) for param in params)
Expand Down
Loading

0 comments on commit 1fb4cc8

Please sign in to comment.