summary refs log tree commit diff
diff options
context:
space:
mode:
authorLucas Moura <lucas.moura128@gmail.com>2016-06-18 17:01:47 -0300
committerSergey M․ <dstftw@gmail.com>2016-06-19 03:29:45 +0700
commit7c05097633138459e9bdf7e10738e021b04689a7 (patch)
treeea2b24f8a11c6253bdcd4ed6e1eefbeb74086c42
parent589568789f500b7a515355a07efec4bcec0f3243 (diff)
downloadyoutube-dl-7c05097633138459e9bdf7e10738e021b04689a7.tar.gz
youtube-dl-7c05097633138459e9bdf7e10738e021b04689a7.tar.xz
youtube-dl-7c05097633138459e9bdf7e10738e021b04689a7.zip
[jsinterp] Avoid double key lookup for setting new key
In order to add a new key to both __objects and __functions dicts on jsinterp.py, it is
necessary to first verify if a key was present and if not, create the key and
assign it to a value.

However, this can be done with a single step using dict setdefault method.
-rw-r--r--youtube_dl/jsinterp.py8
1 files changed, 3 insertions, 5 deletions
diff --git a/youtube_dl/jsinterp.py b/youtube_dl/jsinterp.py
index a7440c582..4a5a0dbc3 100644
--- a/youtube_dl/jsinterp.py
+++ b/youtube_dl/jsinterp.py
@@ -131,9 +131,8 @@ class JSInterpreter(object):
             if variable in local_vars:
                 obj = local_vars[variable]
             else:
-                if variable not in self._objects:
-                    self._objects[variable] = self.extract_object(variable)
-                obj = self._objects[variable]
+                obj = self._objects.setdefault(
+                    variable, self.extract_object(variable))
 
             if arg_str is None:
                 # Member access
@@ -204,8 +203,7 @@ class JSInterpreter(object):
             argvals = tuple([
                 int(v) if v.isdigit() else local_vars[v]
                 for v in m.group('args').split(',')])
-            if fname not in self._functions:
-                self._functions[fname] = self.extract_function(fname)
+            self._functions.setdefault(fname, self.extract_function(fname))
             return self._functions[fname](argvals)
 
         raise ExtractorError('Unsupported JS expression %r' % expr)