[utils] Jython support: tolerate missing fcntl module
authorYen Chi Hsuan <yan12125@gmail.com>
Sat, 20 Feb 2016 19:28:25 +0000 (03:28 +0800)
committerYen Chi Hsuan <yan12125@gmail.com>
Sat, 20 Feb 2016 19:32:03 +0000 (03:32 +0800)
youtube_dl/utils.py

index a2c6780ca354f98514910752cec596a4eee172e5..17747be26e30aaf22cc182847010f6dc6fd6a19a 100644 (file)
@@ -1217,13 +1217,23 @@ if sys.platform == 'win32':
             raise OSError('Unlocking file failed: %r' % ctypes.FormatError())
 
 else:
-    import fcntl
+    # Some platforms, such as Jython, is missing fcntl
+    try:
+        import fcntl
 
-    def _lock_file(f, exclusive):
-        fcntl.flock(f, fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH)
+        def _lock_file(f, exclusive):
+            fcntl.flock(f, fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH)
 
-    def _unlock_file(f):
-        fcntl.flock(f, fcntl.LOCK_UN)
+        def _unlock_file(f):
+            fcntl.flock(f, fcntl.LOCK_UN)
+    except ImportError:
+        UNSUPPORTED_MSG = 'file locking is not supported on this platform'
+
+        def _lock_file(f, exclusive):
+            raise IOError(UNSUPPORTED_MSG)
+
+        def _unlock_file(f):
+            raise IOError(UNSUPPORTED_MSG)
 
 
 class locked_file(object):