def test_to_str_py3(self): s = 'あいう' t = s.encode('utf-8') self.assertTrue(isinstance(s, str)) self.assertTrue(isinstance(string.to_str(s), str)) self.assertTrue(isinstance(t, bytes)) self.assertTrue(isinstance(string.to_str(t), str)) self.assertEqual(string.to_str(s), string.to_str(t))
def test_get_logger_unicode(self): path = '/tmp/__test_console_logger_2.log' self._clear([path]) out = console_logger.get_console_logger(path, 10000, 1) args = ['/bin/sh', '-c', to_str('echo "あいうえお"')] # Note: set shell=False to avoid a Python 3.2 bug subprocess.call(args=args, shell=False, cwd='/tmp', stdin=sys.stdin, stdout=out, stderr=sys.stderr) time.sleep(1) self.assertFalse(os.path.exists(path + '.1')) with open(path) as f: self.assertEqual([l[26:] for l in f.readlines()], [to_str('あいうえお\n')]) self._clear([path])
def _log(self, priority, message): if SYSLOG_AVAILABLE: if self.dry_run: print('Would write to syslog: priority=%s, message=%s' % (priority, message)) else: syslog.openlog(self.name, syslog.LOG_PID) syslog.syslog(priority, to_str(message, 'utf-8', 'ignore')) syslog.closelog()
def assertRaisesRegexp(self, expected_exception, expected_regexp, callable_obj=None, *args, **kwargs): """ Accept difference of the function name between PY2 and PY3. We don't use built-in assertRaisesRegexp because it is unicode-unsafe. """ encoding = 'utf-8' with self.assertRaises(expected_exception) as cm: callable_obj(*args, **kwargs) if six.PY2: try: msg = to_str(cm.exception, encoding) except UnicodeEncodeError: # avoid to use cm.exception.message msg = cm.exception.args[0] self.assertRegexpMatches(msg, expected_regexp) else: self.assertRegex(to_str(cm.exception, encoding), expected_regexp)
def assertRaisesMessage(self, expected_exception, expected_message, callable_obj=None, *args, **kwargs): """ Assert the expected exception is raised and its message is equal to expected. :param expected_exception: class: :param expected_message: string: :param callable_obj: function: :param args: args :param kwargs: kwargs """ encoding = 'utf-8' with self.assertRaises(expected_exception) as cm: callable_obj(*args, **kwargs) try: msg = to_str(cm.exception, encoding) except UnicodeEncodeError: # avoid to use cm.exception.message msg = cm.exception.args[0] self.assertEqual(msg, expected_message)
def get_console_logger(path, max_bytes, backup_count, encoding='utf-8'): """ Note: RotatingFileHandler in Python 2.6 sometimes cause a weird issue. see https://bugs.python.org/issue4749 :param path: :param max_bytes: :param backup_count: :param encoding: :return: """ logger = logging.getLogger('ConsoleLogger') logger.setLevel(logging.INFO) # add a handler handler = RotatingFileHandler(path, 'a', max_bytes, backup_count) handler.setFormatter(logging.Formatter(to_str('[%(asctime)s] %(message)s', encoding, 'ignore'))) logger.addHandler(handler) return LoggerWrapper(logger, logging.INFO)
def _log(self, priority, message): if SYSLOG_AVAILABLE: syslog.openlog(self.name, syslog.LOG_PID) syslog.syslog(priority, to_str(message, self.encoding)) syslog.closelog()
def test_to_str(self): self.assertEqual(string.to_str(b'abc'), string.to_str('abc')) self.assertEqual(string.to_str(1.23), '1.23')