[vlc-commits] commit: Change vlc.stream() and vlc.memory_stream() error reporting to behave like lua's io.open() or loadfile() functions. ( Antoine Cellerier )

git version control git at videolan.org
Tue Mar 2 21:24:59 CET 2010


vlc | branch: master | Antoine Cellerier <dionoea at videolan.org> | Tue Mar  2 12:44:26 2010 +0100| [4822a79c7160571ce020fab27efd2273622512a6] | committer: Antoine Cellerier 

Change vlc.stream() and vlc.memory_stream() error reporting to behave like lua's io.open() or loadfile() functions.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=4822a79c7160571ce020fab27efd2273622512a6
---

 modules/misc/lua/libs/stream.c        |    6 +++++-
 share/lua/README.txt                  |    3 +++
 share/lua/extensions/imdb.lua         |   19 ++++++++++++++++---
 share/lua/meta/art/01_musicbrainz.lua |    1 +
 share/lua/meta/art/10_googleimage.lua |    2 ++
 share/lua/meta/fetcher/tvrage.lua     |    2 ++
 share/lua/modules/simplexml.lua       |    3 ++-
 share/lua/sd/freebox.lua              |    6 +++++-
 8 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/modules/misc/lua/libs/stream.c b/modules/misc/lua/libs/stream.c
