Skip to content

Commit

Permalink
compat: support unicode in environment variables
Browse files Browse the repository at this point in the history
On Python2 we have to encode provide utf-8 bytestrings if we want
unicode values in enviornment variables.  Python3 accepts them as-is.

Related-to: #282
Reported-by: V字龍(Vdragon) <pika1021@gmail.com>
Signed-off-by: David Aguilar <davvid@gmail.com>
  • Loading branch information
davvid committed Jun 7, 2014
1 parent c0120be commit 3084fe2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cola/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
except ImportError:
import urllib


def setenv(key, value):
"""Compatibility wrapper for setting environment variables
Why? win32 requires putenv(). UNIX only requires os.environ.
"""
if not PY3 and type(value) is ustr:
value = value.encode('utf-8', 'replace')
os.environ[key] = value
os.putenv(key, value)

Expand Down
30 changes: 30 additions & 0 deletions test/compat_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from __future__ import unicode_literals

import os
import unittest

from cola import compat


class CompatTestCase(unittest.TestCase):
"""Tests the compat module"""

def test_setenv(self):
"""Test the core.decode function
"""
key = 'COLA_UNICODE_TEST'
value = '字龍'
compat.setenv(key, value)
self.assertTrue(key in os.environ)
self.assertTrue(os.getenv(key))

compat.unsetenv(key)
self.assertFalse(key in os.environ)
self.assertFalse(os.getenv(key))


if __name__ == '__main__':
unittest.main()

0 comments on commit 3084fe2

Please sign in to comment.