Python sys.getdefaultencoding() Examples
The following are 30 code examples for showing how to use sys.getdefaultencoding(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
sys
, or try the search function
.
Example 1
Project: jawfish Author: war-and-code File: calendar.py License: MIT License | 6 votes |
def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None): """ Return a formatted year as a complete HTML page. """ if encoding is None: encoding = sys.getdefaultencoding() v = [] a = v.append a('<?xml version="1.0" encoding="%s"?>\n' % encoding) a('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n') a('<html>\n') a('<head>\n') a('<meta http-equiv="Content-Type" content="text/html; charset=%s" />\n' % encoding) if css is not None: a('<link rel="stylesheet" type="text/css" href="%s" />\n' % css) a('<title>Calendar for %d</title>\n' % theyear) a('</head>\n') a('<body>\n') a(self.formatyear(theyear, width)) a('</body>\n') a('</html>\n') return ''.join(v).encode(encoding, "xmlcharrefreplace")
Example 2
Project: py Author: pytest-dev File: local.py License: MIT License | 6 votes |
def write(self, data, mode='w', ensure=False): """ write data into path. If ensure is True create missing parent directories. """ if ensure: self.dirpath().ensure(dir=1) if 'b' in mode: if not py.builtin._isbytes(data): raise ValueError("can only process bytes") else: if not py.builtin._istext(data): if not py.builtin._isbytes(data): data = str(data) else: data = py.builtin._totext(data, sys.getdefaultencoding()) f = self.open(mode) try: f.write(data) finally: f.close()
Example 3
Project: py Author: pytest-dev File: local.py License: MIT License | 6 votes |
def sysexec(self, *argv, **popen_opts): """ return stdout text from executing a system child process, where the 'self' path points to executable. The process is directly invoked and not through a system shell. """ from subprocess import Popen, PIPE argv = map_as_list(str, argv) popen_opts['stdout'] = popen_opts['stderr'] = PIPE proc = Popen([str(self)] + argv, **popen_opts) stdout, stderr = proc.communicate() ret = proc.wait() if py.builtin._isbytes(stdout): stdout = py.builtin._totext(stdout, sys.getdefaultencoding()) if ret != 0: if py.builtin._isbytes(stderr): stderr = py.builtin._totext(stderr, sys.getdefaultencoding()) raise py.process.cmdexec.Error(ret, ret, str(self), stdout, stderr,) return stdout
Example 4
Project: py Author: pytest-dev File: cmdexec.py License: MIT License | 6 votes |
def cmdexec(cmd): """ return unicode output of executing 'cmd' in a separate process. raise cmdexec.Error exeception if the command failed. the exception will provide an 'err' attribute containing the error-output from the command. if the subprocess module does not provide a proper encoding/unicode strings sys.getdefaultencoding() will be used, if that does not exist, 'UTF-8'. """ process = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = process.communicate() if sys.version_info[0] < 3: # on py3 we get unicode strings, on py2 not try: default_encoding = sys.getdefaultencoding() # jython may not have it except AttributeError: default_encoding = sys.stdout.encoding or 'UTF-8' out = unicode(out, process.stdout.encoding or default_encoding) err = unicode(err, process.stderr.encoding or default_encoding) status = process.poll() if status: raise ExecutionFailed(status, status, cmd, out, err) return out
Example 5
Project: jbox Author: jpush File: pyparsing.py License: MIT License | 6 votes |
def _ustr(obj): """Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It then < returns the unicode object | encodes it with the default encoding | ... >. """ if isinstance(obj,unicode): return obj try: # If this works, then _ustr(obj) has the same behaviour as str(obj), so # it won't break any existing code. return str(obj) except UnicodeEncodeError: # Else encode it ret = unicode(obj).encode(sys.getdefaultencoding(), 'xmlcharrefreplace') xmlcharref = Regex('&#\d+;') xmlcharref.setParseAction(lambda t: '\\u' + hex(int(t[0][2:-1]))[2:]) return xmlcharref.transformString(ret) # build list of single arg builtins, tolerant of Python version, that can be used as parse actions
Example 6
Project: python-netsurv Author: sofia-netsurv File: local.py License: MIT License | 6 votes |
def write(self, data, mode='w', ensure=False): """ write data into path. If ensure is True create missing parent directories. """ if ensure: self.dirpath().ensure(dir=1) if 'b' in mode: if not py.builtin._isbytes(data): raise ValueError("can only process bytes") else: if not py.builtin._istext(data): if not py.builtin._isbytes(data): data = str(data) else: data = py.builtin._totext(data, sys.getdefaultencoding()) f = self.open(mode) try: f.write(data) finally: f.close()
Example 7
Project: python-netsurv Author: sofia-netsurv File: local.py License: MIT License | 6 votes |
def sysexec(self, *argv, **popen_opts): """ return stdout text from executing a system child process, where the 'self' path points to executable. The process is directly invoked and not through a system shell. """ from subprocess import Popen, PIPE argv = map_as_list(str, argv) popen_opts['stdout'] = popen_opts['stderr'] = PIPE proc = Popen([str(self)] + argv, **popen_opts) stdout, stderr = proc.communicate() ret = proc.wait() if py.builtin._isbytes(stdout): stdout = py.builtin._totext(stdout, sys.getdefaultencoding()) if ret != 0: if py.builtin._isbytes(stderr): stderr = py.builtin._totext(stderr, sys.getdefaultencoding()) raise py.process.cmdexec.Error(ret, ret, str(self), stdout, stderr,) return stdout
Example 8
Project: python-netsurv Author: sofia-netsurv File: cmdexec.py License: MIT License | 6 votes |
def cmdexec(cmd): """ return unicode output of executing 'cmd' in a separate process. raise cmdexec.Error exeception if the command failed. the exception will provide an 'err' attribute containing the error-output from the command. if the subprocess module does not provide a proper encoding/unicode strings sys.getdefaultencoding() will be used, if that does not exist, 'UTF-8'. """ process = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = process.communicate() if sys.version_info[0] < 3: # on py3 we get unicode strings, on py2 not try: default_encoding = sys.getdefaultencoding() # jython may not have it except AttributeError: default_encoding = sys.stdout.encoding or 'UTF-8' out = unicode(out, process.stdout.encoding or default_encoding) err = unicode(err, process.stderr.encoding or default_encoding) status = process.poll() if status: raise ExecutionFailed(status, status, cmd, out, err) return out
Example 9
Project: python-netsurv Author: sofia-netsurv File: local.py License: MIT License | 6 votes |
def write(self, data, mode='w', ensure=False): """ write data into path. If ensure is True create missing parent directories. """ if ensure: self.dirpath().ensure(dir=1) if 'b' in mode: if not py.builtin._isbytes(data): raise ValueError("can only process bytes") else: if not py.builtin._istext(data): if not py.builtin._isbytes(data): data = str(data) else: data = py.builtin._totext(data, sys.getdefaultencoding()) f = self.open(mode) try: f.write(data) finally: f.close()
Example 10
Project: python-netsurv Author: sofia-netsurv File: cmdexec.py License: MIT License | 6 votes |
def cmdexec(cmd): """ return unicode output of executing 'cmd' in a separate process. raise cmdexec.Error exeception if the command failed. the exception will provide an 'err' attribute containing the error-output from the command. if the subprocess module does not provide a proper encoding/unicode strings sys.getdefaultencoding() will be used, if that does not exist, 'UTF-8'. """ process = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = process.communicate() if sys.version_info[0] < 3: # on py3 we get unicode strings, on py2 not try: default_encoding = sys.getdefaultencoding() # jython may not have it except AttributeError: default_encoding = sys.stdout.encoding or 'UTF-8' out = unicode(out, process.stdout.encoding or default_encoding) err = unicode(err, process.stderr.encoding or default_encoding) status = process.poll() if status: raise ExecutionFailed(status, status, cmd, out, err) return out
Example 11
Project: meddle Author: glmcdona File: calendar.py License: MIT License | 6 votes |
def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None): """ Return a formatted year as a complete HTML page. """ if encoding is None: encoding = sys.getdefaultencoding() v = [] a = v.append a('<?xml version="1.0" encoding="%s"?>\n' % encoding) a('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n') a('<html>\n') a('<head>\n') a('<meta http-equiv="Content-Type" content="text/html; charset=%s" />\n' % encoding) if css is not None: a('<link rel="stylesheet" type="text/css" href="%s" />\n' % css) a('<title>Calendar for %d</title>\n' % theyear) a('</head>\n') a('<body>\n') a(self.formatyear(theyear, width)) a('</body>\n') a('</html>\n') return ''.join(v).encode(encoding, "xmlcharrefreplace")
Example 12
Project: ironpython2 Author: IronLanguages File: test_file.py License: Apache License 2.0 | 6 votes |
def test_encoding(self): f = file(self.temp_file, 'w') # we throw on flush, CPython throws on write, so both write & close need to catch try: f.write(u'\u6211') f.close() self.fail('UnicodeEncodeError should have been thrown') except UnicodeEncodeError: pass if hasattr(sys, "setdefaultencoding"): #and verify UTF8 round trips correctly setenc = sys.setdefaultencoding saved = sys.getdefaultencoding() try: setenc('utf8') with file(self.temp_file, 'w') as f: f.write(u'\u6211') with file(self.temp_file, 'r') as f: txt = f.read() self.assertEqual(txt, u'\u6211') finally: setenc(saved)
Example 13
Project: kobo-predict Author: awemulya File: pyparsing.py License: BSD 2-Clause "Simplified" License | 6 votes |
def _ustr(obj): """Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It then < returns the unicode object | encodes it with the default encoding | ... >. """ if isinstance(obj,unicode): return obj try: # If this works, then _ustr(obj) has the same behaviour as str(obj), so # it won't break any existing code. return str(obj) except UnicodeEncodeError: # Else encode it ret = unicode(obj).encode(sys.getdefaultencoding(), 'xmlcharrefreplace') xmlcharref = Regex('&#\d+;') xmlcharref.setParseAction(lambda t: '\\u' + hex(int(t[0][2:-1]))[2:]) return xmlcharref.transformString(ret) # build list of single arg builtins, tolerant of Python version, that can be used as parse actions
Example 14
Project: phpsploit Author: nil0x42 File: pyparsing.py License: GNU General Public License v3.0 | 6 votes |
def _ustr(obj): """Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It then < returns the unicode object | encodes it with the default encoding | ... >. """ if isinstance(obj,unicode): return obj try: # If this works, then _ustr(obj) has the same behaviour as str(obj), so # it won't break any existing code. return str(obj) except UnicodeEncodeError: # Else encode it ret = unicode(obj).encode(sys.getdefaultencoding(), 'xmlcharrefreplace') xmlcharref = Regex('&#\d+;') xmlcharref.setParseAction(lambda t: '\\u' + hex(int(t[0][2:-1]))[2:]) return xmlcharref.transformString(ret) # build list of single arg builtins, tolerant of Python version, that can be used as parse actions
Example 15
Project: py Author: pytest-dev File: svnwc.py License: MIT License | 5 votes |
def log(self, rev_start=None, rev_end=1, verbose=False): """ return a list of LogEntry instances for this path. rev_start is the starting revision (defaulting to the first one). rev_end is the last revision (defaulting to HEAD). if verbose is True, then the LogEntry instances also know which files changed. """ assert self.check() # make it simpler for the pipe rev_start = rev_start is None and "HEAD" or rev_start rev_end = rev_end is None and "HEAD" or rev_end if rev_start == "HEAD" and rev_end == 1: rev_opt = "" else: rev_opt = "-r %s:%s" % (rev_start, rev_end) verbose_opt = verbose and "-v" or "" locale_env = fixlocale() # some blather on stderr auth_opt = self._makeauthoptions() #stdin, stdout, stderr = os.popen3(locale_env + # 'svn log --xml %s %s %s "%s"' % ( # rev_opt, verbose_opt, auth_opt, # self.strpath)) cmd = locale_env + 'svn log --xml %s %s %s "%s"' % ( rev_opt, verbose_opt, auth_opt, self.strpath) popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, ) stdout, stderr = popen.communicate() stdout = py.builtin._totext(stdout, sys.getdefaultencoding()) minidom,ExpatError = importxml() try: tree = minidom.parseString(stdout) except ExpatError: raise ValueError('no such revision') result = [] for logentry in filter(None, tree.firstChild.childNodes): if logentry.nodeType == logentry.ELEMENT_NODE: result.append(LogEntry(logentry)) return result
Example 16
Project: py Author: pytest-dev File: test_local.py License: MIT License | 5 votes |
def test_read_write(self, tmpdir): x = tmpdir.join("hello") part = py.builtin._totext("hällo", "utf8") x.write(part) assert x.read() == part x.write(part.encode(sys.getdefaultencoding())) assert x.read() == part.encode(sys.getdefaultencoding())
Example 17
Project: linter-pylama Author: AtomLinter File: utils.py License: MIT License | 5 votes |
def safe_decode(line, encoding, *args, **kwargs): '''return decoded line from encoding or decode with default encoding''' try: return line.decode(encoding or sys.getdefaultencoding(), *args, **kwargs) except LookupError: return line.decode(sys.getdefaultencoding(), *args, **kwargs)
Example 18
Project: linter-pylama Author: AtomLinter File: utils.py License: MIT License | 5 votes |
def decoding_stream(stream, encoding, errors='strict'): try: reader_cls = codecs.getreader(encoding or sys.getdefaultencoding()) except LookupError: reader_cls = codecs.getreader(sys.getdefaultencoding()) return reader_cls(stream, errors)
Example 19
Project: linter-pylama Author: AtomLinter File: __init__.py License: MIT License | 5 votes |
def encode(self, string): if not isinstance(string, six.text_type): return string encoding = (getattr(self.out, 'encoding', None) or locale.getpreferredencoding(do_setlocale=False) or sys.getdefaultencoding()) # errors=replace, we don't want to crash when attempting to show # source code line that can't be encoded with the current locale # settings return string.encode(encoding, 'replace')
Example 20
Project: moler Author: nokia File: loggers.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def format(self, record): if not hasattr(record, 'transfer_direction'): record.transfer_direction = ' ' if not hasattr(record, 'log_name'): record.log_name = "" msg_lines = record.getMessage().splitlines(True) base_output = super(MultilineWithDirectionFormatter, self).format(record) out_lines = base_output.splitlines(True) output = out_lines[0] if len(msg_lines) >= 1: empty_prefix = self._calculate_empty_prefix(msg_lines[0], out_lines[0]) for line in out_lines[1:]: try: output += u"{}|{}".format(empty_prefix, line) except UnicodeDecodeError as err: if hasattr(err, "encoding"): encoding = err.encoding else: encoding = sys.getdefaultencoding() decoded_line = codecs.decode(line, encoding, 'replace') output += u"{}|{}".format(empty_prefix, decoded_line) # TODO: line completion for connection decoded data comming in chunks output = MolerMainMultilineWithDirectionFormatter._remove_duplicate_log_name(record, output) return output
Example 21
Project: vulscan Author: vulscanteam File: pyparsing.py License: MIT License | 5 votes |
def _ustr(obj): """Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It then < returns the unicode object | encodes it with the default encoding | ... >. """ if isinstance(obj,unicode): return obj try: # If this works, then _ustr(obj) has the same behaviour as str(obj), so # it won't break any existing code. return str(obj) except UnicodeEncodeError: # The Python docs (http://docs.python.org/ref/customization.html#l2h-182) # state that "The return value must be a string object". However, does a # unicode object (being a subclass of basestring) count as a "string # object"? # If so, then return a unicode object: return unicode(obj) # Else encode it... but how? There are many choices... :) # Replace unprintables with escape codes? #return unicode(obj).encode(sys.getdefaultencoding(), 'backslashreplace_errors') # Replace unprintables with question marks? #return unicode(obj).encode(sys.getdefaultencoding(), 'replace') # ... # build list of single arg builtins, tolerant of Python version, that can be used as parse actions
Example 22
Project: recruit Author: Frank-qlu File: _compat.py License: Apache License 2.0 | 5 votes |
def to_bytes(x, charset=sys.getdefaultencoding(), errors="strict"): if x is None: return None if isinstance(x, (bytes, bytearray, buffer)): return bytes(x) if isinstance(x, unicode): return x.encode(charset, errors) raise TypeError("Expected bytes")
Example 23
Project: recruit Author: Frank-qlu File: _compat.py License: Apache License 2.0 | 5 votes |
def to_native(x, charset=sys.getdefaultencoding(), errors="strict"): if x is None or isinstance(x, str): return x return x.encode(charset, errors)
Example 24
Project: recruit Author: Frank-qlu File: _compat.py License: Apache License 2.0 | 5 votes |
def to_bytes(x, charset=sys.getdefaultencoding(), errors="strict"): if x is None: return None if isinstance(x, (bytes, bytearray, memoryview)): # noqa return bytes(x) if isinstance(x, str): return x.encode(charset, errors) raise TypeError("Expected bytes")
Example 25
Project: recruit Author: Frank-qlu File: _compat.py License: Apache License 2.0 | 5 votes |
def to_native(x, charset=sys.getdefaultencoding(), errors="strict"): if x is None or isinstance(x, str): return x return x.decode(charset, errors)
Example 26
Project: recruit Author: Frank-qlu File: _compat.py License: Apache License 2.0 | 5 votes |
def to_unicode( x, charset=sys.getdefaultencoding(), errors="strict", allow_none_charset=False ): if x is None: return None if not isinstance(x, bytes): return text_type(x) if charset is None and allow_none_charset: return x return x.decode(charset, errors)
Example 27
Project: recruit Author: Frank-qlu File: testing.py License: Apache License 2.0 | 5 votes |
def set_defaultencoding(encoding): """ Set default encoding (as given by sys.getdefaultencoding()) to the given encoding; restore on exit. Parameters ---------- encoding : str """ if not PY2: raise ValueError("set_defaultencoding context is only available " "in Python 2.") orig = sys.getdefaultencoding() reload(sys) # noqa:F821 sys.setdefaultencoding(encoding) try: yield finally: sys.setdefaultencoding(orig) # ----------------------------------------------------------------------------- # Console debugging tools
Example 28
Project: recruit Author: Frank-qlu File: console.py License: Apache License 2.0 | 5 votes |
def detect_console_encoding(): """ Try to find the most capable encoding supported by the console. slightly modified from the way IPython handles the same issue. """ global _initial_defencoding encoding = None try: encoding = sys.stdout.encoding or sys.stdin.encoding except (AttributeError, IOError): pass # try again for something better if not encoding or 'ascii' in encoding.lower(): try: encoding = locale.getpreferredencoding() except Exception: pass # when all else fails. this will usually be "ascii" if not encoding or 'ascii' in encoding.lower(): encoding = sys.getdefaultencoding() # GH3360, save the reported defencoding at import time # MPL backends may change it. Make available for debugging. if not _initial_defencoding: _initial_defencoding = sys.getdefaultencoding() return encoding
Example 29
Project: recruit Author: Frank-qlu File: __init__.py License: Apache License 2.0 | 5 votes |
def display_path(path): """Gives the display value for a given path, making it relative to cwd if possible.""" path = os.path.normcase(os.path.abspath(path)) if sys.version_info[0] == 2: path = path.decode(sys.getfilesystemencoding(), 'replace') path = path.encode(sys.getdefaultencoding(), 'replace') if path.startswith(os.getcwd() + os.path.sep): path = '.' + path[len(os.getcwd()):] return path
Example 30
Project: recruit Author: Frank-qlu File: _compat.py License: Apache License 2.0 | 5 votes |
def get_filesystem_encoding(): return sys.getfilesystemencoding() or sys.getdefaultencoding()