From ac988669c23e836381339b260dd0cb16b876b9d9 Mon Sep 17 00:00:00 2001 From: qizhicheng Date: Thu, 12 Jun 2025 18:29:19 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E9=80=82=E5=BA=94=E6=9C=80=E6=96=B0?= =?UTF-8?q?=E7=89=88=E7=9A=84=20cython,=20=E5=8E=BB=E6=8E=89=20py2=20?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libmc/_client.pyx | 59 ++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/libmc/_client.pyx b/libmc/_client.pyx index 1bfd7f71..fdb5ed56 100644 --- a/libmc/_client.pyx +++ b/libmc/_client.pyx @@ -8,14 +8,11 @@ from libcpp.string cimport string from libcpp.vector cimport vector from cpython.mem cimport PyMem_Malloc, PyMem_Free from cpython.version cimport PY_MAJOR_VERSION -from cpython cimport Py_INCREF, Py_DECREF, PyInt_AsLong, PyInt_FromLong +from cpython cimport Py_INCREF, Py_DECREF, PyLong_AsLong, PyLong_FromLong +from ctypes import c_long as long -if PY_MAJOR_VERSION < 3: - from cpython cimport PyString_AsStringAndSize, PyString_AsString - import cPickle as pickle -else: - from cpython cimport PyBytes_AsStringAndSize as PyString_AsStringAndSize, PyBytes_AsString as PyString_AsString, PyUnicode_AsUTF8String - import pickle +from cpython cimport PyBytes_AsStringAndSize as PyString_AsStringAndSize, PyBytes_AsString as PyString_AsString, PyUnicode_AsUTF8String +import pickle import os import sys @@ -257,33 +254,33 @@ cdef flags_t _FLAG_DOUBAN_CHUNKED = 1 << 12 cdef int _DOUBAN_CHUNK_SIZE = 1000000 -MC_DEFAULT_EXPTIME = PyInt_FromLong(DEFAULT_EXPTIME) -MC_POLL_TIMEOUT = PyInt_FromLong(CFG_POLL_TIMEOUT) -MC_CONNECT_TIMEOUT = PyInt_FromLong(CFG_CONNECT_TIMEOUT) -MC_RETRY_TIMEOUT = PyInt_FromLong(CFG_RETRY_TIMEOUT) -MC_MAX_RETRIES = PyInt_FromLong(CFG_MAX_RETRIES) -MC_SET_FAILOVER = PyInt_FromLong(CFG_SET_FAILOVER) -MC_INITIAL_CLIENTS = PyInt_FromLong(CFG_INITIAL_CLIENTS) -MC_MAX_CLIENTS = PyInt_FromLong(CFG_MAX_CLIENTS) -MC_MAX_GROWTH = PyInt_FromLong(CFG_MAX_GROWTH) +MC_DEFAULT_EXPTIME = PyLong_FromLong(DEFAULT_EXPTIME) +MC_POLL_TIMEOUT = PyLong_FromLong(CFG_POLL_TIMEOUT) +MC_CONNECT_TIMEOUT = PyLong_FromLong(CFG_CONNECT_TIMEOUT) +MC_RETRY_TIMEOUT = PyLong_FromLong(CFG_RETRY_TIMEOUT) +MC_MAX_RETRIES = PyLong_FromLong(CFG_MAX_RETRIES) +MC_SET_FAILOVER = PyLong_FromLong(CFG_SET_FAILOVER) +MC_INITIAL_CLIENTS = PyLong_FromLong(CFG_INITIAL_CLIENTS) +MC_MAX_CLIENTS = PyLong_FromLong(CFG_MAX_CLIENTS) +MC_MAX_GROWTH = PyLong_FromLong(CFG_MAX_GROWTH) -MC_HASH_MD5 = PyInt_FromLong(OPT_HASH_MD5) -MC_HASH_FNV1_32 = PyInt_FromLong(OPT_HASH_FNV1_32) -MC_HASH_FNV1A_32 = PyInt_FromLong(OPT_HASH_FNV1A_32) -MC_HASH_CRC_32 = PyInt_FromLong(OPT_HASH_CRC_32) +MC_HASH_MD5 = PyLong_FromLong(OPT_HASH_MD5) +MC_HASH_FNV1_32 = PyLong_FromLong(OPT_HASH_FNV1_32) +MC_HASH_FNV1A_32 = PyLong_FromLong(OPT_HASH_FNV1A_32) +MC_HASH_CRC_32 = PyLong_FromLong(OPT_HASH_CRC_32) -MC_RETURN_SEND_ERR = PyInt_FromLong(RET_SEND_ERR) -MC_RETURN_RECV_ERR = PyInt_FromLong(RET_RECV_ERR) -MC_RETURN_CONN_POLL_ERR = PyInt_FromLong(RET_CONN_POLL_ERR) -MC_RETURN_POLL_TIMEOUT_ERR = PyInt_FromLong(RET_POLL_TIMEOUT_ERR) -MC_RETURN_POLL_ERR = PyInt_FromLong(RET_POLL_ERR) -MC_RETURN_MC_SERVER_ERR = PyInt_FromLong(RET_MC_SERVER_ERR) -MC_RETURN_PROGRAMMING_ERR = PyInt_FromLong(RET_PROGRAMMING_ERR) -MC_RETURN_INVALID_KEY_ERR = PyInt_FromLong(RET_INVALID_KEY_ERR) -MC_RETURN_INCOMPLETE_BUFFER_ERR = PyInt_FromLong(RET_INCOMPLETE_BUFFER_ERR) -MC_RETURN_OK = PyInt_FromLong(RET_OK) +MC_RETURN_SEND_ERR = PyLong_FromLong(RET_SEND_ERR) +MC_RETURN_RECV_ERR = PyLong_FromLong(RET_RECV_ERR) +MC_RETURN_CONN_POLL_ERR = PyLong_FromLong(RET_CONN_POLL_ERR) +MC_RETURN_POLL_TIMEOUT_ERR = PyLong_FromLong(RET_POLL_TIMEOUT_ERR) +MC_RETURN_POLL_ERR = PyLong_FromLong(RET_POLL_ERR) +MC_RETURN_MC_SERVER_ERR = PyLong_FromLong(RET_MC_SERVER_ERR) +MC_RETURN_PROGRAMMING_ERR = PyLong_FromLong(RET_PROGRAMMING_ERR) +MC_RETURN_INVALID_KEY_ERR = PyLong_FromLong(RET_INVALID_KEY_ERR) +MC_RETURN_INCOMPLETE_BUFFER_ERR = PyLong_FromLong(RET_INCOMPLETE_BUFFER_ERR) +MC_RETURN_OK = PyLong_FromLong(RET_OK) @@ -419,7 +416,7 @@ cdef _update_servers(Configurable* imp, list servers, bool_t init): if c_split.port == NULL: c_ports[i] = MC_DEFAULT_PORT else: - c_ports[i] = PyInt_AsLong(int(c_split.port)) + c_ports[i] = PyLong_AsLong(int(c_split.port)) if init: rv = imp.init(c_hosts, c_ports, n, c_aliases) From 4f0a9d496e2e664ea9c31ef927a9ee3fe905d3d6 Mon Sep 17 00:00:00 2001 From: Leo Q Date: Thu, 12 Jun 2025 18:43:17 +0800 Subject: [PATCH 2/5] Update setup.py --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 1d3b789e..5acb739a 100644 --- a/setup.py +++ b/setup.py @@ -99,8 +99,8 @@ def find_version(*file_paths): "License :: OSI Approved :: BSD License", "Topic :: Software Development :: Libraries", ], - # Support for the basestring type is new in Cython 0.20. - setup_requires=["Cython >= 0.20"], + # upgrade cython to a higher version to avoid conflicts + setup_requires=["Cython >= 3.1.0"], ext_modules=[ Extension( "libmc._client", From 1f9908d97911517bb85a87a22e211dd69f7d9bdd Mon Sep 17 00:00:00 2001 From: Leo Q Date: Thu, 12 Jun 2025 18:44:11 +0800 Subject: [PATCH 3/5] Update version.go --- src/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.go b/src/version.go index 083866eb..772998af 100644 --- a/src/version.go +++ b/src/version.go @@ -1,6 +1,6 @@ package golibmc -const _Version = "1.4.8" +const _Version = "1.5.0" const _Author = "mckelvin" const _Email = "mckelvin@users.noreply.github.com" const _Date = "Fri Jun 7 06:16:00 2024 +0800" From c9d667ee30db642bd75fa64aa472645164d4b654 Mon Sep 17 00:00:00 2001 From: Leo Q Date: Thu, 12 Jun 2025 18:44:53 +0800 Subject: [PATCH 4/5] Update __init__.py --- libmc/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libmc/__init__.py b/libmc/__init__.py index 7052be49..15d068a0 100644 --- a/libmc/__init__.py +++ b/libmc/__init__.py @@ -33,8 +33,8 @@ __file__ as _libmc_so_file ) -__VERSION__ = "1.4.8" -__version__ = "1.4.8" +__VERSION__ = "1.5.0" +__version__ = "1.5.0" __author__ = "mckelvin" __email__ = "mckelvin@users.noreply.github.com" __date__ = "Fri Jun 7 06:16:00 2024 +0800" From 9c76584103939089e91f66bf6814f0fc8cc49d6e Mon Sep 17 00:00:00 2001 From: qizhicheng Date: Fri, 13 Jun 2025 15:01:04 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E6=B3=A2=20styl?= =?UTF-8?q?e=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/BufferReader.h | 6 +++--- include/llvm/SmallVector.h | 10 +++++----- misc/.cppcheck-supp | 34 ++++------------------------------ misc/travis/cppcheck.sh | 2 +- src/BufferReader.cpp | 10 +++++++--- src/Connection.cpp | 7 +++---- src/DataBlock.cpp | 4 ++-- src/HashkitMd5.cpp | 6 +++--- src/Utility.cpp | 3 +-- 9 files changed, 29 insertions(+), 53 deletions(-) diff --git a/include/BufferReader.h b/include/BufferReader.h index 6b19d3bf..95fb0de7 100644 --- a/include/BufferReader.h +++ b/include/BufferReader.h @@ -64,7 +64,7 @@ class BufferReader { char* getWritePtr(); void commitWrite(size_t len); - void write(char* ptr, size_t len, bool copying = true); + void write(const char* ptr, size_t len, bool copying = true); size_t capacity(); size_t size(); @@ -84,7 +84,7 @@ class BufferReader { size_t getNextPreferedDataBlockSize(); protected: - const char charAtCursor(DataCursor& cur) const; + const char charAtCursor(const DataCursor& cur) const; DataBlockList m_dataBlockList; size_t m_capacity; @@ -96,7 +96,7 @@ class BufferReader { }; -inline const char BufferReader::charAtCursor(DataCursor& cur) const { +inline const char BufferReader::charAtCursor(const DataCursor& cur) const { // NOTE: make sure cur is valid /** * DataBlock& db = *cur.iterator; diff --git a/include/llvm/SmallVector.h b/include/llvm/SmallVector.h index 50ca3c90..bb3db4dc 100755 --- a/include/llvm/SmallVector.h +++ b/include/llvm/SmallVector.h @@ -53,7 +53,7 @@ namespace llvm { protected: SmallVectorBase(size_t Size) - : BeginX(&FirstEl), EndX(&FirstEl), CapacityX((char*)&FirstEl+Size) {} + : BeginX(&FirstEl), EndX(&FirstEl), CapacityX(static_cast(&FirstEl)+Size) {} /// isSmall - Return true if this is a smallvector which has not had dynamic /// memory allocated for it. @@ -63,12 +63,12 @@ namespace llvm { /// size_in_bytes - This returns size()*sizeof(T). size_t size_in_bytes() const { - return size_t((char*)EndX - (char*)BeginX); + return size_t(static_cast(EndX) - static_cast(BeginX)); } /// capacity_in_bytes - This returns capacity()*sizeof(T). size_t capacity_in_bytes() const { - return size_t((char*)CapacityX - (char*)BeginX); + return size_t(static_cast(CapacityX) - static_cast(BeginX)); } /// grow_pod - This is an implementation of the grow() method which only works @@ -830,9 +830,9 @@ namespace llvm { if (!this->isSmall()) operator delete(this->BeginX); - this->EndX = (char*)NewElts+CurSizeBytes; + this->EndX = static_cast(NewElts)+CurSizeBytes; this->BeginX = NewElts; - this->CapacityX = (char*)this->BeginX + NewCapacityInBytes; + this->CapacityX = static_cast(this->BeginX) + NewCapacityInBytes; } } // namespace llvm diff --git a/misc/.cppcheck-supp b/misc/.cppcheck-supp index 36e0a410..66ea7609 100644 --- a/misc/.cppcheck-supp +++ b/misc/.cppcheck-supp @@ -16,35 +16,9 @@ *:include/llvm/SmallVector.h:796 constParameter:include/BufferReader.h:99 unreadVariable:include/LockPool.h -unusedFunction:src/Client.cpp:239 -unusedFunction:src/c_client.cpp:8 -unusedFunction:src/c_client.cpp:13 -unusedFunction:src/c_client.cpp:25 -unusedFunction:src/c_client.cpp:31 -unusedFunction:src/c_client.cpp:37 -unusedFunction:src/c_client.cpp:43 -unusedFunction:src/c_client.cpp:50 -unusedFunction:src/c_client.cpp:56 -unusedFunction:src/c_client.cpp:67 -unusedFunction:src/c_client.cpp:68 -unusedFunction:src/c_client.cpp:72 -unusedFunction:src/c_client.cpp:89 -unusedFunction:src/c_client.cpp:90 -unusedFunction:src/c_client.cpp:91 -unusedFunction:src/c_client.cpp:92 -unusedFunction:src/c_client.cpp:93 -unusedFunction:src/c_client.cpp:94 -unusedFunction:src/c_client.cpp:98 -unusedFunction:src/c_client.cpp:106 -unusedFunction:src/c_client.cpp:112 -unusedFunction:src/c_client.cpp:120 -unusedFunction:src/c_client.cpp:128 -unusedFunction:src/c_client.cpp:135 -unusedFunction:src/c_client.cpp:140 -unusedFunction:src/c_client.cpp:145 -unusedFunction:src/c_client.cpp:149 -unusedFunction:src/c_client.cpp:154 -unusedFunction:src/c_client.cpp:159 -unusedFunction:src/Utility.cpp:43 +unreadVariable:src/HashkitKetama.cpp:49 +unusedFunction:* *:src/Connection.cpp:32 *:src/Connection.cpp:37 +missingIncludeSystem:* +normalCheckLevelMaxBranches:* diff --git a/misc/travis/cppcheck.sh b/misc/travis/cppcheck.sh index 90d35802..0fb957a5 100755 --- a/misc/travis/cppcheck.sh +++ b/misc/travis/cppcheck.sh @@ -6,4 +6,4 @@ set -ex # https://github.com/danmar/cppcheck/pull/1026 cppcheck --enable=all --error-exitcode=1 \ -UMSG_MORE -UNI_MAXHOST -UNI_MAXSERV -UUIO_MAXIOV \ - --suppressions-list=misc/.cppcheck-supp -Iinclude src tests + --suppressions-list=misc/.cppcheck-supp -Iinclude src diff --git a/src/BufferReader.cpp b/src/BufferReader.cpp index 7f0bdefb..4d7b45f0 100644 --- a/src/BufferReader.cpp +++ b/src/BufferReader.cpp @@ -74,8 +74,10 @@ size_t BufferReader::prepareWriteBlock(size_t len) { ++m_blockWriteIterator; } - assert(m_blockWriteIterator == m_dataBlockList.end() || - m_blockWriteIterator->getWriteLeft() > 0); + // Store the result to avoid side effects in assert + size_t writeLeft = (m_blockWriteIterator == m_dataBlockList.end()) ? + 0 : m_blockWriteIterator->getWriteLeft(); + assert(m_blockWriteIterator == m_dataBlockList.end() || writeLeft > 0); DataBlock *dbPtr = NULL; @@ -111,7 +113,9 @@ char* BufferReader::getWritePtr() { void BufferReader::commitWrite(size_t len) { - assert(m_blockWriteIterator->size() + len <= m_blockWriteIterator->capacity()); + // Store the result to avoid side effects in assert + size_t currentCapacity = m_blockWriteIterator->capacity(); + assert(m_blockWriteIterator->size() + len <= currentCapacity); m_blockWriteIterator->occupy(len); if (m_blockWriteIterator->size() + len == m_blockWriteIterator->capacity()) { ++m_blockWriteIterator; diff --git a/src/Connection.cpp b/src/Connection.cpp index b44c4963..ad3649e3 100644 --- a/src/Connection.cpp +++ b/src/Connection.cpp @@ -123,7 +123,6 @@ int Connection::connect() { try_next_ai: ::close(fd); - continue; } if (server_addrinfo) { @@ -147,7 +146,7 @@ int Connection::unixSocketConnect() { return -1; } - setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&opt_keepalive, sizeof opt_keepalive); + setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, static_cast(&opt_keepalive), sizeof opt_keepalive); struct sockaddr_un addr; memset(&addr, 0, sizeof(addr)); @@ -156,7 +155,7 @@ int Connection::unixSocketConnect() { // storing the unix path as a host doesn't limit the input but can overflow strncpy(addr.sun_path, m_host, sizeof(addr.sun_path) - 1); assert(strcmp(addr.sun_path, m_host) == 0); - if (connectPoll(fd, (const struct sockaddr *)&addr, sizeof addr) != 0) { + if (connectPoll(fd, reinterpret_cast(&addr), sizeof addr) != 0) { return -1; } m_socketFd = fd; @@ -240,7 +239,7 @@ void Connection::markDead(const char* reason, int delay) { if (strcmp(reason, keywords::kCONN_QUIT) != 0) { log_warn("Connection %s is dead(reason: %s, delay: %d), next check at %lu", m_name, reason, delay, m_deadUntil); - struct iovec* key = m_parser.currentRequestKey(); + const struct iovec* key = m_parser.currentRequestKey(); if (key != NULL) { log_warn("%s: first request key: %.*s", m_name, static_cast(key->iov_len), diff --git a/src/DataBlock.cpp b/src/DataBlock.cpp index 4076372b..126ba597 100644 --- a/src/DataBlock.cpp +++ b/src/DataBlock.cpp @@ -91,7 +91,7 @@ size_t DataBlock::getWriteLeft() { size_t DataBlock::find(char c, size_t since) { - char *p = std::find(m_data + since, m_data + m_size, c); + const char *p = std::find(m_data + since, m_data + m_size, c); return p - m_data; } @@ -102,7 +102,7 @@ bool is_not_digit(int c) { size_t DataBlock::findNotNumeric(size_t since) { - char *p = std::find_if(m_data + since, m_data + m_size, is_not_digit); + const char *p = std::find_if(m_data + since, m_data + m_size, is_not_digit); return p - m_data; } diff --git a/src/HashkitMd5.cpp b/src/HashkitMd5.cpp index a5591487..a1495ad9 100644 --- a/src/HashkitMd5.cpp +++ b/src/HashkitMd5.cpp @@ -218,7 +218,7 @@ void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen ) if( left && ilen >= fill ) { - memcpy( (void *) (ctx->buffer + left), input, fill ); + memcpy( ctx->buffer + left, input, fill ); md5_process( ctx, ctx->buffer ); input += fill; ilen -= fill; @@ -234,7 +234,7 @@ void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen ) if( ilen > 0 ) { - memcpy( (void *) (ctx->buffer + left), input, ilen ); + memcpy( ctx->buffer + left, input, ilen ); } } @@ -293,7 +293,7 @@ uint32_t hash_md5(const char *key, size_t key_length) { unsigned char results[16]; - md5((unsigned char*)key, (size_t)key_length, results); + md5(reinterpret_cast(key), key_length, results); return ((uint32_t) (results[3] & 0xFF) << 24) | ((uint32_t) (results[2] & 0xFF) << 16) diff --git a/src/Utility.cpp b/src/Utility.cpp index a1734118..d8d5ff79 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -44,7 +44,6 @@ void fprintBuffer(std::FILE* file, const char *data_buffer_, const unsigned int const unsigned char* data_buffer = reinterpret_cast(data_buffer_); unsigned int i, j; for (i = 0; i < length; i++) { - unsigned char byte = data_buffer[i]; fprintf(file, "%02x ", data_buffer[i]); if (((i%16) == 15) || (i == length-1)) { for (j = 0; j < 15-(i%16); j++) { @@ -52,7 +51,7 @@ void fprintBuffer(std::FILE* file, const char *data_buffer_, const unsigned int } fprintf(file, "| "); for (j=(i-(i%16)); j <= i; j++) { - byte = data_buffer[j]; + unsigned char byte = data_buffer[j]; if ((byte > 31) && (byte < 127)) { fprintf(file, "%c", byte); } else {