Rev 68: Change the _scanner code to use PyObject_Size() rather than directly accessing c_obj->ob_size. in http://bazaar.launchpad.net/~jameinel/meliae/trunk

John Arbash Meinel john at arbash-meinel.com
Wed Sep 9 22:12:30 BST 2009


At http://bazaar.launchpad.net/~jameinel/meliae/trunk

------------------------------------------------------------
revno: 68
revision-id: john at arbash-meinel.com-20090909211215-odsdfqw8yd1e56fa
parent: john at arbash-meinel.com-20090908182852-u2l55fm79x78v8ex
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: trunk
timestamp: Wed 2009-09-09 16:12:15 -0500
message:
  Change the _scanner code to use PyObject_Size() rather than directly accessing c_obj->ob_size.
  
  This is a bit safer than assuming that everything that has a tp_itemsize is a VarObject.
  (I'm currently trying to implement a Keys class that has a length, but doesn't
  use ob_size because it is a long when I only want an unsigned char.
-------------- next part --------------
=== modified file 'meliae/_scanner_core.c'
--- a/meliae/_scanner_core.c	2009-09-08 17:06:14 +0000
+++ b/meliae/_scanner_core.c	2009-09-09 21:12:15 +0000
@@ -43,8 +43,15 @@
 Py_ssize_t
 _var_object_size(PyVarObject *c_obj)
 {
+    Py_ssize_t num_entries;
+    num_entries = PyObject_Size((PyObject *)c_obj);
+    if (num_entries < 0) {
+        /* This object doesn't support len() */
+        num_entries = 0;
+        PyErr_Clear();
+    }
     return _basic_object_size((PyObject *)c_obj)
-            + c_obj->ob_size * c_obj->ob_type->tp_itemsize;
+            + num_entries * c_obj->ob_type->tp_itemsize;
 }
 
 



More information about the bazaar-commits mailing list