summaryrefslogtreecommitdiff
path: root/factory/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'factory/utils.py')
-rw-r--r--factory/utils.py36
1 files changed, 25 insertions, 11 deletions
diff --git a/factory/utils.py b/factory/utils.py
index 276977a..15dba0a 100644
--- a/factory/utils.py
+++ b/factory/utils.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2010 Mark Sandstrom
-# Copyright (c) 2011-2013 Raphaël Barrois
+# Copyright (c) 2011-2015 Raphaël Barrois
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -101,7 +101,7 @@ def import_object(module_name, attribute_name):
def _safe_repr(obj):
try:
obj_repr = repr(obj)
- except UnicodeError:
+ except Exception:
return '<bad_repr object at %s>' % id(obj)
try: # Convert to "text type" (= unicode)
@@ -110,15 +110,29 @@ def _safe_repr(obj):
return obj_repr.decode('utf-8')
-def log_pprint(args=(), kwargs=None):
- kwargs = kwargs or {}
- return ', '.join(
- [_safe_repr(arg) for arg in args] +
- [
- '%s=%s' % (key, _safe_repr(value))
- for key, value in kwargs.items()
- ]
- )
+class log_pprint(object):
+ """Helper for properly printing args / kwargs passed to an object.
+
+ Since it is only used with factory.debug(), the computation is
+ performed lazily.
+ """
+ __slots__ = ['args', 'kwargs']
+
+ def __init__(self, args=(), kwargs=None):
+ self.args = args
+ self.kwargs = kwargs or {}
+
+ def __repr__(self):
+ return repr(str(self))
+
+ def __str__(self):
+ return ', '.join(
+ [_safe_repr(arg) for arg in self.args] +
+ [
+ '%s=%s' % (key, _safe_repr(value))
+ for key, value in self.kwargs.items()
+ ]
+ )
class ResetableIterator(object):