Skip to content

Commit

Permalink
Fix PyObject::__toString() for python bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Feb 18, 2024
1 parent 448835b commit 22a5392
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
22 changes: 22 additions & 0 deletions examples/socket/client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
$socket = PyCore::import('socket');

const HOST = "127.0.0.1";
const PORT = 5432;

$client = $socket->socket($socket->AF_INET, $socket->SOCK_STREAM);
$client->connect(PyCore::tuple([HOST, PORT]));

while (1) {
echo "> ";
$msg = trim(fgets(STDIN));
if ($msg == 'quit') {
break;
}
$client->sendall(PyCore::bytes($msg));
$data = $client->recv(1024);
if (empty($data)) {
break;
}
echo strval($data) . PHP_EOL;
}
16 changes: 16 additions & 0 deletions examples/socket/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import socket

HOST = "127.0.0.1"
PORT = 5432

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
1 change: 1 addition & 0 deletions include/phpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ PyObject *new_reference(zval *zv);
PyObject *new_callable(zval *zv);
const char *string2utf8(PyObject *pv, ssize_t *len);
const char *string2char_ptr(PyObject *pv, ssize_t *len);
void string2zval(PyObject *pv, zval *zv);
void tuple2argv(zval *argv, PyObject *args, ssize_t size, int begin = 1);
void release_argv(uint32_t argc, zval *argv);
} // namespace python
Expand Down
19 changes: 19 additions & 0 deletions src/bridge/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ namespace python {
const char *string2utf8(PyObject *pv, ssize_t *len) {
return PyUnicode_AsUTF8AndSize(pv, len);
};

const char *string2char_ptr(PyObject *pv, ssize_t *len) {
const char *c_str;
if (ZendString_Check(pv)) {
Expand All @@ -500,6 +501,24 @@ const char *string2char_ptr(PyObject *pv, ssize_t *len) {
}
return c_str;
}

void string2zval(PyObject *pv, zval *zv) {
Py_ssize_t len;
auto sval = string2char_ptr(pv, &len);
if (sval != NULL) {
ZVAL_STRINGL(zv, sval, len);
return;
}
auto value = PyObject_Str(pv);
if (value != NULL) {
const char *sv = PyUnicode_AsUTF8AndSize(value, &len);
ZVAL_STRINGL(zv, sv, len);
Py_DECREF(value);
} else {
phpy::php::throw_error_if_occurred();
}
}

void tuple2argv(zval *argv, PyObject *args, ssize_t size, int begin) {
Py_ssize_t i;
for (i = begin; i < size; i++) {
Expand Down
11 changes: 1 addition & 10 deletions src/php/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,7 @@ ZEND_METHOD(PyObject, __set) {
}

ZEND_METHOD(PyObject, __toString) {
auto object = phpy_object_get_handle(ZEND_THIS);
auto value = PyObject_Str(object);
if (value != NULL) {
Py_ssize_t sl;
const char *sv = PyUnicode_AsUTF8AndSize(value, &sl);
ZVAL_STRINGL(return_value, sv, sl);
Py_DECREF(value);
} else {
phpy::php::throw_error_if_occurred();
}
phpy::python::string2zval(phpy_object_get_handle(ZEND_THIS), return_value);
}

ZEND_METHOD(PyObject, __invoke) {
Expand Down
7 changes: 7 additions & 0 deletions tests/phpunit/ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@ public function testIter()
$data = iterator_to_array($fset);
$this->assertEquals($data, $arr);
}

public function testBytes2Str()
{
$bytes = random_bytes(128);
$data = PyCore::bytes($bytes);
$this->assertEquals($bytes, strval($data));
}
}

0 comments on commit 22a5392

Please sign in to comment.