changeset 57:a2b617731398

pymonkey.undefined now has a 'falsy' value.
author Atul Varma <varmaa@toolness.com>
date Fri, 10 Jul 2009 18:41:14 -0700
parents 72e84bd75905
children 7a3461ccaf1d
files docs/rendered/_sources/pymonkey.txt docs/rendered/pymonkey.html docs/rendered/searchindex.js docs/src/pymonkey.txt pymonkey.c test_pymonkey.py undefined.c undefined.h
diffstat 8 files changed, 44 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/docs/rendered/_sources/pymonkey.txt	Fri Jul 10 17:39:48 2009 -0700
+++ b/docs/rendered/_sources/pymonkey.txt	Fri Jul 10 18:41:14 2009 -0700
@@ -25,17 +25,13 @@
 
      >>> cx = pymonkey.Runtime().new_context()
      >>> cx.evaluate_script(cx.new_object(), '', '<string>', 1)
-     <type 'pymonkey.undefined'>
+     pymonkey.undefined
 
-   Unfortunately, this object currently does not have a "falsy" value,
-   e.g.:
+   This object also has a "falsy" value:
 
-     >>> if pymonkey.undefined:
-     ...   print 'Huh, this is kind of unintuitive.'
-     Huh, this is kind of unintuitive.
-
-   The reason for this is simply that we don't currently know how to
-   make this object have a falsy value, if it's even possible.
+     >>> if not pymonkey.undefined:
+     ...   print "See, it's falsy!"
+     See, it's falsy!
 
 .. class:: Object
 
