リモートファイルの世話をするためにDjango Fileオブジェクトのサブクラスを作成しました。 Lazyobjectクラスをサブクラス化するRemoteFileLazyを作成して怠惰なバージョンを作成したかったのですが、期待通りに動作しません。私はエラーが発生します。RuntimeError:最大再帰深度を超えました:なぜですか?
import urllib2
from django.core.files.base import File
from django.utils.functional import LazyObject
class RemoteFile(File):
def __init__(self, url):
super(RemoteFile, self).__init__(urllib2.urlopen(urllib2.Request(url)))
def __str__(self):
return 'Remote content'
def __nonzero__(self):
return True
def open(self, mode=None):
self.seek(0)
def close(self):
pass
def chunks(self, chunk_size=None):
# CHUNKS taking care of no known size!
if not chunk_size:
chunk_size = self.DEFAULT_CHUNK_SIZE
if hasattr(self, 'seek'):
self.seek(0)
# Assume the pointer is at zero...
counter = self.size
while True:
data = self.read(chunk_size)
if not data:
break
yield data
class RemoteFileLazy(LazyObject):
def __init__(self, url):
# # as said in the django code: For some reason, we have to inline LazyObject.__init__ here to avoid
# recursion
self._wrapped = None
self.url = url
def _setup(self):
self._wrapped = RemoteFile(self.url)
私はこのコードを実行すると:
RuntimeError: maximum recursion depth exceeded
任意のアイデア:
rfl = filehelper.RemoteFileLazy(url="http://www.google.fr")
が、私はこのエラーを得ましたの?私はLazyObjectを呼び出さなかった。 initこれはdjangoコードでも言及されているように... 私はのinitメソッドの "self.url = url"がこのエラーを正しくトリガーすると思いますか?だから私は属性を持つ怠惰なオブジェクトを使用することはできません?
ありがとうございました。
トレースバック:
c:\Users\Michael\Dropbox\development\tools\Portable Python 2.7.2.1-django1.3.1\App\lib\site-packages\django\utils\functional.pyc in __getattr__(self, name)
274 def __getattr__(self, name):
275 if self._wrapped is None:
--> 276 self._setup()
277 return getattr(self._wrapped, name)
278
C:\Users\Michael\Dropbox\development\projects\django-socialdealing\socialdealing\apps\etl\utils\filehelper.py in _setup(self)
58
59 def _setup(self):
---> 60 self._wrapped = RemoteFile(self.url)
61
62
c:\Users\Michael\Dropbox\development\tools\Portable Python 2.7.2.1-django1.3.1\App\lib\site-packages\django\utils\functional.pyc in __getattr__(self, name)
274 def __getattr__(self, name):
275 if self._wrapped is None:
--> 276 self._setup()
277 return getattr(self._wrapped, name)
278
トレースバックの一部に貼り付けて、どの機能が無限に再帰されたかを確認できますか? –
確かに、私はそれを追加します。 – Michael