index 4a94058..4dcae62 100644
--- a/modules/misc/lua/libs/stream.c
+++ b/modules/misc/lua/libs/stream.c
@@ -62,7 +62,11 @@ static const luaL_Reg vlclua_stream_reg[] = {
 static int vlclua_stream_new_inner( lua_State *L, stream_t *p_stream )
 {
     if( !p_stream )
-        return luaL_error( L, "Error when opening stream" );
+    {
+        lua_pushnil( L );
+        lua_pushliteral( L, "Error when opening stream" );
+        return 2;
+    }
 
     stream_t **pp_stream = lua_newuserdata( L, sizeof( stream_t * ) );
     *pp_stream = p_stream;
diff --git a/share/lua/README.txt b/share/lua/README.txt
index f7e6283..dec466f 100644
--- a/share/lua/README.txt
+++ b/share/lua/README.txt
@@ -337,6 +337,9 @@ n:add_node( ... ): Same as sd.add_node(), but as a subnode of n.
 Stream
 ------
 stream( url ): Instantiate a stream object for specific url.
+memory_stream( string ): Instantiate a stream object containing a specific string.
+  Those two functions return the stream object upon success and nil if an
+  error occured, in that case, the second return value will be an error message.
 
 s = vlc.stream( "http://www.videolan.org/" )
 s:read( 128 ) -- read up to 128 characters. Return 0 if no more data is available (FIXME?).
diff --git a/share/lua/extensions/imdb.lua b/share/lua/extensions/imdb.lua
index 2a36b1b..6509d56 100644
--- a/share/lua/extensions/imdb.lua
+++ b/share/lua/extensions/imdb.lua
@@ -100,7 +100,11 @@ function click_okay()
     -- Search IMDb
     local url = "http://www.imdb.com/find?s=all&q="
     local title = string.gsub(txt:get_text(), " ", "+")
-    local s = vlc.stream(url .. title)
+    local s, msg = vlc.stream(url .. title)
+    if not s then
+        vlc.msg.warn(msg)
+        return
+    end
 
     -- Fetch HTML data
     local data = s:read(65000)
@@ -171,7 +175,12 @@ function click_open()
 
     -- userLink:set_text("<a href=\"url\">" .. url .. "</a>")
 
-    local s = vlc.stream(url)
+    local s, msg = vlc.stream(url)
+    if not s then
+        vlc.msg.warn(msg)
+        return
+    end
+
     data = s:read(65000)
 
     text = "<h1>" .. titles[sel].title .. " (" .. titles[sel].year .. ")</h1>"
@@ -214,7 +223,11 @@ function click_open()
     text = text .. actors .. "</table>"
 
     text = text .. "<h2>Plot Summary</h2>"
-    s = vlc.stream(url .. "plotsummary")
+    s, msg = vlc.stream(url .. "plotsummary")
+    if not s then
+        vlc.msg.warn(msg)
+        return
+    end
     data = s:read(65000)
 
     -- We read only the first summary
diff --git a/share/lua/meta/art/01_musicbrainz.lua b/share/lua/meta/art/01_musicbrainz.lua
index d35d2b4..934e9ad 100644
--- a/share/lua/meta/art/01_musicbrainz.lua
+++ b/share/lua/meta/art/01_musicbrainz.lua
@@ -34,6 +34,7 @@ function try_query(query)
     l = nil
     vlc.msg.dbg( query )
     local s = vlc.stream( query )
+    if not s then return nil end
     local page = s:read( 65653 )
 
     -- FIXME: multiple results may be available
diff --git a/share/lua/meta/art/10_googleimage.lua b/share/lua/meta/art/10_googleimage.lua
index e123060..a3cc35d 100644
--- a/share/lua/meta/art/10_googleimage.lua
+++ b/share/lua/meta/art/10_googleimage.lua
@@ -49,6 +49,8 @@ function fetch_art()
         return nil
     end
     fd = vlc.stream( "http://images.google.com/images?q=" .. get_query( title ) )
+    if not fd then return nil end
+
     page = fd:read( 65653 )
     fd = nil
     _, _, arturl = string.find( page, "imgurl=([^&]+)" )
diff --git a/share/lua/meta/fetcher/tvrage.lua b/share/lua/meta/fetcher/tvrage.lua
index 46c9df0..22a391d 100644
--- a/share/lua/meta/fetcher/tvrage.lua
+++ b/share/lua/meta/fetcher/tvrage.lua
@@ -46,6 +46,7 @@ function fetch_meta()
     end
 
     local fd = vlc.stream("http://services.tvrage.com/feeds/search.php?show=" .. get_query(showName))
+    if not fd then return nil end
     local page = fd:read( 65653 )
     fd = nil
     _, _, showid = string.find( page, "<showid>(.-)</showid>" )
@@ -54,6 +55,7 @@ function fetch_meta()
     end
 
     fd = vlc.stream("http://services.tvrage.com/feeds/full_show_info.php?sid=" .. showid)
+    if not fd then return nil end
     page = fd:read( 65653 )
     fd = nil
     _, _, season = string.find(page, "<Season no=\""..seasonNumber.."\">(.-)</Season>")
diff --git a/share/lua/modules/simplexml.lua b/share/lua/modules/simplexml.lua
index bda68a4..a7be620 100644
--- a/share/lua/modules/simplexml.lua
+++ b/share/lua/modules/simplexml.lua
@@ -29,7 +29,8 @@ module("simplexml",package.seeall)
 --     text content (string)
 --]]
 
-local function parsexml(stream)
+local function parsexml(stream, errormsg)
+    if not stream then return nil, errormsg end
     local xml = vlc.xml()
     local reader = xml:create_reader(stream)
 
diff --git a/share/lua/sd/freebox.lua b/share/lua/sd/freebox.lua
index 08bb515..0ba209b 100644
--- a/share/lua/sd/freebox.lua
+++ b/share/lua/sd/freebox.lua
@@ -25,7 +25,11 @@ function descriptor()
 end
 
 function main()
-    local fd = vlc.stream( "http://mafreebox.freebox.fr/freeboxtv/playlist.m3u" )
+    local fd, msg = vlc.stream( "http://mafreebox.freebox.fr/freeboxtv/playlist.m3u" )
+    if not fd then
+        vlc.msg.warn(msg)
+        return nil
+    end
     local line=  fd:readline()
     if line ~= "#EXTM3U" then
         return nil



More information about the vlc-commits mailing list