『IronPython の世界』を読む (3)
『IronPython の世界』を読む (2) から続きを読む。Part 3 の後半部分。とりあえず、sys.path.append(CPythonのライブラリのパス) で CPython のライブラリは使えるようになるものの、じゃあ、全部動きますかといえば、当然、そんなことはない。.pyc (スクリプトをコンパイルしたキャッシュファイル)、 .pyd (C言語で作成されたライブラリ) は、仕様として使えないから動かないものがたくさんある。Pure Python で書かれているものにしても、すべてが動くとは限らないだろう。例えば、urllib.urlopen なども動かないらしい。Python 1.1.1 でも動かないか一応確認してみる。1.1.1 でも、まだ動いていない。IronPython 1.1.1 (1.1.1) on .NET 2.0.50727.1433Copyright (c) Microsoft Corporation. All rights reserved.>>> import sys>>> sys.path.append("\\python24\\lib")>>> import urllib>>> import urlparse>>> url="http://www.python.jp/pub/doc_jp/pdf-a4-2.4.zip">>> filename=urlparse.urlparse(url)[2].split("/")[-1]>>> filename'pdf-a4-2.4.zip'>>> urllib.urlretrieve(url, filename)Traceback (most recent call last): File System, line unknown, in Read File System, line unknown, in Receive File System, line unknown, in Receive File , line 0, in <stdin>##171 File E:\python24\lib\urllib.py, line 89, in urlretrieve File E:\python24\lib\urllib.py, line 248, in retrieve File , line 0, in Read##166 File System, line unknown, in ReadIOError: 転送接続からデータを読み取れません: 破棄されたオブジェクトにアクセスできません。オブジェクト名 'System.Net.Sockets.Socket' です。。>>> url="http://www.codeplex.com/IronPython/Project/ProjectRss.aspx">>> src=urllib.urlopen(url).read()Traceback (most recent call last): File System, line unknown, in Read File System, line unknown, in Receive File System, line unknown, in Receive File , line 0, in <stdin>##82 File , line 0, in Read##165 File System, line unknown, in ReadIOError: 転送接続からデータを読み取れません: 破棄されたオブジェクトにアクセスできません。オブジェクト名 'System.Net.Sockets.Socket' です。。FePy プロジェクト の IPCE (IronPython Community Edition) の方でもまだだめかな。こっちは、CPython との互換性を向上させようとしているらしいが、2007-10-15 に r7 まで出ている。p.161 からに書かれているように、socket.py を修正してやると、動くようになった。IronPython では、まだ doctest モジュールも使えないのね。その他、ざっと Part 3 は読み流して、Part 4 .NET Framework のライブラリを使用するに入る。>>> dir("")['__add__', '__class__', '__cmp__', '__contains__', '__doc__', '__eq__', '__getitem__', '__getnewargs__', '__getslice__', '__init__', '__len__', '__mod__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'islower', 'isnumeric', 'isspace', 'istitle', 'isunicode', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] clr をインポートすると、Python のデータ型から .NET Framework のデータ型が提供するメソッドが使えるようになる。なるほど、使えるメソッドがどっと増える。>>> import clr>>> dir("")['Add', 'Capitalize', 'Center', 'Clone', 'Compare', 'CompareOrdinal','CompareTo', 'Concat', 'Contains', 'Copy', 'CopyTo', 'Count', 'Decode', 'Empty','Encode', 'EndsWith', 'Equals', 'EqualsRetBool', 'ExpandTabs', 'Finalize', 'Find','Format', 'GetEnumerator', 'GetHashCode', 'GetLength', 'GetNewArgs', 'GetSlice','GetType', 'GetTypeCode', 'Index', 'IndexOf', 'IndexOfAny', 'Insert', 'Intern','IsAlnum', 'IsAlpha', 'IsDecimal', 'IsDigit', 'IsInterned', 'IsLower','IsNormalized', 'IsNullOrEmpty', 'IsNumeric', 'IsSpace', 'IsTitle', 'IsUnicode','IsUpper', 'Join', 'LJust', 'LStrip', 'LastIndexOf', 'LastIndexOfAny', 'Length','Lower', 'Make', 'MakeDynamicType', 'MemberwiseClone', 'Modulus', 'Multiply', 'Normalize', 'NotEquals', 'PadLeft', 'PadRight', 'RFind', 'RIndex', 'RJust', 'RSplit', 'RStrip', 'Reduce', 'ReferenceEquals', 'Remove', 'Replace', 'ReverseMultiply', 'Split', 'SplitLines', 'StartsWith', 'Strip', 'Substring', 'SwapCase', 'Title', 'ToCharArray', 'ToLower', 'ToLowerInvariant', 'ToString', 'ToUpper', 'ToUpperInvariant', 'Translate', 'Trim', 'TrimEnd', 'TrimStart', 'Upper', 'ZFill', '__add__', '__class__', '__cmp__', '__contains__', '__doc__', '__eq__', '__getitem__', '__getnewargs__', '__getslice__', '__init__', '__len__','__mod__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__','__str__', 'capitalize', 'center','count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum','isalpha', 'isdecimal', 'isdigit', 'islower', 'isnumeric', 'isspace', 'istitle','isunicode', 'isupper', 'join', 'ljust','lower', 'lstrip', 'replace', 'rfind','rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']>>> "".GetType().Fullname'System.String'>>> o = object()>>> type(o)<type 'object'>>>> o.GetType().FullName'System.Object'ふーむ、そういうことになっているのね。p.207 に dir 関数の結果が IronPython と CPython で違うことの記述がある。__delattr_, __getattribute__, __hash__, __setattr__, __str__ は CPython では出力されるが、IronPython では出力されない。しかし、__site__ は IronPython でだけ出力されるとかいったことも違っているのね。とりあえず、基本的なところはだいたい分かった。このあと、.NET Framework を使っていきましょうというところにどんどん入っていく。ここまで読んできて、この本、よくまとまっていると思う。IronPython を使うなら買って読んだ方が、よけいなところで躓かないで済むのでいい。この本は買って良かった。IronPythonの世界