Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix python config #45

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:
sudo sh -c "echo 'extension=phpy.so' > /etc/php/${PHP_VERSION}/cli/conf.d/90-phpy.ini"
php --ri phpy
- name: Run phpunit tests
env:
LC_ALL: POSIX # Do not remove this line, PR: https://github.com/swoole/phpy/pull/45
run: |
composer install
composer test
12 changes: 12 additions & 0 deletions src/php/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,19 @@ PHP_MINIT_FUNCTION(phpy) {
return FAILURE;
}
srand(time(NULL));

#if PY_VERSION_HEX >= 0x03080000
// doc: https://docs.python.org/3/c-api/init_config.html
PyConfig py_config;
PyConfig_InitPythonConfig(&py_config);
py_config.install_signal_handlers = 0; // ignore signal
py_config.parse_argv = 0;
Py_InitializeFromConfig(&py_config);
PyConfig_Clear(&py_config);
#else
Py_InitializeEx(0);
#endif

module_phpy = PyImport_ImportModule("phpy");
if (!module_phpy) {
PyErr_Print();
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/LocaleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace phpunit;

use PHPUnit\Framework\TestCase;
use PyCore;

class LocaleTest extends TestCase
{
public function testLocale()
{
$fileName = __DIR__ . '/test_locale.py';
file_put_contents($fileName, <<<PYCODE
import locale;
import sys;

print(locale.getdefaultlocale())
print(locale.getpreferredencoding())
print(sys.getdefaultencoding())
print(sys.getfilesystemencoding())
PYCODE);

$result = shell_exec('python ' . $fileName);
unlink($fileName);

$locale = PyCore::import('locale');
$sys = PyCore::import('sys');
$this->assertEquals(sprintf("%s\n%s\n%s\n%s\n", $locale->getdefaultlocale(), $locale->getpreferredencoding(), $sys->getdefaultencoding(), $sys->getfilesystemencoding()), $result);
}
}