Skip to content

Commit

Permalink
Fix python config (#45)
Browse files Browse the repository at this point in the history
* Fix configuration initialization for Python>=3.8

* Fix
  • Loading branch information
Yurunsoft authored Feb 20, 2024
1 parent bee6a5a commit 1df79c9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
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);
}
}

0 comments on commit 1df79c9

Please sign in to comment.