Path: csiph.com!feeder.erje.net!1.us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: Convert from unsigned long long to PyLong Date: Fri, 22 Jul 2016 15:46:20 +0100 Lines: 52 Message-ID: References: <45db6d56-90a3-4e4f-958a-65ecb5da5ac6@googlegroups.com> <5737fedc-cd76-3223-16ce-9541a6c51a5f@mrabarnett.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de pErb1geIaQg/WqrbrC3llgfZAztZvzUoNL9p3bGoW+0w== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'static': 0.03; 'value,': 0.03; 'finally:': 0.05; 'cursor': 0.09; 'matched': 0.09; 'python:': 0.09; 'snippet': 0.09; 'underlying': 0.09; 'unsigned': 0.09; 'url:github': 0.09; 'weird': 0.15; '*args)': 0.16; '*self,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'pyobject': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'subject:Convert': 0.16; 'used:': 0.16; 'wrote:': 0.16; 'library,': 0.18; 'try:': 0.18; 'versions': 0.20; '64-bit': 0.22; 'converted': 0.22; 'doc': 0.22; 'null;': 0.22; 'long,': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'skip:_ 20': 0.26; 'error': 0.27; 'correct': 0.28; 'function': 0.28; 'skip:( 20': 0.28; "i'm": 0.30; 'connection': 0.30; 'system,': 0.30; 'code': 0.30; 'query': 0.30; 'checked': 0.31; 'supposed': 0.31; 'anyone': 0.32; 'related': 0.32; 'problem': 0.33; 'source': 0.33; 'skip:_ 30': 0.33; 'could': 0.35; 'library.': 0.35; 'url:dev': 0.35; 'sometimes': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'possible': 0.36; '(i.e.': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'there,': 0.37; 'available.': 0.37; 'skip:p 20': 0.38; 'means': 0.39; 'data': 0.39; 'url:en': 0.39; 'does': 0.39; 'subject:from': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'skip:u 10': 0.61; 'here': 0.66; 'subject:long': 0.84; 'url:master': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.2 cv=dMLko6Rb c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=IkcTkHD0fZMA:10 a=NEAV23lmAAAA:8 a=tekm_XskAAAA:8 a=M0hU_hXd69lKnsUxD5cA:9 a=ErvH80kCBTAA:10 a=DXcsBJ1V3WsA:10 a=Bn2pgwyD2vrAyMmN8A2t:22 a=lZt3M7SGLAi8y2mAyNvN:22 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: <45db6d56-90a3-4e4f-958a-65ecb5da5ac6@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <5737fedc-cd76-3223-16ce-9541a6c51a5f@mrabarnett.plus.com> X-Mailman-Original-References: <45db6d56-90a3-4e4f-958a-65ecb5da5ac6@googlegroups.com> Xref: csiph.com comp.lang.python:111755 On 2016-07-22 07:01, Tian JiaLin wrote: > HI There, > > I'm using MySQLdb as the MySQL client. Recently I got a weird problem of this library. After looking into it, I suspect the problem may related to the conversion from unsigned long to PyLongObject. > > Here is the detail, If you are familiar with MySQLdb, the following snippet is a way to query the data from MySQL: > > > connection = MySQLdb.connect(...) > > connection.autocommit(True) > try: > cursor = connection.cursor() > if not cursor.execute(sql, values) > 0: > return None > row = cursor.fetchone() > finally: > connection.close() > return row[0] > > > Sometimes the return value of execute method would be 18446744073709552000 even there is no matched data available. I checked the source code of the library, the underlying implementation is https://github.com/farcepest/MySQLdb1/blob/master/_mysql.c#L835, > > static PyObject * > _mysql_ConnectionObject_affected_rows( > _mysql_ConnectionObject *self, > PyObject *args) > { > if (!PyArg_ParseTuple(args, "")) return NULL; > check_connection(self); > return PyLong_FromUnsignedLongLong(mysql_affected_rows(&(self->connection))); > } > > And here is the official doc for mysql_affected_rows http://dev.mysql.com/doc/refman/5.7/en/mysql-affected-rows.html. > > Let me give a superficial understanding, please correct me if I were wrong. > > In a 64-bit system, the mysql_affected_rows is supposed to return a number of unsigned long, which means the range should be 0 ~ 2^64 (18446744073709551616), How could it be possible the function PyLong_FromUnsignedLongLong return a converted value larger than 2^64, that's what I don't understand. > > Does anyone have some ideas of it? > > > The versions of the components I used: > > Python: 2.7.6 > MySQL 5.7.11 > MySQLdb 1.2.5 > > The function returns an unsigned value, but it will return -1 (i.e. ~0) if there's an error, so check for an error with ~result == 0.