python3の組み込み関数bytesはiterable なシーケンスを受け取る!
>>> bytes('a', 'ascii') b'a' >>> bytes(97, 'ascii') b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' >>> bytes(97) b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' >>> bytes([97], 'ascii') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: encoding or errors without a string argument >>> bytes([97]) b'a'
ややこしいな。そして美しくない。。。
もちろん、そもそもbytes型は旧strと同じようにシーケンスだから、数値のリストと等価であることはわかる。
だったらchrとordに相当するbyte(int), int(byte)みたいなのを用意してほしかったなあ。
Python3.2, windows7でvirtualenv(1.6.1)が失敗する件
追記: virtualenv1.6.4では付属のdistributeが最新のものに変更されたため、下のような問題は起きなくなりました。
というかタイミング悪すぎ俺orz
こんなのに半日費やしちゃったよ。
それはさておき、python3.2にvirtualenvを入れようとするとどうも付属のdistributeのバージョンが古すぎて
うまくいかない模様だと言う事がわかった。
しょうがないので、Lib\site-packages\virtualenv.pyを修正して、無理やり最新バージョンを入れられるようにした。
どうもvirtualenvはdistributeのbootstrapファイルを文字列で持っていて、それを直接subprocess -> python -c で呼び出すみたい。
普通にdistribute_setup.py置いといてそれ呼べばいいのにとか思ったり。。。
とにかく、下に書いといたパッチを当てて、
virtualenv --distribute --distribute-version 0.6.19 myvirtualenv
とかやれば指定のバージョンが入るようになった。
なお、site-packages\virtualenv_supportにdistribute-0.6.19.tar.gz突っ込んどけばそれを使うはず(無いとダウンロード)
だれか開発チームに報告しといて:-)
--- virtualenv_old.py 2011-07-11 17:41:13 +0900 +++ virtualenv.py 2011-07-15 11:47:44 +0900 @@ -458,7 +458,7 @@ return join(dir, filename) return filename -def _install_req(py_executable, unzip=False, distribute=False, +def _install_req(py_executable, unzip=False, distribute=False, distribute_version=None, search_dirs=None, never_download=False): if search_dirs is None: @@ -471,7 +471,9 @@ source = None else: setup_fn = None - source = 'distribute-0.6.16.tar.gz' + if distribute_version is None: + distribute_version = '0.6.16' + source = 'distribute-%s.tar.gz' % distribute_version project_name = 'distribute' bootstrap_script = DISTRIBUTE_SETUP_PY try: @@ -500,7 +502,8 @@ os.close(fd) cmd = [py_executable, ez_setup] else: - cmd = [py_executable, '-c', bootstrap_script] + cmd = [py_executable, '-c', + re.sub(r'(DEFAULT_VERSION\s+?=\s+?)".*?"', r'\1"%s"' % distribute_version, bootstrap_script)] if unzip: cmd.append('--always-unzip') env = {} @@ -587,9 +590,9 @@ _install_req(py_executable, unzip, search_dirs=search_dirs, never_download=never_download) -def install_distribute(py_executable, unzip=False, +def install_distribute(py_executable, unzip=False, version=None, search_dirs=None, never_download=False): - _install_req(py_executable, unzip, distribute=True, + _install_req(py_executable, unzip, distribute=True, distribute_version=version, search_dirs=search_dirs, never_download=never_download) _pip_re = re.compile(r'^pip-.*(zip|tar.gz|tar.bz2|tgz|tbz)$', re.I) @@ -714,6 +717,12 @@ help='Use Distribute instead of Setuptools. Set environ variable ' 'VIRTUALENV_USE_DISTRIBUTE to make it the default ') + parser.add_option( + '--distribute-version', + dest='distribute_version', + metavar='VERSION', + help="distribute's version you want to install.") + default_search_dirs = file_search_dirs() parser.add_option( '--extra-search-dir', @@ -789,7 +798,7 @@ create_environment(home_dir, site_packages=not options.no_site_packages, clear=options.clear, unzip_setuptools=options.unzip_setuptools, - use_distribute=options.use_distribute or majver > 2, + use_distribute=options.use_distribute or majver > 2, distribute_version=options.distribute_version, prompt=options.prompt, search_dirs=options.search_dirs, never_download=options.never_download) @@ -868,7 +877,7 @@ def create_environment(home_dir, site_packages=True, clear=False, - unzip_setuptools=False, use_distribute=False, + unzip_setuptools=False, use_distribute=False, distribute_version=None, prompt=None, search_dirs=None, never_download=False): """ Creates a new environment in ``home_dir``. @@ -888,7 +897,7 @@ install_distutils(home_dir) if use_distribute or os.environ.get('VIRTUALENV_USE_DISTRIBUTE'): - install_distribute(py_executable, unzip=unzip_setuptools, + install_distribute(py_executable, unzip=unzip_setuptools, version=distribute_version, search_dirs=search_dirs, never_download=never_download) else: install_setuptools(py_executable, unzip=unzip_setuptools,