--- a/docs/rendered/pymonkey.html	Fri Jul 10 17:39:48 2009 -0700
+++ b/docs/rendered/pymonkey.html	Fri Jul 10 18:41:14 2009 -0700
@@ -63,18 +63,15 @@
 For instance:</p>
 <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cx</span> <span class="o">=</span> <span class="n">pymonkey</span><span class="o">.</span><span class="n">Runtime</span><span class="p">()</span><span class="o">.</span><span class="n">new_context</span><span class="p">()</span>
 <span class="gp">&gt;&gt;&gt; </span><span class="n">cx</span><span class="o">.</span><span class="n">evaluate_script</span><span class="p">(</span><span class="n">cx</span><span class="o">.</span><span class="n">new_object</span><span class="p">(),</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;string&gt;&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">)</span>
-<span class="go">&lt;type &#39;pymonkey.undefined&#39;&gt;</span>
+<span class="go">pymonkey.undefined</span>
 </pre></div>
 </div>
-<p>Unfortunately, this object currently does not have a &#8220;falsy&#8221; value,
-e.g.:</p>
-<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">if</span> <span class="n">pymonkey</span><span class="o">.</span><span class="n">undefined</span><span class="p">:</span>
-<span class="gp">... </span>  <span class="k">print</span> <span class="s">&#39;Huh, this is kind of unintuitive.&#39;</span>
-<span class="go">Huh, this is kind of unintuitive.</span>
+<p>This object also has a &#8220;falsy&#8221; value:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">if</span> <span class="ow">not</span> <span class="n">pymonkey</span><span class="o">.</span><span class="n">undefined</span><span class="p">:</span>
+<span class="gp">... </span>  <span class="k">print</span> <span class="s">&quot;See, it&#39;s falsy!&quot;</span>
+<span class="go">See, it&#39;s falsy!</span>
 </pre></div>
 </div>
-<p>The reason for this is simply that we don&#8217;t currently know how to
-make this object have a falsy value, if it&#8217;s even possible.</p>
 </dd></dl>
 
 <dl class="class">
--- a/docs/rendered/searchindex.js	Fri Jul 10 17:39:48 2009 -0700
+++ b/docs/rendered/searchindex.js	Fri Jul 10 18:41:14 2009 -0700
@@ -1,1 +1,1 @@
-Search.setIndex({desctypes:{"0":"exception","1":"method","2":"class","3":"data"},terms:{spidermonkei:[0,1],represent:1,all:0,code:[0,1],execut:[0,1],less:0,path_to_objdir:0,untrust:0,lack:0,becaus:0,intermediari:0,product:0,veri:0,concept:0,particularli:0,languag:0,involv:0,web:0,onli:[0,1],depend:0,just:0,expos:0,how:[0,1],readabl:0,except:1,should:0,platform:0,add:[],busi:0,requir:0,easier:0,isinst:1,them:0,liter:1,"return":1,around:0,thei:[],without:0,python:[0,1],swig:0,none:1,pave:0,obtain:0,atul:0,envis:0,capi:0,increas:0,world:0,now:[],vast:0,enabl:0,xpcom:0,like:[0,1],level:1,vibrant:0,anyth:0,probabl:0,resolv:0,server:0,"try":0,easili:0,off:0,either:0,each:1,debug:0,through:1,where:0,manag:0,librari:0,mean:0,right:[],new_context:1,some:0,back:0,realli:0,"static":0,mirror:0,disadvantag:0,stack:0,collector:0,casual:0,best:1,out:0,even:1,index:0,detect:0,string:1,collect:0,test_pymonkei:[],someth:0,abl:0,rhino:0,access:[0,1],state:1,outstand:0,dynam:0,rational:0,between:0,print:1,"new":1,objdir:0,forth:1,challeng:0,refer:0,docutil:1,full:0,run:0,complex:0,garbag:0,javascript:[0,1],situat:0,new_object:1,here:0,standard:0,objcec:[],extens:0,reason:1,base:0,preprocessor:0,repositori:0,complementari:0,path:0,come:0,valu:1,thrown:1,about:0,actual:0,easi:0,thread:1,befor:0,similar:0,current:[0,1],moment:0,page:0,blargh:[],instanc:[0,1],doctest:0,context:1,logic:0,freeli:1,chang:1,com:0,tremend:0,singleton:1,via:[0,1],simpli:1,obviou:0,pyrex:0,feel:[],solut:0,modul:[0,1],runtm:1,number:0,latter:0,header:0,instruct:0,api:0,serverj:0,central:0,contributor:0,side:0,mark:0,facil:0,your:0,duplic:0,quit:0,span:1,advantag:0,wai:0,script:0,ergonom:0,interact:1,due:0,custom:0,avail:0,process:0,program:1,call:1,low:1,lot:0,suit:[],interfac:1,type:1,"final":0,too:0,free:[],from:[0,1],relationship:0,offer:1,tool:0,mozilla:[0,1],huh:1,search:0,ctype:0,indic:0,known:0,don:[0,1],eas:0,repres:1,"true":1,than:0,must:0,count:0,wide:0,kind:1,made:0,possibl:1,provid:0,straight:0,work:0,conceptu:1,can:[0,1],learn:0,akin:0,more:0,root:0,browser:0,pre:1,would:0,traceback:0,file:0,creat:[0,1],give:0,falsi:1,unintuit:1,readm:0,ani:[0,1],doesn:0,trap:0,blah:[],exist:0,have:[0,1],tabl:0,need:0,unfortun:1,featur:0,check:[],contribut:0,packag:0,alwai:0,sweep:0,"null":1,engin:[0,1],goal:0,want:0,secur:0,boop:[],parti:0,make:[0,1],error:1,cross:0,same:1,client:0,note:0,field:0,ideal:0,other:[0,1],read:0,build:0,which:0,test:0,verifi:1,you:0,document:0,noth:0,boof:[],runtim:1,map:1,matur:0,relat:1,http:0,write:0,clone:0,object:[0,1],wherea:0,usabl:0,hand:0,fairli:0,plai:0,never:0,mai:1,develop:0,defin:0,associ:1,"class":1,pymonkei:[0,1],built:1,directori:0,memori:0,todo:[],land:0,sphinx:0,equival:1,third:0,java:0,also:0,macro:0,cycl:0,counter:1,doe:1,sandbox:0,exampl:[],environ:0,obj:1,thi:[0,1],undefin:1,programm:0,model:0,profil:0,evaluate_script:1,awesom:0,know:1},titles:["Pymonkey Documentation","<tt class=\"docutils literal docutils literal\"><span class=\"pre\">pymonkey</span></tt> &#8212; Access SpiderMonkey from Python"],modules:{pymonkey:1},descrefs:{"pymonkey.Runtime":{new_context:[1,1]},pymonkey:{Object:[1,2],Runtime:[1,2],undefined:[1,3],Context:[1,2],error:[1,0]}},filenames:["index","pymonkey"]})
\ No newline at end of file
+Search.setIndex({desctypes:{"0":"exception","1":"method","2":"class","3":"data"},terms:{spidermonkei:[0,1],represent:1,all:0,code:[0,1],execut:[0,1],less:0,abl:0,untrust:0,lack:0,becaus:0,cross:0,veri:0,concept:0,particularli:0,matur:0,languag:0,involv:0,web:0,onli:[0,1],depend:0,just:0,expos:0,how:0,readabl:0,except:1,should:0,platform:0,add:[],busi:0,requir:0,easier:0,isinst:1,build:0,them:0,liter:1,"return":1,around:0,thei:[],python:[0,1],swig:0,kind:[],pave:0,obtain:0,print:1,envis:0,capi:0,increas:0,world:0,now:[],"class":1,vast:0,enabl:0,xpcom:0,like:[0,1],anyth:0,vibrant:0,level:1,probabl:0,resolv:0,server:0,"try":0,easili:0,either:0,have:0,each:1,debug:0,through:1,refer:0,where:0,manag:0,mean:0,right:[],new_context:1,some:0,back:0,realli:0,see:1,"static":0,mirror:0,disadvantag:0,collector:0,casual:0,librari:0,out:0,even:[],index:0,detect:0,string:1,collect:0,facil:0,someth:0,path_to_objdir:0,rhino:0,access:[0,1],state:1,outstand:0,dynam:0,rational:0,between:0,atul:0,"new":1,objdir:0,forth:1,sphinx:0,challeng:0,trap:0,docutil:1,full:0,run:0,garbag:0,javascript:[0,1],situat:0,new_object:1,here:0,standard:0,objcec:[],extens:0,reason:[],base:0,preprocessor:0,repositori:0,complementari:0,path:0,come:0,don:0,valu:1,thrown:1,about:0,actual:0,easi:0,thread:1,fairli:0,root:0,current:[0,1],plai:0,page:0,blargh:[],instanc:[0,1],doctest:0,context:1,logic:0,freeli:1,chang:1,com:0,other:[0,1],tremend:0,singleton:1,via:[0,1],simpli:[],obviou:0,pyrex:0,feel:[],solut:0,modul:[0,1],runtm:1,number:0,header:0,instruct:0,api:0,serverj:0,contributor:0,side:0,custom:0,test_pymonkei:[],your:0,duplic:0,quit:0,span:1,creat:[0,1],wai:0,script:0,ergonom:0,interact:1,due:0,mark:0,avail:0,falsi:1,program:1,call:1,low:1,lot:0,suit:[],interfac:1,type:1,"final":0,too:0,free:[],field:0,from:[0,1],relationship:0,offer:1,tool:0,mozilla:[0,1],huh:[],search:0,ctype:0,doesn:0,known:0,central:0,eas:0,"true":1,than:0,must:0,count:0,wide:0,none:1,made:0,obj:1,possibl:[],provid:0,straight:0,work:0,exampl:[],conceptu:1,can:[0,1],learn:0,akin:0,more:0,similar:0,browser:0,pre:1,would:0,traceback:0,file:0,advantag:0,give:0,process:0,unintuit:[],readm:0,ani:[0,1],indic:0,repres:1,blah:[],exist:0,want:0,tabl:0,need:0,unfortun:[],featur:0,check:[],contribut:0,packag:0,alwai:0,sweep:0,"null":1,engin:[0,1],goal:0,equival:1,secur:0,boop:[],parti:0,make:0,intermediari:0,same:1,client:0,note:0,also:[0,1],ideal:0,complex:0,best:1,which:0,evaluate_script:1,test:0,verifi:1,you:0,document:0,noth:0,boof:[],runtim:1,map:1,product:0,relat:1,http:0,clone:0,object:[0,1],wherea:0,usabl:0,hand:0,befor:0,moment:0,never:0,mai:1,develop:0,defin:0,associ:1,stack:0,pymonkei:[0,1],built:1,directori:0,memori:0,todo:[],land:0,off:0,write:0,third:0,java:0,read:0,macro:0,cycl:0,counter:1,doe:[],sandbox:0,without:0,environ:0,error:1,thi:[0,1],undefin:1,programm:0,model:0,profil:0,latter:0,awesom:0,know:[]},titles:["Pymonkey Documentation","<tt class=\"docutils literal docutils literal\"><span class=\"pre\">pymonkey</span></tt> &#8212; Access SpiderMonkey from Python"],modules:{pymonkey:1},descrefs:{"pymonkey.Runtime":{new_context:[1,1]},pymonkey:{Object:[1,2],Runtime:[1,2],undefined:[1,3],Context:[1,2],error:[1,0]}},filenames:["index","pymonkey"]})
\ No newline at end of file
--- a/docs/src/pymonkey.txt	Fri Jul 10 17:39:48 2009 -0700
+++ b/docs/src/pymonkey.txt	Fri Jul 10 18:41:14 2009 -0700
@@ -25,17 +25,13 @@
 
      >>> cx = pymonkey.Runtime().new_context()
      >>> cx.evaluate_script(cx.new_object(), '', '<string>', 1)
-     <type 'pymonkey.undefined'>
+     pymonkey.undefined
 
-   Unfortunately, this object currently does not have a "falsy" value,
-   e.g.:
+   This object also has a "falsy" value:
 
-     >>> if pymonkey.undefined:
-     ...   print 'Huh, this is kind of unintuitive.'
-     Huh, this is kind of unintuitive.
-
-   The reason for this is simply that we don't currently know how to
-   make this object have a falsy value, if it's even possible.
+     >>> if not pymonkey.undefined:
+     ...   print "See, it's falsy!"
+     See, it's falsy!
 
 .. class:: Object
 
--- a/pymonkey.c	Fri Jul 10 17:39:48 2009 -0700
+++ b/pymonkey.c	Fri Jul 10 18:41:14 2009 -0700
@@ -57,8 +57,11 @@
   if (PyType_Ready(&PYM_undefinedType) < 0)
     return;
 
+  PYM_undefined = PyObject_New(PYM_undefinedObject, &PYM_undefinedType);
+  if (PYM_undefined == NULL)
+    return;
   Py_INCREF(PYM_undefined);
-  PyModule_AddObject(module, "undefined", PYM_undefined);
+  PyModule_AddObject(module, "undefined", (PyObject *) PYM_undefined);
 
   PYM_error = PyErr_NewException("pymonkey.error", NULL, NULL);
   Py_INCREF(PYM_error);
--- a/test_pymonkey.py	Fri Jul 10 17:39:48 2009 -0700
+++ b/test_pymonkey.py	Fri Jul 10 18:41:14 2009 -0700
@@ -28,6 +28,10 @@
             was_raised = True
         self.assertTrue(was_raised)
 
+    def testUndefinedStrIsUndefined(self):
+        self.assertEqual(str(pymonkey.undefined),
+                         "pymonkey.undefined")
+
     def testJsWrappedPythonFuncPassesContext(self):
         contexts = []
 
--- a/undefined.c	Fri Jul 10 17:39:48 2009 -0700
+++ b/undefined.c	Fri Jul 10 18:41:14 2009 -0700
@@ -36,6 +36,20 @@
 
 #include "undefined.h"
 
+static Py_ssize_t PYM_undefinedLength(PyObject *o) {
+  return 0;
+};
+
+static PyMappingMethods PYM_undefinedAsMapping = {
+  PYM_undefinedLength,         /*mp_length*/
+  0,                           /*mp_subscript*/
+  0                            /*mp_ass_subscript*/
+};
+
+static PyObject *PYM_undefinedRepr(PyObject *o) {
+  return PyString_FromString("pymonkey.undefined");
+}
+
 // TODO: We should make this behave as much like JavaScript's
 // "undefined" value as possible; e.g., its string value should
 // be "undefined", the singleton should be falsy, etc.
@@ -50,13 +64,13 @@
   0,                           /*tp_getattr*/
   0,                           /*tp_setattr*/
   0,                           /*tp_compare*/
-  0,                           /*tp_repr*/
+  PYM_undefinedRepr,           /*tp_repr*/
   0,                           /*tp_as_number*/
   0,                           /*tp_as_sequence*/
-  0,                           /*tp_as_mapping*/
+  &PYM_undefinedAsMapping,     /*tp_as_mapping*/
   0,                           /*tp_hash */
   0,                           /*tp_call*/
-  0,                           /*tp_str*/
+  PYM_undefinedRepr,           /*tp_str*/
   0,                           /*tp_getattro*/
   0,                           /*tp_setattro*/
   0,                           /*tp_as_buffer*/
@@ -65,4 +79,4 @@
   "Pythonic equivalent of JavaScript's 'undefined' value",
 };
 
-PyObject *PYM_undefined = (PyObject *) &PYM_undefinedType;
+PYM_undefinedObject *PYM_undefined;
--- a/undefined.h	Fri Jul 10 17:39:48 2009 -0700
+++ b/undefined.h	Fri Jul 10 18:41:14 2009 -0700
@@ -40,7 +40,7 @@
 #include <Python/Python.h>
 
 #define Py_RETURN_UNDEFINED  { Py_INCREF(PYM_undefined);        \
-                               return PYM_undefined; }
+                               return (PyObject *) PYM_undefined; }
 
 typedef struct {
   PyObject_HEAD
@@ -48,6 +48,6 @@
 
 extern PyTypeObject PYM_undefinedType;
 
-extern PyObject *PYM_undefined;
+extern PYM_undefinedObject *PYM_undefined;
 
 #endif