Skip to content

Commit

Permalink
修正只有kwargs没有args时报错 (#28)
Browse files Browse the repository at this point in the history
* 修正只有kwargs没有args时报错

* 添加单元测试

* fix

* update

* Update core.cc

---------

Co-authored-by: 弄月 <{ID}+{username}@users.noreply.github.com>
Co-authored-by: Tianfeng.Han <[email protected]>
  • Loading branch information
3 people authored Dec 26, 2023
1 parent c8bddf7 commit 23d143f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bridge/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ void CallObject::call() {
if (argc == 0 && kwargs == nullptr) {
value = PyObject_CallNoArgs(fn);
} else {
args = args == nullptr ? PyTuple_New(0) : args;
value = PyObject_Call(fn, args, kwargs);
}
if (value != NULL) {
Expand Down
28 changes: 28 additions & 0 deletions tests/phpunit/CollectionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use PHPUnit\Framework\TestCase;

class CollectionsTest extends TestCase
{
public function testNamedTuple()
{
$namedtuple = PyCore::import('collections')->namedtuple;
$Person = $namedtuple('Person', ['name', 'age', 'gender']);
$p1 = $Person(name: '阮奇桢', age: 40, gender: '');
$this->assertEquals('阮奇桢', $p1->name);
$this->assertEquals(40, $p1->age);
$this->assertEquals('', $p1->gender);

[$name, $age, $gender] = $p1;
$this->assertEquals(['阮奇桢', 40, ''], [(string)$name, (int)$age, (string)$gender]);
}

public function testCounter()
{
$Counter = PyCore::import('collections')->Counter;

$words = ['red', 'blue', 'red', 'green', 'blue', 'blue'];
$word_counts = $Counter($words);
$this->assertEquals(['blue' => 3, 'red' => 2, 'green' => 1], [...PyCore::dict($word_counts)]);
}
}

0 comments on commit 23d143f

Please sign in to comment.