Permalink
Browse files

Encapsulate `write`/`buffer` Python 2 vs. Python 3 check into a utili…

…ty method (`fwrite`) in pip.backwardcompat.

Refs pypa#374 (comment)
  • Loading branch information...
1 parent a790180 commit efe449576e1cbcbcffb2ca6ba067716aa4f64f35 @msabramo committed Dec 27, 2011
Showing with 11 additions and 5 deletions.
  1. +7 −0 pip/backwardcompat.py
  2. +4 −5 pip/log.py
View
7 pip/backwardcompat.py
@@ -66,6 +66,9 @@ def console_to_str(s):
except UnicodeDecodeError:
return s.decode('utf_8')
+ def fwrite(f, s):
+ f.buffer.write(b(s))
+
bytes = bytes
string_types = (str,)
raw_input = input
@@ -90,6 +93,10 @@ def u(s):
def console_to_str(s):
return s
+
+ def fwrite(f, s):
+ f.write(s)
+
bytes = str
string_types = (basestring,)
reduce = reduce
View
9 pip/log.py
@@ -4,6 +4,8 @@
import sys
import logging
+import pip.backwardcompat
+
class Logger(object):
@@ -70,12 +72,9 @@ def log(self, level, msg, *args, **kw):
if self.explicit_levels:
## FIXME: should this be a name, not a level number?
rendered = '%02i %s' % (level, rendered)
- if hasattr(consumer, 'buffer'): # Python 3
- rendered += '\n'
- consumer.buffer.write(rendered.encode('UTF-8'))
- elif hasattr(consumer, 'write'):
+ if hasattr(consumer, 'write'):
rendered += '\n'
- consumer.write(rendered)
+ pip.backwardcompat.fwrite(consumer, rendered)
else:
consumer(rendered)

0 comments on commit efe4495

Please sign in to comment.